blob: 8c90c5e51cade4ab4310e0e59e78256a46e157f8 [file] [log] [blame]
Georg Brandl24420152008-05-26 16:32:26 +00001:mod:`http.cookies` --- HTTP state management
2=============================================
Georg Brandl116aa622007-08-15 14:28:22 +00003
Georg Brandl24420152008-05-26 16:32:26 +00004.. module:: http.cookies
Georg Brandl116aa622007-08-15 14:28:22 +00005 :synopsis: Support for HTTP state management (cookies).
6.. moduleauthor:: Timothy O'Malley <timo@alum.mit.edu>
7.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
8
9
Georg Brandl24420152008-05-26 16:32:26 +000010The :mod:`http.cookies` module defines classes for abstracting the concept of
Georg Brandl116aa622007-08-15 14:28:22 +000011cookies, an HTTP state management mechanism. It supports both simple string-only
12cookies, and provides an abstraction for having any serializable data-type as
13cookie value.
14
15The module formerly strictly applied the parsing rules described in the
16:rfc:`2109` and :rfc:`2068` specifications. It has since been discovered that
17MSIE 3.0x doesn't follow the character rules outlined in those specs. As a
18result, the parsing rules used are a bit less strict.
19
20
21.. exception:: CookieError
22
23 Exception failing because of :rfc:`2109` invalidity: incorrect attributes,
24 incorrect :mailheader:`Set-Cookie` header, etc.
25
26
27.. class:: BaseCookie([input])
28
29 This class is a dictionary-like object whose keys are strings and whose values
30 are :class:`Morsel` instances. Note that upon setting a key to a value, the
31 value is first converted to a :class:`Morsel` containing the key and the value.
32
33 If *input* is given, it is passed to the :meth:`load` method.
34
35
36.. class:: SimpleCookie([input])
37
38 This class derives from :class:`BaseCookie` and overrides :meth:`value_decode`
39 and :meth:`value_encode` to be the identity and :func:`str` respectively.
40
41
42.. class:: SerialCookie([input])
43
44 This class derives from :class:`BaseCookie` and overrides :meth:`value_decode`
45 and :meth:`value_encode` to be the :func:`pickle.loads` and
46 :func:`pickle.dumps`.
47
48 .. deprecated:: 2.3
49 Reading pickled values from untrusted cookie data is a huge security hole, as
50 pickle strings can be crafted to cause arbitrary code to execute on your server.
51 It is supported for backwards compatibility only, and may eventually go away.
52
53
54.. class:: SmartCookie([input])
55
56 This class derives from :class:`BaseCookie`. It overrides :meth:`value_decode`
57 to be :func:`pickle.loads` if it is a valid pickle, and otherwise the value
58 itself. It overrides :meth:`value_encode` to be :func:`pickle.dumps` unless it
59 is a string, in which case it returns the value itself.
60
61 .. deprecated:: 2.3
62 The same security warning from :class:`SerialCookie` applies here.
63
64A further security note is warranted. For backwards compatibility, the
Georg Brandl24420152008-05-26 16:32:26 +000065:mod:`http.cookies` module exports a class named :class:`Cookie` which is just an
Georg Brandl116aa622007-08-15 14:28:22 +000066alias for :class:`SmartCookie`. This is probably a mistake and will likely be
67removed in a future version. You should not use the :class:`Cookie` class in
68your applications, for the same reason why you should not use the
69:class:`SerialCookie` class.
70
71
72.. seealso::
73
Georg Brandl24420152008-05-26 16:32:26 +000074 Module :mod:`http.cookiejar`
75 HTTP cookie handling for web *clients*. The :mod:`http.cookiejar` and
76 :mod:`http.cookies` modules do not depend on each other.
Georg Brandl116aa622007-08-15 14:28:22 +000077
78 :rfc:`2109` - HTTP State Management Mechanism
79 This is the state management specification implemented by this module.
80
81
82.. _cookie-objects:
83
84Cookie Objects
85--------------
86
87
88.. method:: BaseCookie.value_decode(val)
89
90 Return a decoded value from a string representation. Return value can be any
91 type. This method does nothing in :class:`BaseCookie` --- it exists so it can be
92 overridden.
93
94
95.. method:: BaseCookie.value_encode(val)
96
97 Return an encoded value. *val* can be any type, but return value must be a
98 string. This method does nothing in :class:`BaseCookie` --- it exists so it can
99 be overridden
100
101 In general, it should be the case that :meth:`value_encode` and
102 :meth:`value_decode` are inverses on the range of *value_decode*.
103
104
105.. method:: BaseCookie.output([attrs[, header[, sep]]])
106
107 Return a string representation suitable to be sent as HTTP headers. *attrs* and
108 *header* are sent to each :class:`Morsel`'s :meth:`output` method. *sep* is used
109 to join the headers together, and is by default the combination ``'\r\n'``
110 (CRLF).
111
Georg Brandl116aa622007-08-15 14:28:22 +0000112
113.. method:: BaseCookie.js_output([attrs])
114
115 Return an embeddable JavaScript snippet, which, if run on a browser which
116 supports JavaScript, will act the same as if the HTTP headers was sent.
117
118 The meaning for *attrs* is the same as in :meth:`output`.
119
120
121.. method:: BaseCookie.load(rawdata)
122
123 If *rawdata* is a string, parse it as an ``HTTP_COOKIE`` and add the values
124 found there as :class:`Morsel`\ s. If it is a dictionary, it is equivalent to::
125
126 for k, v in rawdata.items():
127 cookie[k] = v
128
129
130.. _morsel-objects:
131
132Morsel Objects
133--------------
134
135
136.. class:: Morsel()
137
138 Abstract a key/value pair, which has some :rfc:`2109` attributes.
139
140 Morsels are dictionary-like objects, whose set of keys is constant --- the valid
141 :rfc:`2109` attributes, which are
142
143 * ``expires``
144 * ``path``
145 * ``comment``
146 * ``domain``
147 * ``max-age``
148 * ``secure``
149 * ``version``
150
151 The keys are case-insensitive.
152
153
154.. attribute:: Morsel.value
155
156 The value of the cookie.
157
158
159.. attribute:: Morsel.coded_value
160
161 The encoded value of the cookie --- this is what should be sent.
162
163
164.. attribute:: Morsel.key
165
166 The name of the cookie.
167
168
169.. method:: Morsel.set(key, value, coded_value)
170
171 Set the *key*, *value* and *coded_value* members.
172
173
174.. method:: Morsel.isReservedKey(K)
175
176 Whether *K* is a member of the set of keys of a :class:`Morsel`.
177
178
179.. method:: Morsel.output([attrs[, header]])
180
181 Return a string representation of the Morsel, suitable to be sent as an HTTP
182 header. By default, all the attributes are included, unless *attrs* is given, in
183 which case it should be a list of attributes to use. *header* is by default
184 ``"Set-Cookie:"``.
185
186
187.. method:: Morsel.js_output([attrs])
188
189 Return an embeddable JavaScript snippet, which, if run on a browser which
190 supports JavaScript, will act the same as if the HTTP header was sent.
191
192 The meaning for *attrs* is the same as in :meth:`output`.
193
194
195.. method:: Morsel.OutputString([attrs])
196
197 Return a string representing the Morsel, without any surrounding HTTP or
198 JavaScript.
199
200 The meaning for *attrs* is the same as in :meth:`output`.
201
202
203.. _cookie-example:
204
205Example
206-------
207
Georg Brandl24420152008-05-26 16:32:26 +0000208The following example demonstrates how to use the :mod:`http.cookies` module.
Christian Heimesfe337bf2008-03-23 21:54:12 +0000209
210.. doctest::
211 :options: +NORMALIZE_WHITESPACE
Georg Brandl116aa622007-08-15 14:28:22 +0000212
Georg Brandl24420152008-05-26 16:32:26 +0000213 >>> from http import cookies
214 >>> C = cookies.SimpleCookie()
215 >>> C = cookies.SerialCookie()
216 >>> C = cookies.SmartCookie()
Georg Brandl116aa622007-08-15 14:28:22 +0000217 >>> C["fig"] = "newton"
218 >>> C["sugar"] = "wafer"
Georg Brandl6911e3c2007-09-04 07:15:32 +0000219 >>> print(C) # generate HTTP headers
Georg Brandl116aa622007-08-15 14:28:22 +0000220 Set-Cookie: fig=newton
Christian Heimesfe337bf2008-03-23 21:54:12 +0000221 Set-Cookie: sugar=wafer
Georg Brandl6911e3c2007-09-04 07:15:32 +0000222 >>> print(C.output()) # same thing
Georg Brandl116aa622007-08-15 14:28:22 +0000223 Set-Cookie: fig=newton
Christian Heimesfe337bf2008-03-23 21:54:12 +0000224 Set-Cookie: sugar=wafer
Georg Brandl24420152008-05-26 16:32:26 +0000225 >>> C = cookies.SmartCookie()
Georg Brandl116aa622007-08-15 14:28:22 +0000226 >>> C["rocky"] = "road"
227 >>> C["rocky"]["path"] = "/cookie"
Georg Brandl6911e3c2007-09-04 07:15:32 +0000228 >>> print(C.output(header="Cookie:"))
Georg Brandl116aa622007-08-15 14:28:22 +0000229 Cookie: rocky=road; Path=/cookie
Georg Brandl6911e3c2007-09-04 07:15:32 +0000230 >>> print(C.output(attrs=[], header="Cookie:"))
Georg Brandl116aa622007-08-15 14:28:22 +0000231 Cookie: rocky=road
Georg Brandl24420152008-05-26 16:32:26 +0000232 >>> C = cookies.SmartCookie()
Georg Brandl116aa622007-08-15 14:28:22 +0000233 >>> C.load("chips=ahoy; vienna=finger") # load from a string (HTTP header)
Georg Brandl6911e3c2007-09-04 07:15:32 +0000234 >>> print(C)
Georg Brandl116aa622007-08-15 14:28:22 +0000235 Set-Cookie: chips=ahoy
Christian Heimesfe337bf2008-03-23 21:54:12 +0000236 Set-Cookie: vienna=finger
Georg Brandl24420152008-05-26 16:32:26 +0000237 >>> C = cookies.SmartCookie()
Georg Brandl116aa622007-08-15 14:28:22 +0000238 >>> C.load('keebler="E=everybody; L=\\"Loves\\"; fudge=\\012;";')
Georg Brandl6911e3c2007-09-04 07:15:32 +0000239 >>> print(C)
Georg Brandl116aa622007-08-15 14:28:22 +0000240 Set-Cookie: keebler="E=everybody; L=\"Loves\"; fudge=\012;"
Georg Brandl24420152008-05-26 16:32:26 +0000241 >>> C = cookies.SmartCookie()
Georg Brandl116aa622007-08-15 14:28:22 +0000242 >>> C["oreo"] = "doublestuff"
243 >>> C["oreo"]["path"] = "/"
Georg Brandl6911e3c2007-09-04 07:15:32 +0000244 >>> print(C)
Georg Brandl116aa622007-08-15 14:28:22 +0000245 Set-Cookie: oreo=doublestuff; Path=/
Georg Brandl24420152008-05-26 16:32:26 +0000246 >>> C = cookies.SmartCookie()
Georg Brandl116aa622007-08-15 14:28:22 +0000247 >>> C["twix"] = "none for you"
248 >>> C["twix"].value
249 'none for you'
Georg Brandl24420152008-05-26 16:32:26 +0000250 >>> C = cookies.SimpleCookie()
Georg Brandl116aa622007-08-15 14:28:22 +0000251 >>> C["number"] = 7 # equivalent to C["number"] = str(7)
252 >>> C["string"] = "seven"
253 >>> C["number"].value
254 '7'
255 >>> C["string"].value
256 'seven'
Georg Brandl6911e3c2007-09-04 07:15:32 +0000257 >>> print(C)
Georg Brandl116aa622007-08-15 14:28:22 +0000258 Set-Cookie: number=7
259 Set-Cookie: string=seven
Georg Brandl24420152008-05-26 16:32:26 +0000260 >>> C = cookies.SerialCookie()
Georg Brandl116aa622007-08-15 14:28:22 +0000261 >>> C["number"] = 7
262 >>> C["string"] = "seven"
263 >>> C["number"].value
264 7
265 >>> C["string"].value
266 'seven'
Georg Brandl6911e3c2007-09-04 07:15:32 +0000267 >>> print(C)
Georg Brandl116aa622007-08-15 14:28:22 +0000268 Set-Cookie: number="I7\012."
269 Set-Cookie: string="S'seven'\012p1\012."
Georg Brandl24420152008-05-26 16:32:26 +0000270 >>> C = cookies.SmartCookie()
Georg Brandl116aa622007-08-15 14:28:22 +0000271 >>> C["number"] = 7
272 >>> C["string"] = "seven"
273 >>> C["number"].value
274 7
275 >>> C["string"].value
276 'seven'
Georg Brandl6911e3c2007-09-04 07:15:32 +0000277 >>> print(C)
Georg Brandl116aa622007-08-15 14:28:22 +0000278 Set-Cookie: number="I7\012."
279 Set-Cookie: string=seven
280