From 0bca0fabaa17fa909d072f324347c5187b60a8b2 Mon Sep 17 00:00:00 2001 From: Shane McDonald Date: Sun, 14 Nov 2021 04:14:50 +0000 Subject: [PATCH] Fix bug in named url middleware when running at non-root path The most notable change here is the removal of the conditional in process_request. I don't know why we were preferring REQUEST_URI over PATH_INFO. When the app is running at /, they are always the same as far as I can tell. However, when using SCRIPT_NAME, this was incorrectly setting path and path_info to /myprefix/myprefix/. --- awx/api/serializers.py | 15 ++++++++++++++- awx/main/middleware.py | 6 +----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/awx/api/serializers.py b/awx/api/serializers.py index a14d22822a..69a47bb55d 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -391,7 +391,20 @@ class BaseSerializer(serializers.ModelSerializer, metaclass=BaseSerializerMetacl view = self.context.get('view', None) if view and (hasattr(view, 'retrieve') or view.request.method == 'POST') and type(obj) in settings.NAMED_URL_GRAPH: original_url = self.get_url(obj) - res['named_url'] = self._generate_named_url(original_url, obj, settings.NAMED_URL_GRAPH[type(obj)]) + + # If the app is running at a location other than /, temporarily remove the + # prefix. This is to avoid changing the code in _generate_named_url where + # it is assumed the ID of the resource is at url_units[4]. + url_prefix = view.request.META.get('SCRIPT_NAME', '') + if url_prefix: + original_url = removeprefix(original_url, url_prefix) + + named_url = self._generate_named_url(original_url, obj, settings.NAMED_URL_GRAPH[type(obj)]) + + if url_prefix: + named_url = url_prefix + named_url + + res['named_url'] = named_url if getattr(obj, 'created_by', None): res['created_by'] = self.reverse('api:user_detail', kwargs={'pk': obj.created_by.pk}) if getattr(obj, 'modified_by', None): diff --git a/awx/main/middleware.py b/awx/main/middleware.py index 9688ca1ff5..39caf4a7e7 100644 --- a/awx/main/middleware.py +++ b/awx/main/middleware.py @@ -180,11 +180,7 @@ class URLModificationMiddleware(MiddlewareMixin): return '/'.join(url_units) def process_request(self, request): - if hasattr(request, 'environ') and 'REQUEST_URI' in request.environ: - old_path = urllib.parse.urlsplit(request.environ['REQUEST_URI']).path - old_path = old_path[request.path.find(request.path_info) :] - else: - old_path = request.path_info + old_path = request.path_info new_path = self._convert_named_url(old_path) if request.path_info != new_path: request.environ['awx.named_url_rewritten'] = request.path