paulb@28 | 1 | #!/usr/bin/env python |
paulb@28 | 2 | |
paulb@28 | 3 | """ |
paulb@28 | 4 | BaseHTTPRequestHandler classes. |
paulb@28 | 5 | """ |
paulb@28 | 6 | |
paulb@28 | 7 | import Generic |
paulb@45 | 8 | from Helpers.Request import MessageBodyStream |
paulb@49 | 9 | from Helpers.Auth import UserInfo |
paulb@28 | 10 | from cgi import FieldStorage |
paulb@28 | 11 | from StringIO import StringIO |
paulb@28 | 12 | |
paulb@28 | 13 | class Transaction(Generic.Transaction): |
paulb@28 | 14 | |
paulb@28 | 15 | """ |
paulb@28 | 16 | BaseHTTPRequestHandler transaction interface. |
paulb@28 | 17 | """ |
paulb@28 | 18 | |
paulb@28 | 19 | def __init__(self, trans): |
paulb@28 | 20 | |
paulb@28 | 21 | """ |
paulb@28 | 22 | Initialise the transaction using the BaseHTTPRequestHandler instance |
paulb@28 | 23 | 'trans'. |
paulb@28 | 24 | """ |
paulb@28 | 25 | |
paulb@28 | 26 | self.trans = trans |
paulb@28 | 27 | |
paulb@28 | 28 | # Other attributes of interest in instances of this class. |
paulb@28 | 29 | |
paulb@28 | 30 | self.content_type = None |
paulb@45 | 31 | self.response_code = 200 |
paulb@28 | 32 | self.content = StringIO() |
paulb@49 | 33 | self.headers = {} |
paulb@28 | 34 | |
paulb@28 | 35 | def commit(self): |
paulb@28 | 36 | |
paulb@28 | 37 | """ |
paulb@28 | 38 | A special method, synchronising the transaction with framework-specific |
paulb@28 | 39 | objects. |
paulb@28 | 40 | """ |
paulb@28 | 41 | |
paulb@28 | 42 | self.trans.send_response(self.response_code) |
paulb@45 | 43 | if self.content_type is not None: |
paulb@45 | 44 | self.trans.send_header("Content-Type", self.format_content_type(self.content_type)) |
paulb@49 | 45 | for header, value in self.headers.items(): |
paulb@49 | 46 | self.trans.send_header(self.format_header_value(header), self.format_header_value(value)) |
paulb@28 | 47 | self.trans.end_headers() |
paulb@28 | 48 | self.content.seek(0) |
paulb@28 | 49 | self.trans.wfile.write(self.content.read()) |
paulb@28 | 50 | |
paulb@28 | 51 | # Request-related methods. |
paulb@28 | 52 | |
paulb@28 | 53 | def get_request_stream(self): |
paulb@28 | 54 | |
paulb@28 | 55 | """ |
paulb@28 | 56 | A framework-specific method which returns the request stream for |
paulb@28 | 57 | the transaction. |
paulb@28 | 58 | """ |
paulb@28 | 59 | |
paulb@45 | 60 | return MessageBodyStream(self.trans.rfile, self.get_headers()) |
paulb@28 | 61 | |
paulb@28 | 62 | def get_request_method(self): |
paulb@28 | 63 | |
paulb@28 | 64 | """ |
paulb@28 | 65 | A framework-specific method which gets the request method. |
paulb@28 | 66 | """ |
paulb@28 | 67 | |
paulb@28 | 68 | return self.trans.command |
paulb@28 | 69 | |
paulb@28 | 70 | def get_headers(self): |
paulb@28 | 71 | |
paulb@28 | 72 | """ |
paulb@28 | 73 | A framework-specific method which returns the request headers. |
paulb@28 | 74 | NOTE: Experimental, since framework support varies somewhat. |
paulb@28 | 75 | """ |
paulb@28 | 76 | |
paulb@28 | 77 | return self.trans.headers |
paulb@28 | 78 | |
paulb@28 | 79 | def get_content_type(self): |
paulb@28 | 80 | |
paulb@28 | 81 | """ |
paulb@28 | 82 | A framework-specific method which gets the content type specified on the |
paulb@28 | 83 | request, along with the charset employed. |
paulb@28 | 84 | """ |
paulb@28 | 85 | |
paulb@28 | 86 | return self.parse_content_type(self.trans.headers.get("Content-type") or |
paulb@28 | 87 | self.trans.headers.get("Content-Type")) |
paulb@28 | 88 | |
paulb@28 | 89 | def get_content_charsets(self): |
paulb@28 | 90 | |
paulb@28 | 91 | """ |
paulb@28 | 92 | Returns the character set preferences. |
paulb@28 | 93 | """ |
paulb@28 | 94 | |
paulb@28 | 95 | return self.parse_content_preferences(self.trans.headers["Accept-Charset"]) |
paulb@28 | 96 | |
paulb@28 | 97 | def get_content_languages(self): |
paulb@28 | 98 | |
paulb@28 | 99 | """ |
paulb@28 | 100 | A framework-specific method which extracts language information from |
paulb@28 | 101 | the transaction. |
paulb@28 | 102 | """ |
paulb@28 | 103 | |
paulb@28 | 104 | return self.parse_content_preferences(self.trans.headers["Accept-Language"]) |
paulb@28 | 105 | |
paulb@28 | 106 | def get_path(self): |
paulb@28 | 107 | |
paulb@28 | 108 | """ |
paulb@28 | 109 | A framework-specific method which gets the entire path from the request. |
paulb@28 | 110 | """ |
paulb@28 | 111 | |
paulb@28 | 112 | return self.trans.path |
paulb@28 | 113 | |
paulb@28 | 114 | def get_path_info(self): |
paulb@28 | 115 | |
paulb@28 | 116 | """ |
paulb@28 | 117 | A framework-specific method which gets the "path info" (the part of the |
paulb@28 | 118 | URL after the resource name handling the current request) from the |
paulb@28 | 119 | request. |
paulb@28 | 120 | """ |
paulb@28 | 121 | |
paulb@28 | 122 | # NOTE: No attempt is made to deduce the "path info". |
paulb@28 | 123 | |
paulb@28 | 124 | return self.trans.path |
paulb@28 | 125 | |
paulb@28 | 126 | # Higher level request-related methods. |
paulb@28 | 127 | |
paulb@28 | 128 | def get_fields(self): |
paulb@28 | 129 | |
paulb@28 | 130 | """ |
paulb@28 | 131 | A framework-specific method which extracts the form fields from the |
paulb@28 | 132 | transaction. |
paulb@28 | 133 | """ |
paulb@28 | 134 | |
paulb@28 | 135 | return FieldStorage(self.trans.rfile, keep_blank_values=1) |
paulb@28 | 136 | |
paulb@45 | 137 | def get_user(self): |
paulb@28 | 138 | |
paulb@28 | 139 | """ |
paulb@45 | 140 | A framework-specific method which extracts user information from the |
paulb@45 | 141 | transaction. |
paulb@28 | 142 | """ |
paulb@28 | 143 | |
paulb@49 | 144 | auth_header = self.get_headers().get("Authorization") |
paulb@49 | 145 | if auth_header: |
paulb@49 | 146 | return UserInfo(auth_header).username |
paulb@49 | 147 | else: |
paulb@49 | 148 | return None |
paulb@28 | 149 | |
paulb@28 | 150 | # Response-related methods. |
paulb@28 | 151 | |
paulb@28 | 152 | def get_response_stream(self): |
paulb@28 | 153 | |
paulb@28 | 154 | """ |
paulb@28 | 155 | A framework-specific method which returns the response stream for |
paulb@28 | 156 | the transaction. |
paulb@28 | 157 | """ |
paulb@28 | 158 | |
paulb@28 | 159 | # Return a stream which is later emptied into the real stream. |
paulb@28 | 160 | |
paulb@28 | 161 | return self.content |
paulb@28 | 162 | |
paulb@42 | 163 | def get_response_code(self): |
paulb@42 | 164 | |
paulb@42 | 165 | """ |
paulb@42 | 166 | Get the response code associated with the transaction. If no response |
paulb@42 | 167 | code is defined, None is returned. |
paulb@42 | 168 | """ |
paulb@42 | 169 | |
paulb@42 | 170 | return self.response_code |
paulb@42 | 171 | |
paulb@42 | 172 | def set_response_code(self, response_code): |
paulb@42 | 173 | |
paulb@42 | 174 | """ |
paulb@42 | 175 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@42 | 176 | specification. |
paulb@42 | 177 | """ |
paulb@42 | 178 | |
paulb@42 | 179 | self.response_code = response_code |
paulb@42 | 180 | |
paulb@49 | 181 | def set_header(self, header, value): |
paulb@49 | 182 | |
paulb@49 | 183 | """ |
paulb@49 | 184 | Set the HTTP 'header' with the given 'value'. |
paulb@49 | 185 | """ |
paulb@49 | 186 | |
paulb@49 | 187 | # The header is not written out immediately due to the buffering in use. |
paulb@49 | 188 | |
paulb@49 | 189 | self.headers[header] = value |
paulb@49 | 190 | |
paulb@28 | 191 | def set_content_type(self, content_type): |
paulb@28 | 192 | |
paulb@28 | 193 | """ |
paulb@28 | 194 | A framework-specific method which sets the 'content_type' for the |
paulb@28 | 195 | response. |
paulb@28 | 196 | """ |
paulb@28 | 197 | |
paulb@28 | 198 | # The content type has to be written as a header, before actual content, |
paulb@28 | 199 | # but after the response line. This means that some kind of buffering is |
paulb@28 | 200 | # required. Hence, we don't write the header out immediately. |
paulb@28 | 201 | |
paulb@28 | 202 | self.content_type = content_type |
paulb@28 | 203 | |
paulb@28 | 204 | # vim: tabstop=4 expandtab shiftwidth=4 |