Re: [Django] #34394: ASGIRequest doesn't respect settings.FORCE_SCRIPT_NAME. (was: django.core.handlers.asgi.ASGIRequest.script_name not work with settings.FORCE_SCRIPT_NAME)

2 views
Skip to first unread message

Django

unread,
Mar 9, 2023, 12:29:01 AM3/9/23
to django-...@googlegroups.com
#34394: ASGIRequest doesn't respect settings.FORCE_SCRIPT_NAME.
----------------------------------+------------------------------------
Reporter: julyzergcn | Owner: nobody
Type: Bug | Status: new
Component: HTTP handling | Version: 4.1
Severity: Normal | Resolution:
Keywords: asgi SCRIPT_NAME | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+------------------------------------
Changes (by Mariusz Felisiak):

* cc: Carlton Gibson, Andrew Godwin (added)
* component: Core (URLs) => HTTP handling
* stage: Unreviewed => Accepted


Comment:

Thanks for the report. It seems that `ASGIRequest` should take
`FORCE_SCRIPT_NAME` into account (as `WSGIRequest`), e.g.
{{{#!diff
diff --git a/django/core/handlers/asgi.py b/django/core/handlers/asgi.py
index 569157b277..c5eb87c712 100644
--- a/django/core/handlers/asgi.py
+++ b/django/core/handlers/asgi.py
@@ -40,7 +40,7 @@ class ASGIRequest(HttpRequest):
self._post_parse_error = False
self._read_started = False
self.resolver_match = None
- self.script_name = self.scope.get("root_path", "")
+ self.script_name = get_script_prefix(scope)
if self.script_name:
# TODO: Better is-prefix checking, slash handling?
self.path_info = scope["path"].removeprefix(self.script_name)
@@ -169,7 +169,7 @@ class ASGIHandler(base.BaseHandler):
except RequestAborted:
return
# Request is complete and can be served.
- set_script_prefix(self.get_script_prefix(scope))
+ set_script_prefix(get_script_prefix(scope))
await signals.request_started.asend(sender=self.__class__,
scope=scope)
# Get the request and check for basic issues.
request, error_response = self.create_request(scope, body_file)
@@ -310,10 +310,11 @@ class ASGIHandler(base.BaseHandler):
)
position += cls.chunk_size

- def get_script_prefix(self, scope):
- """
- Return the script prefix to use from either the scope or a
setting.
- """
- if settings.FORCE_SCRIPT_NAME:
- return settings.FORCE_SCRIPT_NAME
- return scope.get("root_path", "") or ""
+
+def get_script_prefix(scope):
+ """
+ Return the script prefix to use from either the scope or a setting.
+ """
+ if settings.FORCE_SCRIPT_NAME:
+ return settings.FORCE_SCRIPT_NAME
+ return scope.get("root_path", "") or ""
}}}

Would you like to prepare a patch via GitHub PR? (a regression test is
required.)

--
Ticket URL: <https://code.djangoproject.com/ticket/34394#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Reply all
Reply to author
Forward
0 new messages