diff --git a/Lib/http/cookies.py b/Lib/http/cookies.py index 351faf428a20cd..9ee50cdccaae63 100644 --- a/Lib/http/cookies.py +++ b/Lib/http/cookies.py @@ -184,51 +184,29 @@ def _quote(str): return '"' + str.translate(_Translator) + '"' -_OctalPatt = re.compile(r"\\[0-3][0-7][0-7]") -_QuotePatt = re.compile(r"[\\].") +_OctalPatt = re.compile(r"\\([0-3][0-7][0-7])") +_QuotePatt = re.compile(r'\\"') def _unquote(str): - # If there aren't any doublequotes, - # then there can't be any special characters. See RFC 2109. if str is None or len(str) < 2: return str if str[0] != '"' or str[-1] != '"': return str - # We have to assume that we must decode this string. - # Down to work. - - # Remove the "s + # Remove the surrounding quotes str = str[1:-1] - # Check for special sequences. Examples: - # \012 --> \n - # \" --> " - # - i = 0 - n = len(str) - res = [] - while 0 <= i < n: - o_match = _OctalPatt.search(str, i) - q_match = _QuotePatt.search(str, i) - if not o_match and not q_match: # Neither matched - res.append(str[i:]) - break - # else: - j = k = -1 - if o_match: - j = o_match.start(0) - if q_match: - k = q_match.start(0) - if q_match and (not o_match or k < j): # QuotePatt matched - res.append(str[i:k]) - res.append(str[k+1]) - i = k + 2 - else: # OctalPatt matched - res.append(str[i:j]) - res.append(chr(int(str[j+1:j+4], 8))) - i = j + 4 - return _nulljoin(res) + # Function to replace octal patterns + def replace_octal(match): + octal_value = match.group(1) + return chr(int(octal_value, 8)) + + # Replace octal escape sequences + str = _OctalPatt.sub(replace_octal, str) + # Replace escaped quotes + str = _QuotePatt.sub('"', str) + + return str # The _getdate() routine is used to set the expiration time in the cookie's HTTP # header. By default, _getdate() returns the current time in the appropriate