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

Revert to disregarding the mount path for /livereload/ endpoint #2804

Merged
merged 1 commit into from Mar 26, 2022
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
15 changes: 7 additions & 8 deletions mkdocs/livereload/__init__.py
Expand Up @@ -32,7 +32,7 @@
setTimeout(launchNext, 3000);
}
};
req.open("GET", "${mount_path}livereload/" + epoch + "/" + requestId);
req.open("GET", "/livereload/" + epoch + "/" + requestId);
req.send();

console.log('Enabled live reload');
Expand Down Expand Up @@ -201,10 +201,8 @@ def _serve_request(self, environ, start_response):
# https://github.com/bottlepy/bottle/blob/f9b1849db4/bottle.py#L984
path = environ["PATH_INFO"].encode("latin-1").decode("utf-8", "ignore")

if path.startswith(self.mount_path):
rel_file_path = path[len(self.mount_path):]

m = re.fullmatch(r"livereload/([0-9]+)/[0-9]+", rel_file_path)
if path.startswith("/livereload/"):
m = re.fullmatch(r"/livereload/([0-9]+)/[0-9]+", path)
if m:
epoch = int(m[1])
start_response("200 OK", [("Content-Type", "text/plain")])
Expand All @@ -220,6 +218,9 @@ def condition():
self._epoch_cond.wait_for(condition, timeout=self.poll_response_timeout)
return [b"%d" % self._visible_epoch]

if path.startswith(self.mount_path):
rel_file_path = path[len(self.mount_path):]

if path.endswith("/"):
rel_file_path += "index.html"
# Prevent directory traversal - normalize the path.
Expand Down Expand Up @@ -266,9 +267,7 @@ def _inject_js_into_html(self, content, epoch):
body_end = len(content)
# The page will reload if the livereload poller returns a newer epoch than what it knows.
# The other timestamp becomes just a unique identifier for the initiating page.
script = _SCRIPT_TEMPLATE.substitute(
mount_path=self.mount_path, epoch=epoch, request_id=_timestamp()
)
script = _SCRIPT_TEMPLATE.substitute(epoch=epoch, request_id=_timestamp())
return b"%b<script>%b</script>%b" % (
content[:body_end],
script.encode(),
Expand Down
4 changes: 2 additions & 2 deletions mkdocs/tests/livereload_tests.py
Expand Up @@ -343,9 +343,9 @@ def test_serves_polling_instantly(self, site_dir):
self.assertTrue(output.isdigit())

@tempdir()
def test_serves_polling_from_mount_path(self, site_dir):
def test_serves_polling_with_mount_path(self, site_dir):
with testing_server(site_dir, mount_path="/test/f*o") as server:
_, output = do_request(server, "GET /test/f*o/livereload/0/0")
_, output = do_request(server, "GET /livereload/0/0")
self.assertTrue(output.isdigit())

@tempdir()
Expand Down