2014-03-10 01:18:05 -04:00
|
|
|
"""CherryPy tools. A "tool" is any helper, adapted to CP.
|
|
|
|
|
|
|
|
Tools are usually designed to be used in a variety of ways (although some
|
|
|
|
may only offer one if they choose):
|
2014-06-04 21:28:59 -04:00
|
|
|
|
|
|
|
Library calls
|
2014-03-10 01:18:05 -04:00
|
|
|
All tools are callables that can be used wherever needed.
|
|
|
|
The arguments are straightforward and should be detailed within the
|
|
|
|
docstring.
|
2014-06-04 21:28:59 -04:00
|
|
|
|
|
|
|
Function decorators
|
2014-03-10 01:18:05 -04:00
|
|
|
All tools, when called, may be used as decorators which configure
|
|
|
|
individual CherryPy page handlers (methods on the CherryPy tree).
|
|
|
|
That is, "@tools.anytool()" should "turn on" the tool via the
|
|
|
|
decorated function's _cp_config attribute.
|
2014-06-04 21:28:59 -04:00
|
|
|
|
|
|
|
CherryPy config
|
2014-03-10 01:18:05 -04:00
|
|
|
If a tool exposes a "_setup" callable, it will be called
|
|
|
|
once per Request (if the feature is "turned on" via config).
|
|
|
|
|
|
|
|
Tools may be implemented as any object with a namespace. The builtins
|
|
|
|
are generally either modules or instances of the tools.Tool class.
|
|
|
|
"""
|
|
|
|
|
2014-06-04 21:28:59 -04:00
|
|
|
import sys
|
2014-03-10 01:18:05 -04:00
|
|
|
import warnings
|
|
|
|
|
2014-06-04 21:28:59 -04:00
|
|
|
import cherrypy
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
def _getargs(func):
|
|
|
|
"""Return the names of all static arguments to the given function."""
|
|
|
|
# Use this instead of importing inspect for less mem overhead.
|
|
|
|
import types
|
2014-06-04 21:28:59 -04:00
|
|
|
if sys.version_info >= (3, 0):
|
|
|
|
if isinstance(func, types.MethodType):
|
|
|
|
func = func.__func__
|
|
|
|
co = func.__code__
|
|
|
|
else:
|
|
|
|
if isinstance(func, types.MethodType):
|
|
|
|
func = func.im_func
|
|
|
|
co = func.func_code
|
2014-03-10 01:18:05 -04:00
|
|
|
return co.co_varnames[:co.co_argcount]
|
|
|
|
|
|
|
|
|
2014-06-04 21:28:59 -04:00
|
|
|
_attr_error = (
|
|
|
|
"CherryPy Tools cannot be turned on directly. Instead, turn them "
|
|
|
|
"on via config, or use them as decorators on your page handlers."
|
|
|
|
)
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
class Tool(object):
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
"""A registered function for use with CherryPy request-processing hooks.
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
help(tool.callable) should give you more information about this Tool.
|
|
|
|
"""
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
namespace = "tools"
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def __init__(self, point, callable, name=None, priority=50):
|
|
|
|
self._point = point
|
|
|
|
self.callable = callable
|
|
|
|
self._name = name
|
|
|
|
self._priority = priority
|
|
|
|
self.__doc__ = self.callable.__doc__
|
|
|
|
self._setargs()
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def _get_on(self):
|
|
|
|
raise AttributeError(_attr_error)
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def _set_on(self, value):
|
|
|
|
raise AttributeError(_attr_error)
|
|
|
|
on = property(_get_on, _set_on)
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def _setargs(self):
|
|
|
|
"""Copy func parameter names to obj attributes."""
|
|
|
|
try:
|
|
|
|
for arg in _getargs(self.callable):
|
|
|
|
setattr(self, arg, None)
|
|
|
|
except (TypeError, AttributeError):
|
|
|
|
if hasattr(self.callable, "__call__"):
|
|
|
|
for arg in _getargs(self.callable.__call__):
|
|
|
|
setattr(self, arg, None)
|
|
|
|
# IronPython 1.0 raises NotImplementedError because
|
|
|
|
# inspect.getargspec tries to access Python bytecode
|
|
|
|
# in co_code attribute.
|
|
|
|
except NotImplementedError:
|
|
|
|
pass
|
|
|
|
# IronPython 1B1 may raise IndexError in some cases,
|
|
|
|
# but if we trap it here it doesn't prevent CP from working.
|
|
|
|
except IndexError:
|
|
|
|
pass
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def _merged_args(self, d=None):
|
|
|
|
"""Return a dict of configuration entries for this Tool."""
|
|
|
|
if d:
|
|
|
|
conf = d.copy()
|
|
|
|
else:
|
|
|
|
conf = {}
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
tm = cherrypy.serving.request.toolmaps[self.namespace]
|
|
|
|
if self._name in tm:
|
|
|
|
conf.update(tm[self._name])
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
if "on" in conf:
|
|
|
|
del conf["on"]
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
return conf
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def __call__(self, *args, **kwargs):
|
|
|
|
"""Compile-time decorator (turn on the tool in config).
|
2014-06-04 21:28:59 -04:00
|
|
|
|
|
|
|
For example::
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
@tools.proxy()
|
|
|
|
def whats_my_base(self):
|
|
|
|
return cherrypy.request.base
|
|
|
|
whats_my_base.exposed = True
|
|
|
|
"""
|
|
|
|
if args:
|
|
|
|
raise TypeError("The %r Tool does not accept positional "
|
|
|
|
"arguments; you must use keyword arguments."
|
|
|
|
% self._name)
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def tool_decorator(f):
|
|
|
|
if not hasattr(f, "_cp_config"):
|
|
|
|
f._cp_config = {}
|
|
|
|
subspace = self.namespace + "." + self._name + "."
|
|
|
|
f._cp_config[subspace + "on"] = True
|
|
|
|
for k, v in kwargs.items():
|
|
|
|
f._cp_config[subspace + k] = v
|
|
|
|
return f
|
|
|
|
return tool_decorator
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def _setup(self):
|
|
|
|
"""Hook this tool into cherrypy.request.
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
The standard CherryPy request object will automatically call this
|
|
|
|
method when the tool is "turned on" in config.
|
|
|
|
"""
|
|
|
|
conf = self._merged_args()
|
|
|
|
p = conf.pop("priority", None)
|
|
|
|
if p is None:
|
|
|
|
p = getattr(self.callable, "priority", self._priority)
|
|
|
|
cherrypy.serving.request.hooks.attach(self._point, self.callable,
|
|
|
|
priority=p, **conf)
|
|
|
|
|
|
|
|
|
|
|
|
class HandlerTool(Tool):
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
"""Tool which is called 'before main', that may skip normal handlers.
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
If the tool successfully handles the request (by setting response.body),
|
|
|
|
if should return True. This will cause CherryPy to skip any 'normal' page
|
|
|
|
handler. If the tool did not handle the request, it should return False
|
|
|
|
to tell CherryPy to continue on and call the normal page handler. If the
|
|
|
|
tool is declared AS a page handler (see the 'handler' method), returning
|
|
|
|
False will raise NotFound.
|
|
|
|
"""
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def __init__(self, callable, name=None):
|
|
|
|
Tool.__init__(self, 'before_handler', callable, name)
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def handler(self, *args, **kwargs):
|
|
|
|
"""Use this tool as a CherryPy page handler.
|
2014-06-04 21:28:59 -04:00
|
|
|
|
|
|
|
For example::
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
class Root:
|
|
|
|
nav = tools.staticdir.handler(section="/nav", dir="nav",
|
|
|
|
root=absDir)
|
|
|
|
"""
|
|
|
|
def handle_func(*a, **kw):
|
|
|
|
handled = self.callable(*args, **self._merged_args(kwargs))
|
|
|
|
if not handled:
|
|
|
|
raise cherrypy.NotFound()
|
|
|
|
return cherrypy.serving.response.body
|
|
|
|
handle_func.exposed = True
|
|
|
|
return handle_func
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def _wrapper(self, **kwargs):
|
|
|
|
if self.callable(**kwargs):
|
|
|
|
cherrypy.serving.request.handler = None
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def _setup(self):
|
|
|
|
"""Hook this tool into cherrypy.request.
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
The standard CherryPy request object will automatically call this
|
|
|
|
method when the tool is "turned on" in config.
|
|
|
|
"""
|
|
|
|
conf = self._merged_args()
|
|
|
|
p = conf.pop("priority", None)
|
|
|
|
if p is None:
|
|
|
|
p = getattr(self.callable, "priority", self._priority)
|
|
|
|
cherrypy.serving.request.hooks.attach(self._point, self._wrapper,
|
|
|
|
priority=p, **conf)
|
|
|
|
|
|
|
|
|
|
|
|
class HandlerWrapperTool(Tool):
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
"""Tool which wraps request.handler in a provided wrapper function.
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
The 'newhandler' arg must be a handler wrapper function that takes a
|
2014-06-04 21:28:59 -04:00
|
|
|
'next_handler' argument, plus ``*args`` and ``**kwargs``. Like all
|
|
|
|
page handler
|
2014-03-10 01:18:05 -04:00
|
|
|
functions, it must return an iterable for use as cherrypy.response.body.
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
For example, to allow your 'inner' page handlers to return dicts
|
2014-06-04 21:28:59 -04:00
|
|
|
which then get interpolated into a template::
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def interpolator(next_handler, *args, **kwargs):
|
|
|
|
filename = cherrypy.request.config.get('template')
|
|
|
|
cherrypy.response.template = env.get_template(filename)
|
|
|
|
response_dict = next_handler(*args, **kwargs)
|
|
|
|
return cherrypy.response.template.render(**response_dict)
|
|
|
|
cherrypy.tools.jinja = HandlerWrapperTool(interpolator)
|
|
|
|
"""
|
2014-06-04 21:28:59 -04:00
|
|
|
|
|
|
|
def __init__(self, newhandler, point='before_handler', name=None,
|
|
|
|
priority=50):
|
2014-03-10 01:18:05 -04:00
|
|
|
self.newhandler = newhandler
|
|
|
|
self._point = point
|
|
|
|
self._name = name
|
|
|
|
self._priority = priority
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def callable(self, debug=False):
|
|
|
|
innerfunc = cherrypy.serving.request.handler
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def wrap(*args, **kwargs):
|
|
|
|
return self.newhandler(innerfunc, *args, **kwargs)
|
|
|
|
cherrypy.serving.request.handler = wrap
|
|
|
|
|
|
|
|
|
|
|
|
class ErrorTool(Tool):
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
"""Tool which is used to replace the default request.error_response."""
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def __init__(self, callable, name=None):
|
|
|
|
Tool.__init__(self, None, callable, name)
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def _wrapper(self):
|
|
|
|
self.callable(**self._merged_args())
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def _setup(self):
|
|
|
|
"""Hook this tool into cherrypy.request.
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
The standard CherryPy request object will automatically call this
|
|
|
|
method when the tool is "turned on" in config.
|
|
|
|
"""
|
|
|
|
cherrypy.serving.request.error_response = self._wrapper
|
|
|
|
|
|
|
|
|
|
|
|
# Builtin tools #
|
|
|
|
|
|
|
|
from cherrypy.lib import cptools, encoding, auth, static, jsontools
|
2014-06-04 21:28:59 -04:00
|
|
|
from cherrypy.lib import sessions as _sessions, xmlrpcutil as _xmlrpc
|
2014-03-10 01:18:05 -04:00
|
|
|
from cherrypy.lib import caching as _caching
|
|
|
|
from cherrypy.lib import auth_basic, auth_digest
|
|
|
|
|
|
|
|
|
|
|
|
class SessionTool(Tool):
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
"""Session Tool for CherryPy.
|
2014-06-04 21:28:59 -04:00
|
|
|
|
|
|
|
sessions.locking
|
2014-03-10 01:18:05 -04:00
|
|
|
When 'implicit' (the default), the session will be locked for you,
|
2014-06-04 21:28:59 -04:00
|
|
|
just before running the page handler.
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
When 'early', the session will be locked before reading the request
|
2014-06-04 21:28:59 -04:00
|
|
|
body. This is off by default for safety reasons; for example,
|
|
|
|
a large upload would block the session, denying an AJAX
|
|
|
|
progress meter
|
|
|
|
(`issue <https://bitbucket.org/cherrypy/cherrypy/issue/630>`_).
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
When 'explicit' (or any other value), you need to call
|
2014-06-04 21:28:59 -04:00
|
|
|
cherrypy.session.acquire_lock() yourself before using
|
|
|
|
session data.
|
2014-03-10 01:18:05 -04:00
|
|
|
"""
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def __init__(self):
|
|
|
|
# _sessions.init must be bound after headers are read
|
|
|
|
Tool.__init__(self, 'before_request_body', _sessions.init)
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def _lock_session(self):
|
|
|
|
cherrypy.serving.session.acquire_lock()
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def _setup(self):
|
|
|
|
"""Hook this tool into cherrypy.request.
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
The standard CherryPy request object will automatically call this
|
|
|
|
method when the tool is "turned on" in config.
|
|
|
|
"""
|
|
|
|
hooks = cherrypy.serving.request.hooks
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
conf = self._merged_args()
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
p = conf.pop("priority", None)
|
|
|
|
if p is None:
|
|
|
|
p = getattr(self.callable, "priority", self._priority)
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
hooks.attach(self._point, self.callable, priority=p, **conf)
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
locking = conf.pop('locking', 'implicit')
|
|
|
|
if locking == 'implicit':
|
|
|
|
hooks.attach('before_handler', self._lock_session)
|
|
|
|
elif locking == 'early':
|
|
|
|
# Lock before the request body (but after _sessions.init runs!)
|
|
|
|
hooks.attach('before_request_body', self._lock_session,
|
|
|
|
priority=60)
|
|
|
|
else:
|
|
|
|
# Don't lock
|
|
|
|
pass
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
hooks.attach('before_finalize', _sessions.save)
|
|
|
|
hooks.attach('on_end_request', _sessions.close)
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def regenerate(self):
|
|
|
|
"""Drop the current session and make a new one (with a new id)."""
|
|
|
|
sess = cherrypy.serving.session
|
|
|
|
sess.regenerate()
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
# Grab cookie-relevant tool args
|
|
|
|
conf = dict([(k, v) for k, v in self._merged_args().items()
|
|
|
|
if k in ('path', 'path_header', 'name', 'timeout',
|
|
|
|
'domain', 'secure')])
|
|
|
|
_sessions.set_response_cookie(**conf)
|
|
|
|
|
|
|
|
|
|
|
|
class XMLRPCController(object):
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
"""A Controller (page handler collection) for XML-RPC.
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
To use it, have your controllers subclass this base class (it will
|
|
|
|
turn on the tool for you).
|
2014-06-04 21:28:59 -04:00
|
|
|
|
|
|
|
You can also supply the following optional config entries::
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
tools.xmlrpc.encoding: 'utf-8'
|
|
|
|
tools.xmlrpc.allow_none: 0
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
XML-RPC is a rather discontinuous layer over HTTP; dispatching to the
|
|
|
|
appropriate handler must first be performed according to the URL, and
|
|
|
|
then a second dispatch step must take place according to the RPC method
|
|
|
|
specified in the request body. It also allows a superfluous "/RPC2"
|
|
|
|
prefix in the URL, supplies its own handler args in the body, and
|
|
|
|
requires a 200 OK "Fault" response instead of 404 when the desired
|
|
|
|
method is not found.
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
Therefore, XML-RPC cannot be implemented for CherryPy via a Tool alone.
|
|
|
|
This Controller acts as the dispatch target for the first half (based
|
|
|
|
on the URL); it then reads the RPC method from the request body and
|
|
|
|
does its own second dispatch step based on that method. It also reads
|
|
|
|
body params, and returns a Fault on error.
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
The XMLRPCDispatcher strips any /RPC2 prefix; if you aren't using /RPC2
|
|
|
|
in your URL's, you can safely skip turning on the XMLRPCDispatcher.
|
2014-06-04 21:28:59 -04:00
|
|
|
Otherwise, you need to use declare it in config::
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
request.dispatch: cherrypy.dispatch.XMLRPCDispatcher()
|
|
|
|
"""
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
# Note we're hard-coding this into the 'tools' namespace. We could do
|
|
|
|
# a huge amount of work to make it relocatable, but the only reason why
|
|
|
|
# would be if someone actually disabled the default_toolbox. Meh.
|
|
|
|
_cp_config = {'tools.xmlrpc.on': True}
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def default(self, *vpath, **params):
|
|
|
|
rpcparams, rpcmethod = _xmlrpc.process_body()
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
subhandler = self
|
|
|
|
for attr in str(rpcmethod).split('.'):
|
|
|
|
subhandler = getattr(subhandler, attr, None)
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
if subhandler and getattr(subhandler, "exposed", False):
|
|
|
|
body = subhandler(*(vpath + rpcparams), **params)
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
else:
|
2014-06-04 21:28:59 -04:00
|
|
|
# https://bitbucket.org/cherrypy/cherrypy/issue/533
|
2014-03-10 01:18:05 -04:00
|
|
|
# if a method is not found, an xmlrpclib.Fault should be returned
|
|
|
|
# raising an exception here will do that; see
|
2014-06-04 21:28:59 -04:00
|
|
|
# cherrypy.lib.xmlrpcutil.on_error
|
2014-03-10 01:18:05 -04:00
|
|
|
raise Exception('method "%s" is not supported' % attr)
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
conf = cherrypy.serving.request.toolmaps['tools'].get("xmlrpc", {})
|
|
|
|
_xmlrpc.respond(body,
|
|
|
|
conf.get('encoding', 'utf-8'),
|
|
|
|
conf.get('allow_none', 0))
|
|
|
|
return cherrypy.serving.response.body
|
|
|
|
default.exposed = True
|
|
|
|
|
|
|
|
|
|
|
|
class SessionAuthTool(HandlerTool):
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def _setargs(self):
|
|
|
|
for name in dir(cptools.SessionAuth):
|
|
|
|
if not name.startswith("__"):
|
|
|
|
setattr(self, name, None)
|
|
|
|
|
|
|
|
|
|
|
|
class CachingTool(Tool):
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
"""Caching Tool for CherryPy."""
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def _wrapper(self, **kwargs):
|
|
|
|
request = cherrypy.serving.request
|
|
|
|
if _caching.get(**kwargs):
|
|
|
|
request.handler = None
|
|
|
|
else:
|
|
|
|
if request.cacheable:
|
|
|
|
# Note the devious technique here of adding hooks on the fly
|
|
|
|
request.hooks.attach('before_finalize', _caching.tee_output,
|
|
|
|
priority=90)
|
|
|
|
_wrapper.priority = 20
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def _setup(self):
|
|
|
|
"""Hook caching into cherrypy.request."""
|
|
|
|
conf = self._merged_args()
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
p = conf.pop("priority", None)
|
|
|
|
cherrypy.serving.request.hooks.attach('before_handler', self._wrapper,
|
|
|
|
priority=p, **conf)
|
|
|
|
|
|
|
|
|
|
|
|
class Toolbox(object):
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
"""A collection of Tools.
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
This object also functions as a config namespace handler for itself.
|
|
|
|
Custom toolboxes should be added to each Application's toolboxes dict.
|
|
|
|
"""
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def __init__(self, namespace):
|
|
|
|
self.namespace = namespace
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def __setattr__(self, name, value):
|
|
|
|
# If the Tool._name is None, supply it from the attribute name.
|
|
|
|
if isinstance(value, Tool):
|
|
|
|
if value._name is None:
|
|
|
|
value._name = name
|
|
|
|
value.namespace = self.namespace
|
|
|
|
object.__setattr__(self, name, value)
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def __enter__(self):
|
|
|
|
"""Populate request.toolmaps from tools specified in config."""
|
|
|
|
cherrypy.serving.request.toolmaps[self.namespace] = map = {}
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def populate(k, v):
|
|
|
|
toolname, arg = k.split(".", 1)
|
|
|
|
bucket = map.setdefault(toolname, {})
|
|
|
|
bucket[arg] = v
|
|
|
|
return populate
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
|
|
"""Run tool._setup() for each tool in our toolmap."""
|
|
|
|
map = cherrypy.serving.request.toolmaps.get(self.namespace)
|
|
|
|
if map:
|
|
|
|
for name, settings in map.items():
|
|
|
|
if settings.get("on", False):
|
|
|
|
tool = getattr(self, name)
|
|
|
|
tool._setup()
|
|
|
|
|
|
|
|
|
|
|
|
class DeprecatedTool(Tool):
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
_name = None
|
|
|
|
warnmsg = "This Tool is deprecated."
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def __init__(self, point, warnmsg=None):
|
|
|
|
self.point = point
|
|
|
|
if warnmsg is not None:
|
|
|
|
self.warnmsg = warnmsg
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def __call__(self, *args, **kwargs):
|
|
|
|
warnings.warn(self.warnmsg)
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def tool_decorator(f):
|
|
|
|
return f
|
|
|
|
return tool_decorator
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def _setup(self):
|
|
|
|
warnings.warn(self.warnmsg)
|
|
|
|
|
|
|
|
|
|
|
|
default_toolbox = _d = Toolbox("tools")
|
|
|
|
_d.session_auth = SessionAuthTool(cptools.session_auth)
|
2014-06-04 21:28:59 -04:00
|
|
|
_d.allow = Tool('on_start_resource', cptools.allow)
|
2014-03-10 01:18:05 -04:00
|
|
|
_d.proxy = Tool('before_request_body', cptools.proxy, priority=30)
|
|
|
|
_d.response_headers = Tool('on_start_resource', cptools.response_headers)
|
|
|
|
_d.log_tracebacks = Tool('before_error_response', cptools.log_traceback)
|
|
|
|
_d.log_headers = Tool('before_error_response', cptools.log_request_headers)
|
|
|
|
_d.log_hooks = Tool('on_end_request', cptools.log_hooks, priority=100)
|
|
|
|
_d.err_redirect = ErrorTool(cptools.redirect)
|
|
|
|
_d.etags = Tool('before_finalize', cptools.validate_etags, priority=75)
|
|
|
|
_d.decode = Tool('before_request_body', encoding.decode)
|
|
|
|
# the order of encoding, gzip, caching is important
|
|
|
|
_d.encode = Tool('before_handler', encoding.ResponseEncoder, priority=70)
|
|
|
|
_d.gzip = Tool('before_finalize', encoding.gzip, priority=80)
|
|
|
|
_d.staticdir = HandlerTool(static.staticdir)
|
|
|
|
_d.staticfile = HandlerTool(static.staticfile)
|
|
|
|
_d.sessions = SessionTool()
|
|
|
|
_d.xmlrpc = ErrorTool(_xmlrpc.on_error)
|
|
|
|
_d.caching = CachingTool('before_handler', _caching.get, 'caching')
|
|
|
|
_d.expires = Tool('before_finalize', _caching.expires)
|
2014-06-04 21:28:59 -04:00
|
|
|
_d.tidy = DeprecatedTool(
|
|
|
|
'before_finalize',
|
|
|
|
"The tidy tool has been removed from the standard distribution of "
|
|
|
|
"CherryPy. The most recent version can be found at "
|
|
|
|
"http://tools.cherrypy.org/browser.")
|
|
|
|
_d.nsgmls = DeprecatedTool(
|
|
|
|
'before_finalize',
|
|
|
|
"The nsgmls tool has been removed from the standard distribution of "
|
|
|
|
"CherryPy. The most recent version can be found at "
|
|
|
|
"http://tools.cherrypy.org/browser.")
|
2014-03-10 01:18:05 -04:00
|
|
|
_d.ignore_headers = Tool('before_request_body', cptools.ignore_headers)
|
|
|
|
_d.referer = Tool('before_request_body', cptools.referer)
|
|
|
|
_d.basic_auth = Tool('on_start_resource', auth.basic_auth)
|
|
|
|
_d.digest_auth = Tool('on_start_resource', auth.digest_auth)
|
|
|
|
_d.trailing_slash = Tool('before_handler', cptools.trailing_slash, priority=60)
|
|
|
|
_d.flatten = Tool('before_finalize', cptools.flatten)
|
|
|
|
_d.accept = Tool('on_start_resource', cptools.accept)
|
|
|
|
_d.redirect = Tool('on_start_resource', cptools.redirect)
|
|
|
|
_d.autovary = Tool('on_start_resource', cptools.autovary, priority=0)
|
|
|
|
_d.json_in = Tool('before_request_body', jsontools.json_in, priority=30)
|
|
|
|
_d.json_out = Tool('before_handler', jsontools.json_out, priority=30)
|
|
|
|
_d.auth_basic = Tool('before_handler', auth_basic.basic_auth, priority=1)
|
|
|
|
_d.auth_digest = Tool('before_handler', auth_digest.digest_auth, priority=1)
|
|
|
|
|
|
|
|
del _d, cptools, encoding, auth, static
|