Ejemplo n.º 1
0
 def testCombinedTools(self):
     expectedResult = (ntou("Hello,world") + europoundUnicode).encode('utf-8')
     zbuf = BytesIO()
     zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=9)
     zfile.write(expectedResult)
     zfile.close()
     
     self.getPage("/euro", headers=[("Accept-Encoding", "gzip"),
                                     ("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7")])
     self.assertInBody(zbuf.getvalue()[:3])
     
     zbuf = BytesIO()
     zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=6)
     zfile.write(expectedResult)
     zfile.close()
     
     self.getPage("/decorated_euro", headers=[("Accept-Encoding", "gzip")])
     self.assertInBody(zbuf.getvalue()[:3])
     
     # This returns a different value because gzip's priority was
     # lowered in conf, allowing the rotator to run after gzip.
     # Of course, we don't want breakage in production apps,
     # but it proves the priority was changed.
     self.getPage("/decorated_euro/subpath",
                  headers=[("Accept-Encoding", "gzip")])
     self.assertInBody(bytes([(x + 3) % 256 for x in zbuf.getvalue()]))
Ejemplo n.º 2
0
 def testGzip(self):
     zbuf = BytesIO()
     zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=9)
     zfile.write(ntob('Hello, world'))
     zfile.close()
     self.getPage('/gzip/', headers=[('Accept-Encoding', 'gzip')])
     self.assertInBody(zbuf.getvalue()[:3])
     self.assertHeader('Vary', 'Accept-Encoding')
     self.assertHeader('Content-Encoding', 'gzip')
     self.getPage('/gzip/', headers=[('Accept-Encoding', 'identity')])
     self.assertHeader('Vary', 'Accept-Encoding')
     self.assertNoHeader('Content-Encoding')
     self.assertBody('Hello, world')
     self.getPage('/gzip/', headers=[('Accept-Encoding', 'gzip;q=0')])
     self.assertHeader('Vary', 'Accept-Encoding')
     self.assertNoHeader('Content-Encoding')
     self.assertBody('Hello, world')
     self.getPage('/gzip/', headers=[('Accept-Encoding', '*;q=0')])
     self.assertStatus(406)
     self.assertNoHeader('Content-Encoding')
     self.assertErrorPage(406, 'identity, gzip')
     self.getPage('/gzip/noshow', headers=[('Accept-Encoding', 'gzip')])
     self.assertNoHeader('Content-Encoding')
     self.assertStatus(500)
     self.assertErrorPage(500, pattern='IndexError\n')
     if cherrypy.server.protocol_version == 'HTTP/1.0' or getattr(cherrypy.server, 'using_apache', False):
         self.getPage('/gzip/noshow_stream', headers=[('Accept-Encoding', 'gzip')])
         self.assertHeader('Content-Encoding', 'gzip')
         self.assertInBody('\x1f\x8b\x08\x00')
     else:
         self.assertRaises((ValueError, IncompleteRead), self.getPage, '/gzip/noshow_stream', headers=[('Accept-Encoding', 'gzip')])
Ejemplo n.º 3
0
 def testGzip(self):
     zbuf = BytesIO()
     zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=9)
     zfile.write(ntob('Hello, world'))
     zfile.close()
     self.getPage('/gzip/', headers=[('Accept-Encoding', 'gzip')])
     self.assertInBody(zbuf.getvalue()[:3])
     self.assertHeader('Vary', 'Accept-Encoding')
     self.assertHeader('Content-Encoding', 'gzip')
     self.getPage('/gzip/', headers=[('Accept-Encoding', 'identity')])
     self.assertHeader('Vary', 'Accept-Encoding')
     self.assertNoHeader('Content-Encoding')
     self.assertBody('Hello, world')
     self.getPage('/gzip/', headers=[('Accept-Encoding', 'gzip;q=0')])
     self.assertHeader('Vary', 'Accept-Encoding')
     self.assertNoHeader('Content-Encoding')
     self.assertBody('Hello, world')
     self.getPage('/gzip/', headers=[('Accept-Encoding', '*;q=0')])
     self.assertStatus(406)
     self.assertNoHeader('Content-Encoding')
     self.assertErrorPage(406, 'identity, gzip')
     self.getPage('/gzip/noshow', headers=[('Accept-Encoding', 'gzip')])
     self.assertNoHeader('Content-Encoding')
     self.assertStatus(500)
     self.assertErrorPage(500, pattern='IndexError\n')
     if cherrypy.server.protocol_version == 'HTTP/1.0' or getattr(cherrypy.server, 'using_apache', False):
         self.getPage('/gzip/noshow_stream', headers=[('Accept-Encoding', 'gzip')])
         self.assertHeader('Content-Encoding', 'gzip')
         self.assertInBody('\x1f\x8b\x08\x00')
     else:
         self.assertRaises((ValueError, IncompleteRead), self.getPage, '/gzip/noshow_stream', headers=[('Accept-Encoding', 'gzip')])
Ejemplo n.º 4
0
def decompress(body):
    import gzip
    zbuf = BytesIO()
    zbuf.write(body)
    zbuf.seek(0)
    zfile = gzip.GzipFile(mode='rb', fileobj=zbuf)
    data = zfile.read()
    zfile.close()
    return data
Ejemplo n.º 5
0
 def stats(self, filename, sortby='cumulative'):
     """:rtype stats(index): output of print_stats() for the given profile.
     """
     sio = BytesIO()
     s = pstats.Stats(os.path.join(self.path, filename), stream=sio)
     s.strip_dirs()
     s.sort_stats(sortby)
     s.print_stats()
     response = sio.getvalue()
     sio.close()
     return response
Ejemplo n.º 6
0
    def testCombinedTools(self):
        expectedResult = (ntou("Hello,world") + europoundUnicode).encode('utf-8')
        zbuf = BytesIO()
        zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=9)
        zfile.write(expectedResult)
        zfile.close()

        self.getPage("/euro", headers=[("Accept-Encoding", "gzip"),
                                        ("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7")])
        self.assertInBody(zbuf.getvalue()[:3])

        zbuf = BytesIO()
        zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=6)
        zfile.write(expectedResult)
        zfile.close()

        self.getPage("/decorated_euro", headers=[("Accept-Encoding", "gzip")])
        self.assertInBody(zbuf.getvalue()[:3])

        # This returns a different value because gzip's priority was
        # lowered in conf, allowing the rotator to run after gzip.
        # Of course, we don't want breakage in production apps,
        # but it proves the priority was changed.
        self.getPage("/decorated_euro/subpath",
                     headers=[("Accept-Encoding", "gzip")])
        if py3k:
            self.assertInBody(bytes([(x + 3) % 256 for x in zbuf.getvalue()]))
        else:
            self.assertInBody(''.join([chr((ord(x) + 3) % 256) for x in zbuf.getvalue()]))
Ejemplo n.º 7
0
    def testGzip(self):
        zbuf = BytesIO()
        zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=9)
        zfile.write(ntob("Hello, world"))
        zfile.close()

        self.getPage('/gzip/', headers=[("Accept-Encoding", "gzip")])
        self.assertInBody(zbuf.getvalue()[:3])
        self.assertHeader("Vary", "Accept-Encoding")
        self.assertHeader("Content-Encoding", "gzip")

        # Test when gzip is denied.
        self.getPage('/gzip/', headers=[("Accept-Encoding", "identity")])
        self.assertHeader("Vary", "Accept-Encoding")
        self.assertNoHeader("Content-Encoding")
        self.assertBody("Hello, world")

        self.getPage('/gzip/', headers=[("Accept-Encoding", "gzip;q=0")])
        self.assertHeader("Vary", "Accept-Encoding")
        self.assertNoHeader("Content-Encoding")
        self.assertBody("Hello, world")

        self.getPage('/gzip/', headers=[("Accept-Encoding", "*;q=0")])
        self.assertStatus(406)
        self.assertNoHeader("Content-Encoding")
        self.assertErrorPage(406, "identity, gzip")

        # Test for ticket #147
        self.getPage('/gzip/noshow', headers=[("Accept-Encoding", "gzip")])
        self.assertNoHeader('Content-Encoding')
        self.assertStatus(500)
        self.assertErrorPage(500, pattern="IndexError\n")

        # In this case, there's nothing we can do to deliver a
        # readable page, since 1) the gzip header is already set,
        # and 2) we may have already written some of the body.
        # The fix is to never stream yields when using gzip.
        if (cherrypy.server.protocol_version == "HTTP/1.0"
                or getattr(cherrypy.server, "using_apache", False)):
            self.getPage('/gzip/noshow_stream',
                         headers=[("Accept-Encoding", "gzip")])
            self.assertHeader('Content-Encoding', 'gzip')
            self.assertInBody('\x1f\x8b\x08\x00')
        else:
            # The wsgiserver will simply stop sending data, and the HTTP client
            # will error due to an incomplete chunk-encoded stream.
            self.assertRaises((ValueError, IncompleteRead),
                              self.getPage,
                              '/gzip/noshow_stream',
                              headers=[("Accept-Encoding", "gzip")])
Ejemplo n.º 8
0
 def testGzip(self):
     zbuf = BytesIO()
     zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=9)
     zfile.write(ntob("Hello, world"))
     zfile.close()
     
     self.getPage('/gzip/', headers=[("Accept-Encoding", "gzip")])
     self.assertInBody(zbuf.getvalue()[:3])
     self.assertHeader("Vary", "Accept-Encoding")
     self.assertHeader("Content-Encoding", "gzip")
     
     # Test when gzip is denied.
     self.getPage('/gzip/', headers=[("Accept-Encoding", "identity")])
     self.assertHeader("Vary", "Accept-Encoding")
     self.assertNoHeader("Content-Encoding")
     self.assertBody("Hello, world")
     
     self.getPage('/gzip/', headers=[("Accept-Encoding", "gzip;q=0")])
     self.assertHeader("Vary", "Accept-Encoding")
     self.assertNoHeader("Content-Encoding")
     self.assertBody("Hello, world")
     
     self.getPage('/gzip/', headers=[("Accept-Encoding", "*;q=0")])
     self.assertStatus(406)
     self.assertNoHeader("Content-Encoding")
     self.assertErrorPage(406, "identity, gzip")
     
     # Test for ticket #147
     self.getPage('/gzip/noshow', headers=[("Accept-Encoding", "gzip")])
     self.assertNoHeader('Content-Encoding')
     self.assertStatus(500)
     self.assertErrorPage(500, pattern="IndexError\n")
     
     # In this case, there's nothing we can do to deliver a
     # readable page, since 1) the gzip header is already set,
     # and 2) we may have already written some of the body.
     # The fix is to never stream yields when using gzip.
     if (cherrypy.server.protocol_version == "HTTP/1.0" or
         getattr(cherrypy.server, "using_apache", False)):
         self.getPage('/gzip/noshow_stream',
                      headers=[("Accept-Encoding", "gzip")])
         self.assertHeader('Content-Encoding', 'gzip')
         self.assertInBody('\x1f\x8b\x08\x00')
     else:
         # The wsgiserver will simply stop sending data, and the HTTP client
         # will error due to an incomplete chunk-encoded stream.
         self.assertRaises((ValueError, IncompleteRead), self.getPage,
                           '/gzip/noshow_stream',
                           headers=[("Accept-Encoding", "gzip")])
Ejemplo n.º 9
0
 def testCombinedTools(self):
     expectedResult = (ntou('Hello,world') + europoundUnicode).encode('utf-8')
     zbuf = BytesIO()
     zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=9)
     zfile.write(expectedResult)
     zfile.close()
     self.getPage('/euro', headers=[('Accept-Encoding', 'gzip'), ('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7')])
     self.assertInBody(zbuf.getvalue()[:3])
     zbuf = BytesIO()
     zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=6)
     zfile.write(expectedResult)
     zfile.close()
     self.getPage('/decorated_euro', headers=[('Accept-Encoding', 'gzip')])
     self.assertInBody(zbuf.getvalue()[:3])
     self.getPage('/decorated_euro/subpath', headers=[('Accept-Encoding', 'gzip')])
     self.assertInBody(''.join([ chr((ord(x) + 3) % 256) for x in zbuf.getvalue() ]))
    def respond(self):
        req = self.req
        try:
            local = req.server.bind_addr
            local = httputil.Host(local[0], local[1], '')
            remote = (req.conn.remote_addr, req.conn.remote_port)
            remote = httputil.Host(remote[0], remote[1], '')
            scheme = req.scheme
            sn = cherrypy.tree.script_name(req.uri or '/')
            if sn is None:
                self.send_response('404 Not Found', [], [''])
            else:
                app = cherrypy.tree.apps[sn]
                method = req.method
                path = req.path
                qs = req.qs or ''
                headers = req.inheaders.items()
                rfile = req.rfile
                prev = None
                try:
                    redirections = []
                    while True:
                        request, response = app.get_serving(
                            local, remote, scheme, 'HTTP/1.1')
                        request.multithread = True
                        request.multiprocess = False
                        request.app = app
                        request.prev = prev
                        try:
                            request.run(method, path, qs, req.request_protocol,
                                        headers, rfile)
                            break
                        except cherrypy.InternalRedirect:
                            ir = sys.exc_info()[1]
                            app.release_serving()
                            prev = request
                            if not self.recursive:
                                if ir.path in redirections:
                                    raise RuntimeError(
                                        'InternalRedirector visited the same URL twice: %r'
                                        % ir.path)
                                else:
                                    if qs:
                                        qs = '?' + qs
                                    redirections.append(sn + path + qs)
                            method = 'GET'
                            path = ir.path
                            qs = ir.query_string
                            rfile = BytesIO()

                    self.send_response(response.output_status,
                                       response.header_list, response.body)
                finally:
                    app.release_serving()

        except:
            tb = format_exc()
            cherrypy.log(tb, 'NATIVE_ADAPTER', severity=logging.ERROR)
            s, h, b = bare_error()
            self.send_response(s, h, b)
Ejemplo n.º 11
0
 def stream_handler(next_handler, *args, **kwargs):
     cherrypy.response.output = o = BytesIO()
     try:
         response = next_handler(*args, **kwargs)
         return o.getvalue()
     finally:
         o.close()
Ejemplo n.º 12
0
 def __call__(self, environ, start_response):
     redirections = []
     while True:
         environ = environ.copy()
         try:
             return self.nextapp(environ, start_response)
         except _cherrypy.InternalRedirect:
             ir = _sys.exc_info()[1]
             sn = environ.get('SCRIPT_NAME', '')
             path = environ.get('PATH_INFO', '')
             qs = environ.get('QUERY_STRING', '')
             
             # Add the *previous* path_info + qs to redirections.
             old_uri = sn + path
             if qs:
                 old_uri += "?" + qs
             redirections.append(old_uri)
             
             if not self.recursive:
                 # Check to see if the new URI has been redirected to already
                 new_uri = sn + ir.path
                 if ir.query_string:
                     new_uri += "?" + ir.query_string
                 if new_uri in redirections:
                     ir.request.close()
                     raise RuntimeError("InternalRedirector visited the "
                                        "same URL twice: %r" % new_uri)
             
             # Munge the environment and try again.
             environ['REQUEST_METHOD'] = "GET"
             environ['PATH_INFO'] = ir.path
             environ['QUERY_STRING'] = ir.query_string
             environ['wsgi.input'] = BytesIO()
             environ['CONTENT_LENGTH'] = "0"
             environ['cherrypy.previous_request'] = ir.request
Ejemplo n.º 13
0
 def __call__(self, environ, start_response):
     redirections = []
     while True:
         environ = environ.copy()
         try:
             return self.nextapp(environ, start_response)
         except _cherrypy.InternalRedirect as ir:
             sn = environ.get('SCRIPT_NAME', '')
             path = environ.get('PATH_INFO', '')
             qs = environ.get('QUERY_STRING', '')
             old_uri = sn + path
             if qs:
                 old_uri += '?' + qs
             redirections.append(old_uri)
             if not self.recursive:
                 new_uri = sn + ir.path
                 if ir.query_string:
                     new_uri += '?' + ir.query_string
                 if new_uri in redirections:
                     ir.request.close()
                     raise RuntimeError('InternalRedirector visited the same URL twice: %r' % new_uri)
             environ['REQUEST_METHOD'] = 'GET'
             environ['PATH_INFO'] = ir.path
             environ['QUERY_STRING'] = ir.query_string
             environ['wsgi.input'] = BytesIO()
             environ['CONTENT_LENGTH'] = '0'
             environ['cherrypy.previous_request'] = ir.request
Ejemplo n.º 14
0
 def stream_handler(next_handler, *args, **kwargs):
     cherrypy.response.output = o = BytesIO()
     try:
         response = next_handler(*args, **kwargs)
         # Ignore the response and return our accumulated output instead.
         return o.getvalue()
     finally:
         o.close()
Ejemplo n.º 15
0
 def stream_handler(next_handler, *args, **kwargs):
     assert cherrypy.request.config.get('tools.streamer.arg') == 'arg value'
     cherrypy.response.output = o = BytesIO()
     try:
         response = next_handler(*args, **kwargs)
         # Ignore the response and return our accumulated output
         # instead.
         return o.getvalue()
     finally:
         o.close()
Ejemplo n.º 16
0
    def stats(self, filename, sortby='cumulative'):
        sio = BytesIO()
        if sys.version_info >= (2, 5):
            s = pstats.Stats(os.path.join(self.path, filename), stream=sio)
            s.strip_dirs()
            s.sort_stats(sortby)
            s.print_stats()
        else:
            s = pstats.Stats(os.path.join(self.path, filename))
            s.strip_dirs()
            s.sort_stats(sortby)
            oldout = sys.stdout
            try:
                sys.stdout = sio
                s.print_stats()
            finally:
                sys.stdout = oldout

        response = sio.getvalue()
        sio.close()
        return response
Ejemplo n.º 17
0
def decompress(body):
    import gzip
    zbuf = BytesIO()
    zbuf.write(body)
    zbuf.seek(0)
    zfile = gzip.GzipFile(mode='rb', fileobj=zbuf)
    data = zfile.read()
    zfile.close()
    return data
Ejemplo n.º 18
0
 def stats(self, filename, sortby='cumulative'):
     """:rtype stats(index): output of print_stats() for the given profile.
     """
     sio = BytesIO()
     if sys.version_info >= (2, 5):
         s = pstats.Stats(os.path.join(self.path, filename), stream=sio)
         s.strip_dirs()
         s.sort_stats(sortby)
         s.print_stats()
     else:
         # pstats.Stats before Python 2.5 didn't take a 'stream' arg,
         # but just printed to stdout. So re-route stdout.
         s = pstats.Stats(os.path.join(self.path, filename))
         s.strip_dirs()
         s.sort_stats(sortby)
         oldout = sys.stdout
         try:
             sys.stdout = sio
             s.print_stats()
         finally:
             sys.stdout = oldout
     response = sio.getvalue()
     sio.close()
     return response
Ejemplo n.º 19
0
 def testCombinedTools(self):
     expectedResult = (ntou('Hello,world') +
                       europoundUnicode).encode('utf-8')
     zbuf = BytesIO()
     zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=9)
     zfile.write(expectedResult)
     zfile.close()
     self.getPage('/euro',
                  headers=[('Accept-Encoding', 'gzip'),
                           ('Accept-Charset',
                            'ISO-8859-1,utf-8;q=0.7,*;q=0.7')])
     self.assertInBody(zbuf.getvalue()[:3])
     zbuf = BytesIO()
     zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=6)
     zfile.write(expectedResult)
     zfile.close()
     self.getPage('/decorated_euro', headers=[('Accept-Encoding', 'gzip')])
     self.assertInBody(zbuf.getvalue()[:3])
     self.getPage('/decorated_euro/subpath',
                  headers=[('Accept-Encoding', 'gzip')])
     self.assertInBody(''.join(
         [chr((ord(x) + 3) % 256) for x in zbuf.getvalue()]))
Ejemplo n.º 20
0
def handler(req):
    from mod_python import apache
    try:
        global _isSetUp
        if not _isSetUp:
            setup(req)
            _isSetUp = True

        # Obtain a Request object from CherryPy
        local = req.connection.local_addr
        local = httputil.Host(local[0], local[1], req.connection.local_host
                              or "")
        remote = req.connection.remote_addr
        remote = httputil.Host(remote[0], remote[1], req.connection.remote_host
                               or "")

        scheme = req.parsed_uri[0] or 'http'
        req.get_basic_auth_pw()

        try:
            # apache.mpm_query only became available in mod_python 3.1
            q = apache.mpm_query
            threaded = q(apache.AP_MPMQ_IS_THREADED)
            forked = q(apache.AP_MPMQ_IS_FORKED)
        except AttributeError:
            bad_value = ("You must provide a PythonOption '%s', "
                         "either 'on' or 'off', when running a version "
                         "of mod_python < 3.1")

            threaded = options.get('multithread', '').lower()
            if threaded == 'on':
                threaded = True
            elif threaded == 'off':
                threaded = False
            else:
                raise ValueError(bad_value % "multithread")

            forked = options.get('multiprocess', '').lower()
            if forked == 'on':
                forked = True
            elif forked == 'off':
                forked = False
            else:
                raise ValueError(bad_value % "multiprocess")

        sn = cherrypy.tree.script_name(req.uri or "/")
        if sn is None:
            send_response(req, '404 Not Found', [], '')
        else:
            app = cherrypy.tree.apps[sn]
            method = req.method
            path = req.uri
            qs = req.args or ""
            reqproto = req.protocol
            headers = copyitems(req.headers_in)
            rfile = _ReadOnlyRequest(req)
            prev = None

            try:
                redirections = []
                while True:
                    request, response = app.get_serving(
                        local, remote, scheme, "HTTP/1.1")
                    request.login = req.user
                    request.multithread = bool(threaded)
                    request.multiprocess = bool(forked)
                    request.app = app
                    request.prev = prev

                    # Run the CherryPy Request object and obtain the response
                    try:
                        request.run(method, path, qs, reqproto, headers, rfile)
                        break
                    except cherrypy.InternalRedirect:
                        ir = sys.exc_info()[1]
                        app.release_serving()
                        prev = request

                        if not recursive:
                            if ir.path in redirections:
                                raise RuntimeError(
                                    "InternalRedirector visited the "
                                    "same URL twice: %r" % ir.path)
                            else:
                                # Add the *previous* path_info + qs to redirections.
                                if qs:
                                    qs = "?" + qs
                                redirections.append(sn + path + qs)

                        # Munge environment and try again.
                        method = "GET"
                        path = ir.path
                        qs = ir.query_string
                        rfile = BytesIO()

                send_response(req, response.output_status,
                              response.header_list, response.body,
                              response.stream)
            finally:
                app.release_serving()
    except:
        tb = format_exc()
        cherrypy.log(tb, 'MOD_PYTHON', severity=logging.ERROR)
        s, h, b = bare_error()
        send_response(req, s, h, b)
    return apache.OK
Ejemplo n.º 21
0
    def respond(self):
        req = self.req
        try:
            # Obtain a Request object from CherryPy
            local = req.server.bind_addr
            local = httputil.Host(local[0], local[1], "")
            remote = req.conn.remote_addr, req.conn.remote_port
            remote = httputil.Host(remote[0], remote[1], "")

            scheme = req.scheme
            sn = cherrypy.tree.script_name(req.uri or "/")
            if sn is None:
                self.send_response('404 Not Found', [], [''])
            else:
                app = cherrypy.tree.apps[sn]
                method = req.method
                path = req.path
                qs = req.qs or ""
                headers = list(req.inheaders.items())
                rfile = req.rfile
                prev = None

                try:
                    redirections = []
                    while True:
                        request, response = app.get_serving(
                            local, remote, scheme, "HTTP/1.1")
                        request.multithread = True
                        request.multiprocess = False
                        request.app = app
                        request.prev = prev

                        # Run the CherryPy Request object and obtain the response
                        try:
                            request.run(method, path, qs, req.request_protocol,
                                        headers, rfile)
                            break
                        except cherrypy.InternalRedirect:
                            ir = sys.exc_info()[1]
                            app.release_serving()
                            prev = request

                            if not self.recursive:
                                if ir.path in redirections:
                                    raise RuntimeError(
                                        "InternalRedirector visited the "
                                        "same URL twice: %r" % ir.path)
                                else:
                                    # Add the *previous* path_info + qs to redirections.
                                    if qs:
                                        qs = "?" + qs
                                    redirections.append(sn + path + qs)

                            # Munge environment and try again.
                            method = "GET"
                            path = ir.path
                            qs = ir.query_string
                            rfile = BytesIO()

                    self.send_response(response.output_status,
                                       response.header_list, response.body)
                finally:
                    app.release_serving()
        except:
            tb = format_exc()
            #print tb
            cherrypy.log(tb, 'NATIVE_ADAPTER', severity=logging.ERROR)
            s, h, b = bare_error()
            self.send_response(s, h, b)
Ejemplo n.º 22
0
def handler(req):
    global _isSetUp
    from mod_python import apache
    try:
        if not _isSetUp:
            setup(req)
            _isSetUp = True
        local = req.connection.local_addr
        local = httputil.Host(local[0], local[1], req.connection.local_host
                              or '')
        remote = req.connection.remote_addr
        remote = httputil.Host(remote[0], remote[1], req.connection.remote_host
                               or '')
        scheme = req.parsed_uri[0] or 'http'
        req.get_basic_auth_pw()
        try:
            q = apache.mpm_query
            threaded = q(apache.AP_MPMQ_IS_THREADED)
            forked = q(apache.AP_MPMQ_IS_FORKED)
        except AttributeError:
            bad_value = "You must provide a PythonOption '%s', either 'on' or 'off', when running a version of mod_python < 3.1"
            threaded = options.get('multithread', '').lower()
            if threaded == 'on':
                threaded = True
            elif threaded == 'off':
                threaded = False
            else:
                raise ValueError(bad_value % 'multithread')
            forked = options.get('multiprocess', '').lower()
            if forked == 'on':
                forked = True
            elif forked == 'off':
                forked = False
            else:
                raise ValueError(bad_value % 'multiprocess')

        sn = cherrypy.tree.script_name(req.uri or '/')
        if sn is None:
            send_response(req, '404 Not Found', [], '')
        else:
            app = cherrypy.tree.apps[sn]
            method = req.method
            path = req.uri
            qs = req.args or ''
            reqproto = req.protocol
            headers = copyitems(req.headers_in)
            rfile = _ReadOnlyRequest(req)
            prev = None
            try:
                redirections = []
                while True:
                    request, response = app.get_serving(
                        local, remote, scheme, 'HTTP/1.1')
                    request.login = req.user
                    request.multithread = bool(threaded)
                    request.multiprocess = bool(forked)
                    request.app = app
                    request.prev = prev
                    try:
                        request.run(method, path, qs, reqproto, headers, rfile)
                        break
                    except cherrypy.InternalRedirect:
                        ir = sys.exc_info()[1]
                        app.release_serving()
                        prev = request
                        if not recursive:
                            if ir.path in redirections:
                                raise RuntimeError(
                                    'InternalRedirector visited the same URL twice: %r'
                                    % ir.path)
                            else:
                                if qs:
                                    qs = '?' + qs
                                redirections.append(sn + path + qs)
                        method = 'GET'
                        path = ir.path
                        qs = ir.query_string
                        rfile = BytesIO()

                send_response(req, response.status, response.header_list,
                              response.body, response.stream)
            finally:
                app.release_serving()

    except:
        tb = format_exc()
        cherrypy.log(tb, 'MOD_PYTHON', severity=logging.ERROR)
        s, h, b = bare_error()
        send_response(req, s, h, b)

    return apache.OK
Ejemplo n.º 23
0
 def bytesio(self):
     f = BytesIO(ntob('Fee\nfie\nfo\nfum'))
     return static.serve_fileobj(f, content_type='text/plain')