From 5ffb4f98e0a8be09675ca508c269b71654294d6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Fri, 4 Oct 2019 13:32:35 +0200 Subject: Make cowboy_compress_h add vary: accept-encoding --- src/cowboy_compress_h.erl | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'src') 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 -- cgit v1.2.3