Skip to content

Bound webpage response reads before text decoding - #378

Merged
Eigenwise merged 1 commit into
Eigenwise:mainfrom
Afloat16:fix/bounded-webpage-download-20261113
Nov 17, 2026
Merged

Eigenwise merged 1 commit into
Eigenwise:mainfrom
Afloat16:fix/bounded-webpage-download-20261113

Conversation

@Afloat16

@Afloat16 Afloat16 commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Problem

max_content_length is 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 consume Response.iter_content with stream=True, check the accumulated byte budget before appending each chunk, and close the response in finally on success, HTTP errors, size rejection and read failures.

Decode the bounded byte buffer directly with the existing UnicodeDammit precedence: BOM, explicit HTTP charset, HTML declaration and detection. No Requests private attributes are accessed or populated, and no second read through response.content or response.text is 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

  • On unchanged current main, 24 new streaming/lifecycle cases fail while 141 existing controls pass.
  • After the change, all 165 webpage scraper tests pass, including the newly merged encoding, redirected-link, article-heading and content-candidate coverage.
  • The shared matrix uses real Requests response objects with observable streams and actual urllib3 gzip decompression. It checks early stop for a 1 MiB response with a 1 KiB budget and absent/understated/overstated 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.
  • Existing tests use local HTTP servers to exercise decoding and redirects end to end through the scraper. No external service is required.
  • Black, Flake8 and git diff --check pass 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.

@Eigenwise Eigenwise left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Afloat16
Afloat16 force-pushed the fix/bounded-webpage-download-20261113 branch from ac4e87c to 213a405 Compare November 17, 2026 12:57

Copy link
Copy Markdown
Contributor Author

Updated in 213a405a93c3 on current main de11c6a.

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 Response._content or other private attributes. The final URL from #412 and the newly merged content-extraction behavior are preserved.

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.

@Eigenwise
Eigenwise merged commit 5d77e01 into Eigenwise:main Nov 17, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants