0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-16 05:00:24 +00:00

Fix a case where request body wouldn't get cleaned up on keepalive

The body was still in the buffer that's being used for the next
request and was thus used as a request, causing errors.
This commit is contained in:
Loïc Hoguin 2012-01-23 21:57:54 +01:00
parent dd08a90568
commit 4b93c2d19a

View file

@ -356,11 +356,11 @@ terminate_request(HandlerState, Req, State) ->
next_request(Req, State, HandlerRes). next_request(Req, State, HandlerRes).
-spec next_request(#http_req{}, #state{}, any()) -> ok | none(). -spec next_request(#http_req{}, #state{}, any()) -> ok | none().
next_request(Req=#http_req{connection=Conn, buffer=Buffer}, next_request(Req=#http_req{connection=Conn},
State=#state{req_keepalive=Keepalive, max_keepalive=MaxKeepalive}, State=#state{req_keepalive=Keepalive, max_keepalive=MaxKeepalive},
HandlerRes) -> HandlerRes) ->
RespRes = ensure_response(Req), RespRes = ensure_response(Req),
BodyRes = ensure_body_processed(Req), {BodyRes, Buffer} = ensure_body_processed(Req),
%% Flush the resp_sent message before moving on. %% Flush the resp_sent message before moving on.
receive {cowboy_http_req, resp_sent} -> ok after 0 -> ok end, receive {cowboy_http_req, resp_sent} -> ok after 0 -> ok end,
case {HandlerRes, BodyRes, RespRes, Conn} of case {HandlerRes, BodyRes, RespRes, Conn} of
@ -372,14 +372,14 @@ next_request(Req=#http_req{connection=Conn, buffer=Buffer},
terminate(State) terminate(State)
end. end.
-spec ensure_body_processed(#http_req{}) -> ok | close. -spec ensure_body_processed(#http_req{}) -> {ok | close, binary()}.
ensure_body_processed(#http_req{body_state=done}) -> ensure_body_processed(#http_req{body_state=done, buffer=Buffer}) ->
ok; {ok, Buffer};
ensure_body_processed(Req=#http_req{body_state=waiting}) -> ensure_body_processed(Req=#http_req{body_state=waiting}) ->
case cowboy_http_req:body(Req) of case cowboy_http_req:body(Req) of
{error, badarg} -> ok; %% No body. {error, badarg} -> {ok, Req#http_req.buffer}; %% No body.
{error, _Reason} -> close; {error, _Reason} -> {close, <<>>};
_Any -> ok {ok, _, Req2} -> {ok, Req2#http_req.buffer}
end; end;
ensure_body_processed(Req=#http_req{body_state={multipart, _, _}}) -> ensure_body_processed(Req=#http_req{body_state={multipart, _, _}}) ->
{ok, Req2} = cowboy_http_req:multipart_skip(Req), {ok, Req2} = cowboy_http_req:multipart_skip(Req),