Repository navigation
httputil: separate HTTPHeaders decoding from validation - #3922
anishpentyala wants to merge 16 commits into
Conversation
bdarnell
left a comment
There was a problem hiding this comment.
There are some merge conflicts (sorry about the timing)
We should remove _chars_are_bytes; this was never meant for public use.
| name: str, | ||
| value: str, | ||
| *, | ||
| strict_http: bool | None = None, |
There was a problem hiding this comment.
strict_http feels like it might grow beyond a bool: it's really a choice between parse_mode=PARSE_HTTP_HEADERS and parse_mode=PARSE_MULTIPART_HEADERS, and there could be more modes in the future.
Really this argument is properly scoped to the HTTPHeaders object itself, rather than the name/value pair being added. Could we add this to the constructor instead of this method? It may be tricky since the dict constructor is very broad. But we could consider a static factory method instead of adding more special cases to the constructor (we don't generally use the full generality of the dict constructor internally, although it is public for third-party use)
| instead of 7.0 as previously documented. | ||
| """ | ||
| if isinstance(line, bytes): | ||
| line = line.decode(encoding) |
There was a problem hiding this comment.
If the encoding argument is just to do a .decode(encoding) at the entry point to the method, maybe it shouldn't be there and we should just require str input and the caller can decode.
|
Thanks for the review. I'll rework this against current master: remove |
Use a factory without reserving dict constructor keywords. Keep caller-side decoding and current master error semantics; remove the internal compatibility flag.
Replace encoding/alias tests with str input, caller decoding and persistent-mode coverage.
Record factory and parse mode keyword, remove private flag and experimental encoding arguments.
Exercise factory, parsed modes, mutations and unchanged dict constructor keywords.
Restore the exact generated snapshot bytes after web editor normalization.
|
|
||
| with self.subTest(mode=mode, method=method): | ||
| if mode == "multipart": | ||
| mutate("名") |
There was a problem hiding this comment.
I think we also need to test that non-ascii characters from latin1 are still allowed (in both http and multipart modes)
| ) -> None: | ||
| @classmethod | ||
| def with_parse_mode( | ||
| cls, parse_mode: typing.Literal["http", "multipart"] |
There was a problem hiding this comment.
Let's make an enum for these, so we don't have to have the list of possible values repeated in type declarations in lots of places.
|
Thanks for the review. Keeping decoding separate from validation while preserving valid HTTP byte values is what interests me in this cleanup. Updated the branch with The focused header/HTTP/curl/API checks pass (286 tests, three existing skips), along with Black, flake8, the type client, strict docs and all 17 benchmark budgets. The full local run still hits the previously noted request-target |
bdarnell
left a comment
There was a problem hiding this comment.
I'm not sure "separating decoding from validation" is a good description of what's happening here. Really we're just cleaning up the same behavior we've had, replacing the hacky _chars_are_bytes flag with something that better describes the differences.
| # Keep the entire Latin-1 non-ASCII range so neither mode can silently | ||
| # become ASCII-only or apply Unicode control-category filtering. | ||
| for mode in HTTPHeaderParseMode: | ||
| for codepoint in range(0x80, 0x100): |
There was a problem hiding this comment.
Testing every code point is excessive. We just need one latin1 char to represent the category. I was expecting you to add this to test_object_parse_modes_validate_all_mutations just so we don't have to repeat the list of mutations.
Fixes #3900.
Reworked after review and merged current master (
73823eda) into the existing branch without rewriting history.HTTPHeaders.with_parse_mode(...)acceptsHTTPHeaderParseMode.HTTPorHTTPHeaderParseMode.MULTIPARTand creates an empty object with a persistent validation mode.HTTPHeaders.parse(text, parse_mode=...)uses that factory._chars_are_bytesentirely. Parsing still takesstr; decoding stays with callers. Multipart keeps its bounded UTF-8 header decoding. HTTP/1 and curl decoding are unchanged.ValueErrorfor invalid constructed headers andHTTPInputErrorfor malformed parsed headers.Tests explicitly preserve all non-ASCII Latin-1 values (0x80 through 0xFF) in both modes through parsing and mutations. Tests also cover controls and token names, copy/cache behavior, constructor compatibility, caller decoding and subclass factories. API snapshot and mypy client are updated.
Local Linux validation:
73823edain this environment.Environment-specific caveat: a separate property-test run generated request target
#and failed; a clean73823edafull run in the same environment also generated that failing example. This was not reproduced on a separate review environment. This patch does not change request-line parsing.Windows and macOS runtime testing has not been run locally. Hosted checks on the final updated head are being verified.