24 # get the querystring |
27 # get the querystring |
25 self.arg_str = env.get('QUERY_STRING', '') |
28 self.arg_str = env.get('QUERY_STRING', '') |
26 |
29 |
27 # parse query args |
30 # parse query args |
28 self.arg_dict = cgi.parse_qs(self.arg_str, True) |
31 self.arg_dict = cgi.parse_qs(self.arg_str, True) |
|
32 |
|
33 def get_script_dir (self) : |
|
34 """ |
|
35 Returns the URL path to the requested script's directory with no trailing slash, i.e. |
|
36 |
|
37 / -> |
|
38 /foo.cgi -> |
|
39 /foo/bar.cgi -> /foo |
|
40 """ |
|
41 |
|
42 return os.path.dirname(self.env['SCRIPT_NAME']) |
|
43 |
|
44 def get_page_name (self) : |
|
45 """ |
|
46 Returns the requested page path with no leading slash, i.e. |
|
47 |
|
48 /foo.cgi -> |
|
49 /foo.cgi/ -> |
|
50 /foo.cgi/bar -> bar |
|
51 /foo.cgi/quux/ -> quux/ |
|
52 """ |
|
53 |
|
54 return os.path.normpath(self.env['PATH_INFO']).lstrip('/') |
29 |
55 |
30 class Response (object) : |
56 class Response (object) : |
31 """ |
57 """ |
32 HTTP Response with headers and data |
58 HTTP Response with headers and data |
33 """ |
59 """ |
89 class ErrorResponse (Response) : |
115 class ErrorResponse (Response) : |
90 """ |
116 """ |
91 A response with an error code / message |
117 A response with an error code / message |
92 """ |
118 """ |
93 |
119 |
94 def __init__ (self, status, message) : |
120 def __init__ (self, status, message, details=None) : |
95 """ |
121 """ |
96 Build a plain error message response with the given status/message |
122 Build a plain error message response with the given status/message |
|
123 |
|
124 @param status HTTP status code |
|
125 @param message short message to describe errors |
|
126 @param details optional details, plaintext |
97 """ |
127 """ |
98 |
128 |
99 data = """\ |
129 data = """\ |
100 <html><head><title>%(title)s</title></head><body> |
130 <html><head><title>%(title)s</title></head><body> |
101 <h1>%(title)s</h1> |
131 <h1>%(title)s</h1> |
102 <p>%(message)s</p> |
132 <p>%(message)s</p> |
|
133 %(details)s |
103 </body></html> |
134 </body></html> |
104 """ % dict( |
135 """ % dict( |
105 title = status, |
136 title = status, |
106 message = message |
137 message = message, |
|
138 details = '<pre>%s</pre>' % details if details else '' |
107 ) |
139 ) |
108 |
140 |
109 super(ErrorResponse, self).__init__(data, status=status) |
141 super(ErrorResponse, self).__init__(data, status=status) |
110 |
142 |
111 class ResponseError (Exception) : |
143 class ResponseError (Exception) : |
112 """ |
144 """ |
113 An exception that results in a specfic 4xx ErrorResponse message to the client |
145 An exception that results in a specfic 4xx ErrorResponse message to the client |
114 """ |
146 """ |
115 |
147 |
116 def __init__ (self, message, status='400 Bad Request') : |
148 def __init__ (self, message, status='400 Bad Request', details=None) : |
117 self.status = status |
149 self.status = status |
118 self.message = message |
150 self.message = message |
|
151 self.details = details |
119 |
152 |
120 super(ResponseError, self).__init__(message) |
153 super(ResponseError, self).__init__(message) |
121 |
154 |
122 def get_response (self) : |
155 def get_response (self) : |
123 return ErrorResponse(self.status, self.message) |
156 return ErrorResponse(self.status, self.message, self.details) |
124 |
157 |
125 class Redirect (Response) : |
158 class Redirect (Response) : |
126 """ |
159 """ |
127 Redirect response |
160 Redirect response |
128 """ |
161 """ |