blob: d2916786f2aa8fb1e305e368142f12f7d4a8fb21 [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#!/usr/bin/env python3
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +00002#
3
4####
5# Copyright 2000 by Timothy O'Malley <timo@alum.mit.edu>
Tim Peters88869f92001-01-14 23:36:06 +00006#
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +00007# All Rights Reserved
Tim Peters88869f92001-01-14 23:36:06 +00008#
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +00009# 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 Peters88869f92001-01-14 23:36:06 +000016# prior permission.
17#
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000018# 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 Peters88869f92001-01-14 23:36:06 +000025# PERFORMANCE OF THIS SOFTWARE.
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000026#
27####
Tim Peters88869f92001-01-14 23:36:06 +000028#
29# Id: Cookie.py,v 2.29 2000/08/23 05:28:49 timo Exp
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000030# 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. Kuchling0b29b112000-08-24 11:52:33 +000037# Dave Mitchell (davem@magnet.com) in 1995, when he released the
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000038# first version of nscookie.py.
39#
40####
41
Guido van Rossum58b6f5b2001-04-06 19:39:11 +000042r"""
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000043Here's a sample session to show how to use this module.
44At the moment, this is the only documentation.
45
46The Basics
47----------
48
Georg Brandl76e155a2010-07-31 21:04:00 +000049Importing is easy...
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000050
Georg Brandl24420152008-05-26 16:32:26 +000051 >>> from http import cookies
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000052
Georg Brandl61013952008-05-28 15:56:30 +000053Most of the time you start by creating a cookie.
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000054
Georg Brandl24420152008-05-26 16:32:26 +000055 >>> C = cookies.SimpleCookie()
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000056
57Once you've created your Cookie, you can add values just as if it were
58a dictionary.
59
Georg Brandl61013952008-05-28 15:56:30 +000060 >>> C = cookies.SimpleCookie()
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000061 >>> C["fig"] = "newton"
62 >>> C["sugar"] = "wafer"
Georg Brandl532efab2005-08-24 22:34:21 +000063 >>> C.output()
64 'Set-Cookie: fig=newton\r\nSet-Cookie: sugar=wafer'
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000065
66Notice that the printable representation of a Cookie is the
67appropriate format for a Set-Cookie: header. This is the
68default behavior. You can change the header and printed
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +000069attributes by using the .output() function
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000070
Georg Brandl61013952008-05-28 15:56:30 +000071 >>> C = cookies.SimpleCookie()
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000072 >>> C["rocky"] = "road"
73 >>> C["rocky"]["path"] = "/cookie"
Guido van Rossumfff80df2007-02-09 20:33:44 +000074 >>> print(C.output(header="Cookie:"))
Georg Brandl532efab2005-08-24 22:34:21 +000075 Cookie: rocky=road; Path=/cookie
Guido van Rossumfff80df2007-02-09 20:33:44 +000076 >>> print(C.output(attrs=[], header="Cookie:"))
Georg Brandl532efab2005-08-24 22:34:21 +000077 Cookie: rocky=road
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000078
79The load() method of a Cookie extracts cookies from a string. In a
80CGI script, you would use this method to extract the cookies from the
81HTTP_COOKIE environment variable.
82
Georg Brandl61013952008-05-28 15:56:30 +000083 >>> C = cookies.SimpleCookie()
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000084 >>> C.load("chips=ahoy; vienna=finger")
Georg Brandl532efab2005-08-24 22:34:21 +000085 >>> C.output()
86 'Set-Cookie: chips=ahoy\r\nSet-Cookie: vienna=finger'
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000087
88The load() method is darn-tootin smart about identifying cookies
89within a string. Escaped quotation marks, nested semicolons, and other
90such trickeries do not confuse it.
91
Georg Brandl61013952008-05-28 15:56:30 +000092 >>> C = cookies.SimpleCookie()
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000093 >>> C.load('keebler="E=everybody; L=\\"Loves\\"; fudge=\\012;";')
Guido van Rossumfff80df2007-02-09 20:33:44 +000094 >>> print(C)
Georg Brandl532efab2005-08-24 22:34:21 +000095 Set-Cookie: keebler="E=everybody; L=\"Loves\"; fudge=\012;"
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +000096
97Each element of the Cookie also supports all of the RFC 2109
98Cookie attributes. Here's an example which sets the Path
99attribute.
100
Georg Brandl61013952008-05-28 15:56:30 +0000101 >>> C = cookies.SimpleCookie()
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000102 >>> C["oreo"] = "doublestuff"
103 >>> C["oreo"]["path"] = "/"
Guido van Rossumfff80df2007-02-09 20:33:44 +0000104 >>> print(C)
Georg Brandl532efab2005-08-24 22:34:21 +0000105 Set-Cookie: oreo=doublestuff; Path=/
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000106
107Each dictionary element has a 'value' attribute, which gives you
Tim Peters88869f92001-01-14 23:36:06 +0000108back the value associated with the key.
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000109
Georg Brandl61013952008-05-28 15:56:30 +0000110 >>> C = cookies.SimpleCookie()
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000111 >>> C["twix"] = "none for you"
112 >>> C["twix"].value
113 'none for you'
114
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000115The SimpleCookie expects that all values should be standard strings.
116Just to be sure, SimpleCookie invokes the str() builtin to convert
117the value to a string, when the values are set dictionary-style.
118
Georg Brandl24420152008-05-26 16:32:26 +0000119 >>> C = cookies.SimpleCookie()
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000120 >>> C["number"] = 7
121 >>> C["string"] = "seven"
122 >>> C["number"].value
123 '7'
124 >>> C["string"].value
125 'seven'
Georg Brandl532efab2005-08-24 22:34:21 +0000126 >>> C.output()
127 'Set-Cookie: number=7\r\nSet-Cookie: string=seven'
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000128
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000129Finis.
Georg Brandl76e155a2010-07-31 21:04:00 +0000130"""
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000131
132#
133# Import our required modules
Tim Peters88869f92001-01-14 23:36:06 +0000134#
Georg Brandl76e155a2010-07-31 21:04:00 +0000135import re
Martin v. Löwis02d893c2001-08-02 07:15:29 +0000136import string
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000137
Georg Brandl61013952008-05-28 15:56:30 +0000138__all__ = ["CookieError", "BaseCookie", "SimpleCookie"]
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000139
Fred Draked451ec12002-04-26 02:29:55 +0000140_nulljoin = ''.join
Georg Brandl532efab2005-08-24 22:34:21 +0000141_semispacejoin = '; '.join
Georg Brandl8246c432005-08-25 07:32:42 +0000142_spacejoin = ' '.join
Fred Draked451ec12002-04-26 02:29:55 +0000143
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000144#
145# Define an exception visible to External modules
146#
147class CookieError(Exception):
148 pass
149
150
151# These quoting routines conform to the RFC2109 specification, which in
152# turn references the character definitions from RFC2068. They provide
153# a two-way quoting algorithm. Any non-text character is translated
154# into a 4 character sequence: a forward-slash followed by the
155# three-digit octal equivalent of the character. Any '\' or '"' is
156# quoted with a preceeding '\' slash.
Tim Peters88869f92001-01-14 23:36:06 +0000157#
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000158# These are taken from RFC2068 and RFC2109.
159# _LegalChars is the list of chars which don't require "'s
160# _Translator hash-table for fast quoting
161#
Senthil Kumaran3a441c12012-04-22 09:19:04 +0800162_LegalChars = string.ascii_letters + string.digits + "!#$%&'*+-.^_`|~:"
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000163_Translator = {
164 '\000' : '\\000', '\001' : '\\001', '\002' : '\\002',
165 '\003' : '\\003', '\004' : '\\004', '\005' : '\\005',
166 '\006' : '\\006', '\007' : '\\007', '\010' : '\\010',
167 '\011' : '\\011', '\012' : '\\012', '\013' : '\\013',
168 '\014' : '\\014', '\015' : '\\015', '\016' : '\\016',
169 '\017' : '\\017', '\020' : '\\020', '\021' : '\\021',
170 '\022' : '\\022', '\023' : '\\023', '\024' : '\\024',
171 '\025' : '\\025', '\026' : '\\026', '\027' : '\\027',
172 '\030' : '\\030', '\031' : '\\031', '\032' : '\\032',
173 '\033' : '\\033', '\034' : '\\034', '\035' : '\\035',
174 '\036' : '\\036', '\037' : '\\037',
175
R. David Murraye05ca2a2010-12-28 18:54:13 +0000176 # Because of the way browsers really handle cookies (as opposed
177 # to what the RFC says) we also encode , and ;
178
179 ',' : '\\054', ';' : '\\073',
180
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000181 '"' : '\\"', '\\' : '\\\\',
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örwald3f1e65c2007-06-08 15:33:46 +0000228def _quote(str, LegalChars=_LegalChars):
Georg Brandl9cf32a12009-09-04 08:28:01 +0000229 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 Rossum0c41e882007-07-03 16:46:40 +0000235 if all(c in LegalChars for c in str):
Fred Drakeff5364a2000-08-24 14:40:35 +0000236 return str
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000237 else:
Georg Brandlcbd2ab12010-12-04 10:39:14 +0000238 return '"' + _nulljoin(_Translator.get(s, s) for s in str) + '"'
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000239
240
241_OctalPatt = re.compile(r"\\[0-3][0-7][0-7]")
242_QuotePatt = re.compile(r"[\\].")
243
Fred Draked451ec12002-04-26 02:29:55 +0000244def _unquote(str):
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000245 # If there aren't any doublequotes,
246 # then there can't be any special characters. See RFC 2109.
Georg Brandl76e155a2010-07-31 21:04:00 +0000247 if len(str) < 2:
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000248 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 Drakeff5364a2000-08-24 14:40:35 +0000257
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000258 # Check for special sequences. Examples:
259 # \012 --> \n
260 # \" --> "
261 #
262 i = 0
263 n = len(str)
264 res = []
265 while 0 <= i < n:
Georg Brandl76e155a2010-07-31 21:04:00 +0000266 o_match = _OctalPatt.search(str, i)
267 q_match = _QuotePatt.search(str, i)
268 if not o_match and not q_match: # Neither matched
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000269 res.append(str[i:])
270 break
271 # else:
272 j = k = -1
Georg Brandl76e155a2010-07-31 21:04:00 +0000273 if o_match:
274 j = o_match.start(0)
275 if q_match:
276 k = q_match.start(0)
277 if q_match and (not o_match or k < j): # QuotePatt matched
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000278 res.append(str[i:k])
279 res.append(str[k+1])
Georg Brandl76e155a2010-07-31 21:04:00 +0000280 i = k + 2
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000281 else: # OctalPatt matched
282 res.append(str[i:j])
Georg Brandl76e155a2010-07-31 21:04:00 +0000283 res.append(chr(int(str[j+1:j+4], 8)))
284 i = j + 4
Fred Draked451ec12002-04-26 02:29:55 +0000285 return _nulljoin(res)
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000286
Georg Brandl76e155a2010-07-31 21:04:00 +0000287# The _getdate() routine is used to set the expiration time in the cookie's HTTP
288# header. By default, _getdate() returns the current time in the appropriate
289# "expires" format for a Set-Cookie header. The one optional argument is an
290# offset from now, in seconds. For example, an offset of -3600 means "one hour
291# ago". The offset may be a floating point number.
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000292#
293
294_weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
295
296_monthname = [None,
297 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
298 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
299
300def _getdate(future=0, weekdayname=_weekdayname, monthname=_monthname):
301 from time import gmtime, time
302 now = time()
303 year, month, day, hh, mm, ss, wd, y, z = gmtime(now + future)
Senthil Kumaran00c2ec22012-05-20 12:05:16 +0800304 return "%s, %02d %3s %4d %02d:%02d:%02d GMT" % \
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000305 (weekdayname[wd], day, monthname[month], year, hh, mm, ss)
306
307
Raymond Hettinger0a2963c2002-06-26 15:19:01 +0000308class Morsel(dict):
Georg Brandl76e155a2010-07-31 21:04:00 +0000309 """A class to hold ONE (key, value) pair.
Georg Brandl9cf32a12009-09-04 08:28:01 +0000310
311 In a cookie, each such pair may have several attributes, so this class is
312 used to keep the attributes associated with the appropriate key,value pair.
313 This class also includes a coded_value attribute, which is used to hold
314 the network representation of the value. This is most useful when Python
315 objects are pickled for network transit.
316 """
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000317 # RFC 2109 lists these attributes as reserved:
318 # path comment domain
319 # max-age secure version
Tim Peters88869f92001-01-14 23:36:06 +0000320 #
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000321 # For historical reasons, these attributes are also reserved:
322 # expires
323 #
Benjamin Peterson35e661c2008-09-06 19:37:35 +0000324 # This is an extension from Microsoft:
325 # httponly
326 #
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000327 # This dictionary provides a mapping from the lowercase
328 # variant on the left to the appropriate traditional
329 # formatting on the right.
Georg Brandl76e155a2010-07-31 21:04:00 +0000330 _reserved = {
331 "expires" : "expires",
332 "path" : "Path",
333 "comment" : "Comment",
334 "domain" : "Domain",
335 "max-age" : "Max-Age",
336 "secure" : "secure",
337 "httponly" : "httponly",
338 "version" : "Version",
339 }
Fred Drakeff5364a2000-08-24 14:40:35 +0000340
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000341 def __init__(self):
342 # Set defaults
343 self.key = self.value = self.coded_value = None
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000344
345 # Set default attributes
Georg Brandl76e155a2010-07-31 21:04:00 +0000346 for key in self._reserved:
347 dict.__setitem__(self, key, "")
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000348
349 def __setitem__(self, K, V):
Fred Draked451ec12002-04-26 02:29:55 +0000350 K = K.lower()
Raymond Hettinger0a2963c2002-06-26 15:19:01 +0000351 if not K in self._reserved:
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000352 raise CookieError("Invalid Attribute %s" % K)
Raymond Hettinger0a2963c2002-06-26 15:19:01 +0000353 dict.__setitem__(self, K, V)
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000354
355 def isReservedKey(self, K):
Raymond Hettinger0a2963c2002-06-26 15:19:01 +0000356 return K.lower() in self._reserved
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000357
Walter Dörwald3f1e65c2007-06-08 15:33:46 +0000358 def set(self, key, val, coded_val, LegalChars=_LegalChars):
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000359 # First we verify that the key isn't a reserved word
360 # Second we make sure it only contains legal characters
Raymond Hettinger0a2963c2002-06-26 15:19:01 +0000361 if key.lower() in self._reserved:
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000362 raise CookieError("Attempt to set a reserved key: %s" % key)
Guido van Rossum0c41e882007-07-03 16:46:40 +0000363 if any(c not in LegalChars for c in key):
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000364 raise CookieError("Illegal key value: %s" % key)
365
366 # It's a good key, so save it.
Georg Brandl76e155a2010-07-31 21:04:00 +0000367 self.key = key
368 self.value = val
369 self.coded_value = coded_val
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000370
Georg Brandl76e155a2010-07-31 21:04:00 +0000371 def output(self, attrs=None, header="Set-Cookie:"):
372 return "%s %s" % (header, self.OutputString(attrs))
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000373
Andrew M. Kuchling0b29b112000-08-24 11:52:33 +0000374 __str__ = output
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000375
Andrew M. Kuchling0b29b112000-08-24 11:52:33 +0000376 def __repr__(self):
377 return '<%s: %s=%s>' % (self.__class__.__name__,
Georg Brandl76e155a2010-07-31 21:04:00 +0000378 self.key, repr(self.value))
Fred Drakeff5364a2000-08-24 14:40:35 +0000379
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000380 def js_output(self, attrs=None):
381 # Print javascript
382 return """
Georg Brandl03a33ea2005-06-26 21:02:49 +0000383 <script type="text/javascript">
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000384 <!-- begin hiding
Georg Brandl03a33ea2005-06-26 21:02:49 +0000385 document.cookie = \"%s\";
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000386 // end hiding -->
387 </script>
Georg Brandl76e155a2010-07-31 21:04:00 +0000388 """ % (self.OutputString(attrs).replace('"', r'\"'))
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000389
390 def OutputString(self, attrs=None):
391 # Build up our result
392 #
393 result = []
Georg Brandl76e155a2010-07-31 21:04:00 +0000394 append = result.append
Fred Drakeff5364a2000-08-24 14:40:35 +0000395
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000396 # First, the key=value pair
Georg Brandl76e155a2010-07-31 21:04:00 +0000397 append("%s=%s" % (self.key, self.coded_value))
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000398
399 # Now add any defined attributes
Fred Drake8152d322000-12-12 23:20:45 +0000400 if attrs is None:
Raymond Hettinger0a2963c2002-06-26 15:19:01 +0000401 attrs = self._reserved
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000402 items = sorted(self.items())
Georg Brandl76e155a2010-07-31 21:04:00 +0000403 for key, value in items:
404 if value == "":
405 continue
406 if key not in attrs:
407 continue
408 if key == "expires" and isinstance(value, int):
409 append("%s=%s" % (self._reserved[key], _getdate(value)))
410 elif key == "max-age" and isinstance(value, int):
411 append("%s=%d" % (self._reserved[key], value))
412 elif key == "secure":
413 append(str(self._reserved[key]))
414 elif key == "httponly":
415 append(str(self._reserved[key]))
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000416 else:
Georg Brandl76e155a2010-07-31 21:04:00 +0000417 append("%s=%s" % (self._reserved[key], value))
Fred Drakeff5364a2000-08-24 14:40:35 +0000418
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000419 # Return the result
Georg Brandl532efab2005-08-24 22:34:21 +0000420 return _semispacejoin(result)
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000421
422
423#
424# Pattern for finding cookie
425#
426# This used to be strict parsing based on the RFC2109 and RFC2068
427# specifications. I have since discovered that MSIE 3.0x doesn't
428# follow the character rules outlined in those specs. As a
429# result, the parsing rules here are less strict.
430#
431
Andrew M. Kuchlingc05abb32001-02-20 22:11:24 +0000432_LegalCharsPatt = r"[\w\d!#%&'~_`><@,:/\$\*\+\-\.\^\|\)\(\?\}\{\=]"
Georg Brandl76e155a2010-07-31 21:04:00 +0000433_CookiePattern = re.compile(r"""
434 (?x) # This is a verbose pattern
435 (?P<key> # Start of group 'key'
436 """ + _LegalCharsPatt + r"""+? # Any word of at least one letter
437 ) # End of group 'key'
438 \s*=\s* # Equal Sign
439 (?P<val> # Start of group 'val'
440 "(?:[^\\"]|\\.)*" # Any doublequoted string
441 | # or
Senthil Kumaranaeeba262012-05-20 16:58:30 +0800442 \w{3},\s[\w\d\s-]{9,11}\s[\d:]{8}\sGMT # Special case for "expires" attr
Georg Brandlb16e38b2010-08-01 09:06:34 +0000443 | # or
Georg Brandl76e155a2010-07-31 21:04:00 +0000444 """ + _LegalCharsPatt + r"""* # Any word or empty string
445 ) # End of group 'val'
446 \s*;? # Probably ending in a semi-colon
447 """, re.ASCII) # May be removed if safe.
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000448
449
Georg Brandl76e155a2010-07-31 21:04:00 +0000450# At long last, here is the cookie class. Using this class is almost just like
451# using a dictionary. See this module's docstring for example usage.
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000452#
Raymond Hettinger0a2963c2002-06-26 15:19:01 +0000453class BaseCookie(dict):
Georg Brandl9cf32a12009-09-04 08:28:01 +0000454 """A container class for a set of Morsels."""
Fred Drakeff5364a2000-08-24 14:40:35 +0000455
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000456 def value_decode(self, val):
457 """real_value, coded_value = value_decode(STRING)
458 Called prior to setting a cookie's value from the network
459 representation. The VALUE is the value read from HTTP
460 header.
461 Override this function to modify the behavior of cookies.
462 """
463 return val, val
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000464
465 def value_encode(self, val):
466 """real_value, coded_value = value_encode(VALUE)
467 Called prior to setting a cookie's value from the dictionary
468 representation. The VALUE is the value being assigned.
469 Override this function to modify the behavior of cookies.
470 """
471 strval = str(val)
472 return strval, strval
Fred Drakeff5364a2000-08-24 14:40:35 +0000473
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000474 def __init__(self, input=None):
Georg Brandl76e155a2010-07-31 21:04:00 +0000475 if input:
476 self.load(input)
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000477
478 def __set(self, key, real_value, coded_value):
479 """Private method for setting a cookie's value"""
480 M = self.get(key, Morsel())
481 M.set(key, real_value, coded_value)
Raymond Hettinger0a2963c2002-06-26 15:19:01 +0000482 dict.__setitem__(self, key, M)
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000483
484 def __setitem__(self, key, value):
485 """Dictionary style assignment."""
486 rval, cval = self.value_encode(value)
487 self.__set(key, rval, cval)
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000488
Georg Brandl532efab2005-08-24 22:34:21 +0000489 def output(self, attrs=None, header="Set-Cookie:", sep="\015\012"):
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000490 """Return a string suitable for HTTP."""
491 result = []
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000492 items = sorted(self.items())
Georg Brandl76e155a2010-07-31 21:04:00 +0000493 for key, value in items:
494 result.append(value.output(attrs, header))
Fred Draked451ec12002-04-26 02:29:55 +0000495 return sep.join(result)
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000496
Andrew M. Kuchling0b29b112000-08-24 11:52:33 +0000497 __str__ = output
498
499 def __repr__(self):
Georg Brandl76e155a2010-07-31 21:04:00 +0000500 l = []
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000501 items = sorted(self.items())
Georg Brandl76e155a2010-07-31 21:04:00 +0000502 for key, value in items:
503 l.append('%s=%s' % (key, repr(value.value)))
504 return '<%s: %s>' % (self.__class__.__name__, _spacejoin(l))
Fred Drakeff5364a2000-08-24 14:40:35 +0000505
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000506 def js_output(self, attrs=None):
507 """Return a string suitable for JavaScript."""
508 result = []
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000509 items = sorted(self.items())
Georg Brandl76e155a2010-07-31 21:04:00 +0000510 for key, value in items:
511 result.append(value.js_output(attrs))
Fred Draked451ec12002-04-26 02:29:55 +0000512 return _nulljoin(result)
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000513
514 def load(self, rawdata):
515 """Load cookies from a string (presumably HTTP_COOKIE) or
516 from a dictionary. Loading cookies from a dictionary 'd'
517 is equivalent to calling:
518 map(Cookie.__setitem__, d.keys(), d.values())
519 """
Georg Brandl76e155a2010-07-31 21:04:00 +0000520 if isinstance(rawdata, str):
521 self.__parse_string(rawdata)
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000522 else:
Benjamin Peterson8719ad52009-09-11 22:24:02 +0000523 # self.update() wouldn't call our custom __setitem__
Georg Brandl76e155a2010-07-31 21:04:00 +0000524 for key, value in rawdata.items():
525 self[key] = value
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000526 return
Fred Drakeff5364a2000-08-24 14:40:35 +0000527
Georg Brandl76e155a2010-07-31 21:04:00 +0000528 def __parse_string(self, str, patt=_CookiePattern):
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000529 i = 0 # Our starting point
530 n = len(str) # Length of string
531 M = None # current morsel
532
533 while 0 <= i < n:
534 # Start looking for a cookie
535 match = patt.search(str, i)
Georg Brandl76e155a2010-07-31 21:04:00 +0000536 if not match:
537 # No more cookies
538 break
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000539
Georg Brandl76e155a2010-07-31 21:04:00 +0000540 key, value = match.group("key"), match.group("val")
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000541 i = match.end(0)
542
543 # Parse the key, value in case it's metainfo
Georg Brandl76e155a2010-07-31 21:04:00 +0000544 if key[0] == "$":
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000545 # We ignore attributes which pertain to the cookie
546 # mechanism as a whole. See RFC 2109.
547 # (Does anyone care?)
548 if M:
Georg Brandl76e155a2010-07-31 21:04:00 +0000549 M[key[1:]] = value
550 elif key.lower() in Morsel._reserved:
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000551 if M:
Georg Brandl76e155a2010-07-31 21:04:00 +0000552 M[key] = _unquote(value)
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000553 else:
Georg Brandl76e155a2010-07-31 21:04:00 +0000554 rval, cval = self.value_decode(value)
555 self.__set(key, rval, cval)
556 M = self[key]
Georg Brandl4eff9f72009-09-04 08:22:00 +0000557
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000558
559class SimpleCookie(BaseCookie):
Georg Brandl9cf32a12009-09-04 08:28:01 +0000560 """
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000561 SimpleCookie supports strings as cookie values. When setting
562 the value using the dictionary assignment notation, SimpleCookie
563 calls the builtin str() to convert the value to a string. Values
564 received from HTTP are kept as strings.
565 """
566 def value_decode(self, val):
Georg Brandl76e155a2010-07-31 21:04:00 +0000567 return _unquote(val), val
568
Andrew M. Kuchling52ea8722000-08-19 13:01:19 +0000569 def value_encode(self, val):
570 strval = str(val)
Georg Brandl76e155a2010-07-31 21:04:00 +0000571 return strval, _quote(strval)