paulb@1 | 1 | #!/usr/bin/env python |
paulb@1 | 2 | |
paulb@1 | 3 | """ |
paulb@1 | 4 | Webware 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@1 | 21 | """ |
paulb@1 | 22 | |
paulb@1 | 23 | import Generic |
paulb@71 | 24 | from cgi import parse_qs |
paulb@85 | 25 | import StringIO |
paulb@108 | 26 | from Helpers import Environment |
paulb@249 | 27 | from Helpers.Request import Cookie, get_body_field, filter_fields |
paulb@167 | 28 | from Helpers.Response import ConvertingStream |
paulb@1 | 29 | |
paulb@1 | 30 | class Transaction(Generic.Transaction): |
paulb@1 | 31 | |
paulb@1 | 32 | """ |
paulb@1 | 33 | Webware transaction interface. |
paulb@1 | 34 | """ |
paulb@1 | 35 | |
paulb@1 | 36 | def __init__(self, trans): |
paulb@1 | 37 | |
paulb@1 | 38 | "Initialise the transaction using the Webware transaction 'trans'." |
paulb@1 | 39 | |
paulb@1 | 40 | self.trans = trans |
paulb@311 | 41 | self.content_type = None |
paulb@128 | 42 | self.user = None |
paulb@311 | 43 | self.path_info = None |
paulb@1 | 44 | |
paulb@338 | 45 | # Server-related methods. |
paulb@338 | 46 | |
paulb@338 | 47 | def get_server_name(self): |
paulb@338 | 48 | |
paulb@338 | 49 | "Returns the server name." |
paulb@338 | 50 | |
paulb@338 | 51 | return self.trans.request().serverURL().split("/")[0].split(":")[0] |
paulb@338 | 52 | |
paulb@338 | 53 | def get_server_port(self): |
paulb@338 | 54 | |
paulb@338 | 55 | "Returns the server port as a string." |
paulb@338 | 56 | |
paulb@338 | 57 | host_and_port = self.trans.request().serverURL().split("/")[0].split(":") |
paulb@338 | 58 | if len(host_and_port) > 1: |
paulb@338 | 59 | return host_and_port[1] |
paulb@338 | 60 | else: |
paulb@338 | 61 | return "80" |
paulb@338 | 62 | |
paulb@1 | 63 | # Request-related methods. |
paulb@1 | 64 | |
paulb@1 | 65 | def get_request_stream(self): |
paulb@1 | 66 | |
paulb@1 | 67 | """ |
paulb@186 | 68 | Returns the request stream for the transaction. |
paulb@1 | 69 | """ |
paulb@1 | 70 | |
paulb@85 | 71 | request = self.trans.request() |
paulb@85 | 72 | try: |
paulb@85 | 73 | stream = request.rawInput(rewind=1) |
paulb@85 | 74 | if stream is None: |
paulb@85 | 75 | return StringIO.StringIO("") |
paulb@85 | 76 | |
paulb@85 | 77 | # NOTE: Dubious catch-all situation, but it is difficult to control |
paulb@85 | 78 | # NOTE: cases where Webware's internals themselves fail. |
paulb@85 | 79 | |
paulb@85 | 80 | except: |
paulb@1 | 81 | return StringIO.StringIO("") |
paulb@1 | 82 | |
paulb@1 | 83 | return stream |
paulb@1 | 84 | |
paulb@1 | 85 | def get_request_method(self): |
paulb@1 | 86 | |
paulb@1 | 87 | """ |
paulb@186 | 88 | Returns the request method. |
paulb@1 | 89 | """ |
paulb@1 | 90 | |
paulb@1 | 91 | return self.trans.request().method() |
paulb@1 | 92 | |
paulb@1 | 93 | def get_headers(self): |
paulb@1 | 94 | |
paulb@1 | 95 | """ |
paulb@186 | 96 | Returns all request headers as a dictionary-like object mapping header |
paulb@186 | 97 | names to values. |
paulb@186 | 98 | |
paulb@91 | 99 | NOTE: If duplicate header names are permitted, then this interface will |
paulb@91 | 100 | NOTE: need to change. |
paulb@1 | 101 | """ |
paulb@1 | 102 | |
paulb@91 | 103 | # Use the Webware environment and some assumptions about variable names. |
paulb@91 | 104 | # NOTE: Using lower case for the header names. |
paulb@1 | 105 | |
paulb@91 | 106 | env = self.trans.request().environ() |
paulb@108 | 107 | return Environment.get_headers(env) |
paulb@1 | 108 | |
paulb@65 | 109 | def get_header_values(self, key): |
paulb@65 | 110 | |
paulb@65 | 111 | """ |
paulb@186 | 112 | Returns a list of all request header values associated with the given |
paulb@186 | 113 | 'key'. Note that according to RFC 2616, 'key' is treated as a |
paulb@186 | 114 | case-insensitive string. |
paulb@65 | 115 | """ |
paulb@65 | 116 | |
paulb@91 | 117 | # Use the Webware environment and some assumptions about variable names. |
paulb@65 | 118 | |
paulb@91 | 119 | env = self.trans.request().environ() |
paulb@91 | 120 | cgi_key = "HTTP_" + key.replace("-", "_").upper() |
paulb@91 | 121 | if env.has_key(cgi_key): |
paulb@91 | 122 | return [env[cgi_key]] |
paulb@91 | 123 | else: |
paulb@91 | 124 | return [] |
paulb@65 | 125 | |
paulb@1 | 126 | def get_content_type(self): |
paulb@1 | 127 | |
paulb@1 | 128 | """ |
paulb@186 | 129 | Returns the content type specified on the request, along with the |
paulb@186 | 130 | charset employed. |
paulb@1 | 131 | """ |
paulb@1 | 132 | |
paulb@1 | 133 | return self.parse_content_type(self.trans.request().contentType()) |
paulb@1 | 134 | |
paulb@1 | 135 | def get_content_charsets(self): |
paulb@1 | 136 | |
paulb@1 | 137 | """ |
paulb@1 | 138 | Returns the character set preferences. |
paulb@1 | 139 | NOTE: Requires enhancements to HTTPRequest. |
paulb@1 | 140 | """ |
paulb@1 | 141 | |
paulb@1 | 142 | return self.trans.request().contentCharsets() |
paulb@1 | 143 | |
paulb@1 | 144 | def get_content_languages(self): |
paulb@1 | 145 | |
paulb@1 | 146 | """ |
paulb@186 | 147 | Returns extracted language information from the transaction. |
paulb@1 | 148 | NOTE: Requires enhancements to HTTPRequest. |
paulb@1 | 149 | """ |
paulb@1 | 150 | |
paulb@1 | 151 | return self.trans.request().contentLanguages() |
paulb@1 | 152 | |
paulb@5 | 153 | def get_path(self): |
paulb@5 | 154 | |
paulb@5 | 155 | """ |
paulb@186 | 156 | Returns the entire path from the request. |
paulb@5 | 157 | """ |
paulb@5 | 158 | |
paulb@14 | 159 | return self.trans.request().uri() |
paulb@5 | 160 | |
paulb@162 | 161 | def get_path_without_query(self): |
paulb@162 | 162 | |
paulb@162 | 163 | """ |
paulb@186 | 164 | Returns the entire path from the request minus the query string. |
paulb@162 | 165 | """ |
paulb@162 | 166 | |
paulb@162 | 167 | return self.get_path().split("?")[0] |
paulb@162 | 168 | |
paulb@1 | 169 | def get_path_info(self): |
paulb@1 | 170 | |
paulb@1 | 171 | """ |
paulb@186 | 172 | Returns the "path info" (the part of the URL after the resource name |
paulb@186 | 173 | handling the current request) from the request. |
paulb@1 | 174 | """ |
paulb@1 | 175 | |
paulb@316 | 176 | path_info = self.trans.request().pathInfo() |
paulb@316 | 177 | context_name = self.trans.request().contextName() |
paulb@316 | 178 | if path_info.startswith(context_name): |
paulb@316 | 179 | return path_info[len(context_name):] |
paulb@216 | 180 | else: |
paulb@316 | 181 | return path_info |
paulb@1 | 182 | |
paulb@69 | 183 | def get_query_string(self): |
paulb@69 | 184 | |
paulb@69 | 185 | """ |
paulb@186 | 186 | Returns the query string from the path in the request. |
paulb@69 | 187 | """ |
paulb@69 | 188 | |
paulb@69 | 189 | return self.trans.request().queryString() |
paulb@69 | 190 | |
paulb@1 | 191 | # Higher level request-related methods. |
paulb@1 | 192 | |
paulb@430 | 193 | def get_fields_from_path(self, encoding=None): |
paulb@69 | 194 | |
paulb@69 | 195 | """ |
paulb@249 | 196 | Extracts fields (or request parameters) from the path specified in the |
paulb@249 | 197 | transaction. The underlying framework may refuse to supply fields from |
paulb@430 | 198 | the path if handling a POST transaction. The optional 'encoding' |
paulb@430 | 199 | parameter specifies the character encoding of the query string for cases |
paulb@430 | 200 | where the default encoding is to be overridden. |
paulb@69 | 201 | |
paulb@71 | 202 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@71 | 203 | single value is associated with any given field name). |
paulb@69 | 204 | """ |
paulb@69 | 205 | |
paulb@249 | 206 | fields = {} |
paulb@249 | 207 | for name, values in parse_qs(self.get_query_string(), keep_blank_values=1).items(): |
paulb@430 | 208 | name = self.decode_path(name, encoding) |
paulb@249 | 209 | fields[name] = [] |
paulb@249 | 210 | for value in values: |
paulb@430 | 211 | value = self.decode_path(value, encoding) |
paulb@430 | 212 | fields[name].append(value) |
paulb@249 | 213 | return fields |
paulb@69 | 214 | |
paulb@167 | 215 | def get_fields_from_body(self, encoding=None): |
paulb@69 | 216 | |
paulb@69 | 217 | """ |
paulb@249 | 218 | Extracts fields (or request parameters) from the message body in the |
paulb@249 | 219 | transaction. The optional 'encoding' parameter specifies the character |
paulb@249 | 220 | encoding of the message body for cases where no such information is |
paulb@249 | 221 | available, but where the default encoding is to be overridden. |
paulb@69 | 222 | |
paulb@73 | 223 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@193 | 224 | single value is associated with any given field name). Each value is |
paulb@193 | 225 | either a Unicode object (representing a simple form field, for example) |
paulb@198 | 226 | or a plain string (representing a file upload form field, for example). |
paulb@69 | 227 | """ |
paulb@69 | 228 | |
paulb@249 | 229 | all_fields = self._get_fields(encoding) |
paulb@249 | 230 | fields_from_path = self.get_fields_from_path() |
paulb@249 | 231 | return filter_fields(all_fields, fields_from_path) |
paulb@249 | 232 | |
paulb@249 | 233 | def _get_fields(self, encoding=None): |
paulb@224 | 234 | encoding = encoding or self.get_content_type().charset or self.default_charset |
paulb@73 | 235 | fields = {} |
paulb@167 | 236 | for field_name, field_values in self.trans.request().fields().items(): |
paulb@167 | 237 | if type(field_values) == type([]): |
paulb@167 | 238 | fields[field_name] = [] |
paulb@198 | 239 | for field_str in field_values: |
paulb@198 | 240 | fields[field_name].append(get_body_field(field_str, encoding)) |
paulb@73 | 241 | else: |
paulb@198 | 242 | fields[field_name] = [get_body_field(field_values, encoding)] |
paulb@73 | 243 | return fields |
paulb@1 | 244 | |
paulb@249 | 245 | def get_fields(self, encoding=None): |
paulb@249 | 246 | |
paulb@249 | 247 | """ |
paulb@249 | 248 | Extracts fields (or request parameters) from both the path specified in |
paulb@249 | 249 | the transaction as well as the message body. The optional 'encoding' |
paulb@249 | 250 | parameter specifies the character encoding of the message body for cases |
paulb@249 | 251 | where no such information is available, but where the default encoding |
paulb@249 | 252 | is to be overridden. |
paulb@249 | 253 | |
paulb@249 | 254 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@249 | 255 | single value is associated with any given field name). Each value is |
paulb@249 | 256 | either a Unicode object (representing a simple form field, for example) |
paulb@249 | 257 | or a plain string (representing a file upload form field, for example). |
paulb@249 | 258 | |
paulb@249 | 259 | Where a given field name is used in both the path and message body to |
paulb@249 | 260 | specify values, the values from both sources will be combined into a |
paulb@249 | 261 | single list associated with that field name. |
paulb@249 | 262 | """ |
paulb@249 | 263 | |
paulb@249 | 264 | return self._get_fields(encoding) |
paulb@249 | 265 | |
paulb@45 | 266 | def get_user(self): |
paulb@1 | 267 | |
paulb@1 | 268 | """ |
paulb@186 | 269 | Extracts user information from the transaction. |
paulb@108 | 270 | |
paulb@108 | 271 | Returns a username as a string or None if no user is defined. |
paulb@1 | 272 | """ |
paulb@1 | 273 | |
paulb@49 | 274 | # NOTE: Webware relies entirely on a CGI-style environment where the |
paulb@49 | 275 | # NOTE: actual headers are not available. Therefore, the Web server must |
paulb@49 | 276 | # NOTE: itself be set up to provide user support. |
paulb@49 | 277 | |
paulb@128 | 278 | if self.user is not None: |
paulb@128 | 279 | return self.user |
paulb@128 | 280 | |
paulb@49 | 281 | try: |
paulb@49 | 282 | return self.trans.request().remoteUser() |
paulb@49 | 283 | except KeyError, exc: |
paulb@49 | 284 | return None |
paulb@1 | 285 | |
paulb@115 | 286 | def get_cookies(self): |
paulb@115 | 287 | |
paulb@115 | 288 | """ |
paulb@186 | 289 | Obtains cookie information from the request. |
paulb@115 | 290 | |
paulb@115 | 291 | Returns a dictionary mapping cookie names to cookie objects. |
paulb@115 | 292 | """ |
paulb@115 | 293 | |
paulb@258 | 294 | return self.process_cookies(self.trans.request().cookies(), using_strings=1) |
paulb@115 | 295 | |
paulb@115 | 296 | def get_cookie(self, cookie_name): |
paulb@115 | 297 | |
paulb@115 | 298 | """ |
paulb@186 | 299 | Obtains cookie information from the request. |
paulb@115 | 300 | |
paulb@115 | 301 | Returns a cookie object for the given 'cookie_name' or None if no such |
paulb@115 | 302 | cookie exists. |
paulb@115 | 303 | """ |
paulb@115 | 304 | |
paulb@115 | 305 | try: |
paulb@258 | 306 | value = self.trans.request().cookie(self.encode_cookie_value(cookie_name)) |
paulb@258 | 307 | return Cookie(cookie_name, self.decode_cookie_value(value)) |
paulb@115 | 308 | except KeyError: |
paulb@115 | 309 | return None |
paulb@115 | 310 | |
paulb@1 | 311 | # Response-related methods. |
paulb@1 | 312 | |
paulb@1 | 313 | def get_response_stream(self): |
paulb@1 | 314 | |
paulb@1 | 315 | """ |
paulb@186 | 316 | Returns the response stream for the transaction. |
paulb@1 | 317 | """ |
paulb@1 | 318 | |
paulb@224 | 319 | # Unicode can upset this operation. Using either the specified charset |
paulb@224 | 320 | # or a default encoding. |
paulb@167 | 321 | |
paulb@252 | 322 | encoding = self.get_response_stream_encoding() |
paulb@252 | 323 | return ConvertingStream(self.trans.response(), encoding) |
paulb@252 | 324 | |
paulb@252 | 325 | def get_response_stream_encoding(self): |
paulb@252 | 326 | |
paulb@252 | 327 | """ |
paulb@252 | 328 | Returns the response stream encoding. |
paulb@252 | 329 | """ |
paulb@252 | 330 | |
paulb@167 | 331 | if self.content_type: |
paulb@224 | 332 | encoding = self.content_type.charset |
paulb@252 | 333 | else: |
paulb@252 | 334 | encoding = None |
paulb@252 | 335 | return encoding or self.default_charset |
paulb@1 | 336 | |
paulb@42 | 337 | def get_response_code(self): |
paulb@42 | 338 | |
paulb@42 | 339 | """ |
paulb@42 | 340 | Get the response code associated with the transaction. If no response |
paulb@42 | 341 | code is defined, None is returned. |
paulb@42 | 342 | """ |
paulb@42 | 343 | |
paulb@42 | 344 | # NOTE: Webware treats the response code as just another header. |
paulb@42 | 345 | |
paulb@42 | 346 | status = self.trans.response().header("Status", None) |
paulb@42 | 347 | try: |
paulb@42 | 348 | if status is not None: |
paulb@42 | 349 | return int(status) |
paulb@42 | 350 | else: |
paulb@42 | 351 | return None |
paulb@42 | 352 | except ValueError: |
paulb@42 | 353 | return None |
paulb@42 | 354 | |
paulb@42 | 355 | def set_response_code(self, response_code): |
paulb@42 | 356 | |
paulb@42 | 357 | """ |
paulb@42 | 358 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@42 | 359 | specification. |
paulb@42 | 360 | """ |
paulb@42 | 361 | |
paulb@42 | 362 | self.trans.response().setStatus(response_code) |
paulb@42 | 363 | |
paulb@67 | 364 | def set_header_value(self, header, value): |
paulb@49 | 365 | |
paulb@49 | 366 | """ |
paulb@49 | 367 | Set the HTTP 'header' with the given 'value'. |
paulb@49 | 368 | """ |
paulb@49 | 369 | |
paulb@49 | 370 | self.trans.response().setHeader(self.format_header_value(header), self.format_header_value(value)) |
paulb@49 | 371 | |
paulb@1 | 372 | def set_content_type(self, content_type): |
paulb@1 | 373 | |
paulb@1 | 374 | """ |
paulb@186 | 375 | Sets the 'content_type' for the response. |
paulb@1 | 376 | """ |
paulb@1 | 377 | |
paulb@167 | 378 | # Remember the content type for encoding purposes later. |
paulb@167 | 379 | |
paulb@167 | 380 | self.content_type = content_type |
paulb@224 | 381 | return self.trans.response().setHeader("Content-Type", str(content_type)) |
paulb@1 | 382 | |
paulb@115 | 383 | # Higher level response-related methods. |
paulb@115 | 384 | |
paulb@115 | 385 | def set_cookie(self, cookie): |
paulb@115 | 386 | |
paulb@115 | 387 | """ |
paulb@186 | 388 | Stores the given 'cookie' object in the response. |
paulb@115 | 389 | """ |
paulb@115 | 390 | |
paulb@258 | 391 | self.set_cookie_value(cookie.name, cookie.value) |
paulb@258 | 392 | #self.trans.response().addCookie(cookie) |
paulb@115 | 393 | |
paulb@115 | 394 | def set_cookie_value(self, name, value, path=None, expires=None): |
paulb@115 | 395 | |
paulb@115 | 396 | """ |
paulb@186 | 397 | Stores a cookie with the given 'name' and 'value' in the response. |
paulb@115 | 398 | |
paulb@115 | 399 | The optional 'path' is a string which specifies the scope of the cookie, |
paulb@115 | 400 | and the optional 'expires' parameter is a value compatible with the |
paulb@115 | 401 | time.time function, and indicates the expiry date/time of the cookie. |
paulb@115 | 402 | """ |
paulb@115 | 403 | |
paulb@258 | 404 | self.trans.response().setCookie(self.encode_cookie_value(name), |
paulb@258 | 405 | self.encode_cookie_value(value), path, expires) |
paulb@115 | 406 | |
paulb@115 | 407 | def delete_cookie(self, cookie_name): |
paulb@115 | 408 | |
paulb@115 | 409 | """ |
paulb@186 | 410 | Adds to the response a request that the cookie with the given |
paulb@186 | 411 | 'cookie_name' be deleted/discarded by the client. |
paulb@115 | 412 | """ |
paulb@115 | 413 | |
paulb@258 | 414 | self.trans.response().delCookie(self.encode_cookie_value(cookie_name)) |
paulb@115 | 415 | |
paulb@183 | 416 | # Session-related methods. |
paulb@183 | 417 | |
paulb@183 | 418 | def get_session(self, create=1): |
paulb@183 | 419 | |
paulb@183 | 420 | """ |
paulb@186 | 421 | Gets a session corresponding to an identifier supplied in the |
paulb@186 | 422 | transaction. |
paulb@183 | 423 | |
paulb@183 | 424 | If no session has yet been established according to information |
paulb@183 | 425 | provided in the transaction then the optional 'create' parameter |
paulb@183 | 426 | determines whether a new session will be established. |
paulb@183 | 427 | |
paulb@183 | 428 | Where no session has been established and where 'create' is set to 0 |
paulb@183 | 429 | then None is returned. In all other cases, a session object is created |
paulb@183 | 430 | (where appropriate) and returned. |
paulb@183 | 431 | """ |
paulb@183 | 432 | |
paulb@280 | 433 | # NOTE: create and hasSession() not used. |
paulb@183 | 434 | |
paulb@280 | 435 | return Session(self.trans.session()) |
paulb@183 | 436 | |
paulb@183 | 437 | def expire_session(self): |
paulb@183 | 438 | |
paulb@183 | 439 | """ |
paulb@186 | 440 | Expires any session established according to information provided in the |
paulb@186 | 441 | transaction. |
paulb@183 | 442 | """ |
paulb@183 | 443 | |
paulb@183 | 444 | self.trans.request().setSessionExpired(1) |
paulb@183 | 445 | |
paulb@183 | 446 | class Session: |
paulb@183 | 447 | |
paulb@183 | 448 | "A more dictionary-like session object than the one Webware provides." |
paulb@183 | 449 | |
paulb@183 | 450 | def __init__(self, session): |
paulb@183 | 451 | self.session = session |
paulb@183 | 452 | |
paulb@183 | 453 | def items(self): |
paulb@183 | 454 | return self.session.values().items() |
paulb@183 | 455 | |
paulb@183 | 456 | def __getattr__(self, name): |
paulb@183 | 457 | return getattr(self.__dict__["session"], name) |
paulb@183 | 458 | |
paulb@280 | 459 | def __delitem__(self, name): |
paulb@280 | 460 | del self.session[name] |
paulb@280 | 461 | |
paulb@280 | 462 | def __setitem__(self, name, value): |
paulb@280 | 463 | self.session[name] = value |
paulb@280 | 464 | |
paulb@280 | 465 | def __getitem__(self, name): |
paulb@280 | 466 | return self.session[name] |
paulb@280 | 467 | |
paulb@1 | 468 | # vim: tabstop=4 expandtab shiftwidth=4 |