lib/wsgi.py
author Tero Marttila <terom@fixme.fi>
Sat, 07 Feb 2009 05:12:57 +0200
changeset 28 b68145b5ce24
parent 7 d6a8258bd90e
child 30 a86a25a9f75b
permissions -rw-r--r--
breadcrumb, but it's hidden

"""
    WSGI application implementation
"""

# for error reporting
import sys, traceback

# for Request/Response
import http

# for the request -> response bit :)
import handler

def request_handler (env, start_response) :
    """
        The actual request handling code
    """

    # build Request object
    request = http.Request(env)
    
    try :
        # request -> response
        response = handler.handle_request(request)

    except http.ResponseError, err :
        # just use the generated response
        response = err.get_response()

    # send response
    assert response, "No response"
    
    # send response status/headers
    start_response(response.get_status(), response.get_headers())

    # send respones data
    yield response.get_data()

def app (env, start_response) :
    """
        Wraps request_handler to trap errors
    """

    try :
        # passthrough request_handler
        for chunk in request_handler(env, start_response) :
            yield chunk

    except :
        # execption info
        info = sys.exc_info()

        # try and send 500 ISE to browser, if no headers yet...
        start_response("500 Internal Server Error", [('Content-type', "text/plain; charset=utf8")], info)

        # send traceback
        yield traceback.format_exc()