An open source, non-profit web search engine
Drop malformed links instead of failing the whole crawl batch
Claude7eb2d0b7ed49
diff --git a/mwmbl/crawler/retrieve.py b/mwmbl/crawler/retrieve.pyindex bb00fa2..9d09557 100644--- a/mwmbl/crawler/retrieve.py+++ b/mwmbl/crawler/retrieve.py@@ -246,12 +246,13 @@ def _cache_robots_content(redis: Redis, domain: str, content: List[str], error:def _resolve_and_validate_link(href: str, current_url: str) -> str | None:"""Resolve a raw href to an absolute URL and validate it. Returns None if invalid."""- href = urljoin(current_url, href)- if not href.startswith("http") or len(href) > MAX_URL_LENGTH:- return None- if BAD_URL_REGEX.search(href):- return Nonetry:+ # Both raise ValueError on malformed hrefs such as "http://[foo/" (invalid IPv6)+ href = urljoin(current_url, href)+ if not href.startswith("http") or len(href) > MAX_URL_LENGTH:+ return None+ if BAD_URL_REGEX.search(href):+ return Noneparsed = urlparse(href)except ValueError:return Nonediff --git a/test/test_retrieve_robots_and_links.py b/test/test_retrieve_robots_and_links.pyindex e65baac..2a5b902 100644--- a/test/test_retrieve_robots_and_links.py+++ b/test/test_retrieve_robots_and_links.py@@ -137,6 +137,15 @@ def test_link_collection_stops_once_both_sets_are_full(monkeypatch):assert len(extra_links) == 1+def test_invalid_ipv6_links_are_dropped_not_raised():+ paragraphs = [_paragraph(["http://[broken/", "/a"], "good")]++ new_links, extra_links = get_new_links(paragraphs, "https://example.test/")++ assert new_links == {"https://example.test/a"}+ assert extra_links == set()++def test_fragments_are_stripped_and_overlong_urls_dropped():dom = html_to_dom((