From 41b578391b654499b29cb66da38dd876a7166eb6 Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 5 Dec 2019 10:01:25 +0800 Subject: [PATCH] fix domain encode bug --- CHANGELOG.md | 1 + .../java/cn/hutool/core/text/StrBuilder.java | 15 +++++------ .../java/cn/hutool/core/util/URLUtil.java | 27 ++++++++++--------- .../java/cn/hutool/core/util/URLUtilTest.java | 2 +- .../cn/hutool/http/test/HttpUtilTest.java | 8 +----- 5 files changed, 24 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f9c5a09c..9245d7d8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,7 @@ * 【core】 修复ZipUtil解压目录遗留问题(issue#I14NO3@Gitee) * 【core】 修复等比缩放给定背景色无效问题(pr#625@Github) * 【poi 】 修复sax方式读取excel中无样式表导致的空指针问题 +* 【core】 修复标准化URL时domain被转义的问题(pr#654@Github) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-core/src/main/java/cn/hutool/core/text/StrBuilder.java b/hutool-core/src/main/java/cn/hutool/core/text/StrBuilder.java index 0684ac277..12267f3f3 100644 --- a/hutool-core/src/main/java/cn/hutool/core/text/StrBuilder.java +++ b/hutool-core/src/main/java/cn/hutool/core/text/StrBuilder.java @@ -86,8 +86,8 @@ public class StrBuilder implements CharSequence, Appendable, Serializable { */ public StrBuilder(CharSequence... strs) { this(ArrayUtil.isEmpty(strs) ? DEFAULT_CAPACITY : (totalLength(strs) + DEFAULT_CAPACITY)); - for (int i = 0; i < strs.length; i++) { - append(strs[i]); + for (CharSequence str : strs) { + append(str); } } // ------------------------------------------------------------------------------------ Constructor end @@ -449,6 +449,7 @@ public class StrBuilder implements CharSequence, Appendable, Serializable { /** * 生成字符串 */ + @SuppressWarnings("NullableProblems") @Override public String toString() { return toString(false); @@ -536,11 +537,7 @@ public class StrBuilder implements CharSequence, Appendable, Serializable { newCapacity = minimumCapacity; } if (newCapacity < 0) { - if (minimumCapacity < 0) { - // overflow - throw new OutOfMemoryError("Capacity is too long and max than Integer.MAX"); - } - newCapacity = Integer.MAX_VALUE; + throw new OutOfMemoryError("Capacity is too long and max than Integer.MAX"); } value = Arrays.copyOf(value, newCapacity); } @@ -555,8 +552,8 @@ public class StrBuilder implements CharSequence, Appendable, Serializable { */ private static int totalLength(CharSequence... strs) { int totalLength = 0; - for (int i = 0; i < strs.length; i++) { - totalLength += (null == strs[i] ? 4 : strs[i].length()); + for (CharSequence str : strs) { + totalLength += (null == str ? 4 : str.length()); } return totalLength; } diff --git a/hutool-core/src/main/java/cn/hutool/core/util/URLUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/URLUtil.java index 523606879..56e4ea049 100644 --- a/hutool-core/src/main/java/cn/hutool/core/util/URLUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/URLUtil.java @@ -637,22 +637,22 @@ public class URLUtil { * * * @param url URL字符串 - * @param isEncodeBody 是否对URL中body部分的中文和特殊字符做转义(不包括 http:, /和域名部分) + * @param isEncodePath 是否对URL中path部分的中文和特殊字符做转义(不包括 http:, /和域名部分) * @return 标准化后的URL字符串 * @since 4.4.1 */ - public static String normalize(String url, boolean isEncodeBody) { + public static String normalize(String url, boolean isEncodePath) { if (StrUtil.isBlank(url)) { return url; } final int sepIndex = url.indexOf("://"); - String pre; + String protocol; String body; if (sepIndex > 0) { - pre = StrUtil.subPre(url, sepIndex + 3); + protocol = StrUtil.subPre(url, sepIndex + 3); body = StrUtil.subSuf(url, sepIndex + 3); } else { - pre = "http://"; + protocol = "http://"; body = url; } @@ -663,21 +663,24 @@ public class URLUtil { body = StrUtil.subPre(body, paramsSepIndex); } - // 去除开头的\或者/ - body = body.replaceAll("^[\\\\/]+", StrUtil.EMPTY); - // 替换多个\或/为单个/ - body = body.replace("\\", "/").replaceAll("//+", "/"); + if(StrUtil.isNotEmpty(body)){ + // 去除开头的\或者/ + //noinspection ConstantConditions + body = body.replaceAll("^[\\\\/]+", StrUtil.EMPTY); + // 替换多个\或/为单个/ + body = body.replace("\\", "/").replaceAll("//+", "/"); + } final int pathSepIndex = StrUtil.indexOf(body, '/'); String domain = body; - String path = ""; + String path = null; if (pathSepIndex > 0) { domain = StrUtil.subPre(body, pathSepIndex); path = StrUtil.subSuf(body, pathSepIndex); } - if (isEncodeBody) { + if (isEncodePath) { path = encode(path); } - return pre + domain + path + StrUtil.nullToEmpty(params); + return protocol + domain + StrUtil.nullToEmpty(path) + StrUtil.nullToEmpty(params); } } \ No newline at end of file diff --git a/hutool-core/src/test/java/cn/hutool/core/util/URLUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/URLUtilTest.java index 94c568ecd..2e925c164 100644 --- a/hutool-core/src/test/java/cn/hutool/core/util/URLUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/util/URLUtilTest.java @@ -55,7 +55,7 @@ public class URLUtilTest { @Test public void normalizeIpv6Test() { String url = "http://[fe80::8f8:2022:a603:d180]:9439"; - String normalize = URLUtil.normalize("http://[fe80::8f8:2022:a603:d180]:9439", false); + String normalize = URLUtil.normalize("http://[fe80::8f8:2022:a603:d180]:9439", true); Assert.assertEquals(url, normalize); } diff --git a/hutool-http/src/test/java/cn/hutool/http/test/HttpUtilTest.java b/hutool-http/src/test/java/cn/hutool/http/test/HttpUtilTest.java index e86e0f313..28c0f030f 100644 --- a/hutool-http/src/test/java/cn/hutool/http/test/HttpUtilTest.java +++ b/hutool-http/src/test/java/cn/hutool/http/test/HttpUtilTest.java @@ -119,7 +119,7 @@ public class HttpUtilTest { String paramsStr = "uuuu=0&a=b&c=3Ddsssss555555"; Map> map = HttpUtil.decodeParams(paramsStr, CharsetUtil.UTF_8); - String encodedParams = HttpUtil.toParams((Map>) map); + String encodedParams = HttpUtil.toParams(map); Assert.assertEquals(paramsStr, encodedParams); } @@ -274,10 +274,4 @@ public class HttpUtilTest { String mimeType = HttpUtil.getMimeType("aaa.aaa"); Assert.assertNull(mimeType); } - - @Test - public void ipv6Test() { - String result = HttpUtil.get("http://[fe80::8f8:2022:a603:d180]:9439"); - Console.log(result); - } }