aboutsummaryrefslogtreecommitdiffstats
path: root/src/cowboy_http_protocol.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2011-03-21 17:26:00 +0100
committerLoïc Hoguin <[email protected]>2011-03-21 17:47:17 +0100
commit8b02992e6abd63eab0aafe0d762c38dbbbb757b5 (patch)
tree31e93989da6065739ac3389073043553430bb63d /src/cowboy_http_protocol.erl
parente40001a8843dbb802331a422c78ce65d7c660484 (diff)
downloadcowboy-8b02992e6abd63eab0aafe0d762c38dbbbb757b5.tar.gz
cowboy-8b02992e6abd63eab0aafe0d762c38dbbbb757b5.tar.bz2
cowboy-8b02992e6abd63eab0aafe0d762c38dbbbb757b5.zip
Skip the request body if it hasn't been read by the handler.
Diffstat (limited to 'src/cowboy_http_protocol.erl')
-rw-r--r--src/cowboy_http_protocol.erl28
1 files changed, 23 insertions, 5 deletions
diff --git a/src/cowboy_http_protocol.erl b/src/cowboy_http_protocol.erl
index 2b5292c..b02f471 100644
--- a/src/cowboy_http_protocol.erl
+++ b/src/cowboy_http_protocol.erl
@@ -154,16 +154,34 @@ handler_loop(HandlerState, Req, State=#state{handler={Handler, _Opts}}) ->
-spec handler_terminate(HandlerState::term(), Req::#http_req{},
State::#state{}) -> ok.
handler_terminate(HandlerState, Req, State=#state{handler={Handler, _Opts}}) ->
- Res = (catch Handler:terminate(
+ HandlerRes = (catch Handler:terminate(
Req#http_req{resp_state=locked}, HandlerState)),
- %% @todo We must skip any body data from the request
- %% before processing another.
+ BodyRes = ensure_body_processed(Req),
ensure_response(Req, State),
- case {Res, State#state.connection} of
- {ok, keepalive} -> next_request(State);
+ case {HandlerRes, BodyRes, State#state.connection} of
+ {ok, ok, keepalive} -> next_request(State);
_Closed -> terminate(State)
end.
+-spec ensure_body_processed(Req::#http_req{}) -> ok | close.
+ensure_body_processed(#http_req{body_state=done}) ->
+ ok;
+ensure_body_processed(Req=#http_req{body_state=waiting}) ->
+ {Length, Req2} = cowboy_http_req:header('Content-Length', Req),
+ case Length of
+ "" -> ok;
+ _Any ->
+ Length2 = list_to_integer(Length),
+ skip_body(Length2, Req2)
+ end.
+
+-spec skip_body(Length::non_neg_integer(), Req::#http_req{}) -> ok | close.
+skip_body(Length, Req) ->
+ case cowboy_http_req:body(Length, Req) of
+ {error, _Reason} -> close;
+ _Any -> ok
+ end.
+
%% No response has been sent but everything apparently went fine.
%% Reply with 204 No Content to indicate this.
ensure_response(#http_req{resp_state=waiting}, State) ->