From 0554271750599756d4946c0d7ba43d04b1a7b220 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Sat, 21 Jan 2017 14:43:44 +0000 Subject: [PATCH] HTTPCLIENT-1803: Improved handling of malformed paths by URIBuilder git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.5.x@1779735 13f79535-47bb-0310-9956-ffa450edef68 --- .../main/java/org/apache/http/client/utils/URIBuilder.java | 5 ++++- .../java/org/apache/http/client/utils/TestURIBuilder.java | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java b/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java index 1cc54ebce..baf7122f3 100644 --- a/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java +++ b/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java @@ -493,7 +493,7 @@ public class URIBuilder { private static String normalizePath(final String path) { String s = path; if (s == null) { - return null; + return "/"; } int n = 0; for (; n < s.length(); n++) { @@ -504,6 +504,9 @@ public class URIBuilder { if (n > 1) { s = s.substring(n - 1); } + if (!s.startsWith("/")) { + s = "/" + s; + } return s; } diff --git a/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java b/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java index 6ed9d7af9..6afc59422 100644 --- a/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java +++ b/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java @@ -292,4 +292,11 @@ public class TestURIBuilder { return parameters; } + @Test + public void testMalformedPath() throws Exception { + final String path = "@notexample.com/mypath"; + final URI uri = new URIBuilder(path).setHost("example.com").build(); + Assert.assertEquals("example.com", uri.getHost()); + } + }