Fix malformed value parsing for Content-Type (#7309)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Nate Prewitt · committed by GitHub · parent bc7dd0f · Browse files · 2 files·+10 added−13 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+10 added−13 removed
- tests:+4 added−4 removed
- other:+6 added−9 removed
Provenance
- Commit
f0198e6- Tree
3279094- Author
Nate Prewitt <nate.prewitt@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
bc7dd0f
2 files changed+10 added−13 removed
modified:src/requests/utils.py+6 added-9 removed
500 unmodified lines501 )502 503 504 def _parse_content_type_header(header):505- """Returns content type and parameters from given header505+ """Returns content type and parameters from given header.506 507 :param header: string508 :return: tuple containing content type and dictionary of509- parameters509+ parameters.510 """511 512 tokens = header.split(";")513 content_type, params = tokens[0].strip(), tokens[1:]514 params_dict = {}515- items_to_strip = "\"' "515+ strip_chars = "\"' "516 517 for param in params:518 param = param.strip()519- if param:520- key, value = param, True521- index_of_equals = param.find("=")522- if index_of_equals != -1:523- key = param[:index_of_equals].strip(items_to_strip)524- value = param[index_of_equals + 1 :].strip(items_to_strip)519+ if param and (idx := param.find("=")) != -1:520+ key = param[:idx].strip(strip_chars)521+ value = param[idx + 1 :].strip(strip_chars)522 params_dict[key.lower()] = value523 return content_type, params_dict524 525 558 unmodified linesmodified:tests/test_utils.py+4 added-4 removed
594 unmodified lines595 "multipart/form-data",596 {597 "boundary": "something",598 "boundary2": "something_else",599- "no_equals": True,599 },600 ),601 ),602 (2 unmodified lines605 "multipart/form-data",606 {607 "boundary": "something",608 "boundary2": "something_else",610- "no_equals": True,609 },610 ),611 ),612 (2 unmodified lines615 "multipart/form-data",616 {617 "boundary": "something",618 "boundary2": "something_else",621- "no_equals": True,619 },620 ),621 ),622 (2 unmodified lines625 "multipart/form-data",626 {627 "boundary": "something",628 "boundary2": "something_else",632- "no_equals": True,629 },630 ),631 ),632 ("application/json ; ; ", ("application/json", {})),11 unmodified lines644 CaseInsensitiveDict({"content-type": "application/json; charset=utf-8"}),645 "utf-8",646 ),647 (CaseInsensitiveDict({"content-type": "text/plain"}), "ISO-8859-1"),648+ (649+ CaseInsensitiveDict({"content-type": "text/html; charset"}),650+ "ISO-8859-1",651+ ),652 ),653 )654 def test_get_encoding_from_headers(value, expected):655 assert get_encoding_from_headers(value) == expected335 unmodified lines