aboutsummaryrefslogtreecommitdiffstats
path: root/src/gun_http.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2019-09-24 13:21:26 +0200
committerLoïc Hoguin <[email protected]>2019-09-24 13:21:26 +0200
commita3c2edbb8c807717e2f10520c6cf1e77a62eab2e (patch)
tree452a3901dfd39f2e1ca02e040d1383513cac48a9 /src/gun_http.erl
parent2709f328b9976c937d417f9d03b6d8b90ca2d1c5 (diff)
downloadgun-a3c2edbb8c807717e2f10520c6cf1e77a62eab2e.tar.gz
gun-a3c2edbb8c807717e2f10520c6cf1e77a62eab2e.tar.bz2
gun-a3c2edbb8c807717e2f10520c6cf1e77a62eab2e.zip
Initial support for raw send/recv operations
Gun can now be used to send or receive arbitrary data in the following scenarios: * Directly after connecting to a server (this is not terribly useful but it works nevertheless due to the Gun architecture) * After connecting through one or more Socks and/or HTTP proxies. This allows using Gun's proxy capabilities to access servers located beyond firewalls. * After performing an HTTP/1.1 Upgrade. This allows using Gun to implement custom protocols that require upgrading from an HTTP/1.1 connection. As there is still no support for HTTP/2 CONNECT for the time being, there are no relevant streams attached to those use cases and therefore the raw protocol currently expects users to use 'undefined' as the StreamRef value. This is not a final decision and will most likely produce a Dialyzer warning at this time.
Diffstat (limited to 'src/gun_http.erl')
-rw-r--r--src/gun_http.erl19
1 files changed, 15 insertions, 4 deletions
diff --git a/src/gun_http.erl b/src/gun_http.erl
index 59f4fe7..87b50c8 100644
--- a/src/gun_http.erl
+++ b/src/gun_http.erl
@@ -265,15 +265,26 @@ handle_head(Data, State=#http_state{version=ClientVersion, opts=Opts,
{Version, Status, _, Rest} = cow_http:parse_status_line(Data),
{Headers, Rest2} = cow_http:parse_headers(Rest),
case {Status, StreamRef} of
- {101, {websocket, RealStreamRef, WsKey, WsExtensions, WsOpts}} ->
+ {101, _} ->
EvHandlerState = EvHandler:response_inform(#{
- stream_ref => RealStreamRef,
+ stream_ref => stream_ref(StreamRef),
reply_to => ReplyTo,
status => 101,
headers => Headers
}, EvHandlerState0),
- {ws_handshake(Rest2, State, RealStreamRef, Headers, WsKey, WsExtensions, WsOpts),
- EvHandlerState};
+ %% @todo We might want to switch to the HTTP/2 protocol or to the TLS transport as well.
+ case StreamRef of
+ {websocket, RealStreamRef, WsKey, WsExtensions, WsOpts} ->
+ {ws_handshake(Rest2, State, RealStreamRef, Headers, WsKey, WsExtensions, WsOpts),
+ EvHandlerState};
+ %% Any other 101 response results in us switching to the raw protocol.
+ %% @todo We should check that we asked for an upgrade before accepting it.
+ _ ->
+ {_, Upgrade0} = lists:keyfind(<<"upgrade">>, 1, Headers),
+ Upgrade = cow_http_hd:parse_upgrade(Upgrade0),
+ ReplyTo ! {gun_upgrade, self(), StreamRef, Upgrade, Headers},
+ {{switch_protocol, raw}, EvHandlerState0}
+ end;
%% @todo If the stream is cancelled we probably shouldn't finish the CONNECT setup.
{_, {connect, RealStreamRef, Destination}} when Status >= 200, Status < 300 ->
case IsAlive of