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_http.erl | 56 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 28 insertions(+), 28 deletions(-) (limited to 'src/gun_http.erl') 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) -> -- cgit v1.2.3