diff --git a/src/mochiweb_http.erl b/src/mochiweb_http.erl index 7d5af66..46c3b8c 100644 --- a/src/mochiweb_http.erl +++ b/src/mochiweb_http.erl @@ -146,7 +146,7 @@ headers(Socket, Opts, Request, Headers, Body, {tcp_closed = Error, _} -> mochiweb_socket:close(Socket), exit({shutdown, Error}); {tcp_error, _, emsgsize} -> - handle_invalid_request(Socket, Opts, Request, Headers) + handle_invalid_request(Socket, Opts, Request, Headers, 431) after ?HEADERS_RECV_TIMEOUT -> mochiweb_socket:close(Socket), exit({shutdown, headers_recv_timeout}) end. @@ -158,18 +158,22 @@ call_body(Body, Req) -> Body(Req). -spec handle_invalid_request(term(), term()) -> no_return(). handle_invalid_request(Socket, Opts) -> - handle_invalid_request(Socket, Opts, - {'GET', {abs_path, "/"}, {0, 9}}, []). + handle_invalid_request( + Socket, Opts, {'GET', {abs_path, "/"}, {0, 9}}, []). +-spec handle_invalid_request( + term(), term(), term(), term()) -> no_return(). +handle_invalid_request(Socket, Opts, Request, RevHeaders) -> + handle_invalid_request(Socket, Opts, Request, RevHeaders, 400). --spec handle_invalid_request(term(), term(), term(), - term()) -> no_return(). +-spec handle_invalid_request( + term(), term(), term(), term(), 400..431) -> no_return(). handle_invalid_request(Socket, Opts, Request, - RevHeaders) -> + RevHeaders, StatusCode) -> {ReqM, _} = Req = new_request(Socket, Opts, Request, RevHeaders), - ReqM:respond({400, [], []}, Req), + ReqM:respond({StatusCode, [], []}, Req), mochiweb_socket:close(Socket), exit({shutdown, invalid_request}). diff --git a/src/mochiweb_request.erl b/src/mochiweb_request.erl index c4fcff0..68187af 100644 --- a/src/mochiweb_request.erl +++ b/src/mochiweb_request.erl @@ -929,6 +929,10 @@ server_headers() -> [{"Server", "MochiWeb/1.0 (" ++ (?QUIP) ++ ")"}, {"Date", mochiweb_clock:rfc1123()}]. +make_code(431) -> + % HTTP Code 431 not yet supported in httpd_util + ["431", + [" " | "Request Header Fields Too Large"]]; make_code(X) when is_integer(X) -> [integer_to_list(X), [" " | httpd_util:reason_phrase(X)]]; diff --git a/test/mochiweb_http_tests.erl b/test/mochiweb_http_tests.erl index 35be11d..7883528 100644 --- a/test/mochiweb_http_tests.erl +++ b/test/mochiweb_http_tests.erl @@ -17,8 +17,7 @@ has_acceptor_bug_tests(Server) -> [{"1000 should be fine even with the bug", ?_assertEqual(false, (has_bug(Port, 1000)))}, {"10000 should trigger the bug if present", - ?_assertEqual(false, - (has_bug(Port, 10000)))}]. + ?_assertNot((has_bug(Port, 10000)))}]. responder(Req) -> mochiweb_request:respond({200, @@ -37,6 +36,6 @@ has_bug(Port, Len) -> {{"HTTP/1.1", 200, "OK"}, _, "Hello"}} -> false; - %% It is expected that the request will fail because the header is too long - {ok, {{"HTTP/1.1", 400, "Bad Request"}, _, []}} -> false - end. + {ok, {{"HTTP/1.1", 431, "Request Header Fields Too Large"}, _, []}} -> + false + end. \ No newline at end of file