Skip to content

Commit b8a45f0

Browse files
committed
HTTPCORE-778: URIBuilder to not encode sub-delims in URI fragment component for consistency with query component encoding
1 parent 00973e8 commit b8a45f0

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

httpcore5/src/main/java/org/apache/hc/core5/net/URIBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ private String buildString() {
338338
if (InetAddressUtils.isIPv6(this.host)) {
339339
sb.append("[").append(this.host).append("]");
340340
} else {
341-
sb.append(PercentCodec.encode(this.host, this.charset));
341+
PercentCodec.encode(sb, this.host, this.charset);
342342
}
343343
if (this.port >= 0) {
344344
sb.append(":").append(this.port);
@@ -369,7 +369,7 @@ private String buildString() {
369369
sb.append("#").append(this.encodedFragment);
370370
} else if (this.fragment != null) {
371371
sb.append("#");
372-
PercentCodec.encode(sb, this.fragment, this.charset);
372+
PercentCodec.encode(sb, this.fragment, this.charset, PercentCodec.URIC, false);
373373
}
374374
return sb.toString();
375375
}

httpcore5/src/test/java/org/apache/hc/core5/net/TestURIBuilder.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,17 @@ void testPathEncoding() throws Exception {
473473
Assertions.assertEquals(uri1, uri2);
474474
}
475475

476+
@Test
477+
void testFragmentEncoding() throws Exception {
478+
final URI uri1 = new URI("https://somehost.com#some%20fragment%20with%20all%20sorts%20of%20$tuff%20in%20it!!!");
479+
final URI uri2 = new URIBuilder()
480+
.setScheme("https")
481+
.setHost("somehost.com")
482+
.setFragment("some fragment with all sorts of $tuff in it!!!")
483+
.build();
484+
Assertions.assertEquals(uri1, uri2);
485+
}
486+
476487
@Test
477488
void testAgainstURI() throws Exception {
478489
// Check that the URI generated by URI builder agrees with that generated by using URI directly

0 commit comments

Comments
 (0)