Skip to content

Commit b09cf63

Browse files
committed
Ensure that file:/// are interpretted as absolute urls
1 parent 3cc1715 commit b09cf63

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

pystac/stac_io.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ def _report_duplicate_object_names(
392392

393393
def _is_url(href: str) -> bool:
394394
parsed = safe_urlparse(href)
395-
return parsed.scheme != "" and parsed.scheme != "file"
395+
return parsed.scheme not in ["", "file"]
396396

397397

398398
if HAS_URLLIB3:

pystac/utils.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def make_relative_href(
246246
):
247247
return source_href
248248

249-
if parsed_start.scheme == "":
249+
if parsed_start.scheme in ["", "file"]:
250250
return _make_relative_href_path(parsed_source, parsed_start, start_is_dir)
251251
else:
252252
return _make_relative_href_url(parsed_source, parsed_start, start_is_dir)
@@ -311,6 +311,9 @@ def _make_absolute_href_path(
311311
make_posix_style(os.path.abspath(start_dir)), start_dir
312312
)
313313

314+
if parsed_source.scheme or parsed_start.scheme:
315+
abs_path = f"file://{abs_path}"
316+
314317
return abs_path
315318

316319

@@ -346,7 +349,10 @@ def make_absolute_href(
346349
parsed_start = safe_urlparse(start_href)
347350
parsed_source = safe_urlparse(source_href)
348351

349-
if parsed_source.scheme != "" or parsed_start.scheme != "":
352+
if parsed_source.scheme not in ["", "file"] or parsed_start.scheme not in [
353+
"",
354+
"file",
355+
]:
350356
return _make_absolute_href_url(parsed_source, parsed_start, start_is_dir)
351357
else:
352358
return _make_absolute_href_path(parsed_source, parsed_start, start_is_dir)
@@ -364,7 +370,7 @@ def is_absolute_href(href: str) -> bool:
364370
bool: ``True`` if the given HREF is absolute, ``False`` if it is relative.
365371
"""
366372
parsed = safe_urlparse(href)
367-
return parsed.scheme != "" or os.path.isabs(parsed.path)
373+
return parsed.scheme not in ["", "file"] or os.path.isabs(parsed.path)
368374

369375

370376
def datetime_to_str(dt: datetime, timespec: str = "auto") -> str:

tests/test_utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ def test_make_relative_href(self) -> None:
3333
("/a/catalog.json", "/a/b/c/catalog.json", "../../catalog.json"),
3434
("/a/b/c/d/", "/a/b/c/catalog.json", "./d/"),
3535
("/a/b/c/d/.dotfile", "/a/b/c/d/catalog.json", "./.dotfile"),
36+
(
37+
"file:///a/b/c/d/catalog.json",
38+
"file:///a/b/c/catalog.json",
39+
"./d/catalog.json",
40+
),
3641
]
3742

3843
for source_href, start_href, expected in test_cases:
@@ -161,6 +166,13 @@ def test_make_absolute_href(self) -> None:
161166
"https://stacspec.org/a/b/item.json",
162167
),
163168
("http://localhost:8000", None, "http://localhost:8000"),
169+
("item.json", "file:///a/b/c/catalog.json", "file:///a/b/c/item.json"),
170+
(
171+
"./z/item.json",
172+
"file:///a/b/c/catalog.json",
173+
"file:///a/b/c/z/item.json",
174+
),
175+
("file:///a/b/c/item.json", None, "file:///a/b/c/item.json"),
164176
]
165177

166178
for source_href, start_href, expected in test_cases:
@@ -219,6 +231,8 @@ def test_is_absolute_href(self) -> None:
219231
("item.json", False),
220232
("./item.json", False),
221233
("../item.json", False),
234+
("/home/someuser/item.json", True),
235+
("file:///home/someuser/item.json", True),
222236
("http://stacspec.org/item.json", True),
223237
]
224238

0 commit comments

Comments
 (0)