Preserve original encoding of the URI path component if the URI is valid

This commit is contained in:
Oleg Kalnichevski 2019-06-18 16:59:37 +02:00
parent 6992339b6c
commit 94fc91dae2
2 changed files with 7 additions and 2 deletions

View File

@ -217,14 +217,17 @@ public class URIUtils {
uribuilder.setFragment(null);
}
if (flags.contains(UriFlag.NORMALIZE)) {
final List<String> pathSegments = new ArrayList<String>(uribuilder.getPathSegments());
final List<String> originalPathSegments = uribuilder.getPathSegments();
final List<String> pathSegments = new ArrayList<String>(originalPathSegments);
for (final Iterator<String> it = pathSegments.iterator(); it.hasNext(); ) {
final String pathSegment = it.next();
if (pathSegment.isEmpty() && it.hasNext()) {
it.remove();
}
}
uribuilder.setPathSegments(pathSegments);
if (pathSegments.size() != originalPathSegments.size()) {
uribuilder.setPathSegments(pathSegments);
}
}
if (uribuilder.isPathEmpty()) {
uribuilder.setPathSegments("");

View File

@ -82,6 +82,8 @@ public class TestURIUtils {
Assert.assertEquals("http://thishost/Fragment_identifier%23Examples",
URIUtils.rewriteURI(
URI.create("http://thishost/Fragment_identifier%23Examples")).toString());
Assert.assertEquals("http://thathost/foo%3Abar", URIUtils.rewriteURI(
URI.create("http://thishost/foo%3Abar"), target).toString());
}
@Test