0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-16 13:10:24 +00:00

Make cowboy_compress_h add vary: accept-encoding

This commit is contained in:
Loïc Hoguin 2019-10-04 13:32:35 +02:00
parent 1a9e62ae2a
commit 5ffb4f98e0
No known key found for this signature in database
GPG key ID: 8A9DF795F6FED764
3 changed files with 79 additions and 4 deletions

View file

@ -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