From c04c7ba71a752ff28cf85a1dbdd77b03cfbe55a2 Mon Sep 17 00:00:00 2001 From: Tom Kralidis Date: Tue, 14 May 2019 15:25:40 -0400 Subject: [PATCH] fix pathing to work with subpaths behind proxies (#120) --- pygeoapi/api.py | 15 +++++++++------ pygeoapi/flask_app.py | 2 +- pygeoapi/templates/api.html | 1 + tests/test_api.py | 4 ++-- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/pygeoapi/api.py b/pygeoapi/api.py index 714077a..29b5c94 100644 --- a/pygeoapi/api.py +++ b/pygeoapi/api.py @@ -145,7 +145,7 @@ class API(object): return headers_, 200, json.dumps(fcm) - def api(self, headers, args, request_path, openapi): + def api(self, headers, args, openapi): """ Provide OpenAPI document @@ -159,9 +159,11 @@ class API(object): headers_ = HEADERS.copy() format_ = check_format(args, headers) + path = '/'.join([self.config['server']['url'].rstrip('/'), 'api']) + if format_ == 'html': data = { - 'openapi-document-path': request_path + 'openapi-document-path': path } headers_['Content-Type'] = 'text/html' content = _render_j2_template(self.config, 'api.html', data) @@ -528,15 +530,16 @@ class API(object): content['links'][0]['rel'] = 'alternate' content['links'][1]['rel'] = 'self' - # For constructing proper URIs to Items - path_info = headers.environ['PATH_INFO'] - if path_info.endswith('/'): - path_info = path_info[:-1] + # For constructing proper URIs to items + path_info = '/'.join([ + self.config['server']['url'].rstrip('/'), + headers.environ['PATH_INFO'].strip('/')]) content['items_path'] = path_info content['dataset_path'] = '/'.join(path_info.split('/')[:-1]) content['collections_path'] = '/'.join(path_info.split('/')[:-2]) content['startindex'] = startindex + content = _render_j2_template(self.config, 'items.html', content) return headers_, 200, content diff --git a/pygeoapi/flask_app.py b/pygeoapi/flask_app.py index 809363a..aeb73d9 100644 --- a/pygeoapi/flask_app.py +++ b/pygeoapi/flask_app.py @@ -68,7 +68,7 @@ def api(): openapi = yaml.load(ff) headers, status_code, content = api_.api(request.headers, request.args, - request.path, openapi) + openapi) response = make_response(content, status_code) if headers: diff --git a/pygeoapi/templates/api.html b/pygeoapi/templates/api.html index a50fc06..f54ed40 100644 --- a/pygeoapi/templates/api.html +++ b/pygeoapi/templates/api.html @@ -1,6 +1,7 @@ + {{ config['metadata']['identification']['title'] }} OpenAPI Document