Skip to content

Commit 872c475

Browse files
committed
Prefer zstd when available
Originally we would default to the first encoding found. Now we apply our own weighting. A typical browser header for accept encoding is `gzip, deflate, br, zstd` for reference.
1 parent d8de429 commit 872c475

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

lib/bandit/compression.ex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@ defmodule Bandit.Compression do
1010
lib_context: term()
1111
}
1212

13-
@accepted_content_encodings ~w(deflate gzip x-gzip)
13+
# Order of importance
14+
@accepted_content_encodings ~w(gzip x-gzip deflate)
1415

1516
if Code.ensure_loaded?(:zstd) do
16-
@accepted_content_encodings @accepted_content_encodings ++ ~w(zstd)
17+
@accepted_content_encodings ["zstd" | @accepted_content_encodings]
1718
end
1819

1920
@spec negotiate_content_encoding(nil | binary(), boolean()) :: String.t() | nil
2021
def negotiate_content_encoding(nil, _), do: nil
2122
def negotiate_content_encoding(_, false), do: nil
2223

2324
def negotiate_content_encoding(accept_encoding, true) do
24-
accept_encoding
25-
|> Plug.Conn.Utils.list()
26-
|> Enum.find(&(&1 in @accepted_content_encodings))
25+
encodings = Plug.Conn.Utils.list(accept_encoding)
26+
Enum.find(@accepted_content_encodings, &(&1 in encodings))
2727
end
2828

2929
def new(adapter, status, headers, empty_body?, streamable \\ false) do

0 commit comments

Comments
 (0)