Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../usr/lib64/python3..../http
File: cookies.py
####
[0] Fix | Delete
# Copyright 2000 by Timothy O'Malley <timo@alum.mit.edu>
[1] Fix | Delete
#
[2] Fix | Delete
# All Rights Reserved
[3] Fix | Delete
#
[4] Fix | Delete
# Permission to use, copy, modify, and distribute this software
[5] Fix | Delete
# and its documentation for any purpose and without fee is hereby
[6] Fix | Delete
# granted, provided that the above copyright notice appear in all
[7] Fix | Delete
# copies and that both that copyright notice and this permission
[8] Fix | Delete
# notice appear in supporting documentation, and that the name of
[9] Fix | Delete
# Timothy O'Malley not be used in advertising or publicity
[10] Fix | Delete
# pertaining to distribution of the software without specific, written
[11] Fix | Delete
# prior permission.
[12] Fix | Delete
#
[13] Fix | Delete
# Timothy O'Malley DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
[14] Fix | Delete
# SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
[15] Fix | Delete
# AND FITNESS, IN NO EVENT SHALL Timothy O'Malley BE LIABLE FOR
[16] Fix | Delete
# ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
[17] Fix | Delete
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
[18] Fix | Delete
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
[19] Fix | Delete
# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
[20] Fix | Delete
# PERFORMANCE OF THIS SOFTWARE.
[21] Fix | Delete
#
[22] Fix | Delete
####
[23] Fix | Delete
#
[24] Fix | Delete
# Id: Cookie.py,v 2.29 2000/08/23 05:28:49 timo Exp
[25] Fix | Delete
# by Timothy O'Malley <timo@alum.mit.edu>
[26] Fix | Delete
#
[27] Fix | Delete
# Cookie.py is a Python module for the handling of HTTP
[28] Fix | Delete
# cookies as a Python dictionary. See RFC 2109 for more
[29] Fix | Delete
# information on cookies.
[30] Fix | Delete
#
[31] Fix | Delete
# The original idea to treat Cookies as a dictionary came from
[32] Fix | Delete
# Dave Mitchell (davem@magnet.com) in 1995, when he released the
[33] Fix | Delete
# first version of nscookie.py.
[34] Fix | Delete
#
[35] Fix | Delete
####
[36] Fix | Delete
[37] Fix | Delete
r"""
[38] Fix | Delete
Here's a sample session to show how to use this module.
[39] Fix | Delete
At the moment, this is the only documentation.
[40] Fix | Delete
[41] Fix | Delete
The Basics
[42] Fix | Delete
----------
[43] Fix | Delete
[44] Fix | Delete
Importing is easy...
[45] Fix | Delete
[46] Fix | Delete
>>> from http import cookies
[47] Fix | Delete
[48] Fix | Delete
Most of the time you start by creating a cookie.
[49] Fix | Delete
[50] Fix | Delete
>>> C = cookies.SimpleCookie()
[51] Fix | Delete
[52] Fix | Delete
Once you've created your Cookie, you can add values just as if it were
[53] Fix | Delete
a dictionary.
[54] Fix | Delete
[55] Fix | Delete
>>> C = cookies.SimpleCookie()
[56] Fix | Delete
>>> C["fig"] = "newton"
[57] Fix | Delete
>>> C["sugar"] = "wafer"
[58] Fix | Delete
>>> C.output()
[59] Fix | Delete
'Set-Cookie: fig=newton\r\nSet-Cookie: sugar=wafer'
[60] Fix | Delete
[61] Fix | Delete
Notice that the printable representation of a Cookie is the
[62] Fix | Delete
appropriate format for a Set-Cookie: header. This is the
[63] Fix | Delete
default behavior. You can change the header and printed
[64] Fix | Delete
attributes by using the .output() function
[65] Fix | Delete
[66] Fix | Delete
>>> C = cookies.SimpleCookie()
[67] Fix | Delete
>>> C["rocky"] = "road"
[68] Fix | Delete
>>> C["rocky"]["path"] = "/cookie"
[69] Fix | Delete
>>> print(C.output(header="Cookie:"))
[70] Fix | Delete
Cookie: rocky=road; Path=/cookie
[71] Fix | Delete
>>> print(C.output(attrs=[], header="Cookie:"))
[72] Fix | Delete
Cookie: rocky=road
[73] Fix | Delete
[74] Fix | Delete
The load() method of a Cookie extracts cookies from a string. In a
[75] Fix | Delete
CGI script, you would use this method to extract the cookies from the
[76] Fix | Delete
HTTP_COOKIE environment variable.
[77] Fix | Delete
[78] Fix | Delete
>>> C = cookies.SimpleCookie()
[79] Fix | Delete
>>> C.load("chips=ahoy; vienna=finger")
[80] Fix | Delete
>>> C.output()
[81] Fix | Delete
'Set-Cookie: chips=ahoy\r\nSet-Cookie: vienna=finger'
[82] Fix | Delete
[83] Fix | Delete
The load() method is darn-tootin smart about identifying cookies
[84] Fix | Delete
within a string. Escaped quotation marks, nested semicolons, and other
[85] Fix | Delete
such trickeries do not confuse it.
[86] Fix | Delete
[87] Fix | Delete
>>> C = cookies.SimpleCookie()
[88] Fix | Delete
>>> C.load('keebler="E=everybody; L=\\"Loves\\"; fudge=\\012;";')
[89] Fix | Delete
>>> print(C)
[90] Fix | Delete
Set-Cookie: keebler="E=everybody; L=\"Loves\"; fudge=\012;"
[91] Fix | Delete
[92] Fix | Delete
Each element of the Cookie also supports all of the RFC 2109
[93] Fix | Delete
Cookie attributes. Here's an example which sets the Path
[94] Fix | Delete
attribute.
[95] Fix | Delete
[96] Fix | Delete
>>> C = cookies.SimpleCookie()
[97] Fix | Delete
>>> C["oreo"] = "doublestuff"
[98] Fix | Delete
>>> C["oreo"]["path"] = "/"
[99] Fix | Delete
>>> print(C)
[100] Fix | Delete
Set-Cookie: oreo=doublestuff; Path=/
[101] Fix | Delete
[102] Fix | Delete
Each dictionary element has a 'value' attribute, which gives you
[103] Fix | Delete
back the value associated with the key.
[104] Fix | Delete
[105] Fix | Delete
>>> C = cookies.SimpleCookie()
[106] Fix | Delete
>>> C["twix"] = "none for you"
[107] Fix | Delete
>>> C["twix"].value
[108] Fix | Delete
'none for you'
[109] Fix | Delete
[110] Fix | Delete
The SimpleCookie expects that all values should be standard strings.
[111] Fix | Delete
Just to be sure, SimpleCookie invokes the str() builtin to convert
[112] Fix | Delete
the value to a string, when the values are set dictionary-style.
[113] Fix | Delete
[114] Fix | Delete
>>> C = cookies.SimpleCookie()
[115] Fix | Delete
>>> C["number"] = 7
[116] Fix | Delete
>>> C["string"] = "seven"
[117] Fix | Delete
>>> C["number"].value
[118] Fix | Delete
'7'
[119] Fix | Delete
>>> C["string"].value
[120] Fix | Delete
'seven'
[121] Fix | Delete
>>> C.output()
[122] Fix | Delete
'Set-Cookie: number=7\r\nSet-Cookie: string=seven'
[123] Fix | Delete
[124] Fix | Delete
Finis.
[125] Fix | Delete
"""
[126] Fix | Delete
[127] Fix | Delete
#
[128] Fix | Delete
# Import our required modules
[129] Fix | Delete
#
[130] Fix | Delete
import re
[131] Fix | Delete
import string
[132] Fix | Delete
[133] Fix | Delete
__all__ = ["CookieError", "BaseCookie", "SimpleCookie"]
[134] Fix | Delete
[135] Fix | Delete
_nulljoin = ''.join
[136] Fix | Delete
_semispacejoin = '; '.join
[137] Fix | Delete
_spacejoin = ' '.join
[138] Fix | Delete
[139] Fix | Delete
#
[140] Fix | Delete
# Define an exception visible to External modules
[141] Fix | Delete
#
[142] Fix | Delete
class CookieError(Exception):
[143] Fix | Delete
pass
[144] Fix | Delete
[145] Fix | Delete
[146] Fix | Delete
# These quoting routines conform to the RFC2109 specification, which in
[147] Fix | Delete
# turn references the character definitions from RFC2068. They provide
[148] Fix | Delete
# a two-way quoting algorithm. Any non-text character is translated
[149] Fix | Delete
# into a 4 character sequence: a forward-slash followed by the
[150] Fix | Delete
# three-digit octal equivalent of the character. Any '\' or '"' is
[151] Fix | Delete
# quoted with a preceding '\' slash.
[152] Fix | Delete
# Because of the way browsers really handle cookies (as opposed to what
[153] Fix | Delete
# the RFC says) we also encode "," and ";".
[154] Fix | Delete
#
[155] Fix | Delete
# These are taken from RFC2068 and RFC2109.
[156] Fix | Delete
# _LegalChars is the list of chars which don't require "'s
[157] Fix | Delete
# _Translator hash-table for fast quoting
[158] Fix | Delete
#
[159] Fix | Delete
_LegalChars = string.ascii_letters + string.digits + "!#$%&'*+-.^_`|~:"
[160] Fix | Delete
_UnescapedChars = _LegalChars + ' ()/<=>?@[]{}'
[161] Fix | Delete
[162] Fix | Delete
_Translator = {n: '\\%03o' % n
[163] Fix | Delete
for n in set(range(256)) - set(map(ord, _UnescapedChars))}
[164] Fix | Delete
_Translator.update({
[165] Fix | Delete
ord('"'): '\\"',
[166] Fix | Delete
ord('\\'): '\\\\',
[167] Fix | Delete
})
[168] Fix | Delete
[169] Fix | Delete
_is_legal_key = re.compile('[%s]+' % re.escape(_LegalChars)).fullmatch
[170] Fix | Delete
[171] Fix | Delete
def _quote(str):
[172] Fix | Delete
r"""Quote a string for use in a cookie header.
[173] Fix | Delete
[174] Fix | Delete
If the string does not need to be double-quoted, then just return the
[175] Fix | Delete
string. Otherwise, surround the string in doublequotes and quote
[176] Fix | Delete
(with a \) special characters.
[177] Fix | Delete
"""
[178] Fix | Delete
if str is None or _is_legal_key(str):
[179] Fix | Delete
return str
[180] Fix | Delete
else:
[181] Fix | Delete
return '"' + str.translate(_Translator) + '"'
[182] Fix | Delete
[183] Fix | Delete
[184] Fix | Delete
_OctalPatt = re.compile(r"\\[0-3][0-7][0-7]")
[185] Fix | Delete
_QuotePatt = re.compile(r"[\\].")
[186] Fix | Delete
[187] Fix | Delete
def _unquote(str):
[188] Fix | Delete
# If there aren't any doublequotes,
[189] Fix | Delete
# then there can't be any special characters. See RFC 2109.
[190] Fix | Delete
if str is None or len(str) < 2:
[191] Fix | Delete
return str
[192] Fix | Delete
if str[0] != '"' or str[-1] != '"':
[193] Fix | Delete
return str
[194] Fix | Delete
[195] Fix | Delete
# We have to assume that we must decode this string.
[196] Fix | Delete
# Down to work.
[197] Fix | Delete
[198] Fix | Delete
# Remove the "s
[199] Fix | Delete
str = str[1:-1]
[200] Fix | Delete
[201] Fix | Delete
# Check for special sequences. Examples:
[202] Fix | Delete
# \012 --> \n
[203] Fix | Delete
# \" --> "
[204] Fix | Delete
#
[205] Fix | Delete
i = 0
[206] Fix | Delete
n = len(str)
[207] Fix | Delete
res = []
[208] Fix | Delete
while 0 <= i < n:
[209] Fix | Delete
o_match = _OctalPatt.search(str, i)
[210] Fix | Delete
q_match = _QuotePatt.search(str, i)
[211] Fix | Delete
if not o_match and not q_match: # Neither matched
[212] Fix | Delete
res.append(str[i:])
[213] Fix | Delete
break
[214] Fix | Delete
# else:
[215] Fix | Delete
j = k = -1
[216] Fix | Delete
if o_match:
[217] Fix | Delete
j = o_match.start(0)
[218] Fix | Delete
if q_match:
[219] Fix | Delete
k = q_match.start(0)
[220] Fix | Delete
if q_match and (not o_match or k < j): # QuotePatt matched
[221] Fix | Delete
res.append(str[i:k])
[222] Fix | Delete
res.append(str[k+1])
[223] Fix | Delete
i = k + 2
[224] Fix | Delete
else: # OctalPatt matched
[225] Fix | Delete
res.append(str[i:j])
[226] Fix | Delete
res.append(chr(int(str[j+1:j+4], 8)))
[227] Fix | Delete
i = j + 4
[228] Fix | Delete
return _nulljoin(res)
[229] Fix | Delete
[230] Fix | Delete
# The _getdate() routine is used to set the expiration time in the cookie's HTTP
[231] Fix | Delete
# header. By default, _getdate() returns the current time in the appropriate
[232] Fix | Delete
# "expires" format for a Set-Cookie header. The one optional argument is an
[233] Fix | Delete
# offset from now, in seconds. For example, an offset of -3600 means "one hour
[234] Fix | Delete
# ago". The offset may be a floating point number.
[235] Fix | Delete
#
[236] Fix | Delete
[237] Fix | Delete
_weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
[238] Fix | Delete
[239] Fix | Delete
_monthname = [None,
[240] Fix | Delete
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
[241] Fix | Delete
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
[242] Fix | Delete
[243] Fix | Delete
def _getdate(future=0, weekdayname=_weekdayname, monthname=_monthname):
[244] Fix | Delete
from time import gmtime, time
[245] Fix | Delete
now = time()
[246] Fix | Delete
year, month, day, hh, mm, ss, wd, y, z = gmtime(now + future)
[247] Fix | Delete
return "%s, %02d %3s %4d %02d:%02d:%02d GMT" % \
[248] Fix | Delete
(weekdayname[wd], day, monthname[month], year, hh, mm, ss)
[249] Fix | Delete
[250] Fix | Delete
[251] Fix | Delete
class Morsel(dict):
[252] Fix | Delete
"""A class to hold ONE (key, value) pair.
[253] Fix | Delete
[254] Fix | Delete
In a cookie, each such pair may have several attributes, so this class is
[255] Fix | Delete
used to keep the attributes associated with the appropriate key,value pair.
[256] Fix | Delete
This class also includes a coded_value attribute, which is used to hold
[257] Fix | Delete
the network representation of the value.
[258] Fix | Delete
"""
[259] Fix | Delete
# RFC 2109 lists these attributes as reserved:
[260] Fix | Delete
# path comment domain
[261] Fix | Delete
# max-age secure version
[262] Fix | Delete
#
[263] Fix | Delete
# For historical reasons, these attributes are also reserved:
[264] Fix | Delete
# expires
[265] Fix | Delete
#
[266] Fix | Delete
# This is an extension from Microsoft:
[267] Fix | Delete
# httponly
[268] Fix | Delete
#
[269] Fix | Delete
# This dictionary provides a mapping from the lowercase
[270] Fix | Delete
# variant on the left to the appropriate traditional
[271] Fix | Delete
# formatting on the right.
[272] Fix | Delete
_reserved = {
[273] Fix | Delete
"expires" : "expires",
[274] Fix | Delete
"path" : "Path",
[275] Fix | Delete
"comment" : "Comment",
[276] Fix | Delete
"domain" : "Domain",
[277] Fix | Delete
"max-age" : "Max-Age",
[278] Fix | Delete
"secure" : "Secure",
[279] Fix | Delete
"httponly" : "HttpOnly",
[280] Fix | Delete
"version" : "Version",
[281] Fix | Delete
"samesite" : "SameSite",
[282] Fix | Delete
}
[283] Fix | Delete
[284] Fix | Delete
_flags = {'secure', 'httponly'}
[285] Fix | Delete
[286] Fix | Delete
def __init__(self):
[287] Fix | Delete
# Set defaults
[288] Fix | Delete
self._key = self._value = self._coded_value = None
[289] Fix | Delete
[290] Fix | Delete
# Set default attributes
[291] Fix | Delete
for key in self._reserved:
[292] Fix | Delete
dict.__setitem__(self, key, "")
[293] Fix | Delete
[294] Fix | Delete
@property
[295] Fix | Delete
def key(self):
[296] Fix | Delete
return self._key
[297] Fix | Delete
[298] Fix | Delete
@property
[299] Fix | Delete
def value(self):
[300] Fix | Delete
return self._value
[301] Fix | Delete
[302] Fix | Delete
@property
[303] Fix | Delete
def coded_value(self):
[304] Fix | Delete
return self._coded_value
[305] Fix | Delete
[306] Fix | Delete
def __setitem__(self, K, V):
[307] Fix | Delete
K = K.lower()
[308] Fix | Delete
if not K in self._reserved:
[309] Fix | Delete
raise CookieError("Invalid attribute %r" % (K,))
[310] Fix | Delete
dict.__setitem__(self, K, V)
[311] Fix | Delete
[312] Fix | Delete
def setdefault(self, key, val=None):
[313] Fix | Delete
key = key.lower()
[314] Fix | Delete
if key not in self._reserved:
[315] Fix | Delete
raise CookieError("Invalid attribute %r" % (key,))
[316] Fix | Delete
return dict.setdefault(self, key, val)
[317] Fix | Delete
[318] Fix | Delete
def __eq__(self, morsel):
[319] Fix | Delete
if not isinstance(morsel, Morsel):
[320] Fix | Delete
return NotImplemented
[321] Fix | Delete
return (dict.__eq__(self, morsel) and
[322] Fix | Delete
self._value == morsel._value and
[323] Fix | Delete
self._key == morsel._key and
[324] Fix | Delete
self._coded_value == morsel._coded_value)
[325] Fix | Delete
[326] Fix | Delete
__ne__ = object.__ne__
[327] Fix | Delete
[328] Fix | Delete
def copy(self):
[329] Fix | Delete
morsel = Morsel()
[330] Fix | Delete
dict.update(morsel, self)
[331] Fix | Delete
morsel.__dict__.update(self.__dict__)
[332] Fix | Delete
return morsel
[333] Fix | Delete
[334] Fix | Delete
def update(self, values):
[335] Fix | Delete
data = {}
[336] Fix | Delete
for key, val in dict(values).items():
[337] Fix | Delete
key = key.lower()
[338] Fix | Delete
if key not in self._reserved:
[339] Fix | Delete
raise CookieError("Invalid attribute %r" % (key,))
[340] Fix | Delete
data[key] = val
[341] Fix | Delete
dict.update(self, data)
[342] Fix | Delete
[343] Fix | Delete
def isReservedKey(self, K):
[344] Fix | Delete
return K.lower() in self._reserved
[345] Fix | Delete
[346] Fix | Delete
def set(self, key, val, coded_val):
[347] Fix | Delete
if key.lower() in self._reserved:
[348] Fix | Delete
raise CookieError('Attempt to set a reserved key %r' % (key,))
[349] Fix | Delete
if not _is_legal_key(key):
[350] Fix | Delete
raise CookieError('Illegal key %r' % (key,))
[351] Fix | Delete
[352] Fix | Delete
# It's a good key, so save it.
[353] Fix | Delete
self._key = key
[354] Fix | Delete
self._value = val
[355] Fix | Delete
self._coded_value = coded_val
[356] Fix | Delete
[357] Fix | Delete
def __getstate__(self):
[358] Fix | Delete
return {
[359] Fix | Delete
'key': self._key,
[360] Fix | Delete
'value': self._value,
[361] Fix | Delete
'coded_value': self._coded_value,
[362] Fix | Delete
}
[363] Fix | Delete
[364] Fix | Delete
def __setstate__(self, state):
[365] Fix | Delete
self._key = state['key']
[366] Fix | Delete
self._value = state['value']
[367] Fix | Delete
self._coded_value = state['coded_value']
[368] Fix | Delete
[369] Fix | Delete
def output(self, attrs=None, header="Set-Cookie:"):
[370] Fix | Delete
return "%s %s" % (header, self.OutputString(attrs))
[371] Fix | Delete
[372] Fix | Delete
__str__ = output
[373] Fix | Delete
[374] Fix | Delete
def __repr__(self):
[375] Fix | Delete
return '<%s: %s>' % (self.__class__.__name__, self.OutputString())
[376] Fix | Delete
[377] Fix | Delete
def js_output(self, attrs=None):
[378] Fix | Delete
# Print javascript
[379] Fix | Delete
return """
[380] Fix | Delete
<script type="text/javascript">
[381] Fix | Delete
<!-- begin hiding
[382] Fix | Delete
document.cookie = \"%s\";
[383] Fix | Delete
// end hiding -->
[384] Fix | Delete
</script>
[385] Fix | Delete
""" % (self.OutputString(attrs).replace('"', r'\"'))
[386] Fix | Delete
[387] Fix | Delete
def OutputString(self, attrs=None):
[388] Fix | Delete
# Build up our result
[389] Fix | Delete
#
[390] Fix | Delete
result = []
[391] Fix | Delete
append = result.append
[392] Fix | Delete
[393] Fix | Delete
# First, the key=value pair
[394] Fix | Delete
append("%s=%s" % (self.key, self.coded_value))
[395] Fix | Delete
[396] Fix | Delete
# Now add any defined attributes
[397] Fix | Delete
if attrs is None:
[398] Fix | Delete
attrs = self._reserved
[399] Fix | Delete
items = sorted(self.items())
[400] Fix | Delete
for key, value in items:
[401] Fix | Delete
if value == "":
[402] Fix | Delete
continue
[403] Fix | Delete
if key not in attrs:
[404] Fix | Delete
continue
[405] Fix | Delete
if key == "expires" and isinstance(value, int):
[406] Fix | Delete
append("%s=%s" % (self._reserved[key], _getdate(value)))
[407] Fix | Delete
elif key == "max-age" and isinstance(value, int):
[408] Fix | Delete
append("%s=%d" % (self._reserved[key], value))
[409] Fix | Delete
elif key == "comment" and isinstance(value, str):
[410] Fix | Delete
append("%s=%s" % (self._reserved[key], _quote(value)))
[411] Fix | Delete
elif key in self._flags:
[412] Fix | Delete
if value:
[413] Fix | Delete
append(str(self._reserved[key]))
[414] Fix | Delete
else:
[415] Fix | Delete
append("%s=%s" % (self._reserved[key], value))
[416] Fix | Delete
[417] Fix | Delete
# Return the result
[418] Fix | Delete
return _semispacejoin(result)
[419] Fix | Delete
[420] Fix | Delete
[421] Fix | Delete
#
[422] Fix | Delete
# Pattern for finding cookie
[423] Fix | Delete
#
[424] Fix | Delete
# This used to be strict parsing based on the RFC2109 and RFC2068
[425] Fix | Delete
# specifications. I have since discovered that MSIE 3.0x doesn't
[426] Fix | Delete
# follow the character rules outlined in those specs. As a
[427] Fix | Delete
# result, the parsing rules here are less strict.
[428] Fix | Delete
#
[429] Fix | Delete
[430] Fix | Delete
_LegalKeyChars = r"\w\d!#%&'~_`><@,:/\$\*\+\-\.\^\|\)\(\?\}\{\="
[431] Fix | Delete
_LegalValueChars = _LegalKeyChars + r'\[\]'
[432] Fix | Delete
_CookiePattern = re.compile(r"""
[433] Fix | Delete
\s* # Optional whitespace at start of cookie
[434] Fix | Delete
(?P<key> # Start of group 'key'
[435] Fix | Delete
[""" + _LegalKeyChars + r"""]+? # Any word of at least one letter
[436] Fix | Delete
) # End of group 'key'
[437] Fix | Delete
( # Optional group: there may not be a value.
[438] Fix | Delete
\s*=\s* # Equal Sign
[439] Fix | Delete
(?P<val> # Start of group 'val'
[440] Fix | Delete
"(?:[^\\"]|\\.)*" # Any doublequoted string
[441] Fix | Delete
| # or
[442] Fix | Delete
\w{3},\s[\w\d\s-]{9,11}\s[\d:]{8}\sGMT # Special case for "expires" attr
[443] Fix | Delete
| # or
[444] Fix | Delete
[""" + _LegalValueChars + r"""]* # Any word or empty string
[445] Fix | Delete
) # End of group 'val'
[446] Fix | Delete
)? # End of optional value group
[447] Fix | Delete
\s* # Any number of spaces.
[448] Fix | Delete
(\s+|;|$) # Ending either at space, semicolon, or EOS.
[449] Fix | Delete
""", re.ASCII | re.VERBOSE) # re.ASCII may be removed if safe.
[450] Fix | Delete
[451] Fix | Delete
[452] Fix | Delete
# At long last, here is the cookie class. Using this class is almost just like
[453] Fix | Delete
# using a dictionary. See this module's docstring for example usage.
[454] Fix | Delete
#
[455] Fix | Delete
class BaseCookie(dict):
[456] Fix | Delete
"""A container class for a set of Morsels."""
[457] Fix | Delete
[458] Fix | Delete
def value_decode(self, val):
[459] Fix | Delete
"""real_value, coded_value = value_decode(STRING)
[460] Fix | Delete
Called prior to setting a cookie's value from the network
[461] Fix | Delete
representation. The VALUE is the value read from HTTP
[462] Fix | Delete
header.
[463] Fix | Delete
Override this function to modify the behavior of cookies.
[464] Fix | Delete
"""
[465] Fix | Delete
return val, val
[466] Fix | Delete
[467] Fix | Delete
def value_encode(self, val):
[468] Fix | Delete
"""real_value, coded_value = value_encode(VALUE)
[469] Fix | Delete
Called prior to setting a cookie's value from the dictionary
[470] Fix | Delete
representation. The VALUE is the value being assigned.
[471] Fix | Delete
Override this function to modify the behavior of cookies.
[472] Fix | Delete
"""
[473] Fix | Delete
strval = str(val)
[474] Fix | Delete
return strval, strval
[475] Fix | Delete
[476] Fix | Delete
def __init__(self, input=None):
[477] Fix | Delete
if input:
[478] Fix | Delete
self.load(input)
[479] Fix | Delete
[480] Fix | Delete
def __set(self, key, real_value, coded_value):
[481] Fix | Delete
"""Private method for setting a cookie's value"""
[482] Fix | Delete
M = self.get(key, Morsel())
[483] Fix | Delete
M.set(key, real_value, coded_value)
[484] Fix | Delete
dict.__setitem__(self, key, M)
[485] Fix | Delete
[486] Fix | Delete
def __setitem__(self, key, value):
[487] Fix | Delete
"""Dictionary style assignment."""
[488] Fix | Delete
if isinstance(value, Morsel):
[489] Fix | Delete
# allow assignment of constructed Morsels (e.g. for pickling)
[490] Fix | Delete
dict.__setitem__(self, key, value)
[491] Fix | Delete
else:
[492] Fix | Delete
rval, cval = self.value_encode(value)
[493] Fix | Delete
self.__set(key, rval, cval)
[494] Fix | Delete
[495] Fix | Delete
def output(self, attrs=None, header="Set-Cookie:", sep="\015\012"):
[496] Fix | Delete
"""Return a string suitable for HTTP."""
[497] Fix | Delete
result = []
[498] Fix | Delete
items = sorted(self.items())
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function