Skip to content

Commit

Permalink
Fix wsgi middleware PATH_INFO encoding (#962)
Browse files Browse the repository at this point in the history
* Fix wsgi middleware PATH_INFO encoding

* Add tests for wsgi PATH_INFO encoding
  • Loading branch information
CoolSpring8 authored Feb 20, 2021
1 parent 9d51e1c commit 2b1a67f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
16 changes: 15 additions & 1 deletion tests/middleware/test_wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import httpx
import pytest

from uvicorn.middleware.wsgi import WSGIMiddleware
from uvicorn.middleware.wsgi import WSGIMiddleware, build_environ


def hello_world(environ, start_response):
Expand Down Expand Up @@ -94,3 +94,17 @@ async def test_wsgi_exc_info():
response = await client.get("/")
assert response.status_code == 500
assert response.text == "Internal Server Error"


def test_build_environ_encoding():
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"", b"")
assert environ["PATH_INFO"] == "/文".encode("utf8").decode("latin-1")
2 changes: 1 addition & 1 deletion uvicorn/middleware/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def build_environ(scope, message, body):
environ = {
"REQUEST_METHOD": scope["method"],
"SCRIPT_NAME": "",
"PATH_INFO": scope["path"],
"PATH_INFO": scope["path"].encode("utf8").decode("latin1"),
"QUERY_STRING": scope["query_string"].decode("ascii"),
"SERVER_PROTOCOL": "HTTP/%s" % scope["http_version"],
"wsgi.version": (1, 0),
Expand Down

0 comments on commit 2b1a67f

Please sign in to comment.