issue[651]: URLUtil.normalize support ipv6

This commit is contained in:
sukaiyi
2019-12-04 18:11:52 +08:00
parent e69965a707
commit 98ebf5f6e2
2 changed files with 19 additions and 5 deletions

View File

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

View File

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