From 94fc91dae266754056585bb847a026454d7da014 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Tue, 18 Jun 2019 16:59:37 +0200 Subject: [PATCH] Preserve original encoding of the URI path component if the URI is valid --- .../main/java/org/apache/http/client/utils/URIUtils.java | 7 +++++-- .../java/org/apache/http/client/utils/TestURIUtils.java | 2 ++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java b/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java index 6a6a742a2..8eb7667e3 100644 --- a/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java +++ b/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java @@ -217,14 +217,17 @@ public class URIUtils { uribuilder.setFragment(null); } if (flags.contains(UriFlag.NORMALIZE)) { - final List pathSegments = new ArrayList(uribuilder.getPathSegments()); + final List originalPathSegments = uribuilder.getPathSegments(); + final List pathSegments = new ArrayList(originalPathSegments); for (final Iterator 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(""); diff --git a/httpclient/src/test/java/org/apache/http/client/utils/TestURIUtils.java b/httpclient/src/test/java/org/apache/http/client/utils/TestURIUtils.java index 4c81ef23e..189966635 100644 --- a/httpclient/src/test/java/org/apache/http/client/utils/TestURIUtils.java +++ b/httpclient/src/test/java/org/apache/http/client/utils/TestURIUtils.java @@ -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