mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 20:30:23 +00:00
Do not send chunked Transfer-Encoding replies for HTTP/1.0
Fixes compatibility issue #140 reported by @majek.
This commit is contained in:
parent
e87f51e542
commit
36a6823e50
3 changed files with 31 additions and 8 deletions
|
@ -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">>),
|
||||
|
|
|
@ -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">>]).
|
||||
|
|
|
@ -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) ->
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue