diff options
author | Georg Brandl <georg@python.org> | 2005-08-24 22:34:21 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2005-08-24 22:34:21 (GMT) |
commit | 532efabf1dcae183c586b78007baaaf59e4d61ca (patch) | |
tree | d840a3112bafdcec8b4a969b08fbaa18b08d72cf /Lib/Cookie.py | |
parent | e1b13d20199f79ffd3407bbb14cc09b1b8fd70d2 (diff) | |
download | cpython-532efabf1dcae183c586b78007baaaf59e4d61ca.zip cpython-532efabf1dcae183c586b78007baaaf59e4d61ca.tar.gz cpython-532efabf1dcae183c586b78007baaaf59e4d61ca.tar.bz2 |
patch #848017: make Cookie more RFC-compliant.
Diffstat (limited to 'Lib/Cookie.py')
-rw-r--r-- | Lib/Cookie.py | 51 |
1 files changed, 23 insertions, 28 deletions
diff --git a/Lib/Cookie.py b/Lib/Cookie.py index 4661d7c..fa2e05e 100644 --- a/Lib/Cookie.py +++ b/Lib/Cookie.py @@ -69,9 +69,8 @@ a dictionary. >>> C = Cookie.SmartCookie() >>> C["fig"] = "newton" >>> C["sugar"] = "wafer" - >>> print C - Set-Cookie: fig=newton; - Set-Cookie: sugar=wafer; + >>> C.output() + 'Set-Cookie: fig=newton\r\nSet-Cookie: sugar=wafer' Notice that the printable representation of a Cookie is the appropriate format for a Set-Cookie: header. This is the @@ -82,9 +81,9 @@ attributes by using the .output() function >>> C["rocky"] = "road" >>> C["rocky"]["path"] = "/cookie" >>> print C.output(header="Cookie:") - Cookie: rocky=road; Path=/cookie; + Cookie: rocky=road; Path=/cookie >>> print C.output(attrs=[], header="Cookie:") - Cookie: rocky=road; + Cookie: rocky=road The load() method of a Cookie extracts cookies from a string. In a CGI script, you would use this method to extract the cookies from the @@ -92,9 +91,8 @@ HTTP_COOKIE environment variable. >>> C = Cookie.SmartCookie() >>> C.load("chips=ahoy; vienna=finger") - >>> print C - Set-Cookie: chips=ahoy; - Set-Cookie: vienna=finger; + >>> C.output() + 'Set-Cookie: chips=ahoy\r\nSet-Cookie: vienna=finger' The load() method is darn-tootin smart about identifying cookies within a string. Escaped quotation marks, nested semicolons, and other @@ -103,7 +101,7 @@ such trickeries do not confuse it. >>> C = Cookie.SmartCookie() >>> C.load('keebler="E=everybody; L=\\"Loves\\"; fudge=\\012;";') >>> 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 Cookie attributes. Here's an example which sets the Path @@ -113,7 +111,7 @@ attribute. >>> C["oreo"] = "doublestuff" >>> C["oreo"]["path"] = "/" >>> print C - Set-Cookie: oreo=doublestuff; Path=/; + Set-Cookie: oreo=doublestuff; Path=/ Each dictionary element has a 'value' attribute, which gives you back the value associated with the key. @@ -144,9 +142,8 @@ the value to a string, when the values are set dictionary-style. '7' >>> C["string"].value 'seven' - >>> print C - Set-Cookie: number=7; - Set-Cookie: string=seven; + >>> C.output() + 'Set-Cookie: number=7\r\nSet-Cookie: string=seven' SerialCookie @@ -165,9 +162,8 @@ values, however.) 7 >>> C["string"].value 'seven' - >>> print C - Set-Cookie: number="I7\012."; - Set-Cookie: string="S'seven'\012p1\012."; + >>> C.output() + 'Set-Cookie: number="I7\\012."\r\nSet-Cookie: string="S\'seven\'\\012p1\\012."' Be warned, however, if SerialCookie cannot de-serialize a value (because it isn't a valid pickle'd object), IT WILL RAISE AN EXCEPTION. @@ -190,9 +186,8 @@ as a string. 7 >>> C["string"].value 'seven' - >>> print C - Set-Cookie: number="I7\012."; - Set-Cookie: string=seven; + >>> C.output() + 'Set-Cookie: number="I7\\012."\r\nSet-Cookie: string=seven' Backwards Compatibility @@ -228,7 +223,7 @@ __all__ = ["CookieError","BaseCookie","SimpleCookie","SerialCookie", "SmartCookie","Cookie"] _nulljoin = ''.join -_spacejoin = ' '.join +_semispacejoin = '; '.join # # Define an exception visible to External modules @@ -485,7 +480,7 @@ class Morsel(dict): RA = result.append # First, the key=value pair - RA("%s=%s;" % (self.key, self.coded_value)) + RA("%s=%s" % (self.key, self.coded_value)) # Now add any defined attributes if attrs is None: @@ -496,16 +491,16 @@ class Morsel(dict): if V == "": continue if K not in attrs: continue if K == "expires" and type(V) == type(1): - RA("%s=%s;" % (self._reserved[K], _getdate(V))) + RA("%s=%s" % (self._reserved[K], _getdate(V))) elif K == "max-age" and type(V) == type(1): - RA("%s=%d;" % (self._reserved[K], V)) + RA("%s=%d" % (self._reserved[K], V)) elif K == "secure": - RA("%s;" % self._reserved[K]) + RA(str(self._reserved[K])) else: - RA("%s=%s;" % (self._reserved[K], V)) + RA("%s=%s" % (self._reserved[K], V)) # Return the result - return _spacejoin(result) + return _semispacejoin(result) # end OutputString # end Morsel class @@ -581,7 +576,7 @@ class BaseCookie(dict): self.__set(key, rval, cval) # end __setitem__ - def output(self, attrs=None, header="Set-Cookie:", sep="\n"): + def output(self, attrs=None, header="Set-Cookie:", sep="\015\012"): """Return a string suitable for HTTP.""" result = [] items = self.items() @@ -599,7 +594,7 @@ class BaseCookie(dict): items.sort() for K,V in items: L.append( '%s=%s' % (K,repr(V.value) ) ) - return '<%s: %s>' % (self.__class__.__name__, _spacejoin(L)) + return '<%s: %s>' % (self.__class__.__name__, _semispacejoin(L)) def js_output(self, attrs=None): """Return a string suitable for JavaScript.""" |