Skip to content
Permalink
Browse files
RFC 3986 conformance: re-use URIBuilder new functionality in URIUtils…
…#normalizeSyntax
  • Loading branch information
ok2c committed Feb 6, 2021
1 parent 004bc85 commit af3a7526d2beb4baf7fe28c992e5974738496642
Showing 2 changed files with 9 additions and 35 deletions.
@@ -32,9 +32,9 @@
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 static URI resolve(final URI baseURI, final URI reference) {
}

/**
* 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 @@ static URI normalizeSyntax(final URI uri) throws URISyntaxException {
// 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<String> inputSegments = builder.getPathSegments();
final Stack<String> 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();
}

@@ -163,13 +163,13 @@ public void testResolve() {
.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

0 comments on commit af3a752

Please sign in to comment.