Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
abersheeran committed Dec 6, 2023
1 parent eaae69f commit 9beb136
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
7 changes: 5 additions & 2 deletions a2wsgi/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,19 @@ def build_scope(environ: Environ) -> Scope:
else:
client = None

root_path = environ.get("SCRIPT_NAME", "").encode("latin1").decode("utf8")
path = root_path + environ["PATH_INFO"].encode("latin1").decode("utf8")

return {
"wsgi_environ": environ,
"type": "http",
"asgi": {"version": "3.0", "spec_version": "3.0"},
"http_version": environ.get("SERVER_PROTOCOL", "http/1.0").split("/")[1],
"method": environ["REQUEST_METHOD"],
"scheme": environ.get("wsgi.url_scheme", "http"),
"path": environ["PATH_INFO"].encode("latin1").decode("utf8"),
"path": path,
"query_string": environ["QUERY_STRING"].encode("ascii"),
"root_path": environ.get("SCRIPT_NAME", "").encode("latin1").decode("utf8"),
"root_path": root_path,
"client": client,
"server": (environ["SERVER_NAME"], int(environ["SERVER_PORT"])),
"headers": headers,
Expand Down
19 changes: 11 additions & 8 deletions a2wsgi/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,21 @@ def build_environ(scope: Scope, body: Body) -> Environ:
"""
Builds a scope and request body into a WSGI environ object.
"""
allow_rewrite_environ = {
"SCRIPT_NAME": scope.get("root_path", "").encode("utf8").decode("latin1"),
}
for key in allow_rewrite_environ.keys():
environ_var = os.environ.get(key, "")
if environ_var:
allow_rewrite_environ[key] = unicode_to_wsgi(environ_var)
script_name = scope.get("root_path", "").encode("utf8").decode("latin1")
path_info = scope["path"].encode("utf8").decode("latin1")
if path_info.startswith(script_name):
path_info = path_info[len(script_name):]

script_name_environ_var = os.environ.get("SCRIPT_NAME", "")
if script_name_environ_var:
script_name = unicode_to_wsgi(script_name_environ_var)

environ = {
**allow_rewrite_environ,
"asgi.scope": scope,
"REQUEST_METHOD": scope["method"],
"PATH_INFO": scope["path"].encode("utf8").decode("latin1"),
"SCRIPT_NAME": script_name,
"PATH_INFO": path_info,
"QUERY_STRING": scope["query_string"].decode("ascii"),
"SERVER_PROTOCOL": f"HTTP/{scope['http_version']}",
"wsgi.version": (1, 0),
Expand Down

0 comments on commit 9beb136

Please sign in to comment.