Repository navigation
Do not collapse consecutive slashes when normalizing URLs - #344
Conversation
Sinkleberg
left a comment
There was a problem hiding this comment.
There is still a mismatch in the Python rewrite path. From https://example.com/index.html, rewriting https://example.com/a///b now returns the ZIM key example.com/a///b but the link a/b: get_document_uri() passes the key through PurePosixPath, which collapses the slashes again. The link no longer points to the preserved key. I reproduced the same problem with repeated slashes in a query value; the base version's key and link matched.
Could you preserve empty path segments when generating the relative document URL and test the full rewrite result, not only normalize()? These were isolated Python checks, not an archive or browser test.
Sinkleberg
left a comment
There was a problem hiding this comment.
Thanks, the original cases and six added cases pass in my isolated checks. One edge remains: from https://example.com/index.html, the target https://example.com//b returns key example.com//b but link /b, which resolves at the reader's root. https://example.com///b produces //b, interpreted as another host.
Could you keep these links relative and add round-trip regressions for leading empty segments and an empty segment after the shared directory? Verified with the actual module and WHATWG URL resolution; no archive test.
|
Thanks for catching this, you're right! I only fixed the normalize() side and missed that get_document_uri() runs the path through PurePosixPath, which squashes the slashes again. I pushed a fix: the relative link is now built segment by segment, so empty segments (the //) are kept. For paths without //, it gives exactly the same result as before. I also added tests that go through the full rewriter like you suggested, covering both the path and the query string cases, plus a page that itself has // in its URL. One thing I noticed while testing: if a page's own URL contains //, a relative link inside it still gets collapsed, because Python's urljoin removes the empty segments when resolving it. That's outside this PR, but I can open a separate issue for it if you think it's worth fixing. |
|
Yes, a separate issue makes sense for the The original fixes still pass. The leading-slash case from my latest review remains on the current head: the link |
|
@anshuman83-40 please open follow-up issue indeed Looks like @Sinkleberg edge case indeed still needs to be fixed. Please add it to the test cases |
|
Fixed the leading-slash case: links that would start with Opened #346 for the urljoin case, thanks! |
|
Thanks, the leading-slash cases now round-trip correctly in the isolated Python/WHATWG checks, and the original cases still pass. That resolves the link issue I raised here. Keeping the separate |
|
Thanks for checking it so carefully @Sinkleberg! |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #344 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 42 42
Lines 2708 2720 +12
Branches 401 404 +3
=========================================
+ Hits 2708 2720 +12 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
benoit74
left a comment
There was a problem hiding this comment.
Looks like JS side is not working: https://github.com/openzim/python-scraperlib/actions/runs/37477146077/job/115192717633?pr=344
|
The three failures are |
|
Sorry about that, I missed three existing JS tests ( |
|
Confirmed, the three expectations are updated and all 140 JS tests pass in CI. That resolves the mismatch I flagged. |
benoit74
left a comment
There was a problem hiding this comment.
Please squash your commits and rebase on main (we do not want merge commits which makes git history impossible to read)
a//b and a/b are different URLs which may serve different content, but URL normalization rewrote any run of slashes in the path or querystring as a single one. Keep them as they are, in both the Python normalization and the JS (wombat) URL rewriting, which must produce the same ZIM paths. get_document_uri computed the relative link with PurePosixPath, which collapsed the slashes again; compute it segment by segment instead, keeping empty segments, and prefix it with ./ when it would otherwise start with / (making the link absolute or scheme-relative). Fix openzim#340
d0f7fd9 to
59ecb8f
Compare
|
Done, squashed into a single commit and rebased on |
Fix #340
URL normalization rewrote any run of slashes in the path or querystring
as a single slash, so
https://example.com/a///bbecame the ZIM pathexample.com/a/b.a//banda/bare different URLs which may servedifferent content, so they are now kept as they are.
The same collapsing existed in the JS URL rewriting (
wombatSetup.js),which must produce the same ZIM paths as the Python normalization, so it
is removed there too.
Tests:
test_normalizecases that expected thecollapsing, and added
a///banda//b?c=d//ecases.consecutiveSlashesAreKeptinwombatUrlRewriting.js.This only changes ZIM paths for URLs containing
//after the host. As faras I can tell existing ZIMs are not affected, since each ZIM ships the wombat
setup it was built with, but please correct me if that's wrong.
I could only run the Python tests locally; the JS test runs in CI.