Skip to content

Commit

Permalink
Fix serving files/paths with Unicode characters
Browse files Browse the repository at this point in the history
  • Loading branch information
oprypin committed Jun 15, 2021
1 parent bd167cf commit 7ca55a1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion mkdocs/livereload/__init__.py
Expand Up @@ -200,7 +200,9 @@ def serve_request(self, environ, start_response):
return [error_content]

def _serve_request(self, environ, start_response):
path = environ["PATH_INFO"]
# https://bugs.python.org/issue16679
# https://github.com/bottlepy/bottle/blob/0.12.19/bottle.py#L854
path = environ["PATH_INFO"].encode("latin-1").decode("utf-8", "ignore")

m = re.fullmatch(r"/livereload/([0-9]+)/[0-9]+", path)
if m:
Expand Down
17 changes: 17 additions & 0 deletions mkdocs/tests/livereload_tests.py
Expand Up @@ -304,6 +304,23 @@ def test_serves_modified_index(self, site_dir):
_, output = do_request(server, "GET /foo/")
self.assertRegex(output, fr"^<body>bbb{SCRIPT_REGEX}</body>$")

@tempdir({"я.html": "<body>aaa</body>", "测试2/index.html": "<body>bbb</body>"})
def test_serves_with_unicode_characters(self, site_dir):
with testing_server(site_dir) as server:
_, output = do_request(server, "GET /я.html")
self.assertRegex(output, fr"^<body>aaa{SCRIPT_REGEX}</body>$")
_, output = do_request(server, "GET /%D1%8F.html")
self.assertRegex(output, fr"^<body>aaa{SCRIPT_REGEX}</body>$")

with self.assertLogs("mkdocs.livereload"):
headers, _ = do_request(server, "GET /%D1.html")
self.assertEqual(headers["_status"], "404 Not Found")

_, output = do_request(server, "GET /测试2/")
self.assertRegex(output, fr"^<body>bbb{SCRIPT_REGEX}</body>$")
_, output = do_request(server, "GET /%E6%B5%8B%E8%AF%952/index.html")
self.assertRegex(output, fr"^<body>bbb{SCRIPT_REGEX}</body>$")

@tempdir()
def test_serves_js(self, site_dir):
with testing_server(site_dir) as server:
Expand Down

0 comments on commit 7ca55a1

Please sign in to comment.