diff options
author | Loïc Hoguin <[email protected]> | 2019-09-20 16:22:53 +0200 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2019-09-22 16:46:44 +0200 |
commit | 07e79aa3a70546fe4419b94cbf41c38a6eb039a1 (patch) | |
tree | 017d06819a79254c19dfafee8a2d0410be02b466 /src | |
parent | 53f0888993d7bb464c094ada645b075e43560403 (diff) | |
download | gun-07e79aa3a70546fe4419b94cbf41c38a6eb039a1.tar.gz gun-07e79aa3a70546fe4419b94cbf41c38a6eb039a1.tar.bz2 gun-07e79aa3a70546fe4419b94cbf41c38a6eb039a1.zip |
Always switch_protocol
When doing a CONNECT from http to http or from socks to socks
we may want to use different configuration options. Switching
the protocol explicitly helps us achieve that. It will also
signal through events that a protocol switch occurred.
Diffstat (limited to 'src')
-rw-r--r-- | src/gun.erl | 20 | ||||
-rw-r--r-- | src/gun_http.erl | 11 |
2 files changed, 6 insertions, 25 deletions
diff --git a/src/gun.erl b/src/gun.erl index ca6c730..93a8d40 100644 --- a/src/gun.erl +++ b/src/gun.erl @@ -945,16 +945,11 @@ ensure_alpn(Protocols0, TransOpts) -> tls_handshake(internal, {tls_handshake, HandshakeEvent, Protocols}, State0=#state{socket=Socket, transport=gun_tcp, protocol=CurrentProtocol}) -> case normal_tls_handshake(Socket, State0, HandshakeEvent, Protocols) of - {ok, TLSSocket, CurrentProtocol, State1} -> - %% We only need to switch the transport when the protocol remains the same. - %% The transport is given in Proto:init/4 in the other case. - {keep_state, State} = commands([{switch_transport, gun_tls, TLSSocket}], State1), - {next_state, connected, State}; - {ok, TLSSocket, NewProtocol, State1} -> + {ok, TLSSocket, NewProtocol, State} -> commands([ {switch_transport, gun_tls, TLSSocket}, {switch_protocol, NewProtocol} - ], State1); + ], State); {error, Reason, State} -> commands({error, Reason}, State) end; @@ -984,16 +979,7 @@ tls_handshake(info, {gun_tls_proxy, Socket, {ok, Negotiated}, {HandshakeEvent, P socket => Socket, protocol => NewProtocol }, EvHandlerState0), - State = State0#state{event_handler_state=EvHandlerState}, - case NewProtocol of - CurrentProtocol -> - %% We only need to switch the transport when the protocol remains the same. - %% The transport is given in Proto:init/4 in the other case. - ProtoState = CurrentProtocol:switch_transport(Transport, Socket, ProtoState0), - {keep_state, State#state{protocol_state=ProtoState}}; - _ -> - commands([{switch_protocol, NewProtocol}], State) - end; + commands([{switch_protocol, NewProtocol}], State0#state{event_handler_state=EvHandlerState}); tls_handshake(info, {gun_tls_proxy, Socket, Error = {error, Reason}, {HandshakeEvent, _}}, State=#state{socket=Socket, event_handler=EvHandler, event_handler_state=EvHandlerState0}) -> EvHandlerState = EvHandler:tls_handshake_end(HandshakeEvent#{ diff --git a/src/gun_http.erl b/src/gun_http.erl index 85fd2c4..44575bf 100644 --- a/src/gun_http.erl +++ b/src/gun_http.erl @@ -313,14 +313,9 @@ handle_head(Data, State=#http_state{version=ClientVersion, content_handlers=Hand {[{origin, <<"https">>, NewHost, NewPort, connect}, {tls_handshake, HandshakeEvent, Protocols}], EvHandlerState1}; _ -> - case Protocols of - [http] -> - {[{state, State2}, - {origin, <<"http">>, NewHost, NewPort, connect}], EvHandlerState1}; - [http2] -> - {[{origin, <<"http">>, NewHost, NewPort, connect}, - {switch_protocol, http2}], EvHandlerState1} - end + [Protocol] = Protocols, + {[{origin, <<"http">>, NewHost, NewPort, connect}, + {switch_protocol, Protocol}], EvHandlerState1} end; {_, _} when Status >= 100, Status =< 199 -> ReplyTo ! {gun_inform, self(), stream_ref(StreamRef), Status, Headers}, |