author | Tero Marttila <terom@fixme.fi> |
Mon, 09 Feb 2009 06:55:51 +0200 | |
changeset 55 | d36efeb64650 |
parent 49 | 9b097385b463 |
child 57 | 2ed89377f339 |
permissions | -rw-r--r-- |
7 | 1 |
""" |
2 |
WSGI HTTP utility code |
|
3 |
""" |
|
4 |
||
5 |
# for utility functions |
|
6 |
import cgi |
|
7 |
||
8 |
# for header handling |
|
9 |
import wsgiref.headers |
|
10 |
||
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
11 |
# for path handling |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
12 |
import os.path |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
13 |
|
7 | 14 |
class Request (object) : |
15 |
""" |
|
16 |
HTTP Request with associated metadata |
|
17 |
""" |
|
18 |
||
19 |
def __init__ (self, env) : |
|
20 |
""" |
|
21 |
Parse env data |
|
22 |
""" |
|
23 |
||
24 |
# store env |
|
25 |
self.env = env |
|
26 |
||
27 |
# get the querystring |
|
28 |
self.arg_str = env.get('QUERY_STRING', '') |
|
29 |
||
30 |
# parse query args |
|
31 |
self.arg_dict = cgi.parse_qs(self.arg_str, True) |
|
55 | 32 |
|
33 |
# load post data? |
|
34 |
if self.is_post() : |
|
35 |
# content-type of post data |
|
36 |
content_type = self.env.get('CONTENT_TYPE', 'application/x-www-form-urlencoded') |
|
37 |
||
38 |
# valid content-type? |
|
39 |
# XXX: how to handle errors? |
|
40 |
assert any(content_type.startswith(x) for x in ( |
|
41 |
'application/x-www-form-urlencoded', |
|
42 |
'multipart/form-data' |
|
43 |
)) |
|
44 |
||
45 |
# input stream |
|
46 |
input = self.env['wsgi.input'] |
|
47 |
||
48 |
# use cgi.FieldStorage to parse this |
|
49 |
self.post_data = cgi.FieldStorage(fp=input, environ=self.env, keep_blank_values=1) |
|
50 |
||
51 |
else : |
|
52 |
# no post data |
|
53 |
self.post_data = None |
|
42
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
25
diff
changeset
|
54 |
|
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
25
diff
changeset
|
55 |
@property |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
25
diff
changeset
|
56 |
def site_host (self) : |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
25
diff
changeset
|
57 |
""" |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
25
diff
changeset
|
58 |
Returns the site's hostname (DNS name) |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
25
diff
changeset
|
59 |
""" |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
25
diff
changeset
|
60 |
|
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
25
diff
changeset
|
61 |
return self.env['HTTP_HOST'] |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
25
diff
changeset
|
62 |
|
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
25
diff
changeset
|
63 |
@property |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
25
diff
changeset
|
64 |
def site_root (self) : |
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
65 |
""" |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
66 |
Returns the URL path to the requested script's directory with no trailing slash, i.e. |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
67 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
68 |
/ -> |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
69 |
/foo.cgi -> |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
70 |
/foo/bar.cgi -> /foo |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
71 |
""" |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
72 |
|
14
b88d23696b98
add cache/template dir, and fix get_script_dir to give '' for root
Tero Marttila <terom@fixme.fi>
parents:
10
diff
changeset
|
73 |
return os.path.dirname(self.env['SCRIPT_NAME']).rstrip('/') |
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
74 |
|
42
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
25
diff
changeset
|
75 |
@property |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
25
diff
changeset
|
76 |
def page_prefix (self) : |
10
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
77 |
""" |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
78 |
Returns the URL path root for page URLs, based on REQUEST_URI with PATH_INFO removed |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
79 |
|
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
80 |
/ -> |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
81 |
/foo.cgi -> /foo.cgi |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
82 |
/foo.cgi/index -> /foo.cgi |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
83 |
/foo.cgi/quux/bar -> /foo.cgi |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
84 |
/quux/foo.cgi/bar -> /quux/foo.cgi |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
85 |
/bar -> |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
86 |
""" |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
87 |
|
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
88 |
# XXX: request uri path without the query string |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
89 |
request_path = self.env.get('REQUEST_URI', '').split('?', 1)[0].rstrip('/') |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
90 |
|
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
91 |
# path info |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
92 |
page_name = self.get_page_name() |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
93 |
|
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
94 |
# special-case for empty page_name |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
95 |
if not page_name : |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
96 |
return request_path |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
97 |
|
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
98 |
# sanity-check |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
99 |
assert request_path.endswith(page_name) |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
100 |
|
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
101 |
# trim |
d83b10c210e3
some vodoo for generating correct URLs
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
102 |
return request_path[:-len(page_name)].rstrip('/') |
42
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
25
diff
changeset
|
103 |
|
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
104 |
def get_page_name (self) : |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
105 |
""" |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
106 |
Returns the requested page path with no leading slash, i.e. |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
107 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
108 |
/foo.cgi -> |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
109 |
/foo.cgi/ -> |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
110 |
/foo.cgi/bar -> bar |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
111 |
/foo.cgi/quux/ -> quux/ |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
112 |
""" |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
113 |
|
9 | 114 |
# the raw PATH_INFO |
115 |
path_info = self.env.get('PATH_INFO') |
|
116 |
||
117 |
# avoid nasty '.' paths |
|
118 |
if path_info : |
|
119 |
return os.path.normpath(path_info).lstrip('/') |
|
120 |
||
121 |
else : |
|
122 |
return '' |
|
42
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
25
diff
changeset
|
123 |
|
49
9b097385b463
remove name from URLType, handle it separately in SimpleValueLabel + improve query argument handling
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
124 |
def get_arg (self, name, default=None) : |
9b097385b463
remove name from URLType, handle it separately in SimpleValueLabel + improve query argument handling
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
125 |
""" |
9b097385b463
remove name from URLType, handle it separately in SimpleValueLabel + improve query argument handling
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
126 |
Get a single value for an argument with the given key, or the default if missing |
9b097385b463
remove name from URLType, handle it separately in SimpleValueLabel + improve query argument handling
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
127 |
""" |
9b097385b463
remove name from URLType, handle it separately in SimpleValueLabel + improve query argument handling
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
128 |
|
9b097385b463
remove name from URLType, handle it separately in SimpleValueLabel + improve query argument handling
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
129 |
if name in self.arg_dict : |
9b097385b463
remove name from URLType, handle it separately in SimpleValueLabel + improve query argument handling
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
130 |
return self.arg_dict[name][0] |
9b097385b463
remove name from URLType, handle it separately in SimpleValueLabel + improve query argument handling
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
131 |
|
9b097385b463
remove name from URLType, handle it separately in SimpleValueLabel + improve query argument handling
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
132 |
else : |
9b097385b463
remove name from URLType, handle it separately in SimpleValueLabel + improve query argument handling
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
133 |
return default |
9b097385b463
remove name from URLType, handle it separately in SimpleValueLabel + improve query argument handling
Tero Marttila <terom@fixme.fi>
parents:
46
diff
changeset
|
134 |
|
42
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
25
diff
changeset
|
135 |
def get_args (self) : |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
25
diff
changeset
|
136 |
""" |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
25
diff
changeset
|
137 |
Iterate over all available (key, value) pairs from the query string |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
25
diff
changeset
|
138 |
""" |
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
25
diff
changeset
|
139 |
|
5a72c00c4ae4
more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents:
25
diff
changeset
|
140 |
return cgi.parse_qsl(self.arg_str) |
55 | 141 |
|
142 |
def is_post (self) : |
|
143 |
""" |
|
144 |
Is this a POST request? |
|
145 |
""" |
|
146 |
||
147 |
# just check REQUEST_METHOD |
|
148 |
return (self.env['REQUEST_METHOD'].upper() == 'POST') |
|
149 |
||
150 |
def get_post (self, name) : |
|
151 |
""" |
|
152 |
Get the value of the given POST field |
|
153 |
""" |
|
154 |
||
155 |
# sanity-check |
|
156 |
assert self.post_data |
|
157 |
||
158 |
# return the FieldStorage value |
|
159 |
return self.post_data[name].value |
|
7 | 160 |
|
161 |
class Response (object) : |
|
162 |
""" |
|
163 |
HTTP Response with headers and data |
|
164 |
""" |
|
165 |
||
25
8f143b1ce0d1
lowercase qmsk.net, and use UTF-8 for the HTTP encoding, not utf8
Tero Marttila <terom@fixme.fi>
parents:
14
diff
changeset
|
166 |
def __init__ (self, data, content_type='text/html', status='200 OK', charset='UTF-8') : |
7 | 167 |
""" |
168 |
Create the response. The Content-type header is built from the given values. The given \a data must be |
|
169 |
either a str (which is sent plain), an unicode object (which is encoded with the relevant charset), or |
|
170 |
None, whereupon an empty response body is sent. The content_type argument can also be forced to None to |
|
171 |
not send a Content-type header (e.g. for redirects) |
|
172 |
""" |
|
173 |
||
174 |
# store info |
|
175 |
self.status = status |
|
176 |
self.data = data |
|
177 |
self.charset = charset |
|
178 |
||
179 |
# headers |
|
180 |
self.headers = wsgiref.headers.Headers([]) |
|
181 |
||
182 |
# add Content-type header? |
|
183 |
if content_type : |
|
184 |
self.add_header('Content-type', content_type, charset=charset) |
|
185 |
||
186 |
def add_header (self, name, value, **params) : |
|
187 |
""" |
|
188 |
Add response header with the given name/value, plus option params |
|
189 |
||
190 |
XXX: uses the wsgiref.headers code, not sure how that behaves re multiple headers with the same name, etc |
|
191 |
""" |
|
192 |
||
193 |
self.headers.add_header(name, value, **params) |
|
194 |
||
195 |
def get_status (self) : |
|
196 |
""" |
|
197 |
Returns response status string (XXX Foo) |
|
198 |
""" |
|
199 |
||
200 |
return self.status |
|
201 |
||
202 |
def get_headers (self) : |
|
203 |
""" |
|
204 |
Returns the list of header (name, value) pairs |
|
205 |
""" |
|
206 |
||
207 |
return self.headers.items() |
|
208 |
||
209 |
def get_data (self) : |
|
210 |
""" |
|
211 |
Returns the response data - as an encoded string |
|
212 |
""" |
|
213 |
||
214 |
if self.data : |
|
215 |
return self.data.encode(self.charset) |
|
216 |
||
217 |
else : |
|
218 |
return '' |
|
219 |
||
220 |
class ErrorResponse (Response) : |
|
221 |
""" |
|
222 |
A response with an error code / message |
|
223 |
""" |
|
224 |
||
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
225 |
def __init__ (self, status, message, details=None) : |
7 | 226 |
""" |
227 |
Build a plain error message response with the given status/message |
|
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
228 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
229 |
@param status HTTP status code |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
230 |
@param message short message to describe errors |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
231 |
@param details optional details, plaintext |
7 | 232 |
""" |
233 |
||
234 |
data = """\ |
|
235 |
<html><head><title>%(title)s</title></head><body> |
|
236 |
<h1>%(title)s</h1> |
|
237 |
<p>%(message)s</p> |
|
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
238 |
%(details)s |
7 | 239 |
</body></html> |
240 |
""" % dict( |
|
241 |
title = status, |
|
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
242 |
message = message, |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
243 |
details = '<pre>%s</pre>' % details if details else '' |
7 | 244 |
) |
245 |
||
246 |
super(ErrorResponse, self).__init__(data, status=status) |
|
247 |
||
248 |
class ResponseError (Exception) : |
|
249 |
""" |
|
250 |
An exception that results in a specfic 4xx ErrorResponse message to the client |
|
251 |
""" |
|
252 |
||
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
253 |
def __init__ (self, message, status='400 Bad Request', details=None) : |
7 | 254 |
self.status = status |
255 |
self.message = message |
|
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
256 |
self.details = details |
7 | 257 |
|
258 |
super(ResponseError, self).__init__(message) |
|
259 |
||
260 |
def get_response (self) : |
|
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
261 |
return ErrorResponse(self.status, self.message, self.details) |
7 | 262 |
|
263 |
class Redirect (Response) : |
|
264 |
""" |
|
265 |
Redirect response |
|
266 |
""" |
|
267 |
||
268 |
def __init__ (self, url) : |
|
269 |
""" |
|
270 |
Redirect to given *absolute* URL |
|
271 |
""" |
|
272 |
||
273 |
# no content-type or data |
|
274 |
super(Redirect, self).__init__(None, content_type=None, status='302 Found') |
|
275 |
||
276 |
# add Location: header |
|
277 |
self.add_header("Location", url) |
|
278 |
||
279 |