From af3a7526d2beb4baf7fe28c992e5974738496642 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Sat, 6 Feb 2021 13:26:05 +0100 Subject: [PATCH] RFC 3986 conformance: re-use URIBuilder new functionality in URIUtils#normalizeSyntax --- .../hc/client5/http/utils/URIUtils.java | 36 +++---------------- .../hc/client5/http/utils/TestURIUtils.java | 8 ++--- 2 files changed, 9 insertions(+), 35 deletions(-) diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/utils/URIUtils.java b/httpclient5/src/main/java/org/apache/hc/client5/http/utils/URIUtils.java index 3fe9cf06d..cfe8a355c 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/utils/URIUtils.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/utils/URIUtils.java @@ -32,9 +32,9 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Locale; -import java.util.Stack; import org.apache.hc.core5.http.HttpHost; +import org.apache.hc.core5.http.URIScheme; import org.apache.hc.core5.net.URIAuthority; import org.apache.hc.core5.net.URIBuilder; import org.apache.hc.core5.util.Args; @@ -190,8 +190,7 @@ public class URIUtils { } /** - * Removes dot segments according to RFC 3986, section 5.2.4 and - * Syntax-Based Normalization according to RFC 3986, section 6.2.2. + * Removes dot segments and performs Syntax-Based Normalization. * * @param uri the original URI * @return the URI without dot segments @@ -201,39 +200,14 @@ public class URIUtils { // opaque and file: URIs return uri; } - Args.check(uri.isAbsolute(), "Base URI must be absolute"); final URIBuilder builder = new URIBuilder(uri); - if (!builder.isPathEmpty()) { - final List inputSegments = builder.getPathSegments(); - final Stack outputSegments = new Stack<>(); - for (final String inputSegment : inputSegments) { - if (!inputSegment.isEmpty() && !".".equals(inputSegment)) { - if ("..".equals(inputSegment)) { - if (!outputSegments.isEmpty()) { - outputSegments.pop(); - } - } else { - outputSegments.push(inputSegment); - } - } - } - if (!inputSegments.isEmpty()) { - final String lastSegment = inputSegments.get(inputSegments.size() - 1); - if (lastSegment.isEmpty()) { - outputSegments.push(""); - } - } - builder.setPathSegments(outputSegments); + builder.normalizeSyntax(); + if (builder.getScheme() == null) { + builder.setScheme(URIScheme.HTTP.id); } if (builder.isPathEmpty()) { builder.setPathSegments(""); } - if (builder.getScheme() != null) { - builder.setScheme(builder.getScheme().toLowerCase(Locale.ROOT)); - } - if (builder.getHost() != null) { - builder.setHost(builder.getHost().toLowerCase(Locale.ROOT)); - } return builder.build(); } diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/utils/TestURIUtils.java b/httpclient5/src/test/java/org/apache/hc/client5/http/utils/TestURIUtils.java index b82e99b84..2d8e09a16 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/utils/TestURIUtils.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/utils/TestURIUtils.java @@ -163,13 +163,13 @@ public class TestURIUtils { .toString()); Assert.assertEquals("http://a/b/c/y", URIUtils.resolve(this.baseURI, "g;x=1/../y") .toString()); - Assert.assertEquals("http://a/b/c/g?y/./x", URIUtils.resolve(this.baseURI, "g?y/./x") + Assert.assertEquals("http://a/b/c/g?y%2F.%2Fx", URIUtils.resolve(this.baseURI, "g?y/./x") .toString()); - Assert.assertEquals("http://a/b/c/g?y/../x", URIUtils.resolve(this.baseURI, "g?y/../x") + Assert.assertEquals("http://a/b/c/g?y%2F..%2Fx", URIUtils.resolve(this.baseURI, "g?y/../x") .toString()); - Assert.assertEquals("http://a/b/c/g#s/./x", URIUtils.resolve(this.baseURI, "g#s/./x") + Assert.assertEquals("http://a/b/c/g#s%2F.%2Fx", URIUtils.resolve(this.baseURI, "g#s/./x") .toString()); - Assert.assertEquals("http://a/b/c/g#s/../x", URIUtils.resolve(this.baseURI, "g#s/../x") + Assert.assertEquals("http://a/b/c/g#s%2F..%2Fx", URIUtils.resolve(this.baseURI, "g#s/../x") .toString()); Assert.assertEquals("http:g", URIUtils.resolve(this.baseURI, "http:g").toString()); // examples from section 5.2.4