aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarcos Ferreira <[email protected]>2019-12-23 22:31:47 -0300
committerLoïc Hoguin <[email protected]>2019-12-31 13:48:05 +0100
commit3b85b808ae3cc48d19b71b72869ee59c25e70b00 (patch)
tree19d7fd58dbd3db97f1cdcc4653c179f4a8125ef3
parentaa1a7d09f345f0038d6769bc13eb8c0544af6475 (diff)
downloadcowboy-3b85b808ae3cc48d19b71b72869ee59c25e70b00.tar.gz
cowboy-3b85b808ae3cc48d19b71b72869ee59c25e70b00.tar.bz2
cowboy-3b85b808ae3cc48d19b71b72869ee59c25e70b00.zip
Ignore malformed accept-encoding headers in cowboy_compress_h
-rw-r--r--src/cowboy_compress_h.erl8
-rw-r--r--test/compress_SUITE.erl18
2 files changed, 24 insertions, 2 deletions
diff --git a/src/cowboy_compress_h.erl b/src/cowboy_compress_h.erl
index e9824ba..374cb6a 100644
--- a/src/cowboy_compress_h.erl
+++ b/src/cowboy_compress_h.erl
@@ -71,9 +71,10 @@ early_error(StreamID, Reason, PartialReq, Resp, Opts) ->
%% Internal.
%% Check if the client supports decoding of gzip responses.
+%%
+%% A malformed accept-encoding header is ignored (no compression).
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 +88,9 @@ check_req(Req) ->
_ ->
#state{compress=gzip}
end
+ catch
+ _:_ ->
+ #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 8891e42..a25c427 100644
--- a/test/compress_SUITE.erl
+++ b/test/compress_SUITE.erl
@@ -62,6 +62,15 @@ do_get(Path, ReqHeaders, Config) ->
%% Tests.
+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_accept_encoding_missing(Config) ->
doc("Don't send accept-encoding; get an uncompressed response."),
{200, Headers, _} = do_get("/reply/large",
@@ -80,6 +89,15 @@ gzip_accept_encoding_no_gzip(Config) ->
{_, <<"100000">>} = lists:keyfind(<<"content-length">>, 1, Headers),
ok.
+gzip_accept_encoding_not_supported(Config) ->
+ doc("Send unsupported 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_reply_content_encoding(Config) ->
doc("Reply with content-encoding header; get an uncompressed response."),
{200, Headers, _} = do_get("/reply/content-encoding",