[wsgi] Fix https detection
authorChristophe de Vienne <christophe@unlish.com>
Tue, 02 Sep 2014 10:30:28 +0200
changeset 9941 8dc1c96d29f1
parent 9940 292f786009ba
child 9942 4b99196102f0
[wsgi] Fix https detection - 'HTTPS' is not part of wsgi, it is a CGI variable. The right wsgi variable is 'wsgi.url_scheme' - Force https if url starts with '/https/' (and remove the prefix from the path. Closes #4305985
wsgi/request.py
wsgi/test/unittest_wsgi.py
--- a/wsgi/request.py	Thu Jul 24 20:57:14 2014 +0200
+++ b/wsgi/request.py	Tue Sep 02 10:30:28 2014 +0200
@@ -61,7 +61,12 @@
                           if k.startswith('HTTP_'))
         if 'CONTENT_TYPE' in environ:
             headers_in['Content-Type'] = environ['CONTENT_TYPE']
-        https = environ.get("HTTPS") in ('yes', 'on', '1')
+        https = environ["wsgi.url_scheme"] == 'https'
+        if self.path.startswith('/https/'):
+            self.path = self.path[6:]
+            self.environ['PATH_INFO'] = self.path
+            https = True
+
         post, files = self.get_posted_data()
 
         super(CubicWebWsgiRequest, self).__init__(vreg, https, post,
--- a/wsgi/test/unittest_wsgi.py	Thu Jul 24 20:57:14 2014 +0200
+++ b/wsgi/test/unittest_wsgi.py	Tue Sep 02 10:30:28 2014 +0200
@@ -23,3 +23,33 @@
         req = CubicWebWsgiRequest(r.environ, self.vreg)
 
         self.assertEqual('some content', req.content.read())
+
+    def test_http_scheme(self):
+        r = webtest.app.TestRequest.blank('/', {
+            'wsgi.url_scheme': 'http'})
+
+        req = CubicWebWsgiRequest(r.environ, self.vreg)
+
+        self.assertFalse(req.https)
+
+    def test_https_scheme(self):
+        r = webtest.app.TestRequest.blank('/', {
+            'wsgi.url_scheme': 'https'})
+
+        req = CubicWebWsgiRequest(r.environ, self.vreg)
+
+        self.assertTrue(req.https)
+
+    def test_https_prefix(self):
+        r = webtest.app.TestRequest.blank('/https/', {
+            'wsgi.url_scheme': 'http'})
+
+        req = CubicWebWsgiRequest(r.environ, self.vreg)
+
+        self.assertTrue(req.https)
+
+    @classmethod
+    def init_config(cls, config):
+        super(WSGIAppTC, cls).init_config(config)
+        config.https_uiprops = None
+        config.https_datadir_url = None