Skip to content

Commit

Permalink
Refactor to keep credo happy
Browse files Browse the repository at this point in the history
  • Loading branch information
mtrudel committed Nov 19, 2024
1 parent 288afc3 commit d8ffbb5
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions lib/bandit/compression.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,12 @@ defmodule Bandit.Compression do
def new(adapter, status, headers, empty_body?, streamable \\ false) do
response_content_encoding_header = Bandit.Headers.get_header(headers, "content-encoding")

response_has_strong_etag =
case Bandit.Headers.get_header(headers, "etag") do
nil -> false
"\W" <> _rest -> false
_strong_etag -> true
end

response_indicates_no_transform =
case Bandit.Headers.get_header(headers, "cache-control") do
nil -> false
header -> "no-transform" in Plug.Conn.Utils.list(header)
end

headers =
if status != 204 && Keyword.get(adapter.opts.http, :compress, true),
do: [{"vary", "accept-encoding"} | headers],
else: headers
headers = maybe_add_vary_header(adapter, status, headers)

if status not in [204, 304] && not is_nil(adapter.content_encoding) &&
is_nil(response_content_encoding_header) &&
!response_has_strong_etag && !response_indicates_no_transform && !empty_body? do
!response_has_strong_etag(headers) && !response_indicates_no_transform(headers) &&
!empty_body? do
deflate_options = Keyword.get(adapter.opts.http, :deflate_options, [])

case start_stream(adapter.content_encoding, deflate_options, streamable) do
Expand All @@ -55,6 +40,27 @@ defmodule Bandit.Compression do
end
end

defp maybe_add_vary_header(adapter, status, headers) do
if status != 204 && Keyword.get(adapter.opts.http, :compress, true),
do: [{"vary", "accept-encoding"} | headers],
else: headers
end

defp response_has_strong_etag(headers) do
case Bandit.Headers.get_header(headers, "etag") do
nil -> false
"\W" <> _rest -> false
_strong_etag -> true
end
end

defp response_indicates_no_transform(headers) do
case Bandit.Headers.get_header(headers, "cache-control") do
nil -> false
header -> "no-transform" in Plug.Conn.Utils.list(header)
end
end

defp start_stream("deflate", opts, _streamable) do
deflate_context = :zlib.open()

Expand Down

0 comments on commit d8ffbb5

Please sign in to comment.