author | Tero Marttila <terom@fixme.fi> |
Fri, 06 Feb 2009 22:48:00 +0200 | |
changeset 8 | 0ce1f471e9d7 |
parent 7 | d6a8258bd90e |
child 9 | 2a47b00f60b0 |
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) |
|
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
32 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
33 |
def get_script_dir (self) : |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
34 |
""" |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
35 |
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
|
36 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
37 |
/ -> |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
38 |
/foo.cgi -> |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
39 |
/foo/bar.cgi -> /foo |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
40 |
""" |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
41 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
42 |
return os.path.dirname(self.env['SCRIPT_NAME']) |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
43 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
44 |
def get_page_name (self) : |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
45 |
""" |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
46 |
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
|
47 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
48 |
/foo.cgi -> |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
49 |
/foo.cgi/ -> |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
50 |
/foo.cgi/bar -> bar |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
51 |
/foo.cgi/quux/ -> quux/ |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
52 |
""" |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
53 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
54 |
return os.path.normpath(self.env['PATH_INFO']).lstrip('/') |
7 | 55 |
|
56 |
class Response (object) : |
|
57 |
""" |
|
58 |
HTTP Response with headers and data |
|
59 |
""" |
|
60 |
||
61 |
def __init__ (self, data, content_type='text/html', status='200 OK', charset='utf8') : |
|
62 |
""" |
|
63 |
Create the response. The Content-type header is built from the given values. The given \a data must be |
|
64 |
either a str (which is sent plain), an unicode object (which is encoded with the relevant charset), or |
|
65 |
None, whereupon an empty response body is sent. The content_type argument can also be forced to None to |
|
66 |
not send a Content-type header (e.g. for redirects) |
|
67 |
""" |
|
68 |
||
69 |
# store info |
|
70 |
self.status = status |
|
71 |
self.data = data |
|
72 |
self.charset = charset |
|
73 |
||
74 |
# headers |
|
75 |
self.headers = wsgiref.headers.Headers([]) |
|
76 |
||
77 |
# add Content-type header? |
|
78 |
if content_type : |
|
79 |
self.add_header('Content-type', content_type, charset=charset) |
|
80 |
||
81 |
def add_header (self, name, value, **params) : |
|
82 |
""" |
|
83 |
Add response header with the given name/value, plus option params |
|
84 |
||
85 |
XXX: uses the wsgiref.headers code, not sure how that behaves re multiple headers with the same name, etc |
|
86 |
""" |
|
87 |
||
88 |
self.headers.add_header(name, value, **params) |
|
89 |
||
90 |
def get_status (self) : |
|
91 |
""" |
|
92 |
Returns response status string (XXX Foo) |
|
93 |
""" |
|
94 |
||
95 |
return self.status |
|
96 |
||
97 |
def get_headers (self) : |
|
98 |
""" |
|
99 |
Returns the list of header (name, value) pairs |
|
100 |
""" |
|
101 |
||
102 |
return self.headers.items() |
|
103 |
||
104 |
def get_data (self) : |
|
105 |
""" |
|
106 |
Returns the response data - as an encoded string |
|
107 |
""" |
|
108 |
||
109 |
if self.data : |
|
110 |
return self.data.encode(self.charset) |
|
111 |
||
112 |
else : |
|
113 |
return '' |
|
114 |
||
115 |
class ErrorResponse (Response) : |
|
116 |
""" |
|
117 |
A response with an error code / message |
|
118 |
""" |
|
119 |
||
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
120 |
def __init__ (self, status, message, details=None) : |
7 | 121 |
""" |
122 |
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
|
123 |
|
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
124 |
@param status HTTP status code |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
125 |
@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
|
126 |
@param details optional details, plaintext |
7 | 127 |
""" |
128 |
||
129 |
data = """\ |
|
130 |
<html><head><title>%(title)s</title></head><body> |
|
131 |
<h1>%(title)s</h1> |
|
132 |
<p>%(message)s</p> |
|
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
133 |
%(details)s |
7 | 134 |
</body></html> |
135 |
""" % dict( |
|
136 |
title = status, |
|
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
137 |
message = message, |
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
138 |
details = '<pre>%s</pre>' % details if details else '' |
7 | 139 |
) |
140 |
||
141 |
super(ErrorResponse, self).__init__(data, status=status) |
|
142 |
||
143 |
class ResponseError (Exception) : |
|
144 |
""" |
|
145 |
An exception that results in a specfic 4xx ErrorResponse message to the client |
|
146 |
""" |
|
147 |
||
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
148 |
def __init__ (self, message, status='400 Bad Request', details=None) : |
7 | 149 |
self.status = status |
150 |
self.message = message |
|
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
151 |
self.details = details |
7 | 152 |
|
153 |
super(ResponseError, self).__init__(message) |
|
154 |
||
155 |
def get_response (self) : |
|
8
0ce1f471e9d7
and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
7
diff
changeset
|
156 |
return ErrorResponse(self.status, self.message, self.details) |
7 | 157 |
|
158 |
class Redirect (Response) : |
|
159 |
""" |
|
160 |
Redirect response |
|
161 |
""" |
|
162 |
||
163 |
def __init__ (self, url) : |
|
164 |
""" |
|
165 |
Redirect to given *absolute* URL |
|
166 |
""" |
|
167 |
||
168 |
# no content-type or data |
|
169 |
super(Redirect, self).__init__(None, content_type=None, status='302 Found') |
|
170 |
||
171 |
# add Location: header |
|
172 |
self.add_header("Location", url) |
|
173 |
||
174 |