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

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

View File

@@ -637,22 +637,22 @@ public class URLUtil {
* </pre>
*
* @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);
}
}

View File

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