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 2025/03/14 12:54:42 UTC

(httpcomponents-core) branch master updated: HTTPCORE-778: URIBuilder to not encode sub-delims in URI fragment component for consistency with query component encoding

This is an automated email from the ASF dual-hosted git repository.

olegk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git


The following commit(s) were added to refs/heads/master by this push:
     new 3426b90e0 HTTPCORE-778: URIBuilder to not encode sub-delims in URI fragment component for consistency with query component encoding
3426b90e0 is described below

commit 3426b90e033a34217a23f66ca2eed5d27a24a6f1
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Fri Mar 14 12:08:11 2025 +0100

    HTTPCORE-778: URIBuilder to not encode sub-delims in URI fragment component for consistency with query component encoding
---
 .../src/main/java/org/apache/hc/core5/net/URIBuilder.java     |  4 ++--
 .../src/test/java/org/apache/hc/core5/net/TestURIBuilder.java | 11 +++++++++++
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/httpcore5/src/main/java/org/apache/hc/core5/net/URIBuilder.java b/httpcore5/src/main/java/org/apache/hc/core5/net/URIBuilder.java
index eb3bcc3ee..7151d7bd9 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/net/URIBuilder.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/net/URIBuilder.java
@@ -367,7 +367,7 @@ private String buildString() {
                 if (InetAddressUtils.isIPv6(this.host)) {
                     sb.append("[").append(this.host).append("]");
                 } else {
-                    sb.append(PercentCodec.encode(this.host, this.charset));
+                    PercentCodec.encode(sb, this.host, this.charset);
                 }
                 if (this.port >= 0) {
                     sb.append(":").append(this.port);
@@ -398,7 +398,7 @@ private String buildString() {
             sb.append("#").append(this.encodedFragment);
         } else if (this.fragment != null) {
             sb.append("#");
-            PercentCodec.encode(sb, this.fragment, this.charset);
+            PercentCodec.encode(sb, this.fragment, this.charset, PercentCodec.URIC, false);
         }
         return sb.toString();
     }
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/net/TestURIBuilder.java b/httpcore5/src/test/java/org/apache/hc/core5/net/TestURIBuilder.java
index 495be9a9d..98f54f390 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/net/TestURIBuilder.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/net/TestURIBuilder.java
@@ -465,6 +465,17 @@ void testPathEncoding() throws Exception {
         Assertions.assertEquals(uri1, uri2);
     }
 
+    @Test
+    void testFragmentEncoding() throws Exception {
+        final URI uri1 = new URI("https://somehost.com#some%20fragment%20with%20all%20sorts%20of%20$tuff%20in%20it!!!");
+        final URI uri2 = new URIBuilder()
+                .setScheme("https")
+                .setHost("somehost.com")
+                .setFragment("some fragment with all sorts of $tuff in it!!!")
+                .build();
+        Assertions.assertEquals(uri1, uri2);
+    }
+
     @Test
     void testAgainstURI() throws Exception {
         // Check that the URI generated by URI builder agrees with that generated by using URI directly