From 7c4b455162fbc2668eba42751c91fc2460113c9c Mon Sep 17 00:00:00 2001 From: abersheeran Date: Mon, 13 Jul 2020 13:28:16 +0800 Subject: [PATCH 1/3] fix wsgi PATH_INFO encoding --- starlette/middleware/wsgi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/starlette/middleware/wsgi.py b/starlette/middleware/wsgi.py index fbc8411bf..49dbba6bf 100644 --- a/starlette/middleware/wsgi.py +++ b/starlette/middleware/wsgi.py @@ -14,7 +14,7 @@ def build_environ(scope: Scope, body: bytes) -> dict: environ = { "REQUEST_METHOD": scope["method"], "SCRIPT_NAME": scope.get("root_path", ""), - "PATH_INFO": scope["path"], + "PATH_INFO": scope["path"].encode("utf8").decode("latin1"), "QUERY_STRING": scope["query_string"].decode("ascii"), "SERVER_PROTOCOL": f"HTTP/{scope['http_version']}", "wsgi.version": (1, 0), From 087a82ec7e2b6aedae245ce2845c96cf3b55aed3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=90=E4=BC=91?= Date: Tue, 14 Jul 2020 10:54:43 +0800 Subject: [PATCH 2/3] encode root_path --- starlette/middleware/wsgi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/starlette/middleware/wsgi.py b/starlette/middleware/wsgi.py index 49dbba6bf..8f7d92711 100644 --- a/starlette/middleware/wsgi.py +++ b/starlette/middleware/wsgi.py @@ -13,7 +13,7 @@ def build_environ(scope: Scope, body: bytes) -> dict: """ environ = { "REQUEST_METHOD": scope["method"], - "SCRIPT_NAME": scope.get("root_path", ""), + "SCRIPT_NAME": scope.get("root_path", "").encode("utf8").decode("latin1"), "PATH_INFO": scope["path"].encode("utf8").decode("latin1"), "QUERY_STRING": scope["query_string"].decode("ascii"), "SERVER_PROTOCOL": f"HTTP/{scope['http_version']}", From 4ed639d8ed7fdc490523f9ff6e939aa5f305c6d5 Mon Sep 17 00:00:00 2001 From: Jamie Hewland Date: Wed, 15 Jul 2020 20:13:47 +0200 Subject: [PATCH 3/3] wsgi middleware: Add test for PATH_INFO & SCRIPT_NAME encoding --- tests/middleware/test_wsgi.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/middleware/test_wsgi.py b/tests/middleware/test_wsgi.py index 202726097..c35fda426 100644 --- a/tests/middleware/test_wsgi.py +++ b/tests/middleware/test_wsgi.py @@ -128,3 +128,18 @@ def test_build_environ(): "wsgi.url_scheme": "https", "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")