Commits

Robert Brewer committed 7fd8e8c

WSGI improvements from Christian's cp3-wsgi-remix branch:

1. cherrypy.Application is now a WSGI callable.
2. cherrypy.tree may now be used as a WSGI dispatcher by simply calling it. This replaces the _cpwsgi.wsgiApp function.
3. You may now mount arbitrary WSGI apps using tree.graft(wsgi callable, script name).
4. If you set an Application object's script_name to None, it will try to pull script_name from cherrypy.request.wsgi_environ['SCRIPT_NAME'].

There's a new test_wsgiapps.py (copied from the branch) which helps demonstrate the changes.

Comments (0)

Files changed (9)

cherrypy/__init__.py

 
 import _cptree
 tree = _cptree.Tree()
+from _cptree import Application
 import _cpengine
 engine = _cpengine.Engine()
 import _cpserver

cherrypy/_cptree.py

 import logging
 import sys
 
-from cherrypy import config
+import cherrypy
+from cherrypy import config, _cpwsgi
 
 
 class Application:
-    """A CherryPy Application."""
+    """A CherryPy Application.
+    
+    An instance of this class may also be used as a WSGI callable
+    (WSGI application object) for itself.
+    
+    root: the top-most container of page handlers for this app.
+    script_name: the URL "mount point" for this app; for example,
+        if script_name is "/my/cool/app", then the URL
+        "http://my.domain.tld/my/cool/app/page1" might be handled
+        by a "page1" method on the root object. If script_name is
+        explicitly set to None, then CherryPy will attempt to provide
+        it each time from request.wsgi_environ['SCRIPT_NAME'].
+    conf: a dict of {path: pathconf} pairs, where 'pathconf' is itself
+        a dict of {key: value} pairs.
+    """
     
     def __init__(self, root, script_name="", conf=None):
         self.access_log = log = logging.getLogger("cherrypy.access.%s" % id(self))
         if conf:
             self.merge(conf)
     
+    def _get_script_name(self):
+        if self._script_name is None:
+            # None signals that the script name should be pulled from WSGI environ.
+            return cherrypy.request.wsgi_environ['SCRIPT_NAME']
+        return self._script_name
+    def _set_script_name(self, value):
+        self._script_name = value
+    script_name = property(_get_script_name, _set_script_name)
+    
     def merge(self, conf):
         """Merge the given config into self.config."""
         config.merge(self.conf, conf)
         if port != 80:
             host += ":%s" % port
         return scheme + host + self.script_name
+    
+    def __call__(self, environ, start_response):
+        return _cpwsgi._wsgi_callable(environ, start_response, app=self)
 
 
 class Tree:
-    """A registry of CherryPy applications, mounted at diverse points."""
+    """A registry of CherryPy applications, mounted at diverse points.
+    
+    An instance of this class may also be used as a WSGI callable
+    (WSGI application object), in which case it dispatches to all
+    mounted apps.
+    """
     
     def __init__(self):
         self.apps = {}
         
         return app
     
+    def graft(self, wsgi_callable, script_name=""):
+        """Mount a wsgi callable at the given script_name."""
+        # Next line both 1) strips trailing slash and 2) maps "/" -> "".
+        script_name = script_name.rstrip("/")
+        self.apps[script_name] = wsgi_callable
+    
     def script_name(self, path=None):
         """The script_name of the app at the given path, or None.
         
         
         from cherrypy.lib import http
         return http.urljoin(script_name, path)
+    
+    def __call__(self, environ, start_response):
+        return _cpwsgi._wsgi_callable(environ, start_response)
 

cherrypy/_cpwsgi.py

                 chunk = chunk.encode("ISO-8859-1")
             yield chunk
 
-def wsgiApp(environ, start_response):
-    """The WSGI 'application object' for CherryPy.
-    
-    Use this as the same WSGI callable for all your CP apps.
-    """
-    return _wsgi_callable(environ, start_response)
-
-def make_app(app):
-    """Factory for making separate WSGI 'application objects' for each CP app.
-    
-    Example:
-        # 'app' will be a CherryPy application object
-        app = cherrypy.tree.mount(Root(), "/", localconf)
-        
-        # 'wsgi_app' will be a WSGI application
-        wsgi_app = _cpwsgi.make_app(app)
-    """
-    def single_app(environ, start_response):
-        return _wsgi_callable(environ, start_response, app)
-    return single_app
-
-
 
 #                            Server components                            #
 
             bind_addr = (conf('server.socket_host'),
                          conf('server.socket_port'))
         
-        apps = [(base, wsgiApp) for base in cherrypy.tree.apps]
-        
         s = _cpwsgiserver.CherryPyWSGIServer
-        s.__init__(self, bind_addr, apps,
+        s.__init__(self, bind_addr, cherrypy.tree,
                    conf('server.thread_pool'),
                    conf('server.socket_host'),
                    request_queue_size = conf('server.socket_queue_size'),

cherrypy/test/benchmark.py

     SetHandler python-program
     PythonFixupHandler cherrypy.test.benchmark::startup_modpython
     PythonHandler modpython_gateway::handler
-    PythonOption wsgi.application cherrypy._cpwsgi::wsgiApp
+    PythonOption wsgi.application cherrypy::tree
     PythonDebug On
 %s%s
 </Location>

cherrypy/test/helper.py

     cherrypy.engine.start_with_callback(_run_test_suite_thread,
                                         args=(moduleNames, conf))
 
+def sync_apps(profile=False):
+    apps = []
+    for base, app in cherrypy.tree.apps.iteritems():
+        if base == "/":
+            base = ""
+        if profile:
+            apps.append((base, profiler.make_app(app, aggregate=False)))
+        else:
+            apps.append((base, app))
+    apps.sort()
+    apps.reverse()
+    for s in cherrypy.server.httpservers:
+        s.mount_points = apps
+
 def _run_test_suite_thread(moduleNames, conf):
-    from cherrypy import _cpwsgi
     for testmod in moduleNames:
         # Must run each module in a separate suite,
         # because each module uses/overwrites cherrypy globals.
         
         # The setup functions probably mounted new apps.
         # Tell our server about them.
-        apps = []
-        for base, app in cherrypy.tree.apps.iteritems():
-            if base == "/":
-                base = ""
-            if conf.get("profiling.on", False):
-                apps.append((base, profiler.make_app(_cpwsgi.wsgiApp)))
-##                apps.append((base, profiler.make_app(_cpwsgi.wsgiApp, aggregate=True)))
-            else:
-                apps.append((base, _cpwsgi.wsgiApp))
-##            # We could use the following line, but it breaks test_tutorials
-##            apps.append((base, _cpwsgi.make_app(app)))
-        apps.sort()
-        apps.reverse()
-        for s in cherrypy.server.httpservers:
-            s.mount_points = apps
+        sync_apps(profile=conf.get("profiling.on", False))
         
         suite = CPTestLoader.loadTestsFromName(testmod)
         CPTestRunner.run(suite)

cherrypy/test/modpy.py

 PythonFixupHandler cherrypy.test.modpy::wsgisetup
 PythonOption testmod %s
 PythonHandler modpython_gateway::handler
-PythonOption wsgi.application cherrypy._cpwsgi::wsgiApp
+PythonOption wsgi.application cherrypy::tree
 PythonDebug On
 """
 

cherrypy/test/test_response_headers.py

-import test
-test.prefer_parent_path()
-
-import cherrypy
-from cherrypy import tools
-
-
-def setup_server():
-    class Root:
-        def index(self):
-            yield "Hello, world"
-        index.exposed = True
-        h = [("Content-Language", "en-GB"), ('Content-Type', 'text/plain')]
-        tools.response_headers(headers=h)(index)
-        
-        def other(self):
-            return "salut"
-        other.exposed = True
-        other._cp_config = {
-            'tools.response_headers.on': True,
-            'tools.response_headers.headers': [("Content-Language", "fr"),
-                                               ('Content-Type', 'text/plain')],
-            }
-    
-    cherrypy.tree.mount(Root())
-    cherrypy.config.update({
-            'log_to_screen': False,
-            'environment': 'production',
-    })
-
-
-import helper
-
-class ResponseHeadersTest(helper.CPWebCase):
-
-    def testResponseHeadersDecorator(self):
-        self.getPage('/')
-        self.assertHeader("Content-Language", "en-GB")
-        self.assertHeader('Content-Type', 'text/plain')
-
-    def testResponseHeaders(self):
-        self.getPage('/other')
-        self.assertHeader("Content-Language", "fr")
-        self.assertHeader('Content-Type', 'text/plain')
-
-if __name__ == "__main__":
-    setup_server()
-    helper.testmain()
+import test
+test.prefer_parent_path()
+
+import cherrypy
+from cherrypy import tools
+
+
+def setup_server():
+    class Root:
+        def index(self):
+            yield "Hello, world"
+        index.exposed = True
+        h = [("Content-Language", "en-GB"), ('Content-Type', 'text/plain')]
+        tools.response_headers(headers=h)(index)
+        
+        def other(self):
+            return "salut"
+        other.exposed = True
+        other._cp_config = {
+            'tools.response_headers.on': True,
+            'tools.response_headers.headers': [("Content-Language", "fr"),
+                                               ('Content-Type', 'text/plain')],
+            }
+    
+    cherrypy.tree.mount(Root())
+    cherrypy.config.update({
+            'log_to_screen': False,
+            'environment': 'production',
+    })
+
+
+import helper
+
+class ResponseHeadersTest(helper.CPWebCase):
+
+    def testResponseHeadersDecorator(self):
+        self.getPage('/')
+        self.assertHeader("Content-Language", "en-GB")
+        self.assertHeader('Content-Type', 'text/plain')
+
+    def testResponseHeaders(self):
+        self.getPage('/other')
+        self.assertHeader("Content-Language", "fr")
+        self.assertHeader('Content-Type', 'text/plain')
+
+if __name__ == "__main__":
+    setup_server()
+    helper.testmain()

cherrypy/test/test_tutorials.py

         app.root.load_tut_module = load_tut_module
         app.root.sessions = sessions
         app.root.traceback_setting = traceback_setting
+        
+        helper.sync_apps()
     load_tut_module.exposed = True
     
     def sessions():

cherrypy/test/test_wsgiapps.py

+import test
+test.prefer_parent_path()
+
+
+def setup_server():
+    import os
+    curdir = os.path.join(os.getcwd(), os.path.dirname(__file__))
+    
+    import cherrypy
+    
+    def test_app(environ, start_response):
+        status = '200 OK'
+        response_headers = [('Content-type', 'text/plain')]
+        start_response(status, response_headers)
+        yield 'Hello, world!\n'
+        yield 'This is a wsgi app running within CherryPy!\n\n'
+        keys = environ.keys()
+        keys.sort()
+        for k in keys:
+            yield '%s: %s\n' % (k,environ[k])
+
+    def reversing_middleware(app):
+        def _app(environ, start_response):
+            results = app(environ, start_response)
+            if not isinstance(results, basestring):
+                results = "".join(results)
+            results = list(results)
+            results.reverse()
+            return "".join(results)
+        return _app
+    
+    class Root:
+        def index(self):
+            return "I'm a regular CherryPy page handler!"
+        index.exposed = True
+    
+    
+    class HostedWSGI(object):
+        _cp_config = {'tools.wsgiapp.on': True,
+                      'tools.wsgiapp.app': test_app,
+                      }
+    
+    cherrypy.config.update({'log_to_screen': False,
+                            'environment': 'production',
+                            'show_tracebacks': True,
+                            })
+    cherrypy.tree.mount(Root())
+    
+    conf0 = {'/static': {'tools.staticdir.on': True,
+                         'tools.staticdir.root': curdir,
+                         'tools.staticdir.dir': 'static',
+                         }}
+    cherrypy.tree.mount(HostedWSGI(), '/hosted/app0', conf0)
+    cherrypy.tree.graft(test_app, '/hosted/app1')
+    
+    app = cherrypy.Application(Root(), None)
+    cherrypy.tree.graft(reversing_middleware(app), '/hosted/app2')
+
+import helper
+
+
+class WSGIAppTest(helper.CPWebCase):
+    
+    wsgi_output = '''Hello, world!
+This is a wsgi app running within CherryPy!'''
+
+    def test_01_standard_app(self):
+        self.getPage("/")
+        self.assertBody("I'm a regular CherryPy page handler!")
+    
+    def test_02_wrapped_wsgi(self):
+        self.getPage("/hosted/app0")
+        self.assertHeader("Content-Type", "text/plain")
+        self.assertInBody(self.wsgi_output)
+    
+    def test_03_static_subdir(self):
+        self.getPage("/hosted/app0/static/index.html")
+        self.assertStatus('200 OK')
+        self.assertHeader('Content-Type', 'text/html')
+        self.assertBody('Hello, world\r\n')
+    
+    def test_04_pure_wsgi(self):
+        self.getPage("/hosted/app1")
+        self.assertHeader("Content-Type", "text/plain")
+        self.assertInBody(self.wsgi_output)
+
+    def test_05_wrapped_cp_app(self):
+        self.getPage("/hosted/app2/")
+        body = list("I'm a regular CherryPy page handler!")
+        body.reverse()
+        body = "".join(body)
+        self.assertInBody(body)
+
+if __name__ == '__main__':
+    setup_server()
+    helper.testmain()
+