Parse a ``Cookie`` HTTP header into a dict of name/value pairs. This function attempts to mimic browser cookie parsing behavior; it specifically does not follow any of the cookie-related RFCs (because browsers don't either). The algorithm used is identical to that used by Django ve
(cookie: str)
| 1109 | |
| 1110 | |
| 1111 | def parse_cookie(cookie: str) -> Dict[str, str]: |
| 1112 | """Parse a ``Cookie`` HTTP header into a dict of name/value pairs. |
| 1113 | |
| 1114 | This function attempts to mimic browser cookie parsing behavior; |
| 1115 | it specifically does not follow any of the cookie-related RFCs |
| 1116 | (because browsers don't either). |
| 1117 | |
| 1118 | The algorithm used is identical to that used by Django version 1.9.10. |
| 1119 | |
| 1120 | .. versionadded:: 4.4.2 |
| 1121 | """ |
| 1122 | cookiedict = {} |
| 1123 | for chunk in cookie.split(str(";")): |
| 1124 | if str("=") in chunk: |
| 1125 | key, val = chunk.split(str("="), 1) |
| 1126 | else: |
| 1127 | # Assume an empty name per |
| 1128 | # https://bugzilla.mozilla.org/show_bug.cgi?id=169091 |
| 1129 | key, val = str(""), chunk |
| 1130 | key, val = key.strip(), val.strip() |
| 1131 | if key or val: |
| 1132 | # unquote using Python's algorithm. |
| 1133 | cookiedict[key] = _unquote_cookie(val) |
| 1134 | return cookiedict |