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