Skip to content

Commit

Permalink
Treat x-gzip content encoding as gzip
Browse files Browse the repository at this point in the history
According to RFC 9110, the "x-gzip" content coding should be treated as
"gzip".

Fixes #3174
  • Loading branch information
jeremycline authored and sethmlarson committed Nov 4, 2023
1 parent ff764a0 commit 5fc48e7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog/3174.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed decoding Gzip-encoded responses which specified ``x-gzip`` content-encoding.
6 changes: 4 additions & 2 deletions src/urllib3/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@ def _get_decoder(mode: str) -> ContentDecoder:
if "," in mode:
return MultiDecoder(mode)

if mode == "gzip":
# According to RFC 9110 section 8.4.1.3, recipients should
# consider x-gzip equivalent to gzip
if mode in ("gzip", "x-gzip"):
return GzipDecoder()

if brotli is not None and mode == "br":
Expand Down Expand Up @@ -280,7 +282,7 @@ def get(self, n: int) -> bytes:


class BaseHTTPResponse(io.IOBase):
CONTENT_DECODERS = ["gzip", "deflate"]
CONTENT_DECODERS = ["gzip", "x-gzip", "deflate"]
if brotli is not None:
CONTENT_DECODERS += ["br"]
if zstd is not None:
Expand Down
5 changes: 3 additions & 2 deletions test/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,15 @@ def test_chunked_decoding_deflate2(self) -> None:
assert r.read() == b""
assert r.read() == b""

def test_chunked_decoding_gzip(self) -> None:
@pytest.mark.parametrize("content_encoding", ["gzip", "x-gzip"])
def test_chunked_decoding_gzip(self, content_encoding: str) -> None:
compress = zlib.compressobj(6, zlib.DEFLATED, 16 + zlib.MAX_WBITS)
data = compress.compress(b"foo")
data += compress.flush()

fp = BytesIO(data)
r = HTTPResponse(
fp, headers={"content-encoding": "gzip"}, preload_content=False
fp, headers={"content-encoding": content_encoding}, preload_content=False
)

assert r.read(1) == b"f"
Expand Down

0 comments on commit 5fc48e7

Please sign in to comment.