Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | |
| 4 | #### |
| 5 | # Copyright 2000 by Timothy O'Malley <timo@alum.mit.edu> |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 6 | # |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 7 | # All Rights Reserved |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 8 | # |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 9 | # Permission to use, copy, modify, and distribute this software |
| 10 | # and its documentation for any purpose and without fee is hereby |
| 11 | # granted, provided that the above copyright notice appear in all |
| 12 | # copies and that both that copyright notice and this permission |
| 13 | # notice appear in supporting documentation, and that the name of |
| 14 | # Timothy O'Malley not be used in advertising or publicity |
| 15 | # pertaining to distribution of the software without specific, written |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 16 | # prior permission. |
| 17 | # |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 18 | # Timothy O'Malley DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS |
| 19 | # SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 20 | # AND FITNESS, IN NO EVENT SHALL Timothy O'Malley BE LIABLE FOR |
| 21 | # ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 22 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 23 | # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS |
| 24 | # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 25 | # PERFORMANCE OF THIS SOFTWARE. |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 26 | # |
| 27 | #### |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 28 | # |
| 29 | # Id: Cookie.py,v 2.29 2000/08/23 05:28:49 timo Exp |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 30 | # by Timothy O'Malley <timo@alum.mit.edu> |
| 31 | # |
| 32 | # Cookie.py is a Python module for the handling of HTTP |
| 33 | # cookies as a Python dictionary. See RFC 2109 for more |
| 34 | # information on cookies. |
| 35 | # |
| 36 | # The original idea to treat Cookies as a dictionary came from |
Andrew M. Kuchling | 0b29b11 | 2000-08-24 11:52:33 +0000 | [diff] [blame] | 37 | # Dave Mitchell (davem@magnet.com) in 1995, when he released the |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 38 | # first version of nscookie.py. |
| 39 | # |
| 40 | #### |
| 41 | |
Guido van Rossum | 58b6f5b | 2001-04-06 19:39:11 +0000 | [diff] [blame] | 42 | r""" |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 43 | Here's a sample session to show how to use this module. |
| 44 | At the moment, this is the only documentation. |
| 45 | |
| 46 | The Basics |
| 47 | ---------- |
| 48 | |
| 49 | Importing is easy.. |
| 50 | |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 51 | >>> from http import cookies |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 52 | |
Georg Brandl | 6101395 | 2008-05-28 15:56:30 +0000 | [diff] [blame] | 53 | Most of the time you start by creating a cookie. |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 54 | |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 55 | >>> C = cookies.SimpleCookie() |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 56 | |
| 57 | Once you've created your Cookie, you can add values just as if it were |
| 58 | a dictionary. |
| 59 | |
Georg Brandl | 6101395 | 2008-05-28 15:56:30 +0000 | [diff] [blame] | 60 | >>> C = cookies.SimpleCookie() |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 61 | >>> C["fig"] = "newton" |
| 62 | >>> C["sugar"] = "wafer" |
Georg Brandl | 532efab | 2005-08-24 22:34:21 +0000 | [diff] [blame] | 63 | >>> C.output() |
| 64 | 'Set-Cookie: fig=newton\r\nSet-Cookie: sugar=wafer' |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 65 | |
| 66 | Notice that the printable representation of a Cookie is the |
| 67 | appropriate format for a Set-Cookie: header. This is the |
| 68 | default behavior. You can change the header and printed |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 69 | attributes by using the .output() function |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 70 | |
Georg Brandl | 6101395 | 2008-05-28 15:56:30 +0000 | [diff] [blame] | 71 | >>> C = cookies.SimpleCookie() |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 72 | >>> C["rocky"] = "road" |
| 73 | >>> C["rocky"]["path"] = "/cookie" |
Guido van Rossum | fff80df | 2007-02-09 20:33:44 +0000 | [diff] [blame] | 74 | >>> print(C.output(header="Cookie:")) |
Georg Brandl | 532efab | 2005-08-24 22:34:21 +0000 | [diff] [blame] | 75 | Cookie: rocky=road; Path=/cookie |
Guido van Rossum | fff80df | 2007-02-09 20:33:44 +0000 | [diff] [blame] | 76 | >>> print(C.output(attrs=[], header="Cookie:")) |
Georg Brandl | 532efab | 2005-08-24 22:34:21 +0000 | [diff] [blame] | 77 | Cookie: rocky=road |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 78 | |
| 79 | The load() method of a Cookie extracts cookies from a string. In a |
| 80 | CGI script, you would use this method to extract the cookies from the |
| 81 | HTTP_COOKIE environment variable. |
| 82 | |
Georg Brandl | 6101395 | 2008-05-28 15:56:30 +0000 | [diff] [blame] | 83 | >>> C = cookies.SimpleCookie() |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 84 | >>> C.load("chips=ahoy; vienna=finger") |
Georg Brandl | 532efab | 2005-08-24 22:34:21 +0000 | [diff] [blame] | 85 | >>> C.output() |
| 86 | 'Set-Cookie: chips=ahoy\r\nSet-Cookie: vienna=finger' |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 87 | |
| 88 | The load() method is darn-tootin smart about identifying cookies |
| 89 | within a string. Escaped quotation marks, nested semicolons, and other |
| 90 | such trickeries do not confuse it. |
| 91 | |
Georg Brandl | 6101395 | 2008-05-28 15:56:30 +0000 | [diff] [blame] | 92 | >>> C = cookies.SimpleCookie() |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 93 | >>> C.load('keebler="E=everybody; L=\\"Loves\\"; fudge=\\012;";') |
Guido van Rossum | fff80df | 2007-02-09 20:33:44 +0000 | [diff] [blame] | 94 | >>> print(C) |
Georg Brandl | 532efab | 2005-08-24 22:34:21 +0000 | [diff] [blame] | 95 | Set-Cookie: keebler="E=everybody; L=\"Loves\"; fudge=\012;" |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 96 | |
| 97 | Each element of the Cookie also supports all of the RFC 2109 |
| 98 | Cookie attributes. Here's an example which sets the Path |
| 99 | attribute. |
| 100 | |
Georg Brandl | 6101395 | 2008-05-28 15:56:30 +0000 | [diff] [blame] | 101 | >>> C = cookies.SimpleCookie() |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 102 | >>> C["oreo"] = "doublestuff" |
| 103 | >>> C["oreo"]["path"] = "/" |
Guido van Rossum | fff80df | 2007-02-09 20:33:44 +0000 | [diff] [blame] | 104 | >>> print(C) |
Georg Brandl | 532efab | 2005-08-24 22:34:21 +0000 | [diff] [blame] | 105 | Set-Cookie: oreo=doublestuff; Path=/ |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 106 | |
| 107 | Each dictionary element has a 'value' attribute, which gives you |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 108 | back the value associated with the key. |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 109 | |
Georg Brandl | 6101395 | 2008-05-28 15:56:30 +0000 | [diff] [blame] | 110 | >>> C = cookies.SimpleCookie() |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 111 | >>> C["twix"] = "none for you" |
| 112 | >>> C["twix"].value |
| 113 | 'none for you' |
| 114 | |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 115 | The SimpleCookie expects that all values should be standard strings. |
| 116 | Just to be sure, SimpleCookie invokes the str() builtin to convert |
| 117 | the value to a string, when the values are set dictionary-style. |
| 118 | |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 119 | >>> C = cookies.SimpleCookie() |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 120 | >>> C["number"] = 7 |
| 121 | >>> C["string"] = "seven" |
| 122 | >>> C["number"].value |
| 123 | '7' |
| 124 | >>> C["string"].value |
| 125 | 'seven' |
Georg Brandl | 532efab | 2005-08-24 22:34:21 +0000 | [diff] [blame] | 126 | >>> C.output() |
| 127 | 'Set-Cookie: number=7\r\nSet-Cookie: string=seven' |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 128 | |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 129 | Finis. |
| 130 | """ #" |
| 131 | # ^ |
| 132 | # |----helps out font-lock |
| 133 | |
| 134 | # |
| 135 | # Import our required modules |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 136 | # |
Martin v. Löwis | 02d893c | 2001-08-02 07:15:29 +0000 | [diff] [blame] | 137 | import string |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 138 | |
Guido van Rossum | 99603b0 | 2007-07-20 00:22:32 +0000 | [diff] [blame] | 139 | from pickle import dumps, loads |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 140 | |
Andrew M. Kuchling | 7877a76 | 2002-12-29 16:44:31 +0000 | [diff] [blame] | 141 | import re, warnings |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 142 | |
Georg Brandl | 6101395 | 2008-05-28 15:56:30 +0000 | [diff] [blame] | 143 | __all__ = ["CookieError", "BaseCookie", "SimpleCookie"] |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 144 | |
Fred Drake | d451ec1 | 2002-04-26 02:29:55 +0000 | [diff] [blame] | 145 | _nulljoin = ''.join |
Georg Brandl | 532efab | 2005-08-24 22:34:21 +0000 | [diff] [blame] | 146 | _semispacejoin = '; '.join |
Georg Brandl | 8246c43 | 2005-08-25 07:32:42 +0000 | [diff] [blame] | 147 | _spacejoin = ' '.join |
Fred Drake | d451ec1 | 2002-04-26 02:29:55 +0000 | [diff] [blame] | 148 | |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 149 | # |
| 150 | # Define an exception visible to External modules |
| 151 | # |
| 152 | class CookieError(Exception): |
| 153 | pass |
| 154 | |
| 155 | |
| 156 | # These quoting routines conform to the RFC2109 specification, which in |
| 157 | # turn references the character definitions from RFC2068. They provide |
| 158 | # a two-way quoting algorithm. Any non-text character is translated |
| 159 | # into a 4 character sequence: a forward-slash followed by the |
| 160 | # three-digit octal equivalent of the character. Any '\' or '"' is |
| 161 | # quoted with a preceeding '\' slash. |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 162 | # |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 163 | # These are taken from RFC2068 and RFC2109. |
| 164 | # _LegalChars is the list of chars which don't require "'s |
| 165 | # _Translator hash-table for fast quoting |
| 166 | # |
Fred Drake | 79e75e1 | 2001-07-20 19:05:50 +0000 | [diff] [blame] | 167 | _LegalChars = string.ascii_letters + string.digits + "!#$%&'*+-.^_`|~" |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 168 | _Translator = { |
| 169 | '\000' : '\\000', '\001' : '\\001', '\002' : '\\002', |
| 170 | '\003' : '\\003', '\004' : '\\004', '\005' : '\\005', |
| 171 | '\006' : '\\006', '\007' : '\\007', '\010' : '\\010', |
| 172 | '\011' : '\\011', '\012' : '\\012', '\013' : '\\013', |
| 173 | '\014' : '\\014', '\015' : '\\015', '\016' : '\\016', |
| 174 | '\017' : '\\017', '\020' : '\\020', '\021' : '\\021', |
| 175 | '\022' : '\\022', '\023' : '\\023', '\024' : '\\024', |
| 176 | '\025' : '\\025', '\026' : '\\026', '\027' : '\\027', |
| 177 | '\030' : '\\030', '\031' : '\\031', '\032' : '\\032', |
| 178 | '\033' : '\\033', '\034' : '\\034', '\035' : '\\035', |
| 179 | '\036' : '\\036', '\037' : '\\037', |
| 180 | |
| 181 | '"' : '\\"', '\\' : '\\\\', |
| 182 | |
| 183 | '\177' : '\\177', '\200' : '\\200', '\201' : '\\201', |
| 184 | '\202' : '\\202', '\203' : '\\203', '\204' : '\\204', |
| 185 | '\205' : '\\205', '\206' : '\\206', '\207' : '\\207', |
| 186 | '\210' : '\\210', '\211' : '\\211', '\212' : '\\212', |
| 187 | '\213' : '\\213', '\214' : '\\214', '\215' : '\\215', |
| 188 | '\216' : '\\216', '\217' : '\\217', '\220' : '\\220', |
| 189 | '\221' : '\\221', '\222' : '\\222', '\223' : '\\223', |
| 190 | '\224' : '\\224', '\225' : '\\225', '\226' : '\\226', |
| 191 | '\227' : '\\227', '\230' : '\\230', '\231' : '\\231', |
| 192 | '\232' : '\\232', '\233' : '\\233', '\234' : '\\234', |
| 193 | '\235' : '\\235', '\236' : '\\236', '\237' : '\\237', |
| 194 | '\240' : '\\240', '\241' : '\\241', '\242' : '\\242', |
| 195 | '\243' : '\\243', '\244' : '\\244', '\245' : '\\245', |
| 196 | '\246' : '\\246', '\247' : '\\247', '\250' : '\\250', |
| 197 | '\251' : '\\251', '\252' : '\\252', '\253' : '\\253', |
| 198 | '\254' : '\\254', '\255' : '\\255', '\256' : '\\256', |
| 199 | '\257' : '\\257', '\260' : '\\260', '\261' : '\\261', |
| 200 | '\262' : '\\262', '\263' : '\\263', '\264' : '\\264', |
| 201 | '\265' : '\\265', '\266' : '\\266', '\267' : '\\267', |
| 202 | '\270' : '\\270', '\271' : '\\271', '\272' : '\\272', |
| 203 | '\273' : '\\273', '\274' : '\\274', '\275' : '\\275', |
| 204 | '\276' : '\\276', '\277' : '\\277', '\300' : '\\300', |
| 205 | '\301' : '\\301', '\302' : '\\302', '\303' : '\\303', |
| 206 | '\304' : '\\304', '\305' : '\\305', '\306' : '\\306', |
| 207 | '\307' : '\\307', '\310' : '\\310', '\311' : '\\311', |
| 208 | '\312' : '\\312', '\313' : '\\313', '\314' : '\\314', |
| 209 | '\315' : '\\315', '\316' : '\\316', '\317' : '\\317', |
| 210 | '\320' : '\\320', '\321' : '\\321', '\322' : '\\322', |
| 211 | '\323' : '\\323', '\324' : '\\324', '\325' : '\\325', |
| 212 | '\326' : '\\326', '\327' : '\\327', '\330' : '\\330', |
| 213 | '\331' : '\\331', '\332' : '\\332', '\333' : '\\333', |
| 214 | '\334' : '\\334', '\335' : '\\335', '\336' : '\\336', |
| 215 | '\337' : '\\337', '\340' : '\\340', '\341' : '\\341', |
| 216 | '\342' : '\\342', '\343' : '\\343', '\344' : '\\344', |
| 217 | '\345' : '\\345', '\346' : '\\346', '\347' : '\\347', |
| 218 | '\350' : '\\350', '\351' : '\\351', '\352' : '\\352', |
| 219 | '\353' : '\\353', '\354' : '\\354', '\355' : '\\355', |
| 220 | '\356' : '\\356', '\357' : '\\357', '\360' : '\\360', |
| 221 | '\361' : '\\361', '\362' : '\\362', '\363' : '\\363', |
| 222 | '\364' : '\\364', '\365' : '\\365', '\366' : '\\366', |
| 223 | '\367' : '\\367', '\370' : '\\370', '\371' : '\\371', |
| 224 | '\372' : '\\372', '\373' : '\\373', '\374' : '\\374', |
| 225 | '\375' : '\\375', '\376' : '\\376', '\377' : '\\377' |
| 226 | } |
| 227 | |
Walter Dörwald | 3f1e65c | 2007-06-08 15:33:46 +0000 | [diff] [blame] | 228 | def _quote(str, LegalChars=_LegalChars): |
Georg Brandl | 9cf32a1 | 2009-09-04 08:28:01 +0000 | [diff] [blame] | 229 | r"""Quote a string for use in a cookie header. |
| 230 | |
| 231 | If the string does not need to be double-quoted, then just return the |
| 232 | string. Otherwise, surround the string in doublequotes and quote |
| 233 | (with a \) special characters. |
| 234 | """ |
Guido van Rossum | 0c41e88 | 2007-07-03 16:46:40 +0000 | [diff] [blame] | 235 | if all(c in LegalChars for c in str): |
Fred Drake | ff5364a | 2000-08-24 14:40:35 +0000 | [diff] [blame] | 236 | return str |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 237 | else: |
Fred Drake | d451ec1 | 2002-04-26 02:29:55 +0000 | [diff] [blame] | 238 | return '"' + _nulljoin( map(_Translator.get, str, str) ) + '"' |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 239 | |
| 240 | |
| 241 | _OctalPatt = re.compile(r"\\[0-3][0-7][0-7]") |
| 242 | _QuotePatt = re.compile(r"[\\].") |
| 243 | |
Fred Drake | d451ec1 | 2002-04-26 02:29:55 +0000 | [diff] [blame] | 244 | def _unquote(str): |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 245 | # If there aren't any doublequotes, |
| 246 | # then there can't be any special characters. See RFC 2109. |
| 247 | if len(str) < 2: |
| 248 | return str |
| 249 | if str[0] != '"' or str[-1] != '"': |
| 250 | return str |
| 251 | |
| 252 | # We have to assume that we must decode this string. |
| 253 | # Down to work. |
| 254 | |
| 255 | # Remove the "s |
| 256 | str = str[1:-1] |
Fred Drake | ff5364a | 2000-08-24 14:40:35 +0000 | [diff] [blame] | 257 | |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 258 | # Check for special sequences. Examples: |
| 259 | # \012 --> \n |
| 260 | # \" --> " |
| 261 | # |
| 262 | i = 0 |
| 263 | n = len(str) |
| 264 | res = [] |
| 265 | while 0 <= i < n: |
| 266 | Omatch = _OctalPatt.search(str, i) |
| 267 | Qmatch = _QuotePatt.search(str, i) |
| 268 | if not Omatch and not Qmatch: # Neither matched |
| 269 | res.append(str[i:]) |
| 270 | break |
| 271 | # else: |
| 272 | j = k = -1 |
| 273 | if Omatch: j = Omatch.start(0) |
| 274 | if Qmatch: k = Qmatch.start(0) |
| 275 | if Qmatch and ( not Omatch or k < j ): # QuotePatt matched |
| 276 | res.append(str[i:k]) |
| 277 | res.append(str[k+1]) |
| 278 | i = k+2 |
| 279 | else: # OctalPatt matched |
| 280 | res.append(str[i:j]) |
Fred Drake | d451ec1 | 2002-04-26 02:29:55 +0000 | [diff] [blame] | 281 | res.append( chr( int(str[j+1:j+4], 8) ) ) |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 282 | i = j+4 |
Fred Drake | d451ec1 | 2002-04-26 02:29:55 +0000 | [diff] [blame] | 283 | return _nulljoin(res) |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 284 | |
| 285 | # The _getdate() routine is used to set the expiration time in |
| 286 | # the cookie's HTTP header. By default, _getdate() returns the |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 287 | # current time in the appropriate "expires" format for a |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 288 | # Set-Cookie header. The one optional argument is an offset from |
| 289 | # now, in seconds. For example, an offset of -3600 means "one hour ago". |
| 290 | # The offset may be a floating point number. |
| 291 | # |
| 292 | |
| 293 | _weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] |
| 294 | |
| 295 | _monthname = [None, |
| 296 | 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', |
| 297 | 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] |
| 298 | |
| 299 | def _getdate(future=0, weekdayname=_weekdayname, monthname=_monthname): |
| 300 | from time import gmtime, time |
| 301 | now = time() |
| 302 | year, month, day, hh, mm, ss, wd, y, z = gmtime(now + future) |
| 303 | return "%s, %02d-%3s-%4d %02d:%02d:%02d GMT" % \ |
| 304 | (weekdayname[wd], day, monthname[month], year, hh, mm, ss) |
| 305 | |
| 306 | |
Raymond Hettinger | 0a2963c | 2002-06-26 15:19:01 +0000 | [diff] [blame] | 307 | class Morsel(dict): |
Georg Brandl | 9cf32a1 | 2009-09-04 08:28:01 +0000 | [diff] [blame] | 308 | """A class to hold ONE key,value pair. |
| 309 | |
| 310 | In a cookie, each such pair may have several attributes, so this class is |
| 311 | used to keep the attributes associated with the appropriate key,value pair. |
| 312 | This class also includes a coded_value attribute, which is used to hold |
| 313 | the network representation of the value. This is most useful when Python |
| 314 | objects are pickled for network transit. |
| 315 | """ |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 316 | # RFC 2109 lists these attributes as reserved: |
| 317 | # path comment domain |
| 318 | # max-age secure version |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 319 | # |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 320 | # For historical reasons, these attributes are also reserved: |
| 321 | # expires |
| 322 | # |
Benjamin Peterson | 35e661c | 2008-09-06 19:37:35 +0000 | [diff] [blame] | 323 | # This is an extension from Microsoft: |
| 324 | # httponly |
| 325 | # |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 326 | # This dictionary provides a mapping from the lowercase |
| 327 | # variant on the left to the appropriate traditional |
| 328 | # formatting on the right. |
| 329 | _reserved = { "expires" : "expires", |
| 330 | "path" : "Path", |
| 331 | "comment" : "Comment", |
| 332 | "domain" : "Domain", |
| 333 | "max-age" : "Max-Age", |
| 334 | "secure" : "secure", |
Benjamin Peterson | 35e661c | 2008-09-06 19:37:35 +0000 | [diff] [blame] | 335 | "httponly" : "httponly", |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 336 | "version" : "Version", |
| 337 | } |
Fred Drake | ff5364a | 2000-08-24 14:40:35 +0000 | [diff] [blame] | 338 | |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 339 | def __init__(self): |
| 340 | # Set defaults |
| 341 | self.key = self.value = self.coded_value = None |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 342 | |
| 343 | # Set default attributes |
Raymond Hettinger | 0a2963c | 2002-06-26 15:19:01 +0000 | [diff] [blame] | 344 | for K in self._reserved: |
| 345 | dict.__setitem__(self, K, "") |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 346 | |
| 347 | def __setitem__(self, K, V): |
Fred Drake | d451ec1 | 2002-04-26 02:29:55 +0000 | [diff] [blame] | 348 | K = K.lower() |
Raymond Hettinger | 0a2963c | 2002-06-26 15:19:01 +0000 | [diff] [blame] | 349 | if not K in self._reserved: |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 350 | raise CookieError("Invalid Attribute %s" % K) |
Raymond Hettinger | 0a2963c | 2002-06-26 15:19:01 +0000 | [diff] [blame] | 351 | dict.__setitem__(self, K, V) |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 352 | |
| 353 | def isReservedKey(self, K): |
Raymond Hettinger | 0a2963c | 2002-06-26 15:19:01 +0000 | [diff] [blame] | 354 | return K.lower() in self._reserved |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 355 | |
Walter Dörwald | 3f1e65c | 2007-06-08 15:33:46 +0000 | [diff] [blame] | 356 | def set(self, key, val, coded_val, LegalChars=_LegalChars): |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 357 | # First we verify that the key isn't a reserved word |
| 358 | # Second we make sure it only contains legal characters |
Raymond Hettinger | 0a2963c | 2002-06-26 15:19:01 +0000 | [diff] [blame] | 359 | if key.lower() in self._reserved: |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 360 | raise CookieError("Attempt to set a reserved key: %s" % key) |
Guido van Rossum | 0c41e88 | 2007-07-03 16:46:40 +0000 | [diff] [blame] | 361 | if any(c not in LegalChars for c in key): |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 362 | raise CookieError("Illegal key value: %s" % key) |
| 363 | |
| 364 | # It's a good key, so save it. |
| 365 | self.key = key |
| 366 | self.value = val |
| 367 | self.coded_value = coded_val |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 368 | |
| 369 | def output(self, attrs=None, header = "Set-Cookie:"): |
| 370 | return "%s %s" % ( header, self.OutputString(attrs) ) |
| 371 | |
Andrew M. Kuchling | 0b29b11 | 2000-08-24 11:52:33 +0000 | [diff] [blame] | 372 | __str__ = output |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 373 | |
Andrew M. Kuchling | 0b29b11 | 2000-08-24 11:52:33 +0000 | [diff] [blame] | 374 | def __repr__(self): |
| 375 | return '<%s: %s=%s>' % (self.__class__.__name__, |
| 376 | self.key, repr(self.value) ) |
Fred Drake | ff5364a | 2000-08-24 14:40:35 +0000 | [diff] [blame] | 377 | |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 378 | def js_output(self, attrs=None): |
| 379 | # Print javascript |
| 380 | return """ |
Georg Brandl | 03a33ea | 2005-06-26 21:02:49 +0000 | [diff] [blame] | 381 | <script type="text/javascript"> |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 382 | <!-- begin hiding |
Georg Brandl | 03a33ea | 2005-06-26 21:02:49 +0000 | [diff] [blame] | 383 | document.cookie = \"%s\"; |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 384 | // end hiding --> |
| 385 | </script> |
Senthil Kumaran | 3e2ea79 | 2009-04-02 03:02:03 +0000 | [diff] [blame] | 386 | """ % ( self.OutputString(attrs).replace('"',r'\"')) |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 387 | |
| 388 | def OutputString(self, attrs=None): |
| 389 | # Build up our result |
| 390 | # |
| 391 | result = [] |
| 392 | RA = result.append |
Fred Drake | ff5364a | 2000-08-24 14:40:35 +0000 | [diff] [blame] | 393 | |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 394 | # First, the key=value pair |
Georg Brandl | 532efab | 2005-08-24 22:34:21 +0000 | [diff] [blame] | 395 | RA("%s=%s" % (self.key, self.coded_value)) |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 396 | |
| 397 | # Now add any defined attributes |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 398 | if attrs is None: |
Raymond Hettinger | 0a2963c | 2002-06-26 15:19:01 +0000 | [diff] [blame] | 399 | attrs = self._reserved |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 400 | items = sorted(self.items()) |
Tim Peters | 2f228e7 | 2001-05-13 00:19:31 +0000 | [diff] [blame] | 401 | for K,V in items: |
Andrew M. Kuchling | 0b29b11 | 2000-08-24 11:52:33 +0000 | [diff] [blame] | 402 | if V == "": continue |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 403 | if K not in attrs: continue |
| 404 | if K == "expires" and type(V) == type(1): |
Georg Brandl | 532efab | 2005-08-24 22:34:21 +0000 | [diff] [blame] | 405 | RA("%s=%s" % (self._reserved[K], _getdate(V))) |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 406 | elif K == "max-age" and type(V) == type(1): |
Georg Brandl | 532efab | 2005-08-24 22:34:21 +0000 | [diff] [blame] | 407 | RA("%s=%d" % (self._reserved[K], V)) |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 408 | elif K == "secure": |
Georg Brandl | 532efab | 2005-08-24 22:34:21 +0000 | [diff] [blame] | 409 | RA(str(self._reserved[K])) |
Benjamin Peterson | 35e661c | 2008-09-06 19:37:35 +0000 | [diff] [blame] | 410 | elif K == "httponly": |
| 411 | RA(str(self._reserved[K])) |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 412 | else: |
Georg Brandl | 532efab | 2005-08-24 22:34:21 +0000 | [diff] [blame] | 413 | RA("%s=%s" % (self._reserved[K], V)) |
Fred Drake | ff5364a | 2000-08-24 14:40:35 +0000 | [diff] [blame] | 414 | |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 415 | # Return the result |
Georg Brandl | 532efab | 2005-08-24 22:34:21 +0000 | [diff] [blame] | 416 | return _semispacejoin(result) |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 417 | |
| 418 | |
| 419 | # |
| 420 | # Pattern for finding cookie |
| 421 | # |
| 422 | # This used to be strict parsing based on the RFC2109 and RFC2068 |
| 423 | # specifications. I have since discovered that MSIE 3.0x doesn't |
| 424 | # follow the character rules outlined in those specs. As a |
| 425 | # result, the parsing rules here are less strict. |
| 426 | # |
| 427 | |
Andrew M. Kuchling | c05abb3 | 2001-02-20 22:11:24 +0000 | [diff] [blame] | 428 | _LegalCharsPatt = r"[\w\d!#%&'~_`><@,:/\$\*\+\-\.\^\|\)\(\?\}\{\=]" |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 429 | _CookiePattern = re.compile( |
| 430 | r"(?x)" # This is a Verbose pattern |
| 431 | r"(?P<key>" # Start of group 'key' |
Andrew M. Kuchling | c05abb3 | 2001-02-20 22:11:24 +0000 | [diff] [blame] | 432 | ""+ _LegalCharsPatt +"+?" # Any word of at least one letter, nongreedy |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 433 | r")" # End of group 'key' |
| 434 | r"\s*=\s*" # Equal Sign |
| 435 | r"(?P<val>" # Start of group 'val' |
| 436 | r'"(?:[^\\"]|\\.)*"' # Any doublequoted string |
| 437 | r"|" # or |
| 438 | ""+ _LegalCharsPatt +"*" # Any word or empty string |
| 439 | r")" # End of group 'val' |
| 440 | r"\s*;?" # Probably ending in a semi-colon |
Antoine Pitrou | fd03645 | 2008-08-19 17:56:33 +0000 | [diff] [blame] | 441 | , re.ASCII) # May be removed if safe. |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 442 | |
| 443 | |
| 444 | # At long last, here is the cookie class. |
| 445 | # Using this class is almost just like using a dictionary. |
| 446 | # See this module's docstring for example usage. |
| 447 | # |
Raymond Hettinger | 0a2963c | 2002-06-26 15:19:01 +0000 | [diff] [blame] | 448 | class BaseCookie(dict): |
Georg Brandl | 9cf32a1 | 2009-09-04 08:28:01 +0000 | [diff] [blame] | 449 | """A container class for a set of Morsels.""" |
Fred Drake | ff5364a | 2000-08-24 14:40:35 +0000 | [diff] [blame] | 450 | |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 451 | def value_decode(self, val): |
| 452 | """real_value, coded_value = value_decode(STRING) |
| 453 | Called prior to setting a cookie's value from the network |
| 454 | representation. The VALUE is the value read from HTTP |
| 455 | header. |
| 456 | Override this function to modify the behavior of cookies. |
| 457 | """ |
| 458 | return val, val |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 459 | |
| 460 | def value_encode(self, val): |
| 461 | """real_value, coded_value = value_encode(VALUE) |
| 462 | Called prior to setting a cookie's value from the dictionary |
| 463 | representation. The VALUE is the value being assigned. |
| 464 | Override this function to modify the behavior of cookies. |
| 465 | """ |
| 466 | strval = str(val) |
| 467 | return strval, strval |
Fred Drake | ff5364a | 2000-08-24 14:40:35 +0000 | [diff] [blame] | 468 | |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 469 | def __init__(self, input=None): |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 470 | if input: self.load(input) |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 471 | |
| 472 | def __set(self, key, real_value, coded_value): |
| 473 | """Private method for setting a cookie's value""" |
| 474 | M = self.get(key, Morsel()) |
| 475 | M.set(key, real_value, coded_value) |
Raymond Hettinger | 0a2963c | 2002-06-26 15:19:01 +0000 | [diff] [blame] | 476 | dict.__setitem__(self, key, M) |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 477 | |
| 478 | def __setitem__(self, key, value): |
| 479 | """Dictionary style assignment.""" |
| 480 | rval, cval = self.value_encode(value) |
| 481 | self.__set(key, rval, cval) |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 482 | |
Georg Brandl | 532efab | 2005-08-24 22:34:21 +0000 | [diff] [blame] | 483 | def output(self, attrs=None, header="Set-Cookie:", sep="\015\012"): |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 484 | """Return a string suitable for HTTP.""" |
| 485 | result = [] |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 486 | items = sorted(self.items()) |
Tim Peters | 2f228e7 | 2001-05-13 00:19:31 +0000 | [diff] [blame] | 487 | for K,V in items: |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 488 | result.append( V.output(attrs, header) ) |
Fred Drake | d451ec1 | 2002-04-26 02:29:55 +0000 | [diff] [blame] | 489 | return sep.join(result) |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 490 | |
Andrew M. Kuchling | 0b29b11 | 2000-08-24 11:52:33 +0000 | [diff] [blame] | 491 | __str__ = output |
| 492 | |
| 493 | def __repr__(self): |
| 494 | L = [] |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 495 | items = sorted(self.items()) |
Tim Peters | 2f228e7 | 2001-05-13 00:19:31 +0000 | [diff] [blame] | 496 | for K,V in items: |
Andrew M. Kuchling | 0b29b11 | 2000-08-24 11:52:33 +0000 | [diff] [blame] | 497 | L.append( '%s=%s' % (K,repr(V.value) ) ) |
Georg Brandl | 8246c43 | 2005-08-25 07:32:42 +0000 | [diff] [blame] | 498 | return '<%s: %s>' % (self.__class__.__name__, _spacejoin(L)) |
Fred Drake | ff5364a | 2000-08-24 14:40:35 +0000 | [diff] [blame] | 499 | |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 500 | def js_output(self, attrs=None): |
| 501 | """Return a string suitable for JavaScript.""" |
| 502 | result = [] |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 503 | items = sorted(self.items()) |
Tim Peters | 2f228e7 | 2001-05-13 00:19:31 +0000 | [diff] [blame] | 504 | for K,V in items: |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 505 | result.append( V.js_output(attrs) ) |
Fred Drake | d451ec1 | 2002-04-26 02:29:55 +0000 | [diff] [blame] | 506 | return _nulljoin(result) |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 507 | |
| 508 | def load(self, rawdata): |
| 509 | """Load cookies from a string (presumably HTTP_COOKIE) or |
| 510 | from a dictionary. Loading cookies from a dictionary 'd' |
| 511 | is equivalent to calling: |
| 512 | map(Cookie.__setitem__, d.keys(), d.values()) |
| 513 | """ |
| 514 | if type(rawdata) == type(""): |
| 515 | self.__ParseString(rawdata) |
| 516 | else: |
Benjamin Peterson | 8719ad5 | 2009-09-11 22:24:02 +0000 | [diff] [blame] | 517 | # self.update() wouldn't call our custom __setitem__ |
| 518 | for k, v in rawdata.items(): |
| 519 | self[k] = v |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 520 | return |
Fred Drake | ff5364a | 2000-08-24 14:40:35 +0000 | [diff] [blame] | 521 | |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 522 | def __ParseString(self, str, patt=_CookiePattern): |
| 523 | i = 0 # Our starting point |
| 524 | n = len(str) # Length of string |
| 525 | M = None # current morsel |
| 526 | |
| 527 | while 0 <= i < n: |
| 528 | # Start looking for a cookie |
| 529 | match = patt.search(str, i) |
| 530 | if not match: break # No more cookies |
| 531 | |
| 532 | K,V = match.group("key"), match.group("val") |
| 533 | i = match.end(0) |
| 534 | |
| 535 | # Parse the key, value in case it's metainfo |
| 536 | if K[0] == "$": |
| 537 | # We ignore attributes which pertain to the cookie |
| 538 | # mechanism as a whole. See RFC 2109. |
| 539 | # (Does anyone care?) |
| 540 | if M: |
| 541 | M[ K[1:] ] = V |
Raymond Hettinger | 0a2963c | 2002-06-26 15:19:01 +0000 | [diff] [blame] | 542 | elif K.lower() in Morsel._reserved: |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 543 | if M: |
Andrew M. Kuchling | 0b29b11 | 2000-08-24 11:52:33 +0000 | [diff] [blame] | 544 | M[ K ] = _unquote(V) |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 545 | else: |
| 546 | rval, cval = self.value_decode(V) |
| 547 | self.__set(K, rval, cval) |
| 548 | M = self[K] |
Georg Brandl | 4eff9f7 | 2009-09-04 08:22:00 +0000 | [diff] [blame] | 549 | |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 550 | |
| 551 | class SimpleCookie(BaseCookie): |
Georg Brandl | 9cf32a1 | 2009-09-04 08:28:01 +0000 | [diff] [blame] | 552 | """ |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 553 | SimpleCookie supports strings as cookie values. When setting |
| 554 | the value using the dictionary assignment notation, SimpleCookie |
| 555 | calls the builtin str() to convert the value to a string. Values |
| 556 | received from HTTP are kept as strings. |
| 557 | """ |
| 558 | def value_decode(self, val): |
| 559 | return _unquote( val ), val |
| 560 | def value_encode(self, val): |
| 561 | strval = str(val) |
| 562 | return strval, _quote( strval ) |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 563 | |
Andrew M. Kuchling | 52ea872 | 2000-08-19 13:01:19 +0000 | [diff] [blame] | 564 | ########################################################### |
| 565 | |
Guido van Rossum | 58b6f5b | 2001-04-06 19:39:11 +0000 | [diff] [blame] | 566 | def _test(): |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 567 | import doctest, http.cookies |
| 568 | return doctest.testmod(http.cookies) |
Guido van Rossum | 58b6f5b | 2001-04-06 19:39:11 +0000 | [diff] [blame] | 569 | |
| 570 | if __name__ == "__main__": |
| 571 | _test() |