diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/NetworkUtils.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/NetworkUtils.java index cbd6e0a72..595e53631 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/NetworkUtils.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/NetworkUtils.java @@ -146,7 +146,10 @@ public final class NetworkUtils { boolean left = true; while (i < location.length()) { char ch = location.charAt(i); - if (ch == ' ' || ch >= 0x80) + if (ch == ' ' + || ch == '[' || ch == ']' + || ch == '{' || ch == '}' + || ch >= 0x80) break; else if (ch == '?') left = false; @@ -163,22 +166,6 @@ public final class NetworkUtils { for (; i < location.length(); i++) { char ch = location.charAt(i); - if (Character.isSurrogate(ch)) { - if (Character.isHighSurrogate(ch) && i < location.length() - 1) { - char ch2 = location.charAt(i + 1); - if (Character.isLowSurrogate(ch2)) { - int codePoint = Character.toCodePoint(ch, ch2); - encodeCodePoint(builder, codePoint); - i++; - continue; - } - } - - // Invalid surrogate pair, encode as '?' - builder.append("%3F"); - continue; - } - if (ch == ' ') { if (left) builder.append("%20"); @@ -187,7 +174,23 @@ public final class NetworkUtils { } else if (ch == '?') { left = false; builder.append('?'); - } else if (ch >= 0x80) { + } else if (ch >= 0x80 || (left && (ch == '[' || ch == ']' || ch == '{' || ch == '}'))) { + if (Character.isSurrogate(ch)) { + if (Character.isHighSurrogate(ch) && i < location.length() - 1) { + char ch2 = location.charAt(i + 1); + if (Character.isLowSurrogate(ch2)) { + int codePoint = Character.toCodePoint(ch, ch2); + encodeCodePoint(builder, codePoint); + i++; + continue; + } + } + + // Invalid surrogate pair, encode as '?' + builder.append("%3F"); + continue; + } + encodeCodePoint(builder, ch); } else { builder.append(ch); diff --git a/HMCLCore/src/test/java/org/jackhuang/hmcl/util/io/NetworkUtilsTest.java b/HMCLCore/src/test/java/org/jackhuang/hmcl/util/io/NetworkUtilsTest.java index 7d22b499b..24841c30a 100644 --- a/HMCLCore/src/test/java/org/jackhuang/hmcl/util/io/NetworkUtilsTest.java +++ b/HMCLCore/src/test/java/org/jackhuang/hmcl/util/io/NetworkUtilsTest.java @@ -36,9 +36,12 @@ public class NetworkUtilsTest { assertEquals("https://github.com/HMCL-dev/HMCL/commits?author=Glavo", encodeLocation("https://github.com/HMCL-dev/HMCL/commits?author=Glavo")); assertEquals("https://www.example.com/file%20with%20space", encodeLocation("https://www.example.com/file with space")); assertEquals("https://www.example.com/file%20with%20space", encodeLocation("https://www.example.com/file%20with%20space")); + assertEquals("https://www.example.com/%5Bfile%5D", encodeLocation("https://www.example.com/[file]")); + assertEquals("https://www.example.com/%7Bfile%7D", encodeLocation("https://www.example.com/{file}")); assertEquals("https://www.example.com/%E6%B5%8B%E8%AF%95", encodeLocation("https://www.example.com/测试")); assertEquals("https://www.example.com/%F0%9F%98%87", encodeLocation("https://www.example.com/\uD83D\uDE07")); assertEquals("https://www.example.com/test?a=10+20", encodeLocation("https://www.example.com/test?a=10 20")); + assertEquals("https://www.example.com/test?a=10+20&b=[30]&c={40}", encodeLocation("https://www.example.com/test?a=10 20&b=[30]&c={40}")); assertEquals("https://www.example.com/%E6%B5%8B%E8%AF%95?a=10+20", encodeLocation("https://www.example.com/测试?a=10 20")); // Invalid surrogate pair