From 56b7fdd6eb15564ad821885937b03516a03dce4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Fri, 26 Jul 2019 14:25:59 +0200 Subject: Add the transport_changed event Also test protocol_changed over CONNECT. --- src/gun.erl | 11 +++++++---- src/gun_default_event_h.erl | 12 ++++++++---- src/gun_event.erl | 48 +++++++++++++++++++++++++++++---------------- 3 files changed, 46 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/gun.erl b/src/gun.erl index ada6272..d158a53 100644 --- a/src/gun.erl +++ b/src/gun.erl @@ -1076,8 +1076,6 @@ commands([Error={error, _}|_], State) -> disconnect(State, Error); commands([{state, ProtoState}|Tail], State) -> commands(Tail, State#state{protocol_state=ProtoState}); -%% @todo The scheme should probably not be ignored. -%% %% Order is important: the origin must be changed before %% the transport and/or protocol in order to keep track %% of the intermediaries properly. @@ -1094,9 +1092,14 @@ commands([{origin, Scheme, Host, Port, Type}|Tail], }, commands(Tail, State#state{origin_scheme=Scheme, origin_host=Host, origin_port=Port, intermediaries=[Info|Intermediaries]}); -commands([{switch_transport, Transport, Socket}|Tail], State) -> +commands([{switch_transport, Transport, Socket}|Tail], State=#state{ + event_handler=EvHandler, event_handler_state=EvHandlerState0}) -> + EvHandlerState = EvHandler:transport_changed(#{ + socket => Socket, + transport => Transport:name() + }, EvHandlerState0), commands(Tail, active(State#state{socket=Socket, transport=Transport, - messages=Transport:messages()})); + messages=Transport:messages(), event_handler_state=EvHandlerState})); %% @todo The two loops should be reunified and this clause generalized. commands([{switch_protocol, Protocol=gun_ws, ProtoState}], State=#state{ event_handler=EvHandler, event_handler_state=EvHandlerState0}) -> diff --git a/src/gun_default_event_h.erl b/src/gun_default_event_h.erl index 213db06..579fb20 100644 --- a/src/gun_default_event_h.erl +++ b/src/gun_default_event_h.erl @@ -33,12 +33,13 @@ -export([response_trailers/2]). -export([response_end/2]). -export([ws_upgrade/2]). --export([protocol_changed/2]). -export([ws_recv_frame_start/2]). -export([ws_recv_frame_header/2]). -export([ws_recv_frame_end/2]). -export([ws_send_frame_start/2]). -export([ws_send_frame_end/2]). +-export([protocol_changed/2]). +-export([transport_changed/2]). -export([cancel/2]). -export([disconnect/2]). -export([terminate/2]). @@ -97,9 +98,6 @@ response_end(_EventData, State) -> ws_upgrade(_EventData, State) -> State. -protocol_changed(_EventData, State) -> - State. - ws_recv_frame_start(_EventData, State) -> State. @@ -115,6 +113,12 @@ ws_send_frame_start(_EventData, State) -> ws_send_frame_end(_EventData, State) -> State. +protocol_changed(_EventData, State) -> + State. + +transport_changed(_EventData, State) -> + State. + cancel(_EventData, State) -> State. diff --git a/src/gun_event.erl b/src/gun_event.erl index 8a40518..2f12b4c 100644 --- a/src/gun_event.erl +++ b/src/gun_event.erl @@ -64,7 +64,7 @@ -type tls_handshake_event() :: #{ stream_ref => reference(), reply_to => pid(), - socket := inet:socket() | ssl:sslsocket(), %% The socket before/after will be different. + socket := inet:socket() | ssl:sslsocket() | pid(), %% The socket before/after will be different. tls_opts := [ssl:connect_option()], timeout := timeout(), protocol => http | http2, @@ -178,21 +178,6 @@ -callback ws_upgrade(ws_upgrade_event(), State) -> State. -%% protocol_changed. -%% -%% This event can occur either following a successful ws_upgrade -%% event or following a successful CONNECT request. -%% -%% @todo Currently there is only a connection-wide variant of this -%% event. In the future there will be a stream-wide variant to -%% support CONNECT and Websocket over HTTP/2. - --type protocol_changed_event() :: #{ - protocol := http2 | ws -}. - --callback protocol_changed(protocol_changed_event(), State) -> State. - %% ws_recv_frame_start. -type ws_recv_frame_start_event() :: #{ @@ -243,6 +228,36 @@ -callback ws_send_frame_start(ws_send_frame_event(), State) -> State. -callback ws_send_frame_end(ws_send_frame_event(), State) -> State. +%% protocol_changed. +%% +%% This event can occur either following a successful ws_upgrade +%% event or following a successful CONNECT request. +%% +%% @todo Currently there is only a connection-wide variant of this +%% event. In the future there will be a stream-wide variant to +%% support CONNECT and Websocket over HTTP/2. + +-type protocol_changed_event() :: #{ + protocol := http2 | ws +}. + +-callback protocol_changed(protocol_changed_event(), State) -> State. + +%% transport_changed. +%% +%% This event can occur following a successful CONNECT request. +%% +%% @todo Currently there is only a connection-wide variant of this +%% event. In the future there will be a stream-wide variant to +%% support CONNECT through TLS proxies over HTTP/2. + +-type transport_changed_event() :: #{ + socket := ssl:sslsocket() | pid(), + transport := tls | tls_proxy +}. + +-callback transport_changed(transport_changed_event(), State) -> State. + %% cancel. %% %% In the case of HTTP/1.1 we cannot actually cancel the stream, @@ -280,4 +295,3 @@ -callback terminate(terminate_event(), State) -> State. %% @todo origin_changed -%% @todo transport_changed -- cgit v1.2.3