1 #!/usr/bin/env python 2 3 """ 4 Twisted classes. 5 """ 6 7 import Generic 8 from Helpers.Auth import UserInfo 9 10 class Transaction(Generic.Transaction): 11 12 """ 13 Twisted transaction interface. 14 """ 15 16 def __init__(self, trans): 17 18 "Initialise the transaction using the Twisted transaction 'trans'." 19 20 self.trans = trans 21 22 # Request-related methods. 23 24 def get_request_stream(self): 25 26 """ 27 A framework-specific method which returns the request stream for 28 the transaction. 29 """ 30 31 return self.trans.content 32 33 def get_request_method(self): 34 35 """ 36 A framework-specific method which gets the request method. 37 """ 38 39 return self.trans.method 40 41 def get_headers(self): 42 43 """ 44 A framework-specific method which returns all request headers. 45 """ 46 47 return self.trans.received_headers 48 49 def get_header_values(self, key): 50 51 """ 52 A framework-specific method which returns a list of all request header 53 values associated with the given 'key'. Note that according to RFC 2616, 54 'key' is treated as a case-insensitive string. 55 """ 56 57 # Twisted does not convert the header key to lower case (which is the 58 # stored representation). 59 60 return self.convert_to_list(self.trans.received_headers.get(key.lower())) 61 62 def get_content_type(self): 63 64 """ 65 A framework-specific method which gets the content type specified on the 66 request, along with the charset employed. 67 """ 68 69 return self.parse_content_type(self.trans.getHeader("Content-Type")) 70 71 def get_content_charsets(self): 72 73 """ 74 Returns the character set preferences. 75 """ 76 77 return self.parse_content_preferences(self.trans.getHeader("Accept-Language")) 78 79 def get_content_languages(self): 80 81 """ 82 A framework-specific method which extracts language information from 83 the transaction. 84 """ 85 86 return self.parse_content_preferences(self.trans.getHeader("Accept-Charset")) 87 88 def get_path(self): 89 90 """ 91 A framework-specific method which gets the entire path from the request. 92 """ 93 94 # NOTE: The path and "path info" are mostly equivalent for Twisted. 95 96 return self.get_path_info() 97 98 def get_path_info(self): 99 100 """ 101 A framework-specific method which gets the "path info" (the part of the 102 URL after the resource name handling the current request) from the 103 request. 104 """ 105 106 return "/%s" % "/".join(self.trans.postpath) 107 108 # Higher level request-related methods. 109 110 def get_fields(self): 111 112 """ 113 A framework-specific method which extracts the form fields from the 114 transaction. 115 """ 116 117 # NOTE: Discard multiple field values. 118 119 return dict([(key, value[0]) for (key, value) in self.trans.args.items()]) 120 121 def get_user(self): 122 123 """ 124 A framework-specific method which extracts user information from the 125 transaction. 126 """ 127 128 # NOTE: Twisted makes headers lower case, for some reason. 129 130 auth_header = self.get_headers().get("authorization") 131 if auth_header: 132 return UserInfo(auth_header).username 133 else: 134 return None 135 136 # Response-related methods. 137 138 def get_response_stream(self): 139 140 """ 141 A framework-specific method which returns the response stream for 142 the transaction. 143 """ 144 145 return self.trans 146 147 def get_response_code(self): 148 149 """ 150 Get the response code associated with the transaction. If no response 151 code is defined, None is returned. 152 """ 153 154 # NOTE: Accessing the request attribute directly. 155 156 return self.trans.code 157 158 def set_response_code(self, response_code): 159 160 """ 161 Set the 'response_code' using a numeric constant defined in the HTTP 162 specification. 163 """ 164 165 self.trans.setResponseCode(response_code) 166 167 def set_header_value(self, header, value): 168 169 """ 170 Set the HTTP 'header' with the given 'value'. 171 """ 172 173 self.trans.setHeader(self.format_header_value(header), self.format_header_value(value)) 174 175 def set_content_type(self, content_type): 176 177 """ 178 A framework-specific method which sets the 'content_type' for the 179 response. 180 """ 181 182 self.trans.setHeader("Content-Type", self.format_content_type(content_type)) 183 184 # vim: tabstop=4 expandtab shiftwidth=4