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