-
Notifications
You must be signed in to change notification settings - Fork 1.1k
requests: Add chunked response body support. #1135
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
Open
pablogventura
wants to merge
9
commits into
micropython:master
Choose a base branch
from
pablogventura:requests-chunked-responses
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+58
−32
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
744678f
requests: Add chunked response body support.
pablogventura 09e9f92
requests: Test chunked response decoding.
pablogventura f968164
requests: Document chunked response support.
pablogventura 4168c73
requests: Merge chunked decoding into BodyStream.
pablogventura aa6561d
requests: Reduce chunked BodyStream bytecode size.
pablogventura 7ca0dce
requests: Unify BodyStream EOF error message.
pablogventura dcd64ab
requests: Shrink chunked BodyStream bytecode further.
pablogventura 1fb0933
requests: Fix ruff format in BodyStream readinto.
pablogventura edbb676
requests: Shrink BodyStream by unifying read via readinto.
pablogventura File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,30 +2,44 @@ | |
|
|
||
|
|
||
| class BodyStream: | ||
| def __init__(self, sock, remaining): | ||
| def __init__(self, sock, remaining=0): | ||
| self._sock = sock | ||
| self._chunked = remaining < 0 | ||
| self._remaining = remaining | ||
|
|
||
| def read(self, n=-1): | ||
| if self._remaining == 0: | ||
| return b"" | ||
| if n < 0 or n > self._remaining: | ||
| n = self._remaining | ||
| data = self._sock.read(n) | ||
| self._remaining -= len(data) | ||
| if not data: | ||
| raise ValueError("Connection closed before Content-Length satisfied") | ||
| return data | ||
| buf = bytearray(n if n >= 0 else 256) | ||
| if n >= 0: | ||
| got = self.readinto(buf) | ||
| return buf[:got] if got else b"" | ||
| result = b"" | ||
| while 1: | ||
| got = self.readinto(buf) | ||
| if not got: | ||
| return result | ||
| result += buf[:got] | ||
|
|
||
| def readinto(self, buf): | ||
| if self._remaining == 0: | ||
| return 0 | ||
| s = self._sock | ||
| if self._remaining <= 0: | ||
| if self._remaining == 0: | ||
| return 0 | ||
| self._remaining = int(s.readline().split(b";")[0], 16) | ||
| if self._remaining == 0: | ||
| while 1: | ||
|
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. suggest |
||
| l = s.readline() | ||
| if not l or l == b"\r\n": | ||
| break | ||
|
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. can this be replaced with the final |
||
| return 0 | ||
| if len(buf) > self._remaining: | ||
| buf = memoryview(buf)[: self._remaining] | ||
| got = self._sock.readinto(buf) | ||
| self._remaining -= got | ||
| got = s.readinto(buf) | ||
| if not got: | ||
| raise ValueError("Connection closed before Content-Length satisfied") | ||
| raise ValueError("Connection closed before body complete") | ||
| self._remaining -= got | ||
| if self._remaining == 0 and self._chunked: | ||
| s.readline() | ||
| self._remaining = -1 | ||
| return got | ||
|
|
||
| def close(self): | ||
|
|
@@ -196,14 +210,15 @@ def request( | |
| if len(l) > 2: | ||
| reason = l[2].rstrip() | ||
| remaining = None | ||
| chunked = False | ||
| while True: | ||
| l = s.readline() | ||
| if not l or l == b"\r\n": | ||
| break | ||
| # print(l) | ||
| if l.startswith(b"Transfer-Encoding:"): | ||
| if b"chunked" in l: | ||
| raise ValueError("Unsupported " + str(l, "utf-8")) | ||
| chunked = True | ||
| elif l.startswith(b"Location:") and not 200 <= status <= 299: | ||
| if status in [301, 302, 303, 307, 308]: | ||
| redirect = str(l[10:-2], "utf-8") | ||
|
|
@@ -235,7 +250,9 @@ def request( | |
| else: | ||
| return request(method, redirect, data, json, headers, stream) | ||
| else: | ||
| if remaining is not None: | ||
| if chunked: | ||
| resp = Response(BodyStream(s, -1)) | ||
| elif remaining is not None: | ||
| resp = Response(BodyStream(s, remaining)) | ||
| else: | ||
| resp = Response(s) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
suggest using
while True:for readability (should be same bytecode size)