Fix fast_urljoin mangling root-absolute paths on pages with a path#2030
Open
jichaowang02-lang wants to merge 1 commit into
Open
Fix fast_urljoin mangling root-absolute paths on pages with a path#2030jichaowang02-lang wants to merge 1 commit into
jichaowang02-lang wants to merge 1 commit into
Conversation
fast_urljoin() handled a root-absolute href ("/path") by appending it to
the full base URL (base + url), which is only correct when base has no path
beyond the host. Per RFC 3986 a root-absolute reference replaces the base's
entire path, so for a real crawled sub-page the join was wrong:
fast_urljoin("https://example.com/docs/guide.html", "/api/reference")
-> "https://example.com/docs/guide.html/api/reference" (broken)
urllib.parse.urljoin(...) -> "https://example.com/api/reference"
The function documents urljoin as its fallback, so this diverges from its own
intended contract. DefaultMarkdownGenerator.convert_links_to_citations() joins
every link with fast_urljoin(base_url, href), so every root-absolute link in a
crawled page produced a broken citation/reference URL.
Resolve root-absolute paths against base's scheme://authority root, keeping
the fast path for the common case and deferring to urljoin for unusual bases.
Adds tests/test_fast_urljoin.py asserting parity with urllib.parse.urljoin for
root-absolute, already-correct, and relative cases, plus an end-to-end check
through convert_links_to_citations. The root-absolute cases fail on the old
code and pass with this fix.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
fast_urljoin()mangles root-absolute links (/path) when the base URL hasa path of its own — which is the normal case for a crawled sub-page. A
root-absolute reference must replace the base's entire path (RFC 3986), but the
function appended it to the full base:
fast_urljoindocumentsurllib.parse.urljoinas its fallback, so this alsodiverges from its own intended contract. Because
DefaultMarkdownGenerator.convert_links_to_citations()joins every link withfast_urljoin(base_url, href)(andbase_urlis the actual crawled page URL,which routinely carries a path), every root-absolute link in a crawled page
produced a broken citation/reference URL in the generated markdown.
The fix resolves root-absolute paths against base's
scheme://authorityroot,keeping the fast path for the common case and deferring to
urljoinforunusual bases (e.g. a query/fragment but no path). Output now matches
urljoinfor every case; links on a bare-host base (
https://example.com) were alreadycorrect and stay correct.
List of files changed and why
crawl4ai/markdown_generation_strategy.py— fix the root-absolute branch offast_urljointo join against the host root instead of the full base path.tests/test_fast_urljoin.py— new regression tests.How Has This Been Tested?
tests/test_fast_urljoin.py(new) asserts:urllib.parse.urljoinacross root-absolute hrefs onpath-bearing bases (the bug), root-absolute hrefs on bare-host bases (already
correct), and relative/
../sibling forms./api/referenceon…/docs/guide.htmlresolves tohttps://example.com/api/reference, not…/guide.html/api/reference.mailto:) are unchanged.convert_links_to_citations, the Referencessection now contains the corrected URL.
Confirmed the tests fail on the current code (7 failed: all root-absolute
cases + the end-to-end citation check) and pass with this fix (13 passed).
Checklist: