2018-09-30 09:43:20 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-04-20 17:57:06 +02:00
|
|
|
# Flask License
|
2019-01-20 19:37:45 +01:00
|
|
|
#
|
2019-04-20 17:57:06 +02:00
|
|
|
# Copyright © 2010 by the Pallets team, cervinko, janeczku, OzzieIsaacs
|
2019-01-20 19:37:45 +01:00
|
|
|
#
|
2019-04-20 17:57:06 +02:00
|
|
|
# Some rights reserved.
|
2019-01-20 19:37:45 +01:00
|
|
|
#
|
2019-04-20 17:57:06 +02:00
|
|
|
# Redistribution and use in source and binary forms of the software as
|
|
|
|
# well as documentation, with or without modification, are permitted
|
|
|
|
# provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions and the following disclaimer.
|
|
|
|
#
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
#
|
|
|
|
# * Neither the name of the copyright holder nor the names of its
|
|
|
|
# contributors may be used to endorse or promote products derived from
|
|
|
|
# this software without specific prior written permission.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
|
|
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
|
|
|
|
# BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
|
|
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
# THIS SOFTWARE AND DOCUMENTATION, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
# SUCH DAMAGE.
|
|
|
|
#
|
|
|
|
# Inspired by http://flask.pocoo.org/snippets/35/
|
2019-01-20 19:37:45 +01:00
|
|
|
|
2018-09-30 09:43:20 +02:00
|
|
|
class ReverseProxied(object):
|
|
|
|
"""Wrap the application in this middleware and configure the
|
|
|
|
front-end server to add these headers, to let you quietly bind
|
|
|
|
this to a URL other than / and to an HTTP scheme that is
|
|
|
|
different than what is used locally.
|
|
|
|
|
|
|
|
Code courtesy of: http://flask.pocoo.org/snippets/35/
|
|
|
|
|
|
|
|
In nginx:
|
|
|
|
location /myprefix {
|
|
|
|
proxy_pass http://127.0.0.1:8083;
|
|
|
|
proxy_set_header Host $host;
|
|
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
|
|
proxy_set_header X-Scheme $scheme;
|
|
|
|
proxy_set_header X-Script-Name /myprefix;
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, application):
|
|
|
|
self.app = application
|
|
|
|
|
|
|
|
def __call__(self, environ, start_response):
|
|
|
|
script_name = environ.get('HTTP_X_SCRIPT_NAME', '')
|
|
|
|
if script_name:
|
|
|
|
environ['SCRIPT_NAME'] = script_name
|
|
|
|
path_info = environ.get('PATH_INFO', '')
|
|
|
|
if path_info and path_info.startswith(script_name):
|
|
|
|
environ['PATH_INFO'] = path_info[len(script_name):]
|
|
|
|
|
|
|
|
scheme = environ.get('HTTP_X_SCHEME', '')
|
|
|
|
if scheme:
|
|
|
|
environ['wsgi.url_scheme'] = scheme
|
2019-04-03 10:25:29 +02:00
|
|
|
servr = environ.get('HTTP_X_FORWARDED_HOST', '')
|
2018-09-30 09:43:20 +02:00
|
|
|
if servr:
|
|
|
|
environ['HTTP_HOST'] = servr
|
|
|
|
return self.app(environ, start_response)
|