diff --git a/src/cowboy_compress_h.erl b/src/cowboy_compress_h.erl index e9824ba1..9f9a06e2 100644 --- a/src/cowboy_compress_h.erl +++ b/src/cowboy_compress_h.erl @@ -72,8 +72,7 @@ early_error(StreamID, Reason, PartialReq, Resp, Opts) -> %% Check if the client supports decoding of gzip responses. check_req(Req) -> - %% @todo Probably shouldn't unconditionally crash on failure. - case cowboy_req:parse_header(<<"accept-encoding">>, Req) of + try cowboy_req:parse_header(<<"accept-encoding">>, Req) of %% Client doesn't support any compression algorithm. undefined -> #state{compress=undefined}; @@ -87,6 +86,10 @@ check_req(Req) -> _ -> #state{compress=gzip} end + catch + %% Don't raise an error because of malformed or unsupported accept-encoding headers. + exit:{request_error, {header, <<"accept-encoding">>}, _Reason} -> + #state{compress=undefined} end. %% Do not compress responses that contain the content-encoding header. diff --git a/test/compress_SUITE.erl b/test/compress_SUITE.erl index 8891e42e..c51e2894 100644 --- a/test/compress_SUITE.erl +++ b/test/compress_SUITE.erl @@ -80,6 +80,24 @@ gzip_accept_encoding_no_gzip(Config) -> {_, <<"100000">>} = lists:keyfind(<<"content-length">>, 1, Headers), ok. +gzip_accept_encoding_not_supported(Config) -> + doc("Send not supported accept-encoding; get an uncompressed response."), + {200, Headers, _} = do_get("/reply/large", + [{<<"accept-encoding">>, <<"application/gzip">>}], Config), + false = lists:keyfind(<<"content-encoding">>, 1, Headers), + false = lists:keyfind(<<"vary">>, 1, Headers), + {_, <<"100000">>} = lists:keyfind(<<"content-length">>, 1, Headers), + ok. + +gzip_accept_encoding_malformed(Config) -> + doc("Send malformed accept-encoding; get an uncompressed response."), + {200, Headers, _} = do_get("/reply/large", + [{<<"accept-encoding">>, <<";">>}], Config), + false = lists:keyfind(<<"content-encoding">>, 1, Headers), + false = lists:keyfind(<<"vary">>, 1, Headers), + {_, <<"100000">>} = lists:keyfind(<<"content-length">>, 1, Headers), + ok. + gzip_reply_content_encoding(Config) -> doc("Reply with content-encoding header; get an uncompressed response."), {200, Headers, _} = do_get("/reply/content-encoding",