1 #!/usr/bin/env python 2 3 """ 4 mod_python classes. 5 """ 6 7 import Generic 8 from mod_python.util import parse_qs, FieldStorage 9 from mod_python import apache 10 11 class Transaction(Generic.Transaction): 12 13 """ 14 mod_python transaction interface. 15 """ 16 17 def __init__(self, trans): 18 19 "Initialise the transaction using the mod_python transaction 'trans'." 20 21 self.trans = trans 22 self.response_code = apache.OK 23 24 # Request-related methods. 25 26 def get_request_stream(self): 27 28 """ 29 A framework-specific method which returns the request stream for 30 the transaction. 31 """ 32 33 return self.trans 34 35 def get_request_method(self): 36 37 """ 38 A framework-specific method which gets the request method. 39 """ 40 41 return self.trans.method 42 43 def get_headers(self): 44 45 """ 46 A framework-specific method which returns all request headers. 47 """ 48 49 return self.trans.headers_in 50 51 def get_header_values(self, key): 52 53 """ 54 A framework-specific method which returns a list of all request header 55 values associated with the given 'key'. Note that according to RFC 2616, 56 'key' is treated as a case-insensitive string. 57 """ 58 59 return self.convert_to_list(self.trans.headers_in.get(key)) 60 61 def get_content_type(self): 62 63 """ 64 A framework-specific method which gets the content type specified on the 65 request, along with the charset employed. 66 """ 67 68 return self.parse_content_type(self.trans.content_type) 69 70 def get_content_charsets(self): 71 72 """ 73 Returns the character set preferences. 74 """ 75 76 return self.parse_content_preferences(self.trans.headers_in["Accept-Charset"]) 77 78 def get_content_languages(self): 79 80 """ 81 A framework-specific method which extracts language information from 82 the transaction. 83 """ 84 85 return self.parse_content_preferences(self.trans.headers_in["Accept-Language"]) 86 87 def get_path(self): 88 89 """ 90 A framework-specific method which gets the entire path from the request. 91 """ 92 93 return self.trans.uri 94 95 def get_path_info(self): 96 97 """ 98 A framework-specific method which gets the "path info" (the part of the 99 URL after the resource name handling the current request) from the 100 request. 101 """ 102 103 return self.trans.path_info 104 105 def get_query_string(self): 106 107 """ 108 A framework-specific method which gets the query string from the path in 109 the request. 110 """ 111 112 return self.trans.args 113 114 # Higher level request-related methods. 115 116 def get_fields_from_path(self): 117 118 """ 119 A framework-specific method which extracts the form fields from the 120 path specified in the transaction. The underlying framework may refuse 121 to supply fields from the path if handling a POST transaction. 122 123 Returns a dictionary mapping field names to lists of values (even if a 124 single value is associated with any given field name). 125 """ 126 127 return parse_qs(self.get_query_string(), 1) # keep_blank_values=1 128 129 def get_fields_from_body(self): 130 131 """ 132 A framework-specific method which extracts the form fields from the 133 message body in the transaction. 134 135 The returned object should employ the cgi.FieldStorage interface. 136 137 The mod_python.util.FieldStorage class may augment the fields from the 138 body with fields found in the path. 139 """ 140 141 return self.get_fields() 142 143 def get_fields(self): 144 145 """ 146 A framework-specific method which extracts the form fields from the 147 transaction. Typically, the origin of the form fields will be affected 148 by the method specified in the transaction. 149 150 The returned object should employ the cgi.FieldStorage interface. 151 """ 152 153 return FieldStorage(self.trans, keep_blank_values=1) 154 155 def get_user(self): 156 157 """ 158 A framework-specific method which extracts user information from the 159 transaction. 160 """ 161 162 return self.trans.user 163 164 # Response-related methods. 165 166 def get_response_stream(self): 167 168 """ 169 A framework-specific method which returns the response stream for 170 the transaction. 171 """ 172 173 return self.trans 174 175 def get_response_code(self): 176 177 """ 178 Get the response code associated with the transaction. If no response 179 code is defined, None is returned. 180 """ 181 182 return self.response_code 183 184 def set_response_code(self, response_code): 185 186 """ 187 Set the 'response_code' using a numeric constant defined in the HTTP 188 specification. 189 """ 190 191 self.response_code = response_code 192 193 def set_header_value(self, header, value): 194 195 """ 196 Set the HTTP 'header' with the given 'value'. 197 """ 198 199 self.trans.headers_out[self.format_header_value(header)] = self.format_header_value(value) 200 201 def set_content_type(self, content_type): 202 203 """ 204 A framework-specific method which sets the 'content_type' for the 205 response. 206 """ 207 208 self.trans.content_type = self.format_content_type(content_type) 209 210 # vim: tabstop=4 expandtab shiftwidth=4