You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2017/03/20 12:27:02 UTC

svn commit: r1787740 - in /httpcomponents/httpclient/branches/4.6.x/httpclient/src: main/java/org/apache/http/client/utils/URIBuilder.java test/java/org/apache/http/client/utils/TestURIBuilder.java

Author: olegk
Date: Mon Mar 20 12:27:02 2017
New Revision: 1787740

URL: http://svn.apache.org/viewvc?rev=1787740&view=rev
Log:
HTTPCLIENT-1831: URIBuilder should not prepend a leading slash to relative URIs

Modified:
    httpcomponents/httpclient/branches/4.6.x/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java
    httpcomponents/httpclient/branches/4.6.x/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java

Modified: httpcomponents/httpclient/branches/4.6.x/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.6.x/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java?rev=1787740&r1=1787739&r2=1787740&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.6.x/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java (original)
+++ httpcomponents/httpclient/branches/4.6.x/httpclient/src/main/java/org/apache/http/client/utils/URIBuilder.java Mon Mar 20 12:27:02 2017
@@ -146,9 +146,9 @@ public class URIBuilder {
                 }
             }
             if (this.encodedPath != null) {
-                sb.append(normalizePath(this.encodedPath));
+                sb.append(normalizePath(this.encodedPath, sb.length() == 0));
             } else if (this.path != null) {
-                sb.append(encodePath(normalizePath(this.path)));
+                sb.append(encodePath(normalizePath(this.path, sb.length() == 0)));
             }
             if (this.encodedQuery != null) {
                 sb.append("?").append(this.encodedQuery);
@@ -166,6 +166,26 @@ public class URIBuilder {
         return sb.toString();
     }
 
+    private static String normalizePath(final String path, final boolean relative) {
+        String s = path;
+        if (TextUtils.isBlank(s)) {
+            return "";
+        }
+        int n = 0;
+        for (; n < s.length(); n++) {
+            if (s.charAt(n) != '/') {
+                break;
+            }
+        }
+        if (n > 1) {
+            s = s.substring(n - 1);
+        }
+        if (!relative && !s.startsWith("/")) {
+            s = "/" + s;
+        }
+        return s;
+    }
+
     private void digestURI(final URI uri) {
         this.scheme = uri.getScheme();
         this.encodedSchemeSpecificPart = uri.getRawSchemeSpecificPart();
@@ -491,24 +511,4 @@ public class URIBuilder {
         return buildString();
     }
 
-    private static String normalizePath(final String path) {
-        String s = path;
-        if (TextUtils.isBlank(s)) {
-            return "";
-        }
-        int n = 0;
-        for (; n < s.length(); n++) {
-            if (s.charAt(n) != '/') {
-                break;
-            }
-        }
-        if (n > 1) {
-            s = s.substring(n - 1);
-        }
-        if (!s.startsWith("/")) {
-            s = "/" + s;
-        }
-        return s;
-    }
-
 }

Modified: httpcomponents/httpclient/branches/4.6.x/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.6.x/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java?rev=1787740&r1=1787739&r2=1787740&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.6.x/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java (original)
+++ httpcomponents/httpclient/branches/4.6.x/httpclient/src/test/java/org/apache/http/client/utils/TestURIBuilder.java Mon Mar 20 12:27:02 2017
@@ -299,4 +299,16 @@ public class TestURIBuilder {
         Assert.assertEquals("example.com", uri.getHost());
     }
 
+    @Test
+    public void testRelativePath() throws Exception {
+        final URI uri = new URIBuilder("./mypath").build();
+        Assert.assertEquals(new URI("./mypath"), uri);
+    }
+
+    @Test
+    public void testRelativePathWithAuthority() throws Exception {
+        final URI uri = new URIBuilder("./mypath").setHost("somehost").setScheme("http").build();
+        Assert.assertEquals(new URI("http://somehost/./mypath"), uri);
+    }
+
 }