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@198 | 11 | from Helpers.Request import Cookie, get_body_fields |
paulb@188 | 12 | from Helpers.Response import ConvertingStream |
paulb@193 | 13 | from ZPublisher.HTTPRequest import FileUpload |
paulb@187 | 14 | |
paulb@187 | 15 | class Transaction(Generic.Transaction): |
paulb@187 | 16 | |
paulb@187 | 17 | """ |
paulb@187 | 18 | Zope transaction interface. |
paulb@187 | 19 | """ |
paulb@187 | 20 | |
paulb@187 | 21 | def __init__(self, request): |
paulb@187 | 22 | |
paulb@187 | 23 | """ |
paulb@187 | 24 | Initialise the transaction with the Zope 'request' object. |
paulb@187 | 25 | """ |
paulb@187 | 26 | |
paulb@187 | 27 | self.request = request |
paulb@187 | 28 | self.response = request.RESPONSE |
paulb@188 | 29 | |
paulb@188 | 30 | # Attributes which may be changed later. |
paulb@188 | 31 | |
paulb@188 | 32 | self.content_type = None |
paulb@187 | 33 | self.user = None |
paulb@187 | 34 | |
paulb@187 | 35 | # Request-related methods. |
paulb@187 | 36 | |
paulb@187 | 37 | def get_request_stream(self): |
paulb@187 | 38 | |
paulb@187 | 39 | """ |
paulb@187 | 40 | Returns the request stream for the transaction. |
paulb@187 | 41 | """ |
paulb@187 | 42 | |
paulb@187 | 43 | # NOTE: Possibly not safe. |
paulb@187 | 44 | |
paulb@187 | 45 | return self.request.stdin |
paulb@187 | 46 | |
paulb@187 | 47 | def get_request_method(self): |
paulb@187 | 48 | |
paulb@187 | 49 | """ |
paulb@187 | 50 | Returns the request method. |
paulb@187 | 51 | """ |
paulb@187 | 52 | |
paulb@187 | 53 | return self.request.environ.get("REQUEST_METHOD") |
paulb@187 | 54 | |
paulb@187 | 55 | def get_headers(self): |
paulb@187 | 56 | |
paulb@187 | 57 | """ |
paulb@187 | 58 | Returns all request headers as a dictionary-like object mapping header |
paulb@187 | 59 | names to values. |
paulb@187 | 60 | """ |
paulb@187 | 61 | |
paulb@187 | 62 | return Environment.get_headers(self.request.environ) |
paulb@187 | 63 | |
paulb@187 | 64 | def get_header_values(self, key): |
paulb@187 | 65 | |
paulb@187 | 66 | """ |
paulb@187 | 67 | Returns a list of all request header values associated with the given |
paulb@187 | 68 | 'key'. Note that according to RFC 2616, 'key' is treated as a |
paulb@187 | 69 | case-insensitive string. |
paulb@187 | 70 | """ |
paulb@187 | 71 | |
paulb@187 | 72 | return self.convert_to_list(self.get_headers().get(key)) |
paulb@187 | 73 | |
paulb@187 | 74 | def get_content_type(self): |
paulb@187 | 75 | |
paulb@187 | 76 | """ |
paulb@187 | 77 | Returns the content type specified on the request, along with the |
paulb@187 | 78 | charset employed. |
paulb@187 | 79 | """ |
paulb@187 | 80 | |
paulb@187 | 81 | return self.parse_content_type(self.request.environ.get("CONTENT_TYPE")) |
paulb@187 | 82 | |
paulb@187 | 83 | def get_content_charsets(self): |
paulb@187 | 84 | |
paulb@187 | 85 | """ |
paulb@187 | 86 | Returns the character set preferences. |
paulb@187 | 87 | |
paulb@187 | 88 | NOTE: Not decently supported. |
paulb@187 | 89 | """ |
paulb@187 | 90 | |
paulb@187 | 91 | return self.parse_content_preferences(None) |
paulb@187 | 92 | |
paulb@187 | 93 | def get_content_languages(self): |
paulb@187 | 94 | |
paulb@187 | 95 | """ |
paulb@187 | 96 | Returns extracted language information from the transaction. |
paulb@187 | 97 | |
paulb@187 | 98 | NOTE: Not decently supported. |
paulb@187 | 99 | """ |
paulb@187 | 100 | |
paulb@187 | 101 | return self.parse_content_preferences(None) |
paulb@187 | 102 | |
paulb@187 | 103 | def get_path(self): |
paulb@187 | 104 | |
paulb@187 | 105 | """ |
paulb@187 | 106 | Returns the entire path from the request. |
paulb@187 | 107 | """ |
paulb@187 | 108 | |
paulb@187 | 109 | # NOTE: Based on WebStack.CGI.get_path. |
paulb@187 | 110 | |
paulb@187 | 111 | path = self.get_path_without_query() |
paulb@187 | 112 | qs = self.get_query_string() |
paulb@187 | 113 | if qs: |
paulb@187 | 114 | path += "?" |
paulb@187 | 115 | path += qs |
paulb@187 | 116 | return path |
paulb@187 | 117 | |
paulb@187 | 118 | def get_path_without_query(self): |
paulb@187 | 119 | |
paulb@187 | 120 | """ |
paulb@187 | 121 | Returns the entire path from the request minus the query string. |
paulb@187 | 122 | """ |
paulb@187 | 123 | |
paulb@187 | 124 | # NOTE: Based on WebStack.CGI.get_path. |
paulb@187 | 125 | |
paulb@187 | 126 | path = self.request.environ.get("SCRIPT_NAME") or "" |
paulb@187 | 127 | if self.request.environ.has_key("PATH_INFO"): |
paulb@187 | 128 | path += self.request.environ["PATH_INFO"] |
paulb@187 | 129 | return path |
paulb@187 | 130 | |
paulb@187 | 131 | def get_path_info(self): |
paulb@187 | 132 | |
paulb@187 | 133 | """ |
paulb@187 | 134 | Returns the "path info" (the part of the URL after the resource name |
paulb@187 | 135 | handling the current request) from the request. |
paulb@187 | 136 | """ |
paulb@187 | 137 | |
paulb@187 | 138 | return self.request.environ.get("PATH_INFO") or "" |
paulb@187 | 139 | |
paulb@187 | 140 | def get_query_string(self): |
paulb@187 | 141 | |
paulb@187 | 142 | """ |
paulb@187 | 143 | Returns the query string from the path in the request. |
paulb@187 | 144 | """ |
paulb@187 | 145 | |
paulb@187 | 146 | return self.request.environ.get("QUERY_STRING") or "" |
paulb@187 | 147 | |
paulb@187 | 148 | # Higher level request-related methods. |
paulb@187 | 149 | |
paulb@187 | 150 | def get_fields_from_path(self): |
paulb@187 | 151 | |
paulb@187 | 152 | """ |
paulb@187 | 153 | Extracts the form fields from the path specified in the transaction. The |
paulb@187 | 154 | underlying framework may refuse to supply fields from the path if |
paulb@187 | 155 | handling a POST transaction. |
paulb@187 | 156 | |
paulb@187 | 157 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@187 | 158 | single value is associated with any given field name). |
paulb@187 | 159 | """ |
paulb@187 | 160 | |
paulb@187 | 161 | fields = {} |
paulb@187 | 162 | for key, value in self.request.form.items(): |
paulb@187 | 163 | if type(value) == type([]): |
paulb@187 | 164 | fields[key] = value |
paulb@187 | 165 | else: |
paulb@187 | 166 | fields[key] = [value] |
paulb@187 | 167 | return fields |
paulb@187 | 168 | |
paulb@187 | 169 | def get_fields_from_body(self, encoding=None): |
paulb@187 | 170 | |
paulb@187 | 171 | """ |
paulb@187 | 172 | Extracts the form fields from the message body in the transaction. The |
paulb@187 | 173 | optional 'encoding' parameter specifies the character encoding of the |
paulb@187 | 174 | message body for cases where no such information is available, but where |
paulb@187 | 175 | the default encoding is to be overridden. |
paulb@187 | 176 | |
paulb@187 | 177 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@193 | 178 | single value is associated with any given field name). Each value is |
paulb@193 | 179 | either a Unicode object (representing a simple form field, for example) |
paulb@198 | 180 | or a plain string (representing a file upload form field, for example). |
paulb@187 | 181 | |
paulb@187 | 182 | NOTE: Zope doesn't distinguish between path and body fields. |
paulb@187 | 183 | """ |
paulb@187 | 184 | |
paulb@187 | 185 | # NOTE: Conversion to Unicode may be inappropriate. |
paulb@187 | 186 | |
paulb@187 | 187 | encoding = self.get_content_type().charset or encoding or "iso-8859-1" |
paulb@198 | 188 | fields = get_body_fields(self.get_fields_from_path().items(), encoding) |
paulb@187 | 189 | return fields |
paulb@187 | 190 | |
paulb@187 | 191 | def get_user(self): |
paulb@187 | 192 | |
paulb@187 | 193 | """ |
paulb@187 | 194 | Extracts user information from the transaction. |
paulb@187 | 195 | |
paulb@187 | 196 | Returns a username as a string or None if no user is defined. |
paulb@187 | 197 | """ |
paulb@187 | 198 | |
paulb@187 | 199 | if self.user is not None: |
paulb@187 | 200 | return self.user |
paulb@187 | 201 | else: |
paulb@187 | 202 | return self.request.environ.get("REMOTE_USER") |
paulb@187 | 203 | |
paulb@187 | 204 | def get_cookies(self): |
paulb@187 | 205 | |
paulb@187 | 206 | """ |
paulb@187 | 207 | Obtains cookie information from the request. |
paulb@187 | 208 | |
paulb@187 | 209 | Returns a dictionary mapping cookie names to cookie objects. |
paulb@187 | 210 | """ |
paulb@187 | 211 | |
paulb@187 | 212 | cookies = {} |
paulb@187 | 213 | for name, value in self.request.cookies.items(): |
paulb@187 | 214 | cookies[name] = Cookie(name, value) |
paulb@187 | 215 | return cookies |
paulb@187 | 216 | |
paulb@187 | 217 | def get_cookie(self, cookie_name): |
paulb@187 | 218 | |
paulb@187 | 219 | """ |
paulb@187 | 220 | Obtains cookie information from the request. |
paulb@187 | 221 | |
paulb@187 | 222 | Returns a cookie object for the given 'cookie_name' or None if no such |
paulb@187 | 223 | cookie exists. |
paulb@187 | 224 | """ |
paulb@187 | 225 | |
paulb@187 | 226 | return Cookie(cookie_name, self.request.cookies.get(cookie_name)) |
paulb@187 | 227 | |
paulb@187 | 228 | # Response-related methods. |
paulb@187 | 229 | |
paulb@187 | 230 | def get_response_stream(self): |
paulb@187 | 231 | |
paulb@187 | 232 | """ |
paulb@187 | 233 | Returns the response stream for the transaction. |
paulb@187 | 234 | """ |
paulb@187 | 235 | |
paulb@188 | 236 | # Unicode can upset this operation. Using either the specified charset, |
paulb@188 | 237 | # the same charset as that used in the request, or a default encoding. |
paulb@187 | 238 | |
paulb@188 | 239 | encoding = self.get_content_type().charset or "utf-8" |
paulb@188 | 240 | if self.content_type: |
paulb@188 | 241 | encoding = self.content_type.charset or encoding |
paulb@188 | 242 | return ConvertingStream(self.response, encoding) |
paulb@187 | 243 | |
paulb@187 | 244 | def get_response_code(self): |
paulb@187 | 245 | |
paulb@187 | 246 | """ |
paulb@187 | 247 | Get the response code associated with the transaction. If no response |
paulb@187 | 248 | code is defined, None is returned. |
paulb@187 | 249 | """ |
paulb@187 | 250 | |
paulb@187 | 251 | return self.response.status |
paulb@187 | 252 | |
paulb@187 | 253 | def set_response_code(self, response_code): |
paulb@187 | 254 | |
paulb@187 | 255 | """ |
paulb@187 | 256 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@187 | 257 | specification. |
paulb@187 | 258 | """ |
paulb@187 | 259 | |
paulb@187 | 260 | self.response.setStatus(response_code) |
paulb@187 | 261 | |
paulb@187 | 262 | def set_header_value(self, header, value): |
paulb@187 | 263 | |
paulb@187 | 264 | """ |
paulb@187 | 265 | Set the HTTP 'header' with the given 'value'. |
paulb@187 | 266 | """ |
paulb@187 | 267 | |
paulb@187 | 268 | self.response.setHeader(header, value) |
paulb@187 | 269 | |
paulb@187 | 270 | def set_content_type(self, content_type): |
paulb@187 | 271 | |
paulb@187 | 272 | """ |
paulb@187 | 273 | Sets the 'content_type' for the response. |
paulb@187 | 274 | """ |
paulb@187 | 275 | |
paulb@188 | 276 | self.content_type = content_type |
paulb@187 | 277 | self.response.setHeader("Content-Type", self.format_content_type(content_type)) |
paulb@187 | 278 | |
paulb@187 | 279 | # Higher level response-related methods. |
paulb@187 | 280 | |
paulb@187 | 281 | def set_cookie(self, cookie): |
paulb@187 | 282 | |
paulb@187 | 283 | """ |
paulb@187 | 284 | Stores the given 'cookie' object in the response. |
paulb@187 | 285 | """ |
paulb@187 | 286 | |
paulb@187 | 287 | self.set_cookie_value(cookie.name, cookie.value) |
paulb@187 | 288 | |
paulb@187 | 289 | def set_cookie_value(self, name, value, path=None, expires=None): |
paulb@187 | 290 | |
paulb@187 | 291 | """ |
paulb@187 | 292 | Stores a cookie with the given 'name' and 'value' in the response. |
paulb@187 | 293 | |
paulb@187 | 294 | The optional 'path' is a string which specifies the scope of the cookie, |
paulb@187 | 295 | and the optional 'expires' parameter is a value compatible with the |
paulb@187 | 296 | time.time function, and indicates the expiry date/time of the cookie. |
paulb@187 | 297 | """ |
paulb@187 | 298 | |
paulb@187 | 299 | self.response.setCookie(name, value) |
paulb@187 | 300 | |
paulb@187 | 301 | def delete_cookie(self, cookie_name): |
paulb@187 | 302 | |
paulb@187 | 303 | """ |
paulb@187 | 304 | Adds to the response a request that the cookie with the given |
paulb@187 | 305 | 'cookie_name' be deleted/discarded by the client. |
paulb@187 | 306 | """ |
paulb@187 | 307 | |
paulb@187 | 308 | self.response.expireCookie(cookie_name) |
paulb@187 | 309 | |
paulb@187 | 310 | # Session-related methods. |
paulb@187 | 311 | |
paulb@187 | 312 | def get_session(self, create=1): |
paulb@187 | 313 | |
paulb@187 | 314 | """ |
paulb@187 | 315 | Gets a session corresponding to an identifier supplied in the |
paulb@187 | 316 | transaction. |
paulb@187 | 317 | |
paulb@187 | 318 | If no session has yet been established according to information |
paulb@187 | 319 | provided in the transaction then the optional 'create' parameter |
paulb@187 | 320 | determines whether a new session will be established. |
paulb@187 | 321 | |
paulb@187 | 322 | Where no session has been established and where 'create' is set to 0 |
paulb@187 | 323 | then None is returned. In all other cases, a session object is created |
paulb@187 | 324 | (where appropriate) and returned. |
paulb@187 | 325 | """ |
paulb@187 | 326 | |
paulb@187 | 327 | raise NotImplementedError, "get_session" |
paulb@187 | 328 | |
paulb@187 | 329 | def expire_session(self): |
paulb@187 | 330 | |
paulb@187 | 331 | """ |
paulb@187 | 332 | Expires any session established according to information provided in the |
paulb@187 | 333 | transaction. |
paulb@187 | 334 | """ |
paulb@187 | 335 | |
paulb@187 | 336 | raise NotImplementedError, "expire_session" |
paulb@187 | 337 | |
paulb@187 | 338 | # Application-specific methods. |
paulb@187 | 339 | |
paulb@187 | 340 | def set_user(self, username): |
paulb@187 | 341 | |
paulb@187 | 342 | """ |
paulb@187 | 343 | An application-specific method which sets the user information with |
paulb@187 | 344 | 'username' in the transaction. This affects subsequent calls to |
paulb@187 | 345 | 'get_user'. |
paulb@187 | 346 | """ |
paulb@187 | 347 | |
paulb@187 | 348 | self.user = username |
paulb@187 | 349 | |
paulb@187 | 350 | # vim: tabstop=4 expandtab shiftwidth=4 |