Commits

Robert Brewer committed 19d41e0

X-Forwarded-For should overwrite remote.ip, not remote.name.

Comments (0)

Files changed (3)

     you must explicitly set base to the full base path, and ALSO set 'local'
     to '', so that the X-Forwarded-Host request header (which never includes
     path info) does not override it.
+    
+    cherrypy.request.remote.ip (the IP address of the client) will be
+    rewritten if the header specified by the 'remote' arg is valid.
+    By default, 'remote' is set to 'X-Forwarded-For'. If you do not
+    want to rewrite remote.ip, set the 'remote' arg to an empty string.
     """
     
     request = cherrypy.request
             if remote == 'X-Forwarded-For':
                 # See http://bob.pythonmac.org/archives/2005/09/23/apache-x-forwarded-for-caveat/
                 xff = xff.split(',')[-1].strip()
-            request.remote.name = xff
+            request.remote.ip = xff
 
 
 def response_headers(headers=None):
 
 
 def decode(encoding=None, default_encoding='utf-8'):
-    """Decode cherrypy.request.params ."""
+    """Decode cherrypy.request.params."""
     if not encoding:
         ct = cherrypy.request.headers.elements("Content-Type")
         if ct:

test/test_proxy.py

             raise cherrypy.HTTPRedirect('dummy')
         index.exposed = True
         
-        def remotehost(self):
-            return cherrypy.request.remote.name
-        remotehost.exposed = True
+        def remoteip(self):
+            return cherrypy.request.remote.ip
+        remoteip.exposed = True
         
         def xhost(self):
             raise cherrypy.HTTPRedirect('blah')
         self.assertHeader('Location', "http://www.yetanother.com/dummy")
         
         # Test X-Forwarded-For (Apache2)
-        self.getPage("/remotehost",
+        self.getPage("/remoteip",
                      headers=[('X-Forwarded-For', '192.168.0.20')])
         self.assertBody("192.168.0.20")
-        self.getPage("/remotehost",
+        self.getPage("/remoteip",
                      headers=[('X-Forwarded-For', '67.15.36.43, 192.168.0.20')])
         self.assertBody("192.168.0.20")
         
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.