Skip to content

Commit

Permalink
Don't require trailing slash when serving a directory index (#2507)
Browse files Browse the repository at this point in the history
in the livereload/local server.

Instead of showing a 404 error, detect if it's a directory and redirect to a path with a trailing slash added
  • Loading branch information
oprypin committed Jul 18, 2021
1 parent e0ba6d7 commit 1b15412
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
11 changes: 7 additions & 4 deletions mkdocs/livereload/__init__.py
Expand Up @@ -183,10 +183,10 @@ def condition():
if path == "/js/livereload.js":
file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "livereload.js")
elif path.startswith(self.mount_path):
rel_file_path = path[len(self.mount_path):].lstrip("/")
if path.endswith("/"):
path += "index.html"
path = path[len(self.mount_path):]
file_path = os.path.join(self.root, path.lstrip("/"))
rel_file_path += "index.html"
file_path = os.path.join(self.root, rel_file_path)
elif path == "/":
start_response("302 Found", [("Location", self.mount_path)])
return []
Expand All @@ -201,9 +201,12 @@ def condition():
try:
file = open(file_path, "rb")
except OSError:
if not path.endswith("/") and os.path.isfile(os.path.join(file_path, "index.html")):
start_response("302 Found", [("Location", path + "/")])
return []
return None # Not found

if path.endswith(".html"):
if file_path.endswith(".html"):
with file:
content = file.read()
content = self._inject_js_into_html(content, epoch)
Expand Down
19 changes: 16 additions & 3 deletions mkdocs/tests/livereload_tests.py
Expand Up @@ -293,16 +293,29 @@ def test_serves_modified_html(self, site_dir):
self.assertRegex(output, fr"^<body>foo</body><body>bar{SCRIPT_REGEX}</body>$")

@tempdir({"index.html": "<body>aaa</body>", "foo/index.html": "<body>bbb</body>"})
def test_serves_modified_index(self, site_dir):
def test_serves_directory_index(self, site_dir):
with testing_server(site_dir) as server:
headers, output = do_request(server, "GET /")
self.assertRegex(output, fr"^<body>aaa{SCRIPT_REGEX}</body>$")
self.assertEqual(headers["_status"], "200 OK")
self.assertEqual(headers.get("content-type"), "text/html")
self.assertEqual(headers.get("content-length"), str(len(output)))

_, output = do_request(server, "GET /foo/")
self.assertRegex(output, fr"^<body>bbb{SCRIPT_REGEX}</body>$")
for path in "/foo/", "/foo/index.html":
_, output = do_request(server, "GET /foo/")
self.assertRegex(output, fr"^<body>bbb{SCRIPT_REGEX}</body>$")

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

@tempdir({"foo/bar/index.html": "<body>aaa</body>"})
def test_redirects_to_directory(self, site_dir):
with testing_server(site_dir, mount_path="/sub") as server:
with self.assertLogs("mkdocs.livereload"):
headers, _ = do_request(server, "GET /sub/foo/bar")
self.assertEqual(headers["_status"], "302 Found")
self.assertEqual(headers.get("location"), "/sub/foo/bar/")

@tempdir({"я.html": "<body>aaa</body>", "测试2/index.html": "<body>bbb</body>"})
def test_serves_with_unicode_characters(self, site_dir):
Expand Down

0 comments on commit 1b15412

Please sign in to comment.