From e19d89f6c6eb1dd21a373c192f50afa6026fdbac Mon Sep 17 00:00:00 2001 From: Wojtek Mach Date: Sat, 12 Aug 2023 00:06:32 +0200 Subject: [PATCH] Remove support for deflate compression Closes #215 Per https://zlib.net/zlib_faq.html#faq39 > What's the difference between the "gzip" and "deflate" HTTP 1.1 encodings? > > "gzip" is the gzip format, and "deflate" is the zlib format. They > should probably have called the second one "zlib" instead to avoid > confusion with the raw deflate compressed data format. While the HTTP > 1.1 RFC 2616 correctly points to the zlib specification in RFC 1950 for > the "deflate" transfer encoding, there have been reports of servers and > browsers that incorrectly produce or expect raw deflate data per the > deflate specification in RFC 1951, most notably Microsoft. So even > though the "deflate" transfer encoding using the zlib format would be > the more efficient approach (and in fact exactly what the zlib format > was designed for), using the "gzip" transfer encoding is probably more > reliable due to an unfortunate choice of name on the part of the HTTP > 1.1 authors. > > Bottom line: use the gzip format for HTTP 1.1 encoding. --- README.md | 2 +- lib/req/request.ex | 4 ++-- lib/req/steps.ex | 8 +------- test/req/request_test.exs | 2 +- test/req/steps_test.exs | 8 ++++---- 5 files changed, 9 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 2a7e4f06..55f50446 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Req is a batteries-included HTTP client for Elixir. * Extensibility via request, response, and error steps. - * Request body compression and automatic response body decompression (via [`compress_body`], [`compressed`], and [`decompress_body`] steps). Supports gzip, deflate, brotli and zstd decompression. + * Request body compression and automatic response body decompression (via [`compress_body`], [`compressed`], and [`decompress_body`] steps). Supports gzip, brotli, and zstd decompression. * Request body encoding and automatic response body decoding (via [`encode_body`] and [`decode_body`] steps.) diff --git a/lib/req/request.ex b/lib/req/request.ex index d91966cd..ac75906a 100644 --- a/lib/req/request.ex +++ b/lib/req/request.ex @@ -218,7 +218,7 @@ defmodule Req.Request do Req.get!(req, url: "https://httpbin.org/json", print_headers: true).status # Outputs: - # > accept-encoding: br, gzip, deflate + # > accept-encoding: br, gzip # > user-agent: req/0.3.0-dev # < date: Wed, 11 May 2022 11:10:47 GMT # < content-type: application/json @@ -228,7 +228,7 @@ defmodule Req.Request do req = Req.new() |> PrintHeaders.attach(print_headers: true) Req.get!(req, url: "https://httpbin.org/json").status # Outputs: - # > accept-encoding: br, gzip, deflate + # > accept-encoding: br, gzip # ... 200 diff --git a/lib/req/steps.ex b/lib/req/steps.ex index 62d64c64..6a78bf6d 100644 --- a/lib/req/steps.ex +++ b/lib/req/steps.ex @@ -190,7 +190,6 @@ defmodule Req.Steps do Supported formats: * `gzip` - * `deflate` * `br` (if [brotli] is installed) * `zstd` (if [ezstd] is installed) @@ -272,7 +271,7 @@ defmodule Req.Steps do end defp supported_accept_encoding do - value = "gzip, deflate" + value = "gzip" value = if brotli_loaded?(), do: "br, " <> value, else: value if ezstd_loaded?(), do: "zstd, " <> value, else: value end @@ -812,7 +811,6 @@ defmodule Req.Steps do | Format | Decoder | | ------------- | ----------------------------------------------- | | gzip, x-gzip | `:zlib.gunzip/1` | - | deflate | `:zlib.unzip/1` | | br | `:brotli.decode/1` (if [brotli] is installed) | | zstd | `:ezstd.decompress/1` (if [ezstd] is installed) | | identity | Returns data as is | @@ -868,10 +866,6 @@ defmodule Req.Steps do :zlib.gunzip(body) end - defp decompress_with_algorithm("deflate", body) do - :zlib.unzip(body) - end - defp decompress_with_algorithm("br", body) do if brotli_loaded?() do {:ok, decompressed} = :brotli.decode(body) diff --git a/test/req/request_test.exs b/test/req/request_test.exs index 0d5db0ac..be0344be 100644 --- a/test/req/request_test.exs +++ b/test/req/request_test.exs @@ -244,7 +244,7 @@ defmodule Req.RequestTest do assert [ {"user-agent", "req/" <> _}, - {"accept-encoding", "zstd, br, gzip, deflate"}, + {"accept-encoding", "zstd, br, gzip"}, {"authorization", ^authorization} ] = request.headers end diff --git a/test/req/steps_test.exs b/test/req/steps_test.exs index 8c459d25..502f20f9 100644 --- a/test/req/steps_test.exs +++ b/test/req/steps_test.exs @@ -242,8 +242,8 @@ defmodule Req.StepsTest do test "multiple codecs", c do Bypass.expect(c.bypass, "GET", "/", fn conn -> conn - |> Plug.Conn.put_resp_header("content-encoding", "gzip, deflate") - |> Plug.Conn.send_resp(200, "foo" |> :zlib.gzip() |> :zlib.zip()) + |> Plug.Conn.put_resp_header("content-encoding", "gzip, zstd") + |> Plug.Conn.send_resp(200, "foo" |> :zlib.gzip() |> :ezstd.compress()) end) assert Req.get!(c.url).body == "foo" @@ -257,12 +257,12 @@ defmodule Req.StepsTest do {:ok, socket} = :gen_tcp.accept(listen_socket) assert {:ok, "GET / HTTP/1.1\r\n" <> _} = :gen_tcp.recv(socket, 0) - body = "foo" |> :zlib.gzip() |> :zlib.zip() + body = "foo" |> :zlib.gzip() |> :ezstd.compress() data = """ HTTP/1.1 200 OK content-encoding: gzip - content-encoding: deflate + content-encoding: zstd content-length: #{byte_size(body)} #{body}