aboutsummaryrefslogtreecommitdiffstats
path: root/src/gun_tunnel.erl
diff options
context:
space:
mode:
authorViktor Söderqvist <[email protected]>2022-03-07 21:07:35 +0100
committerLoïc Hoguin <[email protected]>2022-03-08 12:02:39 +0100
commitb85a3894f3b8f86e255668a3ca3b1722c5d9d94e (patch)
tree918934a7814a34d9ac7bee9f1740d51e2fbdc02f /src/gun_tunnel.erl
parentf9175998687678e227bdd49669e2d83f0648fa57 (diff)
downloadgun-b85a3894f3b8f86e255668a3ca3b1722c5d9d94e.tar.gz
gun-b85a3894f3b8f86e255668a3ca3b1722c5d9d94e.tar.bz2
gun-b85a3894f3b8f86e255668a3ca3b1722c5d9d94e.zip
Return commands instead of state in remaining callbacks
Diffstat (limited to 'src/gun_tunnel.erl')
-rw-r--r--src/gun_tunnel.erl53
1 files changed, 29 insertions, 24 deletions
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) ->