hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1787741 - in /httpcomponents/httpcore/trunk/httpcore5/src: main/java/org/apache/hc/core5/net/URIBuilder.java test/java/org/apache/hc/core5/net/TestURIBuilder.java
Date Mon, 20 Mar 2017 12:27:19 GMT
Author: olegk
Date: Mon Mar 20 12:27:19 2017
New Revision: 1787741

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

Modified:
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/net/URIBuilder.java
    httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/net/TestURIBuilder.java

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/net/URIBuilder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/net/URIBuilder.java?rev=1787741&r1=1787740&r2=1787741&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/net/URIBuilder.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/net/URIBuilder.java
Mon Mar 20 12:27:19 2017
@@ -145,9 +145,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);
@@ -165,6 +165,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();
@@ -472,24 +492,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/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/net/TestURIBuilder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/net/TestURIBuilder.java?rev=1787741&r1=1787740&r2=1787741&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/net/TestURIBuilder.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/net/TestURIBuilder.java
Mon Mar 20 12:27:19 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);
+    }
+
 }



Mime
View raw message