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