Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.3k
Fix Content-Lenght calculation of unicode string#1484
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
cacb452a60a89eb874f4970f688c9b27b7bcfe9a22d091fe5258087e6333de1b3a1f45File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -3,7 +3,7 @@ | ||
| import typing | ||
| from pathlib import Path | ||
| from ._types import FileContent, FileTypes, RequestFiles | ||
| from ._types import FileTypes, RequestFiles | ||
| from ._utils import ( | ||
| format_form_param, | ||
| guess_content_type, | ||
| @@ -40,11 +40,7 @@ def render_headers(self) -> bytes: | ||
| def render_data(self) -> bytes: | ||
| if not hasattr(self, "_data"): | ||
| self._data = ( | ||
| self.value | ||
| if isinstance(self.value, bytes) | ||
| else self.value.encode("utf-8") | ||
| ) | ||
| self._data = to_bytes(self.value) | ||
| return self._data | ||
| @@ -66,8 +62,6 @@ class FileField: | ||
| def __init__(self, name: str, value: FileTypes) -> None: | ||
| self.name = name | ||
| fileobj: FileContent | ||
Member 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. I'd suggest we don't change this. | ||
| if isinstance(value, tuple): | ||
| try: | ||
| filename, fileobj, content_type = value # type: ignore | ||
| @@ -79,6 +73,12 @@ def __init__(self, name: str, value: FileTypes) -> None: | ||
| fileobj = value | ||
| content_type = guess_content_type(filename) | ||
| if isinstance(fileobj, str): | ||
| # Ensure we only deal with bytes to prevent any content-length | ||
| # mismatch due to str -> bytes encoding. | ||
| # See: https://github.com/encode/httpx/issues/1482 | ||
| fileobj = to_bytes(fileobj) | ||
Member 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. Or add this. | ||
| self.filename = filename | ||
| self.file = fileobj | ||
| self.content_type = content_type | ||
| @@ -87,7 +87,7 @@ def __init__(self, name: str, value: FileTypes) -> None: | ||
| def get_length(self) -> int: | ||
| headers = self.render_headers() | ||
| if isinstance(self.file, (str, bytes)): | ||
Member 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. The low-footprint change here is presumably to keep this check the same, but use the following...
Right? | ||
| if isinstance(self.file, bytes): | ||
| return len(headers) + len(self.file) | ||
florimondmanca marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| # Let's do our best not to read `file` into memory. | ||
| @@ -119,8 +119,8 @@ def render_headers(self) -> bytes: | ||
| return self._headers | ||
| def render_data(self) -> typing.Iterator[bytes]: | ||
| if isinstance(self.file, (str, bytes)): | ||
| yield to_bytes(self.file) | ||
Member 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. We shouldn't need to change this. | ||
| if isinstance(self.file, bytes): | ||
| yield self.file | ||
| return | ||
| if hasattr(self, "_data"): | ||
Uh oh!
There was an error while loading. Please reload this page.