Skip to content

Commit

Permalink
Fix mirroring HTML index page sent with chunked transfer encoding.
Browse files Browse the repository at this point in the history
  • Loading branch information
bwoodsend committed Sep 29, 2023
1 parent 64bc25c commit 335208f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion polycotylus/_mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
33 changes: 29 additions & 4 deletions tests/test_mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,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):
Expand Down

0 comments on commit 335208f

Please sign in to comment.