diff options
author | Loïc Hoguin <[email protected]> | 2012-01-02 09:41:30 +0100 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2012-01-06 20:23:59 +0100 |
commit | fd211d3c039e3521537087ef848727bb4f7410c0 (patch) | |
tree | 67e23408d321d6bd779d46d88cef3d1d859ef361 /src/cowboy_http_protocol.erl | |
parent | ba87aa4193f5fadc35cbd34dc0191d2f3e2cef78 (diff) | |
download | cowboy-fd211d3c039e3521537087ef848727bb4f7410c0.tar.gz cowboy-fd211d3c039e3521537087ef848727bb4f7410c0.tar.bz2 cowboy-fd211d3c039e3521537087ef848727bb4f7410c0.zip |
Fix handler crashes handling
We try to send a 500 error only if we didn't send the response
headers yet. If they were, then we have no way to be sure the
response was fully sent, nor should we assume anything about
how this will be handled client-side, so we do nothing more
and in both cases close the connection.
Diffstat (limited to 'src/cowboy_http_protocol.erl')
-rw-r--r-- | src/cowboy_http_protocol.erl | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/src/cowboy_http_protocol.erl b/src/cowboy_http_protocol.erl index 9b71e6c..ea59799 100644 --- a/src/cowboy_http_protocol.erl +++ b/src/cowboy_http_protocol.erl @@ -120,13 +120,13 @@ request({http_request, Method, {abs_path, AbsPath}, Version}, {Path, RawPath, Qs} = cowboy_dispatcher:split_path(AbsPath, URLDecode), ConnAtom = version_to_connection(Version), parse_header(#http_req{socket=Socket, transport=Transport, - connection=ConnAtom, method=Method, version=Version, + connection=ConnAtom, pid=self(), method=Method, version=Version, path=Path, raw_path=RawPath, raw_qs=Qs, urldecode=URLDec}, State); request({http_request, Method, '*', Version}, State=#state{socket=Socket, transport=Transport, urldecode=URLDec}) -> ConnAtom = version_to_connection(Version), parse_header(#http_req{socket=Socket, transport=Transport, - connection=ConnAtom, method=Method, version=Version, + connection=ConnAtom, pid=self(), method=Method, version=Version, path='*', raw_path= <<"*">>, raw_qs= <<>>, urldecode=URLDec}, State); request({http_request, _Method, _URI, _Version}, State) -> error_terminate(501, State); @@ -276,7 +276,7 @@ handler_handle(HandlerState, Req, State=#state{handler={Handler, Opts}}) -> [Handler, Class, Reason, Opts, HandlerState, Req, erlang:get_stacktrace()]), handler_terminate(HandlerState, Req, State), - terminate(State) + error_terminate(500, State) end. %% We don't listen for Transport closes because that would force us @@ -331,7 +331,9 @@ handler_call(HandlerState, Req, State=#state{handler={Handler, Opts}}, "** Options were ~p~n** Handler state was ~p~n" "** Request was ~p~n** Stacktrace: ~p~n~n", [Handler, Class, Reason, Opts, - HandlerState, Req, erlang:get_stacktrace()]) + HandlerState, Req, erlang:get_stacktrace()]), + handler_terminate(HandlerState, Req, State), + error_terminate(500, State) end. -spec handler_terminate(any(), #http_req{}, #state{}) -> ok. @@ -359,6 +361,8 @@ next_request(Req=#http_req{connection=Conn, buffer=Buffer}, HandlerRes) -> RespRes = ensure_response(Req), BodyRes = ensure_body_processed(Req), + %% Flush the resp_sent message before moving on. + receive {cowboy_http_req, resp_sent} -> ok after 0 -> ok end, case {HandlerRes, BodyRes, RespRes, Conn} of {ok, ok, ok, keepalive} when Keepalive < MaxKeepalive -> ?MODULE:parse_request(State#state{ @@ -395,11 +399,17 @@ ensure_response(#http_req{socket=Socket, transport=Transport, Transport:send(Socket, <<"0\r\n\r\n">>), close. +%% Only send an error reply if there is no resp_sent message. -spec error_terminate(http_status(), #state{}) -> ok. error_terminate(Code, State=#state{socket=Socket, transport=Transport}) -> - _ = cowboy_http_req:reply(Code, [], [], #http_req{ - socket=Socket, transport=Transport, - connection=close, resp_state=waiting}), + receive + {cowboy_http_req, resp_sent} -> ok + after 0 -> + _ = cowboy_http_req:reply(Code, #http_req{ + socket=Socket, transport=Transport, + connection=close, pid=self(), resp_state=waiting}), + ok + end, terminate(State). -spec terminate(#state{}) -> ok. |