7
|
1 |
|
|
2 |
"""
|
|
3 |
WSGI application implementation
|
|
4 |
"""
|
|
5 |
|
|
6 |
# for error reporting
|
|
7 |
import sys, traceback
|
|
8 |
|
|
9 |
# for Request/Response
|
|
10 |
import http
|
|
11 |
|
|
12 |
# for the request -> response bit :)
|
|
13 |
import handler
|
|
14 |
|
|
15 |
def request_handler (env, start_response) :
|
|
16 |
"""
|
|
17 |
The actual request handling code
|
|
18 |
"""
|
|
19 |
|
|
20 |
# build Request object
|
|
21 |
request = http.Request(env)
|
|
22 |
|
|
23 |
try :
|
|
24 |
# request -> response
|
|
25 |
response = handler.handle_request(request)
|
|
26 |
|
|
27 |
except http.ResponseError, err :
|
|
28 |
# just use the generated response
|
|
29 |
response = err.get_response()
|
|
30 |
|
|
31 |
# send response
|
|
32 |
assert response, "No response"
|
|
33 |
|
|
34 |
# send response status/headers
|
|
35 |
start_response(response.get_status(), response.get_headers())
|
|
36 |
|
|
37 |
# send respones data
|
|
38 |
yield response.get_data()
|
|
39 |
|
|
40 |
def app (env, start_response) :
|
|
41 |
"""
|
|
42 |
Wraps request_handler to trap errors
|
|
43 |
"""
|
|
44 |
|
|
45 |
try :
|
|
46 |
# passthrough request_handler
|
|
47 |
for chunk in request_handler(env, start_response) :
|
|
48 |
yield chunk
|
|
49 |
|
|
50 |
except :
|
|
51 |
# execption info
|
|
52 |
info = sys.exc_info()
|
|
53 |
|
|
54 |
# try and send 500 ISE to browser, if no headers yet...
|
|
55 |
start_response("500 Internal Server Error", [('Content-type', "text/plain; charset=utf8")], info)
|
|
56 |
|
|
57 |
# send traceback
|
|
58 |
yield traceback.format_exc()
|
|
59 |
|