Commits

Christophe de Vienne committed 0701f36

Add a cherrypy 2.x filter

  • Participants
  • Parent commits a723e66

Comments (0)

Files changed (2)

File wsmorph/__init__.py

 log = logging.getLogger(__name__)
 
 
+def getrequest_content_type(request):
+    if hasattr(request, 'content_type'):
+        content_type = request.content_type
+    else:
+        content_type = request.headers.get('Content-Type', '')
+    return content_type
+
+
+def getrequest_accept(request):
+    if hasattr(request, 'accept'):
+        accept = request.accept
+    else:
+        accept = request.headers.get('Accept', '')
+    return accept
+
+
 def loadxml(node):
     if node.get('nil', False):
         return None
         self.target_webpath = target_webpath
 
     def iter_calls(self, context, request):
-        fname = request.path[len(self.webpath):].strip('/')
+        fname = request.path[len(self.webpath):]
         context.fname = fname
         yield Call(request, fname)
 
     def patch_request(self, context, request):
-        request.upath_info = \
-            self.target_webpath + context.fname
+        newpath = self.target_webpath + context.fname
+        if hasattr(request, 'upath_info'):
+            request.upath_info = newpath
+        else:
+            request.path = newpath
         self.patch_request_body(context, request)
 
     def patch_response(self, request, response):
 
 class RestJson(Rest):
     def accept(self, request):
-        return 'text/javascript' in request.content_type \
-            or 'application/json' in request.content_type \
-            or 'text/javascript' in request.accept \
-            or 'application/json' in request.accept \
+        content_type = getrequest_content_type(request)
+        accept = getrequest_accept(request)
+
+        return 'text/javascript' in content_type \
+            or 'application/json' in content_type \
+            or 'text/javascript' in accept \
+            or 'application/json' in accept \
             or request.path.endswith('.json')
 
     def getjsondata(self, call):
 
 class RestXml(Rest):
     def accept(self, request):
-        return 'text/xml' in request.content_type \
-            or 'text/xml' in request.accept \
+        content_type = getrequest_content_type(request)
+        accept = getrequest_accept(request)
+
+        return 'text/xml' in content_type \
+            or 'text/xml' in accept \
             or request.path.endswith('.xml')
 
     def getxml(self, call):
             self.calls = []
 
     def __init__(self, webpath, target_webpath=None):
-        self.webpath = webpath
-        self.target_webpath = target_webpath or webpath
+        self.webpath = '/%s/' % webpath.strip('/')
+        self.target_webpath = '/%s/' % (target_webpath or webpath).strip('/')
         self.protocols = []
         self.rulesets = []
 
         for protocol in self.protocols:
             if protocol.accept(request):
                 return protocol
-        raise RuntimeError("No procotol found for request %s" % request)
+        msg = "No procotol found for path=%s, ct=%s, accept=%s" % (
+            request, getrequest_content_type(request),
+            getrequest_accept(request)
+        )
+        log.error(msg)
+        raise RuntimeError(msg)
 
     def morph_request(self, request):
         if not request.path.startswith(self.webpath):

File wsmorph/cherrypyfilter.py

+from cherrypy.filters.basefilter import BaseFilter
+import cherrypy
+
+
+class WSMorphFilter(BaseFilter):
+    def __init__(self, morph):
+        self.morph = morph
+
+    def before_request_body(self):
+        print 'start', cherrypy.request.path
+        context = self.morph.morph_request(cherrypy.request)
+        if context is not None:
+            print "something happened", cherrypy.request.path
+            cherrypy.request.object_path = cherrypy.request.path
+            cherrypy.request._wsmorph_context = context
+
+    def on_end_resource(self):
+        if hasattr(cherrypy.request, '_wsmorph_context'):
+            self.morph.morph_response(
+                cherrypy.request._wsmorph_context,
+                cherrypy.response
+            )