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

Changeset 891

Show
Ignore:
Timestamp:
12/28/05 21:13:13
Author:
fumanchu
Message:

Fix for #394 (custom filters). Well, it fixes a related problem, anyway. ;) The real fix for #394 is "don't do that".

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/filters/__init__.py

    r883 r891  
    22from cherrypy import _cputil 
    33 
     4 
    45# These are in order for a reason! 
    5 # They must be strings matching keys in classMap 
    66_input_order = [ 
    7     'CacheFilter'
    8     'LogDebugInfoFilter'
    9     'BaseUrlFilter'
    10     'VirtualHostFilter'
    11     'DecodingFilter'
    12     'SessionFilter'
    13     'SessionAuthenticateFilter'
    14     'StaticFilter'
    15     'NsgmlsFilter'
    16     'TidyFilter'
    17     'XmlRpcFilter'
     7    "cherrypy.filters.cachefilter.CacheFilter"
     8    "cherrypy.filters.logdebuginfofilter.LogDebugInfoFilter"
     9    "cherrypy.filters.baseurlfilter.BaseUrlFilter"
     10    "cherrypy.filters.virtualhostfilter.VirtualHostFilter"
     11    "cherrypy.filters.decodingfilter.DecodingFilter"
     12    "cherrypy.filters.sessionfilter.SessionFilter"
     13    "cherrypy.filters.sessionauthenticatefilter.SessionAuthenticateFilter"
     14    "cherrypy.filters.staticfilter.StaticFilter"
     15    "cherrypy.filters.nsgmlsfilter.NsgmlsFilter"
     16    "cherrypy.filters.tidyfilter.TidyFilter"
     17    "cherrypy.filters.xmlrpcfilter.XmlRpcFilter"
    1818] 
    1919 
    2020_output_order = [ 
    21     'ResponseHeadersFilter'
    22     'XmlRpcFilter'
    23     'EncodingFilter'
    24     'TidyFilter'
    25     'NsgmlsFilter'
    26     'LogDebugInfoFilter'
    27     'GzipFilter'
    28     'SessionFilter'
    29     'CacheFilter'
     21    "cherrypy.filters.responseheadersfilter.ResponseHeadersFilter"
     22    "cherrypy.filters.xmlrpcfilter.XmlRpcFilter"
     23    "cherrypy.filters.encodingfilter.EncodingFilter"
     24    "cherrypy.filters.tidyfilter.TidyFilter"
     25    "cherrypy.filters.nsgmlsfilter.NsgmlsFilter"
     26    "cherrypy.filters.logdebuginfofilter.LogDebugInfoFilter"
     27    "cherrypy.filters.gzipfilter.GzipFilter"
     28    "cherrypy.filters.sessionfilter.SessionFilter"
     29    "cherrypy.filters.cachefilter.CacheFilter"
    3030] 
    3131 
     
    4545} 
    4646 
     47 
    4748def init(): 
    4849    """Initialize the filters.""" 
    49      
    50     from cherrypy.filters import baseurlfilter, cachefilter, \ 
    51         decodingfilter, encodingfilter, gzipfilter, logdebuginfofilter, \ 
    52         staticfilter, nsgmlsfilter, tidyfilter, xmlrpcfilter, \ 
    53         sessionauthenticatefilter, sessionfilter, \ 
    54         responseheadersfilter, virtualhostfilter 
    55      
    56     classMap = { 
    57         'BaseUrlFilter'      : baseurlfilter.BaseUrlFilter, 
    58         'CacheFilter'        : cachefilter.CacheFilter, 
    59         'DecodingFilter'     : decodingfilter.DecodingFilter, 
    60         'EncodingFilter'     : encodingfilter.EncodingFilter, 
    61         'GzipFilter'         : gzipfilter.GzipFilter, 
    62         'LogDebugInfoFilter' : logdebuginfofilter.LogDebugInfoFilter, 
    63         'NsgmlsFilter'       : nsgmlsfilter.NsgmlsFilter, 
    64         'ResponseHeadersFilter': 
    65                     responseheadersfilter.ResponseHeadersFilter, 
    66         'SessionAuthenticateFilter': 
    67                     sessionauthenticatefilter.SessionAuthenticateFilter, 
    68         'SessionFilter'      : sessionfilter.SessionFilter, 
    69         'StaticFilter'       : staticfilter.StaticFilter, 
    70         'TidyFilter'         : tidyfilter.TidyFilter, 
    71         'VirtualHostFilter'  : virtualhostfilter.VirtualHostFilter, 
    72         'XmlRpcFilter'       : xmlrpcfilter.XmlRpcFilter, 
    73     } 
     50    from cherrypy.lib import cptools 
    7451     
    7552    instances = {} 
     
    7855    conf = cherrypy.config.get 
    7956     
    80     for name in _input_order + conf('server.inputFilters', []): 
    81         f = instances.get(name) 
     57    for clsname in _input_order + conf('server.inputFilters', []): 
     58        f = instances.get(clsname) 
    8259        if f is None: 
    83             f = instances[name] = classMap[name]() 
     60            cls = cptools.attributes(clsname) 
     61            f = instances[clsname] = cls() 
    8462        inputs.append(f) 
    8563     
    86     for name in conf('server.outputFilters', []) + _output_order: 
    87         f = instances.get(name) 
     64    for clsname in conf('server.outputFilters', []) + _output_order: 
     65        f = instances.get(clsname) 
    8866        if f is None: 
    89             f = instances[name] = classMap[name]() 
     67            cls = cptools.attributes(clsname) 
     68            f = instances[clsname] = cls() 
    9069        outputs.append(f) 
    9170     
  • trunk/cherrypy/test/test.py

    r884 r891  
    318318        'test_config', 
    319319        'test_core', 
     320        'test_custom_filters', 
    320321        'test_decodingencoding_filter', 
    321322        'test_gzip_filter', 
  • trunk/cherrypy/test/test_core.py

    r884 r891  
    326326 
    327327 
    328 class NadsatFilter: 
    329      
    330     def before_finalize(self): 
    331         self.ended = False 
    332         def nadsat_it_up(body): 
    333             for chunk in body: 
    334                 chunk = chunk.replace("good", "horrorshow") 
    335                 chunk = chunk.replace("piece", "lomtick") 
    336                 yield chunk 
    337         cherrypy.response.body = nadsat_it_up(cherrypy.response.body) 
    338      
    339     def on_end_request(self): 
    340         # This runs after the request has been completely written out. 
    341         cherrypy.response.body = "razdrez" 
    342         self.ended = True 
    343  
    344 _nf = NadsatFilter() 
    345  
    346 class CPFilterList(Test): 
    347      
    348     _cp_filters = [_nf] 
    349      
    350     def index(self): 
    351         return "A good piece of cherry pie" 
    352      
    353     def err(self): 
    354         raise ValueError() 
    355      
    356     def errinstream(self): 
    357         raise ValueError() 
    358         yield "confidential" 
    359  
    360  
    361328log_file = os.path.join(localDir, "error.log") 
    362329log_access_file = os.path.join(localDir, "access.log") 
     
    399366    '/error/rethrow': { 
    400367        'server.throw_errors': True, 
    401     }, 
    402     '/cpfilterlist/errinstream': { 
    403         'streamResponse': True, 
    404368    }, 
    405369}) 
     
    610574        else: 
    611575            self.assertBody("(['http://127.0.0.1:8000/'], 302)") 
    612      
    613     def testCPFilterList(self): 
    614         self.getPage("/cpfilterlist/") 
    615         # If body is "razdrez", then on_end_request is being called too early. 
    616         self.assertBody("A horrorshow lomtick of cherry pie") 
    617         # If this fails, then on_end_request isn't being called at all. 
    618         self.assertEqual(_nf.ended, True) 
    619          
    620         ignore = helper.webtest.ignored_exceptions 
    621         ignore.append(ValueError) 
    622         try: 
    623             valerr = '\n    raise ValueError()\nValueError' 
    624             self.getPage("/cpfilterlist/err") 
    625             # If body is "razdrez", then on_end_request is being called too early. 
    626             self.assertErrorPage(500, pattern=valerr) 
    627             # If this fails, then on_end_request isn't being called at all. 
    628             self.assertEqual(_nf.ended, True) 
    629              
    630             # If body is "razdrez", then on_end_request is being called too early. 
    631             if cherrypy.server.httpserver is None: 
    632                 self.assertRaises(ValueError, self.getPage, 
    633                                   "/cpfilterlist/errinstream") 
    634             else: 
    635                 self.getPage("/cpfilterlist/errinstream") 
    636                 # Because this error is raised after the response body has 
    637                 # started, the status should not change to an error status. 
    638                 self.assertStatus("200 OK") 
    639                 self.assertBody("Unrecoverable error in the server.") 
    640             # If this fails, then on_end_request isn't being called at all. 
    641             self.assertEqual(_nf.ended, True) 
    642         finally: 
    643             ignore.pop() 
    644576     
    645577    def testFlatten(self): 

Hosted by WebFaction

Log in as guest/cpguest to create tickets