1 #!/usr/bin/env python 2 3 """ 4 BaseHTTPRequestHandler classes. 5 """ 6 7 import Generic 8 from Helpers.Request import MessageBodyStream 9 from Helpers.Auth import UserInfo 10 from cgi import parse_qs, FieldStorage 11 from StringIO import StringIO 12 13 class Transaction(Generic.Transaction): 14 15 """ 16 BaseHTTPRequestHandler transaction interface. 17 """ 18 19 def __init__(self, trans): 20 21 """ 22 Initialise the transaction using the BaseHTTPRequestHandler instance 23 'trans'. 24 """ 25 26 self.trans = trans 27 28 # Other attributes of interest in instances of this class. 29 30 self.content_type = None 31 self.response_code = 200 32 self.content = StringIO() 33 self.headers = {} 34 35 def commit(self): 36 37 """ 38 A special method, synchronising the transaction with framework-specific 39 objects. 40 """ 41 42 self.trans.send_response(self.response_code) 43 if self.content_type is not None: 44 self.trans.send_header("Content-Type", self.format_content_type(self.content_type)) 45 for header, value in self.headers.items(): 46 self.trans.send_header(self.format_header_value(header), self.format_header_value(value)) 47 self.trans.end_headers() 48 self.content.seek(0) 49 self.trans.wfile.write(self.content.read()) 50 51 # Request-related methods. 52 53 def get_request_stream(self): 54 55 """ 56 A framework-specific method which returns the request stream for 57 the transaction. 58 """ 59 60 return MessageBodyStream(self.trans.rfile, self.get_headers()) 61 62 def get_request_method(self): 63 64 """ 65 A framework-specific method which gets the request method. 66 """ 67 68 return self.trans.command 69 70 def get_headers(self): 71 72 """ 73 A framework-specific method which returns all request headers. 74 """ 75 76 return self.trans.headers 77 78 def get_header_values(self, key): 79 80 """ 81 A framework-specific method which returns a list of all request header 82 values associated with the given 'key'. Note that according to RFC 2616, 83 'key' is treated as a case-insensitive string. 84 """ 85 86 return self.convert_to_list(self.trans.headers.get(key)) 87 88 def get_content_type(self): 89 90 """ 91 A framework-specific method which gets the content type specified on the 92 request, along with the charset employed. 93 """ 94 95 return self.parse_content_type(self.trans.headers.get("Content-type") or 96 self.trans.headers.get("Content-Type")) 97 98 def get_content_charsets(self): 99 100 """ 101 Returns the character set preferences. 102 """ 103 104 return self.parse_content_preferences(self.trans.headers["Accept-Charset"]) 105 106 def get_content_languages(self): 107 108 """ 109 A framework-specific method which extracts language information from 110 the transaction. 111 """ 112 113 return self.parse_content_preferences(self.trans.headers["Accept-Language"]) 114 115 def get_path(self): 116 117 """ 118 A framework-specific method which gets the entire path from the request. 119 """ 120 121 return self.trans.path 122 123 def get_path_info(self): 124 125 """ 126 A framework-specific method which gets the "path info" (the part of the 127 URL after the resource name handling the current request) from the 128 request. 129 """ 130 131 # Remove the query string from the end of the path. 132 133 return self.trans.path.split("?")[0] 134 135 def get_query_string(self): 136 137 """ 138 A framework-specific method which gets the query string from the path in 139 the request. 140 """ 141 142 t = self.trans.path.split("?") 143 if len(t) == 1: 144 return "" 145 else: 146 147 # NOTE: Overlook erroneous usage of "?" characters in the path. 148 149 return "?".join(t[1:]) 150 151 # Higher level request-related methods. 152 153 def get_fields_from_path(self): 154 155 """ 156 A framework-specific method which extracts the form fields from the 157 path specified in the transaction. The underlying framework may refuse 158 to supply fields from the path if handling a POST transaction. 159 160 Returns a dictionary mapping field names to lists of values (even if a 161 single value is associated with any given field name). 162 """ 163 164 return parse_qs(self.get_query_string(), keep_blank_values=1) 165 166 def get_fields_from_body(self): 167 168 """ 169 A framework-specific method which extracts the form fields from the 170 message body in the transaction. 171 172 Returns a dictionary mapping field names to lists of values (even if a 173 single value is associated with any given field name). 174 """ 175 176 storage = FieldStorage(fp=self.get_request_stream(), headers=self.get_headers(), 177 environ={"REQUEST_METHOD" : self.get_request_method()}, keep_blank_values=1) 178 179 # Traverse the storage, finding each field value. 180 181 fields = {} 182 for field_name in storage.keys(): 183 fields[field_name] = storage.getlist(field_name) 184 return fields 185 186 def get_user(self): 187 188 """ 189 A framework-specific method which extracts user information from the 190 transaction. 191 """ 192 193 auth_header = self.get_headers().get("Authorization") 194 if auth_header: 195 return UserInfo(auth_header).username 196 else: 197 return None 198 199 # Response-related methods. 200 201 def get_response_stream(self): 202 203 """ 204 A framework-specific method which returns the response stream for 205 the transaction. 206 """ 207 208 # Return a stream which is later emptied into the real stream. 209 210 return self.content 211 212 def get_response_code(self): 213 214 """ 215 Get the response code associated with the transaction. If no response 216 code is defined, None is returned. 217 """ 218 219 return self.response_code 220 221 def set_response_code(self, response_code): 222 223 """ 224 Set the 'response_code' using a numeric constant defined in the HTTP 225 specification. 226 """ 227 228 self.response_code = response_code 229 230 def set_header_value(self, header, value): 231 232 """ 233 Set the HTTP 'header' with the given 'value'. 234 """ 235 236 # The header is not written out immediately due to the buffering in use. 237 238 self.headers[header] = value 239 240 def set_content_type(self, content_type): 241 242 """ 243 A framework-specific method which sets the 'content_type' for the 244 response. 245 """ 246 247 # The content type has to be written as a header, before actual content, 248 # but after the response line. This means that some kind of buffering is 249 # required. Hence, we don't write the header out immediately. 250 251 self.content_type = content_type 252 253 # vim: tabstop=4 expandtab shiftwidth=4