Browse Source

Merge pull request #654 from sukaiyi/v5-dev

issue[651]: URLUtil.normalize support ipv6
Golden Looly 6 years ago
parent
commit
affcc57598

+ 11 - 3
hutool-core/src/main/java/cn/hutool/core/util/URLUtil.java

@@ -637,7 +637,7 @@ public class URLUtil {
 	 * </pre>
 	 *
 	 * @param url URL字符串
-	 * @param isEncodeBody 是否对URL中body部分的中文和特殊字符做转义(不包括http:和/
+	 * @param isEncodeBody 是否对URL中body部分的中文和特殊字符做转义(不包括 http:, /和域名部分
 	 * @return 标准化后的URL字符串
 	 * @since 4.4.1
 	 */
@@ -667,9 +667,17 @@ public class URLUtil {
 		body = body.replaceAll("^[\\\\/]+", StrUtil.EMPTY);
 		// 替换多个\或/为单个/
 		body = body.replace("\\", "/").replaceAll("//+", "/");
+
+		final int pathSepIndex = StrUtil.indexOf(body, '/');
+		String domain = body;
+		String path = "";
+		if (pathSepIndex > 0) {
+			domain = StrUtil.subPre(body, pathSepIndex);
+			path = StrUtil.subSuf(body, pathSepIndex);
+		}
 		if (isEncodeBody) {
-			body = encode(body);
+			path = encode(path);
 		}
-		return pre + body + StrUtil.nullToEmpty(params);
+		return pre + domain + path + StrUtil.nullToEmpty(params);
 	}
 }

+ 7 - 1
hutool-http/src/test/java/cn/hutool/http/test/HttpUtilTest.java

@@ -71,7 +71,7 @@ public class HttpUtilTest {
 		FileUtil.writeBytes(str, "f:/test/2D.jpg");
 		Console.log(str);
 	}
-	
+
 	@Test
 	@Ignore
 	public void get12306Test() {
@@ -274,4 +274,10 @@ 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);
+	}
 }