-
Notifications
You must be signed in to change notification settings - Fork 4k
skip Content-Length size check when Content-Encoding indicates compression #1883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -216,11 +216,21 @@ def start( | |
| """ | ||
| assert not self.status.time_started | ||
|
|
||
| # FIXME: some servers still might sent Content-Encoding: gzip | ||
| # <https://github.com/httpie/cli/issues/423> | ||
| try: | ||
| total_size = int(final_response.headers['Content-Length']) | ||
| except (KeyError, ValueError, TypeError): | ||
| # If the server applied a content coding (e.g. ``gzip``), then | ||
| # ``requests`` auto-decompresses the body in ``iter_content`` while | ||
| # ``Content-Length`` still reflects the *encoded* size per | ||
| # RFC 9110 §8.6. Comparing those two numbers would always mark the | ||
| # download as incomplete, so skip the size tracking in that case. | ||
| # See <https://github.com/httpie/cli/issues/423>. | ||
| content_encoding = ( | ||
| final_response.headers.get('Content-Encoding') or 'identity' | ||
| ).strip().lower() | ||
| if content_encoding == 'identity': | ||
| try: | ||
| total_size = int(final_response.headers['Content-Length']) | ||
| except (KeyError, ValueError, TypeError): | ||
| total_size = None | ||
| else: | ||
| total_size = None | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: The tests confirm a complete compressed download is no longer flagged as incomplete. Is there a test (or manual check) confirming a genuinely truncated compressed download still gets caught somehow, e.g. via a decompression error bubbling up from |
||
|
|
||
| if not self._output_file: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fix assumes that whenever Content-Encoding is non-identity, requests has decompressed the body in
iter_content(). That's true for gzip/deflate (built into urllib3), but br (Brotli) only gets decoded if the optional brotli/brotlicffi package is installed. If it isn't, requests passes the body through unmodified, Content-Length does match the byte count, and this change now silently disables truncation detection for a case where the old (broken) comparison would've actually been correct. Could you check whether decompression actually happened — e.g. by comparinglen(response.raw._fp.read())behavior, or checkingresponse.raw.headersafter the fact, rather than inferring it purely from the request header? At minimum, worth a code comment noting the assumption and which encodings it holds for in this codebase's supported environment