fix wsgi PATH_INFO encoding (#998)

* fix wsgi PATH_INFO encoding

* encode root_path

* wsgi middleware: Add test for PATH_INFO & SCRIPT_NAME encoding

Co-authored-by: Jamie Hewland <jhewland@gmail.com>
This commit is contained in:
子休 2020-07-16 02:27:17 +08:00 committed by GitHub
parent 14139c2886
commit 85b9c2642b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 2 deletions

View File

@ -13,8 +13,8 @@ def build_environ(scope: Scope, body: bytes) -> dict:
""" """
environ = { environ = {
"REQUEST_METHOD": scope["method"], "REQUEST_METHOD": scope["method"],
"SCRIPT_NAME": scope.get("root_path", ""), "SCRIPT_NAME": scope.get("root_path", "").encode("utf8").decode("latin1"),
"PATH_INFO": scope["path"], "PATH_INFO": scope["path"].encode("utf8").decode("latin1"),
"QUERY_STRING": scope["query_string"].decode("ascii"), "QUERY_STRING": scope["query_string"].decode("ascii"),
"SERVER_PROTOCOL": f"HTTP/{scope['http_version']}", "SERVER_PROTOCOL": f"HTTP/{scope['http_version']}",
"wsgi.version": (1, 0), "wsgi.version": (1, 0),

View File

@ -128,3 +128,18 @@ def test_build_environ():
"wsgi.url_scheme": "https", "wsgi.url_scheme": "https",
"wsgi.version": (1, 0), "wsgi.version": (1, 0),
} }
def test_build_environ_encoding() -> None:
scope = {
"type": "http",
"http_version": "1.1",
"method": "GET",
"path": "/小星",
"root_path": "/中国",
"query_string": b"a=123&b=456",
"headers": [],
}
environ = build_environ(scope, b"")
assert environ["SCRIPT_NAME"] == "/中国".encode("utf8").decode("latin-1")
assert environ["PATH_INFO"] == "/小星".encode("utf8").decode("latin-1")