Skip to content

Commit

Permalink
[util] Fix parsing of URI with percent-encoded spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
valfirst committed May 31, 2024
1 parent a38937f commit 407b2d6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
11 changes: 7 additions & 4 deletions vividus-util/src/main/java/org/vividus/util/UriUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -317,15 +317,18 @@ public static URI buildNewUrl(URI url, String relativeUrl)
return new URI(url.getScheme(), url.getSchemeSpecificPart(), decodedFragment);
}

URI parsedRelativeUrl = new URI(removeFragment(decodeUrl(normalizedRelativeUrl), decodedFragment));
String path = StringUtils.repeat(SLASH, indexOfFirstNonSlashChar - 1) + parsedRelativeUrl.getRawPath();
String parsedRelativeUrl = removeFragment(decodeUrl(normalizedRelativeUrl), decodedFragment);
String[] parts = StringUtils.split(parsedRelativeUrl, "?", 2);
String rawPath = parts.length > 0 ? parts[0] : "";
String query = parts.length > 1 ? parts[1] : null;
String path = StringUtils.repeat(SLASH, indexOfFirstNonSlashChar - 1) + rawPath;
if (!path.isEmpty() && path.charAt(0) != '/')
{
throw new IllegalArgumentException(String
.format("Relative path '%s' for '%s' should start with forward slash ('/')", path, url));
}
String uriAsString = createUriAsString(url.getScheme(), url.getRawAuthority(), path,
parsedRelativeUrl.getQuery(), decodedFragment);
String uriAsString = createUriAsString(url.getScheme(), url.getRawAuthority(), path, query,
decodedFragment);

return buildUrl(uriAsString, decodedFragment);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ void testCreateUriWithException()
"https://test:pas%40dsad@host.com, '', https://test:pas%40dsad@host.com",
"http://localhost:4200, /m/cool#%E7%94%A2%E5%93%81%E6%A6%82%E8%A6%BD%20overview, http://localhost:4200/m/cool#%E7%94%A2%E5%93%81%E6%A6%82%E8%A6%BD%20overview",
"http://localhost:4200, /m/cool#產品概覽 overview, http://localhost:4200/m/cool#%E7%94%A2%E5%93%81%E6%A6%82%E8%A6%BD%20overview",
"https://somehost.il/, /%D7%92'%D7%95%D7%A0%D7%A1%D7%95%D7%A0%D7%A1-%D7%98%D7%99%D7%A4%D7%95%D7%AA-%D7%A9%D7%9C-%D7%91%D7%A8%D7%A7%20%D7%AA%D7%A8%D7%A1%D7%99%D7%A1-%D7%9E%D7%A8%D7%9B%D7%9A-%D7%A9%D7%99%D7%A2%D7%A8-%D7%9C%D7%99%D7%9C%D7%93%D7%99%D7%9D, https://somehost.il/%D7%92'%D7%95%D7%A0%D7%A1%D7%95%D7%A0%D7%A1-%D7%98%D7%99%D7%A4%D7%95%D7%AA-%D7%A9%D7%9C-%D7%91%D7%A8%D7%A7%20%D7%AA%D7%A8%D7%A1%D7%99%D7%A1-%D7%9E%D7%A8%D7%9B%D7%9A-%D7%A9%D7%99%D7%A2%D7%A8-%D7%9C%D7%99%D7%9C%D7%93%D7%99%D7%9D"
// CHECKSTYLE:ON
})
void testBuildNewUri(String baseUrl, String relativeUrl, String expectedUrl)
Expand Down

0 comments on commit 407b2d6

Please sign in to comment.