aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2011-03-20 18:03:11 +0100
committerLoïc Hoguin <[email protected]>2011-03-20 18:03:36 +0100
commit71b31cee92f5e0c92f57e94c4916e9e19bbafb3f (patch)
treeef6fae811b5499483edd341bd250ff73d3d27bf8 /src
parentd69d0adfa72383736fe9705f5060881baefe2d21 (diff)
downloadcowboy-71b31cee92f5e0c92f57e94c4916e9e19bbafb3f.tar.gz
cowboy-71b31cee92f5e0c92f57e94c4916e9e19bbafb3f.tar.bz2
cowboy-71b31cee92f5e0c92f57e94c4916e9e19bbafb3f.zip
Make sure we can only reply to an HTTP request inside Handler:handle.
Of course since requests are a record the response state can be explicitly overriden, but standard use prevents errors by making sure only one reply is sent.
Diffstat (limited to 'src')
-rw-r--r--src/cowboy_http_protocol.erl6
-rw-r--r--src/cowboy_http_req.erl5
2 files changed, 7 insertions, 4 deletions
diff --git a/src/cowboy_http_protocol.erl b/src/cowboy_http_protocol.erl
index f6047bf..4b3c40b 100644
--- a/src/cowboy_http_protocol.erl
+++ b/src/cowboy_http_protocol.erl
@@ -143,7 +143,8 @@ handler_init(Req, State=#state{handler={Handler, Opts}}) ->
-spec handler_loop(HandlerState::term(), Req::#http_req{},
State::#state{}) -> ok.
handler_loop(HandlerState, Req, State=#state{handler={Handler, _Opts}}) ->
- case catch Handler:handle(Req, HandlerState) of
+ case catch Handler:handle(Req#http_req{resp_state=waiting},
+ HandlerState) of
{ok, Req2, HandlerState2} ->
handler_terminate(HandlerState2, Req2, State);
{'EXIT', _Reason} ->
@@ -153,7 +154,8 @@ 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(Req, HandlerState)),
+ Res = (catch Handler:terminate(
+ Req#http_req{resp_state=locked}, HandlerState)),
%% @todo We need to check if the Req has been replied to.
%% All requests must have a reply, at worst an error.
%% If a request started but wasn't completed, complete it.
diff --git a/src/cowboy_http_req.erl b/src/cowboy_http_req.erl
index 3dcac2f..40af601 100644
--- a/src/cowboy_http_req.erl
+++ b/src/cowboy_http_req.erl
@@ -135,13 +135,14 @@ headers(Req) ->
Body::iolist(), Req::#http_req{}) -> ok.
%% @todo Don't be naive about the headers!
reply(Code, Headers, Body, Req=#http_req{socket=Socket,
- transport=Transport, connection=Connection}) ->
+ transport=Transport, connection=Connection,
+ resp_state=waiting}) ->
StatusLine = ["HTTP/1.1 ", status(Code), "\r\n"],
BaseHeaders = ["Connection: ", atom_to_connection(Connection),
"\r\nContent-Length: ", integer_to_list(iolist_size(Body)), "\r\n"],
Transport:send(Socket,
[StatusLine, BaseHeaders, Headers, "\r\n", Body]),
- {ok, Req}.
+ {ok, Req#http_req{resp_state=done}}.
%% Internal.