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 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 # NOTE: No attempt is made to deduce the "path info". 132 133 return self.trans.path 134 135 # Higher level request-related methods. 136 137 def get_fields(self): 138 139 """ 140 A framework-specific method which extracts the form fields from the 141 transaction. 142 """ 143 144 return FieldStorage(self.get_request_stream(), keep_blank_values=1) 145 146 def get_user(self): 147 148 """ 149 A framework-specific method which extracts user information from the 150 transaction. 151 """ 152 153 auth_header = self.get_headers().get("Authorization") 154 if auth_header: 155 return UserInfo(auth_header).username 156 else: 157 return None 158 159 # Response-related methods. 160 161 def get_response_stream(self): 162 163 """ 164 A framework-specific method which returns the response stream for 165 the transaction. 166 """ 167 168 # Return a stream which is later emptied into the real stream. 169 170 return self.content 171 172 def get_response_code(self): 173 174 """ 175 Get the response code associated with the transaction. If no response 176 code is defined, None is returned. 177 """ 178 179 return self.response_code 180 181 def set_response_code(self, response_code): 182 183 """ 184 Set the 'response_code' using a numeric constant defined in the HTTP 185 specification. 186 """ 187 188 self.response_code = response_code 189 190 def set_header(self, header, value): 191 192 """ 193 Set the HTTP 'header' with the given 'value'. 194 """ 195 196 # The header is not written out immediately due to the buffering in use. 197 198 self.headers[header] = value 199 200 def set_content_type(self, content_type): 201 202 """ 203 A framework-specific method which sets the 'content_type' for the 204 response. 205 """ 206 207 # The content type has to be written as a header, before actual content, 208 # but after the response line. This means that some kind of buffering is 209 # required. Hence, we don't write the header out immediately. 210 211 self.content_type = content_type 212 213 # vim: tabstop=4 expandtab shiftwidth=4