Repository navigation
Bound webpage response reads before text decoding - #378
Conversation
Eigenwise
left a comment
There was a problem hiding this comment.
Capping the download is worth having, but this reaches into Requests' private _content. Please use the public streaming API with a byte budget, and base it on the decoding from #418, which is going in.
Leaving this open for an update. Please also keep to the limit in What gets reviewed before opening new pull requests.
ac4e87c to
213a405
Compare
|
Updated in The response is now consumed only through the public Requests streaming API, with a byte-budget check before each append and unconditional close. Decoding runs directly on that bounded buffer using the logic from #418; there are no writes to All 165 scraper tests pass. The 24 new cases fail against unchanged main, and cover early termination, compressed bodies, encoding boundaries and cleanup on every exit. The PR description and README have been updated to match this implementation. |
Problem
max_content_lengthis checked only after a non-streaming request has downloaded the entire body. An oversized page is fully transferred and retained before rejection.Change
Rebased onto current main
de11c6a, including #418's HTML decoding and #412's final-URL handling. Both the Forge scraper and the deep-research copy now consumeResponse.iter_contentwithstream=True, check the accumulated byte budget before appending each chunk, and close the response infinallyon success, HTTP errors, size rejection and read failures.Decode the bounded byte buffer directly with the existing
UnicodeDammitprecedence: BOM, explicit HTTP charset, HTML declaration and detection. No Requests private attributes are accessed or populated, and no second read throughresponse.contentorresponse.textis needed.The limit still counts bytes after HTTP decompression and before text decoding, independently of
Content-Length. Preserve the final response URL for downstream relative links and images. The README documents these semantics.Validation
Content-Length; exact limits; split UTF-8 characters; declared/detected CP1252; invalid charsets; empty bodies; byte versus character counting; decompression; and cleanup on HTTP/read failures.git diff --checkpass for the changed files. The published tree matches the tested tree.This bounds the scraper's accumulated response body and stops consumption at the first over-budget chunk. It does not claim a hard process-memory ceiling for HTTP/decompression internals.