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 or "" 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 Returns a dictionary mapping field names to lists of values (even if a 136 single value is associated with any given field name). 137 138 The mod_python.util.FieldStorage class may augment the fields from the 139 body with fields found in the path. 140 """ 141 142 storage = FieldStorage(self.trans, keep_blank_values=1) 143 144 # Traverse the storage, finding each field value. 145 146 fields = {} 147 for field in storage.list: 148 if not fields.has_key(field.name): 149 fields[field.name] = [] 150 fields[field.name].append(field.value) 151 return fields 152 153 def get_user(self): 154 155 """ 156 A framework-specific method which extracts user information from the 157 transaction. 158 """ 159 160 return self.trans.user 161 162 # Response-related methods. 163 164 def get_response_stream(self): 165 166 """ 167 A framework-specific method which returns the response stream for 168 the transaction. 169 """ 170 171 return self.trans 172 173 def get_response_code(self): 174 175 """ 176 Get the response code associated with the transaction. If no response 177 code is defined, None is returned. 178 """ 179 180 return self.response_code 181 182 def set_response_code(self, response_code): 183 184 """ 185 Set the 'response_code' using a numeric constant defined in the HTTP 186 specification. 187 """ 188 189 self.response_code = response_code 190 191 def set_header_value(self, header, value): 192 193 """ 194 Set the HTTP 'header' with the given 'value'. 195 """ 196 197 self.trans.headers_out[self.format_header_value(header)] = self.format_header_value(value) 198 199 def set_content_type(self, content_type): 200 201 """ 202 A framework-specific method which sets the 'content_type' for the 203 response. 204 """ 205 206 self.trans.content_type = self.format_content_type(content_type) 207 208 # vim: tabstop=4 expandtab shiftwidth=4