Fixes #6080
Some checks are pending
/ build (macos-latest, 3.8) (push) Waiting to run
/ build (ubuntu-latest, pypy-2.7) (push) Waiting to run
/ build (windows-latest, 3.14) (push) Waiting to run

This commit is contained in:
Miroslav Štampar 2026-07-09 09:02:25 +02:00
parent 5893f069ae
commit c1516f1750
2 changed files with 10 additions and 4 deletions

View file

@ -82,14 +82,20 @@ class SmartRedirectHandler(_urllib.request.HTTPRedirectHandler):
redurl = self._get_header_redirect(headers) if not conf.ignoreRedirects else None
try:
content = fp.fp.read(MAX_CONNECTION_TOTAL_SIZE)
fp.fp = io.BytesIO(content)
# Note: drain via the length-aware response (honors Content-Length/chunked), NOT the raw
# socket 'fp.fp' - the latter has no HTTP framing, so on a Keep-Alive connection (no EOF)
# it blocks for the whole '--timeout' on every redirect (Issue #6080)
content = fp.read(MAX_CONNECTION_TOTAL_SIZE)
except _http_client.IncompleteRead as ex:
content = ex.partial
fp.fp = io.BytesIO(content)
except:
content = b""
# back 'fp' with the captured body so it stays re-readable downstream (Issue #5985); the
# length-aware read above has consumed the response, so restore both the buffer and read()
fp.fp = io.BytesIO(content)
fp.read = fp.fp.read
content = decodePage(content, headers.get(HTTP_HEADER.CONTENT_ENCODING), headers.get(HTTP_HEADER.CONTENT_TYPE))
threadData = getCurrentThreadData()