浏览代码

fix domain encode bug

Looly 6 年之前
父节点
当前提交
41b578391b

+ 1 - 0
CHANGELOG.md

@@ -45,6 +45,7 @@
 * 【core】       修复ZipUtil解压目录遗留问题(issue#I14NO3@Gitee)
 * 【core】       修复ZipUtil解压目录遗留问题(issue#I14NO3@Gitee)
 * 【core】       修复等比缩放给定背景色无效问题(pr#625@Github)
 * 【core】       修复等比缩放给定背景色无效问题(pr#625@Github)
 * 【poi 】       修复sax方式读取excel中无样式表导致的空指针问题
 * 【poi 】       修复sax方式读取excel中无样式表导致的空指针问题
+* 【core】       修复标准化URL时domain被转义的问题(pr#654@Github)
 
 
 -------------------------------------------------------------------------------------------------------------
 -------------------------------------------------------------------------------------------------------------
 
 

+ 6 - 9
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) {
 	public StrBuilder(CharSequence... strs) {
 		this(ArrayUtil.isEmpty(strs) ? DEFAULT_CAPACITY : (totalLength(strs) + DEFAULT_CAPACITY));
 		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
 	// ------------------------------------------------------------------------------------ Constructor end
@@ -449,6 +449,7 @@ public class StrBuilder implements CharSequence, Appendable, Serializable {
 	/**
 	/**
 	 * 生成字符串
 	 * 生成字符串
 	 */
 	 */
+	@SuppressWarnings("NullableProblems")
 	@Override
 	@Override
 	public String toString() {
 	public String toString() {
 		return toString(false);
 		return toString(false);
@@ -536,11 +537,7 @@ public class StrBuilder implements CharSequence, Appendable, Serializable {
 			newCapacity = minimumCapacity;
 			newCapacity = minimumCapacity;
 		}
 		}
 		if (newCapacity < 0) {
 		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);
 		value = Arrays.copyOf(value, newCapacity);
 	}
 	}
@@ -555,8 +552,8 @@ public class StrBuilder implements CharSequence, Appendable, Serializable {
 	 */
 	 */
 	private static int totalLength(CharSequence... strs) {
 	private static int totalLength(CharSequence... strs) {
 		int totalLength = 0;
 		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;
 		return totalLength;
 	}
 	}

+ 15 - 12
hutool-core/src/main/java/cn/hutool/core/util/URLUtil.java

@@ -637,22 +637,22 @@ public class URLUtil {
 	 * </pre>
 	 * </pre>
 	 *
 	 *
 	 * @param url URL字符串
 	 * @param url URL字符串
-	 * @param isEncodeBody 是否对URL中body部分的中文和特殊字符做转义(不包括 http:, /和域名部分)
+	 * @param isEncodePath 是否对URL中path部分的中文和特殊字符做转义(不包括 http:, /和域名部分)
 	 * @return 标准化后的URL字符串
 	 * @return 标准化后的URL字符串
 	 * @since 4.4.1
 	 * @since 4.4.1
 	 */
 	 */
-	public static String normalize(String url, boolean isEncodeBody) {
+	public static String normalize(String url, boolean isEncodePath) {
 		if (StrUtil.isBlank(url)) {
 		if (StrUtil.isBlank(url)) {
 			return url;
 			return url;
 		}
 		}
 		final int sepIndex = url.indexOf("://");
 		final int sepIndex = url.indexOf("://");
-		String pre;
+		String protocol;
 		String body;
 		String body;
 		if (sepIndex > 0) {
 		if (sepIndex > 0) {
-			pre = StrUtil.subPre(url, sepIndex + 3);
+			protocol = StrUtil.subPre(url, sepIndex + 3);
 			body = StrUtil.subSuf(url, sepIndex + 3);
 			body = StrUtil.subSuf(url, sepIndex + 3);
 		} else {
 		} else {
-			pre = "http://";
+			protocol = "http://";
 			body = url;
 			body = url;
 		}
 		}
 
 
@@ -663,21 +663,24 @@ public class URLUtil {
 			body = StrUtil.subPre(body, paramsSepIndex);
 			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, '/');
 		final int pathSepIndex = StrUtil.indexOf(body, '/');
 		String domain = body;
 		String domain = body;
-		String path = "";
+		String path = null;
 		if (pathSepIndex > 0) {
 		if (pathSepIndex > 0) {
 			domain = StrUtil.subPre(body, pathSepIndex);
 			domain = StrUtil.subPre(body, pathSepIndex);
 			path = StrUtil.subSuf(body, pathSepIndex);
 			path = StrUtil.subSuf(body, pathSepIndex);
 		}
 		}
-		if (isEncodeBody) {
+		if (isEncodePath) {
 			path = encode(path);
 			path = encode(path);
 		}
 		}
-		return pre + domain + path + StrUtil.nullToEmpty(params);
+		return protocol + domain + StrUtil.nullToEmpty(path) + StrUtil.nullToEmpty(params);
 	}
 	}
 }
 }

+ 1 - 1
hutool-core/src/test/java/cn/hutool/core/util/URLUtilTest.java

@@ -55,7 +55,7 @@ public class URLUtilTest {
 	@Test
 	@Test
 	public void normalizeIpv6Test() {
 	public void normalizeIpv6Test() {
 		String url = "http://[fe80::8f8:2022:a603:d180]:9439";
 		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);
 		Assert.assertEquals(url, normalize);
 	}
 	}
 	
 	

+ 1 - 7
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";
 		String paramsStr = "uuuu=0&a=b&c=3Ddsssss555555";
 		Map<String, List<String>> map = HttpUtil.decodeParams(paramsStr, CharsetUtil.UTF_8);
 		Map<String, List<String>> map = HttpUtil.decodeParams(paramsStr, CharsetUtil.UTF_8);
 
 
-		String encodedParams = HttpUtil.toParams((Map<String, List<String>>) map);
+		String encodedParams = HttpUtil.toParams(map);
 		Assert.assertEquals(paramsStr, encodedParams);
 		Assert.assertEquals(paramsStr, encodedParams);
 	}
 	}
 
 
@@ -274,10 +274,4 @@ public class HttpUtilTest {
 		String mimeType = HttpUtil.getMimeType("aaa.aaa");
 		String mimeType = HttpUtil.getMimeType("aaa.aaa");
 		Assert.assertNull(mimeType);
 		Assert.assertNull(mimeType);
 	}
 	}
-
-	@Test
-	public void ipv6Test() {
-		String result = HttpUtil.get("http://[fe80::8f8:2022:a603:d180]:9439");
-		Console.log(result);
-	}
 }
 }