diff options
author | Loïc Hoguin <[email protected]> | 2011-05-08 14:40:58 +0200 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2011-05-08 14:40:58 +0200 |
commit | 18582a7a39e3c45231c7d9a09f5fc7e84299ee3e (patch) | |
tree | 10e78718fcad02e8aaf216a4da090a828f9e5e9e | |
parent | 39513deacaf69854ed0ec13433a77dfa2f720d8c (diff) | |
download | cowboy-18582a7a39e3c45231c7d9a09f5fc7e84299ee3e.tar.gz cowboy-18582a7a39e3c45231c7d9a09f5fc7e84299ee3e.tar.bz2 cowboy-18582a7a39e3c45231c7d9a09f5fc7e84299ee3e.zip |
Separate building the response head from replying.
-rw-r--r-- | src/cowboy_http_req.erl | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/src/cowboy_http_req.erl b/src/cowboy_http_req.erl index 38cf71d..c430020 100644 --- a/src/cowboy_http_req.erl +++ b/src/cowboy_http_req.erl @@ -184,18 +184,12 @@ body_qs(Req) -> reply(Code, Headers, Body, Req=#http_req{socket=Socket, transport=Transport, connection=Connection, resp_state=waiting}) -> - StatusLine = <<"HTTP/1.1 ", (status(Code))/binary, "\r\n">>, - DefaultHeaders = [ + Head = response_head(Code, Headers, [ {<<"Connection">>, atom_to_connection(Connection)}, {<<"Content-Length">>, list_to_binary(integer_to_list(iolist_size(Body)))} - ], - Headers2 = [{header_to_binary(Key), Value} || {Key, Value} <- Headers], - Headers3 = lists:keysort(1, Headers2), - Headers4 = lists:ukeymerge(1, Headers3, DefaultHeaders), - Headers5 = [<< Key/binary, ": ", Value/binary, "\r\n" >> - || {Key, Value} <- Headers4], - Transport:send(Socket, [StatusLine, Headers5, <<"\r\n">>, Body]), + ]), + Transport:send(Socket, [Head, Body]), {ok, Req#http_req{resp_state=done}}. %% Internal. @@ -210,6 +204,17 @@ parse_qs(Qs) -> [Name, Value] -> {Name, Value} end || Token <- Tokens]. +-spec response_head(Code::http_status(), Headers::http_headers(), + DefaultHeaders::http_headers()) -> iolist(). +response_head(Code, Headers, DefaultHeaders) -> + StatusLine = <<"HTTP/1.1 ", (status(Code))/binary, "\r\n">>, + Headers2 = [{header_to_binary(Key), Value} || {Key, Value} <- Headers], + Headers3 = lists:keysort(1, Headers2), + Headers4 = lists:ukeymerge(1, Headers3, DefaultHeaders), + Headers5 = [<< Key/binary, ": ", Value/binary, "\r\n" >> + || {Key, Value} <- Headers4], + [StatusLine, Headers5, <<"\r\n">>]. + -spec atom_to_connection(Atom::keepalive | close) -> binary(). atom_to_connection(keepalive) -> <<"keep-alive">>; |