fix domain encode bug

This commit is contained in:
Looly 2019-12-05 10:01:25 +08:00
parent affcc57598
commit 41b578391b
5 changed files with 24 additions and 29 deletions

View File

@ -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
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------

View File

@ -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++) { for (CharSequence str : strs) {
append(strs[i]); 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) { throw new OutOfMemoryError("Capacity is too long and max than Integer.MAX");
// overflow
throw new OutOfMemoryError("Capacity is too long and max than Integer.MAX");
}
newCapacity = Integer.MAX_VALUE;
} }
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++) { for (CharSequence str : strs) {
totalLength += (null == strs[i] ? 4 : strs[i].length()); totalLength += (null == str ? 4 : str.length());
} }
return totalLength; return totalLength;
} }

View File

@ -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);
} }
// 去除开头的\或者/ if(StrUtil.isNotEmpty(body)){
body = body.replaceAll("^[\\\\/]+", StrUtil.EMPTY); // 去除开头的\或者/
// 替换多个\/为单个/ //noinspection ConstantConditions
body = body.replace("\\", "/").replaceAll("//+", "/"); 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);
} }
} }

View File

@ -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);
} }

View File

@ -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);
}
} }