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@1 | 11 | |
paulb@1 | 12 | class Transaction(Generic.Transaction): |
paulb@1 | 13 | |
paulb@1 | 14 | """ |
paulb@1 | 15 | Webware transaction interface. |
paulb@1 | 16 | """ |
paulb@1 | 17 | |
paulb@1 | 18 | def __init__(self, trans): |
paulb@1 | 19 | |
paulb@1 | 20 | "Initialise the transaction using the Webware transaction 'trans'." |
paulb@1 | 21 | |
paulb@1 | 22 | self.trans = trans |
paulb@1 | 23 | |
paulb@1 | 24 | # Request-related methods. |
paulb@1 | 25 | |
paulb@1 | 26 | def get_request_stream(self): |
paulb@1 | 27 | |
paulb@1 | 28 | """ |
paulb@1 | 29 | A framework-specific method which returns the request stream for |
paulb@1 | 30 | the transaction. |
paulb@1 | 31 | """ |
paulb@1 | 32 | |
paulb@85 | 33 | request = self.trans.request() |
paulb@85 | 34 | try: |
paulb@85 | 35 | stream = request.rawInput(rewind=1) |
paulb@85 | 36 | if stream is None: |
paulb@85 | 37 | return StringIO.StringIO("") |
paulb@85 | 38 | |
paulb@85 | 39 | # NOTE: Dubious catch-all situation, but it is difficult to control |
paulb@85 | 40 | # NOTE: cases where Webware's internals themselves fail. |
paulb@85 | 41 | |
paulb@85 | 42 | except: |
paulb@1 | 43 | return StringIO.StringIO("") |
paulb@1 | 44 | |
paulb@1 | 45 | return stream |
paulb@1 | 46 | |
paulb@1 | 47 | def get_request_method(self): |
paulb@1 | 48 | |
paulb@1 | 49 | """ |
paulb@1 | 50 | A framework-specific method which gets the request method. |
paulb@1 | 51 | """ |
paulb@1 | 52 | |
paulb@1 | 53 | return self.trans.request().method() |
paulb@1 | 54 | |
paulb@1 | 55 | def get_headers(self): |
paulb@1 | 56 | |
paulb@1 | 57 | """ |
paulb@108 | 58 | A framework-specific method which returns all request headers as a |
paulb@108 | 59 | dictionary-like object mapping header names to values. |
paulb@91 | 60 | NOTE: If duplicate header names are permitted, then this interface will |
paulb@91 | 61 | NOTE: need to change. |
paulb@1 | 62 | """ |
paulb@1 | 63 | |
paulb@91 | 64 | # Use the Webware environment and some assumptions about variable names. |
paulb@91 | 65 | # NOTE: Using lower case for the header names. |
paulb@1 | 66 | |
paulb@91 | 67 | env = self.trans.request().environ() |
paulb@108 | 68 | return Environment.get_headers(env) |
paulb@1 | 69 | |
paulb@65 | 70 | def get_header_values(self, key): |
paulb@65 | 71 | |
paulb@65 | 72 | """ |
paulb@65 | 73 | A framework-specific method which returns a list of all request header |
paulb@65 | 74 | values associated with the given 'key'. Note that according to RFC 2616, |
paulb@65 | 75 | 'key' is treated as a case-insensitive string. |
paulb@65 | 76 | """ |
paulb@65 | 77 | |
paulb@91 | 78 | # Use the Webware environment and some assumptions about variable names. |
paulb@65 | 79 | |
paulb@91 | 80 | env = self.trans.request().environ() |
paulb@91 | 81 | cgi_key = "HTTP_" + key.replace("-", "_").upper() |
paulb@91 | 82 | if env.has_key(cgi_key): |
paulb@91 | 83 | return [env[cgi_key]] |
paulb@91 | 84 | else: |
paulb@91 | 85 | return [] |
paulb@65 | 86 | |
paulb@1 | 87 | def get_content_type(self): |
paulb@1 | 88 | |
paulb@1 | 89 | """ |
paulb@1 | 90 | A framework-specific method which gets the content type specified on the |
paulb@1 | 91 | request, along with the charset employed. |
paulb@1 | 92 | """ |
paulb@1 | 93 | |
paulb@1 | 94 | return self.parse_content_type(self.trans.request().contentType()) |
paulb@1 | 95 | |
paulb@1 | 96 | def get_content_charsets(self): |
paulb@1 | 97 | |
paulb@1 | 98 | """ |
paulb@1 | 99 | Returns the character set preferences. |
paulb@1 | 100 | NOTE: Requires enhancements to HTTPRequest. |
paulb@1 | 101 | """ |
paulb@1 | 102 | |
paulb@1 | 103 | return self.trans.request().contentCharsets() |
paulb@1 | 104 | |
paulb@1 | 105 | def get_content_languages(self): |
paulb@1 | 106 | |
paulb@1 | 107 | """ |
paulb@1 | 108 | A framework-specific method which extracts language information from |
paulb@1 | 109 | the transaction. |
paulb@1 | 110 | NOTE: Requires enhancements to HTTPRequest. |
paulb@1 | 111 | """ |
paulb@1 | 112 | |
paulb@1 | 113 | return self.trans.request().contentLanguages() |
paulb@1 | 114 | |
paulb@5 | 115 | def get_path(self): |
paulb@5 | 116 | |
paulb@5 | 117 | """ |
paulb@5 | 118 | A framework-specific method which gets the entire path from the request. |
paulb@5 | 119 | """ |
paulb@5 | 120 | |
paulb@14 | 121 | return self.trans.request().uri() |
paulb@5 | 122 | |
paulb@1 | 123 | def get_path_info(self): |
paulb@1 | 124 | |
paulb@1 | 125 | """ |
paulb@1 | 126 | A framework-specific method which gets the "path info" (the part of the |
paulb@1 | 127 | URL after the resource name handling the current request) from the |
paulb@1 | 128 | request. |
paulb@1 | 129 | """ |
paulb@1 | 130 | |
paulb@91 | 131 | return self.trans.request().extraURLPath() |
paulb@1 | 132 | |
paulb@69 | 133 | def get_query_string(self): |
paulb@69 | 134 | |
paulb@69 | 135 | """ |
paulb@69 | 136 | A framework-specific method which gets the query string from the path in |
paulb@69 | 137 | the request. |
paulb@69 | 138 | """ |
paulb@69 | 139 | |
paulb@69 | 140 | return self.trans.request().queryString() |
paulb@69 | 141 | |
paulb@1 | 142 | # Higher level request-related methods. |
paulb@1 | 143 | |
paulb@69 | 144 | def get_fields_from_path(self): |
paulb@69 | 145 | |
paulb@69 | 146 | """ |
paulb@69 | 147 | A framework-specific method which extracts the form fields from the |
paulb@69 | 148 | path specified in the transaction. The underlying framework may refuse |
paulb@69 | 149 | to supply fields from the path if handling a POST transaction. |
paulb@69 | 150 | |
paulb@71 | 151 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@71 | 152 | single value is associated with any given field name). |
paulb@69 | 153 | """ |
paulb@69 | 154 | |
paulb@71 | 155 | return parse_qs(self.get_query_string(), keep_blank_values=1) |
paulb@69 | 156 | |
paulb@69 | 157 | def get_fields_from_body(self): |
paulb@69 | 158 | |
paulb@69 | 159 | """ |
paulb@69 | 160 | A framework-specific method which extracts the form fields from the |
paulb@69 | 161 | message body in the transaction. |
paulb@69 | 162 | |
paulb@73 | 163 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@73 | 164 | single value is associated with any given field name). |
paulb@69 | 165 | """ |
paulb@69 | 166 | |
paulb@73 | 167 | # Fix the non-list results. |
paulb@1 | 168 | |
paulb@73 | 169 | fields = {} |
paulb@73 | 170 | for field_name, field_value in self.trans.request().fields().items(): |
paulb@73 | 171 | if type(field_value) == type([]): |
paulb@73 | 172 | fields[field_name] = field_value |
paulb@73 | 173 | else: |
paulb@73 | 174 | fields[field_name] = [field_value] |
paulb@73 | 175 | return fields |
paulb@1 | 176 | |
paulb@45 | 177 | def get_user(self): |
paulb@1 | 178 | |
paulb@1 | 179 | """ |
paulb@45 | 180 | A framework-specific method which extracts user information from the |
paulb@45 | 181 | transaction. |
paulb@108 | 182 | |
paulb@108 | 183 | Returns a username as a string or None if no user is defined. |
paulb@1 | 184 | """ |
paulb@1 | 185 | |
paulb@49 | 186 | # NOTE: Webware relies entirely on a CGI-style environment where the |
paulb@49 | 187 | # NOTE: actual headers are not available. Therefore, the Web server must |
paulb@49 | 188 | # NOTE: itself be set up to provide user support. |
paulb@49 | 189 | |
paulb@49 | 190 | try: |
paulb@49 | 191 | return self.trans.request().remoteUser() |
paulb@49 | 192 | except KeyError, exc: |
paulb@49 | 193 | return None |
paulb@1 | 194 | |
paulb@1 | 195 | # Response-related methods. |
paulb@1 | 196 | |
paulb@1 | 197 | def get_response_stream(self): |
paulb@1 | 198 | |
paulb@1 | 199 | """ |
paulb@1 | 200 | A framework-specific method which returns the response stream for |
paulb@1 | 201 | the transaction. |
paulb@1 | 202 | """ |
paulb@1 | 203 | |
paulb@1 | 204 | return self.trans.response() |
paulb@1 | 205 | |
paulb@42 | 206 | def get_response_code(self): |
paulb@42 | 207 | |
paulb@42 | 208 | """ |
paulb@42 | 209 | Get the response code associated with the transaction. If no response |
paulb@42 | 210 | code is defined, None is returned. |
paulb@42 | 211 | """ |
paulb@42 | 212 | |
paulb@42 | 213 | # NOTE: Webware treats the response code as just another header. |
paulb@42 | 214 | |
paulb@42 | 215 | status = self.trans.response().header("Status", None) |
paulb@42 | 216 | try: |
paulb@42 | 217 | if status is not None: |
paulb@42 | 218 | return int(status) |
paulb@42 | 219 | else: |
paulb@42 | 220 | return None |
paulb@42 | 221 | except ValueError: |
paulb@42 | 222 | return None |
paulb@42 | 223 | |
paulb@42 | 224 | def set_response_code(self, response_code): |
paulb@42 | 225 | |
paulb@42 | 226 | """ |
paulb@42 | 227 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@42 | 228 | specification. |
paulb@42 | 229 | """ |
paulb@42 | 230 | |
paulb@42 | 231 | self.trans.response().setStatus(response_code) |
paulb@42 | 232 | |
paulb@67 | 233 | def set_header_value(self, header, value): |
paulb@49 | 234 | |
paulb@49 | 235 | """ |
paulb@49 | 236 | Set the HTTP 'header' with the given 'value'. |
paulb@49 | 237 | """ |
paulb@49 | 238 | |
paulb@49 | 239 | self.trans.response().setHeader(self.format_header_value(header), self.format_header_value(value)) |
paulb@49 | 240 | |
paulb@1 | 241 | def set_content_type(self, content_type): |
paulb@1 | 242 | |
paulb@1 | 243 | """ |
paulb@1 | 244 | A framework-specific method which sets the 'content_type' for the |
paulb@1 | 245 | response. |
paulb@1 | 246 | """ |
paulb@1 | 247 | |
paulb@5 | 248 | return self.trans.response().setHeader("Content-Type", self.format_content_type(content_type)) |
paulb@1 | 249 | |
paulb@1 | 250 | # vim: tabstop=4 expandtab shiftwidth=4 |