-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Recognizes non-ASCII filenames in RFC 2231, and suport filename length is zero for multipart/form-data. #1497
Merged
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import asyncio | ||
import email.utils | ||
import json | ||
import sys | ||
|
||
from cgi import parse_header | ||
from collections import namedtuple | ||
from http.cookies import SimpleCookie | ||
from urllib.parse import parse_qs, urlunparse | ||
from urllib.parse import parse_qs, unquote, urlunparse | ||
|
||
from httptools import parse_url | ||
|
||
|
@@ -356,28 +357,35 @@ def parse_multipart_form(body, boundary): | |
) | ||
|
||
if form_header_field == "content-disposition": | ||
file_name = form_parameters.get("filename") | ||
field_name = form_parameters.get("name") | ||
file_name = form_parameters.get("filename") | ||
|
||
# non-ASCII filenames in RFC2231, "filename*" format | ||
if file_name is None and form_parameters.get("filename*"): | ||
encoding, _, value = email.utils.decode_rfc2231( | ||
form_parameters["filename*"] | ||
) | ||
file_name = unquote(value, encoding=encoding) | ||
elif form_header_field == "content-type": | ||
content_type = form_header_value | ||
content_charset = form_parameters.get("charset", "utf-8") | ||
|
||
if field_name: | ||
post_data = form_part[line_index:-4] | ||
if file_name: | ||
if file_name is None: | ||
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. Maybe this can be changed to |
||
value = post_data.decode(content_charset) | ||
if field_name in fields: | ||
fields[field_name].append(value) | ||
else: | ||
fields[field_name] = [value] | ||
else: | ||
form_file = File( | ||
type=content_type, name=file_name, body=post_data | ||
) | ||
if field_name in files: | ||
files[field_name].append(form_file) | ||
else: | ||
files[field_name] = [form_file] | ||
else: | ||
value = post_data.decode(content_charset) | ||
if field_name in fields: | ||
fields[field_name].append(value) | ||
else: | ||
fields[field_name] = [value] | ||
else: | ||
logger.debug( | ||
"Form-data field does not have a 'name' parameter " | ||
|
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
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.
Since the
form_parameters
lookup is being done twice, maybe we can move the lookup to a single line and use a variable instead? (this is just a good to have change)Also the format you are using is
RFC 5987
standard and notRFC2231
if I remember correctly. (Sorry, for the nitpicking)