MCPcopy
hub / github.com/encode/starlette / cookie_parser

Function cookie_parser

starlette/requests.py:46–70  ·  view source on GitHub ↗

This function parses a ``Cookie`` HTTP header into a dict of key/value pairs. It attempts to mimic browser cookie parsing behavior: browsers and web servers frequently disregard the spec (RFC 6265) when setting and reading cookies, so we attempt to suit the common scenarios here.

(cookie_string: str)

Source from the content-addressed store, hash-verified

44
45
46def cookie_parser(cookie_string: str) -> dict[str, str]:
47 """
48 This function parses a ``Cookie`` HTTP header into a dict of key/value pairs.
49
50 It attempts to mimic browser cookie parsing behavior: browsers and web servers
51 frequently disregard the spec (RFC 6265) when setting and reading cookies,
52 so we attempt to suit the common scenarios here.
53
54 This function has been adapted from Django 3.1.0.
55 Note: we are explicitly _NOT_ using `SimpleCookie.load` because it is based
56 on an outdated spec and will fail on lots of input we want to support
57 """
58 cookie_dict: dict[str, str] = {}
59 for chunk in cookie_string.split(";"):
60 if "=" in chunk:
61 key, val = chunk.split("=", 1)
62 else:
63 # Assume an empty name per
64 # https://bugzilla.mozilla.org/show_bug.cgi?id=169091
65 key, val = "", chunk
66 key, val = key.strip(), val.strip()
67 if key or val:
68 # unquote using Python's algorithm.
69 cookie_dict[key] = http_cookies._unquote(val)
70 return cookie_dict
71
72
73class ClientDisconnect(Exception):

Callers 1

cookiesMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected