Ticket #559: middleware.patch
-
__init__.py
old new 20 20 from cherrypy import _cpserver 21 21 server = _cpserver.Server() 22 22 23 # FIXME move this class somewhere else and make it more useful and also change 24 # its name 25 class WSGIBox(object): 26 pass 27 wsgi = WSGIBox() 28 23 29 def quickstart(root, script_name="", conf=None): 24 30 """Mount the given app, start the engine and builtin server, then block.""" 25 31 tree.mount(root, script_name, conf) -
_cptree.py
old new 1 import cherrypy 1 2 from cherrypy import _cpconfig, _cplogging, _cpwsgi 2 3 3 4 … … 17 18 conf: a dict of {path: pathconf} pairs, where 'pathconf' is itself 18 19 a dict of {key: value} pairs. 19 20 """ 20 21 21 22 def __init__(self, root, script_name="", conf=None): 22 23 self.log = _cplogging.LogManager(id(self)) 23 24 self.root = root 24 25 self.script_name = script_name 26 self.middleware_pipeline = [] 27 self.middleware_config = {} 25 28 self.namespaces = {"log": lambda k, v: setattr(self.log, k, v), 29 "wsgi": self._set_wsgi, 26 30 } 27 31 self.conf = {} 28 32 if conf: 29 33 self.merge(conf) 34 35 # configure the middleware pipeline to be 36 # later consumed by tree.mount (ugly :-() 37 wsgi = cherrypy.wsgi 38 global_config = cherrypy.config 39 mw_pipeline = self.middleware_pipeline 40 mw_config = self.middleware_config 41 for i, mw in enumerate(mw_pipeline): 42 mw_factory = getattr(wsgi, mw) 43 mw_pipeline[i] = mw_factory(**mw_config[mw]) 30 44 31 45 def _get_script_name(self): 32 46 if self._script_name is None: … … 39 53 script_name = property(fget=_get_script_name, fset=_set_script_name) 40 54 41 55 def merge(self, conf): 42 """Merge the given config into self.conf ig."""56 """Merge the given config into self.conf.""" 43 57 _cpconfig.merge(self.conf, conf) 44 58 45 59 # Create log handlers as specified in config. … … 72 86 def __call__(self, environ, start_response): 73 87 return _cpwsgi._wsgi_callable(environ, start_response, app=self) 74 88 89 def _set_wsgi(self, k, v): 90 if k == "middleware_pipeline": 91 self.middleware_pipeline = v 92 for mw_name in v: 93 self.middleware_config[mw_name] = {} 94 else: 95 mw_name, mw_arg = k.split(".", 1) 96 if mw_name in self.middleware_pipeline: 97 self.middleware_config[mw_name][mw_arg] = v 75 98 76 99 class Tree(object): 77 100 """A registry of CherryPy applications, mounted at diverse points. … … 94 117 # Next line both 1) strips trailing slash and 2) maps "/" -> "". 95 118 script_name = script_name.rstrip("/") 96 119 app = Application(root, script_name, conf) 120 # wrap the app using its middleware pipeline 121 # ideally this should happen transparently inside Application 122 # since it is responsible for it's config not mount 123 # ATM I haven't been able to move it there 124 for middleware in reversed(app.middleware_pipeline): 125 app = middleware(app) 97 126 self.apps[script_name] = app 98 127 99 128 # If mounted at "", add favicon.ico

