blob: 227add6213b69a2bcd78092ed423eff656837322 [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
Eric S. Raymond83210262001-01-10 19:34:52 +000011cookies, an HTTP state management mechanism. It supports both simple
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
Fred Drake58c95392001-06-29 16:21:47 +000031If \var{input} is given, it is passed to the \method{load()} method.
Moshe Zadka1b07f2b2000-08-19 14:11:41 +000032\end{classdesc}
33
Fred Drakee5c73522000-08-19 16:54:57 +000034\begin{classdesc}{SimpleCookie}{\optional{input}}
Fred Drake58c95392001-06-29 16:21:47 +000035This class derives from \class{BaseCookie} and overrides
36\method{value_decode()} and \method{value_encode()} to be the identity
37and \function{str()} respectively.
Fred Drakee5c73522000-08-19 16:54:57 +000038\end{classdesc}
39
40\begin{classdesc}{SerialCookie}{\optional{input}}
Fred Drake58c95392001-06-29 16:21:47 +000041This class derives from \class{BaseCookie} and overrides
42\method{value_decode()} and \method{value_encode()} to be the
43\function{pickle.loads()} and \function{pickle.dumps()}.
Andrew M. Kuchling120beb62000-08-20 23:33:50 +000044
45Do not use this class. Reading pickled values from a cookie is a
46security hole, as arbitrary client-code can be run on
47\function{pickle.loads()}. It is supported for backwards
48compatibility.
49
Fred Drakee5c73522000-08-19 16:54:57 +000050\end{classdesc}
51
52\begin{classdesc}{SmartCookie}{\optional{input}}
Fred Drake58c95392001-06-29 16:21:47 +000053This class derives from \class{BaseCookie}. It overrides
54\method{value_decode()} to be \function{pickle.loads()} if it is a
55valid pickle, and otherwise the value itself. It overrides
56\method{value_encode()} to be \function{pickle.dumps()} unless it is a
57string, in which case it returns the value itself.
Fred Drakee5c73522000-08-19 16:54:57 +000058
59The same security warning from \class{SerialCookie} applies here.
60\end{classdesc}
61
62
63\begin{seealso}
64 \seerfc{2109}{HTTP State Management Mechanism}{This is the state
65 management specification implemented by this module.}
66\end{seealso}
67
68
69\subsection{Cookie Objects \label{cookie-objects}}
70
Moshe Zadka1b07f2b2000-08-19 14:11:41 +000071\begin{methoddesc}[BaseCookie]{value_decode}{val}
72Return a decoded value from a string representation. Return value can
73be any type. This method does nothing in \class{BaseCookie} --- it exists
74so it can be overridden.
75\end{methoddesc}
76
77\begin{methoddesc}[BaseCookie]{value_encode}{val}
78Return an encoded value. \var{val} can be any type, but return value
79must be a string. This method does nothing in \class{BaseCookie} --- it exists
80so it can be overridden
81
Fred Drake58c95392001-06-29 16:21:47 +000082In general, it should be the case that \method{value_encode()} and
83\method{value_decode()} are inverses on the range of \var{value_decode}.
84\end{methoddesc}
Moshe Zadka1b07f2b2000-08-19 14:11:41 +000085
Fred Drakee5c73522000-08-19 16:54:57 +000086\begin{methoddesc}[BaseCookie]{output}{\optional{attrs\optional{, header\optional{, sep}}}}
Moshe Zadka1b07f2b2000-08-19 14:11:41 +000087Return a string representation suitable to be sent as HTTP headers.
Fred Drake58c95392001-06-29 16:21:47 +000088\var{attrs} and \var{header} are sent to each \class{Morsel}'s
89\method{output()} method. \var{sep} is used to join the headers
90together, and is by default a newline.
Moshe Zadka1b07f2b2000-08-19 14:11:41 +000091\end{methoddesc}
92
Fred Drakee5c73522000-08-19 16:54:57 +000093\begin{methoddesc}[BaseCookie]{js_output}{\optional{attrs}}
Moshe Zadka1b07f2b2000-08-19 14:11:41 +000094Return an embeddable JavaScript snippet, which, if run on a browser which
95supports JavaScript, will act the same as if the HTTP headers was sent.
96
97The meaning for \var{attrs} is the same as in \method{output()}.
98\end{methoddesc}
99
100\begin{methoddesc}[BaseCookie]{load}{rawdata}
101If \var{rawdata} is a string, parse it as an \code{HTTP_COOKIE} and add
102the values found there as \class{Morsel}s. If it is a dictionary, it
Fred Drakee5c73522000-08-19 16:54:57 +0000103is equivalent to:
Moshe Zadka1b07f2b2000-08-19 14:11:41 +0000104
105\begin{verbatim}
Fred Drakee5c73522000-08-19 16:54:57 +0000106for k, v in rawdata.items():
107 cookie[k] = v
108\end{verbatim}
Moshe Zadka1b07f2b2000-08-19 14:11:41 +0000109\end{methoddesc}
110
Fred Drakee5c73522000-08-19 16:54:57 +0000111
112\subsection{Morsel Objects \label{morsel-objects}}
113
114\begin{classdesc}{Morsel}{}
115Abstract a key/value pair, which has some \rfc{2109} attributes.
116
117Morsels are dictionary-like objects, whose set of keys is constant ---
118the valid \rfc{2109} attributes, which are
119
120\begin{itemize}
121\item \code{expires}
122\item \code{path}
123\item \code{comment}
124\item \code{domain}
125\item \code{max-age}
126\item \code{secure}
127\item \code{version}
128\end{itemize}
129
130The keys are case-insensitive.
Moshe Zadka1b07f2b2000-08-19 14:11:41 +0000131\end{classdesc}
132
Fred Drakee5c73522000-08-19 16:54:57 +0000133\begin{memberdesc}[Morsel]{value}
134The value of the cookie.
135\end{memberdesc}
Moshe Zadka1b07f2b2000-08-19 14:11:41 +0000136
Fred Drakee5c73522000-08-19 16:54:57 +0000137\begin{memberdesc}[Morsel]{coded_value}
138The encoded value of the cookie --- this is what should be sent.
139\end{memberdesc}
Moshe Zadka1b07f2b2000-08-19 14:11:41 +0000140
Fred Drakee5c73522000-08-19 16:54:57 +0000141\begin{memberdesc}[Morsel]{key}
142The name of the cookie.
143\end{memberdesc}
144
145\begin{methoddesc}[Morsel]{set}{key, value, coded_value}
146Set the \var{key}, \var{value} and \var{coded_value} members.
147\end{methoddesc}
148
149\begin{methoddesc}[Morsel]{isReservedKey}{K}
150Whether \var{K} is a member of the set of keys of a \class{Morsel}.
151\end{methoddesc}
152
153\begin{methoddesc}[Morsel]{output}{\optional{attrs\optional{, header}}}
154Return a string representation of the Morsel, suitable
155to be sent as an HTTP header. By default, all the attributes are included,
156unless \var{attrs} is given, in which case it should be a list of attributes
157to use. \var{header} is by default \code{"Set-Cookie:"}.
158\end{methoddesc}
159
160\begin{methoddesc}[Morsel]{js_output}{\optional{attrs}}
161Return an embeddable JavaScript snippet, which, if run on a browser which
162supports JavaScript, will act the same as if the HTTP header was sent.
163
164The meaning for \var{attrs} is the same as in \method{output()}.
Fred Drake58c95392001-06-29 16:21:47 +0000165\end{methoddesc}
Fred Drakee5c73522000-08-19 16:54:57 +0000166
167\begin{methoddesc}[Morsel]{OutputString}{\optional{attrs}}
168Return a string representing the Morsel, without any surrounding HTTP
169or JavaScript.
170
171The meaning for \var{attrs} is the same as in \method{output()}.
172\end{methoddesc}
173
174
175\subsection{Example \label{cookie-example}}
176
Ka-Ping Yee3a9582f2001-01-18 07:50:17 +0000177The following example demonstrates how to use the \module{Cookie} module.
Fred Drakee5c73522000-08-19 16:54:57 +0000178
179\begin{verbatim}
180>>> import Cookie
181>>> C = Cookie.SimpleCookie()
182>>> C = Cookie.SerialCookie()
183>>> C = Cookie.SmartCookie()
Ka-Ping Yee3a9582f2001-01-18 07:50:17 +0000184>>> C = Cookie.Cookie() # backwards-compatible alias for SmartCookie
Fred Drakee5c73522000-08-19 16:54:57 +0000185>>> C = Cookie.SmartCookie()
186>>> C["fig"] = "newton"
187>>> C["sugar"] = "wafer"
Ka-Ping Yee3a9582f2001-01-18 07:50:17 +0000188>>> print C # generate HTTP headers
189Set-Cookie: sugar=wafer;
190Set-Cookie: fig=newton;
191>>> print C.output() # same thing
Fred Drakee5c73522000-08-19 16:54:57 +0000192Set-Cookie: sugar=wafer;
193Set-Cookie: fig=newton;
194>>> C = Cookie.SmartCookie()
195>>> C["rocky"] = "road"
196>>> C["rocky"]["path"] = "/cookie"
197>>> print C.output(header="Cookie:")
198Cookie: rocky=road; Path=/cookie;
199>>> print C.output(attrs=[], header="Cookie:")
200Cookie: rocky=road;
201>>> C = Cookie.SmartCookie()
202>>> C.load("chips=ahoy; vienna=finger") # load from a string (HTTP header)
Ka-Ping Yee3a9582f2001-01-18 07:50:17 +0000203>>> print C
Fred Drakee5c73522000-08-19 16:54:57 +0000204Set-Cookie: vienna=finger;
205Set-Cookie: chips=ahoy;
206>>> C = Cookie.SmartCookie()
Ka-Ping Yee3a9582f2001-01-18 07:50:17 +0000207>>> C.load('keebler="E=everybody; L=\\"Loves\\"; fudge=\\012;";')
208>>> print C
Fred Drakee5c73522000-08-19 16:54:57 +0000209Set-Cookie: keebler="E=everybody; L=\"Loves\"; fudge=\012;";
210>>> C = Cookie.SmartCookie()
211>>> C["oreo"] = "doublestuff"
212>>> C["oreo"]["path"] = "/"
Ka-Ping Yee3a9582f2001-01-18 07:50:17 +0000213>>> print C
214Set-Cookie: oreo=doublestuff; Path=/;
Fred Drakee5c73522000-08-19 16:54:57 +0000215>>> C = Cookie.SmartCookie()
216>>> C["twix"] = "none for you"
217>>> C["twix"].value
218'none for you'
219>>> C = Cookie.SimpleCookie()
220>>> C["number"] = 7 # equivalent to C["number"] = str(7)
221>>> C["string"] = "seven"
222>>> C["number"].value
223'7'
224>>> C["string"].value
225'seven'
Ka-Ping Yee3a9582f2001-01-18 07:50:17 +0000226>>> print C
Fred Drakee5c73522000-08-19 16:54:57 +0000227Set-Cookie: number=7;
228Set-Cookie: string=seven;
229>>> C = Cookie.SerialCookie()
230>>> C["number"] = 7
231>>> C["string"] = "seven"
232>>> C["number"].value
2337
234>>> C["string"].value
235'seven'
Ka-Ping Yee3a9582f2001-01-18 07:50:17 +0000236>>> print C
Fred Drakee5c73522000-08-19 16:54:57 +0000237Set-Cookie: number="I7\012.";
238Set-Cookie: string="S'seven'\012p1\012.";
239>>> C = Cookie.SmartCookie()
240>>> C["number"] = 7
241>>> C["string"] = "seven"
242>>> C["number"].value
2437
244>>> C["string"].value
245'seven'
Ka-Ping Yee3a9582f2001-01-18 07:50:17 +0000246>>> print C
Fred Drakee5c73522000-08-19 16:54:57 +0000247Set-Cookie: number="I7\012.";
248Set-Cookie: string=seven;
249\end{verbatim}