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/.
This commit is contained in:
Shane McDonald 2021-11-14 04:14:50 +00:00
parent 93ac3fea43
commit 0bca0fabaa
2 changed files with 15 additions and 6 deletions

View File

@ -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):

View File

@ -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