From 5e3245abde955f231732aa79711f0e902539a858 Mon Sep 17 00:00:00 2001 From: Jiri Kozel Date: Thu, 21 Sep 2023 18:48:22 +0200 Subject: [PATCH] Rename x_forwarded_prefix to x_forwarded_items Not in tests --- src/layman/common/rest.py | 4 ++-- src/layman/geoserver_proxy.py | 4 ++-- src/layman/layer/__init__.py | 4 ++-- src/layman/layer/filesystem/thumbnail.py | 4 ++-- src/layman/layer/geoserver/sld.py | 4 ++-- src/layman/layer/geoserver/wfs.py | 12 +++++++----- src/layman/layer/geoserver/wms.py | 12 +++++++----- src/layman/layer/micka/csw.py | 4 ++-- src/layman/layer/qgis/wms.py | 4 ++-- src/layman/layer/rest_layers.py | 4 ++-- src/layman/layer/rest_workspace_layer.py | 12 ++++++------ src/layman/layer/rest_workspace_layers.py | 12 ++++++------ src/layman/layer/util.py | 6 +++--- src/layman/map/__init__.py | 4 ++-- src/layman/map/filesystem/input_file.py | 4 ++-- src/layman/map/filesystem/thumbnail.py | 4 ++-- src/layman/map/micka/csw.py | 4 ++-- src/layman/map/rest_maps.py | 4 ++-- src/layman/map/rest_workspace_map.py | 12 ++++++------ src/layman/map/rest_workspace_map_file.py | 4 ++-- src/layman/map/rest_workspace_maps.py | 12 ++++++------ src/layman/map/util.py | 13 +++++++------ src/layman/rest_publications.py | 4 ++-- src/layman/util.py | 21 +++++++++++---------- 24 files changed, 89 insertions(+), 83 deletions(-) diff --git a/src/layman/common/rest.py b/src/layman/common/rest.py index 962717bf5..ae02569b9 100644 --- a/src/layman/common/rest.py +++ b/src/layman/common/rest.py @@ -157,7 +157,7 @@ def get_integer_from_param(request_args, param_name, negative=True, zero=True, p return result -def get_publications(publication_type, actor, request_args=None, workspace=None, *, x_forwarded_prefix=None): +def get_publications(publication_type, actor, request_args=None, workspace=None, *, x_forwarded_items=None): request_args = request_args or {} known_order_by_values = [consts.ORDER_BY_TITLE, consts.ORDER_BY_FULL_TEXT, consts.ORDER_BY_LAST_CHANGE, consts.ORDER_BY_BBOX, ] @@ -251,7 +251,7 @@ def get_publications(publication_type, actor, request_args=None, workspace=None, 'workspace': res_workspace, 'publication_type': info['type'].split('.')[1], 'title': info.get("title"), - 'url': layman_util.get_workspace_publication_url(info['type'], res_workspace, name, x_forwarded_prefix=x_forwarded_prefix), + 'url': layman_util.get_workspace_publication_url(info['type'], res_workspace, name, x_forwarded_items=x_forwarded_items), 'uuid': info["uuid"], 'access_rights': info['access_rights'], 'updated_at': info['updated_at'].isoformat(), diff --git a/src/layman/geoserver_proxy.py b/src/layman/geoserver_proxy.py index 30110290a..bb3edb7c5 100644 --- a/src/layman/geoserver_proxy.py +++ b/src/layman/geoserver_proxy.py @@ -233,8 +233,8 @@ def proxy(subpath): 'Host', ]: headers_req.pop(header, None) - x_forwarded_prefix = layman_util.get_x_forwarded_items(request.headers) - headers_req['X-Forwarded-Path'] = x_forwarded_prefix or '' + x_forwarded_items = layman_util.get_x_forwarded_items(request.headers) + headers_req['X-Forwarded-Path'] = x_forwarded_items.prefix or '' # ensure layer attributes in case of WFS-T app.logger.info(f"{request.method} GeoServer proxy, headers_req={headers_req}, url={url}") diff --git a/src/layman/layer/__init__.py b/src/layman/layer/__init__.py index 528d59a23..77df94dc8 100644 --- a/src/layman/layer/__init__.py +++ b/src/layman/layer/__init__.py @@ -154,5 +154,5 @@ def get_layer_info_keys(*, geodata_type, original_data_source): NO_STYLE_DEF = STYLE_TYPES_DEF[0] -def get_workspace_publication_url(workspace, publication_name, *, x_forwarded_prefix=None): - return url_for('rest_workspace_layer.get', layername=publication_name, workspace=workspace, x_forwarded_prefix=x_forwarded_prefix) +def get_workspace_publication_url(workspace, publication_name, *, x_forwarded_items=None): + return url_for('rest_workspace_layer.get', layername=publication_name, workspace=workspace, x_forwarded_items=x_forwarded_items) diff --git a/src/layman/layer/filesystem/thumbnail.py b/src/layman/layer/filesystem/thumbnail.py index 7edc50dbd..5bbe13c5f 100644 --- a/src/layman/layer/filesystem/thumbnail.py +++ b/src/layman/layer/filesystem/thumbnail.py @@ -32,14 +32,14 @@ def ensure_layer_thumbnail_dir(workspace, layername): return thumbnail_dir -def get_layer_info(workspace, layername, *, x_forwarded_prefix=None): +def get_layer_info(workspace, layername, *, x_forwarded_items=None): thumbnail_path = get_layer_thumbnail_path(workspace, layername) if os.path.exists(thumbnail_path): return { 'thumbnail': { 'url': url_for('rest_workspace_layer_thumbnail.get', workspace=workspace, layername=layername, - x_forwarded_prefix=x_forwarded_prefix), + x_forwarded_items=x_forwarded_items), 'path': os.path.relpath(thumbnail_path, common_util.get_workspace_dir(workspace)) }, '_thumbnail': { diff --git a/src/layman/layer/geoserver/sld.py b/src/layman/layer/geoserver/sld.py index 36aa13ab9..7b383b2eb 100644 --- a/src/layman/layer/geoserver/sld.py +++ b/src/layman/layer/geoserver/sld.py @@ -41,10 +41,10 @@ def delete_layer(workspace, layername): return result -def get_layer_info(workspace, layername, *, x_forwarded_prefix=None): +def get_layer_info(workspace, layername, *, x_forwarded_items=None): response = get_style_response(workspace, layername, gs_util.headers_sld['1.0.0'], settings.LAYMAN_GS_AUTH) if response and response.status_code == 200: - url = url_for('rest_workspace_layer_style.get', workspace=workspace, layername=layername, x_forwarded_prefix=x_forwarded_prefix) + url = url_for('rest_workspace_layer_style.get', workspace=workspace, layername=layername, x_forwarded_items=x_forwarded_items) info = { 'style': { 'url': url, diff --git a/src/layman/layer/geoserver/wfs.py b/src/layman/layer/geoserver/wfs.py index c2f7751ce..538f931af 100644 --- a/src/layman/layer/geoserver/wfs.py +++ b/src/layman/layer/geoserver/wfs.py @@ -8,6 +8,7 @@ from layman.layer.util import is_layer_chain_ready from layman import util as layman_util from layman.layer import LAYER_TYPE +from layman.util import XForwardedClass import requests_util.retry from .util import get_gs_proxy_server_url, get_external_db_store_name from . import wms @@ -58,9 +59,10 @@ def delete_layer(workspace, layername): return {} -def get_wfs_url(workspace, external_url=False, *, x_forwarded_prefix=None): - assert external_url or not x_forwarded_prefix - x_forwarded_prefix = x_forwarded_prefix or '' +def get_wfs_url(workspace, external_url=False, *, x_forwarded_items=None): + x_forwarded_items = x_forwarded_items or XForwardedClass() + assert external_url or not x_forwarded_items + x_forwarded_prefix = x_forwarded_items.prefix or '' base_url = urljoin(get_gs_proxy_server_url(), x_forwarded_prefix) + settings.LAYMAN_GS_PATH if external_url else settings.LAYMAN_GS_URL return urljoin(base_url, workspace + '/wfs') @@ -125,11 +127,11 @@ def clear_cache(workspace): mem_redis.delete(key) -def get_layer_info(workspace, layername, *, x_forwarded_prefix=None): +def get_layer_info(workspace, layername, *, x_forwarded_items=None): wfs = get_wfs_proxy(workspace) if wfs is None: return {} - wfs_proxy_url = get_wfs_url(workspace, external_url=True, x_forwarded_prefix=x_forwarded_prefix) + wfs_proxy_url = get_wfs_url(workspace, external_url=True, x_forwarded_items=x_forwarded_items) wfs_layername = f"{workspace}:{layername}" if wfs_layername not in wfs.contents: diff --git a/src/layman/layer/geoserver/wms.py b/src/layman/layer/geoserver/wms.py index 99d3dc35a..1657afb5d 100644 --- a/src/layman/layer/geoserver/wms.py +++ b/src/layman/layer/geoserver/wms.py @@ -13,6 +13,7 @@ from layman.layer.util import is_layer_chain_ready from layman.layer import LAYER_TYPE from layman.layer.filesystem import gdal +from layman.util import XForwardedClass import requests_util.retry from .util import get_gs_proxy_server_url, get_external_db_store_name @@ -82,9 +83,10 @@ def delete_layer(workspace, layername): return {} -def get_wms_url(workspace, external_url=False, *, x_forwarded_prefix=None): - assert external_url or not x_forwarded_prefix - x_forwarded_prefix = x_forwarded_prefix or '' +def get_wms_url(workspace, external_url=False, *, x_forwarded_items=None): + x_forwarded_items = x_forwarded_items or XForwardedClass() + assert external_url or not x_forwarded_items + x_forwarded_prefix = x_forwarded_items.prefix or '' geoserver_workspace = get_geoserver_workspace(workspace) base_url = urljoin(get_gs_proxy_server_url(), x_forwarded_prefix) + settings.LAYMAN_GS_PATH if external_url else settings.LAYMAN_GS_URL return urljoin(base_url, geoserver_workspace + '/ows') @@ -170,11 +172,11 @@ def get_timeregex_props(workspace, layername): return result -def get_layer_info(workspace, layername, *, x_forwarded_prefix=None): +def get_layer_info(workspace, layername, *, x_forwarded_items=None): wms = get_wms_proxy(workspace) if wms is None: return {} - wms_proxy_url = get_wms_url(workspace, external_url=True, x_forwarded_prefix=x_forwarded_prefix) + wms_proxy_url = get_wms_url(workspace, external_url=True, x_forwarded_items=x_forwarded_items) if layername not in wms.contents: return {} diff --git a/src/layman/layer/micka/csw.py b/src/layman/layer/micka/csw.py index 5d1c07e3f..e25ad9551 100644 --- a/src/layman/layer/micka/csw.py +++ b/src/layman/layer/micka/csw.py @@ -28,7 +28,7 @@ post_layer = empty_method -def get_layer_info(workspace, layername, *, x_forwarded_prefix=None): +def get_layer_info(workspace, layername, *, x_forwarded_items=None): uuid = get_layer_uuid(workspace, layername) try: csw = common_util.create_csw() @@ -51,7 +51,7 @@ def get_layer_info(workspace, layername, *, x_forwarded_prefix=None): 'csw_url': settings.CSW_PROXY_URL, 'record_url': common_util.get_metadata_url(uuid, url_type=common_util.RecordUrlType.BASIC), 'comparison_url': url_for('rest_workspace_layer_metadata_comparison.get', workspace=workspace, layername=layername, - x_forwarded_prefix=x_forwarded_prefix), + x_forwarded_items=x_forwarded_items), } } return {} diff --git a/src/layman/layer/qgis/wms.py b/src/layman/layer/qgis/wms.py index cce75d718..56422c101 100644 --- a/src/layman/layer/qgis/wms.py +++ b/src/layman/layer/qgis/wms.py @@ -18,11 +18,11 @@ get_publication_uuid = empty_method_returns_none -def get_layer_info(workspace, layername, *, x_forwarded_prefix=None): +def get_layer_info(workspace, layername, *, x_forwarded_items=None): input_file_dir = qgis.get_layer_dir(workspace, layername) result = {} if os.path.exists(input_file_dir): - url = layman_util.url_for('rest_workspace_layer_style.get', workspace=workspace, layername=layername, x_forwarded_prefix=x_forwarded_prefix) + url = layman_util.url_for('rest_workspace_layer_style.get', workspace=workspace, layername=layername, x_forwarded_items=x_forwarded_items) result = { 'name': layername, 'style': { diff --git a/src/layman/layer/rest_layers.py b/src/layman/layer/rest_layers.py index ff37e9f33..fa2a2e0e7 100644 --- a/src/layman/layer/rest_layers.py +++ b/src/layman/layer/rest_layers.py @@ -21,5 +21,5 @@ def get(): app.logger.info(f"GET Layers, actor={g.user}") actor = get_authn_username() - x_forwarded_prefix = layman_util.get_x_forwarded_items(request.headers) - return rest_common.get_publications(LAYER_TYPE, actor, request_args=request.args, x_forwarded_prefix=x_forwarded_prefix) + x_forwarded_items = layman_util.get_x_forwarded_items(request.headers) + return rest_common.get_publications(LAYER_TYPE, actor, request_args=request.args, x_forwarded_items=x_forwarded_items) diff --git a/src/layman/layer/rest_workspace_layer.py b/src/layman/layer/rest_workspace_layer.py index b3c0bafe5..2fd9965d7 100644 --- a/src/layman/layer/rest_workspace_layer.py +++ b/src/layman/layer/rest_workspace_layer.py @@ -37,8 +37,8 @@ def get(workspace, layername): # pylint: disable=unused-argument app.logger.info(f"GET Layer, actor={g.user}") - x_forwarded_prefix = layman_util.get_x_forwarded_items(request.headers) - info = util.get_complete_layer_info(workspace, layername, x_forwarded_prefix=x_forwarded_prefix) + x_forwarded_items = layman_util.get_x_forwarded_items(request.headers) + info = util.get_complete_layer_info(workspace, layername, x_forwarded_items=x_forwarded_items) return jsonify(info), 200 @@ -48,7 +48,7 @@ def get(workspace, layername): def patch(workspace, layername): app.logger.info(f"PATCH Layer, actor={g.user}") - x_forwarded_prefix = layman_util.get_x_forwarded_items(request.headers) + x_forwarded_items = layman_util.get_x_forwarded_items(request.headers) info = layman_util.get_publication_info(workspace, LAYER_TYPE, layername, context={'keys': ['title', 'name', 'description', 'table_uri', 'geodata_type', 'style_type', @@ -262,7 +262,7 @@ def patch(workspace, layername): ) app.logger.info('PATCH Layer changes done') - info = util.get_complete_layer_info(workspace, layername, x_forwarded_prefix=x_forwarded_prefix) + info = util.get_complete_layer_info(workspace, layername, x_forwarded_items=x_forwarded_items) info.update(layer_result) return jsonify(info), 200 @@ -272,9 +272,9 @@ def patch(workspace, layername): @util.lock_decorator def delete_layer(workspace, layername): app.logger.info(f"DELETE Layer, actor={g.user}") - x_forwarded_prefix = layman_util.get_x_forwarded_items(request.headers) + x_forwarded_items = layman_util.get_x_forwarded_items(request.headers) - info = util.get_complete_layer_info(workspace, layername, x_forwarded_prefix=x_forwarded_prefix) + info = util.get_complete_layer_info(workspace, layername, x_forwarded_items=x_forwarded_items) util.abort_layer_chain(workspace, layername) diff --git a/src/layman/layer/rest_workspace_layers.py b/src/layman/layer/rest_workspace_layers.py index ed92b13eb..19a0d3a7b 100644 --- a/src/layman/layer/rest_workspace_layers.py +++ b/src/layman/layer/rest_workspace_layers.py @@ -32,15 +32,15 @@ def get(workspace): app.logger.info(f"GET Layers, actor={g.user}") actor = get_authn_username() - x_forwarded_prefix = layman_util.get_x_forwarded_items(request.headers) - return rest_common.get_publications(LAYER_TYPE, actor, request_args=request.args, workspace=workspace, x_forwarded_prefix=x_forwarded_prefix) + x_forwarded_items = layman_util.get_x_forwarded_items(request.headers) + return rest_common.get_publications(LAYER_TYPE, actor, request_args=request.args, workspace=workspace, x_forwarded_items=x_forwarded_items) @bp.route(f"/{LAYER_REST_PATH_NAME}", methods=['POST']) def post(workspace): app.logger.info(f"POST Layers, actor={g.user}") - x_forwarded_prefix = layman_util.get_x_forwarded_items(request.headers) + x_forwarded_items = layman_util.get_x_forwarded_items(request.headers) # FILE sent_file_streams = [] @@ -195,7 +195,7 @@ def post(workspace): task_options, ) - layerurl = url_for('rest_workspace_layer.get', layername=layername, workspace=workspace, x_forwarded_prefix=x_forwarded_prefix) + layerurl = url_for('rest_workspace_layer.get', layername=layername, workspace=workspace, x_forwarded_items=x_forwarded_items) layer_result = { 'name': layername, @@ -253,7 +253,7 @@ def post(workspace): def delete(workspace): app.logger.info(f"DELETE Layers, actor={g.user}") - x_forwarded_prefix = layman_util.get_x_forwarded_items(request.headers) + x_forwarded_items = layman_util.get_x_forwarded_items(request.headers) infos = layman_util.delete_publications(workspace, LAYER_TYPE, util.is_layer_chain_ready, @@ -262,6 +262,6 @@ def delete(workspace): request.method, 'rest_workspace_layer.get', 'layername', - x_forwarded_prefix=x_forwarded_prefix, + x_forwarded_items=x_forwarded_items, ) return infos, 200 diff --git a/src/layman/layer/util.py b/src/layman/layer/util.py index d20bda218..6f16cfb85 100644 --- a/src/layman/layer/util.py +++ b/src/layman/layer/util.py @@ -84,8 +84,8 @@ def clear_publication_info(layer_info, file_type): return clear_info -def get_complete_layer_info(workspace, layername, *, x_forwarded_prefix=None): - partial_info = get_layer_info(workspace, layername, context={'x_forwarded_prefix': x_forwarded_prefix}) +def get_complete_layer_info(workspace, layername, *, x_forwarded_items=None): + partial_info = get_layer_info(workspace, layername, context={'x_forwarded_items': x_forwarded_items}) if not any(partial_info): raise LaymanError(15, {'layername': layername}) @@ -100,7 +100,7 @@ def get_complete_layer_info(workspace, layername, *, x_forwarded_prefix=None): complete_info.update({ 'name': layername, - 'url': url_for('rest_workspace_layer.get', layername=layername, workspace=workspace, x_forwarded_prefix=x_forwarded_prefix), + 'url': url_for('rest_workspace_layer.get', layername=layername, workspace=workspace, x_forwarded_items=x_forwarded_items), 'title': layername, 'description': '', }) diff --git a/src/layman/map/__init__.py b/src/layman/map/__init__.py index 6e2f228d2..ac195cf95 100644 --- a/src/layman/map/__init__.py +++ b/src/layman/map/__init__.py @@ -85,5 +85,5 @@ def get_map_info_keys(): } -def get_workspace_publication_url(workspace, publication_name, *, x_forwarded_prefix=None): - return url_for('rest_workspace_map.get', mapname=publication_name, workspace=workspace, x_forwarded_prefix=x_forwarded_prefix) +def get_workspace_publication_url(workspace, publication_name, *, x_forwarded_items=None): + return url_for('rest_workspace_map.get', mapname=publication_name, workspace=workspace, x_forwarded_items=x_forwarded_items) diff --git a/src/layman/map/filesystem/input_file.py b/src/layman/map/filesystem/input_file.py index 8e75d5dd2..36426ddbe 100644 --- a/src/layman/map/filesystem/input_file.py +++ b/src/layman/map/filesystem/input_file.py @@ -36,7 +36,7 @@ def get_map_file(workspace, mapname): return mapfile_path -def get_map_info(workspace, mapname, *, x_forwarded_prefix=None): +def get_map_info(workspace, mapname, *, x_forwarded_items=None): map_file_path = get_map_file(workspace, mapname) result = {} if os.path.exists(map_file_path): @@ -46,7 +46,7 @@ def get_map_info(workspace, mapname, *, x_forwarded_prefix=None): result = { 'file': { 'path': map_file_path, - 'url': url_for('rest_workspace_map_file.get', mapname=mapname, workspace=workspace, x_forwarded_prefix=x_forwarded_prefix), + 'url': url_for('rest_workspace_map_file.get', mapname=mapname, workspace=workspace, x_forwarded_items=x_forwarded_items), }, '_file': { 'url': url_for('rest_workspace_map_file.get', mapname=mapname, workspace=workspace, internal=True), diff --git a/src/layman/map/filesystem/thumbnail.py b/src/layman/map/filesystem/thumbnail.py index af57ae44a..e773b9dc0 100644 --- a/src/layman/map/filesystem/thumbnail.py +++ b/src/layman/map/filesystem/thumbnail.py @@ -35,13 +35,13 @@ def ensure_map_thumbnail_dir(workspace, mapname): return thumbnail_dir -def get_map_info(workspace, mapname, *, x_forwarded_prefix=None): +def get_map_info(workspace, mapname, *, x_forwarded_items=None): thumbnail_path = get_map_thumbnail_path(workspace, mapname) if os.path.exists(thumbnail_path): return { 'thumbnail': { 'url': url_for('rest_workspace_map_thumbnail.get', workspace=workspace, - mapname=mapname, x_forwarded_prefix=x_forwarded_prefix), + mapname=mapname, x_forwarded_items=x_forwarded_items), 'path': os.path.relpath(thumbnail_path, common_util.get_workspace_dir(workspace)) }, '_thumbnail': { diff --git a/src/layman/map/micka/csw.py b/src/layman/map/micka/csw.py index 774f80411..d4519ce7c 100644 --- a/src/layman/map/micka/csw.py +++ b/src/layman/map/micka/csw.py @@ -22,7 +22,7 @@ post_map = empty_method -def get_map_info(workspace, mapname, *, x_forwarded_prefix=None): +def get_map_info(workspace, mapname, *, x_forwarded_items=None): uuid = get_map_uuid(workspace, mapname) try: csw = common_util.create_csw() @@ -45,7 +45,7 @@ def get_map_info(workspace, mapname, *, x_forwarded_prefix=None): 'csw_url': settings.CSW_PROXY_URL, 'record_url': common_util.get_metadata_url(uuid, url_type=common_util.RecordUrlType.BASIC), 'comparison_url': url_for('rest_workspace_map_metadata_comparison.get', workspace=workspace, mapname=mapname, - x_forwarded_prefix=x_forwarded_prefix), + x_forwarded_items=x_forwarded_items), } } return {} diff --git a/src/layman/map/rest_maps.py b/src/layman/map/rest_maps.py index cb9df3ee5..36dbf8b1c 100644 --- a/src/layman/map/rest_maps.py +++ b/src/layman/map/rest_maps.py @@ -21,5 +21,5 @@ def get(): app.logger.info(f"GET Maps, actor={g.user}") actor = get_authn_username() - x_forwarded_prefix = layman_util.get_x_forwarded_items(request.headers) - return rest_common.get_publications(MAP_TYPE, actor, request_args=request.args, x_forwarded_prefix=x_forwarded_prefix) + x_forwarded_items = layman_util.get_x_forwarded_items(request.headers) + return rest_common.get_publications(MAP_TYPE, actor, request_args=request.args, x_forwarded_items=x_forwarded_items) diff --git a/src/layman/map/rest_workspace_map.py b/src/layman/map/rest_workspace_map.py index 371dd8932..aa81c04a5 100644 --- a/src/layman/map/rest_workspace_map.py +++ b/src/layman/map/rest_workspace_map.py @@ -35,8 +35,8 @@ def get(workspace, mapname): # pylint: disable=unused-argument app.logger.info(f"GET Map, actor={g.user}") - x_forwarded_prefix = layman_util.get_x_forwarded_items(request.headers) - info = util.get_complete_map_info(workspace, mapname, x_forwarded_prefix=x_forwarded_prefix) + x_forwarded_items = layman_util.get_x_forwarded_items(request.headers) + info = util.get_complete_map_info(workspace, mapname, x_forwarded_items=x_forwarded_items) return jsonify(info), 200 @@ -46,7 +46,7 @@ def get(workspace, mapname): def patch(workspace, mapname): app.logger.info(f"PATCH Map, actor={g.user}") - x_forwarded_prefix = layman_util.get_x_forwarded_items(request.headers) + x_forwarded_items = layman_util.get_x_forwarded_items(request.headers) info = util.get_complete_map_info(workspace, mapname) # FILE @@ -107,7 +107,7 @@ def patch(workspace, mapname): 'layman.map.filesystem.input_file' if file_changed else None ) - info = util.get_complete_map_info(workspace, mapname, x_forwarded_prefix=x_forwarded_prefix) + info = util.get_complete_map_info(workspace, mapname, x_forwarded_items=x_forwarded_items) return jsonify(info), 200 @@ -116,10 +116,10 @@ def patch(workspace, mapname): @util.lock_decorator def delete_map(workspace, mapname): app.logger.info(f"DELETE Map, actor={g.user}") - x_forwarded_prefix = layman_util.get_x_forwarded_items(request.headers) + x_forwarded_items = layman_util.get_x_forwarded_items(request.headers) # raise exception if map does not exist - info = util.get_complete_map_info(workspace, mapname, x_forwarded_prefix=x_forwarded_prefix) + info = util.get_complete_map_info(workspace, mapname, x_forwarded_items=x_forwarded_items) util.abort_map_chain(workspace, mapname) diff --git a/src/layman/map/rest_workspace_map_file.py b/src/layman/map/rest_workspace_map_file.py index 23e28fce4..1d19895fb 100644 --- a/src/layman/map/rest_workspace_map_file.py +++ b/src/layman/map/rest_workspace_map_file.py @@ -28,9 +28,9 @@ def after_request(response): def get(workspace, mapname): app.logger.info(f"GET Map File, actor={g.user}") - x_forwarded_prefix = layman_util.get_x_forwarded_items(request.headers) + x_forwarded_items = layman_util.get_x_forwarded_items(request.headers) - map_json = util.get_map_file_json(workspace, mapname, x_forwarded_prefix=x_forwarded_prefix) + map_json = util.get_map_file_json(workspace, mapname, x_forwarded_items=x_forwarded_items) if map_json is not None: return jsonify(map_json), 200 diff --git a/src/layman/map/rest_workspace_maps.py b/src/layman/map/rest_workspace_maps.py index c44d39384..631009cc7 100644 --- a/src/layman/map/rest_workspace_maps.py +++ b/src/layman/map/rest_workspace_maps.py @@ -35,14 +35,14 @@ def get(workspace): app.logger.info(f"GET Maps, actor={g.user}") actor = get_authn_username() - x_forwarded_prefix = layman_util.get_x_forwarded_items(request.headers) - return rest_common.get_publications(MAP_TYPE, actor, request_args=request.args, workspace=workspace, x_forwarded_prefix=x_forwarded_prefix) + x_forwarded_items = layman_util.get_x_forwarded_items(request.headers) + return rest_common.get_publications(MAP_TYPE, actor, request_args=request.args, workspace=workspace, x_forwarded_items=x_forwarded_items) @bp.route(f"/{MAP_REST_PATH_NAME}", methods=['POST']) def post(workspace): app.logger.info(f"POST Maps, actor={g.user}") - x_forwarded_prefix = layman_util.get_x_forwarded_items(request.headers) + x_forwarded_items = layman_util.get_x_forwarded_items(request.headers) # FILE if 'file' in request.files and not request.files['file'].filename == '': @@ -75,7 +75,7 @@ def post(workspace): else: description = file_json.get('abstract', '') - mapurl = url_for('rest_workspace_map.get', mapname=mapname, workspace=workspace, x_forwarded_prefix=x_forwarded_prefix) + mapurl = url_for('rest_workspace_map.get', mapname=mapname, workspace=workspace, x_forwarded_items=x_forwarded_items) redis_util.create_lock(workspace, MAP_TYPE, mapname, request.method) @@ -135,7 +135,7 @@ def post(workspace): def delete(workspace): app.logger.info(f"DELETE Maps, actor={g.user}") - x_forwarded_prefix = layman_util.get_x_forwarded_items(request.headers) + x_forwarded_items = layman_util.get_x_forwarded_items(request.headers) infos = layman_util.delete_publications(workspace, MAP_TYPE, util.is_map_chain_ready, @@ -144,7 +144,7 @@ def delete(workspace): request.method, 'rest_workspace_map.get', 'mapname', - x_forwarded_prefix=x_forwarded_prefix, + x_forwarded_items=x_forwarded_items, ) return infos, 200 diff --git a/src/layman/map/util.py b/src/layman/map/util.py index 5752752d9..d8ebb94cf 100644 --- a/src/layman/map/util.py +++ b/src/layman/map/util.py @@ -17,7 +17,7 @@ from layman.layer.geoserver.util import get_gs_proxy_server_url from layman.layer.geoserver.wms import get_layman_workspace from layman.util import call_modules_fn, get_providers_from_source_names, get_internal_sources, \ - to_safe_name, url_for, WORKSPACE_NAME_PATTERN + to_safe_name, url_for, WORKSPACE_NAME_PATTERN, XForwardedClass from . import get_map_sources, MAP_TYPE, get_map_type_def, get_map_info_keys from .filesystem import input_file from .micka import csw @@ -135,8 +135,8 @@ def clear_publication_info(layer_info): return clear_info -def get_complete_map_info(workspace, mapname, *, x_forwarded_prefix=None): - partial_info = get_map_info(workspace, mapname, context={'x_forwarded_prefix': x_forwarded_prefix}) +def get_complete_map_info(workspace, mapname, *, x_forwarded_items=None): + partial_info = get_map_info(workspace, mapname, context={'x_forwarded_items': x_forwarded_items}) if not any(partial_info): raise LaymanError(26, {'mapname': mapname}) @@ -145,7 +145,7 @@ def get_complete_map_info(workspace, mapname, *, x_forwarded_prefix=None): complete_info = { 'name': mapname, - 'url': url_for('rest_workspace_map.get', mapname=mapname, workspace=workspace, x_forwarded_prefix=x_forwarded_prefix), + 'url': url_for('rest_workspace_map.get', mapname=mapname, workspace=workspace, x_forwarded_items=x_forwarded_items), 'title': mapname, 'description': '', } @@ -365,8 +365,9 @@ def _adjust_url(*, url_obj=None, url_key=None, url_list=None, url_idx=None, prox return found_original_base_url -def get_map_file_json(workspace, mapname, *, adjust_urls=True, x_forwarded_prefix=None): - x_forwarded_prefix = x_forwarded_prefix or '' +def get_map_file_json(workspace, mapname, *, adjust_urls=True, x_forwarded_items=None): + x_forwarded_items = x_forwarded_items or XForwardedClass() + x_forwarded_prefix = x_forwarded_items.prefix or '' map_json = input_file.get_map_json(workspace, mapname) if adjust_urls: diff --git a/src/layman/rest_publications.py b/src/layman/rest_publications.py index 8aeed365d..58b3b0cdb 100644 --- a/src/layman/rest_publications.py +++ b/src/layman/rest_publications.py @@ -20,5 +20,5 @@ def get(): app.logger.info(f"GET Publications, actor={g.user}") actor = get_authn_username() - x_forwarded_prefix = util.get_x_forwarded_items(request.headers) - return rest_common.get_publications(publication_type=None, actor=actor, request_args=request.args, x_forwarded_prefix=x_forwarded_prefix) + x_forwarded_items = util.get_x_forwarded_items(request.headers) + return rest_common.get_publications(publication_type=None, actor=actor, request_args=request.args, x_forwarded_items=x_forwarded_items) diff --git a/src/layman/util.py b/src/layman/util.py index cabb84285..c625660e3 100644 --- a/src/layman/util.py +++ b/src/layman/util.py @@ -253,9 +253,9 @@ def get_publication_module(publication_type, use_cache=True): return module -def get_workspace_publication_url(publication_type, workspace, publication_name, use_cache=True, *, x_forwarded_prefix=None): +def get_workspace_publication_url(publication_type, workspace, publication_name, use_cache=True, *, x_forwarded_items=None): publ_module = get_publication_module(publication_type, use_cache=use_cache) - return publ_module.get_workspace_publication_url(workspace, publication_name, x_forwarded_prefix=x_forwarded_prefix) + return publ_module.get_workspace_publication_url(workspace, publication_name, x_forwarded_items=x_forwarded_items) def get_providers_from_source_names(source_names, skip_modules=None): @@ -308,10 +308,11 @@ def call_modules_fn(modules, fn_name, args=None, kwargs=None, omit_duplicate_cal DUMB_MAP_ADAPTER_DICT = {} -def _url_for(endpoint, *, server_name, proxy_server_name, internal=False, x_forwarded_prefix=None, **values): +def _url_for(endpoint, *, server_name, proxy_server_name, internal=False, x_forwarded_items=None, **values): + x_forwarded_items = x_forwarded_items or XForwardedClass() assert not (internal and values.get('_external')) - assert not (internal and x_forwarded_prefix) - x_forwarded_prefix = x_forwarded_prefix or '' + assert not (internal and x_forwarded_items) + x_forwarded_prefix = x_forwarded_items.prefix or '' # It seems SERVER_NAME is not None only in some tests. It also seems TESTING is True only in the same tests. assert (current_app.config.get('SERVER_NAME', None) is not None) == (current_app.config['TESTING'] is True) # Flask does not accept SERVER_NAME without dot, and without SERVER_NAME url_for cannot be used @@ -332,10 +333,10 @@ def _url_for(endpoint, *, server_name, proxy_server_name, internal=False, x_forw return result -def url_for(endpoint, *, internal=False, x_forwarded_prefix=None, **values): +def url_for(endpoint, *, internal=False, x_forwarded_items=None, **values): return _url_for(endpoint, server_name=settings.LAYMAN_SERVER_NAME, proxy_server_name=settings.LAYMAN_PROXY_SERVER_NAME, - internal=internal, x_forwarded_prefix=x_forwarded_prefix, **values) + internal=internal, x_forwarded_items=x_forwarded_items, **values) def get_internal_sources(publ_type): @@ -386,7 +387,7 @@ def get_publication_info(workspace, publ_type, publ_name, context=None): }[publ_type] partial_infos = call_modules_fn(sources, info_method, [workspace, publ_name], kwargs={ 'extra_keys': context.get('extra_keys', []), - 'x_forwarded_prefix': context.get('x_forwarded_prefix'), + 'x_forwarded_items': context.get('x_forwarded_items'), }) result = {} @@ -456,7 +457,7 @@ def delete_publications(workspace, method, url_path, publ_param, - x_forwarded_prefix=None, + x_forwarded_items=None, ): from layman import authn actor_name = authn.get_authn_username() @@ -481,7 +482,7 @@ def delete_publications(workspace, { 'name': info["name"], 'title': info.get("title", None), - 'url': url_for(**{'endpoint': url_path, publ_param: publication[2], 'workspace': publication[0], 'x_forwarded_prefix': x_forwarded_prefix}), + 'url': url_for(**{'endpoint': url_path, publ_param: publication[2], 'workspace': publication[0], 'x_forwarded_items': x_forwarded_items}), 'uuid': info["uuid"], 'access_rights': info['access_rights'], }