Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions src/requests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,26 +502,23 @@ def get_encodings_from_content(content):


def _parse_content_type_header(header):
"""Returns content type and parameters from given header
"""Returns content type and parameters from given header.

:param header: string
:return: tuple containing content type and dictionary of
parameters
parameters.
"""

tokens = header.split(";")
content_type, params = tokens[0].strip(), tokens[1:]
params_dict = {}
items_to_strip = "\"' "
strip_chars = "\"' "

for param in params:
param = param.strip()
if param:
key, value = param, True
index_of_equals = param.find("=")
if index_of_equals != -1:
key = param[:index_of_equals].strip(items_to_strip)
value = param[index_of_equals + 1 :].strip(items_to_strip)
if param and (idx := param.find("=")) != -1:
key = param[:idx].strip(strip_chars)
value = param[idx + 1 :].strip(strip_chars)
params_dict[key.lower()] = value
return content_type, params_dict

Expand Down
8 changes: 4 additions & 4 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,6 @@ def test_parse_dict_header(value, expected):
{
"boundary": "something",
"boundary2": "something_else",
"no_equals": True,
},
),
),
Expand All @@ -604,7 +603,6 @@ def test_parse_dict_header(value, expected):
{
"boundary": "something",
"boundary2": "something_else",
"no_equals": True,
},
),
),
Expand All @@ -615,7 +613,6 @@ def test_parse_dict_header(value, expected):
{
"boundary": "something",
"boundary2": "something_else",
"no_equals": True,
},
),
),
Expand All @@ -626,7 +623,6 @@ def test_parse_dict_header(value, expected):
{
"boundary": "something",
"boundary2": "something_else",
"no_equals": True,
},
),
),
Expand All @@ -646,6 +642,10 @@ def test__parse_content_type_header(value, expected):
"utf-8",
),
(CaseInsensitiveDict({"content-type": "text/plain"}), "ISO-8859-1"),
(
CaseInsensitiveDict({"content-type": "text/html; charset"}),
"ISO-8859-1",
),
),
)
def test_get_encoding_from_headers(value, expected):
Expand Down
Loading