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 # Higher level request-related methods. 106 107 def get_fields(self): 108 109 """ 110 A framework-specific method which extracts the form fields from the 111 transaction. 112 """ 113 114 return FieldStorage(self.trans, keep_blank_values=1) 115 116 def get_user(self): 117 118 """ 119 A framework-specific method which extracts user information from the 120 transaction. 121 """ 122 123 return self.trans.user 124 125 # Response-related methods. 126 127 def get_response_stream(self): 128 129 """ 130 A framework-specific method which returns the response stream for 131 the transaction. 132 """ 133 134 return self.trans 135 136 def get_response_code(self): 137 138 """ 139 Get the response code associated with the transaction. If no response 140 code is defined, None is returned. 141 """ 142 143 return self.response_code 144 145 def set_response_code(self, response_code): 146 147 """ 148 Set the 'response_code' using a numeric constant defined in the HTTP 149 specification. 150 """ 151 152 self.response_code = response_code 153 154 def set_header_value(self, header, value): 155 156 """ 157 Set the HTTP 'header' with the given 'value'. 158 """ 159 160 self.trans.headers_out[self.format_header_value(header)] = self.format_header_value(value) 161 162 def set_content_type(self, content_type): 163 164 """ 165 A framework-specific method which sets the 'content_type' for the 166 response. 167 """ 168 169 self.trans.content_type = self.format_content_type(content_type) 170 171 # vim: tabstop=4 expandtab shiftwidth=4