Changeset 1171
- Timestamp:
- 06/28/06 18:06:42
- Files:
-
- trunk/cherrypy/_cptools.py (modified) (2 diffs)
- trunk/cherrypy/lib/cptools.py (modified) (3 diffs)
- trunk/cherrypy/test/test.py (modified) (1 diff)
- trunk/cherrypy/test/test_proxy.py (moved) (moved from trunk/cherrypy/test/test_baseurl.py) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/_cptools.py
r1163 r1171 63 63 For example: 64 64 65 @tools. base_url()65 @tools.proxy() 66 66 def whats_my_base(self): 67 67 return cherrypy.request.base … … 285 285 default_toolbox = Toolbox() 286 286 default_toolbox.session_auth = MainTool(cptools.session_auth) 287 default_toolbox. base_url = Tool('before_request_body', cptools.base_url)287 default_toolbox.proxy = Tool('before_request_body', cptools.proxy) 288 288 default_toolbox.response_headers = Tool('before_finalize', cptools.response_headers) 289 289 # We can't call virtual_host in on_start_resource, trunk/cherrypy/lib/cptools.py
r1141 r1171 62 62 # Tool code # 63 63 64 def base_url(base=None, use_x_forwarded_host=True):64 def proxy(base=None, local='X-Forwarded-Host', remote='X-Forwarded-For'): 65 65 """Change the base URL (scheme://host[:port]). 66 66 … … 77 77 base = 'http://localhost:%s' % port 78 78 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) 81 81 82 82 if base.find("://") == -1: … … 85 85 86 86 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 87 95 88 96 trunk/cherrypy/test/test.py
r1150 r1171 305 305 306 306 testList = [ 307 'test_ baseurl',307 'test_proxy', 308 308 'test_caching', 309 309 'test_config', trunk/cherrypy/test/test_proxy.py
r1169 r1171 10 10 raise cherrypy.HTTPRedirect('dummy') 11 11 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'} 12 21 13 22 cherrypy.tree.mount(Root()) … … 15 24 'environment': 'production', 16 25 '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', 19 28 }) 20 29 … … 22 31 import helper 23 32 24 class BaseUrlTest(helper.CPWebCase):33 class ProxyTest(helper.CPWebCase): 25 34 26 def test BaseUrl(self):35 def testProxy(self): 27 36 self.getPage("/") 28 37 self.assertHeader('Location', 29 38 "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") 30 57 31 58

