paulb@187 | 1 | #!/usr/bin/env python |
paulb@187 | 2 | |
paulb@187 | 3 | """ |
paulb@187 | 4 | Zope classes. |
paulb@403 | 5 | |
paulb@403 | 6 | Copyright (C) 2004, 2005 Paul Boddie <paul@boddie.org.uk> |
paulb@403 | 7 | |
paulb@403 | 8 | This library is free software; you can redistribute it and/or |
paulb@403 | 9 | modify it under the terms of the GNU Lesser General Public |
paulb@403 | 10 | License as published by the Free Software Foundation; either |
paulb@403 | 11 | version 2.1 of the License, or (at your option) any later version. |
paulb@403 | 12 | |
paulb@403 | 13 | This library is distributed in the hope that it will be useful, |
paulb@403 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@403 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
paulb@403 | 16 | Lesser General Public License for more details. |
paulb@403 | 17 | |
paulb@403 | 18 | You should have received a copy of the GNU Lesser General Public |
paulb@403 | 19 | License along with this library; if not, write to the Free Software |
paulb@403 | 20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
paulb@403 | 21 | |
paulb@403 | 22 | -------- |
paulb@403 | 23 | |
paulb@187 | 24 | In places this resembles CGI a lot because Zope seems to recycle a lot of that |
paulb@187 | 25 | baggage. |
paulb@187 | 26 | """ |
paulb@187 | 27 | |
paulb@187 | 28 | import Generic |
paulb@187 | 29 | from Helpers import Environment |
paulb@249 | 30 | from Helpers.Request import Cookie, get_body_field, filter_fields |
paulb@188 | 31 | from Helpers.Response import ConvertingStream |
paulb@270 | 32 | from Helpers.Auth import UserInfo |
paulb@228 | 33 | import cgi |
paulb@187 | 34 | |
paulb@187 | 35 | class Transaction(Generic.Transaction): |
paulb@187 | 36 | |
paulb@187 | 37 | """ |
paulb@187 | 38 | Zope transaction interface. |
paulb@187 | 39 | """ |
paulb@187 | 40 | |
paulb@299 | 41 | def __init__(self, request, adapter): |
paulb@187 | 42 | |
paulb@187 | 43 | """ |
paulb@299 | 44 | Initialise the transaction with the Zope 'request' object and the |
paulb@299 | 45 | 'adapter' which created this transaction. |
paulb@187 | 46 | """ |
paulb@187 | 47 | |
paulb@187 | 48 | self.request = request |
paulb@187 | 49 | self.response = request.RESPONSE |
paulb@299 | 50 | self.adapter = adapter |
paulb@188 | 51 | |
paulb@276 | 52 | # Cached information. |
paulb@276 | 53 | |
paulb@276 | 54 | self._fields = None |
paulb@276 | 55 | |
paulb@188 | 56 | # Attributes which may be changed later. |
paulb@188 | 57 | |
paulb@188 | 58 | self.content_type = None |
paulb@187 | 59 | self.user = None |
paulb@311 | 60 | self.path_info = None |
paulb@187 | 61 | |
paulb@338 | 62 | # Server-related methods. |
paulb@338 | 63 | |
paulb@338 | 64 | def get_server_name(self): |
paulb@338 | 65 | |
paulb@338 | 66 | "Returns the server name." |
paulb@338 | 67 | |
paulb@338 | 68 | return self.request.environ.get("SERVER_NAME") |
paulb@338 | 69 | |
paulb@338 | 70 | def get_server_port(self): |
paulb@338 | 71 | |
paulb@338 | 72 | "Returns the server port as a string." |
paulb@338 | 73 | |
paulb@338 | 74 | return self.request.environ.get("SERVER_PORT") |
paulb@338 | 75 | |
paulb@187 | 76 | # Request-related methods. |
paulb@187 | 77 | |
paulb@187 | 78 | def get_request_stream(self): |
paulb@187 | 79 | |
paulb@187 | 80 | """ |
paulb@187 | 81 | Returns the request stream for the transaction. |
paulb@378 | 82 | |
paulb@378 | 83 | NOTE: This method actually rewinds to the start of the stream, since |
paulb@378 | 84 | NOTE: Zope likes to read everything automatically. |
paulb@187 | 85 | """ |
paulb@187 | 86 | |
paulb@187 | 87 | # NOTE: Possibly not safe. |
paulb@187 | 88 | |
paulb@378 | 89 | stdin = self.request.stdin |
paulb@378 | 90 | stdin.seek(0) |
paulb@378 | 91 | return stdin |
paulb@187 | 92 | |
paulb@187 | 93 | def get_request_method(self): |
paulb@187 | 94 | |
paulb@187 | 95 | """ |
paulb@187 | 96 | Returns the request method. |
paulb@187 | 97 | """ |
paulb@187 | 98 | |
paulb@187 | 99 | return self.request.environ.get("REQUEST_METHOD") |
paulb@187 | 100 | |
paulb@187 | 101 | def get_headers(self): |
paulb@187 | 102 | |
paulb@187 | 103 | """ |
paulb@187 | 104 | Returns all request headers as a dictionary-like object mapping header |
paulb@187 | 105 | names to values. |
paulb@187 | 106 | """ |
paulb@187 | 107 | |
paulb@187 | 108 | return Environment.get_headers(self.request.environ) |
paulb@187 | 109 | |
paulb@187 | 110 | def get_header_values(self, key): |
paulb@187 | 111 | |
paulb@187 | 112 | """ |
paulb@187 | 113 | Returns a list of all request header values associated with the given |
paulb@187 | 114 | 'key'. Note that according to RFC 2616, 'key' is treated as a |
paulb@187 | 115 | case-insensitive string. |
paulb@187 | 116 | """ |
paulb@187 | 117 | |
paulb@187 | 118 | return self.convert_to_list(self.get_headers().get(key)) |
paulb@187 | 119 | |
paulb@187 | 120 | def get_content_type(self): |
paulb@187 | 121 | |
paulb@187 | 122 | """ |
paulb@187 | 123 | Returns the content type specified on the request, along with the |
paulb@187 | 124 | charset employed. |
paulb@187 | 125 | """ |
paulb@187 | 126 | |
paulb@187 | 127 | return self.parse_content_type(self.request.environ.get("CONTENT_TYPE")) |
paulb@187 | 128 | |
paulb@187 | 129 | def get_content_charsets(self): |
paulb@187 | 130 | |
paulb@187 | 131 | """ |
paulb@187 | 132 | Returns the character set preferences. |
paulb@187 | 133 | |
paulb@187 | 134 | NOTE: Not decently supported. |
paulb@187 | 135 | """ |
paulb@187 | 136 | |
paulb@187 | 137 | return self.parse_content_preferences(None) |
paulb@187 | 138 | |
paulb@187 | 139 | def get_content_languages(self): |
paulb@187 | 140 | |
paulb@187 | 141 | """ |
paulb@187 | 142 | Returns extracted language information from the transaction. |
paulb@187 | 143 | |
paulb@187 | 144 | NOTE: Not decently supported. |
paulb@187 | 145 | """ |
paulb@187 | 146 | |
paulb@187 | 147 | return self.parse_content_preferences(None) |
paulb@187 | 148 | |
paulb@187 | 149 | def get_path(self): |
paulb@187 | 150 | |
paulb@187 | 151 | """ |
paulb@187 | 152 | Returns the entire path from the request. |
paulb@187 | 153 | """ |
paulb@187 | 154 | |
paulb@187 | 155 | # NOTE: Based on WebStack.CGI.get_path. |
paulb@187 | 156 | |
paulb@187 | 157 | path = self.get_path_without_query() |
paulb@187 | 158 | qs = self.get_query_string() |
paulb@187 | 159 | if qs: |
paulb@187 | 160 | path += "?" |
paulb@187 | 161 | path += qs |
paulb@187 | 162 | return path |
paulb@187 | 163 | |
paulb@187 | 164 | def get_path_without_query(self): |
paulb@187 | 165 | |
paulb@187 | 166 | """ |
paulb@187 | 167 | Returns the entire path from the request minus the query string. |
paulb@187 | 168 | """ |
paulb@187 | 169 | |
paulb@187 | 170 | # NOTE: Based on WebStack.CGI.get_path. |
paulb@187 | 171 | |
paulb@187 | 172 | path = self.request.environ.get("SCRIPT_NAME") or "" |
paulb@187 | 173 | if self.request.environ.has_key("PATH_INFO"): |
paulb@187 | 174 | path += self.request.environ["PATH_INFO"] |
paulb@187 | 175 | return path |
paulb@187 | 176 | |
paulb@187 | 177 | def get_path_info(self): |
paulb@187 | 178 | |
paulb@187 | 179 | """ |
paulb@187 | 180 | Returns the "path info" (the part of the URL after the resource name |
paulb@187 | 181 | handling the current request) from the request. |
paulb@187 | 182 | """ |
paulb@187 | 183 | |
paulb@316 | 184 | product_path = "/".join(self.adapter.getPhysicalPath()) |
paulb@316 | 185 | path_info = self.request.environ.get("PATH_INFO") or "" |
paulb@316 | 186 | return path_info[len(product_path):] |
paulb@187 | 187 | |
paulb@187 | 188 | def get_query_string(self): |
paulb@187 | 189 | |
paulb@187 | 190 | """ |
paulb@187 | 191 | Returns the query string from the path in the request. |
paulb@187 | 192 | """ |
paulb@187 | 193 | |
paulb@187 | 194 | return self.request.environ.get("QUERY_STRING") or "" |
paulb@187 | 195 | |
paulb@187 | 196 | # Higher level request-related methods. |
paulb@187 | 197 | |
paulb@187 | 198 | def get_fields_from_path(self): |
paulb@187 | 199 | |
paulb@187 | 200 | """ |
paulb@249 | 201 | Extracts fields (or request parameters) from the path specified in the |
paulb@249 | 202 | transaction. The underlying framework may refuse to supply fields from |
paulb@249 | 203 | the path if handling a POST transaction. |
paulb@187 | 204 | |
paulb@187 | 205 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@187 | 206 | single value is associated with any given field name). |
paulb@187 | 207 | """ |
paulb@187 | 208 | |
paulb@249 | 209 | # NOTE: Support at best ISO-8859-1 values. |
paulb@249 | 210 | |
paulb@249 | 211 | fields = {} |
paulb@249 | 212 | for name, values in cgi.parse_qs(self.get_query_string()).items(): |
paulb@249 | 213 | fields[name] = [] |
paulb@249 | 214 | for value in values: |
paulb@249 | 215 | fields[name].append(unicode(value, "iso-8859-1")) |
paulb@249 | 216 | return fields |
paulb@187 | 217 | |
paulb@187 | 218 | def get_fields_from_body(self, encoding=None): |
paulb@187 | 219 | |
paulb@187 | 220 | """ |
paulb@249 | 221 | Extracts fields (or request parameters) from the message body in the |
paulb@249 | 222 | transaction. The optional 'encoding' parameter specifies the character |
paulb@249 | 223 | encoding of the message body for cases where no such information is |
paulb@249 | 224 | available, but where the default encoding is to be overridden. |
paulb@187 | 225 | |
paulb@187 | 226 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@193 | 227 | single value is associated with any given field name). Each value is |
paulb@193 | 228 | either a Unicode object (representing a simple form field, for example) |
paulb@198 | 229 | or a plain string (representing a file upload form field, for example). |
paulb@187 | 230 | """ |
paulb@187 | 231 | |
paulb@249 | 232 | all_fields = self._get_fields(encoding) |
paulb@249 | 233 | fields_from_path = self.get_fields_from_path() |
paulb@249 | 234 | return filter_fields(all_fields, fields_from_path) |
paulb@228 | 235 | |
paulb@249 | 236 | def _get_fields(self, encoding=None): |
paulb@276 | 237 | if self._fields is not None: |
paulb@276 | 238 | return self._fields |
paulb@276 | 239 | |
paulb@224 | 240 | encoding = encoding or self.get_content_type().charset or self.default_charset |
paulb@276 | 241 | self._fields = {} |
paulb@228 | 242 | for field_name, field_values in self.request.form.items(): |
paulb@228 | 243 | |
paulb@228 | 244 | # Find the body values. |
paulb@228 | 245 | |
paulb@200 | 246 | if type(field_values) == type([]): |
paulb@276 | 247 | self._fields[field_name] = [] |
paulb@200 | 248 | for field_str in field_values: |
paulb@276 | 249 | self._fields[field_name].append(get_body_field(field_str, encoding)) |
paulb@249 | 250 | else: |
paulb@276 | 251 | self._fields[field_name] = [get_body_field(field_values, encoding)] |
paulb@249 | 252 | |
paulb@276 | 253 | return self._fields |
paulb@228 | 254 | |
paulb@249 | 255 | def get_fields(self, encoding=None): |
paulb@249 | 256 | |
paulb@249 | 257 | """ |
paulb@249 | 258 | Extracts fields (or request parameters) from both the path specified in |
paulb@249 | 259 | the transaction as well as the message body. The optional 'encoding' |
paulb@249 | 260 | parameter specifies the character encoding of the message body for cases |
paulb@249 | 261 | where no such information is available, but where the default encoding |
paulb@249 | 262 | is to be overridden. |
paulb@228 | 263 | |
paulb@249 | 264 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@249 | 265 | single value is associated with any given field name). Each value is |
paulb@249 | 266 | either a Unicode object (representing a simple form field, for example) |
paulb@249 | 267 | or a plain string (representing a file upload form field, for example). |
paulb@249 | 268 | |
paulb@249 | 269 | Where a given field name is used in both the path and message body to |
paulb@249 | 270 | specify values, the values from both sources will be combined into a |
paulb@249 | 271 | single list associated with that field name. |
paulb@249 | 272 | """ |
paulb@228 | 273 | |
paulb@249 | 274 | # NOTE: Zope seems to provide only body fields upon POST requests. |
paulb@249 | 275 | |
paulb@249 | 276 | if self.get_request_method() == "GET": |
paulb@249 | 277 | return self._get_fields(encoding) |
paulb@249 | 278 | else: |
paulb@249 | 279 | fields = {} |
paulb@249 | 280 | fields.update(self.get_fields_from_path()) |
paulb@249 | 281 | for name, values in self._get_fields(encoding).items(): |
paulb@249 | 282 | if not fields.has_key(name): |
paulb@249 | 283 | fields[name] = values |
paulb@249 | 284 | else: |
paulb@249 | 285 | fields[name] += values |
paulb@187 | 286 | return fields |
paulb@187 | 287 | |
paulb@187 | 288 | def get_user(self): |
paulb@187 | 289 | |
paulb@187 | 290 | """ |
paulb@187 | 291 | Extracts user information from the transaction. |
paulb@187 | 292 | |
paulb@187 | 293 | Returns a username as a string or None if no user is defined. |
paulb@187 | 294 | """ |
paulb@187 | 295 | |
paulb@187 | 296 | if self.user is not None: |
paulb@187 | 297 | return self.user |
paulb@270 | 298 | |
paulb@270 | 299 | auth_header = self.request._auth |
paulb@270 | 300 | if auth_header: |
paulb@270 | 301 | return UserInfo(auth_header).username |
paulb@187 | 302 | else: |
paulb@270 | 303 | return None |
paulb@187 | 304 | |
paulb@187 | 305 | def get_cookies(self): |
paulb@187 | 306 | |
paulb@187 | 307 | """ |
paulb@187 | 308 | Obtains cookie information from the request. |
paulb@187 | 309 | |
paulb@187 | 310 | Returns a dictionary mapping cookie names to cookie objects. |
paulb@187 | 311 | """ |
paulb@187 | 312 | |
paulb@258 | 313 | return self.process_cookies(self.request.cookies, using_strings=1) |
paulb@187 | 314 | |
paulb@187 | 315 | def get_cookie(self, cookie_name): |
paulb@187 | 316 | |
paulb@187 | 317 | """ |
paulb@187 | 318 | Obtains cookie information from the request. |
paulb@187 | 319 | |
paulb@187 | 320 | Returns a cookie object for the given 'cookie_name' or None if no such |
paulb@187 | 321 | cookie exists. |
paulb@187 | 322 | """ |
paulb@187 | 323 | |
paulb@258 | 324 | value = self.request.cookies.get(self.encode_cookie_value(cookie_name)) |
paulb@258 | 325 | if value is not None: |
paulb@276 | 326 | return Cookie(cookie_name, self.decode_cookie_value(value)) |
paulb@258 | 327 | else: |
paulb@258 | 328 | return None |
paulb@187 | 329 | |
paulb@187 | 330 | # Response-related methods. |
paulb@187 | 331 | |
paulb@187 | 332 | def get_response_stream(self): |
paulb@187 | 333 | |
paulb@187 | 334 | """ |
paulb@187 | 335 | Returns the response stream for the transaction. |
paulb@187 | 336 | """ |
paulb@187 | 337 | |
paulb@224 | 338 | # Unicode can upset this operation. Using either the specified charset |
paulb@224 | 339 | # or a default encoding. |
paulb@187 | 340 | |
paulb@252 | 341 | encoding = self.get_response_stream_encoding() |
paulb@252 | 342 | return ConvertingStream(self.response, encoding) |
paulb@252 | 343 | |
paulb@252 | 344 | def get_response_stream_encoding(self): |
paulb@252 | 345 | |
paulb@252 | 346 | """ |
paulb@252 | 347 | Returns the response stream encoding. |
paulb@252 | 348 | """ |
paulb@252 | 349 | |
paulb@188 | 350 | if self.content_type: |
paulb@224 | 351 | encoding = self.content_type.charset |
paulb@252 | 352 | else: |
paulb@252 | 353 | encoding = None |
paulb@252 | 354 | return encoding or self.default_charset |
paulb@187 | 355 | |
paulb@187 | 356 | def get_response_code(self): |
paulb@187 | 357 | |
paulb@187 | 358 | """ |
paulb@187 | 359 | Get the response code associated with the transaction. If no response |
paulb@187 | 360 | code is defined, None is returned. |
paulb@187 | 361 | """ |
paulb@187 | 362 | |
paulb@187 | 363 | return self.response.status |
paulb@187 | 364 | |
paulb@187 | 365 | def set_response_code(self, response_code): |
paulb@187 | 366 | |
paulb@187 | 367 | """ |
paulb@187 | 368 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@187 | 369 | specification. |
paulb@187 | 370 | """ |
paulb@187 | 371 | |
paulb@187 | 372 | self.response.setStatus(response_code) |
paulb@187 | 373 | |
paulb@187 | 374 | def set_header_value(self, header, value): |
paulb@187 | 375 | |
paulb@187 | 376 | """ |
paulb@187 | 377 | Set the HTTP 'header' with the given 'value'. |
paulb@187 | 378 | """ |
paulb@187 | 379 | |
paulb@187 | 380 | self.response.setHeader(header, value) |
paulb@187 | 381 | |
paulb@187 | 382 | def set_content_type(self, content_type): |
paulb@187 | 383 | |
paulb@187 | 384 | """ |
paulb@187 | 385 | Sets the 'content_type' for the response. |
paulb@187 | 386 | """ |
paulb@187 | 387 | |
paulb@188 | 388 | self.content_type = content_type |
paulb@224 | 389 | self.response.setHeader("Content-Type", str(content_type)) |
paulb@187 | 390 | |
paulb@187 | 391 | # Higher level response-related methods. |
paulb@187 | 392 | |
paulb@187 | 393 | def set_cookie(self, cookie): |
paulb@187 | 394 | |
paulb@187 | 395 | """ |
paulb@187 | 396 | Stores the given 'cookie' object in the response. |
paulb@187 | 397 | """ |
paulb@187 | 398 | |
paulb@187 | 399 | self.set_cookie_value(cookie.name, cookie.value) |
paulb@187 | 400 | |
paulb@187 | 401 | def set_cookie_value(self, name, value, path=None, expires=None): |
paulb@187 | 402 | |
paulb@187 | 403 | """ |
paulb@187 | 404 | Stores a cookie with the given 'name' and 'value' in the response. |
paulb@187 | 405 | |
paulb@187 | 406 | The optional 'path' is a string which specifies the scope of the cookie, |
paulb@187 | 407 | and the optional 'expires' parameter is a value compatible with the |
paulb@187 | 408 | time.time function, and indicates the expiry date/time of the cookie. |
paulb@187 | 409 | """ |
paulb@187 | 410 | |
paulb@258 | 411 | self.response.setCookie(self.encode_cookie_value(name), self.encode_cookie_value(value)) |
paulb@187 | 412 | |
paulb@187 | 413 | def delete_cookie(self, cookie_name): |
paulb@187 | 414 | |
paulb@187 | 415 | """ |
paulb@187 | 416 | Adds to the response a request that the cookie with the given |
paulb@187 | 417 | 'cookie_name' be deleted/discarded by the client. |
paulb@187 | 418 | """ |
paulb@187 | 419 | |
paulb@258 | 420 | self.response.expireCookie(self.encode_cookie_value(cookie_name)) |
paulb@187 | 421 | |
paulb@187 | 422 | # Session-related methods. |
paulb@187 | 423 | |
paulb@187 | 424 | def get_session(self, create=1): |
paulb@187 | 425 | |
paulb@187 | 426 | """ |
paulb@187 | 427 | Gets a session corresponding to an identifier supplied in the |
paulb@187 | 428 | transaction. |
paulb@187 | 429 | |
paulb@187 | 430 | If no session has yet been established according to information |
paulb@187 | 431 | provided in the transaction then the optional 'create' parameter |
paulb@187 | 432 | determines whether a new session will be established. |
paulb@187 | 433 | |
paulb@187 | 434 | Where no session has been established and where 'create' is set to 0 |
paulb@187 | 435 | then None is returned. In all other cases, a session object is created |
paulb@187 | 436 | (where appropriate) and returned. |
paulb@187 | 437 | """ |
paulb@187 | 438 | |
paulb@205 | 439 | return self.request.SESSION |
paulb@187 | 440 | |
paulb@187 | 441 | def expire_session(self): |
paulb@187 | 442 | |
paulb@187 | 443 | """ |
paulb@187 | 444 | Expires any session established according to information provided in the |
paulb@187 | 445 | transaction. |
paulb@187 | 446 | """ |
paulb@187 | 447 | |
paulb@205 | 448 | self.request.SESSION.invalidate() |
paulb@187 | 449 | |
paulb@187 | 450 | # vim: tabstop=4 expandtab shiftwidth=4 |