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

Support for chunk_size #1277

Merged
merged 9 commits into from Nov 25, 2020
Merged

Support for chunk_size #1277

merged 9 commits into from Nov 25, 2020

Conversation

tomchristie
Copy link
Member

@tomchristie tomchristie commented Sep 10, 2020

Closes #394

  • Add iter_raw(chunk_size: int=None)
  • Add iter_bytes(chunk_size: int=None)
  • Add iter_text(chunk_size: int=None)

First bit of work towards chunk_size on the response.iter_[raw|bytes|text] methods,
using a decoder class in line with the other decoding.

Nice thing about this is that it's really easy to unit test, eg...

def test_byte_chunker():
    decoder = ByteChunker()
    assert decoder.decode(b"1234567") == [b"1234567"]
    assert decoder.decode(b"89") == [b"89"]
    assert decoder.flush() == []

    decoder = ByteChunker(chunk_size=3)
    assert decoder.decode(b"1234567") == [b"123", b"456"]
    assert decoder.decode(b"89") == [b"789"]
    assert decoder.flush() == []

    decoder = ByteChunker(chunk_size=3)
    assert decoder.decode(b"123456") == [b"123", b"456"]
    assert decoder.decode(b"789") == [b"789"]
    assert decoder.flush() == []

    decoder = ByteChunker(chunk_size=3)
    assert decoder.decode(b"123456") == [b"123", b"456"]
    assert decoder.decode(b"78") == []
    assert decoder.flush() == [b"78"]

@@ -912,19 +913,28 @@ def read(self) -> bytes:
self._content = b"".join(self.iter_bytes())
return self._content

def iter_bytes(self) -> typing.Iterator[bytes]:
def iter_bytes(self, chunk_size: int = None) -> typing.Iterator[bytes]:
Copy link
Member

Choose a reason for hiding this comment

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

Are you sure about chunk_size with default None?

I do agree that it looks more suitable, but requests provides us with defaults chunk_size=1 or 512

Copy link
Member

Choose a reason for hiding this comment

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

In this PR, when chunk_size=None we just return the input content unchanged, as one single big chunk.

Yes, this would deviate from what Requests seems to do, but:

  • Setting a non-None default would break backward compatibility on our side.
  • Defaulting to "transparently pass the chunk sent by the server" is probably the most reasonable approach anyway.

That said, we'd need to add this deviation from Requests to the compatibility guide. 👍

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.

Superb!

There may be room for factoring in some of the introduced logic within the _decoders.py module (bunch of repetition there), but happy to treat it as a follow-up.

Edit: so as per #1277 (comment), should we add a small note in our Requests compatibility guide on the default chunking behavior?

@@ -912,19 +913,28 @@ def read(self) -> bytes:
self._content = b"".join(self.iter_bytes())
return self._content

def iter_bytes(self) -> typing.Iterator[bytes]:
def iter_bytes(self, chunk_size: int = None) -> typing.Iterator[bytes]:
Copy link
Member

Choose a reason for hiding this comment

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

In this PR, when chunk_size=None we just return the input content unchanged, as one single big chunk.

Yes, this would deviate from what Requests seems to do, but:

  • Setting a non-None default would break backward compatibility on our side.
  • Defaulting to "transparently pass the chunk sent by the server" is probably the most reasonable approach anyway.

That said, we'd need to add this deviation from Requests to the compatibility guide. 👍

@simonw
Copy link
Contributor

simonw commented Nov 16, 2020

I really like this - it's a great answer to my question in #1392 about safely retrieving a truncated version of a resource when dealing with a URL provided by an untrusted user, as an extra protection against denial-of-service on top of the HTTPX timeouts.

@florimondmanca
Copy link
Member

And if I'm correct, this would close #394?

@tomchristie tomchristie merged commit 27df5e4 into master Nov 25, 2020
@tomchristie tomchristie deleted the chunk-size branch November 25, 2020 15:28
@piersoh
Copy link

piersoh commented Nov 25, 2020

Just clarify when it says "chunk_size=None we just return the input content unchanged, as one single big chunk" - presumably that means it returns each HTTP chunk (which may be big or small) as opposed to the whole response? How does this work for HTTP/2?

@tomchristie
Copy link
Member Author

@piersoh Correct yes. For HTTP/2 that'll mean chunk sizes that correspond to the data frames on the wire.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow setting chunk_size for Response.iter_bytes() etc...
5 participants