Use 'wsgicors' for CORS handling.
authorChristophe de Vienne <christophe@unlish.com>
Fri, 19 Sep 2014 19:17:50 +0200
changeset 11511 13e0f569684c
parent 11510 2e52647af650
child 11512 bb548010b390
Use 'wsgicors' for CORS handling. The CW CORS handling (in web/cors.py) is only able to work on cubicweb requests. When a request is not handled by bwcompat, we need a proper solution. The `wsgicors` library provides what we need as a wsgi middleware.
pyramid_cubicweb/__init__.py
pyramid_cubicweb/bwcompat.py
setup.py
--- a/pyramid_cubicweb/__init__.py	Thu Sep 18 17:18:09 2014 +0200
+++ b/pyramid_cubicweb/__init__.py	Fri Sep 19 19:17:50 2014 +0200
@@ -1,4 +1,5 @@
 import os
+import wsgicors
 
 from cubicweb.cwconfig import CubicWebConfiguration as cwcfg
 from pyramid.config import Configurator
@@ -39,4 +40,13 @@
 
     cwconfig = cwcfg.config_for(instance_name, debugmode=debug)
     config = make_cubicweb_application(cwconfig)
-    return config.make_wsgi_app()
+    app = config.make_wsgi_app()
+    # This replaces completely web/cors.py, which is not used by
+    # pyramid_cubicweb anymore
+    app = wsgicors.CORS(
+        app,
+        origin=cwconfig['access-control-allow-origin'],
+        headers=cwconfig['access-control-allow-headers'],
+        methods=cwconfig['access-control-allow-methods'],
+        credentials='true')
+    return app
--- a/pyramid_cubicweb/bwcompat.py	Thu Sep 18 17:18:09 2014 +0200
+++ b/pyramid_cubicweb/bwcompat.py	Fri Sep 19 19:17:50 2014 +0200
@@ -45,9 +45,9 @@
         vreg = request.registry['cubicweb.registry']
 
         try:
+            content = None
             try:
                 with cw_to_pyramid(request):
-                    cors.process_request(req, vreg.config)
                     ctrlid, rset = self.appli.url_resolver.process(req, req.path)
 
                     try:
@@ -69,8 +69,6 @@
                         # commited = True
                         if txuuid is not None:
                             req.data['last_undoable_transaction'] = txuuid
-            except cors.CORSPreflight:
-                request.response.status_int = 200
             except cubicweb.web.ValidationError as ex:
                 # XXX The validation_error_handler implementation is light, we
                 # should redo it better in cw_to_pyramid, so it can be properly
--- a/setup.py	Thu Sep 18 17:18:09 2014 +0200
+++ b/setup.py	Fri Sep 19 19:17:50 2014 +0200
@@ -29,6 +29,7 @@
     install_requires=[
         'pyramid >= 1.5.0',
         'waitress >= 0.8.9',
-        'cubicweb >= 3.19.3'
+        'cubicweb >= 3.19.3',
+        'wsgicors >= 0.3'
     ]
 )