From 8083ba94c3dc66f0ac99c5c27b1161c134dc4ca3 Mon Sep 17 00:00:00 2001 From: Christophe de Vienne Date: Wed, 28 Mar 2012 15:18:32 +0200 Subject: [PATCH] TG1 server.webpath mechanics makes it impossible to use a filter on the controller itself. So we put it on the root controller and carrefully change the WSRoot webpath so that everything works properly --- wsme/root.py | 5 +++-- wsme/tg1.py | 31 ++++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/wsme/root.py b/wsme/root.py index 433bb00..87227c8 100644 --- a/wsme/root.py +++ b/wsme/root.py @@ -105,9 +105,9 @@ class WSRoot(object): def _select_protocol(self, request): log.debug("Selecting a protocol for the following request :\n" "headers: %s\nbody: %s", request.headers, - len(request.body) > 512 + request.body and (len(request.body) > 512 and request.body[:512] - or request.body) + or request.body) or '') protocol = None if 'wsmeproto' in request.params: return self._get_protocol(request.params['wsmeproto']) @@ -173,6 +173,7 @@ class WSRoot(object): protocol = self._select_protocol(request) except Exception, e: msg = ("Error while selecting protocol: %s" % str(e)) + log.exception(msg) protocol = None if protocol is None: diff --git a/wsme/tg1.py b/wsme/tg1.py index e85427e..8afc2e3 100644 --- a/wsme/tg1.py +++ b/wsme/tg1.py @@ -1,17 +1,22 @@ import cherrypy from cherrypy.filters.basefilter import BaseFilter import webob -from turbogears import expose +from turbogears import expose, config +from turbogears.startup import call_on_startup, call_on_shutdown class WSMECherrypyFilter(BaseFilter): + def __init__(self, controller): + self.controller = controller + self.webpath = None + def on_start_resource(self): - cherrypy.request.processRequestBody = False + path = cherrypy.request.path + if path.startswith(self.controller._wsroot._webpath): + cherrypy.request.processRequestBody = False class Controller(object): - _cp_filters = [WSMECherrypyFilter()] - def __init__(self, wsroot): self._wsroot = wsroot @@ -25,4 +30,20 @@ class Controller(object): def adapt(wsroot): - return Controller(wsroot) + controller = Controller(wsroot) + filter_ = WSMECherrypyFilter(controller) + + def install_filter(): + filter_.webpath = config.get('server.webpath') or '' + controller._wsroot._webpath = \ + filter_.webpath + controller._wsroot._webpath + cherrypy.root._cp_filters.append(filter_) + + def uninstall_filter(): + cherrypy.root._cp_filters.remove(filter_) + controller._wsroot._webpath = \ + controller._wsroot._webpath[len(filter_.webpath):] + + call_on_startup.append(install_filter) + call_on_shutdown.append(uninstall_filter) + return controller