aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2012-03-13 03:00:05 +0100
committerLoïc Hoguin <[email protected]>2012-03-13 03:00:05 +0100
commit36a6823e50e36c201df6c7a88a4c77cdaac7f2e6 (patch)
treebd8f0c48615616f7146d94496bb466d0e917f638
parente87f51e542a9ee5bdcadd5841731b32210732e08 (diff)
downloadcowboy-36a6823e50e36c201df6c7a88a4c77cdaac7f2e6.tar.gz
cowboy-36a6823e50e36c201df6c7a88a4c77cdaac7f2e6.tar.bz2
cowboy-36a6823e50e36c201df6c7a88a4c77cdaac7f2e6.zip
Do not send chunked Transfer-Encoding replies for HTTP/1.0
Fixes compatibility issue #140 reported by @majek.
-rw-r--r--src/cowboy_http_protocol.erl2
-rw-r--r--src/cowboy_http_req.erl20
-rw-r--r--test/http_SUITE.erl17
3 files changed, 31 insertions, 8 deletions
diff --git a/src/cowboy_http_protocol.erl b/src/cowboy_http_protocol.erl
index 71518fa..2d58315 100644
--- a/src/cowboy_http_protocol.erl
+++ b/src/cowboy_http_protocol.erl
@@ -402,6 +402,8 @@ ensure_response(Req=#http_req{resp_state=waiting}) ->
%% Close the chunked reply.
ensure_response(#http_req{method='HEAD', resp_state=chunks}) ->
close;
+ensure_response(#http_req{version={1, 0}, resp_state=chunks}) ->
+ close;
ensure_response(#http_req{socket=Socket, transport=Transport,
resp_state=chunks}) ->
Transport:send(Socket, <<"0\r\n\r\n">>),
diff --git a/src/cowboy_http_req.erl b/src/cowboy_http_req.erl
index 6b947d9..c352bbf 100644
--- a/src/cowboy_http_req.erl
+++ b/src/cowboy_http_req.erl
@@ -579,15 +579,21 @@ chunked_reply(Status, Req) ->
-spec chunked_reply(cowboy_http:status(), cowboy_http:headers(), #http_req{})
-> {ok, #http_req{}}.
chunked_reply(Status, Headers, Req=#http_req{socket=Socket,
- transport=Transport, connection=Connection, pid=ReqPid,
- resp_state=waiting, resp_headers=RespHeaders}) ->
+ transport=Transport, version=Version, connection=Connection,
+ pid=ReqPid, resp_state=waiting, resp_headers=RespHeaders}) ->
RespConn = response_connection(Headers, Connection),
- Head = response_head(Status, Headers, RespHeaders, [
- {<<"Connection">>, atom_to_connection(Connection)},
- {<<"Transfer-Encoding">>, <<"chunked">>},
+ DefaultHeaders = [
{<<"Date">>, cowboy_clock:rfc1123()},
{<<"Server">>, <<"Cowboy">>}
- ]),
+ ],
+ DefaultHeaders2 = case Version of
+ {1, 1} -> [
+ {<<"Connection">>, atom_to_connection(Connection)},
+ {<<"Transfer-Encoding">>, <<"chunked">>}
+ ] ++ DefaultHeaders;
+ _ -> DefaultHeaders
+ end,
+ Head = response_head(Status, Headers, RespHeaders, DefaultHeaders2),
Transport:send(Socket, Head),
ReqPid ! {?MODULE, resp_sent},
{ok, Req#http_req{connection=RespConn, resp_state=chunks,
@@ -599,6 +605,8 @@ chunked_reply(Status, Headers, Req=#http_req{socket=Socket,
-spec chunk(iodata(), #http_req{}) -> ok | {error, atom()}.
chunk(_Data, #http_req{socket=_Socket, transport=_Transport, method='HEAD'}) ->
ok;
+chunk(Data, #http_req{socket=Socket, transport=Transport, version={1, 0}}) ->
+ Transport:send(Socket, Data);
chunk(Data, #http_req{socket=Socket, transport=Transport, resp_state=chunks}) ->
Transport:send(Socket, [integer_to_list(iolist_size(Data), 16),
<<"\r\n">>, Data, <<"\r\n">>]).
diff --git a/test/http_SUITE.erl b/test/http_SUITE.erl
index 72892eb..8237750 100644
--- a/test/http_SUITE.erl
+++ b/test/http_SUITE.erl
@@ -28,7 +28,7 @@
-export([http_200/1, http_404/1, handler_errors/1,
file_200/1, file_403/1, dir_403/1, file_404/1,
file_400/1]). %% http and https.
--export([http_10_hostless/1]). %% misc.
+-export([http_10_hostless/1, http_10_chunkless/1]). %% misc.
-export([rest_simple/1, rest_keepalive/1, rest_keepalive_post/1,
rest_nodelete/1, rest_resource_etags/1]). %% rest.
@@ -47,7 +47,7 @@ groups() ->
static_mimetypes_function, static_attribute_etag,
static_function_etag, multipart] ++ BaseTests},
{https, [], BaseTests},
- {misc, [], [http_10_hostless]},
+ {misc, [], [http_10_hostless, http_10_chunkless]},
{rest, [], [rest_simple, rest_keepalive, rest_keepalive_post,
rest_nodelete, rest_resource_etags]}].
@@ -89,6 +89,7 @@ init_per_group(misc, Config) ->
{ok,_} = cowboy:start_listener(misc, 100,
cowboy_tcp_transport, [{port, Port}],
cowboy_http_protocol, [{dispatch, [{'_', [
+ {[<<"chunked_response">>], chunked_handler, []},
{[], http_handler, []}
]}]}]),
[{port, Port}|Config];
@@ -562,12 +563,24 @@ file_400(Config) ->
httpc:request(build_url("/static/%2e", Config)),
{ok, {{"HTTP/1.1", 400, "Bad Request"}, _Headers2, _Body2}} =
httpc:request(build_url("/static/%2e%2e", Config)).
+
%% misc.
http_10_hostless(Config) ->
Packet = "GET / HTTP/1.0\r\n\r\n",
{Packet, 200} = raw_req(Packet, Config).
+http_10_chunkless(Config) ->
+ {port, Port} = lists:keyfind(port, 1, Config),
+ {ok, Socket} = gen_tcp:connect("localhost", Port,
+ [binary, {active, false}, {packet, raw}]),
+ Packet = "GET /chunked_response HTTP/1.0\r\nContent-Length: 0\r\n\r\n",
+ ok = gen_tcp:send(Socket, Packet),
+ {ok, Data} = gen_tcp:recv(Socket, 0, 6000),
+ nomatch = binary:match(Data, <<"Transfer-Encoding">>),
+ {_, _} = binary:match(Data, <<"chunked_handler\r\nworks fine!">>),
+ ok = gen_tcp:close(Socket).
+
%% rest.
rest_simple(Config) ->