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