/lib/galaxy/web/framework/middleware/xforwardedhost.py

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 21 lines · 17 code · 0 blank · 4 comment · 3 complexity · da3733006c851712a8f3dd9a0add732d MD5 · raw file

  1. class XForwardedHostMiddleware( object ):
  2. """
  3. A WSGI middleware that changes the HTTP host header in the WSGI environ
  4. based on the X-Forwarded-Host header IF found
  5. """
  6. def __init__( self, app, global_conf=None ):
  7. self.app = app
  8. def __call__( self, environ, start_response ):
  9. x_forwarded_host = environ.get( 'HTTP_X_FORWARDED_HOST', None )
  10. if x_forwarded_host:
  11. environ[ 'ORGINAL_HTTP_HOST' ] = environ[ 'HTTP_HOST' ]
  12. environ[ 'HTTP_HOST' ] = x_forwarded_host
  13. x_forwarded_for = environ.get( 'HTTP_X_FORWARDED_FOR', None )
  14. if x_forwarded_for:
  15. environ[ 'ORGINAL_REMOTE_ADDR' ] = environ[ 'REMOTE_ADDR' ]
  16. environ[ 'REMOTE_ADDR' ] = x_forwarded_for
  17. x_url_scheme = environ.get( 'HTTP_X_URL_SCHEME', None )
  18. if x_url_scheme:
  19. environ[ 'original_wsgi.url_scheme' ] = environ[ 'wsgi.url_scheme' ]
  20. environ[ 'wsgi.url_scheme' ] = x_url_scheme
  21. return self.app( environ, start_response )