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