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@1 | 33 | A framework-specific method which returns the request stream for |
paulb@1 | 34 | the transaction. |
paulb@1 | 35 | """ |
paulb@1 | 36 | |
paulb@85 | 37 | request = self.trans.request() |
paulb@85 | 38 | try: |
paulb@85 | 39 | stream = request.rawInput(rewind=1) |
paulb@85 | 40 | if stream is None: |
paulb@85 | 41 | return StringIO.StringIO("") |
paulb@85 | 42 | |
paulb@85 | 43 | # NOTE: Dubious catch-all situation, but it is difficult to control |
paulb@85 | 44 | # NOTE: cases where Webware's internals themselves fail. |
paulb@85 | 45 | |
paulb@85 | 46 | except: |
paulb@1 | 47 | return StringIO.StringIO("") |
paulb@1 | 48 | |
paulb@1 | 49 | return stream |
paulb@1 | 50 | |
paulb@1 | 51 | def get_request_method(self): |
paulb@1 | 52 | |
paulb@1 | 53 | """ |
paulb@1 | 54 | A framework-specific method which gets the request method. |
paulb@1 | 55 | """ |
paulb@1 | 56 | |
paulb@1 | 57 | return self.trans.request().method() |
paulb@1 | 58 | |
paulb@1 | 59 | def get_headers(self): |
paulb@1 | 60 | |
paulb@1 | 61 | """ |
paulb@108 | 62 | A framework-specific method which returns all request headers as a |
paulb@108 | 63 | dictionary-like object mapping header names to values. |
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@65 | 77 | A framework-specific method which returns a list of all request header |
paulb@65 | 78 | values associated with the given 'key'. Note that according to RFC 2616, |
paulb@65 | 79 | 'key' is treated as a 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@1 | 94 | A framework-specific method which gets the content type specified on the |
paulb@1 | 95 | request, along with the 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@1 | 112 | A framework-specific method which extracts language information from |
paulb@1 | 113 | the transaction. |
paulb@1 | 114 | NOTE: Requires enhancements to HTTPRequest. |
paulb@1 | 115 | """ |
paulb@1 | 116 | |
paulb@1 | 117 | return self.trans.request().contentLanguages() |
paulb@1 | 118 | |
paulb@5 | 119 | def get_path(self): |
paulb@5 | 120 | |
paulb@5 | 121 | """ |
paulb@5 | 122 | A framework-specific method which gets the entire path from the request. |
paulb@5 | 123 | """ |
paulb@5 | 124 | |
paulb@14 | 125 | return self.trans.request().uri() |
paulb@5 | 126 | |
paulb@162 | 127 | def get_path_without_query(self): |
paulb@162 | 128 | |
paulb@162 | 129 | """ |
paulb@162 | 130 | A framework-specific method which gets the entire path from the request |
paulb@162 | 131 | minus the query string. |
paulb@162 | 132 | """ |
paulb@162 | 133 | |
paulb@162 | 134 | return self.get_path().split("?")[0] |
paulb@162 | 135 | |
paulb@1 | 136 | def get_path_info(self): |
paulb@1 | 137 | |
paulb@1 | 138 | """ |
paulb@1 | 139 | A framework-specific method which gets the "path info" (the part of the |
paulb@1 | 140 | URL after the resource name handling the current request) from the |
paulb@1 | 141 | request. |
paulb@1 | 142 | """ |
paulb@1 | 143 | |
paulb@91 | 144 | return self.trans.request().extraURLPath() |
paulb@1 | 145 | |
paulb@69 | 146 | def get_query_string(self): |
paulb@69 | 147 | |
paulb@69 | 148 | """ |
paulb@69 | 149 | A framework-specific method which gets the query string from the path in |
paulb@69 | 150 | the request. |
paulb@69 | 151 | """ |
paulb@69 | 152 | |
paulb@69 | 153 | return self.trans.request().queryString() |
paulb@69 | 154 | |
paulb@1 | 155 | # Higher level request-related methods. |
paulb@1 | 156 | |
paulb@69 | 157 | def get_fields_from_path(self): |
paulb@69 | 158 | |
paulb@69 | 159 | """ |
paulb@69 | 160 | A framework-specific method which extracts the form fields from the |
paulb@69 | 161 | path specified in the transaction. The underlying framework may refuse |
paulb@69 | 162 | to supply fields from the path if handling a POST transaction. |
paulb@69 | 163 | |
paulb@71 | 164 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@71 | 165 | single value is associated with any given field name). |
paulb@69 | 166 | """ |
paulb@69 | 167 | |
paulb@71 | 168 | return parse_qs(self.get_query_string(), keep_blank_values=1) |
paulb@69 | 169 | |
paulb@167 | 170 | def get_fields_from_body(self, encoding=None): |
paulb@69 | 171 | |
paulb@69 | 172 | """ |
paulb@69 | 173 | A framework-specific method which extracts the form fields from the |
paulb@167 | 174 | message body in the transaction. The optional 'encoding' parameter |
paulb@167 | 175 | specifies the character encoding of the message body for cases where no |
paulb@167 | 176 | such information is available, but where the default encoding is to be |
paulb@167 | 177 | overridden. |
paulb@69 | 178 | |
paulb@73 | 179 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@73 | 180 | single value is associated with any given field name). |
paulb@69 | 181 | """ |
paulb@69 | 182 | |
paulb@167 | 183 | encoding = self.get_content_type().charset or encoding or "iso-8859-1" |
paulb@167 | 184 | |
paulb@73 | 185 | # Fix the non-list results. |
paulb@1 | 186 | |
paulb@73 | 187 | fields = {} |
paulb@167 | 188 | for field_name, field_values in self.trans.request().fields().items(): |
paulb@167 | 189 | if type(field_values) == type([]): |
paulb@167 | 190 | fields[field_name] = [] |
paulb@167 | 191 | for field_value in field_values: |
paulb@167 | 192 | fields[field_name].append(unicode(field_value, encoding)) |
paulb@73 | 193 | else: |
paulb@167 | 194 | fields[field_name] = [unicode(field_values, encoding)] |
paulb@73 | 195 | return fields |
paulb@1 | 196 | |
paulb@45 | 197 | def get_user(self): |
paulb@1 | 198 | |
paulb@1 | 199 | """ |
paulb@45 | 200 | A framework-specific method which extracts user information from the |
paulb@45 | 201 | transaction. |
paulb@108 | 202 | |
paulb@108 | 203 | Returns a username as a string or None if no user is defined. |
paulb@1 | 204 | """ |
paulb@1 | 205 | |
paulb@49 | 206 | # NOTE: Webware relies entirely on a CGI-style environment where the |
paulb@49 | 207 | # NOTE: actual headers are not available. Therefore, the Web server must |
paulb@49 | 208 | # NOTE: itself be set up to provide user support. |
paulb@49 | 209 | |
paulb@128 | 210 | if self.user is not None: |
paulb@128 | 211 | return self.user |
paulb@128 | 212 | |
paulb@49 | 213 | try: |
paulb@49 | 214 | return self.trans.request().remoteUser() |
paulb@49 | 215 | except KeyError, exc: |
paulb@49 | 216 | return None |
paulb@1 | 217 | |
paulb@115 | 218 | def get_cookies(self): |
paulb@115 | 219 | |
paulb@115 | 220 | """ |
paulb@115 | 221 | A framework-specific method which obtains cookie information from the |
paulb@115 | 222 | request. |
paulb@115 | 223 | |
paulb@115 | 224 | Returns a dictionary mapping cookie names to cookie objects. |
paulb@115 | 225 | """ |
paulb@115 | 226 | |
paulb@115 | 227 | cookies = {} |
paulb@115 | 228 | for name, value in self.trans.request().cookies().items(): |
paulb@115 | 229 | cookies[name] = Cookie(name, value) |
paulb@115 | 230 | return cookies |
paulb@115 | 231 | |
paulb@115 | 232 | def get_cookie(self, cookie_name): |
paulb@115 | 233 | |
paulb@115 | 234 | """ |
paulb@115 | 235 | A framework-specific method which obtains cookie information from the |
paulb@115 | 236 | request. |
paulb@115 | 237 | |
paulb@115 | 238 | Returns a cookie object for the given 'cookie_name' or None if no such |
paulb@115 | 239 | cookie exists. |
paulb@115 | 240 | """ |
paulb@115 | 241 | |
paulb@115 | 242 | try: |
paulb@115 | 243 | return Cookie(cookie_name, self.trans.request().cookie(cookie_name)) |
paulb@115 | 244 | except KeyError: |
paulb@115 | 245 | return None |
paulb@115 | 246 | |
paulb@1 | 247 | # Response-related methods. |
paulb@1 | 248 | |
paulb@1 | 249 | def get_response_stream(self): |
paulb@1 | 250 | |
paulb@1 | 251 | """ |
paulb@1 | 252 | A framework-specific method which returns the response stream for |
paulb@1 | 253 | 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@1 | 302 | A framework-specific method which sets the 'content_type' for the |
paulb@1 | 303 | response. |
paulb@1 | 304 | """ |
paulb@1 | 305 | |
paulb@167 | 306 | # Remember the content type for encoding purposes later. |
paulb@167 | 307 | |
paulb@167 | 308 | self.content_type = content_type |
paulb@5 | 309 | return self.trans.response().setHeader("Content-Type", self.format_content_type(content_type)) |
paulb@1 | 310 | |
paulb@115 | 311 | # Higher level response-related methods. |
paulb@115 | 312 | |
paulb@115 | 313 | def set_cookie(self, cookie): |
paulb@115 | 314 | |
paulb@115 | 315 | """ |
paulb@115 | 316 | A framework-specific method which stores the given 'cookie' object in |
paulb@115 | 317 | the response. |
paulb@115 | 318 | """ |
paulb@115 | 319 | |
paulb@115 | 320 | self.trans.response().addCookie(cookie) |
paulb@115 | 321 | |
paulb@115 | 322 | def set_cookie_value(self, name, value, path=None, expires=None): |
paulb@115 | 323 | |
paulb@115 | 324 | """ |
paulb@115 | 325 | A framework-specific method which stores a cookie with the given 'name' |
paulb@115 | 326 | and 'value' in the response. |
paulb@115 | 327 | |
paulb@115 | 328 | The optional 'path' is a string which specifies the scope of the cookie, |
paulb@115 | 329 | and the optional 'expires' parameter is a value compatible with the |
paulb@115 | 330 | time.time function, and indicates the expiry date/time of the cookie. |
paulb@115 | 331 | """ |
paulb@115 | 332 | |
paulb@115 | 333 | self.trans.response().setCookie(name, value, path, expires) |
paulb@115 | 334 | |
paulb@115 | 335 | def delete_cookie(self, cookie_name): |
paulb@115 | 336 | |
paulb@115 | 337 | """ |
paulb@115 | 338 | A framework-specific method which adds to the response a request that |
paulb@115 | 339 | the cookie with the given 'cookie_name' be deleted/discarded by the |
paulb@115 | 340 | client. |
paulb@115 | 341 | """ |
paulb@115 | 342 | |
paulb@115 | 343 | self.trans.response().delCookie(cookie_name) |
paulb@115 | 344 | |
paulb@183 | 345 | # Session-related methods. |
paulb@183 | 346 | |
paulb@183 | 347 | def get_session(self, create=1): |
paulb@183 | 348 | |
paulb@183 | 349 | """ |
paulb@183 | 350 | A framework-specific method which returns a session corresponding to |
paulb@183 | 351 | an identifier supplied in the transaction. |
paulb@183 | 352 | |
paulb@183 | 353 | If no session has yet been established according to information |
paulb@183 | 354 | provided in the transaction then the optional 'create' parameter |
paulb@183 | 355 | determines whether a new session will be established. |
paulb@183 | 356 | |
paulb@183 | 357 | Where no session has been established and where 'create' is set to 0 |
paulb@183 | 358 | then None is returned. In all other cases, a session object is created |
paulb@183 | 359 | (where appropriate) and returned. |
paulb@183 | 360 | """ |
paulb@183 | 361 | |
paulb@183 | 362 | # NOTE: Should really use Webware's hasSession method. |
paulb@183 | 363 | |
paulb@183 | 364 | session = self.trans.session() |
paulb@183 | 365 | return Session(session) |
paulb@183 | 366 | |
paulb@183 | 367 | def expire_session(self): |
paulb@183 | 368 | |
paulb@183 | 369 | """ |
paulb@183 | 370 | A framework-specific method which expires any session established |
paulb@183 | 371 | according to information provided in the transaction. |
paulb@183 | 372 | """ |
paulb@183 | 373 | |
paulb@183 | 374 | self.trans.request().setSessionExpired(1) |
paulb@183 | 375 | |
paulb@128 | 376 | # Application-specific methods. |
paulb@128 | 377 | |
paulb@128 | 378 | def set_user(self, username): |
paulb@128 | 379 | |
paulb@128 | 380 | """ |
paulb@128 | 381 | An application-specific method which sets the user information with |
paulb@128 | 382 | 'username' in the transaction. This affects subsequent calls to |
paulb@128 | 383 | 'get_user'. |
paulb@128 | 384 | """ |
paulb@128 | 385 | |
paulb@128 | 386 | self.user = username |
paulb@128 | 387 | |
paulb@183 | 388 | class Session: |
paulb@183 | 389 | |
paulb@183 | 390 | "A more dictionary-like session object than the one Webware provides." |
paulb@183 | 391 | |
paulb@183 | 392 | def __init__(self, session): |
paulb@183 | 393 | self.session = session |
paulb@183 | 394 | |
paulb@183 | 395 | def items(self): |
paulb@183 | 396 | return self.session.values().items() |
paulb@183 | 397 | |
paulb@183 | 398 | def __getattr__(self, name): |
paulb@183 | 399 | return getattr(self.__dict__["session"], name) |
paulb@183 | 400 | |
paulb@1 | 401 | # vim: tabstop=4 expandtab shiftwidth=4 |