Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Ticket #559: middleware.patch

  • __init__.py

    old new  
    2020from cherrypy import _cpserver 
    2121server = _cpserver.Server() 
    2222 
     23# FIXME move this class somewhere else and make it more useful and also change 
     24# its name 
     25class WSGIBox(object): 
     26    pass 
     27wsgi = WSGIBox() 
     28 
    2329def quickstart(root, script_name="", conf=None): 
    2430    """Mount the given app, start the engine and builtin server, then block.""" 
    2531    tree.mount(root, script_name, conf) 
  • _cptree.py

    old new  
     1import cherrypy 
    12from cherrypy import _cpconfig, _cplogging, _cpwsgi 
    23 
    34 
     
    1718    conf: a dict of {path: pathconf} pairs, where 'pathconf' is itself 
    1819        a dict of {key: value} pairs. 
    1920    """ 
    20      
     21 
    2122    def __init__(self, root, script_name="", conf=None): 
    2223        self.log = _cplogging.LogManager(id(self)) 
    2324        self.root = root 
    2425        self.script_name = script_name 
     26        self.middleware_pipeline = [] 
     27        self.middleware_config = {} 
    2528        self.namespaces = {"log": lambda k, v: setattr(self.log, k, v), 
     29                           "wsgi": self._set_wsgi, 
    2630                           } 
    2731        self.conf = {} 
    2832        if conf: 
    2933            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]) 
    3044     
    3145    def _get_script_name(self): 
    3246        if self._script_name is None: 
     
    3953    script_name = property(fget=_get_script_name, fset=_set_script_name) 
    4054     
    4155    def merge(self, conf): 
    42         """Merge the given config into self.config.""" 
     56        """Merge the given config into self.conf.""" 
    4357        _cpconfig.merge(self.conf, conf) 
    4458         
    4559        # Create log handlers as specified in config. 
     
    7286    def __call__(self, environ, start_response): 
    7387        return _cpwsgi._wsgi_callable(environ, start_response, app=self) 
    7488 
     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 
    7598 
    7699class Tree(object): 
    77100    """A registry of CherryPy applications, mounted at diverse points. 
     
    94117        # Next line both 1) strips trailing slash and 2) maps "/" -> "". 
    95118        script_name = script_name.rstrip("/") 
    96119        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) 
    97126        self.apps[script_name] = app 
    98127         
    99128        # If mounted at "", add favicon.ico 

Hosted by WebFaction

Log in as guest/cpguest to create tickets