Skip to content

Commit 5fc48e7

Browse files
jeremyclinesethmlarson
authored andcommitted
Treat x-gzip content encoding as gzip
According to RFC 9110, the "x-gzip" content coding should be treated as "gzip". Fixes #3174
1 parent ff764a0 commit 5fc48e7

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

changelog/3174.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed decoding Gzip-encoded responses which specified ``x-gzip`` content-encoding.

src/urllib3/response.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,9 @@ def _get_decoder(mode: str) -> ContentDecoder:
208208
if "," in mode:
209209
return MultiDecoder(mode)
210210

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

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

281283

282284
class BaseHTTPResponse(io.IOBase):
283-
CONTENT_DECODERS = ["gzip", "deflate"]
285+
CONTENT_DECODERS = ["gzip", "x-gzip", "deflate"]
284286
if brotli is not None:
285287
CONTENT_DECODERS += ["br"]
286288
if zstd is not None:

test/test_response.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,14 +229,15 @@ def test_chunked_decoding_deflate2(self) -> None:
229229
assert r.read() == b""
230230
assert r.read() == b""
231231

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

237238
fp = BytesIO(data)
238239
r = HTTPResponse(
239-
fp, headers={"content-encoding": "gzip"}, preload_content=False
240+
fp, headers={"content-encoding": content_encoding}, preload_content=False
240241
)
241242

242243
assert r.read(1) == b"f"

0 commit comments

Comments
 (0)