Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

long http headers and http error packets (zd864 + bz1291) #3

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
142 changes: 118 additions & 24 deletions src/mochiweb_http.erl
Expand Up @@ -96,22 +96,31 @@ default_body(Req) ->
default_body(Req, Req:get(method), Req:get(path)). default_body(Req, Req:get(method), Req:get(path)).


loop(Socket, Body) -> loop(Socket, Body) ->
mochiweb_socket:setopts(Socket, [{packet, http}]), ok = mochiweb_socket:setopts(Socket, [{packet, line}]),
request(Socket, Body). request(Socket, Body, <<>>).


request(Socket, Body) -> request(Socket, Body, Prev) ->
mochiweb_socket:setopts(Socket, [{active, once}]), ok = mochiweb_socket:setopts(Socket, [{active, once}]),
receive receive
{Protocol, _, {http_request, Method, Path, Version}} when Protocol == http orelse Protocol == ssl -> {Protocol, _, Bin} when Protocol =:= tcp orelse Protocol =:= ssl ->
mochiweb_socket:setopts(Socket, [{packet, httph}]), FullBin = <<Prev/binary, Bin/binary>>,
headers(Socket, {Method, Path, Version}, [], Body, 0); case erlang:decode_packet(http, FullBin, []) of
{Protocol, _, {http_error, "\r\n"}} when Protocol == http orelse Protocol == ssl -> {ok, {http_request, Method, Path, Version}, <<>>} ->
request(Socket, Body); collect_headers(Socket, {Method, Path, Version}, Body,
{Protocol, _, {http_error, "\n"}} when Protocol == http orelse Protocol == ssl -> <<>>, false, 0);
request(Socket, Body); {ok, {http_error, "\r\n"}, <<>>} ->
request(Socket, Body, <<>>);
{ok, {http_error, "\n"}, <<>>} ->
request(Socket, Body, <<>>);
{more, _} ->
request(Socket, Body, FullBin)
end;
{tcp_closed, _} -> {tcp_closed, _} ->
mochiweb_socket:close(Socket), mochiweb_socket:close(Socket),
exit(normal); exit(normal);
{ssl_closed, _} ->
mochiweb_socket:close(Socket),
exit(normal);
_Other -> _Other ->
handle_invalid_request(Socket) handle_invalid_request(Socket)
after ?REQUEST_RECV_TIMEOUT -> after ?REQUEST_RECV_TIMEOUT ->
Expand All @@ -124,30 +133,64 @@ reentry(Body) ->
?MODULE:after_response(Body, Req) ?MODULE:after_response(Body, Req)
end. end.


headers(Socket, Request, Headers, _Body, ?MAX_HEADERS) -> collect_headers(Socket, Request, _Body, _Collected, _Trunc, ?MAX_HEADERS) ->
%% Too many headers sent, bad request. %% Too many headers sent, bad request.
mochiweb_socket:setopts(Socket, [{packet, raw}]), handle_invalid_request(Socket, Request, []);
handle_invalid_request(Socket, Request, Headers); collect_headers(Socket, Request, Body, Collected, Trunc, HeaderCount) ->
headers(Socket, Request, Headers, Body, HeaderCount) -> ok = mochiweb_socket:setopts(Socket, [{active, once}]),
mochiweb_socket:setopts(Socket, [{active, once}]),
receive receive
{Protocol, _, http_eoh} when Protocol == http orelse Protocol == ssl -> {Protocol, _, More} when Protocol =:= tcp orelse Protocol =:= ssl ->
Req = new_request(Socket, Request, Headers), case {Trunc, More} of
call_body(Body, Req), {false, <<"\n">>} ->
?MODULE:after_response(Body, Req); ok = mochiweb_socket:setopts(Socket, [{packet, raw}]),
{Protocol, _, {http_header, _, Name, _, Value}} when Protocol == http orelse Protocol == ssl -> parse_headers(Socket, Request, Body,
headers(Socket, Request, [{Name, Value} | Headers], Body, <<Collected/binary, "\r\n">>, []);
1 + HeaderCount); {false, <<"\r\n">>} ->
ok = mochiweb_socket:setopts(Socket, [{packet, raw}]),
parse_headers(Socket, Request, Body,
<<Collected/binary, "\r\n">>, []);
{_, More} ->
NewBin = <<Collected/binary, More/binary>>,
AllButOne= size(More) - 1,
{Truncated, NewHdrCount} =
case More of
<<_:AllButOne/binary, "\n">> ->
{false, 1 + HeaderCount};
_ ->
{true, HeaderCount}
end,
collect_headers(Socket, Request, Body, NewBin,
Truncated, NewHdrCount)
end;
{tcp_closed, _} -> {tcp_closed, _} ->
mochiweb_socket:close(Socket), mochiweb_socket:close(Socket),
exit(normal); exit(normal);
{ssl_closed, _} ->
mochiweb_socket:close(Socket),
exit(normal);
_Other -> _Other ->
handle_invalid_request(Socket, Request, Headers) handle_invalid_request(Socket, Request, [])
after ?HEADERS_RECV_TIMEOUT -> after ?HEADERS_RECV_TIMEOUT ->
mochiweb_socket:close(Socket), mochiweb_socket:close(Socket),
exit(normal) exit(normal)
end. end.


parse_headers(Socket, Request, Body, <<"\r\n">>, Headers) ->
Req = new_request(Socket, Request, lists:reverse(Headers)),
call_body(Body, Req),
?MODULE:after_response(Body, Req);
parse_headers(Socket, Request, Body, Bin, Headers) ->
case erlang:decode_packet(httph, Bin, []) of
{ok, {http_header, _, Name, _, Value}, More} ->
parse_headers(Socket, Request, Body, More,
[{Name, Value} | Headers]);
{more, _} ->
handle_invalid_request(Socket, Request, Headers);
{error, _Reason} ->
mochiweb_socket:close(Socket),
exit(normal)
end.

call_body({M, F, A}, Req) -> call_body({M, F, A}, Req) ->
erlang:apply(M, F, [Req | A]); erlang:apply(M, F, [Req | A]);
call_body({M, F}, Req) -> call_body({M, F}, Req) ->
Expand Down Expand Up @@ -290,4 +333,55 @@ range_skip_length_test() ->
range_skip_length({BodySize, none}, BodySize)), range_skip_length({BodySize, none}, BodySize)),
ok. ok.


long_request_line_test() ->
{ok, LS} = gen_tcp:listen(0, [binary, {active, false}]),
{ok, Port} = inet:port(LS),
spawn_link(fun() ->
{ok, S} = gen_tcp:accept(LS),
try
loop(S, {?MODULE, default_body})
after
gen_tcp:close(S),
gen_tcp:close(LS)
end
end),
{ok, S} = gen_tcp:connect("localhost", Port, [binary, {active, false}]),
try
Req = "GET /" ++ string:chars($X, 8192) ++ " HTTP/1.1\r\n"
++ "Host: localhost\r\n\r\n",
ok = gen_tcp:send(S, Req),
inet:setopts(S, [{packet, http}]),
?assertEqual({ok, {http_response, {1,1}, 200, "OK"}},
gen_tcp:recv(S, 0)),
ok
after
gen_tcp:close(S)
end.

long_header_test() ->
{ok, LS} = gen_tcp:listen(0, [binary, {active, false}]),
{ok, Port} = inet:port(LS),
spawn_link(fun() ->
{ok, S} = gen_tcp:accept(LS),
try
loop(S, {?MODULE, default_body})
after
gen_tcp:close(S),
gen_tcp:close(LS)
end
end),
{ok, S} = gen_tcp:connect("localhost", Port, [binary, {active, false}]),
try
Req = "GET / HTTP/1.1\r\n"
++ "Host: localhost\r\n"
++ "Link: /" ++ string:chars($X, 8192) ++ "\r\n\r\n",
ok = gen_tcp:send(S, Req),
inet:setopts(S, [{packet, http}]),
?assertEqual({ok, {http_response, {1,1}, 200, "OK"}},
gen_tcp:recv(S, 0)),
ok
after
gen_tcp:close(S)
end.

-endif. -endif.