mirror of
https://gitee.com/dromara/hutool.git
synced 2026-02-09 09:16:26 +08:00
修复HttpUtil.normalizeParams 在极端输入下抛 StringIndexOutOfBoundsException(pr#4216@Github)
This commit is contained in:
@@ -18,6 +18,7 @@ package cn.hutool.v7.core.net.url;
|
||||
|
||||
import cn.hutool.v7.core.convert.ConvertUtil;
|
||||
import cn.hutool.v7.core.map.MapUtil;
|
||||
import cn.hutool.v7.core.text.CharUtil;
|
||||
import cn.hutool.v7.core.text.StrUtil;
|
||||
import cn.hutool.v7.core.util.CharsetUtil;
|
||||
|
||||
@@ -178,9 +179,12 @@ public class UrlQueryUtil {
|
||||
}
|
||||
|
||||
// 以&结尾则去除之
|
||||
final int lastIndex = builder.length() - 1;
|
||||
if ('&' == builder.charAt(lastIndex)) {
|
||||
builder.delete(lastIndex, builder.length());
|
||||
if (!builder.isEmpty()) {
|
||||
final int lastIndex = builder.length() - 1;
|
||||
if (CharUtil.AMP == builder.charAt(lastIndex)) {
|
||||
//builder.delete(lastIndex, builder.length());
|
||||
builder.deleteCharAt(lastIndex);
|
||||
}
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
@@ -166,4 +166,10 @@ public class UrlQueryUtilTest {
|
||||
final String encodeResult = UrlQueryUtil.normalizeQuery("", CharsetUtil.UTF_8);
|
||||
Assertions.assertEquals("", encodeResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalizeQueryEndWithAmpersand(){
|
||||
final String encodeResult = UrlQueryUtil.normalizeQuery("参数&", CharsetUtil.UTF_8);
|
||||
Assertions.assertEquals("%E5%8F%82%E6%95%B0=", encodeResult);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user