Use 'wsgicors' for CORS handling.
authorChristophe de Vienne <christophe@unlish.com>
Fri, 19 Sep 2014 19:20:35 +0200
changeset 11640 e45e4999dc98
parent 11639 f38ec5e29de3
child 11641 3d4e7be2e3c3
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.
__pkginfo__.py
ccplugin.py
--- a/__pkginfo__.py	Fri Sep 19 18:23:57 2014 +0200
+++ b/__pkginfo__.py	Fri Sep 19 19:20:35 2014 +0200
@@ -13,7 +13,7 @@
 description = "Add the 'pyramid' command to cubicweb-ctl"
 web = 'http://www.cubicweb.org/project/%s' % distname
 
-__depends__ = {'cubicweb': '>= 3.19.3'}
+__depends__ = {'cubicweb': '>= 3.19.3', 'wsgicors': '>= 0.3'}
 __recommends__ = {}
 
 classifiers = [
--- a/ccplugin.py	Fri Sep 19 18:23:57 2014 +0200
+++ b/ccplugin.py	Fri Sep 19 19:20:35 2014 +0200
@@ -14,6 +14,8 @@
 import threading
 import subprocess
 
+import wsgicors
+
 from cubicweb import BadCommandUsage, ExecutionError
 from cubicweb.cwconfig import CubicWebConfiguration as cwcfg
 from cubicweb.cwctl import CWCTL, InstanceCommand, init_cmdline_log_threshold
@@ -263,11 +265,21 @@
 
         pyramid_config = make_cubicweb_application(cwconfig)
 
+        app = pyramid_config.make_wsgi_app()
+
+        # This replaces completely web/cors.py, which is not used by
+        # pyramid_cubicweb anymore
+        app = wsgicors.CORS(
+            app,
+            origin=' '.join(cwconfig['access-control-allow-origin']),
+            headers=','.join(cwconfig['access-control-allow-headers']),
+            methods=','.join(cwconfig['access-control-allow-methods']),
+            credentials='true')
+
         repo = cwconfig.repository()
         try:
             repo.start_looping_tasks()
-            waitress.serve(
-                pyramid_config.make_wsgi_app(), host=host, port=port)
+            waitress.serve(app, host=host, port=port)
         finally:
             repo.shutdown()
         if self._needreload: