1 #!/usr/bin/env python 2 3 """ 4 mod_python classes. 5 """ 6 7 import Generic 8 from mod_python.util import 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 The returned object should employ the cgi.FieldStorage interface. 124 125 The mod_python.util.FieldStorage class should get fields from the path 126 in all situations, although it may then redefine such fields if 127 identically-named fields are then specified in the body of a POST 128 transaction. 129 """ 130 131 return self.get_fields() 132 133 def get_fields_from_body(self): 134 135 """ 136 A framework-specific method which extracts the form fields from the 137 message body in the transaction. 138 139 The returned object should employ the cgi.FieldStorage interface. 140 141 The mod_python.util.FieldStorage class may augment the fields from the 142 body with fields found in the path. 143 """ 144 145 return self.get_fields() 146 147 def get_fields(self): 148 149 """ 150 A framework-specific method which extracts the form fields from the 151 transaction. Typically, the origin of the form fields will be affected 152 by the method specified in the transaction. 153 154 The returned object should employ the cgi.FieldStorage interface. 155 """ 156 157 return FieldStorage(self.trans, keep_blank_values=1) 158 159 def get_user(self): 160 161 """ 162 A framework-specific method which extracts user information from the 163 transaction. 164 """ 165 166 return self.trans.user 167 168 # Response-related methods. 169 170 def get_response_stream(self): 171 172 """ 173 A framework-specific method which returns the response stream for 174 the transaction. 175 """ 176 177 return self.trans 178 179 def get_response_code(self): 180 181 """ 182 Get the response code associated with the transaction. If no response 183 code is defined, None is returned. 184 """ 185 186 return self.response_code 187 188 def set_response_code(self, response_code): 189 190 """ 191 Set the 'response_code' using a numeric constant defined in the HTTP 192 specification. 193 """ 194 195 self.response_code = response_code 196 197 def set_header_value(self, header, value): 198 199 """ 200 Set the HTTP 'header' with the given 'value'. 201 """ 202 203 self.trans.headers_out[self.format_header_value(header)] = self.format_header_value(value) 204 205 def set_content_type(self, content_type): 206 207 """ 208 A framework-specific method which sets the 'content_type' for the 209 response. 210 """ 211 212 self.trans.content_type = self.format_content_type(content_type) 213 214 # vim: tabstop=4 expandtab shiftwidth=4