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