Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't require trailing slash when serving a directory index #2507

Merged
merged 1 commit into from Jul 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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