From 3893f26909f3322995b0262b74ad2c3338f33913 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Br=C3=A9nainn=20Woodsend?= Date: Fri, 29 Sep 2023 01:29:41 +0100 Subject: [PATCH] Fix mirroring HTML index page sent with chunked transfer encoding. --- polycotylus/_mirror.py | 2 +- tests/test_mirror.py | 35 +++++++++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/polycotylus/_mirror.py b/polycotylus/_mirror.py index 1a68a5b..c7cd032 100644 --- a/polycotylus/_mirror.py +++ b/polycotylus/_mirror.py @@ -245,7 +245,7 @@ def do_GET(self): # Forward any header web browsers needs to interpret the # potentially compressed HTML response. for header in ["Content-Encoding", "Content-Type", - "Content-Length", "Transfer-Encoding"]: + "Content-Length"]: if value := self.upstream.headers[header]: self.send_header(header, value) self.end_headers() diff --git a/tests/test_mirror.py b/tests/test_mirror.py index d004202..7985a4c 100644 --- a/tests/test_mirror.py +++ b/tests/test_mirror.py @@ -143,8 +143,6 @@ def test_head(tmp_path): urlopen(Request(url + "cake", method="HEAD")).close() with urlopen(Request("http://localhost:9989", method="HEAD")) as response: - if not response.headers["Transfer-Encoding"] == "chunked": - assert int(response.headers["Content-Length"]) assert not response.read() @@ -175,10 +173,35 @@ def test_index_page_handling(tmp_path): with self: with urlopen(Request("http://localhost:9989", headers={"Accept-Encoding": "gzip"})) as response: - if response.headers["Transfer-Encoding"] == "chunked": - return - content = gzip.decompress(response.read()) - assert b"core" in content + if response.headers["Transfer-Encoding"] != "chunked": + content = gzip.decompress(response.read()) + assert b"core" in content + + self._base_url = "http://localhost:8899" + + def respond_chunked(self): + self.send_response(HTTPStatus.OK) + self.send_header("Transfer-Encoding", "chunked") + self.send_header("Content-Type", "text/html") + self.end_headers() + self.wfile.write(b"6\r\nhello \r\n5\r\nworld\r\n0\r\n\r\n") + + def respond_compressed(self): + payload = gzip.compress(b"hello world") + self.send_response(HTTPStatus.OK) + self.send_header("Transfer-Encoding", "gzip") + self.send_header("Content-Length", "text/html") + self.send_header("Content-Type", str(len(payload))) + self.end_headers() + self.wfile.write(payload) + + for upstream in (respond_chunked, respond_compressed): + with fake_upstream(respond_chunked): + with self: + with urlopen(Request("http://localhost:9989", + headers={"Accept-Encoding": "gzip,deflate"})) as response: + assert response.read() == b"hello world" + def test_concurrent(tmp_path):