Moshe Zadka | 1b07f2b | 2000-08-19 14:11:41 +0000 | [diff] [blame] | 1 | \section{\module{Cookie} --- |
Fred Drake | e5c7352 | 2000-08-19 16:54:57 +0000 | [diff] [blame] | 2 | HTTP state management} |
Moshe Zadka | 1b07f2b | 2000-08-19 14:11:41 +0000 | [diff] [blame] | 3 | |
Fred Drake | e5c7352 | 2000-08-19 16:54:57 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{Cookie} |
| 5 | \modulesynopsis{Support for HTTP state management (cookies).} |
Moshe Zadka | 1b07f2b | 2000-08-19 14:11:41 +0000 | [diff] [blame] | 6 | \moduleauthor{Timothy O'Malley}{timo@alum.mit.edu} |
| 7 | \sectionauthor{Moshe Zadka}{moshez@zadka.site.co.il} |
| 8 | |
Moshe Zadka | 1b07f2b | 2000-08-19 14:11:41 +0000 | [diff] [blame] | 9 | |
| 10 | The \module{Cookie} module defines classes for abstracting the concept of |
Eric S. Raymond | 8321026 | 2001-01-10 19:34:52 +0000 | [diff] [blame] | 11 | cookies, an HTTP state management mechanism. It supports both simple |
Moshe Zadka | 1b07f2b | 2000-08-19 14:11:41 +0000 | [diff] [blame] | 12 | string-only cookies, and provides an abstraction for having any serializable |
| 13 | data-type as cookie value. |
| 14 | |
Andrew M. Kuchling | 120beb6 | 2000-08-20 23:33:50 +0000 | [diff] [blame] | 15 | The module formerly strictly applied the parsing rules described in in |
| 16 | the \rfc{2109} and \rfc{2068} specifications. It has since been discovered |
| 17 | that MSIE 3.0x doesn't follow the character rules outlined in those |
| 18 | specs. As a result, the parsing rules used are a bit less strict. |
Moshe Zadka | 1b07f2b | 2000-08-19 14:11:41 +0000 | [diff] [blame] | 19 | |
| 20 | \begin{excdesc}{CookieError} |
Fred Drake | e5c7352 | 2000-08-19 16:54:57 +0000 | [diff] [blame] | 21 | Exception failing because of \rfc{2109} invalidity: incorrect |
| 22 | attributes, incorrect \code{Set-Cookie} header, etc. |
Moshe Zadka | 1b07f2b | 2000-08-19 14:11:41 +0000 | [diff] [blame] | 23 | \end{excdesc} |
| 24 | |
Fred Drake | e5c7352 | 2000-08-19 16:54:57 +0000 | [diff] [blame] | 25 | \begin{classdesc}{BaseCookie}{\optional{input}} |
Moshe Zadka | 1b07f2b | 2000-08-19 14:11:41 +0000 | [diff] [blame] | 26 | This class is a dictionary-like object whose keys are strings and |
| 27 | whose values are \class{Morsel}s. Note that upon setting a key to |
| 28 | a value, the value is first converted to a \class{Morsel} containing |
| 29 | the key and the value. |
| 30 | |
Fred Drake | 58c9539 | 2001-06-29 16:21:47 +0000 | [diff] [blame] | 31 | If \var{input} is given, it is passed to the \method{load()} method. |
Moshe Zadka | 1b07f2b | 2000-08-19 14:11:41 +0000 | [diff] [blame] | 32 | \end{classdesc} |
| 33 | |
Fred Drake | e5c7352 | 2000-08-19 16:54:57 +0000 | [diff] [blame] | 34 | \begin{classdesc}{SimpleCookie}{\optional{input}} |
Fred Drake | 58c9539 | 2001-06-29 16:21:47 +0000 | [diff] [blame] | 35 | This class derives from \class{BaseCookie} and overrides |
| 36 | \method{value_decode()} and \method{value_encode()} to be the identity |
| 37 | and \function{str()} respectively. |
Fred Drake | e5c7352 | 2000-08-19 16:54:57 +0000 | [diff] [blame] | 38 | \end{classdesc} |
| 39 | |
| 40 | \begin{classdesc}{SerialCookie}{\optional{input}} |
Fred Drake | 58c9539 | 2001-06-29 16:21:47 +0000 | [diff] [blame] | 41 | This 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. Kuchling | 120beb6 | 2000-08-20 23:33:50 +0000 | [diff] [blame] | 44 | |
| 45 | Do not use this class. Reading pickled values from a cookie is a |
| 46 | security hole, as arbitrary client-code can be run on |
| 47 | \function{pickle.loads()}. It is supported for backwards |
| 48 | compatibility. |
| 49 | |
Fred Drake | e5c7352 | 2000-08-19 16:54:57 +0000 | [diff] [blame] | 50 | \end{classdesc} |
| 51 | |
| 52 | \begin{classdesc}{SmartCookie}{\optional{input}} |
Fred Drake | 58c9539 | 2001-06-29 16:21:47 +0000 | [diff] [blame] | 53 | This class derives from \class{BaseCookie}. It overrides |
| 54 | \method{value_decode()} to be \function{pickle.loads()} if it is a |
| 55 | valid pickle, and otherwise the value itself. It overrides |
| 56 | \method{value_encode()} to be \function{pickle.dumps()} unless it is a |
| 57 | string, in which case it returns the value itself. |
Fred Drake | e5c7352 | 2000-08-19 16:54:57 +0000 | [diff] [blame] | 58 | |
| 59 | The 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 Zadka | 1b07f2b | 2000-08-19 14:11:41 +0000 | [diff] [blame] | 71 | \begin{methoddesc}[BaseCookie]{value_decode}{val} |
| 72 | Return a decoded value from a string representation. Return value can |
| 73 | be any type. This method does nothing in \class{BaseCookie} --- it exists |
| 74 | so it can be overridden. |
| 75 | \end{methoddesc} |
| 76 | |
| 77 | \begin{methoddesc}[BaseCookie]{value_encode}{val} |
| 78 | Return an encoded value. \var{val} can be any type, but return value |
| 79 | must be a string. This method does nothing in \class{BaseCookie} --- it exists |
| 80 | so it can be overridden |
| 81 | |
Fred Drake | 58c9539 | 2001-06-29 16:21:47 +0000 | [diff] [blame] | 82 | In 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 Zadka | 1b07f2b | 2000-08-19 14:11:41 +0000 | [diff] [blame] | 85 | |
Fred Drake | e5c7352 | 2000-08-19 16:54:57 +0000 | [diff] [blame] | 86 | \begin{methoddesc}[BaseCookie]{output}{\optional{attrs\optional{, header\optional{, sep}}}} |
Moshe Zadka | 1b07f2b | 2000-08-19 14:11:41 +0000 | [diff] [blame] | 87 | Return a string representation suitable to be sent as HTTP headers. |
Fred Drake | 58c9539 | 2001-06-29 16:21:47 +0000 | [diff] [blame] | 88 | \var{attrs} and \var{header} are sent to each \class{Morsel}'s |
| 89 | \method{output()} method. \var{sep} is used to join the headers |
| 90 | together, and is by default a newline. |
Moshe Zadka | 1b07f2b | 2000-08-19 14:11:41 +0000 | [diff] [blame] | 91 | \end{methoddesc} |
| 92 | |
Fred Drake | e5c7352 | 2000-08-19 16:54:57 +0000 | [diff] [blame] | 93 | \begin{methoddesc}[BaseCookie]{js_output}{\optional{attrs}} |
Moshe Zadka | 1b07f2b | 2000-08-19 14:11:41 +0000 | [diff] [blame] | 94 | Return an embeddable JavaScript snippet, which, if run on a browser which |
| 95 | supports JavaScript, will act the same as if the HTTP headers was sent. |
| 96 | |
| 97 | The meaning for \var{attrs} is the same as in \method{output()}. |
| 98 | \end{methoddesc} |
| 99 | |
| 100 | \begin{methoddesc}[BaseCookie]{load}{rawdata} |
| 101 | If \var{rawdata} is a string, parse it as an \code{HTTP_COOKIE} and add |
| 102 | the values found there as \class{Morsel}s. If it is a dictionary, it |
Fred Drake | e5c7352 | 2000-08-19 16:54:57 +0000 | [diff] [blame] | 103 | is equivalent to: |
Moshe Zadka | 1b07f2b | 2000-08-19 14:11:41 +0000 | [diff] [blame] | 104 | |
| 105 | \begin{verbatim} |
Fred Drake | e5c7352 | 2000-08-19 16:54:57 +0000 | [diff] [blame] | 106 | for k, v in rawdata.items(): |
| 107 | cookie[k] = v |
| 108 | \end{verbatim} |
Moshe Zadka | 1b07f2b | 2000-08-19 14:11:41 +0000 | [diff] [blame] | 109 | \end{methoddesc} |
| 110 | |
Fred Drake | e5c7352 | 2000-08-19 16:54:57 +0000 | [diff] [blame] | 111 | |
| 112 | \subsection{Morsel Objects \label{morsel-objects}} |
| 113 | |
| 114 | \begin{classdesc}{Morsel}{} |
| 115 | Abstract a key/value pair, which has some \rfc{2109} attributes. |
| 116 | |
| 117 | Morsels are dictionary-like objects, whose set of keys is constant --- |
| 118 | the 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 | |
| 130 | The keys are case-insensitive. |
Moshe Zadka | 1b07f2b | 2000-08-19 14:11:41 +0000 | [diff] [blame] | 131 | \end{classdesc} |
| 132 | |
Fred Drake | e5c7352 | 2000-08-19 16:54:57 +0000 | [diff] [blame] | 133 | \begin{memberdesc}[Morsel]{value} |
| 134 | The value of the cookie. |
| 135 | \end{memberdesc} |
Moshe Zadka | 1b07f2b | 2000-08-19 14:11:41 +0000 | [diff] [blame] | 136 | |
Fred Drake | e5c7352 | 2000-08-19 16:54:57 +0000 | [diff] [blame] | 137 | \begin{memberdesc}[Morsel]{coded_value} |
| 138 | The encoded value of the cookie --- this is what should be sent. |
| 139 | \end{memberdesc} |
Moshe Zadka | 1b07f2b | 2000-08-19 14:11:41 +0000 | [diff] [blame] | 140 | |
Fred Drake | e5c7352 | 2000-08-19 16:54:57 +0000 | [diff] [blame] | 141 | \begin{memberdesc}[Morsel]{key} |
| 142 | The name of the cookie. |
| 143 | \end{memberdesc} |
| 144 | |
| 145 | \begin{methoddesc}[Morsel]{set}{key, value, coded_value} |
| 146 | Set the \var{key}, \var{value} and \var{coded_value} members. |
| 147 | \end{methoddesc} |
| 148 | |
| 149 | \begin{methoddesc}[Morsel]{isReservedKey}{K} |
| 150 | Whether \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}}} |
| 154 | Return a string representation of the Morsel, suitable |
| 155 | to be sent as an HTTP header. By default, all the attributes are included, |
| 156 | unless \var{attrs} is given, in which case it should be a list of attributes |
| 157 | to use. \var{header} is by default \code{"Set-Cookie:"}. |
| 158 | \end{methoddesc} |
| 159 | |
| 160 | \begin{methoddesc}[Morsel]{js_output}{\optional{attrs}} |
| 161 | Return an embeddable JavaScript snippet, which, if run on a browser which |
| 162 | supports JavaScript, will act the same as if the HTTP header was sent. |
| 163 | |
| 164 | The meaning for \var{attrs} is the same as in \method{output()}. |
Fred Drake | 58c9539 | 2001-06-29 16:21:47 +0000 | [diff] [blame] | 165 | \end{methoddesc} |
Fred Drake | e5c7352 | 2000-08-19 16:54:57 +0000 | [diff] [blame] | 166 | |
| 167 | \begin{methoddesc}[Morsel]{OutputString}{\optional{attrs}} |
| 168 | Return a string representing the Morsel, without any surrounding HTTP |
| 169 | or JavaScript. |
| 170 | |
| 171 | The 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 Yee | 3a9582f | 2001-01-18 07:50:17 +0000 | [diff] [blame] | 177 | The following example demonstrates how to use the \module{Cookie} module. |
Fred Drake | e5c7352 | 2000-08-19 16:54:57 +0000 | [diff] [blame] | 178 | |
| 179 | \begin{verbatim} |
| 180 | >>> import Cookie |
| 181 | >>> C = Cookie.SimpleCookie() |
| 182 | >>> C = Cookie.SerialCookie() |
| 183 | >>> C = Cookie.SmartCookie() |
Ka-Ping Yee | 3a9582f | 2001-01-18 07:50:17 +0000 | [diff] [blame] | 184 | >>> C = Cookie.Cookie() # backwards-compatible alias for SmartCookie |
Fred Drake | e5c7352 | 2000-08-19 16:54:57 +0000 | [diff] [blame] | 185 | >>> C = Cookie.SmartCookie() |
| 186 | >>> C["fig"] = "newton" |
| 187 | >>> C["sugar"] = "wafer" |
Ka-Ping Yee | 3a9582f | 2001-01-18 07:50:17 +0000 | [diff] [blame] | 188 | >>> print C # generate HTTP headers |
| 189 | Set-Cookie: sugar=wafer; |
| 190 | Set-Cookie: fig=newton; |
| 191 | >>> print C.output() # same thing |
Fred Drake | e5c7352 | 2000-08-19 16:54:57 +0000 | [diff] [blame] | 192 | Set-Cookie: sugar=wafer; |
| 193 | Set-Cookie: fig=newton; |
| 194 | >>> C = Cookie.SmartCookie() |
| 195 | >>> C["rocky"] = "road" |
| 196 | >>> C["rocky"]["path"] = "/cookie" |
| 197 | >>> print C.output(header="Cookie:") |
| 198 | Cookie: rocky=road; Path=/cookie; |
| 199 | >>> print C.output(attrs=[], header="Cookie:") |
| 200 | Cookie: rocky=road; |
| 201 | >>> C = Cookie.SmartCookie() |
| 202 | >>> C.load("chips=ahoy; vienna=finger") # load from a string (HTTP header) |
Ka-Ping Yee | 3a9582f | 2001-01-18 07:50:17 +0000 | [diff] [blame] | 203 | >>> print C |
Fred Drake | e5c7352 | 2000-08-19 16:54:57 +0000 | [diff] [blame] | 204 | Set-Cookie: vienna=finger; |
| 205 | Set-Cookie: chips=ahoy; |
| 206 | >>> C = Cookie.SmartCookie() |
Ka-Ping Yee | 3a9582f | 2001-01-18 07:50:17 +0000 | [diff] [blame] | 207 | >>> C.load('keebler="E=everybody; L=\\"Loves\\"; fudge=\\012;";') |
| 208 | >>> print C |
Fred Drake | e5c7352 | 2000-08-19 16:54:57 +0000 | [diff] [blame] | 209 | Set-Cookie: keebler="E=everybody; L=\"Loves\"; fudge=\012;"; |
| 210 | >>> C = Cookie.SmartCookie() |
| 211 | >>> C["oreo"] = "doublestuff" |
| 212 | >>> C["oreo"]["path"] = "/" |
Ka-Ping Yee | 3a9582f | 2001-01-18 07:50:17 +0000 | [diff] [blame] | 213 | >>> print C |
| 214 | Set-Cookie: oreo=doublestuff; Path=/; |
Fred Drake | e5c7352 | 2000-08-19 16:54:57 +0000 | [diff] [blame] | 215 | >>> 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 Yee | 3a9582f | 2001-01-18 07:50:17 +0000 | [diff] [blame] | 226 | >>> print C |
Fred Drake | e5c7352 | 2000-08-19 16:54:57 +0000 | [diff] [blame] | 227 | Set-Cookie: number=7; |
| 228 | Set-Cookie: string=seven; |
| 229 | >>> C = Cookie.SerialCookie() |
| 230 | >>> C["number"] = 7 |
| 231 | >>> C["string"] = "seven" |
| 232 | >>> C["number"].value |
| 233 | 7 |
| 234 | >>> C["string"].value |
| 235 | 'seven' |
Ka-Ping Yee | 3a9582f | 2001-01-18 07:50:17 +0000 | [diff] [blame] | 236 | >>> print C |
Fred Drake | e5c7352 | 2000-08-19 16:54:57 +0000 | [diff] [blame] | 237 | Set-Cookie: number="I7\012."; |
| 238 | Set-Cookie: string="S'seven'\012p1\012."; |
| 239 | >>> C = Cookie.SmartCookie() |
| 240 | >>> C["number"] = 7 |
| 241 | >>> C["string"] = "seven" |
| 242 | >>> C["number"].value |
| 243 | 7 |
| 244 | >>> C["string"].value |
| 245 | 'seven' |
Ka-Ping Yee | 3a9582f | 2001-01-18 07:50:17 +0000 | [diff] [blame] | 246 | >>> print C |
Fred Drake | e5c7352 | 2000-08-19 16:54:57 +0000 | [diff] [blame] | 247 | Set-Cookie: number="I7\012."; |
| 248 | Set-Cookie: string=seven; |
| 249 | \end{verbatim} |