Commits

Robert Brewer committed e11dafe

New scheme arg to tools.proxy.

  • Participants
  • Parent commits 7b8ea0f

Comments (0)

Files changed (2)

File cherrypy/lib/cptools.py

 
 #                                Tool code                                #
 
-def proxy(base=None, local='X-Forwarded-Host', remote='X-Forwarded-For'):
+def proxy(base=None, local='X-Forwarded-Host', remote='X-Forwarded-For',
+          scheme='X-Forwarded-Proto'):
     """Change the base URL (scheme://host[:port][/path]).
     
-    Useful when running a CP server behind Apache.
+    For running a CP server behind Apache, lighttpd, or other HTTP server.
     
     If you want the new request.base to include path info (not just the host),
     you must explicitly set base to the full base path, and ALSO set 'local'
     
     request = cherrypy.request
     
-    if base is None:
-        port = cherrypy.local.port
-        if port == 80:
-            base = 'http://localhost'
-        else:
-            base = 'http://localhost:%s' % port
+    if scheme:
+        scheme = request.headers.get(scheme, None)
+    if not scheme:
+        scheme = request.base[:request.base.find("://")]
     
     if local:
         base = request.headers.get(local, base)
+    if not base:
+        port = cherrypy.request.local.port
+        if port == 80:
+            base = 'localhost'
+        else:
+            base = 'localhost:%s' % port
     
     if base.find("://") == -1:
         # add http:// or https:// if needed
-        base = request.base[:request.base.find("://") + 3] + base
+        base = scheme + "://" + base
     
     request.base = base
     

File cherrypy/test/test_proxy.py

         xhost.exposed = True
         xhost._cp_config = {'tools.proxy.local': 'X-Host'}
         
+        def base(self):
+            return cherrypy.request.base
+        base.exposed = True
+        
         def newurl(self):
             return ("Browse to <a href='%s'>this page</a>."
                     % cherrypy.tree.url("/this/new/page"))
     cherrypy.config.update({
         'environment': 'test_suite',
         'tools.proxy.on': True,
-        'tools.proxy.base': 'http://www.mydomain.com',
+        'tools.proxy.base': 'www.mydomain.com',
         })
 
 
         self.assertBody("192.168.0.20")
         
         # Test X-Host (lighttpd; see https://trac.lighttpd.net/trac/ticket/418)
-        self.getPage("/xhost", headers=[('X-Host', 'http://www.yetanother.com')])
+        self.getPage("/xhost", headers=[('X-Host', 'www.yetanother.com')])
         self.assertHeader('Location', "http://www.yetanother.com/blah")
         
+        # Test X-Forwarded-Proto (lighttpd)
+        self.getPage("/base", headers=[('X-Forwarded-Proto', 'https')])
+        self.assertBody("https://www.mydomain.com")
+        
         # Test tree.url()
         for sn in script_names:
             self.getPage(sn + "/newurl")