aboutsummaryrefslogtreecommitdiffstats
path: root/src/gun_http.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_http.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_http.erl')
-rw-r--r--src/gun_http.erl56
1 files changed, 28 insertions, 28 deletions
diff --git a/src/gun_http.erl b/src/gun_http.erl
index aaa6d15..ec29b71 100644
--- a/src/gun_http.erl
+++ b/src/gun_http.erl
@@ -546,20 +546,20 @@ close_streams(State, [#stream{ref=StreamRef, reply_to=ReplyTo}|Tail], Reason) ->
close_streams(State, Tail, Reason).
%% We don't send a keep-alive when a CONNECT request was initiated.
-keepalive(State=#http_state{streams=[#stream{ref={connect, _, _}}]}, _, EvHandlerState) ->
- {State, EvHandlerState};
+keepalive(#http_state{streams=[#stream{ref={connect, _, _}}]}, _, EvHandlerState) ->
+ {[], EvHandlerState};
%% We can only keep-alive by sending an empty line in-between streams.
-keepalive(State=#http_state{socket=Socket, transport=Transport, out=head}, _, EvHandlerState) ->
+keepalive(#http_state{socket=Socket, transport=Transport, out=head}, _, EvHandlerState) ->
Transport:send(Socket, <<"\r\n">>),
- {State, EvHandlerState};
-keepalive(State, _, EvHandlerState) ->
- {State, EvHandlerState}.
+ {[], EvHandlerState};
+keepalive(_State, _, EvHandlerState) ->
+ {[], EvHandlerState}.
headers(State, StreamRef, ReplyTo, _, _, _, _, _, _, CookieStore, _, EvHandlerState)
when is_list(StreamRef) ->
ReplyTo ! {gun_error, self(), stream_ref(State, StreamRef),
{badstate, "The stream is not a tunnel."}},
- {State, CookieStore, EvHandlerState};
+ {[], CookieStore, EvHandlerState};
headers(State=#http_state{opts=Opts, out=head},
StreamRef, ReplyTo, Method, Host, Port, Path, Headers,
InitialFlow0, CookieStore0, EvHandler, EvHandlerState0) ->
@@ -567,15 +567,15 @@ headers(State=#http_state{opts=Opts, out=head},
StreamRef, ReplyTo, Method, Host, Port, Path, Headers, undefined,
CookieStore0, EvHandler, EvHandlerState0, ?FUNCTION_NAME),
InitialFlow = initial_flow(InitialFlow0, Opts),
- {new_stream(State#http_state{connection=Conn, out=Out}, StreamRef, ReplyTo,
- Method, Authority, Path, InitialFlow),
+ {{state, new_stream(State#http_state{connection=Conn, out=Out}, StreamRef,
+ ReplyTo, Method, Authority, Path, InitialFlow)},
CookieStore, EvHandlerState}.
request(State, StreamRef, ReplyTo, _, _, _, _, _, _, _, CookieStore, _, EvHandlerState)
when is_list(StreamRef) ->
ReplyTo ! {gun_error, self(), stream_ref(State, StreamRef),
{badstate, "The stream is not a tunnel."}},
- {State, CookieStore, EvHandlerState};
+ {[], CookieStore, EvHandlerState};
request(State=#http_state{opts=Opts, out=head}, StreamRef, ReplyTo,
Method, Host, Port, Path, Headers, Body,
InitialFlow0, CookieStore0, EvHandler, EvHandlerState0) ->
@@ -583,8 +583,8 @@ request(State=#http_state{opts=Opts, out=head}, StreamRef, ReplyTo,
StreamRef, ReplyTo, Method, Host, Port, Path, Headers, Body,
CookieStore0, EvHandler, EvHandlerState0, ?FUNCTION_NAME),
InitialFlow = initial_flow(InitialFlow0, Opts),
- {new_stream(State#http_state{connection=Conn, out=Out}, StreamRef, ReplyTo,
- Method, Authority, Path, InitialFlow),
+ {{state, new_stream(State#http_state{connection=Conn, out=Out}, StreamRef,
+ ReplyTo, Method, Authority, Path, InitialFlow)},
CookieStore, EvHandlerState}.
initial_flow(infinity, #{flow := InitialFlow}) -> InitialFlow;
@@ -704,10 +704,10 @@ data(State=#http_state{socket=Socket, transport=Transport, version=Version,
reply_to => ReplyTo
},
EvHandlerState = EvHandler:request_end(RequestEndEvent, EvHandlerState0),
- {State#http_state{out=head}, EvHandlerState};
+ {{state, State#http_state{out=head}}, EvHandlerState};
body_chunked when Version =:= 'HTTP/1.1' ->
Transport:send(Socket, cow_http_te:chunk(Data)),
- {State, EvHandlerState0};
+ {[], EvHandlerState0};
{body, Length} when DataLength =< Length ->
Transport:send(Socket, Data),
Length2 = Length - DataLength,
@@ -718,13 +718,13 @@ data(State=#http_state{socket=Socket, transport=Transport, version=Version,
reply_to => ReplyTo
},
EvHandlerState = EvHandler:request_end(RequestEndEvent, EvHandlerState0),
- {State#http_state{out=head}, EvHandlerState};
+ {{state, State#http_state{out=head}}, EvHandlerState};
Length2 > 0, IsFin =:= nofin ->
- {State#http_state{out={body, Length2}}, EvHandlerState0}
+ {{state, State#http_state{out={body, Length2}}}, EvHandlerState0}
end;
body_chunked -> %% HTTP/1.0
Transport:send(Socket, Data),
- {State, EvHandlerState0}
+ {[], EvHandlerState0}
end;
_ ->
{error_stream_not_found(State, StreamRef, ReplyTo), EvHandlerState0}
@@ -734,12 +734,12 @@ connect(State, StreamRef, ReplyTo, _, _, _, _, _, EvHandlerState)
when is_list(StreamRef) ->
ReplyTo ! {gun_error, self(), stream_ref(State, StreamRef),
{badstate, "The stream is not a tunnel."}},
- {State, EvHandlerState};
+ {[], EvHandlerState};
connect(State=#http_state{streams=Streams}, StreamRef, ReplyTo, _, _, _, _, _, EvHandlerState)
when Streams =/= [] ->
ReplyTo ! {gun_error, self(), stream_ref(State, StreamRef), {badstate,
"CONNECT can only be used with HTTP/1.1 when no other streams are active."}},
- {State, EvHandlerState};
+ {[], EvHandlerState};
connect(State=#http_state{socket=Socket, transport=Transport, opts=Opts, version=Version},
StreamRef, ReplyTo, Destination=#{host := Host0}, _TunnelInfo, Headers0, InitialFlow0,
EvHandler, EvHandlerState0) ->
@@ -786,8 +786,8 @@ connect(State=#http_state{socket=Socket, transport=Transport, opts=Opts, version
},
EvHandlerState = EvHandler:request_end(RequestEndEvent, EvHandlerState2),
InitialFlow = initial_flow(InitialFlow0, Opts),
- {new_stream(State, {connect, StreamRef, Destination}, ReplyTo,
- <<"CONNECT">>, Authority, <<>>, InitialFlow),
+ {{state, new_stream(State, {connect, StreamRef, Destination}, ReplyTo,
+ <<"CONNECT">>, Authority, <<>>, InitialFlow)},
EvHandlerState}.
%% We can't cancel anything, we can just stop forwarding messages to the owner.
@@ -801,7 +801,7 @@ cancel(State0, StreamRef, ReplyTo, EvHandler, EvHandlerState0) ->
endpoint => local,
reason => cancel
}, EvHandlerState0),
- {State, EvHandlerState};
+ {{state, State}, EvHandlerState};
false ->
{error_stream_not_found(State0, StreamRef, ReplyTo), EvHandlerState0}
end.
@@ -831,12 +831,12 @@ down(#http_state{streams=Streams}) ->
error_stream_closed(State, StreamRef, ReplyTo) ->
ReplyTo ! {gun_error, self(), stream_ref(State, StreamRef), {badstate,
"The stream has already been closed."}},
- State.
+ {state, State}.
error_stream_not_found(State, StreamRef, ReplyTo) ->
ReplyTo ! {gun_error, self(), stream_ref(State, StreamRef), {badstate,
"The stream cannot be found."}},
- State.
+ {state, State}.
%% Headers information retrieval.
@@ -927,12 +927,12 @@ ws_upgrade(State, StreamRef, ReplyTo, _, _, _, _, _, CookieStore, _, EvHandlerSt
when is_list(StreamRef) ->
ReplyTo ! {gun_error, self(), stream_ref(State, StreamRef),
{badstate, "The stream is not a tunnel."}},
- {State, CookieStore, EvHandlerState};
+ {[], CookieStore, EvHandlerState};
ws_upgrade(State=#http_state{version='HTTP/1.0'},
StreamRef, ReplyTo, _, _, _, _, _, CookieStore, _, EvHandlerState) ->
ReplyTo ! {gun_error, self(), stream_ref(State, StreamRef), {badstate,
"Websocket cannot be used over an HTTP/1.0 connection."}},
- {State, CookieStore, EvHandlerState};
+ {[], CookieStore, EvHandlerState};
ws_upgrade(State=#http_state{out=head}, StreamRef, ReplyTo,
Host, Port, Path, Headers0, WsOpts, CookieStore0, EvHandler, EvHandlerState0) ->
{Headers1, GunExtensions} = case maps:get(compress, WsOpts, false) of
@@ -960,9 +960,9 @@ ws_upgrade(State=#http_state{out=head}, StreamRef, ReplyTo,
StreamRef, ReplyTo, <<"GET">>, Host, Port, Path, Headers, undefined,
CookieStore0, EvHandler, EvHandlerState0, ?FUNCTION_NAME),
InitialFlow = maps:get(flow, WsOpts, infinity),
- {new_stream(State#http_state{connection=Conn, out=Out},
+ {{state, new_stream(State#http_state{connection=Conn, out=Out},
#websocket{ref=StreamRef, reply_to=ReplyTo, key=Key, extensions=GunExtensions, opts=WsOpts},
- ReplyTo, <<"GET">>, Authority, Path, InitialFlow),
+ ReplyTo, <<"GET">>, Authority, Path, InitialFlow)},
CookieStore, EvHandlerState}.
ws_handshake(Buffer, State, Ws=#websocket{key=Key}, Headers) ->