Skip to content

Commit b536520

Browse files
committed
Handle semicolons in quoted Link parameters
1 parent ea5675b commit b536520

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

src/requests/utils.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,28 @@ def default_headers() -> CaseInsensitiveDict[str]:
962962
)
963963

964964

965+
def _parse_header_links_params(value: str) -> list[str]:
966+
"""Split Link header parameters without splitting quoted values."""
967+
params: list[str] = []
968+
start = 0
969+
in_quotes = False
970+
escaped = False
971+
972+
for index, char in enumerate(value):
973+
if escaped:
974+
escaped = False
975+
elif char == "\\" and in_quotes:
976+
escaped = True
977+
elif char == '"':
978+
in_quotes = not in_quotes
979+
elif char == ";" and not in_quotes:
980+
params.append(value[start:index])
981+
start = index + 1
982+
983+
params.append(value[start:])
984+
return params
985+
986+
965987
def parse_header_links(value: str) -> list[dict[str, str]]:
966988
"""Return a list of parsed link headers proxies.
967989
@@ -986,7 +1008,7 @@ def parse_header_links(value: str) -> list[dict[str, str]]:
9861008

9871009
link: dict[str, str] = {"url": url.strip("<> '\"")}
9881010

989-
for param in params.split(";"):
1011+
for param in _parse_header_links_params(params):
9901012
try:
9911013
key, value = param.split("=", 1)
9921014
except ValueError:

tests/test_utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,12 @@ def test_iter_slices(value, length):
696696
'<http:/.../front.jpeg>; title="a=b"; rel="next"',
697697
[{"url": "http:/.../front.jpeg", "title": "a=b", "rel": "next"}],
698698
),
699+
(
700+
# Quoted parameter values may contain both "=" and ";"; separators
701+
# inside the quoted string must not create another parameter.
702+
'<http:/.../front.jpeg>; title="a=b;c"; rel="next"',
703+
[{"url": "http:/.../front.jpeg", "title": "a=b;c", "rel": "next"}],
704+
),
699705
("", []),
700706
),
701707
)

0 commit comments

Comments
 (0)