From 2708701cc737de35bd27cde03dc4bd2a32ba91c4 Mon Sep 17 00:00:00 2001 From: Daniel Grossmann-Kavanagh Date: Tue, 8 Jan 2019 12:48:17 -0800 Subject: [PATCH] add wsgi.py file --- connexion/wsgi.py | 23 ++++++++++++ examples/openapi3/reverseproxy/README.rst | 44 +++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 connexion/wsgi.py diff --git a/connexion/wsgi.py b/connexion/wsgi.py new file mode 100644 index 000000000..6bdf14c6f --- /dev/null +++ b/connexion/wsgi.py @@ -0,0 +1,23 @@ +class ReverseProxied(object): + + def __init__(self, app, script_name=None, scheme=None, server=None): + self.app = app + self.script_name = script_name + self.scheme = scheme + self.server = server + + def __call__(self, environ, start_response): + script_name = environ.get('HTTP_X_SCRIPT_NAME', '') or self.script_name + if script_name: + environ['SCRIPT_NAME'] = script_name + path_info = environ['PATH_INFO'] + if path_info.startswith(script_name): + environ['PATH_INFO_OLD'] = path_info + environ['PATH_INFO'] = path_info[len(script_name):] + scheme = environ.get('HTTP_X_SCHEME', '') or self.scheme + if scheme: + environ['wsgi.url_scheme'] = scheme + server = environ.get('HTTP_X_FORWARDED_SERVER', '') or self.server + if server: + environ['HTTP_HOST'] = server + return self.app(environ, start_response) diff --git a/examples/openapi3/reverseproxy/README.rst b/examples/openapi3/reverseproxy/README.rst index 274600eef..e1bc29334 100644 --- a/examples/openapi3/reverseproxy/README.rst +++ b/examples/openapi3/reverseproxy/README.rst @@ -4,6 +4,8 @@ Reverse Proxy Example This example demonstrates how to run a connexion application behind a path-altering reverse proxy. +You can either set the path in your app, or set the ``X-Script-Name`` header. + Running: .. code-block:: bash @@ -12,3 +14,45 @@ Running: $ ./app.py Now open your browser and go to http://localhost:8080/reverse_proxied/ui/ to see the Swagger UI. + + +You can also use the ``X-Script-Name`` header to modify the reverse proxy path. +For example: + +.. code-block:: bash + + curl -H "X-Script-Name: banana" http://localhost:8080/openapi.json + + { + "servers" : [ + { + "url" : "banana" + } + ], + "paths" : { + "/hello" : { + "get" : { + "responses" : { + "200" : { + "description" : "hello", + "content" : { + "text/plain" : { + "schema" : { + "type" : "string" + } + } + } + } + }, + "operationId" : "app.hello", + "summary" : "say hi" + } + } + }, + "openapi" : "3.0.0", + "info" : { + "version" : "1.0", + "title" : "Path-Altering Reverse Proxy Example" + } + } +