aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2011-04-14 01:26:02 +0200
committerLoïc Hoguin <[email protected]>2011-04-14 01:32:02 +0200
commitc32db277c82054597ac4200424fa4cbe55448ea8 (patch)
treebf0921a6fe8d0de3fa9729195dfea45b8d1c1305 /src
parent4048499af2a19ac876da30236bbf06fe3f7ffdb3 (diff)
downloadcowboy-c32db277c82054597ac4200424fa4cbe55448ea8.tar.gz
cowboy-c32db277c82054597ac4200424fa4cbe55448ea8.tar.bz2
cowboy-c32db277c82054597ac4200424fa4cbe55448ea8.zip
Fix a bug where dupe headers were sent in cowboy_http_req:reply/4.
Now the server defines default headers that can be overwritten by the handler simply by passing them to the reply/4 function. Default headers include, for now, Connection and Content-Length headers. Note that it isn't enough to change the Connection header to close a keep-alive connection server-side.
Diffstat (limited to 'src')
-rw-r--r--src/cowboy_http_req.erl13
1 files changed, 8 insertions, 5 deletions
diff --git a/src/cowboy_http_req.erl b/src/cowboy_http_req.erl
index 8ea8efc..4907503 100644
--- a/src/cowboy_http_req.erl
+++ b/src/cowboy_http_req.erl
@@ -174,15 +174,18 @@ body_qs(Req) ->
-spec reply(Code::http_status(), Headers::http_headers(),
Body::iolist(), Req::#http_req{}) -> {ok, Req::#http_req{}}.
-%% @todo Don't be naive about the headers!
reply(Code, Headers, Body, Req=#http_req{socket=Socket,
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]),
+ DefaultHeaders = [
+ {"Connection", atom_to_connection(Connection)},
+ {"Content-Length", integer_to_list(iolist_size(Body))}
+ ],
+ Headers2 = lists:keysort(1, Headers),
+ Headers3 = lists:ukeymerge(1, Headers2, DefaultHeaders),
+ Headers4 = [[Key, ": ", Value, "\r\n"] || {Key, Value} <- Headers3],
+ Transport:send(Socket, [StatusLine, Headers4, "\r\n", Body]),
{ok, Req#http_req{resp_state=done}}.
%% Internal.