-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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 'I/O operation on closed file' and 'Form data has been processed already' upon redirect on multipart data #9201
Open
GLGDLY
wants to merge
4
commits into
aio-libs:master
Choose a base branch
from
GLGDLY:master
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.
+338
−51
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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 |
---|---|---|
|
@@ -307,17 +307,39 @@ | |
if hdrs.CONTENT_DISPOSITION not in self.headers: | ||
self.set_content_disposition(disposition, filename=self._filename) | ||
|
||
self._writable = True | ||
self._seekable = True | ||
try: | ||
# It is weird but some IO object dont have `seekable()` method as IOBase object, | ||
# it seems better for us to direct try if the `seek()` and `tell()` is available | ||
# e.g. tarfile.TarFile._Stream | ||
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. This class doesn't appear to be an IOBase, so I'm wondering why it reaches this code. Does it match one of the payload subclasses? |
||
self._value.seek(self._value.tell()) | ||
except (AttributeError, OSError): | ||
self._seekable = False | ||
|
||
if self._seekable: | ||
self._stream_pos = self._value.tell() | ||
else: | ||
self._stream_pos = 0 | ||
|
||
async def write(self, writer: AbstractStreamWriter) -> None: | ||
loop = asyncio.get_event_loop() | ||
try: | ||
if self._seekable: | ||
await loop.run_in_executor(None, self._value.seek, self._stream_pos) | ||
elif not self._writable: | ||
raise RuntimeError( | ||
f'Non-seekable IO payload "{self._value}" is already consumed (possibly due to redirect, consider storing in a seekable IO buffer instead)' | ||
) | ||
chunk = await loop.run_in_executor(None, self._value.read, 2**16) | ||
while chunk: | ||
await writer.write(chunk) | ||
chunk = await loop.run_in_executor(None, self._value.read, 2**16) | ||
while chunk: | ||
await writer.write(chunk) | ||
chunk = await loop.run_in_executor(None, self._value.read, 2**16) | ||
finally: | ||
await loop.run_in_executor(None, self._value.close) | ||
if not self._seekable: | ||
self._writable = False # Non-seekable IO `_value` can only be consumed once | ||
|
||
def decode(self, encoding: str = "utf-8", errors: str = "strict") -> str: | ||
if self._seekable: | ||
self._value.seek(self._stream_pos) | ||
return "".join(r.decode(encoding, errors) for r in self._value.readlines()) | ||
|
||
|
||
|
@@ -354,40 +376,50 @@ | |
@property | ||
def size(self) -> Optional[int]: | ||
try: | ||
return os.fstat(self._value.fileno()).st_size - self._value.tell() | ||
return os.fstat(self._value.fileno()).st_size - self._stream_pos | ||
except OSError: | ||
return None | ||
|
||
def decode(self, encoding: str = "utf-8", errors: str = "strict") -> str: | ||
if self._seekable: | ||
self._value.seek(self._stream_pos) | ||
return self._value.read() | ||
|
||
async def write(self, writer: AbstractStreamWriter) -> None: | ||
loop = asyncio.get_event_loop() | ||
try: | ||
if self._seekable: | ||
await loop.run_in_executor(None, self._value.seek, self._stream_pos) | ||
elif not self._writable: | ||
raise RuntimeError( | ||
f'Non-seekable IO payload "{self._value}" is already consumed (possibly due to redirect, consider storing in a seekable IO buffer instead)' | ||
) | ||
chunk = await loop.run_in_executor(None, self._value.read, 2**16) | ||
while chunk: | ||
data = ( | ||
chunk.encode(encoding=self._encoding) | ||
if self._encoding | ||
else chunk.encode() | ||
) | ||
await writer.write(data) | ||
chunk = await loop.run_in_executor(None, self._value.read, 2**16) | ||
while chunk: | ||
data = ( | ||
chunk.encode(encoding=self._encoding) | ||
if self._encoding | ||
else chunk.encode() | ||
) | ||
await writer.write(data) | ||
chunk = await loop.run_in_executor(None, self._value.read, 2**16) | ||
finally: | ||
await loop.run_in_executor(None, self._value.close) | ||
if not self._seekable: | ||
self._writable = False # Non-seekable IO `_value` can only be consumed once | ||
|
||
|
||
class BytesIOPayload(IOBasePayload): | ||
_value: io.BytesIO | ||
|
||
@property | ||
def size(self) -> int: | ||
position = self._value.tell() | ||
end = self._value.seek(0, os.SEEK_END) | ||
self._value.seek(position) | ||
return end - position | ||
def size(self) -> Optional[int]: | ||
if self._seekable: | ||
end = self._value.seek(0, os.SEEK_END) | ||
self._value.seek(self._stream_pos) | ||
return end - self._stream_pos | ||
return None | ||
|
||
def decode(self, encoding: str = "utf-8", errors: str = "strict") -> str: | ||
if self._seekable: | ||
self._value.seek(self._stream_pos) | ||
return self._value.read().decode(encoding, errors) | ||
|
||
|
||
|
@@ -397,7 +429,7 @@ | |
@property | ||
def size(self) -> Optional[int]: | ||
try: | ||
return os.fstat(self._value.fileno()).st_size - self._value.tell() | ||
return os.fstat(self._value.fileno()).st_size - self._stream_pos | ||
except (OSError, AttributeError): | ||
# data.fileno() is not supported, e.g. | ||
# io.BufferedReader(io.BytesIO(b'data')) | ||
|
@@ -406,6 +438,8 @@ | |
return None | ||
|
||
def decode(self, encoding: str = "utf-8", errors: str = "strict") -> str: | ||
if self._seekable: | ||
self._value.seek(self._stream_pos) | ||
return self._value.read().decode(encoding, errors) | ||
|
||
|
||
|
This file contains 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 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.
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.
This doesn't appear to save much, one call to .clear().