lib/wsgi.py
branchsites
changeset 31 107062ebb6f9
parent 30 a86a25a9f75b
equal deleted inserted replaced
30:a86a25a9f75b 31:107062ebb6f9
     7 import sys, traceback
     7 import sys, traceback
     8 
     8 
     9 # for Request/Response
     9 # for Request/Response
    10 import http
    10 import http
    11 
    11 
    12 # to lookup the Site
    12 class Application (object) :
    13 from site import lookup as site_lookup
       
    14 
       
    15 # for the request -> response bit :)
       
    16 import handler
       
    17 
       
    18 def request_handler (env, start_response) :
       
    19     """
    13     """
    20         The actual request handling code
    14         Our WSGI application, implements the wsgi __call__ interface
    21     """
    15     """
    22 
    16 
    23     # build Request object
    17     def __init__ (self, handler) :
    24     request = http.Request(env)
    18         """
       
    19             Initialize to use the given handler for requests
       
    20         """
    25 
    21 
    26     # lookup site
    22         self.handler = handler
    27     site = site_lookup(request)
    23     
       
    24     def handle_request (self, env, start_response) :
       
    25         """
       
    26             The actual request handling code
       
    27         """
    28 
    28 
    29     # mapper...
    29         # build Request object
    30     mapper = site.get_mapper()
    30         request = http.Request(env)
    31 
    31 
    32     # lookup handler
    32         try :
    33     handler = mapper.map_request(request)
    33             # request -> response using our handler
    34     
    34             response = self.handler.handle_request(request)
    35     try :
       
    36         # request -> response
       
    37         response = handler.handle_request(request)
       
    38 
    35 
    39     except http.ResponseError, err :
    36         except http.ResponseError, err :
    40         # just use the generated response
    37             # just use the generated response
    41         response = err.get_response()
    38             response = err.get_response()
    42 
    39 
    43     # send response
    40         # send response
    44     assert response, "No response"
    41         assert response, "No response"
    45     
    42         
    46     # send response status/headers
    43         # send response status/headers
    47     start_response(response.get_status(), response.get_headers())
    44         start_response(response.get_status(), response.get_headers())
    48 
    45 
    49     # send respones data
    46         # send respones data
    50     yield response.get_data()
    47         yield response.get_data()
    51 
    48 
    52 def app (env, start_response) :
    49     def __call__ (self, env, start_response) :
    53     """
    50         """
    54         Wraps request_handler to trap errors
    51             Wraps handle_request to trap errors
    55     """
    52         """
    56 
    53 
    57     try :
    54         try :
    58         # passthrough request_handler
    55             # passthrough request_handler
    59         for chunk in request_handler(env, start_response) :
    56             for chunk in self.handle_request(env, start_response) :
    60             yield chunk
    57                 yield chunk
    61 
    58 
    62     except :
    59         except :
    63         # execption info
    60             # execption info
    64         info = sys.exc_info()
    61             info = sys.exc_info()
    65 
    62 
    66         # try and send 500 ISE to browser, if no headers yet...
    63             # try and send 500 ISE to browser, if no headers yet...
    67         start_response("500 Internal Server Error", [('Content-type', "text/plain; charset=utf8")], info)
    64             start_response("500 Internal Server Error", [('Content-type', "text/plain; charset=utf8")], info)
    68 
    65 
    69         # send traceback
    66             # send traceback
    70         yield traceback.format_exc()
    67             yield traceback.format_exc()
    71 
    68