From 29eba22548bc00d46d4ba0126a6bcd49e4a8a0b1 Mon Sep 17 00:00:00 2001 From: Wojtek Mach Date: Fri, 25 Aug 2023 20:28:58 +0200 Subject: [PATCH] Revert unrelated changes --- lib/req/steps.ex | 56 +++++++++++++++++++++++++---------------- test/req/steps_test.exs | 21 ++++++++-------- 2 files changed, 44 insertions(+), 33 deletions(-) diff --git a/lib/req/steps.ex b/lib/req/steps.ex index c6b6d978..0d3ae33e 100644 --- a/lib/req/steps.ex +++ b/lib/req/steps.ex @@ -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 @@ -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 @@ -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 diff --git a/test/req/steps_test.exs b/test/req/steps_test.exs index 8712c704..625d3f7b 100644 --- a/test/req/steps_test.exs +++ b/test/req/steps_test.exs @@ -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