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