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

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.
This commit is contained in:
Loïc Hoguin 2011-03-20 18:03:11 +01:00
parent d69d0adfa7
commit 71b31cee92
3 changed files with 14 additions and 5 deletions

View file

@ -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.