From 5a9360bab7acd9c9299feba7d18ddd33ac161c65 Mon Sep 17 00:00:00 2001 From: wangbill <12449837+YunchuWang@users.noreply.github.com> Date: Fri, 12 Apr 2024 08:30:56 -0700 Subject: [PATCH] BugFix: Asgi Path Param Encoding change to utf-8 (#208) * fix: third party web app, changing request path encoding to utf-8 * fix * style fix * ignore * fix * add tests * fix * Update tests/test_http_wsgi.py Co-authored-by: Fredrik Erlandsson * Update tests/test_http_wsgi.py Co-authored-by: Fredrik Erlandsson * Update test_http_wsgi.py --------- Co-authored-by: Fredrik Erlandsson --- azure/functions/_http_wsgi.py | 7 +++++-- tests/test_http_wsgi.py | 13 +++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/azure/functions/_http_wsgi.py b/azure/functions/_http_wsgi.py index 6df5d16..48d6473 100644 --- a/azure/functions/_http_wsgi.py +++ b/azure/functions/_http_wsgi.py @@ -29,8 +29,11 @@ class WsgiRequest: # Implement interfaces for PEP 3333 environ self.request_method = getattr(func_req, 'method', None) self.script_name = '' - self.path_info = unquote_to_bytes( - getattr(url, 'path', None)).decode('latin-1') # type: ignore + self.path_info = ( + unquote_to_bytes(getattr(url, 'path', None)) # type: ignore + .decode('latin-1' if type(self) is WsgiRequest else 'utf-8') + ) + self.query_string = getattr(url, 'query', None) self.content_type = self._lowercased_headers.get('content-type') self.content_length = str(len(func_req_body)) diff --git a/tests/test_http_wsgi.py b/tests/test_http_wsgi.py index 6397a9d..224d35d 100644 --- a/tests/test_http_wsgi.py +++ b/tests/test_http_wsgi.py @@ -14,6 +14,7 @@ from azure.functions._http_wsgi import ( WsgiResponse, WsgiMiddleware ) +from azure.functions._http_asgi import AsgiRequest class WsgiException(Exception): @@ -211,6 +212,18 @@ class TestHttpWsgi(unittest.TestCase): self.assertEqual(func_response.status_code, 200) self.assertEqual(func_response.get_body(), b'sample string') + def test_path_encoding_utf8(self): + url = 'http://example.com/Pippi%20L%C3%A5ngstrump' + request = AsgiRequest(self._generate_func_request(url=url)) + + self.assertEqual(request.path_info, u'/Pippi L\u00e5ngstrump') + + def test_path_encoding_latin1(self): + url = 'http://example.com/Pippi%20L%C3%A5ngstrump' + request = WsgiRequest(self._generate_func_request(url=url)) + + self.assertEqual(request.path_info, u'/Pippi L\u00c3\u00a5ngstrump') + def _generate_func_request( self, method="POST",