-
-
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
Improve typings for multipart #3905
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Improve typing annotations for multipart.py along with changes required | ||
by mypy in files that references multipart.py. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,7 @@ | |
sentinel, | ||
) | ||
from .http_parser import RawRequestMessage | ||
from .multipart import MultipartReader | ||
from .multipart import BodyPartReader, MultipartReader | ||
from .streams import EmptyStreamReader, StreamReader | ||
from .typedefs import ( | ||
DEFAULT_JSON_DECODER, | ||
|
@@ -633,41 +633,49 @@ async def post(self) -> 'MultiDictProxy[Union[str, bytes, FileField]]': | |
field = await multipart.next() | ||
while field is not None: | ||
size = 0 | ||
content_type = field.headers.get(hdrs.CONTENT_TYPE) | ||
|
||
if field.filename: | ||
# store file in temp file | ||
tmp = tempfile.TemporaryFile() | ||
chunk = await field.read_chunk(size=2**16) | ||
while chunk: | ||
chunk = field.decode(chunk) | ||
tmp.write(chunk) | ||
size += len(chunk) | ||
field_ct = field.headers.get(hdrs.CONTENT_TYPE) | ||
|
||
if isinstance(field, BodyPartReader): | ||
if field.filename and field_ct: | ||
# store file in temp file | ||
tmp = tempfile.TemporaryFile() | ||
chunk = await field.read_chunk(size=2**16) | ||
while chunk: | ||
chunk = field.decode(chunk) | ||
tmp.write(chunk) | ||
size += len(chunk) | ||
if 0 < max_size < size: | ||
raise HTTPRequestEntityTooLarge( | ||
max_size=max_size, | ||
actual_size=size | ||
) | ||
chunk = await field.read_chunk(size=2**16) | ||
tmp.seek(0) | ||
|
||
ff = FileField(field.name, field.filename, | ||
cast(io.BufferedReader, tmp), | ||
field_ct, field.headers) | ||
out.add(field.name, ff) | ||
else: | ||
# deal with ordinary data | ||
value = await field.read(decode=True) | ||
if field_ct is None or \ | ||
field_ct.startswith('text/'): | ||
charset = field.get_charset(default='utf-8') | ||
out.add(field.name, value.decode(charset)) | ||
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. that accounts for producing |
||
else: | ||
out.add(field.name, value) | ||
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. that is for |
||
size += len(value) | ||
if 0 < max_size < size: | ||
raise HTTPRequestEntityTooLarge( | ||
max_size=max_size, | ||
actual_size=size | ||
) | ||
chunk = await field.read_chunk(size=2**16) | ||
tmp.seek(0) | ||
|
||
ff = FileField(field.name, field.filename, | ||
cast(io.BufferedReader, tmp), | ||
content_type, field.headers) | ||
out.add(field.name, ff) | ||
else: | ||
value = await field.read(decode=True) | ||
if content_type is None or \ | ||
content_type.startswith('text/'): | ||
charset = field.get_charset(default='utf-8') | ||
value = value.decode(charset) | ||
out.add(field.name, value) | ||
size += len(value) | ||
if 0 < max_size < size: | ||
raise HTTPRequestEntityTooLarge( | ||
max_size=max_size, | ||
actual_size=size | ||
) | ||
raise ValueError( | ||
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. Given the documentation, I assume that behavior here is correct and decoding nested multipart is a custom job. |
||
'To decode nested multipart you need ' | ||
'to use custom reader', | ||
) | ||
|
||
field = await multipart.next() | ||
else: | ||
|
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.
There is no such of thing as
MultipartBodyReader
so decided to go withMultipartReader
as the only thing that matched the interface. I will give it another look, but for now that was the thing that matched.