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

root/branches/cherrypy-2.x/cherrypy/filters/__init__.py

Revision 1008 (checked in by dowski, 3 years ago)

As per the discussion on cherrypy-devel,
the WSGIAppFilter is no longer configurable through the CP config
system. You should either add an instance of
cherrypy.filters.wsgiappfilter.WSGIAppFilter to your _cp_filters
list or use the cherrypy.lib.cptools.WSGIApp convenience class.

Also tweaked the test for wsgiappfilter to make sure that static
content higher than a WSGI application on the tree gets served
correctly.

  • Property svn:eol-style set to native
Line 
1 import cherrypy
2 from cherrypy import _cputil
3
4
5 # These are in order for a reason!
6 # Entries in the input_filters and output_filters lists
7 # may be either a class, or the full package name of a class.
8
9 input_filters = [
10     "cherrypy.filters.cachefilter.CacheFilter",
11     "cherrypy.filters.logdebuginfofilter.LogDebugInfoFilter",
12     "cherrypy.filters.baseurlfilter.BaseUrlFilter",
13     "cherrypy.filters.virtualhostfilter.VirtualHostFilter",
14     "cherrypy.filters.decodingfilter.DecodingFilter",
15     "cherrypy.filters.sessionfilter.SessionFilter",
16     "cherrypy.filters.sessionauthenticatefilter.SessionAuthenticateFilter",
17     "cherrypy.filters.staticfilter.StaticFilter",
18     "cherrypy.filters.nsgmlsfilter.NsgmlsFilter",
19     "cherrypy.filters.tidyfilter.TidyFilter",
20     "cherrypy.filters.xmlrpcfilter.XmlRpcFilter",
21 ]
22
23 output_filters = [
24     "cherrypy.filters.responseheadersfilter.ResponseHeadersFilter",
25     "cherrypy.filters.xmlrpcfilter.XmlRpcFilter",
26     "cherrypy.filters.encodingfilter.EncodingFilter",
27     "cherrypy.filters.tidyfilter.TidyFilter",
28     "cherrypy.filters.nsgmlsfilter.NsgmlsFilter",
29     "cherrypy.filters.logdebuginfofilter.LogDebugInfoFilter",
30     "cherrypy.filters.gzipfilter.GzipFilter",
31     "cherrypy.filters.sessionfilter.SessionFilter",
32     "cherrypy.filters.cachefilter.CacheFilter",
33 ]
34
35 _input_methods = ['on_start_resource', 'before_request_body', 'before_main']
36 _output_methods = ['before_finalize', 'on_end_resource', 'on_end_request',
37         'before_error_response', 'after_error_response']
38
39 _old_input_methods = ['onStartResource', 'beforeRequestBody', 'beforeMain']
40 _old_output_methods = ['beforeFinalize', 'onEndResource', 'onEndRequest',
41         'beforeErrorResponse', 'afterErrorResponse']
42
43 backward_compatibility_dict = {
44     'on_start_resource': 'onStartResource',
45     'before_request_body': 'beforeRequestBody',
46     'before_main': 'beforeMain',
47     'before_finalize': 'beforeFinalize',
48     'on_end_resource': 'onEndResource',
49     'on_end_request': 'onEndRequest',
50     'before_error_response': 'beforeErrorResponse',
51     'after_error_response': 'afterErrorResponse'
52 }
53
54
55 def init():
56     """Initialize the filters."""
57     from cherrypy.lib import cptools
58    
59     instances = {}
60     inputs, outputs = [], []
61    
62     conf = cherrypy.config.get
63    
64     for filtercls in input_filters + conf('server.input_filters', []):
65         if isinstance(filtercls, basestring):
66             filtercls = cptools.attributes(filtercls)
67        
68         f = instances.get(filtercls)
69         if f is None:
70             f = instances[filtercls] = filtercls()
71         inputs.append(f)
72    
73     for filtercls in conf('server.output_filters', []) + output_filters:
74         if isinstance(filtercls, basestring):
75             filtercls = cptools.attributes(filtercls)
76        
77         f = instances.get(filtercls)
78         if f is None:
79             f = instances[filtercls] = filtercls()
80         outputs.append(f)
81    
82     # Transform the instance lists into a dict of methods
83     # in 2.2 we check the old camelCase filter names first
84     # to provide backward compatibility with 2.1
85     _filterhooks.clear()
86     for old_name, new_name in zip(_old_input_methods, _input_methods):
87         _filterhooks[new_name] = []
88         for f in inputs:
89             method = getattr(f, old_name, None)
90             if method:
91                 _filterhooks[new_name].append(method)
92             else:
93                 method = getattr(f, new_name, None)
94                 if method:
95                     _filterhooks[new_name].append(method)
96     for old_name, new_name in zip(_old_output_methods, _output_methods):
97         _filterhooks[new_name] = []
98         for f in outputs:
99             method = getattr(f, old_name, None)
100             if method:
101                 _filterhooks[new_name].append(method)
102             else:
103                 method = getattr(f, new_name, None)
104                 if method:
105                     _filterhooks[new_name].append(method)
106
107
108 _filterhooks = {}
109
110
111 def applyFilters(method_name, failsafe=False):
112     """Execute the given method for all registered filters."""
113     special_methods = []
114     for f in _cputil.get_special_attribute("_cp_filters", "_cpFilterList"):
115         if cherrypy.lowercase_api is False:
116             # Try old name first
117             old_method_name = backward_compatibility_dict.get(method_name)
118             method = getattr(f, old_method_name, None)
119             if (method is None):
120                 method = getattr(f, method_name, None)
121             if method:
122                 special_methods.append(method)
123         else:
124             # We know for sure that user uses the new lowercase API
125             method = getattr(f, method_name, None)
126             if method:
127                 special_methods.append(method)
128    
129     if method_name in _input_methods:
130         # Run special filters after defaults.
131         methods = _filterhooks[method_name] + special_methods
132     else:
133         # Run special filters before defaults.
134         methods = special_methods + _filterhooks[method_name]
135
136     for method in methods:
137         # The on_start_resource, on_end_resource, and on_end_request methods
138         # are guaranteed to run even if other methods of the same name fail.
139         # We will still log the failure, but proceed on to the next method.
140         # The only way to stop all processing from one of these methods is
141         # to raise SystemExit and stop the whole server. So, trap your own
142         # errors in these methods!
143         if failsafe:
144             try:
145                 method()
146             except (KeyboardInterrupt, SystemExit):
147                 raise
148             except:
149                 cherrypy.log(traceback=True)
150         else:
151             method()
152
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets