Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 1171

Show
Ignore:
Timestamp:
06/28/06 18:06:42
Author:
fumanchu
Message:

Expanded baseurl tool into a new proxy tool.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/_cptools.py

    r1163 r1171  
    6363        For example: 
    6464         
    65             @tools.base_url() 
     65            @tools.proxy() 
    6666            def whats_my_base(self): 
    6767                return cherrypy.request.base 
     
    285285default_toolbox = Toolbox() 
    286286default_toolbox.session_auth = MainTool(cptools.session_auth) 
    287 default_toolbox.base_url = Tool('before_request_body', cptools.base_url
     287default_toolbox.proxy = Tool('before_request_body', cptools.proxy
    288288default_toolbox.response_headers = Tool('before_finalize', cptools.response_headers) 
    289289# We can't call virtual_host in on_start_resource, 
  • trunk/cherrypy/lib/cptools.py

    r1141 r1171  
    6262#                                Tool code                                # 
    6363 
    64 def base_url(base=None, use_x_forwarded_host=True): 
     64def proxy(base=None, local='X-Forwarded-Host', remote='X-Forwarded-For'): 
    6565    """Change the base URL (scheme://host[:port]). 
    6666     
     
    7777            base = 'http://localhost:%s' % port 
    7878     
    79     if use_x_forwarded_host
    80         base = request.headers.get("X-Forwarded-Host", base) 
     79    if local
     80        base = request.headers.get(local, base) 
    8181     
    8282    if base.find("://") == -1: 
     
    8585     
    8686    request.base = base 
     87     
     88    if remote: 
     89        xff = request.headers.get(remote) 
     90        if xff: 
     91            if remote == 'X-Forwarded-For': 
     92                # See http://bob.pythonmac.org/archives/2005/09/23/apache-x-forwarded-for-caveat/ 
     93                xff = xff.split(',')[-1].strip() 
     94            request.remote_host = xff 
    8795 
    8896 
  • trunk/cherrypy/test/test.py

    r1150 r1171  
    305305     
    306306    testList = [ 
    307         'test_baseurl', 
     307        'test_proxy', 
    308308        'test_caching', 
    309309        'test_config', 
  • trunk/cherrypy/test/test_proxy.py

    r1169 r1171  
    1010            raise cherrypy.HTTPRedirect('dummy') 
    1111        index.exposed = True 
     12         
     13        def remotehost(self): 
     14            return cherrypy.request.remote_host 
     15        remotehost.exposed = True 
     16         
     17        def xhost(self): 
     18            raise cherrypy.HTTPRedirect('blah') 
     19        xhost.exposed = True 
     20        xhost._cp_config = {'tools.proxy.local': 'X-Host'} 
    1221     
    1322    cherrypy.tree.mount(Root()) 
     
    1524            'environment': 'production', 
    1625            'log_to_screen': False, 
    17             'tools.base_url.on': True, 
    18             'tools.base_url.base': 'http://www.mydomain.com', 
     26            'tools.proxy.on': True, 
     27            'tools.proxy.base': 'http://www.mydomain.com', 
    1928    }) 
    2029 
     
    2231import helper 
    2332 
    24 class BaseUrlTest(helper.CPWebCase): 
     33class ProxyTest(helper.CPWebCase): 
    2534     
    26     def testBaseUrl(self): 
     35    def testProxy(self): 
    2736        self.getPage("/") 
    2837        self.assertHeader('Location', 
    2938                          "http://www.mydomain.com%s/dummy" % self.prefix()) 
     39         
     40        # Test X-Forwarded-Host (Apache 1.3.33+ and Apache 2) 
     41        self.getPage("/", headers=[('X-Forwarded-Host', 'http://www.yetanother.com')]) 
     42        self.assertHeader('Location', "http://www.yetanother.com/dummy") 
     43        self.getPage("/", headers=[('X-Forwarded-Host', 'www.yetanother.com')]) 
     44        self.assertHeader('Location', "http://www.yetanother.com/dummy") 
     45         
     46        # Test X-Forwarded-For (Apache2) 
     47        self.getPage("/remotehost", 
     48                     headers=[('X-Forwarded-For', '192.168.0.20')]) 
     49        self.assertBody("192.168.0.20") 
     50        self.getPage("/remotehost", 
     51                     headers=[('X-Forwarded-For', '67.15.36.43, 192.168.0.20')]) 
     52        self.assertBody("192.168.0.20") 
     53         
     54        # Test X-Host (lighttpd; see https://trac.lighttpd.net/trac/ticket/418) 
     55        self.getPage("/xhost", headers=[('X-Host', 'http://www.yetanother.com')]) 
     56        self.assertHeader('Location', "http://www.yetanother.com/blah") 
    3057 
    3158 

Hosted by WebFaction

Log in as guest/cpguest to create tickets