fix decode

This commit is contained in:
Looly 2023-04-18 12:24:15 +08:00
parent 549523db6c
commit 91de866ffa
3 changed files with 19 additions and 5 deletions

View File

@ -1,5 +1,6 @@
package cn.hutool.core.net; package cn.hutool.core.net;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.CharUtil; import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
@ -87,6 +88,11 @@ public class URLDecoder implements Serializable {
char c; char c;
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
c = str.charAt(i); c = str.charAt(i);
if('+' == c){
result.append(isPlusToSpace ? CharUtil.SPACE : c);
begin++;
continue;
}
if(ESCAPE_CHAR == c || CharUtil.isHexChar(c)){ if(ESCAPE_CHAR == c || CharUtil.isHexChar(c)){
continue; continue;
} }

View File

@ -14,6 +14,12 @@ public class UrlDecoderTest {
Assert.assertEquals("+", URLDecoder.decodeForPath("+", CharsetUtil.CHARSET_UTF_8)); Assert.assertEquals("+", URLDecoder.decodeForPath("+", CharsetUtil.CHARSET_UTF_8));
} }
@Test
public void decodePlusTest() {
final String decode = URLDecoder.decode("+", CharsetUtil.CHARSET_UTF_8);
Assert.assertEquals(" ", decode);
}
@Test @Test
public void issue3063Test() throws UnsupportedEncodingException { public void issue3063Test() throws UnsupportedEncodingException {
// https://github.com/dromara/hutool/issues/3063 // https://github.com/dromara/hutool/issues/3063

View File

@ -383,17 +383,19 @@ public class HttpUtilTest {
@Test @Test
public void httpParameterDecodeTest () { public void httpParameterDecodeTest () {
String test = "this is test测试"; final String test = "this is test测试";
int port = NetUtil.getUsableLocalPort(); final int port = NetUtil.getUsableLocalPort();
HttpUtil.createServer(port) HttpUtil.createServer(port)
.addAction("/formEncoded", (req, resp) -> resp.write(req.getParam("test"))) .addAction("/formEncoded", (req, resp) -> resp.write(req.getParam("test")))
.addAction("/urlEncoded", (req, resp) -> resp.write(req.getParam("test"))) .addAction("/urlEncoded", (req, resp) -> resp.write(req.getParam("test")))
.start(); .start();
String resp = HttpUtil.createPost(String.format("http://localhost:%s/formEncoded", port))
final String resp = HttpUtil.createPost(String.format("http://localhost:%s/formEncoded", port))
.form("test", test).execute().body(); .form("test", test).execute().body();
Assert.assertEquals("Form请求参数解码", test, resp); Assert.assertEquals("Form请求参数解码", test, resp);
String urlGet = UrlBuilder.of(String.format("http://localhost:%s/urlEncoded", port)).addQuery("test", test).build();
String resp2 = HttpUtil.createGet(urlGet).execute().body(); final String urlGet = UrlBuilder.of(String.format("http://localhost:%s/urlEncoded", port)).addQuery("test", test).build();
final String resp2 = HttpUtil.createGet(urlGet).execute().body();
Assert.assertEquals("QueryString请求参数编码", test, resp2); Assert.assertEquals("QueryString请求参数编码", test, resp2);
} }
} }