| 1 |
import cherrypy |
|---|
| 2 |
from cherrypy import _cputil |
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 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 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 125 |
method = getattr(f, method_name, None) |
|---|
| 126 |
if method: |
|---|
| 127 |
special_methods.append(method) |
|---|
| 128 |
|
|---|
| 129 |
if method_name in _input_methods: |
|---|
| 130 |
|
|---|
| 131 |
methods = _filterhooks[method_name] + special_methods |
|---|
| 132 |
else: |
|---|
| 133 |
|
|---|
| 134 |
methods = special_methods + _filterhooks[method_name] |
|---|
| 135 |
|
|---|
| 136 |
for method in methods: |
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 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 |
|
|---|