Esempio n. 1
0
def application(environ, start_response):
    """
    Entry point for wsgi.
    """
    ## Needed for mod_wsgi, see: <http://code.google.com/p/modwsgi/wiki/ApplicationIssues>
    req = SimulatedModPythonRequest(environ, start_response)
    #print 'Starting mod_python simulation'

    try:
        if (CFG_FULL_HTTPS or
            (CFG_HAS_HTTPS_SUPPORT
             and get_session(req).need_https)) and not req.is_https():
            # We need to isolate the part of the URI that is after
            # CFG_SITE_URL, and append that to our CFG_SITE_SECURE_URL.
            original_parts = urlparse(req.unparsed_uri)
            plain_prefix_parts = urlparse(CFG_SITE_URL)
            secure_prefix_parts = urlparse(CFG_SITE_SECURE_URL)

            # Compute the new path
            plain_path = original_parts[2]
            plain_path = secure_prefix_parts[2] + \
                        plain_path[len(plain_prefix_parts[2]):]

            # ...and recompose the complete URL
            final_parts = list(secure_prefix_parts)
            final_parts[2] = plain_path
            final_parts[-3:] = original_parts[-3:]

            target = urlunparse(final_parts)
            redirect_to_url(req, target)

        possible_module, possible_handler = is_mp_legacy_publisher_path(
            environ['PATH_INFO'])
        if possible_module is not None:
            mp_legacy_publisher(req, possible_module, possible_handler)
        elif CFG_WSGI_SERVE_STATIC_FILES:
            possible_static_path = is_static_path(environ['PATH_INFO'])
            if possible_static_path is not None:
                from invenio.bibdocfile import stream_file
                stream_file(req, possible_static_path)
            else:
                ret = invenio_handler(req)
        else:
            ret = invenio_handler(req)
        req.flush()
    except SERVER_RETURN, status:
        status = int(str(status))
        if status not in (OK, DONE):
            req.status = status
            req.headers_out['content-type'] = 'text/html'
            admin_to_be_alerted = alert_admin_for_server_status_p(
                status, req.headers_in.get('referer'))
            if admin_to_be_alerted:
                register_exception(req=req, alert_admin=True)
            if not req.response_sent_p:
                start_response(req.get_wsgi_status(),
                               req.get_low_level_headers(), sys.exc_info())
            return generate_error_page(req, admin_to_be_alerted)
        else:
            req.flush()
def application(environ, start_response):
    """
    Entry point for wsgi.
    """
    ## Needed for mod_wsgi, see: <http://code.google.com/p/modwsgi/wiki/ApplicationIssues>
    req = SimulatedModPythonRequest(environ, start_response)
    #print 'Starting mod_python simulation'
    try:
        try:
            possible_module, possible_handler = is_mp_legacy_publisher_path(
                environ['PATH_INFO'])
            if possible_module is not None:
                mp_legacy_publisher(req, possible_module, possible_handler)
            elif CFG_WSGI_SERVE_STATIC_FILES:
                possible_static_path = is_static_path(environ['PATH_INFO'])
                if possible_static_path is not None:
                    from invenio.bibdocfile import stream_file
                    stream_file(req, possible_static_path)
                else:
                    ret = invenio_handler(req)
            else:
                ret = invenio_handler(req)
            req.flush()
        except SERVER_RETURN, status:
            status = int(str(status))
            if status not in (OK, DONE):
                req.status = status
                req.headers_out['content-type'] = 'text/html'
                admin_to_be_alerted = alert_admin_for_server_status_p(
                    status, req.headers_in.get('referer'))
                if admin_to_be_alerted:
                    register_exception(req=req, alert_admin=True)
                if not req.response_sent_p:
                    start_response(req.get_wsgi_status(),
                                   req.get_low_level_headers(), sys.exc_info())
                return generate_error_page(req, admin_to_be_alerted)
            else:
                req.flush()
        except:
            register_exception(req=req, alert_admin=True)
            if not req.response_sent_p:
                req.status = HTTP_INTERNAL_SERVER_ERROR
                req.headers_out['content-type'] = 'text/html'
                start_response(req.get_wsgi_status(),
                               req.get_low_level_headers(), sys.exc_info())
                if CFG_DEVEL_SITE:
                    return [
                        "<pre>%s</pre>" % cgi.escape(
                            get_pretty_traceback(req=req,
                                                 exc_info=sys.exc_info()))
                    ]
                    from cgitb import html
                    return [html(sys.exc_info())]
                return generate_error_page(req)
            else:
                return generate_error_page(req, page_already_started=True)
def application(environ, start_response):
    """
    Entry point for wsgi.
    """
    ## Needed for mod_wsgi, see: <http://code.google.com/p/modwsgi/wiki/ApplicationIssues>
    req = SimulatedModPythonRequest(environ, start_response)
    #print 'Starting mod_python simulation'
    try:
        try:
            possible_module, possible_handler = is_mp_legacy_publisher_path(environ['PATH_INFO'])
            if possible_module is not None:
                mp_legacy_publisher(req, possible_module, possible_handler)
            elif CFG_WSGI_SERVE_STATIC_FILES:
                possible_static_path = is_static_path(environ['PATH_INFO'])
                if possible_static_path is not None:
                    from invenio.bibdocfile import stream_file
                    stream_file(req, possible_static_path)
                else:
                    ret = invenio_handler(req)
            else:
                ret = invenio_handler(req)
            req.flush()
        except SERVER_RETURN, status:
            status = int(str(status))
            if status not in (OK, DONE):
                req.status = status
                req.headers_out['content-type'] = 'text/html'
                admin_to_be_alerted = alert_admin_for_server_status_p(status,
                                                  req.headers_in.get('referer'))
                if admin_to_be_alerted:
                    register_exception(req=req, alert_admin=True)
                if not req.response_sent_p:
                    start_response(req.get_wsgi_status(), req.get_low_level_headers(), sys.exc_info())
                return generate_error_page(req, admin_to_be_alerted)
            else:
                req.flush()
        except:
            register_exception(req=req, alert_admin=True)
            if not req.response_sent_p:
                req.status = HTTP_INTERNAL_SERVER_ERROR
                req.headers_out['content-type'] = 'text/html'
                start_response(req.get_wsgi_status(), req.get_low_level_headers(), sys.exc_info())
                if CFG_DEVEL_SITE:
                    return ["<pre>%s</pre>" % cgi.escape(get_pretty_traceback(req=req, exc_info=sys.exc_info()))]
                    from cgitb import html
                    return [html(sys.exc_info())]
                return generate_error_page(req)
            else:
                return generate_error_page(req, page_already_started=True)
def application(environ, start_response, handler=None):
    """
    Entry point for wsgi.
    """
    ## Needed for mod_wsgi, see: <http://code.google.com/p/modwsgi/wiki/ApplicationIssues>
    req = SimulatedModPythonRequest(environ, start_response)
    #print 'Starting mod_python simulation'

    try:
        if handler is None:
            from invenio.webinterface_layout import invenio_handler
            invenio_handler(req)
        else:
            handler(req)
        req.flush()
    ## TODO for future reimplementation of stream_file
    #except StreamFileException as e:
    #    return e.value
    except SERVER_RETURN, status:
        redirection, = status.args
        from werkzeug.wrappers import BaseResponse
        if isinstance(redirection, BaseResponse):
            return redirection
        status = int(str(status))
        if status == 404:
            from werkzeug.exceptions import NotFound
            raise NotFound()
        if status not in (OK, DONE):
            req.status = status
            req.headers_out['content-type'] = 'text/html'
            admin_to_be_alerted = alert_admin_for_server_status_p(
                status, req.headers_in.get('referer'))
            if admin_to_be_alerted:
                register_exception(req=req, alert_admin=True)
            if not req.response_sent_p:
                start_response(req.get_wsgi_status(),
                               req.get_low_level_headers(), sys.exc_info())
            map(req.write, generate_error_page(req, admin_to_be_alerted))

        req.flush()
def application(environ, start_response):
    """
    Entry point for wsgi.
    """
    ## Needed for mod_wsgi, see: <http://code.google.com/p/modwsgi/wiki/ApplicationIssues>
    req = SimulatedModPythonRequest(environ, start_response)
    #print 'Starting mod_python simulation'
    try:
        try:
            if (CFG_FULL_HTTPS or (CFG_HAS_HTTPS_SUPPORT and get_session(req).need_https)) and not req.is_https():
                # We need to isolate the part of the URI that is after
                # CFG_SITE_URL, and append that to our CFG_SITE_SECURE_URL.
                original_parts = urlparse(req.unparsed_uri)
                plain_prefix_parts = urlparse(CFG_SITE_URL)
                secure_prefix_parts = urlparse(CFG_SITE_SECURE_URL)

                # Compute the new path
                plain_path = original_parts[2]
                plain_path = secure_prefix_parts[2] + \
                            plain_path[len(plain_prefix_parts[2]):]

                # ...and recompose the complete URL
                final_parts = list(secure_prefix_parts)
                final_parts[2] = plain_path
                final_parts[-3:] = original_parts[-3:]

                target = urlunparse(final_parts)
                redirect_to_url(req, target)

            possible_module, possible_handler = is_mp_legacy_publisher_path(environ['PATH_INFO'])
            if possible_module is not None:
                mp_legacy_publisher(req, possible_module, possible_handler)
            elif CFG_WSGI_SERVE_STATIC_FILES:
                possible_static_path = is_static_path(environ['PATH_INFO'])
                if possible_static_path is not None:
                    from invenio.bibdocfile import stream_file
                    stream_file(req, possible_static_path)
                else:
                    ret = invenio_handler(req)
            else:
                ret = invenio_handler(req)
            req.flush()
        except SERVER_RETURN, status:
            status = int(str(status))
            if status not in (OK, DONE):
                req.status = status
                req.headers_out['content-type'] = 'text/html'
                admin_to_be_alerted = alert_admin_for_server_status_p(status,
                                                  req.headers_in.get('referer'))
                if admin_to_be_alerted:
                    register_exception(req=req, alert_admin=True)
                if not req.response_sent_p:
                    start_response(req.get_wsgi_status(), req.get_low_level_headers(), sys.exc_info())
                return generate_error_page(req, admin_to_be_alerted)
            else:
                req.flush()
        except:
            register_exception(req=req, alert_admin=True)
            if not req.response_sent_p:
                req.status = HTTP_INTERNAL_SERVER_ERROR
                req.headers_out['content-type'] = 'text/html'
                start_response(req.get_wsgi_status(), req.get_low_level_headers(), sys.exc_info())
                if CFG_DEVEL_SITE:
                    return ["<pre>%s</pre>" % cgi.escape(get_pretty_traceback(req=req, exc_info=sys.exc_info()))]
                    from cgitb import html
                    return [html(sys.exc_info())]
                return generate_error_page(req)
            else:
                return generate_error_page(req, page_already_started=True)