Skip to content

Commit

Permalink
Parse lightly-serve paths correctly (#1503)
Browse files Browse the repository at this point in the history
  • Loading branch information
guarin authored Feb 16, 2024
1 parent 3260da8 commit f779e58
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lightly/api/serve.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from http.server import HTTPServer, SimpleHTTPRequestHandler
from pathlib import Path
from typing import Sequence
from urllib import parse


def get_server(
Expand Down Expand Up @@ -61,6 +62,7 @@ def _translate_path(path: str, directories: Sequence[Path]) -> str:
if the file doesn't exist.
"""
path = parse.unquote(path)
stripped_path = path.lstrip("/")
for directory in directories:
if (directory / stripped_path).exists():
Expand Down
12 changes: 12 additions & 0 deletions tests/api/test_serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,15 @@ def test__translate_path(tmp_path: Path) -> None:
path="/world.txt",
directories=[tmp_path / "hi", tmp_path / "hello"],
) == str(tmp_file)


def test__translate_path__special_chars(tmp_path: Path) -> None:
(tmp_path / "white space.txt").touch()
assert serve._translate_path(
path="/white%20space.txt", directories=[tmp_path]
) == str(tmp_path / "white space.txt")

(tmp_path / "parens(1).txt").touch()
assert serve._translate_path(
path="/parens%281%29.txt", directories=[tmp_path]
) == str(tmp_path / "parens(1).txt")

0 comments on commit f779e58

Please sign in to comment.