From b85a3894f3b8f86e255668a3ca3b1722c5d9d94e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20S=C3=B6derqvist?= Date: Mon, 7 Mar 2022 21:07:35 +0100 Subject: Return commands instead of state in remaining callbacks --- src/gun_tunnel.erl | 53 +++++++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 24 deletions(-) (limited to 'src/gun_tunnel.erl') diff --git a/src/gun_tunnel.erl b/src/gun_tunnel.erl index 43e8767..40addf6 100644 --- a/src/gun_tunnel.erl +++ b/src/gun_tunnel.erl @@ -273,32 +273,32 @@ close(_Reason, _State, _EvHandler, EvHandlerState) -> %% @todo Closing must be propagated to tunnels. EvHandlerState. -keepalive(State, _EvHandler, EvHandlerState) -> +keepalive(_State, _EvHandler, EvHandlerState) -> %% @todo Need to figure out how to handle keepalive for tunnels. - {State, EvHandlerState}. + {[], EvHandlerState}. %% We pass the headers forward and optionally dereference StreamRef. -headers(State=#tunnel_state{protocol=Proto, protocol_state=ProtoState0}, +headers(State0=#tunnel_state{protocol=Proto, protocol_state=ProtoState0}, StreamRef0, ReplyTo, Method, Host, Port, Path, Headers, InitialFlow, CookieStore0, EvHandler, EvHandlerState0) -> - StreamRef = maybe_dereference(State, StreamRef0), - {ProtoState, CookieStore, EvHandlerState} = Proto:headers(ProtoState0, StreamRef, + StreamRef = maybe_dereference(State0, StreamRef0), + {Commands, CookieStore, EvHandlerState1} = Proto:headers(ProtoState0, StreamRef, ReplyTo, Method, Host, Port, Path, Headers, InitialFlow, CookieStore0, EvHandler, EvHandlerState0), - {State#tunnel_state{protocol_state=ProtoState}, - CookieStore, EvHandlerState}. + {State, EvHandlerState} = commands(Commands, State0, EvHandler, EvHandlerState1), + {{state, State}, CookieStore, EvHandlerState}. %% We pass the request forward and optionally dereference StreamRef. -request(State=#tunnel_state{protocol=Proto, protocol_state=ProtoState0, +request(State0=#tunnel_state{protocol=Proto, protocol_state=ProtoState0, info=#{origin_host := OriginHost, origin_port := OriginPort}}, StreamRef0, ReplyTo, Method, _Host, _Port, Path, Headers, Body, InitialFlow, CookieStore0, EvHandler, EvHandlerState0) -> - StreamRef = maybe_dereference(State, StreamRef0), - {ProtoState, CookieStore, EvHandlerState} = Proto:request(ProtoState0, StreamRef, + StreamRef = maybe_dereference(State0, StreamRef0), + {Commands, CookieStore, EvHandlerState1} = Proto:request(ProtoState0, StreamRef, ReplyTo, Method, OriginHost, OriginPort, Path, Headers, Body, InitialFlow, CookieStore0, EvHandler, EvHandlerState0), - {State#tunnel_state{protocol_state=ProtoState}, - CookieStore, EvHandlerState}. + {State, EvHandlerState} = commands(Commands, State0, EvHandler, EvHandlerState1), + {{state, State}, CookieStore, EvHandlerState}. %% When the next tunnel is SOCKS we pass the data forward directly. %% This is needed because SOCKS has no StreamRef and the data cannot @@ -306,9 +306,10 @@ request(State=#tunnel_state{protocol=Proto, protocol_state=ProtoState0, data(State=#tunnel_state{protocol=Proto, protocol_state=ProtoState0, protocol_origin={origin, _, _, _, socks5}}, StreamRef, ReplyTo, IsFin, Data, EvHandler, EvHandlerState0) -> - {ProtoState, EvHandlerState} = Proto:data(ProtoState0, StreamRef, + {Commands, EvHandlerState1} = Proto:data(ProtoState0, StreamRef, ReplyTo, IsFin, Data, EvHandler, EvHandlerState0), - {State#tunnel_state{protocol_state=ProtoState}, EvHandlerState}; + {State1, EvHandlerState} = commands(Commands, State, EvHandler, EvHandlerState1), + {{state, State1}, EvHandlerState}; %% CONNECT tunnels pass the data forward and dereference StreamRef %% unless they are the recipient of the callback, in which case the %% data is sent to the socket. @@ -319,12 +320,14 @@ data(State=#tunnel_state{socket=Socket, transport=Transport, case StreamRef0 of TunnelStreamRef -> ok = Transport:send(Socket, Data), - {State, EvHandlerState0}; + {[], EvHandlerState0}; _ -> StreamRef = maybe_dereference(State, StreamRef0), - {ProtoState, EvHandlerState} = Proto:data(ProtoState0, StreamRef, + {Commands, EvHandlerState1} = Proto:data(ProtoState0, StreamRef, ReplyTo, IsFin, Data, EvHandler, EvHandlerState0), - {State#tunnel_state{protocol_state=ProtoState}, EvHandlerState} + {State1, EvHandlerState} = commands(Commands, State, + EvHandler, EvHandlerState1), + {{state, State1}, EvHandlerState} end. %% We pass the CONNECT request forward and optionally dereference StreamRef. @@ -333,17 +336,19 @@ connect(State=#tunnel_state{info=#{origin_host := Host, origin_port := Port}, StreamRef0, ReplyTo, Destination, _, Headers, InitialFlow, EvHandler, EvHandlerState0) -> StreamRef = maybe_dereference(State, StreamRef0), - {ProtoState, EvHandlerState} = Proto:connect(ProtoState0, StreamRef, + {Commands, EvHandlerState1} = Proto:connect(ProtoState0, StreamRef, ReplyTo, Destination, #{host => Host, port => Port}, Headers, InitialFlow, EvHandler, EvHandlerState0), - {State#tunnel_state{protocol_state=ProtoState}, EvHandlerState}. + {State1, EvHandlerState} = commands(Commands, State, EvHandler, EvHandlerState1), + {{state, State1}, EvHandlerState}. cancel(State=#tunnel_state{protocol=Proto, protocol_state=ProtoState0}, StreamRef0, ReplyTo, EvHandler, EvHandlerState0) -> StreamRef = maybe_dereference(State, StreamRef0), - {ProtoState, EvHandlerState} = Proto:cancel(ProtoState0, StreamRef, + {Commands, EvHandlerState1} = Proto:cancel(ProtoState0, StreamRef, ReplyTo, EvHandler, EvHandlerState0), - {State#tunnel_state{protocol_state=ProtoState}, EvHandlerState}. + {State1, EvHandlerState} = commands(Commands, State, EvHandler, EvHandlerState1), + {{state, State1}, EvHandlerState}. timeout(State=#tunnel_state{protocol=Proto, protocol_state=ProtoState0}, Msg, TRef) -> case Proto:timeout(ProtoState0, Msg, TRef) of @@ -426,11 +431,11 @@ ws_upgrade(State=#tunnel_state{info=TunnelInfo, protocol=Proto, protocol_state=P origin_host := Host, origin_port := Port } = TunnelInfo, - {ProtoState, CookieStore, EvHandlerState} = Proto:ws_upgrade(ProtoState0, StreamRef, ReplyTo, + {Commands, CookieStore, EvHandlerState1} = Proto:ws_upgrade(ProtoState0, StreamRef, ReplyTo, Host, Port, Path, Headers, WsOpts, CookieStore0, EvHandler, EvHandlerState0), - {State#tunnel_state{protocol_state=ProtoState}, - CookieStore, EvHandlerState}. + {State1, EvHandlerState} = commands(Commands, State, EvHandler, EvHandlerState1), + {{state, State1}, CookieStore, EvHandlerState}. ws_send(Frames, State0=#tunnel_state{protocol=Proto, protocol_state=ProtoState}, StreamRef0, ReplyTo, EvHandler, EvHandlerState0) -> -- cgit v1.2.3