Updated version of Cookie.py (rev. 2.29) from timo

This commit is contained in:
Andrew M. Kuchling 2000-08-24 11:52:33 +00:00
parent 45ca3331d9
commit 0b29b11187

View file

@ -26,7 +26,7 @@
# #
#### ####
# #
# $Id$ # Id: Cookie.py,v 2.29 2000/08/23 05:28:49 timo Exp
# by Timothy O'Malley <timo@alum.mit.edu> # by Timothy O'Malley <timo@alum.mit.edu>
# #
# Cookie.py is a Python module for the handling of HTTP # Cookie.py is a Python module for the handling of HTTP
@ -34,7 +34,7 @@
# information on cookies. # information on cookies.
# #
# The original idea to treat Cookies as a dictionary came from # The original idea to treat Cookies as a dictionary came from
# Dave Mitchel (davem@magnet.com) in 1995, when he released the # Dave Mitchell (davem@magnet.com) in 1995, when he released the
# first version of nscookie.py. # first version of nscookie.py.
# #
#### ####
@ -69,7 +69,7 @@ a dictionary.
>>> C = Cookie.SmartCookie() >>> C = Cookie.SmartCookie()
>>> C["fig"] = "newton" >>> C["fig"] = "newton"
>>> C["sugar"] = "wafer" >>> C["sugar"] = "wafer"
>>> C >>> print C
Set-Cookie: sugar=wafer; Set-Cookie: sugar=wafer;
Set-Cookie: fig=newton; Set-Cookie: fig=newton;
@ -92,7 +92,7 @@ HTTP_COOKIE environment variable.
>>> C = Cookie.SmartCookie() >>> C = Cookie.SmartCookie()
>>> C.load("chips=ahoy; vienna=finger") >>> C.load("chips=ahoy; vienna=finger")
>>> C >>> print C
Set-Cookie: vienna=finger; Set-Cookie: vienna=finger;
Set-Cookie: chips=ahoy; Set-Cookie: chips=ahoy;
@ -102,7 +102,7 @@ such trickeries do not confuse it.
>>> C = Cookie.SmartCookie() >>> C = Cookie.SmartCookie()
>>> C.load('keebler="E=everybody; L=\\"Loves\\"; fudge=\\012;";') >>> C.load('keebler="E=everybody; L=\\"Loves\\"; fudge=\\012;";')
>>> C >>> print C
Set-Cookie: keebler="E=everybody; L=\"Loves\"; fudge=\012;"; Set-Cookie: keebler="E=everybody; L=\"Loves\"; fudge=\012;";
Each element of the Cookie also supports all of the RFC 2109 Each element of the Cookie also supports all of the RFC 2109
@ -112,7 +112,7 @@ attribute.
>>> C = Cookie.SmartCookie() >>> C = Cookie.SmartCookie()
>>> C["oreo"] = "doublestuff" >>> C["oreo"] = "doublestuff"
>>> C["oreo"]["path"] = "/" >>> C["oreo"]["path"] = "/"
>>> C >>> print C
Set-Cookie: oreo="doublestuff"; Path=/; Set-Cookie: oreo="doublestuff"; Path=/;
Each dictionary element has a 'value' attribute, which gives you Each dictionary element has a 'value' attribute, which gives you
@ -144,7 +144,7 @@ the value to a string, when the values are set dictionary-style.
'7' '7'
>>> C["string"].value >>> C["string"].value
'seven' 'seven'
>>> C >>> print C
Set-Cookie: number=7; Set-Cookie: number=7;
Set-Cookie: string=seven; Set-Cookie: string=seven;
@ -165,7 +165,7 @@ values, however.)
7 7
>>> C["string"].value >>> C["string"].value
'seven' 'seven'
>>> C >>> print C
Set-Cookie: number="I7\012."; Set-Cookie: number="I7\012.";
Set-Cookie: string="S'seven'\012p1\012."; Set-Cookie: string="S'seven'\012p1\012.";
@ -190,7 +190,7 @@ as a string.
7 7
>>> C["string"].value >>> C["string"].value
'seven' 'seven'
>>> C >>> print C
Set-Cookie: number="I7\012."; Set-Cookie: number="I7\012.";
Set-Cookie: string=seven; Set-Cookie: string=seven;
@ -309,7 +309,7 @@ _Translator = {
} }
def _quote(str, LegalChars=_LegalChars, def _quote(str, LegalChars=_LegalChars,
join=string.join, idmap=string._idmap, translate=string.translate): join=string.join, idmap=string._idmap, translate=string.translate):
# #
# If the string does not need to be double-quoted, # If the string does not need to be double-quoted,
# then just return the string. Otherwise, surround # then just return the string. Otherwise, surround
@ -317,9 +317,9 @@ def _quote(str, LegalChars=_LegalChars,
# special characters. # special characters.
# #
if "" == translate(str, idmap, LegalChars): if "" == translate(str, idmap, LegalChars):
return str return str
else: else:
return '"' + join( map(_Translator.get, str, str), "" ) + '"' return '"' + join( map(_Translator.get, str, str), "" ) + '"'
# end _quote # end _quote
@ -440,7 +440,7 @@ class Morsel(UserDict):
# end __setitem__ # end __setitem__
def isReservedKey(self, K): def isReservedKey(self, K):
return string.lower(K) in self._reserved_keys return string.lower(K) in self._reserved_keys
# end isReservedKey # end isReservedKey
def set(self, key, val, coded_val, def set(self, key, val, coded_val,
@ -462,8 +462,12 @@ class Morsel(UserDict):
def output(self, attrs=None, header = "Set-Cookie:"): def output(self, attrs=None, header = "Set-Cookie:"):
return "%s %s" % ( header, self.OutputString(attrs) ) return "%s %s" % ( header, self.OutputString(attrs) )
__repr__ = output __str__ = output
def __repr__(self):
return '<%s: %s=%s>' % (self.__class__.__name__,
self.key, repr(self.value) )
def js_output(self, attrs=None): def js_output(self, attrs=None):
# Print javascript # Print javascript
return """ return """
@ -488,7 +492,7 @@ class Morsel(UserDict):
if attrs == None: if attrs == None:
attrs = self._reserved_keys attrs = self._reserved_keys
for K,V in self.items(): for K,V in self.items():
if not V: continue if V == "": continue
if K not in attrs: continue if K not in attrs: continue
if K == "expires" and type(V) == type(1): if K == "expires" and type(V) == type(1):
RA("%s=%s;" % (self._reserved[K], _getdate(V))) RA("%s=%s;" % (self._reserved[K], _getdate(V)))
@ -585,8 +589,14 @@ class BaseCookie(UserDict):
return string.join(result, sep) return string.join(result, sep)
# end output # end output
__repr__ = output __str__ = output
def __repr__(self):
L = []
for K,V in self.items():
L.append( '%s=%s' % (K,repr(V.value) ) )
return '<%s: %s>' % (self.__class__.__name__, string.join(L))
def js_output(self, attrs=None): def js_output(self, attrs=None):
"""Return a string suitable for JavaScript.""" """Return a string suitable for JavaScript."""
result = [] result = []
@ -630,13 +640,13 @@ class BaseCookie(UserDict):
M[ K[1:] ] = V M[ K[1:] ] = V
elif string.lower(K) in Morsel._reserved_keys: elif string.lower(K) in Morsel._reserved_keys:
if M: if M:
M[ K ] = V M[ K ] = _unquote(V)
else: else:
rval, cval = self.value_decode(V) rval, cval = self.value_decode(V)
self.__set(K, rval, cval) self.__set(K, rval, cval)
M = self[K] M = self[K]
return return
# end __ParseString # end __ParseString
# end BaseCookie class # end BaseCookie class
@ -716,3 +726,7 @@ Cookie = SmartCookie
# #
# should add a test routine? # should add a test routine?
# #
#Local Variables:
#tab-width: 4
#end: