summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason R. Coombs <jaraco@jaraco.com>2014-02-23 12:50:37 -0500
committerJason R. Coombs <jaraco@jaraco.com>2014-02-23 12:50:37 -0500
commit3960824898a94bfa1629b8bd01974cb2a45f2632 (patch)
tree3d435f0a726ba34dab1cb5b89c5e782fd256fd09
parent38c78df088d89d7556532c59f007c0b3ff85d6f6 (diff)
parent306de1b6617902700c6dd4d6ce6c98d4dc3220f6 (diff)
downloadcherrypy-3960824898a94bfa1629b8bd01974cb2a45f2632.tar.gz
Merge more robust platform-independent locking technique. Fixes #1122.
-rw-r--r--.hgignore7
-rw-r--r--cherrypy/__init__.py214
-rw-r--r--cherrypy/_cpchecker.py149
-rw-r--r--cherrypy/_cpcompat.py133
-rw-r--r--cherrypy/_cpcompat_subprocess.py1544
-rw-r--r--cherrypy/_cpconfig.py64
-rw-r--r--cherrypy/_cpdispatch.py240
-rw-r--r--cherrypy/_cperror.py224
-rw-r--r--cherrypy/_cplogging.py141
-rw-r--r--cherrypy/_cpmodpy.py105
-rw-r--r--cherrypy/_cpnative_server.py61
-rw-r--r--cherrypy/_cpreqbody.py418
-rw-r--r--cherrypy/_cprequest.py349
-rw-r--r--cherrypy/_cpserver.py113
-rw-r--r--cherrypy/_cpthreadinglocal.py6
-rw-r--r--cherrypy/_cptools.py197
-rw-r--r--cherrypy/_cptree.py146
-rw-r--r--cherrypy/_cpwsgi.py154
-rw-r--r--cherrypy/_cpwsgi_server.py31
-rwxr-xr-xcherrypy/cherryd29
-rw-r--r--cherrypy/lib/__init__.py10
-rw-r--r--cherrypy/lib/auth.py60
-rw-r--r--cherrypy/lib/auth_basic.py23
-rw-r--r--cherrypy/lib/auth_digest.py141
-rw-r--r--cherrypy/lib/caching.py161
-rw-r--r--cherrypy/lib/covercp.py100
-rw-r--r--cherrypy/lib/cpstats.py251
-rw-r--r--cherrypy/lib/cptools.py188
-rw-r--r--cherrypy/lib/encoding.py151
-rw-r--r--cherrypy/lib/gctools.py35
-rw-r--r--cherrypy/lib/http.py1
-rw-r--r--cherrypy/lib/httpauth.py161
-rw-r--r--cherrypy/lib/httputil.py213
-rw-r--r--cherrypy/lib/jsontools.py32
-rw-r--r--cherrypy/lib/lockfile.py147
-rw-r--r--cherrypy/lib/profiler.py74
-rw-r--r--cherrypy/lib/reprconf.py10
-rw-r--r--cherrypy/lib/sessions.py189
-rw-r--r--cherrypy/lib/static.py129
-rw-r--r--cherrypy/lib/xmlrpcutil.py4
-rw-r--r--cherrypy/process/plugins.py284
-rw-r--r--cherrypy/process/servers.py134
-rw-r--r--cherrypy/process/win32.py54
-rw-r--r--cherrypy/process/wspbus.py118
-rw-r--r--cherrypy/scaffold/__init__.py18
-rw-r--r--cherrypy/test/__init__.py3
-rw-r--r--cherrypy/test/_test_decorators.py14
-rw-r--r--cherrypy/test/_test_states_demo.py16
-rw-r--r--cherrypy/test/benchmark.py131
-rw-r--r--cherrypy/test/checkerdemo.py7
-rw-r--r--cherrypy/test/helper.py163
-rw-r--r--cherrypy/test/logtest.py69
-rw-r--r--cherrypy/test/modfastcgi.py22
-rw-r--r--cherrypy/test/modfcgid.py18
-rw-r--r--cherrypy/test/modpy.py28
-rw-r--r--cherrypy/test/modwsgi.py24
-rwxr-xr-xcherrypy/test/sessiondemo.py39
-rw-r--r--cherrypy/test/test_auth_basic.py46
-rw-r--r--cherrypy/test/test_auth_digest.py54
-rw-r--r--cherrypy/test/test_bus.py35
-rw-r--r--cherrypy/test/test_caching.py132
-rw-r--r--cherrypy/test/test_compat.py19
-rw-r--r--cherrypy/test/test_config.py25
-rw-r--r--cherrypy/test/test_config_server.py53
-rw-r--r--cherrypy/test/test_conn.py40
-rw-r--r--cherrypy/test/test_core.py316
-rw-r--r--cherrypy/test/test_dynamicobjectmapping.py103
-rw-r--r--cherrypy/test/test_encoding.py272
-rw-r--r--cherrypy/test/test_etags.py23
-rw-r--r--cherrypy/test/test_http.py58
-rw-r--r--cherrypy/test/test_httpauth.py100
-rw-r--r--cherrypy/test/test_httplib.py2
-rw-r--r--cherrypy/test/test_json.py31
-rw-r--r--cherrypy/test/test_logging.py46
-rw-r--r--cherrypy/test/test_mime.py85
-rw-r--r--cherrypy/test/test_misc_tools.py72
-rw-r--r--cherrypy/test/test_objectmapping.py214
-rw-r--r--cherrypy/test/test_proxy.py71
-rw-r--r--cherrypy/test/test_refleaks.py16
-rw-r--r--cherrypy/test/test_request_obj.py423
-rw-r--r--cherrypy/test/test_routes.py34
-rwxr-xr-xcherrypy/test/test_session.py168
-rw-r--r--cherrypy/test/test_sessionauthenticate.py41
-rw-r--r--cherrypy/test/test_states.py143
-rw-r--r--cherrypy/test/test_static.py105
-rw-r--r--cherrypy/test/test_tools.py165
-rw-r--r--cherrypy/test/test_tutorials.py111
-rw-r--r--cherrypy/test/test_virtualhost.py55
-rw-r--r--cherrypy/test/test_wsgi_ns.py43
-rw-r--r--cherrypy/test/test_wsgi_vhost.py21
-rw-r--r--cherrypy/test/test_wsgiapps.py76
-rw-r--r--cherrypy/test/test_xmlrpc.py65
-rw-r--r--cherrypy/test/webtest.py89
-rw-r--r--cherrypy/tutorial/__init__.py2
-rw-r--r--cherrypy/tutorial/bonus-sqlobject.py168
-rw-r--r--cherrypy/tutorial/tut01_helloworld.py2
-rw-r--r--cherrypy/tutorial/tut02_expose_methods.py11
-rw-r--r--cherrypy/tutorial/tut03_get_and_post.py6
-rw-r--r--cherrypy/tutorial/tut04_complex_site.py29
-rw-r--r--cherrypy/tutorial/tut05_derived_objects.py13
-rw-r--r--cherrypy/tutorial/tut06_default_method.py7
-rw-r--r--cherrypy/tutorial/tut07_sessions.py9
-rw-r--r--cherrypy/tutorial/tut08_generators_and_yield.py13
-rw-r--r--cherrypy/tutorial/tut09_files.py10
-rw-r--r--cherrypy/tutorial/tut10_http_errors.py27
-rw-r--r--cherrypy/wsgiserver/ssl_builtin.py27
-rw-r--r--cherrypy/wsgiserver/ssl_pyopenssl.py97
-rw-r--r--cherrypy/wsgiserver/wsgiserver2.py403
-rw-r--r--cherrypy/wsgiserver/wsgiserver3.py360
-rw-r--r--make-sdist2
-rw-r--r--release.py147
-rw-r--r--setup.cfg11
-rw-r--r--setup.py56
-rw-r--r--sphinx/source/_static/bgsides.pngbin0 -> 6498 bytes
-rw-r--r--sphinx/source/_static/cpdocmain.css42
-rw-r--r--sphinx/source/appendix/cherrypyspeed.rst554
-rw-r--r--sphinx/source/appendix/faq.rst10
-rw-r--r--sphinx/source/appendix/success.rst92
-rw-r--r--sphinx/source/conf.py99
-rw-r--r--sphinx/source/deployguide/index.rst20
-rw-r--r--sphinx/source/index.rst21
-rw-r--r--sphinx/source/intro/index.rst15
-rw-r--r--sphinx/source/intro/install.rst155
-rw-r--r--sphinx/source/intro/license.rst2
-rw-r--r--sphinx/source/intro/whycherrypy.rst13
-rw-r--r--sphinx/source/progguide/REST.rst2
-rw-r--r--sphinx/source/progguide/customheaders.rst4
-rw-r--r--sphinx/source/progguide/extending/customtools.rst2
-rw-r--r--sphinx/source/progguide/index.rst1
-rw-r--r--sphinx/source/progguide/security.rst109
-rw-r--r--sphinx/source/progguide/streaming.rst4
-rw-r--r--sphinx/source/refman/_cpreqbody.rst2
-rw-r--r--sphinx/source/refman/_cptools.rst2
-rw-r--r--sphinx/source/refman/lib/cpstats.rst13
-rw-r--r--sphinx/source/refman/process/plugins/dropprivileges.rst2
-rw-r--r--sphinx/source/tutorial/REST.rst308
-rw-r--r--sphinx/source/tutorial/basics.rst (renamed from sphinx/source/concepts/basics.rst)66
-rw-r--r--sphinx/source/tutorial/config.rst (renamed from sphinx/source/concepts/config.rst)24
-rw-r--r--sphinx/source/tutorial/dispatching.rst (renamed from sphinx/source/concepts/dispatching.rst)74
-rw-r--r--sphinx/source/tutorial/engine.rst (renamed from sphinx/source/concepts/engine.rst)2
-rw-r--r--sphinx/source/tutorial/exposing.rst (renamed from sphinx/source/concepts/exposing.rst)44
-rw-r--r--sphinx/source/tutorial/files/songs.py82
-rw-r--r--sphinx/source/tutorial/index.rst (renamed from sphinx/source/concepts/index.rst)13
-rw-r--r--sphinx/source/tutorial/tools.rst (renamed from sphinx/source/concepts/tools.rst)0
-rw-r--r--sphinx/util/convert-trac.py127
-rw-r--r--sphinx/util/test-doc.py11
-rw-r--r--tox.ini8
147 files changed, 9231 insertions, 4991 deletions
diff --git a/.hgignore b/.hgignore
index c41fa4a5..d207c898 100644
--- a/.hgignore
+++ b/.hgignore
@@ -6,3 +6,10 @@ build/*
dist/*
*.swp
*.log
+lib
+share
+bin
+*.kdev4
+include
+sphinx/source/_build
+.tox
diff --git a/cherrypy/__init__.py b/cherrypy/__init__.py
index 41e3898b..d86a458c 100644
--- a/cherrypy/__init__.py
+++ b/cherrypy/__init__.py
@@ -53,11 +53,10 @@ with customized or extended components. The core API's are:
* Server API
* WSGI API
-These API's are described in the CherryPy specification:
-http://www.cherrypy.org/wiki/CherryPySpec
+These API's are described in the `CherryPy specification <https://bitbucket.org/cherrypy/cherrypy/wiki/CherryPySpec>`_.
"""
-__version__ = "3.2.2"
+__version__ = "3.2.5"
from cherrypy._cpcompat import urljoin as _urljoin, urlencode as _urlencode
from cherrypy._cpcompat import basestring, unicodestr, set
@@ -94,21 +93,22 @@ except ImportError:
engine.listeners['before_request'] = set()
engine.listeners['after_request'] = set()
+
class _TimeoutMonitor(process.plugins.Monitor):
-
+
def __init__(self, bus):
self.servings = []
process.plugins.Monitor.__init__(self, bus, self.run)
-
+
def before_request(self):
self.servings.append((serving.request, serving.response))
-
+
def after_request(self):
try:
self.servings.remove((serving.request, serving.response))
except ValueError:
pass
-
+
def run(self):
"""Check timeout on all responses. (Internal)"""
for req, resp in self.servings:
@@ -125,6 +125,24 @@ engine.thread_manager.subscribe()
engine.signal_handler = process.plugins.SignalHandler(engine)
+class _HandleSignalsPlugin(object):
+
+ """Handle signals from other processes based on the configured
+ platform handlers above."""
+
+ def __init__(self, bus):
+ self.bus = bus
+
+ def subscribe(self):
+ """Add the handlers based on the platform"""
+ if hasattr(self.bus, "signal_handler"):
+ self.bus.signal_handler.subscribe()
+ if hasattr(self.bus, "console_control_handler"):
+ self.bus.console_control_handler.subscribe()
+
+engine.signals = _HandleSignalsPlugin(engine)
+
+
from cherrypy import _cpserver
server = _cpserver.Server()
server.subscribe()
@@ -132,7 +150,7 @@ server.subscribe()
def quickstart(root=None, script_name="", config=None):
"""Mount the given root, start the builtin server (and engine), then block.
-
+
root: an instance of a "controller class" (a collection of page handler
methods) which represents the root of the application.
script_name: a string containing the "mount point" of the application.
@@ -140,7 +158,7 @@ def quickstart(root=None, script_name="", config=None):
at which to mount the given root. For example, if root.index() will
handle requests to "http://www.example.com:8080/dept/app1/", then
the script_name argument would be "/dept/app1".
-
+
It MUST NOT end in a slash. If the script_name refers to the root
of the URI, it MUST be an empty string (not "/").
config: a file or dict containing application config. If this contains
@@ -149,23 +167,21 @@ def quickstart(root=None, script_name="", config=None):
"""
if config:
_global_conf_alias.update(config)
-
+
tree.mount(root, script_name, config)
-
- if hasattr(engine, "signal_handler"):
- engine.signal_handler.subscribe()
- if hasattr(engine, "console_control_handler"):
- engine.console_control_handler.subscribe()
-
+
+ engine.signals.subscribe()
engine.start()
engine.block()
from cherrypy._cpcompat import threadlocal as _local
+
class _Serving(_local):
+
"""An interface for registering request and response objects.
-
+
Rather than have a separate "thread local" object for the request and
the response, this class works as a single threadlocal container for
both objects (and any others which developers wish to define). In this
@@ -173,22 +189,22 @@ class _Serving(_local):
conversation, yet still refer to them as module-level globals in a
thread-safe way.
"""
-
+
request = _cprequest.Request(_httputil.Host("127.0.0.1", 80),
_httputil.Host("127.0.0.1", 1111))
"""
The request object for the current thread. In the main thread,
and any threads which are not receiving HTTP requests, this is None."""
-
+
response = _cprequest.Response()
"""
The response object for the current thread. In the main thread,
and any threads which are not receiving HTTP requests, this is None."""
-
+
def load(self, request, response):
self.request = request
self.response = response
-
+
def clear(self):
"""Remove all attributes of self."""
self.__dict__.clear()
@@ -197,54 +213,54 @@ serving = _Serving()
class _ThreadLocalProxy(object):
-
+
__slots__ = ['__attrname__', '__dict__']
-
+
def __init__(self, attrname):
self.__attrname__ = attrname
-
+
def __getattr__(self, name):
child = getattr(serving, self.__attrname__)
return getattr(child, name)
-
+
def __setattr__(self, name, value):
if name in ("__attrname__", ):
object.__setattr__(self, name, value)
else:
child = getattr(serving, self.__attrname__)
setattr(child, name, value)
-
+
def __delattr__(self, name):
child = getattr(serving, self.__attrname__)
delattr(child, name)
-
+
def _get_dict(self):
child = getattr(serving, self.__attrname__)
d = child.__class__.__dict__.copy()
d.update(child.__dict__)
return d
__dict__ = property(_get_dict)
-
+
def __getitem__(self, key):
child = getattr(serving, self.__attrname__)
return child[key]
-
+
def __setitem__(self, key, value):
child = getattr(serving, self.__attrname__)
child[key] = value
-
+
def __delitem__(self, key):
child = getattr(serving, self.__attrname__)
del child[key]
-
+
def __contains__(self, key):
child = getattr(serving, self.__attrname__)
return key in child
-
+
def __len__(self):
child = getattr(serving, self.__attrname__)
return len(child)
-
+
def __nonzero__(self):
child = getattr(serving, self.__attrname__)
return bool(child)
@@ -258,7 +274,10 @@ request = _ThreadLocalProxy('request')
response = _ThreadLocalProxy('response')
# Create thread_data object as a thread-specific all-purpose storage
+
+
class _ThreadData(_local):
+
"""A container for thread-specific data."""
thread_data = _ThreadData()
@@ -283,27 +302,32 @@ except ImportError:
from cherrypy import _cplogging
+
class _GlobalLogManager(_cplogging.LogManager):
+
"""A site-wide LogManager; routes to app.log or global log as appropriate.
-
+
This :class:`LogManager<cherrypy._cplogging.LogManager>` implements
cherrypy.log() and cherrypy.log.access(). If either
function is called during a request, the message will be sent to the
logger for the current Application. If they are called outside of a
request, the message will be sent to the site-wide logger.
"""
-
+
def __call__(self, *args, **kwargs):
- """Log the given message to the app.log or global log as appropriate."""
- # Do NOT use try/except here. See http://www.cherrypy.org/ticket/945
+ """Log the given message to the app.log or global log as appropriate.
+ """
+ # Do NOT use try/except here. See
+ # https://bitbucket.org/cherrypy/cherrypy/issue/945
if hasattr(request, 'app') and hasattr(request.app, 'log'):
log = request.app.log
else:
log = self
return log.error(*args, **kwargs)
-
+
def access(self):
- """Log an access message to the app.log or global log as appropriate."""
+ """Log an access message to the app.log or global log as appropriate.
+ """
try:
return request.app.log.access()
except AttributeError:
@@ -317,6 +341,7 @@ log.error_file = ''
# Using an access file makes CP about 10% slower. Leave off by default.
log.access_file = ''
+
def _buslog(msg, level):
log.error(msg, 'ENGINE', severity=level)
engine.subscribe('log', _buslog)
@@ -335,8 +360,9 @@ def expose(func=None, alias=None):
for a in alias:
parents[a.replace(".", "_")] = func
return func
-
- import sys, types
+
+ import sys
+ import types
if isinstance(func, (types.FunctionType, types.MethodType)):
if alias is None:
# @expose
@@ -363,24 +389,25 @@ def expose(func=None, alias=None):
alias = func
return expose_
+
def popargs(*args, **kwargs):
- """A decorator for _cp_dispatch
+ """A decorator for _cp_dispatch
(cherrypy.dispatch.Dispatcher.dispatch_method_name).
Optional keyword argument: handler=(Object or Function)
-
- Provides a _cp_dispatch function that pops off path segments into
+
+ Provides a _cp_dispatch function that pops off path segments into
cherrypy.request.params under the names specified. The dispatch
is then forwarded on to the next vpath element.
-
+
Note that any existing (and exposed) member function of the class that
popargs is applied to will override that value of the argument. For
instance, if you have a method named "list" on the class decorated with
popargs, then accessing "/list" will call that function instead of popping
- it off as the requested parameter. This restriction applies to all
+ it off as the requested parameter. This restriction applies to all
_cp_dispatch functions. The only way around this restriction is to create
a "blank class" whose only function is to provide _cp_dispatch.
-
+
If there are path elements after the arguments, or more arguments
are requested than are available in the vpath, then the 'handler'
keyword argument specifies the next object to handle the parameterized
@@ -389,9 +416,9 @@ def popargs(*args, **kwargs):
will be called with the args specified and the return value from that
function used as the next object INSTEAD of adding the parameters to
cherrypy.request.args.
-
+
This decorator may be used in one of two ways:
-
+
As a class decorator:
@cherrypy.popargs('year', 'month', 'day')
class Blog:
@@ -399,122 +426,123 @@ def popargs(*args, **kwargs):
#Process the parameters here; any url like
#/, /2009, /2009/12, or /2009/12/31
#will fill in the appropriate parameters.
-
+
def create(self):
#This link will still be available at /create. Defined functions
#take precedence over arguments.
-
+
Or as a member of a class:
class Blog:
_cp_dispatch = cherrypy.popargs('year', 'month', 'day')
#...
-
+
The handler argument may be used to mix arguments with built in functions.
For instance, the following setup allows different activities at the
day, month, and year level:
-
+
class DayHandler:
def index(self, year, month, day):
#Do something with this day; probably list entries
-
+
def delete(self, year, month, day):
#Delete all entries for this day
-
+
@cherrypy.popargs('day', handler=DayHandler())
class MonthHandler:
def index(self, year, month):
#Do something with this month; probably list entries
-
+
def delete(self, year, month):
#Delete all entries for this month
-
+
@cherrypy.popargs('month', handler=MonthHandler())
class YearHandler:
def index(self, year):
#Do something with this year
-
+
#...
-
+
@cherrypy.popargs('year', handler=YearHandler())
class Root:
def index(self):
#...
-
+
"""
- #Since keyword arg comes after *args, we have to process it ourselves
- #for lower versions of python.
+ # Since keyword arg comes after *args, we have to process it ourselves
+ # for lower versions of python.
handler = None
handler_call = False
- for k,v in kwargs.items():
+ for k, v in kwargs.items():
if k == 'handler':
handler = v
else:
raise TypeError(
- "cherrypy.popargs() got an unexpected keyword argument '{0}'" \
+ "cherrypy.popargs() got an unexpected keyword argument '{0}'"
.format(k)
- )
+ )
import inspect
if handler is not None \
- and (hasattr(handler, '__call__') or inspect.isclass(handler)):
+ and (hasattr(handler, '__call__') or inspect.isclass(handler)):
handler_call = True
-
+
def decorated(cls_or_self=None, vpath=None):
if inspect.isclass(cls_or_self):
- #cherrypy.popargs is a class decorator
+ # cherrypy.popargs is a class decorator
cls = cls_or_self
setattr(cls, dispatch.Dispatcher.dispatch_method_name, decorated)
return cls
-
- #We're in the actual function
+
+ # We're in the actual function
self = cls_or_self
parms = {}
for arg in args:
if not vpath:
break
parms[arg] = vpath.pop(0)
-
+
if handler is not None:
if handler_call:
return handler(**parms)
else:
request.params.update(parms)
return handler
-
+
request.params.update(parms)
-
- #If we are the ultimate handler, then to prevent our _cp_dispatch
- #from being called again, we will resolve remaining elements through
- #getattr() directly.
+
+ # If we are the ultimate handler, then to prevent our _cp_dispatch
+ # from being called again, we will resolve remaining elements through
+ # getattr() directly.
if vpath:
return getattr(self, vpath.pop(0), None)
else:
return self
-
+
return decorated
+
def url(path="", qs="", script_name=None, base=None, relative=None):
"""Create an absolute URL for the given path.
-
+
If 'path' starts with a slash ('/'), this will return
(base + script_name + path + qs).
If it does not start with a slash, this returns
(base + script_name [+ request.path_info] + path + qs).
-
+
If script_name is None, cherrypy.request will be used
to find a script_name, if available.
-
+
If base is None, cherrypy.request.base will be used (if available).
Note that you can use cherrypy.tools.proxy to change this.
-
+
Finally, note that this function can be used to obtain an absolute URL
for the current request path (minus the querystring) by passing no args.
If you call url(qs=cherrypy.request.query_string), you should get the
original browser URL (assuming no internal redirections).
-
+
If relative is None or not provided, request.app.relative_urls will
be used (if available, else False). If False, the output will be an
absolute URL (including the scheme, host, vhost, and script_name).
@@ -527,7 +555,7 @@ def url(path="", qs="", script_name=None, base=None, relative=None):
qs = _urlencode(qs)
if qs:
qs = '?' + qs
-
+
if request.app:
if not path.startswith("/"):
# Append/remove trailing slash from path_info as needed
@@ -540,17 +568,17 @@ def url(path="", qs="", script_name=None, base=None, relative=None):
elif request.is_index is False:
if pi.endswith('/') and pi != '/':
pi = pi[:-1]
-
+
if path == "":
path = pi
else:
path = _urljoin(pi, path)
-
+
if script_name is None:
script_name = request.script_name
if base is None:
base = request.base
-
+
newurl = base + script_name + path + qs
else:
# No request.app (we're being called outside a request).
@@ -559,10 +587,10 @@ def url(path="", qs="", script_name=None, base=None, relative=None):
# if you're using vhosts or tools.proxy.
if base is None:
base = server.base()
-
+
path = (script_name or "") + path
newurl = base + path + qs
-
+
if './' in newurl:
# Normalize the URL by removing ./ and ../
atoms = []
@@ -574,12 +602,12 @@ def url(path="", qs="", script_name=None, base=None, relative=None):
else:
atoms.append(atom)
newurl = '/'.join(atoms)
-
+
# At this point, we should have a fully-qualified absolute URL.
-
+
if relative is None:
relative = getattr(request.app, "relative_urls", False)
-
+
# See http://www.ietf.org/rfc/rfc2396.txt
if relative == 'server':
# "A relative reference beginning with a single slash character is
@@ -599,7 +627,7 @@ def url(path="", qs="", script_name=None, base=None, relative=None):
new.pop(0)
new = (['..'] * len(old)) + new
newurl = '/'.join(new)
-
+
return newurl
@@ -613,7 +641,7 @@ config.defaults = {
'tools.log_headers.on': True,
'tools.trailing_slash.on': True,
'tools.encode.on': True
- }
+}
config.namespaces["log"] = lambda k, v: setattr(log, k, v)
config.namespaces["checker"] = lambda k, v: setattr(checker, k, v)
# Must reset to get our defaults applied.
diff --git a/cherrypy/_cpchecker.py b/cherrypy/_cpchecker.py
index 7ccfd89d..4ef82597 100644
--- a/cherrypy/_cpchecker.py
+++ b/cherrypy/_cpchecker.py
@@ -6,26 +6,26 @@ from cherrypy._cpcompat import iteritems, copykeys, builtins
class Checker(object):
+
"""A checker for CherryPy sites and their mounted applications.
-
+
When this object is called at engine startup, it executes each
of its own methods whose names start with ``check_``. If you wish
to disable selected checks, simply add a line in your global
config which sets the appropriate method to False::
-
+
[global]
checker.check_skipped_app_config = False
-
+
You may also dynamically add or replace ``check_*`` methods in this way.
"""
-
+
on = True
"""If True (the default), run all checks; if False, turn off all checks."""
-
-
+
def __init__(self):
self._populate_known_types()
-
+
def __call__(self):
"""Run all check_* methods."""
if self.on:
@@ -39,16 +39,17 @@ class Checker(object):
method()
finally:
warnings.formatwarning = oldformatwarning
-
+
def formatwarning(self, message, category, filename, lineno, line=None):
"""Function to format a warning."""
return "CherryPy Checker:\n%s\n\n" % message
-
+
# This value should be set inside _cpconfig.
global_config_contained_paths = False
-
+
def check_app_config_entries_dont_start_with_script_name(self):
- """Check for Application config with sections that repeat script_name."""
+ """Check for Application config with sections that repeat script_name.
+ """
for sn, app in cherrypy.tree.apps.items():
if not isinstance(app, cherrypy.Application):
continue
@@ -61,30 +62,33 @@ class Checker(object):
key_atoms = key.strip("/").split("/")
if key_atoms[:len(sn_atoms)] == sn_atoms:
warnings.warn(
- "The application mounted at %r has config " \
- "entries that start with its script name: %r" % (sn, key))
-
+ "The application mounted at %r has config "
+ "entries that start with its script name: %r" % (sn,
+ key))
+
def check_site_config_entries_in_app_config(self):
"""Check for mounted Applications that have site-scoped config."""
for sn, app in iteritems(cherrypy.tree.apps):
if not isinstance(app, cherrypy.Application):
continue
-
+
msg = []
for section, entries in iteritems(app.config):
if section.startswith('/'):
for key, value in iteritems(entries):
for n in ("engine.", "server.", "tree.", "checker."):
if key.startswith(n):
- msg.append("[%s] %s = %s" % (section, key, value))
+ msg.append("[%s] %s = %s" %
+ (section, key, value))
if msg:
msg.insert(0,
- "The application mounted at %r contains the following "
- "config entries, which are only allowed in site-wide "
- "config. Move them to a [global] section and pass them "
- "to cherrypy.config.update() instead of tree.mount()." % sn)
+ "The application mounted at %r contains the "
+ "following config entries, which are only allowed "
+ "in site-wide config. Move them to a [global] "
+ "section and pass them to cherrypy.config.update() "
+ "instead of tree.mount()." % sn)
warnings.warn(os.linesep.join(msg))
-
+
def check_skipped_app_config(self):
"""Check for mounted Applications that have no config."""
for sn, app in cherrypy.tree.apps.items():
@@ -100,9 +104,11 @@ class Checker(object):
"cherrypy.tree.mount(..., config=app_config)")
warnings.warn(msg)
return
-
+
def check_app_config_brackets(self):
- """Check for Application config with extraneous brackets in section names."""
+ """Check for Application config with extraneous brackets in section
+ names.
+ """
for sn, app in cherrypy.tree.apps.items():
if not isinstance(app, cherrypy.Application):
continue
@@ -111,11 +117,11 @@ class Checker(object):
for key in app.config.keys():
if key.startswith("[") or key.endswith("]"):
warnings.warn(
- "The application mounted at %r has config " \
+ "The application mounted at %r has config "
"section names with extraneous brackets: %r. "
"Config *files* need brackets; config *dicts* "
"(e.g. passed to tree.mount) do not." % (sn, key))
-
+
def check_static_paths(self):
"""Check Application config for incorrect static paths."""
# Use the dummy Request object in the main thread.
@@ -128,7 +134,7 @@ class Checker(object):
# get_resource will populate request.config
request.get_resource(section + "/dummy.html")
conf = request.config.get
-
+
if conf("tools.staticdir.on", False):
msg = ""
root = conf("tools.staticdir.root")
@@ -144,30 +150,32 @@ class Checker(object):
"though a root is provided.")
testdir = os.path.join(root, dir[1:])
if os.path.exists(testdir):
- msg += ("\nIf you meant to serve the "
- "filesystem folder at %r, remove "
- "the leading slash from dir." % testdir)
+ msg += (
+ "\nIf you meant to serve the "
+ "filesystem folder at %r, remove the "
+ "leading slash from dir." % (testdir,))
else:
if not root:
- msg = "dir is a relative path and no root provided."
+ msg = (
+ "dir is a relative path and "
+ "no root provided.")
else:
fulldir = os.path.join(root, dir)
if not os.path.isabs(fulldir):
- msg = "%r is not an absolute path." % fulldir
-
+ msg = ("%r is not an absolute path." % (
+ fulldir,))
+
if fulldir and not os.path.exists(fulldir):
if msg:
msg += "\n"
msg += ("%r (root + dir) is not an existing "
"filesystem path." % fulldir)
-
+
if msg:
warnings.warn("%s\nsection: [%s]\nroot: %r\ndir: %r"
% (msg, section, root, dir))
-
-
+
# -------------------------- Compatibility -------------------------- #
-
obsolete = {
'server.default_content_type': 'tools.response_headers.headers',
'log_access_file': 'log.access_file',
@@ -180,10 +188,10 @@ class Checker(object):
'throw_errors': 'request.throw_errors',
'profiler.on': ('cherrypy.tree.mount(profiler.make_app('
'cherrypy.Application(Root())))'),
- }
-
+ }
+
deprecated = {}
-
+
def _compat(self, config):
"""Process config and warn on each obsolete or deprecated entry."""
for section, conf in config.items():
@@ -204,7 +212,7 @@ class Checker(object):
elif section in self.deprecated:
warnings.warn("%r is deprecated. Use %r instead."
% (section, self.deprecated[section]))
-
+
def check_compatibility(self):
"""Process config and warn on each obsolete or deprecated entry."""
self._compat(cherrypy.config)
@@ -212,12 +220,10 @@ class Checker(object):
if not isinstance(app, cherrypy.Application):
continue
self._compat(app.config)
-
-
+
# ------------------------ Known Namespaces ------------------------ #
-
extra_config_namespaces = []
-
+
def _known_ns(self, app):
ns = ["wsgi"]
ns.extend(copykeys(app.toolboxes))
@@ -225,7 +231,7 @@ class Checker(object):
ns.extend(copykeys(app.request_class.namespaces))
ns.extend(copykeys(cherrypy.config.namespaces))
ns += self.extra_config_namespaces
-
+
for section, conf in app.config.items():
is_path_section = section.startswith("/")
if is_path_section and isinstance(conf, dict):
@@ -235,22 +241,26 @@ class Checker(object):
if atoms[0] not in ns:
# Spit out a special warning if a known
# namespace is preceded by "cherrypy."
- if (atoms[0] == "cherrypy" and atoms[1] in ns):
- msg = ("The config entry %r is invalid; "
- "try %r instead.\nsection: [%s]"
- % (k, ".".join(atoms[1:]), section))
+ if atoms[0] == "cherrypy" and atoms[1] in ns:
+ msg = (
+ "The config entry %r is invalid; "
+ "try %r instead.\nsection: [%s]"
+ % (k, ".".join(atoms[1:]), section))
else:
- msg = ("The config entry %r is invalid, because "
- "the %r config namespace is unknown.\n"
- "section: [%s]" % (k, atoms[0], section))
+ msg = (
+ "The config entry %r is invalid, "
+ "because the %r config namespace "
+ "is unknown.\n"
+ "section: [%s]" % (k, atoms[0], section))
warnings.warn(msg)
elif atoms[0] == "tools":
if atoms[1] not in dir(cherrypy.tools):
- msg = ("The config entry %r may be invalid, "
- "because the %r tool was not found.\n"
- "section: [%s]" % (k, atoms[1], section))
+ msg = (
+ "The config entry %r may be invalid, "
+ "because the %r tool was not found.\n"
+ "section: [%s]" % (k, atoms[1], section))
warnings.warn(msg)
-
+
def check_config_namespaces(self):
"""Process config and warn on each unknown config namespace."""
for sn, app in cherrypy.tree.apps.items():
@@ -258,17 +268,13 @@ class Checker(object):
continue
self._known_ns(app)
-
-
-
# -------------------------- Config Types -------------------------- #
-
known_config_types = {}
-
+
def _populate_known_types(self):
b = [x for x in vars(builtins).values()
if type(x) is type(str)]
-
+
def traverse(obj, namespace):
for name in dir(obj):
# Hack for 3.2's warning about body_params
@@ -277,17 +283,17 @@ class Checker(object):
vtype = type(getattr(obj, name, None))
if vtype in b:
self.known_config_types[namespace + "." + name] = vtype
-
+
traverse(cherrypy.request, "request")
traverse(cherrypy.response, "response")
traverse(cherrypy.server, "server")
traverse(cherrypy.engine, "engine")
traverse(cherrypy.log, "log")
-
+
def _known_types(self, config):
msg = ("The config entry %r in section %r is of type %r, "
"which does not match the expected type %r.")
-
+
for section, conf in config.items():
if isinstance(conf, dict):
for k, v in conf.items():
@@ -305,7 +311,7 @@ class Checker(object):
if expected_type and vtype != expected_type:
warnings.warn(msg % (k, section, vtype.__name__,
expected_type.__name__))
-
+
def check_config_types(self):
"""Assert that config values are of the same type as default values."""
self._known_types(cherrypy.config)
@@ -313,15 +319,14 @@ class Checker(object):
if not isinstance(app, cherrypy.Application):
continue
self._known_types(app.config)
-
-
+
# -------------------- Specific config warnings -------------------- #
-
def check_localhost(self):
"""Warn if any socket_host is 'localhost'. See #711."""
for k, v in cherrypy.config.items():
if k == 'server.socket_host' and v == 'localhost':
warnings.warn("The use of 'localhost' as a socket host can "
- "cause problems on newer systems, since 'localhost' can "
- "map to either an IPv4 or an IPv6 address. You should "
- "use '127.0.0.1' or '[::1]' instead.")
+ "cause problems on newer systems, since "
+ "'localhost' can map to either an IPv4 or an "
+ "IPv6 address. You should use '127.0.0.1' "
+ "or '[::1]' instead.")
diff --git a/cherrypy/_cpcompat.py b/cherrypy/_cpcompat.py
index ed24c1ab..8a98b38b 100644
--- a/cherrypy/_cpcompat.py
+++ b/cherrypy/_cpcompat.py
@@ -18,6 +18,7 @@ output.
import os
import re
import sys
+import threading
if sys.version_info >= (3, 0):
py3k = True
@@ -25,14 +26,23 @@ if sys.version_info >= (3, 0):
unicodestr = str
nativestr = unicodestr
basestring = (bytes, str)
+
def ntob(n, encoding='ISO-8859-1'):
- """Return the given native string as a byte string in the given encoding."""
+ """Return the given native string as a byte string in the given
+ encoding.
+ """
+ assert_native(n)
# In Python 3, the native string type is unicode
return n.encode(encoding)
+
def ntou(n, encoding='ISO-8859-1'):
- """Return the given native string as a unicode string with the given encoding."""
+ """Return the given native string as a unicode string with the given
+ encoding.
+ """
+ assert_native(n)
# In Python 3, the native string type is unicode
return n
+
def tonative(n, encoding='ISO-8859-1'):
"""Return the given string as a native string in the given encoding."""
# In Python 3, the native string type is unicode
@@ -50,27 +60,36 @@ else:
unicodestr = unicode
nativestr = bytestr
basestring = basestring
+
def ntob(n, encoding='ISO-8859-1'):
- """Return the given native string as a byte string in the given encoding."""
+ """Return the given native string as a byte string in the given
+ encoding.
+ """
+ assert_native(n)
# In Python 2, the native string type is bytes. Assume it's already
# in the given encoding, which for ISO-8859-1 is almost always what
# was intended.
return n
+
def ntou(n, encoding='ISO-8859-1'):
- """Return the given native string as a unicode string with the given encoding."""
+ """Return the given native string as a unicode string with the given
+ encoding.
+ """
+ assert_native(n)
# In Python 2, the native string type is bytes.
- # First, check for the special encoding 'escape'. The test suite uses this
- # to signal that it wants to pass a string with embedded \uXXXX escapes,
- # but without having to prefix it with u'' for Python 2, but no prefix
- # for Python 3.
+ # First, check for the special encoding 'escape'. The test suite uses
+ # this to signal that it wants to pass a string with embedded \uXXXX
+ # escapes, but without having to prefix it with u'' for Python 2,
+ # but no prefix for Python 3.
if encoding == 'escape':
return unicode(
re.sub(r'\\u([0-9a-zA-Z]{4})',
lambda m: unichr(int(m.group(1), 16)),
n.decode('ISO-8859-1')))
- # Assume it's already in the given encoding, which for ISO-8859-1 is almost
- # always what was intended.
+ # Assume it's already in the given encoding, which for ISO-8859-1
+ # is almost always what was intended.
return n.decode(encoding)
+
def tonative(n, encoding='ISO-8859-1'):
"""Return the given string as a native string in the given encoding."""
# In Python 2, the native string type is bytes.
@@ -86,6 +105,11 @@ else:
# bytes:
BytesIO = StringIO
+
+def assert_native(n):
+ if not isinstance(n, nativestr):
+ raise TypeError("n must be a native str (got %s)" % type(n).__name__)
+
try:
set = set
except NameError:
@@ -100,6 +124,7 @@ except ImportError:
# the legacy API of base64
from base64 import decodestring as _base64_decodebytes
+
def base64_decode(n, encoding='ISO-8859-1'):
"""Return the native string base64-decoded (as a native string)."""
if isinstance(n, unicodestr):
@@ -198,28 +223,31 @@ except ImportError:
import __builtin__ as builtins
try:
- # Python 2. We have to do it in this order so Python 2 builds
+ # Python 2. We try Python 2 first clients on Python 2
# don't try to import the 'http' module from cherrypy.lib
from Cookie import SimpleCookie, CookieError
- from httplib import BadStatusLine, HTTPConnection, HTTPSConnection, IncompleteRead, NotConnected
+ from httplib import BadStatusLine, HTTPConnection, IncompleteRead
+ from httplib import NotConnected
from BaseHTTPServer import BaseHTTPRequestHandler
except ImportError:
# Python 3
from http.cookies import SimpleCookie, CookieError
- from http.client import BadStatusLine, HTTPConnection, HTTPSConnection, IncompleteRead, NotConnected
+ from http.client import BadStatusLine, HTTPConnection, IncompleteRead
+ from http.client import NotConnected
from http.server import BaseHTTPRequestHandler
-try:
- # Python 2. We have to do it in this order so Python 2 builds
- # don't try to import the 'http' module from cherrypy.lib
- from httplib import HTTPSConnection
-except ImportError:
+# Some platforms don't expose HTTPSConnection, so handle it separately
+if py3k:
try:
- # Python 3
from http.client import HTTPSConnection
except ImportError:
# Some platforms which don't have SSL don't expose HTTPSConnection
HTTPSConnection = None
+else:
+ try:
+ from httplib import HTTPSConnection
+ except ImportError:
+ HTTPSConnection = None
try:
# Python 2
@@ -233,16 +261,19 @@ if hasattr(threading.Thread, "daemon"):
# Python 2.6+
def get_daemon(t):
return t.daemon
+
def set_daemon(t, val):
t.daemon = val
else:
def get_daemon(t):
return t.isDaemon()
+
def set_daemon(t, val):
t.setDaemon(val)
try:
from email.utils import formatdate
+
def HTTPDate(timeval=None):
return formatdate(timeval, usegmt=True)
except ImportError:
@@ -251,40 +282,49 @@ except ImportError:
try:
# Python 3
from urllib.parse import unquote as parse_unquote
+
def unquote_qs(atom, encoding, errors='strict'):
- return parse_unquote(atom.replace('+', ' '), encoding=encoding, errors=errors)
+ return parse_unquote(
+ atom.replace('+', ' '),
+ encoding=encoding,
+ errors=errors)
except ImportError:
# Python 2
from urllib import unquote as parse_unquote
+
def unquote_qs(atom, encoding, errors='strict'):
return parse_unquote(atom.replace('+', ' ')).decode(encoding, errors)
try:
- # Prefer simplejson, which is usually more advanced than the builtin module.
+ # Prefer simplejson, which is usually more advanced than the builtin
+ # module.
import simplejson as json
json_decode = json.JSONDecoder().decode
- json_encode = json.JSONEncoder().iterencode
+ _json_encode = json.JSONEncoder().iterencode
except ImportError:
- if py3k:
- # Python 3.0: json is part of the standard library,
- # but outputs unicode. We need bytes.
+ if sys.version_info >= (2, 6):
+ # Python >=2.6 : json is part of the standard library
import json
json_decode = json.JSONDecoder().decode
_json_encode = json.JSONEncoder().iterencode
- def json_encode(value):
- for chunk in _json_encode(value):
- yield chunk.encode('utf8')
- elif sys.version_info >= (2, 6):
- # Python 2.6: json is part of the standard library
- import json
- json_decode = json.JSONDecoder().decode
- json_encode = json.JSONEncoder().iterencode
else:
json = None
+
def json_decode(s):
raise ValueError('No JSON library is available')
- def json_encode(s):
+
+ def _json_encode(s):
raise ValueError('No JSON library is available')
+finally:
+ if json and py3k:
+ # The two Python 3 implementations (simplejson/json)
+ # outputs str. We need bytes.
+ def json_encode(value):
+ for chunk in _json_encode(value):
+ yield chunk.encode('utf8')
+ else:
+ json_encode = _json_encode
+
try:
import cPickle as pickle
@@ -296,11 +336,13 @@ except ImportError:
try:
os.urandom(20)
import binascii
+
def random20():
return binascii.hexlify(os.urandom(20)).decode('ascii')
except (AttributeError, NotImplementedError):
import random
# os.urandom not available until Python 2.4. Fall back to random.random.
+
def random20():
return sha('%s' % random.random()).hexdigest()
@@ -316,3 +358,26 @@ except NameError:
# Python 2
def next(i):
return i.next()
+
+if sys.version_info >= (3, 3):
+ Timer = threading.Timer
+ Event = threading.Event
+else:
+ # Python 3.2 and earlier
+ Timer = threading._Timer
+ Event = threading._Event
+
+# Prior to Python 2.6, the Thread class did not have a .daemon property.
+# This mix-in adds that property.
+
+
+class SetDaemonProperty:
+
+ def __get_daemon(self):
+ return self.isDaemon()
+
+ def __set_daemon(self, daemon):
+ self.setDaemon(daemon)
+
+ if sys.version_info < (2, 6):
+ daemon = property(__get_daemon, __set_daemon)
diff --git a/cherrypy/_cpcompat_subprocess.py b/cherrypy/_cpcompat_subprocess.py
new file mode 100644
index 00000000..ce363725
--- /dev/null
+++ b/cherrypy/_cpcompat_subprocess.py
@@ -0,0 +1,1544 @@
+# subprocess - Subprocesses with accessible I/O streams
+#
+# For more information about this module, see PEP 324.
+#
+# This module should remain compatible with Python 2.2, see PEP 291.
+#
+# Copyright (c) 2003-2005 by Peter Astrand <astrand@lysator.liu.se>
+#
+# Licensed to PSF under a Contributor Agreement.
+# See http://www.python.org/2.4/license for licensing details.
+
+r"""subprocess - Subprocesses with accessible I/O streams
+
+This module allows you to spawn processes, connect to their
+input/output/error pipes, and obtain their return codes. This module
+intends to replace several other, older modules and functions, like:
+
+os.system
+os.spawn*
+os.popen*
+popen2.*
+commands.*
+
+Information about how the subprocess module can be used to replace these
+modules and functions can be found below.
+
+
+
+Using the subprocess module
+===========================
+This module defines one class called Popen:
+
+class Popen(args, bufsize=0, executable=None,
+ stdin=None, stdout=None, stderr=None,
+ preexec_fn=None, close_fds=False, shell=False,
+ cwd=None, env=None, universal_newlines=False,
+ startupinfo=None, creationflags=0):
+
+
+Arguments are:
+
+args should be a string, or a sequence of program arguments. The
+program to execute is normally the first item in the args sequence or
+string, but can be explicitly set by using the executable argument.
+
+On UNIX, with shell=False (default): In this case, the Popen class
+uses os.execvp() to execute the child program. args should normally
+be a sequence. A string will be treated as a sequence with the string
+as the only item (the program to execute).
+
+On UNIX, with shell=True: If args is a string, it specifies the
+command string to execute through the shell. If args is a sequence,
+the first item specifies the command string, and any additional items
+will be treated as additional shell arguments.
+
+On Windows: the Popen class uses CreateProcess() to execute the child
+program, which operates on strings. If args is a sequence, it will be
+converted to a string using the list2cmdline method. Please note that
+not all MS Windows applications interpret the command line the same
+way: The list2cmdline is designed for applications using the same
+rules as the MS C runtime.
+
+bufsize, if given, has the same meaning as the corresponding argument
+to the built-in open() function: 0 means unbuffered, 1 means line
+buffered, any other positive value means use a buffer of
+(approximately) that size. A negative bufsize means to use the system
+default, which usually means fully buffered. The default value for
+bufsize is 0 (unbuffered).
+
+stdin, stdout and stderr specify the executed programs' standard
+input, standard output and standard error file handles, respectively.
+Valid values are PIPE, an existing file descriptor (a positive
+integer), an existing file object, and None. PIPE indicates that a
+new pipe to the child should be created. With None, no redirection
+will occur; the child's file handles will be inherited from the
+parent. Additionally, stderr can be STDOUT, which indicates that the
+stderr data from the applications should be captured into the same
+file handle as for stdout.
+
+If preexec_fn is set to a callable object, this object will be called
+in the child process just before the child is executed.
+
+If close_fds is true, all file descriptors except 0, 1 and 2 will be
+closed before the child process is executed.
+
+if shell is true, the specified command will be executed through the
+shell.
+
+If cwd is not None, the current directory will be changed to cwd
+before the child is executed.
+
+If env is not None, it defines the environment variables for the new
+process.
+
+If universal_newlines is true, the file objects stdout and stderr are
+opened as a text files, but lines may be terminated by any of '\n',
+the Unix end-of-line convention, '\r', the Macintosh convention or
+'\r\n', the Windows convention. All of these external representations
+are seen as '\n' by the Python program. Note: This feature is only
+available if Python is built with universal newline support (the
+default). Also, the newlines attribute of the file objects stdout,
+stdin and stderr are not updated by the communicate() method.
+
+The startupinfo and creationflags, if given, will be passed to the
+underlying CreateProcess() function. They can specify things such as
+appearance of the main window and priority for the new process.
+(Windows only)
+
+
+This module also defines some shortcut functions:
+
+call(*popenargs, **kwargs):
+ Run command with arguments. Wait for command to complete, then
+ return the returncode attribute.
+
+ The arguments are the same as for the Popen constructor. Example:
+
+ retcode = call(["ls", "-l"])
+
+check_call(*popenargs, **kwargs):
+ Run command with arguments. Wait for command to complete. If the
+ exit code was zero then return, otherwise raise
+ CalledProcessError. The CalledProcessError object will have the
+ return code in the returncode attribute.
+
+ The arguments are the same as for the Popen constructor. Example:
+
+ check_call(["ls", "-l"])
+
+check_output(*popenargs, **kwargs):
+ Run command with arguments and return its output as a byte string.
+
+ If the exit code was non-zero it raises a CalledProcessError. The
+ CalledProcessError object will have the return code in the returncode
+ attribute and output in the output attribute.
+
+ The arguments are the same as for the Popen constructor. Example:
+
+ output = check_output(["ls", "-l", "/dev/null"])
+
+
+Exceptions
+----------
+Exceptions raised in the child process, before the new program has
+started to execute, will be re-raised in the parent. Additionally,
+the exception object will have one extra attribute called
+'child_traceback', which is a string containing traceback information
+from the childs point of view.
+
+The most common exception raised is OSError. This occurs, for
+example, when trying to execute a non-existent file. Applications
+should prepare for OSErrors.
+
+A ValueError will be raised if Popen is called with invalid arguments.
+
+check_call() and check_output() will raise CalledProcessError, if the
+called process returns a non-zero return code.
+
+
+Security
+--------
+Unlike some other popen functions, this implementation will never call
+/bin/sh implicitly. This means that all characters, including shell
+metacharacters, can safely be passed to child processes.
+
+
+Popen objects
+=============
+Instances of the Popen class have the following methods:
+
+poll()
+ Check if child process has terminated. Returns returncode
+ attribute.
+
+wait()
+ Wait for child process to terminate. Returns returncode attribute.
+
+communicate(input=None)
+ Interact with process: Send data to stdin. Read data from stdout
+ and stderr, until end-of-file is reached. Wait for process to
+ terminate. The optional input argument should be a string to be
+ sent to the child process, or None, if no data should be sent to
+ the child.
+
+ communicate() returns a tuple (stdout, stderr).
+
+ Note: The data read is buffered in memory, so do not use this
+ method if the data size is large or unlimited.
+
+The following attributes are also available:
+
+stdin
+ If the stdin argument is PIPE, this attribute is a file object
+ that provides input to the child process. Otherwise, it is None.
+
+stdout
+ If the stdout argument is PIPE, this attribute is a file object
+ that provides output from the child process. Otherwise, it is
+ None.
+
+stderr
+ If the stderr argument is PIPE, this attribute is file object that
+ provides error output from the child process. Otherwise, it is
+ None.
+
+pid
+ The process ID of the child process.
+
+returncode
+ The child return code. A None value indicates that the process
+ hasn't terminated yet. A negative value -N indicates that the
+ child was terminated by signal N (UNIX only).
+
+
+Replacing older functions with the subprocess module
+====================================================
+In this section, "a ==> b" means that b can be used as a replacement
+for a.
+
+Note: All functions in this section fail (more or less) silently if
+the executed program cannot be found; this module raises an OSError
+exception.
+
+In the following examples, we assume that the subprocess module is
+imported with "from subprocess import *".
+
+
+Replacing /bin/sh shell backquote
+---------------------------------
+output=`mycmd myarg`
+==>
+output = Popen(["mycmd", "myarg"], stdout=PIPE).communicate()[0]
+
+
+Replacing shell pipe line
+-------------------------
+output=`dmesg | grep hda`
+==>
+p1 = Popen(["dmesg"], stdout=PIPE)
+p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
+output = p2.communicate()[0]
+
+
+Replacing os.system()
+---------------------
+sts = os.system("mycmd" + " myarg")
+==>
+p = Popen("mycmd" + " myarg", shell=True)
+pid, sts = os.waitpid(p.pid, 0)
+
+Note:
+
+* Calling the program through the shell is usually not required.
+
+* It's easier to look at the returncode attribute than the
+ exitstatus.
+
+A more real-world example would look like this:
+
+try:
+ retcode = call("mycmd" + " myarg", shell=True)
+ if retcode < 0:
+ print >>sys.stderr, "Child was terminated by signal", -retcode
+ else:
+ print >>sys.stderr, "Child returned", retcode
+except OSError, e:
+ print >>sys.stderr, "Execution failed:", e
+
+
+Replacing os.spawn*
+-------------------
+P_NOWAIT example:
+
+pid = os.spawnlp(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg")
+==>
+pid = Popen(["/bin/mycmd", "myarg"]).pid
+
+
+P_WAIT example:
+
+retcode = os.spawnlp(os.P_WAIT, "/bin/mycmd", "mycmd", "myarg")
+==>
+retcode = call(["/bin/mycmd", "myarg"])
+
+
+Vector example:
+
+os.spawnvp(os.P_NOWAIT, path, args)
+==>
+Popen([path] + args[1:])
+
+
+Environment example:
+
+os.spawnlpe(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg", env)
+==>
+Popen(["/bin/mycmd", "myarg"], env={"PATH": "/usr/bin"})
+
+
+Replacing os.popen*
+-------------------
+pipe = os.popen("cmd", mode='r', bufsize)
+==>
+pipe = Popen("cmd", shell=True, bufsize=bufsize, stdout=PIPE).stdout
+
+pipe = os.popen("cmd", mode='w', bufsize)
+==>
+pipe = Popen("cmd", shell=True, bufsize=bufsize, stdin=PIPE).stdin
+
+
+(child_stdin, child_stdout) = os.popen2("cmd", mode, bufsize)
+==>
+p = Popen("cmd", shell=True, bufsize=bufsize,
+ stdin=PIPE, stdout=PIPE, close_fds=True)
+(child_stdin, child_stdout) = (p.stdin, p.stdout)
+
+
+(child_stdin,
+ child_stdout,
+ child_stderr) = os.popen3("cmd", mode, bufsize)
+==>
+p = Popen("cmd", shell=True, bufsize=bufsize,
+ stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
+(child_stdin,
+ child_stdout,
+ child_stderr) = (p.stdin, p.stdout, p.stderr)
+
+
+(child_stdin, child_stdout_and_stderr) = os.popen4("cmd", mode,
+ bufsize)
+==>
+p = Popen("cmd", shell=True, bufsize=bufsize,
+ stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
+(child_stdin, child_stdout_and_stderr) = (p.stdin, p.stdout)
+
+On Unix, os.popen2, os.popen3 and os.popen4 also accept a sequence as
+the command to execute, in which case arguments will be passed
+directly to the program without shell intervention. This usage can be
+replaced as follows:
+
+(child_stdin, child_stdout) = os.popen2(["/bin/ls", "-l"], mode,
+ bufsize)
+==>
+p = Popen(["/bin/ls", "-l"], bufsize=bufsize, stdin=PIPE, stdout=PIPE)
+(child_stdin, child_stdout) = (p.stdin, p.stdout)
+
+Return code handling translates as follows:
+
+pipe = os.popen("cmd", 'w')
+...
+rc = pipe.close()
+if rc is not None and rc % 256:
+ print "There were some errors"
+==>
+process = Popen("cmd", 'w', shell=True, stdin=PIPE)
+...
+process.stdin.close()
+if process.wait() != 0:
+ print "There were some errors"
+
+
+Replacing popen2.*
+------------------
+(child_stdout, child_stdin) = popen2.popen2("somestring", bufsize, mode)
+==>
+p = Popen(["somestring"], shell=True, bufsize=bufsize
+ stdin=PIPE, stdout=PIPE, close_fds=True)
+(child_stdout, child_stdin) = (p.stdout, p.stdin)
+
+On Unix, popen2 also accepts a sequence as the command to execute, in
+which case arguments will be passed directly to the program without
+shell intervention. This usage can be replaced as follows:
+
+(child_stdout, child_stdin) = popen2.popen2(["mycmd", "myarg"], bufsize,
+ mode)
+==>
+p = Popen(["mycmd", "myarg"], bufsize=bufsize,
+ stdin=PIPE, stdout=PIPE, close_fds=True)
+(child_stdout, child_stdin) = (p.stdout, p.stdin)
+
+The popen2.Popen3 and popen2.Popen4 basically works as subprocess.Popen,
+except that:
+
+* subprocess.Popen raises an exception if the execution fails
+* the capturestderr argument is replaced with the stderr argument.
+* stdin=PIPE and stdout=PIPE must be specified.
+* popen2 closes all filedescriptors by default, but you have to specify
+ close_fds=True with subprocess.Popen.
+"""
+
+import sys
+mswindows = (sys.platform == "win32")
+
+import os
+import types
+import traceback
+import gc
+import signal
+import errno
+
+try:
+ set
+except NameError:
+ from sets import Set as set
+
+# Exception classes used by this module.
+
+
+class CalledProcessError(Exception):
+
+ """This exception is raised when a process run by check_call() or
+ check_output() returns a non-zero exit status.
+ The exit status will be stored in the returncode attribute;
+ check_output() will also store the output in the output attribute.
+ """
+
+ def __init__(self, returncode, cmd, output=None):
+ self.returncode = returncode
+ self.cmd = cmd
+ self.output = output
+
+ def __str__(self):
+ return "Command '%s' returned non-zero exit status %d" % (
+ self.cmd, self.returncode)
+
+
+if mswindows:
+ import threading
+ import msvcrt
+ import _subprocess
+
+ class STARTUPINFO:
+ dwFlags = 0
+ hStdInput = None
+ hStdOutput = None
+ hStdError = None
+ wShowWindow = 0
+
+ class pywintypes:
+ error = IOError
+else:
+ import select
+ _has_poll = hasattr(select, 'poll')
+ import fcntl
+ import pickle
+
+ # When select or poll has indicated that the file is writable,
+ # we can write up to _PIPE_BUF bytes without risk of blocking.
+ # POSIX defines PIPE_BUF as >= 512.
+ _PIPE_BUF = getattr(select, 'PIPE_BUF', 512)
+
+
+__all__ = ["Popen", "PIPE", "STDOUT", "call", "check_call",
+ "check_output", "CalledProcessError"]
+
+if mswindows:
+ from _subprocess import CREATE_NEW_CONSOLE, CREATE_NEW_PROCESS_GROUP, \
+ STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, \
+ STD_ERROR_HANDLE, SW_HIDE, \
+ STARTF_USESTDHANDLES, STARTF_USESHOWWINDOW
+
+ __all__.extend(["CREATE_NEW_CONSOLE", "CREATE_NEW_PROCESS_GROUP",
+ "STD_INPUT_HANDLE", "STD_OUTPUT_HANDLE",
+ "STD_ERROR_HANDLE", "SW_HIDE",
+ "STARTF_USESTDHANDLES", "STARTF_USESHOWWINDOW"])
+try:
+ MAXFD = os.sysconf("SC_OPEN_MAX")
+except:
+ MAXFD = 256
+
+_active = []
+
+
+def _cleanup():
+ for inst in _active[:]:
+ res = inst._internal_poll(_deadstate=sys.maxint)
+ if res is not None:
+ try:
+ _active.remove(inst)
+ except ValueError:
+ # This can happen if two threads create a new Popen instance.
+ # It's harmless that it was already removed, so ignore.
+ pass
+
+PIPE = -1
+STDOUT = -2
+
+
+def _eintr_retry_call(func, *args):
+ while True:
+ try:
+ return func(*args)
+ except (OSError, IOError), e:
+ if e.errno == errno.EINTR:
+ continue
+ raise
+
+
+def call(*popenargs, **kwargs):
+ """Run command with arguments. Wait for command to complete, then
+ return the returncode attribute.
+
+ The arguments are the same as for the Popen constructor. Example:
+
+ retcode = call(["ls", "-l"])
+ """
+ return Popen(*popenargs, **kwargs).wait()
+
+
+def check_call(*popenargs, **kwargs):
+ """Run command with arguments. Wait for command to complete. If
+ the exit code was zero then return, otherwise raise
+ CalledProcessError. The CalledProcessError object will have the
+ return code in the returncode attribute.
+
+ The arguments are the same as for the Popen constructor. Example:
+
+ check_call(["ls", "-l"])
+ """
+ retcode = call(*popenargs, **kwargs)
+ if retcode:
+ cmd = kwargs.get("args")
+ if cmd is None:
+ cmd = popenargs[0]
+ raise CalledProcessError(retcode, cmd)
+ return 0
+
+
+def check_output(*popenargs, **kwargs):
+ r"""Run command with arguments and return its output as a byte string.
+
+ If the exit code was non-zero it raises a CalledProcessError. The
+ CalledProcessError object will have the return code in the returncode
+ attribute and output in the output attribute.
+
+ The arguments are the same as for the Popen constructor. Example:
+
+ >>> check_output(["ls", "-l", "/dev/null"])
+ 'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n'
+
+ The stdout argument is not allowed as it is used internally.
+ To capture standard error in the result, use stderr=STDOUT.
+
+ >>> check_output(["/bin/sh", "-c",
+ ... "ls -l non_existent_file ; exit 0"],
+ ... stderr=STDOUT)
+ 'ls: non_existent_file: No such file or directory\n'
+ """
+ if 'stdout' in kwargs:
+ raise ValueError('stdout argument not allowed, it will be overridden.')
+ process = Popen(stdout=PIPE, *popenargs, **kwargs)
+ output, unused_err = process.communicate()
+ retcode = process.poll()
+ if retcode:
+ cmd = kwargs.get("args")
+ if cmd is None:
+ cmd = popenargs[0]
+ raise CalledProcessError(retcode, cmd, output=output)
+ return output
+
+
+def list2cmdline(seq):
+ """
+ Translate a sequence of arguments into a command line
+ string, using the same rules as the MS C runtime:
+
+ 1) Arguments are delimited by white space, which is either a
+ space or a tab.
+
+ 2) A string surrounded by double quotation marks is
+ interpreted as a single argument, regardless of white space
+ contained within. A quoted string can be embedded in an
+ argument.
+
+ 3) A double quotation mark preceded by a backslash is
+ interpreted as a literal double quotation mark.
+
+ 4) Backslashes are interpreted literally, unless they
+ immediately precede a double quotation mark.
+
+ 5) If backslashes immediately precede a double quotation mark,
+ every pair of backslashes is interpreted as a literal
+ backslash. If the number of backslashes is odd, the last
+ backslash escapes the next double quotation mark as
+ described in rule 3.
+ """
+
+ # See
+ # http://msdn.microsoft.com/en-us/library/17w5ykft.aspx
+ # or search http://msdn.microsoft.com for
+ # "Parsing C++ Command-Line Arguments"
+ result = []
+ needquote = False
+ for arg in seq:
+ bs_buf = []
+
+ # Add a space to separate this argument from the others
+ if result:
+ result.append(' ')
+
+ needquote = (" " in arg) or ("\t" in arg) or not arg
+ if needquote:
+ result.append('"')
+
+ for c in arg:
+ if c == '\\':
+ # Don't know if we need to double yet.
+ bs_buf.append(c)
+ elif c == '"':
+ # Double backslashes.
+ result.append('\\' * len(bs_buf) * 2)
+ bs_buf = []
+ result.append('\\"')
+ else:
+ # Normal char
+ if bs_buf:
+ result.extend(bs_buf)
+ bs_buf = []
+ result.append(c)
+
+ # Add remaining backslashes, if any.
+ if bs_buf:
+ result.extend(bs_buf)
+
+ if needquote:
+ result.extend(bs_buf)
+ result.append('"')
+
+ return ''.join(result)
+
+
+class Popen(object):
+
+ def __init__(self, args, bufsize=0, executable=None,
+ stdin=None, stdout=None, stderr=None,
+ preexec_fn=None, close_fds=False, shell=False,
+ cwd=None, env=None, universal_newlines=False,
+ startupinfo=None, creationflags=0):
+ """Create new Popen instance."""
+ _cleanup()
+
+ self._child_created = False
+ if not isinstance(bufsize, (int, long)):
+ raise TypeError("bufsize must be an integer")
+
+ if mswindows:
+ if preexec_fn is not None:
+ raise ValueError("preexec_fn is not supported on Windows "
+ "platforms")
+ if close_fds and (stdin is not None or stdout is not None or
+ stderr is not None):
+ raise ValueError("close_fds is not supported on Windows "
+ "platforms if you redirect "
+ "stdin/stdout/stderr")
+ else:
+ # POSIX
+ if startupinfo is not None:
+ raise ValueError("startupinfo is only supported on Windows "
+ "platforms")
+ if creationflags != 0:
+ raise ValueError("creationflags is only supported on Windows "
+ "platforms")
+
+ self.stdin = None
+ self.stdout = None
+ self.stderr = None
+ self.pid = None
+ self.returncode = None
+ self.universal_newlines = universal_newlines
+
+ # Input and output objects. The general principle is like
+ # this:
+ #
+ # Parent Child
+ # ------ -----
+ # p2cwrite ---stdin---> p2cread
+ # c2pread <--stdout--- c2pwrite
+ # errread <--stderr--- errwrite
+ #
+ # On POSIX, the child objects are file descriptors. On
+ # Windows, these are Windows file handles. The parent objects
+ # are file descriptors on both platforms. The parent objects
+ # are None when not using PIPEs. The child objects are None
+ # when not redirecting.
+
+ (p2cread, p2cwrite,
+ c2pread, c2pwrite,
+ errread, errwrite) = self._get_handles(stdin, stdout, stderr)
+
+ self._execute_child(args, executable, preexec_fn, close_fds,
+ cwd, env, universal_newlines,
+ startupinfo, creationflags, shell,
+ p2cread, p2cwrite,
+ c2pread, c2pwrite,
+ errread, errwrite)
+
+ if mswindows:
+ if p2cwrite is not None:
+ p2cwrite = msvcrt.open_osfhandle(p2cwrite.Detach(), 0)
+ if c2pread is not None:
+ c2pread = msvcrt.open_osfhandle(c2pread.Detach(), 0)
+ if errread is not None:
+ errread = msvcrt.open_osfhandle(errread.Detach(), 0)
+
+ if p2cwrite is not None:
+ self.stdin = os.fdopen(p2cwrite, 'wb', bufsize)
+ if c2pread is not None:
+ if universal_newlines:
+ self.stdout = os.fdopen(c2pread, 'rU', bufsize)
+ else:
+ self.stdout = os.fdopen(c2pread, 'rb', bufsize)
+ if errread is not None:
+ if universal_newlines:
+ self.stderr = os.fdopen(errread, 'rU', bufsize)
+ else:
+ self.stderr = os.fdopen(errread, 'rb', bufsize)
+
+ def _translate_newlines(self, data):
+ data = data.replace("\r\n", "\n")
+ data = data.replace("\r", "\n")
+ return data
+
+ def __del__(self, _maxint=sys.maxint, _active=_active):
+ # If __init__ hasn't had a chance to execute (e.g. if it
+ # was passed an undeclared keyword argument), we don't
+ # have a _child_created attribute at all.
+ if not getattr(self, '_child_created', False):
+ # We didn't get to successfully create a child process.
+ return
+ # In case the child hasn't been waited on, check if it's done.
+ self._internal_poll(_deadstate=_maxint)
+ if self.returncode is None and _active is not None:
+ # Child is still running, keep us alive until we can wait on it.
+ _active.append(self)
+
+ def communicate(self, input=None):
+ """Interact with process: Send data to stdin. Read data from
+ stdout and stderr, until end-of-file is reached. Wait for
+ process to terminate. The optional input argument should be a
+ string to be sent to the child process, or None, if no data
+ should be sent to the child.
+
+ communicate() returns a tuple (stdout, stderr)."""
+
+ # Optimization: If we are only using one pipe, or no pipe at
+ # all, using select() or threads is unnecessary.
+ if [self.stdin, self.stdout, self.stderr].count(None) >= 2:
+ stdout = None
+ stderr = None
+ if self.stdin:
+ if input:
+ try:
+ self.stdin.write(input)
+ except IOError, e:
+ if e.errno != errno.EPIPE and e.errno != errno.EINVAL:
+ raise
+ self.stdin.close()
+ elif self.stdout:
+ stdout = _eintr_retry_call(self.stdout.read)
+ self.stdout.close()
+ elif self.stderr:
+ stderr = _eintr_retry_call(self.stderr.read)
+ self.stderr.close()
+ self.wait()
+ return (stdout, stderr)
+
+ return self._communicate(input)
+
+ def poll(self):
+ return self._internal_poll()
+
+ if mswindows:
+ #
+ # Windows methods
+ #
+ def _get_handles(self, stdin, stdout, stderr):
+ """Construct and return tuple with IO objects:
+ p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite
+ """
+ if stdin is None and stdout is None and stderr is None:
+ return (None, None, None, None, None, None)
+
+ p2cread, p2cwrite = None, None
+ c2pread, c2pwrite = None, None
+ errread, errwrite = None, None
+
+ if stdin is None:
+ p2cread = _subprocess.GetStdHandle(
+ _subprocess.STD_INPUT_HANDLE)
+ if p2cread is None:
+ p2cread, _ = _subprocess.CreatePipe(None, 0)
+ elif stdin == PIPE:
+ p2cread, p2cwrite = _subprocess.CreatePipe(None, 0)
+ elif isinstance(stdin, int):
+ p2cread = msvcrt.get_osfhandle(stdin)
+ else:
+ # Assuming file-like object
+ p2cread = msvcrt.get_osfhandle(stdin.fileno())
+ p2cread = self._make_inheritable(p2cread)
+
+ if stdout is None:
+ c2pwrite = _subprocess.GetStdHandle(
+ _subprocess.STD_OUTPUT_HANDLE)
+ if c2pwrite is None:
+ _, c2pwrite = _subprocess.CreatePipe(None, 0)
+ elif stdout == PIPE:
+ c2pread, c2pwrite = _subprocess.CreatePipe(None, 0)
+ elif isinstance(stdout, int):
+ c2pwrite = msvcrt.get_osfhandle(stdout)
+ else:
+ # Assuming file-like object
+ c2pwrite = msvcrt.get_osfhandle(stdout.fileno())
+ c2pwrite = self._make_inheritable(c2pwrite)
+
+ if stderr is None:
+ errwrite = _subprocess.GetStdHandle(
+ _subprocess.STD_ERROR_HANDLE)
+ if errwrite is None:
+ _, errwrite = _subprocess.CreatePipe(None, 0)
+ elif stderr == PIPE:
+ errread, errwrite = _subprocess.CreatePipe(None, 0)
+ elif stderr == STDOUT:
+ errwrite = c2pwrite
+ elif isinstance(stderr, int):
+ errwrite = msvcrt.get_osfhandle(stderr)
+ else:
+ # Assuming file-like object
+ errwrite = msvcrt.get_osfhandle(stderr.fileno())
+ errwrite = self._make_inheritable(errwrite)
+
+ return (p2cread, p2cwrite,
+ c2pread, c2pwrite,
+ errread, errwrite)
+
+ def _make_inheritable(self, handle):
+ """Return a duplicate of handle, which is inheritable"""
+ return _subprocess.DuplicateHandle(
+ _subprocess.GetCurrentProcess(),
+ handle,
+ _subprocess.GetCurrentProcess(),
+ 0,
+ 1,
+ _subprocess.DUPLICATE_SAME_ACCESS
+ )
+
+ def _find_w9xpopen(self):
+ """Find and return absolut path to w9xpopen.exe"""
+ w9xpopen = os.path.join(
+ os.path.dirname(_subprocess.GetModuleFileName(0)),
+ "w9xpopen.exe")
+ if not os.path.exists(w9xpopen):
+ # Eeek - file-not-found - possibly an embedding
+ # situation - see if we can locate it in sys.exec_prefix
+ w9xpopen = os.path.join(os.path.dirname(sys.exec_prefix),
+ "w9xpopen.exe")
+ if not os.path.exists(w9xpopen):
+ raise RuntimeError("Cannot locate w9xpopen.exe, which is "
+ "needed for Popen to work with your "
+ "shell or platform.")
+ return w9xpopen
+
+ def _execute_child(self, args, executable, preexec_fn, close_fds,
+ cwd, env, universal_newlines,
+ startupinfo, creationflags, shell,
+ p2cread, p2cwrite,
+ c2pread, c2pwrite,
+ errread, errwrite):
+ """Execute program (MS Windows version)"""
+
+ if not isinstance(args, types.StringTypes):
+ args = list2cmdline(args)
+
+ # Process startup details
+ if startupinfo is None:
+ startupinfo = STARTUPINFO()
+ if None not in (p2cread, c2pwrite, errwrite):
+ startupinfo.dwFlags |= _subprocess.STARTF_USESTDHANDLES
+ startupinfo.hStdInput = p2cread
+ startupinfo.hStdOutput = c2pwrite
+ startupinfo.hStdError = errwrite
+
+ if shell:
+ startupinfo.dwFlags |= _subprocess.STARTF_USESHOWWINDOW
+ startupinfo.wShowWindow = _subprocess.SW_HIDE
+ comspec = os.environ.get("COMSPEC", "cmd.exe")
+ args = '{} /c "{}"'.format(comspec, args)
+ if (_subprocess.GetVersion() >= 0x80000000 or
+ os.path.basename(comspec).lower() == "command.com"):
+ # Win9x, or using command.com on NT. We need to
+ # use the w9xpopen intermediate program. For more
+ # information, see KB Q150956
+ # (http://web.archive.org/web/20011105084002/http://support.microsoft.com/support/kb/articles/Q150/9/56.asp)
+ w9xpopen = self._find_w9xpopen()
+ args = '"%s" %s' % (w9xpopen, args)
+ # Not passing CREATE_NEW_CONSOLE has been known to
+ # cause random failures on win9x. Specifically a
+ # dialog: "Your program accessed mem currently in
+ # use at xxx" and a hopeful warning about the
+ # stability of your system. Cost is Ctrl+C wont
+ # kill children.
+ creationflags |= _subprocess.CREATE_NEW_CONSOLE
+
+ # Start the process
+ try:
+ try:
+ hp, ht, pid, tid = _subprocess.CreateProcess(
+ executable, args,
+ # no special
+ # security
+ None, None,
+ int(not close_fds),
+ creationflags,
+ env,
+ cwd,
+ startupinfo)
+ except pywintypes.error, e:
+ # Translate pywintypes.error to WindowsError, which is
+ # a subclass of OSError. FIXME: We should really
+ # translate errno using _sys_errlist (or similar), but
+ # how can this be done from Python?
+ raise WindowsError(*e.args)
+ finally:
+ # Child is launched. Close the parent's copy of those pipe
+ # handles that only the child should have open. You need
+ # to make sure that no handles to the write end of the
+ # output pipe are maintained in this process or else the
+ # pipe will not close when the child process exits and the
+ # ReadFile will hang.
+ if p2cread is not None:
+ p2cread.Close()
+ if c2pwrite is not None:
+ c2pwrite.Close()
+ if errwrite is not None:
+ errwrite.Close()
+
+ # Retain the process handle, but close the thread handle
+ self._child_created = True
+ self._handle = hp
+ self.pid = pid
+ ht.Close()
+
+ def _internal_poll(
+ self, _deadstate=None,
+ _WaitForSingleObject=_subprocess.WaitForSingleObject,
+ _WAIT_OBJECT_0=_subprocess.WAIT_OBJECT_0,
+ _GetExitCodeProcess=_subprocess.GetExitCodeProcess
+ ):
+ """Check if child process has terminated. Returns returncode
+ attribute.
+
+ This method is called by __del__, so it can only refer to objects
+ in its local scope.
+
+ """
+ if self.returncode is None:
+ if _WaitForSingleObject(self._handle, 0) == _WAIT_OBJECT_0:
+ self.returncode = _GetExitCodeProcess(self._handle)
+ return self.returncode
+
+ def wait(self):
+ """Wait for child process to terminate. Returns returncode
+ attribute."""
+ if self.returncode is None:
+ _subprocess.WaitForSingleObject(self._handle,
+ _subprocess.INFINITE)
+ self.returncode = _subprocess.GetExitCodeProcess(self._handle)
+ return self.returncode
+
+ def _readerthread(self, fh, buffer):
+ buffer.append(fh.read())
+
+ def _communicate(self, input):
+ stdout = None # Return
+ stderr = None # Return
+
+ if self.stdout:
+ stdout = []
+ stdout_thread = threading.Thread(target=self._readerthread,
+ args=(self.stdout, stdout))
+ stdout_thread.setDaemon(True)
+ stdout_thread.start()
+ if self.stderr:
+ stderr = []
+ stderr_thread = threading.Thread(target=self._readerthread,
+ args=(self.stderr, stderr))
+ stderr_thread.setDaemon(True)
+ stderr_thread.start()
+
+ if self.stdin:
+ if input is not None:
+ try:
+ self.stdin.write(input)
+ except IOError, e:
+ if e.errno != errno.EPIPE:
+ raise
+ self.stdin.close()
+
+ if self.stdout:
+ stdout_thread.join()
+ if self.stderr:
+ stderr_thread.join()
+
+ # All data exchanged. Translate lists into strings.
+ if stdout is not None:
+ stdout = stdout[0]
+ if stderr is not None:
+ stderr = stderr[0]
+
+ # Translate newlines, if requested. We cannot let the file
+ # object do the translation: It is based on stdio, which is
+ # impossible to combine with select (unless forcing no
+ # buffering).
+ if self.universal_newlines and hasattr(file, 'newlines'):
+ if stdout:
+ stdout = self._translate_newlines(stdout)
+ if stderr:
+ stderr = self._translate_newlines(stderr)
+
+ self.wait()
+ return (stdout, stderr)
+
+ def send_signal(self, sig):
+ """Send a signal to the process
+ """
+ if sig == signal.SIGTERM:
+ self.terminate()
+ elif sig == signal.CTRL_C_EVENT:
+ os.kill(self.pid, signal.CTRL_C_EVENT)
+ elif sig == signal.CTRL_BREAK_EVENT:
+ os.kill(self.pid, signal.CTRL_BREAK_EVENT)
+ else:
+ raise ValueError("Unsupported signal: {}".format(sig))
+
+ def terminate(self):
+ """Terminates the process
+ """
+ _subprocess.TerminateProcess(self._handle, 1)
+
+ kill = terminate
+
+ else:
+ #
+ # POSIX methods
+ #
+ def _get_handles(self, stdin, stdout, stderr):
+ """Construct and return tuple with IO objects:
+ p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite
+ """
+ p2cread, p2cwrite = None, None
+ c2pread, c2pwrite = None, None
+ errread, errwrite = None, None
+
+ if stdin is None:
+ pass
+ elif stdin == PIPE:
+ p2cread, p2cwrite = self.pipe_cloexec()
+ elif isinstance(stdin, int):
+ p2cread = stdin
+ else:
+ # Assuming file-like object
+ p2cread = stdin.fileno()
+
+ if stdout is None:
+ pass
+ elif stdout == PIPE:
+ c2pread, c2pwrite = self.pipe_cloexec()
+ elif isinstance(stdout, int):
+ c2pwrite = stdout
+ else:
+ # Assuming file-like object
+ c2pwrite = stdout.fileno()
+
+ if stderr is None:
+ pass
+ elif stderr == PIPE:
+ errread, errwrite = self.pipe_cloexec()
+ elif stderr == STDOUT:
+ errwrite = c2pwrite
+ elif isinstance(stderr, int):
+ errwrite = stderr
+ else:
+ # Assuming file-like object
+ errwrite = stderr.fileno()
+
+ return (p2cread, p2cwrite,
+ c2pread, c2pwrite,
+ errread, errwrite)
+
+ def _set_cloexec_flag(self, fd, cloexec=True):
+ try:
+ cloexec_flag = fcntl.FD_CLOEXEC
+ except AttributeError:
+ cloexec_flag = 1
+
+ old = fcntl.fcntl(fd, fcntl.F_GETFD)
+ if cloexec:
+ fcntl.fcntl(fd, fcntl.F_SETFD, old | cloexec_flag)
+ else:
+ fcntl.fcntl(fd, fcntl.F_SETFD, old & ~cloexec_flag)
+
+ def pipe_cloexec(self):
+ """Create a pipe with FDs set CLOEXEC."""
+ # Pipes' FDs are set CLOEXEC by default because we don't want them
+ # to be inherited by other subprocesses: the CLOEXEC flag is
+ # removed from the child's FDs by _dup2(), between fork() and
+ # exec().
+ # This is not atomic: we would need the pipe2() syscall for that.
+ r, w = os.pipe()
+ self._set_cloexec_flag(r)
+ self._set_cloexec_flag(w)
+ return r, w
+
+ def _close_fds(self, but):
+ if hasattr(os, 'closerange'):
+ os.closerange(3, but)
+ os.closerange(but + 1, MAXFD)
+ else:
+ for i in xrange(3, MAXFD):
+ if i == but:
+ continue
+ try:
+ os.close(i)
+ except:
+ pass
+
+ def _execute_child(self, args, executable, preexec_fn, close_fds,
+ cwd, env, universal_newlines,
+ startupinfo, creationflags, shell,
+ p2cread, p2cwrite,
+ c2pread, c2pwrite,
+ errread, errwrite):
+ """Execute program (POSIX version)"""
+
+ if isinstance(args, types.StringTypes):
+ args = [args]
+ else:
+ args = list(args)
+
+ if shell:
+ args = ["/bin/sh", "-c"] + args
+ if executable:
+ args[0] = executable
+
+ if executable is None:
+ executable = args[0]
+
+ # For transferring possible exec failure from child to parent
+ # The first char specifies the exception type: 0 means
+ # OSError, 1 means some other error.
+ errpipe_read, errpipe_write = self.pipe_cloexec()
+ try:
+ try:
+ gc_was_enabled = gc.isenabled()
+ # Disable gc to avoid bug where gc -> file_dealloc ->
+ # write to stderr -> hang.
+ # http://bugs.python.org/issue1336
+ gc.disable()
+ try:
+ self.pid = os.fork()
+ except:
+ if gc_was_enabled:
+ gc.enable()
+ raise
+ self._child_created = True
+ if self.pid == 0:
+ # Child
+ try:
+ # Close parent's pipe ends
+ if p2cwrite is not None:
+ os.close(p2cwrite)
+ if c2pread is not None:
+ os.close(c2pread)
+ if errread is not None:
+ os.close(errread)
+ os.close(errpipe_read)
+
+ # When duping fds, if there arises a situation
+ # where one of the fds is either 0, 1 or 2, it
+ # is possible that it is overwritten (#12607).
+ if c2pwrite == 0:
+ c2pwrite = os.dup(c2pwrite)
+ if errwrite == 0 or errwrite == 1:
+ errwrite = os.dup(errwrite)
+
+ # Dup fds for child
+ def _dup2(a, b):
+ # dup2() removes the CLOEXEC flag but
+ # we must do it ourselves if dup2()
+ # would be a no-op (issue #10806).
+ if a == b:
+ self._set_cloexec_flag(a, False)
+ elif a is not None:
+ os.dup2(a, b)
+ _dup2(p2cread, 0)
+ _dup2(c2pwrite, 1)
+ _dup2(errwrite, 2)
+
+ # Close pipe fds. Make sure we don't close the
+ # same fd more than once, or standard fds.
+ closed = set([None])
+ for fd in [p2cread, c2pwrite, errwrite]:
+ if fd not in closed and fd > 2:
+ os.close(fd)
+ closed.add(fd)
+
+ # Close all other fds, if asked for
+ if close_fds:
+ self._close_fds(but=errpipe_write)
+
+ if cwd is not None:
+ os.chdir(cwd)
+
+ if preexec_fn:
+ preexec_fn()
+
+ if env is None:
+ os.execvp(executable, args)
+ else:
+ os.execvpe(executable, args, env)
+
+ except:
+ exc_type, exc_value, tb = sys.exc_info()
+ # Save the traceback and attach it to the exception
+ # object
+ exc_lines = traceback.format_exception(exc_type,
+ exc_value,
+ tb)
+ exc_value.child_traceback = ''.join(exc_lines)
+ os.write(errpipe_write, pickle.dumps(exc_value))
+
+ # This exitcode won't be reported to applications,
+ # so it really doesn't matter what we return.
+ os._exit(255)
+
+ # Parent
+ if gc_was_enabled:
+ gc.enable()
+ finally:
+ # be sure the FD is closed no matter what
+ os.close(errpipe_write)
+
+ if p2cread is not None and p2cwrite is not None:
+ os.close(p2cread)
+ if c2pwrite is not None and c2pread is not None:
+ os.close(c2pwrite)
+ if errwrite is not None and errread is not None:
+ os.close(errwrite)
+
+ # Wait for exec to fail or succeed; possibly raising exception
+ # Exception limited to 1M
+ data = _eintr_retry_call(os.read, errpipe_read, 1048576)
+ finally:
+ # be sure the FD is closed no matter what
+ os.close(errpipe_read)
+
+ if data != "":
+ try:
+ _eintr_retry_call(os.waitpid, self.pid, 0)
+ except OSError, e:
+ if e.errno != errno.ECHILD:
+ raise
+ child_exception = pickle.loads(data)
+ for fd in (p2cwrite, c2pread, errread):
+ if fd is not None:
+ os.close(fd)
+ raise child_exception
+
+ def _handle_exitstatus(self, sts, _WIFSIGNALED=os.WIFSIGNALED,
+ _WTERMSIG=os.WTERMSIG, _WIFEXITED=os.WIFEXITED,
+ _WEXITSTATUS=os.WEXITSTATUS):
+ # This method is called (indirectly) by __del__, so it cannot
+ # refer to anything outside of its local scope."""
+ if _WIFSIGNALED(sts):
+ self.returncode = -_WTERMSIG(sts)
+ elif _WIFEXITED(sts):
+ self.returncode = _WEXITSTATUS(sts)
+ else:
+ # Should never happen
+ raise RuntimeError("Unknown child exit status!")
+
+ def _internal_poll(self, _deadstate=None, _waitpid=os.waitpid,
+ _WNOHANG=os.WNOHANG, _os_error=os.error):
+ """Check if child process has terminated. Returns returncode
+ attribute.
+
+ This method is called by __del__, so it cannot reference anything
+ outside of the local scope (nor can any methods it calls).
+
+ """
+ if self.returncode is None:
+ try:
+ pid, sts = _waitpid(self.pid, _WNOHANG)
+ if pid == self.pid:
+ self._handle_exitstatus(sts)
+ except _os_error:
+ if _deadstate is not None:
+ self.returncode = _deadstate
+ return self.returncode
+
+ def wait(self):
+ """Wait for child process to terminate. Returns returncode
+ attribute."""
+ if self.returncode is None:
+ try:
+ pid, sts = _eintr_retry_call(os.waitpid, self.pid, 0)
+ except OSError, e:
+ if e.errno != errno.ECHILD:
+ raise
+ # This happens if SIGCLD is set to be ignored or waiting
+ # for child processes has otherwise been disabled for our
+ # process. This child is dead, we can't get the status.
+ sts = 0
+ self._handle_exitstatus(sts)
+ return self.returncode
+
+ def _communicate(self, input):
+ if self.stdin:
+ # Flush stdio buffer. This might block, if the user has
+ # been writing to .stdin in an uncontrolled fashion.
+ self.stdin.flush()
+ if not input:
+ self.stdin.close()
+
+ if _has_poll:
+ stdout, stderr = self._communicate_with_poll(input)
+ else:
+ stdout, stderr = self._communicate_with_select(input)
+
+ # All data exchanged. Translate lists into strings.
+ if stdout is not None:
+ stdout = ''.join(stdout)
+ if stderr is not None:
+ stderr = ''.join(stderr)
+
+ # Translate newlines, if requested. We cannot let the file
+ # object do the translation: It is based on stdio, which is
+ # impossible to combine with select (unless forcing no
+ # buffering).
+ if self.universal_newlines and hasattr(file, 'newlines'):
+ if stdout:
+ stdout = self._translate_newlines(stdout)
+ if stderr:
+ stderr = self._translate_newlines(stderr)
+
+ self.wait()
+ return (stdout, stderr)
+
+ def _communicate_with_poll(self, input):
+ stdout = None # Return
+ stderr = None # Return
+ fd2file = {}
+ fd2output = {}
+
+ poller = select.poll()
+
+ def register_and_append(file_obj, eventmask):
+ poller.register(file_obj.fileno(), eventmask)
+ fd2file[file_obj.fileno()] = file_obj
+
+ def close_unregister_and_remove(fd):
+ poller.unregister(fd)
+ fd2file[fd].close()
+ fd2file.pop(fd)
+
+ if self.stdin and input:
+ register_and_append(self.stdin, select.POLLOUT)
+
+ select_POLLIN_POLLPRI = select.POLLIN | select.POLLPRI
+ if self.stdout:
+ register_and_append(self.stdout, select_POLLIN_POLLPRI)
+ fd2output[self.stdout.fileno()] = stdout = []
+ if self.stderr:
+ register_and_append(self.stderr, select_POLLIN_POLLPRI)
+ fd2output[self.stderr.fileno()] = stderr = []
+
+ input_offset = 0
+ while fd2file:
+ try:
+ ready = poller.poll()
+ except select.error, e:
+ if e.args[0] == errno.EINTR:
+ continue
+ raise
+
+ for fd, mode in ready:
+ if mode & select.POLLOUT:
+ chunk = input[input_offset: input_offset + _PIPE_BUF]
+ try:
+ input_offset += os.write(fd, chunk)
+ except OSError, e:
+ if e.errno == errno.EPIPE:
+ close_unregister_and_remove(fd)
+ else:
+ raise
+ else:
+ if input_offset >= len(input):
+ close_unregister_and_remove(fd)
+ elif mode & select_POLLIN_POLLPRI:
+ data = os.read(fd, 4096)
+ if not data:
+ close_unregister_and_remove(fd)
+ fd2output[fd].append(data)
+ else:
+ # Ignore hang up or errors.
+ close_unregister_and_remove(fd)
+
+ return (stdout, stderr)
+
+ def _communicate_with_select(self, input):
+ read_set = []
+ write_set = []
+ stdout = None # Return
+ stderr = None # Return
+
+ if self.stdin and input:
+ write_set.append(self.stdin)
+ if self.stdout:
+ read_set.append(self.stdout)
+ stdout = []
+ if self.stderr:
+ read_set.append(self.stderr)
+ stderr = []
+
+ input_offset = 0
+ while read_set or write_set:
+ try:
+ rlist, wlist, xlist = select.select(
+ read_set, write_set, [])
+ except select.error, e:
+ if e.args[0] == errno.EINTR:
+ continue
+ raise
+
+ if self.stdin in wlist:
+ chunk = input[input_offset: input_offset + _PIPE_BUF]
+ try:
+ bytes_written = os.write(self.stdin.fileno(), chunk)
+ except OSError, e:
+ if e.errno == errno.EPIPE:
+ self.stdin.close()
+ write_set.remove(self.stdin)
+ else:
+ raise
+ else:
+ input_offset += bytes_written
+ if input_offset >= len(input):
+ self.stdin.close()
+ write_set.remove(self.stdin)
+
+ if self.stdout in rlist:
+ data = os.read(self.stdout.fileno(), 1024)
+ if data == "":
+ self.stdout.close()
+ read_set.remove(self.stdout)
+ stdout.append(data)
+
+ if self.stderr in rlist:
+ data = os.read(self.stderr.fileno(), 1024)
+ if data == "":
+ self.stderr.close()
+ read_set.remove(self.stderr)
+ stderr.append(data)
+
+ return (stdout, stderr)
+
+ def send_signal(self, sig):
+ """Send a signal to the process
+ """
+ os.kill(self.pid, sig)
+
+ def terminate(self):
+ """Terminate the process with SIGTERM
+ """
+ self.send_signal(signal.SIGTERM)
+
+ def kill(self):
+ """Kill the process with SIGKILL
+ """
+ self.send_signal(signal.SIGKILL)
+
+
+def _demo_posix():
+ #
+ # Example 1: Simple redirection: Get process list
+ #
+ plist = Popen(["ps"], stdout=PIPE).communicate()[0]
+ print "Process list:"
+ print plist
+
+ #
+ # Example 2: Change uid before executing child
+ #
+ if os.getuid() == 0:
+ p = Popen(["id"], preexec_fn=lambda: os.setuid(100))
+ p.wait()
+
+ #
+ # Example 3: Connecting several subprocesses
+ #
+ print "Looking for 'hda'..."
+ p1 = Popen(["dmesg"], stdout=PIPE)
+ p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
+ print repr(p2.communicate()[0])
+
+ #
+ # Example 4: Catch execution error
+ #
+ print
+ print "Trying a weird file..."
+ try:
+ print Popen(["/this/path/does/not/exist"]).communicate()
+ except OSError, e:
+ if e.errno == errno.ENOENT:
+ print "The file didn't exist. I thought so..."
+ print "Child traceback:"
+ print e.child_traceback
+ else:
+ print "Error", e.errno
+ else:
+ print >>sys.stderr, "Gosh. No error."
+
+
+def _demo_windows():
+ #
+ # Example 1: Connecting several subprocesses
+ #
+ print "Looking for 'PROMPT' in set output..."
+ p1 = Popen("set", stdout=PIPE, shell=True)
+ p2 = Popen('find "PROMPT"', stdin=p1.stdout, stdout=PIPE)
+ print repr(p2.communicate()[0])
+
+ #
+ # Example 2: Simple execution of program
+ #
+ print "Executing calc..."
+ p = Popen("calc")
+ p.wait()
+
+
+if __name__ == "__main__":
+ if mswindows:
+ _demo_windows()
+ else:
+ _demo_posix()
diff --git a/cherrypy/_cpconfig.py b/cherrypy/_cpconfig.py
index 7b4c6a46..c11bc1d1 100644
--- a/cherrypy/_cpconfig.py
+++ b/cherrypy/_cpconfig.py
@@ -50,14 +50,14 @@ attribute. For example::
class Demo:
_cp_config = {'tools.gzip.on': True}
-
+
def index(self):
return "Hello world"
index.exposed = True
index._cp_config = {'request.show_tracebacks': False}
.. note::
-
+
This behavior is only guaranteed for the default dispatcher.
Other dispatchers may have different restrictions on where
you can attach _cp_config attributes.
@@ -125,15 +125,16 @@ from cherrypy.lib import reprconf
# Deprecated in CherryPy 3.2--remove in 3.3
NamespaceSet = reprconf.NamespaceSet
+
def merge(base, other):
"""Merge one app config (from a dict, file, or filename) into another.
-
+
If the given config is a filename, it will be appended to
the list of files to monitor for "autoreload" changes.
"""
if isinstance(other, basestring):
cherrypy.engine.autoreload.files.add(other)
-
+
# Load other into base
for section, value_map in reprconf.as_dict(other).items():
if not isinstance(value_map, dict):
@@ -146,6 +147,7 @@ def merge(base, other):
class Config(reprconf.Config):
+
"""The 'global' configuration data for the entire CherryPy process."""
def update(self, config):
@@ -157,20 +159,21 @@ class Config(reprconf.Config):
def _apply(self, config):
"""Update self from a dict."""
- if isinstance(config.get("global", None), dict):
+ if isinstance(config.get("global"), dict):
if len(config) > 1:
cherrypy.checker.global_config_contained_paths = True
config = config["global"]
if 'tools.staticdir.dir' in config:
config['tools.staticdir.section'] = "global"
reprconf.Config._apply(self, config)
-
+
def __call__(self, *args, **kwargs):
"""Decorator for page handlers to set _cp_config."""
if args:
raise TypeError(
"The cherrypy.config decorator does not accept positional "
"arguments; you must use keyword arguments.")
+
def tool_decorator(f):
if not hasattr(f, "_cp_config"):
f._cp_config = {}
@@ -180,25 +183,26 @@ class Config(reprconf.Config):
return tool_decorator
+# Sphinx begin config.environments
Config.environments = environments = {
"staging": {
- 'engine.autoreload_on': False,
+ 'engine.autoreload.on': False,
'checker.on': False,
'tools.log_headers.on': False,
'request.show_tracebacks': False,
'request.show_mismatched_params': False,
- },
+ },
"production": {
- 'engine.autoreload_on': False,
+ 'engine.autoreload.on': False,
'checker.on': False,
'tools.log_headers.on': False,
'request.show_tracebacks': False,
'request.show_mismatched_params': False,
'log.screen': False,
- },
+ },
"embedded": {
# For use with CherryPy embedded in another deployment stack.
- 'engine.autoreload_on': False,
+ 'engine.autoreload.on': False,
'checker.on': False,
'tools.log_headers.on': False,
'request.show_tracebacks': False,
@@ -206,16 +210,17 @@ Config.environments = environments = {
'log.screen': False,
'engine.SIGHUP': None,
'engine.SIGTERM': None,
- },
+ },
"test_suite": {
- 'engine.autoreload_on': False,
+ 'engine.autoreload.on': False,
'checker.on': False,
'tools.log_headers.on': False,
'request.show_tracebacks': True,
'request.show_mismatched_params': True,
'log.screen': False,
- },
- }
+ },
+}
+# Sphinx end config.environments
def _server_namespace_handler(k, v):
@@ -226,14 +231,14 @@ def _server_namespace_handler(k, v):
# to configure additional HTTP servers.
if not hasattr(cherrypy, "servers"):
cherrypy.servers = {}
-
+
servername, k = atoms
if servername not in cherrypy.servers:
from cherrypy import _cpserver
cherrypy.servers[servername] = _cpserver.Server()
# On by default, but 'on = False' can unsubscribe it (see below).
cherrypy.servers[servername].subscribe()
-
+
if k == 'on':
if v:
cherrypy.servers[servername].subscribe()
@@ -245,9 +250,24 @@ def _server_namespace_handler(k, v):
setattr(cherrypy.server, k, v)
Config.namespaces["server"] = _server_namespace_handler
+
def _engine_namespace_handler(k, v):
"""Backward compatibility handler for the "engine" namespace."""
engine = cherrypy.engine
+
+ deprecated = {
+ 'autoreload_on': 'autoreload.on',
+ 'autoreload_frequency': 'autoreload.frequency',
+ 'autoreload_match': 'autoreload.match',
+ 'reload_files': 'autoreload.files',
+ 'deadlock_poll_freq': 'timeout_monitor.frequency'
+ }
+
+ if k in deprecated:
+ engine.log(
+ 'WARNING: Use of engine.%s is deprecated and will be removed in a '
+ 'future version. Use engine.%s instead.' % (k, deprecated[k]))
+
if k == 'autoreload_on':
if v:
engine.autoreload.subscribe()
@@ -272,7 +292,10 @@ def _engine_namespace_handler(k, v):
if v and hasattr(getattr(plugin, 'subscribe', None), '__call__'):
plugin.subscribe()
return
- elif (not v) and hasattr(getattr(plugin, 'unsubscribe', None), '__call__'):
+ elif (
+ (not v) and
+ hasattr(getattr(plugin, 'unsubscribe', None), '__call__')
+ ):
plugin.unsubscribe()
return
setattr(plugin, attrname, v)
@@ -286,10 +309,9 @@ def _tree_namespace_handler(k, v):
if isinstance(v, dict):
for script_name, app in v.items():
cherrypy.tree.graft(app, script_name)
- cherrypy.engine.log("Mounted: %s on %s" % (app, script_name or "/"))
+ cherrypy.engine.log("Mounted: %s on %s" %
+ (app, script_name or "/"))
else:
cherrypy.tree.graft(v, v.script_name)
cherrypy.engine.log("Mounted: %s on %s" % (v, v.script_name or "/"))
Config.namespaces["tree"] = _tree_namespace_handler
-
-
diff --git a/cherrypy/_cpdispatch.py b/cherrypy/_cpdispatch.py
index d614e086..7289e494 100644
--- a/cherrypy/_cpdispatch.py
+++ b/cherrypy/_cpdispatch.py
@@ -22,13 +22,40 @@ from cherrypy._cpcompat import set
class PageHandler(object):
+
"""Callable which sets response.body."""
-
+
def __init__(self, callable, *args, **kwargs):
self.callable = callable
self.args = args
self.kwargs = kwargs
-
+
+ def get_args(self):
+ return cherrypy.serving.request.args
+
+ def set_args(self, args):
+ cherrypy.serving.request.args = args
+ return cherrypy.serving.request.args
+
+ args = property(
+ get_args,
+ set_args,
+ doc="The ordered args should be accessible from post dispatch hooks"
+ )
+
+ def get_kwargs(self):
+ return cherrypy.serving.request.kwargs
+
+ def set_kwargs(self, kwargs):
+ cherrypy.serving.request.kwargs = kwargs
+ return cherrypy.serving.request.kwargs
+
+ kwargs = property(
+ get_kwargs,
+ set_kwargs,
+ doc="The named kwargs should be accessible from post dispatch hooks"
+ )
+
def __call__(self):
try:
return self.callable(*self.args, **self.kwargs)
@@ -54,7 +81,8 @@ def test_callable_spec(callable, callable_args, callable_kwargs):
2. Too little parameters are passed to the function.
There are 3 sources of parameters to a cherrypy handler.
- 1. query string parameters are passed as keyword parameters to the handler.
+ 1. query string parameters are passed as keyword parameters to the
+ handler.
2. body parameters are also passed as keyword parameters.
3. when partial matching occurs, the final path atoms are passed as
positional args.
@@ -68,9 +96,10 @@ def test_callable_spec(callable, callable_args, callable_kwargs):
(args, varargs, varkw, defaults) = inspect.getargspec(callable)
except TypeError:
if isinstance(callable, object) and hasattr(callable, '__call__'):
- (args, varargs, varkw, defaults) = inspect.getargspec(callable.__call__)
+ (args, varargs, varkw,
+ defaults) = inspect.getargspec(callable.__call__)
else:
- # If it wasn't one of our own types, re-raise
+ # If it wasn't one of our own types, re-raise
# the original error
raise
@@ -117,15 +146,15 @@ def test_callable_spec(callable, callable_args, callable_kwargs):
# 2. not enough body parameters -> 400
# 3. not enough path parts (partial matches) -> 404
#
- # We can't actually tell which case it is,
+ # We can't actually tell which case it is,
# so I'm raising a 404 because that covers 2/3 of the
# possibilities
- #
+ #
# In the case where the method does not allow body
# arguments it's definitely a 404.
message = None
if show_mismatched_params:
- message="Missing parameters: %s" % ",".join(missing_args)
+ message = "Missing parameters: %s" % ",".join(missing_args)
raise cherrypy.HTTPError(404, message=message)
# the extra positional arguments come from the path - 404 Not Found
@@ -147,8 +176,8 @@ def test_callable_spec(callable, callable_args, callable_kwargs):
message = None
if show_mismatched_params:
- message="Multiple values for parameters: "\
- "%s" % ",".join(multiple_args)
+ message = "Multiple values for parameters: "\
+ "%s" % ",".join(multiple_args)
raise cherrypy.HTTPError(error, message=message)
if not varkw and varkw_usage > 0:
@@ -158,8 +187,8 @@ def test_callable_spec(callable, callable_args, callable_kwargs):
if extra_qs_params:
message = None
if show_mismatched_params:
- message="Unexpected query string "\
- "parameters: %s" % ", ".join(extra_qs_params)
+ message = "Unexpected query string "\
+ "parameters: %s" % ", ".join(extra_qs_params)
raise cherrypy.HTTPError(404, message=message)
# If there were any extra body parameters, it's a 400 Not Found
@@ -167,8 +196,8 @@ def test_callable_spec(callable, callable_args, callable_kwargs):
if extra_body_params:
message = None
if show_mismatched_params:
- message="Unexpected body parameters: "\
- "%s" % ", ".join(extra_body_params)
+ message = "Unexpected body parameters: "\
+ "%s" % ", ".join(extra_body_params)
raise cherrypy.HTTPError(400, message=message)
@@ -178,8 +207,8 @@ except ImportError:
test_callable_spec = lambda callable, args, kwargs: None
-
class LateParamPageHandler(PageHandler):
+
"""When passing cherrypy.request.params to the page handler, we do not
want to capture that dict too early; we want to give tools like the
decoding tool a chance to modify the params dict in-between the lookup
@@ -187,16 +216,17 @@ class LateParamPageHandler(PageHandler):
takes that into account, and allows request.params to be 'bound late'
(it's more complicated than that, but that's the effect).
"""
-
+
def _get_kwargs(self):
kwargs = cherrypy.serving.request.params.copy()
if self._kwargs:
kwargs.update(self._kwargs)
return kwargs
-
+
def _set_kwargs(self, kwargs):
+ cherrypy.serving.request.kwargs = kwargs
self._kwargs = kwargs
-
+
kwargs = property(_get_kwargs, _set_kwargs,
doc='page handler kwargs (with '
'cherrypy.request.params copied in)')
@@ -205,19 +235,24 @@ class LateParamPageHandler(PageHandler):
if sys.version_info < (3, 0):
punctuation_to_underscores = string.maketrans(
string.punctuation, '_' * len(string.punctuation))
+
def validate_translator(t):
if not isinstance(t, str) or len(t) != 256:
- raise ValueError("The translate argument must be a str of len 256.")
+ raise ValueError(
+ "The translate argument must be a str of len 256.")
else:
punctuation_to_underscores = str.maketrans(
string.punctuation, '_' * len(string.punctuation))
+
def validate_translator(t):
if not isinstance(t, dict):
raise ValueError("The translate argument must be a dict.")
+
class Dispatcher(object):
+
"""CherryPy Dispatcher which walks a tree of objects to find a handler.
-
+
The tree is rooted at cherrypy.request.app.root, and each hierarchical
component in the path_info argument is matched to a corresponding nested
attribute of the root object. Matching handlers must have an 'exposed'
@@ -225,16 +260,16 @@ class Dispatcher(object):
matches a URI which ends in a slash ("/"). The special method name
"default" may match a portion of the path_info (but only when no longer
substring of the path_info matches some other object).
-
+
This is the default, built-in dispatcher for CherryPy.
"""
-
+
dispatch_method_name = '_cp_dispatch'
"""
The name of the dispatch method that nodes may optionally implement
to provide their own dynamic dispatch algorithm.
"""
-
+
def __init__(self, dispatch_method_name=None,
translate=punctuation_to_underscores):
validate_translator(translate)
@@ -246,27 +281,27 @@ class Dispatcher(object):
"""Set handler and config for the current request."""
request = cherrypy.serving.request
func, vpath = self.find_handler(path_info)
-
+
if func:
# Decode any leftover %2F in the virtual_path atoms.
vpath = [x.replace("%2F", "/") for x in vpath]
request.handler = LateParamPageHandler(func, *vpath)
else:
request.handler = cherrypy.NotFound()
-
+
def find_handler(self, path):
"""Return the appropriate page handler, plus any virtual path.
-
+
This will return two objects. The first will be a callable,
which can be used to generate page output. Any parameters from
the query string or request body will be sent to that callable
as keyword arguments.
-
+
The callable is found by traversing the application's tree,
starting from cherrypy.request.app.root, and matching path
components to successive objects in the tree. For example, the
URL "/path/to/handler" might return root.path.to.handler.
-
+
The second object returned will be a list of names which are
'virtual path' components: parts of the URL which are dynamic,
and were not used when looking up the handler.
@@ -277,7 +312,7 @@ class Dispatcher(object):
app = request.app
root = app.root
dispatch_name = self.dispatch_method_name
-
+
# Get config for the root object/path.
fullpath = [x for x in path.strip('/').split('/') if x] + ['index']
fullpath_len = len(fullpath)
@@ -288,14 +323,14 @@ class Dispatcher(object):
if "/" in app.config:
nodeconf.update(app.config["/"])
object_trail = [['root', root, nodeconf, segleft]]
-
+
node = root
iternames = fullpath[:]
while iternames:
name = iternames[0]
# map to legal Python identifiers (e.g. replace '.' with '_')
objname = name.translate(self.translate)
-
+
nodeconf = {}
subnode = getattr(node, objname, None)
pre_len = len(iternames)
@@ -304,31 +339,31 @@ class Dispatcher(object):
if dispatch and hasattr(dispatch, '__call__') and not \
getattr(dispatch, 'exposed', False) and \
pre_len > 1:
- #Don't expose the hidden 'index' token to _cp_dispatch
- #We skip this if pre_len == 1 since it makes no sense
- #to call a dispatcher when we have no tokens left.
+ # Don't expose the hidden 'index' token to _cp_dispatch
+ # We skip this if pre_len == 1 since it makes no sense
+ # to call a dispatcher when we have no tokens left.
index_name = iternames.pop()
subnode = dispatch(vpath=iternames)
iternames.append(index_name)
else:
- #We didn't find a path, but keep processing in case there
- #is a default() handler.
+ # We didn't find a path, but keep processing in case there
+ # is a default() handler.
iternames.pop(0)
else:
- #We found the path, remove the vpath entry
+ # We found the path, remove the vpath entry
iternames.pop(0)
segleft = len(iternames)
if segleft > pre_len:
- #No path segment was removed. Raise an error.
+ # No path segment was removed. Raise an error.
raise cherrypy.CherryPyException(
"A vpath segment was added. Custom dispatchers may only "
+ "remove elements. While trying to process "
+ "{0} in {1}".format(name, fullpath)
- )
+ )
elif segleft == pre_len:
- #Assume that the handler used the current path segment, but
- #did not pop it. This allows things like
- #return getattr(self, vpath[0], None)
+ # Assume that the handler used the current path segment, but
+ # did not pop it. This allows things like
+ # return getattr(self, vpath[0], None)
iternames.pop(0)
segleft -= 1
node = subnode
@@ -337,7 +372,7 @@ class Dispatcher(object):
# Get _cp_config attached to this node.
if hasattr(node, "_cp_config"):
nodeconf.update(node._cp_config)
-
+
# Mix in values from app.config for this path.
existing_len = fullpath_len - pre_len
if existing_len != 0:
@@ -349,43 +384,47 @@ class Dispatcher(object):
curpath += '/' + seg
if curpath in app.config:
nodeconf.update(app.config[curpath])
-
+
object_trail.append([name, node, nodeconf, segleft])
-
+
def set_conf():
- """Collapse all object_trail config into cherrypy.request.config."""
+ """Collapse all object_trail config into cherrypy.request.config.
+ """
base = cherrypy.config.copy()
# Note that we merge the config from each node
# even if that node was None.
for name, obj, conf, segleft in object_trail:
base.update(conf)
if 'tools.staticdir.dir' in conf:
- base['tools.staticdir.section'] = '/' + '/'.join(fullpath[0:fullpath_len - segleft])
+ base['tools.staticdir.section'] = '/' + \
+ '/'.join(fullpath[0:fullpath_len - segleft])
return base
-
+
# Try successive objects (reverse order)
num_candidates = len(object_trail) - 1
for i in range(num_candidates, -1, -1):
-
+
name, candidate, nodeconf, segleft = object_trail[i]
if candidate is None:
continue
-
+
# Try a "default" method on the current leaf.
if hasattr(candidate, "default"):
defhandler = candidate.default
if getattr(defhandler, 'exposed', False):
# Insert any extra _cp_config from the default handler.
conf = getattr(defhandler, "_cp_config", {})
- object_trail.insert(i+1, ["default", defhandler, conf, segleft])
+ object_trail.insert(
+ i + 1, ["default", defhandler, conf, segleft])
request.config = set_conf()
- # See http://www.cherrypy.org/ticket/613
+ # See https://bitbucket.org/cherrypy/cherrypy/issue/613
request.is_index = path.endswith("/")
return defhandler, fullpath[fullpath_len - segleft:-1]
-
- # Uncomment the next line to restrict positional params to "default".
+
+ # Uncomment the next line to restrict positional params to
+ # "default".
# if i < num_candidates - 2: continue
-
+
# Try the current leaf.
if getattr(candidate, 'exposed', False):
request.config = set_conf()
@@ -400,28 +439,29 @@ class Dispatcher(object):
# positional parameters (virtual paths).
request.is_index = False
return candidate, fullpath[fullpath_len - segleft:-1]
-
+
# We didn't find anything
request.config = set_conf()
return None, []
class MethodDispatcher(Dispatcher):
+
"""Additional dispatch based on cherrypy.request.method.upper().
-
+
Methods named GET, POST, etc will be called on an exposed class.
The method names must be all caps; the appropriate Allow header
will be output showing all capitalized method names as allowable
HTTP verbs.
-
+
Note that the containing class must be exposed, not the methods.
"""
-
+
def __call__(self, path_info):
"""Set handler and config for the current request."""
request = cherrypy.serving.request
resource, vpath = self.find_handler(path_info)
-
+
if resource:
# Set Allow header
avail = [m for m in dir(resource) if m.isupper()]
@@ -429,7 +469,7 @@ class MethodDispatcher(Dispatcher):
avail.append("HEAD")
avail.sort()
cherrypy.serving.response.headers['Allow'] = ", ".join(avail)
-
+
# Find the subhandler
meth = request.method.upper()
func = getattr(resource, meth, None)
@@ -439,7 +479,7 @@ class MethodDispatcher(Dispatcher):
# Grab any _cp_config on the subhandler.
if hasattr(func, "_cp_config"):
request.config.update(func._cp_config)
-
+
# Decode any leftover %2F in the virtual_path atoms.
vpath = [x.replace("%2F", "/") for x in vpath]
request.handler = LateParamPageHandler(func, *vpath)
@@ -450,9 +490,10 @@ class MethodDispatcher(Dispatcher):
class RoutesDispatcher(object):
+
"""A Routes based dispatcher for CherryPy."""
-
- def __init__(self, full_result=False):
+
+ def __init__(self, full_result=False, **mapper_options):
"""
Routes dispatcher
@@ -463,16 +504,16 @@ class RoutesDispatcher(object):
import routes
self.full_result = full_result
self.controllers = {}
- self.mapper = routes.Mapper()
+ self.mapper = routes.Mapper(**mapper_options)
self.mapper.controller_scan = self.controllers.keys
-
+
def connect(self, name, route, controller, **kwargs):
self.controllers[name] = controller
self.mapper.connect(name, route, controller=name, **kwargs)
-
+
def redirect(self, url):
raise cherrypy.HTTPRedirect(url)
-
+
def __call__(self, path_info):
"""Set handler and config for the current request."""
func = self.find_handler(path_info)
@@ -480,13 +521,13 @@ class RoutesDispatcher(object):
cherrypy.serving.request.handler = LateParamPageHandler(func)
else:
cherrypy.serving.request.handler = cherrypy.NotFound()
-
+
def find_handler(self, path_info):
"""Find the right page handler, and set request.config."""
import routes
-
+
request = cherrypy.serving.request
-
+
config = routes.request_config()
config.mapper = self.mapper
if hasattr(request, 'wsgi_environ'):
@@ -494,9 +535,9 @@ class RoutesDispatcher(object):
config.host = request.headers.get('Host', None)
config.protocol = request.scheme
config.redirect = self.redirect
-
+
result = self.mapper.match(path_info)
-
+
config.mapper_dict = result
params = {}
if result:
@@ -505,23 +546,23 @@ class RoutesDispatcher(object):
params.pop('controller', None)
params.pop('action', None)
request.params.update(params)
-
+
# Get config for the root object/path.
request.config = base = cherrypy.config.copy()
curpath = ""
-
+
def merge(nodeconf):
if 'tools.staticdir.dir' in nodeconf:
nodeconf['tools.staticdir.section'] = curpath or "/"
base.update(nodeconf)
-
+
app = request.app
root = app.root
if hasattr(root, "_cp_config"):
merge(root._cp_config)
if "/" in app.config:
merge(app.config["/"])
-
+
# Mix in values from app.config.
atoms = [x for x in path_info.split("/") if x]
if atoms:
@@ -532,7 +573,7 @@ class RoutesDispatcher(object):
curpath = "/".join((curpath, atom))
if curpath in app.config:
merge(app.config[curpath])
-
+
handler = None
if result:
controller = result.get('controller')
@@ -543,64 +584,66 @@ class RoutesDispatcher(object):
# Get config from the controller.
if hasattr(controller, "_cp_config"):
merge(controller._cp_config)
-
+
action = result.get('action')
if action is not None:
handler = getattr(controller, action, None)
- # Get config from the handler
- if hasattr(handler, "_cp_config"):
+ # Get config from the handler
+ if hasattr(handler, "_cp_config"):
merge(handler._cp_config)
else:
handler = controller
-
+
# Do the last path atom here so it can
# override the controller's _cp_config.
if last:
curpath = "/".join((curpath, last))
if curpath in app.config:
merge(app.config[curpath])
-
+
return handler
def XMLRPCDispatcher(next_dispatcher=Dispatcher()):
from cherrypy.lib import xmlrpcutil
+
def xmlrpc_dispatch(path_info):
path_info = xmlrpcutil.patched_path(path_info)
return next_dispatcher(path_info)
return xmlrpc_dispatch
-def VirtualHost(next_dispatcher=Dispatcher(), use_x_forwarded_host=True, **domains):
+def VirtualHost(next_dispatcher=Dispatcher(), use_x_forwarded_host=True,
+ **domains):
"""
Select a different handler based on the Host header.
-
+
This can be useful when running multiple sites within one CP server.
It allows several domains to point to different parts of a single
website structure. For example::
-
+
http://www.domain.example -> root
http://www.domain2.example -> root/domain2/
http://www.domain2.example:443 -> root/secure
-
+
can be accomplished via the following config::
-
+
[/]
request.dispatch = cherrypy.dispatch.VirtualHost(
**{'www.domain2.example': '/domain2',
'www.domain2.example:443': '/secure',
})
-
+
next_dispatcher
The next dispatcher object in the dispatch chain.
The VirtualHost dispatcher adds a prefix to the URL and calls
another dispatcher. Defaults to cherrypy.dispatch.Dispatcher().
-
+
use_x_forwarded_host
If True (the default), any "X-Forwarded-Host"
request header will be used instead of the "Host" header. This
is commonly added by HTTP servers (such as Apache) when proxying.
-
+
``**domains``
A dict of {host header value: virtual prefix} pairs.
The incoming "Host" request header is looked up in this dict,
@@ -611,26 +654,27 @@ def VirtualHost(next_dispatcher=Dispatcher(), use_x_forwarded_host=True, **domai
headers may contain the port number.
"""
from cherrypy.lib import httputil
+
def vhost_dispatch(path_info):
request = cherrypy.serving.request
header = request.headers.get
-
+
domain = header('Host', '')
if use_x_forwarded_host:
domain = header("X-Forwarded-Host", domain)
-
+
prefix = domains.get(domain, "")
if prefix:
path_info = httputil.urljoin(prefix, path_info)
-
+
result = next_dispatcher(path_info)
-
- # Touch up staticdir config. See http://www.cherrypy.org/ticket/614.
+
+ # Touch up staticdir config. See
+ # https://bitbucket.org/cherrypy/cherrypy/issue/614.
section = request.config.get('tools.staticdir.section')
if section:
section = section[len(prefix):]
request.config['tools.staticdir.section'] = section
-
+
return result
return vhost_dispatch
-
diff --git a/cherrypy/_cperror.py b/cherrypy/_cperror.py
index 76a409ff..f09fa270 100644
--- a/cherrypy/_cperror.py
+++ b/cherrypy/_cperror.py
@@ -2,8 +2,9 @@
CherryPy provides (and uses) exceptions for declaring that the HTTP response
should be a status other than the default "200 OK". You can ``raise`` them like
-normal Python exceptions. You can also call them and they will raise themselves;
-this means you can set an :class:`HTTPError<cherrypy._cperror.HTTPError>`
+normal Python exceptions. You can also call them and they will raise
+themselves; this means you can set an
+:class:`HTTPError<cherrypy._cperror.HTTPError>`
or :class:`HTTPRedirect<cherrypy._cperror.HTTPRedirect>` as the
:attr:`request.handler<cherrypy._cprequest.Request.handler>`.
@@ -21,7 +22,8 @@ POST, however, is neither safe nor idempotent--if you
charge a credit card, you don't want to be charged twice by a redirect!
For this reason, *none* of the 3xx responses permit a user-agent (browser) to
-resubmit a POST on redirection without first confirming the action with the user:
+resubmit a POST on redirection without first confirming the action with the
+user:
===== ================================= ===========
300 Multiple Choices Confirm with the user
@@ -53,14 +55,16 @@ Anticipated HTTP responses
--------------------------
The 'error_page' config namespace can be used to provide custom HTML output for
-expected responses (like 404 Not Found). Supply a filename from which the output
-will be read. The contents will be interpolated with the values %(status)s,
-%(message)s, %(traceback)s, and %(version)s using plain old Python
-`string formatting <http://www.python.org/doc/2.6.4/library/stdtypes.html#string-formatting-operations>`_.
+expected responses (like 404 Not Found). Supply a filename from which the
+output will be read. The contents will be interpolated with the values
+%(status)s, %(message)s, %(traceback)s, and %(version)s using plain old Python
+`string formatting <http://docs.python.org/2/library/stdtypes.html#string-formatting-operations>`_.
::
- _cp_config = {'error_page.404': os.path.join(localDir, "static/index.html")}
+ _cp_config = {
+ 'error_page.404': os.path.join(localDir, "static/index.html")
+ }
Beginning in version 3.1, you may also provide a function or other callable as
@@ -72,7 +76,8 @@ version arguments that are interpolated into templates::
cherrypy.config.update({'error_page.402': error_page_402})
Also in 3.1, in addition to the numbered error codes, you may also supply
-"error_page.default" to handle all codes which do not have their own error_page entry.
+"error_page.default" to handle all codes which do not have their own error_page
+entry.
@@ -81,8 +86,9 @@ Unanticipated errors
CherryPy also has a generic error handling mechanism: whenever an unanticipated
error occurs in your code, it will call
-:func:`Request.error_response<cherrypy._cprequest.Request.error_response>` to set
-the response status, headers, and body. By default, this is the same output as
+:func:`Request.error_response<cherrypy._cprequest.Request.error_response>` to
+set the response status, headers, and body. By default, this is the same
+output as
:class:`HTTPError(500) <cherrypy._cperror.HTTPError>`. If you want to provide
some other behavior, you generally replace "request.error_response".
@@ -93,67 +99,78 @@ send an e-mail containing the error::
def handle_error():
cherrypy.response.status = 500
- cherrypy.response.body = ["<html><body>Sorry, an error occured</body></html>"]
- sendMail('error@domain.com', 'Error in your web app', _cperror.format_exc())
+ cherrypy.response.body = [
+ "<html><body>Sorry, an error occured</body></html>"
+ ]
+ sendMail('error@domain.com',
+ 'Error in your web app',
+ _cperror.format_exc())
class Root:
_cp_config = {'request.error_response': handle_error}
-Note that you have to explicitly set :attr:`response.body <cherrypy._cprequest.Response.body>`
+Note that you have to explicitly set
+:attr:`response.body <cherrypy._cprequest.Response.body>`
and not simply return an error message as a result.
"""
from cgi import escape as _escape
from sys import exc_info as _exc_info
from traceback import format_exception as _format_exception
-from cherrypy._cpcompat import basestring, bytestr, iteritems, ntob, tonative, urljoin as _urljoin
+from cherrypy._cpcompat import basestring, bytestr, iteritems, ntob
+from cherrypy._cpcompat import tonative, urljoin as _urljoin
from cherrypy.lib import httputil as _httputil
class CherryPyException(Exception):
+
"""A base class for CherryPy exceptions."""
pass
class TimeoutError(CherryPyException):
+
"""Exception raised when Response.timed_out is detected."""
pass
class InternalRedirect(CherryPyException):
+
"""Exception raised to switch to the handler for a different URL.
-
+
This exception will redirect processing to another path within the site
(without informing the client). Provide the new path as an argument when
- raising the exception. Provide any params in the querystring for the new URL.
+ raising the exception. Provide any params in the querystring for the new
+ URL.
"""
-
+
def __init__(self, path, query_string=""):
import cherrypy
self.request = cherrypy.serving.request
-
+
self.query_string = query_string
if "?" in path:
# Separate any params included in the path
path, self.query_string = path.split("?", 1)
-
+
# Note that urljoin will "do the right thing" whether url is:
# 1. a URL relative to root (e.g. "/dummy")
# 2. a URL relative to the current path
# Note that any query string will be discarded.
path = _urljoin(self.request.path_info, path)
-
+
# Set a 'path' member attribute so that code which traps this
# error can have access to it.
self.path = path
-
+
CherryPyException.__init__(self, path, self.query_string)
class HTTPRedirect(CherryPyException):
+
"""Exception raised when the request should be redirected.
-
+
This exception will force a HTTP redirect to the URL or URL's you give it.
The new URL must be passed as the first argument to the Exception,
e.g., HTTPRedirect(newUrl). Multiple URLs are allowed in a list.
@@ -162,40 +179,40 @@ class HTTPRedirect(CherryPyException):
If one of the provided URL is a unicode object, it will be encoded
using the default encoding or the one passed in parameter.
-
+
There are multiple types of redirect, from which you can select via the
``status`` argument. If you do not provide a ``status`` arg, it defaults to
303 (or 302 if responding with HTTP/1.0).
-
+
Examples::
-
+
raise cherrypy.HTTPRedirect("")
raise cherrypy.HTTPRedirect("/abs/path", 307)
raise cherrypy.HTTPRedirect(["path1", "path2?a=1&b=2"], 301)
-
+
See :ref:`redirectingpost` for additional caveats.
"""
-
+
status = None
"""The integer HTTP status code to emit."""
-
+
urls = None
"""The list of URL's to emit."""
encoding = 'utf-8'
"""The encoding when passed urls are not native strings"""
-
+
def __init__(self, urls, status=None, encoding=None):
import cherrypy
request = cherrypy.serving.request
-
+
if isinstance(urls, basestring):
urls = [urls]
-
+
abs_urls = []
for url in urls:
url = tonative(url, encoding or self.encoding)
-
+
# Note that urljoin will "do the right thing" whether url is:
# 1. a complete URL with host (e.g. "http://www.example.com/test")
# 2. a URL relative to root (e.g. "/dummy")
@@ -204,7 +221,7 @@ class HTTPRedirect(CherryPyException):
url = _urljoin(cherrypy.url(), url)
abs_urls.append(url)
self.urls = abs_urls
-
+
# RFC 2616 indicates a 301 response code fits our goal; however,
# browser support for 301 is quite messy. Do 302/303 instead. See
# http://www.alanflavell.org.uk/www/post-redirect.html
@@ -217,35 +234,38 @@ class HTTPRedirect(CherryPyException):
status = int(status)
if status < 300 or status > 399:
raise ValueError("status must be between 300 and 399.")
-
+
self.status = status
CherryPyException.__init__(self, abs_urls, status)
-
+
def set_response(self):
- """Modify cherrypy.response status, headers, and body to represent self.
-
+ """Modify cherrypy.response status, headers, and body to represent
+ self.
+
CherryPy uses this internally, but you can also use it to create an
HTTPRedirect object and set its output without *raising* the exception.
"""
import cherrypy
response = cherrypy.serving.response
response.status = status = self.status
-
+
if status in (300, 301, 302, 303, 307):
response.headers['Content-Type'] = "text/html;charset=utf-8"
# "The ... URI SHOULD be given by the Location field
# in the response."
response.headers['Location'] = self.urls[0]
-
+
# "Unless the request method was HEAD, the entity of the response
# SHOULD contain a short hypertext note with a hyperlink to the
# new URI(s)."
- msg = {300: "This resource can be found at <a href='%s'>%s</a>.",
- 301: "This resource has permanently moved to <a href='%s'>%s</a>.",
- 302: "This resource resides temporarily at <a href='%s'>%s</a>.",
- 303: "This resource can be found at <a href='%s'>%s</a>.",
- 307: "This resource has moved temporarily to <a href='%s'>%s</a>.",
- }[status]
+ msg = {
+ 300: "This resource can be found at ",
+ 301: "This resource has permanently moved to ",
+ 302: "This resource resides temporarily at ",
+ 303: "This resource can be found at ",
+ 307: "This resource has moved temporarily to ",
+ }[status]
+ msg += "<a href='%s'>%s</a>."
msgs = [msg % (u, u) for u in self.urls]
response.body = ntob("<br />\n".join(msgs), 'utf-8')
# Previous code may have set C-L, so we have to reset it
@@ -256,7 +276,7 @@ class HTTPRedirect(CherryPyException):
# "The response MUST include the following header fields:
# Date, unless its omission is required by section 14.18.1"
# The "Date" header should have been set in Response.__init__
-
+
# "...the response SHOULD NOT include other entity-headers."
for key in ('Allow', 'Content-Encoding', 'Content-Language',
'Content-Length', 'Content-Location', 'Content-MD5',
@@ -264,7 +284,7 @@ class HTTPRedirect(CherryPyException):
'Last-Modified'):
if key in response.headers:
del response.headers[key]
-
+
# "The 304 response MUST NOT contain a message-body."
response.body = None
# Previous code may have set C-L, so we have to reset it.
@@ -278,7 +298,7 @@ class HTTPRedirect(CherryPyException):
response.headers.pop('Content-Length', None)
else:
raise ValueError("The %s status code is unknown." % status)
-
+
def __call__(self):
"""Use this exception as a request.handler (raise self)."""
raise self
@@ -287,9 +307,9 @@ class HTTPRedirect(CherryPyException):
def clean_headers(status):
"""Remove any headers which should not apply to an error response."""
import cherrypy
-
+
response = cherrypy.serving.response
-
+
# Remove headers which applied to the original content,
# but do not apply to the error page.
respheaders = response.headers
@@ -298,7 +318,7 @@ def clean_headers(status):
"Content-Location", "Content-MD5", "Last-Modified"]:
if key in respheaders:
del respheaders[key]
-
+
if status != 416:
# A server sending a response with status code 416 (Requested
# range not satisfiable) SHOULD include a Content-Range field
@@ -311,58 +331,62 @@ def clean_headers(status):
class HTTPError(CherryPyException):
+
"""Exception used to return an HTTP error code (4xx-5xx) to the client.
-
- This exception can be used to automatically send a response using a http status
- code, with an appropriate error page. It takes an optional
+
+ This exception can be used to automatically send a response using a
+ http status code, with an appropriate error page. It takes an optional
``status`` argument (which must be between 400 and 599); it defaults to 500
("Internal Server Error"). It also takes an optional ``message`` argument,
which will be returned in the response body. See
- `RFC 2616 <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4>`_
+ `RFC2616 <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4>`_
for a complete list of available error codes and when to use them.
-
+
Examples::
-
+
raise cherrypy.HTTPError(403)
- raise cherrypy.HTTPError("403 Forbidden", "You are not allowed to access this resource.")
+ raise cherrypy.HTTPError(
+ "403 Forbidden", "You are not allowed to access this resource.")
"""
-
+
status = None
- """The HTTP status code. May be of type int or str (with a Reason-Phrase)."""
-
+ """The HTTP status code. May be of type int or str (with a Reason-Phrase).
+ """
+
code = None
"""The integer HTTP status code."""
-
+
reason = None
"""The HTTP Reason-Phrase string."""
-
+
def __init__(self, status=500, message=None):
self.status = status
try:
self.code, self.reason, defaultmsg = _httputil.valid_status(status)
except ValueError:
raise self.__class__(500, _exc_info()[1].args[0])
-
+
if self.code < 400 or self.code > 599:
raise ValueError("status must be between 400 and 599.")
-
+
# See http://www.python.org/dev/peps/pep-0352/
# self.message = message
self._message = message or defaultmsg
CherryPyException.__init__(self, status, message)
-
+
def set_response(self):
- """Modify cherrypy.response status, headers, and body to represent self.
-
+ """Modify cherrypy.response status, headers, and body to represent
+ self.
+
CherryPy uses this internally, but you can also use it to create an
HTTPError object and set its output without *raising* the exception.
"""
import cherrypy
-
+
response = cherrypy.serving.response
-
+
clean_headers(self.code)
-
+
# In all cases, finalize will be called after this method,
# so don't bother cleaning up response values here.
response.status = self.status
@@ -371,28 +395,29 @@ class HTTPError(CherryPyException):
tb = format_exc()
response.headers['Content-Type'] = "text/html;charset=utf-8"
response.headers.pop('Content-Length', None)
-
- content = ntob(self.get_error_page(self.status, traceback=tb,
- message=self._message), 'utf-8')
+
+ content = self.get_error_page(self.status, traceback=tb,
+ message=self._message).encode('utf-8')
response.body = content
-
+
_be_ie_unfriendly(self.code)
-
+
def get_error_page(self, *args, **kwargs):
return get_error_page(*args, **kwargs)
-
+
def __call__(self):
"""Use this exception as a request.handler (raise self)."""
raise self
class NotFound(HTTPError):
+
"""Exception raised when a URL could not be mapped to any handler (404).
-
+
This is equivalent to raising
:class:`HTTPError("404 Not Found") <cherrypy._cperror.HTTPError>`.
"""
-
+
def __init__(self, path=None):
if path is None:
import cherrypy
@@ -402,7 +427,8 @@ class NotFound(HTTPError):
HTTPError.__init__(self, 404, "The path '%s' was not found." % path)
-_HTTPErrorTemplate = '''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+_HTTPErrorTemplate = '''<!DOCTYPE html PUBLIC
+"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
@@ -425,25 +451,28 @@ _HTTPErrorTemplate = '''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitiona
<p>%(message)s</p>
<pre id="traceback">%(traceback)s</pre>
<div id="powered_by">
- <span>Powered by <a href="http://www.cherrypy.org">CherryPy %(version)s</a></span>
+ <span>
+ Powered by <a href="http://www.cherrypy.org">CherryPy %(version)s</a>
+ </span>
</div>
</body>
</html>
'''
+
def get_error_page(status, **kwargs):
"""Return an HTML page, containing a pretty error response.
-
+
status should be an int or a str.
kwargs will be interpolated into the page template.
"""
import cherrypy
-
+
try:
code, reason, message = _httputil.valid_status(status)
except ValueError:
raise cherrypy.HTTPError(500, _exc_info()[1].args[0])
-
+
# We can't use setdefault here, because some
# callers send None for kwarg values.
if kwargs.get('status') is None:
@@ -454,13 +483,13 @@ def get_error_page(status, **kwargs):
kwargs['traceback'] = ''
if kwargs.get('version') is None:
kwargs['version'] = cherrypy.__version__
-
+
for k, v in iteritems(kwargs):
if v is None:
kwargs[k] = ""
else:
kwargs[k] = _escape(kwargs[k])
-
+
# Use a custom template or callable for the error page?
pages = cherrypy.serving.request.error_page
error_page = pages.get(code) or pages.get('default')
@@ -478,7 +507,7 @@ def get_error_page(status, **kwargs):
m += "<br />"
m += "In addition, the custom error page failed:\n<br />%s" % e
kwargs['message'] = m
-
+
return _HTTPErrorTemplate % kwargs
@@ -486,13 +515,13 @@ _ie_friendly_error_sizes = {
400: 512, 403: 256, 404: 512, 405: 256,
406: 512, 408: 512, 409: 512, 410: 256,
500: 512, 501: 512, 505: 512,
- }
+}
def _be_ie_unfriendly(status):
import cherrypy
response = cherrypy.serving.response
-
+
# For some statuses, Internet Explorer 5+ shows "friendly error
# messages" instead of our response.body if the body is smaller
# than a given size. Fix this by returning a body over that size
@@ -525,32 +554,31 @@ def format_exc(exc=None):
finally:
del exc
+
def bare_error(extrabody=None):
"""Produce status, headers, body for a critical error.
-
+
Returns a triple without calling any other questionable functions,
so it should be as error-free as possible. Call it from an HTTP server
if you get errors outside of the request.
-
+
If extrabody is None, a friendly but rather unhelpful error message
is set in the body. If extrabody is a string, it will be appended
as-is to the body.
"""
-
+
# The whole point of this function is to be a last line-of-defense
# in handling errors. That is, it must not raise any errors itself;
# it cannot be allowed to fail. Therefore, don't add to it!
# In particular, don't call any other CP functions.
-
+
body = ntob("Unrecoverable error in the server.")
if extrabody is not None:
if not isinstance(extrabody, bytestr):
extrabody = extrabody.encode('utf-8')
body += ntob("\n") + extrabody
-
+
return (ntob("500 Internal Server Error"),
[(ntob('Content-Type'), ntob('text/plain')),
- (ntob('Content-Length'), ntob(str(len(body)),'ISO-8859-1'))],
+ (ntob('Content-Length'), ntob(str(len(body)), 'ISO-8859-1'))],
[body])
-
-
diff --git a/cherrypy/_cplogging.py b/cherrypy/_cplogging.py
index e10c9420..d1b99f70 100644
--- a/cherrypy/_cplogging.py
+++ b/cherrypy/_cplogging.py
@@ -34,10 +34,11 @@ and another set of rules specific to each application. The global log
manager is found at :func:`cherrypy.log`, and the log manager for each
application is found at :attr:`app.log<cherrypy._cptree.Application.log>`.
If you're inside a request, the latter is reachable from
-``cherrypy.request.app.log``; if you're outside a request, you'll have to obtain
-a reference to the ``app``: either the return value of
+``cherrypy.request.app.log``; if you're outside a request, you'll have to
+obtain a reference to the ``app``: either the return value of
:func:`tree.mount()<cherrypy._cptree.Tree.mount>` or, if you used
-:func:`quickstart()<cherrypy.quickstart>` instead, via ``cherrypy.tree.apps['/']``.
+:func:`quickstart()<cherrypy.quickstart>` instead, via
+``cherrypy.tree.apps['/']``.
By default, the global logs are named "cherrypy.error" and "cherrypy.access",
and the application logs are named "cherrypy.error.2378745" and
@@ -69,21 +70,21 @@ and uses a RotatingFileHandler instead:
#python
log = app.log
-
+
# Remove the default FileHandlers if present.
log.error_file = ""
log.access_file = ""
-
+
maxBytes = getattr(log, "rot_maxBytes", 10000000)
backupCount = getattr(log, "rot_backupCount", 1000)
-
+
# Make a new RotatingFileHandler for the error log.
fname = getattr(log, "rot_error_file", "error.log")
h = handlers.RotatingFileHandler(fname, 'a', maxBytes, backupCount)
h.setLevel(DEBUG)
h.setFormatter(_cplogging.logfmt)
log.error_log.addHandler(h)
-
+
# Make a new RotatingFileHandler for the access log.
fname = getattr(log, "rot_access_file", "access.log")
h = handlers.RotatingFileHandler(fname, 'a', maxBytes, backupCount)
@@ -113,6 +114,7 @@ from cherrypy._cpcompat import ntob, py3k
class NullHandler(logging.Handler):
+
"""A no-op logging handler to silence the logging.lastResort handler."""
def handle(self, record):
@@ -126,39 +128,40 @@ class NullHandler(logging.Handler):
class LogManager(object):
+
"""An object to assist both simple and advanced logging.
-
+
``cherrypy.log`` is an instance of this class.
"""
-
+
appid = None
"""The id() of the Application object which owns this log manager. If this
is a global log manager, appid is None."""
-
+
error_log = None
"""The actual :class:`logging.Logger` instance for error messages."""
-
+
access_log = None
"""The actual :class:`logging.Logger` instance for access messages."""
-
+
if py3k:
access_log_format = \
'{h} {l} {u} {t} "{r}" {s} {b} "{f}" "{a}"'
else:
access_log_format = \
'%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"'
-
+
logger_root = None
"""The "top-level" logger name.
-
+
This string will be used as the first segment in the Logger names.
The default is "cherrypy", for example, in which case the Logger names
will be of the form::
-
+
cherrypy.error.<appid>
cherrypy.access.<appid>
"""
-
+
def __init__(self, appid=None, logger_root="cherrypy"):
self.logger_root = logger_root
self.appid = appid
@@ -166,8 +169,10 @@ class LogManager(object):
self.error_log = logging.getLogger("%s.error" % logger_root)
self.access_log = logging.getLogger("%s.access" % logger_root)
else:
- self.error_log = logging.getLogger("%s.error.%s" % (logger_root, appid))
- self.access_log = logging.getLogger("%s.access.%s" % (logger_root, appid))
+ self.error_log = logging.getLogger(
+ "%s.error.%s" % (logger_root, appid))
+ self.access_log = logging.getLogger(
+ "%s.access.%s" % (logger_root, appid))
self.error_log.setLevel(logging.INFO)
self.access_log.setLevel(logging.INFO)
@@ -186,34 +191,36 @@ class LogManager(object):
h.stream.close()
h.stream = open(h.baseFilename, h.mode)
h.release()
-
- def error(self, msg='', context='', severity=logging.INFO, traceback=False):
+
+ def error(self, msg='', context='', severity=logging.INFO,
+ traceback=False):
"""Write the given ``msg`` to the error log.
-
+
This is not just for errors! Applications may call this at any time
to log application-specific information.
-
+
If ``traceback`` is True, the traceback of the current exception
(if any) will be appended to ``msg``.
"""
if traceback:
msg += _cperror.format_exc()
self.error_log.log(severity, ' '.join((self.time(), context, msg)))
-
+
def __call__(self, *args, **kwargs):
"""An alias for ``error``."""
return self.error(*args, **kwargs)
-
+
def access(self):
"""Write to the access log (in Apache/NCSA Combined Log format).
-
- See http://httpd.apache.org/docs/2.0/logs.html#combined for format
- details.
-
+
+ See the
+ `apache documentation <http://httpd.apache.org/docs/current/logs.html#combined>`_
+ for format details.
+
CherryPy calls this automatically for you. Note there are no arguments;
it collects the data itself from
:class:`cherrypy.request<cherrypy._cprequest.Request>`.
-
+
Like Apache started doing in 2.0.46, non-printable and other special
characters in %r (and we expand that to all parts) are escaped using
\\xhh sequences, where hh stands for the hexadecimal representation
@@ -232,7 +239,7 @@ class LogManager(object):
status = response.output_status.split(ntob(" "), 1)[0]
if py3k:
status = status.decode('ISO-8859-1')
-
+
atoms = {'h': remote.name or remote.ip,
'l': '-',
'u': getattr(request, "login", None) or "-",
@@ -251,17 +258,18 @@ class LogManager(object):
# Fortunately, repr(str) escapes unprintable chars, \n, \t, etc
# and backslash for us. All we have to do is strip the quotes.
v = repr(v)[2:-1]
-
- # in python 3.0 the repr of bytes (as returned by encode)
+
+ # in python 3.0 the repr of bytes (as returned by encode)
# uses double \'s. But then the logger escapes them yet, again
# resulting in quadruple slashes. Remove the extra one here.
v = v.replace('\\\\', '\\')
-
+
# Escape double-quote.
atoms[k] = v
-
+
try:
- self.access_log.log(logging.INFO, self.access_log_format.format(**atoms))
+ self.access_log.log(
+ logging.INFO, self.access_log_format.format(**atoms))
except:
self(traceback=True)
else:
@@ -275,12 +283,13 @@ class LogManager(object):
v = repr(v)[1:-1]
# Escape double-quote.
atoms[k] = v.replace('"', '\\"')
-
+
try:
- self.access_log.log(logging.INFO, self.access_log_format % atoms)
+ self.access_log.log(
+ logging.INFO, self.access_log_format % atoms)
except:
self(traceback=True)
-
+
def time(self):
"""Return now() in Apache Common Log Format (no timezone)."""
now = datetime.datetime.now()
@@ -289,51 +298,49 @@ class LogManager(object):
month = monthnames[now.month - 1].capitalize()
return ('[%02d/%s/%04d:%02d:%02d:%02d]' %
(now.day, month, now.year, now.hour, now.minute, now.second))
-
+
def _get_builtin_handler(self, log, key):
for h in log.handlers:
if getattr(h, "_cpbuiltin", None) == key:
return h
-
-
+
# ------------------------- Screen handlers ------------------------- #
-
def _set_screen_handler(self, log, enable, stream=None):
h = self._get_builtin_handler(log, "screen")
if enable:
if not h:
if stream is None:
- stream=sys.stderr
+ stream = sys.stderr
h = logging.StreamHandler(stream)
h.setFormatter(logfmt)
h._cpbuiltin = "screen"
log.addHandler(h)
elif h:
log.handlers.remove(h)
-
+
def _get_screen(self):
h = self._get_builtin_handler
has_h = h(self.error_log, "screen") or h(self.access_log, "screen")
return bool(has_h)
-
+
def _set_screen(self, newvalue):
self._set_screen_handler(self.error_log, newvalue, stream=sys.stderr)
self._set_screen_handler(self.access_log, newvalue, stream=sys.stdout)
screen = property(_get_screen, _set_screen,
- doc="""Turn stderr/stdout logging on or off.
-
+ doc="""Turn stderr/stdout logging on or off.
+
If you set this to True, it'll add the appropriate StreamHandler for
you. If you set it to False, it will remove the handler.
""")
-
+
# -------------------------- File handlers -------------------------- #
-
+
def _add_builtin_file_handler(self, log, fname):
h = logging.FileHandler(fname)
h.setFormatter(logfmt)
h._cpbuiltin = "file"
log.addHandler(h)
-
+
def _set_file_handler(self, log, filename):
h = self._get_builtin_handler(log, "file")
if filename:
@@ -348,37 +355,39 @@ class LogManager(object):
if h:
h.close()
log.handlers.remove(h)
-
+
def _get_error_file(self):
h = self._get_builtin_handler(self.error_log, "file")
if h:
return h.baseFilename
return ''
+
def _set_error_file(self, newvalue):
self._set_file_handler(self.error_log, newvalue)
error_file = property(_get_error_file, _set_error_file,
- doc="""The filename for self.error_log.
-
+ doc="""The filename for self.error_log.
+
If you set this to a string, it'll add the appropriate FileHandler for
you. If you set it to ``None`` or ``''``, it will remove the handler.
""")
-
+
def _get_access_file(self):
h = self._get_builtin_handler(self.access_log, "file")
if h:
return h.baseFilename
return ''
+
def _set_access_file(self, newvalue):
self._set_file_handler(self.access_log, newvalue)
access_file = property(_get_access_file, _set_access_file,
- doc="""The filename for self.access_log.
-
+ doc="""The filename for self.access_log.
+
If you set this to a string, it'll add the appropriate FileHandler for
you. If you set it to ``None`` or ``''``, it will remove the handler.
""")
-
+
# ------------------------- WSGI handlers ------------------------- #
-
+
def _set_wsgi_handler(self, log, enable):
h = self._get_builtin_handler(log, "wsgi")
if enable:
@@ -389,15 +398,15 @@ class LogManager(object):
log.addHandler(h)
elif h:
log.handlers.remove(h)
-
+
def _get_wsgi(self):
return bool(self._get_builtin_handler(self.error_log, "wsgi"))
-
+
def _set_wsgi(self, newvalue):
self._set_wsgi_handler(self.error_log, newvalue)
wsgi = property(_get_wsgi, _set_wsgi,
- doc="""Write errors to wsgi.errors.
-
+ doc="""Write errors to wsgi.errors.
+
If you set this to True, it'll add the appropriate
:class:`WSGIErrorHandler<cherrypy._cplogging.WSGIErrorHandler>` for you
(which writes errors to ``wsgi.errors``).
@@ -406,8 +415,9 @@ class LogManager(object):
class WSGIErrorHandler(logging.Handler):
+
"A handler class which writes logging records to environ['wsgi.errors']."
-
+
def flush(self):
"""Flushes the stream."""
try:
@@ -416,7 +426,7 @@ class WSGIErrorHandler(logging.Handler):
pass
else:
stream.flush()
-
+
def emit(self, record):
"""Emit a record."""
try:
@@ -428,7 +438,8 @@ class WSGIErrorHandler(logging.Handler):
msg = self.format(record)
fs = "%s\n"
import types
- if not hasattr(types, "UnicodeType"): #if no unicode support...
+ # if no unicode support...
+ if not hasattr(types, "UnicodeType"):
stream.write(fs % msg)
else:
try:
diff --git a/cherrypy/_cpmodpy.py b/cherrypy/_cpmodpy.py
index 76ef6ead..02154d69 100644
--- a/cherrypy/_cpmodpy.py
+++ b/cherrypy/_cpmodpy.py
@@ -35,12 +35,12 @@ Listen 8080
LoadModule python_module /usr/lib/apache2/modules/mod_python.so
<Location "/">
- PythonPath "sys.path+['/path/to/my/application']"
- SetHandler python-program
- PythonHandler cherrypy._cpmodpy::handler
- PythonOption cherrypy.setup myapp::setup_server
- PythonDebug On
-</Location>
+ PythonPath "sys.path+['/path/to/my/application']"
+ SetHandler python-program
+ PythonHandler cherrypy._cpmodpy::handler
+ PythonOption cherrypy.setup myapp::setup_server
+ PythonDebug On
+</Location>
# End
The actual path to your mod_python.so is dependent on your
@@ -67,11 +67,11 @@ from cherrypy.lib import httputil
# ------------------------------ Request-handling
-
def setup(req):
from mod_python import apache
-
- # Run any setup functions defined by a "PythonOption cherrypy.setup" directive.
+
+ # Run any setup functions defined by a "PythonOption cherrypy.setup"
+ # directive.
options = req.get_options()
if 'cherrypy.setup' in options:
for function in options['cherrypy.setup'].split():
@@ -83,12 +83,12 @@ def setup(req):
mod = __import__(modname, globals(), locals(), [fname])
func = getattr(mod, fname)
func()
-
+
cherrypy.config.update({'log.screen': False,
"tools.ignore_headers.on": True,
"tools.ignore_headers.headers": ['Range'],
})
-
+
engine = cherrypy.engine
if hasattr(engine, "signal_handler"):
engine.signal_handler.unsubscribe()
@@ -96,7 +96,7 @@ def setup(req):
engine.console_control_handler.unsubscribe()
engine.autoreload.unsubscribe()
cherrypy.server.unsubscribe()
-
+
def _log(msg, level):
newlevel = apache.APLOG_ERR
if logging.DEBUG >= level:
@@ -106,13 +106,13 @@ def setup(req):
elif logging.WARNING >= level:
newlevel = apache.APLOG_WARNING
# On Windows, req.server is required or the msg will vanish. See
- # http://www.modpython.org/pipermail/mod_python/2003-October/014291.html.
+ # http://www.modpython.org/pipermail/mod_python/2003-October/014291.html
# Also, "When server is not specified...LogLevel does not apply..."
apache.log_error(msg, newlevel, req.server)
engine.subscribe('log', _log)
-
+
engine.start()
-
+
def cherrypy_cleanup(data):
engine.exit()
try:
@@ -124,6 +124,7 @@ def setup(req):
class _ReadOnlyRequest:
expose = ('read', 'readline', 'readlines')
+
def __init__(self, req):
for method in self.expose:
self.__dict__[method] = getattr(req, method)
@@ -132,6 +133,8 @@ class _ReadOnlyRequest:
recursive = False
_isSetUp = False
+
+
def handler(req):
from mod_python import apache
try:
@@ -139,16 +142,18 @@ def handler(req):
if not _isSetUp:
setup(req)
_isSetUp = True
-
+
# Obtain a Request object from CherryPy
local = req.connection.local_addr
- local = httputil.Host(local[0], local[1], req.connection.local_host or "")
+ local = httputil.Host(
+ local[0], local[1], req.connection.local_host or "")
remote = req.connection.remote_addr
- remote = httputil.Host(remote[0], remote[1], req.connection.remote_host or "")
-
+ remote = httputil.Host(
+ remote[0], remote[1], req.connection.remote_host or "")
+
scheme = req.parsed_uri[0] or 'http'
req.get_basic_auth_pw()
-
+
try:
# apache.mpm_query only became available in mod_python 3.1
q = apache.mpm_query
@@ -158,7 +163,7 @@ def handler(req):
bad_value = ("You must provide a PythonOption '%s', "
"either 'on' or 'off', when running a version "
"of mod_python < 3.1")
-
+
threaded = options.get('multithread', '').lower()
if threaded == 'on':
threaded = True
@@ -166,7 +171,7 @@ def handler(req):
threaded = False
else:
raise ValueError(bad_value % "multithread")
-
+
forked = options.get('multiprocess', '').lower()
if forked == 'on':
forked = True
@@ -174,7 +179,7 @@ def handler(req):
forked = False
else:
raise ValueError(bad_value % "multiprocess")
-
+
sn = cherrypy.tree.script_name(req.uri or "/")
if sn is None:
send_response(req, '404 Not Found', [], '')
@@ -187,7 +192,7 @@ def handler(req):
headers = copyitems(req.headers_in)
rfile = _ReadOnlyRequest(req)
prev = None
-
+
try:
redirections = []
while True:
@@ -198,7 +203,7 @@ def handler(req):
request.multiprocess = bool(forked)
request.app = app
request.prev = prev
-
+
# Run the CherryPy Request object and obtain the response
try:
request.run(method, path, qs, reqproto, headers, rfile)
@@ -207,25 +212,28 @@ def handler(req):
ir = sys.exc_info()[1]
app.release_serving()
prev = request
-
+
if not recursive:
if ir.path in redirections:
- raise RuntimeError("InternalRedirector visited the "
- "same URL twice: %r" % ir.path)
+ raise RuntimeError(
+ "InternalRedirector visited the same URL "
+ "twice: %r" % ir.path)
else:
- # Add the *previous* path_info + qs to redirections.
+ # Add the *previous* path_info + qs to
+ # redirections.
if qs:
qs = "?" + qs
redirections.append(sn + path + qs)
-
+
# Munge environment and try again.
method = "GET"
path = ir.path
qs = ir.query_string
rfile = BytesIO()
-
- send_response(req, response.output_status, response.header_list,
- response.body, response.stream)
+
+ send_response(
+ req, response.output_status, response.header_list,
+ response.body, response.stream)
finally:
app.release_serving()
except:
@@ -239,7 +247,7 @@ def handler(req):
def send_response(req, status, headers, body, stream=False):
# Set response status
req.status = int(status[:3])
-
+
# Set response headers
req.content_type = "text/plain"
for header, value in headers:
@@ -247,11 +255,11 @@ def send_response(req, status, headers, body, stream=False):
req.content_type = value
continue
req.headers_out.add(header, value)
-
+
if stream:
# Flush now so the status and headers are sent immediately.
req.flush()
-
+
# Set response body
if isinstance(body, basestring):
req.write(body)
@@ -260,14 +268,12 @@ def send_response(req, status, headers, body, stream=False):
req.write(seg)
-
# --------------- Startup tools for CherryPy + mod_python --------------- #
-
-
import os
import re
try:
import subprocess
+
def popen(fullcmd):
p = subprocess.Popen(fullcmd, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
@@ -284,8 +290,12 @@ def read_process(cmd, args=""):
pipeout = popen(fullcmd)
try:
firstline = pipeout.readline()
- if (re.search(ntob("(not recognized|No such file|not found)"), firstline,
- re.IGNORECASE)):
+ cmd_not_found = re.search(
+ ntob("(not recognized|No such file|not found)"),
+ firstline,
+ re.IGNORECASE
+ )
+ if cmd_not_found:
raise IOError('%s must be on your system path.' % cmd)
output = firstline + pipeout.read()
finally:
@@ -294,7 +304,7 @@ def read_process(cmd, args=""):
class ModPythonServer(object):
-
+
template = """
# Apache2 server configuration file for running CherryPy with mod_python.
@@ -309,7 +319,7 @@ LoadModule python_module modules/mod_python.so
%(opts)s
</Location>
"""
-
+
def __init__(self, loc="/", port=80, opts=None, apache_path="apache",
handler="cherrypy._cpmodpy::handler"):
self.loc = loc
@@ -317,7 +327,7 @@ LoadModule python_module modules/mod_python.so
self.opts = opts
self.apache_path = apache_path
self.handler = handler
-
+
def start(self):
opts = "".join([" PythonOption %s %s\n" % (k, v)
for k, v in self.opts])
@@ -326,19 +336,18 @@ LoadModule python_module modules/mod_python.so
"opts": opts,
"handler": self.handler,
}
-
+
mpconf = os.path.join(os.path.dirname(__file__), "cpmodpy.conf")
f = open(mpconf, 'wb')
try:
f.write(conf_data)
finally:
f.close()
-
+
response = read_process(self.apache_path, "-k start -f %s" % mpconf)
self.ready = True
return response
-
+
def stop(self):
os.popen("apache -k stop")
self.ready = False
-
diff --git a/cherrypy/_cpnative_server.py b/cherrypy/_cpnative_server.py
index 57f715a9..e303573d 100644
--- a/cherrypy/_cpnative_server.py
+++ b/cherrypy/_cpnative_server.py
@@ -11,9 +11,9 @@ from cherrypy import wsgiserver
class NativeGateway(wsgiserver.Gateway):
-
+
recursive = False
-
+
def respond(self):
req = self.req
try:
@@ -22,7 +22,7 @@ class NativeGateway(wsgiserver.Gateway):
local = httputil.Host(local[0], local[1], "")
remote = req.conn.remote_addr, req.conn.remote_port
remote = httputil.Host(remote[0], remote[1], "")
-
+
scheme = req.scheme
sn = cherrypy.tree.script_name(req.uri or "/")
if sn is None:
@@ -35,7 +35,7 @@ class NativeGateway(wsgiserver.Gateway):
headers = req.inheaders.items()
rfile = req.rfile
prev = None
-
+
try:
redirections = []
while True:
@@ -45,32 +45,36 @@ class NativeGateway(wsgiserver.Gateway):
request.multiprocess = False
request.app = app
request.prev = prev
-
- # Run the CherryPy Request object and obtain the response
+
+ # Run the CherryPy Request object and obtain the
+ # response
try:
- request.run(method, path, qs, req.request_protocol, headers, rfile)
+ request.run(method, path, qs,
+ req.request_protocol, headers, rfile)
break
except cherrypy.InternalRedirect:
ir = sys.exc_info()[1]
app.release_serving()
prev = request
-
+
if not self.recursive:
if ir.path in redirections:
- raise RuntimeError("InternalRedirector visited the "
- "same URL twice: %r" % ir.path)
+ raise RuntimeError(
+ "InternalRedirector visited the same "
+ "URL twice: %r" % ir.path)
else:
- # Add the *previous* path_info + qs to redirections.
+ # Add the *previous* path_info + qs to
+ # redirections.
if qs:
qs = "?" + qs
redirections.append(sn + path + qs)
-
+
# Munge environment and try again.
method = "GET"
path = ir.path
qs = ir.query_string
rfile = BytesIO()
-
+
self.send_response(
response.output_status, response.header_list,
response.body)
@@ -78,59 +82,62 @@ class NativeGateway(wsgiserver.Gateway):
app.release_serving()
except:
tb = format_exc()
- #print tb
+ # print tb
cherrypy.log(tb, 'NATIVE_ADAPTER', severity=logging.ERROR)
s, h, b = bare_error()
self.send_response(s, h, b)
-
+
def send_response(self, status, headers, body):
req = self.req
-
+
# Set response status
req.status = str(status or "500 Server Error")
-
+
# Set response headers
for header, value in headers:
req.outheaders.append((header, value))
if (req.ready and not req.sent_headers):
req.sent_headers = True
req.send_headers()
-
+
# Set response body
for seg in body:
req.write(seg)
class CPHTTPServer(wsgiserver.HTTPServer):
+
"""Wrapper for wsgiserver.HTTPServer.
-
+
wsgiserver has been designed to not reference CherryPy in any way,
so that it can be used in other frameworks and applications.
Therefore, we wrap it here, so we can apply some attributes
from config -> cherrypy.server -> HTTPServer.
"""
-
+
def __init__(self, server_adapter=cherrypy.server):
self.server_adapter = server_adapter
-
+
server_name = (self.server_adapter.socket_host or
self.server_adapter.socket_file or
None)
-
+
wsgiserver.HTTPServer.__init__(
self, server_adapter.bind_addr, NativeGateway,
minthreads=server_adapter.thread_pool,
maxthreads=server_adapter.thread_pool_max,
server_name=server_name)
-
- self.max_request_header_size = self.server_adapter.max_request_header_size or 0
- self.max_request_body_size = self.server_adapter.max_request_body_size or 0
+
+ self.max_request_header_size = (
+ self.server_adapter.max_request_header_size or 0)
+ self.max_request_body_size = (
+ self.server_adapter.max_request_body_size or 0)
self.request_queue_size = self.server_adapter.socket_queue_size
self.timeout = self.server_adapter.socket_timeout
self.shutdown_timeout = self.server_adapter.shutdown_timeout
self.protocol = self.server_adapter.protocol_version
self.nodelay = self.server_adapter.nodelay
-
+
ssl_module = self.server_adapter.ssl_module or 'pyopenssl'
if self.server_adapter.ssl_context:
adapter_class = wsgiserver.get_ssl_adapter_class(ssl_module)
@@ -145,5 +152,3 @@ class CPHTTPServer(wsgiserver.HTTPServer):
self.server_adapter.ssl_certificate,
self.server_adapter.ssl_private_key,
self.server_adapter.ssl_certificate_chain)
-
-
diff --git a/cherrypy/_cpreqbody.py b/cherrypy/_cpreqbody.py
index 5d72c854..d2dbbc92 100644
--- a/cherrypy/_cpreqbody.py
+++ b/cherrypy/_cpreqbody.py
@@ -3,8 +3,10 @@
.. versionadded:: 3.2
Application authors have complete control over the parsing of HTTP request
-entities. In short, :attr:`cherrypy.request.body<cherrypy._cprequest.Request.body>`
-is now always set to an instance of :class:`RequestBody<cherrypy._cpreqbody.RequestBody>`,
+entities. In short,
+:attr:`cherrypy.request.body<cherrypy._cprequest.Request.body>`
+is now always set to an instance of
+:class:`RequestBody<cherrypy._cpreqbody.RequestBody>`,
and *that* class is a subclass of :class:`Entity<cherrypy._cpreqbody.Entity>`.
When an HTTP request includes an entity body, it is often desirable to
@@ -21,9 +23,9 @@ key to look up a value in the
:attr:`request.body.processors<cherrypy._cpreqbody.Entity.processors>` dict.
If the full media
type is not found, then the major type is tried; for example, if no processor
-is found for the 'image/jpeg' type, then we look for a processor for the 'image'
-types altogether. If neither the full type nor the major type has a matching
-processor, then a default processor is used
+is found for the 'image/jpeg' type, then we look for a processor for the
+'image' types altogether. If neither the full type nor the major type has a
+matching processor, then a default processor is used
(:func:`default_proc<cherrypy._cpreqbody.Entity.default_proc>`). For most
types, this means no processing is done, and the body is left unread as a
raw byte stream. Processors are configurable in an 'on_start_resource' hook.
@@ -53,7 +55,7 @@ Custom Processors
You can add your own processors for any specific or major MIME type. Simply add
it to the :attr:`processors<cherrypy._cprequest.Entity.processors>` dict in a
-hook/tool that runs at ``on_start_resource`` or ``before_request_body``.
+hook/tool that runs at ``on_start_resource`` or ``before_request_body``.
Here's the built-in JSON tool for an example::
def json_in(force=True, debug=False):
@@ -62,7 +64,7 @@ Here's the built-in JSON tool for an example::
\"""Read application/json data into request.json.\"""
if not entity.headers.get("Content-Length", ""):
raise cherrypy.HTTPError(411)
-
+
body = entity.fp.read()
try:
request.json = json_decode(body)
@@ -74,31 +76,36 @@ Here's the built-in JSON tool for an example::
415, 'Expected an application/json content type')
request.body.processors['application/json'] = json_processor
-We begin by defining a new ``json_processor`` function to stick in the ``processors``
-dictionary. All processor functions take a single argument, the ``Entity`` instance
-they are to process. It will be called whenever a request is received (for those
-URI's where the tool is turned on) which has a ``Content-Type`` of
-"application/json".
-
-First, it checks for a valid ``Content-Length`` (raising 411 if not valid), then
-reads the remaining bytes on the socket. The ``fp`` object knows its own length, so
-it won't hang waiting for data that never arrives. It will return when all data
-has been read. Then, we decode those bytes using Python's built-in ``json`` module,
-and stick the decoded result onto ``request.json`` . If it cannot be decoded, we
-raise 400.
-
-If the "force" argument is True (the default), the ``Tool`` clears the ``processors``
-dict so that request entities of other ``Content-Types`` aren't parsed at all. Since
-there's no entry for those invalid MIME types, the ``default_proc`` method of ``cherrypy.request.body``
-is called. But this does nothing by default (usually to provide the page handler an opportunity to handle it.)
-But in our case, we want to raise 415, so we replace ``request.body.default_proc``
+We begin by defining a new ``json_processor`` function to stick in the
+``processors`` dictionary. All processor functions take a single argument,
+the ``Entity`` instance they are to process. It will be called whenever a
+request is received (for those URI's where the tool is turned on) which
+has a ``Content-Type`` of "application/json".
+
+First, it checks for a valid ``Content-Length`` (raising 411 if not valid),
+then reads the remaining bytes on the socket. The ``fp`` object knows its
+own length, so it won't hang waiting for data that never arrives. It will
+return when all data has been read. Then, we decode those bytes using
+Python's built-in ``json`` module, and stick the decoded result onto
+``request.json`` . If it cannot be decoded, we raise 400.
+
+If the "force" argument is True (the default), the ``Tool`` clears the
+``processors`` dict so that request entities of other ``Content-Types``
+aren't parsed at all. Since there's no entry for those invalid MIME
+types, the ``default_proc`` method of ``cherrypy.request.body`` is
+called. But this does nothing by default (usually to provide the page
+handler an opportunity to handle it.)
+But in our case, we want to raise 415, so we replace
+``request.body.default_proc``
with the error (``HTTPError`` instances, when called, raise themselves).
-If we were defining a custom processor, we can do so without making a ``Tool``. Just add the config entry::
+If we were defining a custom processor, we can do so without making a ``Tool``.
+Just add the config entry::
request.body.processors = {'application/json': json_processor}
-Note that you can only replace the ``processors`` dict wholesale this way, not update the existing one.
+Note that you can only replace the ``processors`` dict wholesale this way,
+not update the existing one.
"""
try:
@@ -129,7 +136,7 @@ from cherrypy._cpcompat import basestring, ntob, ntou
from cherrypy.lib import httputil
-# -------------------------------- Processors -------------------------------- #
+# ------------------------------- Processors -------------------------------- #
def process_urlencoded(entity):
"""Read application/x-www-form-urlencoded data into entity.params."""
@@ -141,14 +148,14 @@ def process_urlencoded(entity):
for pair in aparam.split(ntob(';')):
if not pair:
continue
-
+
atoms = pair.split(ntob('='), 1)
if len(atoms) == 1:
atoms.append(ntob(''))
-
+
key = unquote_plus(atoms[0]).decode(charset)
value = unquote_plus(atoms[1]).decode(charset)
-
+
if key in params:
if not isinstance(params[key], list):
params[key] = [params[key]]
@@ -164,7 +171,7 @@ def process_urlencoded(entity):
raise cherrypy.HTTPError(
400, "The request entity could not be decoded. The following "
"charsets were attempted: %s" % repr(entity.attempt_charsets))
-
+
# Now that all values have been successfully parsed and decoded,
# apply them to the entity.params dict.
for key, value in params.items():
@@ -185,22 +192,22 @@ def process_multipart(entity):
# is often necessary to enclose the boundary parameter values in quotes
# on the Content-type line"
ib = entity.content_type.params['boundary'].strip('"')
-
+
if not re.match("^[ -~]{0,200}[!-~]$", ib):
raise ValueError('Invalid boundary in multipart form: %r' % (ib,))
-
+
ib = ('--' + ib).encode('ascii')
-
+
# Find the first marker
while True:
b = entity.readline()
if not b:
return
-
+
b = b.strip()
if b == ib:
break
-
+
# Read all parts
while True:
part = entity.part_class.from_fp(entity.fp, ib)
@@ -209,10 +216,12 @@ def process_multipart(entity):
if part.fp.done:
break
+
def process_multipart_form_data(entity):
- """Read all multipart/form-data parts into entity.parts or entity.params."""
+ """Read all multipart/form-data parts into entity.parts or entity.params.
+ """
process_multipart(entity)
-
+
kept_parts = []
for part in entity.parts:
if part.name is None:
@@ -225,28 +234,29 @@ def process_multipart_form_data(entity):
# It's a file upload. Retain the whole part so consumer code
# has access to its .file and .filename attributes.
value = part
-
+
if part.name in entity.params:
if not isinstance(entity.params[part.name], list):
entity.params[part.name] = [entity.params[part.name]]
entity.params[part.name].append(value)
else:
entity.params[part.name] = value
-
+
entity.parts = kept_parts
+
def _old_process_multipart(entity):
"""The behavior of 3.2 and lower. Deprecated and will be changed in 3.3."""
process_multipart(entity)
-
+
params = entity.params
-
+
for part in entity.parts:
if part.name is None:
key = ntou('parts')
else:
key = part.name
-
+
if part.filename is None:
# It's a regular field
value = part.fullvalue()
@@ -254,7 +264,7 @@ def _old_process_multipart(entity):
# It's a file upload. Retain the whole part so consumer code
# has access to its .file and .filename attributes.
value = part
-
+
if key in params:
if not isinstance(params[key], list):
params[key] = [params[key]]
@@ -263,31 +273,32 @@ def _old_process_multipart(entity):
params[key] = value
-
-# --------------------------------- Entities --------------------------------- #
-
-
+# -------------------------------- Entities --------------------------------- #
class Entity(object):
+
"""An HTTP request body, or MIME multipart body.
-
+
This class collects information about the HTTP request entity. When a
given entity is of MIME type "multipart", each part is parsed into its own
Entity instance, and the set of parts stored in
:attr:`entity.parts<cherrypy._cpreqbody.Entity.parts>`.
-
+
Between the ``before_request_body`` and ``before_handler`` tools, CherryPy
tries to process the request body (if any) by calling
- :func:`request.body.process<cherrypy._cpreqbody.RequestBody.process`.
- This uses the ``content_type`` of the Entity to look up a suitable processor
- in :attr:`Entity.processors<cherrypy._cpreqbody.Entity.processors>`, a dict.
+ :func:`request.body.process<cherrypy._cpreqbody.RequestBody.process>`.
+ This uses the ``content_type`` of the Entity to look up a suitable
+ processor in
+ :attr:`Entity.processors<cherrypy._cpreqbody.Entity.processors>`,
+ a dict.
If a matching processor cannot be found for the complete Content-Type,
it tries again using the major type. For example, if a request with an
entity of type "image/jpeg" arrives, but no processor can be found for
that complete type, then one is sought for the major type "image". If a
processor is still not found, then the
- :func:`default_proc<cherrypy._cpreqbody.Entity.default_proc>` method of the
- Entity is called (which does nothing by default; you can override this too).
-
+ :func:`default_proc<cherrypy._cpreqbody.Entity.default_proc>` method
+ of the Entity is called (which does nothing by default; you can
+ override this too).
+
CherryPy includes processors for the "application/x-www-form-urlencoded"
type, the "multipart/form-data" type, and the "multipart" major type.
CherryPy 3.2 processes these types almost exactly as older versions.
@@ -298,43 +309,43 @@ class Entity(object):
case it will have ``file`` and ``filename`` attributes, or possibly a
``value`` attribute). Each Part is itself a subclass of
Entity, and has its own ``process`` method and ``processors`` dict.
-
+
There is a separate processor for the "multipart" major type which is more
flexible, and simply stores all multipart parts in
:attr:`request.body.parts<cherrypy._cpreqbody.Entity.parts>`. You can
enable it with::
-
+
cherrypy.request.body.processors['multipart'] = _cpreqbody.process_multipart
-
+
in an ``on_start_resource`` tool.
"""
-
+
# http://tools.ietf.org/html/rfc2046#section-4.1.2:
# "The default character set, which must be assumed in the
# absence of a charset parameter, is US-ASCII."
# However, many browsers send data in utf-8 with no charset.
attempt_charsets = ['utf-8']
"""A list of strings, each of which should be a known encoding.
-
+
When the Content-Type of the request body warrants it, each of the given
encodings will be tried in order. The first one to successfully decode the
entity without raising an error is stored as
:attr:`entity.charset<cherrypy._cpreqbody.Entity.charset>`. This defaults
- to ``['utf-8']`` (plus 'ISO-8859-1' for "text/\*" types, as required by
- `HTTP/1.1 <http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.1>`_),
+ to ``['utf-8']`` (plus 'ISO-8859-1' for "text/\*" types, as required by
+ `HTTP/1.1 <http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.1>`_),
but ``['us-ascii', 'utf-8']`` for multipart parts.
"""
-
+
charset = None
"""The successful decoding; see "attempt_charsets" above."""
-
+
content_type = None
"""The value of the Content-Type request header.
-
+
If the Entity is part of a multipart payload, this will be the Content-Type
given in the MIME headers for this part.
"""
-
+
default_content_type = 'application/x-www-form-urlencoded'
"""This defines a default ``Content-Type`` to use if no Content-Type header
is given. The empty string is used for RequestBody, which results in the
@@ -344,26 +355,26 @@ class Entity(object):
declares that a part with no Content-Type defaults to "text/plain"
(see :class:`Part<cherrypy._cpreqbody.Part>`).
"""
-
+
filename = None
"""The ``Content-Disposition.filename`` header, if available."""
-
+
fp = None
"""The readable socket file object."""
-
+
headers = None
"""A dict of request/multipart header names and values.
-
+
This is a copy of the ``request.headers`` for the ``request.body``;
for multipart parts, it is the set of headers for that part.
"""
-
+
length = None
"""The value of the ``Content-Length`` header, if provided."""
-
+
name = None
"""The "name" parameter of the ``Content-Disposition`` header, if any."""
-
+
params = None
"""
If the request Content-Type is 'application/x-www-form-urlencoded' or
@@ -373,39 +384,40 @@ class Entity(object):
can be sent with various HTTP method verbs). This value is set between
the 'before_request_body' and 'before_handler' hooks (assuming that
process_request_body is True)."""
-
+
processors = {'application/x-www-form-urlencoded': process_urlencoded,
'multipart/form-data': process_multipart_form_data,
'multipart': process_multipart,
}
"""A dict of Content-Type names to processor methods."""
-
+
parts = None
- """A list of Part instances if ``Content-Type`` is of major type "multipart"."""
-
+ """A list of Part instances if ``Content-Type`` is of major type
+ "multipart"."""
+
part_class = None
"""The class used for multipart parts.
-
+
You can replace this with custom subclasses to alter the processing of
multipart parts.
"""
-
+
def __init__(self, fp, headers, params=None, parts=None):
# Make an instance-specific copy of the class processors
# so Tools, etc. can replace them per-request.
self.processors = self.processors.copy()
-
+
self.fp = fp
self.headers = headers
-
+
if params is None:
params = {}
self.params = params
-
+
if parts is None:
parts = []
self.parts = parts
-
+
# Content-Type
self.content_type = headers.elements('Content-Type')
if self.content_type:
@@ -413,25 +425,29 @@ class Entity(object):
else:
self.content_type = httputil.HeaderElement.from_str(
self.default_content_type)
-
- # Copy the class 'attempt_charsets', prepending any Content-Type charset
+
+ # Copy the class 'attempt_charsets', prepending any Content-Type
+ # charset
dec = self.content_type.params.get("charset", None)
if dec:
self.attempt_charsets = [dec] + [c for c in self.attempt_charsets
if c != dec]
else:
self.attempt_charsets = self.attempt_charsets[:]
-
+
# Length
self.length = None
clen = headers.get('Content-Length', None)
# If Transfer-Encoding is 'chunked', ignore any Content-Length.
- if clen is not None and 'chunked' not in headers.get('Transfer-Encoding', ''):
+ if (
+ clen is not None and
+ 'chunked' not in headers.get('Transfer-Encoding', '')
+ ):
try:
self.length = int(clen)
except ValueError:
pass
-
+
# Content-Disposition
self.name = None
self.filename = None
@@ -444,25 +460,31 @@ class Entity(object):
self.name = self.name[1:-1]
if 'filename' in disp.params:
self.filename = disp.params['filename']
- if self.filename.startswith('"') and self.filename.endswith('"'):
+ if (
+ self.filename.startswith('"') and
+ self.filename.endswith('"')
+ ):
self.filename = self.filename[1:-1]
-
+
# The 'type' attribute is deprecated in 3.2; remove it in 3.3.
- type = property(lambda self: self.content_type,
- doc="""A deprecated alias for :attr:`content_type<cherrypy._cpreqbody.Entity.content_type>`.""")
-
+ type = property(
+ lambda self: self.content_type,
+ doc="A deprecated alias for "
+ ":attr:`content_type<cherrypy._cpreqbody.Entity.content_type>`."
+ )
+
def read(self, size=None, fp_out=None):
return self.fp.read(size, fp_out)
-
+
def readline(self, size=None):
return self.fp.readline(size)
-
+
def readlines(self, sizehint=None):
return self.fp.readlines(sizehint)
-
+
def __iter__(self):
return self
-
+
def __next__(self):
line = self.readline()
if not line:
@@ -471,21 +493,24 @@ class Entity(object):
def next(self):
return self.__next__()
-
+
def read_into_file(self, fp_out=None):
- """Read the request body into fp_out (or make_file() if None). Return fp_out."""
+ """Read the request body into fp_out (or make_file() if None).
+
+ Return fp_out.
+ """
if fp_out is None:
fp_out = self.make_file()
self.read(fp_out=fp_out)
return fp_out
-
+
def make_file(self):
"""Return a file-like object into which the request body will be read.
-
+
By default, this will return a TemporaryFile. Override as needed.
See also :attr:`cherrypy._cpreqbody.Part.maxrambytes`."""
return tempfile.TemporaryFile()
-
+
def fullvalue(self):
"""Return this entity as a string, whether stored in a file or not."""
if self.file:
@@ -496,7 +521,7 @@ class Entity(object):
else:
value = self.value
return value
-
+
def process(self):
"""Execute the best-match processor for the given media type."""
proc = None
@@ -513,9 +538,11 @@ class Entity(object):
self.default_proc()
else:
proc(self)
-
+
def default_proc(self):
- """Called if a more-specific processor is not found for the ``Content-Type``."""
+ """Called if a more-specific processor is not found for the
+ ``Content-Type``.
+ """
# Leave the fp alone for someone else to read. This works fine
# for request.body, but the Part subclasses need to override this
# so they can move on to the next part.
@@ -523,25 +550,26 @@ class Entity(object):
class Part(Entity):
+
"""A MIME part entity, part of a multipart entity."""
-
+
# "The default character set, which must be assumed in the absence of a
# charset parameter, is US-ASCII."
attempt_charsets = ['us-ascii', 'utf-8']
"""A list of strings, each of which should be a known encoding.
-
+
When the Content-Type of the request body warrants it, each of the given
encodings will be tried in order. The first one to successfully decode the
entity without raising an error is stored as
:attr:`entity.charset<cherrypy._cpreqbody.Entity.charset>`. This defaults
- to ``['utf-8']`` (plus 'ISO-8859-1' for "text/\*" types, as required by
- `HTTP/1.1 <http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.1>`_),
+ to ``['utf-8']`` (plus 'ISO-8859-1' for "text/\*" types, as required by
+ `HTTP/1.1 <http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.1>`_),
but ``['us-ascii', 'utf-8']`` for multipart parts.
"""
-
+
boundary = None
"""The MIME multipart boundary."""
-
+
default_content_type = 'text/plain'
"""This defines a default ``Content-Type`` to use if no Content-Type header
is given. The empty string is used for RequestBody, which results in the
@@ -551,26 +579,27 @@ class Part(Entity):
the MIME spec declares that a part with no Content-Type defaults to
"text/plain".
"""
-
+
# This is the default in stdlib cgi. We may want to increase it.
maxrambytes = 1000
- """The threshold of bytes after which point the ``Part`` will store its data
- in a file (generated by :func:`make_file<cherrypy._cprequest.Entity.make_file>`)
- instead of a string. Defaults to 1000, just like the :mod:`cgi` module in
- Python's standard library.
+ """The threshold of bytes after which point the ``Part`` will store
+ its data in a file (generated by
+ :func:`make_file<cherrypy._cprequest.Entity.make_file>`)
+ instead of a string. Defaults to 1000, just like the :mod:`cgi`
+ module in Python's standard library.
"""
-
+
def __init__(self, fp, headers, boundary):
Entity.__init__(self, fp, headers)
self.boundary = boundary
self.file = None
self.value = None
-
+
def from_fp(cls, fp, boundary):
headers = cls.read_headers(fp)
return cls(fp, headers, boundary)
from_fp = classmethod(from_fp)
-
+
def read_headers(cls, fp):
headers = httputil.HeaderMap()
while True:
@@ -578,13 +607,13 @@ class Part(Entity):
if not line:
# No more data--illegal end of headers
raise EOFError("Illegal end of headers.")
-
+
if line == ntob('\r\n'):
# Normal end of headers
break
if not line.endswith(ntob('\r\n')):
raise ValueError("MIME requires CRLF terminators: %r" % line)
-
+
if line[0] in ntob(' \t'):
# It's a continuation line.
v = line.strip().decode('ISO-8859-1')
@@ -592,24 +621,24 @@ class Part(Entity):
k, v = line.split(ntob(":"), 1)
k = k.strip().decode('ISO-8859-1')
v = v.strip().decode('ISO-8859-1')
-
+
existing = headers.get(k)
if existing:
v = ", ".join((existing, v))
headers[k] = v
-
+
return headers
read_headers = classmethod(read_headers)
-
+
def read_lines_to_boundary(self, fp_out=None):
"""Read bytes from self.fp and return or write them to a file.
-
+
If the 'fp_out' argument is None (the default), all bytes read are
returned in a single byte string.
-
- If the 'fp_out' argument is not None, it must be a file-like object that
- supports the 'write' method; all bytes read will be written to the fp,
- and that fp is returned.
+
+ If the 'fp_out' argument is not None, it must be a file-like
+ object that supports the 'write' method; all bytes read will be
+ written to the fp, and that fp is returned.
"""
endmarker = self.boundary + ntob("--")
delim = ntob("")
@@ -617,7 +646,7 @@ class Part(Entity):
lines = []
seen = 0
while True:
- line = self.fp.readline(1<<16)
+ line = self.fp.readline(1 << 16)
if not line:
raise EOFError("Illegal end of multipart body.")
if line.startswith(ntob("--")) and prev_lf:
@@ -627,9 +656,9 @@ class Part(Entity):
if strippedline == endmarker:
self.fp.finish()
break
-
+
line = delim + line
-
+
if line.endswith(ntob("\r\n")):
delim = ntob("\r\n")
line = line[:-2]
@@ -641,7 +670,7 @@ class Part(Entity):
else:
delim = ntob("")
prev_lf = False
-
+
if fp_out is None:
lines.append(line)
seen += len(line)
@@ -651,7 +680,7 @@ class Part(Entity):
fp_out.write(line)
else:
fp_out.write(line)
-
+
if fp_out is None:
result = ntob('').join(lines)
for charset in self.attempt_charsets:
@@ -664,14 +693,18 @@ class Part(Entity):
return result
else:
raise cherrypy.HTTPError(
- 400, "The request entity could not be decoded. The following "
- "charsets were attempted: %s" % repr(self.attempt_charsets))
+ 400,
+ "The request entity could not be decoded. The following "
+ "charsets were attempted: %s" % repr(self.attempt_charsets)
+ )
else:
fp_out.seek(0)
return fp_out
-
+
def default_proc(self):
- """Called if a more-specific processor is not found for the ``Content-Type``."""
+ """Called if a more-specific processor is not found for the
+ ``Content-Type``.
+ """
if self.filename:
# Always read into a file if a .filename was given.
self.file = self.read_into_file()
@@ -681,9 +714,12 @@ class Part(Entity):
self.value = result
else:
self.file = result
-
+
def read_into_file(self, fp_out=None):
- """Read the request body into fp_out (or make_file() if None). Return fp_out."""
+ """Read the request body into fp_out (or make_file() if None).
+
+ Return fp_out.
+ """
if fp_out is None:
fp_out = self.make_file()
self.read_lines_to_boundary(fp_out=fp_out)
@@ -696,23 +732,30 @@ try:
except ValueError:
# Python 2.4 and lower
class Infinity(object):
+
def __cmp__(self, other):
return 1
+
def __sub__(self, other):
return self
inf = Infinity()
-comma_separated_headers = ['Accept', 'Accept-Charset', 'Accept-Encoding',
- 'Accept-Language', 'Accept-Ranges', 'Allow', 'Cache-Control', 'Connection',
- 'Content-Encoding', 'Content-Language', 'Expect', 'If-Match',
- 'If-None-Match', 'Pragma', 'Proxy-Authenticate', 'Te', 'Trailer',
- 'Transfer-Encoding', 'Upgrade', 'Vary', 'Via', 'Warning', 'Www-Authenticate']
+comma_separated_headers = [
+ 'Accept', 'Accept-Charset', 'Accept-Encoding',
+ 'Accept-Language', 'Accept-Ranges', 'Allow',
+ 'Cache-Control', 'Connection', 'Content-Encoding',
+ 'Content-Language', 'Expect', 'If-Match',
+ 'If-None-Match', 'Pragma', 'Proxy-Authenticate',
+ 'Te', 'Trailer', 'Transfer-Encoding', 'Upgrade',
+ 'Vary', 'Via', 'Warning', 'Www-Authenticate'
+]
class SizedReader:
-
- def __init__(self, fp, length, maxbytes, bufsize=DEFAULT_BUFFER_SIZE, has_trailers=False):
+
+ def __init__(self, fp, length, maxbytes, bufsize=DEFAULT_BUFFER_SIZE,
+ has_trailers=False):
# Wrap our fp in a buffer so peek() works
self.fp = fp
self.length = length
@@ -722,25 +765,25 @@ class SizedReader:
self.bytes_read = 0
self.done = False
self.has_trailers = has_trailers
-
+
def read(self, size=None, fp_out=None):
"""Read bytes from the request body and return or write them to a file.
-
+
A number of bytes less than or equal to the 'size' argument are read
off the socket. The actual number of bytes read are tracked in
self.bytes_read. The number may be smaller than 'size' when 1) the
client sends fewer bytes, 2) the 'Content-Length' request header
specifies fewer bytes than requested, or 3) the number of bytes read
exceeds self.maxbytes (in which case, 413 is raised).
-
+
If the 'fp_out' argument is None (the default), all bytes read are
returned in a single byte string.
-
- If the 'fp_out' argument is not None, it must be a file-like object that
- supports the 'write' method; all bytes read will be written to the fp,
- and None is returned.
+
+ If the 'fp_out' argument is not None, it must be a file-like
+ object that supports the 'write' method; all bytes read will be
+ written to the fp, and None is returned.
"""
-
+
if self.length is None:
if size is None:
remaining = inf
@@ -756,9 +799,9 @@ class SizedReader:
return ntob('')
else:
return None
-
+
chunks = []
-
+
# Read bytes from the buffer.
if self.buffer:
if remaining is inf:
@@ -769,18 +812,18 @@ class SizedReader:
self.buffer = self.buffer[remaining:]
datalen = len(data)
remaining -= datalen
-
+
# Check lengths.
self.bytes_read += datalen
if self.maxbytes and self.bytes_read > self.maxbytes:
raise cherrypy.HTTPError(413)
-
+
# Store the data.
if fp_out is None:
chunks.append(data)
else:
fp_out.write(data)
-
+
# Read bytes from the socket.
while remaining > 0:
chunksize = min(remaining, self.bufsize)
@@ -799,21 +842,21 @@ class SizedReader:
break
datalen = len(data)
remaining -= datalen
-
+
# Check lengths.
self.bytes_read += datalen
if self.maxbytes and self.bytes_read > self.maxbytes:
raise cherrypy.HTTPError(413)
-
+
# Store the data.
if fp_out is None:
chunks.append(data)
else:
fp_out.write(data)
-
+
if fp_out is None:
return ntob('').join(chunks)
-
+
def readline(self, size=None):
"""Read a line from the request body and return it."""
chunks = []
@@ -834,7 +877,7 @@ class SizedReader:
else:
chunks.append(data)
return ntob('').join(chunks)
-
+
def readlines(self, sizehint=None):
"""Read lines from the request body and return them."""
if self.length is not None:
@@ -842,7 +885,7 @@ class SizedReader:
sizehint = self.length - self.bytes_read
else:
sizehint = min(sizehint, self.length - self.bytes_read)
-
+
lines = []
seen = 0
while True:
@@ -854,12 +897,12 @@ class SizedReader:
if seen >= sizehint:
break
return lines
-
+
def finish(self):
self.done = True
if self.has_trailers and hasattr(self.fp, 'read_trailer_lines'):
self.trailers = {}
-
+
try:
for line in self.fp.read_trailer_lines():
if line[0] in ntob(' \t'):
@@ -872,7 +915,7 @@ class SizedReader:
raise ValueError("Illegal header line.")
k = k.strip().title()
v = v.strip()
-
+
if k in comma_separated_headers:
existing = self.trailers.get(envname)
if existing:
@@ -889,13 +932,15 @@ class SizedReader:
class RequestBody(Entity):
+
"""The entity of the HTTP request."""
-
+
bufsize = 8 * 1024
"""The buffer size used when reading the socket."""
-
+
# Don't parse the request body at all if the client didn't provide
- # a Content-Type header. See http://www.cherrypy.org/ticket/790
+ # a Content-Type header. See
+ # https://bitbucket.org/cherrypy/cherrypy/issue/790
default_content_type = ''
"""This defines a default ``Content-Type`` to use if no Content-Type header
is given. The empty string is used for RequestBody, which results in the
@@ -905,13 +950,15 @@ class RequestBody(Entity):
declares that a part with no Content-Type defaults to "text/plain"
(see :class:`Part<cherrypy._cpreqbody.Part>`).
"""
-
+
maxbytes = None
- """Raise ``MaxSizeExceeded`` if more bytes than this are read from the socket."""
-
+ """Raise ``MaxSizeExceeded`` if more bytes than this are read from
+ the socket.
+ """
+
def __init__(self, fp, headers, params=None, request_params=None):
Entity.__init__(self, fp, headers, params)
-
+
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.1
# When no explicit charset parameter is provided by the
# sender, media subtypes of the "text" type are defined
@@ -923,14 +970,14 @@ class RequestBody(Entity):
break
else:
self.attempt_charsets.append('ISO-8859-1')
-
+
# Temporary fix while deprecating passing .parts as .params.
self.processors['multipart'] = _old_process_multipart
-
+
if request_params is None:
request_params = {}
self.request_params = request_params
-
+
def process(self):
"""Process the request entity based on its Content-Type."""
# "The presence of a message-body in a request is signaled by the
@@ -942,21 +989,22 @@ class RequestBody(Entity):
h = cherrypy.serving.request.headers
if 'Content-Length' not in h and 'Transfer-Encoding' not in h:
raise cherrypy.HTTPError(411)
-
+
self.fp = SizedReader(self.fp, self.length,
self.maxbytes, bufsize=self.bufsize,
has_trailers='Trailer' in h)
super(RequestBody, self).process()
-
+
# Body params should also be a part of the request_params
# add them in here.
request_params = self.request_params
for key, value in self.params.items():
- # Python 2 only: keyword arguments must be byte strings (type 'str').
+ # Python 2 only: keyword arguments must be byte strings (type
+ # 'str').
if sys.version_info < (3, 0):
if isinstance(key, unicode):
key = key.encode('ISO-8859-1')
-
+
if key in request_params:
if not isinstance(request_params[key], list):
request_params[key] = [request_params[key]]
diff --git a/cherrypy/_cprequest.py b/cherrypy/_cprequest.py
index 5890c728..290bd2eb 100644
--- a/cherrypy/_cprequest.py
+++ b/cherrypy/_cprequest.py
@@ -13,42 +13,43 @@ from cherrypy.lib import httputil, file_generator
class Hook(object):
+
"""A callback and its metadata: failsafe, priority, and kwargs."""
-
+
callback = None
"""
The bare callable that this Hook object is wrapping, which will
be called when the Hook is called."""
-
+
failsafe = False
"""
If True, the callback is guaranteed to run even if other callbacks
from the same call point raise exceptions."""
-
+
priority = 50
"""
Defines the order of execution for a list of Hooks. Priority numbers
should be limited to the closed interval [0, 100], but values outside
this range are acceptable, as are fractional values."""
-
+
kwargs = {}
"""
A set of keyword arguments that will be passed to the
callable on each call."""
-
+
def __init__(self, callback, failsafe=None, priority=None, **kwargs):
self.callback = callback
-
+
if failsafe is None:
failsafe = getattr(callback, "failsafe", False)
self.failsafe = failsafe
-
+
if priority is None:
priority = getattr(callback, "priority", 50)
self.priority = priority
-
+
self.kwargs = kwargs
-
+
def __lt__(self, other):
# Python 3
return self.priority < other.priority
@@ -56,11 +57,11 @@ class Hook(object):
def __cmp__(self, other):
# Python 2
return cmp(self.priority, other.priority)
-
+
def __call__(self):
"""Run self.callback(**self.kwargs)."""
return self.callback(**self.kwargs)
-
+
def __repr__(self):
cls = self.__class__
return ("%s.%s(callback=%r, failsafe=%r, priority=%r, %s)"
@@ -71,21 +72,22 @@ class Hook(object):
class HookMap(dict):
+
"""A map of call points to lists of callbacks (Hook objects)."""
-
+
def __new__(cls, points=None):
d = dict.__new__(cls)
for p in points or []:
d[p] = []
return d
-
+
def __init__(self, *a, **kw):
pass
-
+
def attach(self, point, callback, failsafe=None, priority=None, **kwargs):
"""Append a new Hook made from the supplied arguments."""
self[point].append(Hook(callback, failsafe, priority, **kwargs))
-
+
def run(self, point):
"""Execute all registered Hooks (callbacks) for the given point."""
exc = None
@@ -110,7 +112,7 @@ class HookMap(dict):
cherrypy.log(traceback=True, severity=40)
if exc:
raise exc
-
+
def __copy__(self):
newmap = self.__class__()
# We can't just use 'update' because we want copies of the
@@ -119,10 +121,14 @@ class HookMap(dict):
newmap[k] = v[:]
return newmap
copy = __copy__
-
+
def __repr__(self):
cls = self.__class__
- return "%s.%s(points=%r)" % (cls.__module__, cls.__name__, copykeys(self))
+ return "%s.%s(points=%r)" % (
+ cls.__module__,
+ cls.__name__,
+ copykeys(self)
+ )
# Config namespace handlers
@@ -139,14 +145,17 @@ def hooks_namespace(k, v):
v = Hook(v)
cherrypy.serving.request.hooks[hookpoint].append(v)
+
def request_namespace(k, v):
"""Attach request attributes declared in config."""
- # Provides config entries to set request.body attrs (like attempt_charsets).
+ # Provides config entries to set request.body attrs (like
+ # attempt_charsets).
if k[:5] == 'body.':
setattr(cherrypy.serving.request.body, k[5:], v)
else:
setattr(cherrypy.serving.request, k, v)
+
def response_namespace(k, v):
"""Attach response attributes declared in config."""
# Provides config entries to set default response headers
@@ -156,6 +165,7 @@ def response_namespace(k, v):
else:
setattr(cherrypy.serving.response, k, v)
+
def error_page_namespace(k, v):
"""Attach error pages declared in config."""
if k != 'default':
@@ -170,8 +180,9 @@ hookpoints = ['on_start_resource', 'before_request_body',
class Request(object):
+
"""An HTTP request.
-
+
This object represents the metadata of an HTTP request message;
that is, it contains attributes which describe the environment
in which the request URL, headers, and body were sent (if you
@@ -181,43 +192,43 @@ class Request(object):
also contains data regarding the configuration in effect for
the given URL, and the execution plan for generating a response.
"""
-
+
prev = None
"""
The previous Request object (if any). This should be None
unless we are processing an InternalRedirect."""
-
+
# Conversation/connection attributes
local = httputil.Host("127.0.0.1", 80)
"An httputil.Host(ip, port, hostname) object for the server socket."
-
+
remote = httputil.Host("127.0.0.1", 1111)
"An httputil.Host(ip, port, hostname) object for the client socket."
-
+
scheme = "http"
"""
The protocol used between client and server. In most cases,
this will be either 'http' or 'https'."""
-
+
server_protocol = "HTTP/1.1"
"""
The HTTP version for which the HTTP server is at least
conditionally compliant."""
-
+
base = ""
"""The (scheme://host) portion of the requested URL.
In some cases (e.g. when proxying via mod_rewrite), this may contain
path segments which cherrypy.url uses when constructing url's, but
which otherwise are ignored by CherryPy. Regardless, this value
MUST NOT end in a slash."""
-
+
# Request-Line attributes
request_line = ""
"""
The complete Request-Line received from the client. This is a
single string consisting of the request method, URI, and protocol
version (joined by spaces). Any final CRLF is removed."""
-
+
method = "GET"
"""
Indicates the HTTP method to be performed on the resource identified
@@ -225,7 +236,7 @@ class Request(object):
DELETE. CherryPy allows any extension method; however, various HTTP
servers and gateways may restrict the set of allowable methods.
CherryPy applications SHOULD restrict the set (on a per-URI basis)."""
-
+
query_string = ""
"""
The query component of the Request-URI, a string of information to be
@@ -233,7 +244,7 @@ class Request(object):
path component, and is separated by a '?'. For example, the URI
'http://www.cherrypy.org/wiki?a=3&b=4' has the query component,
'a=3&b=4'."""
-
+
query_string_encoding = 'utf8'
"""
The encoding expected for query string arguments after % HEX HEX decoding).
@@ -242,7 +253,7 @@ class Request(object):
arbitrary encodings to not error, set this to 'Latin-1'; you can then
encode back to bytes and re-decode to whatever encoding you like later.
"""
-
+
protocol = (1, 1)
"""The HTTP protocol version corresponding to the set
of features which should be allowed in the response. If BOTH
@@ -250,20 +261,20 @@ class Request(object):
compliance is HTTP/1.1, this attribute will be the tuple (1, 1).
If either is 1.0, this attribute will be the tuple (1, 0).
Lower HTTP protocol versions are not explicitly supported."""
-
+
params = {}
"""
A dict which combines query string (GET) and request entity (POST)
variables. This is populated in two stages: GET params are added
before the 'on_start_resource' hook, and POST params are added
between the 'before_request_body' and 'before_handler' hooks."""
-
+
# Message attributes
header_list = []
"""
A list of the HTTP request headers as (name, value) tuples.
In general, you should use request.headers (a dict) instead."""
-
+
headers = httputil.HeaderMap()
"""
A dict-like object containing the request headers. Keys are header
@@ -272,10 +283,10 @@ class Request(object):
headers['content-type'] refer to the same value. Values are header
values (decoded according to :rfc:`2047` if necessary). See also:
httputil.HeaderMap, httputil.HeaderElement."""
-
+
cookie = SimpleCookie()
"""See help(Cookie)."""
-
+
rfile = None
"""
If the request included an entity (body), it will be available
@@ -283,11 +294,11 @@ class Request(object):
be read for you between the 'before_request_body' hook and the
'before_handler' hook, and the resulting string is placed into
either request.params or the request.body attribute.
-
+
You may disable the automatic consumption of the rfile by setting
request.process_request_body to False, either in config for the desired
path, or in an 'on_start_resource' or 'before_request_body' hook.
-
+
WARNING: In almost every case, you should not attempt to read from the
rfile stream after CherryPy's automatic mechanism has read it. If you
turn off the automatic parsing of rfile, you should read exactly the
@@ -295,17 +306,20 @@ class Request(object):
Ignoring either of these warnings may result in a hung request thread
or in corruption of the next (pipelined) request.
"""
-
+
process_request_body = True
"""
If True, the rfile (if any) is automatically read and parsed,
and the result placed into request.params or request.body."""
-
+
methods_with_bodies = ("POST", "PUT")
"""
A sequence of HTTP methods for which CherryPy will automatically
- attempt to read a body from the rfile."""
-
+ attempt to read a body from the rfile. If you are going to change
+ this property, modify it on the configuration (recommended)
+ or on the "hook point" `on_start_resource`.
+ """
+
body = None
"""
If the request Content-Type is 'application/x-www-form-urlencoded'
@@ -313,7 +327,7 @@ class Request(object):
of :class:`RequestBody<cherrypy._cpreqbody.RequestBody>` (which you
can .read()); this value is set between the 'before_request_body' and
'before_handler' hooks (assuming that process_request_body is True)."""
-
+
# Dispatch attributes
dispatch = cherrypy.dispatch.Dispatcher()
"""
@@ -322,19 +336,19 @@ class Request(object):
request attributes, and the application architecture. The core
calls the dispatcher as early as possible, passing it a 'path_info'
argument.
-
+
The default dispatcher discovers the page handler by matching path_info
to a hierarchical arrangement of objects, starting at request.app.root.
See help(cherrypy.dispatch) for more information."""
-
+
script_name = ""
"""
The 'mount point' of the application which is handling this request.
-
+
This attribute MUST NOT end in a slash. If the script_name refers to
the root of the URI, it MUST be an empty string (not "/").
"""
-
+
path_info = "/"
"""
The 'relative path' portion of the Request-URI. This is relative
@@ -346,12 +360,12 @@ class Request(object):
When authentication is used during the request processing this is
set to 'False' if it failed and to the 'username' value if it succeeded.
The default 'None' implies that no authentication happened."""
-
+
# Note that cherrypy.url uses "if request.app:" to determine whether
# the call is during a real HTTP request or not. So leave this None.
app = None
"""The cherrypy.Application object which is handling this request."""
-
+
handler = None
"""
The function, method, or other callable which CherryPy will call to
@@ -360,12 +374,12 @@ class Request(object):
By default, the handler is discovered by walking a tree of objects
starting at request.app.root, and is then passed all HTTP params
(from the query string and POST body) as keyword arguments."""
-
+
toolmaps = {}
"""
A nested dict of all Toolboxes and Tools in effect for this request,
of the form: {Toolbox.namespace: {Tool.name: config dict}}."""
-
+
config = None
"""
A flat dict of all configuration entries which apply to the
@@ -375,7 +389,7 @@ class Request(object):
effect for this request; by default, handler config can be attached
anywhere in the tree between request.app.root and the final handler,
and inherits downward)."""
-
+
is_index = None
"""
This will be True if the current request is mapped to an 'index'
@@ -383,7 +397,7 @@ class Request(object):
a slash). The value may be used to automatically redirect the
user-agent to a 'more canonical' URL which either adds or removes
the trailing slash. See cherrypy.tools.trailing_slash."""
-
+
hooks = HookMap(hookpoints)
"""
A HookMap (dict-like object) of the form: {hookpoint: [hook, ...]}.
@@ -392,7 +406,7 @@ class Request(object):
The list of hooks is generally populated as early as possible (mostly
from Tools specified in config), but may be extended at any time.
See also: _cprequest.Hook, _cprequest.HookMap, and cherrypy.tools."""
-
+
error_response = cherrypy.HTTPError(500).set_response
"""
The no-arg callable which will handle unexpected, untrapped errors
@@ -402,31 +416,31 @@ class Request(object):
via request.error_page or by overriding HTTPError.set_response).
By default, error_response uses HTTPError(500) to return a generic
error response to the user-agent."""
-
+
error_page = {}
"""
A dict of {error code: response filename or callable} pairs.
-
+
The error code must be an int representing a given HTTP error code,
or the string 'default', which will be used if no matching entry
is found for a given numeric code.
-
+
If a filename is provided, the file should contain a Python string-
- formatting template, and can expect by default to receive format
+ formatting template, and can expect by default to receive format
values with the mapping keys %(status)s, %(message)s, %(traceback)s,
and %(version)s. The set of format mappings can be extended by
overriding HTTPError.set_response.
-
+
If a callable is provided, it will be called by default with keyword
arguments 'status', 'message', 'traceback', and 'version', as for a
- string-formatting template. The callable must return a string or iterable of
- strings which will be set to response.body. It may also override headers or
- perform any other processing.
-
+ string-formatting template. The callable must return a string or
+ iterable of strings which will be set to response.body. It may also
+ override headers or perform any other processing.
+
If no entry is given for an error code, and no 'default' entry exists,
a default template will be used.
"""
-
+
show_tracebacks = True
"""
If True, unexpected errors encountered during request processing will
@@ -436,23 +450,23 @@ class Request(object):
"""
If True, mismatched parameters encountered during PageHandler invocation
processing will be included in the response body."""
-
+
throws = (KeyboardInterrupt, SystemExit, cherrypy.InternalRedirect)
"""The sequence of exceptions which Request.run does not trap."""
-
+
throw_errors = False
"""
If True, Request.run will not trap any errors (except HTTPRedirect and
HTTPError, which are more properly called 'exceptions', not errors)."""
-
+
closed = False
"""True once the close method has been called, False otherwise."""
-
+
stage = None
"""
A string containing the stage reached in the request-handling process.
This is useful when debugging a live server with hung requests."""
-
+
namespaces = _cpconfig.NamespaceSet(
**{"hooks": hooks_namespace,
"request": request_namespace,
@@ -460,11 +474,11 @@ class Request(object):
"error_page": error_page_namespace,
"tools": cherrypy.tools,
})
-
+
def __init__(self, local_host, remote_host, scheme="http",
server_protocol="HTTP/1.1"):
"""Populate a new Request object.
-
+
local_host should be an httputil.Host object with the server info.
remote_host should be an httputil.Host object with the client info.
scheme should be a string, either "http" or "https".
@@ -473,17 +487,17 @@ class Request(object):
self.remote = remote_host
self.scheme = scheme
self.server_protocol = server_protocol
-
+
self.closed = False
-
+
# Put a *copy* of the class error_page into self.
self.error_page = self.error_page.copy()
-
+
# Put a *copy* of the class namespaces into self.
self.namespaces = self.namespaces.copy()
-
+
self.stage = None
-
+
def close(self):
"""Run cleanup code. (Core)"""
if not self.closed:
@@ -491,49 +505,49 @@ class Request(object):
self.stage = 'on_end_request'
self.hooks.run('on_end_request')
self.stage = 'close'
-
+
def run(self, method, path, query_string, req_protocol, headers, rfile):
r"""Process the Request. (Core)
-
+
method, path, query_string, and req_protocol should be pulled directly
from the Request-Line (e.g. "GET /path?key=val HTTP/1.0").
-
+
path
This should be %XX-unquoted, but query_string should not be.
-
+
When using Python 2, they both MUST be byte strings,
not unicode strings.
-
+
When using Python 3, they both MUST be unicode strings,
not byte strings, and preferably not bytes \x00-\xFF
disguised as unicode.
-
+
headers
A list of (name, value) tuples.
-
+
rfile
A file-like object containing the HTTP request entity.
-
+
When run() is done, the returned object should have 3 attributes:
-
+
* status, e.g. "200 OK"
* header_list, a list of (name, value) tuples
* body, an iterable yielding strings
-
+
Consumer code (HTTP servers) should then access these response
attributes to build the outbound stream.
-
+
"""
response = cherrypy.serving.response
self.stage = 'run'
try:
self.error_response = cherrypy.HTTPError(500).set_response
-
+
self.method = method
path = path or "/"
self.query_string = query_string or ''
self.params = {}
-
+
# Compare request and server HTTP protocol versions, in case our
# server does not support the requested protocol. Limit our output
# to min(req, server). We want the following output:
@@ -550,30 +564,30 @@ class Request(object):
sp = int(self.server_protocol[5]), int(self.server_protocol[7])
self.protocol = min(rp, sp)
response.headers.protocol = self.protocol
-
+
# Rebuild first line of the request (e.g. "GET /path HTTP/1.0").
url = path
if query_string:
url += '?' + query_string
self.request_line = '%s %s %s' % (method, url, req_protocol)
-
+
self.header_list = list(headers)
self.headers = httputil.HeaderMap()
-
+
self.rfile = rfile
self.body = None
-
+
self.cookie = SimpleCookie()
self.handler = None
-
+
# path_info should be the path from the
# app root (script_name) to the handler.
self.script_name = self.app.script_name
self.path_info = pi = path[len(self.script_name):]
-
+
self.stage = 'respond'
self.respond(pi)
-
+
except self.throws:
raise
except:
@@ -589,24 +603,24 @@ class Request(object):
body = ""
r = bare_error(body)
response.output_status, response.header_list, response.body = r
-
+
if self.method == "HEAD":
# HEAD requests MUST NOT return a message-body in the response.
response.body = []
-
+
try:
cherrypy.log.access()
except:
cherrypy.log.error(traceback=True)
-
+
if response.timed_out:
raise cherrypy.TimeoutError()
-
+
return response
-
+
# Uncomment for stage debugging
# stage = property(lambda self: self._stage, lambda self, v: print(v))
-
+
def respond(self, path_info):
"""Generate a response for the resource at self.path_info. (Core)"""
response = cherrypy.serving.response
@@ -615,30 +629,30 @@ class Request(object):
try:
if self.app is None:
raise cherrypy.NotFound()
-
+
# Get the 'Host' header, so we can HTTPRedirect properly.
self.stage = 'process_headers'
self.process_headers()
-
+
# Make a copy of the class hooks
self.hooks = self.__class__.hooks.copy()
self.toolmaps = {}
-
+
self.stage = 'get_resource'
self.get_resource(path_info)
-
+
self.body = _cpreqbody.RequestBody(
self.rfile, self.headers, request_params=self.params)
-
+
self.namespaces(self.config)
-
+
self.stage = 'on_start_resource'
self.hooks.run('on_start_resource')
-
+
# Parse the querystring
self.stage = 'process_query_string'
self.process_query_string()
-
+
# Process the body
if self.process_request_body:
if self.method not in self.methods_with_bodies:
@@ -647,14 +661,14 @@ class Request(object):
self.hooks.run('before_request_body')
if self.process_request_body:
self.body.process()
-
+
# Run the handler
self.stage = 'before_handler'
self.hooks.run('before_handler')
if self.handler:
self.stage = 'handler'
response.body = self.handler()
-
+
# Finalize
self.stage = 'before_finalize'
self.hooks.run('before_finalize')
@@ -674,7 +688,7 @@ class Request(object):
if self.throw_errors:
raise
self.handle_error()
-
+
def process_query_string(self):
"""Parse the query string into Python structures. (Core)"""
try:
@@ -685,7 +699,7 @@ class Request(object):
404, "The given query string could not be processed. Query "
"strings for this resource must be encoded with %r." %
self.query_string_encoding)
-
+
# Python 2 only: keyword arguments must be byte strings (type 'str').
if not py3k:
for key, value in p.items():
@@ -693,7 +707,7 @@ class Request(object):
del p[key]
p[key.encode(self.query_string_encoding)] = value
self.params.update(p)
-
+
def process_headers(self):
"""Parse HTTP header data into Python structures. (Core)"""
# Process the headers into self.headers
@@ -703,15 +717,16 @@ class Request(object):
# so title doesn't have to be called twice.
name = name.title()
value = value.strip()
-
- # Warning: if there is more than one header entry for cookies (AFAIK,
- # only Konqueror does that), only the last one will remain in headers
- # (but they will be correctly stored in request.cookie).
+
+ # Warning: if there is more than one header entry for cookies
+ # (AFAIK, only Konqueror does that), only the last one will
+ # remain in headers (but they will be correctly stored in
+ # request.cookie).
if "=?" in value:
dict.__setitem__(headers, name, httputil.decode_TEXT(value))
else:
dict.__setitem__(headers, name, value)
-
+
# Handle cookies differently because on Konqueror, multiple
# cookies come on different lines with the same key
if name == 'Cookie':
@@ -720,7 +735,7 @@ class Request(object):
except CookieError:
msg = "Illegal cookie name %s" % value.split('=')[0]
raise cherrypy.HTTPError(400, msg)
-
+
if not dict.__contains__(headers, 'Host'):
# All Internet-based HTTP/1.1 servers MUST respond with a 400
# (Bad Request) status code to any HTTP/1.1 request message
@@ -732,17 +747,18 @@ class Request(object):
if not host:
host = self.local.name or self.local.ip
self.base = "%s://%s" % (self.scheme, host)
-
+
def get_resource(self, path):
"""Call a dispatcher (which sets self.handler and .config). (Core)"""
# First, see if there is a custom dispatch at this URI. Custom
# dispatchers can only be specified in app.config, not in _cp_config
# (since custom dispatchers may not even have an app.root).
- dispatch = self.app.find_config(path, "request.dispatch", self.dispatch)
-
+ dispatch = self.app.find_config(
+ path, "request.dispatch", self.dispatch)
+
# dispatch() should set self.handler and self.config
dispatch(path)
-
+
def handle_error(self):
"""Handle the last unanticipated exception. (Core)"""
try:
@@ -755,18 +771,18 @@ class Request(object):
inst = sys.exc_info()[1]
inst.set_response()
cherrypy.serving.response.finalize()
-
+
# ------------------------- Properties ------------------------- #
-
+
def _get_body_params(self):
warnings.warn(
- "body_params is deprecated in CherryPy 3.2, will be removed in "
- "CherryPy 3.3.",
- DeprecationWarning
- )
+ "body_params is deprecated in CherryPy 3.2, will be removed in "
+ "CherryPy 3.3.",
+ DeprecationWarning
+ )
return self.body.params
body_params = property(_get_body_params,
- doc= """
+ doc="""
If the request Content-Type is 'application/x-www-form-urlencoded' or
multipart, this will be a dict of the params pulled from the entity
body; that is, it will be the portion of request.params that come
@@ -774,30 +790,31 @@ class Request(object):
can be sent with various HTTP method verbs). This value is set between
the 'before_request_body' and 'before_handler' hooks (assuming that
process_request_body is True).
-
+
Deprecated in 3.2, will be removed for 3.3 in favor of
:attr:`request.body.params<cherrypy._cprequest.RequestBody.params>`.""")
class ResponseBody(object):
+
"""The body of the HTTP response (the response entity)."""
-
+
if py3k:
unicode_err = ("Page handlers MUST return bytes. Use tools.encode "
"if you wish to return unicode.")
-
+
def __get__(self, obj, objclass=None):
if obj is None:
# When calling on the class instead of an instance...
return self
else:
return obj._body
-
+
def __set__(self, obj, value):
# Convert the given value to an iterable object.
if py3k and isinstance(value, str):
raise ValueError(self.unicode_err)
-
+
if isinstance(value, basestring):
# strings get wrapped in a list because iterating over a single
# item list is much faster than iterating over every character
@@ -808,7 +825,7 @@ class ResponseBody(object):
# [''] doesn't evaluate to False, so replace it with [].
value = []
elif py3k and isinstance(value, list):
- # every item in a list must be bytes...
+ # every item in a list must be bytes...
for i, item in enumerate(value):
if isinstance(item, str):
raise ValueError(self.unicode_err)
@@ -822,18 +839,19 @@ class ResponseBody(object):
class Response(object):
+
"""An HTTP Response, including status, headers, and body."""
-
+
status = ""
"""The HTTP Status-Code and Reason-Phrase."""
-
+
header_list = []
"""
A list of the HTTP response headers as (name, value) tuples.
In general, you should use response.headers (a dict) instead. This
attribute is generated from response.headers and is not valid until
after the finalize phase."""
-
+
headers = httputil.HeaderMap()
"""
A dict-like object containing the response headers. Keys are header
@@ -841,36 +859,36 @@ class Response(object):
a case-insensitive manner. That is, headers['Content-Type'] and
headers['content-type'] refer to the same value. Values are header
values (decoded according to :rfc:`2047` if necessary).
-
+
.. seealso:: classes :class:`HeaderMap`, :class:`HeaderElement`
"""
-
+
cookie = SimpleCookie()
"""See help(Cookie)."""
-
+
body = ResponseBody()
"""The body (entity) of the HTTP response."""
-
+
time = None
"""The value of time.time() when created. Use in HTTP dates."""
-
+
timeout = 300
"""Seconds after which the response will be aborted."""
-
+
timed_out = False
"""
Flag to indicate the response should be aborted, because it has
exceeded its timeout."""
-
+
stream = False
"""If False, buffer the response body."""
-
+
def __init__(self):
self.status = None
self.header_list = None
self._body = []
self.time = time.time()
-
+
self.headers = httputil.HeaderMap()
# Since we know all our keys are titled strings, we can
# bypass HeaderMap.update and get a big speed boost.
@@ -880,34 +898,36 @@ class Response(object):
"Date": httputil.HTTPDate(self.time),
})
self.cookie = SimpleCookie()
-
+
def collapse_body(self):
"""Collapse self.body to a single string; replace it and return it."""
if isinstance(self.body, basestring):
return self.body
-
+
newbody = []
for chunk in self.body:
if py3k and not isinstance(chunk, bytes):
- raise TypeError("Chunk %s is not of type 'bytes'." % repr(chunk))
+ raise TypeError("Chunk %s is not of type 'bytes'." %
+ repr(chunk))
newbody.append(chunk)
newbody = ntob('').join(newbody)
-
+
self.body = newbody
return newbody
-
+
def finalize(self):
"""Transform headers (and cookies) into self.header_list. (Core)"""
try:
code, reason, _ = httputil.valid_status(self.status)
except ValueError:
raise cherrypy.HTTPError(500, sys.exc_info()[1].args[0])
-
+
headers = self.headers
-
+
self.status = "%s %s" % (code, reason)
- self.output_status = ntob(str(code), 'ascii') + ntob(" ") + headers.encode(reason)
-
+ self.output_status = ntob(str(code), 'ascii') + \
+ ntob(" ") + headers.encode(reason)
+
if self.stream:
# The upshot: wsgiserver will chunk the response if
# you pop Content-Length (or set it explicitly to None).
@@ -926,10 +946,10 @@ class Response(object):
if dict.get(headers, 'Content-Length') is None:
content = self.collapse_body()
dict.__setitem__(headers, 'Content-Length', len(content))
-
+
# Transform our header dict into a list of tuples.
self.header_list = h = headers.output()
-
+
cookie = self.cookie.output()
if cookie:
for line in cookie.split("\n"):
@@ -942,15 +962,12 @@ class Response(object):
if isinstance(value, unicodestr):
value = headers.encode(value)
h.append((name, value))
-
+
def check_timeout(self):
"""If now > self.time + self.timeout, set self.timed_out.
-
+
This purposefully sets a flag, rather than raising an error,
so that a monitor thread can interrupt the Response thread.
"""
if time.time() > self.time + self.timeout:
self.timed_out = True
-
-
-
diff --git a/cherrypy/_cpserver.py b/cherrypy/_cpserver.py
index 2eecd6ec..1ee3ee2b 100644
--- a/cherrypy/_cpserver.py
+++ b/cherrypy/_cpserver.py
@@ -12,112 +12,121 @@ from cherrypy.process.servers import *
class Server(ServerAdapter):
+
"""An adapter for an HTTP server.
-
+
You can set attributes (like socket_host and socket_port)
on *this* object (which is probably cherrypy.server), and call
quickstart. For example::
-
+
cherrypy.server.socket_port = 80
cherrypy.quickstart()
"""
-
+
socket_port = 8080
"""The TCP port on which to listen for connections."""
-
+
_socket_host = '127.0.0.1'
+
def _get_socket_host(self):
return self._socket_host
+
def _set_socket_host(self, value):
if value == '':
raise ValueError("The empty string ('') is not an allowed value. "
"Use '0.0.0.0' instead to listen on all active "
"interfaces (INADDR_ANY).")
self._socket_host = value
- socket_host = property(_get_socket_host, _set_socket_host,
+ socket_host = property(
+ _get_socket_host,
+ _set_socket_host,
doc="""The hostname or IP address on which to listen for connections.
-
+
Host values may be any IPv4 or IPv6 address, or any valid hostname.
The string 'localhost' is a synonym for '127.0.0.1' (or '::1', if
your hosts file prefers IPv6). The string '0.0.0.0' is a special
IPv4 entry meaning "any active interface" (INADDR_ANY), and '::'
is the similar IN6ADDR_ANY for IPv6. The empty string or None are
not allowed.""")
-
+
socket_file = None
"""If given, the name of the UNIX socket to use instead of TCP/IP.
-
+
When this option is not None, the `socket_host` and `socket_port` options
are ignored."""
-
+
socket_queue_size = 5
"""The 'backlog' argument to socket.listen(); specifies the maximum number
of queued connections (default 5)."""
-
+
socket_timeout = 10
"""The timeout in seconds for accepted connections (default 10)."""
-
+
shutdown_timeout = 5
"""The time to wait for HTTP worker threads to clean up."""
-
+
protocol_version = 'HTTP/1.1'
"""The version string to write in the Status-Line of all HTTP responses,
for example, "HTTP/1.1" (the default). Depending on the HTTP server used,
this should also limit the supported features used in the response."""
-
+
thread_pool = 10
"""The number of worker threads to start up in the pool."""
-
+
thread_pool_max = -1
- """The maximum size of the worker-thread pool. Use -1 to indicate no limit."""
-
+ """The maximum size of the worker-thread pool. Use -1 to indicate no limit.
+ """
+
max_request_header_size = 500 * 1024
- """The maximum number of bytes allowable in the request headers. If exceeded,
- the HTTP server should return "413 Request Entity Too Large"."""
-
+ """The maximum number of bytes allowable in the request headers.
+ If exceeded, the HTTP server should return "413 Request Entity Too Large".
+ """
+
max_request_body_size = 100 * 1024 * 1024
"""The maximum number of bytes allowable in the request body. If exceeded,
the HTTP server should return "413 Request Entity Too Large"."""
-
+
instance = None
"""If not None, this should be an HTTP server instance (such as
CPWSGIServer) which cherrypy.server will control. Use this when you need
more control over object instantiation than is available in the various
configuration options."""
-
+
ssl_context = None
"""When using PyOpenSSL, an instance of SSL.Context."""
-
+
ssl_certificate = None
"""The filename of the SSL certificate to use."""
-
+
ssl_certificate_chain = None
"""When using PyOpenSSL, the certificate chain to pass to
Context.load_verify_locations."""
-
+
ssl_private_key = None
"""The filename of the private key to use with SSL."""
-
+
if py3k:
ssl_module = 'builtin'
- """The name of a registered SSL adaptation module to use with the builtin
- WSGI server. Builtin options are: 'builtin' (to use the SSL library built
- into recent versions of Python). You may also register your
- own classes in the wsgiserver.ssl_adapters dict."""
+ """The name of a registered SSL adaptation module to use with
+ the builtin WSGI server. Builtin options are: 'builtin' (to
+ use the SSL library built into recent versions of Python).
+ You may also register your own classes in the
+ wsgiserver.ssl_adapters dict."""
else:
ssl_module = 'pyopenssl'
- """The name of a registered SSL adaptation module to use with the builtin
- WSGI server. Builtin options are 'builtin' (to use the SSL library built
- into recent versions of Python) and 'pyopenssl' (to use the PyOpenSSL
- project, which you must install separately). You may also register your
- own classes in the wsgiserver.ssl_adapters dict."""
-
+ """The name of a registered SSL adaptation module to use with the
+ builtin WSGI server. Builtin options are 'builtin' (to use the SSL
+ library built into recent versions of Python) and 'pyopenssl' (to
+ use the PyOpenSSL project, which you must install separately). You
+ may also register your own classes in the wsgiserver.ssl_adapters
+ dict."""
+
statistics = False
"""Turns statistics-gathering on or off for aware HTTP servers."""
-
+
nodelay = True
"""If True (the default since 3.1), sets the TCP_NODELAY socket option."""
-
+
wsgi_version = (1, 0)
"""The WSGI version tuple to use with the builtin WSGI server.
The provided options are (1, 0) [which includes support for PEP 3333,
@@ -125,13 +134,13 @@ class Server(ServerAdapter):
wsgi.version (1, 0)] and ('u', 0), an experimental unicode version.
You may create and register your own experimental versions of the WSGI
protocol by adding custom classes to the wsgiserver.wsgi_gateways dict."""
-
+
def __init__(self):
self.bus = cherrypy.engine
self.httpserver = None
self.interrupt = None
self.running = False
-
+
def httpserver_from_self(self, httpserver=None):
"""Return a (httpserver, bind_addr) pair based on self attributes."""
if httpserver is None:
@@ -143,20 +152,21 @@ class Server(ServerAdapter):
# Is anyone using this? Can I add an arg?
httpserver = attributes(httpserver)(self)
return httpserver, self.bind_addr
-
+
def start(self):
"""Start the HTTP server."""
if not self.httpserver:
self.httpserver, self.bind_addr = self.httpserver_from_self()
ServerAdapter.start(self)
start.priority = 75
-
+
def _get_bind_addr(self):
if self.socket_file:
return self.socket_file
if self.socket_host is None and self.socket_port is None:
return None
return (self.socket_host, self.socket_port)
+
def _set_bind_addr(self, value):
if value is None:
self.socket_file = None
@@ -174,14 +184,18 @@ class Server(ServerAdapter):
raise ValueError("bind_addr must be a (host, port) tuple "
"(for TCP sockets) or a string (for Unix "
"domain sockets), not %r" % value)
- bind_addr = property(_get_bind_addr, _set_bind_addr,
- doc='A (host, port) tuple for TCP sockets or a str for Unix domain sockets.')
-
+ bind_addr = property(
+ _get_bind_addr,
+ _set_bind_addr,
+ doc='A (host, port) tuple for TCP sockets or '
+ 'a str for Unix domain sockets.')
+
def base(self):
- """Return the base (scheme://host[:port] or sock file) for this server."""
+ """Return the base (scheme://host[:port] or sock file) for this server.
+ """
if self.socket_file:
return self.socket_file
-
+
host = self.socket_host
if host in ('0.0.0.0', '::'):
# 0.0.0.0 is INADDR_ANY and :: is IN6ADDR_ANY.
@@ -189,9 +203,9 @@ class Server(ServerAdapter):
# safest thing to spit out in a URL.
import socket
host = socket.gethostname()
-
+
port = self.socket_port
-
+
if self.ssl_certificate:
scheme = "https"
if port != 443:
@@ -200,6 +214,5 @@ class Server(ServerAdapter):
scheme = "http"
if port != 80:
host += ":%s" % port
-
- return "%s://%s" % (scheme, host)
+ return "%s://%s" % (scheme, host)
diff --git a/cherrypy/_cpthreadinglocal.py b/cherrypy/_cpthreadinglocal.py
index 34c17ac4..238c3224 100644
--- a/cherrypy/_cpthreadinglocal.py
+++ b/cherrypy/_cpthreadinglocal.py
@@ -137,6 +137,7 @@ affects what we see:
# Threading import is at end
+
class _localbase(object):
__slots__ = '_local__key', '_local__args', '_local__lock'
@@ -158,6 +159,7 @@ class _localbase(object):
return self
+
def _patch(self):
key = object.__getattribute__(self, '_local__key')
d = currentThread().__dict__.get(key)
@@ -175,6 +177,7 @@ def _patch(self):
else:
object.__setattr__(self, '__dict__', d)
+
class local(_localbase):
def __getattribute__(self, name):
@@ -204,7 +207,6 @@ class local(_localbase):
finally:
lock.release()
-
def __del__():
threading_enumerate = enumerate
__getattribute__ = object.__getattribute__
@@ -231,7 +233,7 @@ class local(_localbase):
try:
del __dict__[key]
except KeyError:
- pass # didn't have anything in this thread
+ pass # didn't have anything in this thread
return __del__
__del__ = __del__()
diff --git a/cherrypy/_cptools.py b/cherrypy/_cptools.py
index 22316b31..ce574492 100644
--- a/cherrypy/_cptools.py
+++ b/cherrypy/_cptools.py
@@ -2,18 +2,18 @@
Tools are usually designed to be used in a variety of ways (although some
may only offer one if they choose):
-
+
Library calls
All tools are callables that can be used wherever needed.
The arguments are straightforward and should be detailed within the
docstring.
-
+
Function decorators
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.
-
+
CherryPy config
If a tool exposes a "_setup" callable, it will be called
once per Request (if the feature is "turned on" via config).
@@ -43,17 +43,21 @@ def _getargs(func):
return co.co_varnames[:co.co_argcount]
-_attr_error = ("CherryPy Tools cannot be turned on directly. Instead, turn them "
- "on via config, or use them as decorators on your page handlers.")
+_attr_error = (
+ "CherryPy Tools cannot be turned on directly. Instead, turn them "
+ "on via config, or use them as decorators on your page handlers."
+)
+
class Tool(object):
+
"""A registered function for use with CherryPy request-processing hooks.
-
+
help(tool.callable) should give you more information about this Tool.
"""
-
+
namespace = "tools"
-
+
def __init__(self, point, callable, name=None, priority=50):
self._point = point
self.callable = callable
@@ -61,13 +65,14 @@ class Tool(object):
self._priority = priority
self.__doc__ = self.callable.__doc__
self._setargs()
-
+
def _get_on(self):
raise AttributeError(_attr_error)
+
def _set_on(self, value):
raise AttributeError(_attr_error)
on = property(_get_on, _set_on)
-
+
def _setargs(self):
"""Copy func parameter names to obj attributes."""
try:
@@ -86,28 +91,28 @@ class Tool(object):
# but if we trap it here it doesn't prevent CP from working.
except IndexError:
pass
-
+
def _merged_args(self, d=None):
"""Return a dict of configuration entries for this Tool."""
if d:
conf = d.copy()
else:
conf = {}
-
+
tm = cherrypy.serving.request.toolmaps[self.namespace]
if self._name in tm:
conf.update(tm[self._name])
-
+
if "on" in conf:
del conf["on"]
-
+
return conf
-
+
def __call__(self, *args, **kwargs):
"""Compile-time decorator (turn on the tool in config).
-
+
For example::
-
+
@tools.proxy()
def whats_my_base(self):
return cherrypy.request.base
@@ -117,6 +122,7 @@ class Tool(object):
raise TypeError("The %r Tool does not accept positional "
"arguments; you must use keyword arguments."
% self._name)
+
def tool_decorator(f):
if not hasattr(f, "_cp_config"):
f._cp_config = {}
@@ -126,10 +132,10 @@ class Tool(object):
f._cp_config[subspace + k] = v
return f
return tool_decorator
-
+
def _setup(self):
"""Hook this tool into cherrypy.request.
-
+
The standard CherryPy request object will automatically call this
method when the tool is "turned on" in config.
"""
@@ -142,8 +148,9 @@ class Tool(object):
class HandlerTool(Tool):
+
"""Tool which is called 'before main', that may skip normal handlers.
-
+
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
@@ -151,15 +158,15 @@ class HandlerTool(Tool):
tool is declared AS a page handler (see the 'handler' method), returning
False will raise NotFound.
"""
-
+
def __init__(self, callable, name=None):
Tool.__init__(self, 'before_handler', callable, name)
-
+
def handler(self, *args, **kwargs):
"""Use this tool as a CherryPy page handler.
-
+
For example::
-
+
class Root:
nav = tools.staticdir.handler(section="/nav", dir="nav",
root=absDir)
@@ -171,14 +178,14 @@ class HandlerTool(Tool):
return cherrypy.serving.response.body
handle_func.exposed = True
return handle_func
-
+
def _wrapper(self, **kwargs):
if self.callable(**kwargs):
cherrypy.serving.request.handler = None
-
+
def _setup(self):
"""Hook this tool into cherrypy.request.
-
+
The standard CherryPy request object will automatically call this
method when the tool is "turned on" in config.
"""
@@ -191,16 +198,17 @@ class HandlerTool(Tool):
class HandlerWrapperTool(Tool):
+
"""Tool which wraps request.handler in a provided wrapper function.
-
+
The 'newhandler' arg must be a handler wrapper function that takes a
'next_handler' argument, plus ``*args`` and ``**kwargs``. Like all
page handler
functions, it must return an iterable for use as cherrypy.response.body.
-
+
For example, to allow your 'inner' page handlers to return dicts
which then get interpolated into a template::
-
+
def interpolator(next_handler, *args, **kwargs):
filename = cherrypy.request.config.get('template')
cherrypy.response.template = env.get_template(filename)
@@ -208,32 +216,35 @@ class HandlerWrapperTool(Tool):
return cherrypy.response.template.render(**response_dict)
cherrypy.tools.jinja = HandlerWrapperTool(interpolator)
"""
-
- def __init__(self, newhandler, point='before_handler', name=None, priority=50):
+
+ def __init__(self, newhandler, point='before_handler', name=None,
+ priority=50):
self.newhandler = newhandler
self._point = point
self._name = name
self._priority = priority
-
+
def callable(self, debug=False):
innerfunc = cherrypy.serving.request.handler
+
def wrap(*args, **kwargs):
return self.newhandler(innerfunc, *args, **kwargs)
cherrypy.serving.request.handler = wrap
class ErrorTool(Tool):
+
"""Tool which is used to replace the default request.error_response."""
-
+
def __init__(self, callable, name=None):
Tool.__init__(self, None, callable, name)
-
+
def _wrapper(self):
self.callable(**self._merged_args())
-
+
def _setup(self):
"""Hook this tool into cherrypy.request.
-
+
The standard CherryPy request object will automatically call this
method when the tool is "turned on" in config.
"""
@@ -249,45 +260,47 @@ from cherrypy.lib import auth_basic, auth_digest
class SessionTool(Tool):
+
"""Session Tool for CherryPy.
-
+
sessions.locking
When 'implicit' (the default), the session will be locked for you,
just before running the page handler.
-
+
When 'early', the session will be locked before reading the request
body. This is off by default for safety reasons; for example,
a large upload would block the session, denying an AJAX
- progress meter (see http://www.cherrypy.org/ticket/630).
-
+ progress meter
+ (`issue <https://bitbucket.org/cherrypy/cherrypy/issue/630>`_).
+
When 'explicit' (or any other value), you need to call
cherrypy.session.acquire_lock() yourself before using
session data.
"""
-
+
def __init__(self):
# _sessions.init must be bound after headers are read
Tool.__init__(self, 'before_request_body', _sessions.init)
-
+
def _lock_session(self):
cherrypy.serving.session.acquire_lock()
-
+
def _setup(self):
"""Hook this tool into cherrypy.request.
-
+
The standard CherryPy request object will automatically call this
method when the tool is "turned on" in config.
"""
hooks = cherrypy.serving.request.hooks
-
+
conf = self._merged_args()
-
+
p = conf.pop("priority", None)
if p is None:
p = getattr(self.callable, "priority", self._priority)
-
+
hooks.attach(self._point, self.callable, priority=p, **conf)
-
+
locking = conf.pop('locking', 'implicit')
if locking == 'implicit':
hooks.attach('before_handler', self._lock_session)
@@ -298,15 +311,15 @@ class SessionTool(Tool):
else:
# Don't lock
pass
-
+
hooks.attach('before_finalize', _sessions.save)
hooks.attach('on_end_request', _sessions.close)
-
+
def regenerate(self):
"""Drop the current session and make a new one (with a new id)."""
sess = cherrypy.serving.session
sess.regenerate()
-
+
# 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',
@@ -314,19 +327,18 @@ class SessionTool(Tool):
_sessions.set_response_cookie(**conf)
-
-
class XMLRPCController(object):
+
"""A Controller (page handler collection) for XML-RPC.
-
+
To use it, have your controllers subclass this base class (it will
turn on the tool for you).
-
+
You can also supply the following optional config entries::
-
+
tools.xmlrpc.encoding: 'utf-8'
tools.xmlrpc.allow_none: 0
-
+
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
@@ -334,42 +346,42 @@ class XMLRPCController(object):
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.
-
+
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.
-
+
The XMLRPCDispatcher strips any /RPC2 prefix; if you aren't using /RPC2
in your URL's, you can safely skip turning on the XMLRPCDispatcher.
Otherwise, you need to use declare it in config::
-
+
request.dispatch: cherrypy.dispatch.XMLRPCDispatcher()
"""
-
+
# 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}
-
+
def default(self, *vpath, **params):
rpcparams, rpcmethod = _xmlrpc.process_body()
-
+
subhandler = self
for attr in str(rpcmethod).split('.'):
subhandler = getattr(subhandler, attr, None)
-
+
if subhandler and getattr(subhandler, "exposed", False):
body = subhandler(*(vpath + rpcparams), **params)
-
+
else:
- # http://www.cherrypy.org/ticket/533
+ # https://bitbucket.org/cherrypy/cherrypy/issue/533
# if a method is not found, an xmlrpclib.Fault should be returned
# raising an exception here will do that; see
# cherrypy.lib.xmlrpcutil.on_error
raise Exception('method "%s" is not supported' % attr)
-
+
conf = cherrypy.serving.request.toolmaps['tools'].get("xmlrpc", {})
_xmlrpc.respond(body,
conf.get('encoding', 'utf-8'),
@@ -379,7 +391,7 @@ class XMLRPCController(object):
class SessionAuthTool(HandlerTool):
-
+
def _setargs(self):
for name in dir(cptools.SessionAuth):
if not name.startswith("__"):
@@ -387,8 +399,9 @@ class SessionAuthTool(HandlerTool):
class CachingTool(Tool):
+
"""Caching Tool for CherryPy."""
-
+
def _wrapper(self, **kwargs):
request = cherrypy.serving.request
if _caching.get(**kwargs):
@@ -397,29 +410,29 @@ class CachingTool(Tool):
if request.cacheable:
# Note the devious technique here of adding hooks on the fly
request.hooks.attach('before_finalize', _caching.tee_output,
- priority = 90)
+ priority=90)
_wrapper.priority = 20
-
+
def _setup(self):
"""Hook caching into cherrypy.request."""
conf = self._merged_args()
-
+
p = conf.pop("priority", None)
cherrypy.serving.request.hooks.attach('before_handler', self._wrapper,
priority=p, **conf)
-
class Toolbox(object):
+
"""A collection of Tools.
-
+
This object also functions as a config namespace handler for itself.
Custom toolboxes should be added to each Application's toolboxes dict.
"""
-
+
def __init__(self, namespace):
self.namespace = namespace
-
+
def __setattr__(self, name, value):
# If the Tool._name is None, supply it from the attribute name.
if isinstance(value, Tool):
@@ -427,16 +440,17 @@ class Toolbox(object):
value._name = name
value.namespace = self.namespace
object.__setattr__(self, name, value)
-
+
def __enter__(self):
"""Populate request.toolmaps from tools specified in config."""
cherrypy.serving.request.toolmaps[self.namespace] = map = {}
+
def populate(k, v):
toolname, arg = k.split(".", 1)
bucket = map.setdefault(toolname, {})
bucket[arg] = v
return populate
-
+
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)
@@ -448,21 +462,22 @@ class Toolbox(object):
class DeprecatedTool(Tool):
-
+
_name = None
warnmsg = "This Tool is deprecated."
-
+
def __init__(self, point, warnmsg=None):
self.point = point
if warnmsg is not None:
self.warnmsg = warnmsg
-
+
def __call__(self, *args, **kwargs):
warnings.warn(self.warnmsg)
+
def tool_decorator(f):
return f
return tool_decorator
-
+
def _setup(self):
warnings.warn(self.warnmsg)
@@ -487,12 +502,16 @@ _d.sessions = SessionTool()
_d.xmlrpc = ErrorTool(_xmlrpc.on_error)
_d.caching = CachingTool('before_handler', _caching.get, 'caching')
_d.expires = Tool('before_finalize', _caching.expires)
-_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.")
+_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.")
_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)
diff --git a/cherrypy/_cptree.py b/cherrypy/_cptree.py
index 3aa4b9e1..32e79b02 100644
--- a/cherrypy/_cptree.py
+++ b/cherrypy/_cptree.py
@@ -10,101 +10,106 @@ from cherrypy.lib import httputil
class Application(object):
+
"""A CherryPy Application.
-
+
Servers and gateways should not instantiate Request objects directly.
Instead, they should ask an Application object for a request object.
-
+
An instance of this class may also be used as a WSGI callable
(WSGI application object) for itself.
"""
-
+
root = None
"""The top-most container of page handlers for this app. Handlers should
be arranged in a hierarchy of attributes, matching the expected URI
hierarchy; the default dispatcher then searches this hierarchy for a
matching handler. When using a dispatcher other than the default,
this value may be None."""
-
+
config = {}
"""A dict of {path: pathconf} pairs, where 'pathconf' is itself a dict
of {key: value} pairs."""
-
+
namespaces = _cpconfig.NamespaceSet()
toolboxes = {'tools': cherrypy.tools}
-
+
log = None
"""A LogManager instance. See _cplogging."""
-
+
wsgiapp = None
"""A CPWSGIApp instance. See _cpwsgi."""
-
+
request_class = _cprequest.Request
response_class = _cprequest.Response
-
+
relative_urls = False
-
+
def __init__(self, root, script_name="", config=None):
self.log = _cplogging.LogManager(id(self), cherrypy.log.logger_root)
self.root = root
self.script_name = script_name
self.wsgiapp = _cpwsgi.CPWSGIApp(self)
-
+
self.namespaces = self.namespaces.copy()
self.namespaces["log"] = lambda k, v: setattr(self.log, k, v)
self.namespaces["wsgi"] = self.wsgiapp.namespace_handler
-
+
self.config = self.__class__.config.copy()
if config:
self.merge(config)
-
+
def __repr__(self):
return "%s.%s(%r, %r)" % (self.__module__, self.__class__.__name__,
self.root, self.script_name)
-
- script_name_doc = """The URI "mount point" for this app. A mount point is that portion of
- the URI which is constant for all URIs that are serviced by this
- application; it does not include scheme, host, or proxy ("virtual host")
- portions of the URI.
-
+
+ script_name_doc = """The URI "mount point" for this app. A mount point
+ is that portion of the URI which is constant for all URIs that are
+ serviced by this application; it does not include scheme, host, or proxy
+ ("virtual host") portions of the URI.
+
For example, if script_name is "/my/cool/app", then the URL
"http://www.example.com/my/cool/app/page1" might be handled by a
"page1" method on the root object.
-
+
The value of script_name MUST NOT end in a slash. If the script_name
refers to the root of the URI, it MUST be an empty string (not "/").
-
+
If script_name is explicitly set to None, then the script_name will be
provided for each call from request.wsgi_environ['SCRIPT_NAME'].
"""
+
def _get_script_name(self):
- if self._script_name is None:
- # None signals that the script name should be pulled from WSGI environ.
- return cherrypy.serving.request.wsgi_environ['SCRIPT_NAME'].rstrip("/")
- return self._script_name
+ if self._script_name is not None:
+ return self._script_name
+
+ # A `_script_name` with a value of None signals that the script name
+ # should be pulled from WSGI environ.
+ return cherrypy.serving.request.wsgi_environ['SCRIPT_NAME'].rstrip("/")
+
def _set_script_name(self, value):
if value:
value = value.rstrip("/")
self._script_name = value
script_name = property(fget=_get_script_name, fset=_set_script_name,
doc=script_name_doc)
-
+
def merge(self, config):
"""Merge the given config into self.config."""
_cpconfig.merge(self.config, config)
-
+
# Handle namespaces specified in config.
self.namespaces(self.config.get("/", {}))
-
+
def find_config(self, path, key, default=None):
"""Return the most-specific value for key along path, or default."""
trail = path or "/"
while trail:
nodeconf = self.config.get(trail, {})
-
+
if key in nodeconf:
return nodeconf[key]
-
+
lastslash = trail.rfind("/")
if lastslash == -1:
break
@@ -112,78 +117,79 @@ class Application(object):
trail = "/"
else:
trail = trail[:lastslash]
-
+
return default
-
+
def get_serving(self, local, remote, scheme, sproto):
"""Create and return a Request and Response object."""
req = self.request_class(local, remote, scheme, sproto)
req.app = self
-
+
for name, toolbox in self.toolboxes.items():
req.namespaces[name] = toolbox
-
+
resp = self.response_class()
cherrypy.serving.load(req, resp)
cherrypy.engine.publish('acquire_thread')
cherrypy.engine.publish('before_request')
-
+
return req, resp
-
+
def release_serving(self):
"""Release the current serving (request and response)."""
req = cherrypy.serving.request
-
+
cherrypy.engine.publish('after_request')
-
+
try:
req.close()
except:
cherrypy.log(traceback=True, severity=40)
-
+
cherrypy.serving.clear()
-
+
def __call__(self, environ, start_response):
return self.wsgiapp(environ, start_response)
class Tree(object):
+
"""A registry of CherryPy applications, mounted at diverse points.
-
+
An instance of this class may also be used as a WSGI callable
(WSGI application object), in which case it dispatches to all
mounted apps.
"""
-
+
apps = {}
"""
A dict of the form {script name: application}, where "script name"
is a string declaring the URI mount point (no trailing slash), and
"application" is an instance of cherrypy.Application (or an arbitrary
WSGI callable if you happen to be using a WSGI server)."""
-
+
def __init__(self):
self.apps = {}
-
+
def mount(self, root, script_name="", config=None):
"""Mount a new app from a root object, script_name, and config.
-
+
root
An instance of a "controller class" (a collection of page
handler methods) which represents the root of the application.
This may also be an Application instance, or None if using
a dispatcher other than the default.
-
+
script_name
A string containing the "mount point" of the application.
This should start with a slash, and be the path portion of the
URL at which to mount the given root. For example, if root.index()
will handle requests to "http://www.example.com:8080/dept/app1/",
then the script_name argument would be "/dept/app1".
-
+
It MUST NOT end in a slash. If the script_name refers to the
root of the URI, it MUST be an empty string (not "/").
-
+
config
A file or dict containing application config.
"""
@@ -194,42 +200,43 @@ class Tree(object):
"order to inpect the WSGI environ for SCRIPT_NAME upon each "
"request). You cannot mount such Applications on this Tree; "
"you must pass them to a WSGI server interface directly.")
-
+
# Next line both 1) strips trailing slash and 2) maps "/" -> "".
script_name = script_name.rstrip("/")
-
+
if isinstance(root, Application):
app = root
if script_name != "" and script_name != app.script_name:
- raise ValueError("Cannot specify a different script name and "
- "pass an Application instance to cherrypy.mount")
+ raise ValueError(
+ "Cannot specify a different script name and pass an "
+ "Application instance to cherrypy.mount")
script_name = app.script_name
else:
app = Application(root, script_name)
-
+
# If mounted at "", add favicon.ico
if (script_name == "" and root is not None
and not hasattr(root, "favicon_ico")):
favicon = os.path.join(os.getcwd(), os.path.dirname(__file__),
"favicon.ico")
root.favicon_ico = tools.staticfile.handler(favicon)
-
+
if config:
app.merge(config)
-
+
self.apps[script_name] = app
-
+
return app
-
+
def graft(self, wsgi_callable, script_name=""):
"""Mount a wsgi callable at the given script_name."""
# Next line both 1) strips trailing slash and 2) maps "/" -> "".
script_name = script_name.rstrip("/")
self.apps[script_name] = wsgi_callable
-
+
def script_name(self, path=None):
"""The script_name of the app at the given path, or None.
-
+
If path is None, cherrypy.request is used.
"""
if path is None:
@@ -239,17 +246,17 @@ class Tree(object):
request.path_info)
except AttributeError:
return None
-
+
while True:
if path in self.apps:
return path
-
+
if path == "":
return None
-
+
# Move one node up the tree and try again.
path = path[:path.rfind("/")]
-
+
def __call__(self, environ, start_response):
# If you're calling this, then you're probably setting SCRIPT_NAME
# to '' (some WSGI servers always set SCRIPT_NAME to '').
@@ -263,9 +270,9 @@ class Tree(object):
if sn is None:
start_response('404 Not Found', [])
return []
-
+
app = self.apps[sn]
-
+
# Correct the SCRIPT_NAME and PATH_INFO environ entries.
environ = environ.copy()
if not py3k:
@@ -273,7 +280,8 @@ class Tree(object):
# Python 2/WSGI u.0: all strings MUST be of type unicode
enc = environ[ntou('wsgi.url_encoding')]
environ[ntou('SCRIPT_NAME')] = sn.decode(enc)
- environ[ntou('PATH_INFO')] = path[len(sn.rstrip("/")):].decode(enc)
+ environ[ntou('PATH_INFO')] = path[
+ len(sn.rstrip("/")):].decode(enc)
else:
# Python 2/WSGI 1.x: all strings MUST be of type str
environ['SCRIPT_NAME'] = sn
@@ -285,6 +293,8 @@ class Tree(object):
environ['PATH_INFO'] = path[len(sn.rstrip("/")):]
else:
# Python 3/WSGI 1.x: all strings MUST be ISO-8859-1 str
- environ['SCRIPT_NAME'] = sn.encode('utf-8').decode('ISO-8859-1')
- environ['PATH_INFO'] = path[len(sn.rstrip("/")):].encode('utf-8').decode('ISO-8859-1')
+ environ['SCRIPT_NAME'] = sn.encode(
+ 'utf-8').decode('ISO-8859-1')
+ environ['PATH_INFO'] = path[
+ len(sn.rstrip("/")):].encode('utf-8').decode('ISO-8859-1')
return app(environ, start_response)
diff --git a/cherrypy/_cpwsgi.py b/cherrypy/_cpwsgi.py
index 91cd044e..4d1fd704 100644
--- a/cherrypy/_cpwsgi.py
+++ b/cherrypy/_cpwsgi.py
@@ -16,9 +16,10 @@ from cherrypy.lib import httputil
def downgrade_wsgi_ux_to_1x(environ):
- """Return a new environ dict for WSGI 1.x from the given WSGI u.x environ."""
+ """Return a new environ dict for WSGI 1.x from the given WSGI u.x environ.
+ """
env1x = {}
-
+
url_encoding = environ[ntou('wsgi.url_encoding')]
for k, v in list(environ.items()):
if k in [ntou('PATH_INFO'), ntou('SCRIPT_NAME'), ntou('QUERY_STRING')]:
@@ -26,36 +27,37 @@ def downgrade_wsgi_ux_to_1x(environ):
elif isinstance(v, unicodestr):
v = v.encode('ISO-8859-1')
env1x[k.encode('ISO-8859-1')] = v
-
+
return env1x
class VirtualHost(object):
+
"""Select a different WSGI application based on the Host header.
-
+
This can be useful when running multiple sites within one CP server.
It allows several domains to point to different applications. For example::
-
+
root = Root()
RootApp = cherrypy.Application(root)
Domain2App = cherrypy.Application(root)
SecureApp = cherrypy.Application(Secure())
-
+
vhost = cherrypy._cpwsgi.VirtualHost(RootApp,
domains={'www.domain2.example': Domain2App,
'www.domain2.example:443': SecureApp,
})
-
+
cherrypy.tree.graft(vhost)
"""
default = None
"""Required. The default WSGI application."""
-
+
use_x_forwarded_host = True
"""If True (the default), any "X-Forwarded-Host"
request header will be used instead of the "Host" header. This
is commonly added by HTTP servers (such as Apache) when proxying."""
-
+
domains = {}
"""A dict of {host header value: application} pairs.
The incoming "Host" request header is looked up in this dict,
@@ -64,17 +66,17 @@ class VirtualHost(object):
separate entries for "example.com" and "www.example.com".
In addition, "Host" headers may contain the port number.
"""
-
+
def __init__(self, default, domains=None, use_x_forwarded_host=True):
self.default = default
self.domains = domains or {}
self.use_x_forwarded_host = use_x_forwarded_host
-
+
def __call__(self, environ, start_response):
domain = environ.get('HTTP_HOST', '')
if self.use_x_forwarded_host:
domain = environ.get("HTTP_X_FORWARDED_HOST", domain)
-
+
nextapp = self.domains.get(domain)
if nextapp is None:
nextapp = self.default
@@ -82,12 +84,13 @@ class VirtualHost(object):
class InternalRedirector(object):
+
"""WSGI middleware that handles raised cherrypy.InternalRedirect."""
-
+
def __init__(self, nextapp, recursive=False):
self.nextapp = nextapp
self.recursive = recursive
-
+
def __call__(self, environ, start_response):
redirections = []
while True:
@@ -99,15 +102,16 @@ class InternalRedirector(object):
sn = environ.get('SCRIPT_NAME', '')
path = environ.get('PATH_INFO', '')
qs = environ.get('QUERY_STRING', '')
-
+
# Add the *previous* path_info + qs to redirections.
old_uri = sn + path
if qs:
old_uri += "?" + qs
redirections.append(old_uri)
-
+
if not self.recursive:
- # Check to see if the new URI has been redirected to already
+ # Check to see if the new URI has been redirected to
+ # already
new_uri = sn + ir.path
if ir.query_string:
new_uri += "?" + ir.query_string
@@ -115,7 +119,7 @@ class InternalRedirector(object):
ir.request.close()
raise RuntimeError("InternalRedirector visited the "
"same URL twice: %r" % new_uri)
-
+
# Munge the environment and try again.
environ['REQUEST_METHOD'] = "GET"
environ['PATH_INFO'] = ir.path
@@ -126,44 +130,51 @@ class InternalRedirector(object):
class ExceptionTrapper(object):
+
"""WSGI middleware that traps exceptions."""
-
+
def __init__(self, nextapp, throws=(KeyboardInterrupt, SystemExit)):
self.nextapp = nextapp
self.throws = throws
-
+
def __call__(self, environ, start_response):
- return _TrappedResponse(self.nextapp, environ, start_response, self.throws)
+ return _TrappedResponse(
+ self.nextapp,
+ environ,
+ start_response,
+ self.throws
+ )
class _TrappedResponse(object):
-
+
response = iter([])
-
+
def __init__(self, nextapp, environ, start_response, throws):
self.nextapp = nextapp
self.environ = environ
self.start_response = start_response
self.throws = throws
self.started_response = False
- self.response = self.trap(self.nextapp, self.environ, self.start_response)
+ self.response = self.trap(
+ self.nextapp, self.environ, self.start_response)
self.iter_response = iter(self.response)
-
+
def __iter__(self):
self.started_response = True
return self
-
+
if py3k:
def __next__(self):
return self.trap(next, self.iter_response)
else:
def next(self):
return self.trap(self.iter_response.next)
-
+
def close(self):
if hasattr(self.response, 'close'):
self.response.close()
-
+
def trap(self, func, *args, **kwargs):
try:
return func(*args, **kwargs)
@@ -188,7 +199,7 @@ class _TrappedResponse(object):
self.iter_response = iter([])
else:
self.iter_response = iter(b)
-
+
try:
self.start_response(s, h, _sys.exc_info())
except:
@@ -199,7 +210,7 @@ class _TrappedResponse(object):
# But we still log and call close() to clean up ourselves.
_cherrypy.log(traceback=True, severity=40)
raise
-
+
if self.started_response:
return ntob("").join(b)
else:
@@ -210,8 +221,9 @@ class _TrappedResponse(object):
class AppResponse(object):
+
"""WSGI response iterable for CherryPy applications."""
-
+
def __init__(self, environ, start_response, cpapp):
self.cpapp = cpapp
try:
@@ -226,20 +238,24 @@ class AppResponse(object):
outstatus = r.output_status
if not isinstance(outstatus, bytestr):
raise TypeError("response.output_status is not a byte string.")
-
+
outheaders = []
for k, v in r.header_list:
if not isinstance(k, bytestr):
- raise TypeError("response.header_list key %r is not a byte string." % k)
+ raise TypeError(
+ "response.header_list key %r is not a byte string." %
+ k)
if not isinstance(v, bytestr):
- raise TypeError("response.header_list value %r is not a byte string." % v)
+ raise TypeError(
+ "response.header_list value %r is not a byte string." %
+ v)
outheaders.append((k, v))
-
+
if py3k:
- # According to PEP 3333, when using Python 3, the response status
- # and headers must be bytes masquerading as unicode; that is, they
- # must be of type "str" but are restricted to code points in the
- # "latin-1" set.
+ # According to PEP 3333, when using Python 3, the response
+ # status and headers must be bytes masquerading as unicode;
+ # that is, they must be of type "str" but are restricted to
+ # code points in the "latin-1" set.
outstatus = outstatus.decode('ISO-8859-1')
outheaders = [(k.decode('ISO-8859-1'), v.decode('ISO-8859-1'))
for k, v in outheaders]
@@ -249,34 +265,34 @@ class AppResponse(object):
except:
self.close()
raise
-
+
def __iter__(self):
return self
-
+
if py3k:
def __next__(self):
return next(self.iter_response)
else:
def next(self):
return self.iter_response.next()
-
+
def close(self):
"""Close and de-reference the current request and response. (Core)"""
self.cpapp.release_serving()
-
+
def run(self):
"""Create a Request object using environ."""
env = self.environ.get
-
+
local = httputil.Host('', int(env('SERVER_PORT', 80)),
- env('SERVER_NAME', ''))
+ env('SERVER_NAME', ''))
remote = httputil.Host(env('REMOTE_ADDR', ''),
int(env('REMOTE_PORT', -1) or -1),
env('REMOTE_HOST', ''))
scheme = env('wsgi.url_scheme')
sproto = env('ACTUAL_SERVER_PROTOCOL', "HTTP/1.1")
request, resp = self.cpapp.get_serving(local, remote, scheme, sproto)
-
+
# LOGON_USER is served by IIS, and is the name of the
# user after having been mapped to a local account.
# Both IIS and Apache set REMOTE_USER, when possible.
@@ -285,24 +301,25 @@ class AppResponse(object):
request.multiprocess = self.environ['wsgi.multiprocess']
request.wsgi_environ = self.environ
request.prev = env('cherrypy.previous_request', None)
-
+
meth = self.environ['REQUEST_METHOD']
-
+
path = httputil.urljoin(self.environ.get('SCRIPT_NAME', ''),
self.environ.get('PATH_INFO', ''))
qs = self.environ.get('QUERY_STRING', '')
if py3k:
- # This isn't perfect; if the given PATH_INFO is in the wrong encoding,
- # it may fail to match the appropriate config section URI. But meh.
+ # This isn't perfect; if the given PATH_INFO is in the
+ # wrong encoding, it may fail to match the appropriate config
+ # section URI. But meh.
old_enc = self.environ.get('wsgi.url_encoding', 'ISO-8859-1')
new_enc = self.cpapp.find_config(self.environ.get('PATH_INFO', ''),
"request.uri_encoding", 'utf-8')
if new_enc.lower() != old_enc.lower():
- # Even though the path and qs are unicode, the WSGI server is
- # required by PEP 3333 to coerce them to ISO-8859-1 masquerading
- # as unicode. So we have to encode back to bytes and then decode
- # again using the "correct" encoding.
+ # Even though the path and qs are unicode, the WSGI server
+ # is required by PEP 3333 to coerce them to ISO-8859-1
+ # masquerading as unicode. So we have to encode back to
+ # bytes and then decode again using the "correct" encoding.
try:
u_path = path.encode(old_enc).decode(new_enc)
u_qs = qs.encode(old_enc).decode(new_enc)
@@ -313,19 +330,19 @@ class AppResponse(object):
# Only set transcoded values if they both succeed.
path = u_path
qs = u_qs
-
+
rproto = self.environ.get('SERVER_PROTOCOL')
headers = self.translate_headers(self.environ)
rfile = self.environ['wsgi.input']
request.run(meth, path, qs, rproto, headers, rfile)
-
+
headerNames = {'HTTP_CGI_AUTHORIZATION': 'Authorization',
'CONTENT_LENGTH': 'Content-Length',
'CONTENT_TYPE': 'Content-Type',
'REMOTE_HOST': 'Remote-Host',
'REMOTE_ADDR': 'Remote-Addr',
}
-
+
def translate_headers(self, environ):
"""Translate CGI-environ header names to HTTP header names."""
for cgiName in environ:
@@ -339,8 +356,9 @@ class AppResponse(object):
class CPWSGIApp(object):
+
"""A WSGI application object for a CherryPy Application."""
-
+
pipeline = [('ExceptionTrapper', ExceptionTrapper),
('InternalRedirector', InternalRedirector),
]
@@ -349,35 +367,36 @@ class CPWSGIApp(object):
plus optional keyword arguments, and returns a WSGI application
(that takes environ and start_response arguments). The 'name' can
be any you choose, and will correspond to keys in self.config."""
-
+
head = None
"""Rather than nest all apps in the pipeline on each call, it's only
done the first time, and the result is memoized into self.head. Set
this to None again if you change self.pipeline after calling self."""
-
+
config = {}
"""A dict whose keys match names listed in the pipeline. Each
value is a further dict which will be passed to the corresponding
named WSGI callable (from the pipeline) as keyword arguments."""
-
+
response_class = AppResponse
- """The class to instantiate and return as the next app in the WSGI chain."""
-
+ """The class to instantiate and return as the next app in the WSGI chain.
+ """
+
def __init__(self, cpapp, pipeline=None):
self.cpapp = cpapp
self.pipeline = self.pipeline[:]
if pipeline:
self.pipeline.extend(pipeline)
self.config = self.config.copy()
-
+
def tail(self, environ, start_response):
"""WSGI application callable for the actual CherryPy application.
-
+
You probably shouldn't call this; call self.__call__ instead,
so that any WSGI middleware in self.pipeline can run first.
"""
return self.response_class(environ, start_response, self.cpapp)
-
+
def __call__(self, environ, start_response):
head = self.head
if head is None:
@@ -389,7 +408,7 @@ class CPWSGIApp(object):
head = callable(head, **conf)
self.head = head
return head(environ, start_response)
-
+
def namespace_handler(self, k, v):
"""Config handler for the 'wsgi' namespace."""
if k == "pipeline":
@@ -405,4 +424,3 @@ class CPWSGIApp(object):
name, arg = k.split(".", 1)
bucket = self.config.setdefault(name, {})
bucket[arg] = v
-
diff --git a/cherrypy/_cpwsgi_server.py b/cherrypy/_cpwsgi_server.py
index 21af5134..b648bab1 100644
--- a/cherrypy/_cpwsgi_server.py
+++ b/cherrypy/_cpwsgi_server.py
@@ -8,32 +8,37 @@ from cherrypy import wsgiserver
class CPWSGIServer(wsgiserver.CherryPyWSGIServer):
+
"""Wrapper for wsgiserver.CherryPyWSGIServer.
-
+
wsgiserver has been designed to not reference CherryPy in any way,
so that it can be used in other frameworks and applications. Therefore,
we wrap it here, so we can set our own mount points from cherrypy.tree
and apply some attributes from config -> cherrypy.server -> wsgiserver.
"""
-
+
def __init__(self, server_adapter=cherrypy.server):
self.server_adapter = server_adapter
- self.max_request_header_size = self.server_adapter.max_request_header_size or 0
- self.max_request_body_size = self.server_adapter.max_request_body_size or 0
-
+ self.max_request_header_size = (
+ self.server_adapter.max_request_header_size or 0
+ )
+ self.max_request_body_size = (
+ self.server_adapter.max_request_body_size or 0
+ )
+
server_name = (self.server_adapter.socket_host or
self.server_adapter.socket_file or
None)
-
+
self.wsgi_version = self.server_adapter.wsgi_version
s = wsgiserver.CherryPyWSGIServer
s.__init__(self, server_adapter.bind_addr, cherrypy.tree,
self.server_adapter.thread_pool,
server_name,
- max = self.server_adapter.thread_pool_max,
- request_queue_size = self.server_adapter.socket_queue_size,
- timeout = self.server_adapter.socket_timeout,
- shutdown_timeout = self.server_adapter.shutdown_timeout,
+ max=self.server_adapter.thread_pool_max,
+ request_queue_size=self.server_adapter.socket_queue_size,
+ timeout=self.server_adapter.socket_timeout,
+ shutdown_timeout=self.server_adapter.shutdown_timeout,
)
self.protocol = self.server_adapter.protocol_version
self.nodelay = self.server_adapter.nodelay
@@ -55,9 +60,9 @@ class CPWSGIServer(wsgiserver.CherryPyWSGIServer):
self.server_adapter.ssl_certificate,
self.server_adapter.ssl_private_key,
self.server_adapter.ssl_certificate_chain)
-
- self.stats['Enabled'] = getattr(self.server_adapter, 'statistics', False)
+
+ self.stats['Enabled'] = getattr(
+ self.server_adapter, 'statistics', False)
def error_log(self, msg="", level=20, traceback=False):
cherrypy.engine.log(msg, level, traceback)
-
diff --git a/cherrypy/cherryd b/cherrypy/cherryd
index adb2a02e..5afb27ad 100755
--- a/cherrypy/cherryd
+++ b/cherrypy/cherryd
@@ -7,6 +7,7 @@ import cherrypy
from cherrypy.process import plugins, servers
from cherrypy import Application
+
def start(configfiles=None, daemonize=False, environment=None,
fastcgi=False, scgi=False, pidfile=None, imports=None,
cgi=False):
@@ -14,7 +15,7 @@ def start(configfiles=None, daemonize=False, environment=None,
sys.path = [''] + sys.path
for i in imports or []:
exec("import %s" % i)
-
+
for c in configfiles or []:
cherrypy.config.update(c)
# If there's only one app mounted, merge config into it.
@@ -22,26 +23,26 @@ def start(configfiles=None, daemonize=False, environment=None,
for app in cherrypy.tree.apps.values():
if isinstance(app, Application):
app.merge(c)
-
+
engine = cherrypy.engine
-
+
if environment is not None:
cherrypy.config.update({'environment': environment})
-
+
# Only daemonize if asked to.
if daemonize:
# Don't print anything to stdout/sterr.
cherrypy.config.update({'log.screen': False})
plugins.Daemonizer(engine).subscribe()
-
+
if pidfile:
plugins.PIDFile(engine, pidfile).subscribe()
-
+
if hasattr(engine, "signal_handler"):
engine.signal_handler.subscribe()
if hasattr(engine, "console_control_handler"):
engine.console_control_handler.subscribe()
-
+
if (fastcgi and (scgi or cgi)) or (scgi and cgi):
cherrypy.log.error("You may only specify one of the cgi, fastcgi, and "
"scgi options.", 'ENGINE')
@@ -51,7 +52,7 @@ def start(configfiles=None, daemonize=False, environment=None,
cherrypy.config.update({'engine.autoreload_on': False})
# Turn off the default HTTP server (which is subscribed by default).
cherrypy.server.unsubscribe()
-
+
addr = cherrypy.server.bind_addr
if fastcgi:
f = servers.FlupFCGIServer(application=cherrypy.tree,
@@ -64,7 +65,7 @@ def start(configfiles=None, daemonize=False, environment=None,
bindAddress=addr)
s = servers.ServerAdapter(engine, httpserver=f, bind_addr=addr)
s.subscribe()
-
+
# Always start the engine; this will start all other services
try:
engine.start()
@@ -77,7 +78,7 @@ def start(configfiles=None, daemonize=False, environment=None,
if __name__ == '__main__':
from optparse import OptionParser
-
+
p = OptionParser()
p.add_option('-c', '--config', action="append", dest='config',
help="specify config file(s)")
@@ -86,7 +87,8 @@ if __name__ == '__main__':
p.add_option('-e', '--environment', dest='environment', default=None,
help="apply the given config environment")
p.add_option('-f', action="store_true", dest='fastcgi',
- help="start a fastcgi server instead of the default HTTP server")
+ help="start a fastcgi server instead of the default HTTP "
+ "server")
p.add_option('-s', action="store_true", dest='scgi',
help="start a scgi server instead of the default HTTP server")
p.add_option('-x', action="store_true", dest='cgi',
@@ -98,12 +100,11 @@ if __name__ == '__main__':
p.add_option('-P', '--Path', action="append", dest='Path',
help="add the given paths to sys.path")
options, args = p.parse_args()
-
+
if options.Path:
for p in options.Path:
sys.path.insert(0, p)
-
+
start(options.config, options.daemonize,
options.environment, options.fastcgi, options.scgi,
options.pidfile, options.imports, options.cgi)
-
diff --git a/cherrypy/lib/__init__.py b/cherrypy/lib/__init__.py
index 3fc0ec58..554b3920 100644
--- a/cherrypy/lib/__init__.py
+++ b/cherrypy/lib/__init__.py
@@ -3,16 +3,18 @@
# Deprecated in CherryPy 3.2 -- remove in CherryPy 3.3
from cherrypy.lib.reprconf import unrepr, modules, attributes
+
class file_generator(object):
+
"""Yield the given input (a file object) in chunks (default 64k). (Core)"""
-
+
def __init__(self, input, chunkSize=65536):
self.input = input
self.chunkSize = chunkSize
-
+
def __iter__(self):
return self
-
+
def __next__(self):
chunk = self.input.read(self.chunkSize)
if chunk:
@@ -23,6 +25,7 @@ class file_generator(object):
raise StopIteration()
next = __next__
+
def file_generator_limited(fileobj, count, chunk_size=65536):
"""Yield the given file object in chunks, stopping after `count`
bytes has been emitted. Default chunk size is 64kB. (Core)
@@ -36,6 +39,7 @@ def file_generator_limited(fileobj, count, chunk_size=65536):
remaining -= chunklen
yield chunk
+
def set_vary_header(response, header_name):
"Add a Vary header to a response"
varies = response.headers.get("Vary", "")
diff --git a/cherrypy/lib/auth.py b/cherrypy/lib/auth.py
index 7d2f6dc2..71591aaa 100644
--- a/cherrypy/lib/auth.py
+++ b/cherrypy/lib/auth.py
@@ -3,25 +3,27 @@ from cherrypy.lib import httpauth
def check_auth(users, encrypt=None, realm=None):
- """If an authorization header contains credentials, return True, else False."""
+ """If an authorization header contains credentials, return True or False.
+ """
request = cherrypy.serving.request
if 'authorization' in request.headers:
# make sure the provided credentials are correctly set
ah = httpauth.parseAuthorization(request.headers['authorization'])
if ah is None:
raise cherrypy.HTTPError(400, 'Bad Request')
-
+
if not encrypt:
encrypt = httpauth.DIGEST_AUTH_ENCODERS[httpauth.MD5]
-
+
if hasattr(users, '__call__'):
try:
# backward compatibility
- users = users() # expect it to return a dictionary
-
+ users = users() # expect it to return a dictionary
+
if not isinstance(users, dict):
- raise ValueError("Authentication users must be a dictionary")
-
+ raise ValueError(
+ "Authentication users must be a dictionary")
+
# fetch the user password
password = users.get(ah["username"], None)
except TypeError:
@@ -30,58 +32,66 @@ def check_auth(users, encrypt=None, realm=None):
else:
if not isinstance(users, dict):
raise ValueError("Authentication users must be a dictionary")
-
+
# fetch the user password
password = users.get(ah["username"], None)
-
+
# validate the authorization by re-computing it here
# and compare it with what the user-agent provided
if httpauth.checkResponse(ah, password, method=request.method,
encrypt=encrypt, realm=realm):
request.login = ah["username"]
return True
-
+
request.login = False
return False
+
def basic_auth(realm, users, encrypt=None, debug=False):
"""If auth fails, raise 401 with a basic authentication header.
-
+
realm
A string containing the authentication realm.
-
+
users
- A dict of the form: {username: password} or a callable returning a dict.
-
+ A dict of the form: {username: password} or a callable returning
+ a dict.
+
encrypt
callable used to encrypt the password returned from the user-agent.
if None it defaults to a md5 encryption.
-
+
"""
if check_auth(users, encrypt):
if debug:
cherrypy.log('Auth successful', 'TOOLS.BASIC_AUTH')
return
-
+
# inform the user-agent this path is protected
- cherrypy.serving.response.headers['www-authenticate'] = httpauth.basicAuth(realm)
-
- raise cherrypy.HTTPError(401, "You are not authorized to access that resource")
+ cherrypy.serving.response.headers[
+ 'www-authenticate'] = httpauth.basicAuth(realm)
+
+ raise cherrypy.HTTPError(
+ 401, "You are not authorized to access that resource")
+
def digest_auth(realm, users, debug=False):
"""If auth fails, raise 401 with a digest authentication header.
-
+
realm
A string containing the authentication realm.
users
- A dict of the form: {username: password} or a callable returning a dict.
+ A dict of the form: {username: password} or a callable returning
+ a dict.
"""
if check_auth(users, realm=realm):
if debug:
cherrypy.log('Auth successful', 'TOOLS.DIGEST_AUTH')
return
-
+
# inform the user-agent this path is protected
- cherrypy.serving.response.headers['www-authenticate'] = httpauth.digestAuth(realm)
-
- raise cherrypy.HTTPError(401, "You are not authorized to access that resource")
+ cherrypy.serving.response.headers[
+ 'www-authenticate'] = httpauth.digestAuth(realm)
+
+ raise cherrypy.HTTPError(
+ 401, "You are not authorized to access that resource")
diff --git a/cherrypy/lib/auth_basic.py b/cherrypy/lib/auth_basic.py
index 2c05e013..5ba16f7f 100644
--- a/cherrypy/lib/auth_basic.py
+++ b/cherrypy/lib/auth_basic.py
@@ -3,7 +3,8 @@
# vim:ts=4:sw=4:expandtab:fileencoding=utf-8
__doc__ = """This module provides a CherryPy 3.x tool which implements
-the server-side of HTTP Basic Access Authentication, as described in :rfc:`2617`.
+the server-side of HTTP Basic Access Authentication, as described in
+:rfc:`2617`.
Example usage, using the built-in checkpassword_dict function which uses a dict
as the credentials store::
@@ -60,13 +61,13 @@ def basic_auth(realm, checkpassword, debug=False):
username and password are the values obtained from the request's
'authorization' header. If authentication succeeds, checkpassword
returns True, else it returns False.
-
+
"""
-
+
if '"' in realm:
raise ValueError('Realm cannot contain the " (quote) character.')
request = cherrypy.serving.request
-
+
auth_header = request.headers.get('authorization')
if auth_header is not None:
try:
@@ -77,11 +78,13 @@ def basic_auth(realm, checkpassword, debug=False):
if debug:
cherrypy.log('Auth succeeded', 'TOOLS.AUTH_BASIC')
request.login = username
- return # successful authentication
- except (ValueError, binascii.Error): # split() error, base64.decodestring() error
+ return # successful authentication
+ # split() error, base64.decodestring() error
+ except (ValueError, binascii.Error):
raise cherrypy.HTTPError(400, 'Bad Request')
-
- # Respond with 401 status and a WWW-Authenticate header
- cherrypy.serving.response.headers['www-authenticate'] = 'Basic realm="%s"' % realm
- raise cherrypy.HTTPError(401, "You are not authorized to access that resource")
+ # Respond with 401 status and a WWW-Authenticate header
+ cherrypy.serving.response.headers[
+ 'www-authenticate'] = 'Basic realm="%s"' % realm
+ raise cherrypy.HTTPError(
+ 401, "You are not authorized to access that resource")
diff --git a/cherrypy/lib/auth_digest.py b/cherrypy/lib/auth_digest.py
index 4bdaccd6..e06535dc 100644
--- a/cherrypy/lib/auth_digest.py
+++ b/cherrypy/lib/auth_digest.py
@@ -41,6 +41,8 @@ def TRACE(msg):
# Three helper functions for users of the tool, providing three variants
# of get_ha1() functions for three different kinds of credential stores.
+
+
def get_ha1_dict_plain(user_password_dict):
"""Returns a get_ha1 function which obtains a plaintext password from a
dictionary of the form: {username : password}.
@@ -57,6 +59,7 @@ def get_ha1_dict_plain(user_password_dict):
return get_ha1
+
def get_ha1_dict(user_ha1_dict):
"""Returns a get_ha1 function which obtains a HA1 password hash from a
dictionary of the form: {username : HA1}.
@@ -71,6 +74,7 @@ def get_ha1_dict(user_ha1_dict):
return get_ha1
+
def get_ha1_file_htdigest(filename):
"""Returns a get_ha1 function which obtains a HA1 password hash from a
flat file with lines of the same format as that produced by the Apache
@@ -99,8 +103,9 @@ def get_ha1_file_htdigest(filename):
def synthesize_nonce(s, key, timestamp=None):
- """Synthesize a nonce value which resists spoofing and can be checked for staleness.
- Returns a string suitable as the value for 'nonce' in the www-authenticate header.
+ """Synthesize a nonce value which resists spoofing and can be checked
+ for staleness. Returns a string suitable as the value for 'nonce' in
+ the www-authenticate header.
s
A string related to the resource, such as the hostname of the server.
@@ -125,6 +130,7 @@ def H(s):
class HttpDigestAuthorization (object):
+
"""Class to parse a Digest Authorization header and perform re-calculation
of the digest.
"""
@@ -135,7 +141,7 @@ class HttpDigestAuthorization (object):
def __init__(self, auth_header, http_method, debug=False):
self.http_method = http_method
self.debug = debug
- scheme, params = auth_header.split(" ", 1)
+ scheme, params = auth_header.split(" ", 1)
self.scheme = scheme.lower()
if self.scheme != 'digest':
raise ValueError('Authorization scheme is not "Digest"')
@@ -151,84 +157,95 @@ class HttpDigestAuthorization (object):
self.nonce = paramsd.get('nonce')
self.uri = paramsd.get('uri')
self.method = paramsd.get('method')
- self.response = paramsd.get('response') # the response digest
- self.algorithm = paramsd.get('algorithm', 'MD5')
+ self.response = paramsd.get('response') # the response digest
+ self.algorithm = paramsd.get('algorithm', 'MD5').upper()
self.cnonce = paramsd.get('cnonce')
self.opaque = paramsd.get('opaque')
- self.qop = paramsd.get('qop') # qop
- self.nc = paramsd.get('nc') # nonce count
+ self.qop = paramsd.get('qop') # qop
+ self.nc = paramsd.get('nc') # nonce count
# perform some correctness checks
if self.algorithm not in valid_algorithms:
- raise ValueError(self.errmsg("Unsupported value for algorithm: '%s'" % self.algorithm))
-
- has_reqd = self.username and \
- self.realm and \
- self.nonce and \
- self.uri and \
- self.response
+ raise ValueError(
+ self.errmsg("Unsupported value for algorithm: '%s'" %
+ self.algorithm))
+
+ has_reqd = (
+ self.username and
+ self.realm and
+ self.nonce and
+ self.uri and
+ self.response
+ )
if not has_reqd:
- raise ValueError(self.errmsg("Not all required parameters are present."))
+ raise ValueError(
+ self.errmsg("Not all required parameters are present."))
if self.qop:
if self.qop not in valid_qops:
- raise ValueError(self.errmsg("Unsupported value for qop: '%s'" % self.qop))
+ raise ValueError(
+ self.errmsg("Unsupported value for qop: '%s'" % self.qop))
if not (self.cnonce and self.nc):
- raise ValueError(self.errmsg("If qop is sent then cnonce and nc MUST be present"))
+ raise ValueError(
+ self.errmsg("If qop is sent then "
+ "cnonce and nc MUST be present"))
else:
if self.cnonce or self.nc:
- raise ValueError(self.errmsg("If qop is not sent, neither cnonce nor nc can be present"))
-
+ raise ValueError(
+ self.errmsg("If qop is not sent, "
+ "neither cnonce nor nc can be present"))
def __str__(self):
return 'authorization : %s' % self.auth_header
def validate_nonce(self, s, key):
"""Validate the nonce.
- Returns True if nonce was generated by synthesize_nonce() and the timestamp
- is not spoofed, else returns False.
+ Returns True if nonce was generated by synthesize_nonce() and the
+ timestamp is not spoofed, else returns False.
s
- A string related to the resource, such as the hostname of the server.
+ A string related to the resource, such as the hostname of
+ the server.
key
A secret string known only to the server.
- Both s and key must be the same values which were used to synthesize the nonce
- we are trying to validate.
+ Both s and key must be the same values which were used to synthesize
+ the nonce we are trying to validate.
"""
try:
timestamp, hashpart = self.nonce.split(':', 1)
- s_timestamp, s_hashpart = synthesize_nonce(s, key, timestamp).split(':', 1)
+ s_timestamp, s_hashpart = synthesize_nonce(
+ s, key, timestamp).split(':', 1)
is_valid = s_hashpart == hashpart
if self.debug:
TRACE('validate_nonce: %s' % is_valid)
return is_valid
- except ValueError: # split() error
+ except ValueError: # split() error
pass
return False
-
def is_nonce_stale(self, max_age_seconds=600):
"""Returns True if a validated nonce is stale. The nonce contains a
- timestamp in plaintext and also a secure hash of the timestamp. You should
- first validate the nonce to ensure the plaintext timestamp is not spoofed.
+ timestamp in plaintext and also a secure hash of the timestamp.
+ You should first validate the nonce to ensure the plaintext
+ timestamp is not spoofed.
"""
try:
timestamp, hashpart = self.nonce.split(':', 1)
if int(timestamp) + max_age_seconds > int(time.time()):
return False
- except ValueError: # int() error
+ except ValueError: # int() error
pass
if self.debug:
TRACE("nonce is stale")
return True
-
def HA2(self, entity_body=''):
"""Returns the H(A2) string. See :rfc:`2617` section 3.2.2.3."""
# RFC 2617 3.2.2.3
- # If the "qop" directive's value is "auth" or is unspecified, then A2 is:
+ # If the "qop" directive's value is "auth" or is unspecified,
+ # then A2 is:
# A2 = method ":" digest-uri-value
#
# If the "qop" value is "auth-int", then A2 is:
@@ -238,11 +255,11 @@ class HttpDigestAuthorization (object):
elif self.qop == "auth-int":
a2 = "%s:%s:%s" % (self.http_method, self.uri, H(entity_body))
else:
- # in theory, this should never happen, since I validate qop in __init__()
+ # in theory, this should never happen, since I validate qop in
+ # __init__()
raise ValueError(self.errmsg("Unrecognized value for qop!"))
return H(a2)
-
def request_digest(self, ha1, entity_body=''):
"""Calculates the Request-Digest. See :rfc:`2617` section 3.2.2.1.
@@ -253,22 +270,24 @@ class HttpDigestAuthorization (object):
If 'qop' is set to 'auth-int', then A2 includes a hash
of the "entity body". The entity body is the part of the
message which follows the HTTP headers. See :rfc:`2617` section
- 4.3. This refers to the entity the user agent sent in the request which
- has the Authorization header. Typically GET requests don't have an entity,
- and POST requests do.
+ 4.3. This refers to the entity the user agent sent in the
+ request which has the Authorization header. Typically GET
+ requests don't have an entity, and POST requests do.
"""
ha2 = self.HA2(entity_body)
# Request-Digest -- RFC 2617 3.2.2.1
if self.qop:
- req = "%s:%s:%s:%s:%s" % (self.nonce, self.nc, self.cnonce, self.qop, ha2)
+ req = "%s:%s:%s:%s:%s" % (
+ self.nonce, self.nc, self.cnonce, self.qop, ha2)
else:
req = "%s:%s" % (self.nonce, ha2)
# RFC 2617 3.2.2.2
#
- # If the "algorithm" directive's value is "MD5" or is unspecified, then A1 is:
- # A1 = unq(username-value) ":" unq(realm-value) ":" passwd
+ # If the "algorithm" directive's value is "MD5" or is unspecified,
+ # then A1 is:
+ # A1 = unq(username-value) ":" unq(realm-value) ":" passwd
#
# If the "algorithm" directive's value is "MD5-sess", then A1 is
# calculated only once - on the first request by the client following
@@ -282,8 +301,8 @@ class HttpDigestAuthorization (object):
return digest
-
-def www_authenticate(realm, key, algorithm='MD5', nonce=None, qop=qop_auth, stale=False):
+def www_authenticate(realm, key, algorithm='MD5', nonce=None, qop=qop_auth,
+ stale=False):
"""Constructs a WWW-Authenticate header for Digest authentication."""
if qop not in valid_qops:
raise ValueError("Unsupported value for qop: '%s'" % qop)
@@ -293,7 +312,7 @@ def www_authenticate(realm, key, algorithm='MD5', nonce=None, qop=qop_auth, stal
if nonce is None:
nonce = synthesize_nonce(realm, key)
s = 'Digest realm="%s", nonce="%s", algorithm="%s", qop="%s"' % (
- realm, nonce, algorithm, qop)
+ realm, nonce, algorithm, qop)
if stale:
s += ', stale="true"'
return s
@@ -303,11 +322,11 @@ def digest_auth(realm, get_ha1, key, debug=False):
"""A CherryPy tool which hooks at before_handler to perform
HTTP Digest Access Authentication, as specified in :rfc:`2617`.
- If the request has an 'authorization' header with a 'Digest' scheme, this
- tool authenticates the credentials supplied in that header. If
- the request has no 'authorization' header, or if it does but the scheme is
- not "Digest", or if authentication fails, the tool sends a 401 response with
- a 'WWW-Authenticate' Digest header.
+ If the request has an 'authorization' header with a 'Digest' scheme,
+ this tool authenticates the credentials supplied in that header.
+ If the request has no 'authorization' header, or if it does but the
+ scheme is not "Digest", or if authentication fails, the tool sends
+ a 401 response with a 'WWW-Authenticate' Digest header.
realm
A string containing the authentication realm.
@@ -322,7 +341,8 @@ def digest_auth(realm, get_ha1, key, debug=False):
None.
key
- A secret string known only to the server, used in the synthesis of nonces.
+ A secret string known only to the server, used in the synthesis
+ of nonces.
"""
request = cherrypy.serving.request
@@ -331,9 +351,11 @@ def digest_auth(realm, get_ha1, key, debug=False):
nonce_is_stale = False
if auth_header is not None:
try:
- auth = HttpDigestAuthorization(auth_header, request.method, debug=debug)
+ auth = HttpDigestAuthorization(
+ auth_header, request.method, debug=debug)
except ValueError:
- raise cherrypy.HTTPError(400, "The Authorization header could not be parsed.")
+ raise cherrypy.HTTPError(
+ 400, "The Authorization header could not be parsed.")
if debug:
TRACE(str(auth))
@@ -341,19 +363,22 @@ def digest_auth(realm, get_ha1, key, debug=False):
if auth.validate_nonce(realm, key):
ha1 = get_ha1(realm, auth.username)
if ha1 is not None:
- # note that for request.body to be available we need to hook in at
- # before_handler, not on_start_resource like 3.1.x digest_auth does.
+ # note that for request.body to be available we need to
+ # hook in at before_handler, not on_start_resource like
+ # 3.1.x digest_auth does.
digest = auth.request_digest(ha1, entity_body=request.body)
- if digest == auth.response: # authenticated
+ if digest == auth.response: # authenticated
if debug:
TRACE("digest matches auth.response")
# Now check if nonce is stale.
- # The choice of ten minutes' lifetime for nonce is somewhat arbitrary
+ # The choice of ten minutes' lifetime for nonce is somewhat
+ # arbitrary
nonce_is_stale = auth.is_nonce_stale(max_age_seconds=600)
if not nonce_is_stale:
request.login = auth.username
if debug:
- TRACE("authentication of %s successful" % auth.username)
+ TRACE("authentication of %s successful" %
+ auth.username)
return
# Respond with 401 status and a WWW-Authenticate header
@@ -361,5 +386,5 @@ def digest_auth(realm, get_ha1, key, debug=False):
if debug:
TRACE(header)
cherrypy.serving.response.headers['WWW-Authenticate'] = header
- raise cherrypy.HTTPError(401, "You are not authorized to access that resource")
-
+ raise cherrypy.HTTPError(
+ 401, "You are not authorized to access that resource")
diff --git a/cherrypy/lib/caching.py b/cherrypy/lib/caching.py
index 435b9dc1..fab6b569 100644
--- a/cherrypy/lib/caching.py
+++ b/cherrypy/lib/caching.py
@@ -1,7 +1,7 @@
"""
-CherryPy implements a simple caching system as a pluggable Tool. This tool tries
-to be an (in-process) HTTP/1.1-compliant cache. It's not quite there yet, but
-it's probably good enough for most sites.
+CherryPy implements a simple caching system as a pluggable Tool. This tool
+tries to be an (in-process) HTTP/1.1-compliant cache. It's not quite there
+yet, but it's probably good enough for most sites.
In general, GET responses are cached (along with selecting headers) and, if
another request arrives for the same resource, the caching Tool will return 304
@@ -9,8 +9,8 @@ Not Modified if possible, or serve the cached response otherwise. It also sets
request.cached to True if serving a cached representation, and sets
request.cacheable to False (so it doesn't get cached again).
-If POST, PUT, or DELETE requests are made for a cached resource, they invalidate
-(delete) any cached response.
+If POST, PUT, or DELETE requests are made for a cached resource, they
+invalidate (delete) any cached response.
Usage
=====
@@ -39,58 +39,58 @@ import time
import cherrypy
from cherrypy.lib import cptools, httputil
-from cherrypy._cpcompat import copyitems, ntob, set_daemon, sorted
+from cherrypy._cpcompat import copyitems, ntob, set_daemon, sorted, Event
class Cache(object):
+
"""Base class for Cache implementations."""
-
+
def get(self):
"""Return the current variant if in the cache, else None."""
raise NotImplemented
-
+
def put(self, obj, size):
"""Store the current variant in the cache."""
raise NotImplemented
-
+
def delete(self):
"""Remove ALL cached variants of the current resource."""
raise NotImplemented
-
+
def clear(self):
"""Reset the cache to its initial, empty state."""
raise NotImplemented
-
-# ------------------------------- Memory Cache ------------------------------- #
-
-
+# ------------------------------ Memory Cache ------------------------------- #
class AntiStampedeCache(dict):
+
"""A storage system for cached items which reduces stampede collisions."""
-
+
def wait(self, key, timeout=5, debug=False):
"""Return the cached value for the given key, or None.
-
+
If timeout is not None, and the value is already
being calculated by another thread, wait until the given timeout has
elapsed. If the value is available before the timeout expires, it is
returned. If not, None is returned, and a sentinel placed in the cache
to signal other threads to wait.
-
+
If timeout is None, no waiting is performed nor sentinels used.
"""
value = self.get(key)
- if isinstance(value, threading._Event):
+ if isinstance(value, Event):
if timeout is None:
# Ignore the other thread and recalc it ourselves.
if debug:
cherrypy.log('No timeout', 'TOOLS.CACHING')
return None
-
+
# Wait until it's done or times out.
if debug:
- cherrypy.log('Waiting up to %s seconds' % timeout, 'TOOLS.CACHING')
+ cherrypy.log('Waiting up to %s seconds' %
+ timeout, 'TOOLS.CACHING')
value.wait(timeout)
if value.result is not None:
# The other thread finished its calculation. Use it.
@@ -104,7 +104,7 @@ class AntiStampedeCache(dict):
e = threading.Event()
e.result = None
dict.__setitem__(self, key, e)
-
+
return None
elif value is None:
# Stick an Event in the slot so other threads wait
@@ -115,12 +115,12 @@ class AntiStampedeCache(dict):
e.result = None
dict.__setitem__(self, key, e)
return value
-
+
def __setitem__(self, key, value):
"""Set the cached value for the given key."""
existing = self.get(key)
dict.__setitem__(self, key, value)
- if isinstance(existing, threading._Event):
+ if isinstance(existing, Event):
# Set Event.result so other threads waiting on it have
# immediate access without needing to poll the cache again.
existing.result = value
@@ -128,49 +128,51 @@ class AntiStampedeCache(dict):
class MemoryCache(Cache):
+
"""An in-memory cache for varying response content.
-
+
Each key in self.store is a URI, and each value is an AntiStampedeCache.
The response for any given URI may vary based on the values of
"selecting request headers"; that is, those named in the Vary
response header. We assume the list of header names to be constant
for each URI throughout the lifetime of the application, and store
that list in ``self.store[uri].selecting_headers``.
-
+
The items contained in ``self.store[uri]`` have keys which are tuples of
request header values (in the same order as the names in its
selecting_headers), and values which are the actual responses.
"""
-
+
maxobjects = 1000
"""The maximum number of cached objects; defaults to 1000."""
-
+
maxobj_size = 100000
"""The maximum size of each cached object in bytes; defaults to 100 KB."""
-
+
maxsize = 10000000
"""The maximum size of the entire cache in bytes; defaults to 10 MB."""
-
+
delay = 600
- """Seconds until the cached content expires; defaults to 600 (10 minutes)."""
-
+ """Seconds until the cached content expires; defaults to 600 (10 minutes).
+ """
+
antistampede_timeout = 5
"""Seconds to wait for other threads to release a cache lock."""
-
+
expire_freq = 0.1
"""Seconds to sleep between cache expiration sweeps."""
-
+
debug = False
-
+
def __init__(self):
self.clear()
-
+
# Run self.expire_cache in a separate daemon thread.
t = threading.Thread(target=self.expire_cache, name='expire_cache')
self.expiration_thread = t
set_daemon(t, True)
t.start()
-
+
def clear(self):
"""Reset the cache to its initial, empty state."""
self.store = {}
@@ -181,10 +183,10 @@ class MemoryCache(Cache):
self.tot_expires = 0
self.tot_non_modified = 0
self.cursize = 0
-
+
def expire_cache(self):
"""Continuously examine cached objects, expiring stale ones.
-
+
This function is designed to be run in its own daemon thread,
referenced at ``self.expiration_thread``.
"""
@@ -207,17 +209,17 @@ class MemoryCache(Cache):
pass
del self.expirations[expiration_time]
time.sleep(self.expire_freq)
-
+
def get(self):
"""Return the current variant if in the cache, else None."""
request = cherrypy.serving.request
self.tot_gets += 1
-
+
uri = cherrypy.url(qs=request.query_string)
uricache = self.store.get(uri)
if uricache is None:
return None
-
+
header_values = [request.headers.get(h, '')
for h in uricache.selecting_headers]
variant = uricache.wait(key=tuple(sorted(header_values)),
@@ -226,12 +228,12 @@ class MemoryCache(Cache):
if variant is not None:
self.tot_hist += 1
return variant
-
+
def put(self, variant, size):
"""Store the current variant in the cache."""
request = cherrypy.serving.request
response = cherrypy.serving.response
-
+
uri = cherrypy.url(qs=request.query_string)
uricache = self.store.get(uri)
if uricache is None:
@@ -239,24 +241,24 @@ class MemoryCache(Cache):
uricache.selecting_headers = [
e.value for e in response.headers.elements('Vary')]
self.store[uri] = uricache
-
+
if len(self.store) < self.maxobjects:
total_size = self.cursize + size
-
+
# checks if there's space for the object
if (size < self.maxobj_size and total_size < self.maxsize):
# add to the expirations list
expiration_time = response.time + self.delay
bucket = self.expirations.setdefault(expiration_time, [])
bucket.append((size, uri, uricache.selecting_headers))
-
+
# add to the cache
header_values = [request.headers.get(h, '')
for h in uricache.selecting_headers]
uricache[tuple(sorted(header_values))] = variant
self.tot_puts += 1
self.cursize = total_size
-
+
def delete(self):
"""Remove ALL cached variants of the current resource."""
uri = cherrypy.url(qs=cherrypy.serving.request.query_string)
@@ -265,12 +267,12 @@ class MemoryCache(Cache):
def get(invalid_methods=("POST", "PUT", "DELETE"), debug=False, **kwargs):
"""Try to obtain cached output. If fresh enough, raise HTTPError(304).
-
+
If POST, PUT, or DELETE:
* invalidates (deletes) any cached response for this resource
* sets request.cached = False
* sets request.cacheable = False
-
+
else if a cached copy exists:
* sets request.cached = True
* sets request.cacheable = False
@@ -280,7 +282,7 @@ def get(invalid_methods=("POST", "PUT", "DELETE"), debug=False, **kwargs):
if necessary.
* sets response.status and response.body to the cached values
* returns True
-
+
otherwise:
* sets request.cached = False
* sets request.cacheable = True
@@ -288,16 +290,16 @@ def get(invalid_methods=("POST", "PUT", "DELETE"), debug=False, **kwargs):
"""
request = cherrypy.serving.request
response = cherrypy.serving.response
-
+
if not hasattr(cherrypy, "_cache"):
# Make a process-wide Cache object.
cherrypy._cache = kwargs.pop("cache_class", MemoryCache)()
-
+
# Take all remaining kwargs and set them on the Cache object.
for k, v in kwargs.items():
setattr(cherrypy._cache, k, v)
cherrypy._cache.debug = debug
-
+
# POST, PUT, DELETE should invalidate (delete) the cached copy.
# See http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.10.
if request.method in invalid_methods:
@@ -308,12 +310,12 @@ def get(invalid_methods=("POST", "PUT", "DELETE"), debug=False, **kwargs):
request.cached = False
request.cacheable = False
return False
-
+
if 'no-cache' in [e.value for e in request.headers.elements('Pragma')]:
request.cached = False
request.cacheable = True
return False
-
+
cache_data = cherrypy._cache.get()
request.cached = bool(cache_data)
request.cacheable = not request.cached
@@ -325,17 +327,19 @@ def get(invalid_methods=("POST", "PUT", "DELETE"), debug=False, **kwargs):
directive = atoms.pop(0)
if directive == 'max-age':
if len(atoms) != 1 or not atoms[0].isdigit():
- raise cherrypy.HTTPError(400, "Invalid Cache-Control header")
+ raise cherrypy.HTTPError(
+ 400, "Invalid Cache-Control header")
max_age = int(atoms[0])
break
elif directive == 'no-cache':
if debug:
- cherrypy.log('Ignoring cache due to Cache-Control: no-cache',
- 'TOOLS.CACHING')
+ cherrypy.log(
+ 'Ignoring cache due to Cache-Control: no-cache',
+ 'TOOLS.CACHING')
request.cached = False
request.cacheable = True
return False
-
+
if debug:
cherrypy.log('Reading response from cache', 'TOOLS.CACHING')
s, h, b, create_time = cache_data
@@ -347,15 +351,16 @@ def get(invalid_methods=("POST", "PUT", "DELETE"), debug=False, **kwargs):
request.cached = False
request.cacheable = True
return False
-
- # Copy the response headers. See http://www.cherrypy.org/ticket/721.
+
+ # Copy the response headers. See
+ # https://bitbucket.org/cherrypy/cherrypy/issue/721.
response.headers = rh = httputil.HeaderMap()
for k in h:
dict.__setitem__(rh, k, dict.__getitem__(h, k))
-
+
# Add the required Age header
response.headers["Age"] = str(age)
-
+
try:
# Note that validate_since depends on a Last-Modified header;
# this was put into the cached copy, and should have been
@@ -366,7 +371,7 @@ def get(invalid_methods=("POST", "PUT", "DELETE"), debug=False, **kwargs):
if x.status == 304:
cherrypy._cache.tot_non_modified += 1
raise
-
+
# serve it & get out from the request
response.status = s
response.body = b
@@ -379,29 +384,29 @@ def get(invalid_methods=("POST", "PUT", "DELETE"), debug=False, **kwargs):
def tee_output():
"""Tee response output to cache storage. Internal."""
# Used by CachingTool by attaching to request.hooks
-
+
request = cherrypy.serving.request
if 'no-store' in request.headers.values('Cache-Control'):
return
-
+
def tee(body):
"""Tee response.body into a list."""
if ('no-cache' in response.headers.values('Pragma') or
- 'no-store' in response.headers.values('Cache-Control')):
+ 'no-store' in response.headers.values('Cache-Control')):
for chunk in body:
yield chunk
return
-
+
output = []
for chunk in body:
output.append(chunk)
yield chunk
-
+
# save the cache data
body = ntob('').join(output)
cherrypy._cache.put((response.status, response.headers or {},
body, response.time), len(body))
-
+
response = cherrypy.serving.response
response.body = tee(response.body)
@@ -415,25 +420,25 @@ def expires(secs=0, force=False, debug=False):
expire. The 'Expires' header will be set to response.time + secs.
If secs is zero, the 'Expires' header is set one year in the past, and
the following "cache prevention" headers are also set:
-
+
* Pragma: no-cache
* Cache-Control': no-cache, must-revalidate
force
If False, the following headers are checked:
-
+
* Etag
* Last-Modified
* Age
* Expires
-
+
If any are already present, none of the above response headers are set.
-
+
"""
-
+
response = cherrypy.serving.response
headers = response.headers
-
+
cacheable = False
if not force:
# some header names that indicate that the response can be cached
@@ -441,7 +446,7 @@ def expires(secs=0, force=False, debug=False):
if indicator in headers:
cacheable = True
break
-
+
if not cacheable and not force:
if debug:
cherrypy.log('request is not cacheable', 'TOOLS.EXPIRES')
@@ -450,7 +455,7 @@ def expires(secs=0, force=False, debug=False):
cherrypy.log('request is cacheable', 'TOOLS.EXPIRES')
if isinstance(secs, datetime.timedelta):
secs = (86400 * secs.days) + secs.seconds
-
+
if secs == 0:
if force or ("Pragma" not in headers):
headers["Pragma"] = "no-cache"
diff --git a/cherrypy/lib/covercp.py b/cherrypy/lib/covercp.py
index 9b701b56..a74ec342 100644
--- a/cherrypy/lib/covercp.py
+++ b/cherrypy/lib/covercp.py
@@ -1,7 +1,7 @@
"""Code-coverage tools for CherryPy.
To use this module, or the coverage tools in the test suite,
-you need to download 'coverage.py', either Gareth Rees' `original
+you need to download 'coverage.py', either Gareth Rees' `original
implementation <http://www.garethrees.org/2001/12/04/python-coverage/>`_
or Ned Batchelder's `enhanced version:
<http://www.nedbatchelder.com/code/modules/coverage.html>`_
@@ -24,23 +24,27 @@ import re
import sys
import cgi
from cherrypy._cpcompat import quote_plus
-import os, os.path
+import os
+import os.path
localFile = os.path.join(os.path.dirname(__file__), "coverage.cache")
the_coverage = None
try:
from coverage import coverage
the_coverage = coverage(data_file=localFile)
+
def start():
the_coverage.start()
except ImportError:
# Setting the_coverage to None will raise errors
# that need to be trapped downstream.
the_coverage = None
-
+
import warnings
- warnings.warn("No code coverage will be performed; coverage.py could not be imported.")
-
+ warnings.warn(
+ "No code coverage will be performed; "
+ "coverage.py could not be imported.")
+
def start():
pass
start.priority = 20
@@ -69,7 +73,7 @@ TEMPLATE_MENU = """<html>
font-size: small;
font-weight: bold;
font-style: italic;
- margin-top: 5px;
+ margin-top: 5px;
}
input { border: 1px solid #ccc; padding: 2px; }
.directory {
@@ -118,15 +122,18 @@ TEMPLATE_FORM = """
<div id="options">
<form action='menu' method=GET>
<input type='hidden' name='base' value='%(base)s' />
- Show percentages <input type='checkbox' %(showpct)s name='showpct' value='checked' /><br />
- Hide files over <input type='text' id='pct' name='pct' value='%(pct)s' size='3' />%%<br />
+ Show percentages
+ <input type='checkbox' %(showpct)s name='showpct' value='checked' /><br />
+ Hide files over
+ <input type='text' id='pct' name='pct' value='%(pct)s' size='3' />%%<br />
Exclude files matching<br />
- <input type='text' id='exclude' name='exclude' value='%(exclude)s' size='20' />
+ <input type='text' id='exclude' name='exclude'
+ value='%(exclude)s' size='20' />
<br />
<input type='submit' value='Change view' id="submit"/>
</form>
-</div>"""
+</div>"""
TEMPLATE_FRAMESET = """<html>
<head><title>CherryPy coverage data</title></head>
@@ -173,7 +180,10 @@ TEMPLATE_LOC_EXCLUDED = """<tr class="excluded">
<td>%s</td>
</tr>\n"""
-TEMPLATE_ITEM = "%s%s<a class='file' href='report?name=%s' target='main'>%s</a>\n"
+TEMPLATE_ITEM = (
+ "%s%s<a class='file' href='report?name=%s' target='main'>%s</a>\n"
+)
+
def _percent(statements, missing):
s = len(statements)
@@ -182,24 +192,31 @@ def _percent(statements, missing):
return int(round(100.0 * e / s))
return 0
+
def _show_branch(root, base, path, pct=0, showpct=False, exclude="",
coverage=the_coverage):
-
+
# Show the directory name and any of our children
dirs = [k for k, v in root.items() if v]
dirs.sort()
for name in dirs:
newpath = os.path.join(path, name)
-
+
if newpath.lower().startswith(base):
relpath = newpath[len(base):]
yield "| " * relpath.count(os.sep)
- yield "<a class='directory' href='menu?base=%s&exclude=%s'>%s</a>\n" % \
- (newpath, quote_plus(exclude), name)
-
- for chunk in _show_branch(root[name], base, newpath, pct, showpct, exclude, coverage=coverage):
+ yield (
+ "<a class='directory' "
+ "href='menu?base=%s&exclude=%s'>%s</a>\n" %
+ (newpath, quote_plus(exclude), name)
+ )
+
+ for chunk in _show_branch(
+ root[name], base, newpath, pct, showpct,
+ exclude, coverage=coverage
+ ):
yield chunk
-
+
# Now list the files
if path.lower().startswith(base):
relpath = path[len(base):]
@@ -207,7 +224,7 @@ def _show_branch(root, base, path, pct=0, showpct=False, exclude="",
files.sort()
for name in files:
newpath = os.path.join(path, name)
-
+
pc_str = ""
if showpct:
try:
@@ -217,22 +234,24 @@ def _show_branch(root, base, path, pct=0, showpct=False, exclude="",
pass
else:
pc = _percent(statements, missing)
- pc_str = ("%3d%% " % pc).replace(' ','&nbsp;')
+ pc_str = ("%3d%% " % pc).replace(' ', '&nbsp;')
if pc < float(pct) or pc == -1:
pc_str = "<span class='fail'>%s</span>" % pc_str
else:
pc_str = "<span class='pass'>%s</span>" % pc_str
-
+
yield TEMPLATE_ITEM % ("| " * (relpath.count(os.sep) + 1),
pc_str, newpath, name)
+
def _skip_file(path, exclude):
if exclude:
return bool(re.search(exclude, path))
+
def _graft(path, tree):
d = tree
-
+
p = path
atoms = []
while True:
@@ -243,12 +262,13 @@ def _graft(path, tree):
atoms.append(p)
if p != "/":
atoms.append("/")
-
+
atoms.reverse()
for node in atoms:
if node:
d = d.setdefault(node, {})
+
def get_tree(base, exclude, coverage=the_coverage):
"""Return covered module names as a nested dict."""
tree = {}
@@ -258,8 +278,9 @@ def get_tree(base, exclude, coverage=the_coverage):
_graft(path, tree)
return tree
+
class CoverStats(object):
-
+
def __init__(self, coverage, root=None):
self.coverage = coverage
if root is None:
@@ -268,20 +289,20 @@ class CoverStats(object):
import cherrypy
root = os.path.dirname(cherrypy.__file__)
self.root = root
-
+
def index(self):
return TEMPLATE_FRAMESET % self.root.lower()
index.exposed = True
-
+
def menu(self, base="/", pct="50", showpct="",
exclude=r'python\d\.\d|test|tut\d|tutorial'):
-
+
# The coverage module uses all-lower-case names.
base = base.lower().rstrip(os.sep)
-
+
yield TEMPLATE_MENU
yield TEMPLATE_FORM % locals()
-
+
# Start by showing links for parent paths
yield "<div id='crumbs'>"
path = ""
@@ -292,22 +313,23 @@ class CoverStats(object):
yield ("<a href='menu?base=%s&exclude=%s'>%s</a> %s"
% (path, quote_plus(exclude), atom, os.sep))
yield "</div>"
-
+
yield "<div id='tree'>"
-
+
# Then display the tree
tree = get_tree(base, exclude, self.coverage)
if not tree:
yield "<p>No modules covered.</p>"
else:
for chunk in _show_branch(tree, base, "/", pct,
- showpct=='checked', exclude, coverage=self.coverage):
+ showpct == 'checked', exclude,
+ coverage=self.coverage):
yield chunk
-
+
yield "</div>"
yield "</body></html>"
menu.exposed = True
-
+
def annotated_file(self, filename, statements, excluded, missing):
source = open(filename, 'r')
buffer = []
@@ -329,9 +351,10 @@ class CoverStats(object):
yield template % (lno, cgi.escape(pastline))
buffer = []
yield template % (lineno, cgi.escape(line))
-
+
def report(self, name):
- filename, statements, excluded, missing, _ = self.coverage.analysis2(name)
+ filename, statements, excluded, missing, _ = self.coverage.analysis2(
+ name)
pc = _percent(statements, missing)
yield TEMPLATE_COVERAGE % dict(name=os.path.basename(name),
fullpath=name,
@@ -350,9 +373,9 @@ def serve(path=localFile, port=8080, root=None):
if coverage is None:
raise ImportError("The coverage module could not be imported.")
from coverage import coverage
- cov = coverage(data_file = path)
+ cov = coverage(data_file=path)
cov.load()
-
+
import cherrypy
cherrypy.config.update({'server.socket_port': int(port),
'server.thread_pool': 10,
@@ -362,4 +385,3 @@ def serve(path=localFile, port=8080, root=None):
if __name__ == "__main__":
serve(*tuple(sys.argv[1:]))
-
diff --git a/cherrypy/lib/cpstats.py b/cherrypy/lib/cpstats.py
index 9be947f2..a8661a14 100644
--- a/cherrypy/lib/cpstats.py
+++ b/cherrypy/lib/cpstats.py
@@ -21,33 +21,37 @@ to collect stats to import a third-party module. Therefore, we choose to
re-use the `logging` module by adding a `statistics` object to it.
That `logging.statistics` object is a nested dict. It is not a custom class,
-because that would 1) require libraries and applications to import a third-
-party module in order to participate, 2) inhibit innovation in extrapolation
-approaches and in reporting tools, and 3) be slow. There are, however, some
-specifications regarding the structure of the dict.
-
- {
- +----"SQLAlchemy": {
- | "Inserts": 4389745,
- | "Inserts per Second":
- | lambda s: s["Inserts"] / (time() - s["Start"]),
- | C +---"Table Statistics": {
- | o | "widgets": {-----------+
- N | l | "Rows": 1.3M, | Record
- a | l | "Inserts": 400, |
- m | e | },---------------------+
- e | c | "froobles": {
- s | t | "Rows": 7845,
- p | i | "Inserts": 0,
- a | o | },
- c | n +---},
- e | "Slow Queries":
- | [{"Query": "SELECT * FROM widgets;",
- | "Processing Time": 47.840923343,
- | },
- | ],
- +----},
- }
+because that would:
+
+ 1. require libraries and applications to import a third-party module in
+ order to participate
+ 2. inhibit innovation in extrapolation approaches and in reporting tools, and
+ 3. be slow.
+
+There are, however, some specifications regarding the structure of the dict.::
+
+ {
+ +----"SQLAlchemy": {
+ | "Inserts": 4389745,
+ | "Inserts per Second":
+ | lambda s: s["Inserts"] / (time() - s["Start"]),
+ | C +---"Table Statistics": {
+ | o | "widgets": {-----------+
+ N | l | "Rows": 1.3M, | Record
+ a | l | "Inserts": 400, |
+ m | e | },---------------------+
+ e | c | "froobles": {
+ s | t | "Rows": 7845,
+ p | i | "Inserts": 0,
+ a | o | },
+ c | n +---},
+ e | "Slow Queries":
+ | [{"Query": "SELECT * FROM widgets;",
+ | "Processing Time": 47.840923343,
+ | },
+ | ],
+ +----},
+ }
The `logging.statistics` dict has four levels. The topmost level is nothing
more than a set of names to introduce modularity, usually along the lines of
@@ -65,13 +69,13 @@ Each namespace, then, is a dict of named statistical values, such as
good on a report: spaces and capitalization are just fine.
In addition to scalars, values in a namespace MAY be a (third-layer)
-dict, or a list, called a "collection". For example, the CherryPy StatsTool
-keeps track of what each request is doing (or has most recently done)
-in a 'Requests' collection, where each key is a thread ID; each
+dict, or a list, called a "collection". For example, the CherryPy
+:class:`StatsTool` keeps track of what each request is doing (or has most
+recently done) in a 'Requests' collection, where each key is a thread ID; each
value in the subdict MUST be a fourth dict (whew!) of statistical data about
each thread. We call each subdict in the collection a "record". Similarly,
-the StatsTool also keeps a list of slow queries, where each record contains
-data about each slow query, in order.
+the :class:`StatsTool` also keeps a list of slow queries, where each record
+contains data about each slow query, in order.
Values in a namespace or record may also be functions, which brings us to:
@@ -86,17 +90,17 @@ scalar values you already have on hand.
When it comes time to report on the gathered data, however, we usually have
much more freedom in what we can calculate. Therefore, whenever reporting
-tools (like the provided StatsPage CherryPy class) fetch the contents of
-`logging.statistics` for reporting, they first call `extrapolate_statistics`
-(passing the whole `statistics` dict as the only argument). This makes a
-deep copy of the statistics dict so that the reporting tool can both iterate
-over it and even change it without harming the original. But it also expands
-any functions in the dict by calling them. For example, you might have a
-'Current Time' entry in the namespace with the value "lambda scope: time.time()".
-The "scope" parameter is the current namespace dict (or record, if we're
-currently expanding one of those instead), allowing you access to existing
-static entries. If you're truly evil, you can even modify more than one entry
-at a time.
+tools (like the provided :class:`StatsPage` CherryPy class) fetch the contents
+of `logging.statistics` for reporting, they first call
+`extrapolate_statistics` (passing the whole `statistics` dict as the only
+argument). This makes a deep copy of the statistics dict so that the
+reporting tool can both iterate over it and even change it without harming
+the original. But it also expands any functions in the dict by calling them.
+For example, you might have a 'Current Time' entry in the namespace with the
+value "lambda scope: time.time()". The "scope" parameter is the current
+namespace dict (or record, if we're currently expanding one of those
+instead), allowing you access to existing static entries. If you're truly
+evil, you can even modify more than one entry at a time.
However, don't try to calculate an entry and then use its value in further
extrapolations; the order in which the functions are called is not guaranteed.
@@ -108,19 +112,20 @@ After the whole thing has been extrapolated, it's time for:
Reporting
---------
-The StatsPage class grabs the `logging.statistics` dict, extrapolates it all,
-and then transforms it to HTML for easy viewing. Each namespace gets its own
-header and attribute table, plus an extra table for each collection. This is
-NOT part of the statistics specification; other tools can format how they like.
+The :class:`StatsPage` class grabs the `logging.statistics` dict, extrapolates
+it all, and then transforms it to HTML for easy viewing. Each namespace gets
+its own header and attribute table, plus an extra table for each collection.
+This is NOT part of the statistics specification; other tools can format how
+they like.
You can control which columns are output and how they are formatted by updating
StatsPage.formatting, which is a dict that mirrors the keys and nesting of
`logging.statistics`. The difference is that, instead of data values, it has
formatting values. Use None for a given key to indicate to the StatsPage that a
-given column should not be output. Use a string with formatting (such as '%.3f')
-to interpolate the value(s), or use a callable (such as lambda v: v.isoformat())
-for more advanced formatting. Any entry which is not mentioned in the formatting
-dict is output unchanged.
+given column should not be output. Use a string with formatting
+(such as '%.3f') to interpolate the value(s), or use a callable (such as
+lambda v: v.isoformat()) for more advanced formatting. Any entry which is not
+mentioned in the formatting dict is output unchanged.
Monitoring
----------
@@ -145,12 +150,12 @@ entries to False or True, if present.
Usage
=====
-To collect statistics on CherryPy applications:
+To collect statistics on CherryPy applications::
from cherrypy.lib import cpstats
appconfig['/']['tools.cpstats.on'] = True
-To collect statistics on your own code:
+To collect statistics on your own code::
import logging
# Initialize the repository
@@ -172,20 +177,22 @@ To collect statistics on your own code:
if mystats.get('Enabled', False):
mystats['Important Events'] += 1
-To report statistics:
+To report statistics::
root.cpstats = cpstats.StatsPage()
-To format statistics reports:
+To format statistics reports::
See 'Reporting', above.
"""
-# -------------------------------- Statistics -------------------------------- #
+# ------------------------------- Statistics -------------------------------- #
import logging
-if not hasattr(logging, 'statistics'): logging.statistics = {}
+if not hasattr(logging, 'statistics'):
+ logging.statistics = {}
+
def extrapolate_statistics(scope):
"""Return an extrapolated copy of the given scope."""
@@ -201,7 +208,7 @@ def extrapolate_statistics(scope):
return c
-# --------------------- CherryPy Applications Statistics --------------------- #
+# -------------------- CherryPy Applications Statistics --------------------- #
import threading
import time
@@ -211,12 +218,20 @@ import cherrypy
appstats = logging.statistics.setdefault('CherryPy Applications', {})
appstats.update({
'Enabled': True,
- 'Bytes Read/Request': lambda s: (s['Total Requests'] and
- (s['Total Bytes Read'] / float(s['Total Requests'])) or 0.0),
+ 'Bytes Read/Request': lambda s: (
+ s['Total Requests'] and
+ (s['Total Bytes Read'] / float(s['Total Requests'])) or
+ 0.0
+ ),
'Bytes Read/Second': lambda s: s['Total Bytes Read'] / s['Uptime'](s),
- 'Bytes Written/Request': lambda s: (s['Total Requests'] and
- (s['Total Bytes Written'] / float(s['Total Requests'])) or 0.0),
- 'Bytes Written/Second': lambda s: s['Total Bytes Written'] / s['Uptime'](s),
+ 'Bytes Written/Request': lambda s: (
+ s['Total Requests'] and
+ (s['Total Bytes Written'] / float(s['Total Requests'])) or
+ 0.0
+ ),
+ 'Bytes Written/Second': lambda s: (
+ s['Total Bytes Written'] / s['Uptime'](s)
+ ),
'Current Time': lambda s: time.time(),
'Current Requests': 0,
'Requests/Second': lambda s: float(s['Total Requests']) / s['Uptime'](s),
@@ -228,28 +243,29 @@ appstats.update({
'Total Time': 0,
'Uptime': lambda s: time.time() - s['Start Time'],
'Requests': {},
- })
+})
proc_time = lambda s: time.time() - s['Start Time']
class ByteCountWrapper(object):
+
"""Wraps a file-like object, counting the number of bytes read."""
-
+
def __init__(self, rfile):
self.rfile = rfile
self.bytes_read = 0
-
+
def read(self, size=-1):
data = self.rfile.read(size)
self.bytes_read += len(data)
return data
-
+
def readline(self, size=-1):
data = self.rfile.readline(size)
self.bytes_read += len(data)
return data
-
+
def readlines(self, sizehint=0):
# Shamelessly stolen from StringIO
total = 0
@@ -262,13 +278,13 @@ class ByteCountWrapper(object):
break
line = self.readline()
return lines
-
+
def close(self):
self.rfile.close()
-
+
def __iter__(self):
return self
-
+
def next(self):
data = self.rfile.next()
self.bytes_read += len(data)
@@ -279,30 +295,31 @@ average_uriset_time = lambda s: s['Count'] and (s['Sum'] / s['Count']) or 0
class StatsTool(cherrypy.Tool):
+
"""Record various information about the current request."""
-
+
def __init__(self):
cherrypy.Tool.__init__(self, 'on_end_request', self.record_stop)
-
+
def _setup(self):
"""Hook this tool into cherrypy.request.
-
+
The standard CherryPy request object will automatically call this
method when the tool is "turned on" in config.
"""
if appstats.get('Enabled', False):
cherrypy.Tool._setup(self)
self.record_start()
-
+
def record_start(self):
"""Record the beginning of a request."""
request = cherrypy.serving.request
if not hasattr(request.rfile, 'bytes_read'):
request.rfile = ByteCountWrapper(request.rfile)
request.body.fp = request.rfile
-
+
r = request.remote
-
+
appstats['Current Requests'] += 1
appstats['Total Requests'] += 1
appstats['Requests'][threading._get_ident()] = {
@@ -315,37 +332,39 @@ class StatsTool(cherrypy.Tool):
'Request-Line': request.request_line,
'Response Status': None,
'Start Time': time.time(),
- }
+ }
- def record_stop(self, uriset=None, slow_queries=1.0, slow_queries_count=100,
- debug=False, **kwargs):
+ def record_stop(
+ self, uriset=None, slow_queries=1.0, slow_queries_count=100,
+ debug=False, **kwargs):
"""Record the end of a request."""
resp = cherrypy.serving.response
w = appstats['Requests'][threading._get_ident()]
-
+
r = cherrypy.request.rfile.bytes_read
w['Bytes Read'] = r
appstats['Total Bytes Read'] += r
-
+
if resp.stream:
w['Bytes Written'] = 'chunked'
else:
cl = int(resp.headers.get('Content-Length', 0))
w['Bytes Written'] = cl
appstats['Total Bytes Written'] += cl
-
- w['Response Status'] = getattr(resp, 'output_status', None) or resp.status
-
+
+ w['Response Status'] = getattr(
+ resp, 'output_status', None) or resp.status
+
w['End Time'] = time.time()
p = w['End Time'] - w['Start Time']
w['Processing Time'] = p
appstats['Total Time'] += p
-
+
appstats['Current Requests'] -= 1
-
+
if debug:
cherrypy.log('Stats recorded: %s' % repr(w), 'TOOLS.CPSTATS')
-
+
if uriset:
rs = appstats.setdefault('URI Set Tracking', {})
r = rs.setdefault(uriset, {
@@ -357,7 +376,7 @@ class StatsTool(cherrypy.Tool):
r['Max'] = p
r['Count'] += 1
r['Sum'] += p
-
+
if slow_queries and p > slow_queries:
sq = appstats.setdefault('Slow Queries', [])
sq.append(w.copy())
@@ -388,6 +407,7 @@ missing = object()
locale_date = lambda v: time.strftime('%c', time.gmtime(v))
iso_format = lambda v: time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(v))
+
def pause_resume(ns):
def _pause_resume(enabled):
pause_disabled = ''
@@ -410,7 +430,7 @@ def pause_resume(ns):
class StatsPage(object):
-
+
formatting = {
'CherryPy Applications': {
'Enabled': pause_resume('CherryPy Applications'),
@@ -427,20 +447,20 @@ class StatsPage(object):
'End Time': None,
'Processing Time': '%.3f',
'Start Time': iso_format,
- },
+ },
'URI Set Tracking': {
'Avg': '%.3f',
'Max': '%.3f',
'Min': '%.3f',
'Sum': '%.3f',
- },
+ },
'Requests': {
'Bytes Read': '%s',
'Bytes Written': '%s',
'End Time': None,
'Processing Time': '%.3f',
'Start Time': None,
- },
+ },
},
'CherryPy WSGIServer': {
'Enabled': pause_resume('CherryPy WSGIServer'),
@@ -448,8 +468,7 @@ class StatsPage(object):
'Start time': iso_format,
},
}
-
-
+
def index(self):
# Transform the raw data into pretty output for HTML
yield """
@@ -500,18 +519,25 @@ table.stats2 th {
""" % title
for i, (key, value) in enumerate(scalars):
colnum = i % 3
- if colnum == 0: yield """
+ if colnum == 0:
+ yield """
<tr>"""
- yield """
- <th>%(key)s</th><td id='%(title)s-%(key)s'>%(value)s</td>""" % vars()
- if colnum == 2: yield """
+ yield (
+ """
+ <th>%(key)s</th><td id='%(title)s-%(key)s'>%(value)s</td>""" %
+ vars()
+ )
+ if colnum == 2:
+ yield """
</tr>"""
-
- if colnum == 0: yield """
+
+ if colnum == 0:
+ yield """
<th></th><td></td>
<th></th><td></td>
</tr>"""
- elif colnum == 1: yield """
+ elif colnum == 1:
+ yield """
<th></th><td></td>
</tr>"""
yield """
@@ -547,7 +573,7 @@ table.stats2 th {
</html>
"""
index.exposed = True
-
+
def get_namespaces(self):
"""Yield (title, scalars, collections) for each namespace."""
s = extrapolate_statistics(logging.statistics)
@@ -574,7 +600,7 @@ table.stats2 th {
v = format % v
scalars.append((k, v))
yield title, scalars, collections
-
+
def get_dict_collection(self, v, formatting):
"""Return ([headers], [rows]) for the given collection."""
# E.g., the 'Requests' dict.
@@ -588,7 +614,7 @@ table.stats2 th {
if k3 not in headers:
headers.append(k3)
headers.sort()
-
+
subrows = []
for k2, record in sorted(v.items()):
subrow = [k2]
@@ -604,9 +630,9 @@ table.stats2 th {
v3 = format % v3
subrow.append(v3)
subrows.append(subrow)
-
+
return headers, subrows
-
+
def get_list_collection(self, v, formatting):
"""Return ([headers], [subrows]) for the given collection."""
# E.g., the 'Slow Queries' list.
@@ -620,7 +646,7 @@ table.stats2 th {
if k3 not in headers:
headers.append(k3)
headers.sort()
-
+
subrows = []
for record in v:
subrow = []
@@ -636,27 +662,26 @@ table.stats2 th {
v3 = format % v3
subrow.append(v3)
subrows.append(subrow)
-
+
return headers, subrows
-
+
if json is not None:
def data(self):
s = extrapolate_statistics(logging.statistics)
cherrypy.response.headers['Content-Type'] = 'application/json'
return json.dumps(s, sort_keys=True, indent=4)
data.exposed = True
-
+
def pause(self, namespace):
logging.statistics.get(namespace, {})['Enabled'] = False
raise cherrypy.HTTPRedirect('./')
pause.exposed = True
pause.cp_config = {'tools.allow.on': True,
'tools.allow.methods': ['POST']}
-
+
def resume(self, namespace):
logging.statistics.get(namespace, {})['Enabled'] = True
raise cherrypy.HTTPRedirect('./')
resume.exposed = True
resume.cp_config = {'tools.allow.on': True,
'tools.allow.methods': ['POST']}
-
diff --git a/cherrypy/lib/cptools.py b/cherrypy/lib/cptools.py
index b426a3e7..84b8fad0 100644
--- a/cherrypy/lib/cptools.py
+++ b/cherrypy/lib/cptools.py
@@ -4,7 +4,7 @@ import logging
import re
import cherrypy
-from cherrypy._cpcompat import basestring, ntob, md5, set
+from cherrypy._cpcompat import basestring, md5, set, unicodestr
from cherrypy.lib import httputil as _httputil
@@ -12,12 +12,12 @@ from cherrypy.lib import httputil as _httputil
def validate_etags(autotags=False, debug=False):
"""Validate the current ETag against If-Match, If-None-Match headers.
-
+
If autotags is True, an ETag response-header value will be provided
from an MD5 hash of the response body (unless some other code has
already provided an ETag header). If False (the default), the ETag
will not be automatic.
-
+
WARNING: the autotags feature is not designed for URL's which allow
methods other than GET. For example, if a POST to the same URL returns
no content, the automatic ETag will be incorrect, breaking a fundamental
@@ -27,15 +27,15 @@ def validate_etags(autotags=False, debug=False):
See :rfc:`2616` Section 14.24.
"""
response = cherrypy.serving.response
-
+
# Guard against being run twice.
if hasattr(response, "ETag"):
return
-
+
status, reason, msg = _httputil.valid_status(response.status)
-
+
etag = response.headers.get('ETag')
-
+
# Automatic ETag generation. See warning in docstring.
if etag:
if debug:
@@ -52,9 +52,9 @@ def validate_etags(autotags=False, debug=False):
if debug:
cherrypy.log('Setting ETag: %s' % etag, 'TOOLS.ETAGS')
response.headers['ETag'] = etag
-
+
response.ETag = etag
-
+
# "If the request would, without the If-Match header field, result in
# anything other than a 2xx or 412 status, then the If-Match header
# MUST be ignored."
@@ -62,7 +62,7 @@ def validate_etags(autotags=False, debug=False):
cherrypy.log('Status: %s' % status, 'TOOLS.ETAGS')
if status >= 200 and status <= 299:
request = cherrypy.serving.request
-
+
conditions = request.headers.elements('If-Match') or []
conditions = [str(x) for x in conditions]
if debug:
@@ -71,7 +71,7 @@ def validate_etags(autotags=False, debug=False):
if conditions and not (conditions == ["*"] or etag in conditions):
raise cherrypy.HTTPError(412, "If-Match failed: ETag %r did "
"not match %r" % (etag, conditions))
-
+
conditions = request.headers.elements('If-None-Match') or []
conditions = [str(x) for x in conditions]
if debug:
@@ -79,16 +79,18 @@ def validate_etags(autotags=False, debug=False):
'TOOLS.ETAGS')
if conditions == ["*"] or etag in conditions:
if debug:
- cherrypy.log('request.method: %s' % request.method, 'TOOLS.ETAGS')
+ cherrypy.log('request.method: %s' %
+ request.method, 'TOOLS.ETAGS')
if request.method in ("GET", "HEAD"):
raise cherrypy.HTTPRedirect([], 304)
else:
raise cherrypy.HTTPError(412, "If-None-Match failed: ETag %r "
"matched %r" % (etag, conditions))
+
def validate_since():
"""Validate the current Last-Modified against If-Modified-Since headers.
-
+
If no code has set the Last-Modified response header, then no validation
will be performed.
"""
@@ -96,14 +98,14 @@ def validate_since():
lastmod = response.headers.get('Last-Modified')
if lastmod:
status, reason, msg = _httputil.valid_status(response.status)
-
+
request = cherrypy.serving.request
-
+
since = request.headers.get('If-Unmodified-Since')
if since and since != lastmod:
if (status >= 200 and status <= 299) or status == 412:
raise cherrypy.HTTPError(412)
-
+
since = request.headers.get('If-Modified-Since')
if since and since == lastmod:
if (status >= 200 and status <= 299) or status == 304:
@@ -117,11 +119,11 @@ def validate_since():
def allow(methods=None, debug=False):
"""Raise 405 if request.method not in methods (default ['GET', 'HEAD']).
-
+
The given methods are case-insensitive, and may be in any order.
If only one method is allowed, you may supply a single string;
if more than one, supply a list of strings.
-
+
Regardless of whether the current method is allowed or not, this
also emits an 'Allow' response header, containing the given methods.
"""
@@ -132,7 +134,7 @@ def allow(methods=None, debug=False):
methods = ['GET', 'HEAD']
elif 'GET' in methods and 'HEAD' not in methods:
methods.append('HEAD')
-
+
cherrypy.response.headers['Allow'] = ', '.join(methods)
if cherrypy.request.method not in methods:
if debug:
@@ -148,27 +150,27 @@ def allow(methods=None, debug=False):
def proxy(base=None, local='X-Forwarded-Host', remote='X-Forwarded-For',
scheme='X-Forwarded-Proto', debug=False):
"""Change the base URL (scheme://host[:port][/path]).
-
+
For running a CP server behind Apache, lighttpd, or other HTTP server.
-
+
For Apache and lighttpd, you should leave the 'local' argument at the
default value of 'X-Forwarded-Host'. For Squid, you probably want to set
tools.proxy.local = 'Origin'.
-
+
If you want the new request.base to include path info (not just the host),
you must explicitly set base to the full base path, and ALSO set 'local'
to '', so that the X-Forwarded-Host request header (which never includes
path info) does not override it. Regardless, the value for 'base' MUST
NOT end in a slash.
-
+
cherrypy.request.remote.ip (the IP address of the client) will be
rewritten if the header specified by the 'remote' arg is valid.
By default, 'remote' is set to 'X-Forwarded-For'. If you do not
want to rewrite remote.ip, set the 'remote' arg to an empty string.
"""
-
+
request = cherrypy.serving.request
-
+
if scheme:
s = request.headers.get(scheme, None)
if debug:
@@ -181,7 +183,7 @@ def proxy(base=None, local='X-Forwarded-Host', remote='X-Forwarded-For',
scheme = s
if not scheme:
scheme = request.base[:request.base.find("://")]
-
+
if local:
lbase = request.headers.get(local, None)
if debug:
@@ -194,27 +196,28 @@ def proxy(base=None, local='X-Forwarded-Host', remote='X-Forwarded-For',
base = '127.0.0.1'
else:
base = '127.0.0.1:%s' % port
-
+
if base.find("://") == -1:
# add http:// or https:// if needed
base = scheme + "://" + base
-
+
request.base = base
-
+
if remote:
xff = request.headers.get(remote)
if debug:
cherrypy.log('Testing remote %r:%r' % (remote, xff), 'TOOLS.PROXY')
if xff:
if remote == 'X-Forwarded-For':
- # See http://bob.pythonmac.org/archives/2005/09/23/apache-x-forwarded-for-caveat/
+ # See
+ # http://bob.pythonmac.org/archives/2005/09/23/apache-x-forwarded-for-caveat/
xff = xff.split(',')[-1].strip()
request.remote.ip = xff
def ignore_headers(headers=('Range',), debug=False):
"""Delete request headers whose field names are included in 'headers'.
-
+
This is a useful tool for working behind certain HTTP servers;
for example, Apache duplicates the work that CP does for 'Range'
headers, and will doubly-truncate the response.
@@ -241,10 +244,10 @@ response_headers.failsafe = True
def referer(pattern, accept=True, accept_missing=False, error=403,
message='Forbidden Referer header.', debug=False):
"""Raise HTTPError if Referer header does/does not match the given pattern.
-
+
pattern
A regular expression pattern to test against the Referer.
-
+
accept
If True, the Referer must match the pattern; if False,
the Referer must NOT match the pattern.
@@ -254,10 +257,10 @@ def referer(pattern, accept=True, accept_missing=False, error=403,
error
The HTTP error code to return to the client on failure.
-
+
message
A string to include in the response body on failure.
-
+
"""
try:
ref = cherrypy.serving.request.headers['Referer']
@@ -272,44 +275,48 @@ def referer(pattern, accept=True, accept_missing=False, error=403,
cherrypy.log('No Referer header', 'TOOLS.REFERER')
if accept_missing:
return
-
+
raise cherrypy.HTTPError(error, message)
class SessionAuth(object):
+
"""Assert that the user is logged in."""
-
+
session_key = "username"
debug = False
-
+
def check_username_and_password(self, username, password):
pass
-
+
def anonymous(self):
"""Provide a temporary user name for anonymous users."""
pass
-
+
def on_login(self, username):
pass
-
+
def on_logout(self, username):
pass
-
+
def on_check(self, username):
pass
-
- def login_screen(self, from_page='..', username='', error_msg='', **kwargs):
- return ntob("""<html><body>
+
+ def login_screen(self, from_page='..', username='', error_msg='',
+ **kwargs):
+ return (unicodestr("""<html><body>
Message: %(error_msg)s
<form method="post" action="do_login">
- Login: <input type="text" name="username" value="%(username)s" size="10" /><br />
- Password: <input type="password" name="password" size="10" /><br />
- <input type="hidden" name="from_page" value="%(from_page)s" /><br />
+ Login: <input type="text" name="username" value="%(username)s" size="10" />
+ <br />
+ Password: <input type="password" name="password" size="10" />
+ <br />
+ <input type="hidden" name="from_page" value="%(from_page)s" />
+ <br />
<input type="submit" />
</form>
-</body></html>""" % {'from_page': from_page, 'username': username,
- 'error_msg': error_msg}, "utf-8")
-
+</body></html>""") % vars()).encode("utf-8")
+
def do_login(self, username, password, from_page='..', **kwargs):
"""Login. May raise redirect, or return True if request handled."""
response = cherrypy.serving.response
@@ -326,7 +333,7 @@ Message: %(error_msg)s
cherrypy.session[self.session_key] = username
self.on_login(username)
raise cherrypy.HTTPRedirect(from_page or "/")
-
+
def do_logout(self, from_page='..', **kwargs):
"""Logout. May raise redirect, or return True if request handled."""
sess = cherrypy.session
@@ -336,18 +343,20 @@ Message: %(error_msg)s
cherrypy.serving.request.login = None
self.on_logout(username)
raise cherrypy.HTTPRedirect(from_page)
-
+
def do_check(self):
- """Assert username. May raise redirect, or return True if request handled."""
+ """Assert username. Raise redirect, or return True if request handled.
+ """
sess = cherrypy.session
request = cherrypy.serving.request
response = cherrypy.serving.response
-
+
username = sess.get(self.session_key)
if not username:
sess[self.session_key] = username = self.anonymous()
if self.debug:
- cherrypy.log('No session[username], trying anonymous', 'TOOLS.SESSAUTH')
+ cherrypy.log(
+ 'No session[username], trying anonymous', 'TOOLS.SESSAUTH')
if not username:
url = cherrypy.url(qs=request.query_string)
if self.debug:
@@ -359,18 +368,20 @@ Message: %(error_msg)s
del response.headers["Content-Length"]
return True
if self.debug:
- cherrypy.log('Setting request.login to %r' % username, 'TOOLS.SESSAUTH')
+ cherrypy.log('Setting request.login to %r' %
+ username, 'TOOLS.SESSAUTH')
request.login = username
self.on_check(username)
-
+
def run(self):
request = cherrypy.serving.request
response = cherrypy.serving.response
-
+
path = request.path_info
if path.endswith('login_screen'):
if self.debug:
- cherrypy.log('routing %r to login_screen' % path, 'TOOLS.SESSAUTH')
+ cherrypy.log('routing %r to login_screen' %
+ path, 'TOOLS.SESSAUTH')
return self.login_screen(**request.params)
elif path.endswith('do_login'):
if request.method != 'POST':
@@ -386,11 +397,13 @@ Message: %(error_msg)s
response.headers['Allow'] = "POST"
raise cherrypy.HTTPError(405)
if self.debug:
- cherrypy.log('routing %r to do_logout' % path, 'TOOLS.SESSAUTH')
+ cherrypy.log('routing %r to do_logout' %
+ path, 'TOOLS.SESSAUTH')
return self.do_logout(**request.params)
else:
if self.debug:
- cherrypy.log('No special path, running do_check', 'TOOLS.SESSAUTH')
+ cherrypy.log('No special path, running do_check',
+ 'TOOLS.SESSAUTH')
return self.do_check()
@@ -412,15 +425,17 @@ def log_traceback(severity=logging.ERROR, debug=False):
"""Write the last error's traceback to the cherrypy error log."""
cherrypy.log("", "HTTP", severity=severity, traceback=True)
+
def log_request_headers(debug=False):
"""Write request headers to the cherrypy error log."""
h = [" %s: %s" % (k, v) for k, v in cherrypy.serving.request.header_list]
cherrypy.log('\nRequest Headers:\n' + '\n'.join(h), "HTTP")
+
def log_hooks(debug=False):
"""Write request.hooks to the cherrypy error log."""
request = cherrypy.serving.request
-
+
msg = []
# Sort by the standard points if possible.
from cherrypy import _cprequest
@@ -428,7 +443,7 @@ def log_hooks(debug=False):
for k in request.hooks.keys():
if k not in points:
points.append(k)
-
+
for k in points:
msg.append(" %s:" % k)
v = request.hooks.get(k, [])
@@ -438,6 +453,7 @@ def log_hooks(debug=False):
cherrypy.log('\nRequest Hooks for ' + cherrypy.url() +
':\n' + '\n'.join(msg), "HTTP")
+
def redirect(url='', internal=True, debug=False):
"""Raise InternalRedirect or HTTPRedirect to the given url."""
if debug:
@@ -449,11 +465,12 @@ def redirect(url='', internal=True, debug=False):
else:
raise cherrypy.HTTPRedirect(url)
+
def trailing_slash(missing=True, extra=False, status=None, debug=False):
"""Redirect if path_info has (missing|extra) trailing slash."""
request = cherrypy.serving.request
pi = request.path_info
-
+
if debug:
cherrypy.log('is_index: %r, missing: %r, extra: %r, path_info: %r' %
(request.is_index, missing, extra, pi),
@@ -470,13 +487,15 @@ def trailing_slash(missing=True, extra=False, status=None, debug=False):
new_url = cherrypy.url(pi[:-1], request.query_string)
raise cherrypy.HTTPRedirect(new_url, status=status or 301)
+
def flatten(debug=False):
"""Wrap response.body in a generator that recursively iterates over body.
-
+
This allows cherrypy.response.body to consist of 'nested generators';
that is, a set of generators that yield generators.
"""
import types
+
def flattener(input):
numchunks = 0
for x in input:
@@ -495,9 +514,9 @@ def flatten(debug=False):
def accept(media=None, debug=False):
"""Return the client's preferred media-type (from the given Content-Types).
-
+
If 'media' is None (the default), no test will be performed.
-
+
If 'media' is provided, it should be the Content-Type value (as a string)
or values (as a list or tuple of strings) which the current resource
can emit. The client's acceptable media ranges (as declared in the
@@ -505,16 +524,16 @@ def accept(media=None, debug=False):
values; the first such string is returned. That is, the return value
will always be one of the strings provided in the 'media' arg (or None
if 'media' is None).
-
+
If no match is found, then HTTPError 406 (Not Acceptable) is raised.
Note that most web browsers send */* as a (low-quality) acceptable
media range, which should match any Content-Type. In addition, "...if
no Accept header field is present, then it is assumed that the client
accepts all media types."
-
+
Matching types are checked in order of client preference first,
and then in the order of the given 'media' values.
-
+
Note that this function does not honor accept-params (other than "q").
"""
if not media:
@@ -522,7 +541,7 @@ def accept(media=None, debug=False):
if isinstance(media, basestring):
media = [media]
request = cherrypy.serving.request
-
+
# Parse the Accept request header, and try to match one
# of the requested media-ranges (in order of preference).
ranges = request.headers.elements('Accept')
@@ -556,7 +575,7 @@ def accept(media=None, debug=False):
cherrypy.log('Match due to %s' % element.value,
'TOOLS.ACCEPT')
return element.value
-
+
# No suitable media-range found.
ah = request.headers.get('Accept')
if ah is None:
@@ -569,22 +588,22 @@ def accept(media=None, debug=False):
class MonitoredHeaderMap(_httputil.HeaderMap):
-
+
def __init__(self):
self.accessed_headers = set()
-
+
def __getitem__(self, key):
self.accessed_headers.add(key)
return _httputil.HeaderMap.__getitem__(self, key)
-
+
def __contains__(self, key):
self.accessed_headers.add(key)
return _httputil.HeaderMap.__contains__(self, key)
-
+
def get(self, key, default=None):
self.accessed_headers.add(key)
return _httputil.HeaderMap.get(self, key, default=default)
-
+
if hasattr({}, 'has_key'):
# Python 2
def has_key(self, key):
@@ -593,25 +612,26 @@ class MonitoredHeaderMap(_httputil.HeaderMap):
def autovary(ignore=None, debug=False):
- """Auto-populate the Vary response header based on request.header access."""
+ """Auto-populate the Vary response header based on request.header access.
+ """
request = cherrypy.serving.request
-
+
req_h = request.headers
request.headers = MonitoredHeaderMap()
request.headers.update(req_h)
if ignore is None:
ignore = set(['Content-Disposition', 'Content-Length', 'Content-Type'])
-
+
def set_response_header():
resp_h = cherrypy.serving.response.headers
v = set([e.value for e in resp_h.elements('Vary')])
if debug:
- cherrypy.log('Accessed headers: %s' % request.headers.accessed_headers,
- 'TOOLS.AUTOVARY')
+ cherrypy.log(
+ 'Accessed headers: %s' % request.headers.accessed_headers,
+ 'TOOLS.AUTOVARY')
v = v.union(request.headers.accessed_headers)
v = v.difference(ignore)
v = list(v)
v.sort()
resp_h['Vary'] = ', '.join(v)
request.hooks.attach('before_finalize', set_response_header, 95)
-
diff --git a/cherrypy/lib/encoding.py b/cherrypy/lib/encoding.py
index 64597465..59ae3b15 100644
--- a/cherrypy/lib/encoding.py
+++ b/cherrypy/lib/encoding.py
@@ -9,19 +9,19 @@ from cherrypy.lib import set_vary_header
def decode(encoding=None, default_encoding='utf-8'):
"""Replace or extend the list of charsets used to decode a request entity.
-
+
Either argument may be a single string or a list of strings.
-
+
encoding
If not None, restricts the set of charsets attempted while decoding
- a request entity to the given set (even if a different charset is given in
- the Content-Type request header).
-
+ a request entity to the given set (even if a different charset is
+ given in the Content-Type request header).
+
default_encoding
Only in effect if the 'encoding' argument is not given.
- If given, the set of charsets attempted while decoding a request entity is
- *extended* with the given value(s).
-
+ If given, the set of charsets attempted while decoding a request
+ entity is *extended* with the given value(s).
+
"""
body = cherrypy.request.body
if encoding is not None:
@@ -35,7 +35,7 @@ def decode(encoding=None, default_encoding='utf-8'):
class ResponseEncoder:
-
+
default_encoding = 'utf-8'
failmsg = "Response body could not be encoded with %r."
encoding = None
@@ -43,11 +43,11 @@ class ResponseEncoder:
text_only = True
add_charset = True
debug = False
-
+
def __init__(self, **kwargs):
for k, v in kwargs.items():
setattr(self, k, v)
-
+
self.attempted_charsets = set()
request = cherrypy.serving.request
if request.handler is not None:
@@ -56,17 +56,17 @@ class ResponseEncoder:
cherrypy.log('Replacing request.handler', 'TOOLS.ENCODE')
self.oldhandler = request.handler
request.handler = self
-
+
def encode_stream(self, encoding):
"""Encode a streaming response body.
-
+
Use a generator wrapper, and just pray it works as the stream is
being written out.
"""
if encoding in self.attempted_charsets:
return False
self.attempted_charsets.add(encoding)
-
+
def encoder(body):
for chunk in body:
if isinstance(chunk, unicodestr):
@@ -74,31 +74,30 @@ class ResponseEncoder:
yield chunk
self.body = encoder(self.body)
return True
-
+
def encode_string(self, encoding):
"""Encode a buffered response body."""
if encoding in self.attempted_charsets:
return False
self.attempted_charsets.add(encoding)
-
- try:
- body = []
- for chunk in self.body:
- if isinstance(chunk, unicodestr):
+ body = []
+ for chunk in self.body:
+ if isinstance(chunk, unicodestr):
+ try:
chunk = chunk.encode(encoding, self.errors)
- body.append(chunk)
- self.body = body
- except (LookupError, UnicodeError):
- return False
- else:
- return True
-
+ except (LookupError, UnicodeError):
+ return False
+ body.append(chunk)
+ self.body = body
+ return True
+
def find_acceptable_charset(self):
request = cherrypy.serving.request
response = cherrypy.serving.response
-
+
if self.debug:
- cherrypy.log('response.stream %r' % response.stream, 'TOOLS.ENCODE')
+ cherrypy.log('response.stream %r' %
+ response.stream, 'TOOLS.ENCODE')
if response.stream:
encoder = self.encode_stream
else:
@@ -115,22 +114,24 @@ class ResponseEncoder:
# >>> len(t.encode("utf7"))
# 8
del response.headers["Content-Length"]
-
+
# Parse the Accept-Charset request header, and try to provide one
# of the requested charsets (in order of user preference).
encs = request.headers.elements('Accept-Charset')
charsets = [enc.value.lower() for enc in encs]
if self.debug:
cherrypy.log('charsets %s' % repr(charsets), 'TOOLS.ENCODE')
-
+
if self.encoding is not None:
# If specified, force this encoding to be used, or fail.
encoding = self.encoding.lower()
if self.debug:
- cherrypy.log('Specified encoding %r' % encoding, 'TOOLS.ENCODE')
+ cherrypy.log('Specified encoding %r' %
+ encoding, 'TOOLS.ENCODE')
if (not charsets) or "*" in charsets or encoding in charsets:
if self.debug:
- cherrypy.log('Attempting encoding %r' % encoding, 'TOOLS.ENCODE')
+ cherrypy.log('Attempting encoding %r' %
+ encoding, 'TOOLS.ENCODE')
if encoder(encoding):
return encoding
else:
@@ -142,7 +143,8 @@ class ResponseEncoder:
if encoder(self.default_encoding):
return self.default_encoding
else:
- raise cherrypy.HTTPError(500, self.failmsg % self.default_encoding)
+ raise cherrypy.HTTPError(500, self.failmsg %
+ self.default_encoding)
else:
for element in encs:
if element.qvalue > 0:
@@ -160,7 +162,7 @@ class ResponseEncoder:
'0)' % element, 'TOOLS.ENCODE')
if encoder(encoding):
return encoding
-
+
if "*" not in charsets:
# If no "*" is present in an Accept-Charset field, then all
# character sets not explicitly mentioned get a quality
@@ -173,20 +175,21 @@ class ResponseEncoder:
'TOOLS.ENCODE')
if encoder(iso):
return iso
-
+
# No suitable encoding found.
ac = request.headers.get('Accept-Charset')
if ac is None:
msg = "Your client did not send an Accept-Charset header."
else:
msg = "Your client sent this Accept-Charset header: %s." % ac
- msg += " We tried these charsets: %s." % ", ".join(self.attempted_charsets)
+ _charsets = ", ".join(sorted(self.attempted_charsets))
+ msg += " We tried these charsets: %s." % (_charsets,)
raise cherrypy.HTTPError(406, msg)
-
+
def __call__(self, *args, **kwargs):
response = cherrypy.serving.response
self.body = self.oldhandler(*args, **kwargs)
-
+
if isinstance(self.body, basestring):
# strings get wrapped in a list because iterating over a single
# item list is much faster than iterating over every character
@@ -200,46 +203,49 @@ class ResponseEncoder:
self.body = file_generator(self.body)
elif self.body is None:
self.body = []
-
+
ct = response.headers.elements("Content-Type")
if self.debug:
- cherrypy.log('Content-Type: %r' % [str(h) for h in ct], 'TOOLS.ENCODE')
- if ct:
+ cherrypy.log('Content-Type: %r' % [str(h)
+ for h in ct], 'TOOLS.ENCODE')
+ if ct and self.add_charset:
ct = ct[0]
if self.text_only:
if ct.value.lower().startswith("text/"):
if self.debug:
- cherrypy.log('Content-Type %s starts with "text/"' % ct,
- 'TOOLS.ENCODE')
+ cherrypy.log(
+ 'Content-Type %s starts with "text/"' % ct,
+ 'TOOLS.ENCODE')
do_find = True
else:
if self.debug:
- cherrypy.log('Not finding because Content-Type %s does '
- 'not start with "text/"' % ct,
+ cherrypy.log('Not finding because Content-Type %s '
+ 'does not start with "text/"' % ct,
'TOOLS.ENCODE')
do_find = False
else:
if self.debug:
- cherrypy.log('Finding because not text_only', 'TOOLS.ENCODE')
+ cherrypy.log('Finding because not text_only',
+ 'TOOLS.ENCODE')
do_find = True
-
+
if do_find:
# Set "charset=..." param on response Content-Type header
ct.params['charset'] = self.find_acceptable_charset()
- if self.add_charset:
- if self.debug:
- cherrypy.log('Setting Content-Type %s' % ct,
- 'TOOLS.ENCODE')
- response.headers["Content-Type"] = str(ct)
-
+ if self.debug:
+ cherrypy.log('Setting Content-Type %s' % ct,
+ 'TOOLS.ENCODE')
+ response.headers["Content-Type"] = str(ct)
+
return self.body
# GZIP
+
def compress(body, compress_level):
"""Compress 'body' at the given compress_level."""
import zlib
-
+
# See http://www.gzip.org/zlib/rfc-gzip.html
yield ntob('\x1f\x8b') # ID1 and ID2: gzip marker
yield ntob('\x08') # CM: compression method
@@ -248,7 +254,7 @@ def compress(body, compress_level):
yield struct.pack("<L", int(time.time()) & int('FFFFFFFF', 16))
yield ntob('\x02') # XFL: max compression, slowest algo
yield ntob('\xff') # OS: unknown
-
+
crc = zlib.crc32(ntob(""))
size = 0
zobj = zlib.compressobj(compress_level,
@@ -259,15 +265,16 @@ def compress(body, compress_level):
crc = zlib.crc32(line, crc)
yield zobj.compress(line)
yield zobj.flush()
-
+
# CRC32: 4 bytes
yield struct.pack("<L", crc & int('FFFFFFFF', 16))
# ISIZE: 4 bytes
yield struct.pack("<L", size & int('FFFFFFFF', 16))
+
def decompress(body):
import gzip
-
+
zbuf = BytesIO()
zbuf.write(body)
zbuf.seek(0)
@@ -277,9 +284,10 @@ def decompress(body):
return data
-def gzip(compress_level=5, mime_types=['text/html', 'text/plain'], debug=False):
+def gzip(compress_level=5, mime_types=['text/html', 'text/plain'],
+ debug=False):
"""Try to gzip the response body if Content-Type in mime_types.
-
+
cherrypy.response.headers['Content-Type'] must be set to one of the
values in the mime_types arg before calling this function.
@@ -287,32 +295,32 @@ def gzip(compress_level=5, mime_types=['text/html', 'text/plain'], debug=False):
* type/subtype
* type/*
* type/*+subtype
-
+
No compression is performed if any of the following hold:
* The client sends no Accept-Encoding request header
* No 'gzip' or 'x-gzip' is present in the Accept-Encoding header
* No 'gzip' or 'x-gzip' with a qvalue > 0 is present
* The 'identity' value is given with a qvalue > 0.
-
+
"""
request = cherrypy.serving.request
response = cherrypy.serving.response
-
+
set_vary_header(response, "Accept-Encoding")
-
+
if not response.body:
# Response body is empty (might be a 304 for instance)
if debug:
cherrypy.log('No response body', context='TOOLS.GZIP')
return
-
+
# If returning cached content (which should already have been gzipped),
# don't re-zip.
if getattr(request, "cached", False):
if debug:
cherrypy.log('Not gzipping cached response', context='TOOLS.GZIP')
return
-
+
acceptable = request.headers.elements('Accept-Encoding')
if not acceptable:
# If no Accept-Encoding field is present in a request,
@@ -325,7 +333,7 @@ def gzip(compress_level=5, mime_types=['text/html', 'text/plain'], debug=False):
if debug:
cherrypy.log('No Accept-Encoding', context='TOOLS.GZIP')
return
-
+
ct = response.headers.get('Content-Type', '').split(';')[0]
for coding in acceptable:
if coding.value == 'identity' and coding.qvalue != 0:
@@ -339,7 +347,7 @@ def gzip(compress_level=5, mime_types=['text/html', 'text/plain'], debug=False):
cherrypy.log('Zero gzip qvalue: %s' % coding,
context='TOOLS.GZIP')
return
-
+
if ct not in mime_types:
# If the list of provided mime-types contains tokens
# such as 'text/*' or 'application/*+xml',
@@ -370,7 +378,7 @@ def gzip(compress_level=5, mime_types=['text/html', 'text/plain'], debug=False):
cherrypy.log('Content-Type %s not in mime_types %r' %
(ct, mime_types), context='TOOLS.GZIP')
return
-
+
if debug:
cherrypy.log('Gzipping', context='TOOLS.GZIP')
# Return a generator that compresses the page
@@ -379,10 +387,9 @@ def gzip(compress_level=5, mime_types=['text/html', 'text/plain'], debug=False):
if "Content-Length" in response.headers:
# Delete Content-Length header so finalize() recalcs it.
del response.headers["Content-Length"]
-
+
return
-
+
if debug:
cherrypy.log('No acceptable encoding found.', context='GZIP')
cherrypy.HTTPError(406, "identity, gzip").set_response()
-
diff --git a/cherrypy/lib/gctools.py b/cherrypy/lib/gctools.py
index 183148b2..4b616c59 100644
--- a/cherrypy/lib/gctools.py
+++ b/cherrypy/lib/gctools.py
@@ -3,7 +3,7 @@ import inspect
import os
import sys
import time
-
+
try:
import objgraph
except ImportError:
@@ -15,6 +15,7 @@ from cherrypy.process.plugins import SimplePlugin
class ReferrerTree(object):
+
"""An object which gathers all referrers of an object to a given depth."""
peek_length = 40
@@ -89,6 +90,7 @@ class ReferrerTree(object):
def format(self, tree):
"""Return a list of string reprs from a nested list of referrers."""
output = []
+
def ascend(branch, depth=1):
for parent, grandparents in branch:
output.append((" " * depth) + self._format(parent))
@@ -103,15 +105,15 @@ def get_instances(cls):
class RequestCounter(SimplePlugin):
-
+
def start(self):
self.count = 0
-
+
def before_request(self):
self.count += 1
-
+
def after_request(self):
- self.count -=1
+ self.count -= 1
request_counter = RequestCounter(cherrypy.engine)
request_counter.subscribe()
@@ -129,15 +131,17 @@ def get_context(obj):
class GCRoot(object):
+
"""A CherryPy page handler for testing reference leaks."""
- classes = [(_cprequest.Request, 2, 2,
- "Should be 1 in this request thread and 1 in the main thread."),
- (_cprequest.Response, 2, 2,
- "Should be 1 in this request thread and 1 in the main thread."),
- (_cpwsgi.AppResponse, 1, 1,
- "Should be 1 in this request thread only."),
- ]
+ classes = [
+ (_cprequest.Request, 2, 2,
+ "Should be 1 in this request thread and 1 in the main thread."),
+ (_cprequest.Response, 2, 2,
+ "Should be 1 in this request thread and 1 in the main thread."),
+ (_cpwsgi.AppResponse, 1, 1,
+ "Should be 1 in this request thread only."),
+ ]
def index(self):
return "Hello, world!"
@@ -145,14 +149,14 @@ class GCRoot(object):
def stats(self):
output = ["Statistics:"]
-
+
for trial in range(10):
if request_counter.count > 0:
break
time.sleep(0.5)
else:
output.append("\nNot all requests closed properly.")
-
+
# gc_collect isn't perfectly synchronous, because it may
# break reference cycles that then take time to fully
# finalize. Call it thrice and hope for the best.
@@ -208,7 +212,6 @@ class GCRoot(object):
t = ReferrerTree(ignore=[objs], maxdepth=3)
tree = t.ascend(obj)
output.extend(t.format(tree))
-
+
return "\n".join(output)
stats.exposed = True
-
diff --git a/cherrypy/lib/http.py b/cherrypy/lib/http.py
index 4661d69e..12043ad1 100644
--- a/cherrypy/lib/http.py
+++ b/cherrypy/lib/http.py
@@ -4,4 +4,3 @@ warnings.warn('cherrypy.lib.http has been deprecated and will be removed '
DeprecationWarning)
from cherrypy.lib.httputil import *
-
diff --git a/cherrypy/lib/httpauth.py b/cherrypy/lib/httpauth.py
index ad7c6eba..66d254e6 100644
--- a/cherrypy/lib/httpauth.py
+++ b/cherrypy/lib/httpauth.py
@@ -1,5 +1,6 @@
"""
-This module defines functions to implement HTTP Digest Authentication (:rfc:`2617`).
+This module defines functions to implement HTTP Digest Authentication
+(:rfc:`2617`).
This has full compliance with 'Digest' and 'Basic' authentication methods. In
'Digest' it supports both MD5 and MD5-sess algorithms.
@@ -7,13 +8,14 @@ Usage:
First use 'doAuth' to request the client authentication for a
certain resource. You should send an httplib.UNAUTHORIZED response to the
client so he knows he has to authenticate itself.
-
+
Then use 'parseAuthorization' to retrieve the 'auth_map' used in
'checkResponse'.
- To use 'checkResponse' you must have already verified the password associated
- with the 'username' key in 'auth_map' dict. Then you use the 'checkResponse'
- function to verify if the password matches the one sent by the client.
+ To use 'checkResponse' you must have already verified the password
+ associated with the 'username' key in 'auth_map' dict. Then you use the
+ 'checkResponse' function to verify if the password matches the one sent
+ by the client.
SUPPORTED_ALGORITHM - list of supported 'Digest' algorithms
SUPPORTED_QOP - list of supported 'Digest' 'qop'.
@@ -21,7 +23,8 @@ SUPPORTED_QOP - list of supported 'Digest' 'qop'.
__version__ = 1, 0, 1
__author__ = "Tiago Cogumbreiro <cogumbreiro@users.sf.net>"
__credits__ = """
- Peter van Kampen for its recipe which implement most of Digest authentication:
+ Peter van Kampen for its recipe which implement most of Digest
+ authentication:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/302378
"""
@@ -29,27 +32,27 @@ __license__ = """
Copyright (c) 2005, Tiago Cogumbreiro <cogumbreiro@users.sf.net>
All rights reserved.
-Redistribution and use in source and binary forms, with or without modification,
-are permitted provided that the following conditions are met:
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
- * Redistributions of source code must retain the above copyright notice,
+ * Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright notice,
- this list of conditions and the following disclaimer in the documentation
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
- * Neither the name of Sylvain Hellegouarch nor the names of his contributors
- may be used to endorse or promote products derived from this software
- without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
-ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
-DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
-FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
-SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
-CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
-OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * Neither the name of Sylvain Hellegouarch nor the names of his
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
@@ -57,7 +60,7 @@ __all__ = ("digestAuth", "basicAuth", "doAuth", "checkResponse",
"parseAuthorization", "SUPPORTED_ALGORITHM", "md5SessionKey",
"calculateNonce", "SUPPORTED_QOP")
-################################################################################
+##########################################################################
import time
from cherrypy._cpcompat import base64_decode, ntob, md5
from cherrypy._cpcompat import parse_http_list, parse_keqv_list
@@ -70,16 +73,17 @@ AUTH_INT = "auth-int"
SUPPORTED_ALGORITHM = (MD5, MD5_SESS)
SUPPORTED_QOP = (AUTH, AUTH_INT)
-################################################################################
+##########################################################################
# doAuth
#
DIGEST_AUTH_ENCODERS = {
MD5: lambda val: md5(ntob(val)).hexdigest(),
MD5_SESS: lambda val: md5(ntob(val)).hexdigest(),
-# SHA: lambda val: sha.new(ntob(val)).hexdigest (),
+ # SHA: lambda val: sha.new(ntob(val)).hexdigest (),
}
-def calculateNonce (realm, algorithm = MD5):
+
+def calculateNonce(realm, algorithm=MD5):
"""This is an auxaliary function that calculates 'nonce' value. It is used
to handle sessions."""
@@ -89,44 +93,47 @@ def calculateNonce (realm, algorithm = MD5):
try:
encoder = DIGEST_AUTH_ENCODERS[algorithm]
except KeyError:
- raise NotImplementedError ("The chosen algorithm (%s) does not have "\
- "an implementation yet" % algorithm)
+ raise NotImplementedError("The chosen algorithm (%s) does not have "
+ "an implementation yet" % algorithm)
+
+ return encoder("%d:%s" % (time.time(), realm))
- return encoder ("%d:%s" % (time.time(), realm))
-def digestAuth (realm, algorithm = MD5, nonce = None, qop = AUTH):
+def digestAuth(realm, algorithm=MD5, nonce=None, qop=AUTH):
"""Challenges the client for a Digest authentication."""
global SUPPORTED_ALGORITHM, DIGEST_AUTH_ENCODERS, SUPPORTED_QOP
assert algorithm in SUPPORTED_ALGORITHM
assert qop in SUPPORTED_QOP
if nonce is None:
- nonce = calculateNonce (realm, algorithm)
+ nonce = calculateNonce(realm, algorithm)
return 'Digest realm="%s", nonce="%s", algorithm="%s", qop="%s"' % (
realm, nonce, algorithm, qop
)
-def basicAuth (realm):
+
+def basicAuth(realm):
"""Challengenes the client for a Basic authentication."""
assert '"' not in realm, "Realms cannot contain the \" (quote) character."
return 'Basic realm="%s"' % realm
-def doAuth (realm):
+
+def doAuth(realm):
"""'doAuth' function returns the challenge string b giving priority over
Digest and fallback to Basic authentication when the browser doesn't
support the first one.
-
+
This should be set in the HTTP header under the key 'WWW-Authenticate'."""
- return digestAuth (realm) + " " + basicAuth (realm)
+ return digestAuth(realm) + " " + basicAuth(realm)
-################################################################################
+##########################################################################
# Parse authorization parameters
#
-def _parseDigestAuthorization (auth_params):
+def _parseDigestAuthorization(auth_params):
# Convert the auth params to a dict
items = parse_http_list(auth_params)
params = parse_keqv_list(items)
@@ -140,8 +147,8 @@ def _parseDigestAuthorization (auth_params):
return None
# If qop is sent then cnonce and nc MUST be present
- if "qop" in params and not ("cnonce" in params \
- and "nc" in params):
+ if "qop" in params and not ("cnonce" in params
+ and "nc" in params):
return None
# If qop is not sent, neither cnonce nor nc can be present
@@ -152,7 +159,7 @@ def _parseDigestAuthorization (auth_params):
return params
-def _parseBasicAuthorization (auth_params):
+def _parseBasicAuthorization(auth_params):
username, password = base64_decode(auth_params).split(":", 1)
return {"username": username, "password": password}
@@ -161,18 +168,19 @@ AUTH_SCHEMES = {
"digest": _parseDigestAuthorization,
}
-def parseAuthorization (credentials):
+
+def parseAuthorization(credentials):
"""parseAuthorization will convert the value of the 'Authorization' key in
the HTTP header to a map itself. If the parsing fails 'None' is returned.
"""
global AUTH_SCHEMES
- auth_scheme, auth_params = credentials.split(" ", 1)
- auth_scheme = auth_scheme.lower ()
+ auth_scheme, auth_params = credentials.split(" ", 1)
+ auth_scheme = auth_scheme.lower()
parser = AUTH_SCHEMES[auth_scheme]
- params = parser (auth_params)
+ params = parser(auth_params)
if params is None:
return
@@ -182,12 +190,12 @@ def parseAuthorization (credentials):
return params
-################################################################################
+##########################################################################
# Check provided response for a valid password
#
-def md5SessionKey (params, password):
+def md5SessionKey(params, password):
"""
- If the "algorithm" directive's value is "MD5-sess", then A1
+ If the "algorithm" directive's value is "MD5-sess", then A1
[the session key] is calculated only once - on the first request by the
client following receipt of a WWW-Authenticate challenge from the server.
@@ -210,10 +218,11 @@ def md5SessionKey (params, password):
params_copy[key] = params[key]
params_copy["algorithm"] = MD5_SESS
- return _A1 (params_copy, password)
+ return _A1(params_copy, password)
+
def _A1(params, password):
- algorithm = params.get ("algorithm", MD5)
+ algorithm = params.get("algorithm", MD5)
H = DIGEST_AUTH_ENCODERS[algorithm]
if algorithm == MD5:
@@ -227,7 +236,7 @@ def _A1(params, password):
# This is A1 if qop is set
# A1 = H( unq(username-value) ":" unq(realm-value) ":" passwd )
# ":" unq(nonce-value) ":" unq(cnonce-value)
- h_a1 = H ("%s:%s:%s" % (params["username"], params["realm"], password))
+ h_a1 = H("%s:%s:%s" % (params["username"], params["realm"], password))
return "%s:%s:%s" % (h_a1, params["nonce"], params["cnonce"])
@@ -235,13 +244,13 @@ def _A2(params, method, kwargs):
# If the "qop" directive's value is "auth" or is unspecified, then A2 is:
# A2 = Method ":" digest-uri-value
- qop = params.get ("qop", "auth")
+ qop = params.get("qop", "auth")
if qop == "auth":
return method + ":" + params["uri"]
elif qop == "auth-int":
# If the "qop" value is "auth-int", then A2 is:
# A2 = Method ":" digest-uri-value ":" H(entity-body)
- entity_body = kwargs.get ("entity_body", "")
+ entity_body = kwargs.get("entity_body", "")
H = kwargs["H"]
return "%s:%s:%s" % (
@@ -251,20 +260,22 @@ def _A2(params, method, kwargs):
)
else:
- raise NotImplementedError ("The 'qop' method is unknown: %s" % qop)
+ raise NotImplementedError("The 'qop' method is unknown: %s" % qop)
-def _computeDigestResponse(auth_map, password, method = "GET", A1 = None,**kwargs):
+
+def _computeDigestResponse(auth_map, password, method="GET", A1=None,
+ **kwargs):
"""
Generates a response respecting the algorithm defined in RFC 2617
"""
params = auth_map
- algorithm = params.get ("algorithm", MD5)
+ algorithm = params.get("algorithm", MD5)
H = DIGEST_AUTH_ENCODERS[algorithm]
KD = lambda secret, data: H(secret + ":" + data)
- qop = params.get ("qop", None)
+ qop = params.get("qop", None)
H_A2 = H(_A2(params, method, kwargs))
@@ -297,7 +308,8 @@ def _computeDigestResponse(auth_map, password, method = "GET", A1 = None,**kwarg
return KD(H_A1, request)
-def _checkDigestResponse(auth_map, password, method = "GET", A1 = None, **kwargs):
+
+def _checkDigestResponse(auth_map, password, method="GET", A1=None, **kwargs):
"""This function is used to verify the response given by the client when
he tries to authenticate.
Optional arguments:
@@ -312,11 +324,14 @@ def _checkDigestResponse(auth_map, password, method = "GET", A1 = None, **kwargs
if auth_map['realm'] != kwargs.get('realm', None):
return False
- response = _computeDigestResponse(auth_map, password, method, A1,**kwargs)
+ response = _computeDigestResponse(
+ auth_map, password, method, A1, **kwargs)
return response == auth_map["response"]
-def _checkBasicResponse (auth_map, password, method='GET', encrypt=None, **kwargs):
+
+def _checkBasicResponse(auth_map, password, method='GET', encrypt=None,
+ **kwargs):
# Note that the Basic response doesn't provide the realm value so we cannot
# test it
try:
@@ -329,26 +344,24 @@ AUTH_RESPONSES = {
"digest": _checkDigestResponse,
}
-def checkResponse (auth_map, password, method = "GET", encrypt=None, **kwargs):
+
+def checkResponse(auth_map, password, method="GET", encrypt=None, **kwargs):
"""'checkResponse' compares the auth_map with the password and optionally
other arguments that each implementation might need.
-
+
If the response is of type 'Basic' then the function has the following
signature::
-
- checkBasicResponse (auth_map, password) -> bool
-
+
+ checkBasicResponse(auth_map, password) -> bool
+
If the response is of type 'Digest' then the function has the following
signature::
-
- checkDigestResponse (auth_map, password, method = 'GET', A1 = None) -> bool
-
+
+ checkDigestResponse(auth_map, password, method='GET', A1=None) -> bool
+
The 'A1' argument is only used in MD5_SESS algorithm based responses.
Check md5SessionKey() for more info.
"""
checker = AUTH_RESPONSES[auth_map["auth_scheme"]]
- return checker (auth_map, password, method=method, encrypt=encrypt, **kwargs)
-
-
-
-
+ return checker(auth_map, password, method=method, encrypt=encrypt,
+ **kwargs)
diff --git a/cherrypy/lib/httputil.py b/cherrypy/lib/httputil.py
index 5f77d547..585a4824 100644
--- a/cherrypy/lib/httputil.py
+++ b/cherrypy/lib/httputil.py
@@ -8,27 +8,27 @@ to a public caning.
"""
from binascii import b2a_base64
-from cherrypy._cpcompat import BaseHTTPRequestHandler, HTTPDate, ntob, ntou, reversed, sorted
-from cherrypy._cpcompat import basestring, bytestr, iteritems, nativestr, unicodestr, unquote_qs
+from cherrypy._cpcompat import BaseHTTPRequestHandler, HTTPDate, ntob, ntou
+from cherrypy._cpcompat import basestring, bytestr, iteritems, nativestr
+from cherrypy._cpcompat import reversed, sorted, unicodestr, unquote_qs
response_codes = BaseHTTPRequestHandler.responses.copy()
-# From http://www.cherrypy.org/ticket/361
+# From https://bitbucket.org/cherrypy/cherrypy/issue/361
response_codes[500] = ('Internal Server Error',
- 'The server encountered an unexpected condition '
- 'which prevented it from fulfilling the request.')
+ 'The server encountered an unexpected condition '
+ 'which prevented it from fulfilling the request.')
response_codes[503] = ('Service Unavailable',
- 'The server is currently unable to handle the '
- 'request due to a temporary overloading or '
- 'maintenance of the server.')
+ 'The server is currently unable to handle the '
+ 'request due to a temporary overloading or '
+ 'maintenance of the server.')
import re
import urllib
-
def urljoin(*atoms):
"""Return the given path \*atoms, joined into a single URL.
-
+
This will correctly join a SCRIPT_NAME and PATH_INFO into the
original URL, even if either atom is blank.
"""
@@ -38,9 +38,10 @@ def urljoin(*atoms):
# Special-case the final url of "", and return "/" instead.
return url or "/"
+
def urljoin_bytes(*atoms):
"""Return the given path *atoms, joined into a single URL.
-
+
This will correctly join a SCRIPT_NAME and PATH_INFO into the
original URL, even if either atom is blank.
"""
@@ -50,24 +51,26 @@ def urljoin_bytes(*atoms):
# Special-case the final url of "", and return "/" instead.
return url or ntob("/")
+
def protocol_from_http(protocol_str):
"""Return a protocol tuple from the given 'HTTP/x.y' string."""
return int(protocol_str[5]), int(protocol_str[7])
+
def get_ranges(headervalue, content_length):
"""Return a list of (start, stop) indices from a Range header, or None.
-
+
Each (start, stop) tuple will be composed of two ints, which are suitable
for use in a slicing operation. That is, the header "Range: bytes=3-6",
if applied against a Python string, is requesting resource[3:7]. This
function will return the list [(3, 7)].
-
+
If this function returns an empty list, you should return HTTP 416.
"""
-
+
if not headervalue:
return None
-
+
result = []
bytesunit, byteranges = headervalue.split("=", 1)
for brange in byteranges.split(","):
@@ -101,35 +104,36 @@ def get_ranges(headervalue, content_length):
return None
# Negative subscript (last N bytes)
result.append((content_length - int(stop), content_length))
-
+
return result
class HeaderElement(object):
+
"""An element (with parameters) from an HTTP header's element list."""
-
+
def __init__(self, value, params=None):
self.value = value
if params is None:
params = {}
self.params = params
-
+
def __cmp__(self, other):
return cmp(self.value, other.value)
-
+
def __lt__(self, other):
return self.value < other.value
-
+
def __str__(self):
p = [";%s=%s" % (k, v) for k, v in iteritems(self.params)]
- return "%s%s" % (self.value, "".join(p))
-
+ return str("%s%s" % (self.value, "".join(p)))
+
def __bytes__(self):
return ntob(self.__str__())
def __unicode__(self):
return ntou(self.__str__())
-
+
def parse(elementstr):
"""Transform 'token;key=val' to ('token', {'key': 'val'})."""
# Split the element into a value and parameters. The 'value' may
@@ -150,7 +154,7 @@ class HeaderElement(object):
params[key] = val
return initial_value, params
parse = staticmethod(parse)
-
+
def from_str(cls, elementstr):
"""Construct an instance from a string of the form 'token;key=val'."""
ival, params = cls.parse(elementstr)
@@ -160,16 +164,18 @@ class HeaderElement(object):
q_separator = re.compile(r'; *q *=')
+
class AcceptElement(HeaderElement):
+
"""An element (with parameters) from an Accept* header's element list.
-
+
AcceptElement objects are comparable; the more-preferred object will be
"less than" the less-preferred object. They are also therefore sortable;
if you sort a list of AcceptElement objects, they will be listed in
priority order; the most preferred value will be first. Yes, it should
have been the other way around, but it's too late to fix now.
"""
-
+
def from_str(cls, elementstr):
qvalue = None
# The first "q" parameter (if any) separates the initial
@@ -180,26 +186,26 @@ class AcceptElement(HeaderElement):
# The qvalue for an Accept header can have extensions. The other
# headers cannot, but it's easier to parse them as if they did.
qvalue = HeaderElement.from_str(atoms[0].strip())
-
+
media_type, params = cls.parse(media_range)
if qvalue is not None:
params["q"] = qvalue
return cls(media_type, params)
from_str = classmethod(from_str)
-
+
def qvalue(self):
val = self.params.get("q", "1")
if isinstance(val, HeaderElement):
val = val.value
return float(val)
qvalue = property(qvalue, doc="The qvalue, or priority, of this value.")
-
+
def __cmp__(self, other):
diff = cmp(self.qvalue, other.qvalue)
if diff == 0:
diff = cmp(str(self), str(other))
return diff
-
+
def __lt__(self, other):
if self.qvalue == other.qvalue:
return str(self) < str(other)
@@ -208,10 +214,11 @@ class AcceptElement(HeaderElement):
def header_elements(fieldname, fieldvalue):
- """Return a sorted HeaderElement list from a comma-separated header string."""
+ """Return a sorted HeaderElement list from a comma-separated header string.
+ """
if not fieldvalue:
return []
-
+
result = []
for element in fieldvalue.split(","):
if fieldname.startswith("Accept") or fieldname == 'TE':
@@ -219,9 +226,10 @@ def header_elements(fieldname, fieldvalue):
else:
hv = HeaderElement.from_str(element)
result.append(hv)
-
+
return list(reversed(sorted(result)))
+
def decode_TEXT(value):
r"""Decode :rfc:`2047` TEXT (e.g. "=?utf-8?q?f=C3=BCr?=" -> "f\xfcr")."""
try:
@@ -237,18 +245,19 @@ def decode_TEXT(value):
decodedvalue += atom
return decodedvalue
+
def valid_status(status):
"""Return legal HTTP status Code, Reason-phrase and Message.
-
+
The status arg must be an int, or a str that begins with an int.
-
+
If status is an int, or a str and no reason-phrase is supplied,
a default reason-phrase will be provided.
"""
-
+
if not status:
status = 200
-
+
status = str(status)
parts = status.split(" ", 1)
if len(parts) == 1:
@@ -258,26 +267,26 @@ def valid_status(status):
else:
code, reason = parts
reason = reason.strip()
-
+
try:
code = int(code)
except ValueError:
raise ValueError("Illegal response status from server "
"(%s is non-numeric)." % repr(code))
-
+
if code < 100 or code > 599:
raise ValueError("Illegal response status from server "
"(%s is out of range)." % repr(code))
-
+
if code not in response_codes:
# code is unknown but not illegal
default_reason, message = "", ""
else:
default_reason, message = response_codes[code]
-
+
if reason is None:
reason = default_reason
-
+
return code, reason, message
@@ -287,21 +296,21 @@ def valid_status(status):
def _parse_qs(qs, keep_blank_values=0, strict_parsing=0, encoding='utf-8'):
"""Parse a query given as a string argument.
-
+
Arguments:
-
+
qs: URL-encoded query string to be parsed
-
+
keep_blank_values: flag indicating whether blank values in
URL encoded queries should be treated as blank strings. A
true value indicates that blanks should be retained as blank
strings. The default false value indicates that blank values
are to be ignored and treated as if they were not included.
-
+
strict_parsing: flag indicating what to do with parsing errors. If
false (the default), errors are silently ignored. If true,
errors raise a ValueError exception.
-
+
Returns a dict, as G-d intended.
"""
pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')]
@@ -332,9 +341,10 @@ def _parse_qs(qs, keep_blank_values=0, strict_parsing=0, encoding='utf-8'):
image_map_pattern = re.compile(r"[0-9]+,[0-9]+")
+
def parse_query_string(query_string, keep_blank_values=True, encoding='utf-8'):
"""Build a params dictionary from a query_string.
-
+
Duplicate key/value pairs in the provided query_string will be
returned as {'key': [val1, val2, ...]}. Single key/values will
be returned as strings: {'key': 'value'}.
@@ -350,41 +360,42 @@ def parse_query_string(query_string, keep_blank_values=True, encoding='utf-8'):
class CaseInsensitiveDict(dict):
+
"""A case-insensitive dict subclass.
-
+
Each key is changed on entry to str(key).title().
"""
-
+
def __getitem__(self, key):
return dict.__getitem__(self, str(key).title())
-
+
def __setitem__(self, key, value):
dict.__setitem__(self, str(key).title(), value)
-
+
def __delitem__(self, key):
dict.__delitem__(self, str(key).title())
-
+
def __contains__(self, key):
return dict.__contains__(self, str(key).title())
-
+
def get(self, key, default=None):
return dict.get(self, str(key).title(), default)
-
+
if hasattr({}, 'has_key'):
def has_key(self, key):
- return dict.has_key(self, str(key).title())
-
+ return str(key).title() in self
+
def update(self, E):
for k in E.keys():
self[str(k).title()] = E[k]
-
+
def fromkeys(cls, seq, value=None):
newdict = cls()
for k in seq:
newdict[str(k).title()] = value
return newdict
fromkeys = classmethod(fromkeys)
-
+
def setdefault(self, key, x=None):
key = str(key).title()
try:
@@ -392,7 +403,7 @@ class CaseInsensitiveDict(dict):
except KeyError:
self[key] = x
return x
-
+
def pop(self, key, default):
return dict.pop(self, str(key).title(), default)
@@ -404,103 +415,115 @@ class CaseInsensitiveDict(dict):
# replaced with a single SP before interpretation of the TEXT value."
if nativestr == bytestr:
header_translate_table = ''.join([chr(i) for i in xrange(256)])
- header_translate_deletechars = ''.join([chr(i) for i in xrange(32)]) + chr(127)
+ header_translate_deletechars = ''.join(
+ [chr(i) for i in xrange(32)]) + chr(127)
else:
header_translate_table = None
header_translate_deletechars = bytes(range(32)) + bytes([127])
class HeaderMap(CaseInsensitiveDict):
+
"""A dict subclass for HTTP request and response headers.
-
+
Each key is changed on entry to str(key).title(). This allows headers
to be case-insensitive and avoid duplicates.
-
+
Values are header values (decoded according to :rfc:`2047` if necessary).
"""
-
- protocol=(1, 1)
+
+ protocol = (1, 1)
encodings = ["ISO-8859-1"]
-
+
# Someday, when http-bis is done, this will probably get dropped
# since few servers, clients, or intermediaries do it. But until then,
# we're going to obey the spec as is.
# "Words of *TEXT MAY contain characters from character sets other than
# ISO-8859-1 only when encoded according to the rules of RFC 2047."
use_rfc_2047 = True
-
+
def elements(self, key):
"""Return a sorted list of HeaderElements for the given header."""
key = str(key).title()
value = self.get(key)
return header_elements(key, value)
-
+
def values(self, key):
"""Return a sorted list of HeaderElement.value for the given header."""
return [e.value for e in self.elements(key)]
-
+
def output(self):
"""Transform self into a list of (name, value) tuples."""
- header_list = []
- for k, v in self.items():
+ return list(self.encode_header_items(self.items()))
+
+ def encode_header_items(cls, header_items):
+ """
+ Prepare the sequence of name, value tuples into a form suitable for
+ transmitting on the wire for HTTP.
+ """
+ for k, v in header_items:
if isinstance(k, unicodestr):
- k = self.encode(k)
-
+ k = cls.encode(k)
+
if not isinstance(v, basestring):
v = str(v)
-
+
if isinstance(v, unicodestr):
- v = self.encode(v)
-
+ v = cls.encode(v)
+
# See header_translate_* constants above.
# Replace only if you really know what you're doing.
- k = k.translate(header_translate_table, header_translate_deletechars)
- v = v.translate(header_translate_table, header_translate_deletechars)
-
- header_list.append((k, v))
- return header_list
-
- def encode(self, v):
+ k = k.translate(header_translate_table,
+ header_translate_deletechars)
+ v = v.translate(header_translate_table,
+ header_translate_deletechars)
+
+ yield (k, v)
+ encode_header_items = classmethod(encode_header_items)
+
+ def encode(cls, v):
"""Return the given header name or value, encoded for HTTP output."""
- for enc in self.encodings:
+ for enc in cls.encodings:
try:
return v.encode(enc)
except UnicodeEncodeError:
continue
-
- if self.protocol == (1, 1) and self.use_rfc_2047:
- # Encode RFC-2047 TEXT
- # (e.g. u"\u8200" -> "=?utf-8?b?6IiA?=").
+
+ if cls.protocol == (1, 1) and cls.use_rfc_2047:
+ # Encode RFC-2047 TEXT
+ # (e.g. u"\u8200" -> "=?utf-8?b?6IiA?=").
# We do our own here instead of using the email module
# because we never want to fold lines--folding has
# been deprecated by the HTTP working group.
v = b2a_base64(v.encode('utf-8'))
return (ntob('=?utf-8?b?') + v.strip(ntob('\n')) + ntob('?='))
-
+
raise ValueError("Could not encode header part %r using "
"any of the encodings %r." %
- (v, self.encodings))
+ (v, cls.encodings))
+ encode = classmethod(encode)
class Host(object):
+
"""An internet address.
-
+
name
Should be the client's host name. If not available (because no DNS
lookup is performed), the IP address should be used instead.
-
+
"""
-
+
ip = "0.0.0.0"
port = 80
name = "unknown.tld"
-
+
def __init__(self, ip, port, name=None):
self.ip = ip
self.port = port
if name is None:
name = ip
self.name = name
-
+
def __repr__(self):
return "httputil.Host(%r, %r, %r)" % (self.ip, self.port, self.name)
diff --git a/cherrypy/lib/jsontools.py b/cherrypy/lib/jsontools.py
index 20925791..1450806a 100644
--- a/cherrypy/lib/jsontools.py
+++ b/cherrypy/lib/jsontools.py
@@ -2,19 +2,21 @@ import sys
import cherrypy
from cherrypy._cpcompat import basestring, ntou, json, json_encode, json_decode
+
def json_processor(entity):
"""Read application/json data into request.json."""
if not entity.headers.get(ntou("Content-Length"), ntou("")):
raise cherrypy.HTTPError(411)
-
+
body = entity.fp.read()
try:
cherrypy.serving.request.json = json_decode(body.decode('utf-8'))
except ValueError:
raise cherrypy.HTTPError(400, 'Invalid JSON document')
+
def json_in(content_type=[ntou('application/json'), ntou('text/javascript')],
- force=True, debug=False, processor = json_processor):
+ force=True, debug=False, processor=json_processor):
"""Add a processor to parse JSON request entities:
The default processor places the parsed data into request.json.
@@ -22,11 +24,11 @@ def json_in(content_type=[ntou('application/json'), ntou('text/javascript')],
be deserialized from JSON to the Python equivalent, and the result
stored at cherrypy.request.json. The 'content_type' argument may
be a Content-Type string or a list of allowable Content-Type strings.
-
+
If the 'force' argument is True (the default), then entities of other
content types will not be allowed; "415 Unsupported Media Type" is
raised instead.
-
+
Supply your own processor to use a custom decoder, or to handle the parsed
data differently. The processor can be configured via
tools.json_in.processor or via the decorator method.
@@ -35,14 +37,14 @@ def json_in(content_type=[ntou('application/json'), ntou('text/javascript')],
request header, or it will raise "411 Length Required". If for any
other reason the request entity cannot be deserialized from JSON,
it will raise "400 Bad Request: Invalid JSON document".
-
+
You must be using Python 2.6 or greater, or have the 'simplejson'
package importable; otherwise, ValueError is raised during processing.
"""
request = cherrypy.serving.request
if isinstance(content_type, basestring):
content_type = [content_type]
-
+
if force:
if debug:
cherrypy.log('Removing body processors %s' %
@@ -51,19 +53,22 @@ def json_in(content_type=[ntou('application/json'), ntou('text/javascript')],
request.body.default_proc = cherrypy.HTTPError(
415, 'Expected an entity of content type %s' %
', '.join(content_type))
-
+
for ct in content_type:
if debug:
cherrypy.log('Adding body processor for %s' % ct, 'TOOLS.JSON_IN')
request.body.processors[ct] = processor
+
def json_handler(*args, **kwargs):
value = cherrypy.serving.request._json_inner_handler(*args, **kwargs)
return json_encode(value)
-def json_out(content_type='application/json', debug=False, handler=json_handler):
+
+def json_out(content_type='application/json', debug=False,
+ handler=json_handler):
"""Wrap request.handler to serialize its output to JSON. Sets Content-Type.
-
+
If the given content_type is None, the Content-Type response header
is not set.
@@ -75,6 +80,11 @@ def json_out(content_type='application/json', debug=False, handler=json_handler)
package importable; otherwise, ValueError is raised during processing.
"""
request = cherrypy.serving.request
+ # request.handler may be set to None by e.g. the caching tool
+ # to signal to all components that a response body has already
+ # been attached, in which case we don't need to wrap anything.
+ if request.handler is None:
+ return
if debug:
cherrypy.log('Replacing %s with JSON handler' % request.handler,
'TOOLS.JSON_OUT')
@@ -82,6 +92,6 @@ def json_out(content_type='application/json', debug=False, handler=json_handler)
request.handler = handler
if content_type is not None:
if debug:
- cherrypy.log('Setting Content-Type to %s' % content_type, 'TOOLS.JSON_OUT')
+ cherrypy.log('Setting Content-Type to %s' %
+ content_type, 'TOOLS.JSON_OUT')
cherrypy.serving.response.headers['Content-Type'] = content_type
-
diff --git a/cherrypy/lib/lockfile.py b/cherrypy/lib/lockfile.py
new file mode 100644
index 00000000..b9d8e027
--- /dev/null
+++ b/cherrypy/lib/lockfile.py
@@ -0,0 +1,147 @@
+"""
+Platform-independent file locking. Inspired by and modeled after zc.lockfile.
+"""
+
+import os
+
+try:
+ import msvcrt
+except ImportError:
+ pass
+
+try:
+ import fcntl
+except ImportError:
+ pass
+
+
+class LockError(Exception):
+
+ "Could not obtain a lock"
+
+ msg = "Unable to lock %r"
+
+ def __init__(self, path):
+ super(LockError, self).__init__(self.msg % path)
+
+
+class UnlockError(LockError):
+
+ "Could not release a lock"
+
+ msg = "Unable to unlock %r"
+
+
+# first, a default, naive locking implementation
+class LockFile(object):
+
+ """
+ A default, naive locking implementation. Always fails if the file
+ already exists.
+ """
+
+ def __init__(self, path):
+ self.path = path
+ try:
+ fd = os.open(path, os.O_CREAT | os.O_WRONLY | os.O_EXCL)
+ except OSError:
+ raise LockError(self.path)
+ os.close(fd)
+
+ def release(self):
+ os.remove(self.path)
+
+ def remove(self):
+ pass
+
+
+class SystemLockFile(object):
+
+ """
+ An abstract base class for platform-specific locking.
+ """
+
+ def __init__(self, path):
+ self.path = path
+
+ try:
+ # Open lockfile for writing without truncation:
+ self.fp = open(path, 'r+')
+ except IOError:
+ # If the file doesn't exist, IOError is raised; Use a+ instead.
+ # Note that there may be a race here. Multiple processes
+ # could fail on the r+ open and open the file a+, but only
+ # one will get the the lock and write a pid.
+ self.fp = open(path, 'a+')
+
+ try:
+ self._lock_file()
+ except:
+ self.fp.seek(1)
+ self.fp.close()
+ del self.fp
+ raise
+
+ self.fp.write(" %s\n" % os.getpid())
+ self.fp.truncate()
+ self.fp.flush()
+
+ def release(self):
+ if not hasattr(self, 'fp'):
+ return
+ self._unlock_file()
+ self.fp.close()
+ del self.fp
+
+ def remove(self):
+ """
+ Attempt to remove the file
+ """
+ try:
+ os.remove(self.path)
+ except:
+ pass
+
+ #@abc.abstract_method
+ # def _lock_file(self):
+ # """Attempt to obtain the lock on self.fp. Raise LockError if not
+ # acquired."""
+
+ def _unlock_file(self):
+ """Attempt to obtain the lock on self.fp. Raise UnlockError if not
+ released."""
+
+
+class WindowsLockFile(SystemLockFile):
+
+ def _lock_file(self):
+ # Lock just the first byte
+ try:
+ msvcrt.locking(self.fp.fileno(), msvcrt.LK_NBLCK, 1)
+ except IOError:
+ raise LockError(self.fp.name)
+
+ def _unlock_file(self):
+ try:
+ self.fp.seek(0)
+ msvcrt.locking(self.fp.fileno(), msvcrt.LK_UNLCK, 1)
+ except IOError:
+ raise UnlockError(self.fp.name)
+
+if 'msvcrt' in globals():
+ LockFile = WindowsLockFile
+
+
+class UnixLockFile(SystemLockFile):
+
+ def _lock_file(self):
+ flags = fcntl.LOCK_EX | fcntl.LOCK_NB
+ try:
+ fcntl.flock(self.fp.fileno(), flags)
+ except IOError:
+ raise LockError(self.fp.name)
+
+ # no need to implement _unlock_file, it will be unlocked on close()
+
+if 'fcntl' in globals():
+ LockFile = UnixLockFile
diff --git a/cherrypy/lib/profiler.py b/cherrypy/lib/profiler.py
index 785d58a3..5dac386e 100644
--- a/cherrypy/lib/profiler.py
+++ b/cherrypy/lib/profiler.py
@@ -6,17 +6,17 @@ CherryPy users
You can profile any of your pages as follows::
from cherrypy.lib import profiler
-
+
class Root:
p = profile.Profiler("/path/to/profile/dir")
-
+
def index(self):
self.p.run(self._index)
index.exposed = True
-
+
def _index(self):
return "Hello, world!"
-
+
cherrypy.tree.mount(Root())
You can also turn on profiling for all requests
@@ -35,7 +35,8 @@ module from the command line, it will call ``serve()`` for you.
def new_func_strip_path(func_name):
- """Make profiler output more readable by adding ``__init__`` modules' parents"""
+ """Make profiler output more readable by adding `__init__` modules' parents
+ """
filename, line, name = func_name
if filename.endswith("__init__.py"):
return os.path.basename(filename[:-12]) + filename[-12:], line, name
@@ -49,23 +50,25 @@ except ImportError:
profile = None
pstats = None
-import os, os.path
+import os
+import os.path
import sys
import warnings
-from cherrypy._cpcompat import BytesIO
+from cherrypy._cpcompat import StringIO
_count = 0
+
class Profiler(object):
-
+
def __init__(self, path=None):
if not path:
path = os.path.join(os.path.dirname(__file__), "profile")
self.path = path
if not os.path.exists(path):
os.makedirs(path)
-
+
def run(self, func, *args, **params):
"""Dump profile data into self.path."""
global _count
@@ -75,17 +78,17 @@ class Profiler(object):
result = prof.runcall(func, *args, **params)
prof.dump_stats(path)
return result
-
+
def statfiles(self):
""":rtype: list of available profiles.
"""
return [f for f in os.listdir(self.path)
if f.startswith("cp_") and f.endswith(".prof")]
-
+
def stats(self, filename, sortby='cumulative'):
""":rtype stats(index): output of print_stats() for the given profile.
"""
- sio = BytesIO()
+ sio = StringIO()
if sys.version_info >= (2, 5):
s = pstats.Stats(os.path.join(self.path, filename), stream=sio)
s.strip_dirs()
@@ -106,7 +109,7 @@ class Profiler(object):
response = sio.getvalue()
sio.close()
return response
-
+
def index(self):
return """<html>
<head><title>CherryPy profile data</title></head>
@@ -117,16 +120,17 @@ class Profiler(object):
</html>
"""
index.exposed = True
-
+
def menu(self):
yield "<h2>Profiling runs</h2>"
yield "<p>Click on one of the runs below to see profiling data.</p>"
runs = self.statfiles()
runs.sort()
for i in runs:
- yield "<a href='report?filename=%s' target='main'>%s</a><br />" % (i, i)
+ yield "<a href='report?filename=%s' target='main'>%s</a><br />" % (
+ i, i)
menu.exposed = True
-
+
def report(self, filename):
import cherrypy
cherrypy.response.headers['Content-Type'] = 'text/plain'
@@ -135,50 +139,53 @@ class Profiler(object):
class ProfileAggregator(Profiler):
-
+
def __init__(self, path=None):
Profiler.__init__(self, path)
global _count
self.count = _count = _count + 1
self.profiler = profile.Profile()
-
- def run(self, func, *args):
+
+ def run(self, func, *args, **params):
path = os.path.join(self.path, "cp_%04d.prof" % self.count)
- result = self.profiler.runcall(func, *args)
+ result = self.profiler.runcall(func, *args, **params)
self.profiler.dump_stats(path)
return result
class make_app:
+
def __init__(self, nextapp, path=None, aggregate=False):
"""Make a WSGI middleware app which wraps 'nextapp' with profiling.
-
+
nextapp
the WSGI application to wrap, usually an instance of
cherrypy.Application.
-
+
path
where to dump the profiling output.
-
+
aggregate
if True, profile data for all HTTP requests will go in
a single file. If False (the default), each HTTP request will
dump its profile data into a separate file.
-
+
"""
if profile is None or pstats is None:
- msg = ("Your installation of Python does not have a profile module. "
- "If you're on Debian, try `sudo apt-get install python-profiler`. "
- "See http://www.cherrypy.org/wiki/ProfilingOnDebian for details.")
+ msg = ("Your installation of Python does not have a profile "
+ "module. If you're on Debian, try "
+ "`sudo apt-get install python-profiler`. "
+ "See http://www.cherrypy.org/wiki/ProfilingOnDebian "
+ "for details.")
warnings.warn(msg)
-
+
self.nextapp = nextapp
self.aggregate = aggregate
if aggregate:
self.profiler = ProfileAggregator(path)
else:
self.profiler = Profiler(path)
-
+
def __call__(self, environ, start_response):
def gather():
result = []
@@ -191,10 +198,12 @@ class make_app:
def serve(path=None, port=8080):
if profile is None or pstats is None:
msg = ("Your installation of Python does not have a profile module. "
- "If you're on Debian, try `sudo apt-get install python-profiler`. "
- "See http://www.cherrypy.org/wiki/ProfilingOnDebian for details.")
+ "If you're on Debian, try "
+ "`sudo apt-get install python-profiler`. "
+ "See http://www.cherrypy.org/wiki/ProfilingOnDebian "
+ "for details.")
warnings.warn(msg)
-
+
import cherrypy
cherrypy.config.update({'server.socket_port': int(port),
'server.thread_pool': 10,
@@ -205,4 +214,3 @@ def serve(path=None, port=8080):
if __name__ == "__main__":
serve(*tuple(sys.argv[1:]))
-
diff --git a/cherrypy/lib/reprconf.py b/cherrypy/lib/reprconf.py
index 3ec47e7f..67ed9f3c 100644
--- a/cherrypy/lib/reprconf.py
+++ b/cherrypy/lib/reprconf.py
@@ -44,6 +44,7 @@ except ImportError:
import operator as _operator
import sys
+
def as_dict(config):
"""Return a dict from 'config' whether it is a dict, file, or filename."""
if isinstance(config, basestring):
@@ -54,6 +55,7 @@ def as_dict(config):
class NamespaceSet(dict):
+
"""A dict of config namespace names and handlers.
Each config entry should begin with a namespace name; the corresponding
@@ -129,6 +131,7 @@ class NamespaceSet(dict):
class Config(dict):
+
"""A dict-like set of configuration data, with defaults and namespaces.
May take a file, filename, or dict.
@@ -180,6 +183,7 @@ class Config(dict):
class Parser(ConfigParser):
+
"""Sub-class of ConfigParser that keeps the case of options and that
raises an exception if the file cannot be read.
"""
@@ -415,6 +419,9 @@ class _Builder3:
raise TypeError("unrepr could not resolve the name %s" % repr(name))
+ def build_NameConstant(self, o):
+ return o.value
+
def build_UnaryOp(self, o):
op, operand = map(self.build, [o.op, o.operand])
return op(operand)
@@ -457,6 +464,7 @@ def modules(modulePath):
__import__(modulePath)
return sys.modules[modulePath]
+
def attributes(full_attribute_name):
"""Load a module and retrieve an attribute of that module."""
@@ -475,5 +483,3 @@ def attributes(full_attribute_name):
# Return a reference to the attribute.
return attr
-
-
diff --git a/cherrypy/lib/sessions.py b/cherrypy/lib/sessions.py
index 8c7fc5ce..23115c50 100644
--- a/cherrypy/lib/sessions.py
+++ b/cherrypy/lib/sessions.py
@@ -8,10 +8,11 @@ You need to edit your config file to use sessions. Here's an example::
tools.sessions.storage_path = "/home/site/sessions"
tools.sessions.timeout = 60
-This sets the session to be stored in files in the directory /home/site/sessions,
-and the session timeout to 60 minutes. If you omit ``storage_type`` the sessions
-will be saved in RAM. ``tools.sessions.on`` is the only required line for
-working sessions, the rest are optional.
+This sets the session to be stored in files in the directory
+/home/site/sessions, and the session timeout to 60 minutes. If you omit
+``storage_type`` the sessions will be saved in RAM.
+``tools.sessions.on`` is the only required line for working sessions,
+the rest are optional.
By default, the session ID is passed in a cookie, so the client's browser must
have cookies enabled for your site.
@@ -25,9 +26,14 @@ Locking sessions
================
By default, the ``'locking'`` mode of sessions is ``'implicit'``, which means
-the session is locked early and unlocked late. If you want to control when the
-session data is locked and unlocked, set ``tools.sessions.locking = 'explicit'``.
-Then call ``cherrypy.session.acquire_lock()`` and ``cherrypy.session.release_lock()``.
+the session is locked early and unlocked late. Be mindful of this default mode
+for any requests that take a long time to process (streaming responses,
+expensive calculations, database lookups, API calls, etc), as other concurrent
+requests that also utilize sessions will hang until the session is unlocked.
+
+If you want to control when the session data is locked and unlocked,
+set ``tools.sessions.locking = 'explicit'``. Then call
+``cherrypy.session.acquire_lock()`` and ``cherrypy.session.release_lock()``.
Regardless of which mode you use, the session is guaranteed to be unlocked when
the request is complete.
@@ -83,24 +89,24 @@ On the other extreme, some users report Firefox sending cookies after their
expiration date, although this was on a system with an inaccurate system time.
Maybe FF doesn't trust system time.
"""
-
+import sys
import datetime
import os
-import random
import time
import threading
import types
-from warnings import warn
import cherrypy
from cherrypy._cpcompat import copyitems, pickle, random20, unicodestr
from cherrypy.lib import httputil
+from cherrypy.lib import lockfile
from cherrypy.lib import locking
-
missing = object()
+
class Session(object):
+
"""A CherryPy dict-like Session object (one per request)."""
_id = None
@@ -110,6 +116,7 @@ class Session(object):
def _get_id(self):
return self._id
+
def _set_id(self, value):
self._id = value
for o in self.id_observers:
@@ -146,7 +153,10 @@ class Session(object):
True if the application called session.regenerate(). This is not set by
internal calls to regenerate the session id."""
- debug=False
+ debug = False
+ "If True, log debug information."
+
+ # --------------------- Session management methods --------------------- #
def __init__(self, id=None, **kwargs):
self.id_observers = []
@@ -163,12 +173,15 @@ class Session(object):
self._regenerate()
else:
self.id = id
- if not self._exists():
+ if self._exists():
+ if self.debug:
+ cherrypy.log('Set id to %s.' % id, 'TOOLS.SESSIONS')
+ else:
if self.debug:
cherrypy.log('Expired or malicious session %r; '
'making a new one' % id, 'TOOLS.SESSIONS')
# Expired or malicious session. Make a new one.
- # See http://www.cherrypy.org/ticket/709.
+ # See https://bitbucket.org/cherrypy/cherrypy/issue/709.
self.id = None
self.missing = True
self._regenerate()
@@ -188,11 +201,18 @@ class Session(object):
def _regenerate(self):
if self.id is not None:
+ if self.debug:
+ cherrypy.log(
+ 'Deleting the existing session %r before '
+ 'regeneration.' % self.id,
+ 'TOOLS.SESSIONS')
self.delete()
old_session_was_locked = self.locked
if old_session_was_locked:
self.release_lock()
+ if self.debug:
+ cherrypy.log('Old lock released.', 'TOOLS.SESSIONS')
self.id = None
while self.id is None:
@@ -200,9 +220,14 @@ class Session(object):
# Assert that the generated id is not already stored.
if self._exists():
self.id = None
+ if self.debug:
+ cherrypy.log('Set id to generated %s.' % self.id,
+ 'TOOLS.SESSIONS')
if old_session_was_locked:
self.acquire_lock()
+ if self.debug:
+ cherrypy.log('Regenerated lock acquired.', 'TOOLS.SESSIONS')
def clean_up(self):
"""Clean up expired sessions."""
@@ -218,17 +243,24 @@ class Session(object):
# If session data has never been loaded then it's never been
# accessed: no need to save it
if self.loaded:
- t = datetime.timedelta(seconds = self.timeout * 60)
+ t = datetime.timedelta(seconds=self.timeout * 60)
expiration_time = self.now() + t
if self.debug:
- cherrypy.log('Saving with expiry %s' % expiration_time,
+ cherrypy.log('Saving session %r with expiry %s' %
+ (self.id, expiration_time),
'TOOLS.SESSIONS')
self._save(expiration_time)
-
+ else:
+ if self.debug:
+ cherrypy.log(
+ 'Skipping save of session %r (no session loaded).' %
+ self.id, 'TOOLS.SESSIONS')
finally:
if self.locked:
# Always release the lock if the user didn't release it
self.release_lock()
+ if self.debug:
+ cherrypy.log('Lock released after save.', 'TOOLS.SESSIONS')
def load(self):
"""Copy stored session data into this session instance."""
@@ -236,9 +268,13 @@ class Session(object):
# data is either None or a tuple (session_data, expiration_time)
if data is None or data[1] < self.now():
if self.debug:
- cherrypy.log('Expired session, flushing data', 'TOOLS.SESSIONS')
+ cherrypy.log('Expired session %r, flushing data.' % self.id,
+ 'TOOLS.SESSIONS')
self._data = {}
else:
+ if self.debug:
+ cherrypy.log('Data loaded for session %r.' % self.id,
+ 'TOOLS.SESSIONS')
self._data = data[0]
self.loaded = True
@@ -246,7 +282,7 @@ class Session(object):
# The instances are created and destroyed per-request.
cls = self.__class__
if self.clean_freq and not cls.clean_thread:
- # clean_up is in instancemethod and not a classmethod,
+ # clean_up is an instancemethod and not a classmethod,
# so that tool config can be accessed inside the method.
t = cherrypy.process.plugins.Monitor(
cherrypy.engine, self.clean_up, self.clean_freq * 60,
@@ -254,21 +290,31 @@ class Session(object):
t.subscribe()
cls.clean_thread = t
t.start()
+ if self.debug:
+ cherrypy.log('Started cleanup thread.', 'TOOLS.SESSIONS')
def delete(self):
"""Delete stored session data."""
self._delete()
+ if self.debug:
+ cherrypy.log('Deleted session %s.' % self.id,
+ 'TOOLS.SESSIONS')
+
+ # -------------------- Application accessor methods -------------------- #
def __getitem__(self, key):
- if not self.loaded: self.load()
+ if not self.loaded:
+ self.load()
return self._data[key]
def __setitem__(self, key, value):
- if not self.loaded: self.load()
+ if not self.loaded:
+ self.load()
self._data[key] = value
def __delitem__(self, key):
- if not self.loaded: self.load()
+ if not self.loaded:
+ self.load()
del self._data[key]
def pop(self, key, default=missing):
@@ -276,55 +322,65 @@ class Session(object):
If key is not found, default is returned if given,
otherwise KeyError is raised.
"""
- if not self.loaded: self.load()
+ if not self.loaded:
+ self.load()
if default is missing:
return self._data.pop(key)
else:
return self._data.pop(key, default)
def __contains__(self, key):
- if not self.loaded: self.load()
+ if not self.loaded:
+ self.load()
return key in self._data
if hasattr({}, 'has_key'):
def has_key(self, key):
"""D.has_key(k) -> True if D has a key k, else False."""
- if not self.loaded: self.load()
+ if not self.loaded:
+ self.load()
return key in self._data
def get(self, key, default=None):
"""D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None."""
- if not self.loaded: self.load()
+ if not self.loaded:
+ self.load()
return self._data.get(key, default)
def update(self, d):
"""D.update(E) -> None. Update D from E: for k in E: D[k] = E[k]."""
- if not self.loaded: self.load()
+ if not self.loaded:
+ self.load()
self._data.update(d)
def setdefault(self, key, default=None):
"""D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D."""
- if not self.loaded: self.load()
+ if not self.loaded:
+ self.load()
return self._data.setdefault(key, default)
def clear(self):
"""D.clear() -> None. Remove all items from D."""
- if not self.loaded: self.load()
+ if not self.loaded:
+ self.load()
self._data.clear()
def keys(self):
"""D.keys() -> list of D's keys."""
- if not self.loaded: self.load()
+ if not self.loaded:
+ self.load()
return self._data.keys()
def items(self):
"""D.items() -> list of D's (key, value) pairs, as 2-tuples."""
- if not self.loaded: self.load()
+ if not self.loaded:
+ self.load()
return self._data.items()
def values(self):
"""D.values() -> list of D's values."""
- if not self.loaded: self.load()
+ if not self.loaded:
+ self.load()
return self._data.values()
@@ -381,6 +437,7 @@ class RamSession(Session):
class FileSession(Session):
+
"""Implementation of the File backend for sessions
storage_path
@@ -423,17 +480,6 @@ class FileSession(Session):
for k, v in kwargs.items():
setattr(cls, k, v)
-
- # Warn if any lock files exist at startup.
- lockfiles = [fname for fname in os.listdir(cls.storage_path)
- if (fname.startswith(cls.SESSION_PREFIX)
- and fname.endswith(cls.LOCK_SUFFIX))]
- if lockfiles:
- plural = ('', 's')[len(lockfiles) > 1]
- warn("%s session lockfile%s found at startup. If you are "
- "only running one process, then you may need to "
- "manually delete the lockfiles found at %r."
- % (len(lockfiles), plural, cls.storage_path))
setup = classmethod(setup)
def _get_file_path(self):
@@ -447,6 +493,8 @@ class FileSession(Session):
return os.path.exists(path)
def _load(self, path=None):
+ assert self.locked, ("The session load without being locked. "
+ "Check your tools' priority levels.")
if path is None:
path = self._get_file_path()
try:
@@ -456,9 +504,15 @@ class FileSession(Session):
finally:
f.close()
except (IOError, EOFError):
+ e = sys.exc_info()[1]
+ if self.debug:
+ cherrypy.log("Error loading the session pickle: %s" %
+ e, 'TOOLS.SESSIONS')
return None
def _save(self, expiration_time):
+ assert self.locked, ("The session was saved without being locked. "
+ "Check your tools' priority levels.")
f = open(self._get_file_path(), "wb")
try:
pickle.dump((self._data, expiration_time), f, self.pickle_protocol)
@@ -466,6 +520,8 @@ class FileSession(Session):
f.close()
def _delete(self):
+ assert self.locked, ("The session deletion without being locked. "
+ "Check your tools' priority levels.")
try:
os.unlink(self._get_file_path())
except OSError:
@@ -479,19 +535,19 @@ class FileSession(Session):
checker = locking.LockChecker(self.id, self.lock_timeout)
while not checker.expired():
try:
- lockfd = os.open(path, os.O_CREAT|os.O_WRONLY|os.O_EXCL)
- except OSError:
+ self.lock = lockfile.LockFile(path)
+ except lockfile.LockError:
time.sleep(0.1)
else:
- os.close(lockfd)
break
self.locked = True
+ if self.debug:
+ cherrypy.log('Lock acquired.', 'TOOLS.SESSIONS')
def release_lock(self, path=None):
"""Release the lock on the currently-loaded session data."""
- if path is None:
- path = self._get_file_path()
- os.unlink(path + self.LOCK_SUFFIX)
+ self.lock.release()
+ self.lock.remove()
self.locked = False
def clean_up(self):
@@ -500,11 +556,18 @@ class FileSession(Session):
# Iterate over all session files in self.storage_path
for fname in os.listdir(self.storage_path):
if (fname.startswith(self.SESSION_PREFIX)
- and not fname.endswith(self.LOCK_SUFFIX)):
+ and not fname.endswith(self.LOCK_SUFFIX)):
# We have a session file: lock and load it and check
# if it's expired. If it fails, nevermind.
path = os.path.join(self.storage_path, fname)
self.acquire_lock(path)
+ if self.debug:
+ # This is a bit of a hack, since we're calling clean_up
+ # on the first instance rather than the entire class,
+ # so depending on whether you have "debug" set on the
+ # path of the first session called, this may not run.
+ cherrypy.log('Cleanup lock acquired.', 'TOOLS.SESSIONS')
+
try:
contents = self._load(path)
# _load returns None on IOError
@@ -524,6 +587,7 @@ class FileSession(Session):
class PostgresqlSession(Session):
+
""" Implementation of the PostgreSQL backend for sessions. It assumes
a table like this::
@@ -593,6 +657,8 @@ class PostgresqlSession(Session):
self.locked = True
self.cursor.execute('select id from session where id=%s for update',
(self.id,))
+ if self.debug:
+ cherrypy.log('Lock acquired.', 'TOOLS.SESSIONS')
def release_lock(self):
"""Release the lock on the currently-loaded session data."""
@@ -633,6 +699,7 @@ class MemcachedSession(Session):
def _get_id(self):
return self._id
+
def _set_id(self, value):
# This encode() call is where we differ from the superclass.
# Memcache keys MUST be byte strings, not unicode.
@@ -664,7 +731,8 @@ class MemcachedSession(Session):
self.mc_lock.acquire()
try:
if not self.cache.set(self.id, (self._data, expiration_time), td):
- raise AssertionError("Session data for id %r not set." % self.id)
+ raise AssertionError(
+ "Session data for id %r not set." % self.id)
finally:
self.mc_lock.release()
@@ -675,6 +743,8 @@ class MemcachedSession(Session):
"""Acquire an exclusive lock on the currently-loaded session data."""
self.locked = True
self.locks.setdefault(self.id, threading.RLock()).acquire()
+ if self.debug:
+ cherrypy.log('Lock acquired.', 'TOOLS.SESSIONS')
def release_lock(self):
"""Release the lock on the currently-loaded session data."""
@@ -713,12 +783,15 @@ def save():
cherrypy.session.save()
save.failsafe = True
+
def close():
"""Close the session object for this request."""
sess = getattr(cherrypy.serving, "session", None)
if getattr(sess, "locked", False):
# If the session is still locked we release the lock
sess.release_lock()
+ if sess.debug:
+ cherrypy.log('Lock released on close.', 'TOOLS.SESSIONS')
close.failsafe = True
close.priority = 90
@@ -802,6 +875,7 @@ def init(storage_type='ram', path=None, path_header=None, name='session_id',
kwargs['clean_freq'] = clean_freq
cherrypy.serving.session = sess = storage_class(id, **kwargs)
sess.debug = debug
+
def update_cookie(id):
"""Update the cookie every time the session id changes."""
cherrypy.serving.response.cookie[name] = id
@@ -856,8 +930,11 @@ def set_response_cookie(path=None, path_header=None, name='session_id',
# Set response cookie
cookie = cherrypy.serving.response.cookie
cookie[name] = cherrypy.serving.session.id
- cookie[name]['path'] = (path or cherrypy.serving.request.headers.get(path_header)
- or '/')
+ cookie[name]['path'] = (
+ path or
+ cherrypy.serving.request.headers.get(path_header) or
+ '/'
+ )
# We'd like to use the "max-age" param as indicated in
# http://www.faqs.org/rfcs/rfc2109.html but IE doesn't
@@ -876,11 +953,11 @@ def set_response_cookie(path=None, path_header=None, name='session_id',
raise ValueError("The httponly cookie token is not supported.")
cookie[name]['httponly'] = 1
+
def expire():
"""Expire the current session cookie."""
- name = cherrypy.serving.request.config.get('tools.sessions.name', 'session_id')
+ name = cherrypy.serving.request.config.get(
+ 'tools.sessions.name', 'session_id')
one_year = 60 * 60 * 24 * 365
e = time.time() - one_year
cherrypy.serving.response.cookie[name]['expires'] = httputil.HTTPDate(e)
-
-
diff --git a/cherrypy/lib/static.py b/cherrypy/lib/static.py
index 2d142307..d5bb9d02 100644
--- a/cherrypy/lib/static.py
+++ b/cherrypy/lib/static.py
@@ -5,10 +5,10 @@ except ImportError:
import logging
import mimetypes
mimetypes.init()
-mimetypes.types_map['.dwg']='image/x-dwg'
-mimetypes.types_map['.ico']='image/x-icon'
-mimetypes.types_map['.bz2']='application/x-bzip2'
-mimetypes.types_map['.gz']='application/x-gzip'
+mimetypes.types_map['.dwg'] = 'image/x-dwg'
+mimetypes.types_map['.ico'] = 'image/x-icon'
+mimetypes.types_map['.bz2'] = 'application/x-bzip2'
+mimetypes.types_map['.gz'] = 'application/x-gzip'
import os
import re
@@ -20,21 +20,22 @@ from cherrypy._cpcompat import ntob, unquote
from cherrypy.lib import cptools, httputil, file_generator_limited
-def serve_file(path, content_type=None, disposition=None, name=None, debug=False):
+def serve_file(path, content_type=None, disposition=None, name=None,
+ debug=False):
"""Set status, headers, and body in order to serve the given path.
-
+
The Content-Type header will be set to the content_type arg, if provided.
If not provided, the Content-Type will be guessed by the file extension
of the 'path' argument.
-
+
If disposition is not None, the Content-Disposition header will be set
to "<disposition>; filename=<name>". If name is None, it will be set
to the basename of path. If disposition is None, no Content-Disposition
header will be written.
"""
-
+
response = cherrypy.serving.response
-
+
# If path is relative, users should fix it by making path absolute.
# That is, CherryPy should not guess where the application root is.
# It certainly should *not* use cwd (since CP may be invoked from a
@@ -45,26 +46,26 @@ def serve_file(path, content_type=None, disposition=None, name=None, debug=False
if debug:
cherrypy.log(msg, 'TOOLS.STATICFILE')
raise ValueError(msg)
-
+
try:
st = os.stat(path)
except OSError:
if debug:
cherrypy.log('os.stat(%r) failed' % path, 'TOOLS.STATIC')
raise cherrypy.NotFound()
-
+
# Check if path is a directory.
if stat.S_ISDIR(st.st_mode):
# Let the caller deal with it as they like.
if debug:
cherrypy.log('%r is a directory' % path, 'TOOLS.STATIC')
raise cherrypy.NotFound()
-
+
# Set the Last-Modified response header, so that
# modified-since validation code can work.
response.headers['Last-Modified'] = httputil.HTTPDate(st.st_mtime)
cptools.validate_since()
-
+
if content_type is None:
# Set content-type based on filename extension
ext = ""
@@ -76,7 +77,7 @@ def serve_file(path, content_type=None, disposition=None, name=None, debug=False
response.headers['Content-Type'] = content_type
if debug:
cherrypy.log('Content-Type: %r' % content_type, 'TOOLS.STATIC')
-
+
cd = None
if disposition is not None:
if name is None:
@@ -85,19 +86,20 @@ def serve_file(path, content_type=None, disposition=None, name=None, debug=False
response.headers["Content-Disposition"] = cd
if debug:
cherrypy.log('Content-Disposition: %r' % cd, 'TOOLS.STATIC')
-
+
# Set Content-Length and use an iterable (file object)
# this way CP won't load the whole file in memory
content_length = st.st_size
fileobj = open(path, 'rb')
return _serve_fileobj(fileobj, content_type, content_length, debug=debug)
+
def serve_fileobj(fileobj, content_type=None, disposition=None, name=None,
debug=False):
"""Set status, headers, and body in order to serve the given file object.
-
+
The Content-Type header will be set to the content_type arg, if provided.
-
+
If disposition is not None, the Content-Disposition header will be set
to "<disposition>; filename=<name>". If name is None, 'filename' will
not be set. If disposition is None, no Content-Disposition header will
@@ -110,9 +112,9 @@ def serve_fileobj(fileobj, content_type=None, disposition=None, name=None,
serve_fileobj(), expecting that the data would be served starting from that
position.
"""
-
+
response = cherrypy.serving.response
-
+
try:
st = os.fstat(fileobj.fileno())
except AttributeError:
@@ -127,12 +129,12 @@ def serve_fileobj(fileobj, content_type=None, disposition=None, name=None,
response.headers['Last-Modified'] = httputil.HTTPDate(st.st_mtime)
cptools.validate_since()
content_length = st.st_size
-
+
if content_type is not None:
response.headers['Content-Type'] = content_type
if debug:
cherrypy.log('Content-Type: %r' % content_type, 'TOOLS.STATIC')
-
+
cd = None
if disposition is not None:
if name is None:
@@ -142,13 +144,14 @@ def serve_fileobj(fileobj, content_type=None, disposition=None, name=None,
response.headers["Content-Disposition"] = cd
if debug:
cherrypy.log('Content-Disposition: %r' % cd, 'TOOLS.STATIC')
-
+
return _serve_fileobj(fileobj, content_type, content_length, debug=debug)
+
def _serve_fileobj(fileobj, content_type, content_length, debug=False):
"""Internal. Set response.body to the given file object, perhaps ranged."""
response = cherrypy.serving.response
-
+
# HTTP/1.0 didn't have Range/Accept-Ranges headers, or the 206 code
request = cherrypy.serving.request
if request.protocol >= (1, 1):
@@ -156,11 +159,12 @@ def _serve_fileobj(fileobj, content_type, content_length, debug=False):
r = httputil.get_ranges(request.headers.get('Range'), content_length)
if r == []:
response.headers['Content-Range'] = "bytes */%s" % content_length
- message = "Invalid Range (first-byte-pos greater than Content-Length)"
+ message = ("Invalid Range (first-byte-pos greater than "
+ "Content-Length)")
if debug:
cherrypy.log(message, 'TOOLS.STATIC')
raise cherrypy.HTTPError(416, message)
-
+
if r:
if len(r) == 1:
# Return a single-part response.
@@ -169,8 +173,9 @@ def _serve_fileobj(fileobj, content_type, content_length, debug=False):
stop = content_length
r_len = stop - start
if debug:
- cherrypy.log('Single part; start: %r, stop: %r' % (start, stop),
- 'TOOLS.STATIC')
+ cherrypy.log(
+ 'Single part; start: %r, stop: %r' % (start, stop),
+ 'TOOLS.STATIC')
response.status = "206 Partial Content"
response.headers['Content-Range'] = (
"bytes %s-%s/%s" % (start, stop - 1, content_length))
@@ -182,36 +187,42 @@ def _serve_fileobj(fileobj, content_type, content_length, debug=False):
response.status = "206 Partial Content"
try:
# Python 3
- from email.generator import _make_boundary as choose_boundary
+ from email.generator import _make_boundary as make_boundary
except ImportError:
# Python 2
- from mimetools import choose_boundary
- boundary = choose_boundary()
+ from mimetools import choose_boundary as make_boundary
+ boundary = make_boundary()
ct = "multipart/byteranges; boundary=%s" % boundary
response.headers['Content-Type'] = ct
if "Content-Length" in response.headers:
# Delete Content-Length header so finalize() recalcs it.
del response.headers["Content-Length"]
-
+
def file_ranges():
# Apache compatibility:
yield ntob("\r\n")
-
+
for start, stop in r:
if debug:
- cherrypy.log('Multipart; start: %r, stop: %r' % (start, stop),
- 'TOOLS.STATIC')
+ cherrypy.log(
+ 'Multipart; start: %r, stop: %r' % (
+ start, stop),
+ 'TOOLS.STATIC')
yield ntob("--" + boundary, 'ascii')
- yield ntob("\r\nContent-type: %s" % content_type, 'ascii')
- yield ntob("\r\nContent-range: bytes %s-%s/%s\r\n\r\n"
- % (start, stop - 1, content_length), 'ascii')
+ yield ntob("\r\nContent-type: %s" % content_type,
+ 'ascii')
+ yield ntob(
+ "\r\nContent-range: bytes %s-%s/%s\r\n\r\n" % (
+ start, stop - 1, content_length),
+ 'ascii')
fileobj.seek(start)
- for chunk in file_generator_limited(fileobj, stop-start):
+ gen = file_generator_limited(fileobj, stop - start)
+ for chunk in gen:
yield chunk
yield ntob("\r\n")
# Final boundary
yield ntob("--" + boundary + "--", 'ascii')
-
+
# Apache compatibility:
yield ntob("\r\n")
response.body = file_ranges()
@@ -219,13 +230,14 @@ def _serve_fileobj(fileobj, content_type, content_length, debug=False):
else:
if debug:
cherrypy.log('No byteranges requested', 'TOOLS.STATIC')
-
+
# Set Content-Length and use an iterable (file object)
# this way CP won't load the whole file in memory
response.headers['Content-Length'] = content_length
response.body = fileobj
return response.body
+
def serve_download(path, name=None):
"""Serve 'path' as an application/x-download attachment."""
# This is such a common idiom I felt it deserved its own wrapper.
@@ -252,20 +264,21 @@ def _attempt(filename, content_types, debug=False):
cherrypy.log('NotFound', 'TOOLS.STATICFILE')
return False
+
def staticdir(section, dir, root="", match="", content_types=None, index="",
debug=False):
"""Serve a static resource from the given (root +) dir.
-
+
match
If given, request.path_info will be searched for the given
regular expression before attempting to serve static content.
-
+
content_types
If given, it should be a Python dictionary of
{file-extension: content-type} pairs, where 'file-extension' is
a string (e.g. "gif") and 'content-type' is the value to write
out in the Content-Type response header (e.g. "image/gif").
-
+
index
If provided, it should be the (relative) name of a file to
serve for directory requests. For example, if the dir argument is
@@ -277,13 +290,13 @@ def staticdir(section, dir, root="", match="", content_types=None, index="",
if debug:
cherrypy.log('request.method not GET or HEAD', 'TOOLS.STATICDIR')
return False
-
+
if match and not re.search(match, request.path_info):
if debug:
cherrypy.log('request.path_info %r does not match pattern %r' %
(request.path_info, match), 'TOOLS.STATICDIR')
return False
-
+
# Allow the use of '~' to refer to a user's home directory.
dir = os.path.expanduser(dir)
@@ -295,7 +308,7 @@ def staticdir(section, dir, root="", match="", content_types=None, index="",
cherrypy.log(msg, 'TOOLS.STATICDIR')
raise ValueError(msg)
dir = os.path.join(root, dir)
-
+
# Determine where we are in the object tree relative to 'section'
# (where the static tool was defined).
if section == 'global':
@@ -303,19 +316,19 @@ def staticdir(section, dir, root="", match="", content_types=None, index="",
section = section.rstrip(r"\/")
branch = request.path_info[len(section) + 1:]
branch = unquote(branch.lstrip(r"\/"))
-
+
# If branch is "", filename will end in a slash
filename = os.path.join(dir, branch)
if debug:
cherrypy.log('Checking file %r to fulfill %r' %
(filename, request.path_info), 'TOOLS.STATICDIR')
-
+
# There's a chance that the branch pulled from the URL might
# have ".." or similar uplevel attacks in it. Check that the final
# filename is a child of dir.
if not os.path.normpath(filename).startswith(os.path.normpath(dir)):
- raise cherrypy.HTTPError(403) # Forbidden
-
+ raise cherrypy.HTTPError(403) # Forbidden
+
handled = _attempt(filename, content_types)
if not handled:
# Check for an index file if a folder was requested.
@@ -325,39 +338,41 @@ def staticdir(section, dir, root="", match="", content_types=None, index="",
request.is_index = filename[-1] in (r"\/")
return handled
+
def staticfile(filename, root=None, match="", content_types=None, debug=False):
"""Serve a static resource from the given (root +) filename.
-
+
match
If given, request.path_info will be searched for the given
regular expression before attempting to serve static content.
-
+
content_types
If given, it should be a Python dictionary of
{file-extension: content-type} pairs, where 'file-extension' is
a string (e.g. "gif") and 'content-type' is the value to write
out in the Content-Type response header (e.g. "image/gif").
-
+
"""
request = cherrypy.serving.request
if request.method not in ('GET', 'HEAD'):
if debug:
cherrypy.log('request.method not GET or HEAD', 'TOOLS.STATICFILE')
return False
-
+
if match and not re.search(match, request.path_info):
if debug:
cherrypy.log('request.path_info %r does not match pattern %r' %
(request.path_info, match), 'TOOLS.STATICFILE')
return False
-
+
# If filename is relative, make absolute using "root".
if not os.path.isabs(filename):
if not root:
- msg = "Static tool requires an absolute filename (got '%s')." % filename
+ msg = "Static tool requires an absolute filename (got '%s')." % (
+ filename,)
if debug:
cherrypy.log(msg, 'TOOLS.STATICFILE')
raise ValueError(msg)
filename = os.path.join(root, filename)
-
+
return _attempt(filename, content_types, debug=debug)
diff --git a/cherrypy/lib/xmlrpcutil.py b/cherrypy/lib/xmlrpcutil.py
index 9a44464b..9fc9564f 100644
--- a/cherrypy/lib/xmlrpcutil.py
+++ b/cherrypy/lib/xmlrpcutil.py
@@ -3,6 +3,7 @@ import sys
import cherrypy
from cherrypy._cpcompat import ntob
+
def get_xmlrpclib():
try:
import xmlrpc.client as x
@@ -10,6 +11,7 @@ def get_xmlrpclib():
import xmlrpclib as x
return x
+
def process_body():
"""Return (params, method) from request body."""
try:
@@ -48,8 +50,8 @@ def respond(body, encoding='utf-8', allow_none=0):
encoding=encoding,
allow_none=allow_none))
+
def on_error(*args, **kwargs):
body = str(sys.exc_info()[1])
xmlrpclib = get_xmlrpclib()
_set_response(xmlrpclib.dumps(xmlrpclib.Fault(1, body)))
-
diff --git a/cherrypy/process/plugins.py b/cherrypy/process/plugins.py
index ba618a0b..c787ba92 100644
--- a/cherrypy/process/plugins.py
+++ b/cherrypy/process/plugins.py
@@ -7,7 +7,8 @@ import sys
import time
import threading
-from cherrypy._cpcompat import basestring, get_daemon, get_thread_ident, ntob, set
+from cherrypy._cpcompat import basestring, get_daemon, get_thread_ident
+from cherrypy._cpcompat import ntob, set, Timer, SetDaemonProperty
# _module__file__base is used by Autoreload to make
# absolute any filenames retrieved from sys.modules which are not
@@ -19,8 +20,8 @@ from cherrypy._cpcompat import basestring, get_daemon, get_thread_ident, ntob, s
# changes the current directory by executing os.chdir(), then the next time
# Autoreload runs, it will not be able to find any filenames which are
# not absolute paths, because the current directory is not the same as when the
-# module was first imported. Autoreload will then wrongly conclude the file has
-# "changed", and initiate the shutdown/re-exec sequence.
+# module was first imported. Autoreload will then wrongly conclude the file
+# has "changed", and initiate the shutdown/re-exec sequence.
# See ticket #917.
# For this workaround to have a decent probability of success, this module
# needs to be imported as early as possible, before the app has much chance
@@ -29,14 +30,16 @@ _module__file__base = os.getcwd()
class SimplePlugin(object):
+
"""Plugin base class which auto-subscribes methods for known channels."""
-
+
bus = None
- """A :class:`Bus <cherrypy.process.wspbus.Bus>`, usually cherrypy.engine."""
-
+ """A :class:`Bus <cherrypy.process.wspbus.Bus>`, usually cherrypy.engine.
+ """
+
def __init__(self, bus):
self.bus = bus
-
+
def subscribe(self):
"""Register this object as a (multi-channel) listener on the bus."""
for channel in self.bus.listeners:
@@ -44,7 +47,7 @@ class SimplePlugin(object):
method = getattr(self, channel, None)
if method is not None:
self.bus.subscribe(channel, method)
-
+
def unsubscribe(self):
"""Unregister this object as a listener on the bus."""
for channel in self.bus.listeners:
@@ -54,42 +57,42 @@ class SimplePlugin(object):
self.bus.unsubscribe(channel, method)
-
class SignalHandler(object):
+
"""Register bus channels (and listeners) for system signals.
-
+
You can modify what signals your application listens for, and what it does
when it receives signals, by modifying :attr:`SignalHandler.handlers`,
a dict of {signal name: callback} pairs. The default set is::
-
+
handlers = {'SIGTERM': self.bus.exit,
'SIGHUP': self.handle_SIGHUP,
'SIGUSR1': self.bus.graceful,
}
-
+
The :func:`SignalHandler.handle_SIGHUP`` method calls
:func:`bus.restart()<cherrypy.process.wspbus.Bus.restart>`
if the process is daemonized, but
:func:`bus.exit()<cherrypy.process.wspbus.Bus.exit>`
if the process is attached to a TTY. This is because Unix window
managers tend to send SIGHUP to terminal windows when the user closes them.
-
- Feel free to add signals which are not available on every platform. The
- :class:`SignalHandler` will ignore errors raised from attempting to register
- handlers for unknown signals.
+
+ Feel free to add signals which are not available on every platform.
+ The :class:`SignalHandler` will ignore errors raised from attempting
+ to register handlers for unknown signals.
"""
-
+
handlers = {}
"""A map from signal names (e.g. 'SIGTERM') to handlers (e.g. bus.exit)."""
-
+
signals = {}
"""A map from signal numbers to names."""
-
+
for k, v in vars(_signal).items():
if k.startswith('SIG') and not k.startswith('SIG_'):
signals[v] = k
del k, v
-
+
def __init__(self, bus):
self.bus = bus
# Set default handlers
@@ -106,12 +109,12 @@ class SignalHandler(object):
self.handlers['SIGINT'] = self._jython_SIGINT_handler
self._previous_handlers = {}
-
+
def _jython_SIGINT_handler(self, signum=None, frame=None):
# See http://bugs.jython.org/issue1313
self.bus.log('Keyboard Interrupt: shutting down bus')
self.bus.exit()
-
+
def subscribe(self):
"""Subscribe self.handlers to signals."""
for sig, func in self.handlers.items():
@@ -119,18 +122,18 @@ class SignalHandler(object):
self.set_handler(sig, func)
except ValueError:
pass
-
+
def unsubscribe(self):
"""Unsubscribe self.handlers from signals."""
for signum, handler in self._previous_handlers.items():
signame = self.signals[signum]
-
+
if handler is None:
self.bus.log("Restoring %s handler to SIG_DFL." % signame)
handler = _signal.SIG_DFL
else:
self.bus.log("Restoring %s handler %r." % (signame, handler))
-
+
try:
our_handler = _signal.signal(signum, handler)
if our_handler is None:
@@ -140,13 +143,13 @@ class SignalHandler(object):
except ValueError:
self.bus.log("Unable to restore %s handler %r." %
(signame, handler), level=40, traceback=True)
-
+
def set_handler(self, signal, listener=None):
"""Subscribe a handler for the given signal (number or name).
-
+
If the optional 'listener' argument is provided, it will be
subscribed as a listener for the given signal's channel.
-
+
If the given signal name or number is not available on the current
platform, ValueError is raised.
"""
@@ -161,20 +164,20 @@ class SignalHandler(object):
except KeyError:
raise ValueError("No such signal: %r" % signal)
signum = signal
-
+
prev = _signal.signal(signum, self._handle_signal)
self._previous_handlers[signum] = prev
-
+
if listener is not None:
self.bus.log("Listening for %s." % signame)
self.bus.subscribe(signame, listener)
-
+
def _handle_signal(self, signum=None, frame=None):
"""Python signal handler (self.set_handler subscribes it for you)."""
signame = self.signals[signum]
self.bus.log("Caught signal %s." % signame)
self.bus.publish(signame)
-
+
def handle_SIGHUP(self):
"""Restart if daemonized, else exit."""
if os.isatty(sys.stdin.fileno()):
@@ -187,26 +190,29 @@ class SignalHandler(object):
try:
- import pwd, grp
+ import pwd
+ import grp
except ImportError:
pwd, grp = None, None
class DropPrivileges(SimplePlugin):
+
"""Drop privileges. uid/gid arguments not available on Windows.
-
- Special thanks to Gavin Baker: http://antonym.org/node/100.
+
+ Special thanks to `Gavin Baker <http://antonym.org/2005/12/dropping-privileges-in-python.html>`_
"""
-
+
def __init__(self, bus, umask=None, uid=None, gid=None):
SimplePlugin.__init__(self, bus)
self.finalized = False
self.uid = uid
self.gid = gid
self.umask = umask
-
+
def _get_uid(self):
return self._uid
+
def _set_uid(self, val):
if val is not None:
if pwd is None:
@@ -217,10 +223,11 @@ class DropPrivileges(SimplePlugin):
val = pwd.getpwnam(val)[2]
self._uid = val
uid = property(_get_uid, _set_uid,
- doc="The uid under which to run. Availability: Unix.")
-
+ doc="The uid under which to run. Availability: Unix.")
+
def _get_gid(self):
return self._gid
+
def _set_gid(self, val):
if val is not None:
if grp is None:
@@ -231,10 +238,11 @@ class DropPrivileges(SimplePlugin):
val = grp.getgrnam(val)[2]
self._gid = val
gid = property(_get_gid, _set_gid,
- doc="The gid under which to run. Availability: Unix.")
-
+ doc="The gid under which to run. Availability: Unix.")
+
def _get_umask(self):
return self._umask
+
def _set_umask(self, val):
if val is not None:
try:
@@ -244,13 +252,16 @@ class DropPrivileges(SimplePlugin):
level=30)
val = None
self._umask = val
- umask = property(_get_umask, _set_umask,
- doc="""The default permission mode for newly created files and directories.
-
+ umask = property(
+ _get_umask,
+ _set_umask,
+ doc="""The default permission mode for newly created files and
+ directories.
+
Usually expressed in octal format, for example, ``0644``.
Availability: Unix, Windows.
""")
-
+
def start(self):
# uid/gid
def current_ids():
@@ -261,7 +272,7 @@ class DropPrivileges(SimplePlugin):
if grp:
group = grp.getgrgid(os.getgid())[0]
return name, group
-
+
if self.finalized:
if not (self.uid is None and self.gid is None):
self.bus.log('Already running as uid: %r gid: %r' %
@@ -278,7 +289,7 @@ class DropPrivileges(SimplePlugin):
if self.uid is not None:
os.setuid(self.uid)
self.bus.log('Running as uid: %r gid: %r' % current_ids())
-
+
# umask
if self.finalized:
if self.umask is not None:
@@ -290,7 +301,7 @@ class DropPrivileges(SimplePlugin):
old_umask = os.umask(self.umask)
self.bus.log('umask old: %03o, new: %03o' %
(old_umask, self.umask))
-
+
self.finalized = True
# This is slightly higher than the priority for server.start
# in order to facilitate the most common use: starting on a low
@@ -299,12 +310,13 @@ class DropPrivileges(SimplePlugin):
class Daemonizer(SimplePlugin):
+
"""Daemonize the running script.
-
+
Use this with a Web Site Process Bus via::
-
+
Daemonizer(bus).subscribe()
-
+
When this component finishes, the process is completely decoupled from
the parent environment. Please note that when this component is used,
the return code from the parent process will still be 0 if a startup
@@ -314,7 +326,7 @@ class Daemonizer(SimplePlugin):
of whether the process fully started. In fact, that return code only
indicates if the process succesfully finished the first fork.
"""
-
+
def __init__(self, bus, stdin='/dev/null', stdout='/dev/null',
stderr='/dev/null'):
SimplePlugin.__init__(self, bus)
@@ -322,11 +334,11 @@ class Daemonizer(SimplePlugin):
self.stdout = stdout
self.stderr = stderr
self.finalized = False
-
+
def start(self):
if self.finalized:
self.bus.log('Already deamonized.')
-
+
# forking has issues with threads:
# http://www.opengroup.org/onlinepubs/000095399/functions/fork.html
# "The general problem with making fork() work in a multi-threaded
@@ -336,15 +348,15 @@ class Daemonizer(SimplePlugin):
self.bus.log('There are %r active threads. '
'Daemonizing now may cause strange failures.' %
threading.enumerate(), level=30)
-
+
# See http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
# (or http://www.faqs.org/faqs/unix-faq/programmer/faq/ section 1.7)
# and http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012
-
+
# Finish up with the current stdout/stderr
sys.stdout.flush()
sys.stderr.flush()
-
+
# Do first fork.
try:
pid = os.fork()
@@ -360,23 +372,23 @@ class Daemonizer(SimplePlugin):
exc = sys.exc_info()[1]
sys.exit("%s: fork #1 failed: (%d) %s\n"
% (sys.argv[0], exc.errno, exc.strerror))
-
+
os.setsid()
-
+
# Do second fork
try:
pid = os.fork()
if pid > 0:
self.bus.log('Forking twice.')
- os._exit(0) # Exit second parent
+ os._exit(0) # Exit second parent
except OSError:
exc = sys.exc_info()[1]
sys.exit("%s: fork #2 failed: (%d) %s\n"
% (sys.argv[0], exc.errno, exc.strerror))
-
+
os.chdir("/")
os.umask(0)
-
+
si = open(self.stdin, "r")
so = open(self.stdout, "a+")
se = open(self.stderr, "a+")
@@ -387,30 +399,31 @@ class Daemonizer(SimplePlugin):
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
-
+
self.bus.log('Daemonized to PID: %s' % os.getpid())
self.finalized = True
start.priority = 65
class PIDFile(SimplePlugin):
+
"""Maintain a PID file via a WSPBus."""
-
+
def __init__(self, bus, pidfile):
SimplePlugin.__init__(self, bus)
self.pidfile = pidfile
self.finalized = False
-
+
def start(self):
pid = os.getpid()
if self.finalized:
self.bus.log('PID %r already written to %r.' % (pid, self.pidfile))
else:
- open(self.pidfile, "wb").write(ntob("%s" % pid, 'utf8'))
+ open(self.pidfile, "wb").write(ntob("%s\n" % pid, 'utf8'))
self.bus.log('PID %r written to %r.' % (pid, self.pidfile))
self.finalized = True
start.priority = 70
-
+
def exit(self):
try:
os.remove(self.pidfile)
@@ -421,14 +434,20 @@ class PIDFile(SimplePlugin):
pass
-class PerpetualTimer(threading._Timer):
- """A responsive subclass of threading._Timer whose run() method repeats.
-
+class PerpetualTimer(Timer):
+
+ """A responsive subclass of threading.Timer whose run() method repeats.
+
Use this timer only when you really need a very interruptible timer;
this checks its 'finished' condition up to 20 times a second, which can
- results in pretty high CPU usage
+ results in pretty high CPU usage
"""
-
+
+ def __init__(self, *args, **kwargs):
+ "Override parent constructor to allow 'bus' to be provided."
+ self.bus = kwargs.pop('bus', None)
+ super(PerpetualTimer, self).__init__(*args, **kwargs)
+
def run(self):
while True:
self.finished.wait(self.interval)
@@ -437,22 +456,25 @@ class PerpetualTimer(threading._Timer):
try:
self.function(*self.args, **self.kwargs)
except Exception:
- self.bus.log("Error in perpetual timer thread function %r." %
- self.function, level=40, traceback=True)
+ if self.bus:
+ self.bus.log(
+ "Error in perpetual timer thread function %r." %
+ self.function, level=40, traceback=True)
# Quit on first error to avoid massive logs.
raise
-class BackgroundTask(threading.Thread):
+class BackgroundTask(SetDaemonProperty, threading.Thread):
+
"""A subclass of threading.Thread whose run() method repeats.
-
+
Use this class for most repeating tasks. It uses time.sleep() to wait
for each interval, which isn't very responsive; that is, even if you call
self.cancel(), you'll have to wait until the sleep() call finishes before
the thread stops. To compensate, it defaults to being daemonic, which means
it won't delay stopping the whole process.
"""
-
+
def __init__(self, interval, function, args=[], kwargs={}, bus=None):
threading.Thread.__init__(self)
self.interval = interval
@@ -461,10 +483,13 @@ class BackgroundTask(threading.Thread):
self.kwargs = kwargs
self.running = False
self.bus = bus
-
+
+ # default to daemonic
+ self.daemon = True
+
def cancel(self):
self.running = False
-
+
def run(self):
self.running = True
while self.running:
@@ -479,48 +504,49 @@ class BackgroundTask(threading.Thread):
% self.function, level=40, traceback=True)
# Quit on first error to avoid massive logs.
raise
-
- def _set_daemon(self):
- return True
class Monitor(SimplePlugin):
+
"""WSPBus listener to periodically run a callback in its own thread."""
-
+
callback = None
"""The function to call at intervals."""
-
+
frequency = 60
"""The time in seconds between callback runs."""
-
+
thread = None
- """A :class:`BackgroundTask<cherrypy.process.plugins.BackgroundTask>` thread."""
-
+ """A :class:`BackgroundTask<cherrypy.process.plugins.BackgroundTask>`
+ thread.
+ """
+
def __init__(self, bus, callback, frequency=60, name=None):
SimplePlugin.__init__(self, bus)
self.callback = callback
self.frequency = frequency
self.thread = None
self.name = name
-
+
def start(self):
"""Start our callback in its own background thread."""
if self.frequency > 0:
threadname = self.name or self.__class__.__name__
if self.thread is None:
self.thread = BackgroundTask(self.frequency, self.callback,
- bus = self.bus)
+ bus=self.bus)
self.thread.setName(threadname)
self.thread.start()
self.bus.log("Started monitor thread %r." % threadname)
else:
self.bus.log("Monitor thread %r already started." % threadname)
start.priority = 70
-
+
def stop(self):
"""Stop our callback's background task thread."""
if self.thread is None:
- self.bus.log("No thread running for %s." % self.name or self.__class__.__name__)
+ self.bus.log("No thread running for %s." %
+ self.name or self.__class__.__name__)
else:
if self.thread is not threading.currentThread():
name = self.thread.getName()
@@ -530,7 +556,7 @@ class Monitor(SimplePlugin):
self.thread.join()
self.bus.log("Stopped thread %r." % name)
self.thread = None
-
+
def graceful(self):
"""Stop the callback's background task thread and restart it."""
self.stop()
@@ -538,102 +564,111 @@ class Monitor(SimplePlugin):
class Autoreloader(Monitor):
+
"""Monitor which re-executes the process when files change.
-
+
This :ref:`plugin<plugins>` restarts the process (via :func:`os.execv`)
if any of the files it monitors change (or is deleted). By default, the
autoreloader monitors all imported modules; you can add to the
set by adding to ``autoreload.files``::
-
+
cherrypy.engine.autoreload.files.add(myFile)
-
- If there are imported files you do *not* wish to monitor, you can adjust the
- ``match`` attribute, a regular expression. For example, to stop monitoring
- cherrypy itself::
-
+
+ If there are imported files you do *not* wish to monitor, you can
+ adjust the ``match`` attribute, a regular expression. For example,
+ to stop monitoring cherrypy itself::
+
cherrypy.engine.autoreload.match = r'^(?!cherrypy).+'
-
+
Like all :class:`Monitor<cherrypy.process.plugins.Monitor>` plugins,
the autoreload plugin takes a ``frequency`` argument. The default is
1 second; that is, the autoreloader will examine files once each second.
"""
-
+
files = None
"""The set of files to poll for modifications."""
-
+
frequency = 1
"""The interval in seconds at which to poll for modified files."""
-
+
match = '.*'
"""A regular expression by which to match filenames."""
-
+
def __init__(self, bus, frequency=1, match='.*'):
self.mtimes = {}
self.files = set()
self.match = match
Monitor.__init__(self, bus, self.run, frequency)
-
+
def start(self):
"""Start our own background task thread for self.run."""
if self.thread is None:
self.mtimes = {}
Monitor.start(self)
- start.priority = 70
-
+ start.priority = 70
+
def sysfiles(self):
"""Return a Set of sys.modules filenames to monitor."""
files = set()
- for k, m in sys.modules.items():
+ for k, m in list(sys.modules.items()):
if re.match(self.match, k):
- if hasattr(m, '__loader__') and hasattr(m.__loader__, 'archive'):
+ if (
+ hasattr(m, '__loader__') and
+ hasattr(m.__loader__, 'archive')
+ ):
f = m.__loader__.archive
else:
f = getattr(m, '__file__', None)
if f is not None and not os.path.isabs(f):
- # ensure absolute paths so a os.chdir() in the app doesn't break me
- f = os.path.normpath(os.path.join(_module__file__base, f))
+ # ensure absolute paths so a os.chdir() in the app
+ # doesn't break me
+ f = os.path.normpath(
+ os.path.join(_module__file__base, f))
files.add(f)
return files
-
+
def run(self):
"""Reload the process if registered files have been modified."""
for filename in self.sysfiles() | self.files:
if filename:
if filename.endswith('.pyc'):
filename = filename[:-1]
-
+
oldtime = self.mtimes.get(filename, 0)
if oldtime is None:
# Module with no .py file. Skip it.
continue
-
+
try:
mtime = os.stat(filename).st_mtime
except OSError:
# Either a module with no .py file, or it's been deleted.
mtime = None
-
+
if filename not in self.mtimes:
# If a module has no .py file, this will be None.
self.mtimes[filename] = mtime
else:
if mtime is None or mtime > oldtime:
# The file has been deleted or modified.
- self.bus.log("Restarting because %s changed." % filename)
+ self.bus.log("Restarting because %s changed." %
+ filename)
self.thread.cancel()
- self.bus.log("Stopped thread %r." % self.thread.getName())
+ self.bus.log("Stopped thread %r." %
+ self.thread.getName())
self.bus.restart()
return
class ThreadManager(SimplePlugin):
+
"""Manager for HTTP request threads.
-
+
If you have control over thread creation and destruction, publish to
the 'acquire_thread' and 'release_thread' channels (for each thread).
This will register/unregister the current thread and publish to
'start_thread' and 'stop_thread' listeners in the bus as needed.
-
+
If threads are created and destroyed by code you do not control
(e.g., Apache), then, at the beginning of every HTTP request,
publish to 'acquire_thread' only. You should not publish to
@@ -641,10 +676,10 @@ class ThreadManager(SimplePlugin):
the thread will be re-used or not. The bus will call
'stop_thread' listeners for you when it stops.
"""
-
+
threads = None
"""A map of {thread ident: index number} pairs."""
-
+
def __init__(self, bus):
self.threads = {}
SimplePlugin.__init__(self, bus)
@@ -655,7 +690,7 @@ class ThreadManager(SimplePlugin):
def acquire_thread(self):
"""Run 'start_thread' listeners for the current thread.
-
+
If the current thread has already been seen, any 'start_thread'
listeners will not be run again.
"""
@@ -666,18 +701,17 @@ class ThreadManager(SimplePlugin):
i = len(self.threads) + 1
self.threads[thread_ident] = i
self.bus.publish('start_thread', i)
-
+
def release_thread(self):
"""Release the current thread and run 'stop_thread' listeners."""
thread_ident = get_thread_ident()
i = self.threads.pop(thread_ident, None)
if i is not None:
self.bus.publish('stop_thread', i)
-
+
def stop(self):
"""Release all threads and run all 'stop_thread' listeners."""
for thread_ident, i in self.threads.items():
self.bus.publish('stop_thread', i)
self.threads.clear()
graceful = stop
-
diff --git a/cherrypy/process/servers.py b/cherrypy/process/servers.py
index fa714d65..fef37f77 100644
--- a/cherrypy/process/servers.py
+++ b/cherrypy/process/servers.py
@@ -13,7 +13,9 @@ protocols, etc.), you can manually register each one and then start them all
with engine.start::
s1 = ServerAdapter(cherrypy.engine, MyWSGIServer(host='0.0.0.0', port=80))
- s2 = ServerAdapter(cherrypy.engine, another.HTTPServer(host='127.0.0.1', SSL=True))
+ s2 = ServerAdapter(cherrypy.engine,
+ another.HTTPServer(host='127.0.0.1',
+ SSL=True))
s1.subscribe()
s2.subscribe()
cherrypy.engine.start()
@@ -54,16 +56,16 @@ hello.py::
#!/usr/bin/python
import cherrypy
-
+
class HelloWorld:
\"""Sample request handler class.\"""
def index(self):
return "Hello world!"
index.exposed = True
-
+
cherrypy.tree.mount(HelloWorld())
# CherryPy autoreload must be disabled for the flup server to work
- cherrypy.config.update({'engine.autoreload_on':False})
+ cherrypy.config.update({'engine.autoreload.on':False})
Then run :doc:`/deployguide/cherryd` with the '-f' arg::
@@ -107,75 +109,91 @@ directive, configure your fastcgi script like the following::
} # end of $HTTP["url"] =~ "^/"
Please see `Lighttpd FastCGI Docs
-<http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModFastCGI>`_ for an explanation
-of the possible configuration options.
+<http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModFastCGI>`_ for
+an explanation of the possible configuration options.
"""
import sys
import time
+import warnings
class ServerAdapter(object):
+
"""Adapter for an HTTP server.
-
+
If you need to start more than one HTTP server (to serve on multiple
ports, or protocols, etc.), you can manually register each one and then
start them all with bus.start:
-
+
s1 = ServerAdapter(bus, MyWSGIServer(host='0.0.0.0', port=80))
s2 = ServerAdapter(bus, another.HTTPServer(host='127.0.0.1', SSL=True))
s1.subscribe()
s2.subscribe()
bus.start()
"""
-
+
def __init__(self, bus, httpserver=None, bind_addr=None):
self.bus = bus
self.httpserver = httpserver
self.bind_addr = bind_addr
self.interrupt = None
self.running = False
-
+
def subscribe(self):
self.bus.subscribe('start', self.start)
self.bus.subscribe('stop', self.stop)
-
+
def unsubscribe(self):
self.bus.unsubscribe('start', self.start)
self.bus.unsubscribe('stop', self.stop)
-
+
def start(self):
"""Start the HTTP server."""
if self.bind_addr is None:
on_what = "unknown interface (dynamic?)"
elif isinstance(self.bind_addr, tuple):
- host, port = self.bind_addr
- on_what = "%s:%s" % (host, port)
+ on_what = self._get_base()
else:
on_what = "socket file: %s" % self.bind_addr
-
+
if self.running:
self.bus.log("Already serving on %s" % on_what)
return
-
+
self.interrupt = None
if not self.httpserver:
raise ValueError("No HTTP server has been created.")
-
+
# Start the httpserver in a new thread.
if isinstance(self.bind_addr, tuple):
wait_for_free_port(*self.bind_addr)
-
+
import threading
t = threading.Thread(target=self._start_http_thread)
t.setName("HTTPServer " + t.getName())
t.start()
-
+
self.wait()
self.running = True
self.bus.log("Serving on %s" % on_what)
start.priority = 75
-
+
+ def _get_base(self):
+ if not self.httpserver:
+ return ''
+ host, port = self.bind_addr
+ if getattr(self.httpserver, 'ssl_certificate', None):
+ scheme = "https"
+ if port != 443:
+ host += ":%s" % port
+ else:
+ scheme = "http"
+ if port != 80:
+ host += ":%s" % port
+
+ return "%s://%s" % (scheme, host)
+
def _start_http_thread(self):
"""HTTP servers MUST be running in new threads, so that the
main thread persists to receive KeyboardInterrupt's. If an
@@ -200,19 +218,19 @@ class ServerAdapter(object):
traceback=True, level=40)
self.bus.exit()
raise
-
+
def wait(self):
"""Wait until the HTTP server is ready to receive requests."""
while not getattr(self.httpserver, "ready", False):
if self.interrupt:
raise self.interrupt
time.sleep(.1)
-
+
# Wait for port to be occupied
if isinstance(self.bind_addr, tuple):
host, port = self.bind_addr
wait_for_occupied_port(host, port)
-
+
def stop(self):
"""Stop the HTTP server."""
if self.running:
@@ -226,7 +244,7 @@ class ServerAdapter(object):
else:
self.bus.log("HTTP Server %s already shut down" % self.httpserver)
stop.priority = 25
-
+
def restart(self):
"""Restart the HTTP server."""
self.stop()
@@ -234,31 +252,33 @@ class ServerAdapter(object):
class FlupCGIServer(object):
+
"""Adapter for a flup.server.cgi.WSGIServer."""
-
+
def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
self.ready = False
-
+
def start(self):
"""Start the CGI server."""
# We have to instantiate the server class here because its __init__
# starts a threadpool. If we do it too early, daemonize won't work.
from flup.server.cgi import WSGIServer
-
+
self.cgiserver = WSGIServer(*self.args, **self.kwargs)
self.ready = True
self.cgiserver.run()
-
+
def stop(self):
"""Stop the HTTP server."""
self.ready = False
class FlupFCGIServer(object):
+
"""Adapter for a flup.server.fcgi.WSGIServer."""
-
+
def __init__(self, *args, **kwargs):
if kwargs.get('bindAddress', None) is None:
import socket
@@ -270,7 +290,7 @@ class FlupFCGIServer(object):
self.args = args
self.kwargs = kwargs
self.ready = False
-
+
def start(self):
"""Start the FCGI server."""
# We have to instantiate the server class here because its __init__
@@ -290,24 +310,26 @@ class FlupFCGIServer(object):
self.fcgiserver._oldSIGs = []
self.ready = True
self.fcgiserver.run()
-
+
def stop(self):
"""Stop the HTTP server."""
# Forcibly stop the fcgi server main event loop.
self.fcgiserver._keepGoing = False
# Force all worker threads to die off.
- self.fcgiserver._threadPool.maxSpare = self.fcgiserver._threadPool._idleCount
+ self.fcgiserver._threadPool.maxSpare = (
+ self.fcgiserver._threadPool._idleCount)
self.ready = False
class FlupSCGIServer(object):
+
"""Adapter for a flup.server.scgi.WSGIServer."""
-
+
def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
self.ready = False
-
+
def start(self):
"""Start the SCGI server."""
# We have to instantiate the server class here because its __init__
@@ -327,7 +349,7 @@ class FlupSCGIServer(object):
self.scgiserver._oldSIGs = []
self.ready = True
self.scgiserver.run()
-
+
def stop(self):
"""Stop the HTTP server."""
self.ready = False
@@ -344,19 +366,21 @@ def client_host(server_host):
return '127.0.0.1'
if server_host in ('::', '::0', '::0.0.0.0'):
# :: is IN6ADDR_ANY, which should answer on localhost.
- # ::0 and ::0.0.0.0 are non-canonical but common ways to write IN6ADDR_ANY.
+ # ::0 and ::0.0.0.0 are non-canonical but common
+ # ways to write IN6ADDR_ANY.
return '::1'
return server_host
+
def check_port(host, port, timeout=1.0):
"""Raise an error if the given port is not free on the given host."""
if not host:
raise ValueError("Host values of '' or None are not allowed.")
host = client_host(host)
port = int(port)
-
+
import socket
-
+
# AF_INET or AF_INET6 socket
# Get the correct address family for our host (allows IPv6 addresses)
try:
@@ -364,10 +388,12 @@ def check_port(host, port, timeout=1.0):
socket.SOCK_STREAM)
except socket.gaierror:
if ':' in host:
- info = [(socket.AF_INET6, socket.SOCK_STREAM, 0, "", (host, port, 0, 0))]
+ info = [(
+ socket.AF_INET6, socket.SOCK_STREAM, 0, "", (host, port, 0, 0)
+ )]
else:
info = [(socket.AF_INET, socket.SOCK_STREAM, 0, "", (host, port))]
-
+
for res in info:
af, socktype, proto, canonname, sa = res
s = None
@@ -378,25 +404,27 @@ def check_port(host, port, timeout=1.0):
s.settimeout(timeout)
s.connect((host, port))
s.close()
- raise IOError("Port %s is in use on %s; perhaps the previous "
- "httpserver did not shut down properly." %
- (repr(port), repr(host)))
except socket.error:
if s:
s.close()
+ else:
+ raise IOError("Port %s is in use on %s; perhaps the previous "
+ "httpserver did not shut down properly." %
+ (repr(port), repr(host)))
# Feel free to increase these defaults on slow systems:
free_port_timeout = 0.1
occupied_port_timeout = 1.0
+
def wait_for_free_port(host, port, timeout=None):
"""Wait for the specified port to become free (drop requests)."""
if not host:
raise ValueError("Host values of '' or None are not allowed.")
if timeout is None:
timeout = free_port_timeout
-
+
for trial in range(50):
try:
# we are expecting a free port, so reduce the timeout
@@ -406,22 +434,32 @@ def wait_for_free_port(host, port, timeout=None):
time.sleep(timeout)
else:
return
-
+
raise IOError("Port %r not free on %r" % (port, host))
+
def wait_for_occupied_port(host, port, timeout=None):
"""Wait for the specified port to become active (receive requests)."""
if not host:
raise ValueError("Host values of '' or None are not allowed.")
if timeout is None:
timeout = occupied_port_timeout
-
+
for trial in range(50):
try:
check_port(host, port, timeout=timeout)
except IOError:
+ # port is occupied
return
else:
time.sleep(timeout)
-
- raise IOError("Port %r not bound on %r" % (port, host))
+
+ if host == client_host(host):
+ raise IOError("Port %r not bound on %r" % (port, host))
+
+ # On systems where a loopback interface is not available and the
+ # server is bound to all interfaces, it's difficult to determine
+ # whether the server is in fact occupying the port. In this case,
+ # just issue a warning and move on. See issue #1100.
+ msg = "Unable to verify that the server is bound on %r" % port
+ warnings.warn(msg)
diff --git a/cherrypy/process/win32.py b/cherrypy/process/win32.py
index 83f99a5d..4afd3f14 100644
--- a/cherrypy/process/win32.py
+++ b/cherrypy/process/win32.py
@@ -11,17 +11,18 @@ from cherrypy.process import wspbus, plugins
class ConsoleCtrlHandler(plugins.SimplePlugin):
+
"""A WSPBus plugin for handling Win32 console events (like Ctrl-C)."""
-
+
def __init__(self, bus):
self.is_set = False
plugins.SimplePlugin.__init__(self, bus)
-
+
def start(self):
if self.is_set:
self.bus.log('Handler for console events already set.', level=40)
return
-
+
result = win32api.SetConsoleCtrlHandler(self.handle, 1)
if result == 0:
self.bus.log('Could not SetConsoleCtrlHandler (error %r)' %
@@ -29,38 +30,38 @@ class ConsoleCtrlHandler(plugins.SimplePlugin):
else:
self.bus.log('Set handler for console events.', level=40)
self.is_set = True
-
+
def stop(self):
if not self.is_set:
self.bus.log('Handler for console events already off.', level=40)
return
-
+
try:
result = win32api.SetConsoleCtrlHandler(self.handle, 0)
except ValueError:
# "ValueError: The object has not been registered"
result = 1
-
+
if result == 0:
self.bus.log('Could not remove SetConsoleCtrlHandler (error %r)' %
win32api.GetLastError(), level=40)
else:
self.bus.log('Removed handler for console events.', level=40)
self.is_set = False
-
+
def handle(self, event):
"""Handle console control events (like Ctrl-C)."""
if event in (win32con.CTRL_C_EVENT, win32con.CTRL_LOGOFF_EVENT,
win32con.CTRL_BREAK_EVENT, win32con.CTRL_SHUTDOWN_EVENT,
win32con.CTRL_CLOSE_EVENT):
self.bus.log('Console event %s: shutting down bus' % event)
-
+
# Remove self immediately so repeated Ctrl-C doesn't re-call it.
try:
self.stop()
except ValueError:
pass
-
+
self.bus.exit()
# 'First to return True stops the calls'
return 1
@@ -68,15 +69,16 @@ class ConsoleCtrlHandler(plugins.SimplePlugin):
class Win32Bus(wspbus.Bus):
+
"""A Web Site Process Bus implementation for Win32.
-
+
Instead of time.sleep, this bus blocks using native win32event objects.
"""
-
+
def __init__(self):
self.events = {}
wspbus.Bus.__init__(self)
-
+
def _get_state_event(self, state):
"""Return a win32event for the given state (creating it if needed)."""
try:
@@ -87,18 +89,19 @@ class Win32Bus(wspbus.Bus):
(state.name, os.getpid()))
self.events[state] = event
return event
-
+
def _get_state(self):
return self._state
+
def _set_state(self, value):
self._state = value
event = self._get_state_event(value)
win32event.PulseEvent(event)
state = property(_get_state, _set_state)
-
+
def wait(self, state, interval=0.1, channel=None):
"""Wait for the given state(s), KeyboardInterrupt or SystemExit.
-
+
Since this class uses native win32event objects, the interval
argument is ignored.
"""
@@ -106,7 +109,8 @@ class Win32Bus(wspbus.Bus):
# Don't wait for an event that beat us to the punch ;)
if self.state not in state:
events = tuple([self._get_state_event(s) for s in state])
- win32event.WaitForMultipleObjects(events, 0, win32event.INFINITE)
+ win32event.WaitForMultipleObjects(
+ events, 0, win32event.INFINITE)
else:
# Don't wait for an event that beat us to the punch ;)
if self.state != state:
@@ -115,16 +119,17 @@ class Win32Bus(wspbus.Bus):
class _ControlCodes(dict):
+
"""Control codes used to "signal" a service via ControlService.
-
+
User-defined control codes are in the range 128-255. We generally use
the standard Python value for the Linux signal and add 128. Example:
-
+
>>> signal.SIGUSR1
10
control_codes['graceful'] = 128 + 10
"""
-
+
def key_for(self, obj):
"""For the given value, return its corresponding key."""
for key, val in self.items():
@@ -145,27 +150,28 @@ def signal_child(service, command):
class PyWebService(win32serviceutil.ServiceFramework):
+
"""Python Web Service."""
-
+
_svc_name_ = "Python Web Service"
_svc_display_name_ = "Python Web Service"
_svc_deps_ = None # sequence of service names on which this depends
_exe_name_ = "pywebsvc"
_exe_args_ = None # Default to no arguments
-
+
# Only exists on Windows 2000 or later, ignored on windows NT
_svc_description_ = "Python Web Service"
-
+
def SvcDoRun(self):
from cherrypy import process
process.bus.start()
process.bus.block()
-
+
def SvcStop(self):
from cherrypy import process
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
process.bus.exit()
-
+
def SvcOther(self, control):
process.bus.publish(control_codes.key_for(control))
diff --git a/cherrypy/process/wspbus.py b/cherrypy/process/wspbus.py
index 6ef768dc..5409d038 100644
--- a/cherrypy/process/wspbus.py
+++ b/cherrypy/process/wspbus.py
@@ -78,24 +78,27 @@ from cherrypy._cpcompat import set
# sys.executable is a relative-path, and/or cause other problems).
_startup_cwd = os.getcwd()
+
class ChannelFailures(Exception):
- """Exception raised when errors occur in a listener during Bus.publish()."""
+
+ """Exception raised when errors occur in a listener during Bus.publish().
+ """
delimiter = '\n'
-
+
def __init__(self, *args, **kwargs):
# Don't use 'super' here; Exceptions are old-style in Py2.4
- # See http://www.cherrypy.org/ticket/959
+ # See https://bitbucket.org/cherrypy/cherrypy/issue/959
Exception.__init__(self, *args, **kwargs)
self._exceptions = list()
-
+
def handle_exception(self):
"""Append the current exception to self."""
self._exceptions.append(sys.exc_info()[1])
-
+
def get_instances(self):
"""Return a list of seen exception instances."""
return self._exceptions[:]
-
+
def __str__(self):
exception_strings = map(repr, self.get_instances())
return self.delimiter.join(exception_strings)
@@ -107,12 +110,16 @@ class ChannelFailures(Exception):
__nonzero__ = __bool__
# Use a flag to indicate the state of the bus.
+
+
class _StateEnum(object):
+
class State(object):
name = None
+
def __repr__(self):
return "states.%s" % self.name
-
+
def __setattr__(self, key, value):
if isinstance(value, self.State):
value.name = key
@@ -137,20 +144,21 @@ else:
class Bus(object):
+
"""Process state-machine and messenger for HTTP site deployment.
-
+
All listeners for a given channel are guaranteed to be called even
if others at the same channel fail. Each failure is logged, but
execution proceeds on to the next listener. The only way to stop all
processing from inside a listener is to raise SystemExit and stop the
whole server.
"""
-
+
states = states
state = states.STOPPED
execv = False
max_cloexec_files = max_files
-
+
def __init__(self):
self.execv = False
self.state = states.STOPPED
@@ -158,32 +166,32 @@ class Bus(object):
[(channel, set()) for channel
in ('start', 'stop', 'exit', 'graceful', 'log', 'main')])
self._priorities = {}
-
+
def subscribe(self, channel, callback, priority=None):
"""Add the given callback at the given channel (if not present)."""
if channel not in self.listeners:
self.listeners[channel] = set()
self.listeners[channel].add(callback)
-
+
if priority is None:
priority = getattr(callback, 'priority', 50)
self._priorities[(channel, callback)] = priority
-
+
def unsubscribe(self, channel, callback):
"""Discard the given callback (if present)."""
listeners = self.listeners.get(channel)
if listeners and callback in listeners:
listeners.discard(callback)
del self._priorities[(channel, callback)]
-
+
def publish(self, channel, *args, **kwargs):
"""Return output of all subscribers for the given channel."""
if channel not in self.listeners:
return []
-
+
exc = ChannelFailures()
output = []
-
+
items = [(self._priorities[(channel, listener)], listener)
for listener in self.listeners[channel]]
try:
@@ -214,7 +222,7 @@ class Bus(object):
if exc:
raise exc
return output
-
+
def _clean_exit(self):
"""An atexit handler which asserts the Bus is not running."""
if self.state != states.EXITING:
@@ -224,11 +232,11 @@ class Bus(object):
"bus.block() after start(), or call bus.exit() before the "
"main thread exits." % self.state, RuntimeWarning)
self.exit()
-
+
def start(self):
"""Start all services."""
atexit.register(self._clean_exit)
-
+
self.state = states.STARTING
self.log('Bus STARTING')
try:
@@ -248,13 +256,13 @@ class Bus(object):
pass
# Re-raise the original error
raise e_info
-
+
def exit(self):
"""Stop all services and prepare to exit the process."""
exitstate = self.state
try:
self.stop()
-
+
self.state = states.EXITING
self.log('Bus EXITING')
self.publish('exit')
@@ -266,32 +274,32 @@ class Bus(object):
# signal handler, console handler, or atexit handler), so we
# can't just let exceptions propagate out unhandled.
# Assume it's been logged and just die.
- os._exit(70) # EX_SOFTWARE
-
+ os._exit(70) # EX_SOFTWARE
+
if exitstate == states.STARTING:
# exit() was called before start() finished, possibly due to
# Ctrl-C because a start listener got stuck. In this case,
# we could get stuck in a loop where Ctrl-C never exits the
# process, so we just call os.exit here.
- os._exit(70) # EX_SOFTWARE
-
+ os._exit(70) # EX_SOFTWARE
+
def restart(self):
"""Restart the process (may close connections).
-
+
This method does not restart the process from the calling thread;
instead, it stops the bus and asks the main thread to call execv.
"""
self.execv = True
self.exit()
-
+
def graceful(self):
"""Advise all services to reload."""
self.log('Bus graceful')
self.publish('graceful')
-
+
def block(self, interval=0.1):
"""Wait for the EXITING state, KeyboardInterrupt or SystemExit.
-
+
This function is intended to be called only by the main thread.
After waiting for the EXITING state, it also waits for all threads
to terminate, and then calls os.execv if self.execv is True. This
@@ -309,15 +317,23 @@ class Bus(object):
self.log('SystemExit raised: shutting down bus')
self.exit()
raise
-
+
# Waiting for ALL child threads to finish is necessary on OS X.
- # See http://www.cherrypy.org/ticket/581.
+ # See https://bitbucket.org/cherrypy/cherrypy/issue/581.
# It's also good to let them all shut down before allowing
# the main thread to call atexit handlers.
- # See http://www.cherrypy.org/ticket/751.
+ # See https://bitbucket.org/cherrypy/cherrypy/issue/751.
self.log("Waiting for child threads to terminate...")
for t in threading.enumerate():
- if t != threading.currentThread() and t.isAlive():
+ # Validate the we're not trying to join the MainThread
+ # that will cause a deadlock and the case exist when
+ # implemented as a windows service and in any other case
+ # that another thread executes cherrypy.engine.exit()
+ if (
+ t != threading.currentThread() and
+ t.isAlive() and
+ not isinstance(t, threading._MainThread)
+ ):
# Note that any dummy (external) threads are always daemonic.
if hasattr(threading.Thread, "daemon"):
# Python 2.6+
@@ -327,22 +343,22 @@ class Bus(object):
if not d:
self.log("Waiting for thread %s." % t.getName())
t.join()
-
+
if self.execv:
self._do_execv()
-
+
def wait(self, state, interval=0.1, channel=None):
"""Poll for the given state(s) at intervals; publish to channel."""
if isinstance(state, (tuple, list)):
states = state
else:
states = [state]
-
+
def _wait():
while self.state not in states:
time.sleep(interval)
self.publish(channel)
-
+
# From http://psyco.sourceforge.net/psycoguide/bugs.html:
# "The compiled machine code does not include the regular polling
# done by Python, meaning that a KeyboardInterrupt will not be
@@ -353,18 +369,18 @@ class Bus(object):
sys.modules['psyco'].cannotcompile(_wait)
except (KeyError, AttributeError):
pass
-
+
_wait()
-
+
def _do_execv(self):
"""Re-execute the current process.
-
+
This must be called from the main thread, because certain platforms
(OS X) don't allow execv to be called in a child thread very well.
"""
args = sys.argv[:]
self.log('Re-spawning %s' % ' '.join(args))
-
+
if sys.platform[:4] == 'java':
from _systemrestart import SystemRestart
raise SystemRestart
@@ -377,25 +393,25 @@ class Bus(object):
if self.max_cloexec_files:
self._set_cloexec()
os.execv(sys.executable, args)
-
+
def _set_cloexec(self):
"""Set the CLOEXEC flag on all open files (except stdin/out/err).
-
+
If self.max_cloexec_files is an integer (the default), then on
platforms which support it, it represents the max open files setting
for the operating system. This function will be called just before
the process is restarted via os.execv() to prevent open files
from persisting into the new process.
-
+
Set self.max_cloexec_files to 0 to disable this behavior.
"""
- for fd in range(3, self.max_cloexec_files): # skip stdin/out/err
+ for fd in range(3, self.max_cloexec_files): # skip stdin/out/err
try:
flags = fcntl.fcntl(fd, fcntl.F_GETFD)
except IOError:
continue
fcntl.fcntl(fd, fcntl.F_SETFD, flags | fcntl.FD_CLOEXEC)
-
+
def stop(self):
"""Stop all services."""
self.state = states.STOPPING
@@ -403,7 +419,7 @@ class Bus(object):
self.publish('stop')
self.state = states.STOPPED
self.log('Bus STOPPED')
-
+
def start_with_callback(self, func, args=None, kwargs=None):
"""Start 'func' in a new thread T, then start self (and return T)."""
if args is None:
@@ -411,18 +427,18 @@ class Bus(object):
if kwargs is None:
kwargs = {}
args = (func,) + args
-
+
def _callback(func, *a, **kw):
self.wait(states.STARTED)
func(*a, **kw)
t = threading.Thread(target=_callback, args=args, kwargs=kwargs)
t.setName('Bus Callback ' + t.getName())
t.start()
-
+
self.start()
-
+
return t
-
+
def log(self, msg="", level=20, traceback=False):
"""Log the given message. Append the last traceback if requested."""
if traceback:
diff --git a/cherrypy/scaffold/__init__.py b/cherrypy/scaffold/__init__.py
index 00964ac5..50de34bb 100644
--- a/cherrypy/scaffold/__init__.py
+++ b/cherrypy/scaffold/__init__.py
@@ -20,10 +20,10 @@ local_dir = os.path.join(os.getcwd(), os.path.dirname(__file__))
class Root:
-
+
_cp_config = {'tools.log_tracebacks.on': True,
}
-
+
def index(self):
return """<html>
<body>Try some <a href='%s?a=7'>other</a> path,
@@ -33,11 +33,11 @@ Or, just look at the pretty picture:<br />
</body></html>""" % (url("other"), url("else"),
url("files/made_with_cherrypy_small.png"))
index.exposed = True
-
+
def default(self, *args, **kwargs):
return "args: %s kwargs: %s" % (args, kwargs)
default.exposed = True
-
+
def other(self, a=2, b='bananas', c=None):
cherrypy.response.headers['Content-Type'] = 'text/plain'
if c is None:
@@ -45,13 +45,13 @@ Or, just look at the pretty picture:<br />
else:
return "Have %d %s, %s." % (int(a), b, c)
other.exposed = True
-
+
files = cherrypy.tools.staticdir.handler(
- section="/files",
- dir=os.path.join(local_dir, "static"),
- # Ignore .php files, etc.
+ section="/files",
+ dir=os.path.join(local_dir, "static"),
+ # Ignore .php files, etc.
match=r'\.(css|gif|html?|ico|jpe?g|js|png|swf|xml)$',
- )
+ )
root = Root()
diff --git a/cherrypy/test/__init__.py b/cherrypy/test/__init__.py
index 77036070..0927c170 100644
--- a/cherrypy/test/__init__.py
+++ b/cherrypy/test/__init__.py
@@ -11,15 +11,18 @@ interactive input.
import os
import sys
+
def newexit():
os._exit(1)
+
def setup():
# We want to monkey patch sys.exit so that we can get some
# information about where exit is being called.
newexit._old = sys.exit
sys.exit = newexit
+
def teardown():
try:
sys.exit = sys.exit._old
diff --git a/cherrypy/test/_test_decorators.py b/cherrypy/test/_test_decorators.py
index 5bcbc1e6..666ea6d9 100644
--- a/cherrypy/test/_test_decorators.py
+++ b/cherrypy/test/_test_decorators.py
@@ -5,30 +5,30 @@ from cherrypy._cpcompat import ntob
class ExposeExamples(object):
-
+
@expose
def no_call(self):
return "Mr E. R. Bradshaw"
-
+
@expose()
def call_empty(self):
return "Mrs. B.J. Smegma"
-
+
@expose("call_alias")
def nesbitt(self):
return "Mr Nesbitt"
-
+
@expose(["alias1", "alias2"])
def andrews(self):
return "Mr Ken Andrews"
-
+
@expose(alias="alias3")
def watson(self):
return "Mr. and Mrs. Watson"
class ToolExamples(object):
-
+
@expose
@tools.response_headers(headers=[('Content-Type', 'application/data')])
def blah(self):
@@ -37,5 +37,3 @@ class ToolExamples(object):
# the _cp_config attribute added by the Tool decorator. You have
# to write _cp_config[k] = v or _cp_config.update(...) instead.
blah._cp_config['response.stream'] = True
-
-
diff --git a/cherrypy/test/_test_states_demo.py b/cherrypy/test/_test_states_demo.py
index 3f8f196c..f90f14f9 100644
--- a/cherrypy/test/_test_states_demo.py
+++ b/cherrypy/test/_test_states_demo.py
@@ -7,23 +7,23 @@ import cherrypy
class Root:
-
+
def index(self):
return "Hello World"
index.exposed = True
-
+
def mtimes(self):
return repr(cherrypy.engine.publish("Autoreloader", "mtimes"))
mtimes.exposed = True
-
+
def pid(self):
return str(os.getpid())
pid.exposed = True
-
+
def start(self):
return repr(starttime)
start.exposed = True
-
+
def exit(self):
# This handler might be called before the engine is STARTED if an
# HTTP worker thread handles it before the HTTP server returns
@@ -32,7 +32,7 @@ class Root:
cherrypy.engine.wait(state=cherrypy.engine.states.STARTED)
cherrypy.engine.exit()
exit.exposed = True
-
+
def unsub_sig():
cherrypy.log("unsubsig: %s" % cherrypy.config.get('unsubsig', False))
@@ -57,9 +57,11 @@ def starterror():
zerodiv = 1 / 0
cherrypy.engine.subscribe('start', starterror, priority=6)
+
def log_test_case_name():
if cherrypy.config.get('test_case_name', False):
- cherrypy.log("STARTED FROM: %s" % cherrypy.config.get('test_case_name'))
+ cherrypy.log("STARTED FROM: %s" %
+ cherrypy.config.get('test_case_name'))
cherrypy.engine.subscribe('start', log_test_case_name, priority=6)
diff --git a/cherrypy/test/benchmark.py b/cherrypy/test/benchmark.py
index bd5deb60..cb3ce9f6 100644
--- a/cherrypy/test/benchmark.py
+++ b/cherrypy/test/benchmark.py
@@ -1,8 +1,8 @@
"""CherryPy Benchmark Tool
Usage:
- benchmark.py --null --notests --help --cpmodpy --modpython --ab=path --apache=path
-
+ benchmark.py [options]
+
--null: use a null Request object (to bench the HTTP server only)
--notests: start the server but do not run the tests; this allows
you to check the tested pages with a browser
@@ -11,10 +11,10 @@
--modpython: run tests via apache on 54583 (with modpython_gateway)
--ab=path: Use the ab script/executable at 'path' (see below)
--apache=path: Use the apache script/exe at 'path' (see below)
-
+
To run the benchmarks, the Apache Benchmark tool "ab" must either be on
your system path, or specified via the --ab=path option.
-
+
To run the modpython tests, the "apache" executable or script must be
on your system path, or provided via the --apache=path option. On some
platforms, "apache" may be called "apachectl" or "apache2ctl"--create
@@ -47,8 +47,9 @@ __all__ = ['ABSession', 'Root', 'print_report',
size_cache = {}
+
class Root:
-
+
def index(self):
return """<html>
<head>
@@ -65,11 +66,11 @@ class Root:
</body>
</html>"""
index.exposed = True
-
+
def hello(self):
return "Hello, world\r\n"
hello.exposed = True
-
+
def sizer(self, size):
resp = size_cache.get(size, None)
if resp is None:
@@ -86,7 +87,7 @@ cherrypy.config.update({
'server.max_request_header_size': 0,
'server.max_request_body_size': 0,
'engine.deadlock_poll_freq': 0,
- })
+})
# Cheat mode on ;)
del cherrypy.config['tools.log_tracebacks.on']
@@ -98,20 +99,21 @@ appconf = {
'tools.staticdir.on': True,
'tools.staticdir.dir': 'static',
'tools.staticdir.root': curdir,
- },
- }
+ },
+}
app = cherrypy.tree.mount(Root(), SCRIPT_NAME, appconf)
class NullRequest:
+
"""A null HTTP request class, returning 200 and an empty body."""
-
+
def __init__(self, local, remote, scheme="http"):
pass
-
+
def close(self):
pass
-
+
def run(self, method, path, query_string, protocol, headers, rfile):
cherrypy.response.status = "200 OK"
cherrypy.response.header_list = [("Content-Type", 'text/html'),
@@ -128,6 +130,7 @@ class NullResponse:
class ABSession:
+
"""A session of 'ab', the Apache HTTP server benchmarking tool.
Example output from ab:
@@ -186,33 +189,36 @@ Percentage of the requests served within a certain time (ms)
100% 130 (longest request)
Finished 1000 requests
"""
-
- parse_patterns = [('complete_requests', 'Completed',
- ntob(r'^Complete requests:\s*(\d+)')),
- ('failed_requests', 'Failed',
- ntob(r'^Failed requests:\s*(\d+)')),
- ('requests_per_second', 'req/sec',
- ntob(r'^Requests per second:\s*([0-9.]+)')),
- ('time_per_request_concurrent', 'msec/req',
- ntob(r'^Time per request:\s*([0-9.]+).*concurrent requests\)$')),
- ('transfer_rate', 'KB/sec',
- ntob(r'^Transfer rate:\s*([0-9.]+)')),
- ]
-
- def __init__(self, path=SCRIPT_NAME + "/hello", requests=1000, concurrency=10):
+
+ parse_patterns = [
+ ('complete_requests', 'Completed',
+ ntob(r'^Complete requests:\s*(\d+)')),
+ ('failed_requests', 'Failed',
+ ntob(r'^Failed requests:\s*(\d+)')),
+ ('requests_per_second', 'req/sec',
+ ntob(r'^Requests per second:\s*([0-9.]+)')),
+ ('time_per_request_concurrent', 'msec/req',
+ ntob(r'^Time per request:\s*([0-9.]+).*concurrent requests\)$')),
+ ('transfer_rate', 'KB/sec',
+ ntob(r'^Transfer rate:\s*([0-9.]+)'))
+ ]
+
+ def __init__(self, path=SCRIPT_NAME + "/hello", requests=1000,
+ concurrency=10):
self.path = path
self.requests = requests
self.concurrency = concurrency
-
+
def args(self):
port = cherrypy.server.socket_port
assert self.concurrency > 0
assert self.requests > 0
# Don't use "localhost".
- # Cf http://mail.python.org/pipermail/python-win32/2008-March/007050.html
+ # Cf
+ # http://mail.python.org/pipermail/python-win32/2008-March/007050.html
return ("-k -n %s -c %s http://127.0.0.1:%s%s" %
(self.requests, self.concurrency, port, self.path))
-
+
def run(self):
# Parse output of ab, setting attributes on self
try:
@@ -220,7 +226,7 @@ Finished 1000 requests
except:
print(_cperror.format_exc())
raise
-
+
for attr, name, pattern in self.parse_patterns:
val = re.search(pattern, self.output, re.MULTILINE)
if val:
@@ -240,7 +246,7 @@ def thread_report(path=SCRIPT_NAME + "/hello", concurrency=safe_threads):
sess = ABSession(path)
attrs, names, patterns = list(zip(*sess.parse_patterns))
avg = dict.fromkeys(attrs, 0.0)
-
+
yield ('threads',) + names
for c in concurrency:
sess.concurrency = c
@@ -257,12 +263,13 @@ def thread_report(path=SCRIPT_NAME + "/hello", concurrency=safe_threads):
row.append(val)
if row:
yield row
-
+
# Add a row of averages.
yield ["Average"] + [str(avg[attr] / len(concurrency)) for attr in attrs]
+
def size_report(sizes=(10, 100, 1000, 10000, 100000, 100000000),
- concurrency=50):
+ concurrency=50):
sess = ABSession(concurrency=concurrency)
attrs, names, patterns = list(zip(*sess.parse_patterns))
yield ('bytes',) + names
@@ -271,6 +278,7 @@ def size_report(sizes=(10, 100, 1000, 10000, 100000, 100000000),
sess.run()
yield [sz] + [getattr(sess, attr) for attr in attrs]
+
def print_report(rows):
for row in rows:
print("")
@@ -282,24 +290,25 @@ def print_report(rows):
def run_standard_benchmarks():
print("")
print("Client Thread Report (1000 requests, 14 byte response body, "
- "%s server threads):" % cherrypy.server.thread_pool)
+ "%s server threads):" % cherrypy.server.thread_pool)
print_report(thread_report())
-
+
print("")
print("Client Thread Report (1000 requests, 14 bytes via staticdir, "
- "%s server threads):" % cherrypy.server.thread_pool)
+ "%s server threads):" % cherrypy.server.thread_pool)
print_report(thread_report("%s/static/index.html" % SCRIPT_NAME))
-
+
print("")
print("Size Report (1000 requests, 50 client threads, "
- "%s server threads):" % cherrypy.server.thread_pool)
+ "%s server threads):" % cherrypy.server.thread_pool)
print_report(size_report())
# modpython and other WSGI #
def startup_modpython(req=None):
- """Start the CherryPy app server in 'serverless' mode (for modpython/WSGI)."""
+ """Start the CherryPy app server in 'serverless' mode (for modpython/WSGI).
+ """
if cherrypy.engine.state == cherrypy._cpengine.STOPPED:
if req:
if "nullreq" in req.get_options():
@@ -312,30 +321,33 @@ def startup_modpython(req=None):
cherrypy.engine.start()
if cherrypy.engine.state == cherrypy._cpengine.STARTING:
cherrypy.engine.wait()
- return 0 # apache.OK
+ return 0 # apache.OK
def run_modpython(use_wsgi=False):
print("Starting mod_python...")
pyopts = []
-
+
# Pass the null and ab=path options through Apache
if "--null" in opts:
pyopts.append(("nullreq", ""))
-
+
if "--ab" in opts:
pyopts.append(("ab", opts["--ab"]))
-
+
s = _cpmodpy.ModPythonServer
if use_wsgi:
pyopts.append(("wsgi.application", "cherrypy::tree"))
- pyopts.append(("wsgi.startup", "cherrypy.test.benchmark::startup_modpython"))
+ pyopts.append(
+ ("wsgi.startup", "cherrypy.test.benchmark::startup_modpython"))
handler = "modpython_gateway::handler"
- s = s(port=54583, opts=pyopts, apache_path=APACHE_PATH, handler=handler)
+ s = s(port=54583, opts=pyopts,
+ apache_path=APACHE_PATH, handler=handler)
else:
- pyopts.append(("cherrypy.setup", "cherrypy.test.benchmark::startup_modpython"))
+ pyopts.append(
+ ("cherrypy.setup", "cherrypy.test.benchmark::startup_modpython"))
s = s(port=54583, opts=pyopts, apache_path=APACHE_PATH)
-
+
try:
s.start()
run()
@@ -343,7 +355,6 @@ def run_modpython(use_wsgi=False):
s.stop()
-
if __name__ == '__main__':
longopts = ['cpmodpy', 'modpython', 'null', 'notests',
'help', 'ab=', 'apache=']
@@ -353,22 +364,22 @@ if __name__ == '__main__':
except getopt.GetoptError:
print(__doc__)
sys.exit(2)
-
+
if "--help" in opts:
print(__doc__)
sys.exit(0)
-
+
if "--ab" in opts:
AB_PATH = opts['--ab']
-
+
if "--notests" in opts:
# Return without stopping the server, so that the pages
# can be tested from a standard web browser.
def run():
port = cherrypy.server.socket_port
print("You may now open http://127.0.0.1:%s%s/" %
- (port, SCRIPT_NAME))
-
+ (port, SCRIPT_NAME))
+
if "--null" in opts:
print("Using null Request object")
else:
@@ -385,17 +396,19 @@ if __name__ == '__main__':
raise
finally:
cherrypy.engine.exit()
-
+
print("Starting CherryPy app server...")
-
+
class NullWriter(object):
+
"""Suppresses the printing of socket errors."""
+
def write(self, data):
pass
sys.stderr = NullWriter()
-
+
start = time.time()
-
+
if "--cpmodpy" in opts:
run_modpython()
elif "--modpython" in opts:
@@ -404,6 +417,6 @@ if __name__ == '__main__':
if "--null" in opts:
cherrypy.server.request_class = NullRequest
cherrypy.server.response_class = NullResponse
-
+
cherrypy.engine.start_with_callback(run)
cherrypy.engine.block()
diff --git a/cherrypy/test/checkerdemo.py b/cherrypy/test/checkerdemo.py
index 32a7dee2..68fb222b 100644
--- a/cherrypy/test/checkerdemo.py
+++ b/cherrypy/test/checkerdemo.py
@@ -9,6 +9,7 @@ import os
import cherrypy
thisdir = os.path.dirname(os.path.abspath(__file__))
+
class Root:
pass
@@ -19,13 +20,13 @@ if __name__ == '__main__':
},
# This entry should be OK.
'/base/static': {'tools.staticdir.on': True,
- 'tools.staticdir.dir': 'static'},
+ 'tools.staticdir.dir': 'static'},
# Warn on missing folder.
'/base/js': {'tools.staticdir.on': True,
- 'tools.staticdir.dir': 'js'},
+ 'tools.staticdir.dir': 'js'},
# Warn on dir with an abs path even though we provide root.
'/base/static2': {'tools.staticdir.on': True,
- 'tools.staticdir.dir': '/static'},
+ 'tools.staticdir.dir': '/static'},
# Warn on dir with a relative path with no root.
'/static3': {'tools.staticdir.on': True,
'tools.staticdir.dir': 'static'},
diff --git a/cherrypy/test/helper.py b/cherrypy/test/helper.py
index e3006a52..a76abce3 100644
--- a/cherrypy/test/helper.py
+++ b/cherrypy/test/helper.py
@@ -19,11 +19,18 @@ from cherrypy.lib import gctools
from cherrypy.lib.reprconf import unrepr
from cherrypy.test import webtest
+# Use subprocess module from Python 2.7 on Python 2.3-2.6
+if sys.version_info < (2, 7):
+ import cherrypy._cpcompat_subprocess as subprocess
+else:
+ import subprocess
+
import nose
_testconfig = None
-def get_tst_config(overconf = {}):
+
+def get_tst_config(overconf={}):
global _testconfig
if _testconfig is None:
conf = {
@@ -51,7 +58,9 @@ def get_tst_config(overconf = {}):
return conf
+
class Supervisor(object):
+
"""Base class for modeling and controlling servers during testing."""
def __init__(self, **kwargs):
@@ -63,13 +72,16 @@ class Supervisor(object):
log_to_stderr = lambda msg, level: sys.stderr.write(msg + os.linesep)
+
class LocalSupervisor(Supervisor):
- """Base class for modeling/controlling servers which run in the same process.
+
+ """Base class for modeling/controlling servers which run in the same
+ process.
When the server side runs in a different process, start/stop can dump all
state between each test module easily. When the server side runs in the
- same process as the client, however, we have to do a bit more work to ensure
- config and mounted apps are reset between tests.
+ same process as the client, however, we have to do a bit more work to
+ ensure config and mounted apps are reset between tests.
"""
using_apache = False
@@ -110,15 +122,16 @@ class LocalSupervisor(Supervisor):
td = getattr(self, 'teardown', None)
if td:
td()
-
+
cherrypy.engine.exit()
-
+
for name, server in copyitems(getattr(cherrypy, 'servers', {})):
server.unsubscribe()
del cherrypy.servers[name]
class NativeServerSupervisor(LocalSupervisor):
+
"""Server supervisor for the builtin HTTP server."""
httpserver_class = "cherrypy._cpnative_server.CPHTTPServer"
@@ -130,6 +143,7 @@ class NativeServerSupervisor(LocalSupervisor):
class LocalWSGISupervisor(LocalSupervisor):
+
"""Server supervisor for the builtin WSGI server."""
httpserver_class = "cherrypy._cpwsgi_server.CPWSGIServer"
@@ -147,24 +161,26 @@ class LocalWSGISupervisor(LocalSupervisor):
"""Obtain a new (decorated) WSGI app to hook into the origin server."""
if app is None:
app = cherrypy.tree
-
+
if self.conquer:
try:
import wsgiconq
except ImportError:
- warnings.warn("Error importing wsgiconq. pyconquer will not run.")
+ warnings.warn(
+ "Error importing wsgiconq. pyconquer will not run.")
else:
app = wsgiconq.WSGILogger(app, c_calls=True)
-
+
if self.validate:
try:
from wsgiref import validate
except ImportError:
- warnings.warn("Error importing wsgiref. The validator will not run.")
+ warnings.warn(
+ "Error importing wsgiref. The validator will not run.")
else:
- #wraps the app in the validator
+ # wraps the app in the validator
app = validate.validator(app)
-
+
return app
@@ -174,6 +190,7 @@ def get_cpmodpy_supervisor(**options):
sup.template = modpy.conf_cpmodpy
return sup
+
def get_modpygw_supervisor(**options):
from cherrypy.test import modpy
sup = modpy.ModPythonSupervisor(**options)
@@ -181,25 +198,29 @@ def get_modpygw_supervisor(**options):
sup.using_wsgi = True
return sup
+
def get_modwsgi_supervisor(**options):
from cherrypy.test import modwsgi
return modwsgi.ModWSGISupervisor(**options)
+
def get_modfcgid_supervisor(**options):
from cherrypy.test import modfcgid
return modfcgid.ModFCGISupervisor(**options)
+
def get_modfastcgi_supervisor(**options):
from cherrypy.test import modfastcgi
return modfastcgi.ModFCGISupervisor(**options)
+
def get_wsgi_u_supervisor(**options):
cherrypy.server.wsgi_version = ('u', 0)
return LocalWSGISupervisor(**options)
class CPWebCase(webtest.WebCase):
-
+
script_name = ""
scheme = "http"
@@ -213,7 +234,7 @@ class CPWebCase(webtest.WebCase):
'modfastcgi': get_modfastcgi_supervisor,
}
default_server = "wsgi"
-
+
def _setup_server(cls, supervisor, conf):
v = sys.version.split()[0]
log.info("Python version used to run this test script: %s" % v)
@@ -257,17 +278,18 @@ class CPWebCase(webtest.WebCase):
webtest.WebCase.HTTP_CONN = HTTPSConnection
return baseconf
_setup_server = classmethod(_setup_server)
-
+
def setup_class(cls):
''
- #Creates a server
+ # Creates a server
conf = get_tst_config()
- supervisor_factory = cls.available_servers.get(conf.get('server', 'wsgi'))
+ supervisor_factory = cls.available_servers.get(
+ conf.get('server', 'wsgi'))
if supervisor_factory is None:
raise RuntimeError('Unknown server in config: %s' % conf['server'])
supervisor = supervisor_factory(**conf)
- #Copied from "run_test_suite"
+ # Copied from "run_test_suite"
cherrypy.config.reset()
baseconf = cls._setup_server(supervisor, conf)
cherrypy.config.update(baseconf)
@@ -293,62 +315,67 @@ class CPWebCase(webtest.WebCase):
if hasattr(cls, 'setup_server'):
cls.supervisor.stop()
teardown_class = classmethod(teardown_class)
-
+
do_gc_test = False
-
+
def test_gc(self):
if self.do_gc_test:
self.getPage("/gc/stats")
self.assertBody("Statistics:")
# Tell nose to run this last in each class.
# Prefer sys.maxint for Python 2.3, which didn't have float('inf')
- test_gc.compat_co_firstlineno = getattr(sys, 'maxint', None) or float('inf')
-
+ test_gc.compat_co_firstlineno = getattr(
+ sys, 'maxint', None) or float('inf')
+
def prefix(self):
return self.script_name.rstrip("/")
-
+
def base(self):
if ((self.scheme == "http" and self.PORT == 80) or
- (self.scheme == "https" and self.PORT == 443)):
+ (self.scheme == "https" and self.PORT == 443)):
port = ""
else:
port = ":%s" % self.PORT
-
+
return "%s://%s%s%s" % (self.scheme, self.HOST, port,
self.script_name.rstrip("/"))
-
+
def exit(self):
sys.exit()
-
- def getPage(self, url, headers=None, method="GET", body=None, protocol=None):
+
+ def getPage(self, url, headers=None, method="GET", body=None,
+ protocol=None):
"""Open the url. Return status, headers, body."""
if self.script_name:
url = httputil.urljoin(self.script_name, url)
- return webtest.WebCase.getPage(self, url, headers, method, body, protocol)
-
+ return webtest.WebCase.getPage(self, url, headers, method, body,
+ protocol)
+
def skip(self, msg='skipped '):
raise nose.SkipTest(msg)
-
+
def assertErrorPage(self, status, message=None, pattern=''):
"""Compare the response body with a built in error page.
-
+
The function will optionally look for the regexp pattern,
within the exception embedded in the error page."""
-
+
# This will never contain a traceback
page = cherrypy._cperror.get_error_page(status, message=message)
-
+
# First, test the response body without checking the traceback.
# Stick a match-all group (.*) in to grab the traceback.
esc = re.escape
epage = esc(page)
- epage = epage.replace(esc('<pre id="traceback"></pre>'),
- esc('<pre id="traceback">') + '(.*)' + esc('</pre>'))
+ epage = epage.replace(
+ esc('<pre id="traceback"></pre>'),
+ esc('<pre id="traceback">') + '(.*)' + esc('</pre>'))
m = re.match(ntob(epage, self.encoding), self.body, re.DOTALL)
if not m:
- self._handlewebError('Error page does not match; expected:\n' + page)
+ self._handlewebError(
+ 'Error page does not match; expected:\n' + page)
return
-
+
# Now test the pattern against the traceback
if pattern is None:
# Special-case None to mean that there should be *no* traceback.
@@ -360,14 +387,14 @@ class CPWebCase(webtest.WebCase):
m.group(1))):
msg = 'Error page does not contain %s in traceback'
self._handlewebError(msg % repr(pattern))
-
+
date_tolerance = 2
-
+
def assertEqualDates(self, dt1, dt2, seconds=None):
"""Assert abs(dt1 - dt2) is within Y seconds."""
if seconds is None:
seconds = self.date_tolerance
-
+
if dt1 > dt2:
diff = dt1 - dt2
else:
@@ -388,7 +415,7 @@ def setup_client():
class CPProcess(object):
-
+
pid_file = os.path.join(thisdir, 'test.pid')
config_file = os.path.join(thisdir, 'test.conf')
config_template = """[global]
@@ -403,14 +430,15 @@ log.access_file: r'%(access_log)s'
"""
error_log = os.path.join(thisdir, 'test.error.log')
access_log = os.path.join(thisdir, 'test.access.log')
-
- def __init__(self, wait=False, daemonize=False, ssl=False, socket_host=None, socket_port=None):
+
+ def __init__(self, wait=False, daemonize=False, ssl=False,
+ socket_host=None, socket_port=None):
self.wait = wait
self.daemonize = daemonize
self.ssl = ssl
self.host = socket_host or cherrypy.server.socket_host
self.port = socket_port or cherrypy.server.socket_port
-
+
def write_conf(self, extra=""):
if self.ssl:
serverpem = os.path.join(thisdir, 'test.pem')
@@ -420,7 +448,7 @@ server.ssl_private_key: r'%s'
""" % (serverpem, serverpem)
else:
ssl = ""
-
+
conf = self.config_template % {
'host': self.host,
'port': self.port,
@@ -428,52 +456,64 @@ server.ssl_private_key: r'%s'
'access_log': self.access_log,
'ssl': ssl,
'extra': extra,
- }
+ }
f = open(self.config_file, 'wb')
f.write(ntob(conf, 'utf-8'))
f.close()
-
+
def start(self, imports=None):
"""Start cherryd in a subprocess."""
cherrypy._cpserver.wait_for_free_port(self.host, self.port)
-
- args = [sys.executable, os.path.join(thisdir, '..', 'cherryd'),
- '-c', self.config_file, '-p', self.pid_file]
-
+
+ args = [
+ os.path.join(thisdir, '..', 'cherryd'),
+ '-c', self.config_file,
+ '-p', self.pid_file,
+ ]
+
if not isinstance(imports, (list, tuple)):
imports = [imports]
for i in imports:
if i:
args.append('-i')
args.append(i)
-
+
if self.daemonize:
args.append('-d')
env = os.environ.copy()
- # Make sure we import the cherrypy package in which this module is defined.
+ # Make sure we import the cherrypy package in which this module is
+ # defined.
grandparentdir = os.path.abspath(os.path.join(thisdir, '..', '..'))
if env.get('PYTHONPATH', ''):
- env['PYTHONPATH'] = os.pathsep.join((grandparentdir, env['PYTHONPATH']))
+ env['PYTHONPATH'] = os.pathsep.join(
+ (grandparentdir, env['PYTHONPATH']))
else:
env['PYTHONPATH'] = grandparentdir
+ self._proc = subprocess.Popen([sys.executable] + args, env=env)
if self.wait:
- self.exit_code = os.spawnve(os.P_WAIT, sys.executable, args, env)
+ self.exit_code = self._proc.wait()
else:
- os.spawnve(os.P_NOWAIT, sys.executable, args, env)
cherrypy._cpserver.wait_for_occupied_port(self.host, self.port)
-
+
# Give the engine a wee bit more time to finish STARTING
if self.daemonize:
time.sleep(2)
else:
time.sleep(1)
-
+
def get_pid(self):
- return int(open(self.pid_file, 'rb').read())
-
+ if self.daemonize:
+ return int(open(self.pid_file, 'rb').read())
+ return self._proc.pid
+
def join(self):
"""Wait for the process to exit."""
+ if self.daemonize:
+ return self._join_daemon()
+ self._proc.wait()
+
+ def _join_daemon(self):
try:
try:
# Mac, UNIX
@@ -491,4 +531,3 @@ server.ssl_private_key: r'%s'
x = sys.exc_info()[1]
if x.args != (10, 'No child processes'):
raise
-
diff --git a/cherrypy/test/logtest.py b/cherrypy/test/logtest.py
index 3c6f114c..cc59499a 100644
--- a/cherrypy/test/logtest.py
+++ b/cherrypy/test/logtest.py
@@ -10,11 +10,14 @@ from cherrypy._cpcompat import basestring, ntob, unicodestr
try:
# On Windows, msvcrt.getch reads a single char without output.
import msvcrt
+
def getchar():
return msvcrt.getch()
except ImportError:
# Unix getchr
- import tty, termios
+ import tty
+ import termios
+
def getchar():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
@@ -27,30 +30,33 @@ except ImportError:
class LogCase(object):
+
"""unittest.TestCase mixin for testing log messages.
-
+
logfile: a filename for the desired log. Yes, I know modes are evil,
but it makes the test functions so much cleaner to set this once.
-
+
lastmarker: the last marker in the log. This can be used to search for
messages since the last marker.
-
+
markerPrefix: a string with which to prefix log markers. This should be
unique enough from normal log output to use for marker identification.
"""
-
+
logfile = None
lastmarker = None
markerPrefix = ntob("test suite marker: ")
-
+
def _handleLogError(self, msg, data, marker, pattern):
print("")
print(" ERROR: %s" % msg)
-
+
if not self.interactive:
raise self.failureException(msg)
-
- p = " Show: [L]og [M]arker [P]attern; [I]gnore, [R]aise, or sys.e[X]it >> "
+
+ p = (" Show: "
+ "[L]og [M]arker [P]attern; "
+ "[I]gnore, [R]aise, or sys.e[X]it >> ")
sys.stdout.write(p + ' ')
# ARGH
sys.stdout.flush()
@@ -82,36 +88,37 @@ class LogCase(object):
elif i == "X":
self.exit()
sys.stdout.write(p + ' ')
-
+
def exit(self):
sys.exit()
-
+
def emptyLog(self):
"""Overwrite self.logfile with 0 bytes."""
open(self.logfile, 'wb').write("")
-
+
def markLog(self, key=None):
"""Insert a marker line into the log and set self.lastmarker."""
if key is None:
key = str(time.time())
self.lastmarker = key
-
- open(self.logfile, 'ab+').write(ntob("%s%s\n" % (self.markerPrefix, key),"utf-8"))
-
+
+ open(self.logfile, 'ab+').write(
+ ntob("%s%s\n" % (self.markerPrefix, key), "utf-8"))
+
def _read_marked_region(self, marker=None):
"""Return lines from self.logfile in the marked region.
-
+
If marker is None, self.lastmarker is used. If the log hasn't
been marked (using self.markLog), the entire log will be returned.
"""
-## # Give the logger time to finish writing?
-## time.sleep(0.5)
-
+# Give the logger time to finish writing?
+# time.sleep(0.5)
+
logfile = self.logfile
marker = marker or self.lastmarker
if marker is None:
return open(logfile, 'rb').readlines()
-
+
if isinstance(marker, unicodestr):
marker = marker.encode('utf-8')
data = []
@@ -125,10 +132,10 @@ class LogCase(object):
elif marker in line:
in_region = True
return data
-
+
def assertInLog(self, line, marker=None):
"""Fail if the given (partial) line is not in the log.
-
+
The log will be searched from the given marker to the next marker.
If marker is None, self.lastmarker is used. If the log hasn't
been marked (using self.markLog), the entire log will be searched.
@@ -139,10 +146,10 @@ class LogCase(object):
return
msg = "%r not found in log" % line
self._handleLogError(msg, data, marker, line)
-
+
def assertNotInLog(self, line, marker=None):
"""Fail if the given (partial) line is in the log.
-
+
The log will be searched from the given marker to the next marker.
If marker is None, self.lastmarker is used. If the log hasn't
been marked (using self.markLog), the entire log will be searched.
@@ -152,10 +159,10 @@ class LogCase(object):
if line in logline:
msg = "%r found in log" % line
self._handleLogError(msg, data, marker, line)
-
+
def assertLog(self, sliceargs, lines, marker=None):
"""Fail if log.readlines()[sliceargs] is not contained in 'lines'.
-
+
The log will be searched from the given marker to the next marker.
If marker is None, self.lastmarker is used. If the log hasn't
been marked (using self.markLog), the entire log will be searched.
@@ -169,7 +176,12 @@ class LogCase(object):
lines = lines.encode('utf-8')
if lines not in data[sliceargs]:
msg = "%r not found on log line %r" % (lines, sliceargs)
- self._handleLogError(msg, [data[sliceargs],"--EXTRA CONTEXT--"] + data[sliceargs+1:sliceargs+6], marker, lines)
+ self._handleLogError(
+ msg,
+ [data[sliceargs], "--EXTRA CONTEXT--"] + data[
+ sliceargs + 1:sliceargs + 6],
+ marker,
+ lines)
else:
# Multiple args. Use __getslice__ and require lines to be list.
if isinstance(lines, tuple):
@@ -177,7 +189,7 @@ class LogCase(object):
elif isinstance(lines, basestring):
raise TypeError("The 'lines' arg must be a list when "
"'sliceargs' is a tuple.")
-
+
start, stop = sliceargs
for line, logline in zip(lines, data[start:stop]):
if isinstance(line, unicodestr):
@@ -185,4 +197,3 @@ class LogCase(object):
if line not in logline:
msg = "%r not found in log" % line
self._handleLogError(msg, data[start:stop], marker, line)
-
diff --git a/cherrypy/test/modfastcgi.py b/cherrypy/test/modfastcgi.py
index 95acf141..53645474 100644
--- a/cherrypy/test/modfastcgi.py
+++ b/cherrypy/test/modfastcgi.py
@@ -80,20 +80,22 @@ RewriteRule ^(.*)$ /fastcgi.pyc [L]
FastCgiExternalServer "%(server)s" -host 127.0.0.1:4000
"""
+
def erase_script_name(environ, start_response):
environ['SCRIPT_NAME'] = ''
return cherrypy.tree(environ, start_response)
+
class ModFCGISupervisor(helper.LocalWSGISupervisor):
-
+
httpserver_class = "cherrypy.process.servers.FlupFCGIServer"
using_apache = True
using_wsgi = True
template = conf_fastcgi
-
+
def __str__(self):
return "FCGI Server on %s:%s" % (self.host, self.port)
-
+
def start(self, modulename):
cherrypy.server.httpserver = servers.FlupFCGIServer(
application=erase_script_name, bindAddress=('127.0.0.1', 4000))
@@ -104,12 +106,12 @@ class ModFCGISupervisor(helper.LocalWSGISupervisor):
# ...and our local server
cherrypy.engine.start()
self.sync_apps()
-
+
def start_apache(self):
fcgiconf = CONF_PATH
if not os.path.isabs(fcgiconf):
fcgiconf = os.path.join(curdir, fcgiconf)
-
+
# Write the Apache conf file.
f = open(fcgiconf, 'wb')
try:
@@ -120,16 +122,16 @@ class ModFCGISupervisor(helper.LocalWSGISupervisor):
f.write(output)
finally:
f.close()
-
+
result = read_process(APACHE_PATH, "-k start -f %s" % fcgiconf)
if result:
print(result)
-
+
def stop(self):
"""Gracefully shutdown a server that is serving forever."""
read_process(APACHE_PATH, "-k stop")
helper.LocalWSGISupervisor.stop(self)
-
- def sync_apps(self):
- cherrypy.server.httpserver.fcgiserver.application = self.get_app(erase_script_name)
+ def sync_apps(self):
+ cherrypy.server.httpserver.fcgiserver.application = self.get_app(
+ erase_script_name)
diff --git a/cherrypy/test/modfcgid.py b/cherrypy/test/modfcgid.py
index 736aa4c8..6f8a3584 100644
--- a/cherrypy/test/modfcgid.py
+++ b/cherrypy/test/modfcgid.py
@@ -77,15 +77,16 @@ RewriteRule ^(.*)$ /fastcgi.pyc [L]
FastCgiExternalServer "%(server)s" -host 127.0.0.1:4000
"""
+
class ModFCGISupervisor(helper.LocalSupervisor):
-
+
using_apache = True
using_wsgi = True
template = conf_fcgid
-
+
def __str__(self):
return "FCGI Server on %s:%s" % (self.host, self.port)
-
+
def start(self, modulename):
cherrypy.server.httpserver = servers.FlupFCGIServer(
application=cherrypy.tree, bindAddress=('127.0.0.1', 4000))
@@ -94,12 +95,12 @@ class ModFCGISupervisor(helper.LocalSupervisor):
self.start_apache()
# ...and our local server
helper.LocalServer.start(self, modulename)
-
+
def start_apache(self):
fcgiconf = CONF_PATH
if not os.path.isabs(fcgiconf):
fcgiconf = os.path.join(curdir, fcgiconf)
-
+
# Write the Apache conf file.
f = open(fcgiconf, 'wb')
try:
@@ -110,16 +111,15 @@ class ModFCGISupervisor(helper.LocalSupervisor):
f.write(output)
finally:
f.close()
-
+
result = read_process(APACHE_PATH, "-k start -f %s" % fcgiconf)
if result:
print(result)
-
+
def stop(self):
"""Gracefully shutdown a server that is serving forever."""
read_process(APACHE_PATH, "-k stop")
helper.LocalServer.stop(self)
-
+
def sync_apps(self):
cherrypy.server.httpserver.fcgiserver.application = self.get_app()
-
diff --git a/cherrypy/test/modpy.py b/cherrypy/test/modpy.py
index 519571fc..829628b7 100644
--- a/cherrypy/test/modpy.py
+++ b/cherrypy/test/modpy.py
@@ -91,20 +91,21 @@ PythonOption socket_host %(host)s
PythonDebug On
"""
+
class ModPythonSupervisor(helper.Supervisor):
-
+
using_apache = True
using_wsgi = False
template = None
-
+
def __str__(self):
return "ModPython Server on %s:%s" % (self.host, self.port)
-
+
def start(self, modulename):
mpconf = CONF_PATH
if not os.path.isabs(mpconf):
mpconf = os.path.join(curdir, mpconf)
-
+
f = open(mpconf, 'wb')
try:
f.write(self.template %
@@ -112,34 +113,36 @@ class ModPythonSupervisor(helper.Supervisor):
'host': self.host})
finally:
f.close()
-
+
result = read_process(APACHE_PATH, "-k start -f %s" % mpconf)
if result:
print(result)
-
+
def stop(self):
"""Gracefully shutdown a server that is serving forever."""
read_process(APACHE_PATH, "-k stop")
loaded = False
+
+
def wsgisetup(req):
global loaded
if not loaded:
loaded = True
options = req.get_options()
-
+
import cherrypy
cherrypy.config.update({
"log.error_file": os.path.join(curdir, "test.log"),
"environment": "test_suite",
"server.socket_host": options['socket_host'],
- })
-
+ })
+
modname = options['testmod']
mod = __import__(modname, globals(), locals(), [''])
mod.setup_server()
-
+
cherrypy.server.unsubscribe()
cherrypy.engine.start()
from mod_python import apache
@@ -151,13 +154,12 @@ def cpmodpysetup(req):
if not loaded:
loaded = True
options = req.get_options()
-
+
import cherrypy
cherrypy.config.update({
"log.error_file": os.path.join(curdir, "test.log"),
"environment": "test_suite",
"server.socket_host": options['socket_host'],
- })
+ })
from mod_python import apache
return apache.OK
-
diff --git a/cherrypy/test/modwsgi.py b/cherrypy/test/modwsgi.py
index 309a541c..043fb6e8 100644
--- a/cherrypy/test/modwsgi.py
+++ b/cherrypy/test/modwsgi.py
@@ -89,20 +89,21 @@ SetEnv testmod %(testmod)s
class ModWSGISupervisor(helper.Supervisor):
+
"""Server Controller for ModWSGI and CherryPy."""
-
+
using_apache = True
using_wsgi = True
- template=conf_modwsgi
-
+ template = conf_modwsgi
+
def __str__(self):
return "ModWSGI Server on %s:%s" % (self.host, self.port)
-
+
def start(self, modulename):
mpconf = CONF_PATH
if not os.path.isabs(mpconf):
mpconf = os.path.join(curdir, mpconf)
-
+
f = open(mpconf, 'wb')
try:
output = (self.template %
@@ -111,23 +112,25 @@ class ModWSGISupervisor(helper.Supervisor):
f.write(output)
finally:
f.close()
-
+
result = read_process(APACHE_PATH, "-k start -f %s" % mpconf)
if result:
print(result)
-
+
# Make a request so mod_wsgi starts up our app.
# If we don't, concurrent initial requests will 404.
cherrypy._cpserver.wait_for_occupied_port("127.0.0.1", self.port)
webtest.openURL('/ihopetheresnodefault', port=self.port)
time.sleep(1)
-
+
def stop(self):
"""Gracefully shutdown a server that is serving forever."""
read_process(APACHE_PATH, "-k stop")
loaded = False
+
+
def application(environ, start_response):
import cherrypy
global loaded
@@ -136,13 +139,12 @@ def application(environ, start_response):
modname = "cherrypy.test." + environ['testmod']
mod = __import__(modname, globals(), locals(), [''])
mod.setup_server()
-
+
cherrypy.config.update({
"log.error_file": os.path.join(curdir, "test.error.log"),
"log.access_file": os.path.join(curdir, "test.access.log"),
"environment": "test_suite",
"engine.SIGHUP": None,
"engine.SIGTERM": None,
- })
+ })
return cherrypy.tree(environ, start_response)
-
diff --git a/cherrypy/test/sessiondemo.py b/cherrypy/test/sessiondemo.py
index 342e5b59..f76b130a 100755
--- a/cherrypy/test/sessiondemo.py
+++ b/cherrypy/test/sessiondemo.py
@@ -15,7 +15,7 @@ page = """
<style type='text/css'>
table { border-collapse: collapse; border: 1px solid #663333; }
th { text-align: right; background-color: #663333; color: white; padding: 0.5em; }
-td { white-space: pre-wrap; font-family: monospace; padding: 0.5em;
+td { white-space: pre-wrap; font-family: monospace; padding: 0.5em;
border: 1px solid #663333; }
.warn { font-family: serif; color: #990000; }
</style>
@@ -51,15 +51,15 @@ function init() {
// Set the content of the 'btime' cell.
var currentTime = new Date();
var bunixtime = Math.floor(currentTime.getTime() / 1000);
-
+
var v = formattime(currentTime);
v += " (Unix time: " + bunixtime + ")";
-
+
var diff = Math.abs(%(serverunixtime)s - bunixtime);
if (diff > fudge_seconds) v += "<p class='warn'>Browser and Server times disagree.</p>";
-
+
document.getElementById('btime').innerHTML = v;
-
+
// Warn if response cookie expires is not close to one hour in the future.
// Yes, we want this to happen when wit hit the 'Expire' link, too.
var expires = Date.parse("%(expires)s") / 1000;
@@ -95,47 +95,53 @@ function init() {
</body></html>
"""
+
class Root(object):
-
+
def page(self):
changemsg = []
if cherrypy.session.id != cherrypy.session.originalid:
if cherrypy.session.originalid is None:
- changemsg.append('Created new session because no session id was given.')
+ changemsg.append(
+ 'Created new session because no session id was given.')
if cherrypy.session.missing:
- changemsg.append('Created new session due to missing (expired or malicious) session.')
+ changemsg.append(
+ 'Created new session due to missing '
+ '(expired or malicious) session.')
if cherrypy.session.regenerated:
changemsg.append('Application generated a new session.')
-
+
try:
expires = cherrypy.response.cookie['session_id']['expires']
except KeyError:
expires = ''
-
+
return page % {
'sessionid': cherrypy.session.id,
'changemsg': '<br>'.join(changemsg),
'respcookie': cherrypy.response.cookie.output(),
'reqcookie': cherrypy.request.cookie.output(),
'sessiondata': copyitems(cherrypy.session),
- 'servertime': datetime.utcnow().strftime("%Y/%m/%d %H:%M") + " UTC",
+ 'servertime': (
+ datetime.utcnow().strftime("%Y/%m/%d %H:%M") + " UTC"
+ ),
'serverunixtime': calendar.timegm(datetime.utcnow().timetuple()),
'cpversion': cherrypy.__version__,
'pyversion': sys.version,
'expires': expires,
- }
-
+ }
+
def index(self):
# Must modify data or the session will not be saved.
cherrypy.session['color'] = 'green'
return self.page()
index.exposed = True
-
+
def expire(self):
sessions.expire()
return self.page()
expire.exposed = True
-
+
def regen(self):
cherrypy.session.regenerate()
# Must modify data or the session will not be saved.
@@ -148,6 +154,5 @@ if __name__ == '__main__':
#'environment': 'production',
'log.screen': True,
'tools.sessions.on': True,
- })
+ })
cherrypy.quickstart(Root())
-
diff --git a/cherrypy/test/test_auth_basic.py b/cherrypy/test/test_auth_basic.py
index 3a9781d8..7ba11dfc 100644
--- a/cherrypy/test/test_auth_basic.py
+++ b/cherrypy/test/test_auth_basic.py
@@ -12,34 +12,45 @@ class BasicAuthTest(helper.CPWebCase):
def setup_server():
class Root:
+
def index(self):
return "This is public."
index.exposed = True
class BasicProtected:
+
def index(self):
- return "Hello %s, you've been authorized." % cherrypy.request.login
+ return "Hello %s, you've been authorized." % (
+ cherrypy.request.login)
index.exposed = True
class BasicProtected2:
+
def index(self):
- return "Hello %s, you've been authorized." % cherrypy.request.login
+ return "Hello %s, you've been authorized." % (
+ cherrypy.request.login)
index.exposed = True
- userpassdict = {'xuser' : 'xpassword'}
- userhashdict = {'xuser' : md5(ntob('xpassword')).hexdigest()}
+ userpassdict = {'xuser': 'xpassword'}
+ userhashdict = {'xuser': md5(ntob('xpassword')).hexdigest()}
def checkpasshash(realm, user, password):
p = userhashdict.get(user)
return p and p == md5(ntob(password)).hexdigest() or False
- conf = {'/basic': {'tools.auth_basic.on': True,
- 'tools.auth_basic.realm': 'wonderland',
- 'tools.auth_basic.checkpassword': auth_basic.checkpassword_dict(userpassdict)},
- '/basic2': {'tools.auth_basic.on': True,
- 'tools.auth_basic.realm': 'wonderland',
- 'tools.auth_basic.checkpassword': checkpasshash},
- }
+ basic_checkpassword_dict = auth_basic.checkpassword_dict(userpassdict)
+ conf = {
+ '/basic': {
+ 'tools.auth_basic.on': True,
+ 'tools.auth_basic.realm': 'wonderland',
+ 'tools.auth_basic.checkpassword': basic_checkpassword_dict
+ },
+ '/basic2': {
+ 'tools.auth_basic.on': True,
+ 'tools.auth_basic.realm': 'wonderland',
+ 'tools.auth_basic.checkpassword': checkpasshash
+ },
+ }
root = Root()
root.basic = BasicProtected()
@@ -58,10 +69,12 @@ class BasicAuthTest(helper.CPWebCase):
self.assertStatus(401)
self.assertHeader('WWW-Authenticate', 'Basic realm="wonderland"')
- self.getPage('/basic/', [('Authorization', 'Basic eHVzZXI6eHBhc3N3b3JX')])
+ self.getPage('/basic/',
+ [('Authorization', 'Basic eHVzZXI6eHBhc3N3b3JX')])
self.assertStatus(401)
- self.getPage('/basic/', [('Authorization', 'Basic eHVzZXI6eHBhc3N3b3Jk')])
+ self.getPage('/basic/',
+ [('Authorization', 'Basic eHVzZXI6eHBhc3N3b3Jk')])
self.assertStatus('200 OK')
self.assertBody("Hello xuser, you've been authorized.")
@@ -70,10 +83,11 @@ class BasicAuthTest(helper.CPWebCase):
self.assertStatus(401)
self.assertHeader('WWW-Authenticate', 'Basic realm="wonderland"')
- self.getPage('/basic2/', [('Authorization', 'Basic eHVzZXI6eHBhc3N3b3JX')])
+ self.getPage('/basic2/',
+ [('Authorization', 'Basic eHVzZXI6eHBhc3N3b3JX')])
self.assertStatus(401)
- self.getPage('/basic2/', [('Authorization', 'Basic eHVzZXI6eHBhc3N3b3Jk')])
+ self.getPage('/basic2/',
+ [('Authorization', 'Basic eHVzZXI6eHBhc3N3b3Jk')])
self.assertStatus('200 OK')
self.assertBody("Hello xuser, you've been authorized.")
-
diff --git a/cherrypy/test/test_auth_digest.py b/cherrypy/test/test_auth_digest.py
index 1960fa81..b46698d9 100644
--- a/cherrypy/test/test_auth_digest.py
+++ b/cherrypy/test/test_auth_digest.py
@@ -8,23 +8,26 @@ from cherrypy.lib import auth_digest
from cherrypy.test import helper
+
class DigestAuthTest(helper.CPWebCase):
def setup_server():
class Root:
+
def index(self):
return "This is public."
index.exposed = True
class DigestProtected:
+
def index(self):
- return "Hello %s, you've been authorized." % cherrypy.request.login
+ return "Hello %s, you've been authorized." % (
+ cherrypy.request.login)
index.exposed = True
def fetch_users():
return {'test': 'test'}
-
get_ha1 = cherrypy.lib.auth_digest.get_ha1_dict_plain(fetch_users())
conf = {'/digest': {'tools.auth_digest.on': True,
'tools.auth_digest.realm': 'localhost',
@@ -36,7 +39,7 @@ class DigestAuthTest(helper.CPWebCase):
root.digest = DigestProtected()
cherrypy.tree.mount(root, config=conf)
setup_server = staticmethod(setup_server)
-
+
def testPublic(self):
self.getPage("/")
self.assertStatus('200 OK')
@@ -55,7 +58,8 @@ class DigestAuthTest(helper.CPWebCase):
break
if value is None:
- self._handlewebError("Digest authentification scheme was not found")
+ self._handlewebError(
+ "Digest authentification scheme was not found")
value = value[7:]
items = value.split(', ')
@@ -70,7 +74,8 @@ class DigestAuthTest(helper.CPWebCase):
if 'realm' not in tokens:
self._handlewebError(missing_msg % 'realm')
elif tokens['realm'] != '"localhost"':
- self._handlewebError(bad_value_msg % ('realm', '"localhost"', tokens['realm']))
+ self._handlewebError(bad_value_msg %
+ ('realm', '"localhost"', tokens['realm']))
if 'nonce' not in tokens:
self._handlewebError(missing_msg % 'nonce')
else:
@@ -78,18 +83,29 @@ class DigestAuthTest(helper.CPWebCase):
if 'algorithm' not in tokens:
self._handlewebError(missing_msg % 'algorithm')
elif tokens['algorithm'] != '"MD5"':
- self._handlewebError(bad_value_msg % ('algorithm', '"MD5"', tokens['algorithm']))
+ self._handlewebError(bad_value_msg %
+ ('algorithm', '"MD5"', tokens['algorithm']))
if 'qop' not in tokens:
self._handlewebError(missing_msg % 'qop')
elif tokens['qop'] != '"auth"':
- self._handlewebError(bad_value_msg % ('qop', '"auth"', tokens['qop']))
+ self._handlewebError(bad_value_msg %
+ ('qop', '"auth"', tokens['qop']))
- get_ha1 = auth_digest.get_ha1_dict_plain({'test' : 'test'})
+ get_ha1 = auth_digest.get_ha1_dict_plain({'test': 'test'})
# Test user agent response with a wrong value for 'realm'
- base_auth = 'Digest username="test", realm="wrong realm", nonce="%s", uri="/digest/", algorithm=MD5, response="%s", qop=auth, nc=%s, cnonce="1522e61005789929"'
-
- auth_header = base_auth % (nonce, '11111111111111111111111111111111', '00000001')
+ base_auth = ('Digest username="test", '
+ 'realm="wrong realm", '
+ 'nonce="%s", '
+ 'uri="/digest/", '
+ 'algorithm=MD5, '
+ 'response="%s", '
+ 'qop=auth, '
+ 'nc=%s, '
+ 'cnonce="1522e61005789929"')
+
+ auth_header = base_auth % (
+ nonce, '11111111111111111111111111111111', '00000001')
auth = auth_digest.HttpDigestAuthorization(auth_header, 'GET')
# calculate the response digest
ha1 = get_ha1(auth.realm, 'test')
@@ -100,9 +116,18 @@ class DigestAuthTest(helper.CPWebCase):
self.assertStatus(401)
# Test that must pass
- base_auth = 'Digest username="test", realm="localhost", nonce="%s", uri="/digest/", algorithm=MD5, response="%s", qop=auth, nc=%s, cnonce="1522e61005789929"'
-
- auth_header = base_auth % (nonce, '11111111111111111111111111111111', '00000001')
+ base_auth = ('Digest username="test", '
+ 'realm="localhost", '
+ 'nonce="%s", '
+ 'uri="/digest/", '
+ 'algorithm=MD5, '
+ 'response="%s", '
+ 'qop=auth, '
+ 'nc=%s, '
+ 'cnonce="1522e61005789929"')
+
+ auth_header = base_auth % (
+ nonce, '11111111111111111111111111111111', '00000001')
auth = auth_digest.HttpDigestAuthorization(auth_header, 'GET')
# calculate the response digest
ha1 = get_ha1('localhost', 'test')
@@ -112,4 +137,3 @@ class DigestAuthTest(helper.CPWebCase):
self.getPage('/digest/', [('Authorization', auth_header)])
self.assertStatus('200 OK')
self.assertBody("Hello test, you've been authorized.")
-
diff --git a/cherrypy/test/test_bus.py b/cherrypy/test/test_bus.py
index 51c10220..06ad6acb 100644
--- a/cherrypy/test/test_bus.py
+++ b/cherrypy/test/test_bus.py
@@ -24,7 +24,8 @@ class PublishSubscribeTests(unittest.TestCase):
for channel in b.listeners:
for index, priority in enumerate([100, 50, 0, 51]):
- b.subscribe(channel, self.get_listener(channel, index), priority)
+ b.subscribe(channel,
+ self.get_listener(channel, index), priority)
for channel in b.listeners:
b.publish(channel)
@@ -42,11 +43,13 @@ class PublishSubscribeTests(unittest.TestCase):
custom_listeners = ('hugh', 'louis', 'dewey')
for channel in custom_listeners:
for index, priority in enumerate([None, 10, 60, 40]):
- b.subscribe(channel, self.get_listener(channel, index), priority)
+ b.subscribe(channel,
+ self.get_listener(channel, index), priority)
for channel in custom_listeners:
b.publish(channel, 'ah so')
- expected.extend([msg % (i, channel, 'ah so') for i in (1, 3, 0, 2)])
+ expected.extend([msg % (i, channel, 'ah so')
+ for i in (1, 3, 0, 2)])
b.publish(channel)
expected.extend([msg % (i, channel, None) for i in (1, 3, 0, 2)])
@@ -74,6 +77,7 @@ class BusMethodTests(unittest.TestCase):
def log(self, bus):
self._log_entries = []
+
def logit(msg, level):
self._log_entries.append(msg)
bus.subscribe('log', logit)
@@ -98,8 +102,9 @@ class BusMethodTests(unittest.TestCase):
b.start()
try:
# The start method MUST call all 'start' listeners.
- self.assertEqual(set(self.responses),
- set([msg % (i, 'start', None) for i in range(num)]))
+ self.assertEqual(
+ set(self.responses),
+ set([msg % (i, 'start', None) for i in range(num)]))
# The start method MUST move the state to STARTED
# (or EXITING, if errors occur)
self.assertEqual(b.state, b.states.STARTED)
@@ -140,8 +145,9 @@ class BusMethodTests(unittest.TestCase):
b.graceful()
# The graceful method MUST call all 'graceful' listeners.
- self.assertEqual(set(self.responses),
- set([msg % (i, 'graceful', None) for i in range(num)]))
+ self.assertEqual(
+ set(self.responses),
+ set([msg % (i, 'graceful', None) for i in range(num)]))
# The graceful method MUST log its states.
self.assertLog(['Bus graceful'])
@@ -165,7 +171,8 @@ class BusMethodTests(unittest.TestCase):
# The exit method MUST move the state to EXITING
self.assertEqual(b.state, b.states.EXITING)
# The exit method MUST log its states.
- self.assertLog(['Bus STOPPING', 'Bus STOPPED', 'Bus EXITING', 'Bus EXITED'])
+ self.assertLog(
+ ['Bus STOPPING', 'Bus STOPPED', 'Bus EXITING', 'Bus EXITED'])
def test_wait(self):
b = wspbus.Bus()
@@ -176,7 +183,8 @@ class BusMethodTests(unittest.TestCase):
for method, states in [('start', [b.states.STARTED]),
('stop', [b.states.STOPPED]),
- ('start', [b.states.STARTING, b.states.STARTED]),
+ ('start',
+ [b.states.STARTING, b.states.STARTED]),
('exit', [b.states.EXITING]),
]:
threading.Thread(target=f, args=(method,)).start()
@@ -193,6 +201,7 @@ class BusMethodTests(unittest.TestCase):
def f():
time.sleep(0.2)
b.exit()
+
def g():
time.sleep(0.4)
threading.Thread(target=f).start()
@@ -204,10 +213,12 @@ class BusMethodTests(unittest.TestCase):
# The block method MUST wait for the EXITING state.
self.assertEqual(b.state, b.states.EXITING)
- # The block method MUST wait for ALL non-main, non-daemon threads to finish.
+ # The block method MUST wait for ALL non-main, non-daemon threads to
+ # finish.
threads = [t for t in threading.enumerate() if not get_daemon(t)]
self.assertEqual(len(threads), 1)
- # The last message will mention an indeterminable thread name; ignore it
+ # The last message will mention an indeterminable thread name; ignore
+ # it
self.assertEqual(self._log_entries[:-1],
['Bus STOPPING', 'Bus STOPPED',
'Bus EXITING', 'Bus EXITED',
@@ -218,8 +229,10 @@ class BusMethodTests(unittest.TestCase):
self.log(b)
try:
events = []
+
def f(*args, **kwargs):
events.append(("f", args, kwargs))
+
def g():
events.append("g")
b.subscribe("start", g)
diff --git a/cherrypy/test/test_caching.py b/cherrypy/test/test_caching.py
index c210e6e0..be1950d6 100644
--- a/cherrypy/test/test_caching.py
+++ b/cherrypy/test/test_caching.py
@@ -12,43 +12,46 @@ import cherrypy
from cherrypy._cpcompat import next, ntob, quote, xrange
from cherrypy.lib import httputil
-gif_bytes = ntob('GIF89a\x01\x00\x01\x00\x82\x00\x01\x99"\x1e\x00\x00\x00\x00\x00'
- '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
- '\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x02\x03\x02\x08\t\x00;')
-
+gif_bytes = ntob(
+ 'GIF89a\x01\x00\x01\x00\x82\x00\x01\x99"\x1e\x00\x00\x00\x00\x00'
+ '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
+ '\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x02\x03\x02\x08\t\x00;'
+)
from cherrypy.test import helper
+
class CacheTest(helper.CPWebCase):
def setup_server():
-
+
class Root:
-
+
_cp_config = {'tools.caching.on': True}
-
+
def __init__(self):
self.counter = 0
self.control_counter = 0
self.longlock = threading.Lock()
-
+
def index(self):
self.counter += 1
msg = "visit #%s" % self.counter
return msg
index.exposed = True
-
+
def control(self):
self.control_counter += 1
return "visit #%s" % self.control_counter
control.exposed = True
-
+
def a_gif(self):
- cherrypy.response.headers['Last-Modified'] = httputil.HTTPDate()
+ cherrypy.response.headers[
+ 'Last-Modified'] = httputil.HTTPDate()
return gif_bytes
a_gif.exposed = True
-
+
def long_process(self, seconds='1'):
try:
self.longlock.acquire()
@@ -57,25 +60,28 @@ class CacheTest(helper.CPWebCase):
self.longlock.release()
return 'success!'
long_process.exposed = True
-
+
def clear_cache(self, path):
cherrypy._cache.store[cherrypy.request.base + path].clear()
clear_cache.exposed = True
-
+
class VaryHeaderCachingServer(object):
-
- _cp_config = {'tools.caching.on': True,
+
+ _cp_config = {
+ 'tools.caching.on': True,
'tools.response_headers.on': True,
- 'tools.response_headers.headers': [('Vary', 'Our-Varying-Header')],
- }
-
+ 'tools.response_headers.headers': [
+ ('Vary', 'Our-Varying-Header')
+ ],
+ }
+
def __init__(self):
self.counter = count(1)
-
+
def index(self):
return "visit #%s" % next(self.counter)
index.exposed = True
-
+
class UnCached(object):
_cp_config = {'tools.expires.on': True,
'tools.expires.secs': 60,
@@ -104,19 +110,22 @@ class CacheTest(helper.CPWebCase):
cacheable.exposed = True
def specific(self):
- cherrypy.response.headers['Etag'] = 'need_this_to_make_me_cacheable'
+ cherrypy.response.headers[
+ 'Etag'] = 'need_this_to_make_me_cacheable'
return "I am being specific"
specific.exposed = True
specific._cp_config = {'tools.expires.secs': 86400}
- class Foo(object):pass
-
+ class Foo(object):
+ pass
+
def wrongtype(self):
- cherrypy.response.headers['Etag'] = 'need_this_to_make_me_cacheable'
+ cherrypy.response.headers[
+ 'Etag'] = 'need_this_to_make_me_cacheable'
return "Woops"
wrongtype.exposed = True
wrongtype._cp_config = {'tools.expires.secs': Foo()}
-
+
cherrypy.tree.mount(Root())
cherrypy.tree.mount(UnCached(), "/expires")
cherrypy.tree.mount(VaryHeaderCachingServer(), "/varying_headers")
@@ -134,11 +143,12 @@ class CacheTest(helper.CPWebCase):
age = int(self.assertHeader("Age"))
self.assert_(age >= elapsed)
elapsed = age
-
+
# POST, PUT, DELETE should not be cached.
self.getPage("/", method="POST")
self.assertBody('visit #2')
- # Because gzip is turned on, the Vary header should always Vary for content-encoding
+ # Because gzip is turned on, the Vary header should always Vary for
+ # content-encoding
self.assertHeader('Vary', 'Accept-Encoding')
# The previous request should have invalidated the cache,
# so this request will recalc the response.
@@ -149,66 +159,70 @@ class CacheTest(helper.CPWebCase):
self.assertBody('visit #3')
self.getPage("/", method="DELETE")
self.assertBody('visit #4')
-
+
# The previous request should have invalidated the cache,
# so this request will recalc the response.
self.getPage("/", method="GET", headers=[('Accept-Encoding', 'gzip')])
self.assertHeader('Content-Encoding', 'gzip')
self.assertHeader('Vary')
- self.assertEqual(cherrypy.lib.encoding.decompress(self.body), ntob("visit #5"))
-
+ self.assertEqual(
+ cherrypy.lib.encoding.decompress(self.body), ntob("visit #5"))
+
# Now check that a second request gets the gzip header and gzipped body
# This also tests a bug in 3.0 to 3.0.2 whereby the cached, gzipped
# response body was being gzipped a second time.
self.getPage("/", method="GET", headers=[('Accept-Encoding', 'gzip')])
self.assertHeader('Content-Encoding', 'gzip')
- self.assertEqual(cherrypy.lib.encoding.decompress(self.body), ntob("visit #5"))
-
+ self.assertEqual(
+ cherrypy.lib.encoding.decompress(self.body), ntob("visit #5"))
+
# Now check that a third request that doesn't accept gzip
# skips the cache (because the 'Vary' header denies it).
self.getPage("/", method="GET")
self.assertNoHeader('Content-Encoding')
self.assertBody('visit #6')
-
+
def testVaryHeader(self):
self.getPage("/varying_headers/")
self.assertStatus("200 OK")
self.assertHeaderItemValue('Vary', 'Our-Varying-Header')
self.assertBody('visit #1')
-
+
# Now check that different 'Vary'-fields don't evict each other.
# This test creates 2 requests with different 'Our-Varying-Header'
# and then tests if the first one still exists.
- self.getPage("/varying_headers/", headers=[('Our-Varying-Header', 'request 2')])
+ self.getPage("/varying_headers/",
+ headers=[('Our-Varying-Header', 'request 2')])
self.assertStatus("200 OK")
self.assertBody('visit #2')
-
- self.getPage("/varying_headers/", headers=[('Our-Varying-Header', 'request 2')])
+
+ self.getPage("/varying_headers/",
+ headers=[('Our-Varying-Header', 'request 2')])
self.assertStatus("200 OK")
self.assertBody('visit #2')
-
+
self.getPage("/varying_headers/")
self.assertStatus("200 OK")
self.assertBody('visit #1')
-
+
def testExpiresTool(self):
# test setting an expires header
self.getPage("/expires/specific")
self.assertStatus("200 OK")
self.assertHeader("Expires")
-
+
# test exceptions for bad time values
self.getPage("/expires/wrongtype")
self.assertStatus(500)
self.assertInBody("TypeError")
-
+
# static content should not have "cache prevention" headers
self.getPage("/expires/index.html")
self.assertStatus("200 OK")
self.assertNoHeader("Pragma")
self.assertNoHeader("Cache-Control")
self.assertHeader("Expires")
-
+
# dynamic content that sets indicators should not have
# "cache prevention" headers
self.getPage("/expires/cacheable")
@@ -216,13 +230,13 @@ class CacheTest(helper.CPWebCase):
self.assertNoHeader("Pragma")
self.assertNoHeader("Cache-Control")
self.assertHeader("Expires")
-
+
self.getPage('/expires/dynamic')
self.assertBody("D-d-d-dynamic!")
# the Cache-Control header should be untouched
self.assertHeader("Cache-Control", "private")
self.assertHeader("Expires")
-
+
# configure the tool to ignore indicators and replace existing headers
self.getPage("/expires/force")
self.assertStatus("200 OK")
@@ -231,7 +245,7 @@ class CacheTest(helper.CPWebCase):
if cherrypy.server.protocol_version == "HTTP/1.1":
self.assertHeader("Cache-Control", "no-cache, must-revalidate")
self.assertHeader("Expires", "Sun, 28 Jan 2007 00:00:00 GMT")
-
+
# static content should now have "cache prevention" headers
self.getPage("/expires/index.html")
self.assertStatus("200 OK")
@@ -239,7 +253,7 @@ class CacheTest(helper.CPWebCase):
if cherrypy.server.protocol_version == "HTTP/1.1":
self.assertHeader("Cache-Control", "no-cache, must-revalidate")
self.assertHeader("Expires", "Sun, 28 Jan 2007 00:00:00 GMT")
-
+
# the cacheable handler should now have "cache prevention" headers
self.getPage("/expires/cacheable")
self.assertStatus("200 OK")
@@ -247,7 +261,7 @@ class CacheTest(helper.CPWebCase):
if cherrypy.server.protocol_version == "HTTP/1.1":
self.assertHeader("Cache-Control", "no-cache, must-revalidate")
self.assertHeader("Expires", "Sun, 28 Jan 2007 00:00:00 GMT")
-
+
self.getPage('/expires/dynamic')
self.assertBody("D-d-d-dynamic!")
# dynamic sets Cache-Control to private but it should be
@@ -256,13 +270,13 @@ class CacheTest(helper.CPWebCase):
if cherrypy.server.protocol_version == "HTTP/1.1":
self.assertHeader("Cache-Control", "no-cache, must-revalidate")
self.assertHeader("Expires", "Sun, 28 Jan 2007 00:00:00 GMT")
-
+
def testLastModified(self):
self.getPage("/a.gif")
self.assertStatus(200)
self.assertBody(gif_bytes)
lm1 = self.assertHeader("Last-Modified")
-
+
# this request should get the cached copy.
self.getPage("/a.gif")
self.assertStatus(200)
@@ -270,14 +284,14 @@ class CacheTest(helper.CPWebCase):
self.assertHeader("Age")
lm2 = self.assertHeader("Last-Modified")
self.assertEqual(lm1, lm2)
-
+
# this request should match the cached copy, but raise 304.
self.getPage("/a.gif", [('If-Modified-Since', lm1)])
self.assertStatus(304)
self.assertNoHeader("Last-Modified")
if not getattr(cherrypy.server, "using_apache", False):
self.assertHeader("Age")
-
+
def test_antistampede(self):
SECONDS = 4
# We MUST make an initial synchronous request in order to create the
@@ -286,10 +300,11 @@ class CacheTest(helper.CPWebCase):
self.getPage("/long_process?seconds=%d" % SECONDS)
self.assertBody('success!')
self.getPage("/clear_cache?path=" +
- quote('/long_process?seconds=%d' % SECONDS, safe=''))
+ quote('/long_process?seconds=%d' % SECONDS, safe=''))
self.assertStatus(200)
-
+
start = datetime.datetime.now()
+
def run():
self.getPage("/long_process?seconds=%d" % SECONDS)
# The response should be the same every time
@@ -303,26 +318,25 @@ class CacheTest(helper.CPWebCase):
# Allow a second (two, for slow hosts)
# for our thread/TCP overhead etc.
seconds=SECONDS + 2)
-
+
def test_cache_control(self):
self.getPage("/control")
self.assertBody('visit #1')
self.getPage("/control")
self.assertBody('visit #1')
-
+
self.getPage("/control", headers=[('Cache-Control', 'no-cache')])
self.assertBody('visit #2')
self.getPage("/control")
self.assertBody('visit #2')
-
+
self.getPage("/control", headers=[('Pragma', 'no-cache')])
self.assertBody('visit #3')
self.getPage("/control")
self.assertBody('visit #3')
-
+
time.sleep(1)
self.getPage("/control", headers=[('Cache-Control', 'max-age=0')])
self.assertBody('visit #4')
self.getPage("/control")
self.assertBody('visit #4')
-
diff --git a/cherrypy/test/test_compat.py b/cherrypy/test/test_compat.py
new file mode 100644
index 00000000..62cb3b4f
--- /dev/null
+++ b/cherrypy/test/test_compat.py
@@ -0,0 +1,19 @@
+import unittest
+
+import nose
+
+from cherrypy import _cpcompat as compat
+
+
+class StringTester(unittest.TestCase):
+
+ def test_ntob_non_native(self):
+ """
+ ntob should raise an Exception on unicode.
+ (Python 2 only)
+
+ See #1132 for discussion.
+ """
+ if compat.py3k:
+ raise nose.SkipTest("Only useful on Python 2")
+ self.assertRaises(Exception, compat.ntob, unicode('fight'))
diff --git a/cherrypy/test/test_config.py b/cherrypy/test/test_config.py
index b1ef6a37..48fd79f0 100644
--- a/cherrypy/test/test_config.py
+++ b/cherrypy/test/test_config.py
@@ -1,6 +1,7 @@
"""Tests for the CherryPy configuration system."""
-import os, sys
+import os
+import sys
localDir = os.path.join(os.getcwd(), os.path.dirname(__file__))
from cherrypy._cpcompat import ntob, StringIO
@@ -8,6 +9,7 @@ import unittest
import cherrypy
+
def setup_server():
class Root:
@@ -41,7 +43,7 @@ def setup_server():
plain._cp_config = {'request.body.attempt_charsets': ['utf-16']}
favicon_ico = cherrypy.tools.staticfile.handler(
- filename=os.path.join(localDir, '../favicon.ico'))
+ filename=os.path.join(localDir, '../favicon.ico'))
class Foo:
@@ -57,7 +59,7 @@ def setup_server():
return 'Hello world'
silly.exposed = True
silly._cp_config = {'response.headers.X-silly': 'sillyval'}
-
+
# Test the expose and config decorators
#@cherrypy.expose
#@cherrypy.config(foo='this3', **{'bax': 'this4'})
@@ -72,10 +74,10 @@ def setup_server():
return str(cherrypy.request.config.get(key, "None"))
index.exposed = True
-
def raw_namespace(key, value):
if key == 'input.map':
handler = cherrypy.request.handler
+
def wrapper():
params = cherrypy.request.params
for name, coercer in list(value.items()):
@@ -87,6 +89,7 @@ def setup_server():
cherrypy.request.handler = wrapper
elif key == 'output':
handler = cherrypy.request.handler
+
def wrapper():
# 'value' is a type (like int or str).
return value(handler())
@@ -133,6 +136,7 @@ tools.staticfile.filename = %r
from cherrypy.test import helper
+
class ConfigTests(helper.CPWebCase):
setup_server = staticmethod(setup_server)
@@ -147,8 +151,9 @@ class ConfigTests(helper.CPWebCase):
('/foo/', 'bax', 'None'),
('/foo/bar', 'baz', "'that2'"),
('/foo/nex', 'baz', 'that2'),
- # If 'foo' == 'this', then the mount point '/another' leaks into '/'.
- ('/another/','foo', 'None'),
+ # If 'foo' == 'this', then the mount point '/another' leaks into
+ # '/'.
+ ('/another/', 'foo', 'None'),
]
for path, key, expected in tests:
self.getPage(path + "?key=" + key)
@@ -161,7 +166,7 @@ class ConfigTests(helper.CPWebCase):
'request.show_tracebacks': True,
'log.screen': False,
'environment': 'test_suite',
- 'engine.autoreload_on': False,
+ 'engine.autoreload.on': False,
# From global config
'luxuryyacht': 'throatwobblermangrove',
# From Root._cp_config
@@ -171,7 +176,7 @@ class ConfigTests(helper.CPWebCase):
# From Foo.bar._cp_config
'foo': 'this3',
'bax': 'this4',
- }
+ }
for key, expected in expectedconf.items():
self.getPage("/foo/bar?key=" + key)
self.assertBody(repr(expected))
@@ -252,5 +257,5 @@ class VariableSubstitutionTests(unittest.TestCase):
cherrypy.config.update(fp)
self.assertEqual(cherrypy.config["my"]["my.dir"], "/some/dir/my/dir")
- self.assertEqual(cherrypy.config["my"]["my.dir2"], "/some/dir/my/dir/dir2")
-
+ self.assertEqual(cherrypy.config["my"]
+ ["my.dir2"], "/some/dir/my/dir/dir2")
diff --git a/cherrypy/test/test_config_server.py b/cherrypy/test/test_config_server.py
index 0b9718da..40504d8f 100644
--- a/cherrypy/test/test_config_server.py
+++ b/cherrypy/test/test_config_server.py
@@ -1,6 +1,7 @@
"""Tests for the CherryPy configuration system."""
-import os, sys
+import os
+import sys
localDir = os.path.join(os.getcwd(), os.path.dirname(__file__))
import socket
import time
@@ -12,49 +13,51 @@ import cherrypy
from cherrypy.test import helper
+
class ServerConfigTests(helper.CPWebCase):
def setup_server():
-
+
class Root:
+
def index(self):
return cherrypy.request.wsgi_environ['SERVER_PORT']
index.exposed = True
-
+
def upload(self, file):
return "Size: %s" % len(file.file.read())
upload.exposed = True
-
+
def tinyupload(self):
return cherrypy.request.body.read()
tinyupload.exposed = True
tinyupload._cp_config = {'request.body.maxbytes': 100}
-
+
cherrypy.tree.mount(Root())
-
+
cherrypy.config.update({
'server.socket_host': '0.0.0.0',
'server.socket_port': 9876,
'server.max_request_body_size': 200,
'server.max_request_header_size': 500,
'server.socket_timeout': 0.5,
-
+
# Test explicit server.instance
'server.2.instance': 'cherrypy._cpwsgi_server.CPWSGIServer',
'server.2.socket_port': 9877,
-
+
# Test non-numeric <servername>
# Also test default server.instance = builtin server
'server.yetanother.socket_port': 9878,
- })
+ })
setup_server = staticmethod(setup_server)
-
+
PORT = 9876
-
+
def testBasicConfig(self):
self.getPage("/")
self.assertBody(str(self.PORT))
-
+
def testAdditionalServers(self):
if self.scheme == 'https':
return self.skip("not available under ssl")
@@ -64,44 +67,49 @@ class ServerConfigTests(helper.CPWebCase):
self.PORT = 9878
self.getPage("/")
self.assertBody(str(self.PORT))
-
+
def testMaxRequestSizePerHandler(self):
if getattr(cherrypy.server, "using_apache", False):
return self.skip("skipped due to known Apache differences... ")
-
+
self.getPage('/tinyupload', method="POST",
headers=[('Content-Type', 'text/plain'),
('Content-Length', '100')],
body="x" * 100)
self.assertStatus(200)
self.assertBody("x" * 100)
-
+
self.getPage('/tinyupload', method="POST",
headers=[('Content-Type', 'text/plain'),
('Content-Length', '101')],
body="x" * 101)
self.assertStatus(413)
-
+
def testMaxRequestSize(self):
if getattr(cherrypy.server, "using_apache", False):
return self.skip("skipped due to known Apache differences... ")
-
+
for size in (500, 5000, 50000):
self.getPage("/", headers=[('From', "x" * 500)])
self.assertStatus(413)
-
- # Test for http://www.cherrypy.org/ticket/421
+
+ # Test for https://bitbucket.org/cherrypy/cherrypy/issue/421
# (Incorrect border condition in readline of SizeCheckWrapper).
# This hangs in rev 891 and earlier.
lines256 = "x" * 248
self.getPage("/",
headers=[('Host', '%s:%s' % (self.HOST, self.PORT)),
('From', lines256)])
-
+
# Test upload
+ cd = (
+ 'Content-Disposition: form-data; '
+ 'name="file"; '
+ 'filename="hello.txt"'
+ )
body = '\r\n'.join([
'--x',
- 'Content-Disposition: form-data; name="file"; filename="hello.txt"',
+ cd,
'Content-Type: text/plain',
'',
'%s',
@@ -112,10 +120,9 @@ class ServerConfigTests(helper.CPWebCase):
("Content-Length", "%s" % len(b))]
self.getPage('/upload', h, "POST", b)
self.assertBody('Size: %d' % partlen)
-
+
b = body % ("x" * 200)
h = [("Content-type", "multipart/form-data; boundary=x"),
("Content-Length", "%s" % len(b))]
self.getPage('/upload', h, "POST", b)
self.assertStatus(413)
-
diff --git a/cherrypy/test/test_conn.py b/cherrypy/test/test_conn.py
index 1346f593..9aa071e4 100644
--- a/cherrypy/test/test_conn.py
+++ b/cherrypy/test/test_conn.py
@@ -7,14 +7,15 @@ timeout = 1
import cherrypy
-from cherrypy._cpcompat import HTTPConnection, HTTPSConnection, NotConnected, BadStatusLine
-from cherrypy._cpcompat import ntob, urlopen, unicodestr
+from cherrypy._cpcompat import HTTPConnection, HTTPSConnection, NotConnected
+from cherrypy._cpcompat import BadStatusLine, ntob, urlopen, unicodestr
from cherrypy.test import webtest
from cherrypy import _cperror
pov = 'pPeErRsSiIsStTeEnNcCeE oOfF vViIsSiIoOnN'
+
def setup_server():
def raise500():
@@ -93,11 +94,12 @@ def setup_server():
cherrypy.config.update({
'server.max_request_body_size': 1001,
'server.socket_timeout': timeout,
- })
+ })
from cherrypy.test import helper
+
class ConnectionCloseTests(helper.CPWebCase):
setup_server = staticmethod(setup_server)
@@ -179,10 +181,12 @@ class ConnectionCloseTests(helper.CPWebCase):
else:
self.assertHeader("Connection", "close")
- # Make another request on the same connection, which should error.
+ # Make another request on the same connection, which should
+ # error.
self.assertRaises(NotConnected, self.getPage, "/")
- # Try HEAD. See http://www.cherrypy.org/ticket/864.
+ # Try HEAD. See
+ # https://bitbucket.org/cherrypy/cherrypy/issue/864.
self.getPage("/stream", method='HEAD')
self.assertStatus('200 OK')
self.assertBody('')
@@ -220,7 +224,8 @@ class ConnectionCloseTests(helper.CPWebCase):
self.assertNoHeader("Connection", "Keep-Alive")
self.assertNoHeader("Transfer-Encoding")
- # Make another request on the same connection, which should error.
+ # Make another request on the same connection, which should
+ # error.
self.assertRaises(NotConnected, self.getPage, "/")
def test_HTTP10_KeepAlive(self):
@@ -235,7 +240,7 @@ class ConnectionCloseTests(helper.CPWebCase):
self.assertStatus('200 OK')
self.assertBody(pov)
# Apache, for example, may emit a Connection header even for HTTP/1.0
-## self.assertNoHeader("Connection")
+# self.assertNoHeader("Connection")
# Test a keep-alive HTTP/1.0 request.
self.persistent = True
@@ -250,7 +255,7 @@ class ConnectionCloseTests(helper.CPWebCase):
self.assertStatus('200 OK')
self.assertBody(pov)
# Apache, for example, may emit a Connection header even for HTTP/1.0
-## self.assertNoHeader("Connection")
+# self.assertNoHeader("Connection")
class PipelineTests(helper.CPWebCase):
@@ -361,7 +366,6 @@ class PipelineTests(helper.CPWebCase):
self.body = response.read()
self.assertBody(pov)
-
# Make another request on the same socket,
# but timeout on the headers
conn.send(ntob('GET /hello HTTP/1.1'))
@@ -471,7 +475,9 @@ class PipelineTests(helper.CPWebCase):
while True:
line = response.fp.readline().strip()
if line:
- self.fail("100 Continue should not output any headers. Got %r" % line)
+ self.fail(
+ "100 Continue should not output any headers. Got %r" %
+ line)
else:
break
@@ -596,7 +602,7 @@ class ConnectionTests(helper.CPWebCase):
return self.skip()
if (hasattr(self, 'harness') and
- "modpython" in self.harness.__class__.__name__.lower()):
+ "modpython" in self.harness.__class__.__name__.lower()):
# mod_python forbids chunked encoding
return self.skip()
@@ -608,8 +614,8 @@ class ConnectionTests(helper.CPWebCase):
# Try a normal chunked request (with extensions)
body = ntob("8;key=value\r\nxx\r\nxxxx\r\n5\r\nyyyyy\r\n0\r\n"
- "Content-Type: application/json\r\n"
- "\r\n")
+ "Content-Type: application/json\r\n"
+ "\r\n")
conn.putrequest("POST", "/upload", skip_host=True)
conn.putheader("Host", self.HOST)
conn.putheader("Transfer-Encoding", "chunked")
@@ -680,8 +686,9 @@ class ConnectionTests(helper.CPWebCase):
# the actual bytes in the response body.
self.persistent = True
conn = self.HTTP_CONN
- conn.putrequest("GET", "/custom_cl?body=I+too&body=+have+too+many&cl=5",
- skip_host=True)
+ conn.putrequest(
+ "GET", "/custom_cl?body=I+too&body=+have+too+many&cl=5",
+ skip_host=True)
conn.putheader("Host", self.HOST)
conn.endheaders()
response = conn.getresponse()
@@ -692,7 +699,7 @@ class ConnectionTests(helper.CPWebCase):
def test_598(self):
remote_data_conn = urlopen('%s://%s:%s/one_megabyte_of_a/' %
- (self.scheme, self.HOST, self.PORT,))
+ (self.scheme, self.HOST, self.PORT,))
buf = remote_data_conn.read(512)
time.sleep(timeout * 0.6)
remaining = (1024 * 1024) - 512
@@ -731,4 +738,3 @@ class BadRequestTests(helper.CPWebCase):
self.body = response.read()
self.assertBody("HTTP requires CRLF terminators")
conn.close()
-
diff --git a/cherrypy/test/test_core.py b/cherrypy/test/test_core.py
index 0956a94a..a09125da 100644
--- a/cherrypy/test/test_core.py
+++ b/cherrypy/test/test_core.py
@@ -17,38 +17,40 @@ favicon_path = os.path.join(os.getcwd(), localDir, "../favicon.ico")
from cherrypy.test import helper
+
class CoreRequestHandlingTest(helper.CPWebCase):
def setup_server():
class Root:
-
+
def index(self):
return "hello"
index.exposed = True
-
+
favicon_ico = tools.staticfile.handler(filename=favicon_path)
-
+
def defct(self, newct):
newct = "text/%s" % newct
cherrypy.config.update({'tools.response_headers.on': True,
'tools.response_headers.headers':
[('Content-Type', newct)]})
defct.exposed = True
-
+
def baseurl(self, path_info, relative=None):
return cherrypy.url(path_info, relative=bool(relative))
baseurl.exposed = True
-
+
root = Root()
-
+
if sys.version_info >= (2, 5):
from cherrypy.test._test_decorators import ExposeExamples
root.expose_dec = ExposeExamples()
-
class TestType(type):
- """Metaclass which automatically exposes all functions in each subclass,
- and adds an instance of the subclass as an attribute of root.
+
+ """Metaclass which automatically exposes all functions in each
+ subclass, and adds an instance of the subclass as an attribute
+ of root.
"""
def __init__(cls, name, bases, dct):
type.__init__(cls, name, bases, dct)
@@ -57,133 +59,134 @@ class CoreRequestHandlingTest(helper.CPWebCase):
value.exposed = True
setattr(root, name.lower(), cls())
Test = TestType('Test', (object, ), {})
-
-
+
class URL(Test):
-
+
_cp_config = {'tools.trailing_slash.on': False}
-
+
def index(self, path_info, relative=None):
if relative != 'server':
relative = bool(relative)
return cherrypy.url(path_info, relative=relative)
-
+
def leaf(self, path_info, relative=None):
if relative != 'server':
relative = bool(relative)
return cherrypy.url(path_info, relative=relative)
-
def log_status():
Status.statuses.append(cherrypy.response.status)
- cherrypy.tools.log_status = cherrypy.Tool('on_end_resource', log_status)
-
+ cherrypy.tools.log_status = cherrypy.Tool(
+ 'on_end_resource', log_status)
class Status(Test):
-
+
def index(self):
return "normal"
-
+
def blank(self):
cherrypy.response.status = ""
-
+
# According to RFC 2616, new status codes are OK as long as they
# are between 100 and 599.
-
+
# Here is an illegal code...
def illegal(self):
cherrypy.response.status = 781
return "oops"
-
+
# ...and here is an unknown but legal code.
def unknown(self):
cherrypy.response.status = "431 My custom error"
return "funky"
-
+
# Non-numeric code
def bad(self):
cherrypy.response.status = "error"
return "bad news"
-
+
statuses = []
+
def on_end_resource_stage(self):
return repr(self.statuses)
on_end_resource_stage._cp_config = {'tools.log_status.on': True}
-
class Redirect(Test):
-
+
class Error:
_cp_config = {"tools.err_redirect.on": True,
"tools.err_redirect.url": "/errpage",
"tools.err_redirect.internal": False,
}
-
+
def index(self):
raise NameError("redirect_test")
index.exposed = True
error = Error()
-
+
def index(self):
return "child"
-
+
def custom(self, url, code):
raise cherrypy.HTTPRedirect(url, code)
-
+
def by_code(self, code):
raise cherrypy.HTTPRedirect("somewhere%20else", code)
by_code._cp_config = {'tools.trailing_slash.extra': True}
-
+
def nomodify(self):
raise cherrypy.HTTPRedirect("", 304)
-
+
def proxy(self):
raise cherrypy.HTTPRedirect("proxy", 305)
-
+
def stringify(self):
return str(cherrypy.HTTPRedirect("/"))
-
+
def fragment(self, frag):
raise cherrypy.HTTPRedirect("/some/url#%s" % frag)
-
+
def login_redir():
if not getattr(cherrypy.request, "login", None):
raise cherrypy.InternalRedirect("/internalredirect/login")
tools.login_redir = _cptools.Tool('before_handler', login_redir)
-
+
def redir_custom():
raise cherrypy.InternalRedirect("/internalredirect/custom_err")
-
+
class InternalRedirect(Test):
-
+
def index(self):
raise cherrypy.InternalRedirect("/")
-
+
def choke(self):
return 3 / 0
choke.exposed = True
choke._cp_config = {'hooks.before_error_response': redir_custom}
-
+
def relative(self, a, b):
raise cherrypy.InternalRedirect("cousin?t=6")
-
+
def cousin(self, t):
assert cherrypy.request.prev.closed
return cherrypy.request.prev.query_string
-
+
def petshop(self, user_id):
if user_id == "parrot":
# Trade it for a slug when redirecting
- raise cherrypy.InternalRedirect('/image/getImagesByUser?user_id=slug')
+ raise cherrypy.InternalRedirect(
+ '/image/getImagesByUser?user_id=slug')
elif user_id == "terrier":
# Trade it for a fish when redirecting
- raise cherrypy.InternalRedirect('/image/getImagesByUser?user_id=fish')
+ raise cherrypy.InternalRedirect(
+ '/image/getImagesByUser?user_id=fish')
else:
# This should pass the user_id through to getImagesByUser
raise cherrypy.InternalRedirect(
'/image/getImagesByUser?user_id=%s' % str(user_id))
-
- # We support Python 2.3, but the @-deco syntax would look like this:
+
+ # We support Python 2.3, but the @-deco syntax would look like
+ # this:
# @tools.login_redir()
def secure(self):
return "Welcome!"
@@ -191,65 +194,63 @@ class CoreRequestHandlingTest(helper.CPWebCase):
# Since calling the tool returns the same function you pass in,
# you could skip binding the return value, and just write:
# tools.login_redir()(secure)
-
+
def login(self):
return "Please log in"
-
+
def custom_err(self):
return "Something went horribly wrong."
-
+
def early_ir(self, arg):
return "whatever"
early_ir._cp_config = {'hooks.before_request_body': redir_custom}
-
-
+
class Image(Test):
-
+
def getImagesByUser(self, user_id):
return "0 images for %s" % user_id
-
class Flatten(Test):
-
+
def as_string(self):
return "content"
-
+
def as_list(self):
return ["con", "tent"]
-
+
def as_yield(self):
yield ntob("content")
-
+
def as_dblyield(self):
yield self.as_yield()
as_dblyield._cp_config = {'tools.flatten.on': True}
-
+
def as_refyield(self):
for chunk in self.as_yield():
yield chunk
-
-
+
class Ranges(Test):
-
+
def get_ranges(self, bytes):
return repr(httputil.get_ranges('bytes=%s' % bytes, 8))
-
+
def slice_file(self):
path = os.path.join(os.getcwd(), os.path.dirname(__file__))
- return static.serve_file(os.path.join(path, "static/index.html"))
-
+ return static.serve_file(
+ os.path.join(path, "static/index.html"))
class Cookies(Test):
-
+
def single(self, name):
cookie = cherrypy.request.cookie[name]
# Python2's SimpleCookie.__setitem__ won't take unicode keys.
cherrypy.response.cookie[str(name)] = cookie.value
-
+
def multiple(self, names):
for name in names:
cookie = cherrypy.request.cookie[name]
- # Python2's SimpleCookie.__setitem__ won't take unicode keys.
+ # Python2's SimpleCookie.__setitem__ won't take unicode
+ # keys.
cherrypy.response.cookie[str(name)] = cookie.value
def append_headers(header_list, debug=False):
@@ -258,44 +259,44 @@ class CoreRequestHandlingTest(helper.CPWebCase):
"Extending response headers with %s" % repr(header_list),
"TOOLS.APPEND_HEADERS")
cherrypy.serving.response.header_list.extend(header_list)
- cherrypy.tools.append_headers = cherrypy.Tool('on_end_resource', append_headers)
-
+ cherrypy.tools.append_headers = cherrypy.Tool(
+ 'on_end_resource', append_headers)
+
class MultiHeader(Test):
-
+
def header_list(self):
pass
header_list = cherrypy.tools.append_headers(header_list=[
(ntob('WWW-Authenticate'), ntob('Negotiate')),
(ntob('WWW-Authenticate'), ntob('Basic realm="foo"')),
- ])(header_list)
-
- def commas(self):
- cherrypy.response.headers['WWW-Authenticate'] = 'Negotiate,Basic realm="foo"'
+ ])(header_list)
+ def commas(self):
+ cherrypy.response.headers[
+ 'WWW-Authenticate'] = 'Negotiate,Basic realm="foo"'
cherrypy.tree.mount(root)
setup_server = staticmethod(setup_server)
-
def testStatus(self):
self.getPage("/status/")
self.assertBody('normal')
self.assertStatus(200)
-
+
self.getPage("/status/blank")
self.assertBody('')
self.assertStatus(200)
-
+
self.getPage("/status/illegal")
self.assertStatus(500)
msg = "Illegal response status from server (781 is out of range)."
self.assertErrorPage(500, msg)
-
+
if not getattr(cherrypy.server, 'using_apache', False):
self.getPage("/status/unknown")
self.assertBody('funky')
self.assertStatus(431)
-
+
self.getPage("/status/bad")
self.assertStatus(500)
msg = "Illegal response status from server ('error' is non-numeric)."
@@ -315,7 +316,7 @@ class CoreRequestHandlingTest(helper.CPWebCase):
self.assertStatus(301)
self.assertInBody("<a href='%s/redirect/?id=3'>"
"%s/redirect/?id=3</a>" % (self.base(), self.base()))
-
+
if self.prefix():
# Corner case: the "trailing slash" redirect could be tricky if
# we're using a virtual root and the URI is "/vroot" (no slash).
@@ -323,7 +324,7 @@ class CoreRequestHandlingTest(helper.CPWebCase):
self.assertStatus(301)
self.assertInBody("<a href='%s/'>%s/</a>" %
(self.base(), self.base()))
-
+
# Test that requests for NON-index methods WITH a trailing slash
# get redirected to the same URI path WITHOUT a trailing slash.
# Make sure GET params are preserved.
@@ -332,7 +333,7 @@ class CoreRequestHandlingTest(helper.CPWebCase):
self.assertInBody("<a href='%s/redirect/by_code?code=307'>"
"%s/redirect/by_code?code=307</a>"
% (self.base(), self.base()))
-
+
# If the trailing_slash tool is off, CP should just continue
# as if the slashes were correct. But it needs some help
# inside cherrypy.url to form correct output.
@@ -340,45 +341,50 @@ class CoreRequestHandlingTest(helper.CPWebCase):
self.assertBody('%s/url/page1' % self.base())
self.getPage('/url/leaf/?path_info=page1')
self.assertBody('%s/url/page1' % self.base())
-
+
def testRedirect(self):
self.getPage("/redirect/")
self.assertBody('child')
self.assertStatus(200)
-
+
self.getPage("/redirect/by_code?code=300")
- self.assertMatchesBody(r"<a href='(.*)somewhere%20else'>\1somewhere%20else</a>")
+ self.assertMatchesBody(
+ r"<a href='(.*)somewhere%20else'>\1somewhere%20else</a>")
self.assertStatus(300)
-
+
self.getPage("/redirect/by_code?code=301")
- self.assertMatchesBody(r"<a href='(.*)somewhere%20else'>\1somewhere%20else</a>")
+ self.assertMatchesBody(
+ r"<a href='(.*)somewhere%20else'>\1somewhere%20else</a>")
self.assertStatus(301)
-
+
self.getPage("/redirect/by_code?code=302")
- self.assertMatchesBody(r"<a href='(.*)somewhere%20else'>\1somewhere%20else</a>")
+ self.assertMatchesBody(
+ r"<a href='(.*)somewhere%20else'>\1somewhere%20else</a>")
self.assertStatus(302)
-
+
self.getPage("/redirect/by_code?code=303")
- self.assertMatchesBody(r"<a href='(.*)somewhere%20else'>\1somewhere%20else</a>")
+ self.assertMatchesBody(
+ r"<a href='(.*)somewhere%20else'>\1somewhere%20else</a>")
self.assertStatus(303)
-
+
self.getPage("/redirect/by_code?code=307")
- self.assertMatchesBody(r"<a href='(.*)somewhere%20else'>\1somewhere%20else</a>")
+ self.assertMatchesBody(
+ r"<a href='(.*)somewhere%20else'>\1somewhere%20else</a>")
self.assertStatus(307)
-
+
self.getPage("/redirect/nomodify")
self.assertBody('')
self.assertStatus(304)
-
+
self.getPage("/redirect/proxy")
self.assertBody('')
self.assertStatus(305)
-
+
# HTTPRedirect on error
self.getPage("/redirect/error/")
self.assertStatus(('302 Found', '303 See Other'))
self.assertInBody('/errpage')
-
+
# Make sure str(HTTPRedirect()) works.
self.getPage("/redirect/stringify", protocol="HTTP/1.0")
self.assertStatus(200)
@@ -387,82 +393,87 @@ class CoreRequestHandlingTest(helper.CPWebCase):
self.getPage("/redirect/stringify", protocol="HTTP/1.1")
self.assertStatus(200)
self.assertBody("(['%s/'], 303)" % self.base())
-
+
# check that #fragments are handled properly
# http://skrb.org/ietf/http_errata.html#location-fragments
frag = "foo"
self.getPage("/redirect/fragment/%s" % frag)
- self.assertMatchesBody(r"<a href='(.*)\/some\/url\#%s'>\1\/some\/url\#%s</a>" % (frag, frag))
+ self.assertMatchesBody(
+ r"<a href='(.*)\/some\/url\#%s'>\1\/some\/url\#%s</a>" % (
+ frag, frag))
loc = self.assertHeader('Location')
assert loc.endswith("#%s" % frag)
self.assertStatus(('302 Found', '303 See Other'))
-
+
# check injection protection
- # See http://www.cherrypy.org/ticket/1003
- self.getPage("/redirect/custom?code=303&url=/foobar/%0d%0aSet-Cookie:%20somecookie=someval")
+ # See https://bitbucket.org/cherrypy/cherrypy/issue/1003
+ self.getPage(
+ "/redirect/custom?"
+ "code=303&url=/foobar/%0d%0aSet-Cookie:%20somecookie=someval")
self.assertStatus(303)
loc = self.assertHeader('Location')
assert 'Set-Cookie' in loc
self.assertNoHeader('Set-Cookie')
-
+
def test_InternalRedirect(self):
# InternalRedirect
self.getPage("/internalredirect/")
self.assertBody('hello')
self.assertStatus(200)
-
+
# Test passthrough
- self.getPage("/internalredirect/petshop?user_id=Sir-not-appearing-in-this-film")
+ self.getPage(
+ "/internalredirect/petshop?user_id=Sir-not-appearing-in-this-film")
self.assertBody('0 images for Sir-not-appearing-in-this-film')
self.assertStatus(200)
-
+
# Test args
self.getPage("/internalredirect/petshop?user_id=parrot")
self.assertBody('0 images for slug')
self.assertStatus(200)
-
+
# Test POST
self.getPage("/internalredirect/petshop", method="POST",
body="user_id=terrier")
self.assertBody('0 images for fish')
self.assertStatus(200)
-
+
# Test ir before body read
self.getPage("/internalredirect/early_ir", method="POST",
body="arg=aha!")
self.assertBody("Something went horribly wrong.")
self.assertStatus(200)
-
+
self.getPage("/internalredirect/secure")
self.assertBody('Please log in')
self.assertStatus(200)
-
+
# Relative path in InternalRedirect.
# Also tests request.prev.
self.getPage("/internalredirect/relative?a=3&b=5")
self.assertBody("a=3&b=5")
self.assertStatus(200)
-
+
# InternalRedirect on error
self.getPage("/internalredirect/choke")
self.assertStatus(200)
self.assertBody("Something went horribly wrong.")
-
+
def testFlatten(self):
for url in ["/flatten/as_string", "/flatten/as_list",
"/flatten/as_yield", "/flatten/as_dblyield",
"/flatten/as_refyield"]:
self.getPage(url)
self.assertBody('content')
-
+
def testRanges(self):
self.getPage("/ranges/get_ranges?bytes=3-6")
self.assertBody("[(3, 7)]")
-
+
# Test multiple ranges and a suffix-byte-range-spec, for good measure.
self.getPage("/ranges/get_ranges?bytes=2-4,-1")
self.assertBody("[(2, 5), (7, 8)]")
-
+
# Get a partial file.
if cherrypy.server.protocol_version == "HTTP/1.1":
self.getPage("/ranges/slice_file", [('Range', 'bytes=2-5')])
@@ -470,7 +481,7 @@ class CoreRequestHandlingTest(helper.CPWebCase):
self.assertHeader("Content-Type", "text/html;charset=utf-8")
self.assertHeader("Content-Range", "bytes 2-5/14")
self.assertBody("llo,")
-
+
# What happens with overlapping ranges (and out of order, too)?
self.getPage("/ranges/slice_file", [('Range', 'bytes=4-6,2-5')])
self.assertStatus(206)
@@ -491,7 +502,7 @@ class CoreRequestHandlingTest(helper.CPWebCase):
"--%s--\r\n" % (boundary, boundary, boundary))
self.assertBody(expected_body)
self.assertHeader("Content-Length")
-
+
# Test "416 Requested Range Not Satisfiable"
self.getPage("/ranges/slice_file", [('Range', 'bytes=2300-2900')])
self.assertStatus(416)
@@ -504,37 +515,37 @@ class CoreRequestHandlingTest(helper.CPWebCase):
self.getPage("/ranges/slice_file", [('Range', 'bytes=2-5')])
self.assertStatus(200)
self.assertBody("Hello, world\r\n")
-
+
def testFavicon(self):
# favicon.ico is served by staticfile.
icofilename = os.path.join(localDir, "../favicon.ico")
icofile = open(icofilename, "rb")
data = icofile.read()
icofile.close()
-
+
self.getPage("/favicon.ico")
self.assertBody(data)
-
+
def testCookies(self):
if sys.version_info >= (2, 5):
header_value = lambda x: x
else:
- header_value = lambda x: x+';'
-
+ header_value = lambda x: x + ';'
+
self.getPage("/cookies/single?name=First",
[('Cookie', 'First=Dinsdale;')])
self.assertHeader('Set-Cookie', header_value('First=Dinsdale'))
-
+
self.getPage("/cookies/multiple?names=First&names=Last",
[('Cookie', 'First=Dinsdale; Last=Piranha;'),
])
self.assertHeader('Set-Cookie', header_value('First=Dinsdale'))
self.assertHeader('Set-Cookie', header_value('Last=Piranha'))
-
- self.getPage("/cookies/single?name=Something-With:Colon",
- [('Cookie', 'Something-With:Colon=some-value')])
+
+ self.getPage("/cookies/single?name=Something-With%2CComma",
+ [('Cookie', 'Something-With,Comma=some-value')])
self.assertStatus(400)
-
+
def testDefaultContentType(self):
self.getPage('/')
self.assertHeader('Content-Type', 'text/html;charset=utf-8')
@@ -542,16 +553,17 @@ class CoreRequestHandlingTest(helper.CPWebCase):
self.getPage('/')
self.assertHeader('Content-Type', 'text/plain;charset=utf-8')
self.getPage('/defct/html')
-
+
def test_multiple_headers(self):
self.getPage('/multiheader/header_list')
- self.assertEqual([(k, v) for k, v in self.headers if k == 'WWW-Authenticate'],
- [('WWW-Authenticate', 'Negotiate'),
- ('WWW-Authenticate', 'Basic realm="foo"'),
- ])
+ self.assertEqual(
+ [(k, v) for k, v in self.headers if k == 'WWW-Authenticate'],
+ [('WWW-Authenticate', 'Negotiate'),
+ ('WWW-Authenticate', 'Basic realm="foo"'),
+ ])
self.getPage('/multiheader/commas')
self.assertHeader('WWW-Authenticate', 'Negotiate,Basic realm="foo"')
-
+
def test_cherrypy_url(self):
# Input relative to current
self.getPage('/url/leaf?path_info=page1')
@@ -563,13 +575,13 @@ class CoreRequestHandlingTest(helper.CPWebCase):
self.getPage('/url/leaf?path_info=page1',
headers=[('Host', host)])
self.assertBody('%s://%s/url/page1' % (self.scheme, host))
-
+
# Input is 'absolute'; that is, relative to script_name
self.getPage('/url/leaf?path_info=/page1')
self.assertBody('%s/page1' % self.base())
self.getPage('/url/?path_info=/page1')
self.assertBody('%s/page1' % self.base())
-
+
# Single dots
self.getPage('/url/leaf?path_info=./page1')
self.assertBody('%s/url/page1' % self.base())
@@ -577,7 +589,7 @@ class CoreRequestHandlingTest(helper.CPWebCase):
self.assertBody('%s/url/other/page1' % self.base())
self.getPage('/url/?path_info=/other/./page1')
self.assertBody('%s/other/page1' % self.base())
-
+
# Double dots
self.getPage('/url/leaf?path_info=../page1')
self.assertBody('%s/page1' % self.base())
@@ -585,7 +597,7 @@ class CoreRequestHandlingTest(helper.CPWebCase):
self.assertBody('%s/url/page1' % self.base())
self.getPage('/url/leaf?path_info=/other/../page1')
self.assertBody('%s/page1' % self.base())
-
+
# Output relative to current path or script_name
self.getPage('/url/?path_info=page1&relative=True')
self.assertBody('page1')
@@ -599,14 +611,14 @@ class CoreRequestHandlingTest(helper.CPWebCase):
self.assertBody('../page1')
self.getPage('/url/?path_info=other/../page1&relative=True')
self.assertBody('page1')
-
+
# Output relative to /
self.getPage('/baseurl?path_info=ab&relative=True')
self.assertBody('ab')
# Output relative to /
self.getPage('/baseurl?path_info=/ab&relative=True')
self.assertBody('ab')
-
+
# absolute-path references ("server-relative")
# Input relative to current
self.getPage('/url/leaf?path_info=page1&relative=server')
@@ -618,21 +630,21 @@ class CoreRequestHandlingTest(helper.CPWebCase):
self.assertBody('/page1')
self.getPage('/url/?path_info=/page1&relative=server')
self.assertBody('/page1')
-
+
def test_expose_decorator(self):
if not sys.version_info >= (2, 5):
return self.skip("skipped (Python 2.5+ only) ")
-
+
# Test @expose
self.getPage("/expose_dec/no_call")
self.assertStatus(200)
self.assertBody("Mr E. R. Bradshaw")
-
+
# Test @expose()
self.getPage("/expose_dec/call_empty")
self.assertStatus(200)
self.assertBody("Mrs. B.J. Smegma")
-
+
# Test @expose("alias")
self.getPage("/expose_dec/call_alias")
self.assertStatus(200)
@@ -641,7 +653,7 @@ class CoreRequestHandlingTest(helper.CPWebCase):
self.getPage("/expose_dec/nesbitt")
self.assertStatus(200)
self.assertBody("Mr Nesbitt")
-
+
# Test @expose(["alias1", "alias2"])
self.getPage("/expose_dec/alias1")
self.assertStatus(200)
@@ -653,7 +665,7 @@ class CoreRequestHandlingTest(helper.CPWebCase):
self.getPage("/expose_dec/andrews")
self.assertStatus(200)
self.assertBody("Mr Ken Andrews")
-
+
# Test @expose(alias="alias")
self.getPage("/expose_dec/alias3")
self.assertStatus(200)
@@ -666,23 +678,25 @@ class ErrorTests(helper.CPWebCase):
def break_header():
# Add a header after finalize that is invalid
cherrypy.serving.response.header_list.append((2, 3))
- cherrypy.tools.break_header = cherrypy.Tool('on_end_resource', break_header)
-
+ cherrypy.tools.break_header = cherrypy.Tool(
+ 'on_end_resource', break_header)
+
class Root:
+
def index(self):
return "hello"
index.exposed = True
-
+
def start_response_error(self):
return "salud!"
start_response_error._cp_config = {'tools.break_header.on': True}
root = Root()
-
+
cherrypy.tree.mount(root)
setup_server = staticmethod(setup_server)
def test_start_response_error(self):
self.getPage("/start_response_error")
self.assertStatus(500)
- self.assertInBody("TypeError: response.header_list key 2 is not a byte string.")
-
+ self.assertInBody(
+ "TypeError: response.header_list key 2 is not a byte string.")
diff --git a/cherrypy/test/test_dynamicobjectmapping.py b/cherrypy/test/test_dynamicobjectmapping.py
index 0395b7b4..a64e9925 100644
--- a/cherrypy/test/test_dynamicobjectmapping.py
+++ b/cherrypy/test/test_dynamicobjectmapping.py
@@ -6,9 +6,9 @@ from cherrypy.test import helper
script_names = ["", "/foo", "/users/fred/blog", "/corp/blog"]
-
def setup_server():
class SubSubRoot:
+
def index(self):
return "SubSubRoot index"
index.exposed = True
@@ -31,6 +31,7 @@ def setup_server():
}
class SubRoot:
+
def index(self):
return "SubRoot index"
index.exposed = True
@@ -50,7 +51,9 @@ def setup_server():
'1': SubRoot(),
'2': SubRoot(),
}
+
class Root:
+
def index(self):
return "index"
index.exposed = True
@@ -70,12 +73,14 @@ def setup_server():
# DynamicNodeAndMethodDispatcher example.
# This example exposes a fairly naive HTTP api
class User(object):
+
def __init__(self, id, name):
self.id = id
self.name = name
def __unicode__(self):
return unicode(self.name)
+
def __str__(self):
return str(self.name)
@@ -111,6 +116,7 @@ def setup_server():
class UserInstanceNode(object):
exposed = True
+
def __init__(self, id):
self.id = id
self.user = user_lookup.get(id, None)
@@ -135,7 +141,8 @@ def setup_server():
def PUT(self, name):
"""
- Create a new user with the specified id, or edit it if it already exists
+ Create a new user with the specified id, or edit it if it already
+ exists
"""
if self.user:
# Edit the current user
@@ -154,79 +161,90 @@ def setup_server():
del self.user
return "DELETE %d" % id
-
class ABHandler:
+
class CustomDispatch:
+
def index(self, a, b):
return "custom"
index.exposed = True
-
+
def _cp_dispatch(self, vpath):
"""Make sure that if we don't pop anything from vpath,
processing still works.
"""
return self.CustomDispatch()
-
+
def index(self, a, b=None):
- body = [ 'a:' + str(a) ]
+ body = ['a:' + str(a)]
if b is not None:
body.append(',b:' + str(b))
return ''.join(body)
index.exposed = True
-
+
def delete(self, a, b):
return 'deleting ' + str(a) + ' and ' + str(b)
delete.exposed = True
-
+
class IndexOnly:
+
def _cp_dispatch(self, vpath):
- """Make sure that popping ALL of vpath still shows the index
+ """Make sure that popping ALL of vpath still shows the index
handler.
"""
while vpath:
vpath.pop()
return self
-
+
def index(self):
return "IndexOnly index"
index.exposed = True
-
+
class DecoratedPopArgs:
+
"""Test _cp_dispatch with @cherrypy.popargs."""
+
def index(self):
return "no params"
index.exposed = True
-
+
def hi(self):
return "hi was not interpreted as 'a' param"
hi.exposed = True
- DecoratedPopArgs = cherrypy.popargs('a', 'b', handler=ABHandler())(DecoratedPopArgs)
-
+ DecoratedPopArgs = cherrypy.popargs(
+ 'a', 'b', handler=ABHandler())(DecoratedPopArgs)
+
class NonDecoratedPopArgs:
+
"""Test _cp_dispatch = cherrypy.popargs()"""
-
+
_cp_dispatch = cherrypy.popargs('a')
-
+
def index(self, a):
return "index: " + str(a)
index.exposed = True
-
+
class ParameterizedHandler:
+
"""Special handler created for each request"""
-
+
def __init__(self, a):
self.a = a
-
+
def index(self):
if 'a' in cherrypy.request.params:
- raise Exception("Parameterized handler argument ended up in request.params")
+ raise Exception(
+ "Parameterized handler argument ended up in "
+ "request.params")
return self.a
index.exposed = True
-
+
class ParameterizedPopArgs:
+
"""Test cherrypy.popargs() with a function call handler"""
- ParameterizedPopArgs = cherrypy.popargs('a', handler=ParameterizedHandler)(ParameterizedPopArgs)
-
+ ParameterizedPopArgs = cherrypy.popargs(
+ 'a', handler=ParameterizedHandler)(ParameterizedPopArgs)
+
Root.decorated = DecoratedPopArgs()
Root.undecorated = NonDecoratedPopArgs()
Root.index_only = IndexOnly()
@@ -237,14 +255,15 @@ def setup_server():
md = cherrypy.dispatch.MethodDispatcher('dynamic_dispatch')
for url in script_names:
conf = {'/': {
- 'user': (url or "/").split("/")[-2],
- },
- '/users': {
- 'request.dispatch': md
- },
- }
+ 'user': (url or "/").split("/")[-2],
+ },
+ '/users': {
+ 'request.dispatch': md
+ },
+ }
cherrypy.tree.mount(Root(), url, conf)
+
class DynamicObjectMappingTest(helper.CPWebCase):
setup_server = staticmethod(setup_server)
@@ -354,12 +373,14 @@ class DynamicObjectMappingTest(helper.CPWebCase):
self.assertHeader('Allow', headers)
# Make sure POSTs update already existings resources
- self.getPage("/users/%d" % id, method='POST', body="name=%s" % updatedname)
+ self.getPage("/users/%d" %
+ id, method='POST', body="name=%s" % updatedname)
self.assertBody("POST %d" % id)
self.assertHeader('Allow', headers)
# Make sure PUTs Update already existing resources.
- self.getPage("/users/%d" % id, method='PUT', body="name=%s" % updatedname)
+ self.getPage("/users/%d" %
+ id, method='PUT', body="name=%s" % updatedname)
self.assertBody("PUT %d" % id)
self.assertHeader('Allow', headers)
@@ -368,37 +389,35 @@ class DynamicObjectMappingTest(helper.CPWebCase):
self.assertBody("DELETE %d" % id)
self.assertHeader('Allow', headers)
-
# GET acts like a container
self.getPage("/users")
self.assertBody("[]")
self.assertHeader('Allow', 'GET, HEAD, POST')
-
+
def testVpathDispatch(self):
self.getPage("/decorated/")
self.assertBody("no params")
-
+
self.getPage("/decorated/hi")
self.assertBody("hi was not interpreted as 'a' param")
-
+
self.getPage("/decorated/yo/")
self.assertBody("a:yo")
-
+
self.getPage("/decorated/yo/there/")
self.assertBody("a:yo,b:there")
-
+
self.getPage("/decorated/yo/there/delete")
self.assertBody("deleting yo and there")
-
+
self.getPage("/decorated/yo/there/handled_by_dispatch/")
self.assertBody("custom")
-
+
self.getPage("/undecorated/blah/")
self.assertBody("index: blah")
-
+
self.getPage("/index_only/a/b/c/d/e/f/g/")
self.assertBody("IndexOnly index")
-
+
self.getPage("/parameter_test/argument2/")
self.assertBody("argument2")
-
diff --git a/cherrypy/test/test_encoding.py b/cherrypy/test/test_encoding.py
index 2d0ce761..3961b4d4 100644
--- a/cherrypy/test/test_encoding.py
+++ b/cherrypy/test/test_encoding.py
@@ -18,66 +18,76 @@ class EncodingTests(helper.CPWebCase):
def setup_server():
class Root:
+
def index(self, param):
- assert param == europoundUnicode, "%r != %r" % (param, europoundUnicode)
+ assert param == europoundUnicode, "%r != %r" % (
+ param, europoundUnicode)
yield europoundUnicode
index.exposed = True
-
+
def mao_zedong(self):
return sing
mao_zedong.exposed = True
-
+
def utf8(self):
return sing8
utf8.exposed = True
utf8._cp_config = {'tools.encode.encoding': 'utf-8'}
-
+
def cookies_and_headers(self):
# if the headers have non-ascii characters and a cookie has
# any part which is unicode (even ascii), the response
# should not fail.
cherrypy.response.cookie['candy'] = 'bar'
cherrypy.response.cookie['candy']['domain'] = 'cherrypy.org'
- cherrypy.response.headers['Some-Header'] = 'My d\xc3\xb6g has fleas'
+ cherrypy.response.headers[
+ 'Some-Header'] = 'My d\xc3\xb6g has fleas'
return 'Any content'
cookies_and_headers.exposed = True
def reqparams(self, *args, **kwargs):
- return ntob(', ').join([": ".join((k, v)).encode('utf8')
- for k, v in cherrypy.request.params.items()])
+ return ntob(', ').join(
+ [": ".join((k, v)).encode('utf8')
+ for k, v in sorted(cherrypy.request.params.items())]
+ )
reqparams.exposed = True
-
+
def nontext(self, *args, **kwargs):
- cherrypy.response.headers['Content-Type'] = 'application/binary'
+ cherrypy.response.headers[
+ 'Content-Type'] = 'application/binary'
return '\x00\x01\x02\x03'
nontext.exposed = True
nontext._cp_config = {'tools.encode.text_only': False,
'tools.encode.add_charset': True,
}
-
+
class GZIP:
+
def index(self):
yield "Hello, world"
index.exposed = True
-
+
def noshow(self):
- # Test for ticket #147, where yield showed no exceptions (content-
- # encoding was still gzip even though traceback wasn't zipped).
+ # Test for ticket #147, where yield showed no exceptions
+ # (content-encoding was still gzip even though traceback
+ # wasn't zipped).
raise IndexError()
yield "Here be dragons"
noshow.exposed = True
# Turn encoding off so the gzip tool is the one doing the collapse.
noshow._cp_config = {'tools.encode.on': False}
-
+
def noshow_stream(self):
- # Test for ticket #147, where yield showed no exceptions (content-
- # encoding was still gzip even though traceback wasn't zipped).
+ # Test for ticket #147, where yield showed no exceptions
+ # (content-encoding was still gzip even though traceback
+ # wasn't zipped).
raise IndexError()
yield "Here be dragons"
noshow_stream.exposed = True
noshow_stream._cp_config = {'response.stream': True}
-
+
class Decode:
+
def extra_charset(self, *args, **kwargs):
return ', '.join([": ".join((k, v))
for k, v in cherrypy.request.params.items()])
@@ -85,8 +95,8 @@ class EncodingTests(helper.CPWebCase):
extra_charset._cp_config = {
'tools.decode.on': True,
'tools.decode.default_encoding': ['utf-16'],
- }
-
+ }
+
def force_charset(self, *args, **kwargs):
return ', '.join([": ".join((k, v))
for k, v in cherrypy.request.params.items()])
@@ -94,8 +104,8 @@ class EncodingTests(helper.CPWebCase):
force_charset._cp_config = {
'tools.decode.on': True,
'tools.decode.encoding': 'utf-16',
- }
-
+ }
+
root = Root()
root.gzip = GZIP()
root.decode = Decode()
@@ -106,193 +116,218 @@ class EncodingTests(helper.CPWebCase):
europoundUtf8 = europoundUnicode.encode('utf-8')
self.getPage(ntob('/?param=') + europoundUtf8)
self.assertBody(europoundUtf8)
-
+
# Encoded utf8 query strings MUST be parsed correctly.
# Here, q is the POUND SIGN U+00A3 encoded in utf8 and then %HEX
self.getPage("/reqparams?q=%C2%A3")
# The return value will be encoded as utf8.
self.assertBody(ntob("q: \xc2\xa3"))
-
+
# Query strings that are incorrectly encoded MUST raise 404.
# Here, q is the POUND SIGN U+00A3 encoded in latin1 and then %HEX
self.getPage("/reqparams?q=%A3")
self.assertStatus(404)
- self.assertErrorPage(404,
+ self.assertErrorPage(
+ 404,
"The given query string could not be processed. Query "
"strings for this resource must be encoded with 'utf8'.")
-
+
def test_urlencoded_decoding(self):
# Test the decoding of an application/x-www-form-urlencoded entity.
europoundUtf8 = europoundUnicode.encode('utf-8')
- body=ntob("param=") + europoundUtf8
- self.getPage('/', method='POST',
- headers=[("Content-Type", "application/x-www-form-urlencoded"),
- ("Content-Length", str(len(body))),
- ],
+ body = ntob("param=") + europoundUtf8
+ self.getPage('/',
+ method='POST',
+ headers=[
+ ("Content-Type", "application/x-www-form-urlencoded"),
+ ("Content-Length", str(len(body))),
+ ],
body=body),
self.assertBody(europoundUtf8)
-
+
# Encoded utf8 entities MUST be parsed and decoded correctly.
# Here, q is the POUND SIGN U+00A3 encoded in utf8
body = ntob("q=\xc2\xa3")
self.getPage('/reqparams', method='POST',
- headers=[("Content-Type", "application/x-www-form-urlencoded"),
- ("Content-Length", str(len(body))),
- ],
+ headers=[(
+ "Content-Type", "application/x-www-form-urlencoded"),
+ ("Content-Length", str(len(body))),
+ ],
body=body),
self.assertBody(ntob("q: \xc2\xa3"))
-
+
# ...and in utf16, which is not in the default attempt_charsets list:
body = ntob("\xff\xfeq\x00=\xff\xfe\xa3\x00")
- self.getPage('/reqparams', method='POST',
- headers=[("Content-Type", "application/x-www-form-urlencoded;charset=utf-16"),
- ("Content-Length", str(len(body))),
- ],
+ self.getPage('/reqparams',
+ method='POST',
+ headers=[
+ ("Content-Type",
+ "application/x-www-form-urlencoded;charset=utf-16"),
+ ("Content-Length", str(len(body))),
+ ],
body=body),
self.assertBody(ntob("q: \xc2\xa3"))
-
+
# Entities that are incorrectly encoded MUST raise 400.
# Here, q is the POUND SIGN U+00A3 encoded in utf16, but
# the Content-Type incorrectly labels it utf-8.
body = ntob("\xff\xfeq\x00=\xff\xfe\xa3\x00")
- self.getPage('/reqparams', method='POST',
- headers=[("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"),
- ("Content-Length", str(len(body))),
- ],
+ self.getPage('/reqparams',
+ method='POST',
+ headers=[
+ ("Content-Type",
+ "application/x-www-form-urlencoded;charset=utf-8"),
+ ("Content-Length", str(len(body))),
+ ],
body=body),
self.assertStatus(400)
- self.assertErrorPage(400,
+ self.assertErrorPage(
+ 400,
"The request entity could not be decoded. The following charsets "
"were attempted: ['utf-8']")
-
+
def test_decode_tool(self):
# An extra charset should be tried first, and succeed if it matches.
# Here, we add utf-16 as a charset and pass a utf-16 body.
body = ntob("\xff\xfeq\x00=\xff\xfe\xa3\x00")
self.getPage('/decode/extra_charset', method='POST',
- headers=[("Content-Type", "application/x-www-form-urlencoded"),
- ("Content-Length", str(len(body))),
- ],
+ headers=[(
+ "Content-Type", "application/x-www-form-urlencoded"),
+ ("Content-Length", str(len(body))),
+ ],
body=body),
self.assertBody(ntob("q: \xc2\xa3"))
-
+
# An extra charset should be tried first, and continue to other default
# charsets if it doesn't match.
# Here, we add utf-16 as a charset but still pass a utf-8 body.
body = ntob("q=\xc2\xa3")
self.getPage('/decode/extra_charset', method='POST',
- headers=[("Content-Type", "application/x-www-form-urlencoded"),
- ("Content-Length", str(len(body))),
- ],
+ headers=[(
+ "Content-Type", "application/x-www-form-urlencoded"),
+ ("Content-Length", str(len(body))),
+ ],
body=body),
self.assertBody(ntob("q: \xc2\xa3"))
-
+
# An extra charset should error if force is True and it doesn't match.
# Here, we force utf-16 as a charset but still pass a utf-8 body.
body = ntob("q=\xc2\xa3")
self.getPage('/decode/force_charset', method='POST',
- headers=[("Content-Type", "application/x-www-form-urlencoded"),
- ("Content-Length", str(len(body))),
- ],
+ headers=[(
+ "Content-Type", "application/x-www-form-urlencoded"),
+ ("Content-Length", str(len(body))),
+ ],
body=body),
- self.assertErrorPage(400,
+ self.assertErrorPage(
+ 400,
"The request entity could not be decoded. The following charsets "
"were attempted: ['utf-16']")
-
+
def test_multipart_decoding(self):
# Test the decoding of a multipart entity when the charset (utf16) is
# explicitly given.
- body=ntob('\r\n'.join(['--X',
- 'Content-Type: text/plain;charset=utf-16',
- 'Content-Disposition: form-data; name="text"',
- '',
- '\xff\xfea\x00b\x00\x1c c\x00',
- '--X',
- 'Content-Type: text/plain;charset=utf-16',
- 'Content-Disposition: form-data; name="submit"',
- '',
- '\xff\xfeC\x00r\x00e\x00a\x00t\x00e\x00',
- '--X--']))
+ body = ntob('\r\n'.join([
+ '--X',
+ 'Content-Type: text/plain;charset=utf-16',
+ 'Content-Disposition: form-data; name="text"',
+ '',
+ '\xff\xfea\x00b\x00\x1c c\x00',
+ '--X',
+ 'Content-Type: text/plain;charset=utf-16',
+ 'Content-Disposition: form-data; name="submit"',
+ '',
+ '\xff\xfeC\x00r\x00e\x00a\x00t\x00e\x00',
+ '--X--'
+ ]))
self.getPage('/reqparams', method='POST',
- headers=[("Content-Type", "multipart/form-data;boundary=X"),
- ("Content-Length", str(len(body))),
- ],
+ headers=[(
+ "Content-Type", "multipart/form-data;boundary=X"),
+ ("Content-Length", str(len(body))),
+ ],
body=body),
- self.assertBody(ntob("text: ab\xe2\x80\x9cc, submit: Create"))
-
+ self.assertBody(ntob("submit: Create, text: ab\xe2\x80\x9cc"))
+
def test_multipart_decoding_no_charset(self):
# Test the decoding of a multipart entity when the charset (utf8) is
# NOT explicitly given, but is in the list of charsets to attempt.
- body=ntob('\r\n'.join(['--X',
- 'Content-Disposition: form-data; name="text"',
- '',
- '\xe2\x80\x9c',
- '--X',
- 'Content-Disposition: form-data; name="submit"',
- '',
- 'Create',
- '--X--']))
+ body = ntob('\r\n'.join([
+ '--X',
+ 'Content-Disposition: form-data; name="text"',
+ '',
+ '\xe2\x80\x9c',
+ '--X',
+ 'Content-Disposition: form-data; name="submit"',
+ '',
+ 'Create',
+ '--X--'
+ ]))
self.getPage('/reqparams', method='POST',
- headers=[("Content-Type", "multipart/form-data;boundary=X"),
- ("Content-Length", str(len(body))),
- ],
+ headers=[(
+ "Content-Type", "multipart/form-data;boundary=X"),
+ ("Content-Length", str(len(body))),
+ ],
body=body),
- self.assertBody(ntob("text: \xe2\x80\x9c, submit: Create"))
-
+ self.assertBody(ntob("submit: Create, text: \xe2\x80\x9c"))
+
def test_multipart_decoding_no_successful_charset(self):
# Test the decoding of a multipart entity when the charset (utf16) is
# NOT explicitly given, and is NOT in the list of charsets to attempt.
- body=ntob('\r\n'.join(['--X',
- 'Content-Disposition: form-data; name="text"',
- '',
- '\xff\xfea\x00b\x00\x1c c\x00',
- '--X',
- 'Content-Disposition: form-data; name="submit"',
- '',
- '\xff\xfeC\x00r\x00e\x00a\x00t\x00e\x00',
- '--X--']))
+ body = ntob('\r\n'.join([
+ '--X',
+ 'Content-Disposition: form-data; name="text"',
+ '',
+ '\xff\xfea\x00b\x00\x1c c\x00',
+ '--X',
+ 'Content-Disposition: form-data; name="submit"',
+ '',
+ '\xff\xfeC\x00r\x00e\x00a\x00t\x00e\x00',
+ '--X--'
+ ]))
self.getPage('/reqparams', method='POST',
- headers=[("Content-Type", "multipart/form-data;boundary=X"),
- ("Content-Length", str(len(body))),
- ],
+ headers=[(
+ "Content-Type", "multipart/form-data;boundary=X"),
+ ("Content-Length", str(len(body))),
+ ],
body=body),
self.assertStatus(400)
- self.assertErrorPage(400,
+ self.assertErrorPage(
+ 400,
"The request entity could not be decoded. The following charsets "
"were attempted: ['us-ascii', 'utf-8']")
-
+
def test_nontext(self):
self.getPage('/nontext')
self.assertHeader('Content-Type', 'application/binary;charset=utf-8')
self.assertBody('\x00\x01\x02\x03')
-
+
def testEncoding(self):
# Default encoding should be utf-8
self.getPage('/mao_zedong')
self.assertBody(sing8)
-
+
# Ask for utf-16.
self.getPage('/mao_zedong', [('Accept-Charset', 'utf-16')])
self.assertHeader('Content-Type', 'text/html;charset=utf-16')
self.assertBody(sing16)
-
+
# Ask for multiple encodings. ISO-8859-1 should fail, and utf-16
# should be produced.
self.getPage('/mao_zedong', [('Accept-Charset',
'iso-8859-1;q=1, utf-16;q=0.5')])
self.assertBody(sing16)
-
+
# The "*" value should default to our default_encoding, utf-8
self.getPage('/mao_zedong', [('Accept-Charset', '*;q=1, utf-7;q=.2')])
self.assertBody(sing8)
-
+
# Only allow iso-8859-1, which should fail and raise 406.
self.getPage('/mao_zedong', [('Accept-Charset', 'iso-8859-1, *;q=0')])
self.assertStatus("406 Not Acceptable")
self.assertInBody("Your client sent this Accept-Charset header: "
"iso-8859-1, *;q=0. We tried these charsets: "
"iso-8859-1.")
-
+
# Ask for x-mac-ce, which should be unknown. See ticket #569.
self.getPage('/mao_zedong', [('Accept-Charset',
'us-ascii, ISO-8859-1, x-mac-ce')])
@@ -300,52 +335,52 @@ class EncodingTests(helper.CPWebCase):
self.assertInBody("Your client sent this Accept-Charset header: "
"us-ascii, ISO-8859-1, x-mac-ce. We tried these "
"charsets: ISO-8859-1, us-ascii, x-mac-ce.")
-
+
# Test the 'encoding' arg to encode.
self.getPage('/utf8')
self.assertBody(sing8)
self.getPage('/utf8', [('Accept-Charset', 'us-ascii, ISO-8859-1')])
self.assertStatus("406 Not Acceptable")
-
+
def testGzip(self):
zbuf = BytesIO()
zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=9)
zfile.write(ntob("Hello, world"))
zfile.close()
-
+
self.getPage('/gzip/', headers=[("Accept-Encoding", "gzip")])
self.assertInBody(zbuf.getvalue()[:3])
self.assertHeader("Vary", "Accept-Encoding")
self.assertHeader("Content-Encoding", "gzip")
-
+
# Test when gzip is denied.
self.getPage('/gzip/', headers=[("Accept-Encoding", "identity")])
self.assertHeader("Vary", "Accept-Encoding")
self.assertNoHeader("Content-Encoding")
self.assertBody("Hello, world")
-
+
self.getPage('/gzip/', headers=[("Accept-Encoding", "gzip;q=0")])
self.assertHeader("Vary", "Accept-Encoding")
self.assertNoHeader("Content-Encoding")
self.assertBody("Hello, world")
-
+
self.getPage('/gzip/', headers=[("Accept-Encoding", "*;q=0")])
self.assertStatus(406)
self.assertNoHeader("Content-Encoding")
self.assertErrorPage(406, "identity, gzip")
-
+
# Test for ticket #147
self.getPage('/gzip/noshow', headers=[("Accept-Encoding", "gzip")])
self.assertNoHeader('Content-Encoding')
self.assertStatus(500)
self.assertErrorPage(500, pattern="IndexError\n")
-
+
# In this case, there's nothing we can do to deliver a
# readable page, since 1) the gzip header is already set,
# and 2) we may have already written some of the body.
# The fix is to never stream yields when using gzip.
if (cherrypy.server.protocol_version == "HTTP/1.0" or
- getattr(cherrypy.server, "using_apache", False)):
+ getattr(cherrypy.server, "using_apache", False)):
self.getPage('/gzip/noshow_stream',
headers=[("Accept-Encoding", "gzip")])
self.assertHeader('Content-Encoding', 'gzip')
@@ -360,4 +395,3 @@ class EncodingTests(helper.CPWebCase):
def test_UnicodeHeaders(self):
self.getPage('/cookies_and_headers')
self.assertBody('Any content')
-
diff --git a/cherrypy/test/test_etags.py b/cherrypy/test/test_etags.py
index aec16936..4a263f36 100644
--- a/cherrypy/test/test_etags.py
+++ b/cherrypy/test/test_etags.py
@@ -7,10 +7,11 @@ class ETagTest(helper.CPWebCase):
def setup_server():
class Root:
+
def resource(self):
return "Oh wah ta goo Siam."
resource.exposed = True
-
+
def fail(self, code):
code = int(code)
if 300 <= code <= 399:
@@ -18,26 +19,26 @@ class ETagTest(helper.CPWebCase):
else:
raise cherrypy.HTTPError(code)
fail.exposed = True
-
+
def unicoded(self):
return ntou('I am a \u1ee4nicode string.', 'escape')
unicoded.exposed = True
# In Python 3, tools.encode is on by default
unicoded._cp_config = {'tools.encode.on': True}
-
+
conf = {'/': {'tools.etags.on': True,
'tools.etags.autotags': True,
}}
cherrypy.tree.mount(Root(), config=conf)
setup_server = staticmethod(setup_server)
-
+
def test_etags(self):
self.getPage("/resource")
self.assertStatus('200 OK')
self.assertHeader('Content-Type', 'text/html;charset=utf-8')
self.assertBody('Oh wah ta goo Siam.')
etag = self.assertHeader('ETag')
-
+
# Test If-Match (both valid and invalid)
self.getPage("/resource", headers=[('If-Match', etag)])
self.assertStatus("200 OK")
@@ -47,22 +48,23 @@ class ETagTest(helper.CPWebCase):
self.assertStatus("200 OK")
self.getPage("/resource", headers=[('If-Match', "a bogus tag")])
self.assertStatus("412 Precondition Failed")
-
+
# Test If-None-Match (both valid and invalid)
self.getPage("/resource", headers=[('If-None-Match', etag)])
self.assertStatus(304)
- self.getPage("/resource", method='POST', headers=[('If-None-Match', etag)])
+ self.getPage("/resource", method='POST',
+ headers=[('If-None-Match', etag)])
self.assertStatus("412 Precondition Failed")
self.getPage("/resource", headers=[('If-None-Match', "*")])
self.assertStatus(304)
self.getPage("/resource", headers=[('If-None-Match', "a bogus tag")])
self.assertStatus("200 OK")
-
+
def test_errors(self):
self.getPage("/resource")
self.assertStatus(200)
etag = self.assertHeader('ETag')
-
+
# Test raising errors in page handler
self.getPage("/fail/412", headers=[('If-Match', etag)])
self.assertStatus(412)
@@ -72,7 +74,7 @@ class ETagTest(helper.CPWebCase):
self.assertStatus(412)
self.getPage("/fail/304", headers=[('If-None-Match', "*")])
self.assertStatus(304)
-
+
def test_unicode_body(self):
self.getPage("/unicoded")
self.assertStatus(200)
@@ -80,4 +82,3 @@ class ETagTest(helper.CPWebCase):
self.getPage("/unicoded", headers=[('If-Match', etag1)])
self.assertStatus(200)
self.assertHeader('ETag', etag1)
-
diff --git a/cherrypy/test/test_http.py b/cherrypy/test/test_http.py
index 639c6c4b..10df13b4 100644
--- a/cherrypy/test/test_http.py
+++ b/cherrypy/test/test_http.py
@@ -11,7 +11,7 @@ from cherrypy._cpcompat import HTTPConnection, HTTPSConnection, ntob, py3k
def encode_multipart_formdata(files):
"""Return (content_type, body) ready for httplib.HTTP instance.
-
+
files: a sequence of (name, filename, value) tuples for multipart uploads.
"""
BOUNDARY = '________ThIs_Is_tHe_bouNdaRY_$'
@@ -31,25 +31,27 @@ def encode_multipart_formdata(files):
return content_type, body
-
-
from cherrypy.test import helper
+
class HTTPTests(helper.CPWebCase):
def setup_server():
class Root:
+
def index(self, *args, **kwargs):
return "Hello world!"
index.exposed = True
-
+
def no_body(self, *args, **kwargs):
return "Hello world!"
no_body.exposed = True
no_body._cp_config = {'request.process_request_body': False}
-
+
def post_multipart(self, file):
- """Return a summary ("a * 65536\nb * 65536") of the uploaded file."""
+ """Return a summary ("a * 65536\nb * 65536") of the uploaded
+ file.
+ """
contents = file.file.read()
summary = []
curchar = None
@@ -59,25 +61,27 @@ class HTTPTests(helper.CPWebCase):
count += 1
else:
if count:
- if py3k: curchar = chr(curchar)
+ if py3k:
+ curchar = chr(curchar)
summary.append("%s * %d" % (curchar, count))
count = 1
curchar = c
if count:
- if py3k: curchar = chr(curchar)
+ if py3k:
+ curchar = chr(curchar)
summary.append("%s * %d" % (curchar, count))
return ", ".join(summary)
post_multipart.exposed = True
-
+
cherrypy.tree.mount(Root())
cherrypy.config.update({'server.max_request_body_size': 30000000})
setup_server = staticmethod(setup_server)
-
+
def test_no_content_length(self):
# "The presence of a message-body in a request is signaled by the
# inclusion of a Content-Length or Transfer-Encoding header field in
# the request's message-headers."
- #
+ #
# Send a message with neither header and no body. Even though
# the request is of method POST, this should be OK because we set
# request.process_request_body to False for our handler.
@@ -91,7 +95,7 @@ class HTTPTests(helper.CPWebCase):
self.status = str(response.status)
self.assertStatus(200)
self.assertBody(ntob('Hello world!'))
-
+
# Now send a message that has no Content-Length, but does send a body.
# Verify that CP times out the socket and responds
# with 411 Length Required.
@@ -104,17 +108,17 @@ class HTTPTests(helper.CPWebCase):
self.body = response.fp.read()
self.status = str(response.status)
self.assertStatus(411)
-
+
def test_post_multipart(self):
alphabet = "abcdefghijklmnopqrstuvwxyz"
# generate file contents for a large post
contents = "".join([c * 65536 for c in alphabet])
-
+
# encode as multipart form data
- files=[('file', 'file.txt', contents)]
+ files = [('file', 'file.txt', contents)]
content_type, body = encode_multipart_formdata(files)
body = body.encode('Latin-1')
-
+
# post file
if self.scheme == 'https':
c = HTTPSConnection('%s:%s' % (self.interface(), self.PORT))
@@ -125,7 +129,7 @@ class HTTPTests(helper.CPWebCase):
c.putheader('Content-Length', str(len(body)))
c.endheaders()
c.send(body)
-
+
response = c.getresponse()
self.body = response.fp.read()
self.status = str(response.status)
@@ -135,7 +139,7 @@ class HTTPTests(helper.CPWebCase):
def test_malformed_request_line(self):
if getattr(cherrypy.server, "using_apache", False):
return self.skip("skipped due to known Apache differences...")
-
+
# Test missing version in Request-Line
if self.scheme == 'https':
c = HTTPSConnection('%s:%s' % (self.interface(), self.PORT))
@@ -154,6 +158,12 @@ class HTTPTests(helper.CPWebCase):
self.assertEqual(response.fp.read(22), ntob("Malformed Request-Line"))
c.close()
+ def test_request_line_split_issue_1220(self):
+ Request_URI = "/index?intervenant-entreprise-evenement_classaction=evenement-mailremerciements&_path=intervenant-entreprise-evenement&intervenant-entreprise-evenement_action-id=19404&intervenant-entreprise-evenement_id=19404&intervenant-entreprise_id=28092"
+ self.assertEqual(len("GET %s HTTP/1.1\r\n" % Request_URI), 256)
+ self.getPage(Request_URI)
+ self.assertBody("Hello world!")
+
def test_malformed_header(self):
if self.scheme == 'https':
c = HTTPSConnection('%s:%s' % (self.interface(), self.PORT))
@@ -161,20 +171,20 @@ class HTTPTests(helper.CPWebCase):
c = HTTPConnection('%s:%s' % (self.interface(), self.PORT))
c.putrequest('GET', '/')
c.putheader('Content-Type', 'text/plain')
- # See http://www.cherrypy.org/ticket/941
+ # See https://bitbucket.org/cherrypy/cherrypy/issue/941
c._output(ntob('Re, 1.2.3.4#015#012'))
c.endheaders()
-
+
response = c.getresponse()
self.status = str(response.status)
self.assertStatus(400)
self.body = response.fp.read(20)
self.assertBody("Illegal header line.")
-
+
def test_http_over_https(self):
if self.scheme != 'https':
return self.skip("skipped (not running HTTPS)... ")
-
+
# Try connecting without SSL.
conn = HTTPConnection('%s:%s' % (self.interface(), self.PORT))
conn.putrequest("GET", "/", skip_host=True)
@@ -202,11 +212,11 @@ class HTTPTests(helper.CPWebCase):
try:
response.begin()
self.assertEqual(response.status, 400)
- self.assertEqual(response.fp.read(22), ntob("Malformed Request-Line"))
+ self.assertEqual(response.fp.read(22),
+ ntob("Malformed Request-Line"))
c.close()
except socket.error:
e = sys.exc_info()[1]
# "Connection reset by peer" is also acceptable.
if e.errno != errno.ECONNRESET:
raise
-
diff --git a/cherrypy/test/test_httpauth.py b/cherrypy/test/test_httpauth.py
index 9d0eecb2..98a300f0 100644
--- a/cherrypy/test/test_httpauth.py
+++ b/cherrypy/test/test_httpauth.py
@@ -4,27 +4,35 @@ from cherrypy.lib import httpauth
from cherrypy.test import helper
+
class HTTPAuthTest(helper.CPWebCase):
def setup_server():
class Root:
+
def index(self):
return "This is public."
index.exposed = True
class DigestProtected:
+
def index(self):
- return "Hello %s, you've been authorized." % cherrypy.request.login
+ return "Hello %s, you've been authorized." % (
+ cherrypy.request.login)
index.exposed = True
class BasicProtected:
+
def index(self):
- return "Hello %s, you've been authorized." % cherrypy.request.login
+ return "Hello %s, you've been authorized." % (
+ cherrypy.request.login)
index.exposed = True
class BasicProtected2:
+
def index(self):
- return "Hello %s, you've been authorized." % cherrypy.request.login
+ return "Hello %s, you've been authorized." % (
+ cherrypy.request.login)
index.exposed = True
def fetch_users():
@@ -32,21 +40,31 @@ class HTTPAuthTest(helper.CPWebCase):
def sha_password_encrypter(password):
return sha(ntob(password)).hexdigest()
-
+
def fetch_password(username):
return sha(ntob('test')).hexdigest()
- conf = {'/digest': {'tools.digest_auth.on': True,
- 'tools.digest_auth.realm': 'localhost',
- 'tools.digest_auth.users': fetch_users},
- '/basic': {'tools.basic_auth.on': True,
- 'tools.basic_auth.realm': 'localhost',
- 'tools.basic_auth.users': {'test': md5(ntob('test')).hexdigest()}},
- '/basic2': {'tools.basic_auth.on': True,
- 'tools.basic_auth.realm': 'localhost',
- 'tools.basic_auth.users': fetch_password,
- 'tools.basic_auth.encrypt': sha_password_encrypter}}
-
+ conf = {
+ '/digest': {
+ 'tools.digest_auth.on': True,
+ 'tools.digest_auth.realm': 'localhost',
+ 'tools.digest_auth.users': fetch_users
+ },
+ '/basic': {
+ 'tools.basic_auth.on': True,
+ 'tools.basic_auth.realm': 'localhost',
+ 'tools.basic_auth.users': {
+ 'test': md5(ntob('test')).hexdigest()
+ }
+ },
+ '/basic2': {
+ 'tools.basic_auth.on': True,
+ 'tools.basic_auth.realm': 'localhost',
+ 'tools.basic_auth.users': fetch_password,
+ 'tools.basic_auth.encrypt': sha_password_encrypter
+ }
+ }
+
root = Root()
root.digest = DigestProtected()
root.basic = BasicProtected()
@@ -54,7 +72,6 @@ class HTTPAuthTest(helper.CPWebCase):
cherrypy.tree.mount(root, config=conf)
setup_server = staticmethod(setup_server)
-
def testPublic(self):
self.getPage("/")
self.assertStatus('200 OK')
@@ -68,7 +85,7 @@ class HTTPAuthTest(helper.CPWebCase):
self.getPage('/basic/', [('Authorization', 'Basic dGVzdDp0ZX60')])
self.assertStatus(401)
-
+
self.getPage('/basic/', [('Authorization', 'Basic dGVzdDp0ZXN0')])
self.assertStatus('200 OK')
self.assertBody("Hello test, you've been authorized.")
@@ -80,7 +97,7 @@ class HTTPAuthTest(helper.CPWebCase):
self.getPage('/basic2/', [('Authorization', 'Basic dGVzdDp0ZX60')])
self.assertStatus(401)
-
+
self.getPage('/basic2/', [('Authorization', 'Basic dGVzdDp0ZXN0')])
self.assertStatus('200 OK')
self.assertBody("Hello test, you've been authorized.")
@@ -88,7 +105,7 @@ class HTTPAuthTest(helper.CPWebCase):
def testDigest(self):
self.getPage("/digest/")
self.assertStatus(401)
-
+
value = None
for k, v in self.headers:
if k.lower() == "www-authenticate":
@@ -97,7 +114,8 @@ class HTTPAuthTest(helper.CPWebCase):
break
if value is None:
- self._handlewebError("Digest authentification scheme was not found")
+ self._handlewebError(
+ "Digest authentification scheme was not found")
value = value[7:]
items = value.split(', ')
@@ -105,14 +123,15 @@ class HTTPAuthTest(helper.CPWebCase):
for item in items:
key, value = item.split('=')
tokens[key.lower()] = value
-
+
missing_msg = "%s is missing"
bad_value_msg = "'%s' was expecting '%s' but found '%s'"
nonce = None
if 'realm' not in tokens:
self._handlewebError(missing_msg % 'realm')
elif tokens['realm'] != '"localhost"':
- self._handlewebError(bad_value_msg % ('realm', '"localhost"', tokens['realm']))
+ self._handlewebError(bad_value_msg %
+ ('realm', '"localhost"', tokens['realm']))
if 'nonce' not in tokens:
self._handlewebError(missing_msg % 'nonce')
else:
@@ -120,32 +139,55 @@ class HTTPAuthTest(helper.CPWebCase):
if 'algorithm' not in tokens:
self._handlewebError(missing_msg % 'algorithm')
elif tokens['algorithm'] != '"MD5"':
- self._handlewebError(bad_value_msg % ('algorithm', '"MD5"', tokens['algorithm']))
+ self._handlewebError(bad_value_msg %
+ ('algorithm', '"MD5"', tokens['algorithm']))
if 'qop' not in tokens:
self._handlewebError(missing_msg % 'qop')
elif tokens['qop'] != '"auth"':
- self._handlewebError(bad_value_msg % ('qop', '"auth"', tokens['qop']))
+ self._handlewebError(bad_value_msg %
+ ('qop', '"auth"', tokens['qop']))
# Test a wrong 'realm' value
- base_auth = 'Digest username="test", realm="wrong realm", nonce="%s", uri="/digest/", algorithm=MD5, response="%s", qop=auth, nc=%s, cnonce="1522e61005789929"'
+ base_auth = (
+ 'Digest '
+ 'username="test", '
+ 'realm="wrong realm", '
+ 'nonce="%s", '
+ 'uri="/digest/", '
+ 'algorithm=MD5, '
+ 'response="%s", '
+ 'qop=auth, '
+ 'nc=%s, '
+ 'cnonce="1522e61005789929"'
+ )
auth = base_auth % (nonce, '', '00000001')
params = httpauth.parseAuthorization(auth)
response = httpauth._computeDigestResponse(params, 'test')
-
+
auth = base_auth % (nonce, response, '00000001')
self.getPage('/digest/', [('Authorization', auth)])
self.assertStatus(401)
# Test that must pass
- base_auth = 'Digest username="test", realm="localhost", nonce="%s", uri="/digest/", algorithm=MD5, response="%s", qop=auth, nc=%s, cnonce="1522e61005789929"'
+ base_auth = (
+ 'Digest '
+ 'username="test", '
+ 'realm="localhost", '
+ 'nonce="%s", '
+ 'uri="/digest/", '
+ 'algorithm=MD5, '
+ 'response="%s", '
+ 'qop=auth, '
+ 'nc=%s, '
+ 'cnonce="1522e61005789929"'
+ )
auth = base_auth % (nonce, '', '00000001')
params = httpauth.parseAuthorization(auth)
response = httpauth._computeDigestResponse(params, 'test')
-
+
auth = base_auth % (nonce, response, '00000001')
self.getPage('/digest/', [('Authorization', auth)])
self.assertStatus('200 OK')
self.assertBody("Hello test, you've been authorized.")
-
diff --git a/cherrypy/test/test_httplib.py b/cherrypy/test/test_httplib.py
index 5dc40fd2..76ca7641 100644
--- a/cherrypy/test/test_httplib.py
+++ b/cherrypy/test/test_httplib.py
@@ -5,7 +5,7 @@ from cherrypy.lib import httputil
class UtilityTests(unittest.TestCase):
-
+
def test_urljoin(self):
# Test all slash+atom combinations for SCRIPT_NAME and PATH_INFO
self.assertEqual(httputil.urljoin("/sn/", "/pi/"), "/sn/pi/")
diff --git a/cherrypy/test/test_json.py b/cherrypy/test/test_json.py
index a02c0767..8776d998 100644
--- a/cherrypy/test/test_json.py
+++ b/cherrypy/test/test_json.py
@@ -3,9 +3,12 @@ from cherrypy.test import helper
from cherrypy._cpcompat import json
+
class JsonTest(helper.CPWebCase):
+
def setup_server():
class Root(object):
+
def plain(self):
return 'hello'
plain.exposed = True
@@ -33,15 +36,23 @@ class JsonTest(helper.CPWebCase):
json_post.exposed = True
json_post._cp_config = {'tools.json_in.on': True}
+ def json_cached(self):
+ return 'hello there'
+ json_cached.exposed = True
+ json_cached._cp_config = {
+ 'tools.json_out.on': True,
+ 'tools.caching.on': True,
+ }
+
root = Root()
cherrypy.tree.mount(root)
setup_server = staticmethod(setup_server)
-
+
def test_json_output(self):
if json is None:
self.skip("json not found ")
return
-
+
self.getPage("/plain")
self.assertBody("hello")
@@ -58,22 +69,32 @@ class JsonTest(helper.CPWebCase):
if json is None:
self.skip("json not found ")
return
-
+
body = '[13, "c"]'
headers = [('Content-Type', 'application/json'),
('Content-Length', str(len(body)))]
self.getPage("/json_post", method="POST", headers=headers, body=body)
self.assertBody('ok')
-
+
body = '[13, "c"]'
headers = [('Content-Type', 'text/plain'),
('Content-Length', str(len(body)))]
self.getPage("/json_post", method="POST", headers=headers, body=body)
self.assertStatus(415, 'Expected an application/json content type')
-
+
body = '[13, -]'
headers = [('Content-Type', 'application/json'),
('Content-Length', str(len(body)))]
self.getPage("/json_post", method="POST", headers=headers, body=body)
self.assertStatus(400, 'Invalid JSON document')
+ def test_cached(self):
+ if json is None:
+ self.skip("json not found ")
+ return
+
+ self.getPage("/json_cached")
+ self.assertStatus(200, '"hello"')
+
+ self.getPage("/json_cached") # 2'nd time to hit cache
+ self.assertStatus(200, '"hello"')
diff --git a/cherrypy/test/test_logging.py b/cherrypy/test/test_logging.py
index 7d506e87..9375494f 100644
--- a/cherrypy/test/test_logging.py
+++ b/cherrypy/test/test_logging.py
@@ -16,20 +16,20 @@ erebos = ntou('\u0388\u03c1\u03b5\u03b2\u03bf\u03c2.com', 'escape')
def setup_server():
class Root:
-
+
def index(self):
return "hello"
index.exposed = True
-
+
def uni_code(self):
cherrypy.request.login = tartaros
cherrypy.request.remote.name = erebos
uni_code.exposed = True
-
+
def slashes(self):
cherrypy.request.request_line = r'GET /slashed\path HTTP/1.1'
slashes.exposed = True
-
+
def whitespace(self):
# User-Agent = "User-Agent" ":" 1*( product | comment )
# comment = "(" *( ctext | quoted-pair | comment ) ")"
@@ -38,22 +38,21 @@ def setup_server():
# LWS = [CRLF] 1*( SP | HT )
cherrypy.request.headers['User-Agent'] = 'Browzuh (1.0\r\n\t\t.3)'
whitespace.exposed = True
-
+
def as_string(self):
return "content"
as_string.exposed = True
-
+
def as_yield(self):
yield "content"
as_yield.exposed = True
-
+
def error(self):
raise ValueError()
error.exposed = True
error._cp_config = {'tools.log_tracebacks.on': True}
-
- root = Root()
+ root = Root()
cherrypy.config.update({'log.error_file': error_log,
'log.access_file': access_log,
@@ -61,14 +60,14 @@ def setup_server():
cherrypy.tree.mount(root)
-
from cherrypy.test import helper, logtest
+
class AccessLogTests(helper.CPWebCase, logtest.LogCase):
setup_server = staticmethod(setup_server)
-
+
logfile = access_log
-
+
def testNormalReturn(self):
self.markLog()
self.getPage("/as_string",
@@ -76,11 +75,11 @@ class AccessLogTests(helper.CPWebCase, logtest.LogCase):
('User-Agent', 'Mozilla/5.0')])
self.assertBody('content')
self.assertStatus(200)
-
+
intro = '%s - - [' % self.interface()
-
+
self.assertLog(-1, intro)
-
+
if [k for k, v in self.headers if k.lower() == 'content-length']:
self.assertLog(-1, '] "GET %s/as_string HTTP/1.1" 200 7 '
'"http://www.cherrypy.org/" "Mozilla/5.0"'
@@ -89,15 +88,15 @@ class AccessLogTests(helper.CPWebCase, logtest.LogCase):
self.assertLog(-1, '] "GET %s/as_string HTTP/1.1" 200 - '
'"http://www.cherrypy.org/" "Mozilla/5.0"'
% self.prefix())
-
+
def testNormalYield(self):
self.markLog()
self.getPage("/as_yield")
self.assertBody('content')
self.assertStatus(200)
-
+
intro = '%s - - [' % self.interface()
-
+
self.assertLog(-1, intro)
if [k for k, v in self.headers if k.lower() == 'content-length']:
self.assertLog(-1, '] "GET %s/as_yield HTTP/1.1" 200 7 "" ""' %
@@ -105,7 +104,7 @@ class AccessLogTests(helper.CPWebCase, logtest.LogCase):
else:
self.assertLog(-1, '] "GET %s/as_yield HTTP/1.1" 200 - "" ""'
% self.prefix())
-
+
def testEscapedOutput(self):
# Test unicode in access log pieces.
self.markLog()
@@ -119,7 +118,7 @@ class AccessLogTests(helper.CPWebCase, logtest.LogCase):
# Test the erebos value. Included inline for your enlightenment.
# Note the 'r' prefix--those backslashes are literals.
self.assertLog(-1, r'\xce\x88\xcf\x81\xce\xb5\xce\xb2\xce\xbf\xcf\x82')
-
+
# Test backslashes in output.
self.markLog()
self.getPage("/slashes")
@@ -128,7 +127,7 @@ class AccessLogTests(helper.CPWebCase, logtest.LogCase):
self.assertLog(-1, ntob('"GET /slashed\\path HTTP/1.1"'))
else:
self.assertLog(-1, r'"GET /slashed\\path HTTP/1.1"')
-
+
# Test whitespace in output.
self.markLog()
self.getPage("/whitespace")
@@ -139,9 +138,9 @@ class AccessLogTests(helper.CPWebCase, logtest.LogCase):
class ErrorLogTests(helper.CPWebCase, logtest.LogCase):
setup_server = staticmethod(setup_server)
-
+
logfile = error_log
-
+
def testTracebacks(self):
# Test that tracebacks get written to the error log.
self.markLog()
@@ -154,4 +153,3 @@ class ErrorLogTests(helper.CPWebCase, logtest.LogCase):
self.assertLog(-3, 'raise ValueError()')
finally:
ignore.pop()
-
diff --git a/cherrypy/test/test_mime.py b/cherrypy/test/test_mime.py
index 16059913..f5f2b9fb 100644
--- a/cherrypy/test/test_mime.py
+++ b/cherrypy/test/test_mime.py
@@ -3,23 +3,24 @@
import cherrypy
from cherrypy._cpcompat import ntob, ntou, sorted
+
def setup_server():
-
+
class Root:
-
+
def multipart(self, parts):
return repr(parts)
multipart.exposed = True
-
+
def multipart_form_data(self, **kwargs):
return repr(list(sorted(kwargs.items())))
multipart_form_data.exposed = True
-
+
def flashupload(self, Filedata, Upload, Filename):
return ("Upload: %s, Filename: %s, Filedata: %r" %
(Upload, Filename, Filedata.file.read()))
flashupload.exposed = True
-
+
cherrypy.config.update({'server.max_request_body_size': 0})
cherrypy.tree.mount(Root())
@@ -28,12 +29,14 @@ def setup_server():
from cherrypy.test import helper
+
class MultipartTest(helper.CPWebCase):
setup_server = staticmethod(setup_server)
-
+
def test_multipart(self):
text_part = ntou("This is the text version")
- html_part = ntou("""<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
+ html_part = ntou(
+ """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
@@ -58,32 +61,37 @@ This is the <strong>HTML</strong> version
headers = [
('Content-Type', 'multipart/mixed; boundary=123456789'),
('Content-Length', str(len(body))),
- ]
+ ]
self.getPage('/multipart', headers, "POST", body)
self.assertBody(repr([text_part, html_part]))
-
+
def test_multipart_form_data(self):
- body='\r\n'.join(['--X',
- 'Content-Disposition: form-data; name="foo"',
- '',
- 'bar',
- '--X',
- # Test a param with more than one value.
- # See http://www.cherrypy.org/ticket/1028
- 'Content-Disposition: form-data; name="baz"',
- '',
- '111',
- '--X',
- 'Content-Disposition: form-data; name="baz"',
- '',
- '333',
- '--X--'])
+ body = '\r\n'.join([
+ '--X',
+ 'Content-Disposition: form-data; name="foo"',
+ '',
+ 'bar',
+ '--X',
+ # Test a param with more than one value.
+ # See
+ # https://bitbucket.org/cherrypy/cherrypy/issue/1028
+ 'Content-Disposition: form-data; name="baz"',
+ '',
+ '111',
+ '--X',
+ 'Content-Disposition: form-data; name="baz"',
+ '',
+ '333',
+ '--X--'
+ ])
self.getPage('/multipart_form_data', method='POST',
- headers=[("Content-Type", "multipart/form-data;boundary=X"),
- ("Content-Length", str(len(body))),
- ],
+ headers=[(
+ "Content-Type", "multipart/form-data;boundary=X"),
+ ("Content-Length", str(len(body))),
+ ],
body=body),
- self.assertBody(repr([('baz', [ntou('111'), ntou('333')]), ('foo', ntou('bar'))]))
+ self.assertBody(
+ repr([('baz', [ntou('111'), ntou('333')]), ('foo', ntou('bar'))]))
class SafeMultipartHandlingTest(helper.CPWebCase):
@@ -93,13 +101,13 @@ class SafeMultipartHandlingTest(helper.CPWebCase):
headers = [
('Accept', 'text/*'),
('Content-Type', 'multipart/form-data; '
- 'boundary=----------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6'),
+ 'boundary=----------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6'),
('User-Agent', 'Shockwave Flash'),
('Host', 'www.example.com:54583'),
('Content-Length', '499'),
('Connection', 'Keep-Alive'),
('Cache-Control', 'no-cache'),
- ]
+ ]
filedata = ntob('<?xml version="1.0" encoding="UTF-8"?>\r\n'
'<projectDescription>\r\n'
'</projectDescription>\r\n')
@@ -113,16 +121,15 @@ class SafeMultipartHandlingTest(helper.CPWebCase):
'name="Filedata"; filename=".project"\r\n'
'Content-Type: application/octet-stream\r\n'
'\r\n')
- + filedata +
+ + filedata +
ntob('\r\n'
- '------------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6\r\n'
- 'Content-Disposition: form-data; name="Upload"\r\n'
- '\r\n'
- 'Submit Query\r\n'
- # Flash apps omit the trailing \r\n on the last line:
- '------------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6--'
- ))
+ '------------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6\r\n'
+ 'Content-Disposition: form-data; name="Upload"\r\n'
+ '\r\n'
+ 'Submit Query\r\n'
+ # Flash apps omit the trailing \r\n on the last line:
+ '------------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6--'
+ ))
self.getPage('/flashupload', headers, "POST", body)
self.assertBody("Upload: Submit Query, Filename: .project, "
"Filedata: %r" % filedata)
-
diff --git a/cherrypy/test/test_misc_tools.py b/cherrypy/test/test_misc_tools.py
index 1dd1429b..df3e2e65 100644
--- a/cherrypy/test/test_misc_tools.py
+++ b/cherrypy/test/test_misc_tools.py
@@ -8,12 +8,13 @@ from cherrypy import tools
def setup_server():
class Root:
+
def index(self):
yield "Hello, world"
index.exposed = True
h = [("Content-Language", "en-GB"), ('Content-Type', 'text/plain')]
tools.response_headers(headers=h)(index)
-
+
def other(self):
return "salut"
other.exposed = True
@@ -22,16 +23,15 @@ def setup_server():
'tools.response_headers.headers': [("Content-Language", "fr"),
('Content-Type', 'text/plain')],
'tools.log_hooks.on': True,
- }
-
-
+ }
+
class Accept:
_cp_config = {'tools.accept.on': True}
-
+
def index(self):
return '<a href="feed">Atom feed</a>'
index.exposed = True
-
+
# In Python 2.4+, we could use a decorator instead:
# @tools.accept('application/atom+xml')
def feed(self):
@@ -41,7 +41,7 @@ def setup_server():
</feed>"""
feed.exposed = True
feed._cp_config = {'tools.accept.media': 'application/atom+xml'}
-
+
def select(self):
# We could also write this: mtype = cherrypy.lib.accept.accept(...)
mtype = tools.accept.callable(['text/html', 'text/plain'])
@@ -50,14 +50,16 @@ def setup_server():
else:
return "PAGE TITLE"
select.exposed = True
-
+
class Referer:
+
def accept(self):
return "Accepted!"
accept.exposed = True
reject = accept
-
+
class AutoVary:
+
def index(self):
# Read a header directly with 'get'
ae = cherrypy.request.headers.get('Accept-Encoding')
@@ -76,7 +78,7 @@ def setup_server():
mtype = tools.accept.callable(['text/html', 'text/plain'])
return "Hello, world!"
index.exposed = True
-
+
conf = {'/referer': {'tools.referer.on': True,
'tools.referer.pattern': r'http://[^/]*example\.com',
},
@@ -85,7 +87,7 @@ def setup_server():
},
'/autovary': {'tools.autovary.on': True},
}
-
+
root = Root()
root.referer = Referer()
root.accept = Accept()
@@ -96,6 +98,7 @@ def setup_server():
from cherrypy.test import helper
+
class ResponseHeadersTest(helper.CPWebCase):
setup_server = staticmethod(setup_server)
@@ -112,21 +115,21 @@ class ResponseHeadersTest(helper.CPWebCase):
class RefererTest(helper.CPWebCase):
setup_server = staticmethod(setup_server)
-
+
def testReferer(self):
self.getPage('/referer/accept')
self.assertErrorPage(403, 'Forbidden Referer header.')
-
+
self.getPage('/referer/accept',
headers=[('Referer', 'http://www.example.com/')])
self.assertStatus(200)
self.assertBody('Accepted!')
-
+
# Reject
self.getPage('/referer/reject')
self.assertStatus(200)
self.assertBody('Accepted!')
-
+
self.getPage('/referer/reject',
headers=[('Referer', 'http://www.example.com/')])
self.assertErrorPage(403, 'Forbidden Referer header.')
@@ -134,40 +137,41 @@ class RefererTest(helper.CPWebCase):
class AcceptTest(helper.CPWebCase):
setup_server = staticmethod(setup_server)
-
+
def test_Accept_Tool(self):
# Test with no header provided
self.getPage('/accept/feed')
self.assertStatus(200)
self.assertInBody('<title>Unknown Blog</title>')
-
+
# Specify exact media type
- self.getPage('/accept/feed', headers=[('Accept', 'application/atom+xml')])
+ self.getPage('/accept/feed',
+ headers=[('Accept', 'application/atom+xml')])
self.assertStatus(200)
self.assertInBody('<title>Unknown Blog</title>')
-
+
# Specify matching media range
self.getPage('/accept/feed', headers=[('Accept', 'application/*')])
self.assertStatus(200)
self.assertInBody('<title>Unknown Blog</title>')
-
+
# Specify all media ranges
self.getPage('/accept/feed', headers=[('Accept', '*/*')])
self.assertStatus(200)
self.assertInBody('<title>Unknown Blog</title>')
-
+
# Specify unacceptable media types
self.getPage('/accept/feed', headers=[('Accept', 'text/html')])
self.assertErrorPage(406,
"Your client sent this Accept header: text/html. "
"But this resource only emits these media types: "
"application/atom+xml.")
-
+
# Test resource where tool is 'on' but media is None (not set).
self.getPage('/accept/')
self.assertStatus(200)
self.assertBody('<a href="feed">Atom feed</a>')
-
+
def test_accept_selection(self):
# Try both our expected media types
self.getPage('/accept/select', [('Accept', 'text/html')])
@@ -176,10 +180,11 @@ class AcceptTest(helper.CPWebCase):
self.getPage('/accept/select', [('Accept', 'text/plain')])
self.assertStatus(200)
self.assertBody('PAGE TITLE')
- self.getPage('/accept/select', [('Accept', 'text/plain, text/*;q=0.5')])
+ self.getPage('/accept/select',
+ [('Accept', 'text/plain, text/*;q=0.5')])
self.assertStatus(200)
self.assertBody('PAGE TITLE')
-
+
# text/* and */* should prefer text/html since it comes first
# in our 'media' argument to tools.accept
self.getPage('/accept/select', [('Accept', 'text/*')])
@@ -188,13 +193,14 @@ class AcceptTest(helper.CPWebCase):
self.getPage('/accept/select', [('Accept', '*/*')])
self.assertStatus(200)
self.assertBody('<h2>Page Title</h2>')
-
+
# Try unacceptable media types
self.getPage('/accept/select', [('Accept', 'application/xml')])
- self.assertErrorPage(406,
- "Your client sent this Accept header: application/xml. "
- "But this resource only emits these media types: "
- "text/html, text/plain.")
+ self.assertErrorPage(
+ 406,
+ "Your client sent this Accept header: application/xml. "
+ "But this resource only emits these media types: "
+ "text/html, text/plain.")
class AutoVaryTest(helper.CPWebCase):
@@ -203,5 +209,7 @@ class AutoVaryTest(helper.CPWebCase):
def testAutoVary(self):
self.getPage('/autovary/')
self.assertHeader(
- "Vary", 'Accept, Accept-Charset, Accept-Encoding, Host, If-Modified-Since, Range')
-
+ "Vary",
+ 'Accept, Accept-Charset, Accept-Encoding, '
+ 'Host, If-Modified-Since, Range'
+ )
diff --git a/cherrypy/test/test_objectmapping.py b/cherrypy/test/test_objectmapping.py
index 8dcf2d33..28362b63 100644
--- a/cherrypy/test/test_objectmapping.py
+++ b/cherrypy/test/test_objectmapping.py
@@ -10,128 +10,133 @@ class ObjectMappingTest(helper.CPWebCase):
def setup_server():
class Root:
+
def index(self, name="world"):
return name
index.exposed = True
-
+
def foobar(self):
return "bar"
foobar.exposed = True
-
+
def default(self, *params, **kwargs):
return "default:" + repr(params)
default.exposed = True
-
+
def other(self):
return "other"
other.exposed = True
-
+
def extra(self, *p):
return repr(p)
extra.exposed = True
-
+
def redirect(self):
raise cherrypy.HTTPRedirect('dir1/', 302)
redirect.exposed = True
-
+
def notExposed(self):
return "not exposed"
-
+
def confvalue(self):
return cherrypy.request.config.get("user")
confvalue.exposed = True
-
+
def redirect_via_url(self, path):
raise cherrypy.HTTPRedirect(cherrypy.url(path))
redirect_via_url.exposed = True
-
+
def translate_html(self):
return "OK"
translate_html.exposed = True
-
+
def mapped_func(self, ID=None):
return "ID is %s" % ID
mapped_func.exposed = True
setattr(Root, "Von B\xfclow", mapped_func)
-
-
+
class Exposing:
+
def base(self):
return "expose works!"
cherrypy.expose(base)
cherrypy.expose(base, "1")
cherrypy.expose(base, "2")
-
+
class ExposingNewStyle(object):
+
def base(self):
return "expose works!"
cherrypy.expose(base)
cherrypy.expose(base, "1")
cherrypy.expose(base, "2")
-
-
+
class Dir1:
+
def index(self):
return "index for dir1"
index.exposed = True
-
+
def myMethod(self):
- return "myMethod from dir1, path_info is:" + repr(cherrypy.request.path_info)
+ return "myMethod from dir1, path_info is:" + repr(
+ cherrypy.request.path_info)
myMethod.exposed = True
myMethod._cp_config = {'tools.trailing_slash.extra': True}
-
+
def default(self, *params):
return "default for dir1, param is:" + repr(params)
default.exposed = True
-
class Dir2:
+
def index(self):
return "index for dir2, path is:" + cherrypy.request.path_info
index.exposed = True
-
+
def script_name(self):
return cherrypy.tree.script_name()
script_name.exposed = True
-
+
def cherrypy_url(self):
return cherrypy.url("/extra")
cherrypy_url.exposed = True
-
+
def posparam(self, *vpath):
return "/".join(vpath)
posparam.exposed = True
-
-
+
class Dir3:
+
def default(self):
return "default for dir3, not exposed"
-
+
class Dir4:
+
def index(self):
return "index for dir4, not exposed"
-
+
class DefNoIndex:
+
def default(self, *args):
raise cherrypy.HTTPRedirect("contact")
default.exposed = True
-
+
# MethodDispatcher code
class ByMethod:
exposed = True
-
+
def __init__(self, *things):
self.things = list(things)
-
+
def GET(self):
return repr(self.things)
-
+
def POST(self, thing):
self.things.append(thing)
-
+
class Collection:
default = ByMethod('a', 'bit')
-
+
Root.exposing = Exposing()
Root.exposingnew = ExposingNewStyle()
Root.dir1 = Dir1()
@@ -141,7 +146,7 @@ class ObjectMappingTest(helper.CPWebCase):
Root.defnoindex = DefNoIndex()
Root.bymethod = ByMethod('another')
Root.collection = Collection()
-
+
d = cherrypy.dispatch.MethodDispatcher()
for url in script_names:
conf = {'/': {'user': (url or "/").split("/")[-2]},
@@ -149,65 +154,68 @@ class ObjectMappingTest(helper.CPWebCase):
'/collection': {'request.dispatch': d},
}
cherrypy.tree.mount(Root(), url, conf)
-
-
+
class Isolated:
+
def index(self):
return "made it!"
index.exposed = True
-
+
cherrypy.tree.mount(Isolated(), "/isolated")
-
+
class AnotherApp:
-
+
exposed = True
-
+
def GET(self):
return "milk"
-
- cherrypy.tree.mount(AnotherApp(), "/app", {'/': {'request.dispatch': d}})
+
+ cherrypy.tree.mount(AnotherApp(), "/app",
+ {'/': {'request.dispatch': d}})
setup_server = staticmethod(setup_server)
-
def testObjectMapping(self):
for url in script_names:
prefix = self.script_name = url
-
+
self.getPage('/')
self.assertBody('world')
-
+
self.getPage("/dir1/myMethod")
- self.assertBody("myMethod from dir1, path_info is:'/dir1/myMethod'")
-
+ self.assertBody(
+ "myMethod from dir1, path_info is:'/dir1/myMethod'")
+
self.getPage("/this/method/does/not/exist")
- self.assertBody("default:('this', 'method', 'does', 'not', 'exist')")
-
+ self.assertBody(
+ "default:('this', 'method', 'does', 'not', 'exist')")
+
self.getPage("/extra/too/much")
self.assertBody("('too', 'much')")
-
+
self.getPage("/other")
self.assertBody('other')
-
+
self.getPage("/notExposed")
self.assertBody("default:('notExposed',)")
-
+
self.getPage("/dir1/dir2/")
self.assertBody('index for dir2, path is:/dir1/dir2/')
-
+
# Test omitted trailing slash (should be redirected by default).
self.getPage("/dir1/dir2")
self.assertStatus(301)
self.assertHeader('Location', '%s/dir1/dir2/' % self.base())
-
+
# Test extra trailing slash (should be redirected if configured).
self.getPage("/dir1/myMethod/")
self.assertStatus(301)
self.assertHeader('Location', '%s/dir1/myMethod' % self.base())
-
+
# Test that default method must be exposed in order to match.
self.getPage("/dir1/dir2/dir3/dir4/index")
- self.assertBody("default for dir1, param is:('dir2', 'dir3', 'dir4', 'index')")
-
+ self.assertBody(
+ "default for dir1, param is:('dir2', 'dir3', 'dir4', 'index')")
+
# Test *vpath when default() is defined but not index()
# This also tests HTTPRedirect with default.
self.getPage("/defnoindex")
@@ -215,49 +223,52 @@ class ObjectMappingTest(helper.CPWebCase):
self.assertHeader('Location', '%s/contact' % self.base())
self.getPage("/defnoindex/")
self.assertStatus((302, 303))
- self.assertHeader('Location', '%s/defnoindex/contact' % self.base())
+ self.assertHeader('Location', '%s/defnoindex/contact' %
+ self.base())
self.getPage("/defnoindex/page")
self.assertStatus((302, 303))
- self.assertHeader('Location', '%s/defnoindex/contact' % self.base())
-
+ self.assertHeader('Location', '%s/defnoindex/contact' %
+ self.base())
+
self.getPage("/redirect")
self.assertStatus('302 Found')
self.assertHeader('Location', '%s/dir1/' % self.base())
-
+
if not getattr(cherrypy.server, "using_apache", False):
- # Test that we can use URL's which aren't all valid Python identifiers
+ # Test that we can use URL's which aren't all valid Python
+ # identifiers
# This should also test the %XX-unquoting of URL's.
self.getPage("/Von%20B%fclow?ID=14")
self.assertBody("ID is 14")
-
+
# Test that %2F in the path doesn't get unquoted too early;
# that is, it should not be used to separate path components.
# See ticket #393.
self.getPage("/page%2Fname")
self.assertBody("default:('page/name',)")
-
+
self.getPage("/dir1/dir2/script_name")
self.assertBody(url)
self.getPage("/dir1/dir2/cherrypy_url")
self.assertBody("%s/extra" % self.base())
-
+
# Test that configs don't overwrite each other from diferent apps
self.getPage("/confvalue")
self.assertBody((url or "/").split("/")[-2])
-
+
self.script_name = ""
-
+
# Test absoluteURI's in the Request-Line
self.getPage('http://%s:%s/' % (self.interface(), self.PORT))
self.assertBody('world')
-
+
self.getPage('http://%s:%s/abs/?service=http://192.168.0.1/x/y/z' %
(self.interface(), self.PORT))
self.assertBody("default:('abs',)")
-
+
self.getPage('/rel/?service=http://192.168.120.121:8000/x/y/z')
self.assertBody("default:('rel',)")
-
+
# Test that the "isolated" app doesn't leak url's into the root app.
# If it did leak, Root.default() would answer with
# "default:('isolated', 'doesnt', 'exist')".
@@ -266,124 +277,126 @@ class ObjectMappingTest(helper.CPWebCase):
self.assertBody("made it!")
self.getPage("/isolated/doesnt/exist")
self.assertStatus("404 Not Found")
-
+
# Make sure /foobar maps to Root.foobar and not to the app
- # mounted at /foo. See http://www.cherrypy.org/ticket/573
+ # mounted at /foo. See
+ # https://bitbucket.org/cherrypy/cherrypy/issue/573
self.getPage("/foobar")
self.assertBody("bar")
-
+
def test_translate(self):
self.getPage("/translate_html")
self.assertStatus("200 OK")
self.assertBody("OK")
-
+
self.getPage("/translate.html")
self.assertStatus("200 OK")
self.assertBody("OK")
-
+
self.getPage("/translate-html")
self.assertStatus("200 OK")
self.assertBody("OK")
-
+
def test_redir_using_url(self):
for url in script_names:
prefix = self.script_name = url
-
+
# Test the absolute path to the parent (leading slash)
self.getPage('/redirect_via_url?path=./')
self.assertStatus(('302 Found', '303 See Other'))
self.assertHeader('Location', '%s/' % self.base())
-
+
# Test the relative path to the parent (no leading slash)
self.getPage('/redirect_via_url?path=./')
self.assertStatus(('302 Found', '303 See Other'))
self.assertHeader('Location', '%s/' % self.base())
-
+
# Test the absolute path to the parent (leading slash)
self.getPage('/redirect_via_url/?path=./')
self.assertStatus(('302 Found', '303 See Other'))
self.assertHeader('Location', '%s/' % self.base())
-
+
# Test the relative path to the parent (no leading slash)
self.getPage('/redirect_via_url/?path=./')
self.assertStatus(('302 Found', '303 See Other'))
self.assertHeader('Location', '%s/' % self.base())
-
+
def testPositionalParams(self):
self.getPage("/dir1/dir2/posparam/18/24/hut/hike")
self.assertBody("18/24/hut/hike")
-
+
# intermediate index methods should not receive posparams;
# only the "final" index method should do so.
self.getPage("/dir1/dir2/5/3/sir")
self.assertBody("default for dir1, param is:('dir2', '5', '3', 'sir')")
-
+
# test that extra positional args raises an 404 Not Found
- # See http://www.cherrypy.org/ticket/733.
+ # See https://bitbucket.org/cherrypy/cherrypy/issue/733.
self.getPage("/dir1/dir2/script_name/extra/stuff")
self.assertStatus(404)
-
+
def testExpose(self):
# Test the cherrypy.expose function/decorator
self.getPage("/exposing/base")
self.assertBody("expose works!")
-
+
self.getPage("/exposing/1")
self.assertBody("expose works!")
-
+
self.getPage("/exposing/2")
self.assertBody("expose works!")
-
+
self.getPage("/exposingnew/base")
self.assertBody("expose works!")
-
+
self.getPage("/exposingnew/1")
self.assertBody("expose works!")
-
+
self.getPage("/exposingnew/2")
self.assertBody("expose works!")
-
+
def testMethodDispatch(self):
self.getPage("/bymethod")
self.assertBody("['another']")
self.assertHeader('Allow', 'GET, HEAD, POST')
-
+
self.getPage("/bymethod", method="HEAD")
self.assertBody("")
self.assertHeader('Allow', 'GET, HEAD, POST')
-
+
self.getPage("/bymethod", method="POST", body="thing=one")
self.assertBody("")
self.assertHeader('Allow', 'GET, HEAD, POST')
-
+
self.getPage("/bymethod")
self.assertBody(repr(['another', ntou('one')]))
self.assertHeader('Allow', 'GET, HEAD, POST')
-
+
self.getPage("/bymethod", method="PUT")
self.assertErrorPage(405)
self.assertHeader('Allow', 'GET, HEAD, POST')
-
+
# Test default with posparams
self.getPage("/collection/silly", method="POST")
self.getPage("/collection", method="GET")
self.assertBody("['a', 'bit', 'silly']")
-
+
# Test custom dispatcher set on app root (see #737).
self.getPage("/app")
self.assertBody("milk")
def testTreeMounting(self):
class Root(object):
+
def hello(self):
return "Hello world!"
hello.exposed = True
-
- # When mounting an application instance,
+
+ # When mounting an application instance,
# we can't specify a different script name in the call to mount.
a = Application(Root(), '/somewhere')
self.assertRaises(ValueError, cherrypy.tree.mount, a, '/somewhereelse')
-
+
# When mounting an application instance...
a = Application(Root(), '/somewhere')
# ...we MUST allow in identical script name in the call to mount...
@@ -395,10 +408,9 @@ class ObjectMappingTest(helper.CPWebCase):
cherrypy.tree.mount(a)
self.getPage('/somewhere/hello')
self.assertStatus(200)
-
+
# In addition, we MUST be able to create an Application using
# script_name == None for access to the wsgi_environ.
a = Application(Root(), script_name=None)
# However, this does not apply to tree.mount
self.assertRaises(TypeError, cherrypy.tree.mount, a, None)
-
diff --git a/cherrypy/test/test_proxy.py b/cherrypy/test/test_proxy.py
index 2fbb619a..49356448 100644
--- a/cherrypy/test/test_proxy.py
+++ b/cherrypy/test/test_proxy.py
@@ -7,106 +7,113 @@ script_names = ["", "/path/to/myapp"]
class ProxyTest(helper.CPWebCase):
def setup_server():
-
+
# Set up site
cherrypy.config.update({
'tools.proxy.on': True,
'tools.proxy.base': 'www.mydomain.test',
- })
-
+ })
+
# Set up application
-
+
class Root:
-
+
def __init__(self, sn):
# Calculate a URL outside of any requests.
- self.thisnewpage = cherrypy.url("/this/new/page", script_name=sn)
-
+ self.thisnewpage = cherrypy.url(
+ "/this/new/page", script_name=sn)
+
def pageurl(self):
return self.thisnewpage
pageurl.exposed = True
-
+
def index(self):
raise cherrypy.HTTPRedirect('dummy')
index.exposed = True
-
+
def remoteip(self):
return cherrypy.request.remote.ip
remoteip.exposed = True
-
+
def xhost(self):
raise cherrypy.HTTPRedirect('blah')
xhost.exposed = True
xhost._cp_config = {'tools.proxy.local': 'X-Host',
'tools.trailing_slash.extra': True,
}
-
+
def base(self):
return cherrypy.request.base
base.exposed = True
-
+
def ssl(self):
return cherrypy.request.base
ssl.exposed = True
ssl._cp_config = {'tools.proxy.scheme': 'X-Forwarded-Ssl'}
-
+
def newurl(self):
return ("Browse to <a href='%s'>this page</a>."
% cherrypy.url("/this/new/page"))
newurl.exposed = True
-
+
for sn in script_names:
cherrypy.tree.mount(Root(sn), sn)
setup_server = staticmethod(setup_server)
-
+
def testProxy(self):
self.getPage("/")
self.assertHeader('Location',
"%s://www.mydomain.test%s/dummy" %
(self.scheme, self.prefix()))
-
+
# Test X-Forwarded-Host (Apache 1.3.33+ and Apache 2)
- self.getPage("/", headers=[('X-Forwarded-Host', 'http://www.example.test')])
+ self.getPage(
+ "/", headers=[('X-Forwarded-Host', 'http://www.example.test')])
self.assertHeader('Location', "http://www.example.test/dummy")
self.getPage("/", headers=[('X-Forwarded-Host', 'www.example.test')])
- self.assertHeader('Location', "%s://www.example.test/dummy" % self.scheme)
+ self.assertHeader('Location', "%s://www.example.test/dummy" %
+ self.scheme)
# Test multiple X-Forwarded-Host headers
self.getPage("/", headers=[
('X-Forwarded-Host', 'http://www.example.test, www.cherrypy.test'),
- ])
+ ])
self.assertHeader('Location', "http://www.example.test/dummy")
-
+
# Test X-Forwarded-For (Apache2)
self.getPage("/remoteip",
headers=[('X-Forwarded-For', '192.168.0.20')])
self.assertBody("192.168.0.20")
self.getPage("/remoteip",
- headers=[('X-Forwarded-For', '67.15.36.43, 192.168.0.20')])
+ headers=[
+ ('X-Forwarded-For', '67.15.36.43, 192.168.0.20')
+ ])
self.assertBody("192.168.0.20")
-
+
# Test X-Host (lighttpd; see https://trac.lighttpd.net/trac/ticket/418)
self.getPage("/xhost", headers=[('X-Host', 'www.example.test')])
- self.assertHeader('Location', "%s://www.example.test/blah" % self.scheme)
-
+ self.assertHeader('Location', "%s://www.example.test/blah" %
+ self.scheme)
+
# Test X-Forwarded-Proto (lighttpd)
self.getPage("/base", headers=[('X-Forwarded-Proto', 'https')])
self.assertBody("https://www.mydomain.test")
-
+
# Test X-Forwarded-Ssl (webfaction?)
self.getPage("/ssl", headers=[('X-Forwarded-Ssl', 'on')])
self.assertBody("https://www.mydomain.test")
-
+
# Test cherrypy.url()
for sn in script_names:
# Test the value inside requests
self.getPage(sn + "/newurl")
- self.assertBody("Browse to <a href='%s://www.mydomain.test" % self.scheme
- + sn + "/this/new/page'>this page</a>.")
+ self.assertBody(
+ "Browse to <a href='%s://www.mydomain.test" % self.scheme
+ + sn + "/this/new/page'>this page</a>.")
self.getPage(sn + "/newurl", headers=[('X-Forwarded-Host',
'http://www.example.test')])
self.assertBody("Browse to <a href='http://www.example.test"
+ sn + "/this/new/page'>this page</a>.")
-
+
# Test the value outside requests
port = ""
if self.scheme == "http" and self.PORT != 80:
@@ -121,9 +128,9 @@ class ProxyTest(helper.CPWebCase):
% (self.scheme, host, port, sn))
self.getPage(sn + "/pageurl")
self.assertBody(expected)
-
- # Test trailing slash (see http://www.cherrypy.org/ticket/562).
+
+ # Test trailing slash (see
+ # https://bitbucket.org/cherrypy/cherrypy/issue/562).
self.getPage("/xhost/", headers=[('X-Host', 'www.example.test')])
self.assertHeader('Location', "%s://www.example.test/xhost"
% self.scheme)
-
diff --git a/cherrypy/test/test_refleaks.py b/cherrypy/test/test_refleaks.py
index 279935e9..a87b8df7 100644
--- a/cherrypy/test/test_refleaks.py
+++ b/cherrypy/test/test_refleaks.py
@@ -15,19 +15,20 @@ from cherrypy.test import helper
class ReferenceTests(helper.CPWebCase):
def setup_server():
-
+
class Root:
+
def index(self, *args, **kwargs):
cherrypy.request.thing = data
return "Hello world!"
index.exposed = True
-
+
cherrypy.tree.mount(Root())
setup_server = staticmethod(setup_server)
-
+
def test_threadlocal_garbage(self):
success = []
-
+
def getpage():
host = '%s:%s' % (self.interface(), self.PORT)
if self.scheme == 'https':
@@ -44,16 +45,15 @@ class ReferenceTests(helper.CPWebCase):
finally:
c.close()
success.append(True)
-
+
ITERATIONS = 25
ts = []
for _ in range(ITERATIONS):
t = threading.Thread(target=getpage)
ts.append(t)
t.start()
-
+
for t in ts:
t.join()
-
- self.assertEqual(len(success), ITERATIONS)
+ self.assertEqual(len(success), ITERATIONS)
diff --git a/cherrypy/test/test_request_obj.py b/cherrypy/test/test_request_obj.py
index 26eea561..d9989e97 100644
--- a/cherrypy/test/test_request_obj.py
+++ b/cherrypy/test/test_request_obj.py
@@ -18,25 +18,26 @@ defined_http_methods = ("OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE",
from cherrypy.test import helper
+
class RequestObjectTests(helper.CPWebCase):
def setup_server():
class Root:
-
+
def index(self):
return "hello"
index.exposed = True
-
+
def scheme(self):
return cherrypy.request.scheme
scheme.exposed = True
-
+
root = Root()
-
-
+
class TestType(type):
- """Metaclass which automatically exposes all functions in each subclass,
- and adds an instance of the subclass as an attribute of root.
+ """Metaclass which automatically exposes all functions in each
+ subclass, and adds an instance of the subclass as an attribute
+ of root.
"""
def __init__(cls, name, bases, dct):
type.__init__(cls, name, bases, dct)
@@ -52,20 +53,20 @@ class RequestObjectTests(helper.CPWebCase):
return cherrypy.request.path_info
class Params(Test):
-
+
def index(self, thing):
return repr(thing)
-
+
def ismap(self, x, y):
return "Coordinates: %s, %s" % (x, y)
-
+
def default(self, *args, **kwargs):
- return "args: %s kwargs: %s" % (args, kwargs)
+ return "args: %s kwargs: %s" % (args, sorted(kwargs.items()))
default._cp_config = {'request.query_string_encoding': 'latin1'}
-
class ParamErrorsCallable(object):
exposed = True
+
def __call__(self):
return "data"
@@ -110,61 +111,66 @@ class RequestObjectTests(helper.CPWebCase):
raise_type_error.exposed = True
def raise_type_error_with_default_param(self, x, y=None):
- return '%d' % 'a' # throw an exception
+ return '%d' % 'a' # throw an exception
raise_type_error_with_default_param.exposed = True
def callable_error_page(status, **kwargs):
- return "Error %s - Well, I'm very sorry but you haven't paid!" % status
-
-
+ return "Error %s - Well, I'm very sorry but you haven't paid!" % (
+ status)
+
class Error(Test):
-
+
_cp_config = {'tools.log_tracebacks.on': True,
}
-
+
def reason_phrase(self):
raise cherrypy.HTTPError("410 Gone fishin'")
-
+
def custom(self, err='404'):
- raise cherrypy.HTTPError(int(err), "No, <b>really</b>, not found!")
- custom._cp_config = {'error_page.404': os.path.join(localDir, "static/index.html"),
- 'error_page.401': callable_error_page,
- }
-
+ raise cherrypy.HTTPError(
+ int(err), "No, <b>really</b>, not found!")
+ custom._cp_config = {
+ 'error_page.404': os.path.join(localDir, "static/index.html"),
+ 'error_page.401': callable_error_page,
+ }
+
def custom_default(self):
- return 1 + 'a' # raise an unexpected error
- custom_default._cp_config = {'error_page.default': callable_error_page}
-
+ return 1 + 'a' # raise an unexpected error
+ custom_default._cp_config = {
+ 'error_page.default': callable_error_page}
+
def noexist(self):
raise cherrypy.HTTPError(404, "No, <b>really</b>, not found!")
noexist._cp_config = {'error_page.404': "nonexistent.html"}
-
+
def page_method(self):
raise ValueError()
-
+
def page_yield(self):
yield "howdy"
raise ValueError()
-
+
def page_streamed(self):
yield "word up"
raise ValueError()
yield "very oops"
page_streamed._cp_config = {"response.stream": True}
-
+
def cause_err_in_finalize(self):
# Since status must start with an int, this should error.
cherrypy.response.status = "ZOO OK"
- cause_err_in_finalize._cp_config = {'request.show_tracebacks': False}
-
+ cause_err_in_finalize._cp_config = {
+ 'request.show_tracebacks': False}
+
def rethrow(self):
- """Test that an error raised here will be thrown out to the server."""
+ """Test that an error raised here will be thrown out to
+ the server.
+ """
raise ValueError()
rethrow._cp_config = {'request.throw_errors': True}
-
-
+
class Expect(Test):
-
+
def expectation_failed(self):
expect = cherrypy.request.headers.elements("Expect")
if expect and expect[0].value != '100-continue':
@@ -172,17 +178,18 @@ class RequestObjectTests(helper.CPWebCase):
raise cherrypy.HTTPError(417, 'Expectation Failed')
class Headers(Test):
-
+
def default(self, headername):
"""Spit back out the value for the requested header."""
return cherrypy.request.headers[headername]
-
+
def doubledheaders(self):
- # From http://www.cherrypy.org/ticket/165:
- # "header field names should not be case sensitive sayes the rfc.
- # if i set a headerfield in complete lowercase i end up with two
- # header fields, one in lowercase, the other in mixed-case."
-
+ # From https://bitbucket.org/cherrypy/cherrypy/issue/165:
+ # "header field names should not be case sensitive sayes the
+ # rfc. if i set a headerfield in complete lowercase i end up
+ # with two header fields, one in lowercase, the other in
+ # mixed-case."
+
# Set the most common headers
hMap = cherrypy.response.headers
hMap['content-type'] = "text/html"
@@ -192,70 +199,71 @@ class RequestObjectTests(helper.CPWebCase):
% (cherrypy.request.local.ip,
cherrypy.request.local.port,
cherrypy.request.scheme))
-
+
# Set a rare header for fun
hMap['Expires'] = 'Thu, 01 Dec 2194 16:00:00 GMT'
-
+
return "double header test"
-
+
def ifmatch(self):
val = cherrypy.request.headers['If-Match']
assert isinstance(val, unicodestr)
cherrypy.response.headers['ETag'] = val
return val
-
-
+
class HeaderElements(Test):
-
+
def get_elements(self, headername):
e = cherrypy.request.headers.elements(headername)
return "\n".join([unicodestr(x) for x in e])
-
-
+
class Method(Test):
-
+
def index(self):
m = cherrypy.request.method
if m in defined_http_methods or m == "CONNECT":
return m
-
+
if m == "LINK":
raise cherrypy.HTTPError(405)
else:
raise cherrypy.HTTPError(501)
-
+
def parameterized(self, data):
return data
-
+
def request_body(self):
# This should be a file object (temp file),
# which CP will just pipe back out if we tell it to.
return cherrypy.request.body
-
+
def reachable(self):
return "success"
class Divorce:
+
"""HTTP Method handlers shouldn't collide with normal method names.
- For example, a GET-handler shouldn't collide with a method named 'get'.
-
- If you build HTTP method dispatching into CherryPy, rewrite this class
- to use your new dispatch mechanism and make sure that:
+ For example, a GET-handler shouldn't collide with a method named
+ 'get'.
+
+ If you build HTTP method dispatching into CherryPy, rewrite this
+ class to use your new dispatch mechanism and make sure that:
"GET /divorce HTTP/1.1" maps to divorce.index() and
"GET /divorce/get?ID=13 HTTP/1.1" maps to divorce.get()
"""
-
+
documents = {}
-
+
def index(self):
yield "<h1>Choose your document</h1>\n"
yield "<ul>\n"
for id, contents in self.documents.items():
- yield (" <li><a href='/divorce/get?ID=%s'>%s</a>: %s</li>\n"
- % (id, id, contents))
+ yield (
+ " <li><a href='/divorce/get?ID=%s'>%s</a>:"
+ " %s</li>\n" % (id, id, contents))
yield "</ul>"
index.exposed = True
-
+
def get(self, ID):
return ("Divorce document %s: %s" %
(ID, self.documents.get(ID, "empty")))
@@ -263,17 +271,18 @@ class RequestObjectTests(helper.CPWebCase):
root.divorce = Divorce()
-
class ThreadLocal(Test):
-
+
def index(self):
existing = repr(getattr(cherrypy.request, "asdf", None))
cherrypy.request.asdf = "rassfrassin"
return existing
-
+
appconf = {
- '/method': {'request.methods_with_bodies': ("POST", "PUT", "PROPFIND")},
- }
+ '/method': {
+ 'request.methods_with_bodies': ("POST", "PUT", "PROPFIND")
+ },
+ }
cherrypy.tree.mount(root, config=appconf)
setup_server = staticmethod(setup_server)
@@ -293,7 +302,7 @@ class RequestObjectTests(helper.CPWebCase):
def testParams(self):
self.getPage("/params/?thing=a")
self.assertBody(repr(ntou("a")))
-
+
self.getPage("/params/?thing=a&thing=b&thing=c")
self.assertBody(repr([ntou('a'), ntou('b'), ntou('c')]))
@@ -303,7 +312,7 @@ class RequestObjectTests(helper.CPWebCase):
self.assertInBody("Missing parameters: thing")
self.getPage("/params/?thing=meeting&notathing=meeting")
self.assertInBody("Unexpected query string parameters: notathing")
-
+
# Test ability to turn off friendly error messages
cherrypy.config.update({"request.show_mismatched_params": False})
self.getPage("/params/?notathing=meeting")
@@ -315,28 +324,29 @@ class RequestObjectTests(helper.CPWebCase):
self.getPage("/params/%d4%20%e3/cheese?Gruy%E8re=Bulgn%e9ville")
self.assertBody("args: %s kwargs: %s" %
(('\xd4 \xe3', 'cheese'),
- {'Gruy\xe8re': ntou('Bulgn\xe9ville')}))
-
+ [('Gruy\xe8re', ntou('Bulgn\xe9ville'))]))
+
# Make sure that encoded = and & get parsed correctly
- self.getPage("/params/code?url=http%3A//cherrypy.org/index%3Fa%3D1%26b%3D2")
+ self.getPage(
+ "/params/code?url=http%3A//cherrypy.org/index%3Fa%3D1%26b%3D2")
self.assertBody("args: %s kwargs: %s" %
(('code',),
- {'url': ntou('http://cherrypy.org/index?a=1&b=2')}))
-
+ [('url', ntou('http://cherrypy.org/index?a=1&b=2'))]))
+
# Test coordinates sent by <img ismap>
self.getPage("/params/ismap?223,114")
self.assertBody("Coordinates: 223, 114")
-
+
# Test "name[key]" dict-like params
self.getPage("/params/dictlike?a[1]=1&a[2]=2&b=foo&b[bar]=baz")
self.assertBody("args: %s kwargs: %s" %
(('dictlike',),
- {'a[1]': ntou('1'), 'b[bar]': ntou('baz'),
- 'b': ntou('foo'), 'a[2]': ntou('2')}))
+ [('a[1]', ntou('1')), ('a[2]', ntou('2')),
+ ('b', ntou('foo')), ('b[bar]', ntou('baz'))]))
def testParamErrors(self):
- # test that all of the handlers work when given
+ # test that all of the handlers work when given
# the correct parameters in order to ensure that the
# errors below aren't coming from some other source.
for uri in (
@@ -344,51 +354,66 @@ class RequestObjectTests(helper.CPWebCase):
'/paramerrors/one_positional_args?param1=foo',
'/paramerrors/one_positional_args/foo',
'/paramerrors/one_positional_args/foo/bar/baz',
- '/paramerrors/one_positional_args_kwargs?param1=foo&param2=bar',
- '/paramerrors/one_positional_args_kwargs/foo?param2=bar&param3=baz',
- '/paramerrors/one_positional_args_kwargs/foo/bar/baz?param2=bar&param3=baz',
- '/paramerrors/one_positional_kwargs?param1=foo&param2=bar&param3=baz',
- '/paramerrors/one_positional_kwargs/foo?param4=foo&param2=bar&param3=baz',
+ '/paramerrors/one_positional_args_kwargs?'
+ 'param1=foo&param2=bar',
+ '/paramerrors/one_positional_args_kwargs/foo?'
+ 'param2=bar&param3=baz',
+ '/paramerrors/one_positional_args_kwargs/foo/bar/baz?'
+ 'param2=bar&param3=baz',
+ '/paramerrors/one_positional_kwargs?'
+ 'param1=foo&param2=bar&param3=baz',
+ '/paramerrors/one_positional_kwargs/foo?'
+ 'param4=foo&param2=bar&param3=baz',
'/paramerrors/no_positional',
'/paramerrors/no_positional_args/foo',
'/paramerrors/no_positional_args/foo/bar/baz',
'/paramerrors/no_positional_args_kwargs?param1=foo&param2=bar',
'/paramerrors/no_positional_args_kwargs/foo?param2=bar',
- '/paramerrors/no_positional_args_kwargs/foo/bar/baz?param2=bar&param3=baz',
+ '/paramerrors/no_positional_args_kwargs/foo/bar/baz?'
+ 'param2=bar&param3=baz',
'/paramerrors/no_positional_kwargs?param1=foo&param2=bar',
'/paramerrors/callable_object',
- ):
+ ):
self.getPage(uri)
self.assertStatus(200)
# query string parameters are part of the URI, so if they are wrong
# for a particular handler, the status MUST be a 404.
error_msgs = [
- 'Missing parameters',
- 'Nothing matches the given URI',
- 'Multiple values for parameters',
- 'Unexpected query string parameters',
- 'Unexpected body parameters',
- ]
+ 'Missing parameters',
+ 'Nothing matches the given URI',
+ 'Multiple values for parameters',
+ 'Unexpected query string parameters',
+ 'Unexpected body parameters',
+ ]
for uri, msg in (
('/paramerrors/one_positional', error_msgs[0]),
('/paramerrors/one_positional?foo=foo', error_msgs[0]),
('/paramerrors/one_positional/foo/bar/baz', error_msgs[1]),
('/paramerrors/one_positional/foo?param1=foo', error_msgs[2]),
- ('/paramerrors/one_positional/foo?param1=foo&param2=foo', error_msgs[2]),
- ('/paramerrors/one_positional_args/foo?param1=foo&param2=foo', error_msgs[2]),
- ('/paramerrors/one_positional_args/foo/bar/baz?param2=foo', error_msgs[3]),
- ('/paramerrors/one_positional_args_kwargs/foo/bar/baz?param1=bar&param3=baz', error_msgs[2]),
- ('/paramerrors/one_positional_kwargs/foo?param1=foo&param2=bar&param3=baz', error_msgs[2]),
+ ('/paramerrors/one_positional/foo?param1=foo&param2=foo',
+ error_msgs[2]),
+ ('/paramerrors/one_positional_args/foo?param1=foo&param2=foo',
+ error_msgs[2]),
+ ('/paramerrors/one_positional_args/foo/bar/baz?param2=foo',
+ error_msgs[3]),
+ ('/paramerrors/one_positional_args_kwargs/foo/bar/baz?'
+ 'param1=bar&param3=baz',
+ error_msgs[2]),
+ ('/paramerrors/one_positional_kwargs/foo?'
+ 'param1=foo&param2=bar&param3=baz',
+ error_msgs[2]),
('/paramerrors/no_positional/boo', error_msgs[1]),
('/paramerrors/no_positional?param1=foo', error_msgs[3]),
('/paramerrors/no_positional_args/boo?param1=foo', error_msgs[3]),
- ('/paramerrors/no_positional_kwargs/boo?param1=foo', error_msgs[1]),
+ ('/paramerrors/no_positional_kwargs/boo?param1=foo',
+ error_msgs[1]),
('/paramerrors/callable_object?param1=foo', error_msgs[3]),
('/paramerrors/callable_object/boo', error_msgs[1]),
- ):
+ ):
for show_mismatched_params in (True, False):
- cherrypy.config.update({'request.show_mismatched_params': show_mismatched_params})
+ cherrypy.config.update(
+ {'request.show_mismatched_params': show_mismatched_params})
self.getPage(uri)
self.assertStatus(404)
if show_mismatched_params:
@@ -398,18 +423,26 @@ class RequestObjectTests(helper.CPWebCase):
# if body parameters are wrong, a 400 must be returned.
for uri, body, msg in (
- ('/paramerrors/one_positional/foo', 'param1=foo', error_msgs[2]),
- ('/paramerrors/one_positional/foo', 'param1=foo&param2=foo', error_msgs[2]),
- ('/paramerrors/one_positional_args/foo', 'param1=foo&param2=foo', error_msgs[2]),
- ('/paramerrors/one_positional_args/foo/bar/baz', 'param2=foo', error_msgs[4]),
- ('/paramerrors/one_positional_args_kwargs/foo/bar/baz', 'param1=bar&param3=baz', error_msgs[2]),
- ('/paramerrors/one_positional_kwargs/foo', 'param1=foo&param2=bar&param3=baz', error_msgs[2]),
+ ('/paramerrors/one_positional/foo',
+ 'param1=foo', error_msgs[2]),
+ ('/paramerrors/one_positional/foo',
+ 'param1=foo&param2=foo', error_msgs[2]),
+ ('/paramerrors/one_positional_args/foo',
+ 'param1=foo&param2=foo', error_msgs[2]),
+ ('/paramerrors/one_positional_args/foo/bar/baz',
+ 'param2=foo', error_msgs[4]),
+ ('/paramerrors/one_positional_args_kwargs/foo/bar/baz',
+ 'param1=bar&param3=baz', error_msgs[2]),
+ ('/paramerrors/one_positional_kwargs/foo',
+ 'param1=foo&param2=bar&param3=baz', error_msgs[2]),
('/paramerrors/no_positional', 'param1=foo', error_msgs[4]),
- ('/paramerrors/no_positional_args/boo', 'param1=foo', error_msgs[4]),
+ ('/paramerrors/no_positional_args/boo',
+ 'param1=foo', error_msgs[4]),
('/paramerrors/callable_object', 'param1=foo', error_msgs[4]),
- ):
+ ):
for show_mismatched_params in (True, False):
- cherrypy.config.update({'request.show_mismatched_params': show_mismatched_params})
+ cherrypy.config.update(
+ {'request.show_mismatched_params': show_mismatched_params})
self.getPage(uri, method='POST', body=body)
self.assertStatus(400)
if show_mismatched_params:
@@ -417,20 +450,27 @@ class RequestObjectTests(helper.CPWebCase):
else:
self.assertInBody("400 Bad")
-
- # even if body parameters are wrong, if we get the uri wrong, then
+ # even if body parameters are wrong, if we get the uri wrong, then
# it's a 404
for uri, body, msg in (
- ('/paramerrors/one_positional?param2=foo', 'param1=foo', error_msgs[3]),
- ('/paramerrors/one_positional/foo/bar', 'param2=foo', error_msgs[1]),
- ('/paramerrors/one_positional_args/foo/bar?param2=foo', 'param3=foo', error_msgs[3]),
- ('/paramerrors/one_positional_kwargs/foo/bar', 'param2=bar&param3=baz', error_msgs[1]),
- ('/paramerrors/no_positional?param1=foo', 'param2=foo', error_msgs[3]),
- ('/paramerrors/no_positional_args/boo?param2=foo', 'param1=foo', error_msgs[3]),
- ('/paramerrors/callable_object?param2=bar', 'param1=foo', error_msgs[3]),
- ):
+ ('/paramerrors/one_positional?param2=foo',
+ 'param1=foo', error_msgs[3]),
+ ('/paramerrors/one_positional/foo/bar',
+ 'param2=foo', error_msgs[1]),
+ ('/paramerrors/one_positional_args/foo/bar?param2=foo',
+ 'param3=foo', error_msgs[3]),
+ ('/paramerrors/one_positional_kwargs/foo/bar',
+ 'param2=bar&param3=baz', error_msgs[1]),
+ ('/paramerrors/no_positional?param1=foo',
+ 'param2=foo', error_msgs[3]),
+ ('/paramerrors/no_positional_args/boo?param2=foo',
+ 'param1=foo', error_msgs[3]),
+ ('/paramerrors/callable_object?param2=bar',
+ 'param1=foo', error_msgs[3]),
+ ):
for show_mismatched_params in (True, False):
- cherrypy.config.update({'request.show_mismatched_params': show_mismatched_params})
+ cherrypy.config.update(
+ {'request.show_mismatched_params': show_mismatched_params})
self.getPage(uri, method='POST', body=body)
self.assertStatus(404)
if show_mismatched_params:
@@ -444,7 +484,7 @@ class RequestObjectTests(helper.CPWebCase):
'/paramerrors/raise_type_error',
'/paramerrors/raise_type_error_with_default_param?x=0',
'/paramerrors/raise_type_error_with_default_param?x=0&y=0',
- ):
+ ):
self.getPage(uri, method='GET')
self.assertStatus(500)
self.assertTrue('Client Error', self.body)
@@ -453,19 +493,19 @@ class RequestObjectTests(helper.CPWebCase):
self.getPage("/error/missing")
self.assertStatus(404)
self.assertErrorPage(404, "The path '/error/missing' was not found.")
-
+
ignore = helper.webtest.ignored_exceptions
ignore.append(ValueError)
try:
valerr = '\n raise ValueError()\nValueError'
self.getPage("/error/page_method")
self.assertErrorPage(500, pattern=valerr)
-
+
self.getPage("/error/page_yield")
self.assertErrorPage(500, pattern=valerr)
-
+
if (cherrypy.server.protocol_version == "HTTP/1.0" or
- getattr(cherrypy.server, "using_apache", False)):
+ getattr(cherrypy.server, "using_apache", False)):
self.getPage("/error/page_streamed")
# Because this error is raised after the response body has
# started, the status should not change to an error status.
@@ -476,57 +516,66 @@ class RequestObjectTests(helper.CPWebCase):
# The HTTP client will choke when the output is incomplete.
self.assertRaises((ValueError, IncompleteRead), self.getPage,
"/error/page_streamed")
-
+
# No traceback should be present
self.getPage("/error/cause_err_in_finalize")
msg = "Illegal response status from server ('ZOO' is non-numeric)."
self.assertErrorPage(500, msg, None)
finally:
ignore.pop()
-
+
# Test HTTPError with a reason-phrase in the status arg.
self.getPage('/error/reason_phrase')
self.assertStatus("410 Gone fishin'")
-
+
# Test custom error page for a specific error.
self.getPage("/error/custom")
self.assertStatus(404)
self.assertBody("Hello, world\r\n" + (" " * 499))
-
+
# Test custom error page for a specific error.
self.getPage("/error/custom?err=401")
self.assertStatus(401)
- self.assertBody("Error 401 Unauthorized - Well, I'm very sorry but you haven't paid!")
-
+ self.assertBody(
+ "Error 401 Unauthorized - "
+ "Well, I'm very sorry but you haven't paid!")
+
# Test default custom error page.
self.getPage("/error/custom_default")
self.assertStatus(500)
- self.assertBody("Error 500 Internal Server Error - Well, I'm very sorry but you haven't paid!".ljust(513))
-
+ self.assertBody(
+ "Error 500 Internal Server Error - "
+ "Well, I'm very sorry but you haven't paid!".ljust(513))
+
# Test error in custom error page (ticket #305).
# Note that the message is escaped for HTML (ticket #310).
self.getPage("/error/noexist")
self.assertStatus(404)
+ if sys.version_info >= (3, 3):
+ exc_name = "FileNotFoundError"
+ else:
+ exc_name = "IOError"
msg = ("No, &lt;b&gt;really&lt;/b&gt;, not found!<br />"
"In addition, the custom error page failed:\n<br />"
- "IOError: [Errno 2] No such file or directory: 'nonexistent.html'")
+ "%s: [Errno 2] "
+ "No such file or directory: 'nonexistent.html'") % (exc_name,)
self.assertInBody(msg)
-
+
if getattr(cherrypy.server, "using_apache", False):
pass
else:
# Test throw_errors (ticket #186).
self.getPage("/error/rethrow")
self.assertInBody("raise ValueError()")
-
+
def testExpect(self):
e = ('Expect', '100-continue')
self.getPage("/headerelements/get_elements?headername=Expect", [e])
self.assertBody('100-continue')
-
+
self.getPage("/expect/expectation_failed", [e])
self.assertStatus(417)
-
+
def testHeaderElements(self):
# Accept-* header elements should be sorted, with most preferred first.
h = [('Accept', 'audio/*; q=0.2, audio/basic')]
@@ -534,15 +583,18 @@ class RequestObjectTests(helper.CPWebCase):
self.assertStatus(200)
self.assertBody("audio/basic\n"
"audio/*;q=0.2")
-
- h = [('Accept', 'text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c')]
+
+ h = [
+ ('Accept',
+ 'text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c')
+ ]
self.getPage("/headerelements/get_elements?headername=Accept", h)
self.assertStatus(200)
self.assertBody("text/x-c\n"
"text/html\n"
"text/x-dvi;q=0.8\n"
"text/plain;q=0.5")
-
+
# Test that more specific media ranges get priority.
h = [('Accept', 'text/*, text/html, text/html;level=1, */*')]
self.getPage("/headerelements/get_elements?headername=Accept", h)
@@ -551,45 +603,49 @@ class RequestObjectTests(helper.CPWebCase):
"text/html\n"
"text/*\n"
"*/*")
-
+
# Test Accept-Charset
h = [('Accept-Charset', 'iso-8859-5, unicode-1-1;q=0.8')]
- self.getPage("/headerelements/get_elements?headername=Accept-Charset", h)
+ self.getPage(
+ "/headerelements/get_elements?headername=Accept-Charset", h)
self.assertStatus("200 OK")
self.assertBody("iso-8859-5\n"
"unicode-1-1;q=0.8")
-
+
# Test Accept-Encoding
h = [('Accept-Encoding', 'gzip;q=1.0, identity; q=0.5, *;q=0')]
- self.getPage("/headerelements/get_elements?headername=Accept-Encoding", h)
+ self.getPage(
+ "/headerelements/get_elements?headername=Accept-Encoding", h)
self.assertStatus("200 OK")
self.assertBody("gzip;q=1.0\n"
"identity;q=0.5\n"
"*;q=0")
-
+
# Test Accept-Language
h = [('Accept-Language', 'da, en-gb;q=0.8, en;q=0.7')]
- self.getPage("/headerelements/get_elements?headername=Accept-Language", h)
+ self.getPage(
+ "/headerelements/get_elements?headername=Accept-Language", h)
self.assertStatus("200 OK")
self.assertBody("da\n"
"en-gb;q=0.8\n"
"en;q=0.7")
-
- # Test malformed header parsing. See http://www.cherrypy.org/ticket/763.
+
+ # Test malformed header parsing. See
+ # https://bitbucket.org/cherrypy/cherrypy/issue/763.
self.getPage("/headerelements/get_elements?headername=Content-Type",
# Note the illegal trailing ";"
headers=[('Content-Type', 'text/html; charset=utf-8;')])
self.assertStatus(200)
self.assertBody("text/html;charset=utf-8")
-
+
def test_repeated_headers(self):
# Test that two request headers are collapsed into one.
- # See http://www.cherrypy.org/ticket/542.
+ # See https://bitbucket.org/cherrypy/cherrypy/issue/542.
self.getPage("/headers/Accept-Charset",
headers=[("Accept-Charset", "iso-8859-5"),
("Accept-Charset", "unicode-1-1;q=0.8")])
self.assertBody("iso-8859-5, unicode-1-1;q=0.8")
-
+
# Tests that each header only appears once, regardless of case.
self.getPage("/headers/doubledheaders")
self.assertBody("double header test")
@@ -597,53 +653,56 @@ class RequestObjectTests(helper.CPWebCase):
for key in ['Content-Length', 'Content-Type', 'Date',
'Expires', 'Location', 'Server']:
self.assertEqual(hnames.count(key), 1, self.headers)
-
+
def test_encoded_headers(self):
# First, make sure the innards work like expected.
- self.assertEqual(httputil.decode_TEXT(ntou("=?utf-8?q?f=C3=BCr?=")), ntou("f\xfcr"))
-
+ self.assertEqual(
+ httputil.decode_TEXT(ntou("=?utf-8?q?f=C3=BCr?=")), ntou("f\xfcr"))
+
if cherrypy.server.protocol_version == "HTTP/1.1":
# Test RFC-2047-encoded request and response header values
u = ntou('\u212bngstr\xf6m', 'escape')
c = ntou("=E2=84=ABngstr=C3=B6m")
- self.getPage("/headers/ifmatch", [('If-Match', ntou('=?utf-8?q?%s?=') % c)])
+ self.getPage("/headers/ifmatch",
+ [('If-Match', ntou('=?utf-8?q?%s?=') % c)])
# The body should be utf-8 encoded.
self.assertBody(ntob("\xe2\x84\xabngstr\xc3\xb6m"))
# But the Etag header should be RFC-2047 encoded (binary)
self.assertHeader("ETag", ntou('=?utf-8?b?4oSrbmdzdHLDtm0=?='))
-
+
# Test a *LONG* RFC-2047-encoded request and response header value
self.getPage("/headers/ifmatch",
[('If-Match', ntou('=?utf-8?q?%s?=') % (c * 10))])
self.assertBody(ntob("\xe2\x84\xabngstr\xc3\xb6m") * 10)
# Note: this is different output for Python3, but it decodes fine.
- etag = self.assertHeader("ETag",
+ etag = self.assertHeader(
+ "ETag",
'=?utf-8?b?4oSrbmdzdHLDtm3ihKtuZ3N0csO2beKEq25nc3Ryw7Zt'
'4oSrbmdzdHLDtm3ihKtuZ3N0csO2beKEq25nc3Ryw7Zt'
'4oSrbmdzdHLDtm3ihKtuZ3N0csO2beKEq25nc3Ryw7Zt'
'4oSrbmdzdHLDtm0=?=')
self.assertEqual(httputil.decode_TEXT(etag), u * 10)
-
+
def test_header_presence(self):
# If we don't pass a Content-Type header, it should not be present
# in cherrypy.request.headers
self.getPage("/headers/Content-Type",
headers=[])
self.assertStatus(500)
-
+
# If Content-Type is present in the request, it should be present in
# cherrypy.request.headers
self.getPage("/headers/Content-Type",
headers=[("Content-type", "application/json")])
self.assertBody("application/json")
-
+
def test_basic_HTTPMethods(self):
helper.webtest.methods_with_bodies = ("POST", "PUT", "PROPFIND")
-
+
# Test that all defined HTTP methods work.
for m in defined_http_methods:
self.getPage("/method/", method=m)
-
+
# HEAD requests should not return any body.
if m == "HEAD":
self.assertBody("")
@@ -652,12 +711,12 @@ class RequestObjectTests(helper.CPWebCase):
self.assertEqual(self.body[:5], ntob("TRACE"))
else:
self.assertBody(m)
-
+
# Request a PUT method with a form-urlencoded body
self.getPage("/method/parameterized", method="PUT",
- body="data=on+top+of+other+things")
+ body="data=on+top+of+other+things")
self.assertBody("on top of other things")
-
+
# Request a PUT method with a file body
b = "one thing on top of another"
h = [("Content-Type", "text/plain"),
@@ -665,9 +724,9 @@ class RequestObjectTests(helper.CPWebCase):
self.getPage("/method/request_body", headers=h, method="PUT", body=b)
self.assertStatus(200)
self.assertBody(b)
-
+
# Request a PUT method with a file body but no Content-Type.
- # See http://www.cherrypy.org/ticket/790.
+ # See https://bitbucket.org/cherrypy/cherrypy/issue/790.
b = ntob("one thing on top of another")
self.persistent = True
try:
@@ -684,32 +743,33 @@ class RequestObjectTests(helper.CPWebCase):
self.assertBody(b)
finally:
self.persistent = False
-
+
# Request a PUT method with no body whatsoever (not an empty one).
- # See http://www.cherrypy.org/ticket/650.
+ # See https://bitbucket.org/cherrypy/cherrypy/issue/650.
# Provide a C-T or webtest will provide one (and a C-L) for us.
h = [("Content-Type", "text/plain")]
self.getPage("/method/reachable", headers=h, method="PUT")
self.assertStatus(411)
-
+
# Request a custom method with a request body
b = ('<?xml version="1.0" encoding="utf-8" ?>\n\n'
'<propfind xmlns="DAV:"><prop><getlastmodified/>'
'</prop></propfind>')
h = [('Content-Type', 'text/xml'),
('Content-Length', str(len(b)))]
- self.getPage("/method/request_body", headers=h, method="PROPFIND", body=b)
+ self.getPage("/method/request_body", headers=h,
+ method="PROPFIND", body=b)
self.assertStatus(200)
self.assertBody(b)
-
+
# Request a disallowed method
self.getPage("/method/", method="LINK")
self.assertStatus(405)
-
+
# Request an unknown method
self.getPage("/method/", method="SEARCH")
self.assertStatus(501)
-
+
# For method dispatchers: make sure that an HTTP method doesn't
# collide with a virtual path atom. If you build HTTP-method
# dispatching into the core, rewrite these handlers to use
@@ -720,18 +780,17 @@ class RequestObjectTests(helper.CPWebCase):
self.getPage("/divorce/", method="GET")
self.assertBody('<h1>Choose your document</h1>\n<ul>\n</ul>')
self.assertStatus(200)
-
+
def test_CONNECT_method(self):
if getattr(cherrypy.server, "using_apache", False):
return self.skip("skipped due to known Apache differences... ")
-
+
self.getPage("/method/", method="CONNECT")
self.assertBody("CONNECT")
-
+
def testEmptyThreadlocals(self):
results = []
for x in range(20):
self.getPage("/threadlocal/")
results.append(self.body)
self.assertEqual(results, [ntob("None")] * 20)
-
diff --git a/cherrypy/test/test_routes.py b/cherrypy/test/test_routes.py
index a8062f8f..41f744f9 100644
--- a/cherrypy/test/test_routes.py
+++ b/cherrypy/test/test_routes.py
@@ -6,6 +6,7 @@ import cherrypy
from cherrypy.test import helper
import nose
+
class RoutesDispatchTest(helper.CPWebCase):
def setup_server():
@@ -16,33 +17,39 @@ class RoutesDispatchTest(helper.CPWebCase):
raise nose.SkipTest('Install routes to test RoutesDispatcher code')
class Dummy:
+
def index(self):
return "I said good day!"
-
+
class City:
-
+
def __init__(self, name):
self.name = name
self.population = 10000
-
+
def index(self, **kwargs):
return "Welcome to %s, pop. %s" % (self.name, self.population)
- index._cp_config = {'tools.response_headers.on': True,
- 'tools.response_headers.headers': [('Content-Language', 'en-GB')]}
-
+ index._cp_config = {
+ 'tools.response_headers.on': True,
+ 'tools.response_headers.headers': [
+ ('Content-Language', 'en-GB')
+ ]
+ }
+
def update(self, **kwargs):
self.population = kwargs['pop']
return "OK"
-
+
d = cherrypy.dispatch.RoutesDispatcher()
d.connect(action='index', name='hounslow', route='/hounslow',
controller=City('Hounslow'))
- d.connect(name='surbiton', route='/surbiton', controller=City('Surbiton'),
- action='index', conditions=dict(method=['GET']))
+ d.connect(
+ name='surbiton', route='/surbiton', controller=City('Surbiton'),
+ action='index', conditions=dict(method=['GET']))
d.mapper.connect('/surbiton', controller='surbiton',
action='update', conditions=dict(method=['POST']))
d.connect('main', ':action', controller=Dummy())
-
+
conf = {'/': {'request.dispatch': d}}
cherrypy.tree.mount(root=None, config=conf)
setup_server = staticmethod(setup_server)
@@ -51,14 +58,14 @@ class RoutesDispatchTest(helper.CPWebCase):
self.getPage("/hounslow")
self.assertStatus("200 OK")
self.assertBody("Welcome to Hounslow, pop. 10000")
-
+
self.getPage("/foo")
self.assertStatus("404 Not Found")
-
+
self.getPage("/surbiton")
self.assertStatus("200 OK")
self.assertBody("Welcome to Surbiton, pop. 10000")
-
+
self.getPage("/surbiton", method="POST", body="pop=1327")
self.assertStatus("200 OK")
self.assertBody("OK")
@@ -66,4 +73,3 @@ class RoutesDispatchTest(helper.CPWebCase):
self.assertStatus("200 OK")
self.assertHeader("Content-Language", "en-GB")
self.assertBody("Welcome to Surbiton, pop. 1327")
-
diff --git a/cherrypy/test/test_session.py b/cherrypy/test/test_session.py
index 9143a1d2..355a2a3a 100755
--- a/cherrypy/test/test_session.py
+++ b/cherrypy/test/test_session.py
@@ -9,6 +9,7 @@ from cherrypy._cpcompat import copykeys, HTTPConnection, HTTPSConnection
from cherrypy.lib import sessions
from cherrypy.lib.httputil import response_codes
+
def http_methods_allowed(methods=['GET', 'HEAD']):
method = cherrypy.request.method.upper()
if method not in methods:
@@ -19,39 +20,40 @@ cherrypy.tools.allow = cherrypy.Tool('on_start_resource', http_methods_allowed)
def setup_server():
-
+
class Root:
-
+
_cp_config = {'tools.sessions.on': True,
- 'tools.sessions.storage_type' : 'ram',
- 'tools.sessions.storage_path' : localDir,
+ 'tools.sessions.storage_type': 'ram',
+ 'tools.sessions.storage_path': localDir,
'tools.sessions.timeout': (1.0 / 60),
'tools.sessions.clean_freq': (1.0 / 60),
}
-
+
def clear(self):
cherrypy.session.cache.clear()
clear.exposed = True
-
+
def data(self):
cherrypy.session['aha'] = 'foo'
return repr(cherrypy.session._data)
data.exposed = True
-
+
def testGen(self):
counter = cherrypy.session.get('counter', 0) + 1
cherrypy.session['counter'] = counter
yield str(counter)
testGen.exposed = True
-
+
def testStr(self):
counter = cherrypy.session.get('counter', 0) + 1
cherrypy.session['counter'] = counter
return str(counter)
testStr.exposed = True
-
+
def setsessiontype(self, newtype):
- self.__class__._cp_config.update({'tools.sessions.storage_type': newtype})
+ self.__class__._cp_config.update(
+ {'tools.sessions.storage_type': newtype})
if hasattr(cherrypy, "session"):
del cherrypy.session
cls = getattr(sessions, newtype.title() + 'Session')
@@ -61,7 +63,7 @@ def setup_server():
del cls.clean_thread
setsessiontype.exposed = True
setsessiontype._cp_config = {'tools.sessions.on': False}
-
+
def index(self):
sess = cherrypy.session
c = sess.get('counter', 0) + 1
@@ -69,45 +71,45 @@ def setup_server():
sess['counter'] = c
return str(c)
index.exposed = True
-
+
def keyin(self, key):
return str(key in cherrypy.session)
keyin.exposed = True
-
+
def delete(self):
cherrypy.session.delete()
sessions.expire()
return "done"
delete.exposed = True
-
+
def delkey(self, key):
del cherrypy.session[key]
return "OK"
delkey.exposed = True
-
+
def blah(self):
return self._cp_config['tools.sessions.storage_type']
blah.exposed = True
-
+
def iredir(self):
raise cherrypy.InternalRedirect('/blah')
iredir.exposed = True
-
+
def restricted(self):
return cherrypy.request.method
restricted.exposed = True
restricted._cp_config = {'tools.allow.on': True,
'tools.allow.methods': ['GET']}
-
+
def regen(self):
cherrypy.tools.sessions.regenerate()
return "logged in"
regen.exposed = True
-
+
def length(self):
return str(len(cherrypy.session))
length.exposed = True
-
+
def session_cookie(self):
# Must load() to start the clean thread.
cherrypy.session.load()
@@ -117,25 +119,26 @@ def setup_server():
'tools.sessions.path': '/session_cookie',
'tools.sessions.name': 'temp',
'tools.sessions.persistent': False}
-
+
cherrypy.tree.mount(Root())
from cherrypy.test import helper
+
class SessionTest(helper.CPWebCase):
setup_server = staticmethod(setup_server)
-
+
def tearDown(self):
# Clean up sessions.
for fname in os.listdir(localDir):
if fname.startswith(sessions.FileSession.SESSION_PREFIX):
os.unlink(os.path.join(localDir, fname))
-
+
def test_0_Session(self):
self.getPage('/setsessiontype/ram')
self.getPage('/clear')
-
+
# Test that a normal request gets the same id in the cookies.
# Note: this wouldn't work if /data didn't load the session.
self.getPage('/data')
@@ -143,7 +146,7 @@ class SessionTest(helper.CPWebCase):
c = self.cookies[0]
self.getPage('/data', self.cookies)
self.assertEqual(self.cookies[0], c)
-
+
self.getPage('/testStr')
self.assertBody('1')
cookie_parts = dict([p.strip().split('=')
@@ -161,7 +164,7 @@ class SessionTest(helper.CPWebCase):
self.assertBody('2')
self.getPage('/delkey?key=counter', self.cookies)
self.assertStatus(200)
-
+
self.getPage('/setsessiontype/file')
self.getPage('/testStr')
self.assertBody('1')
@@ -171,59 +174,61 @@ class SessionTest(helper.CPWebCase):
self.assertBody('3')
self.getPage('/delkey?key=counter', self.cookies)
self.assertStatus(200)
-
+
# Wait for the session.timeout (1 second)
time.sleep(2)
self.getPage('/')
self.assertBody('1')
self.getPage('/length', self.cookies)
self.assertBody('1')
-
+
# Test session __contains__
self.getPage('/keyin?key=counter', self.cookies)
self.assertBody("True")
cookieset1 = self.cookies
-
+
# Make a new session and test __len__ again
self.getPage('/')
self.getPage('/length', self.cookies)
self.assertBody('2')
-
+
# Test session delete
self.getPage('/delete', self.cookies)
self.assertBody("done")
self.getPage('/delete', cookieset1)
self.assertBody("done")
- f = lambda: [x for x in os.listdir(localDir) if x.startswith('session-')]
+ f = lambda: [
+ x for x in os.listdir(localDir) if x.startswith('session-')]
self.assertEqual(f(), [])
-
+
# Wait for the cleanup thread to delete remaining session files
self.getPage('/')
- f = lambda: [x for x in os.listdir(localDir) if x.startswith('session-')]
+ f = lambda: [
+ x for x in os.listdir(localDir) if x.startswith('session-')]
self.assertNotEqual(f(), [])
time.sleep(2)
self.assertEqual(f(), [])
-
+
def test_1_Ram_Concurrency(self):
self.getPage('/setsessiontype/ram')
self._test_Concurrency()
-
+
def test_2_File_Concurrency(self):
self.getPage('/setsessiontype/file')
self._test_Concurrency()
-
+
def _test_Concurrency(self):
client_thread_count = 5
request_count = 30
-
+
# Get initial cookie
self.getPage("/")
self.assertBody("1")
cookies = self.cookies
-
+
data_dict = {}
errors = []
-
+
def request(index):
if self.scheme == 'https':
c = HTTPSConnection('%s:%s' % (self.interface(), self.PORT))
@@ -242,7 +247,7 @@ class SessionTest(helper.CPWebCase):
data_dict[index] = max(data_dict[index], int(body))
# Uncomment the following line to prove threads overlap.
## sys.stdout.write("%d " % index)
-
+
# Start <request_count> requests from each of
# <client_thread_count> concurrent clients
ts = []
@@ -251,23 +256,23 @@ class SessionTest(helper.CPWebCase):
t = threading.Thread(target=request, args=(c,))
ts.append(t)
t.start()
-
+
for t in ts:
t.join()
-
+
hitcount = max(data_dict.values())
expected = 1 + (client_thread_count * request_count)
-
+
for e in errors:
print(e)
self.assertEqual(hitcount, expected)
-
+
def test_3_Redirect(self):
# Start a new session
self.getPage('/testStr')
self.getPage('/iredir', self.cookies)
self.assertBody("file")
-
+
def test_4_File_deletion(self):
# Start a new session
self.getPage('/testStr')
@@ -276,11 +281,11 @@ class SessionTest(helper.CPWebCase):
path = os.path.join(localDir, "session-" + id)
os.unlink(path)
self.getPage('/testStr', self.cookies)
-
+
def test_5_Error_paths(self):
self.getPage('/unknown/page')
self.assertErrorPage(404, "The path '/unknown/page' was not found.")
-
+
# Note: this path is *not* the same as above. The above
# takes a normal route through the session code; this one
# skips the session code's before_handler and only calls
@@ -288,7 +293,7 @@ class SessionTest(helper.CPWebCase):
# code has to survive calling save/close without init.
self.getPage('/restricted', self.cookies, method='POST')
self.assertErrorPage(405, response_codes[405][1])
-
+
def test_6_regenerate(self):
self.getPage('/testStr')
# grab the cookie ID
@@ -297,48 +302,53 @@ class SessionTest(helper.CPWebCase):
self.assertBody('logged in')
id2 = self.cookies[0][1].split(";", 1)[0].split("=", 1)[1]
self.assertNotEqual(id1, id2)
-
+
self.getPage('/testStr')
# grab the cookie ID
id1 = self.cookies[0][1].split(";", 1)[0].split("=", 1)[1]
self.getPage('/testStr',
- headers=[('Cookie',
- 'session_id=maliciousid; '
- 'expires=Sat, 27 Oct 2017 04:18:28 GMT; Path=/;')])
+ headers=[
+ ('Cookie',
+ 'session_id=maliciousid; '
+ 'expires=Sat, 27 Oct 2017 04:18:28 GMT; Path=/;')])
id2 = self.cookies[0][1].split(";", 1)[0].split("=", 1)[1]
self.assertNotEqual(id1, id2)
self.assertNotEqual(id2, 'maliciousid')
-
+
def test_7_session_cookies(self):
self.getPage('/setsessiontype/ram')
self.getPage('/clear')
self.getPage('/session_cookie')
# grab the cookie ID
- cookie_parts = dict([p.strip().split('=') for p in self.cookies[0][1].split(";")])
+ cookie_parts = dict([p.strip().split('=')
+ for p in self.cookies[0][1].split(";")])
# Assert there is no 'expires' param
self.assertEqual(set(cookie_parts.keys()), set(['temp', 'Path']))
id1 = cookie_parts['temp']
self.assertEqual(copykeys(sessions.RamSession.cache), [id1])
-
+
# Send another request in the same "browser session".
self.getPage('/session_cookie', self.cookies)
- cookie_parts = dict([p.strip().split('=') for p in self.cookies[0][1].split(";")])
+ cookie_parts = dict([p.strip().split('=')
+ for p in self.cookies[0][1].split(";")])
# Assert there is no 'expires' param
self.assertEqual(set(cookie_parts.keys()), set(['temp', 'Path']))
self.assertBody(id1)
self.assertEqual(copykeys(sessions.RamSession.cache), [id1])
-
+
# Simulate a browser close by just not sending the cookies
self.getPage('/session_cookie')
# grab the cookie ID
- cookie_parts = dict([p.strip().split('=') for p in self.cookies[0][1].split(";")])
+ cookie_parts = dict([p.strip().split('=')
+ for p in self.cookies[0][1].split(";")])
# Assert there is no 'expires' param
self.assertEqual(set(cookie_parts.keys()), set(['temp', 'Path']))
# Assert a new id has been generated...
id2 = cookie_parts['temp']
self.assertNotEqual(id1, id2)
- self.assertEqual(set(sessions.RamSession.cache.keys()), set([id1, id2]))
-
+ self.assertEqual(set(sessions.RamSession.cache.keys()),
+ set([id1, id2]))
+
# Wait for the session.timeout on both sessions
time.sleep(2.5)
cache = copykeys(sessions.RamSession.cache)
@@ -352,7 +362,7 @@ class SessionTest(helper.CPWebCase):
import socket
try:
import memcache
-
+
host, port = '127.0.0.1', 11211
for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC,
socket.SOCK_STREAM):
@@ -373,16 +383,16 @@ try:
except (ImportError, socket.error):
class MemcachedSessionTest(helper.CPWebCase):
setup_server = staticmethod(setup_server)
-
+
def test(self):
return self.skip("memcached not reachable ")
else:
class MemcachedSessionTest(helper.CPWebCase):
setup_server = staticmethod(setup_server)
-
+
def test_0_Session(self):
self.getPage('/setsessiontype/memcached')
-
+
self.getPage('/testStr')
self.assertBody('1')
self.getPage('/testGen', self.cookies)
@@ -394,31 +404,31 @@ else:
self.assertInBody("NotImplementedError")
self.getPage('/delkey?key=counter', self.cookies)
self.assertStatus(200)
-
+
# Wait for the session.timeout (1 second)
time.sleep(1.25)
self.getPage('/')
self.assertBody('1')
-
+
# Test session __contains__
self.getPage('/keyin?key=counter', self.cookies)
self.assertBody("True")
-
+
# Test session delete
self.getPage('/delete', self.cookies)
self.assertBody("done")
-
+
def test_1_Concurrency(self):
client_thread_count = 5
request_count = 30
-
+
# Get initial cookie
self.getPage("/")
self.assertBody("1")
cookies = self.cookies
-
+
data_dict = {}
-
+
def request(index):
for i in range(request_count):
self.getPage("/", cookies)
@@ -427,7 +437,7 @@ else:
if not self.body.isdigit():
self.fail(self.body)
data_dict[index] = v = int(self.body)
-
+
# Start <request_count> concurrent requests from
# each of <client_thread_count> clients
ts = []
@@ -436,24 +446,25 @@ else:
t = threading.Thread(target=request, args=(c,))
ts.append(t)
t.start()
-
+
for t in ts:
t.join()
-
+
hitcount = max(data_dict.values())
expected = 1 + (client_thread_count * request_count)
self.assertEqual(hitcount, expected)
-
+
def test_3_Redirect(self):
# Start a new session
self.getPage('/testStr')
self.getPage('/iredir', self.cookies)
self.assertBody("memcached")
-
+
def test_5_Error_paths(self):
self.getPage('/unknown/page')
- self.assertErrorPage(404, "The path '/unknown/page' was not found.")
-
+ self.assertErrorPage(
+ 404, "The path '/unknown/page' was not found.")
+
# Note: this path is *not* the same as above. The above
# takes a normal route through the session code; this one
# skips the session code's before_handler and only calls
@@ -461,4 +472,3 @@ else:
# code has to survive calling save/close without init.
self.getPage('/restricted', self.cookies, method='POST')
self.assertErrorPage(405, response_codes[405][1])
-
diff --git a/cherrypy/test/test_sessionauthenticate.py b/cherrypy/test/test_sessionauthenticate.py
index ab1fe51e..2a6aa8d0 100644
--- a/cherrypy/test/test_sessionauthenticate.py
+++ b/cherrypy/test/test_sessionauthenticate.py
@@ -5,58 +5,57 @@ from cherrypy.test import helper
class SessionAuthenticateTest(helper.CPWebCase):
def setup_server():
-
+
def check(username, password):
# Dummy check_username_and_password function
if username != 'test' or password != 'password':
return 'Wrong login/password'
-
+
def augment_params():
# A simple tool to add some things to request.params
- # This is to check to make sure that session_auth can handle request
- # params (ticket #780)
+ # This is to check to make sure that session_auth can handle
+ # request params (ticket #780)
cherrypy.request.params["test"] = "test"
- cherrypy.tools.augment_params = cherrypy.Tool('before_handler',
- augment_params, None, priority=30)
+ cherrypy.tools.augment_params = cherrypy.Tool(
+ 'before_handler', augment_params, None, priority=30)
class Test:
-
- _cp_config = {'tools.sessions.on': True,
- 'tools.session_auth.on': True,
- 'tools.session_auth.check_username_and_password': check,
- 'tools.augment_params.on': True,
- }
-
+
+ _cp_config = {
+ 'tools.sessions.on': True,
+ 'tools.session_auth.on': True,
+ 'tools.session_auth.check_username_and_password': check,
+ 'tools.augment_params.on': True,
+ }
+
def index(self, **kwargs):
return "Hi %s, you are logged in" % cherrypy.request.login
index.exposed = True
-
+
cherrypy.tree.mount(Test())
setup_server = staticmethod(setup_server)
-
def testSessionAuthenticate(self):
# request a page and check for login form
self.getPage('/')
self.assertInBody('<form method="post" action="do_login">')
-
+
# setup credentials
login_body = 'username=test&password=password&from_page=/'
-
+
# attempt a login
self.getPage('/do_login', method='POST', body=login_body)
self.assertStatus((302, 303))
-
+
# get the page now that we are logged in
self.getPage('/', self.cookies)
self.assertBody('Hi test, you are logged in')
-
+
# do a logout
self.getPage('/do_logout', self.cookies, method='POST')
self.assertStatus((302, 303))
-
+
# verify we are logged out
self.getPage('/', self.cookies)
self.assertInBody('<form method="post" action="do_login">')
-
diff --git a/cherrypy/test/test_states.py b/cherrypy/test/test_states.py
index 63226875..63635e52 100644
--- a/cherrypy/test/test_states.py
+++ b/cherrypy/test/test_states.py
@@ -1,10 +1,15 @@
from cherrypy._cpcompat import BadStatusLine, ntob
import os
import sys
-import threading
import time
+import signal
+import unittest
+import socket
import cherrypy
+import cherrypy.process.servers
+from cherrypy.test import helper
+
engine = cherrypy.engine
thisdir = os.path.join(os.getcwd(), os.path.dirname(__file__))
@@ -43,8 +48,10 @@ class Dependency:
db_connection = Dependency(engine)
+
def setup_server():
class Root:
+
def index(self):
return "Hello World"
index.exposed = True
@@ -75,17 +82,13 @@ def setup_server():
cherrypy.config.update({
'environment': 'test_suite',
'engine.deadlock_poll_freq': 0.1,
- })
+ })
db_connection.subscribe()
-
-
# ------------ Enough helpers. Time for real live test cases. ------------ #
-from cherrypy.test import helper
-
class ServerStateTests(helper.CPWebCase):
setup_server = staticmethod(setup_server)
@@ -246,9 +249,8 @@ class ServerStateTests(helper.CPWebCase):
def test_4_Autoreload(self):
# Start the demo script in a new process
- p = helper.CPProcess(ssl=(self.scheme.lower()=='https'))
- p.write_conf(
- extra='test_case_name: "test_4_Autoreload"')
+ p = helper.CPProcess(ssl=(self.scheme.lower() == 'https'))
+ p.write_conf(extra='test_case_name: "test_4_Autoreload"')
p.start(imports='cherrypy.test._test_states_demo')
try:
self.getPage("/start")
@@ -278,10 +280,10 @@ class ServerStateTests(helper.CPWebCase):
def test_5_Start_Error(self):
# If a process errors during start, it should stop the engine
# and exit with a non-zero exit code.
- p = helper.CPProcess(ssl=(self.scheme.lower()=='https'),
+ p = helper.CPProcess(ssl=(self.scheme.lower() == 'https'),
wait=True)
p.write_conf(
- extra="""starterror: True
+ extra="""starterror: True
test_case_name: "test_5_Start_Error"
"""
)
@@ -291,6 +293,7 @@ test_case_name: "test_5_Start_Error"
class PluginTests(helper.CPWebCase):
+
def test_daemonize(self):
if os.name not in ['posix']:
return self.skip("skipped (not on posix) ")
@@ -299,12 +302,12 @@ class PluginTests(helper.CPWebCase):
# Spawn the process and wait, when this returns, the original process
# is finished. If it daemonized properly, we should still be able
# to access pages.
- p = helper.CPProcess(ssl=(self.scheme.lower()=='https'),
+ p = helper.CPProcess(ssl=(self.scheme.lower() == 'https'),
wait=True, daemonize=True,
socket_host='127.0.0.1',
socket_port=8081)
p.write_conf(
- extra='test_case_name: "test_daemonize"')
+ extra='test_case_name: "test_daemonize"')
p.start(imports='cherrypy.test._test_states_demo')
try:
# Just get the pid of the daemonization process.
@@ -324,6 +327,7 @@ class PluginTests(helper.CPWebCase):
class SignalHandlingTests(helper.CPWebCase):
+
def test_SIGHUP_tty(self):
# When not daemonized, SIGHUP should shut down the server.
try:
@@ -332,9 +336,9 @@ class SignalHandlingTests(helper.CPWebCase):
return self.skip("skipped (no SIGHUP) ")
# Spawn the process.
- p = helper.CPProcess(ssl=(self.scheme.lower()=='https'))
+ p = helper.CPProcess(ssl=(self.scheme.lower() == 'https'))
p.write_conf(
- extra='test_case_name: "test_SIGHUP_tty"')
+ extra='test_case_name: "test_SIGHUP_tty"')
p.start(imports='cherrypy.test._test_states_demo')
# Send a SIGHUP
os.kill(p.get_pid(), SIGHUP)
@@ -354,10 +358,10 @@ class SignalHandlingTests(helper.CPWebCase):
# Spawn the process and wait, when this returns, the original process
# is finished. If it daemonized properly, we should still be able
# to access pages.
- p = helper.CPProcess(ssl=(self.scheme.lower()=='https'),
+ p = helper.CPProcess(ssl=(self.scheme.lower() == 'https'),
wait=True, daemonize=True)
p.write_conf(
- extra='test_case_name: "test_SIGHUP_daemonized"')
+ extra='test_case_name: "test_SIGHUP_daemonized"')
p.start(imports='cherrypy.test._test_states_demo')
pid = p.get_pid()
@@ -375,60 +379,58 @@ class SignalHandlingTests(helper.CPWebCase):
self.getPage("/exit")
p.join()
- def test_SIGTERM(self):
- # SIGTERM should shut down the server whether daemonized or not.
- try:
- from signal import SIGTERM
- except ImportError:
- return self.skip("skipped (no SIGTERM) ")
+ def _require_signal_and_kill(self, signal_name):
+ if not hasattr(signal, signal_name):
+ self.skip("skipped (no %(signal_name)s)" % vars())
- try:
- from os import kill
- except ImportError:
- return self.skip("skipped (no os.kill) ")
+ if not hasattr(os, 'kill'):
+ self.skip("skipped (no os.kill)")
+
+ def test_SIGTERM(self):
+ "SIGTERM should shut down the server whether daemonized or not."
+ self._require_signal_and_kill('SIGTERM')
# Spawn a normal, undaemonized process.
- p = helper.CPProcess(ssl=(self.scheme.lower()=='https'))
+ p = helper.CPProcess(ssl=(self.scheme.lower() == 'https'))
p.write_conf(
- extra='test_case_name: "test_SIGTERM"')
+ extra='test_case_name: "test_SIGTERM"')
p.start(imports='cherrypy.test._test_states_demo')
# Send a SIGTERM
- os.kill(p.get_pid(), SIGTERM)
+ os.kill(p.get_pid(), signal.SIGTERM)
# This might hang if things aren't working right, but meh.
p.join()
if os.name in ['posix']:
# Spawn a daemonized process and test again.
- p = helper.CPProcess(ssl=(self.scheme.lower()=='https'),
+ p = helper.CPProcess(ssl=(self.scheme.lower() == 'https'),
wait=True, daemonize=True)
p.write_conf(
- extra='test_case_name: "test_SIGTERM_2"')
+ extra='test_case_name: "test_SIGTERM_2"')
p.start(imports='cherrypy.test._test_states_demo')
# Send a SIGTERM
- os.kill(p.get_pid(), SIGTERM)
+ os.kill(p.get_pid(), signal.SIGTERM)
# This might hang if things aren't working right, but meh.
p.join()
def test_signal_handler_unsubscribe(self):
- try:
- from signal import SIGTERM
- except ImportError:
- return self.skip("skipped (no SIGTERM) ")
+ self._require_signal_and_kill('SIGTERM')
- try:
- from os import kill
- except ImportError:
- return self.skip("skipped (no os.kill) ")
+ # Although Windows has `os.kill` and SIGTERM is defined, the
+ # platform does not implement signals and sending SIGTERM
+ # will result in a forced termination of the process.
+ # Therefore, this test is not suitable for Windows.
+ if os.name == 'nt':
+ self.skip("SIGTERM not available")
# Spawn a normal, undaemonized process.
- p = helper.CPProcess(ssl=(self.scheme.lower()=='https'))
+ p = helper.CPProcess(ssl=(self.scheme.lower() == 'https'))
p.write_conf(
extra="""unsubsig: True
test_case_name: "test_signal_handler_unsubscribe"
""")
p.start(imports='cherrypy.test._test_states_demo')
- # Send a SIGTERM
- os.kill(p.get_pid(), SIGTERM)
+ # Ask the process to quit
+ os.kill(p.get_pid(), signal.SIGTERM)
# This might hang if things aren't working right, but meh.
p.join()
@@ -437,3 +439,56 @@ test_case_name: "test_signal_handler_unsubscribe"
if not ntob("I am an old SIGTERM handler.") in target_line:
self.fail("Old SIGTERM handler did not run.\n%r" % target_line)
+
+class WaitTests(unittest.TestCase):
+
+ def test_wait_for_occupied_port_INADDR_ANY(self):
+ """
+ Wait on INADDR_ANY should not raise IOError
+
+ In cases where the loopback interface does not exist, CherryPy cannot
+ effectively determine if a port binding to INADDR_ANY was effected.
+ In this situation, CherryPy should assume that it failed to detect
+ the binding (not that the binding failed) and only warn that it could
+ not verify it.
+ """
+ # At such a time that CherryPy can reliably determine one or more
+ # viable IP addresses of the host, this test may be removed.
+
+ # Simulate the behavior we observe when no loopback interface is
+ # present by: finding a port that's not occupied, then wait on it.
+
+ free_port = self.find_free_port()
+
+ servers = cherrypy.process.servers
+
+ def with_shorter_timeouts(func):
+ """
+ A context where occupied_port_timeout is much smaller to speed
+ test runs.
+ """
+ # When we have Python 2.5, simplify using the with_statement.
+ orig_timeout = servers.occupied_port_timeout
+ servers.occupied_port_timeout = .07
+ try:
+ func()
+ finally:
+ servers.occupied_port_timeout = orig_timeout
+
+ def do_waiting():
+ # Wait on the free port that's unbound
+ servers.wait_for_occupied_port('0.0.0.0', free_port)
+ # The wait should still raise an IO error if INADDR_ANY was
+ # not supplied.
+ self.assertRaises(IOError, servers.wait_for_occupied_port,
+ '127.0.0.1', free_port)
+
+ with_shorter_timeouts(do_waiting)
+
+ def find_free_port(self):
+ "Find a free port by binding to port 0 then unbinding."
+ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ sock.bind(('', 0))
+ free_port = sock.getsockname()[1]
+ sock.close()
+ return free_port
diff --git a/cherrypy/test/test_static.py b/cherrypy/test/test_static.py
index 871420bd..21bc635d 100644
--- a/cherrypy/test/test_static.py
+++ b/cherrypy/test/test_static.py
@@ -1,12 +1,13 @@
+import os
+import sys
+
from cherrypy._cpcompat import HTTPConnection, HTTPSConnection, ntob
from cherrypy._cpcompat import BytesIO
-import os
curdir = os.path.join(os.getcwd(), os.path.dirname(__file__))
has_space_filepath = os.path.join(curdir, 'static', 'has space.html')
bigfile_filepath = os.path.join(curdir, "static", "bigfile.log")
BIGFILE_SIZE = 1024 * 1024
-import threading
import cherrypy
from cherrypy.lib import static
@@ -20,46 +21,45 @@ class StaticTest(helper.CPWebCase):
open(has_space_filepath, 'wb').write(ntob('Hello, world\r\n'))
if not os.path.exists(bigfile_filepath):
open(bigfile_filepath, 'wb').write(ntob("x" * BIGFILE_SIZE))
-
+
class Root:
-
+
def bigfile(self):
from cherrypy.lib import static
self.f = static.serve_file(bigfile_filepath)
return self.f
bigfile.exposed = True
bigfile._cp_config = {'response.stream': True}
-
+
def tell(self):
if self.f.input.closed:
return ''
return repr(self.f.input.tell()).rstrip('L')
tell.exposed = True
-
+
def fileobj(self):
f = open(os.path.join(curdir, 'style.css'), 'rb')
return static.serve_fileobj(f, content_type='text/css')
fileobj.exposed = True
-
+
def bytesio(self):
f = BytesIO(ntob('Fee\nfie\nfo\nfum'))
return static.serve_fileobj(f, content_type='text/plain')
bytesio.exposed = True
-
+
class Static:
-
+
def index(self):
return 'You want the Baron? You can have the Baron!'
index.exposed = True
-
+
def dynamic(self):
return "This is a DYNAMIC page"
dynamic.exposed = True
-
-
+
root = Root()
root.static = Static()
-
+
rootconf = {
'/static': {
'tools.staticdir.on': True,
@@ -80,26 +80,25 @@ class StaticTest(helper.CPWebCase):
'tools.staticdir.on': True,
'request.show_tracebacks': True,
},
- }
+ }
rootApp = cherrypy.Application(root)
rootApp.merge(rootconf)
-
+
test_app_conf = {
'/test': {
'tools.staticdir.index': 'index.html',
'tools.staticdir.on': True,
'tools.staticdir.root': curdir,
'tools.staticdir.dir': 'static',
- },
- }
+ },
+ }
testApp = cherrypy.Application(Static())
testApp.merge(test_app_conf)
-
+
vhost = cherrypy._cpwsgi.VirtualHost(rootApp, {'virt.net': testApp})
cherrypy.tree.graft(vhost)
setup_server = staticmethod(setup_server)
-
def teardown_server():
for f in (has_space_filepath, bigfile_filepath):
if os.path.exists(f):
@@ -109,25 +108,24 @@ class StaticTest(helper.CPWebCase):
pass
teardown_server = staticmethod(teardown_server)
-
def testStatic(self):
self.getPage("/static/index.html")
self.assertStatus('200 OK')
self.assertHeader('Content-Type', 'text/html')
self.assertBody('Hello, world\r\n')
-
+
# Using a staticdir.root value in a subdir...
self.getPage("/docroot/index.html")
self.assertStatus('200 OK')
self.assertHeader('Content-Type', 'text/html')
self.assertBody('Hello, world\r\n')
-
+
# Check a filename with spaces in it
self.getPage("/static/has%20space.html")
self.assertStatus('200 OK')
self.assertHeader('Content-Type', 'text/html')
self.assertBody('Hello, world\r\n')
-
+
self.getPage("/style.css")
self.assertStatus('200 OK')
self.assertHeader('Content-Type', 'text/css')
@@ -136,18 +134,18 @@ class StaticTest(helper.CPWebCase):
# into \r\n on Windows when extracting the CherryPy tarball so
# we just check the content
self.assertMatchesBody('^Dummy stylesheet')
-
+
def test_fallthrough(self):
# Test that NotFound will then try dynamic handlers (see [878]).
self.getPage("/static/dynamic")
self.assertBody("This is a DYNAMIC page")
-
+
# Check a directory via fall-through to dynamic handler.
self.getPage("/static/")
self.assertStatus('200 OK')
self.assertHeader('Content-Type', 'text/html;charset=utf-8')
self.assertBody('You want the Baron? You can have the Baron!')
-
+
def test_index(self):
# Check a directory via "staticdir.index".
self.getPage("/docroot/")
@@ -160,19 +158,24 @@ class StaticTest(helper.CPWebCase):
self.assertHeader('Location', '%s/docroot/' % self.base())
self.assertMatchesBody("This resource .* <a href='%s/docroot/'>"
"%s/docroot/</a>." % (self.base(), self.base()))
-
+
def test_config_errors(self):
# Check that we get an error if no .file or .dir
self.getPage("/error/thing.html")
self.assertErrorPage(500)
- self.assertMatchesBody(ntob("TypeError: staticdir\(\) takes at least 2 "
- "(positional )?arguments \(0 given\)"))
-
+ if sys.version_info >= (3, 3):
+ errmsg = ntob("TypeError: staticdir\(\) missing 2 "
+ "required positional arguments")
+ else:
+ errmsg = ntob("TypeError: staticdir\(\) takes at least 2 "
+ "(positional )?arguments \(0 given\)")
+ self.assertMatchesBody(errmsg)
+
def test_security(self):
# Test up-level security
self.getPage("/static/../../test/style.css")
self.assertStatus((400, 403))
-
+
def test_modif(self):
# Test modified-since on a reasonably-large file
self.getPage("/static/dirback.jpg")
@@ -188,33 +191,33 @@ class StaticTest(helper.CPWebCase):
self.assertNoHeader("Content-Length")
self.assertNoHeader("Content-Disposition")
self.assertBody("")
-
+
def test_755_vhost(self):
self.getPage("/test/", [('Host', 'virt.net')])
self.assertStatus(200)
self.getPage("/test", [('Host', 'virt.net')])
self.assertStatus(301)
self.assertHeader('Location', self.scheme + '://virt.net/test/')
-
+
def test_serve_fileobj(self):
self.getPage("/fileobj")
self.assertStatus('200 OK')
self.assertHeader('Content-Type', 'text/css;charset=utf-8')
self.assertMatchesBody('^Dummy stylesheet')
-
+
def test_serve_bytesio(self):
self.getPage("/bytesio")
self.assertStatus('200 OK')
self.assertHeader('Content-Type', 'text/plain;charset=utf-8')
self.assertHeader('Content-Length', 14)
self.assertMatchesBody('Fee\nfie\nfo\nfum')
-
+
def test_file_stream(self):
if cherrypy.server.protocol_version != "HTTP/1.1":
return self.skip()
-
+
self.PROTOCOL = "HTTP/1.1"
-
+
# Make an initial request
self.persistent = True
conn = self.HTTP_CONN
@@ -224,7 +227,7 @@ class StaticTest(helper.CPWebCase):
response = conn.response_class(conn.sock, method="GET")
response.begin()
self.assertEqual(response.status, 200)
-
+
body = ntob('')
remaining = BIGFILE_SIZE
while remaining > 0:
@@ -233,7 +236,7 @@ class StaticTest(helper.CPWebCase):
break
body += data
remaining -= len(data)
-
+
if self.scheme == "https":
newconn = HTTPSConnection
else:
@@ -246,35 +249,36 @@ class StaticTest(helper.CPWebCase):
tell_position = BIGFILE_SIZE
else:
tell_position = int(b)
-
+
expected = len(body)
if tell_position >= BIGFILE_SIZE:
- # We can't exactly control how much content the server asks for.
+ # We can't exactly control how much content the server asks
+ # for.
# Fudge it by only checking the first half of the reads.
if expected < (BIGFILE_SIZE / 2):
self.fail(
- "The file should have advanced to position %r, but has "
- "already advanced to the end of the file. It may not be "
- "streamed as intended, or at the wrong chunk size (64k)" %
- expected)
+ "The file should have advanced to position %r, but "
+ "has already advanced to the end of the file. It "
+ "may not be streamed as intended, or at the wrong "
+ "chunk size (64k)" % expected)
elif tell_position < expected:
self.fail(
"The file should have advanced to position %r, but has "
"only advanced to position %r. It may not be streamed "
"as intended, or at the wrong chunk size (65536)" %
(expected, tell_position))
-
+
if body != ntob("x" * BIGFILE_SIZE):
self.fail("Body != 'x' * %d. Got %r instead (%d bytes)." %
(BIGFILE_SIZE, body[:50], len(body)))
conn.close()
-
+
def test_file_stream_deadlock(self):
if cherrypy.server.protocol_version != "HTTP/1.1":
return self.skip()
-
+
self.PROTOCOL = "HTTP/1.1"
-
+
# Make an initial request but abort early.
self.persistent = True
conn = self.HTTP_CONN
@@ -290,11 +294,10 @@ class StaticTest(helper.CPWebCase):
(65536, body[:50], len(body)))
response.close()
conn.close()
-
+
# Make a second request, which should fetch the whole file.
self.persistent = False
self.getPage("/bigfile")
if self.body != ntob("x" * BIGFILE_SIZE):
self.fail("Body != 'x' * %d. Got %r instead (%d bytes)." %
(BIGFILE_SIZE, self.body[:50], len(body)))
-
diff --git a/cherrypy/test/test_tools.py b/cherrypy/test/test_tools.py
index 02bacda6..d650ca5e 100644
--- a/cherrypy/test/test_tools.py
+++ b/cherrypy/test/test_tools.py
@@ -2,8 +2,10 @@
import gzip
import sys
+import unittest
from cherrypy._cpcompat import BytesIO, copyitems, itervalues
from cherrypy._cpcompat import IncompleteRead, ntob, ntou, py3k, xrange
+from cherrypy._cpcompat import bytestr, unicodestr
import time
timeout = 0.2
import types
@@ -21,16 +23,18 @@ from cherrypy.test import helper
class ToolTests(helper.CPWebCase):
+
def setup_server():
-
+
# Put check_access in a custom toolbox with its own namespace
myauthtools = cherrypy._cptools.Toolbox("myauth")
-
+
def check_access(default=False):
if not getattr(cherrypy.request, "userid", default):
raise cherrypy.HTTPError(401)
- myauthtools.check_access = cherrypy.Tool('before_request_body', check_access)
-
+ myauthtools.check_access = cherrypy.Tool(
+ 'before_request_body', check_access)
+
def numerify():
def number_it(body):
for chunk in body:
@@ -38,30 +42,32 @@ class ToolTests(helper.CPWebCase):
chunk = chunk.replace(k, v)
yield chunk
cherrypy.response.body = number_it(cherrypy.response.body)
-
+
class NumTool(cherrypy.Tool):
+
def _setup(self):
def makemap():
m = self._merged_args().get("map", {})
cherrypy.request.numerify_map = copyitems(m)
cherrypy.request.hooks.attach('on_start_resource', makemap)
-
+
def critical():
- cherrypy.request.error_response = cherrypy.HTTPError(502).set_response
+ cherrypy.request.error_response = cherrypy.HTTPError(
+ 502).set_response
critical.failsafe = True
-
+
cherrypy.request.hooks.attach('on_start_resource', critical)
cherrypy.request.hooks.attach(self._point, self.callable)
-
+
tools.numerify = NumTool('before_finalize', numerify)
-
+
# It's not mandatory to inherit from cherrypy.Tool.
class NadsatTool:
-
+
def __init__(self):
self.ended = {}
self._name = "nadsat"
-
+
def nadsat(self):
def nadsat_it_up(body):
for chunk in body:
@@ -70,7 +76,7 @@ class ToolTests(helper.CPWebCase):
yield chunk
cherrypy.response.body = nadsat_it_up(cherrypy.response.body)
nadsat.priority = 0
-
+
def cleanup(self):
# This runs after the request has been completely written out.
cherrypy.response.body = [ntob("razdrez")]
@@ -78,19 +84,20 @@ class ToolTests(helper.CPWebCase):
if id:
self.ended[id] = True
cleanup.failsafe = True
-
+
def _setup(self):
cherrypy.request.hooks.attach('before_finalize', self.nadsat)
cherrypy.request.hooks.attach('on_end_request', self.cleanup)
tools.nadsat = NadsatTool()
-
+
def pipe_body():
cherrypy.request.process_request_body = False
clen = int(cherrypy.request.headers['Content-Length'])
cherrypy.request.body = cherrypy.request.rfile.read(clen)
-
+
# Assert that we can use a callable object instead of a function.
class Rotator(object):
+
def __call__(self, scale):
r = cherrypy.response
r.collapse_body()
@@ -99,28 +106,31 @@ class ToolTests(helper.CPWebCase):
else:
r.body = [chr((ord(x) + scale) % 256) for x in r.body[0]]
cherrypy.tools.rotator = cherrypy.Tool('before_finalize', Rotator())
-
+
def stream_handler(next_handler, *args, **kwargs):
cherrypy.response.output = o = BytesIO()
try:
response = next_handler(*args, **kwargs)
- # Ignore the response and return our accumulated output instead.
+ # Ignore the response and return our accumulated output
+ # instead.
return o.getvalue()
finally:
o.close()
- cherrypy.tools.streamer = cherrypy._cptools.HandlerWrapperTool(stream_handler)
-
+ cherrypy.tools.streamer = cherrypy._cptools.HandlerWrapperTool(
+ stream_handler)
+
class Root:
+
def index(self):
return "Howdy earth!"
index.exposed = True
-
+
def tarfile(self):
cherrypy.response.output.write(ntob('I am '))
cherrypy.response.output.write(ntob('a tarfile'))
tarfile.exposed = True
tarfile._cp_config = {'tools.streamer.on': True}
-
+
def euro(self):
hooks = list(cherrypy.request.hooks['before_finalize'])
hooks.sort()
@@ -132,13 +142,13 @@ class ToolTests(helper.CPWebCase):
yield ntou("world")
yield europoundUnicode
euro.exposed = True
-
+
# Bare hooks
def pipe(self):
return cherrypy.request.body
pipe.exposed = True
pipe._cp_config = {'hooks.before_request_body': pipe_body}
-
+
# Multiple decorators; include kwargs just for fun.
# Note that rotator must run before gzip.
def decorated_euro(self, *vpath):
@@ -148,13 +158,13 @@ class ToolTests(helper.CPWebCase):
decorated_euro.exposed = True
decorated_euro = tools.gzip(compress_level=6)(decorated_euro)
decorated_euro = tools.rotator(scale=3)(decorated_euro)
-
+
root = Root()
-
-
+
class TestType(type):
- """Metaclass which automatically exposes all functions in each subclass,
- and adds an instance of the subclass as an attribute of root.
+ """Metaclass which automatically exposes all functions in each
+ subclass, and adds an instance of the subclass as an attribute
+ of root.
"""
def __init__(cls, name, bases, dct):
type.__init__(cls, name, bases, dct)
@@ -163,45 +173,44 @@ class ToolTests(helper.CPWebCase):
value.exposed = True
setattr(root, name.lower(), cls())
Test = TestType('Test', (object,), {})
-
-
+
# METHOD ONE:
# Declare Tools in _cp_config
class Demo(Test):
-
+
_cp_config = {"tools.nadsat.on": True}
-
+
def index(self, id=None):
return "A good piece of cherry pie"
-
+
def ended(self, id):
return repr(tools.nadsat.ended[id])
-
+
def err(self, id=None):
raise ValueError()
-
+
def errinstream(self, id=None):
yield "nonconfidential"
raise ValueError()
yield "confidential"
-
+
# METHOD TWO: decorator using Tool()
- # We support Python 2.3, but the @-deco syntax would look like this:
+ # We support Python 2.3, but the @-deco syntax would look like
+ # this:
# @tools.check_access()
def restricted(self):
return "Welcome!"
restricted = myauthtools.check_access()(restricted)
userid = restricted
-
+
def err_in_onstart(self):
return "success!"
-
+
def stream(self, id=None):
for x in xrange(100000000):
yield str(x)
stream._cp_config = {'response.stream': True}
-
-
+
conf = {
# METHOD THREE:
# Declare Tools in detached config
@@ -237,7 +246,7 @@ class ToolTests(helper.CPWebCase):
}
app = cherrypy.tree.mount(root, config=conf)
app.request_class.namespaces['myauth'] = myauthtools
-
+
if sys.version_info >= (2, 5):
from cherrypy.test import _test_decorators
root.tooldecs = _test_decorators.ToolExamples()
@@ -251,7 +260,7 @@ class ToolTests(helper.CPWebCase):
time.sleep(0.1)
self.getPage("/demo/ended/1")
self.assertBody("True")
-
+
valerr = '\n raise ValueError()\nValueError'
self.getPage("/demo/err?id=3")
# If body is "razdrez", then on_end_request is being called too early.
@@ -260,10 +269,10 @@ class ToolTests(helper.CPWebCase):
time.sleep(0.1)
self.getPage("/demo/ended/3")
self.assertBody("True")
-
+
# If body is "razdrez", then on_end_request is being called too early.
if (cherrypy.server.protocol_version == "HTTP/1.0" or
- getattr(cherrypy.server, "using_apache", False)):
+ getattr(cherrypy.server, "using_apache", False)):
self.getPage("/demo/errinstream?id=5")
# Because this error is raised after the response body has
# started, the status should not change to an error status.
@@ -279,15 +288,15 @@ class ToolTests(helper.CPWebCase):
time.sleep(0.1)
self.getPage("/demo/ended/5")
self.assertBody("True")
-
+
# Test the "__call__" technique (compile-time decorator).
self.getPage("/demo/restricted")
self.assertErrorPage(401)
-
+
# Test compile-time decorator with kwargs from config.
self.getPage("/demo/userid")
self.assertBody("Welcome!")
-
+
def testEndRequestOnDrop(self):
old_timeout = None
try:
@@ -295,10 +304,10 @@ class ToolTests(helper.CPWebCase):
old_timeout = httpserver.timeout
except (AttributeError, IndexError):
return self.skip()
-
+
try:
httpserver.timeout = timeout
-
+
# Test that on_end_request is called even if the client drops.
self.persistent = True
try:
@@ -320,7 +329,7 @@ class ToolTests(helper.CPWebCase):
finally:
if old_timeout is not None:
httpserver.timeout = old_timeout
-
+
def testGuaranteedHooks(self):
# The 'critical' on_start_resource hook is 'failsafe' (guaranteed
# to run even if there are failures in other on_start methods).
@@ -329,27 +338,31 @@ class ToolTests(helper.CPWebCase):
# but our 'critical' hook should run and set the error to 502.
self.getPage("/demo/err_in_onstart")
self.assertErrorPage(502)
- self.assertInBody("AttributeError: 'str' object has no attribute 'items'")
-
+ self.assertInBody(
+ "AttributeError: 'str' object has no attribute 'items'")
+
def testCombinedTools(self):
- expectedResult = (ntou("Hello,world") + europoundUnicode).encode('utf-8')
+ expectedResult = (ntou("Hello,world") +
+ europoundUnicode).encode('utf-8')
zbuf = BytesIO()
zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=9)
zfile.write(expectedResult)
zfile.close()
-
- self.getPage("/euro", headers=[("Accept-Encoding", "gzip"),
- ("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7")])
+
+ self.getPage("/euro",
+ headers=[
+ ("Accept-Encoding", "gzip"),
+ ("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7")])
self.assertInBody(zbuf.getvalue()[:3])
-
+
zbuf = BytesIO()
zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=6)
zfile.write(expectedResult)
zfile.close()
-
+
self.getPage("/decorated_euro", headers=[("Accept-Encoding", "gzip")])
self.assertInBody(zbuf.getvalue()[:3])
-
+
# This returns a different value because gzip's priority was
# lowered in conf, allowing the rotator to run after gzip.
# Of course, we don't want breakage in production apps,
@@ -359,8 +372,9 @@ class ToolTests(helper.CPWebCase):
if py3k:
self.assertInBody(bytes([(x + 3) % 256 for x in zbuf.getvalue()]))
else:
- self.assertInBody(''.join([chr((ord(x) + 3) % 256) for x in zbuf.getvalue()]))
-
+ self.assertInBody(''.join([chr((ord(x) + 3) % 256)
+ for x in zbuf.getvalue()]))
+
def testBareHooks(self):
content = "bit of a pain in me gulliver"
self.getPage("/pipe",
@@ -368,27 +382,27 @@ class ToolTests(helper.CPWebCase):
("Content-Type", "text/plain")],
method="POST", body=content)
self.assertBody(content)
-
+
def testHandlerWrapperTool(self):
self.getPage("/tarfile")
self.assertBody("I am a tarfile")
-
+
def testToolWithConfig(self):
if not sys.version_info >= (2, 5):
return self.skip("skipped (Python 2.5+ only)")
-
+
self.getPage('/tooldecs/blah')
self.assertHeader('Content-Type', 'application/data')
-
+
def testWarnToolOn(self):
# get
try:
- numon = cherrypy.tools.numerify.on
+ cherrypy.tools.numerify.on
except AttributeError:
pass
else:
raise AssertionError("Tool.on did not error as it should have.")
-
+
# set
try:
cherrypy.tools.numerify.on = True
@@ -397,3 +411,16 @@ class ToolTests(helper.CPWebCase):
else:
raise AssertionError("Tool.on did not error as it should have.")
+
+class SessionAuthTest(unittest.TestCase):
+
+ def test_login_screen_returns_bytes(self):
+ """
+ login_screen must return bytes even if unicode parameters are passed.
+ Issue 1132 revealed that login_screen would return unicode if the
+ username and password were unicode.
+ """
+ sa = cherrypy.lib.cptools.SessionAuth()
+ res = sa.login_screen(None, username=unicodestr('nobody'),
+ password=unicodestr('anypass'))
+ self.assertIsInstance(res, bytestr)
diff --git a/cherrypy/test/test_tutorials.py b/cherrypy/test/test_tutorials.py
index aab27861..ad89a9ea 100644
--- a/cherrypy/test/test_tutorials.py
+++ b/cherrypy/test/test_tutorials.py
@@ -7,14 +7,14 @@ from cherrypy.test import helper
class TutorialTest(helper.CPWebCase):
def setup_server(cls):
-
+
conf = cherrypy.config.copy()
-
+
def load_tut_module(name):
"""Import or reload tutorial module as needed."""
cherrypy.config.reset()
cherrypy.config.update(conf)
-
+
target = "cherrypy.tutorial." + name
if target in sys.modules:
module = reload(sys.modules[target])
@@ -22,22 +22,22 @@ class TutorialTest(helper.CPWebCase):
module = __import__(target, globals(), locals(), [''])
# The above import will probably mount a new app at "".
app = cherrypy.tree.apps[""]
-
+
app.root.load_tut_module = load_tut_module
app.root.sessions = sessions
app.root.traceback_setting = traceback_setting
-
+
cls.supervisor.sync_apps()
load_tut_module.exposed = True
-
+
def sessions():
cherrypy.config.update({"tools.sessions.on": True})
sessions.exposed = True
-
+
def traceback_setting():
return repr(cherrypy.request.show_tracebacks)
traceback_setting.exposed = True
-
+
class Dummy:
pass
root = Dummy()
@@ -45,51 +45,50 @@ class TutorialTest(helper.CPWebCase):
cherrypy.tree.mount(root)
setup_server = classmethod(setup_server)
-
def test01HelloWorld(self):
self.getPage("/load_tut_module/tut01_helloworld")
self.getPage("/")
self.assertBody('Hello world!')
-
+
def test02ExposeMethods(self):
self.getPage("/load_tut_module/tut02_expose_methods")
- self.getPage("/showMessage")
+ self.getPage("/show_msg")
self.assertBody('Hello world!')
-
+
def test03GetAndPost(self):
self.getPage("/load_tut_module/tut03_get_and_post")
-
+
# Try different GET queries
self.getPage("/greetUser?name=Bob")
self.assertBody("Hey Bob, what's up?")
-
+
self.getPage("/greetUser")
self.assertBody('Please enter your name <a href="./">here</a>.')
-
+
self.getPage("/greetUser?name=")
self.assertBody('No, really, enter your name <a href="./">here</a>.')
-
+
# Try the same with POST
self.getPage("/greetUser", method="POST", body="name=Bob")
self.assertBody("Hey Bob, what's up?")
-
+
self.getPage("/greetUser", method="POST", body="name=")
self.assertBody('No, really, enter your name <a href="./">here</a>.')
-
+
def test04ComplexSite(self):
self.getPage("/load_tut_module/tut04_complex_site")
msg = '''
<p>Here are some extra useful links:</p>
-
+
<ul>
<li><a href="http://del.icio.us">del.icio.us</a></li>
- <li><a href="http://www.mornography.de">Hendrik's weblog</a></li>
+ <li><a href="http://www.cherrypy.org">CherryPy</a></li>
</ul>
-
+
<p>[<a href="../">Return to links page</a>]</p>'''
self.getPage("/links/extra/")
self.assertBody(msg)
-
+
def test05DerivedObjects(self):
self.getPage("/load_tut_module/tut05_derived_objects")
msg = '''
@@ -99,58 +98,63 @@ class TutorialTest(helper.CPWebCase):
<head>
<body>
<h2>Another Page</h2>
-
+
<p>
And this is the amazing second page!
</p>
-
+
</body>
</html>
'''
+ # the tutorial has some annoying spaces in otherwise blank lines
+ msg = msg.replace('</h2>\n\n', '</h2>\n \n')
+ msg = msg.replace('</p>\n\n', '</p>\n \n')
self.getPage("/another/")
self.assertBody(msg)
-
+
def test06DefaultMethod(self):
self.getPage("/load_tut_module/tut06_default_method")
self.getPage('/hendrik')
self.assertBody('Hendrik Mans, CherryPy co-developer & crazy German '
'(<a href="./">back</a>)')
-
+
def test07Sessions(self):
self.getPage("/load_tut_module/tut07_sessions")
self.getPage("/sessions")
-
+
self.getPage('/')
- self.assertBody("\n During your current session, you've viewed this"
- "\n page 1 times! Your life is a patio of fun!"
- "\n ")
-
+ self.assertBody(
+ "\n During your current session, you've viewed this"
+ "\n page 1 times! Your life is a patio of fun!"
+ "\n ")
+
self.getPage('/', self.cookies)
- self.assertBody("\n During your current session, you've viewed this"
- "\n page 2 times! Your life is a patio of fun!"
- "\n ")
-
+ self.assertBody(
+ "\n During your current session, you've viewed this"
+ "\n page 2 times! Your life is a patio of fun!"
+ "\n ")
+
def test08GeneratorsAndYield(self):
self.getPage("/load_tut_module/tut08_generators_and_yield")
self.getPage('/')
self.assertBody('<html><body><h2>Generators rule!</h2>'
- '<h3>List of users:</h3>'
- 'Remi<br/>Carlos<br/>Hendrik<br/>Lorenzo Lamas<br/>'
- '</body></html>')
-
+ '<h3>List of users:</h3>'
+ 'Remi<br/>Carlos<br/>Hendrik<br/>Lorenzo Lamas<br/>'
+ '</body></html>')
+
def test09Files(self):
self.getPage("/load_tut_module/tut09_files")
-
+
# Test upload
filesize = 5
h = [("Content-type", "multipart/form-data; boundary=x"),
("Content-Length", str(105 + filesize))]
- b = '--x\n' + \
- 'Content-Disposition: form-data; name="myFile"; filename="hello.txt"\r\n' + \
- 'Content-Type: text/plain\r\n' + \
- '\r\n' + \
- 'a' * filesize + '\n' + \
- '--x--\n'
+ b = ('--x\n'
+ 'Content-Disposition: form-data; name="myFile"; '
+ 'filename="hello.txt"\r\n'
+ 'Content-Type: text/plain\r\n'
+ '\r\n')
+ b += 'a' * filesize + '\n' + '--x--\n'
self.getPage('/upload', h, "POST", b)
self.assertBody('''<html>
<body>
@@ -159,7 +163,7 @@ class TutorialTest(helper.CPWebCase):
myFile mime-type: text/plain
</body>
</html>''' % filesize)
-
+
# Test download
self.getPage('/download')
self.assertStatus("200 OK")
@@ -168,34 +172,33 @@ class TutorialTest(helper.CPWebCase):
# Make sure the filename is quoted.
'attachment; filename="pdf_file.pdf"')
self.assertEqual(len(self.body), 85698)
-
+
def test10HTTPErrors(self):
self.getPage("/load_tut_module/tut10_http_errors")
-
+
self.getPage("/")
self.assertInBody("""<a href="toggleTracebacks">""")
self.assertInBody("""<a href="/doesNotExist">""")
self.assertInBody("""<a href="/error?code=403">""")
self.assertInBody("""<a href="/error?code=500">""")
self.assertInBody("""<a href="/messageArg">""")
-
+
self.getPage("/traceback_setting")
setting = self.body
self.getPage("/toggleTracebacks")
self.assertStatus((302, 303))
self.getPage("/traceback_setting")
self.assertBody(str(not eval(setting)))
-
+
self.getPage("/error?code=500")
self.assertStatus(500)
self.assertInBody("The server encountered an unexpected condition "
"which prevented it from fulfilling the request.")
-
+
self.getPage("/error?code=403")
self.assertStatus(403)
self.assertInBody("<h2>You can't do that!</h2>")
-
+
self.getPage("/messageArg")
self.assertStatus(500)
self.assertInBody("If you construct an HTTPError with a 'message'")
-
diff --git a/cherrypy/test/test_virtualhost.py b/cherrypy/test/test_virtualhost.py
index dbd2dbcc..7c3d8c71 100644
--- a/cherrypy/test/test_virtualhost.py
+++ b/cherrypy/test/test_virtualhost.py
@@ -9,37 +9,40 @@ class VirtualHostTest(helper.CPWebCase):
def setup_server():
class Root:
+
def index(self):
return "Hello, world"
index.exposed = True
-
+
def dom4(self):
return "Under construction"
dom4.exposed = True
-
+
def method(self, value):
return "You sent %s" % value
method.exposed = True
-
+
class VHost:
+
def __init__(self, sitename):
self.sitename = sitename
-
+
def index(self):
return "Welcome to %s" % self.sitename
index.exposed = True
-
+
def vmethod(self, value):
return "You sent %s" % value
vmethod.exposed = True
-
+
def url(self):
return cherrypy.url("nextpage")
url.exposed = True
-
+
# Test static as a handler (section must NOT include vhost prefix)
- static = cherrypy.tools.staticdir.handler(section='/static', dir=curdir)
-
+ static = cherrypy.tools.staticdir.handler(
+ section='/static', dir=curdir)
+
root = Root()
root.mydom2 = VHost("Domain 2")
root.mydom3 = VHost("Domain 3")
@@ -48,29 +51,32 @@ class VirtualHostTest(helper.CPWebCase):
'www.mydom4.com': '/dom4',
}
cherrypy.tree.mount(root, config={
- '/': {'request.dispatch': cherrypy.dispatch.VirtualHost(**hostmap)},
+ '/': {
+ 'request.dispatch': cherrypy.dispatch.VirtualHost(**hostmap)
+ },
# Test static in config (section must include vhost prefix)
- '/mydom2/static2': {'tools.staticdir.on': True,
- 'tools.staticdir.root': curdir,
- 'tools.staticdir.dir': 'static',
- 'tools.staticdir.index': 'index.html',
- },
- })
+ '/mydom2/static2': {
+ 'tools.staticdir.on': True,
+ 'tools.staticdir.root': curdir,
+ 'tools.staticdir.dir': 'static',
+ 'tools.staticdir.index': 'index.html',
+ },
+ })
setup_server = staticmethod(setup_server)
-
+
def testVirtualHost(self):
self.getPage("/", [('Host', 'www.mydom1.com')])
self.assertBody('Hello, world')
self.getPage("/mydom2/", [('Host', 'www.mydom1.com')])
self.assertBody('Welcome to Domain 2')
-
+
self.getPage("/", [('Host', 'www.mydom2.com')])
self.assertBody('Welcome to Domain 2')
self.getPage("/", [('Host', 'www.mydom3.com')])
self.assertBody('Welcome to Domain 3')
self.getPage("/", [('Host', 'www.mydom4.com')])
self.assertBody('Under construction')
-
+
# Test GET, POST, and positional params
self.getPage("/method?value=root")
self.assertBody("You sent root")
@@ -81,22 +87,22 @@ class VirtualHostTest(helper.CPWebCase):
self.assertBody("You sent dom3 POST")
self.getPage("/vmethod/pos", [('Host', 'www.mydom3.com')])
self.assertBody("You sent pos")
-
+
# Test that cherrypy.url uses the browser url, not the virtual url
self.getPage("/url", [('Host', 'www.mydom2.com')])
self.assertBody("%s://www.mydom2.com/nextpage" % self.scheme)
-
+
def test_VHost_plus_Static(self):
# Test static as a handler
self.getPage("/static/style.css", [('Host', 'www.mydom2.com')])
self.assertStatus('200 OK')
self.assertHeader('Content-Type', 'text/css;charset=utf-8')
-
+
# Test static in config
self.getPage("/static2/dirback.jpg", [('Host', 'www.mydom2.com')])
self.assertStatus('200 OK')
- self.assertHeader('Content-Type', 'image/jpeg')
-
+ self.assertHeaderIn('Content-Type', ['image/jpeg', 'image/pjpeg'])
+
# Test static config with "index" arg
self.getPage("/static2/", [('Host', 'www.mydom2.com')])
self.assertStatus('200 OK')
@@ -104,4 +110,3 @@ class VirtualHostTest(helper.CPWebCase):
# Since tools.trailing_slash is on by default, this should redirect
self.getPage("/static2", [('Host', 'www.mydom2.com')])
self.assertStatus(301)
-
diff --git a/cherrypy/test/test_wsgi_ns.py b/cherrypy/test/test_wsgi_ns.py
index e3c6ce6e..0df2365b 100644
--- a/cherrypy/test/test_wsgi_ns.py
+++ b/cherrypy/test/test_wsgi_ns.py
@@ -6,86 +6,89 @@ from cherrypy.test import helper
class WSGI_Namespace_Test(helper.CPWebCase):
def setup_server():
-
+
class WSGIResponse(object):
-
+
def __init__(self, appresults):
self.appresults = appresults
self.iter = iter(appresults)
-
+
def __iter__(self):
return self
-
+
def next(self):
return self.iter.next()
+
def __next__(self):
return next(self.iter)
-
+
def close(self):
if hasattr(self.appresults, "close"):
self.appresults.close()
-
-
+
class ChangeCase(object):
-
+
def __init__(self, app, to=None):
self.app = app
self.to = to
-
+
def __call__(self, environ, start_response):
res = self.app(environ, start_response)
+
class CaseResults(WSGIResponse):
+
def next(this):
return getattr(this.iter.next(), self.to)()
+
def __next__(this):
return getattr(next(this.iter), self.to)()
return CaseResults(res)
-
+
class Replacer(object):
-
+
def __init__(self, app, map={}):
self.app = app
self.map = map
-
+
def __call__(self, environ, start_response):
res = self.app(environ, start_response)
+
class ReplaceResults(WSGIResponse):
+
def next(this):
line = this.iter.next()
for k, v in self.map.iteritems():
line = line.replace(k, v)
return line
+
def __next__(this):
line = next(this.iter)
for k, v in self.map.items():
line = line.replace(k, v)
return line
return ReplaceResults(res)
-
+
class Root(object):
-
+
def index(self):
return "HellO WoRlD!"
index.exposed = True
-
-
+
root_conf = {'wsgi.pipeline': [('replace', Replacer)],
'wsgi.replace.map': {ntob('L'): ntob('X'),
ntob('l'): ntob('r')},
}
-
+
app = cherrypy.Application(Root())
app.wsgiapp.pipeline.append(('changecase', ChangeCase))
app.wsgiapp.config['changecase'] = {'to': 'upper'}
cherrypy.tree.mount(app, config={'/': root_conf})
setup_server = staticmethod(setup_server)
-
def test_pipeline(self):
if not cherrypy.server.httpserver:
return self.skip()
-
+
self.getPage("/")
# If body is "HEXXO WORXD!", the middleware was applied out of order.
self.assertBody("HERRO WORRD!")
-
diff --git a/cherrypy/test/test_wsgi_vhost.py b/cherrypy/test/test_wsgi_vhost.py
index abb1a917..a36a936c 100644
--- a/cherrypy/test/test_wsgi_vhost.py
+++ b/cherrypy/test/test_wsgi_vhost.py
@@ -5,32 +5,31 @@ from cherrypy.test import helper
class WSGI_VirtualHost_Test(helper.CPWebCase):
def setup_server():
-
+
class ClassOfRoot(object):
-
+
def __init__(self, name):
self.name = name
-
+
def index(self):
return "Welcome to the %s website!" % self.name
index.exposed = True
-
-
+
default = cherrypy.Application(None)
-
+
domains = {}
for year in range(1997, 2008):
app = cherrypy.Application(ClassOfRoot('Class of %s' % year))
domains['www.classof%s.example' % year] = app
-
+
cherrypy.tree.graft(cherrypy._cpwsgi.VirtualHost(default, domains))
setup_server = staticmethod(setup_server)
-
+
def test_welcome(self):
if not cherrypy.server.using_wsgi:
return self.skip("skipped (not using WSGI)... ")
-
+
for year in range(1997, 2008):
- self.getPage("/", headers=[('Host', 'www.classof%s.example' % year)])
+ self.getPage(
+ "/", headers=[('Host', 'www.classof%s.example' % year)])
self.assertBody("Welcome to the Class of %s website!" % year)
-
diff --git a/cherrypy/test/test_wsgiapps.py b/cherrypy/test/test_wsgiapps.py
index d4b8b79a..ac65c1eb 100644
--- a/cherrypy/test/test_wsgiapps.py
+++ b/cherrypy/test/test_wsgiapps.py
@@ -1,3 +1,5 @@
+import sys
+
from cherrypy._cpcompat import ntob
from cherrypy.test import helper
@@ -7,9 +9,9 @@ class WSGIGraftTests(helper.CPWebCase):
def setup_server():
import os
curdir = os.path.join(os.getcwd(), os.path.dirname(__file__))
-
+
import cherrypy
-
+
def test_app(environ, start_response):
status = '200 OK'
response_headers = [('Content-type', 'text/plain')]
@@ -19,64 +21,71 @@ class WSGIGraftTests(helper.CPWebCase):
keys = list(environ.keys())
keys.sort()
for k in keys:
- output.append('%s: %s\n' % (k,environ[k]))
+ output.append('%s: %s\n' % (k, environ[k]))
return [ntob(x, 'utf-8') for x in output]
-
+
def test_empty_string_app(environ, start_response):
status = '200 OK'
response_headers = [('Content-type', 'text/plain')]
start_response(status, response_headers)
- return [ntob('Hello'), ntob(''), ntob(' '), ntob(''), ntob('world')]
-
-
+ return [
+ ntob('Hello'), ntob(''), ntob(' '), ntob(''), ntob('world')
+ ]
+
class WSGIResponse(object):
-
+
def __init__(self, appresults):
self.appresults = appresults
self.iter = iter(appresults)
-
+
def __iter__(self):
return self
-
- def next(self):
- return self.iter.next()
- def __next__(self):
- return next(self.iter)
-
+
+ if sys.version_info >= (3, 0):
+ def __next__(self):
+ return next(self.iter)
+ else:
+ def next(self):
+ return self.iter.next()
+
def close(self):
if hasattr(self.appresults, "close"):
self.appresults.close()
-
-
+
class ReversingMiddleware(object):
-
+
def __init__(self, app):
self.app = app
-
+
def __call__(self, environ, start_response):
results = app(environ, start_response)
+
class Reverser(WSGIResponse):
- def next(this):
- line = list(this.iter.next())
- line.reverse()
- return "".join(line)
- def __next__(this):
- line = list(next(this.iter))
- line.reverse()
- return bytes(line)
+
+ if sys.version_info >= (3, 0):
+ def __next__(this):
+ line = list(next(this.iter))
+ line.reverse()
+ return bytes(line)
+ else:
+ def next(this):
+ line = list(this.iter.next())
+ line.reverse()
+ return "".join(line)
+
return Reverser(results)
-
+
class Root:
+
def index(self):
return ntob("I'm a regular CherryPy page handler!")
index.exposed = True
-
-
+
cherrypy.tree.mount(Root())
-
+
cherrypy.tree.graft(test_app, '/hosted/app1')
cherrypy.tree.graft(test_empty_string_app, '/hosted/app3')
-
+
# Set script_name explicitly to None to signal CP that it should
# be pulled from the WSGI environ each time.
app = cherrypy.Application(Root(), script_name=None)
@@ -89,7 +98,7 @@ This is a wsgi app running within CherryPy!'''
def test_01_standard_app(self):
self.getPage("/")
self.assertBody("I'm a regular CherryPy page handler!")
-
+
def test_04_pure_wsgi(self):
import cherrypy
if not cherrypy.server.using_wsgi:
@@ -115,4 +124,3 @@ This is a wsgi app running within CherryPy!'''
self.getPage("/hosted/app3")
self.assertHeader("Content-Type", "text/plain")
self.assertInBody('Hello world')
-
diff --git a/cherrypy/test/test_xmlrpc.py b/cherrypy/test/test_xmlrpc.py
index f7a69279..8f091ff1 100644
--- a/cherrypy/test/test_xmlrpc.py
+++ b/cherrypy/test/test_xmlrpc.py
@@ -2,9 +2,11 @@ import sys
from cherrypy._cpcompat import py3k
try:
- from xmlrpclib import DateTime, Fault, ProtocolError, ServerProxy, SafeTransport
+ from xmlrpclib import DateTime, Fault, ProtocolError, ServerProxy
+ from xmlrpclib import SafeTransport
except ImportError:
- from xmlrpc.client import DateTime, Fault, ProtocolError, ServerProxy, SafeTransport
+ from xmlrpc.client import DateTime, Fault, ProtocolError, ServerProxy
+ from xmlrpc.client import SafeTransport
if py3k:
HTTPSTransport = SafeTransport
@@ -15,32 +17,34 @@ if py3k:
socket.ssl = True
else:
class HTTPSTransport(SafeTransport):
- """Subclass of SafeTransport to fix sock.recv errors (by using file)."""
-
+
+ """Subclass of SafeTransport to fix sock.recv errors (by using file).
+ """
+
def request(self, host, handler, request_body, verbose=0):
# issue XML-RPC request
h = self.make_connection(host)
if verbose:
h.set_debuglevel(1)
-
+
self.send_request(h, handler, request_body)
self.send_host(h, host)
self.send_user_agent(h)
self.send_content(h, request_body)
-
+
errcode, errmsg, headers = h.getreply()
if errcode != 200:
raise ProtocolError(host + handler, errcode, errmsg, headers)
-
+
self.verbose = verbose
-
+
# Here's where we differ from the superclass. It says:
# try:
# sock = h._conn.sock
# except AttributeError:
# sock = None
# return self._parse_response(h.getfile(), sock)
-
+
return self.parse_response(h.getfile())
import cherrypy
@@ -48,37 +52,37 @@ import cherrypy
def setup_server():
from cherrypy import _cptools
-
+
class Root:
+
def index(self):
return "I'm a standard index!"
index.exposed = True
-
class XmlRpc(_cptools.XMLRPCController):
-
+
def foo(self):
return "Hello world!"
foo.exposed = True
-
+
def return_single_item_list(self):
return [42]
return_single_item_list.exposed = True
-
+
def return_string(self):
return "here is a string"
return_string.exposed = True
-
+
def return_tuple(self):
return ('here', 'is', 1, 'tuple')
return_tuple.exposed = True
-
+
def return_dict(self):
return dict(a=1, b=2, c=3)
return_dict.exposed = True
-
+
def return_composite(self):
- return dict(a=1,z=26), 'hi', ['welcome', 'friend']
+ return dict(a=1, z=26), 'hi', ['welcome', 'friend']
return_composite.exposed = True
def return_int(self):
@@ -110,15 +114,17 @@ def setup_server():
cherrypy.tree.mount(root, config={'/': {
'request.dispatch': cherrypy.dispatch.XMLRPCDispatcher(),
'tools.xmlrpc.allow_none': 0,
- }})
+ }})
from cherrypy.test import helper
+
class XmlRpcTest(helper.CPWebCase):
setup_server = staticmethod(setup_server)
+
def testXmlRpc(self):
-
+
scheme = self.scheme
if scheme == "https":
url = 'https://%s:%s/xmlrpc/' % (self.interface(), self.PORT)
@@ -126,15 +132,16 @@ class XmlRpcTest(helper.CPWebCase):
else:
url = 'http://%s:%s/xmlrpc/' % (self.interface(), self.PORT)
proxy = ServerProxy(url)
-
+
# begin the tests ...
self.getPage("/xmlrpc/foo")
self.assertBody("Hello world!")
-
+
self.assertEqual(proxy.return_single_item_list(), [42])
self.assertNotEqual(proxy.return_single_item_list(), 'one bazillion')
self.assertEqual(proxy.return_string(), "here is a string")
- self.assertEqual(proxy.return_tuple(), list(('here', 'is', 1, 'tuple')))
+ self.assertEqual(proxy.return_tuple(),
+ list(('here', 'is', 1, 'tuple')))
self.assertEqual(proxy.return_dict(), {'a': 1, 'c': 3, 'b': 2})
self.assertEqual(proxy.return_composite(),
[{'a': 1, 'z': 26}, 'hi', ['welcome', 'friend']])
@@ -144,7 +151,7 @@ class XmlRpcTest(helper.CPWebCase):
DateTime((2003, 10, 7, 8, 1, 0, 1, 280, -1)))
self.assertEqual(proxy.return_boolean(), True)
self.assertEqual(proxy.test_argument_passing(22), 22 * 2)
-
+
# Test an error in the page handler (should raise an xmlrpclib.Fault)
try:
proxy.test_argument_passing({})
@@ -155,18 +162,19 @@ class XmlRpcTest(helper.CPWebCase):
"for *: 'dict' and 'int'"))
else:
self.fail("Expected xmlrpclib.Fault")
-
- # http://www.cherrypy.org/ticket/533
+
+ # https://bitbucket.org/cherrypy/cherrypy/issue/533
# if a method is not found, an xmlrpclib.Fault should be raised
try:
proxy.non_method()
except Exception:
x = sys.exc_info()[1]
self.assertEqual(x.__class__, Fault)
- self.assertEqual(x.faultString, 'method "non_method" is not supported')
+ self.assertEqual(x.faultString,
+ 'method "non_method" is not supported')
else:
self.fail("Expected xmlrpclib.Fault")
-
+
# Test returning a Fault from the page handler.
try:
proxy.test_returning_Fault()
@@ -176,4 +184,3 @@ class XmlRpcTest(helper.CPWebCase):
self.assertEqual(x.faultString, ("custom Fault response"))
else:
self.fail("Expected xmlrpclib.Fault")
-
diff --git a/cherrypy/test/webtest.py b/cherrypy/test/webtest.py
index 50cfbad8..1fa3f969 100644
--- a/cherrypy/test/webtest.py
+++ b/cherrypy/test/webtest.py
@@ -16,7 +16,6 @@ the traceback to stdout, and keep any assertions you have from running
be of further significance to your tests).
"""
-import os
import pprint
import re
import socket
@@ -28,8 +27,8 @@ import types
from unittest import *
from unittest import _TextTestResult
-from cherrypy._cpcompat import basestring, ntob, py3k, HTTPConnection, HTTPSConnection, unicodestr
-
+from cherrypy._cpcompat import basestring, ntob, py3k, HTTPConnection
+from cherrypy._cpcompat import HTTPSConnection, unicodestr
def interface(host):
@@ -58,6 +57,7 @@ class TerseTestResult(_TextTestResult):
class TerseTestRunner(TextTestRunner):
+
"""A test runner class that displays results in textual form."""
def _makeResult(self):
@@ -75,7 +75,8 @@ class TerseTestRunner(TextTestRunner):
if failed:
self.stream.write("failures=%d" % failed)
if errored:
- if failed: self.stream.write(", ")
+ if failed:
+ self.stream.write(", ")
self.stream.write("errors=%d" % errored)
self.stream.writeln(")")
return result
@@ -111,7 +112,7 @@ class ReloadingTestLoader(TestLoader):
parts = unused_parts
break
except ImportError:
- unused_parts.insert(0,parts_copy[-1])
+ unused_parts.insert(0, parts_copy[-1])
del parts_copy[-1]
if not parts_copy:
raise
@@ -120,13 +121,13 @@ class ReloadingTestLoader(TestLoader):
for part in parts:
obj = getattr(obj, part)
- if type(obj) == types.ModuleType:
+ if isinstance(obj, types.ModuleType):
return self.loadTestsFromModule(obj)
elif (((py3k and isinstance(obj, type))
or isinstance(obj, (type, types.ClassType)))
and issubclass(obj, TestCase)):
return self.loadTestsFromTestCase(obj)
- elif type(obj) == types.UnboundMethodType:
+ elif isinstance(obj, types.UnboundMethodType):
if py3k:
return obj.__self__.__class__(obj.__name__)
else:
@@ -136,7 +137,7 @@ class ReloadingTestLoader(TestLoader):
if not isinstance(test, TestCase) and \
not isinstance(test, TestSuite):
raise ValueError("calling %s returned %s, "
- "not a test" % (obj,test))
+ "not a test" % (obj, test))
return test
else:
raise ValueError("do not know how to make test from: %s" % obj)
@@ -151,11 +152,14 @@ try:
else:
# On Windows, msvcrt.getch reads a single char without output.
import msvcrt
+
def getchar():
return msvcrt.getch()
except ImportError:
# Unix getchr
- import tty, termios
+ import tty
+ import termios
+
def getchar():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
@@ -179,9 +183,9 @@ class WebCase(TestCase):
status = None
headers = None
body = None
-
+
encoding = 'utf-8'
-
+
time = None
def get_conn(self, auto_open=False):
@@ -221,6 +225,7 @@ class WebCase(TestCase):
def _get_persistent(self):
return hasattr(self.HTTP_CONN, "__class__")
+
def _set_persistent(self, on):
self.set_persistent(on)
persistent = property(_get_persistent, _set_persistent)
@@ -232,15 +237,17 @@ class WebCase(TestCase):
or '::' (IN6ADDR_ANY), this will return the proper localhost."""
return interface(self.HOST)
- def getPage(self, url, headers=None, method="GET", body=None, protocol=None):
- """Open the url with debugging support. Return status, headers, body."""
+ def getPage(self, url, headers=None, method="GET", body=None,
+ protocol=None):
+ """Open the url with debugging support. Return status, headers, body.
+ """
ServerError.on = False
-
+
if isinstance(url, unicodestr):
url = url.encode('utf-8')
if isinstance(body, unicodestr):
body = body.encode('utf-8')
-
+
self.url = url
self.time = None
start = time.time()
@@ -267,7 +274,9 @@ class WebCase(TestCase):
if not self.interactive:
raise self.failureException(msg)
- p = " Show: [B]ody [H]eaders [S]tatus [U]RL; [I]gnore, [R]aise, or sys.e[X]it >> "
+ p = (" Show: "
+ "[B]ody [H]eaders [S]tatus [U]RL; "
+ "[I]gnore, [R]aise, or sys.e[X]it >> ")
sys.stdout.write(p)
sys.stdout.flush()
while True:
@@ -351,6 +360,19 @@ class WebCase(TestCase):
msg = '%r:%r not in headers' % (key, value)
self._handlewebError(msg)
+ def assertHeaderIn(self, key, values, msg=None):
+ """Fail if header indicated by key doesn't have one of the values."""
+ lowkey = key.lower()
+ for k, v in self.headers:
+ if k.lower() == lowkey:
+ matches = [value for value in values if str(value) == v]
+ if matches:
+ return matches
+
+ if msg is None:
+ msg = '%(key)r not in %(values)r' % vars()
+ self._handlewebError(msg)
+
def assertHeaderItemValue(self, key, value, msg=None):
"""Fail if the header does not contain the specified value"""
actual_value = self.assertHeader(key, msg=msg)
@@ -377,7 +399,8 @@ class WebCase(TestCase):
value = value.encode(self.encoding)
if value != self.body:
if msg is None:
- msg = 'expected body:\n%r\n\nactual body:\n%r' % (value, self.body)
+ msg = 'expected body:\n%r\n\nactual body:\n%r' % (
+ value, self.body)
self._handlewebError(msg)
def assertInBody(self, value, msg=None):
@@ -410,6 +433,7 @@ class WebCase(TestCase):
methods_with_bodies = ("POST", "PUT")
+
def cleanHeaders(headers, method, body, host, port):
"""Return request headers, with required headers added (if missing)."""
if headers is None:
@@ -436,7 +460,8 @@ def cleanHeaders(headers, method, body, host, port):
found = True
break
if not found:
- headers.append(("Content-Type", "application/x-www-form-urlencoded"))
+ headers.append(
+ ("Content-Type", "application/x-www-form-urlencoded"))
headers.append(("Content-Length", str(len(body or ""))))
return headers
@@ -492,7 +517,8 @@ def openURL(url, headers=None, method="GET", body=None,
return
self.__class__.putheader(self, header, value)
import new
- conn.putheader = new.instancemethod(putheader, conn, conn.__class__)
+ conn.putheader = new.instancemethod(
+ putheader, conn, conn.__class__)
conn.putrequest(method.upper(), url, skip_host=True)
elif not py3k:
conn.putrequest(method.upper(), url, skip_host=True,
@@ -500,28 +526,35 @@ def openURL(url, headers=None, method="GET", body=None,
else:
import http.client
# Replace the stdlib method, which only accepts ASCII url's
+
def putrequest(self, method, url):
- if self._HTTPConnection__response and self._HTTPConnection__response.isclosed():
+ if (
+ self._HTTPConnection__response and
+ self._HTTPConnection__response.isclosed()
+ ):
self._HTTPConnection__response = None
-
+
if self._HTTPConnection__state == http.client._CS_IDLE:
- self._HTTPConnection__state = http.client._CS_REQ_STARTED
+ self._HTTPConnection__state = (
+ http.client._CS_REQ_STARTED)
else:
raise http.client.CannotSendRequest()
-
+
self._method = method
if not url:
url = ntob('/')
- request = ntob(' ').join((method.encode("ASCII"), url,
- self._http_vsn_str.encode("ASCII")))
+ request = ntob(' ').join(
+ (method.encode("ASCII"),
+ url,
+ self._http_vsn_str.encode("ASCII")))
self._output(request)
import types
conn.putrequest = types.MethodType(putrequest, conn)
-
+
conn.putrequest(method.upper(), url)
for key, value in headers:
- conn.putheader(key, ntob(value, "Latin-1"))
+ conn.putheader(key, value.encode("Latin-1"))
conn.endheaders()
if body is not None:
@@ -552,6 +585,7 @@ ignored_exceptions = []
# for example, when handling requests via multiple threads.
ignore_all = False
+
class ServerError(Exception):
on = False
@@ -572,4 +606,3 @@ def server_error(exc=None):
print("")
print("".join(traceback.format_exception(*exc)))
return True
-
diff --git a/cherrypy/tutorial/__init__.py b/cherrypy/tutorial/__init__.py
index c4e2c558..08c142c5 100644
--- a/cherrypy/tutorial/__init__.py
+++ b/cherrypy/tutorial/__init__.py
@@ -1,3 +1,3 @@
# This is used in test_config to test unrepr of "from A import B"
-thing2 = object() \ No newline at end of file
+thing2 = object()
diff --git a/cherrypy/tutorial/bonus-sqlobject.py b/cherrypy/tutorial/bonus-sqlobject.py
deleted file mode 100644
index c43feb45..00000000
--- a/cherrypy/tutorial/bonus-sqlobject.py
+++ /dev/null
@@ -1,168 +0,0 @@
-'''
-Bonus Tutorial: Using SQLObject
-
-This is a silly little contacts manager application intended to
-demonstrate how to use SQLObject from within a CherryPy2 project. It
-also shows how to use inline Cheetah templates.
-
-SQLObject is an Object/Relational Mapper that allows you to access
-data stored in an RDBMS in a pythonic fashion. You create data objects
-as Python classes and let SQLObject take care of all the nasty details.
-
-This code depends on the latest development version (0.6+) of SQLObject.
-You can get it from the SQLObject Subversion server. You can find all
-necessary information at <http://www.sqlobject.org>. This code will NOT
-work with the 0.5.x version advertised on their website!
-
-This code also depends on a recent version of Cheetah. You can find
-Cheetah at <http://www.cheetahtemplate.org>.
-
-After starting this application for the first time, you will need to
-access the /reset URI in order to create the database table and some
-sample data. Accessing /reset again will drop and re-create the table,
-so you may want to be careful. :-)
-
-This application isn't supposed to be fool-proof, it's not even supposed
-to be very GOOD. Play around with it some, browse the source code, smile.
-
-:)
-
--- Hendrik Mans <hendrik@mans.de>
-'''
-
-import cherrypy
-from Cheetah.Template import Template
-from sqlobject import *
-
-# configure your database connection here
-__connection__ = 'mysql://root:@localhost/test'
-
-# this is our (only) data class.
-class Contact(SQLObject):
- lastName = StringCol(length = 50, notNone = True)
- firstName = StringCol(length = 50, notNone = True)
- phone = StringCol(length = 30, notNone = True, default = '')
- email = StringCol(length = 30, notNone = True, default = '')
- url = StringCol(length = 100, notNone = True, default = '')
-
-
-class ContactManager:
- def index(self):
- # Let's display a list of all stored contacts.
- contacts = Contact.select()
-
- template = Template('''
- <h2>All Contacts</h2>
-
- #for $contact in $contacts
- <a href="mailto:$contact.email">$contact.lastName, $contact.firstName</a>
- [<a href="./edit?id=$contact.id">Edit</a>]
- [<a href="./delete?id=$contact.id">Delete</a>]
- <br/>
- #end for
-
- <p>[<a href="./edit">Add new contact</a>]</p>
- ''', [locals(), globals()])
-
- return template.respond()
-
- index.exposed = True
-
-
- def edit(self, id = 0):
- # we really want id as an integer. Since GET/POST parameters
- # are always passed as strings, let's convert it.
- id = int(id)
-
- if id > 0:
- # if an id is specified, we're editing an existing contact.
- contact = Contact.get(id)
- title = "Edit Contact"
- else:
- # if no id is specified, we're entering a new contact.
- contact = None
- title = "New Contact"
-
-
- # In the following template code, please note that we use
- # Cheetah's $getVar() construct for the form values. We have
- # to do this because contact may be set to None (see above).
- template = Template('''
- <h2>$title</h2>
-
- <form action="./store" method="POST">
- <input type="hidden" name="id" value="$id" />
- Last Name: <input name="lastName" value="$getVar('contact.lastName', '')" /><br/>
- First Name: <input name="firstName" value="$getVar('contact.firstName', '')" /><br/>
- Phone: <input name="phone" value="$getVar('contact.phone', '')" /><br/>
- Email: <input name="email" value="$getVar('contact.email', '')" /><br/>
- URL: <input name="url" value="$getVar('contact.url', '')" /><br/>
- <input type="submit" value="Store" />
- </form>
- ''', [locals(), globals()])
-
- return template.respond()
-
- edit.exposed = True
-
-
- def delete(self, id):
- # Delete the specified contact
- contact = Contact.get(int(id))
- contact.destroySelf()
- return 'Deleted. <a href="./">Return to Index</a>'
-
- delete.exposed = True
-
-
- def store(self, lastName, firstName, phone, email, url, id = None):
- if id and int(id) > 0:
- # If an id was specified, update an existing contact.
- contact = Contact.get(int(id))
-
- # We could set one field after another, but that would
- # cause multiple UPDATE clauses. So we'll just do it all
- # in a single pass through the set() method.
- contact.set(
- lastName = lastName,
- firstName = firstName,
- phone = phone,
- email = email,
- url = url)
- else:
- # Otherwise, add a new contact.
- contact = Contact(
- lastName = lastName,
- firstName = firstName,
- phone = phone,
- email = email,
- url = url)
-
- return 'Stored. <a href="./">Return to Index</a>'
-
- store.exposed = True
-
-
- def reset(self):
- # Drop existing table
- Contact.dropTable(True)
-
- # Create new table
- Contact.createTable()
-
- # Create some sample data
- Contact(
- firstName = 'Hendrik',
- lastName = 'Mans',
- email = 'hendrik@mans.de',
- phone = '++49 89 12345678',
- url = 'http://www.mornography.de')
-
- return "reset completed!"
-
- reset.exposed = True
-
-
-print("If you're running this application for the first time, please go to http://localhost:8080/reset once in order to create the database!")
-
-cherrypy.quickstart(ContactManager())
diff --git a/cherrypy/tutorial/tut01_helloworld.py b/cherrypy/tutorial/tut01_helloworld.py
index ef947601..2fa61afa 100644
--- a/cherrypy/tutorial/tut01_helloworld.py
+++ b/cherrypy/tutorial/tut01_helloworld.py
@@ -7,7 +7,9 @@ The most basic (working) CherryPy application possible.
# Import CherryPy global namespace
import cherrypy
+
class HelloWorld:
+
""" Sample request handler class. """
def index(self):
diff --git a/cherrypy/tutorial/tut02_expose_methods.py b/cherrypy/tutorial/tut02_expose_methods.py
index 600fca34..64a10f26 100644
--- a/cherrypy/tutorial/tut02_expose_methods.py
+++ b/cherrypy/tutorial/tut02_expose_methods.py
@@ -7,17 +7,18 @@ handler.
import cherrypy
+
class HelloWorld:
-
+
def index(self):
# Let's link to another method here.
- return 'We have an <a href="showMessage">important message</a> for you!'
+ return 'We have an <a href="show_msg">important message</a> for you!'
index.exposed = True
-
- def showMessage(self):
+
+ def show_msg(self):
# Here's the important message!
return "Hello world!"
- showMessage.exposed = True
+ show_msg.exposed = True
import os.path
tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf')
diff --git a/cherrypy/tutorial/tut03_get_and_post.py b/cherrypy/tutorial/tut03_get_and_post.py
index 283477d5..a487a909 100644
--- a/cherrypy/tutorial/tut03_get_and_post.py
+++ b/cherrypy/tutorial/tut03_get_and_post.py
@@ -18,8 +18,8 @@ class WelcomePage:
<input type="submit" />
</form>'''
index.exposed = True
-
- def greetUser(self, name = None):
+
+ def greetUser(self, name=None):
# CherryPy passes all GET and POST variables as method parameters.
# It doesn't make a difference where the variables come from, how
# large their contents are, and so on.
@@ -27,7 +27,7 @@ class WelcomePage:
# You can define default parameter values as usual. In this
# example, the "name" parameter defaults to None so we can check
# if a name was actually specified.
-
+
if name:
# Greet the user!
return "Hey %s, what's up?" % name
diff --git a/cherrypy/tutorial/tut04_complex_site.py b/cherrypy/tutorial/tut04_complex_site.py
index b4d820ed..e0660024 100644
--- a/cherrypy/tutorial/tut04_complex_site.py
+++ b/cherrypy/tutorial/tut04_complex_site.py
@@ -9,11 +9,12 @@ import cherrypy
class HomePage:
+
def index(self):
return '''
<p>Hi, this is the home page! Check out the other
fun stuff on this site:</p>
-
+
<ul>
<li><a href="/joke/">A silly joke</a></li>
<li><a href="/links/">Useful links</a></li>
@@ -22,6 +23,7 @@ class HomePage:
class JokePage:
+
def index(self):
return '''
<p>"In Python, how do you create a string of random
@@ -31,12 +33,13 @@ class JokePage:
class LinksPage:
+
def __init__(self):
# Request handler objects can create their own nested request
# handler objects. Simply create them inside their __init__
# methods!
self.extra = ExtraLinksPage()
-
+
def index(self):
# Note the way we link to the extra links page (and back).
# As you can see, this object doesn't really care about its
@@ -44,31 +47,36 @@ class LinksPage:
# links exclusively.
return '''
<p>Here are some useful links:</p>
-
+
<ul>
- <li><a href="http://www.cherrypy.org">The CherryPy Homepage</a></li>
- <li><a href="http://www.python.org">The Python Homepage</a></li>
+ <li>
+ <a href="http://www.cherrypy.org">The CherryPy Homepage</a>
+ </li>
+ <li>
+ <a href="http://www.python.org">The Python Homepage</a>
+ </li>
</ul>
-
+
<p>You can check out some extra useful
links <a href="./extra/">here</a>.</p>
-
+
<p>[<a href="../">Return</a>]</p>
'''
index.exposed = True
class ExtraLinksPage:
+
def index(self):
# Note the relative link back to the Links page!
return '''
<p>Here are some extra useful links:</p>
-
+
<ul>
<li><a href="http://del.icio.us">del.icio.us</a></li>
- <li><a href="http://www.mornography.de">Hendrik's weblog</a></li>
+ <li><a href="http://www.cherrypy.org">CherryPy</a></li>
</ul>
-
+
<p>[<a href="../">Return to links page</a>]</p>'''
index.exposed = True
@@ -95,4 +103,3 @@ if __name__ == '__main__':
else:
# This branch is for the test suite; you can ignore it.
cherrypy.tree.mount(root, config=tutconf)
-
diff --git a/cherrypy/tutorial/tut05_derived_objects.py b/cherrypy/tutorial/tut05_derived_objects.py
index 3d4ec9b1..9f4f2c32 100644
--- a/cherrypy/tutorial/tut05_derived_objects.py
+++ b/cherrypy/tutorial/tut05_derived_objects.py
@@ -13,7 +13,7 @@ import cherrypy
class Page:
# Store the page title in a class attribute
title = 'Untitled Page'
-
+
def header(self):
return '''
<html>
@@ -23,13 +23,13 @@ class Page:
<body>
<h2>%s</h2>
''' % (self.title, self.title)
-
+
def footer(self):
return '''
</body>
</html>
'''
-
+
# Note that header and footer don't get their exposed attributes
# set to True. This isn't necessary since the user isn't supposed
# to call header or footer directly; instead, we'll call them from
@@ -40,11 +40,11 @@ class Page:
class HomePage(Page):
# Different title for this page
title = 'Tutorial 5'
-
+
def __init__(self):
# create a subpage
self.another = AnotherPage()
-
+
def index(self):
# Note that we call the header and footer methods inherited
# from the Page class!
@@ -59,7 +59,7 @@ class HomePage(Page):
class AnotherPage(Page):
title = 'Another Page'
-
+
def index(self):
return self.header() + '''
<p>
@@ -80,4 +80,3 @@ if __name__ == '__main__':
else:
# This branch is for the test suite; you can ignore it.
cherrypy.tree.mount(HomePage(), config=tutconf)
-
diff --git a/cherrypy/tutorial/tut06_default_method.py b/cherrypy/tutorial/tut06_default_method.py
index fe24f380..f71c315a 100644
--- a/cherrypy/tutorial/tut06_default_method.py
+++ b/cherrypy/tutorial/tut06_default_method.py
@@ -20,7 +20,7 @@ import cherrypy
class UsersPage:
-
+
def index(self):
# Since this is just a stupid little example, we'll simply
# display a list of links to random, made-up users. In a real
@@ -31,7 +31,7 @@ class UsersPage:
<a href="./lorenzo">Lorenzo Lamas</a><br/>
'''
index.exposed = True
-
+
def default(self, user):
# Here we react depending on the virtualPath -- the part of the
# path that could not be mapped to an object method. In a real
@@ -45,7 +45,7 @@ class UsersPage:
out = "Lorenzo Lamas, famous actor and singer!"
else:
out = "Unknown user. :-("
-
+
return '%s (<a href="./">back</a>)' % out
default.exposed = True
@@ -61,4 +61,3 @@ if __name__ == '__main__':
else:
# This branch is for the test suite; you can ignore it.
cherrypy.tree.mount(UsersPage(), config=tutconf)
-
diff --git a/cherrypy/tutorial/tut07_sessions.py b/cherrypy/tutorial/tut07_sessions.py
index 4b1386b4..62efa440 100644
--- a/cherrypy/tutorial/tut07_sessions.py
+++ b/cherrypy/tutorial/tut07_sessions.py
@@ -12,16 +12,16 @@ import cherrypy
class HitCounter:
-
+
_cp_config = {'tools.sessions.on': True}
-
+
def index(self):
# Increase the silly hit counter
count = cherrypy.session.get('count', 0) + 1
-
+
# Store the new value in the session dictionary
cherrypy.session['count'] = count
-
+
# And display a silly hit count message!
return '''
During your current session, you've viewed this
@@ -41,4 +41,3 @@ if __name__ == '__main__':
else:
# This branch is for the test suite; you can ignore it.
cherrypy.tree.mount(HitCounter(), config=tutconf)
-
diff --git a/cherrypy/tutorial/tut08_generators_and_yield.py b/cherrypy/tutorial/tut08_generators_and_yield.py
index a6fbdc24..40566e32 100644
--- a/cherrypy/tutorial/tut08_generators_and_yield.py
+++ b/cherrypy/tutorial/tut08_generators_and_yield.py
@@ -11,24 +11,24 @@ import cherrypy
class GeneratorDemo:
-
+
def header(self):
return "<html><body><h2>Generators rule!</h2>"
-
+
def footer(self):
return "</body></html>"
-
+
def index(self):
# Let's make up a list of users for presentation purposes
users = ['Remi', 'Carlos', 'Hendrik', 'Lorenzo Lamas']
-
+
# Every yield line adds one part to the total result body.
yield self.header()
yield "<h3>List of users:</h3>"
-
+
for user in users:
yield "%s<br/>" % user
-
+
yield self.footer()
index.exposed = True
@@ -44,4 +44,3 @@ if __name__ == '__main__':
else:
# This branch is for the test suite; you can ignore it.
cherrypy.tree.mount(GeneratorDemo(), config=tutconf)
-
diff --git a/cherrypy/tutorial/tut09_files.py b/cherrypy/tutorial/tut09_files.py
index 4c8e5815..6a7b4a34 100644
--- a/cherrypy/tutorial/tut09_files.py
+++ b/cherrypy/tutorial/tut09_files.py
@@ -49,7 +49,7 @@ from cherrypy.lib import static
class FileDemo(object):
-
+
def index(self):
return """
<html><body>
@@ -63,7 +63,7 @@ class FileDemo(object):
</body></html>
"""
index.exposed = True
-
+
def upload(self, myFile):
out = """<html>
<body>
@@ -72,7 +72,7 @@ class FileDemo(object):
myFile mime-type: %s
</body>
</html>"""
-
+
# Although this just counts the file length, it demonstrates
# how to read large files in chunks instead of all at once.
# CherryPy reads the uploaded file into a temporary file;
@@ -83,10 +83,10 @@ class FileDemo(object):
if not data:
break
size += len(data)
-
+
return out % (size, myFile.filename, myFile.content_type)
upload.exposed = True
-
+
def download(self):
path = os.path.join(absDir, "pdf_file.pdf")
return static.serve_file(path, "application/x-download",
diff --git a/cherrypy/tutorial/tut10_http_errors.py b/cherrypy/tutorial/tut10_http_errors.py
index dfa57331..c0e9893b 100644
--- a/cherrypy/tutorial/tut10_http_errors.py
+++ b/cherrypy/tutorial/tut10_http_errors.py
@@ -16,10 +16,11 @@ import cherrypy
class HTTPErrorDemo(object):
-
+
# Set a custom response for 403 errors.
- _cp_config = {'error_page.403' : os.path.join(curpath, "custom_error.html")}
-
+ _cp_config = {'error_page.403':
+ os.path.join(curpath, "custom_error.html")}
+
def index(self):
# display some links that will result in errors
tracebacks = cherrypy.request.show_tracebacks
@@ -27,12 +28,16 @@ class HTTPErrorDemo(object):
trace = 'off'
else:
trace = 'on'
-
+
return """
<html><body>
<p>Toggle tracebacks <a href="toggleTracebacks">%s</a></p>
<p><a href="/doesNotExist">Click me; I'm a broken link!</a></p>
- <p><a href="/error?code=403">Use a custom error page from a file.</a></p>
+ <p>
+ <a href="/error?code=403">
+ Use a custom error page from a file.
+ </a>
+ </p>
<p>These errors are explicitly raised by the application:</p>
<ul>
<li><a href="/error?code=400">400</a></li>
@@ -45,21 +50,21 @@ class HTTPErrorDemo(object):
</body></html>
""" % trace
index.exposed = True
-
+
def toggleTracebacks(self):
- # simple function to toggle tracebacks on and off
+ # simple function to toggle tracebacks on and off
tracebacks = cherrypy.request.show_tracebacks
cherrypy.config.update({'request.show_tracebacks': not tracebacks})
-
+
# redirect back to the index
raise cherrypy.HTTPRedirect('/')
toggleTracebacks.exposed = True
-
+
def error(self, code):
# raise an error based on the get query
- raise cherrypy.HTTPError(status = code)
+ raise cherrypy.HTTPError(status=code)
error.exposed = True
-
+
def messageArg(self):
message = ("If you construct an HTTPError with a 'message' "
"argument, it wil be placed on the error page "
diff --git a/cherrypy/wsgiserver/ssl_builtin.py b/cherrypy/wsgiserver/ssl_builtin.py
index 03bf05de..2c74ad84 100644
--- a/cherrypy/wsgiserver/ssl_builtin.py
+++ b/cherrypy/wsgiserver/ssl_builtin.py
@@ -25,31 +25,33 @@ from cherrypy import wsgiserver
class BuiltinSSLAdapter(wsgiserver.SSLAdapter):
+
"""A wrapper for integrating Python's builtin ssl module with CherryPy."""
-
+
certificate = None
"""The filename of the server SSL certificate."""
-
+
private_key = None
"""The filename of the server's private key file."""
-
+
def __init__(self, certificate, private_key, certificate_chain=None):
if ssl is None:
raise ImportError("You must install the ssl module to use HTTPS.")
self.certificate = certificate
self.private_key = private_key
self.certificate_chain = certificate_chain
-
+
def bind(self, sock):
"""Wrap and return the given socket."""
return sock
-
+
def wrap(self, sock):
"""Wrap and return the given socket, plus WSGI environ entries."""
try:
s = ssl.wrap_socket(sock, do_handshake_on_connect=True,
- server_side=True, certfile=self.certificate,
- keyfile=self.private_key, ssl_version=ssl.PROTOCOL_SSLv23)
+ server_side=True, certfile=self.certificate,
+ keyfile=self.private_key,
+ ssl_version=ssl.PROTOCOL_SSLv23)
except ssl.SSLError:
e = sys.exc_info()[1]
if e.errno == ssl.SSL_ERROR_EOF:
@@ -67,7 +69,7 @@ class BuiltinSSLAdapter(wsgiserver.SSLAdapter):
return None, {}
raise
return s, self.get_environ(s)
-
+
# TODO: fill this out more with mod ssl env
def get_environ(self, sock):
"""Create WSGI environ entries to be merged into each request."""
@@ -77,15 +79,14 @@ class BuiltinSSLAdapter(wsgiserver.SSLAdapter):
"HTTPS": "on",
'SSL_PROTOCOL': cipher[1],
'SSL_CIPHER': cipher[0]
-## SSL_VERSION_INTERFACE string The mod_ssl program version
-## SSL_VERSION_LIBRARY string The OpenSSL program version
- }
+ # SSL_VERSION_INTERFACE string The mod_ssl program version
+ # SSL_VERSION_LIBRARY string The OpenSSL program version
+ }
return ssl_environ
-
+
if sys.version_info >= (3, 0):
def makefile(self, sock, mode='r', bufsize=DEFAULT_BUFFER_SIZE):
return wsgiserver.CP_makefile(sock, mode, bufsize)
else:
def makefile(self, sock, mode='r', bufsize=DEFAULT_BUFFER_SIZE):
return wsgiserver.CP_fileobject(sock, mode, bufsize)
-
diff --git a/cherrypy/wsgiserver/ssl_pyopenssl.py b/cherrypy/wsgiserver/ssl_pyopenssl.py
index f3d9bf54..f8f2dafe 100644
--- a/cherrypy/wsgiserver/ssl_pyopenssl.py
+++ b/cherrypy/wsgiserver/ssl_pyopenssl.py
@@ -1,7 +1,7 @@
"""A library for integrating pyOpenSSL with CherryPy.
The OpenSSL module must be importable for SSL functionality.
-You can obtain it from http://pyopenssl.sourceforge.net/
+You can obtain it from `here <https://launchpad.net/pyopenssl>`_.
To use this module, set CherryPyWSGIServer.ssl_adapter to an instance of
SSLAdapter. There are two ways to use SSL:
@@ -44,14 +44,15 @@ except ImportError:
class SSL_fileobject(wsgiserver.CP_fileobject):
+
"""SSL file object attached to a socket object."""
-
+
ssl_timeout = 3
ssl_retry = .01
-
+
def _safe_call(self, is_reader, call, *args, **kwargs):
"""Wrap the given call with SSL error-trapping.
-
+
is_reader: if False EOF errors will be raised. If True, EOF errors
will return "" (to emulate normal sockets).
"""
@@ -70,7 +71,7 @@ class SSL_fileobject(wsgiserver.CP_fileobject):
except SSL.SysCallError, e:
if is_reader and e.args == (-1, 'Unexpected EOF'):
return ""
-
+
errnum = e.args[0]
if is_reader and errnum in wsgiserver.socket_errors_to_ignore:
return ""
@@ -78,34 +79,27 @@ class SSL_fileobject(wsgiserver.CP_fileobject):
except SSL.Error, e:
if is_reader and e.args == (-1, 'Unexpected EOF'):
return ""
-
+
thirdarg = None
try:
thirdarg = e.args[0][0][2]
except IndexError:
pass
-
+
if thirdarg == 'http request':
# The client is talking HTTP to an HTTPS server.
raise wsgiserver.NoSSLError()
-
+
raise wsgiserver.FatalSSLAlert(*e.args)
except:
raise
-
+
if time.time() - start > self.ssl_timeout:
raise socket.timeout("timed out")
-
- def recv(self, *args, **kwargs):
- buf = []
- r = super(SSL_fileobject, self).recv
- while True:
- data = self._safe_call(True, r, *args, **kwargs)
- buf.append(data)
- p = self._sock.pending()
- if not p:
- return "".join(buf)
-
+
+ def recv(self, size):
+ return self._safe_call(True, super(SSL_fileobject, self).recv, size)
+
def sendall(self, *args, **kwargs):
return self._safe_call(False, super(SSL_fileobject, self).sendall,
*args, **kwargs)
@@ -116,15 +110,16 @@ class SSL_fileobject(wsgiserver.CP_fileobject):
class SSLConnection:
+
"""A thread-safe wrapper for an SSL.Connection.
-
+
``*args``: the arguments to create the wrapped ``SSL.Connection(*args)``.
"""
-
+
def __init__(self, *args):
self._ssl_conn = SSL.Connection(*args)
self._lock = threading.RLock()
-
+
for f in ('get_context', 'pending', 'send', 'write', 'recv', 'read',
'renegotiate', 'bind', 'listen', 'connect', 'accept',
'setblocking', 'fileno', 'close', 'get_cipher_list',
@@ -140,7 +135,7 @@ class SSLConnection:
finally:
self._lock.release()
""" % (f, f))
-
+
def shutdown(self, *args):
self._lock.acquire()
try:
@@ -151,33 +146,34 @@ class SSLConnection:
class pyOpenSSLAdapter(wsgiserver.SSLAdapter):
+
"""A wrapper for integrating pyOpenSSL with CherryPy."""
-
+
context = None
"""An instance of SSL.Context."""
-
+
certificate = None
"""The filename of the server SSL certificate."""
-
+
private_key = None
"""The filename of the server's private key file."""
-
+
certificate_chain = None
"""Optional. The filename of CA's intermediate certificate bundle.
-
+
This is needed for cheaper "chained root" SSL certificates, and should be
left as None if not required."""
-
+
def __init__(self, certificate, private_key, certificate_chain=None):
if SSL is None:
raise ImportError("You must install pyOpenSSL to use HTTPS.")
-
+
self.context = None
self.certificate = certificate
self.private_key = private_key
self.certificate_chain = certificate_chain
self._environ = None
-
+
def bind(self, sock):
"""Wrap and return the given socket."""
if self.context is None:
@@ -185,11 +181,11 @@ class pyOpenSSLAdapter(wsgiserver.SSLAdapter):
conn = SSLConnection(self.context, sock)
self._environ = self.get_environ()
return conn
-
+
def wrap(self, sock):
"""Wrap and return the given socket, plus WSGI environ entries."""
return sock, self._environ.copy()
-
+
def get_context(self):
"""Return an SSL.Context from self attributes."""
# See http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/442473
@@ -199,18 +195,18 @@ class pyOpenSSLAdapter(wsgiserver.SSLAdapter):
c.load_verify_locations(self.certificate_chain)
c.use_certificate_file(self.certificate)
return c
-
+
def get_environ(self):
"""Return WSGI environ entries to be merged into each request."""
ssl_environ = {
"HTTPS": "on",
# pyOpenSSL doesn't provide access to any of these AFAICT
-## 'SSL_PROTOCOL': 'SSLv2',
-## SSL_CIPHER string The cipher specification name
-## SSL_VERSION_INTERFACE string The mod_ssl program version
-## SSL_VERSION_LIBRARY string The OpenSSL program version
- }
-
+ # 'SSL_PROTOCOL': 'SSLv2',
+ # SSL_CIPHER string The cipher specification name
+ # SSL_VERSION_INTERFACE string The mod_ssl program version
+ # SSL_VERSION_LIBRARY string The OpenSSL program version
+ }
+
if self.certificate:
# Server certificate attributes
cert = open(self.certificate, 'rb').read()
@@ -218,20 +214,22 @@ class pyOpenSSLAdapter(wsgiserver.SSLAdapter):
ssl_environ.update({
'SSL_SERVER_M_VERSION': cert.get_version(),
'SSL_SERVER_M_SERIAL': cert.get_serial_number(),
-## 'SSL_SERVER_V_START': Validity of server's certificate (start time),
-## 'SSL_SERVER_V_END': Validity of server's certificate (end time),
- })
-
+ # 'SSL_SERVER_V_START':
+ # Validity of server's certificate (start time),
+ # 'SSL_SERVER_V_END':
+ # Validity of server's certificate (end time),
+ })
+
for prefix, dn in [("I", cert.get_issuer()),
("S", cert.get_subject())]:
# X509Name objects don't seem to have a way to get the
# complete DN string. Use str() and slice it instead,
# because str(dn) == "<X509Name object '/C=US/ST=...'>"
dnstr = str(dn)[18:-2]
-
+
wsgikey = 'SSL_SERVER_%s_DN' % prefix
ssl_environ[wsgikey] = dnstr
-
+
# The DN should be of the form: /k1=v1/k2=v2, but we must allow
# for any value to contain slashes itself (in a URL).
while dnstr:
@@ -242,9 +240,9 @@ class pyOpenSSLAdapter(wsgiserver.SSLAdapter):
if key and value:
wsgikey = 'SSL_SERVER_%s_DN_%s' % (prefix, key)
ssl_environ[wsgikey] = value
-
+
return ssl_environ
-
+
def makefile(self, sock, mode='r', bufsize=-1):
if SSL and isinstance(sock, SSL.ConnectionType):
timeout = sock.gettimeout()
@@ -253,4 +251,3 @@ class pyOpenSSLAdapter(wsgiserver.SSLAdapter):
return f
else:
return wsgiserver.CP_fileobject(sock, mode, bufsize)
-
diff --git a/cherrypy/wsgiserver/wsgiserver2.py b/cherrypy/wsgiserver/wsgiserver2.py
index 91ef3226..9ca13e6c 100644
--- a/cherrypy/wsgiserver/wsgiserver2.py
+++ b/cherrypy/wsgiserver/wsgiserver2.py
@@ -86,19 +86,34 @@ import re
import rfc822
import socket
import sys
-if 'win' in sys.platform and not hasattr(socket, 'IPPROTO_IPV6'):
- socket.IPPROTO_IPV6 = 41
+if 'win' in sys.platform and hasattr(socket, "AF_INET6"):
+ if not hasattr(socket, 'IPPROTO_IPV6'):
+ socket.IPPROTO_IPV6 = 41
+ if not hasattr(socket, 'IPV6_V6ONLY'):
+ socket.IPV6_V6ONLY = 27
try:
import cStringIO as StringIO
except ImportError:
import StringIO
DEFAULT_BUFFER_SIZE = -1
-_fileobject_uses_str_type = isinstance(socket._fileobject(None)._rbuf, basestring)
+
+class FauxSocket(object):
+
+ """Faux socket with the minimal interface required by pypy"""
+
+ def _reuse(self):
+ pass
+
+_fileobject_uses_str_type = isinstance(
+ socket._fileobject(FauxSocket())._rbuf, basestring)
+del FauxSocket # this class is not longer required for anything.
import threading
import time
import traceback
+
+
def format_exc(limit=None):
"""Like print_exc() but return a string. Backport for Python 2.3."""
try:
@@ -107,25 +122,31 @@ def format_exc(limit=None):
finally:
etype = value = tb = None
+import operator
from urllib import unquote
-from urlparse import urlparse
import warnings
if sys.version_info >= (3, 0):
bytestr = bytes
unicodestr = str
basestring = (bytes, str)
+
def ntob(n, encoding='ISO-8859-1'):
- """Return the given native string as a byte string in the given encoding."""
+ """Return the given native string as a byte string in the given
+ encoding.
+ """
# In Python 3, the native string type is unicode
return n.encode(encoding)
else:
bytestr = str
unicodestr = unicode
basestring = basestring
+
def ntob(n, encoding='ISO-8859-1'):
- """Return the given native string as a byte string in the given encoding."""
+ """Return the given native string as a byte string in the given
+ encoding.
+ """
# In Python 2, the native string type is bytes. Assume it's already
# in the given encoding, which for ISO-8859-1 is almost always what
# was intended.
@@ -146,6 +167,7 @@ quoted_slash = re.compile(ntob("(?i)%2F"))
import errno
+
def plat_specific_errors(*errnames):
"""Return error numbers for all errors in errnames on this platform.
@@ -170,24 +192,27 @@ socket_errors_to_ignore = plat_specific_errors(
"ECONNABORTED", "WSAECONNABORTED",
"ENETRESET", "WSAENETRESET",
"EHOSTDOWN", "EHOSTUNREACH",
- )
+)
socket_errors_to_ignore.append("timed out")
socket_errors_to_ignore.append("The read operation timed out")
socket_errors_nonblocking = plat_specific_errors(
'EAGAIN', 'EWOULDBLOCK', 'WSAEWOULDBLOCK')
-comma_separated_headers = [ntob(h) for h in
+comma_separated_headers = [
+ ntob(h) for h in
['Accept', 'Accept-Charset', 'Accept-Encoding',
'Accept-Language', 'Accept-Ranges', 'Allow', 'Cache-Control',
'Connection', 'Content-Encoding', 'Content-Language', 'Expect',
'If-Match', 'If-None-Match', 'Pragma', 'Proxy-Authenticate', 'TE',
'Trailer', 'Transfer-Encoding', 'Upgrade', 'Vary', 'Via', 'Warning',
- 'WWW-Authenticate']]
+ 'WWW-Authenticate']
+]
import logging
-if not hasattr(logging, 'statistics'): logging.statistics = {}
+if not hasattr(logging, 'statistics'):
+ logging.statistics = {}
def read_headers(rfile, hdict=None):
@@ -242,7 +267,9 @@ def read_headers(rfile, hdict=None):
class MaxSizeExceeded(Exception):
pass
+
class SizeCheckWrapper(object):
+
"""Wraps a file-like object, raising MaxSizeExceeded if too large."""
def __init__(self, rfile, maxlen):
@@ -275,8 +302,8 @@ class SizeCheckWrapper(object):
self.bytes_read += len(data)
self._check_length()
res.append(data)
- # See http://www.cherrypy.org/ticket/421
- if len(data) < 256 or data[-1:] == "\n":
+ # See https://bitbucket.org/cherrypy/cherrypy/issue/421
+ if len(data) < 256 or data[-1:] == LF:
return EMPTY.join(res)
def readlines(self, sizehint=0):
@@ -312,6 +339,7 @@ class SizeCheckWrapper(object):
class KnownLengthRFile(object):
+
"""Wraps a file-like object, returning an empty string when exhausted."""
def __init__(self, rfile, content_length):
@@ -368,6 +396,7 @@ class KnownLengthRFile(object):
class ChunkedRFile(object):
+
"""Wraps a file-like object, returning an empty string when exhausted.
This class is intended to provide a conforming wsgi.input value for
@@ -417,8 +446,8 @@ class ChunkedRFile(object):
crlf = self.rfile.read(2)
if crlf != CRLF:
raise ValueError(
- "Bad chunked transfer coding (expected '\\r\\n', "
- "got " + repr(crlf) + ")")
+ "Bad chunked transfer coding (expected '\\r\\n', "
+ "got " + repr(crlf) + ")")
def read(self, size=None):
data = EMPTY
@@ -520,6 +549,7 @@ class ChunkedRFile(object):
class HTTPRequest(object):
+
"""An HTTP Request (and response).
A single HTTP connection may consist of multiple request/response pairs.
@@ -553,7 +583,7 @@ class HTTPRequest(object):
This value is set automatically inside send_headers."""
def __init__(self, server, conn):
- self.server= server
+ self.server = server
self.conn = conn
self.ready = False
@@ -579,7 +609,8 @@ class HTTPRequest(object):
try:
success = self.read_request_line()
except MaxSizeExceeded:
- self.simple_response("414 Request-URI Too Long",
+ self.simple_response(
+ "414 Request-URI Too Long",
"The Request-URI sent with the request exceeds the maximum "
"allowed bytes.")
return
@@ -590,7 +621,8 @@ class HTTPRequest(object):
try:
success = self.read_request_headers()
except MaxSizeExceeded:
- self.simple_response("413 Request Entity Too Large",
+ self.simple_response(
+ "413 Request Entity Too Large",
"The headers sent with the request exceed the maximum "
"allowed bytes.")
return
@@ -626,7 +658,8 @@ class HTTPRequest(object):
return False
if not request_line.endswith(CRLF):
- self.simple_response("400 Bad Request", "HTTP requires CRLF terminators")
+ self.simple_response(
+ "400 Bad Request", "HTTP requires CRLF terminators")
return False
try:
@@ -709,7 +742,8 @@ class HTTPRequest(object):
mrbs = self.server.max_request_body_size
if mrbs and int(self.inheaders.get("Content-Length", 0)) > mrbs:
- self.simple_response("413 Request Entity Too Large",
+ self.simple_response(
+ "413 Request Entity Too Large",
"The entity sent with the request exceeds the maximum "
"allowed bytes.")
return False
@@ -763,7 +797,8 @@ class HTTPRequest(object):
# but it seems like it would be a big slowdown for such a rare case.
if self.inheaders.get("Expect", "") == "100-continue":
# Don't use simple_response here, because it emits headers
- # we don't want. See http://www.cherrypy.org/ticket/951
+ # we don't want. See
+ # https://bitbucket.org/cherrypy/cherrypy/issue/951
msg = self.server.protocol + " 100 Continue\r\n\r\n"
try:
self.conn.wfile.sendall(msg)
@@ -800,7 +835,8 @@ class HTTPRequest(object):
if i > 0 and QUESTION_MARK not in uri[:i]:
# An absoluteURI.
# If there's a scheme (and it must be http or https), then:
- # http_URL = "http:" "//" host [ ":" port ] [ abs_path [ "?" query ]]
+ # http_URL = "http:" "//" host [ ":" port ] [ abs_path [ "?" query
+ # ]]
scheme, remainder = uri[:i].lower(), uri[i + 3:]
authority, path = remainder.split(FORWARD_SLASH, 1)
path = FORWARD_SLASH + path
@@ -822,7 +858,8 @@ class HTTPRequest(object):
cl = int(self.inheaders.get("Content-Length", 0))
if mrbs and mrbs < cl:
if not self.sent_headers:
- self.simple_response("413 Request Entity Too Large",
+ self.simple_response(
+ "413 Request Entity Too Large",
"The entity sent with the request exceeds the maximum "
"allowed bytes.")
return
@@ -897,7 +934,7 @@ class HTTPRequest(object):
pass
else:
if (self.response_protocol == 'HTTP/1.1'
- and self.method != 'HEAD'):
+ and self.method != 'HEAD'):
# Use the chunked transfer-coding
self.chunked_write = True
self.outheaders.append(("Transfer-Encoding", "chunked"))
@@ -946,16 +983,19 @@ class HTTPRequest(object):
class NoSSLError(Exception):
+
"""Exception raised when a client speaks HTTP to an HTTPS socket."""
pass
class FatalSSLAlert(Exception):
+
"""Exception raised when the SSL implementation signals a fatal alert."""
pass
class CP_fileobject(socket._fileobject):
+
"""Faux file object attached to a socket object."""
def __init__(self, *args, **kwargs):
@@ -992,23 +1032,26 @@ class CP_fileobject(socket._fileobject):
return data
except socket.error, e:
if (e.args[0] not in socket_errors_nonblocking
- and e.args[0] not in socket_error_eintr):
+ and e.args[0] not in socket_error_eintr):
raise
if not _fileobject_uses_str_type:
def read(self, size=-1):
- # Use max, disallow tiny reads in a loop as they are very inefficient.
- # We never leave read() with any leftover data from a new recv() call
- # in our internal buffer.
+ # Use max, disallow tiny reads in a loop as they are very
+ # inefficient.
+ # We never leave read() with any leftover data from a new recv()
+ # call in our internal buffer.
rbufsize = max(self._rbufsize, self.default_bufsize)
- # Our use of StringIO rather than lists of string objects returned by
- # recv() minimizes memory usage and fragmentation that occurs when
- # rbufsize is large compared to the typical return value of recv().
+ # Our use of StringIO rather than lists of string objects returned
+ # by recv() minimizes memory usage and fragmentation that occurs
+ # when rbufsize is large compared to the typical return value of
+ # recv().
buf = self._rbuf
buf.seek(0, 2) # seek end
if size < 0:
# Read until EOF
- self._rbuf = StringIO.StringIO() # reset _rbuf. we consume it via buf.
+ # reset _rbuf. we consume it via buf.
+ self._rbuf = StringIO.StringIO()
while True:
data = self.recv(rbufsize)
if not data:
@@ -1019,14 +1062,16 @@ class CP_fileobject(socket._fileobject):
# Read until size bytes or EOF seen, whichever comes first
buf_len = buf.tell()
if buf_len >= size:
- # Already have size bytes in our buffer? Extract and return.
+ # Already have size bytes in our buffer? Extract and
+ # return.
buf.seek(0)
rv = buf.read(size)
self._rbuf = StringIO.StringIO()
self._rbuf.write(buf.read())
return rv
- self._rbuf = StringIO.StringIO() # reset _rbuf. we consume it via buf.
+ # reset _rbuf. we consume it via buf.
+ self._rbuf = StringIO.StringIO()
while True:
left = size - buf_len
# recv() will malloc the amount of memory given as its
@@ -1074,7 +1119,8 @@ class CP_fileobject(socket._fileobject):
# Speed up unbuffered case
buf.seek(0)
buffers = [buf.read()]
- self._rbuf = StringIO.StringIO() # reset _rbuf. we consume it via buf.
+ # reset _rbuf. we consume it via buf.
+ self._rbuf = StringIO.StringIO()
data = None
recv = self.recv
while data != "\n":
@@ -1085,7 +1131,8 @@ class CP_fileobject(socket._fileobject):
return "".join(buffers)
buf.seek(0, 2) # seek end
- self._rbuf = StringIO.StringIO() # reset _rbuf. we consume it via buf.
+ # reset _rbuf. we consume it via buf.
+ self._rbuf = StringIO.StringIO()
while True:
data = self.recv(self._rbufsize)
if not data:
@@ -1100,7 +1147,8 @@ class CP_fileobject(socket._fileobject):
buf.write(data)
return buf.getvalue()
else:
- # Read until size bytes or \n or EOF seen, whichever comes first
+ # Read until size bytes or \n or EOF seen, whichever comes
+ # first
buf.seek(0, 2) # seek end
buf_len = buf.tell()
if buf_len >= size:
@@ -1109,7 +1157,8 @@ class CP_fileobject(socket._fileobject):
self._rbuf = StringIO.StringIO()
self._rbuf.write(buf.read())
return rv
- self._rbuf = StringIO.StringIO() # reset _rbuf. we consume it via buf.
+ # reset _rbuf. we consume it via buf.
+ self._rbuf = StringIO.StringIO()
while True:
data = self.recv(self._rbufsize)
if not data:
@@ -1125,8 +1174,8 @@ class CP_fileobject(socket._fileobject):
buf.write(data[:nl])
break
else:
- # Shortcut. Avoid data copy through buf when returning
- # a substring of our first recv().
+ # Shortcut. Avoid data copy through buf when
+ # returning a substring of our first recv().
return data[:nl]
n = len(data)
if n == size and not buf_len:
@@ -1220,7 +1269,8 @@ class CP_fileobject(socket._fileobject):
break
return "".join(buffers)
else:
- # Read until size bytes or \n or EOF seen, whichever comes first
+ # Read until size bytes or \n or EOF seen, whichever comes
+ # first
nl = data.find('\n', 0, size)
if nl >= 0:
nl += 1
@@ -1256,6 +1306,7 @@ class CP_fileobject(socket._fileobject):
class HTTPConnection(object):
+
"""An HTTP connection (active socket).
server: the Server object which received this connection.
@@ -1273,8 +1324,8 @@ class HTTPConnection(object):
def __init__(self, server, sock, makefile=CP_fileobject):
self.server = server
self.socket = sock
- self.rfile = makefile(sock, "rb", self.rbufsize)
- self.wfile = makefile(sock, "wb", self.wbufsize)
+ self.rfile = makefile(sock._sock, "rb", self.rbufsize)
+ self.wfile = makefile(sock._sock, "wb", self.wbufsize)
self.requests_seen = 0
def communicate(self):
@@ -1306,11 +1357,14 @@ class HTTPConnection(object):
e = sys.exc_info()[1]
errnum = e.args[0]
# sadly SSL sockets return a different (longer) time out string
- if errnum == 'timed out' or errnum == 'The read operation timed out':
+ if (
+ errnum == 'timed out' or
+ errnum == 'The read operation timed out'
+ ):
# Don't error if we're between requests; only error
# if 1) no request has been started at all, or 2) we're
# in the middle of a request.
- # See http://www.cherrypy.org/ticket/853
+ # See https://bitbucket.org/cherrypy/cherrypy/issue/853
if (not request_seen) or (req and req.started_request):
# Don't bother writing the 408 if the response
# has already started being written.
@@ -1338,8 +1392,10 @@ class HTTPConnection(object):
except NoSSLError:
if req and not req.sent_headers:
# Unwrap our wfile
- self.wfile = CP_fileobject(self.socket._sock, "wb", self.wbufsize)
- req.simple_response("400 Bad Request",
+ self.wfile = CP_fileobject(
+ self.socket._sock, "wb", self.wbufsize)
+ req.simple_response(
+ "400 Bad Request",
"The client sent a plain HTTP request, but "
"this server only speaks HTTPS on this port.")
self.linger = True
@@ -1360,11 +1416,12 @@ class HTTPConnection(object):
self.rfile.close()
if not self.linger:
- # Python's socket module does NOT call close on the kernel socket
- # when you call socket.close(). We do so manually here because we
- # want this server to send a FIN TCP segment immediately. Note this
- # must be called *before* calling socket.close(), because the latter
- # drops its reference to the kernel socket.
+ # Python's socket module does NOT call close on the kernel
+ # socket when you call socket.close(). We do so manually here
+ # because we want this server to send a FIN TCP segment
+ # immediately. Note this must be called *before* calling
+ # socket.close(), because the latter drops its reference to
+ # the kernel socket.
if hasattr(self.socket, '_sock'):
self.socket._sock.close()
self.socket.close()
@@ -1379,9 +1436,13 @@ class HTTPConnection(object):
class TrueyZero(object):
- """An object which equals and does math like the integer '0' but evals True."""
+
+ """An object which equals and does math like the integer 0 but evals True.
+ """
+
def __add__(self, other):
return other
+
def __radd__(self, other):
return other
trueyzero = TrueyZero()
@@ -1389,7 +1450,9 @@ trueyzero = TrueyZero()
_SHUTDOWNREQUEST = None
+
class WorkerThread(threading.Thread):
+
"""Thread which continuously polls a Queue for Connection objects.
Due to the timing issues of polling a Queue, a WorkerThread does not
@@ -1409,7 +1472,6 @@ class WorkerThread(threading.Thread):
"""A simple flag for the calling server to know when this thread
has begun polling the Queue."""
-
def __init__(self, server):
self.ready = False
self.server = server
@@ -1420,12 +1482,30 @@ class WorkerThread(threading.Thread):
self.start_time = None
self.work_time = 0
self.stats = {
- 'Requests': lambda s: self.requests_seen + ((self.start_time is None) and trueyzero or self.conn.requests_seen),
- 'Bytes Read': lambda s: self.bytes_read + ((self.start_time is None) and trueyzero or self.conn.rfile.bytes_read),
- 'Bytes Written': lambda s: self.bytes_written + ((self.start_time is None) and trueyzero or self.conn.wfile.bytes_written),
- 'Work Time': lambda s: self.work_time + ((self.start_time is None) and trueyzero or time.time() - self.start_time),
- 'Read Throughput': lambda s: s['Bytes Read'](s) / (s['Work Time'](s) or 1e-6),
- 'Write Throughput': lambda s: s['Bytes Written'](s) / (s['Work Time'](s) or 1e-6),
+ 'Requests': lambda s: self.requests_seen + (
+ (self.start_time is None) and
+ trueyzero or
+ self.conn.requests_seen
+ ),
+ 'Bytes Read': lambda s: self.bytes_read + (
+ (self.start_time is None) and
+ trueyzero or
+ self.conn.rfile.bytes_read
+ ),
+ 'Bytes Written': lambda s: self.bytes_written + (
+ (self.start_time is None) and
+ trueyzero or
+ self.conn.wfile.bytes_written
+ ),
+ 'Work Time': lambda s: self.work_time + (
+ (self.start_time is None) and
+ trueyzero or
+ time.time() - self.start_time
+ ),
+ 'Read Throughput': lambda s: s['Bytes Read'](s) / (
+ s['Work Time'](s) or 1e-6),
+ 'Write Throughput': lambda s: s['Bytes Written'](s) / (
+ s['Work Time'](s) or 1e-6),
}
threading.Thread.__init__(self)
@@ -1458,6 +1538,7 @@ class WorkerThread(threading.Thread):
class ThreadPool(object):
+
"""A Request Queue for an HTTPServer which pools threads.
ThreadPool objects must provide min, get(), put(obj), start()
@@ -1495,13 +1576,29 @@ class ThreadPool(object):
def grow(self, amount):
"""Spawn new worker threads (not above self.max)."""
- for i in range(amount):
- if self.max > 0 and len(self._threads) >= self.max:
- break
- worker = WorkerThread(self.server)
- worker.setName("CP Server " + worker.getName())
- self._threads.append(worker)
- worker.start()
+ if self.max > 0:
+ budget = max(self.max - len(self._threads), 0)
+ else:
+ # self.max <= 0 indicates no maximum
+ budget = float('inf')
+
+ n_new = min(amount, budget)
+
+ workers = [self._spawn_worker() for i in range(n_new)]
+ while not self._all(operator.attrgetter('ready'), workers):
+ time.sleep(.1)
+ self._threads.extend(workers)
+
+ def _spawn_worker(self):
+ worker = WorkerThread(self.server)
+ worker.setName("CP Server " + worker.getName())
+ worker.start()
+ return worker
+
+ def _all(func, items):
+ results = [func(item) for item in items]
+ return reduce(operator.and_, results, True)
+ _all = staticmethod(_all)
def shrink(self, amount):
"""Kill off worker threads (not below self.min)."""
@@ -1512,13 +1609,17 @@ class ThreadPool(object):
self._threads.remove(t)
amount -= 1
- if amount > 0:
- for i in range(min(amount, len(self._threads) - self.min)):
- # Put a number of shutdown requests on the queue equal
- # to 'amount'. Once each of those is processed by a worker,
- # that worker will terminate and be culled from our list
- # in self.put.
- self._queue.put(_SHUTDOWNREQUEST)
+ # calculate the number of threads above the minimum
+ n_extra = max(len(self._threads) - self.min, 0)
+
+ # don't remove more than amount
+ n_to_remove = min(amount, n_extra)
+
+ # put shutdown requests on the queue equal to the number of threads
+ # to remove. As each request is processed by a worker, that worker
+ # will terminate and be culled from the list.
+ for n in range(n_to_remove):
+ self._queue.put(_SHUTDOWNREQUEST)
def stop(self, timeout=5):
# Must shut down threads here so the code that calls
@@ -1553,7 +1654,8 @@ class ThreadPool(object):
worker.join()
except (AssertionError,
# Ignore repeated Ctrl-C.
- # See http://www.cherrypy.org/ticket/691.
+ # See
+ # https://bitbucket.org/cherrypy/cherrypy/issue/691.
KeyboardInterrupt):
pass
@@ -1562,7 +1664,6 @@ class ThreadPool(object):
qsize = property(_get_qsize)
-
try:
import fcntl
except ImportError:
@@ -1594,12 +1695,14 @@ else:
class SSLAdapter(object):
+
"""Base class for SSL driver library adapters.
Required methods:
* ``wrap(sock) -> (wrapped socket, ssl environ dict)``
- * ``makefile(sock, mode='r', bufsize=DEFAULT_BUFFER_SIZE) -> socket file object``
+ * ``makefile(sock, mode='r', bufsize=DEFAULT_BUFFER_SIZE) ->
+ socket file object``
"""
def __init__(self, certificate, private_key, certificate_chain=None):
@@ -1615,6 +1718,7 @@ class SSLAdapter(object):
class HTTPServer(object):
+
"""An HTTP server."""
_bind_addr = "127.0.0.1"
@@ -1627,7 +1731,8 @@ class HTTPServer(object):
"""The minimum number of worker threads to create (default 10)."""
maxthreads = None
- """The maximum number of worker threads to create (default -1 = no limit)."""
+ """The maximum number of worker threads to create (default -1 = no limit).
+ """
server_name = None
"""The name of the server; defaults to socket.gethostname()."""
@@ -1639,15 +1744,18 @@ class HTTPServer(object):
features used in the response."""
request_queue_size = 5
- """The 'backlog' arg to socket.listen(); max queued connections (default 5)."""
+ """The 'backlog' arg to socket.listen(); max queued connections
+ (default 5).
+ """
shutdown_timeout = 5
- """The total time, in seconds, to wait for worker threads to cleanly exit."""
+ """The total time, in seconds, to wait for worker threads to cleanly exit.
+ """
timeout = 10
"""The timeout in seconds for accepted connections (default 10)."""
- version = "CherryPy/3.2.2"
+ version = "CherryPy/3.2.5"
"""A version string for the HTTPServer."""
software = None
@@ -1656,7 +1764,8 @@ class HTTPServer(object):
If None, this defaults to ``'%s Server' % self.version``."""
ready = False
- """An internal flag which marks whether the socket is accepting connections."""
+ """An internal flag which marks whether the socket is accepting connections
+ """
max_request_header_size = 0
"""The maximum size, in bytes, for request headers, or 0 for no limit."""
@@ -1700,14 +1809,15 @@ class HTTPServer(object):
'Threads': lambda s: len(getattr(self.requests, "_threads", [])),
'Threads Idle': lambda s: getattr(self.requests, "idle", None),
'Socket Errors': 0,
- 'Requests': lambda s: (not s['Enabled']) and -1 or sum([w['Requests'](w) for w
- in s['Worker Threads'].values()], 0),
- 'Bytes Read': lambda s: (not s['Enabled']) and -1 or sum([w['Bytes Read'](w) for w
- in s['Worker Threads'].values()], 0),
- 'Bytes Written': lambda s: (not s['Enabled']) and -1 or sum([w['Bytes Written'](w) for w
- in s['Worker Threads'].values()], 0),
- 'Work Time': lambda s: (not s['Enabled']) and -1 or sum([w['Work Time'](w) for w
- in s['Worker Threads'].values()], 0),
+ 'Requests': lambda s: (not s['Enabled']) and -1 or sum(
+ [w['Requests'](w) for w in s['Worker Threads'].values()], 0),
+ 'Bytes Read': lambda s: (not s['Enabled']) and -1 or sum(
+ [w['Bytes Read'](w) for w in s['Worker Threads'].values()], 0),
+ 'Bytes Written': lambda s: (not s['Enabled']) and -1 or sum(
+ [w['Bytes Written'](w) for w in s['Worker Threads'].values()],
+ 0),
+ 'Work Time': lambda s: (not s['Enabled']) and -1 or sum(
+ [w['Work Time'](w) for w in s['Worker Threads'].values()], 0),
'Read Throughput': lambda s: (not s['Enabled']) and -1 or sum(
[w['Bytes Read'](w) / (w['Work Time'](w) or 1e-6)
for w in s['Worker Threads'].values()], 0),
@@ -1715,7 +1825,7 @@ class HTTPServer(object):
[w['Bytes Written'](w) / (w['Work Time'](w) or 1e-6)
for w in s['Worker Threads'].values()], 0),
'Worker Threads': {},
- }
+ }
logging.statistics["CherryPy HTTPServer %d" % id(self)] = self.stats
def runtime(self):
@@ -1730,6 +1840,7 @@ class HTTPServer(object):
def _get_bind_addr(self):
return self._bind_addr
+
def _set_bind_addr(self, value):
if isinstance(value, tuple) and value[0] in ('', None):
# Despite the socket module docs, using '' does not
@@ -1746,7 +1857,9 @@ class HTTPServer(object):
"Use '0.0.0.0' (IPv4) or '::' (IPv6) instead "
"to listen on all active interfaces.")
self._bind_addr = value
- bind_addr = property(_get_bind_addr, _set_bind_addr,
+ bind_addr = property(
+ _get_bind_addr,
+ _set_bind_addr,
doc="""The interface on which to listen for connections.
For TCP sockets, a (host, port) tuple. Host values may be any IPv4
@@ -1771,14 +1884,14 @@ class HTTPServer(object):
# SSL backward compatibility
if (self.ssl_adapter is None and
- getattr(self, 'ssl_certificate', None) and
- getattr(self, 'ssl_private_key', None)):
+ getattr(self, 'ssl_certificate', None) and
+ getattr(self, 'ssl_private_key', None)):
warnings.warn(
- "SSL attributes are deprecated in CherryPy 3.2, and will "
- "be removed in CherryPy 3.3. Use an ssl_adapter attribute "
- "instead.",
- DeprecationWarning
- )
+ "SSL attributes are deprecated in CherryPy 3.2, and will "
+ "be removed in CherryPy 3.3. Use an ssl_adapter attribute "
+ "instead.",
+ DeprecationWarning
+ )
try:
from cherrypy.wsgiserver.ssl_pyopenssl import pyOpenSSLAdapter
except ImportError:
@@ -1793,21 +1906,28 @@ class HTTPServer(object):
# AF_UNIX socket
# So we can reuse the socket...
- try: os.unlink(self.bind_addr)
- except: pass
+ try:
+ os.unlink(self.bind_addr)
+ except:
+ pass
# So everyone can access the socket...
- try: os.chmod(self.bind_addr, 511) # 0777
- except: pass
+ try:
+ os.chmod(self.bind_addr, 511) # 0777
+ except:
+ pass
- info = [(socket.AF_UNIX, socket.SOCK_STREAM, 0, "", self.bind_addr)]
+ info = [
+ (socket.AF_UNIX, socket.SOCK_STREAM, 0, "", self.bind_addr)]
else:
# AF_INET or AF_INET6 socket
- # Get the correct address family for our host (allows IPv6 addresses)
+ # Get the correct address family for our host (allows IPv6
+ # addresses)
host, port = self.bind_addr
try:
- info = socket.getaddrinfo(host, port, socket.AF_UNSPEC,
- socket.SOCK_STREAM, 0, socket.AI_PASSIVE)
+ info = socket.getaddrinfo(
+ host, port, socket.AF_UNSPEC,
+ socket.SOCK_STREAM, 0, socket.AI_PASSIVE)
except socket.gaierror:
if ':' in self.bind_addr[0]:
info = [(socket.AF_INET6, socket.SOCK_STREAM,
@@ -1822,7 +1942,8 @@ class HTTPServer(object):
af, socktype, proto, canonname, sa = res
try:
self.bind(af, socktype, proto)
- except socket.error:
+ except socket.error, serr:
+ msg = "%s -- (%s: %s)" % (msg, sa, serr)
if self.socket:
self.socket.close()
self.socket = None
@@ -1877,11 +1998,13 @@ class HTTPServer(object):
self.socket = self.ssl_adapter.bind(self.socket)
# If listening on the IPV6 any address ('::' = IN6ADDR_ANY),
- # activate dual-stack. See http://www.cherrypy.org/ticket/871.
+ # activate dual-stack. See
+ # https://bitbucket.org/cherrypy/cherrypy/issue/871.
if (hasattr(socket, 'AF_INET6') and family == socket.AF_INET6
- and self.bind_addr[0] in ('::', '::0', '::0.0.0.0')):
+ and self.bind_addr[0] in ('::', '::0', '::0.0.0.0')):
try:
- self.socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
+ self.socket.setsockopt(
+ socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
except (AttributeError, socket.error):
# Apparently, the socket option is not available in
# this machine's TCP stack
@@ -1916,7 +2039,7 @@ class HTTPServer(object):
"Content-Type: text/plain\r\n\r\n",
msg]
- wfile = makefile(s, "wb", DEFAULT_BUFFER_SIZE)
+ wfile = makefile(s._sock, "wb", DEFAULT_BUFFER_SIZE)
try:
wfile.sendall("".join(buf))
except socket.error:
@@ -1936,7 +2059,7 @@ class HTTPServer(object):
if not isinstance(self.bind_addr, basestring):
# optional values
# Until we do DNS lookups, omit REMOTE_HOST
- if addr is None: # sometimes this can happen
+ if addr is None: # sometimes this can happen
# figure out if AF_INET or AF_INET6.
if len(s.getsockname()) == 2:
# AF_INET
@@ -1964,19 +2087,22 @@ class HTTPServer(object):
# is received during the accept() call; all docs say retry
# the call, and I *think* I'm reading it right that Python
# will then go ahead and poll for and handle the signal
- # elsewhere. See http://www.cherrypy.org/ticket/707.
+ # elsewhere. See
+ # https://bitbucket.org/cherrypy/cherrypy/issue/707.
return
if x.args[0] in socket_errors_nonblocking:
- # Just try again. See http://www.cherrypy.org/ticket/479.
+ # Just try again. See
+ # https://bitbucket.org/cherrypy/cherrypy/issue/479.
return
if x.args[0] in socket_errors_to_ignore:
# Our socket was closed.
- # See http://www.cherrypy.org/ticket/686.
+ # See https://bitbucket.org/cherrypy/cherrypy/issue/686.
return
raise
def _get_interrupt(self):
return self._interrupt
+
def _set_interrupt(self, interrupt):
self._interrupt = True
self.stop()
@@ -2002,7 +2128,8 @@ class HTTPServer(object):
x = sys.exc_info()[1]
if x.args[0] not in socket_errors_to_ignore:
# Changed to use error code and not message
- # See http://www.cherrypy.org/ticket/860.
+ # See
+ # https://bitbucket.org/cherrypy/cherrypy/issue/860.
raise
else:
# Note that we're explicitly NOT using AI_PASSIVE,
@@ -2015,8 +2142,9 @@ class HTTPServer(object):
s = None
try:
s = socket.socket(af, socktype, proto)
- # See http://groups.google.com/group/cherrypy-users/
- # browse_frm/thread/bbfe5eb39c904fe0
+ # See
+ # http://groups.google.com/group/cherrypy-users/
+ # browse_frm/thread/bbfe5eb39c904fe0
s.settimeout(1.0)
s.connect((host, port))
s.close()
@@ -2031,7 +2159,9 @@ class HTTPServer(object):
class Gateway(object):
- """A base class to interface HTTPServer with other systems, such as WSGI."""
+
+ """A base class to interface HTTPServer with other systems, such as WSGI.
+ """
def __init__(self, req):
self.req = req
@@ -2046,7 +2176,8 @@ class Gateway(object):
ssl_adapters = {
'builtin': 'cherrypy.wsgiserver.ssl_builtin.BuiltinSSLAdapter',
'pyopenssl': 'cherrypy.wsgiserver.ssl_pyopenssl.pyOpenSSLAdapter',
- }
+}
+
def get_ssl_adapter_class(name='pyopenssl'):
"""Return an SSL adapter class for the given name."""
@@ -2073,10 +2204,11 @@ def get_ssl_adapter_class(name='pyopenssl'):
return adapter
-# -------------------------------- WSGI Stuff -------------------------------- #
+# ------------------------------- WSGI Stuff -------------------------------- #
class CherryPyWSGIServer(HTTPServer):
+
"""A subclass of HTTPServer which calls a WSGI application."""
wsgi_version = (1, 0)
@@ -2100,12 +2232,14 @@ class CherryPyWSGIServer(HTTPServer):
def _get_numthreads(self):
return self.requests.min
+
def _set_numthreads(self, value):
self.requests.min = value
numthreads = property(_get_numthreads, _set_numthreads)
class WSGIGateway(Gateway):
+
"""A base class to interface HTTPServer with WSGI."""
def __init__(self, req):
@@ -2137,7 +2271,7 @@ class WSGIGateway(Gateway):
if hasattr(response, "close"):
response.close()
- def start_response(self, status, headers, exc_info = None):
+ def start_response(self, status, headers, exc_info=None):
"""WSGI callable to begin the HTTP response."""
# "The application may call start_response more than once,
# if and only if the exc_info argument is provided."
@@ -2158,9 +2292,11 @@ class WSGIGateway(Gateway):
self.req.status = status
for k, v in headers:
if not isinstance(k, str):
- raise TypeError("WSGI response header key %r is not of type str." % k)
+ raise TypeError(
+ "WSGI response header key %r is not of type str." % k)
if not isinstance(v, str):
- raise TypeError("WSGI response header value %r is not of type str." % v)
+ raise TypeError(
+ "WSGI response header value %r is not of type str." % v)
if k.lower() == 'content-length':
self.remaining_bytes_out = int(v)
self.req.outheaders.extend(headers)
@@ -2181,7 +2317,8 @@ class WSGIGateway(Gateway):
if rbo is not None and chunklen > rbo:
if not self.req.sent_headers:
# Whew. We can send a 500 to the client.
- self.req.simple_response("500 Internal Server Error",
+ self.req.simple_response(
+ "500 Internal Server Error",
"The requested resource returned more bytes than the "
"declared Content-Length.")
else:
@@ -2203,6 +2340,7 @@ class WSGIGateway(Gateway):
class WSGIGateway_10(WSGIGateway):
+
"""A Gateway class to interface HTTPServer with WSGI 1.0.x."""
def get_environ(self):
@@ -2231,7 +2369,7 @@ class WSGIGateway_10(WSGIGateway):
'wsgi.run_once': False,
'wsgi.url_scheme': req.scheme,
'wsgi.version': (1, 0),
- }
+ }
if isinstance(req.server.bind_addr, basestring):
# AF_UNIX. This isn't really allowed by WSGI, which doesn't
@@ -2259,17 +2397,19 @@ class WSGIGateway_10(WSGIGateway):
class WSGIGateway_u0(WSGIGateway_10):
+
"""A Gateway class to interface HTTPServer with WSGI u.0.
- WSGI u.0 is an experimental protocol, which uses unicode for keys and values
- in both Python 2 and Python 3.
+ WSGI u.0 is an experimental protocol, which uses unicode for keys and
+ values in both Python 2 and Python 3.
"""
def get_environ(self):
"""Return a new environ dict targeting the given wsgi.version"""
req = self.req
env_10 = WSGIGateway_10.get_environ(self)
- env = dict([(k.decode('ISO-8859-1'), v) for k, v in env_10.iteritems()])
+ env = dict([(k.decode('ISO-8859-1'), v)
+ for k, v in env_10.iteritems()])
env[u'wsgi.version'] = ('u', 0)
# Request-URI
@@ -2294,7 +2434,9 @@ wsgi_gateways = {
('u', 0): WSGIGateway_u0,
}
+
class WSGIPathInfoDispatcher(object):
+
"""A WSGI dispatcher for dispatch based on the PATH_INFO.
apps: a dict or list of (path_prefix, app) pairs.
@@ -2307,7 +2449,7 @@ class WSGIPathInfoDispatcher(object):
pass
# Sort the apps by len(path), descending
- apps.sort(cmp=lambda x,y: cmp(len(x[0]), len(y[0])))
+ apps.sort(cmp=lambda x, y: cmp(len(x[0]), len(y[0])))
apps.reverse()
# The path_prefix strings must start, but not end, with a slash.
@@ -2327,4 +2469,3 @@ class WSGIPathInfoDispatcher(object):
start_response('404 Not Found', [('Content-Type', 'text/plain'),
('Content-Length', '0')])
return ['']
-
diff --git a/cherrypy/wsgiserver/wsgiserver3.py b/cherrypy/wsgiserver/wsgiserver3.py
index e734a1a1..038e11f3 100644
--- a/cherrypy/wsgiserver/wsgiserver3.py
+++ b/cherrypy/wsgiserver/wsgiserver3.py
@@ -86,9 +86,12 @@ import re
import email.utils
import socket
import sys
-if 'win' in sys.platform and not hasattr(socket, 'IPPROTO_IPV6'):
- socket.IPPROTO_IPV6 = 41
-if sys.version_info < (3,1):
+if 'win' in sys.platform and hasattr(socket, "AF_INET6"):
+ if not hasattr(socket, 'IPPROTO_IPV6'):
+ socket.IPPROTO_IPV6 = 41
+ if not hasattr(socket, 'IPV6_V6ONLY'):
+ socket.IPV6_V6ONLY = 27
+if sys.version_info < (3, 1):
import io
else:
import _pyio as io
@@ -97,25 +100,27 @@ DEFAULT_BUFFER_SIZE = io.DEFAULT_BUFFER_SIZE
import threading
import time
from traceback import format_exc
-from urllib.parse import unquote
-from urllib.parse import urlparse
-from urllib.parse import scheme_chars
-import warnings
if sys.version_info >= (3, 0):
bytestr = bytes
unicodestr = str
basestring = (bytes, str)
+
def ntob(n, encoding='ISO-8859-1'):
- """Return the given native string as a byte string in the given encoding."""
+ """Return the given native string as a byte string in the given
+ encoding.
+ """
# In Python 3, the native string type is unicode
return n.encode(encoding)
else:
bytestr = str
unicodestr = unicode
basestring = basestring
+
def ntob(n, encoding='ISO-8859-1'):
- """Return the given native string as a byte string in the given encoding."""
+ """Return the given native string as a byte string in the given
+ encoding.
+ """
# In Python 2, the native string type is bytes. Assume it's already
# in the given encoding, which for ISO-8859-1 is almost always what
# was intended.
@@ -136,6 +141,7 @@ quoted_slash = re.compile(ntob("(?i)%2F"))
import errno
+
def plat_specific_errors(*errnames):
"""Return error numbers for all errors in errnames on this platform.
@@ -160,24 +166,27 @@ socket_errors_to_ignore = plat_specific_errors(
"ECONNABORTED", "WSAECONNABORTED",
"ENETRESET", "WSAENETRESET",
"EHOSTDOWN", "EHOSTUNREACH",
- )
+)
socket_errors_to_ignore.append("timed out")
socket_errors_to_ignore.append("The read operation timed out")
socket_errors_nonblocking = plat_specific_errors(
'EAGAIN', 'EWOULDBLOCK', 'WSAEWOULDBLOCK')
-comma_separated_headers = [ntob(h) for h in
+comma_separated_headers = [
+ ntob(h) for h in
['Accept', 'Accept-Charset', 'Accept-Encoding',
'Accept-Language', 'Accept-Ranges', 'Allow', 'Cache-Control',
'Connection', 'Content-Encoding', 'Content-Language', 'Expect',
'If-Match', 'If-None-Match', 'Pragma', 'Proxy-Authenticate', 'TE',
'Trailer', 'Transfer-Encoding', 'Upgrade', 'Vary', 'Via', 'Warning',
- 'WWW-Authenticate']]
+ 'WWW-Authenticate']
+]
import logging
-if not hasattr(logging, 'statistics'): logging.statistics = {}
+if not hasattr(logging, 'statistics'):
+ logging.statistics = {}
def read_headers(rfile, hdict=None):
@@ -232,7 +241,9 @@ def read_headers(rfile, hdict=None):
class MaxSizeExceeded(Exception):
pass
+
class SizeCheckWrapper(object):
+
"""Wraps a file-like object, raising MaxSizeExceeded if too large."""
def __init__(self, rfile, maxlen):
@@ -265,8 +276,8 @@ class SizeCheckWrapper(object):
self.bytes_read += len(data)
self._check_length()
res.append(data)
- # See http://www.cherrypy.org/ticket/421
- if len(data) < 256 or data[-1:] == "\n":
+ # See https://bitbucket.org/cherrypy/cherrypy/issue/421
+ if len(data) < 256 or data[-1:] == LF:
return EMPTY.join(res)
def readlines(self, sizehint=0):
@@ -302,6 +313,7 @@ class SizeCheckWrapper(object):
class KnownLengthRFile(object):
+
"""Wraps a file-like object, returning an empty string when exhausted."""
def __init__(self, rfile, content_length):
@@ -358,6 +370,7 @@ class KnownLengthRFile(object):
class ChunkedRFile(object):
+
"""Wraps a file-like object, returning an empty string when exhausted.
This class is intended to provide a conforming wsgi.input value for
@@ -407,8 +420,8 @@ class ChunkedRFile(object):
crlf = self.rfile.read(2)
if crlf != CRLF:
raise ValueError(
- "Bad chunked transfer coding (expected '\\r\\n', "
- "got " + repr(crlf) + ")")
+ "Bad chunked transfer coding (expected '\\r\\n', "
+ "got " + repr(crlf) + ")")
def read(self, size=None):
data = EMPTY
@@ -510,6 +523,7 @@ class ChunkedRFile(object):
class HTTPRequest(object):
+
"""An HTTP Request (and response).
A single HTTP connection may consist of multiple request/response pairs.
@@ -543,7 +557,7 @@ class HTTPRequest(object):
This value is set automatically inside send_headers."""
def __init__(self, server, conn):
- self.server= server
+ self.server = server
self.conn = conn
self.ready = False
@@ -569,7 +583,8 @@ class HTTPRequest(object):
try:
success = self.read_request_line()
except MaxSizeExceeded:
- self.simple_response("414 Request-URI Too Long",
+ self.simple_response(
+ "414 Request-URI Too Long",
"The Request-URI sent with the request exceeds the maximum "
"allowed bytes.")
return
@@ -580,7 +595,8 @@ class HTTPRequest(object):
try:
success = self.read_request_headers()
except MaxSizeExceeded:
- self.simple_response("413 Request Entity Too Large",
+ self.simple_response(
+ "413 Request Entity Too Large",
"The headers sent with the request exceed the maximum "
"allowed bytes.")
return
@@ -616,12 +632,14 @@ class HTTPRequest(object):
return False
if not request_line.endswith(CRLF):
- self.simple_response("400 Bad Request", "HTTP requires CRLF terminators")
+ self.simple_response(
+ "400 Bad Request", "HTTP requires CRLF terminators")
return False
try:
method, uri, req_protocol = request_line.strip().split(SPACE, 2)
- # The [x:y] slicing is necessary for byte strings to avoid getting ord's
+ # The [x:y] slicing is necessary for byte strings to avoid getting
+ # ord's
rp = int(req_protocol[5:6]), int(req_protocol[7:8])
except ValueError:
self.simple_response("400 Bad Request", "Malformed Request-Line")
@@ -676,7 +694,8 @@ class HTTPRequest(object):
# Notice that, in (b), the response will be "HTTP/1.1" even though
# the client only understands 1.0. RFC 2616 10.5.6 says we should
# only return 505 if the _major_ version is different.
- # The [x:y] slicing is necessary for byte strings to avoid getting ord's
+ # The [x:y] slicing is necessary for byte strings to avoid getting
+ # ord's
sp = int(self.server.protocol[5:6]), int(self.server.protocol[7:8])
if sp[0] != rp[0]:
@@ -700,7 +719,8 @@ class HTTPRequest(object):
mrbs = self.server.max_request_body_size
if mrbs and int(self.inheaders.get(b"Content-Length", 0)) > mrbs:
- self.simple_response("413 Request Entity Too Large",
+ self.simple_response(
+ "413 Request Entity Too Large",
"The entity sent with the request exceeds the maximum "
"allowed bytes.")
return False
@@ -754,8 +774,10 @@ class HTTPRequest(object):
# but it seems like it would be a big slowdown for such a rare case.
if self.inheaders.get(b"Expect", b"") == b"100-continue":
# Don't use simple_response here, because it emits headers
- # we don't want. See http://www.cherrypy.org/ticket/951
- msg = self.server.protocol.encode('ascii') + b" 100 Continue\r\n\r\n"
+ # we don't want. See
+ # https://bitbucket.org/cherrypy/cherrypy/issue/951
+ msg = self.server.protocol.encode(
+ 'ascii') + b" 100 Continue\r\n\r\n"
try:
self.conn.wfile.write(msg)
except socket.error:
@@ -791,9 +813,10 @@ class HTTPRequest(object):
if sep and QUESTION_MARK not in scheme:
# An absoluteURI.
# If there's a scheme (and it must be http or https), then:
- # http_URL = "http:" "//" host [ ":" port ] [ abs_path [ "?" query ]]
+ # http_URL = "http:" "//" host [ ":" port ] [ abs_path [ "?" query
+ # ]]
authority, path_a, path_b = remainder.partition(FORWARD_SLASH)
- return scheme.lower(), authority, path_a+path_b
+ return scheme.lower(), authority, path_a + path_b
if uri.startswith(FORWARD_SLASH):
# An abs_path.
@@ -823,9 +846,10 @@ class HTTPRequest(object):
cl = int(self.inheaders.get(b"Content-Length", 0))
if mrbs and mrbs < cl:
if not self.sent_headers:
- self.simple_response("413 Request Entity Too Large",
- "The entity sent with the request exceeds the maximum "
- "allowed bytes.")
+ self.simple_response(
+ "413 Request Entity Too Large",
+ "The entity sent with the request exceeds the "
+ "maximum allowed bytes.")
return
self.rfile = KnownLengthRFile(self.conn.rfile, cl)
@@ -898,7 +922,7 @@ class HTTPRequest(object):
pass
else:
if (self.response_protocol == 'HTTP/1.1'
- and self.method != b'HEAD'):
+ and self.method != b'HEAD'):
# Use the chunked transfer-coding
self.chunked_write = True
self.outheaders.append((b"Transfer-Encoding", b"chunked"))
@@ -934,14 +958,17 @@ class HTTPRequest(object):
self.rfile.read(remaining)
if b"date" not in hkeys:
- self.outheaders.append(
- (b"Date", email.utils.formatdate(usegmt=True).encode('ISO-8859-1')))
+ self.outheaders.append((
+ b"Date",
+ email.utils.formatdate(usegmt=True).encode('ISO-8859-1')
+ ))
if b"server" not in hkeys:
self.outheaders.append(
(b"Server", self.server.server_name.encode('ISO-8859-1')))
- buf = [self.server.protocol.encode('ascii') + SPACE + self.status + CRLF]
+ buf = [self.server.protocol.encode(
+ 'ascii') + SPACE + self.status + CRLF]
for k, v in self.outheaders:
buf.append(k + COLON + SPACE + v + CRLF)
buf.append(CRLF)
@@ -949,16 +976,19 @@ class HTTPRequest(object):
class NoSSLError(Exception):
+
"""Exception raised when a client speaks HTTP to an HTTPS socket."""
pass
class FatalSSLAlert(Exception):
+
"""Exception raised when the SSL implementation signals a fatal alert."""
pass
class CP_BufferedWriter(io.BufferedWriter):
+
"""Faux file object attached to a socket object."""
def write(self, b):
@@ -989,7 +1019,9 @@ def CP_makefile(sock, mode='r', bufsize=DEFAULT_BUFFER_SIZE):
else:
return CP_BufferedWriter(socket.SocketIO(sock, mode), bufsize)
+
class HTTPConnection(object):
+
"""An HTTP connection (active socket).
server: the Server object which received this connection.
@@ -1040,11 +1072,14 @@ class HTTPConnection(object):
e = sys.exc_info()[1]
errnum = e.args[0]
# sadly SSL sockets return a different (longer) time out string
- if errnum == 'timed out' or errnum == 'The read operation timed out':
+ if (
+ errnum == 'timed out' or
+ errnum == 'The read operation timed out'
+ ):
# Don't error if we're between requests; only error
# if 1) no request has been started at all, or 2) we're
# in the middle of a request.
- # See http://www.cherrypy.org/ticket/853
+ # See https://bitbucket.org/cherrypy/cherrypy/issue/853
if (not request_seen) or (req and req.started_request):
# Don't bother writing the 408 if the response
# has already started being written.
@@ -1072,10 +1107,12 @@ class HTTPConnection(object):
except NoSSLError:
if req and not req.sent_headers:
# Unwrap our wfile
- self.wfile = CP_makefile(self.socket._sock, "wb", self.wbufsize)
- req.simple_response("400 Bad Request",
- "The client sent a plain HTTP request, but "
- "this server only speaks HTTPS on this port.")
+ self.wfile = CP_makefile(
+ self.socket._sock, "wb", self.wbufsize)
+ req.simple_response(
+ "400 Bad Request",
+ "The client sent a plain HTTP request, but this server "
+ "only speaks HTTPS on this port.")
self.linger = True
except Exception:
e = sys.exc_info()[1]
@@ -1094,13 +1131,15 @@ class HTTPConnection(object):
self.rfile.close()
if not self.linger:
- # Python's socket module does NOT call close on the kernel socket
- # when you call socket.close(). We do so manually here because we
- # want this server to send a FIN TCP segment immediately. Note this
- # must be called *before* calling socket.close(), because the latter
- # drops its reference to the kernel socket.
- # Python 3 *probably* fixed this with socket._real_close; hard to tell.
-## self.socket._sock.close()
+ # Python's socket module does NOT call close on the kernel
+ # socket when you call socket.close(). We do so manually here
+ # because we want this server to send a FIN TCP segment
+ # immediately. Note this must be called *before* calling
+ # socket.close(), because the latter drops its reference to
+ # the kernel socket.
+ # Python 3 *probably* fixed this with socket._real_close;
+ # hard to tell.
+# self.socket._sock.close()
self.socket.close()
else:
# On the other hand, sometimes we want to hang around for a bit
@@ -1113,9 +1152,13 @@ class HTTPConnection(object):
class TrueyZero(object):
- """An object which equals and does math like the integer '0' but evals True."""
+
+ """An object which equals and does math like the integer 0 but evals True.
+ """
+
def __add__(self, other):
return other
+
def __radd__(self, other):
return other
trueyzero = TrueyZero()
@@ -1123,7 +1166,9 @@ trueyzero = TrueyZero()
_SHUTDOWNREQUEST = None
+
class WorkerThread(threading.Thread):
+
"""Thread which continuously polls a Queue for Connection objects.
Due to the timing issues of polling a Queue, a WorkerThread does not
@@ -1143,7 +1188,6 @@ class WorkerThread(threading.Thread):
"""A simple flag for the calling server to know when this thread
has begun polling the Queue."""
-
def __init__(self, server):
self.ready = False
self.server = server
@@ -1154,12 +1198,30 @@ class WorkerThread(threading.Thread):
self.start_time = None
self.work_time = 0
self.stats = {
- 'Requests': lambda s: self.requests_seen + ((self.start_time is None) and trueyzero or self.conn.requests_seen),
- 'Bytes Read': lambda s: self.bytes_read + ((self.start_time is None) and trueyzero or self.conn.rfile.bytes_read),
- 'Bytes Written': lambda s: self.bytes_written + ((self.start_time is None) and trueyzero or self.conn.wfile.bytes_written),
- 'Work Time': lambda s: self.work_time + ((self.start_time is None) and trueyzero or time.time() - self.start_time),
- 'Read Throughput': lambda s: s['Bytes Read'](s) / (s['Work Time'](s) or 1e-6),
- 'Write Throughput': lambda s: s['Bytes Written'](s) / (s['Work Time'](s) or 1e-6),
+ 'Requests': lambda s: self.requests_seen + (
+ (self.start_time is None) and
+ trueyzero or
+ self.conn.requests_seen
+ ),
+ 'Bytes Read': lambda s: self.bytes_read + (
+ (self.start_time is None) and
+ trueyzero or
+ self.conn.rfile.bytes_read
+ ),
+ 'Bytes Written': lambda s: self.bytes_written + (
+ (self.start_time is None) and
+ trueyzero or
+ self.conn.wfile.bytes_written
+ ),
+ 'Work Time': lambda s: self.work_time + (
+ (self.start_time is None) and
+ trueyzero or
+ time.time() - self.start_time
+ ),
+ 'Read Throughput': lambda s: s['Bytes Read'](s) / (
+ s['Work Time'](s) or 1e-6),
+ 'Write Throughput': lambda s: s['Bytes Written'](s) / (
+ s['Work Time'](s) or 1e-6),
}
threading.Thread.__init__(self)
@@ -1192,6 +1254,7 @@ class WorkerThread(threading.Thread):
class ThreadPool(object):
+
"""A Request Queue for an HTTPServer which pools threads.
ThreadPool objects must provide min, get(), put(obj), start()
@@ -1229,13 +1292,24 @@ class ThreadPool(object):
def grow(self, amount):
"""Spawn new worker threads (not above self.max)."""
- for i in range(amount):
- if self.max > 0 and len(self._threads) >= self.max:
- break
- worker = WorkerThread(self.server)
- worker.setName("CP Server " + worker.getName())
- self._threads.append(worker)
- worker.start()
+ if self.max > 0:
+ budget = max(self.max - len(self._threads), 0)
+ else:
+ # self.max <= 0 indicates no maximum
+ budget = float('inf')
+
+ n_new = min(amount, budget)
+
+ workers = [self._spawn_worker() for i in range(n_new)]
+ while not all(worker.ready for worker in workers):
+ time.sleep(.1)
+ self._threads.extend(workers)
+
+ def _spawn_worker(self):
+ worker = WorkerThread(self.server)
+ worker.setName("CP Server " + worker.getName())
+ worker.start()
+ return worker
def shrink(self, amount):
"""Kill off worker threads (not below self.min)."""
@@ -1246,13 +1320,17 @@ class ThreadPool(object):
self._threads.remove(t)
amount -= 1
- if amount > 0:
- for i in range(min(amount, len(self._threads) - self.min)):
- # Put a number of shutdown requests on the queue equal
- # to 'amount'. Once each of those is processed by a worker,
- # that worker will terminate and be culled from our list
- # in self.put.
- self._queue.put(_SHUTDOWNREQUEST)
+ # calculate the number of threads above the minimum
+ n_extra = max(len(self._threads) - self.min, 0)
+
+ # don't remove more than amount
+ n_to_remove = min(amount, n_extra)
+
+ # put shutdown requests on the queue equal to the number of threads
+ # to remove. As each request is processed by a worker, that worker
+ # will terminate and be culled from the list.
+ for n in range(n_to_remove):
+ self._queue.put(_SHUTDOWNREQUEST)
def stop(self, timeout=5):
# Must shut down threads here so the code that calls
@@ -1287,7 +1365,8 @@ class ThreadPool(object):
worker.join()
except (AssertionError,
# Ignore repeated Ctrl-C.
- # See http://www.cherrypy.org/ticket/691.
+ # See
+ # https://bitbucket.org/cherrypy/cherrypy/issue/691.
KeyboardInterrupt):
pass
@@ -1296,7 +1375,6 @@ class ThreadPool(object):
qsize = property(_get_qsize)
-
try:
import fcntl
except ImportError:
@@ -1328,12 +1406,14 @@ else:
class SSLAdapter(object):
+
"""Base class for SSL driver library adapters.
Required methods:
* ``wrap(sock) -> (wrapped socket, ssl environ dict)``
- * ``makefile(sock, mode='r', bufsize=DEFAULT_BUFFER_SIZE) -> socket file object``
+ * ``makefile(sock, mode='r', bufsize=DEFAULT_BUFFER_SIZE) ->
+ socket file object``
"""
def __init__(self, certificate, private_key, certificate_chain=None):
@@ -1349,6 +1429,7 @@ class SSLAdapter(object):
class HTTPServer(object):
+
"""An HTTP server."""
_bind_addr = "127.0.0.1"
@@ -1361,7 +1442,8 @@ class HTTPServer(object):
"""The minimum number of worker threads to create (default 10)."""
maxthreads = None
- """The maximum number of worker threads to create (default -1 = no limit)."""
+ """The maximum number of worker threads to create (default -1 = no limit).
+ """
server_name = None
"""The name of the server; defaults to socket.gethostname()."""
@@ -1373,15 +1455,18 @@ class HTTPServer(object):
features used in the response."""
request_queue_size = 5
- """The 'backlog' arg to socket.listen(); max queued connections (default 5)."""
+ """The 'backlog' arg to socket.listen(); max queued connections
+ (default 5).
+ """
shutdown_timeout = 5
- """The total time, in seconds, to wait for worker threads to cleanly exit."""
+ """The total time, in seconds, to wait for worker threads to cleanly exit.
+ """
timeout = 10
"""The timeout in seconds for accepted connections (default 10)."""
- version = "CherryPy/3.2.2"
+ version = "CherryPy/3.2.5"
"""A version string for the HTTPServer."""
software = None
@@ -1390,7 +1475,9 @@ class HTTPServer(object):
If None, this defaults to ``'%s Server' % self.version``."""
ready = False
- """An internal flag which marks whether the socket is accepting connections."""
+ """An internal flag which marks whether the socket is accepting
+ connections.
+ """
max_request_header_size = 0
"""The maximum size, in bytes, for request headers, or 0 for no limit."""
@@ -1434,14 +1521,15 @@ class HTTPServer(object):
'Threads': lambda s: len(getattr(self.requests, "_threads", [])),
'Threads Idle': lambda s: getattr(self.requests, "idle", None),
'Socket Errors': 0,
- 'Requests': lambda s: (not s['Enabled']) and -1 or sum([w['Requests'](w) for w
- in s['Worker Threads'].values()], 0),
- 'Bytes Read': lambda s: (not s['Enabled']) and -1 or sum([w['Bytes Read'](w) for w
- in s['Worker Threads'].values()], 0),
- 'Bytes Written': lambda s: (not s['Enabled']) and -1 or sum([w['Bytes Written'](w) for w
- in s['Worker Threads'].values()], 0),
- 'Work Time': lambda s: (not s['Enabled']) and -1 or sum([w['Work Time'](w) for w
- in s['Worker Threads'].values()], 0),
+ 'Requests': lambda s: (not s['Enabled']) and -1 or sum(
+ [w['Requests'](w) for w in s['Worker Threads'].values()], 0),
+ 'Bytes Read': lambda s: (not s['Enabled']) and -1 or sum(
+ [w['Bytes Read'](w) for w in s['Worker Threads'].values()], 0),
+ 'Bytes Written': lambda s: (not s['Enabled']) and -1 or sum(
+ [w['Bytes Written'](w) for w in s['Worker Threads'].values()],
+ 0),
+ 'Work Time': lambda s: (not s['Enabled']) and -1 or sum(
+ [w['Work Time'](w) for w in s['Worker Threads'].values()], 0),
'Read Throughput': lambda s: (not s['Enabled']) and -1 or sum(
[w['Bytes Read'](w) / (w['Work Time'](w) or 1e-6)
for w in s['Worker Threads'].values()], 0),
@@ -1449,7 +1537,7 @@ class HTTPServer(object):
[w['Bytes Written'](w) / (w['Work Time'](w) or 1e-6)
for w in s['Worker Threads'].values()], 0),
'Worker Threads': {},
- }
+ }
logging.statistics["CherryPy HTTPServer %d" % id(self)] = self.stats
def runtime(self):
@@ -1464,6 +1552,7 @@ class HTTPServer(object):
def _get_bind_addr(self):
return self._bind_addr
+
def _set_bind_addr(self, value):
if isinstance(value, tuple) and value[0] in ('', None):
# Despite the socket module docs, using '' does not
@@ -1480,7 +1569,9 @@ class HTTPServer(object):
"Use '0.0.0.0' (IPv4) or '::' (IPv6) instead "
"to listen on all active interfaces.")
self._bind_addr = value
- bind_addr = property(_get_bind_addr, _set_bind_addr,
+ bind_addr = property(
+ _get_bind_addr,
+ _set_bind_addr,
doc="""The interface on which to listen for connections.
For TCP sockets, a (host, port) tuple. Host values may be any IPv4
@@ -1508,21 +1599,28 @@ class HTTPServer(object):
# AF_UNIX socket
# So we can reuse the socket...
- try: os.unlink(self.bind_addr)
- except: pass
+ try:
+ os.unlink(self.bind_addr)
+ except:
+ pass
# So everyone can access the socket...
- try: os.chmod(self.bind_addr, 511) # 0777
- except: pass
+ try:
+ os.chmod(self.bind_addr, 511) # 0777
+ except:
+ pass
- info = [(socket.AF_UNIX, socket.SOCK_STREAM, 0, "", self.bind_addr)]
+ info = [
+ (socket.AF_UNIX, socket.SOCK_STREAM, 0, "", self.bind_addr)]
else:
# AF_INET or AF_INET6 socket
- # Get the correct address family for our host (allows IPv6 addresses)
+ # Get the correct address family for our host (allows IPv6
+ # addresses)
host, port = self.bind_addr
try:
info = socket.getaddrinfo(host, port, socket.AF_UNSPEC,
- socket.SOCK_STREAM, 0, socket.AI_PASSIVE)
+ socket.SOCK_STREAM, 0,
+ socket.AI_PASSIVE)
except socket.gaierror:
if ':' in self.bind_addr[0]:
info = [(socket.AF_INET6, socket.SOCK_STREAM,
@@ -1537,7 +1635,8 @@ class HTTPServer(object):
af, socktype, proto, canonname, sa = res
try:
self.bind(af, socktype, proto)
- except socket.error:
+ except socket.error as serr:
+ msg = "%s -- (%s: %s)" % (msg, sa, serr)
if self.socket:
self.socket.close()
self.socket = None
@@ -1591,11 +1690,13 @@ class HTTPServer(object):
self.socket = self.ssl_adapter.bind(self.socket)
# If listening on the IPV6 any address ('::' = IN6ADDR_ANY),
- # activate dual-stack. See http://www.cherrypy.org/ticket/871.
+ # activate dual-stack. See
+ # https://bitbucket.org/cherrypy/cherrypy/issue/871.
if (hasattr(socket, 'AF_INET6') and family == socket.AF_INET6
- and self.bind_addr[0] in ('::', '::0', '::0.0.0.0')):
+ and self.bind_addr[0] in ('::', '::0', '::0.0.0.0')):
try:
- self.socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
+ self.socket.setsockopt(
+ socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
except (AttributeError, socket.error):
# Apparently, the socket option is not available in
# this machine's TCP stack
@@ -1650,7 +1751,7 @@ class HTTPServer(object):
if not isinstance(self.bind_addr, basestring):
# optional values
# Until we do DNS lookups, omit REMOTE_HOST
- if addr is None: # sometimes this can happen
+ if addr is None: # sometimes this can happen
# figure out if AF_INET or AF_INET6.
if len(s.getsockname()) == 2:
# AF_INET
@@ -1678,19 +1779,22 @@ class HTTPServer(object):
# is received during the accept() call; all docs say retry
# the call, and I *think* I'm reading it right that Python
# will then go ahead and poll for and handle the signal
- # elsewhere. See http://www.cherrypy.org/ticket/707.
+ # elsewhere. See
+ # https://bitbucket.org/cherrypy/cherrypy/issue/707.
return
if x.args[0] in socket_errors_nonblocking:
- # Just try again. See http://www.cherrypy.org/ticket/479.
+ # Just try again. See
+ # https://bitbucket.org/cherrypy/cherrypy/issue/479.
return
if x.args[0] in socket_errors_to_ignore:
# Our socket was closed.
- # See http://www.cherrypy.org/ticket/686.
+ # See https://bitbucket.org/cherrypy/cherrypy/issue/686.
return
raise
def _get_interrupt(self):
return self._interrupt
+
def _set_interrupt(self, interrupt):
self._interrupt = True
self.stop()
@@ -1716,7 +1820,8 @@ class HTTPServer(object):
x = sys.exc_info()[1]
if x.args[0] not in socket_errors_to_ignore:
# Changed to use error code and not message
- # See http://www.cherrypy.org/ticket/860.
+ # See
+ # https://bitbucket.org/cherrypy/cherrypy/issue/860.
raise
else:
# Note that we're explicitly NOT using AI_PASSIVE,
@@ -1729,8 +1834,9 @@ class HTTPServer(object):
s = None
try:
s = socket.socket(af, socktype, proto)
- # See http://groups.google.com/group/cherrypy-users/
- # browse_frm/thread/bbfe5eb39c904fe0
+ # See
+ # http://groups.google.com/group/cherrypy-users/
+ # browse_frm/thread/bbfe5eb39c904fe0
s.settimeout(1.0)
s.connect((host, port))
s.close()
@@ -1745,7 +1851,9 @@ class HTTPServer(object):
class Gateway(object):
- """A base class to interface HTTPServer with other systems, such as WSGI."""
+
+ """A base class to interface HTTPServer with other systems, such as WSGI.
+ """
def __init__(self, req):
self.req = req
@@ -1759,7 +1867,8 @@ class Gateway(object):
# of such classes (in which case they will be lazily loaded).
ssl_adapters = {
'builtin': 'cherrypy.wsgiserver.ssl_builtin.BuiltinSSLAdapter',
- }
+}
+
def get_ssl_adapter_class(name='builtin'):
"""Return an SSL adapter class for the given name."""
@@ -1786,10 +1895,11 @@ def get_ssl_adapter_class(name='builtin'):
return adapter
-# -------------------------------- WSGI Stuff -------------------------------- #
+# ------------------------------- WSGI Stuff -------------------------------- #
class CherryPyWSGIServer(HTTPServer):
+
"""A subclass of HTTPServer which calls a WSGI application."""
wsgi_version = (1, 0)
@@ -1813,12 +1923,14 @@ class CherryPyWSGIServer(HTTPServer):
def _get_numthreads(self):
return self.requests.min
+
def _set_numthreads(self, value):
self.requests.min = value
numthreads = property(_get_numthreads, _set_numthreads)
class WSGIGateway(Gateway):
+
"""A base class to interface HTTPServer with WSGI."""
def __init__(self, req):
@@ -1850,7 +1962,7 @@ class WSGIGateway(Gateway):
if hasattr(response, "close"):
response.close()
- def start_response(self, status, headers, exc_info = None):
+ def start_response(self, status, headers, exc_info=None):
"""WSGI callable to begin the HTTP response."""
# "The application may call start_response more than once,
# if and only if the exc_info argument is provided."
@@ -1878,12 +1990,15 @@ class WSGIGateway(Gateway):
for k, v in headers:
if not isinstance(k, str):
- raise TypeError("WSGI response header key %r is not of type str." % k)
+ raise TypeError(
+ "WSGI response header key %r is not of type str." % k)
if not isinstance(v, str):
- raise TypeError("WSGI response header value %r is not of type str." % v)
+ raise TypeError(
+ "WSGI response header value %r is not of type str." % v)
if k.lower() == 'content-length':
self.remaining_bytes_out = int(v)
- self.req.outheaders.append((k.encode('ISO-8859-1'), v.encode('ISO-8859-1')))
+ self.req.outheaders.append(
+ (k.encode('ISO-8859-1'), v.encode('ISO-8859-1')))
return self.write
@@ -1902,8 +2017,9 @@ class WSGIGateway(Gateway):
if not self.req.sent_headers:
# Whew. We can send a 500 to the client.
self.req.simple_response("500 Internal Server Error",
- "The requested resource returned more bytes than the "
- "declared Content-Length.")
+ "The requested resource returned "
+ "more bytes than the declared "
+ "Content-Length.")
else:
# Dang. We have probably already sent data. Truncate the chunk
# to fit (so the client doesn't hang) and raise an error later.
@@ -1923,6 +2039,7 @@ class WSGIGateway(Gateway):
class WSGIGateway_10(WSGIGateway):
+
"""A Gateway class to interface HTTPServer with WSGI 1.0.x."""
def get_environ(self):
@@ -1938,7 +2055,7 @@ class WSGIGateway_10(WSGIGateway):
'REMOTE_ADDR': req.conn.remote_addr or '',
'REMOTE_PORT': str(req.conn.remote_port or ''),
'REQUEST_METHOD': req.method.decode('ISO-8859-1'),
- 'REQUEST_URI': req.uri,
+ 'REQUEST_URI': req.uri.decode('ISO-8859-1'),
'SCRIPT_NAME': '',
'SERVER_NAME': req.server.server_name,
# Bah. "SERVER_PROTOCOL" is actually the REQUEST protocol.
@@ -1951,8 +2068,7 @@ class WSGIGateway_10(WSGIGateway):
'wsgi.run_once': False,
'wsgi.url_scheme': req.scheme.decode('ISO-8859-1'),
'wsgi.version': (1, 0),
- }
-
+ }
if isinstance(req.server.bind_addr, basestring):
# AF_UNIX. This isn't really allowed by WSGI, which doesn't
# address unix domain sockets. But it's better than nothing.
@@ -1980,10 +2096,11 @@ class WSGIGateway_10(WSGIGateway):
class WSGIGateway_u0(WSGIGateway_10):
+
"""A Gateway class to interface HTTPServer with WSGI u.0.
- WSGI u.0 is an experimental protocol, which uses unicode for keys and values
- in both Python 2 and Python 3.
+ WSGI u.0 is an experimental protocol, which uses unicode for keys
+ and values in both Python 2 and Python 3.
"""
def get_environ(self):
@@ -2012,7 +2129,9 @@ wsgi_gateways = {
('u', 0): WSGIGateway_u0,
}
+
class WSGIPathInfoDispatcher(object):
+
"""A WSGI dispatcher for dispatch based on the PATH_INFO.
apps: a dict or list of (path_prefix, app) pairs.
@@ -2045,4 +2164,3 @@ class WSGIPathInfoDispatcher(object):
start_response('404 Not Found', [('Content-Type', 'text/plain'),
('Content-Length', '0')])
return ['']
-
diff --git a/make-sdist b/make-sdist
index 6d1ba84d..0d2f57e4 100644
--- a/make-sdist
+++ b/make-sdist
@@ -1,2 +1,2 @@
rm MANIFEST
-python setup.py sdist --formats=gztar
+python setup.py sdist
diff --git a/release.py b/release.py
new file mode 100644
index 00000000..a6b86a2e
--- /dev/null
+++ b/release.py
@@ -0,0 +1,147 @@
+"""
+This script will walk a developer through the process of cutting a release.
+
+Based on https://bitbucket.org/cherrypy/cherrypy/wiki/ReleaseProcess
+
+To cut a release, simply invoke this script at the changeset to be released.
+"""
+
+from __future__ import print_function
+
+import subprocess
+import sys
+import os
+import platform
+import shutil
+import importlib
+
+VERSION = '3.2.5'
+
+if sys.version_info < (3,):
+ input = raw_input
+
+
+def get_next_version():
+ print("The last release on this branch was {version}".format(
+ version=VERSION))
+ return input("What version are you releasing? ")
+
+NEXT_VERSION = get_next_version()
+
+files_with_versions = ('release.py', 'setup.py', 'cherrypy/__init__.py',
+ 'cherrypy/wsgiserver/wsgiserver2.py',
+ 'cherrypy/wsgiserver/wsgiserver3.py',
+ )
+
+
+def check_wheel():
+ """
+ Ensure 'wheel' is installed (required for bdist_wheel).
+ """
+ try:
+ importlib.import_module('wheel')
+ except ImportError:
+ print("CherryPy requires 'wheel' be installed to produce wheels.",
+ file=sys.stderr)
+ raise SystemExit(5)
+
+
+def check_status():
+ """
+ Make sure there aren't outstanding changesets that are unpushed, maybe
+ run the tests or ask the user if the tests are passing.
+ """
+ print("You're about to release CherryPy {NEXT_VERSION}".format(
+ **globals()))
+ res = input('Have you run the tests with `nosetests -s ./` on '
+ 'Windows, Linux, and Mac on at least Python '
+ '2.4, 2.5, 2.7, and 3.2? '
+ .format(**globals()))
+ if not res.lower().startswith('y'):
+ print("Please do that")
+ raise SystemExit(1)
+
+
+def bump_versions():
+ """
+ Bump the versions in each of the places where it appears and commit.
+ """
+ list(map(bump_version, files_with_versions))
+
+ subprocess.check_call(['hg', 'ci', '-m',
+ 'Bumped to {NEXT_VERSION} in preparation for next '
+ 'release.'.format(**globals())])
+
+
+def bump_version(filename):
+ with open(filename, 'rb') as f:
+ b_version = VERSION.encode('ascii')
+ b_next_version = NEXT_VERSION.encode('ascii')
+ lines = [line.replace(b_version, b_next_version) for line in f]
+ with open(filename, 'wb') as f:
+ f.writelines(lines)
+
+
+def tag_release():
+ """
+ Tag the release.
+ """
+ subprocess.check_call(['hg', 'tag', NEXT_VERSION])
+
+dist_commands = ['sdist', 'bdist_wininst', 'bdist_wheel']
+
+
+def build():
+ if os.path.isfile('MANIFEST'):
+ os.remove('MANIFEST')
+ if os.path.isdir('dist'):
+ shutil.rmtree('dist')
+ subprocess.check_call([sys.executable, 'setup.py'] + dist_commands)
+
+
+def push():
+ "The build went well, so let's push the SCM changesets"
+ subprocess.check_call(['hg', 'push', '-r', '.'])
+
+
+def publish():
+ """
+ Publish the dists on PyPI
+ """
+ try:
+ upload_dist_command = [sys.executable, 'setup.py'] + dist_commands + ['register', 'upload']
+ subprocess.check_call(upload_dist_command)
+ except:
+ print("Unable to upload the dist files. Ask in IRC for help access 57"
+ "or assistance.")
+ raise SystemExit(4)
+ print('Distributions have been uploaded.')
+ print('Please ask in IRC for others to help you test '
+ 'CherryPy {NEXT_VERSION}.'
+ .format(**globals()))
+ print("Please confirm that the distro installs properly "
+ "with `easy_install CherryPy=={NEXT_VERSION}`.".format(**globals()))
+
+
+def announce():
+ print("Please change the Wiki: Home page (news), CherryPyDownload")
+ print("Please announce the release on newsgroups, mailing lists, "
+ "and IRC /topic.")
+
+
+def main():
+ assert sys.version_info >= (2, 6), ("Release script requires Python 2.6 "
+ "or later.")
+ assert platform.system() == 'Windows', ('You must release on Windows '
+ '(to create Windows installers)')
+ check_wheel()
+ check_status()
+ bump_versions()
+ tag_release()
+ build()
+ push()
+ publish()
+ announce()
+
+if __name__ == '__main__':
+ main()
diff --git a/setup.cfg b/setup.cfg
new file mode 100644
index 00000000..8a8e58cd
--- /dev/null
+++ b/setup.cfg
@@ -0,0 +1,11 @@
+[sdist]
+formats=gztar,zip
+
+[nosetests]
+where=cherrypy
+logging-filter=cherrypy
+verbosity=2
+nocapture=True
+
+[wheel]
+universal=1
diff --git a/setup.py b/setup.py
index a7ba2327..1b7f90bf 100644
--- a/setup.py
+++ b/setup.py
@@ -14,19 +14,21 @@ except ImportError:
from distutils.command.install import INSTALL_SCHEMES
from distutils.command.build_py import build_py
import sys
-import os
import re
+
class cherrypy_build_py(build_py):
- "Custom version of build_py that selects Python-specific wsgiserver"
+ """Custom version of build_py that excludes Python-specific modules"""
+
def build_module(self, module, module_file, package):
python3 = sys.version_info >= (3,)
if python3:
- exclude_pattern = re.compile('wsgiserver2|ssl_pyopenssl')
+ exclude_pattern = re.compile('wsgiserver2|ssl_pyopenssl|'
+ '_cpcompat_subprocess')
else:
exclude_pattern = re.compile('wsgiserver3')
if exclude_pattern.match(module):
- return # skip it
+ return # skip it
return build_py.build_module(self, module, module_file, package)
@@ -34,18 +36,30 @@ class cherrypy_build_py(build_py):
# arguments for the setup command
###############################################################################
name = "CherryPy"
-version = "3.2.2"
+version = "3.2.5"
desc = "Object-Oriented HTTP framework"
long_desc = "CherryPy is a pythonic, object-oriented HTTP framework"
-classifiers=[
+classifiers = [
"Development Status :: 5 - Production/Stable",
"Environment :: Web Environment",
"Intended Audience :: Developers",
"License :: Freely Distributable",
"Operating System :: OS Independent",
+ "Framework :: CherryPy",
+ "License :: OSI Approved :: BSD License",
"Programming Language :: Python",
"Programming Language :: Python :: 2",
+ "Programming Language :: Python :: 2.3",
+ "Programming Language :: Python :: 2.4",
+ "Programming Language :: Python :: 2.5",
+ "Programming Language :: Python :: 2.6",
+ "Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
+ "Programming Language :: Python :: 3.3",
+ "Programming Language :: Python :: Implementation",
+ "Programming Language :: Python :: Implementation :: CPython",
+ "Programming Language :: Python :: Implementation :: Jython",
+ "Programming Language :: Python :: Implementation :: PyPy",
"Topic :: Internet :: WWW/HTTP",
"Topic :: Internet :: WWW/HTTP :: Dynamic Content",
"Topic :: Internet :: WWW/HTTP :: HTTP Servers",
@@ -54,19 +68,18 @@ classifiers=[
"Topic :: Internet :: WWW/HTTP :: WSGI :: Server",
"Topic :: Software Development :: Libraries :: Application Frameworks",
]
-author="CherryPy Team"
-author_email="team@cherrypy.org"
-url="http://www.cherrypy.org"
-cp_license="BSD"
-packages=[
+author = "CherryPy Team"
+author_email = "team@cherrypy.org"
+url = "http://www.cherrypy.org"
+cp_license = "BSD"
+packages = [
"cherrypy", "cherrypy.lib",
"cherrypy.tutorial", "cherrypy.test",
"cherrypy.process",
"cherrypy.scaffold",
"cherrypy.wsgiserver",
]
-download_url="http://download.cherrypy.org/cherrypy/3.2.2/"
-data_files=[
+data_files = [
('cherrypy', ['cherrypy/cherryd',
'cherrypy/favicon.ico',
'cherrypy/LICENSE.txt',
@@ -75,13 +88,13 @@ data_files=[
('cherrypy/scaffold', ['cherrypy/scaffold/example.conf',
'cherrypy/scaffold/site.conf',
]),
- ('cherrypy/scaffold/static', ['cherrypy/scaffold/static/made_with_cherrypy_small.png',
- ]),
+ ('cherrypy/scaffold/static', [
+ 'cherrypy/scaffold/static/made_with_cherrypy_small.png']),
('cherrypy/test', ['cherrypy/test/style.css',
'cherrypy/test/test.pem',
]),
('cherrypy/test/static', ['cherrypy/test/static/index.html',
- 'cherrypy/test/static/dirback.jpg',]),
+ 'cherrypy/test/static/dirback.jpg', ]),
('cherrypy/tutorial',
[
'cherrypy/tutorial/tutorial.conf',
@@ -89,12 +102,12 @@ data_files=[
'cherrypy/tutorial/pdf_file.pdf',
'cherrypy/tutorial/custom_error.html',
]
- ),
+ ),
]
scripts = ["cherrypy/cherryd"]
cmd_class = dict(
- build_py = cherrypy_build_py,
+ build_py=cherrypy_build_py,
)
if sys.version_info >= (3, 0):
@@ -108,10 +121,12 @@ else:
# wininst may install data_files in Python/x.y instead of the cherrypy package.
# Django's solution is at http://code.djangoproject.com/changeset/8313
-# See also http://mail.python.org/pipermail/distutils-sig/2004-August/004134.html
+# See also
+# http://mail.python.org/pipermail/distutils-sig/2004-August/004134.html
if 'bdist_wininst' in sys.argv or '--format=wininst' in sys.argv:
data_files = [(r'\PURELIB\%s' % path, files) for path, files in data_files]
+
def main():
if sys.version < required_python_version:
s = "I'm sorry, but %s %s requires Python %s or later."
@@ -122,7 +137,7 @@ def main():
for scheme in list(INSTALL_SCHEMES.values()):
scheme['data'] = scheme['purelib']
- dist = setup(
+ setup(
name=name,
version=version,
description=desc,
@@ -133,7 +148,6 @@ def main():
url=url,
license=cp_license,
packages=packages,
- download_url=download_url,
data_files=data_files,
scripts=scripts,
cmdclass=cmd_class,
diff --git a/sphinx/source/_static/bgsides.png b/sphinx/source/_static/bgsides.png
new file mode 100644
index 00000000..27c305fa
--- /dev/null
+++ b/sphinx/source/_static/bgsides.png
Binary files differ
diff --git a/sphinx/source/_static/cpdocmain.css b/sphinx/source/_static/cpdocmain.css
index 57716dd5..4414d472 100644
--- a/sphinx/source/_static/cpdocmain.css
+++ b/sphinx/source/_static/cpdocmain.css
@@ -10,14 +10,14 @@
body {
font-family: sans-serif;
font-size: 100%;
- background-color: #880000;
+ background: url("bgsides.png") repeat;
color: #000;
margin: 0;
padding: 0;
}
div.document {
- background-color: #880000;
+ margin: 30px 0;
}
div.documentwrapper {
@@ -32,11 +32,13 @@ div.bodywrapper {
div.body {
background-color: #ffffff;
color: #000000;
- padding: 0 20px 30px 20px;
+ padding: 20px 40px;
+ border-radius: 3px 0 0 3px;
+ box-shadow: -1px 1px 5px #ddd;
}
div.footer {
- color: white;
+ color: #BF0000;
width: 100%;
padding: 9px 0 9px 0;
text-align: center;
@@ -44,18 +46,20 @@ div.footer {
}
div.footer a {
- color: white;
+ color: #BF0000;
text-decoration: underline;
}
div.related {
- background-color: #880000;
+ background: #BF0000;
line-height: 30px;
- color: white;
+ color: #ff9393;
+ font-family: verdana;
+ font-size: 11px;
}
div.related a {
- color: #FFEEEE;
+ color: #fff;
}
div.sphinxsidebar {
@@ -63,7 +67,7 @@ div.sphinxsidebar {
div.sphinxsidebar h3 {
font-family: 'Trebuchet MS', sans-serif;
- color: white;
+ color: #BF0000;
font-size: 1.4em;
font-weight: normal;
margin: 0;
@@ -71,12 +75,12 @@ div.sphinxsidebar h3 {
}
div.sphinxsidebar h3 a {
- color: white;
+ color: #BF0000;
}
div.sphinxsidebar h4 {
font-family: 'Trebuchet MS', sans-serif;
- color: white;
+ color: #BF0000;
font-size: 1.3em;
font-weight: normal;
margin: 5px 0 0 0;
@@ -84,7 +88,7 @@ div.sphinxsidebar h4 {
}
div.sphinxsidebar p {
- color: white;
+ color: #BF0000;
}
div.sphinxsidebar p.topless {
@@ -94,11 +98,12 @@ div.sphinxsidebar p.topless {
div.sphinxsidebar ul {
margin: 10px;
padding: 0;
- color: white;
+ color: #BF0000;
}
div.sphinxsidebar a {
- color: #FFEEEE;
+ color: #666666
+
}
div.sphinxsidebar input {
@@ -130,7 +135,7 @@ div.body h4,
div.body h5,
div.body h6 {
font-family: 'Trebuchet MS', sans-serif;
- background-color: #FFF8FB;
+ background-color: white;
font-weight: normal;
color: black;
border-bottom: 1px solid #ccc;
@@ -154,7 +159,7 @@ a.headerlink {
a.headerlink:hover {
background-color: #660000;
- color: white;
+ color: #BF0000;
}
div.body p, div.body dd, div.body li {
@@ -199,7 +204,7 @@ p.admonition-title:after {
pre {
padding: 5px;
- background-color: #FFEEEE;
+ background-color: #F9F9F9;
color: #333333;
line-height: 120%;
border: 1px solid #ac9;
@@ -224,3 +229,6 @@ table.docutils td {
padding: 0.25em;
}
+div.sphinxsidebarwrapper {
+ padding: 10px 5px 0 25px;
+} \ No newline at end of file
diff --git a/sphinx/source/appendix/cherrypyspeed.rst b/sphinx/source/appendix/cherrypyspeed.rst
index 6659958e..90580a40 100644
--- a/sphinx/source/appendix/cherrypyspeed.rst
+++ b/sphinx/source/appendix/cherrypyspeed.rst
@@ -1,65 +1,96 @@
.. _cherrypyspeed:
-**********************
-How fast is CherryPy ?
-**********************
+*********************
+How fast is CherryPy?
+*********************
Introduction
============
-When people ask this question, they usually mean "how fast will my CherryPy-based application be ?".
+When people ask this question, they usually mean "how fast will my CherryPy-based application be?".
-In 99% of the cases, the answer is "this depends on your actual application code, not on !CherryPy itself".
+In 99% of the cases, the answer is "this depends on your actual application code, not on CherryPy itself".
The reason is that, for 99% of the real-world dynamic applications, most of the time spent to return a page will be spent in your actual application code, and the time actually spent in the CherryPy code will be negligible.
For instance, a typical page that requires a few database calls to be built might take in total 200ms to be served. Out of these 200ms, about 2ms will be spent by CherryPy itself, and 198ms will be spend in your actual database calls and page rendering...
-So you can see that, if you want to optimize anything, you should really optimize your actual application code before you try to optimize !CherryPy
+So you can see that, if you want to optimize anything, you should really optimize your actual application code before you try to optimize CherryPy.
+
Raw speed of the CherryPy HTTP server
=====================================
Despite the real-life most common scenario explained in the introduction, some people still want to know the raw speed of the CherryPy HTTP server.
-So I sat down and did some benchmarking...
About the benchmark
-------------------
-This benchmarking only makes sense on very small documents, otherwise we're no longer measuring the raw speed of the HTTP server, but also the speed of the application ...
+This benchmarking only makes sense on very small documents, otherwise we're no longer measuring the raw speed of the HTTP server, but also the speed of the application.
+
+.. warning::
+
+ This benchmark uses the ``ab`` tool from the Apache project, it's far from being a perfect tool nor does it provide a realistic user workflow. Take these results lightly and always perform real load/performance tests in your environment with better tools.
This benchmarking was performed on a laptop in the following environment:
- * Processor: Pentium M 1.6 Ghz
- * RAM: 1GB
- * Windows XP 2
+ * Processor: Intel® Core™ i3-2330M CPU @ 2.20GHz × 4
+ * RAM: 4GB
+ * Ubuntu 13.10
+ * Python 2.7
+ * CherryPy 3.2.4
* Load testing tool: ab from Apache2
- * CherryPy version: SVN snapshot on 2005/01/13
-Note that "ab" was running on the same machine as the CherryPy server, so CherryPy is probably a bit faster than what we're getting.
+We used the following basic CherryPy app:
-Test 1: Dynamic content / single threaded server
-------------------------------------------------
+.. code-block:: python
-I used the following basic CherryPy app::
+ import cherrypy
- #!python
- from cherrypy import cpg
- class Root:
+ class Root(object):
def index(self):
return "OK"
index.exposed = True
-
- cpg.root = Root()
-
- cpg.server.start(configMap = {'socketPort': 10000})
-Here are the "ab" results::
+ if __name__ == "__main__":
+ # remove the default CherryPy enabled tools
+ # to really check the speed of the request processing stack
+ cherrypy.config.clear()
+
+ # we don't need the logging for this test
+ cherrypy.config.update({'log.screen': False})
+
+ cherrypy.quickstart(Root())
+
+For the raw WSGI server tests, we used the following code:
+
+.. code-block:: python
+
+ from cherrypy import wsgiserver
+
+ def my_crazy_app(environ, start_response):
+ start_response('200 OK', [('Content-type','text/plain')])
+ return ['OK']
+
+ if __name__ == '__main__':
+ server = wsgiserver.CherryPyWSGIServer(
+ ('0.0.0.0', 8080), my_crazy_app)
+ server.stats['Enabled'] = False
+ try:
+ server.start()
+ except KeyboardInterrupt:
+ server.stop()
+
+
+Test 1: Dynamic content / no concurrent connections
+---------------------------------------------------
+
+.. code-block:: text
+
+ $ ab -n 1000 http://localhost:8080/
+ This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
+ Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
+ Licensed to The Apache Software Foundation, http://www.apache.org/
- $ ./ab.exe -n 1000 http://localhost:10000/
- This is ApacheBench, Version 2.0.41-dev <$Revision: 1.121.2.12 $> apache-2.0
- Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
- Copyright (c) 1998-2002 The Apache Software Foundation, http://www.apache.org/
-
Benchmarking localhost (be patient)
Completed 100 requests
Completed 200 requests
@@ -70,60 +101,58 @@ Here are the "ab" results::
Completed 700 requests
Completed 800 requests
Completed 900 requests
+ Completed 1000 requests
Finished 1000 requests
-
-
- Server Software: CherryPy/2.0.0b
+
+
+ Server Software: CherryPy/3.2.4
Server Hostname: localhost
- Server Port: 10000
-
+ Server Port: 8080
+
Document Path: /
Document Length: 2 bytes
-
+
Concurrency Level: 1
- Time taken for tests: 1.789044 seconds
+ Time taken for tests: 2.414 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
- Total transferred: 127000 bytes
+ Total transferred: 126000 bytes
HTML transferred: 2000 bytes
- Requests per second: 558.96 [#/sec] (mean)
- Time per request: 1.789 [ms] (mean)
- Time per request: 1.789 [ms] (mean, across all concurrent requests)
- Transfer rate: 69.31 [Kbytes/sec] received
-
+ Requests per second: 414.19 [#/sec] (mean)
+ Time per request: 2.414 [ms] (mean)
+ Time per request: 2.414 [ms] (mean, across all concurrent requests)
+ Transfer rate: 50.96 [Kbytes/sec] received
+
Connection Times (ms)
- min mean[+/-sd] median max
- Connect: 0 0 1.9 0 15
- Processing: 0 1 4.2 0 15
- Waiting: 0 0 0.8 0 15
- Total: 0 1 4.5 0 15
-
+ min mean[+/-sd] median max
+ Connect: 0 0 0.0 0 1
+ Processing: 1 2 0.6 2 8
+ Waiting: 1 2 0.5 2 5
+ Total: 1 2 0.6 2 8
+
Percentage of the requests served within a certain time (ms)
- 50% 0
- 66% 0
- 75% 0
- 80% 0
- 90% 15
- 95% 15
- 98% 15
- 99% 15
- 100% 15 (longest request)
+ 50% 2
+ 66% 2
+ 75% 2
+ 80% 2
+ 90% 3
+ 95% 3
+ 98% 4
+ 99% 5
+ 100% 8 (longest request)
-As you can see, CherryPy averaged 558 requests/second, which is pretty good ...
-Test 2: Dynamic content / multi threaded server
------------------------------------------------
+Test 2: Dynamic content / concurrent connections / persistent connections
+-------------------------------------------------------------------------
-I used the same code as test 1, but started CherryPy in thread-pool mode, with 10 threads.
-I also told "ab" to simulate 10 concurrent users ...
-Here are the "ab" results::
+.. code-block:: text
+
+ $ ab -k -n 1000 http://localhost:8080/
+ This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
+ Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
+ Licensed to The Apache Software Foundation, http://www.apache.org/
- $ ./ab.exe -c 10 -n 1000 http://localhost:10000/
- This is ApacheBench, Version 2.0.41-dev <$Revision: 1.121.2.12 $> apache-2.0
- Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
- Copyright (c) 1998-2002 The Apache Software Foundation, http://www.apache.org/
-
Benchmarking localhost (be patient)
Completed 100 requests
Completed 200 requests
@@ -134,69 +163,187 @@ Here are the "ab" results::
Completed 700 requests
Completed 800 requests
Completed 900 requests
+ Completed 1000 requests
Finished 1000 requests
-
-
- Server Software: CherryPy/2.0.0b
+
+
+ Server Software: CherryPy/3.2.4
Server Hostname: localhost
- Server Port: 10000
-
+ Server Port: 8080
+
Document Path: /
Document Length: 2 bytes
-
+
+ Concurrency Level: 1
+ Time taken for tests: 0.626 seconds
+ Complete requests: 1000
+ Failed requests: 0
+ Write errors: 0
+ Keep-Alive requests: 1000
+ Total transferred: 150000 bytes
+ HTML transferred: 2000 bytes
+ Requests per second: 1597.89 [#/sec] (mean)
+ Time per request: 0.626 [ms] (mean)
+ Time per request: 0.626 [ms] (mean, across all concurrent requests)
+ Transfer rate: 234.07 [Kbytes/sec] received
+
+ Connection Times (ms)
+ min mean[+/-sd] median max
+ Connect: 0 0 0.0 0 0
+ Processing: 1 1 0.2 1 3
+ Waiting: 1 1 0.2 1 3
+ Total: 1 1 0.2 1 3
+
+ Percentage of the requests served within a certain time (ms)
+ 50% 1
+ 66% 1
+ 75% 1
+ 80% 1
+ 90% 1
+ 95% 1
+ 98% 1
+ 99% 2
+ 100% 3 (longest request)
+
+Test 3: Dynamic content / concurrent connections
+------------------------------------------------
+
+Now let's alo tell "ab" to simulate 10 concurrent users.
+
+.. code-block:: text
+
+ $ ab -c 10 -n 1000 http://localhost:8080/
+ This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
+ Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
+ Licensed to The Apache Software Foundation, http://www.apache.org/
+
+ Benchmarking localhost (be patient)
+ Completed 100 requests
+ Completed 200 requests
+ Completed 300 requests
+ Completed 400 requests
+ Completed 500 requests
+ Completed 600 requests
+ Completed 700 requests
+ Completed 800 requests
+ Completed 900 requests
+ Completed 1000 requests
+ Finished 1000 requests
+
+
+ Server Software: CherryPy/3.2.4
+ Server Hostname: localhost
+ Server Port: 8080
+
+ Document Path: /
+ Document Length: 2 bytes
+
Concurrency Level: 10
- Time taken for tests: 2.327670 seconds
+ Time taken for tests: 2.653 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
- Total transferred: 127000 bytes
+ Total transferred: 126000 bytes
HTML transferred: 2000 bytes
- Requests per second: 429.61 [#/sec] (mean)
- Time per request: 23.277 [ms] (mean)
- Time per request: 2.328 [ms] (mean, across all concurrent requests)
- Transfer rate: 53.27 [Kbytes/sec] received
-
+ Requests per second: 376.99 [#/sec] (mean)
+ Time per request: 26.526 [ms] (mean)
+ Time per request: 2.653 [ms] (mean, across all concurrent requests)
+ Transfer rate: 46.39 [Kbytes/sec] received
+
Connection Times (ms)
- min mean[+/-sd] median max
- Connect: 0 0 2.3 0 15
- Processing: 15 21 8.9 15 47
- Waiting: 0 16 6.2 15 47
- Total: 15 22 9.0 15 47
-
+ min mean[+/-sd] median max
+ Connect: 0 2 44.7 0 1000
+ Processing: 5 21 33.7 17 406
+ Waiting: 4 20 33.7 16 406
+ Total: 5 23 55.9 17 1020
+
Percentage of the requests served within a certain time (ms)
- 50% 15
- 66% 31
- 75% 31
- 80% 31
- 90% 31
- 95% 31
- 98% 47
- 99% 47
- 100% 47 (longest request)
+ 50% 17
+ 66% 19
+ 75% 20
+ 80% 21
+ 90% 23
+ 95% 25
+ 98% 31
+ 99% 286
+ 100% 1020 (longest request)
-As you can see, CherryPy averaged 429 requests/second, which is a bit less than test 1 (there is a small thread-switching overhead), but is still pretty good ...
-Test 3: Static content / single threaded server
------------------------------------------------
+Test 4: Dynamic content / concurrent connections / persistent connections
+-------------------------------------------------------------------------
-This time, I used CherryPy to serve a static file from disc.
-The file was a simple text containing "OK".
-Here was the config file for CherryPy::
+In this use case, we tell ab to reuse connections as per HTTP/1.1.
- [server]
- socketPort = 10000
-
- [staticContent]
- static.html = static.html
+.. code-block:: text
+ $ ab -k -c 10 -n 1000 http://localhost:8080/
+ This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
+ Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
+ Licensed to The Apache Software Foundation, http://www.apache.org/
-Here are the "ab" results::
+ Benchmarking localhost (be patient)
+ Completed 100 requests
+ Completed 200 requests
+ Completed 300 requests
+ Completed 400 requests
+ Completed 500 requests
+ Completed 600 requests
+ Completed 700 requests
+ Completed 800 requests
+ Completed 900 requests
+ Completed 1000 requests
+ Finished 1000 requests
+
+
+ Server Software: CherryPy/3.2.4
+ Server Hostname: localhost
+ Server Port: 8080
- $ ./ab.exe -n 1000 http://localhost:10000/static.html
- This is ApacheBench, Version 2.0.41-dev <$Revision: 1.121.2.12 $> apache-2.0
- Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
- Copyright (c) 1998-2002 The Apache Software Foundation, http://www.apache.org/
+ Document Path: /
+ Document Length: 2 bytes
+
+ Concurrency Level: 10
+ Time taken for tests: 1.944 seconds
+ Complete requests: 1000
+ Failed requests: 0
+ Write errors: 0
+ Keep-Alive requests: 1000
+ Total transferred: 150000 bytes
+ HTML transferred: 2000 bytes
+ Requests per second: 514.36 [#/sec] (mean)
+ Time per request: 19.442 [ms] (mean)
+ Time per request: 1.944 [ms] (mean, across all concurrent requests)
+ Transfer rate: 75.35 [Kbytes/sec] received
+
+ Connection Times (ms)
+ min mean[+/-sd] median max
+ Connect: 0 1 31.6 0 1000
+ Processing: 1 18 16.6 16 424
+ Waiting: 1 14 15.6 12 418
+ Total: 1 19 36.3 16 1040
+
+ Percentage of the requests served within a certain time (ms)
+ 50% 16
+ 66% 21
+ 75% 24
+ 80% 26
+ 90% 32
+ 95% 38
+ 98% 47
+ 99% 51
+ 100% 1040 (longest request)
+
+
+Test 5: Raw WSGI server / no concurrent connections
+---------------------------------------------------
+.. code-block:: text
+
+ $ ab -n 1000 http://localhost:8080/
+ This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
+ Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
+ Licensed to The Apache Software Foundation, http://www.apache.org/
+
Benchmarking localhost (be patient)
Completed 100 requests
Completed 200 requests
@@ -207,45 +354,168 @@ Here are the "ab" results::
Completed 700 requests
Completed 800 requests
Completed 900 requests
+ Completed 1000 requests
Finished 1000 requests
-
-
- Server Software: CherryPy/2.0.0b
+
+
+ Server Software: sylvain-laptop
Server Hostname: localhost
- Server Port: 10000
-
- Document Path: /static.html
- Document Length: 4 bytes
-
+ Server Port: 8080
+
+ Document Path: /
+ Document Length: 2 bytes
+
Concurrency Level: 1
- Time taken for tests: 1.979130 seconds
+ Time taken for tests: 1.041 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
- Total transferred: 175000 bytes
- HTML transferred: 4000 bytes
- Requests per second: 505.27 [#/sec] (mean)
- Time per request: 1.979 [ms] (mean)
- Time per request: 1.979 [ms] (mean, across all concurrent requests)
- Transfer rate: 85.90 [Kbytes/sec] received
-
+ Total transferred: 108000 bytes
+ HTML transferred: 2000 bytes
+ Requests per second: 960.58 [#/sec] (mean)
+ Time per request: 1.041 [ms] (mean)
+ Time per request: 1.041 [ms] (mean, across all concurrent requests)
+ Transfer rate: 101.31 [Kbytes/sec] received
+
Connection Times (ms)
- min mean[+/-sd] median max
- Connect: 0 0 2.2 0 15
- Processing: 0 1 4.3 0 15
- Waiting: 0 0 0.5 0 15
- Total: 0 1 4.8 0 15
+ min mean[+/-sd] median max
+ Connect: 0 0 0.0 0 0
+ Processing: 0 1 0.3 1 4
+ Waiting: 0 1 0.3 1 4
+ Total: 0 1 0.3 1 4
+
+ Percentage of the requests served within a certain time (ms)
+ 50% 1
+ 66% 1
+ 75% 1
+ 80% 1
+ 90% 1
+ 95% 1
+ 98% 2
+ 99% 2
+ 100% 4 (longest request)
+
+
+Test 6: Raw WSGI server / concurrent connections
+------------------------------------------------
+.. code-block:: text
+
+ $ ab -c 10 -n 1000 http://localhost:8080/
+ This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
+ Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
+ Licensed to The Apache Software Foundation, http://www.apache.org/
+
+ Benchmarking localhost (be patient)
+ Completed 100 requests
+ Completed 200 requests
+ Completed 300 requests
+ Completed 400 requests
+ Completed 500 requests
+ Completed 600 requests
+ Completed 700 requests
+ Completed 800 requests
+ Completed 900 requests
+ Completed 1000 requests
+ Finished 1000 requests
+
+
+ Server Software: sylvain-laptop
+ Server Hostname: localhost
+ Server Port: 8080
+
+ Document Path: /
+ Document Length: 2 bytes
+
+ Concurrency Level: 10
+ Time taken for tests: 1.235 seconds
+ Complete requests: 1000
+ Failed requests: 0
+ Write errors: 0
+ Total transferred: 108000 bytes
+ HTML transferred: 2000 bytes
+ Requests per second: 809.88 [#/sec] (mean)
+ Time per request: 12.348 [ms] (mean)
+ Time per request: 1.235 [ms] (mean, across all concurrent requests)
+ Transfer rate: 85.42 [Kbytes/sec] received
+
+ Connection Times (ms)
+ min mean[+/-sd] median max
+ Connect: 0 1 31.5 0 996
+ Processing: 2 8 19.4 7 341
+ Waiting: 1 8 19.4 6 341
+ Total: 2 9 37.0 7 1003
+
Percentage of the requests served within a certain time (ms)
- 50% 0
- 66% 0
- 75% 0
- 80% 0
- 90% 15
- 95% 15
+ 50% 7
+ 66% 7
+ 75% 8
+ 80% 8
+ 90% 10
+ 95% 11
98% 15
- 99% 15
- 100% 15 (longest request)
+ 99% 17
+ 100% 1003 (longest request)
+
+
+Test 7: Raw WSGI server / concurrent connections / persistent connections
+-------------------------------------------------------------------------
+
+.. code-block:: text
+
+ $ ab -k -c 10 -n 1000 http://localhost:8080/
+ This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
+ Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
+ Licensed to The Apache Software Foundation, http://www.apache.org/
+
+ Benchmarking localhost (be patient)
+ Completed 100 requests
+ Completed 200 requests
+ Completed 300 requests
+ Completed 400 requests
+ Completed 500 requests
+ Completed 600 requests
+ Completed 700 requests
+ Completed 800 requests
+ Completed 900 requests
+ Completed 1000 requests
+ Finished 1000 requests
-As you can see, CherryPy averaged 505 requests/second. Again it is a little bit less than a dynamic page, but it is still pretty good ...
+ Server Software: sylvain-laptop
+ Server Hostname: localhost
+ Server Port: 8080
+
+ Document Path: /
+ Document Length: 2 bytes
+
+ Concurrency Level: 10
+ Time taken for tests: 0.992 seconds
+ Complete requests: 1000
+ Failed requests: 0
+ Write errors: 0
+ Keep-Alive requests: 0
+ Total transferred: 108000 bytes
+ HTML transferred: 2000 bytes
+ Requests per second: 1008.08 [#/sec] (mean)
+ Time per request: 9.920 [ms] (mean)
+ Time per request: 0.992 [ms] (mean, across all concurrent requests)
+ Transfer rate: 106.32 [Kbytes/sec] received
+
+ Connection Times (ms)
+ min mean[+/-sd] median max
+ Connect: 0 0 0.1 0 1
+ Processing: 1 7 22.8 6 345
+ Waiting: 0 7 22.7 5 344
+ Total: 1 7 22.8 6 345
+
+ Percentage of the requests served within a certain time (ms)
+ 50% 6
+ 66% 7
+ 75% 7
+ 80% 7
+ 90% 9
+ 95% 10
+ 98% 11
+ 99% 13
+ 100% 345 (longest request)
diff --git a/sphinx/source/appendix/faq.rst b/sphinx/source/appendix/faq.rst
index 0544ff39..7aadbb60 100644
--- a/sphinx/source/appendix/faq.rst
+++ b/sphinx/source/appendix/faq.rst
@@ -31,7 +31,7 @@ Server Features and Configuration
:Q: Does CherryPy support https?
:A: CherryPy has built-in SSL support as of 3.0.0beta. See the `ssl_*`
- properties of :mod:`cherrypy._cpserver`.
+ properties of :mod:`cherrypy._cpserver` and the :doc:`programmer's guide </progguide/security>`.
Earlier versions do not have built-in SSL support, but Tim Evans has
written a module called `SslCherry <http://tools.cherrypy.org/wiki/SSLWithM2Crypto>`_
@@ -43,8 +43,12 @@ Server Features and Configuration
:A: See `Malicious HTML Tags Embedded in Client Web Requests <http://www.cert.org/advisories/CA-2000-02.html>`_
and `Understanding Malicious Content Mitigation for Web Developers <http://www.cert.org/tech_tips/malicious_code_mitigation.html>`_
at `CERT <http://www.cert.org/>`_ for an overview of Cross-Site Scripting
- (XSS) issues. It is ultimately up to the developer to remove potential XSS
- vulnerabilities from their apps and sites.
+ (XSS) issues.
+
+ While it is ultimately up to the developer to remove potential XSS
+ vulnerabilities from their apps and sites, there are several Cherrypy
+ settings that can help. They are discussed in the
+ :doc:`programmer's guide </progguide/security>`.
:Q: Why does CherryPy take CPU/RAM even though it's not yet receiving requests?
diff --git a/sphinx/source/appendix/success.rst b/sphinx/source/appendix/success.rst
index 83363d26..b05587da 100644
--- a/sphinx/source/appendix/success.rst
+++ b/sphinx/source/appendix/success.rst
@@ -4,78 +4,84 @@
Success Stories
***************
-You are interested in CherryPy but you would like to hear more from people using it, or simply check out products or application running it.
+You are interested in CherryPy but you would like to hear more from people
+using it, or simply check out products or application running it.
-If you would like to have your CherryPy powered website or product listed here, contact us via our [wiki:CherryPyMailingLists mailing lists] or [wiki:CherryPyIrcChannel IRC].
+If you would like to have your CherryPy powered website or product listed here,
+contact us via our `mailing list <http://groups.google.com/group/cherrypy-users>`_
+or IRC (#cherrypy on `OFTC <http://www.oftc.net/oftc/>`_).
Websites running atop CherryPy
==============================
-http://www.yougov.com - International market research firm, conducts millions of surveys on CherryPy yearly
+`Hulu Deejay and Hulu Sod <http://tech.hulu.com/blog/2013/03/13/python-and-hulu>`_ - Hulu uses
+CherryPy for some projects.
+"The service needs to be very high performance.
+Python, together with CherryPy,
+`gunicorn <http://gunicorn.org>`_, and gevent more than provides for this."
-http://www.cuil.com - "The world's biggest search engine"
+`Netflix <http://techblog.netflix.com/2013/03/python-at-netflix.html>`_ - Netflix uses CherryPy as a building block in their infrastructure: "Restful APIs to
+large applications with requests, providing web interfaces with CherryPy and Bottle,
+and crunching data with scipy."
-`PictureHouse cinemas web sale <http://sales.picturehouses.org.uk/>`_ - E-commerce site for the !PictureHouse cinema chain in the UK
+`Urbanility <http://urbanility.com>`_ - French website for local neighbourhood assets in Rennes, France.
-`http://jammersbase.com <http://jammersbase.com>`_
+`MROP Supply <https://www.mropsupply.com>`_ - Webshop for industrial equipment,
+developed using CherryPy 3.2.2 utilizing Python 3.2,
+with libs: `Jinja2-2.6 <http://jinja.pocoo.org/docs>`_, davispuh-MySQL-for-Python-3-3403794,
+pyenchant-1.6.5 (for search spelling).
+"I'm coming over from .net development and found Python and CherryPy to
+be surprisingly minimalistic. No unnecessary overhead - build everything you
+need without the extra fluff. I'm a fan!"
-`http://www.pennspurchase.com <http://www.pennspurchase.com>`_
+`CherryMusic <http://www.fomori.org/cherrymusic>`_ - A music streaming server written in python:
+Stream your own music collection to all your devices! CherryMusic is open source.
-`Almad's Homepage <http://www.almad.net/>`_ - Simple homepage with blog (Czech only, sorry)
+`YouGov Global <http://www.yougov.com>`_ - International market research firm, conducts
+millions of surveys on CherryPy yearly.
-`http://bank.muratordom.pl] & [http://bank.muratorplus.pl http://bank.muratorplus.pl <http://bank.muratordom.pl>`_ - Sorry, Polish only
+`Aculab Cloud <http://cloud.aculab.com>`_ - Voice and fax applications on the cloud.
+A simple telephony API for Python, C#, C++, VB, etc...
+The website and all front-end and back-end web services are built with CherryPy,
+fronted by nginx (just handling the ssh and reverse-proxy), and running on AWS in two regions.
-[http://www.defuze.org/] - A blog based on CherryPy2
+`Learnit Training <http://www.learnit.nl>`_ - Dutch website for an IT, Management and
+Communication training company. Built on CherryPy 3.2.0 and Python 2.7.3, with
+`oursql <http://pythonhosted.org/oursql>`_ and
+`DBUtils <http://www.webwareforpython.org/DBUtils>`_ libraries, amongst others.
-`CTChallenge <http://ctchallenge.org>`_ - A small, but growing site about a bicycle tour that raises money to fight cancer.
+`Linstic <http://linstic.com>`_ - Sticky Notes in your browser (with linking).
-`Food on line <http://www.foodfileonline.com>`_ - An online calorie counter and nutrition database
+`Almad's Homepage <http://www.almad.net>`_ - Simple homepage with blog.
-`Personal site of Maries Ionel Cristian (ionel_mc) <http://ionel.zapto.org/>`_ - source code for the site and supporting projects available for download.
-
-`diggdot.us] - Built using [http://www.turbogears.org/ TurboGears <http://diggdot.us/>`_, running CherryPy: Cycling Digg, slashdot, and del.icio.us/popular news items.
-
-`stratolab <http://stratolab.com/>`_ - Video Game Courses for Children
-
-`Serpia <http://www.serpia.org/>`_ - A place for Pythoners with some interesting tutorials
-
-`hosted-projects.com <http://www.hosted-projects.com/>`_ - hosted-projects is a company specialized in web hosting services for software project teams offering the Subversion revision control system coupled with project and issue management software like Trac and Bugzilla. hosted-projects.com uses CherryPy for its web control panel.
-
-`eswap.com <http://www.eswap.com>`_ - website for financial charts, tagging stocks and researching investment ideas.
-
-I have built (and I'm maintaining it now) the `PennsPurchase <http://www.pennspurchase.com>`_ website. It uses Cherry, compiled (or not) !CheetahTemplates and PostgreSQL database (with sqlobject module). -- ''Pawel Maczewski''
-
-`indextar.com/ba <http://www.indextar.com/ba>`_ - a comprehensive directory of Buenos Aires businesses and services for expatriates.
-
-[http://www.worldwildweather.com/] - Personalized marine weather forecasts.
Products based on CherryPy
==========================
-`TurboGears <http://www.turbogears.org/>`_ - The rapid web development megaframework
-
-`Indigo <http://www.perceptiveautomation.com/indigo/index.html>`_ - An X10 home control server for Mac
+`SABnzbd <http://sabnzbd.org>`_ - Open Source Binary Newsreader written in Python.
-`Subway <http://www.gosubway.org/>`_ - Web development stack
+`Headphones <https://github.com/rembo10/headphones>`_ - Third-party add-on for SABnzbd.
-`SlikiWiki <http://www.sf.net/projects/slikiwiki>`_ - A simple wiki
+`SickBeard <http://sickbeard.com>`_ - "Sick Beard is a PVR for newsgroup users (with limited torrent support). It watches for new episodes of your favorite shows and when they are posted it downloads them, sorts and renames them, and optionally generates metadata for them."
-`opkee <http://svn.defuze.org/oss/opkee/>`_ - A simple blogging example
+`TurboGears <http://www.turbogears.org>`_ - The rapid web development megaframework. Turbogears 1.x used Cherrypy. "CherryPy is the underlying application server for TurboGears. It is responsible for taking the requests from the user’s browser, parses them and turns them into calls into the Python code of the web application. Its role is similar to application servers used in other programming languages".
-`BAOW <http://www.baow.org>`_ - A lightweight content manage system based on CherryPy, Python language and SQL database.
+`Indigo <http://www.perceptiveautomation.com/indigo/index.html>`_ - "An intelligent home control
+server that integrates home control hardware modules to provide control of your home. Indigo's built-in
+Web server and client/server architecture give you control and access to your home remotely from
+other Macs, PCs, internet tablets, PDAs, and mobile phones."
-`Planet Plus <http://planetplus.python-hosting.com/>`_ is an add-on for Planet, the feed aggregator.
+`SlikiWiki <http://www.sf.net/projects/slikiwiki>`_ - Wiki built on CherryPy and featuring WikiWords, automatic backlinking, site map generation, full text search, locking for concurrent edits, RSS feed embedding, per page access control lists, and page formatting using PyTextile markup."
-`Brockman <http://projects.dowski.com/projects/brockman>`_ - A simple software project catalog example
+`read4me <http://sourceforge.net/projects/read4me>`_ - read4me is a Python feed-reading web service.
-`read4me <http://read4me.sourceforge.net/wiki/index.php/Main_Page>`_ - read4me is a Python feed-reading web service.
+`Firebird QA tools <http://www.firebirdsql.org/en/quality-assurance>`_ - Firebird QA tools are based on CherryPy.
-`Skeletonz <http://www.daimi.au.dk/~amix/skeletonz/>`_ - Skeletonz is an Ajax approach on a content management system (CMS)
-
-Firebird QA tools are based on CherryPy
+`salt-api <https://github.com/saltstack/salt-api>`_ - A REST API for Salt, the infrastructure orchestration tool.
Products inspired by CherryPy
=============================
-`OOWeb <http://ooweb.sourceforge.net/>`_ ''is a lightweight, embedded HTTP server for '''Java''' applications that maps objects to URL directories, methods to pages and form/querystring arguments as method parameters.'' ... ''OOWeb was unashamedly inspired by CherryPy'' =======
+`OOWeb <http://ooweb.sourceforge.net/>`_ - "OOWeb is a lightweight, embedded HTTP server for Java applications that maps objects to URL directories, methods to pages and form/querystring arguments as method parameters. OOWeb was originally inspired by CherryPy."
+
diff --git a/sphinx/source/conf.py b/sphinx/source/conf.py
index a8da54eb..d0fa3432 100644
--- a/sphinx/source/conf.py
+++ b/sphinx/source/conf.py
@@ -3,7 +3,8 @@
# CherryPy documentation build configuration file, created by
# sphinx-quickstart on Sat Feb 20 09:18:03 2010.
#
-# This file is execfile()d with the current directory set to its containing dir.
+# This file is execfile()d with the current directory set to its containing
+# dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
@@ -11,19 +12,21 @@
# All configuration values have a default; values that are commented out
# serve to show the default.
-import sys, os
+import sys
+import os
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
-#sys.path.append(os.path.abspath('.'))
+# sys.path.append(os.path.abspath('.'))
-# -- General configuration -----------------------------------------------------
+# -- General configuration -----------------------------------------------
-# Add any Sphinx extension module names here, as strings. They can be extensions
-# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
-extensions = ['sphinx.ext.autodoc', 'sphinx.ext.todo', 'sphinx.ext.intersphinx']
-intersphinx_mapping = {'http://www.python.org/doc/2.6.4': None}
+# Add any Sphinx extension module names here, as strings. They can be
+# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
+extensions = ['sphinx.ext.autodoc',
+ 'sphinx.ext.todo', 'sphinx.ext.intersphinx']
+intersphinx_mapping = {'http://docs.python.org/2/': None}
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
@@ -39,7 +42,7 @@ master_doc = 'index'
# General information about the project.
project = u'CherryPy'
-copyright = u'2010, CherryPy Team'
+copyright = u'2013, CherryPy Team'
# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
@@ -70,7 +73,8 @@ release = cherrypy.__version__
# for source files.
exclude_trees = []
-# The reST default role (used for this markup: `text`) to use for all documents.
+# The reST default role (used for this markup: `text`) to use for all
+# documents.
#default_role = None
# If true, '()' will be appended to :func: etc. cross-reference text.
@@ -91,7 +95,7 @@ pygments_style = 'sphinx'
#modindex_common_prefix = []
-# -- Options for HTML output ---------------------------------------------------
+# -- Options for HTML output ---------------------------------------------
# The theme to use for HTML and HTML Help pages. Major themes that come with
# Sphinx are currently 'default' and 'sphinxdoc'.
@@ -100,20 +104,20 @@ html_theme = 'default'
# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
# documentation.
-html_theme_options = {
- "relbarbgcolor": "#880000",
- "relbartextcolor": "white",
- "relbarlinkcolor": "#FFEEEE",
- "sidebarbgcolor": "#880000",
- "sidebartextcolor": "white",
- "sidebarlinkcolor": "#FFEEEE",
- "headbgcolor": "#FFF8FB",
- "headtextcolor": "black",
- "headlinkcolor": "#660000",
- "footerbgcolor": "#880000",
- "footertextcolor": "white",
- "codebgcolor": "#FFEEEE",
-}
+# html_theme_options = {
+# "relbarbgcolor": "#880000",
+# "relbartextcolor": "white",
+# "relbarlinkcolor": "#FFEEEE",
+# "sidebarbgcolor": "#880000",
+# "sidebartextcolor": "white",
+# "sidebarlinkcolor": "#FFEEEE",
+# "headbgcolor": "#FFF8FB",
+# "headtextcolor": "black",
+# "headlinkcolor": "#660000",
+# "footerbgcolor": "#880000",
+# "footertextcolor": "white",
+# "codebgcolor": "#FFEEEE",
+# }
# Add any paths that contain custom themes here, relative to this directory.
#html_theme_path = []
@@ -139,7 +143,7 @@ html_theme_options = {
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
-html_style = 'cpdocmain.css'
+# html_style = 'cpdocmain.css'
# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
# using the given strftime format.
@@ -180,7 +184,7 @@ html_style = 'cpdocmain.css'
htmlhelp_basename = 'CherryPydoc'
-# -- Options for LaTeX output --------------------------------------------------
+# -- Options for LaTeX output --------------------------------------------
# The paper size ('letter' or 'a4').
#latex_paper_size = 'letter'
@@ -189,10 +193,11 @@ htmlhelp_basename = 'CherryPydoc'
#latex_font_size = '10pt'
# Grouping the document tree into LaTeX files. List of tuples
-# (source start file, target name, title, author, documentclass [howto/manual]).
+# (source start file, target name, title, author,
+# documentclass [howto/manual]).
latex_documents = [
- ('index', 'CherryPy.tex', u'CherryPy Documentation',
- u'CherryPy Team', 'manual'),
+ ('index', 'CherryPy.tex', u'CherryPy Documentation',
+ u'CherryPy Team', 'manual'),
]
# The name of an image file (relative to this directory) to place at the top of
@@ -211,3 +216,37 @@ latex_documents = [
# If false, no module index is generated.
#latex_use_modindex = True
+
+import os
+
+on_rtd = os.environ.get('READTHEDOCS', None) == 'True'
+if on_rtd:
+ # so that ReadTheDocs can generate the docs properly
+ # even the PDF version. Since ReadTheDocs runs on Linux,
+ # it can't import pywin32. See:
+ # http://read-the-docs.readthedocs.org/en/latest/faq.html#i-get-import-errors-on-libraries-that-depend-on-c-modules
+ import sys
+
+ class Mock(object):
+
+ def __init__(self, *args, **kwargs):
+ pass
+
+ def __call__(self, *args, **kwargs):
+ return Mock()
+
+ @classmethod
+ def __getattr__(cls, name):
+ if name in ('__file__', '__path__'):
+ return '/dev/null'
+ elif name[0] == name[0].upper():
+ mockType = type(name, (), {})
+ mockType.__module__ = __name__
+ return mockType
+ else:
+ return Mock()
+
+ MOCK_MODULES = ['win32api', 'win32con', 'win32event',
+ 'win32service', 'win32serviceutil']
+ for mod_name in MOCK_MODULES:
+ sys.modules[mod_name] = Mock()
diff --git a/sphinx/source/deployguide/index.rst b/sphinx/source/deployguide/index.rst
index ab4d9220..c8eff7bb 100644
--- a/sphinx/source/deployguide/index.rst
+++ b/sphinx/source/deployguide/index.rst
@@ -2,9 +2,29 @@
Deployment Guide
****************
+CherryPy the application framework is quite flexible and can be deployed in a
+wide variety of ways. CherryPy the server is a production-ready, performant
+server that can be used to deploy any WSGI or CGI application.
+
+Applications
+============
+
+An easy way to deploy a CherryPy application is using the standard
+``quickstart()`` server. The ``cherryd`` script mentioned below wraps this same
+server. It is a production-ready, performant server that can be quickly
+configured for development or production use (or even somewhere in-between) by
+setting an :ref:`environment <environments>` in the application config.
+
+A CherryPy application can also be deployed using any WSGI-capable server. The
+return from :py:class:`cherrypy.tree.mount <cherrypy._cptree.Tree>` is a
+standard WSGI application.
+
Servers
=======
+CherryPy ships with a fast, production-ready server that can be used to serve
+applications independently of the CherryPy application framework.
+
.. toctree::
:maxdepth: 2
diff --git a/sphinx/source/index.rst b/sphinx/source/index.rst
index 1f07e267..492bdbbf 100644
--- a/sphinx/source/index.rst
+++ b/sphinx/source/index.rst
@@ -2,7 +2,7 @@
:hidden:
intro/index
- concepts/index
+ tutorial/index
progguide/index
deployguide/index
refman/index
@@ -13,31 +13,32 @@
CherryPy |release| Documentation
********************************
-Sections
-========
-
:doc:`Introduction <intro/index>`
---------------------------------
-*The who, what, and why of CherryPy*
+The who, what, and why of CherryPy
-:doc:`Concepts <concepts/index>`
+:doc:`Tutorial <tutorial/index>`
---------------------------------
-*The basics of CherryPy*
+The basics of CherryPy
:doc:`Programmer's Guide <progguide/index>`
-------------------------------------------
-*How to perform common tasks*
+How to perform common tasks
:doc:`Deployment Guide <deployguide/index>`
-------------------------------------------
-*Getting your application running and on the web*
+Getting your application running and on the web
:doc:`Reference Manual <refman/index>`
--------------------------------------
-*Module, class, and function reference*
+Module, class, and function reference
:doc:`Appendix <appendix/index>`
--------------------------------
+:doc:`FAQ </appendix/faq>`, :doc:`Performance benchmarking </appendix/cherrypyspeed>`, and :doc:`Success Stories </appendix/success>`
+
+Other
+-----
:ref:`genindex`
diff --git a/sphinx/source/intro/index.rst b/sphinx/source/intro/index.rst
index 678ee698..cf5a5951 100644
--- a/sphinx/source/intro/index.rst
+++ b/sphinx/source/intro/index.rst
@@ -13,13 +13,20 @@ source code developed in less time.
CherryPy does its best to stay out of the way between the programmer and the
problem. It works out of the box; default behavior is sensible enough to allow
-use without extensive setup or customization. However, its configuration
-and plugin systems are more than enough to easily build and deploy complex
+use without extensive setup or customization. However, its :doc:`configuration </tutorial/config>`
+and :doc:`plugin </progguide/extending/customplugins>` systems are more than enough to easily build and deploy complex
sites.
You are free to use any kind of templating, data access etc. technology
-you want. CherryPy also has built-in tools for sessions, static files, cookies,
-file uploads, caching, encoding, authorization, compression, and many more.
+you want. CherryPy also has built-in tools for
+:doc:`sessions </refman/lib/sessions>`,
+:doc:`static files </progguide/files/static>`,
+:doc:`cookies </progguide/cookies>`,
+:doc:`file uploads </progguide/files/uploading>`,
+:doc:`caching </refman/lib/caching>`, :doc:`encoding </refman/lib/encoding>`,
+:doc:`authorization </refman/lib/auth_basic>`,
+:doc:`compression </refman/lib/encoding>`,
+and :doc:`many more </tutorial/tools>`.
The production-ready, HTTP/1.1-compliant web server allows you to deploy
web applications anywhere Python is installed. It also supports any other
diff --git a/sphinx/source/intro/install.rst b/sphinx/source/intro/install.rst
index 39dfbca0..51f59cbb 100644
--- a/sphinx/source/intro/install.rst
+++ b/sphinx/source/intro/install.rst
@@ -14,49 +14,68 @@ Prerequisites
=============
All you need is a working version of Python-2.3 or later on your computer.
+If you are running Max OS X or some Linux distribution (e.g. Ubuntu, Debian, Fedora)
+you most likely already have python on you system, for a detailed instruction
+on how to install python follow the instruction on the
+`python wiki <http://wiki.python.org/moin/BeginnersGuide/Download>`_.
-Get Python on Debian::
+.. _stableversions:
- $ apt-get install python python-dev
+Download Stable Versions
+========================
+Using `pip` or `easy_install`
+-----------------------------
-.. _stableversions:
+Using pip::
-Stable versions
-===============
+ $ pip install CherryPy
-Download
---------
+or with easy_install::
-You may download this version from http://download.cherrypy.org/cherrypy/3.2.0rc1/
+ $ easy_install CherryPy
-For other releases, browse our
-`download index <http://download.cherrypy.org/ download index>`_.
+It is recommended to use `pip` instead of `easy_install`.
+If you want to download and install CherryPy for yourself proceed to the
+next instructions depending on your platform.
-Windows Installation
---------------------
+Unix/Mac
+--------
-* Download the latest CherryPy release from the
- `repository <http://download.cherrypy.org/cherrypy/>`_.
- Select the file ending in ".exe"
-* Run the downloaded file.
+You may download the most current version from `PyPI <https://pypi.python.org/pypi/CherryPy/3.2.3>`_
-Unix/Mac Installation
----------------------
+For other releases, browse our
+`download index <http://download.cherrypy.org/cherrypy>`_.
-* Download the latest CherryPy release from the
- `repository <http://download.cherrypy.org/cherrypy/>`_.
* Unzip/untar the files
-* Go to the directory created by the file extraction.
+* Enter the directory created by the file extraction.
* Type "python setup.py install" to install the CherryPy module
+
+Windows
+-------
+
+You may download the most current version from `PyPI <https://pypi.python.org/pypi/CherryPy/3.2.3>`_.
+
+For other releases, browse our `download index <http://download.cherrypy.org/cherrypy>`_.
+
+* Select the file ending in ".exe".
+* Run the downloaded file.
+
+
Next Steps
-----------
+==========
+
+To run your first sample website:
+
+ 1. In a command terminal or console go to cherrypy/tutorial/
+ 2. Type::
+
+ $ python tut01_helloworld.py
-* To run your first sample website, go to cherrypy/tutorial/ and type
- "python tut01_helloworld.py", and you'll have a running website on port 8080.
-* Open your favorite browser and point it to http://localhost:8080 to see your
- first CherryPy-served page :-)
+ and you'll have a running website on port 8080.
+
+ 3. Open your favorite browser and point it to http://localhost:8080 to see your first CherryPy-served page :-)
Now, you should try running some of the other tutorials found in the tutorial
directory and look at their source code to understand how to develop a website
@@ -67,78 +86,24 @@ with CherryPy.
Development versions
====================
-CherryPy's source code is managed using `Subversion <http://subversion.tigris.org>`_,
-a source code control system.
-
-You can access our Subversion repository using your favorite Subversion client
-at http://svn.cherrypy.org
-
-For Windows users, we recommend the wonderful Subversion client
-`TortoiseSVN <http://tortoisesvn.tigris.org/>`_. Users of other operating
-systems are advised to use multi-platform, Qt-based
-`eSVN <http://esvn.umputun.com/>`_ or the command line tools provided by the
-`core Subversion distribution <http://subversion.tigris.org/>`_.
-
-Note: In some cases http access fails with the error 'Cannot checkout' even
-though the web interface works fine. This is usually because of a proxy between
-you and the server which doesn't support all the access methods that SVN needs.
-Many ISPs insert a transparent proxy on all http traffic, causing this problem.
-The solution is to switch to https access so the proxy cannot interfere.
-
-You may also browse present and past versions of CherryPy source code,
-inspect change sets, and even follow changes to specific trees/files using
-RSS feeds. This web interface is located at http://www.cherrypy.org/browser/
-
-Usage notes
------------
-
-* The repository is open for anonymous read-only access. CherryPy developers
- have write permissions. To obtain write permission, please contact us via
- email or IRC (see http://www.cherrypy.org/wiki/CherryPyInvolved).
-* The repository follows the standard trunk/branches/tags structure that is
- recommended in the Subversion documentation:
-
- * ``trunk`` contains the official development code. Please do not checkin
- any code on trunk that is untested, or that breaks the test suite.
- * ``branches`` contain experimental branches. Patches for complex tickets
- may also be developed and tested on a branch, to allow for easier
- collaboration during test of inherently unstable features.
- * ``tags`` contain frozen, known quality releases.
-
-Configuring the Subversion client
----------------------------------
-
-Popular Subversion clients, including TortoiseSVN and the standard command line
-tools, are configurable by editing a standard ``config`` file. The file is
-stored at:
-
- * **Linux**: ``~/.subversion/config``
- * **Windows XP, 2000, NT**: ``%APPDATA%\Subversion\config``
- * **Windows 98 (and possibly ME also)**: ``\Windows\Application Data\Subversion\config``
-
-Configuration is necessary because line endings do matter for Subversion, and
-different code editors and IDEs use different conventions. This problem can be
-solved by telling Subversion to automatically map the line endings of the code
-in the repository to the conventions of your local install. The configuration
-file should contain the following entries::
-
- [miscellany]
- global-ignores = *.o *.lo *.la #*# .*.rej *.rej .*~ *~ .#* .DS_Store *.pyc
- enable-auto-props = yes
-
- [auto-props]
- *.py = svn:eol-style=native
- README.* = svn:eol-style=CRLF
- *.TXT = svn:eol-style=CRLF
-
-The miscellany section contains two important settings: first, it tells
-Subversion to ignore pyc files (in addition to the standard files it already
-ignores); and also, it enables the auto-props section, which in turn provides
-the standard line-ending convention.
+CherryPy's source code is managed using `Mercurial <http://mercurial.selenic.com/>`_,
+a source code control system written in python.
+
+You can access our Mercurial repository using your favorite
+Mercurial client at `bitbucket <https://bitbucket.org/cherrypy/cherrypy>`_.
+
+For Windows users, we recommend the wonderful Mercurial
+client `TortoiseHg <http://tortoisehg.org/>`_. Users of
+other operating systems are advised to use multi-platform
+command line tools provided by the
+`core Mercurial distribution <http://mercurial.selenic.com/downloads/>`_.
+
+* To submit a patch: fork the repository and submit your pull request.
+ For further information please contact us via email or IRC
+ (see `getting involved <http://bitbucket.org/cherrypy/cherrypy/wiki/CherryPyInvolved>`_).
Standalone WSGI server
----------------------
The WSGI server that comes bundled with CherryPy is available as a standalone
module. Feel free to use it for all of your WSGI serving needs.
-
diff --git a/sphinx/source/intro/license.rst b/sphinx/source/intro/license.rst
index e4805eb6..86125af4 100644
--- a/sphinx/source/intro/license.rst
+++ b/sphinx/source/intro/license.rst
@@ -4,7 +4,7 @@ CherryPy License (BSD)
::
- Copyright (c) 2002-2010, CherryPy Team (team@cherrypy.org)
+ Copyright (c) 2002-2013, CherryPy Team (team@cherrypy.org)
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
diff --git a/sphinx/source/intro/whycherrypy.rst b/sphinx/source/intro/whycherrypy.rst
index eaa0146c..ffbcd0c7 100644
--- a/sphinx/source/intro/whycherrypy.rst
+++ b/sphinx/source/intro/whycherrypy.rst
@@ -42,8 +42,9 @@ templating engines, etc. CherryPy applications are just like regular Python
applications. CherryPy does not stand in your way if you want to use these
brilliant tools.
-CherryPy also provides Tools and Plugins, which are powerful extension points
-needed to develop world-class web applications.
+CherryPy also provides :doc:`Tools </tutorial/tools>` and
+:doc:`Plugins </progguide/extending/customplugins>`, which are powerful
+extension points needed to develop world-class web applications.
3. Maturity
-----------
@@ -64,7 +65,9 @@ a superior tool for the job.
------------
CherryPy has an active community that develops deployed CherryPy applications
-and are willing and ready to assist you on the CherryPy mailing list or IRC.
+and are willing and ready to assist you on the
+`CherryPy mailing list <http://groups.google.com/group/cherrypy-users>`_ or
+IRC (#cherrypy on `OFTC <http://www.oftc.net/oftc/>`_).
The developers also frequent the list and often answer questions and implement
features requested by the end-users.
@@ -91,12 +94,12 @@ several others).
6. It's free!
-------------
-All of CherryPy is licensed under the open-source BSD license, which means
+:doc:`All of CherryPy is licensed </intro/license>` under the open-source BSD license, which means
**CherryPy can be used commercially for ZERO cost**.
7. Where to go from here?
-------------------------
-Check out the :doc:`/concepts/index` and :doc:`/progguide/index` for
+Check out the :doc:`/tutorial/index` and :doc:`/progguide/index` for
more complete documentation.
diff --git a/sphinx/source/progguide/REST.rst b/sphinx/source/progguide/REST.rst
index 2a2cf542..13cecadf 100644
--- a/sphinx/source/progguide/REST.rst
+++ b/sphinx/source/progguide/REST.rst
@@ -65,7 +65,7 @@ hosting server.
Within the CherryPy server, the remainder of the hierarchical
identifier--the path--is mapped to Python objects
via the Dispatch mechanism. This behavior is highly
-customizable and documented in :doc:`/concepts/dispatching`.
+customizable and documented in :doc:`/tutorial/dispatching`.
Using the default dispatcher and page handlers, the path of the URI
maps to a hierarchy of Python identifiers in the CherryPy app. For
diff --git a/sphinx/source/progguide/customheaders.rst b/sphinx/source/progguide/customheaders.rst
index 51802dc9..1b1a6f80 100644
--- a/sphinx/source/progguide/customheaders.rst
+++ b/sphinx/source/progguide/customheaders.rst
@@ -35,7 +35,9 @@ deliver multiple headers with the same key in the response.
import cherrypy
def multi_headers():
- cherrypy.response.header_list.extend(cherrypy.response.multiheaders)
+ cherrypy.response.header_list.extend(
+ cherrypy.response.headers.encode_header_items(
+ cherrypy.response.multiheaders))
cherrypy.tools.multiheaders = cherrypy.Tool('on_end_resource', multi_headers)
diff --git a/sphinx/source/progguide/extending/customtools.rst b/sphinx/source/progguide/extending/customtools.rst
index 3d59a308..b4d22336 100644
--- a/sphinx/source/progguide/extending/customtools.rst
+++ b/sphinx/source/progguide/extending/customtools.rst
@@ -12,7 +12,7 @@ Tools can be enabled for any point of your CherryPy application: a certain
path, a certain class, or even individual methods using the
:ref:`_cp_config <cp_config>` dictionary. Tools can also be used as decorators
which provide syntactic sugar for configuring a tool for a specific callable.
-See :doc:`/concepts/tools` for more information on how to use Tools.
+See :doc:`/tutorial/tools` for more information on how to use Tools.
This document will show you how to make your own.
Your First Custom Tool
diff --git a/sphinx/source/progguide/index.rst b/sphinx/source/progguide/index.rst
index 00494414..9b54428b 100644
--- a/sphinx/source/progguide/index.rst
+++ b/sphinx/source/progguide/index.rst
@@ -18,6 +18,7 @@ Features
Logging </refman/_cplogging>
responsetimeouts
Sessions </refman/lib/sessions>
+ security
extending/index
diff --git a/sphinx/source/progguide/security.rst b/sphinx/source/progguide/security.rst
new file mode 100644
index 00000000..3375c8cc
--- /dev/null
+++ b/sphinx/source/progguide/security.rst
@@ -0,0 +1,109 @@
+********************************
+Securing CherryPy
+********************************
+
+There are several settings that can be enabled to make CherryPy pages more secure. These include:
+
+ Transmitting data:
+
+ #. Use Secure Cookies
+
+ Rendering pages:
+
+ #. Set HttpOnly cookies
+ #. Set XFrame options
+ #. Enable XSS Protection
+ #. Set the Content Security Policy
+
+An easy way to accomplish this is to set headers with a :doc:`Tool </tutorial/tools>` and wrap your entire CherryPy application with it::
+
+ import cherrypy
+
+ def secureheaders():
+ headers = cherrypy.response.headers
+ headers['X-Frame-Options'] = 'DENY'
+ headers['X-XSS-Protection'] = '1; mode=block'
+ headers['Content-Security-Policy'] = "default-src='self'"
+
+ # set the priority according to your needs if you are hooking something
+ # else on the 'before_finalize' hook point.
+ cherrypy.tools.secureheaders = \
+ cherrypy.Tool('before_finalize', secureheaders, priority=60)
+
+
+Then, in the :doc:`configuration file </tutorial/config>` (or any other place that you want to enable the tool)::
+
+ [/]
+ tools.secureheaders.on = True
+
+
+If you use :doc:`sessions </refman/lib/sessions>` you can also enable these settings::
+
+ [/]
+ tools.sessions.on = True
+ # increase security on sessions
+ tools.sessions.secure = True
+ tools.sessions.httponly = True
+
+
+If you use SSL you can also enable Strict Transport Security::
+
+ # add this to secureheaders():
+ # only add Strict-Transport headers if we're actually using SSL; see the ietf spec
+ # "An HSTS Host MUST NOT include the STS header field in HTTP responses
+ # conveyed over non-secure transport"
+ # http://tools.ietf.org/html/draft-ietf-websec-strict-transport-sec-14#section-7.2
+ if (cherrypy.server.ssl_certificate != None and cherrypy.server.ssl_private_key != None):
+ headers['Strict-Transport-Security'] = 'max-age=31536000' # one year
+
+
+Using Secure Socket Layers (SSL)
+================================
+
+CherryPy can encrypt connections using SSL to create an https connection. This keeps your web traffic secure. Here's how.
+
+1. Generate a private key. We'll use openssl and follow the `OpenSSL Keys HOWTO <https://www.openssl.org/docs/HOWTO/keys.txt>`_.::
+
+ $ openssl genrsa -out privkey.pem 2048
+
+You can create either a key that requires a password to use, or one without a password. Protecting your private key with a password is much more secure, but requires that you enter the password every time you use the key. For example, you may have to enter the password when you start or restart your CherryPy server. This may or may not be feasible, depending on your setup.
+
+If you want to require a password, add one of the ``-aes128``, ``-aes192`` or ``-aes256`` switches to the command above. You should not use any of the DES, 3DES, or SEED algoritms to protect your password, as they are insecure.
+
+SSL Labs recommends using 2048-bit RSA keys for security (see references section at the end).
+
+
+2. Generate a certificate. We'll use openssl and follow the `OpenSSL Certificates HOWTO <https://www.openssl.org/docs/HOWTO/certificates.txt>`_. Let's start off with a self-signed certificate for testing::
+
+ $ openssl req -new -x509 -days 365 -key privkey.pem -out cert.pem
+
+openssl will then ask you a series of questions. You can enter whatever values are applicable, or leave most fields blank. The one field you *must* fill in is the 'Common Name': enter the hostname you will use to access your site. If you are just creating a certificate to test on your own machine and you access the server by typing 'localhost' into your browser, enter the Common Name 'localhost'.
+
+
+3. Decide whether you want to use python's built-in SSL library, or the pyOpenSSL library. CherryPy supports either.
+
+ a) *Built-in.* To use python's built-in SSL, add the following line to your CherryPy config::
+
+ cherrypy.server.ssl_module = 'builtin'
+
+ b) *pyOpenSSL*. Because python did not have a built-in SSL library when CherryPy was first created, the default setting is to use pyOpenSSL. To use it you'll need to install it::
+
+ $ pip install pyOpenSSL
+
+
+4. Add the following lines in your CherryPy config to point to your certificate files::
+
+ cherrypy.server.ssl_certificate = "cert.pem"
+ cherrypy.server.ssl_private_key = "privkey.pem"
+
+
+5. Start your CherryPy server normally. Note that if you are debugging locally and/or using a self-signed certificate, your browser may show you security warnings.
+
+
+
+Further Security Resources
+==========================
+
+* For an introduction to webserver security see `Dan Callahan's presentation from PyCon CA 2013 <http://pyvideo.org/video/2315/quick-wins-for-better-website-security>`_.
+* SSL Labs: `SSL/TLS Deployment Best Practises <https://www.ssllabs.com/projects/best-practices/>`_
+
diff --git a/sphinx/source/progguide/streaming.rst b/sphinx/source/progguide/streaming.rst
index 0811bb0a..58b00fe7 100644
--- a/sphinx/source/progguide/streaming.rst
+++ b/sphinx/source/progguide/streaming.rst
@@ -10,7 +10,9 @@ to *yield* content, rather than *return* content. When you use "yield", you also
have the option of streaming the output.
**In general, it is safer and easier to not stream output.** Therefore,
-streaming output is off by default.
+streaming output is off by default. Streaming output and also using sessions
+requires a good understanding of :py:mod:`how session locks work
+<cherrypy.lib.sessions>`.
The "normal" CherryPy response process
======================================
diff --git a/sphinx/source/refman/_cpreqbody.rst b/sphinx/source/refman/_cpreqbody.rst
index 0a17423e..58dca8f5 100644
--- a/sphinx/source/refman/_cpreqbody.rst
+++ b/sphinx/source/refman/_cpreqbody.rst
@@ -33,5 +33,3 @@ Functions
.. autofunction:: process_multipart_form_data
-.. autofunction:: process_multipart
-
diff --git a/sphinx/source/refman/_cptools.rst b/sphinx/source/refman/_cptools.rst
index 2a19412e..e3ddc48c 100644
--- a/sphinx/source/refman/_cptools.rst
+++ b/sphinx/source/refman/_cptools.rst
@@ -2,7 +2,7 @@
:mod:`cherrypy._cptools`
************************
-See :doc:`/concepts/tools` for a conceptual overview of CherryPy Tools.
+See :doc:`/tutorial/tools` for a conceptual overview of CherryPy Tools.
.. automodule:: cherrypy._cptools
diff --git a/sphinx/source/refman/lib/cpstats.rst b/sphinx/source/refman/lib/cpstats.rst
new file mode 100644
index 00000000..697bd7d2
--- /dev/null
+++ b/sphinx/source/refman/lib/cpstats.rst
@@ -0,0 +1,13 @@
+*****************************
+:mod:`cherrypy.lib.cpstats`
+*****************************
+
+.. automodule:: cherrypy.lib.cpstats
+
+Classes
+=======
+
+.. autoclass:: StatsTool
+ :members:
+.. autoclass:: StatsPage
+
diff --git a/sphinx/source/refman/process/plugins/dropprivileges.rst b/sphinx/source/refman/process/plugins/dropprivileges.rst
index daaa1efc..5eab3337 100644
--- a/sphinx/source/refman/process/plugins/dropprivileges.rst
+++ b/sphinx/source/refman/process/plugins/dropprivileges.rst
@@ -12,7 +12,7 @@ starting on a low port (which requires root) and then dropping to another user.
Example::
- DropPrivileges(cherrypy.engine, '/var/run/myapp.pid').subscribe()
+ DropPrivileges(cherrypy.engine, uid=1000, gid=1000).subscribe()
.. currentmodule:: cherrypy.process.plugins
diff --git a/sphinx/source/tutorial/REST.rst b/sphinx/source/tutorial/REST.rst
new file mode 100644
index 00000000..8a91a506
--- /dev/null
+++ b/sphinx/source/tutorial/REST.rst
@@ -0,0 +1,308 @@
+**********************
+Creating a RESTful API
+**********************
+
+`REST <http://en.wikipedia.org/wiki/Representational_state_transfer>`_ is an elegant architecture concept which is widely used nowadays.
+
+The point is quite simple: rely on HTTP methods and statuses and associate them with actual data manipulations.
+
+You can read more theory on the :doc:`../progguide/REST` page.
+
+Overview
+========
+
+In this tutorial, we will create a RESTful backend for a song collection management web app.
+
+A song is a *resource* with certain data (called *state*). Let's assume, every song has **title** and **artist**, and is identified by a unique **ID**.
+
+There are also *methods* to view and change the state of a resource. The basic set of methods is called `CRUD <http://en.wikipedia.org/wiki/Create,_read,_update_and_delete>`_—Create, Read, Update, and Delete.
+
+Let's assume that the frontend part is developed by someone else, and can interact with our backend part only with API requests. Our jobs is only to handle those requests, perform actions, and return the proper response.
+
+Therefore, we will not take care about templating or page rendering.
+
+We will also not use a database in this tutorial for the sake of concentrating solely on the RESTful API concept.
+
+.. note::
+
+ REST principles assume that a response status must always be meaningful. HTTP 1.1 specification already has all necessary error codes, and a developer should properly map erroneous backend events with according HTTP error codes.
+
+ Fortunately, CherryPy has done it for us. For instance, if our backend app receives a request with wrong parameters, CherryPy will raise a ``400 Bad Request`` response automatically.
+
+Download the :download:`complete example file <files/songs.py>`.
+
+Getting Started
+===============
+
+Create a file called ``songs.py`` with the following content::
+
+ import cherrypy
+
+ songs = {
+ '1': {
+ 'title': 'Lumberjack Song',
+ 'artist': 'Canadian Guard Choir'
+ },
+
+ '2': {
+ 'title': 'Always Look On the Bright Side of Life',
+ 'artist': 'Eric Idle'
+ },
+
+ '3': {
+ 'title': 'Spam Spam Spam',
+ 'artist': 'Monty Python'
+ }
+ }
+
+ class Songs:
+
+ exposed = True
+
+ if __name__ == '__main__':
+
+ cherrypy.tree.mount(
+ Songs(), '/api/songs',
+ {'/':
+ {'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
+ }
+ )
+
+ cherrypy.engine.start()
+ cherrypy.engine.block()
+
+Let's go through this code line by line.
+
+Import CherryPy::
+
+ import cherrypy
+
+Define the song "database", which is a simple Python dictionary::
+
+ songs = {
+ '1': {
+ 'title': 'Lumberjack Song',
+ 'artist': 'Canadian Guard Choir'
+ },
+
+ '2': {
+ 'title': 'Always Look On the Bright Side of Life',
+ 'artist': 'Eric Idle'
+ },
+
+ '3': {
+ 'title': 'Spam Spam Spam',
+ 'artist': 'Monty Python'
+ }
+ }
+
+Note that we are using *strings* as dict keys, not *integers*. This is done only to avoid extra type convertings when we will parse the request parameters (which are always strings.) Normally, the ID handling is performed by a database automatically, but since we do not use any, we have to deal with it manually.
+
+Create a class to represent the *songs* resource::
+
+ class Songs:
+
+Expose all the (future) class methods at once::
+
+ exposed = True
+
+Standard Python check on whether the file is used directly or as module::
+
+ if __name__ == '__main__':
+
+Create an instance of the class (called a CherryPy application) and mount it to ``/api/songs``::
+
+ cherrypy.tree.mount(
+ Songs(), '/api/songs',
+
+This means that this app will handle requests coming to the URLs starting with ``/api/songs``.
+
+Now, here goes the interesting part.
+
+CherryPy has a very helpful tool for creating RESTful APIs—the **MethodDispatcher**.
+
+Learn it and love it.
+
+Briefly speaking, it is a special sort of dispatcher which automatically connects the HTTP requests to the according handlers based on the request method. All you have to do is just name the handlers to correspond to the HTTP method names.
+
+Long story short, just call the HTTP GET handler ``GET``, and the HTTP POST handle ``POST``.
+
+Activate this dispatcher for our app::
+
+ {'/':
+ {'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
+ }
+ )
+
+Note that the ``/`` path in this config is relative to the application mount point (``/api/songs``), and will apply only to it.
+
+The last 2 lines do just the same as the ``quickstart`` method, only written a bit more explicitly—run the server::
+
+ cherrypy.engine.start()
+ cherrypy.engine.block()
+
+GET
+===
+
+Represents the Read method in CRUD.
+
+Add a new method to the ``Songs`` class in ``songs.py``, called ``GET``::
+
+ def GET(self, id=None):
+
+ if id == None:
+ return('Here are all the songs we have: %s' % songs)
+ elif id in songs:
+ song = songs[id]
+ return('Song with the ID %s is called %s, and the artist is %s' % (id, song['title'], song['artist']))
+ else:
+ return('No song with the ID %s :-(' % id)
+
+This method will return the whole song dictionary if the ID is not specified (``/api/songs``), a particular song data if the ID is specified and exists (``/api/songs/1`` ), and the message about a not existing song otherwise (``/api/songs/42``.)
+
+Try it out in your browser by going to ``127.0.0.1:8080/api/songs/``, ``127.0.0.1:8080/api/songs/1``, or ``127.0.0.1:8080/api/songs/42``.
+
+POST
+====
+
+Represents the Create method in CRUD.
+
+Add a new method to the ``Songs`` class, called ``POST``::
+
+ def POST(self, title, artist):
+
+ id = str(max([int(_) for _ in songs.keys()]) + 1)
+
+ songs[id] = {
+ 'title': title,
+ 'artist': artist
+ }
+
+ return ('Create a new song with the ID: %s' % id)
+
+This method defines the next unique ID and adds an item to the ``songs`` dictionary.
+
+Note that we do not validate the input arguments. CherryPy does it for us. If any parameter is missing or and extra one is provided, the 400 Bad Request error will be raised automatically.
+
+.. note::
+
+ Unlike GET request, POST, PUT, and DELETE requests cannot be sent via the browser URL promt.
+
+ You will need to use some special software to do it.
+
+ The recommendation here is to use `cURL <http://en.wikipedia.org/wiki/CURL>`_, which is available by default in most GNU/Linux distributions and is available for Windows and Mac.
+
+ Basic cURL usage to send a request, applied in the examples below, is as follows:
+
+ .. code-block:: bash
+
+ curl -d <param1>=<value1> -d <param2>=<value2> -X <HTTPMethod> <URL>
+
+ You can send GET requests with cURL too, but using a browser is easier.
+
+Send a POST HTTP request to ``127.0.0.1:8080/api/songs/`` with cURL:
+
+.. code-block:: bash
+
+ curl -d title='Frozen' -d artist='Madonna' -X POST '127.0.0.1:8080/api/songs/'
+
+You will see the response:
+
+ Create a new song with the ID: 4%
+
+Now, if you go to ``127.0.0.1:8080/api/songs/4`` in your browser you will se the following message:
+
+ Song with the ID 4 is called Frozen, and the artist is Madonna
+
+So it actually works!
+
+PUT
+===
+
+Represents the Update method in CRUD.
+
+Add a new method to the ``Songs`` class, called ``PUT``::
+
+ def PUT(self, id, title=None, artist=None):
+ if id in songs:
+ song = songs['id']
+
+ song['title'] = title or song['title']
+ song['artist'] = artist or song['artist']
+
+ return('Song with the ID %s is now called %s, and the artist is now %s' % (id, song['title'], song['artist']))
+ else:
+ return('No song with the ID %s :-(' % id)
+
+This method checks whether the requested song exists and updates the fields that are provided. If some field is not specified, the corresponding value will not be updated.
+
+Try sending some PUT HTTP requests to ``127.0.0.1:8080/api/songs/3`` via cURL, and check the result by requesting ``127.0.0.1:8080/api/songs/4`` in your browser:
+
+* .. code-block:: bash
+
+ curl -d title='Yesterday' -X PUT '127.0.0.1:8080/api/songs/3'
+
+ The response:
+
+ Song with the ID 3 is now called Yesterday, and the artist is now Monty Python%
+
+ What you'll see in the browser:
+
+ Song with the ID 3 is called Yesterday, and the artist is Monty Python
+
+* .. code-block:: bash
+
+ curl -d artist='Beatles' -X PUT '127.0.0.1:8080/api/songs/3'
+
+ The response:
+
+ Song with the ID 3 is now called Yesterday, and the artist is now Beatles%
+
+ What you'll see in the browser:
+
+ Song with the ID 3 is called Yesterday, and the artist is Beatles
+
+DELETE
+======
+
+Represents the DELETE method in CRUD.
+
+Add a new method to the ``Songs`` class, called ``DELETE``::
+
+ def DELETE(self, id):
+ if id in songs:
+ songs.pop(id)
+
+ return('Song with the ID %s has been deleted.' % id)
+ else:
+ return('No song with the ID %s :-(' % id)
+
+This method, like the previous ones, check if the given ID point to an existing song and pops it out of the ``songs`` dictionary.
+
+Send a DELETE HTTP request to ``127.0.0.1:8080/api/songs/2`` via cURL:
+
+.. code-block:: bash
+
+ curl -X DELETE '127.0.0.1:8080/api/songs/2'
+
+The response:
+
+ Song with the ID 2 has been deleted.%
+
+And the browser output:
+
+ No song with the ID 2 :-(
+
+Multiple Resources
+==================
+
+You can have any number of resources represented this way. Each resource is a CherryPy application, i.e. a class.
+
+For another resource, say, *users*, just create a class ``Users`` the same way you created ``Songs``, and mount it to ``/api/users`` with the same config.
+
+Conclusion and Further Steps
+============================
+
+This is pretty much it about the logic of REST API in CherryPy.
+
+You can now add actual database manipulations, parameter validation, and whatever your project may require.
diff --git a/sphinx/source/concepts/basics.rst b/sphinx/source/tutorial/basics.rst
index 4fc51332..3fc9f6bf 100644
--- a/sphinx/source/concepts/basics.rst
+++ b/sphinx/source/tutorial/basics.rst
@@ -6,12 +6,13 @@ The standard 'Hello world!' application takes less than 10 lines of code
when written using CherryPy::
import cherrypy
-
- class HelloWorld:
+
+
+ class HelloWorld(object):
def index(self):
return "Hello world!"
index.exposed = True
-
+
cherrypy.quickstart(HelloWorld())
We assume that you already have :doc:`installed </intro/install>` CherryPy.
@@ -43,7 +44,8 @@ Let's take a look at ``hello.py``:
``index()`` method will be **exposed**. Only exposed methods can be called
to answer a request. This feature allows the user to select which methods
of an object will be accessible via the Web; non-exposed methods can't be
- accessed.
+ accessed. Another way to **expose** a method is to use the decorator
+ :func:`cherrypy.expose`.
* ``cherrypy.quickstart(HelloWorld())`` mounts an instance of the HelloWorld
class, and starts the embedded webserver. It runs until explicitly
interrupted, either with ``Ctrl-C`` or via a suitable signal (a simple
@@ -51,7 +53,7 @@ Let's take a look at ``hello.py``:
When the application is executed, the CherryPy server is started with the
default configuration. It will listen on ``localhost`` at port ``8080``. These
-defaults can be overridden by using a configuration file or dictionary
+defaults can be overridden by using a :doc:`configuration file </tutorial/config>` or dictionary
(more on this later).
Finally, the web server receives the request for the URL
@@ -74,7 +76,7 @@ module. It contains several members:
* :class:`cherrypy.engine <cherrypy.process.wspbus.Bus>`
controls process startup, shutdown, and other events, including your own
- Plugins. See :doc:`/concepts/engine`.
+ Plugins. See :doc:`/tutorial/engine`.
* :class:`cherrypy.server <cherrypy._cpserver.Server>` configures and controls
the HTTP server.
* :class:`cherrypy.request <cherrypy._cprequest.Request>` contains all
@@ -87,12 +89,60 @@ module. It contains several members:
mapping that is automatically generated and encoded by CherryPy; it can
be used to store session-data in a persistent cookie. For it to work you
have to enable the session functionality by setting 'tools.session.on' to
- True in your config.
+ True in your :doc:`config </tutorial/config>`.
* :class:`cherrypy.response <cherrypy._cprequest.Response>` contains the
- data that is used to build the HTTP response.
+ data that is used to build the HTTP response.
* :attr:`cherrypy.response.headers <cherrypy.lib.httputil.HeaderMap>`
contains a mapping with the header options that will be returned by the
server, before the contents get sent.
* :attr:`cherrypy.response.body <cherrypy._cprequest.Response.body>` contains
the actual contents of the webpage that will be sent as a response.
+CherryPy Response
+-----------------
+
+The :class:`cherrypy.response <cherrypy._cprequest.Response>` object is
+available to affect aspects of the response
+to a request. Like the request, the response object is a thread-local,
+meaning although it appears to be a global variable, its value is specific
+to the current thread, and thus the current request.
+
+One may store arbitrary data in the response object.
+
+HTTP Headers
+------------
+
+CherryPy exposes the :attr:`request headers <cherrypy.lib.httputil.HeaderMap>`
+(as sent from the client), and response headers (to be returned in the
+response) in the `headers` attribute of `cherrypy.request` and
+`cherrypy.response`.
+
+For example, to find out what "host" to which the client intended to connect::
+
+ import cherrypy
+
+
+ class HelloWorld(object):
+ @cherrypy.expose
+ def index(self):
+ host = cherrypy.request.headers['Host']
+ return "You have successfully reached " + host
+
+ cherrypy.quickstart(HelloWorld())
+
+Or to set headers on the response::
+
+ import cherrypy
+
+
+ class HelloWorld(object):
+ def _get_jpeg_data(self):
+ """This method should return the jpeg data"""
+ return ""
+
+ @cherrypy.expose
+ def index(self):
+ cherrypy.response.headers['Content-Type'] = 'application/jpeg'
+ return self._get_jpeg_data()
+
+ cherrypy.quickstart(HelloWorld())
diff --git a/sphinx/source/concepts/config.rst b/sphinx/source/tutorial/config.rst
index 1b8336b0..7d231fe9 100644
--- a/sphinx/source/concepts/config.rst
+++ b/sphinx/source/tutorial/config.rst
@@ -44,7 +44,7 @@ config entries. Here's an example of passing a dict argument::
'server.socket_port': 80,
})
-The ``server.socket_host`` option n this example determines on which network
+The ``server.socket_host`` option in this example determines on which network
interface CherryPy will listen. The ``server.socket_port`` option declares
the TCP port on which to listen.
@@ -77,9 +77,9 @@ or, in python code::
'tools.trailing_slash.on': False,
}
}
- cherrypy.tree.mount(Root(), "/", config=config)
+ cherrypy.tree.mount(Root(), config=config)
-CherryPy doesn't use any sections that don't start with ``"/"`` (except
+CherryPy only uses sections that start with ``"/"`` (except
``[global]``, see below). That means you can place your own configuration
entries in a CherryPy config file by giving them a section name which does not
start with ``"/"``. For example, you might include database entries like this::
@@ -110,7 +110,7 @@ This dict contains only those config entries which apply to the given request.
.. note::
- when you do an :class:`InternalRedirect<cherrypy._cperror.InternalRedirect`,
+ when you do an :class:`InternalRedirect<cherrypy._cperror.InternalRedirect>`,
this config attribute is recalculated for the new path.
Declaration
@@ -164,7 +164,7 @@ an application, you'll see the tracebacks. In other words, use this order::
})
# Mount each app and pass it its own config
- cherrypy.tree.mount(root1, "/", appconf1)
+ cherrypy.tree.mount(root1, "", appconf1)
cherrypy.tree.mount(root2, "/forum", appconf2)
cherrypy.tree.mount(root3, "/blog", appconf3)
@@ -353,7 +353,7 @@ tools
^^^^^
Enables and configures additional request-processing packages. See the
-:doc:`/concepts/tools` overview for more information.
+:doc:`/tutorial/tools` overview for more information.
wsgi
^^^^
@@ -425,10 +425,14 @@ Environments
============
The only key that does not exist in a namespace is the *"environment"* entry.
-This special entry *imports* other config entries from a template stored in
-``cherrypy._cpconfig.environments[environment]``. It only applies to the
-global config, and only when you use
-:func:`cherrypy.config.update <cherrypy._cpconfig.Config.update>`.
+It only applies to the global config, and only when you use
+:func:`cherrypy.config.update <cherrypy._cpconfig.Config.update>`. This special
+entry *imports* other config entries from the following template stored in
+``cherrypy._cpconfig.environments[environment]``.
+
+.. literalinclude:: ../../../cherrypy/_cpconfig.py
+ :start-after: Sphinx begin config.environments
+ :end-before: Sphinx end config.environments
If you find the set of existing environments (production, staging, etc) too
limiting or just plain wrong, feel free to extend them or add new environments::
diff --git a/sphinx/source/concepts/dispatching.rst b/sphinx/source/tutorial/dispatching.rst
index b7b9c416..fb39a8c2 100644
--- a/sphinx/source/concepts/dispatching.rst
+++ b/sphinx/source/tutorial/dispatching.rst
@@ -12,7 +12,7 @@ Dispatching
request content. All of these implementation-specific issues are hidden
behind the Web interface; their nature cannot be assumed by a client that
only has access through the Web interface.
-
+
`Roy Fielding <http://www.ics.uci.edu/~fielding/pubs/dissertation/evaluation.htm>`_
When you wish to serve a resource on the Web, you never actually serve the
@@ -89,17 +89,21 @@ In this example, the URL ``http://localhost/some/page`` will be mapped to the
``root.some.page`` object. If this object is exposed (or alternatively, its
``index`` method is), it will be called for that URL.
-In our HelloWorld example, adding the ``http://onepage/`` mapping
+In our HelloWorld example, adding the ``http://localhost/onepage/`` mapping
to ``OnePage().index`` could be done like this::
+ import cherrypy
+
+
class OnePage(object):
def index(self):
return "one page!"
index.exposed = True
+
class HelloWorld(object):
onepage = OnePage()
-
+
def index(self):
return "hello world"
index.exposed = True
@@ -114,11 +118,29 @@ Normal methods
CherryPy can directly call methods on the mounted objects, if it receives a
URL that is directly mapped to them. For example::
- def foo(self):
+
+ """This example can handle the URIs
+ / -> OnePage.index
+ /foo -> OnePage.foo -> foo
+ """
+ import cherrypy
+
+
+ class OnePage(object):
+ def index(self):
+ return "one page!"
+ index.exposed = True
+
+
+ def foo():
return 'Foo!'
foo.exposed = True
-
- root.foo = foo
+
+ if __name__ == '__main__':
+ root = OnePage()
+ root.foo = foo
+ cherrypy.quickstart(root)
+
In the example, ``root.foo`` contains a function object, named ``foo``. When
CherryPy receives a request for the ``/foo`` URL, it will automatically call
@@ -162,10 +184,10 @@ method) can receive additional data from HTML or other forms using
<form action="doLogin" method="post">
<p>Username</p>
- <input type="text" name="username" value=""
+ <input type="text" name="username" value=""
size="15" maxlength="40"/>
<p>Password</p>
- <input type="password" name="password" value=""
+ <input type="password" name="password" value=""
size="10" maxlength="40"/>
<p><input type="submit" value="Login"/></p>
<p><input type="reset" value="Clear"/></p>
@@ -173,10 +195,9 @@ method) can receive additional data from HTML or other forms using
The following code can be used to handle this URL::
- class Root:
+ class Root(object):
def doLogin(self, username=None, password=None):
- # check the username & password
- ...
+ """Check the username & password"""
doLogin.exposed = True
Both arguments have to be declared as *keyword arguments*. The default value
@@ -212,16 +233,17 @@ that takes the year, month and day as part of the URL
``http://localhost/blog/2005/01/17``. This URL can be handled by the
following code::
- class Root:
+ class Root(object):
def blog(self, year, month, day):
- ...
+ """Deliver the blog post. According to *year* *month* *day*.
+ """
blog.exposed = True
-
+
root = Root()
So the URL above will be mapped as a call to::
- root.blog('2005', '1', '17')
+ root.blog('2005', '01', '17')
In this case, there is a partial match up to the ``blog`` component. The rest
of the URL can't be found in the mounted object tree. In this case, the
@@ -244,19 +266,23 @@ any other method with positional arguments, but are defined one level further
down, in case you have multiple methods to expose. For example, we could have
written the above "blog" example equivalently with a "default" method instead::
- class Blog:
+ class Blog(object):
def default(self, year, month, day):
- ...
+ """This method catch the positional arguments
+ *year*,*month*,*day* to delivery the blog content.
+ """
default.exposed = True
-
- class Root: pass
-
+
+
+ class Root(object):
+ pass
+
root = Root()
root.blog = Blog()
So the URL ``http://localhost/blog/2005/01/17`` will be mapped as a call to::
- root.blog.default('2005', '1', '17')
+ root.blog.default('2005', '01', '17')
You could achieve the same effect by defining a ``__call__`` method in this
case, but "default" just reads better. ;)
@@ -348,12 +374,12 @@ object for this, which looks a lot like::
class PageHandler(object):
"""Callable which sets response.body."""
-
+
def __init__(self, callable, *args, **kwargs):
self.callable = callable
self.args = args
self.kwargs = kwargs
-
+
def __call__(self):
return self.callable(*self.args, **self.kwargs)
@@ -369,7 +395,7 @@ Replacing page handlers
-----------------------
The handler that's going to be called during a request is available at
-:attr:`cherrypy.request.handler <cherrypy._cprequest.Request.handler`,
+:attr:`cherrypy.request.handler <cherrypy._cprequest.Request.handler>`,
which means your code has a chance to replace it before the handler runs.
It's a snap to write a Tool to do so with a
:class:`HandlerWrapperTool <cherrypy._cptools.HandlerWrapperTool>`::
diff --git a/sphinx/source/concepts/engine.rst b/sphinx/source/tutorial/engine.rst
index ac925349..ebb174ea 100644
--- a/sphinx/source/concepts/engine.rst
+++ b/sphinx/source/tutorial/engine.rst
@@ -71,6 +71,6 @@ The following builtin plugins are subscribed by default:
* :class:`Autoreloader<cherrypy.process.plugins.Autoreloader>` (off in the "production" :ref:`environment<environments>`)
* :class:`cherrypy.server<cherrypy._cpserver.Server>`
* :class:`cherrypy.checker<cherrypy._cpchecker.Checker>`
- * Engine log messages go to :class:`cherrypy.log<cherrypy._GlobalLogManager>`.
+ * :doc:`Engine log messages </refman/_cplogging>` go to :class:`cherrypy.log<cherrypy._GlobalLogManager>`.
* A :class:`Signal Handler<cherrypy.process.plugins.SignalHandler>`.
diff --git a/sphinx/source/concepts/exposing.rst b/sphinx/source/tutorial/exposing.rst
index bab4c67a..14ca5880 100644
--- a/sphinx/source/concepts/exposing.rst
+++ b/sphinx/source/tutorial/exposing.rst
@@ -18,25 +18,59 @@ Objects are **exposed** in CherryPy by setting the ``exposed`` attribute.
Most often, a method on an object is the callable that is to be invoked. In
this case, one can directly set the exposed attribute::
- class Root:
+ class Root(object):
def index(self):
- ...
+ """Handle the / URI"""
index.exposed = True
or use a decorator::
+ class Root(object):
@cherrypy.expose
def index(self):
- ...
+ """Handle the / URI"""
When it is a special method, such as ``__call__``, that is to be invoked,
the exposed attribute must be set on the class itself::
- class Node:
+ class Node(object):
exposed = True
def __call__(self):
- ...
+ """ """
+The techniques can be mixed, for example::
+ """This example can handle the URIs:
+ / -> Root.index
+ /page -> Root.page
+ /node -> Node.__call__
+ """
+ import cherrypy
+
+
+ class Node(object):
+ exposed = True
+
+ def __call__(self):
+ return "The node content"
+
+
+ class Root(object):
+
+ def __init__(self):
+ self.node = Node()
+
+ @cherrypy.expose
+ def index(self):
+ return "The index of the root object"
+
+ def page(self):
+ return 'This is the "page" content'
+ page.exposed = True
+
+
+ if __name__ == '__main__':
+ cherrypy.quickstart(Root())
+
diff --git a/sphinx/source/tutorial/files/songs.py b/sphinx/source/tutorial/files/songs.py
new file mode 100644
index 00000000..c8658b05
--- /dev/null
+++ b/sphinx/source/tutorial/files/songs.py
@@ -0,0 +1,82 @@
+import cherrypy
+
+songs = {
+ '1': {
+ 'title': 'Lumberjack Song',
+ 'artist': 'Canadian Guard Choir'
+ },
+
+ '2': {
+ 'title': 'Always Look On the Bright Side of Life',
+ 'artist': 'Eric Idle'
+ },
+
+ '3': {
+ 'title': 'Spam Spam Spam',
+ 'artist': 'Monty Python'
+ }
+}
+
+
+class Songs:
+
+ exposed = True
+
+ def GET(self, id=None):
+
+ if id is None:
+ return('Here are all the songs we have: %s' % songs)
+ elif id in songs:
+ song = songs[id]
+
+ return(
+ 'Song with the ID %s is called %s, and the artist is %s' % (
+ id, song['title'], song['artist']))
+ else:
+ return('No song with the ID %s :-(' % id)
+
+ def POST(self, title, artist):
+
+ id = str(max([int(_) for _ in songs.keys()]) + 1)
+
+ songs[id] = {
+ 'title': title,
+ 'artist': artist
+ }
+
+ return ('Create a new song with the ID: %s' % id)
+
+ def PUT(self, id, title=None, artist=None):
+ if id in songs:
+ song = songs[id]
+
+ song['title'] = title or song['title']
+ song['artist'] = artist or song['artist']
+
+ return(
+ 'Song with the ID %s is now called %s, '
+ 'and the artist is now %s' % (
+ id, song['title'], song['artist'])
+ )
+ else:
+ return('No song with the ID %s :-(' % id)
+
+ def DELETE(self, id):
+ if id in songs:
+ songs.pop(id)
+
+ return('Song with the ID %s has been deleted.' % id)
+ else:
+ return('No song with the ID %s :-(' % id)
+
+if __name__ == '__main__':
+
+ cherrypy.tree.mount(
+ Songs(), '/api/songs',
+ {'/':
+ {'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
+ }
+ )
+
+ cherrypy.engine.start()
+ cherrypy.engine.block()
diff --git a/sphinx/source/concepts/index.rst b/sphinx/source/tutorial/index.rst
index 128b9348..ea1615eb 100644
--- a/sphinx/source/concepts/index.rst
+++ b/sphinx/source/tutorial/index.rst
@@ -1,6 +1,6 @@
-*********************
-Tutorial and Concepts
-*********************
+********
+Tutorial
+********
What is this tutorial about?
============================
@@ -35,9 +35,9 @@ Learning Python
As stated above, this is not a guide to the Python language. There are plenty of
good resources for those learning Python (just to name a few among the best:
-`Python course in Bioinformatics <http://www.pasteur.fr/recherche/unites/sis/formation/python/>`_,
-`A Byte Of Python <http://www.byteofpython.info/>`_ and
-`Dive into Python <http://www.diveintopython.org/>`_).
+`Learn Python The Hard Way <http://learnpythonthehardway.org/>`_,
+`A Byte Of Python <http://swaroopch.com/notes/python/>`_ and
+`Dive into Python <http://www.diveintopython.net/>`_).
The `official Python website <http://www.python.org>`_ lists some good
resources, including an excellent
`tutorial <http://docs.python.org/tut/tut.html>`_.
@@ -54,4 +54,5 @@ Start the Tutorial
config
tools
engine
+ REST
diff --git a/sphinx/source/concepts/tools.rst b/sphinx/source/tutorial/tools.rst
index 9076b77f..9076b77f 100644
--- a/sphinx/source/concepts/tools.rst
+++ b/sphinx/source/tutorial/tools.rst
diff --git a/sphinx/util/convert-trac.py b/sphinx/util/convert-trac.py
index 52538632..ebb77d42 100644
--- a/sphinx/util/convert-trac.py
+++ b/sphinx/util/convert-trac.py
@@ -20,98 +20,115 @@ import shutil
import urllib2
from StringIO import StringIO
+
def get_options():
- global options
- parser = optparse.OptionParser(usage=inspect.cleandoc(__doc__))
- parser.add_option('-U', '--url', help="Trac URL from which to retrieve source")
- options, args = parser.parse_args()
- try:
- options.filename = args.pop()
- except IndexError:
- parser.error("Filename required")
+ global options
+ parser = optparse.OptionParser(usage=inspect.cleandoc(__doc__))
+ parser.add_option('-U', '--url',
+ help="Trac URL from which to retrieve source")
+ options, args = parser.parse_args()
+ try:
+ options.filename = args.pop()
+ except IndexError:
+ parser.error("Filename required")
# each of the replacement functions should have a docstring
# which is a regular expression to be matched.
+
def replace_external_link(matcher):
- r"\[(?P<href>(?P<scheme>\w+)\://.+?) (?P<name>.+?)\]"
- return '`{name} <{href}>`_'.format(**matcher.groupdict())
+ r"\[(?P<href>(?P<scheme>\w+)\://.+?) (?P<name>.+?)\]"
+ return '`{name} <{href}>`_'.format(**matcher.groupdict())
+
def replace_wiki_link(matcher):
- r"\[wiki\:(?P<ref>.+?) (?P<name>.+?)\]"
- return '`{name} <TODO-fix wiki target {ref}>`_'.format(**matcher.groupdict())
+ r"\[wiki\:(?P<ref>.+?) (?P<name>.+?)\]"
+ return '`{name} <TODO-fix wiki target {ref}>`_'.format(
+ **matcher.groupdict()
+ )
# character array indexed by level for characters
heading_characters = [None, '*', '=', '-', '^']
+
def replace_headings(matcher):
- r"^(?P<level>=+) (?P<name>.*) (?P=level)$"
- level = len(matcher.groupdict()['level'])
- char = heading_characters[level]
- name = matcher.groupdict()['name']
- lines = [name, char*len(name)]
- if level == 1:
- lines.insert(0, char*len(name))
- return '\n'.join(lines)
+ r"^(?P<level>=+) (?P<name>.*) (?P=level)$"
+ level = len(matcher.groupdict()['level'])
+ char = heading_characters[level]
+ name = matcher.groupdict()['name']
+ lines = [name, char * len(name)]
+ if level == 1:
+ lines.insert(0, char * len(name))
+ return '\n'.join(lines)
+
def indent(block):
- add_indent = lambda s: ' ' + s
- lines = StringIO(block)
- i_lines = map(add_indent, lines)
- return ''.join(i_lines)
+ add_indent = lambda s: ' ' + s
+ lines = StringIO(block)
+ i_lines = map(add_indent, lines)
+ return ''.join(i_lines)
+
def replace_inline_code(matcher):
- r"\{\{\{(?P<code>[^\n]*?)\}\}\}"
- return '``{code}``'.format(**matcher.groupdict())
+ r"\{\{\{(?P<code>[^\n]*?)\}\}\}"
+ return '``{code}``'.format(**matcher.groupdict())
+
def replace_code_block(matcher):
- r"\{\{\{\n(?P<code>(.|\n)*?)^\}\}\}"
- return '::\n\n' + indent(matcher.groupdict()['code'])
+ r"\{\{\{\n(?P<code>(.|\n)*?)^\}\}\}"
+ return '::\n\n' + indent(matcher.groupdict()['code'])
+
def replace_page_outline(matcher):
- r"\[\[PageOutline\]\]\n"
- return ''
+ r"\[\[PageOutline\]\]\n"
+ return ''
+
def replace_bang_symbols(matcher):
- r"!(?P<symbol>\w+)"
- return matcher.groupdict()['symbol']
+ r"!(?P<symbol>\w+)"
+ return matcher.groupdict()['symbol']
# a number of the files end in
"""{{{
#!html
<h2 class='compatibility'>Older versions</h2>
-}}}""" # and everything after is garbage, so just remove it.
+}}}""" # and everything after is garbage, so just remove it.
+
+
def remove_2x_compat_notes(matcher):
- r"\{\{\{\n#!html\n<h2(.|\n)*"
- return ''
+ r"\{\{\{\n#!html\n<h2(.|\n)*"
+ return ''
replacements = [remove_2x_compat_notes] + \
- [func for name, func in globals().items() if name.startswith('replace_')]
+ [func for name,
+ func in globals().items() if name.startswith('replace_')]
+
def normalize_linebreaks(text):
- return text.replace('\r\n', '\n')
+ return text.replace('\r\n', '\n')
+
def convert_file():
- filename = options.filename
- if options.url:
- text = urllib2.urlopen(options.url).read()
- text = normalize_linebreaks(text)
- else:
- shutil.copy(filename, filename+'.bak')
- text = open(filename).read()
- # iterate over each of the replacements and execute it
- new_text = text
- for repl in replacements:
- pattern = re.compile(inspect.getdoc(repl), re.MULTILINE)
- new_text = pattern.sub(repl, new_text)
-
- open(filename, 'w').write(new_text)
- print("done")
+ filename = options.filename
+ if options.url:
+ text = urllib2.urlopen(options.url).read()
+ text = normalize_linebreaks(text)
+ else:
+ shutil.copy(filename, filename + '.bak')
+ text = open(filename).read()
+ # iterate over each of the replacements and execute it
+ new_text = text
+ for repl in replacements:
+ pattern = re.compile(inspect.getdoc(repl), re.MULTILINE)
+ new_text = pattern.sub(repl, new_text)
+
+ open(filename, 'w').write(new_text)
+ print("done")
def handle_command_line():
- get_options()
- convert_file()
+ get_options()
+ convert_file()
if __name__ == '__main__':
- handle_command_line()
+ handle_command_line()
diff --git a/sphinx/util/test-doc.py b/sphinx/util/test-doc.py
index 29423602..3d06da99 100644
--- a/sphinx/util/test-doc.py
+++ b/sphinx/util/test-doc.py
@@ -5,11 +5,12 @@ import docutils.utils
import docutils.parsers.rst
from StringIO import StringIO
+
def print_with_line_numbers(block):
- stream = StringIO(block)
- for number, line in enumerate(stream):
- number += 1
- print(number, line.rstrip())
+ stream = StringIO(block)
+ for number, line in enumerate(stream):
+ number += 1
+ print(number, line.rstrip())
target_class_spec = sys.argv[1]
import cherrypy
@@ -17,6 +18,6 @@ target_class = eval(target_class_spec)
source = inspect.getdoc(target_class)
print_with_line_numbers(source)
parser = docutils.parsers.rst.Parser()
-settings = None ## ?
+settings = None # ?
document = docutils.utils.new_document(source, settings)
parser.parse(source, document)
diff --git a/tox.ini b/tox.ini
new file mode 100644
index 00000000..e68f3e2c
--- /dev/null
+++ b/tox.ini
@@ -0,0 +1,8 @@
+[tox]
+envlist = py26, py27, py32, py33, pypy
+
+[testenv]
+deps = nose
+ Routes
+commands =
+ nosetests -s []