aboutsummaryrefslogtreecommitdiffstats
path: root/src/cowboy_http.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2020-01-16 17:33:49 +0100
committerLoïc Hoguin <[email protected]>2020-01-17 11:42:28 +0100
commit752297b1539b4f74c9329c873f78485a52b5b8bd (patch)
treef874c4f3fc5bf2c3f5db94a62155a0b2b067f4e4 /src/cowboy_http.erl
parentedea415da87d7f6fe9f4fcd4306ce3e7e38727ed (diff)
downloadcowboy-752297b1539b4f74c9329c873f78485a52b5b8bd.tar.gz
cowboy-752297b1539b4f74c9329c873f78485a52b5b8bd.tar.bz2
cowboy-752297b1539b4f74c9329c873f78485a52b5b8bd.zip
Fix bugs related to HTTP/1.1 pipelining
The flow control is now only set to infinity when we are skipping the request body of the stream that is being terminated. This fixes a bug where it was set to infinity while reading a subsequent request's body, leading to a crash. The timeout is no longer reset on stream termination. Timeout handling is already done when receiving data from the socket and doing a reset on stream termination was leading to the wrong timeout being set or the right timeout being reset needlessly.
Diffstat (limited to 'src/cowboy_http.erl')
-rw-r--r--src/cowboy_http.erl35
1 files changed, 18 insertions, 17 deletions
diff --git a/src/cowboy_http.erl b/src/cowboy_http.erl
index ad2ef75..9f0b865 100644
--- a/src/cowboy_http.erl
+++ b/src/cowboy_http.erl
@@ -1266,6 +1266,7 @@ stream_terminate(State0=#state{opts=Opts, in_streamid=InStreamID, in_state=InSta
children=Children0}, StreamID, Reason) ->
#stream{version=Version, local_expected_size=ExpectedSize, local_sent_size=SentSize}
= lists:keyfind(StreamID, #stream.id, Streams0),
+ %% Send a response or terminate chunks depending on the current output state.
State1 = #state{streams=Streams1} = case OutState of
wait when element(1, Reason) =:= internal_error ->
info(State0, StreamID, {response, 500, #{<<"content-length">> => <<"0">>}, <<>>});
@@ -1280,19 +1281,15 @@ stream_terminate(State0=#state{opts=Opts, in_streamid=InStreamID, in_state=InSta
_ -> %% done or Version =:= 'HTTP/1.0'
State0
end,
- %% Remove the stream from the state and reset the overriden options.
+ %% Stop the stream, shutdown children and reset overriden options.
{value, #stream{state=StreamState}, Streams}
= lists:keytake(StreamID, #stream.id, Streams1),
- State2 = State1#state{streams=Streams, overriden_opts=#{}, flow=infinity},
- %% Stop the stream.
- stream_call_terminate(StreamID, Reason, StreamState, State2),
+ stream_call_terminate(StreamID, Reason, StreamState, State1),
Children = cowboy_children:shutdown(Children0, StreamID),
- %% We reset the timeout if there are no active streams anymore.
- State = set_timeout(State2#state{streams=Streams, children=Children}, request_timeout),
+ State = State1#state{overriden_opts=#{}, streams=Streams, children=Children},
%% We want to drop the connection if the body was not read fully
%% and we don't know its length or more remains to be read than
%% configuration allows.
- %% @todo Only do this if Current =:= StreamID.
MaxSkipBodyLength = maps:get(max_skip_body_length, Opts, 1000000),
case InState of
#ps_body{length=undefined}
@@ -1301,17 +1298,21 @@ stream_terminate(State0=#state{opts=Opts, in_streamid=InStreamID, in_state=InSta
#ps_body{length=Len, received=Received}
when InStreamID =:= OutStreamID, Received + MaxSkipBodyLength < Len ->
terminate(State, skip_body_too_large);
+ #ps_body{} when InStreamID =:= OutStreamID ->
+ stream_next(State#state{flow=infinity});
_ ->
- %% Move on to the next stream.
- NextOutStreamID = OutStreamID + 1,
- case lists:keyfind(NextOutStreamID, #stream.id, Streams) of
- false ->
- State#state{out_streamid=NextOutStreamID, out_state=wait};
- #stream{queue=Commands} ->
- %% @todo Remove queue from the stream.
- commands(State#state{out_streamid=NextOutStreamID, out_state=wait},
- NextOutStreamID, Commands)
- end
+ stream_next(State)
+ end.
+
+stream_next(State=#state{out_streamid=OutStreamID, streams=Streams}) ->
+ NextOutStreamID = OutStreamID + 1,
+ case lists:keyfind(NextOutStreamID, #stream.id, Streams) of
+ false ->
+ State#state{out_streamid=NextOutStreamID, out_state=wait};
+ #stream{queue=Commands} ->
+ %% @todo Remove queue from the stream.
+ commands(State#state{out_streamid=NextOutStreamID, out_state=wait},
+ NextOutStreamID, Commands)
end.
stream_call_terminate(StreamID, Reason, StreamState, #state{opts=Opts}) ->