This is an automated email from the ASF dual-hosted git repository.
coheigea pushed a commit to branch 3.6.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git
The following commit(s) were added to refs/heads/3.6.x-fixes by this push:
new 960c4a7f5b1 Encode supplementary characters correctly in
URIParserUtil.escapeChars (#3787)
960c4a7f5b1 is described below
commit 960c4a7f5b1cb640ede9ad6dd915eef2c5095351
Author: Colm O hEigeartaigh <[email protected]>
AuthorDate: Thu Nov 26 11:29:58 2026 +0000
Encode supplementary characters correctly in URIParserUtil.escapeChars
(#3787)
(cherry picked from commit 93677953a8fc98037510b446b452c34b4f8d408e)
---
.../main/java/org/apache/cxf/common/util/URIParserUtil.java | 12 +++++++-----
.../java/org/apache/cxf/common/util/URIParserUtilTest.java | 10 +++++++++-
2 files changed, 16 insertions(+), 6 deletions(-)
diff --git a/core/src/main/java/org/apache/cxf/common/util/URIParserUtil.java
b/core/src/main/java/org/apache/cxf/common/util/URIParserUtil.java
index 462896f038c..63d2db13a3e 100644
--- a/core/src/main/java/org/apache/cxf/common/util/URIParserUtil.java
+++ b/core/src/main/java/org/apache/cxf/common/util/URIParserUtil.java
@@ -67,17 +67,19 @@ public final class URIParserUtil {
public static String escapeChars(String s) {
StringBuilder sb = new StringBuilder(s.length());
- for (int x = 0; x < s.length(); x++) {
- char ch = s.charAt(x);
- if (isExcluded(ch)) {
- byte[] bytes =
Character.toString(ch).getBytes(StandardCharsets.UTF_8);
+ for (int x = 0; x < s.length();) {
+ int cp = s.codePointAt(x);
+ int len = Character.charCount(cp);
+ if (len > 1 || isExcluded((char)cp)) {
+ byte[] bytes = s.substring(x, x +
len).getBytes(StandardCharsets.UTF_8);
for (byte b : bytes) {
sb.append('%');
StringUtils.byteToHex(b, sb);
}
} else {
- sb.append(ch);
+ sb.append((char)cp);
}
+ x += len;
}
return sb.toString();
}
diff --git
a/core/src/test/java/org/apache/cxf/common/util/URIParserUtilTest.java
b/core/src/test/java/org/apache/cxf/common/util/URIParserUtilTest.java
index d6a9fb190f7..e94bb683010 100644
--- a/core/src/test/java/org/apache/cxf/common/util/URIParserUtilTest.java
+++ b/core/src/test/java/org/apache/cxf/common/util/URIParserUtilTest.java
@@ -124,4 +124,12 @@ public class URIParserUtilTest {
assertEquals(orig, URLDecoder.decode(s,
StandardCharsets.UTF_8.name()));
}
-}
\ No newline at end of file
+ @Test
+ public void testEscapeCharsSupplementaryCharacter() throws Exception {
+ String orig = "/foo" + new String(Character.toChars(0x1F600)) + ".txt";
+ String s = URIParserUtil.escapeChars(orig);
+ assertEquals("/foo%f0%9f%98%80.txt", s);
+ assertEquals(orig, URLDecoder.decode(s,
StandardCharsets.UTF_8.name()));
+ }
+
+}