diff options
author | Loïc Hoguin <[email protected]> | 2019-10-04 13:32:35 +0200 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2019-10-04 13:32:35 +0200 |
commit | 5ffb4f98e0a8be09675ca508c269b71654294d6a (patch) | |
tree | 23a61a995736d313aa719c9b36033749991207b1 /src/cowboy_compress_h.erl | |
parent | 1a9e62ae2a3b4e39c10c87b5841b460ed81124a9 (diff) | |
download | cowboy-5ffb4f98e0a8be09675ca508c269b71654294d6a.tar.gz cowboy-5ffb4f98e0a8be09675ca508c269b71654294d6a.tar.bz2 cowboy-5ffb4f98e0a8be09675ca508c269b71654294d6a.zip |
Make cowboy_compress_h add vary: accept-encoding
Diffstat (limited to 'src/cowboy_compress_h.erl')
-rw-r--r-- | src/cowboy_compress_h.erl | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/src/cowboy_compress_h.erl b/src/cowboy_compress_h.erl index f4ee7b9..e9824ba 100644 --- a/src/cowboy_compress_h.erl +++ b/src/cowboy_compress_h.erl @@ -176,10 +176,10 @@ gzip_response({response, Status, Headers, Body}, State) -> after zlib:close(Z) end, - {{response, Status, Headers#{ + {{response, Status, vary(Headers#{ <<"content-length">> => integer_to_binary(iolist_size(GzBody)), <<"content-encoding">> => <<"gzip">> - }, GzBody}, State}. + }), GzBody}, State}. gzip_headers({headers, Status, Headers0}, State) -> Z = zlib:open(), @@ -187,9 +187,25 @@ gzip_headers({headers, Status, Headers0}, State) -> %% @todo It might be good to allow them to be configured? zlib:deflateInit(Z, default, deflated, 31, 8, default), Headers = maps:remove(<<"content-length">>, Headers0), - {{headers, Status, Headers#{ + {{headers, Status, vary(Headers#{ <<"content-encoding">> => <<"gzip">> - }}, State#state{deflate=Z}}. + })}, State#state{deflate=Z}}. + +%% We must add content-encoding to vary if it's not already there. +vary(Headers=#{<<"vary">> := Vary}) -> + try cow_http_hd:parse_vary(iolist_to_binary(Vary)) of + '*' -> Headers; + List -> + case lists:member(<<"accept-encoding">>, List) of + true -> Headers; + false -> Headers#{<<"vary">> => [Vary, <<", accept-encoding">>]} + end + catch _:_ -> + %% The vary header is invalid. Probably empty. We replace it with ours. + Headers#{<<"vary">> => <<"accept-encoding">>} + end; +vary(Headers) -> + Headers#{<<"vary">> => <<"accept-encoding">>}. %% It is not possible to combine zlib and the sendfile %% syscall as far as I can tell, because the zlib format |