Skip to content
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

Fix Content-Lenght calculation of unicode string #1484

Closed
wants to merge 10 commits into from

Conversation

j178
Copy link
Member

@j178 j178 commented Feb 24, 2021

Fixes #1482

Copy link
Member

@florimondmanca florimondmanca left a comment

Choose a reason for hiding this comment

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

Looking very nice, just a few suggestions to make this tighter?

httpx/_multipart.py Outdated Show resolved Hide resolved
httpx/_multipart.py Show resolved Hide resolved
httpx/_multipart.py Outdated Show resolved Hide resolved
@tomchristie
Copy link
Member

Looks fiddly, just flagging up that I'd also like to be involved in any review on this. 😇

@tomchristie
Copy link
Member

Great, thanks for this, good work!

I'd like to see the change footprint reduced on it slightly. I don't think we really need to change _types.py at all, and I think we can reduce the number of changes in _multipart.py. I'll leave a couple of inline suggestions there.

@@ -66,8 +62,6 @@ class FileField:
def __init__(self, name: str, value: FileTypes) -> None:
self.name = name

fileobj: FileContent

Copy link
Member

Choose a reason for hiding this comment

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

I'd suggest we don't change this.

# mismatch due to str -> bytes encoding.
# See: https://github.com/encode/httpx/issues/1482
fileobj = to_bytes(fileobj)

Copy link
Member

Choose a reason for hiding this comment

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

Or add this.

@@ -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)):
Copy link
Member

Choose a reason for hiding this comment

The 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...

return len(headers) + len(to_bytes(self.file))

Right?

@@ -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)
Copy link
Member

Choose a reason for hiding this comment

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

We shouldn't need to change this.

@j178 j178 closed this Mar 25, 2021
@j178 j178 deleted the jh/multipart-unicode branch March 25, 2021 15:30
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.

Too much data for declared Content-Length when passing string with non-ascii characters via files parameter
3 participants