blob: e1bc09a62b17a1df6ad9326976f2ba184c3ded38 [file] [log] [blame]
Moshe Zadka1b07f2b2000-08-19 14:11:41 +00001\section{\module{Cookie} ---
Fred Drakee5c73522000-08-19 16:54:57 +00002 HTTP state management}
Moshe Zadka1b07f2b2000-08-19 14:11:41 +00003
Fred Drakee5c73522000-08-19 16:54:57 +00004\declaremodule{standard}{Cookie}
5\modulesynopsis{Support for HTTP state management (cookies).}
Moshe Zadka1b07f2b2000-08-19 14:11:41 +00006\moduleauthor{Timothy O'Malley}{timo@alum.mit.edu}
7\sectionauthor{Moshe Zadka}{moshez@zadka.site.co.il}
8
Moshe Zadka1b07f2b2000-08-19 14:11:41 +00009
10The \module{Cookie} module defines classes for abstracting the concept of
Andrew M. Kuchling120beb62000-08-20 23:33:50 +000011cookies, an HTTP state management mechanism. It supports both simplistic
Moshe Zadka1b07f2b2000-08-19 14:11:41 +000012string-only cookies, and provides an abstraction for having any serializable
13data-type as cookie value.
14
Andrew M. Kuchling120beb62000-08-20 23:33:50 +000015The module formerly strictly applied the parsing rules described in in
16the \rfc{2109} and \rfc{2068} specifications. It has since been discovered
17that MSIE 3.0x doesn't follow the character rules outlined in those
18specs. As a result, the parsing rules used are a bit less strict.
Moshe Zadka1b07f2b2000-08-19 14:11:41 +000019
20\begin{excdesc}{CookieError}
Fred Drakee5c73522000-08-19 16:54:57 +000021Exception failing because of \rfc{2109} invalidity: incorrect
22attributes, incorrect \code{Set-Cookie} header, etc.
Moshe Zadka1b07f2b2000-08-19 14:11:41 +000023\end{excdesc}
24
Fred Drakee5c73522000-08-19 16:54:57 +000025\begin{classdesc}{BaseCookie}{\optional{input}}
Moshe Zadka1b07f2b2000-08-19 14:11:41 +000026This class is a dictionary-like object whose keys are strings and
27whose values are \class{Morsel}s. Note that upon setting a key to
28a value, the value is first converted to a \class{Morsel} containing
29the key and the value.
30
31If \var{input} is given, it is passed to the \method{load} method.
32\end{classdesc}
33
Fred Drakee5c73522000-08-19 16:54:57 +000034\begin{classdesc}{SimpleCookie}{\optional{input}}
35This class derives from \class{BaseCookie} and overrides \method{value_decode}
36and \method{value_encode} to be the identity and \function{str()} respectively.
37\end{classdesc}
38
39\begin{classdesc}{SerialCookie}{\optional{input}}
40This class derives from \class{BaseCookie} and overrides \method{value_decode}
41and \method{value_encode} to be the \function{pickle.loads()} and
Andrew M. Kuchling120beb62000-08-20 23:33:50 +000042\function{pickle.dumps}.
43
44Do not use this class. Reading pickled values from a cookie is a
45security hole, as arbitrary client-code can be run on
46\function{pickle.loads()}. It is supported for backwards
47compatibility.
48
Fred Drakee5c73522000-08-19 16:54:57 +000049\end{classdesc}
50
51\begin{classdesc}{SmartCookie}{\optional{input}}
52This class derives from \class{BaseCookie}. It overrides \method{value_decode}
53to be \function{pickle.loads()} if it is a valid pickle, and otherwise
54the value itself. It overrides \method{value_encode} to be
55\function{pickle.dumps()} unless it is a string, in which case it returns
56the value itself.
57
58The same security warning from \class{SerialCookie} applies here.
59\end{classdesc}
60
61
62\begin{seealso}
63 \seerfc{2109}{HTTP State Management Mechanism}{This is the state
64 management specification implemented by this module.}
65\end{seealso}
66
67
68\subsection{Cookie Objects \label{cookie-objects}}
69
Moshe Zadka1b07f2b2000-08-19 14:11:41 +000070\begin{methoddesc}[BaseCookie]{value_decode}{val}
71Return a decoded value from a string representation. Return value can
72be any type. This method does nothing in \class{BaseCookie} --- it exists
73so it can be overridden.
74\end{methoddesc}
75
76\begin{methoddesc}[BaseCookie]{value_encode}{val}
77Return an encoded value. \var{val} can be any type, but return value
78must be a string. This method does nothing in \class{BaseCookie} --- it exists
79so it can be overridden
80
81In general, it should be the case that \method{value_encode} and
82\method{value_decode} are inverses on the range of \var{value_decode}.
83\end{methoddesc}.
84
Fred Drakee5c73522000-08-19 16:54:57 +000085\begin{methoddesc}[BaseCookie]{output}{\optional{attrs\optional{, header\optional{, sep}}}}
Moshe Zadka1b07f2b2000-08-19 14:11:41 +000086Return a string representation suitable to be sent as HTTP headers.
87\var{attrs} and \var{header} are sent to each \class{Morsel}'s \method{output}
88method. \var{sep} is used to join the headers together, and is by default
89a newline.
90\end{methoddesc}
91
Fred Drakee5c73522000-08-19 16:54:57 +000092\begin{methoddesc}[BaseCookie]{js_output}{\optional{attrs}}
Moshe Zadka1b07f2b2000-08-19 14:11:41 +000093Return an embeddable JavaScript snippet, which, if run on a browser which
94supports JavaScript, will act the same as if the HTTP headers was sent.
95
96The meaning for \var{attrs} is the same as in \method{output()}.
97\end{methoddesc}
98
99\begin{methoddesc}[BaseCookie]{load}{rawdata}
100If \var{rawdata} is a string, parse it as an \code{HTTP_COOKIE} and add
101the values found there as \class{Morsel}s. If it is a dictionary, it
Fred Drakee5c73522000-08-19 16:54:57 +0000102is equivalent to:
Moshe Zadka1b07f2b2000-08-19 14:11:41 +0000103
104\begin{verbatim}
Fred Drakee5c73522000-08-19 16:54:57 +0000105for k, v in rawdata.items():
106 cookie[k] = v
107\end{verbatim}
Moshe Zadka1b07f2b2000-08-19 14:11:41 +0000108\end{methoddesc}
109
Fred Drakee5c73522000-08-19 16:54:57 +0000110
111\subsection{Morsel Objects \label{morsel-objects}}
112
113\begin{classdesc}{Morsel}{}
114Abstract a key/value pair, which has some \rfc{2109} attributes.
115
116Morsels are dictionary-like objects, whose set of keys is constant ---
117the valid \rfc{2109} attributes, which are
118
119\begin{itemize}
120\item \code{expires}
121\item \code{path}
122\item \code{comment}
123\item \code{domain}
124\item \code{max-age}
125\item \code{secure}
126\item \code{version}
127\end{itemize}
128
129The keys are case-insensitive.
Moshe Zadka1b07f2b2000-08-19 14:11:41 +0000130\end{classdesc}
131
Fred Drakee5c73522000-08-19 16:54:57 +0000132\begin{memberdesc}[Morsel]{value}
133The value of the cookie.
134\end{memberdesc}
Moshe Zadka1b07f2b2000-08-19 14:11:41 +0000135
Fred Drakee5c73522000-08-19 16:54:57 +0000136\begin{memberdesc}[Morsel]{coded_value}
137The encoded value of the cookie --- this is what should be sent.
138\end{memberdesc}
Moshe Zadka1b07f2b2000-08-19 14:11:41 +0000139
Fred Drakee5c73522000-08-19 16:54:57 +0000140\begin{memberdesc}[Morsel]{key}
141The name of the cookie.
142\end{memberdesc}
143
144\begin{methoddesc}[Morsel]{set}{key, value, coded_value}
145Set the \var{key}, \var{value} and \var{coded_value} members.
146\end{methoddesc}
147
148\begin{methoddesc}[Morsel]{isReservedKey}{K}
149Whether \var{K} is a member of the set of keys of a \class{Morsel}.
150\end{methoddesc}
151
152\begin{methoddesc}[Morsel]{output}{\optional{attrs\optional{, header}}}
153Return a string representation of the Morsel, suitable
154to be sent as an HTTP header. By default, all the attributes are included,
155unless \var{attrs} is given, in which case it should be a list of attributes
156to use. \var{header} is by default \code{"Set-Cookie:"}.
157\end{methoddesc}
158
159\begin{methoddesc}[Morsel]{js_output}{\optional{attrs}}
160Return an embeddable JavaScript snippet, which, if run on a browser which
161supports JavaScript, will act the same as if the HTTP header was sent.
162
163The meaning for \var{attrs} is the same as in \method{output()}.
164\end{methoddesc}.
165
166\begin{methoddesc}[Morsel]{OutputString}{\optional{attrs}}
167Return a string representing the Morsel, without any surrounding HTTP
168or JavaScript.
169
170The meaning for \var{attrs} is the same as in \method{output()}.
171\end{methoddesc}
172
173
174\subsection{Example \label{cookie-example}}
175
176The following example demonstrates how to open a can of spam using the
177\module{spam} module.
178
179\begin{verbatim}
180>>> import Cookie
181>>> C = Cookie.SimpleCookie()
182>>> C = Cookie.SerialCookie()
183>>> C = Cookie.SmartCookie()
184>>> C = Cookie.Cookie() # backwards compatible alias for SmartCookie
185>>> C = Cookie.SmartCookie()
186>>> C["fig"] = "newton"
187>>> C["sugar"] = "wafer"
188>>> C # generate HTTP headers
189Set-Cookie: sugar=wafer;
190Set-Cookie: fig=newton;
191>>> C = Cookie.SmartCookie()
192>>> C["rocky"] = "road"
193>>> C["rocky"]["path"] = "/cookie"
194>>> print C.output(header="Cookie:")
195Cookie: rocky=road; Path=/cookie;
196>>> print C.output(attrs=[], header="Cookie:")
197Cookie: rocky=road;
198>>> C = Cookie.SmartCookie()
199>>> C.load("chips=ahoy; vienna=finger") # load from a string (HTTP header)
200>>> C
201Set-Cookie: vienna=finger;
202Set-Cookie: chips=ahoy;
203>>> C = Cookie.SmartCookie()
204>>> C.load('keebler="E=everybody; L=\"Loves\"; fudge=\012;";')
205>>> C
206Set-Cookie: keebler="E=everybody; L=\"Loves\"; fudge=\012;";
207>>> C = Cookie.SmartCookie()
208>>> C["oreo"] = "doublestuff"
209>>> C["oreo"]["path"] = "/"
210>>> C
211Set-Cookie: oreo="doublestuff"; Path=/;
212>>> C = Cookie.SmartCookie()
213>>> C["twix"] = "none for you"
214>>> C["twix"].value
215'none for you'
216>>> C = Cookie.SimpleCookie()
217>>> C["number"] = 7 # equivalent to C["number"] = str(7)
218>>> C["string"] = "seven"
219>>> C["number"].value
220'7'
221>>> C["string"].value
222'seven'
223>>> C
224Set-Cookie: number=7;
225Set-Cookie: string=seven;
226>>> C = Cookie.SerialCookie()
227>>> C["number"] = 7
228>>> C["string"] = "seven"
229>>> C["number"].value
2307
231>>> C["string"].value
232'seven'
233>>> C
234Set-Cookie: number="I7\012.";
235Set-Cookie: string="S'seven'\012p1\012.";
236>>> C = Cookie.SmartCookie()
237>>> C["number"] = 7
238>>> C["string"] = "seven"
239>>> C["number"].value
2407
241>>> C["string"].value
242'seven'
243>>> C
244Set-Cookie: number="I7\012.";
245Set-Cookie: string=seven;
246\end{verbatim}