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