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 the request headers. 45 NOTE: Experimental, since framework support varies somewhat. 46 """ 47 48 # NOTE: Accessing attribute of transaction object. 49 50 return self.trans.received_headers 51 52 def get_content_type(self): 53 54 """ 55 A framework-specific method which gets the content type specified on the 56 request, along with the charset employed. 57 """ 58 59 return self.parse_content_type(self.trans.getHeader("Content-Type")) 60 61 def get_content_charsets(self): 62 63 """ 64 Returns the character set preferences. 65 """ 66 67 return self.parse_content_preferences(self.trans.getHeader("Accept-Language")) 68 69 def get_content_languages(self): 70 71 """ 72 A framework-specific method which extracts language information from 73 the transaction. 74 """ 75 76 return self.parse_content_preferences(self.trans.getHeader("Accept-Charset")) 77 78 def get_path(self): 79 80 """ 81 A framework-specific method which gets the entire path from the request. 82 """ 83 84 # NOTE: The path and "path info" are mostly equivalent for Twisted. 85 86 return self.get_path_info() 87 88 def get_path_info(self): 89 90 """ 91 A framework-specific method which gets the "path info" (the part of the 92 URL after the resource name handling the current request) from the 93 request. 94 """ 95 96 return "/%s" % "/".join(self.trans.postpath) 97 98 # Higher level request-related methods. 99 100 def get_fields(self): 101 102 """ 103 A framework-specific method which extracts the form fields from the 104 transaction. 105 """ 106 107 # NOTE: Discard multiple field values. 108 109 return dict([(key, value[0]) for (key, value) in self.trans.args.items()]) 110 111 def get_user(self): 112 113 """ 114 A framework-specific method which extracts user information from the 115 transaction. 116 """ 117 118 # NOTE: Twisted makes headers lower case, for some reason. 119 120 auth_header = self.get_headers().get("authorization") 121 if auth_header: 122 return UserInfo(auth_header).username 123 else: 124 return None 125 126 # Response-related methods. 127 128 def get_response_stream(self): 129 130 """ 131 A framework-specific method which returns the response stream for 132 the transaction. 133 """ 134 135 return self.trans 136 137 def get_response_code(self): 138 139 """ 140 Get the response code associated with the transaction. If no response 141 code is defined, None is returned. 142 """ 143 144 # NOTE: Accessing the request attribute directly. 145 146 return self.trans.code 147 148 def set_response_code(self, response_code): 149 150 """ 151 Set the 'response_code' using a numeric constant defined in the HTTP 152 specification. 153 """ 154 155 self.trans.setResponseCode(response_code) 156 157 def set_header(self, header, value): 158 159 """ 160 Set the HTTP 'header' with the given 'value'. 161 """ 162 163 self.trans.setHeader(self.format_header_value(header), self.format_header_value(value)) 164 165 def set_content_type(self, content_type): 166 167 """ 168 A framework-specific method which sets the 'content_type' for the 169 response. 170 """ 171 172 self.trans.setHeader("Content-Type", self.format_content_type(content_type)) 173 174 # vim: tabstop=4 expandtab shiftwidth=4