Repository navigation
Conversation
|
Thanks @sean0407, this fixes what #2198 reports. I checked the three URLs from the issue and they now come back unchanged, including with A few things to look at before merging. 1. A
|
input (removeAnchorPart: false) |
output with this PR | expected |
|---|---|---|
http://a.example/p&a=b#frag |
http://a.example/p?a=b#frag |
same ✔ |
http://a.example/p&a=b#frag?x |
http://a.example/p&a=b#frag?x |
http://a.example/p?a=b#frag?x |
Splitting off the fragment once at the start, working on the part before it and putting it back at the end fixes this. It also makes the ? check on the last element unnecessary. Something along these lines:
private String unmangleQueryString(String urlToFilter) {
// the fragment is not part of the query: leave it out of the checks
// and put it back as is
final int hash = urlToFilter.indexOf('#');
final String url = hash == -1 ? urlToFilter : urlToFilter.substring(0, hash);
if (url.indexOf('?') != -1) {
// there is already a query
return urlToFilter;
}
// only look at what follows the last slash, so that an & in an
// earlier path segment is not mistaken for the start of a query
final int lastSlash = url.lastIndexOf('/');
final String last = url.substring(lastSlash + 1);
final int firstAmp = last.indexOf('&');
if (firstAmp == -1 || last.indexOf('=') <= 0) {
return urlToFilter;
}
return url.substring(0, lastSlash + 1)
+ last.substring(0, firstAmp)
+ '?'
+ last.substring(firstAmp + 1)
+ (hash == -1 ? "" : urlToFilter.substring(hash));
}2. Tests
The new test only uses an empty config. It would be worth adding:
-
The third URL from the issue, with
queryElementsToRemove: ["utm_source"], since that's where the bug did the most damage:input before this PR with this PR http://a.example/login/?next=/a/b/&utm_source=ref&z=1http://a.example/login/?next=%2Fa%2Fb%2F%3Futm_source%3Dref&z=1(utm_sourcestuck insidenext)http://a.example/login/?next=%2Fa%2Fb%2F&z=1 -
The fragment case from point 1, with
removeAnchorPart: false. At the moment no test reaches the fragment branch of the new guard.
Nits
- The Javadoc of
unmangleQueryStringstill only describes the&→?repair. It should say that a URL already containing a?before the fragment is left alone, and why only the last path segment is checked (so an&in an earlier segment isn't taken for the start of a query).
|
@sean0407 please address comments + format code when u commit. |
Summary
Fixes #2198.
Evidence
http://a.example/article.pl?sid=01/10/23/1816257&mode=thread&tid=107as a URL containing a second?, changing the query structure.git diff --checkpassed. The patch policy accepted 2 files and 25 changed lines.Merge Danger
Door: Two-way; the normalizer change is easy to revert.
Blast Radius: Low; it only bypasses the legacy repair when a query already exists before the fragment.