Skip to content

Commit f0198e6

Browse files
authored
Fix malformed value parsing for Content-Type (#7309)
1 parent bc7dd0f commit f0198e6

2 files changed

Lines changed: 10 additions & 13 deletions

File tree

src/requests/utils.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -502,26 +502,23 @@ def get_encodings_from_content(content):
502502

503503

504504
def _parse_content_type_header(header):
505-
"""Returns content type and parameters from given header
505+
"""Returns content type and parameters from given header.
506506
507507
:param header: string
508508
:return: tuple containing content type and dictionary of
509-
parameters
509+
parameters.
510510
"""
511511

512512
tokens = header.split(";")
513513
content_type, params = tokens[0].strip(), tokens[1:]
514514
params_dict = {}
515-
items_to_strip = "\"' "
515+
strip_chars = "\"' "
516516

517517
for param in params:
518518
param = param.strip()
519-
if param:
520-
key, value = param, True
521-
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)
525522
params_dict[key.lower()] = value
526523
return content_type, params_dict
527524

tests/test_utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,6 @@ def test_parse_dict_header(value, expected):
596596
{
597597
"boundary": "something",
598598
"boundary2": "something_else",
599-
"no_equals": True,
600599
},
601600
),
602601
),
@@ -607,7 +606,6 @@ def test_parse_dict_header(value, expected):
607606
{
608607
"boundary": "something",
609608
"boundary2": "something_else",
610-
"no_equals": True,
611609
},
612610
),
613611
),
@@ -618,7 +616,6 @@ def test_parse_dict_header(value, expected):
618616
{
619617
"boundary": "something",
620618
"boundary2": "something_else",
621-
"no_equals": True,
622619
},
623620
),
624621
),
@@ -629,7 +626,6 @@ def test_parse_dict_header(value, expected):
629626
{
630627
"boundary": "something",
631628
"boundary2": "something_else",
632-
"no_equals": True,
633629
},
634630
),
635631
),
@@ -649,6 +645,10 @@ def test__parse_content_type_header(value, expected):
649645
"utf-8",
650646
),
651647
(CaseInsensitiveDict({"content-type": "text/plain"}), "ISO-8859-1"),
648+
(
649+
CaseInsensitiveDict({"content-type": "text/html; charset"}),
650+
"ISO-8859-1",
651+
),
652652
),
653653
)
654654
def test_get_encoding_from_headers(value, expected):

0 commit comments

Comments
 (0)