Skip to content

Commit

Permalink
Revert unrelated changes
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmach committed Aug 25, 2023
1 parent 7b0286f commit 29eba22
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 33 deletions.
56 changes: 34 additions & 22 deletions lib/req/steps.ex
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,9 @@ defmodule Req.Steps do
| _other_ | Returns data as is |
This step updates the following headers to reflect the changes:
- `content-length` is set to the length of the decompressed body
* `content-length` is set to the length of the decompressed body
* `content-encoding` is removed
## Options
Expand Down Expand Up @@ -904,49 +906,59 @@ defmodule Req.Steps do
if request.options[:raw] do
{request, response}
else
compression_algorithms = get_content_encoding_header(response.headers)
decompressed_body = decompress_body(response.body, compression_algorithms)
codecs = get_content_encoding_header(response.headers)
{decompressed_body, unknown_codecs} = decompress_body(codecs, response.body, [])
decompressed_content_length = decompressed_body |> byte_size() |> to_string()

response =
%Req.Response{response | body: decompressed_body}
|> Req.Response.put_header("content-length", decompressed_content_length)

response =
if unknown_codecs == [] do
Req.Response.delete_header(response, "content-encoding")
else
Req.Response.put_header(response, "content-encoding", Enum.join(unknown_codecs, ", "))
end

{request, response}
end
end

defp decompress_body(body, algorithms) do
Enum.reduce(algorithms, body, &decompress_with_algorithm(&1, &2))
defp decompress_body([gzip | rest], body, acc) when gzip in ["gzip", "x-gzip"] do
decompress_body(rest, :zlib.gunzip(body), acc)
end

defp decompress_with_algorithm(gzip, body) when gzip in ["gzip", "x-gzip"] do
:zlib.gunzip(body)
end

defp decompress_with_algorithm("br", body) do
defp decompress_body(["br" | rest], body, acc) do
if brotli_loaded?() do
{:ok, decompressed} = :brotli.decode(body)
decompressed
decompress_body(rest, decompressed, acc)
else
raise("`:brotli` decompression library not loaded")
Logger.debug("decompress_body: :brotli library not loaded, skipping brotli decompression")
decompress_body(rest, body, ["br" | acc])
end
end

defp decompress_with_algorithm("zstd", body) do
defp decompress_body(["zstd" | rest], body, acc) do
if ezstd_loaded?() do
:ezstd.decompress(body)
decompress_body(rest, :ezstd.decompress(body), acc)
else
raise("`:ezstd` decompression library not loaded")
Logger.debug("decompress_body: :ezstd library not loaded, skipping zstd decompression")
decompress_body(rest, body, ["zstd" | acc])
end
end

defp decompress_with_algorithm("identity", body) do
body
defp decompress_body(["identity" | rest], body, acc) do
decompress_body(rest, body, acc)
end

defp decompress_body([codec | rest], body, acc) do
Logger.debug("decompress_body: algorithm #{inspect(codec)} is not supported")
decompress_body(rest, body, [codec | acc])
end

defp decompress_with_algorithm(_algorithm, body) do
body
defp decompress_body([], body, acc) do
{body, acc}
end

defmacrop nimble_csv_loaded? do
Expand Down Expand Up @@ -1423,14 +1435,14 @@ defmodule Req.Steps do
end
end

defp apply_retry( fun, request, response_or_exception)
defp apply_retry(fun, request, response_or_exception)

defp apply_retry(fun, _request, response_or_exception) when is_function( fun, 1) do
defp apply_retry(fun, _request, response_or_exception) when is_function(fun, 1) do
IO.warn("`retry: fun/1` has been deprecated in favor of `retry: fun/2`")
fun.(response_or_exception)
end

defp apply_retry(fun, request, response_or_exception) when is_function( fun, 2) do
defp apply_retry(fun, request, response_or_exception) when is_function(fun, 2) do
fun.(request, response_or_exception)
end

Expand Down
21 changes: 10 additions & 11 deletions test/req/steps_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -390,17 +390,16 @@ defmodule Req.StepsTest do
assert String.to_integer(content_length) == byte_size(body)
end

# raises
# test "delete content-encoding header", c do
# Bypass.expect(c.bypass, "GET", "/", fn conn ->
# conn
# |> Plug.Conn.put_resp_header("content-encoding", "x-gzip")
# |> Plug.Conn.send_resp(200, :zlib.gzip("foo"))
# end)
#
# resp = Req.get!(c.url)
# assert [] = Req.Response.get_header(resp, "content-encoding")
# end
test "delete content-encoding header", c do
Bypass.expect(c.bypass, "GET", "/", fn conn ->
conn
|> Plug.Conn.put_resp_header("content-encoding", "x-gzip")
|> Plug.Conn.send_resp(200, :zlib.gzip("foo"))
end)

resp = Req.get!(c.url)
assert [] = Req.Response.get_header(resp, "content-encoding")
end
end

describe "output" do
Expand Down

0 comments on commit 29eba22

Please sign in to comment.