Changeset 1008
- Timestamp:
- 03/15/06 22:06:27
- Files:
-
- trunk/cherrypy/filters/__init__.py (modified) (1 diff)
- trunk/cherrypy/filters/wsgiappfilter.py (modified) (7 diffs)
- trunk/cherrypy/lib/cptools.py (modified) (2 diffs)
- trunk/cherrypy/test/test_wsgiapp_filter.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/filters/__init__.py
r1005 r1008 19 19 "cherrypy.filters.tidyfilter.TidyFilter", 20 20 "cherrypy.filters.xmlrpcfilter.XmlRpcFilter", 21 "cherrypy.filters.wsgiappfilter.WSGIAppFilter",22 21 ] 23 22 trunk/cherrypy/filters/wsgiappfilter.py
r1007 r1008 1 """a WSGI application filter for CherryPy""" 1 """a WSGI application filter for CherryPy 2 3 also see cherrypy.lib.cptools.WSGIApp""" 2 4 3 5 # by Christian Wyglendowski … … 7 9 import cherrypy 8 10 from cherrypy.filters.basefilter import BaseFilter 9 from cherrypy.lib import cptools10 11 from cherrypy._cputil import get_object_trail 11 12 13 class NoWSGIEnvironmentError(Exception):14 pass15 12 16 13 … … 54 51 """grabbed some of below from _cpwsgiserver.py 55 52 56 for compatibility with the 2.2beta release of CP53 for hosting WSGI apps in non-WSGI environments (yikes!) 57 54 """ 58 55 … … 92 89 """A filter for running any WSGI middleware/application within CP. 93 90 94 It can be used through the config system or by adding it to 95 a classes _cp_filters special attribute. Here are the parameters: 91 Here are the parameters: 96 92 97 wsgi_app (wsgiapp_filter.app) - any wsgi application callable 98 env_update (wsgiapp_filter.env_update) - a dictionary with 99 arbitrary keys and values to be merged with the WSGI 100 environment dictionary. 93 wsgi_app - any wsgi application callable 94 env_update - a dictionary with arbitrary keys and values to be 95 merged with the WSGI environment dictionary. 101 96 102 Examples: 103 104 # using _cp_filters 97 Example: 98 105 99 class Whatever: 106 100 _cp_filters = [WSGIAppFilter(some_app)] 107 108 # using a config file109 [/hosted/app]110 wsgiapp_filter.on = True111 wsgiapp_filter.app = 'package.module.wsgi_app_callable'112 wsgiapp_filter.env_update = {'running_in_cp':True}113 101 """ 114 102 115 def __init__(self, wsgi_app =None, env_update=None):103 def __init__(self, wsgi_app, env_update=None): 116 104 self.app = wsgi_app 117 105 self.env_update = env_update or {} 118 106 119 107 def before_request_body(self): 120 request = cherrypy.request121 enabled = cherrypy.config.get('wsgiapp_filter.on', False)122 request.wsgi_app = self.app123 request.wsgi_env_update = self.env_update124 request.wsgiapp_filter_on = enabled or bool(self.app)125 126 if not request.wsgiapp_filter_on:127 return128 129 if not request.wsgi_app:130 app = cherrypy.config.get('wsgiapp_filter.app', None)131 if app and isinstance(app, basestring):132 app = cptools.attributes(app)133 request.wsgi_app = app134 108 135 if not request.wsgi_env_update:136 env_update = cherrypy.config.get('wsgiapp_filter.env_update', {})137 request.wsgi_env_update = env_update138 139 109 # keep the request body intact so the wsgi app 140 110 # can have its way with it … … 146 116 147 117 request = cherrypy.request 148 149 if not request.wsgiapp_filter_on:150 return151 152 118 # if the static filter is on for this path and 153 119 # request.execute_main is False, assume that the … … 167 133 # update the environ with the dict passed to the filter's 168 134 # constructor 169 environ.update( request.wsgi_env_update)135 environ.update(self.env_update) 170 136 171 137 # run the wsgi app and have it set response.body 172 cherrypy.response.body = request.wsgi_app(environ, start_response)138 cherrypy.response.body = self.app(environ, start_response) 173 139 174 140 # tell CP not to handle the request further 175 141 request.execute_main = False 176 177 178 class WSGIApp(object):179 """a convenience class used to easily mount a wsgi app.180 181 example:182 cherrypy.root = SomeRoot()183 cherrypy.root.otherapp = WSGIApp(other_wsgi_app)184 """185 def __init__(self, app, env_update=None):186 self._cpFilterList = [WSGIAppFilter(app, env_update)]187 142 188 143 … … 209 164 index.exposed = True 210 165 166 class HostedWSGI(object): 167 _cp_filters = [WSGIAppFilter(my_app, {'cherrypy.wsgi':True,}),] 168 211 169 # mount standard CherryPy app 212 cherrypy. root = Root()170 cherrypy.tree.mount(Root(), '/') 213 171 # mount the WSGI app 214 cherrypy.root.app = WSGIApp(my_app, {'cherrypy.wsgi_filter':True}) 215 172 cherrypy.tree.mount(HostedWSGI(), '/app') 216 173 217 174 cherrypy.server.start() trunk/cherrypy/lib/cptools.py
r974 r1008 14 14 import cherrypy 15 15 import httptools 16 17 from cherrypy.filters.wsgiappfilter import WSGIAppFilter 16 18 17 19 … … 234 236 235 237 238 class WSGIApp(object): 239 """a convenience class that uses the WSGIAppFilter 240 241 to easily add a WSGI application to the CP object tree. 242 243 example: 244 cherrypy.tree.mount(SomeRoot(), '/') 245 cherrypy.tree.mount(WSGIApp(other_wsgi_app), '/ext_app') 246 """ 247 def __init__(self, app, env_update=None): 248 self._cpFilterList = [WSGIAppFilter(app, env_update)] 249 250 236 251 # public domain "unrepr" implementation, found on the web and then improved. 237 252 import compiler trunk/cherrypy/test/test_wsgiapp_filter.py
r1005 r1008 2 2 test.prefer_parent_path() 3 3 4 import os 5 curdir = os.path.join(os.getcwd(), os.path.dirname(__file__)) 6 4 7 import cherrypy 5 from cherrypy.filters.wsgiappfilter import WSGIAppFilter, WSGIApp 8 from cherrypy.filters.wsgiappfilter import WSGIAppFilter 9 from cherrypy.lib.cptools import WSGIApp 6 10 7 11 def test_app(environ, start_response): … … 39 43 'server.socket_port': helper.CPWebCase.PORT, 40 44 }, 41 '/xmlrpc': {'xmlrpc_filter.on':True}, 42 '/hosted/app2': {'wsgiapp_filter.on':True, 43 'wsgiapp_filter.app': test_app, 44 'wsgiapp_filter.env_update': {'cp.hosted':True}, 45 }, 45 '/hosted/app0/static' : { 46 'static_filter.on':True, 47 'static_filter.root': curdir, 48 'static_filter.dir': 'static', 49 }, 50 46 51 }) 47 52 … … 66 71 self.assertInBody(self.wsgi_output) 67 72 68 def test_04_ from_config_prog(self):69 self.getPage("/hosted/app 2")70 self.assert Header("Content-Type", "text/plain")71 self.assert InBody(self.wsgi_output)72 self.assert InBody("cp.hosted")73 73 def test_04_static_subdir(self): 74 self.getPage("/hosted/app0/static/index.html") 75 self.assertStatus('200 OK') 76 self.assertHeader('Content-Type', 'text/html') 77 self.assertBody('Hello, world\r\n') 78 74 79 if __name__ == '__main__': 75 80 from cherrypy import _cpwsgi

