From 8d34a4d26eefb86bebfdad59a143dedaf1d4feef Mon Sep 17 00:00:00 2001 From: Looly Date: Fri, 7 Mar 2025 09:26:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D`HttpUtil.normalizeParams`?= =?UTF-8?q?=E8=A7=84=E5=88=99=E9=97=AE=E9=A2=98=EF=BC=88issue#IBQIYQ@Gitee?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 ++- .../main/java/cn/hutool/http/HttpUtil.java | 20 ++++++++----------- .../java/cn/hutool/http/IssueIBQIYQTest.java | 20 +++++++++++++++++++ 3 files changed, 30 insertions(+), 13 deletions(-) create mode 100644 hutool-http/src/test/java/cn/hutool/http/IssueIBQIYQTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 497f80fc9..d3e1054a5 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ # 🚀Changelog ------------------------------------------------------------------------------------------------------------- -# 5.8.37(2025-03-03) +# 5.8.37(2025-03-07) ### 🐣新特性 * 【json 】 ObjectMapper删除重复trim(pr#3859@Github) @@ -16,6 +16,7 @@ * 【setting】 修复`SettingLoader`load未抛出异常导致配置文件无法正常遍历的问题(pr#3868@Github) * 【cache 】 修复`ReentrantCache#getOrRemoveExpired`方法丢失onRemove触发问题(pr#1315@Gitee) * 【json 】 修复`JsonUtil.toBean`泛型数组类型丢失问题(pr#3876@Github) +* 【http 】 修复`HttpUtil.normalizeParams`规则问题(issue#IBQIYQ@Gitee) ------------------------------------------------------------------------------------------------------------- # 5.8.36(2025-02-18) diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java b/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java index de88d1630..10093e61b 100755 --- a/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java @@ -9,11 +9,7 @@ import cn.hutool.core.map.MapUtil; import cn.hutool.core.net.RFC3986; import cn.hutool.core.net.url.UrlQuery; import cn.hutool.core.text.StrBuilder; -import cn.hutool.core.util.CharsetUtil; -import cn.hutool.core.util.ObjectUtil; -import cn.hutool.core.util.ReUtil; -import cn.hutool.core.util.StrUtil; -import cn.hutool.core.util.URLUtil; +import cn.hutool.core.util.*; import cn.hutool.http.cookie.GlobalCookieManager; import cn.hutool.http.server.SimpleServer; @@ -557,17 +553,17 @@ public class HttpUtil { pos = i + 1; } } else if (c == '&') { // 参数对的分界点 - if (pos != i) { - if (null == name) { - // 对于像&a&这类无参数值的字符串,我们将name为a的值设为"" + if (null == name) { + // 对于像&a&这类无参数值的字符串,我们将name为a的值设为"" + if(pos != i){ name = paramPart.substring(pos, i); builder.append(RFC3986.QUERY_PARAM_NAME.encode(name, charset)).append('='); - } else { - builder.append(RFC3986.QUERY_PARAM_NAME.encode(name, charset)).append('=') - .append(RFC3986.QUERY_PARAM_VALUE.encode(paramPart.substring(pos, i), charset)).append('&'); } - name = null; + } else { + builder.append(RFC3986.QUERY_PARAM_NAME.encode(name, charset)).append('=') + .append(RFC3986.QUERY_PARAM_VALUE.encode(paramPart.substring(pos, i), charset)).append('&'); } + name = null; pos = i + 1; } } diff --git a/hutool-http/src/test/java/cn/hutool/http/IssueIBQIYQTest.java b/hutool-http/src/test/java/cn/hutool/http/IssueIBQIYQTest.java new file mode 100644 index 000000000..239070d20 --- /dev/null +++ b/hutool-http/src/test/java/cn/hutool/http/IssueIBQIYQTest.java @@ -0,0 +1,20 @@ +package cn.hutool.http; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.Map; + +public class IssueIBQIYQTest { + @Test + void normalizeParamsTest1() { + Map map = new HashMap<>(); + map.put("id", ""); + map.put("type", "4"); + String url = HttpUtil.toParams(map); + Assertions.assertEquals("id=&type=4", url); + url = HttpUtil.normalizeParams(url, null); + Assertions.assertEquals("id=&type=4", url); + } +}