mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 12:20:24 +00:00
Separate building the response head from replying.
This commit is contained in:
parent
39513deaca
commit
18582a7a39
1 changed files with 14 additions and 9 deletions
|
@ -184,18 +184,12 @@ body_qs(Req) ->
|
||||||
reply(Code, Headers, Body, Req=#http_req{socket=Socket,
|
reply(Code, Headers, Body, Req=#http_req{socket=Socket,
|
||||||
transport=Transport, connection=Connection,
|
transport=Transport, connection=Connection,
|
||||||
resp_state=waiting}) ->
|
resp_state=waiting}) ->
|
||||||
StatusLine = <<"HTTP/1.1 ", (status(Code))/binary, "\r\n">>,
|
Head = response_head(Code, Headers, [
|
||||||
DefaultHeaders = [
|
|
||||||
{<<"Connection">>, atom_to_connection(Connection)},
|
{<<"Connection">>, atom_to_connection(Connection)},
|
||||||
{<<"Content-Length">>,
|
{<<"Content-Length">>,
|
||||||
list_to_binary(integer_to_list(iolist_size(Body)))}
|
list_to_binary(integer_to_list(iolist_size(Body)))}
|
||||||
],
|
]),
|
||||||
Headers2 = [{header_to_binary(Key), Value} || {Key, Value} <- Headers],
|
Transport:send(Socket, [Head, Body]),
|
||||||
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]),
|
|
||||||
{ok, Req#http_req{resp_state=done}}.
|
{ok, Req#http_req{resp_state=done}}.
|
||||||
|
|
||||||
%% Internal.
|
%% Internal.
|
||||||
|
@ -210,6 +204,17 @@ parse_qs(Qs) ->
|
||||||
[Name, Value] -> {Name, Value}
|
[Name, Value] -> {Name, Value}
|
||||||
end || Token <- Tokens].
|
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().
|
-spec atom_to_connection(Atom::keepalive | close) -> binary().
|
||||||
atom_to_connection(keepalive) ->
|
atom_to_connection(keepalive) ->
|
||||||
<<"keep-alive">>;
|
<<"keep-alive">>;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue