Handle double quotes and escaping in cookie values. This method is copied verbatim from the Python 3.5 standard library (http.cookies._unquote) so we don't have to depend on non-public interfaces.
(s: str)
| 1059 | |
| 1060 | |
| 1061 | def _unquote_cookie(s: str) -> str: |
| 1062 | """Handle double quotes and escaping in cookie values. |
| 1063 | |
| 1064 | This method is copied verbatim from the Python 3.5 standard |
| 1065 | library (http.cookies._unquote) so we don't have to depend on |
| 1066 | non-public interfaces. |
| 1067 | """ |
| 1068 | # If there aren't any doublequotes, |
| 1069 | # then there can't be any special characters. See RFC 2109. |
| 1070 | if s is None or len(s) < 2: |
| 1071 | return s |
| 1072 | if s[0] != '"' or s[-1] != '"': |
| 1073 | return s |
| 1074 | |
| 1075 | # We have to assume that we must decode this string. |
| 1076 | # Down to work. |
| 1077 | |
| 1078 | # Remove the "s |
| 1079 | s = s[1:-1] |
| 1080 | |
| 1081 | # Check for special sequences. Examples: |
| 1082 | # \012 --> \n |
| 1083 | # \" --> " |
| 1084 | # |
| 1085 | i = 0 |
| 1086 | n = len(s) |
| 1087 | res = [] |
| 1088 | while 0 <= i < n: |
| 1089 | o_match = _OctalPatt.search(s, i) |
| 1090 | q_match = _QuotePatt.search(s, i) |
| 1091 | if not o_match and not q_match: # Neither matched |
| 1092 | res.append(s[i:]) |
| 1093 | break |
| 1094 | # else: |
| 1095 | j = k = -1 |
| 1096 | if o_match: |
| 1097 | j = o_match.start(0) |
| 1098 | if q_match: |
| 1099 | k = q_match.start(0) |
| 1100 | if q_match and (not o_match or k < j): # QuotePatt matched |
| 1101 | res.append(s[i:k]) |
| 1102 | res.append(s[k + 1]) |
| 1103 | i = k + 2 |
| 1104 | else: # OctalPatt matched |
| 1105 | res.append(s[i:j]) |
| 1106 | res.append(chr(int(s[j + 1 : j + 4], 8))) |
| 1107 | i = j + 4 |
| 1108 | return _nulljoin(res) |
| 1109 | |
| 1110 | |
| 1111 | def parse_cookie(cookie: str) -> Dict[str, str]: |
no test coverage detected