Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions courlan/urlutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,13 @@ def fix_relative_urls(baseurl: str, url: str) -> str:
if url.startswith("{"):
return url

parsed_base = urlsplit(baseurl)
try:
parsed_base = urlsplit(baseurl)
split_url = urlsplit(url)
except ValueError:
return url

base_netloc = parsed_base.netloc
split_url = urlsplit(url)

if split_url.netloc not in (base_netloc, ""):
if split_url.scheme:
Expand Down
7 changes: 7 additions & 0 deletions tests/unit_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ def test_fix_relative():
== "https://www.example.org/foo.html?q=bar#baz"
)
assert fix_relative_urls("https://www.example.org", "{privacy}") == "{privacy}"
# malformed IPv6 in protocol-relative href must not raise (urlsplit, not _parse)
assert fix_relative_urls("https://example.org", "//[::1/path") == "//[::1/path"


def test_scrub():
Expand Down Expand Up @@ -984,6 +986,11 @@ def test_extraction():
extract_links(None, base_url="https://test.com/", external_bool=False)
assert not extract_links(None, url="https://test.com/", external_bool=False)
assert not extract_links("", "https://test.com/", False)
# malformed IPv6 protocol-relative href must not raise (fix_relative_urls path)
pagecontent = '<html><a href="//[::1/path">x</a><a href="/ok">y</a></html>'
assert extract_links(pagecontent, "https://example.com/", False) == {
"https://example.com/ok"
}
# anchor tags matched by the regex but without an href yield no candidate
pagecontent = '<html><a class="logo">home</a><a name="x">y</a></html>'
assert not extract_links(pagecontent, "https://test.com/", False)
Expand Down
Loading