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