Conversation
Makes all headers lower case, fixing case sensitivity issues. Exposes jheaders property in Files and Fields.
4304002 to
f1e28d6
Compare
python_multipart/multipart.py
Outdated
|
|
||
| class FieldProtocol(_FormProtocol, Protocol): | ||
| def __init__(self, name: bytes | None) -> None: ... | ||
| def __init__(self, name: bytes, content_type: str | None = None) -> None: ... |
There was a problem hiding this comment.
Yes, but not frequently used. Examples:
- Passing json and specifying "application/json"
- Embedding "multipart/mixed" inside ( https://www.ietf.org/rfc/rfc2388.txt ).
- Specifying a charset for a field
There was a problem hiding this comment.
Specifying a charset for a field
This is enough to convince me. Thanks.
0fc5e2d to
f1e28d6
Compare
python_multipart/multipart.py
Outdated
|
|
||
| # Get the field and filename. | ||
| field_name = options.get(b"name") | ||
| field_name = options.get(b"name", b"") |
There was a problem hiding this comment.
Is this needed? Isn't the field_name supposed to always be present?
There was a problem hiding this comment.
I added this for typing. Without it, we'll need to raise or assert it's not None. Which makes sense actually.
Will probably need a test to cover it. too.
There was a problem hiding this comment.
Removed the default value and added a check for None.
python_multipart/multipart.py
Outdated
|
|
||
| def on_header_end() -> None: | ||
| headers[b"".join(header_name)] = b"".join(header_value) | ||
| headers[b"".join(header_name).decode().lower()] = b"".join(header_value) |
There was a problem hiding this comment.
I think the headers dict is easier to use if the keys are strings rather than bytes, so I used decode to do the transformation. The string is guaranteed to be valid since it can only contain a subset of ASCII characters (it's filtered as it's built). Can be bytes if you prefer, in which case decode isn't needed.
There was a problem hiding this comment.
Indeed it's not needed here. I'll create another PR for that. The change made the internal headers consistent with the interface of create_form_parser , but isn't needed to achieve the aims of this PR.
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
python_multipart/multipart.py
Outdated
| """ | ||
|
|
||
| def __init__(self, name: bytes | None) -> None: | ||
| def __init__(self, name: bytes, content_type: str | None = None) -> None: |
There was a problem hiding this comment.
Why did the name type changed here? Now we have a mismatch between this and the File.field_name. 🤔
Can we revert this bit?
There was a problem hiding this comment.
This can be reverted indeed. Done.
python_multipart/multipart.py
Outdated
| def on_start() -> None: | ||
| nonlocal file | ||
| file = FileClass(file_name, None, config=cast("FileConfig", self.config)) | ||
| file = FileClass(file_name, b"", config=cast("FileConfig", self.config)) |
There was a problem hiding this comment.
| file = FileClass(file_name, b"", config=cast("FileConfig", self.config)) | |
| file = FileClass(file_name, config=cast("FileConfig", self.config)) |
There was a problem hiding this comment.
This suggestion can't be done without a significant change in the function and Protocol signature because it would place an optional argument before a positional argument. We can keep the None as it was though.
python_multipart/multipart.py
Outdated
|
|
||
| def on_header_end() -> None: | ||
| headers[b"".join(header_name)] = b"".join(header_value) | ||
| headers[b"".join(header_name).decode().lower()] = b"".join(header_value) |
jhnstrk
left a comment
There was a problem hiding this comment.
Updated the code in response to suggestions. Additional comments inline.
python_multipart/multipart.py
Outdated
| """ | ||
|
|
||
| def __init__(self, name: bytes | None) -> None: | ||
| def __init__(self, name: bytes, content_type: str | None = None) -> None: |
There was a problem hiding this comment.
This can be reverted indeed. Done.
python_multipart/multipart.py
Outdated
| def on_start() -> None: | ||
| nonlocal file | ||
| file = FileClass(file_name, None, config=cast("FileConfig", self.config)) | ||
| file = FileClass(file_name, b"", config=cast("FileConfig", self.config)) |
There was a problem hiding this comment.
This suggestion can't be done without a significant change in the function and Protocol signature because it would place an optional argument before a positional argument. We can keep the None as it was though.
python_multipart/multipart.py
Outdated
|
|
||
| def on_header_end() -> None: | ||
| headers[b"".join(header_name)] = b"".join(header_value) | ||
| headers[b"".join(header_name).decode().lower()] = b"".join(header_value) |
There was a problem hiding this comment.
Indeed it's not needed here. I'll create another PR for that. The change made the internal headers consistent with the interface of create_form_parser , but isn't needed to achieve the aims of this PR.
Adds MIME type information as
File.content_typeas requested in issue #58.Tests have been updated and a case-sensitive header issue was fixed too.
The part headers are also exposed as a dict, with keys as strings, lower-cased as
File.headers.