UrlQuery增加setStrict方法,区分是否严格模式

This commit is contained in:
Looly 2023-05-28 12:21:03 +08:00
parent bcc21df50a
commit 18e73eaa3b
4 changed files with 45 additions and 1 deletions

View File

@ -2,9 +2,10 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.8.20(2023-05-27)
# 5.8.20(2023-05-28)
### 🐣新特性
* 【core 】 UrlQuery增加setStrict方法区分是否严格模式issue#I78PB1@Gitee
### 🐞Bug修复

View File

@ -69,12 +69,26 @@ public class RFC3986 {
*/
public static final PercentCodec QUERY_PARAM_VALUE = PercentCodec.of(QUERY).removeSafe('&');
/**
* query中的value编码器严格模式value中不能包含任何分隔符
*
* @since 6.0.0
*/
public static final PercentCodec QUERY_PARAM_VALUE_STRICT = UNRESERVED;
/**
* query中的key<br>
* key不能包含"{@code &}" "="
*/
public static final PercentCodec QUERY_PARAM_NAME = PercentCodec.of(QUERY_PARAM_VALUE).removeSafe('=');
/**
* query中的key编码器严格模式key中不能包含任何分隔符
*
* @since 6.0.0
*/
public static final PercentCodec QUERY_PARAM_NAME_STRICT = UNRESERVED;
/**
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
*

View File

@ -33,6 +33,10 @@ public class UrlQuery {
* 是否为x-www-form-urlencoded模式此模式下空格会编码为'+'
*/
private final boolean isFormUrlEncoded;
/**
* 是否严格模式严格模式下query的name和value中均不允许有分隔符
*/
private boolean isStrict;
/**
* 构建UrlQuery
@ -136,6 +140,17 @@ public class UrlQuery {
this.isFormUrlEncoded = isFormUrlEncoded;
}
/**
* 设置是否严格模式
* @param strict 是否严格模式
* @return this
* @since 5.8.20
*/
public UrlQuery setStrict(final boolean strict) {
isStrict = strict;
return this;
}
/**
* 增加键值对
*
@ -254,6 +269,9 @@ public class UrlQuery {
return build(FormUrlencoded.ALL, FormUrlencoded.ALL, charset, encodePercent);
}
if (isStrict) {
return build(RFC3986.QUERY_PARAM_NAME_STRICT, RFC3986.QUERY_PARAM_VALUE_STRICT, charset, encodePercent);
}
return build(RFC3986.QUERY_PARAM_NAME, RFC3986.QUERY_PARAM_VALUE, charset, encodePercent);
}

View File

@ -144,4 +144,15 @@ public class UrlQueryTest {
final UrlQuery query = UrlQuery.of(queryStr, null);
Assert.assertEquals(queryStr, query.toString());
}
@Test
public void issueI78PB1Test() {
// 严格模式
final UrlQuery query = new UrlQuery().setStrict(true);
query.add(":/?#[]@!$&'()*+,;= ", ":/?#[]@!$&'()*+,;= ");
final String string = query.build(CharsetUtil.CHARSET_UTF_8);
Assert.assertEquals("%3A%2F%3F%23%5B%5D%40%21%24%26%27%28%29%2A%2B%2C%3B%3D%20=" +
"%3A%2F%3F%23%5B%5D%40%21%24%26%27%28%29%2A%2B%2C%3B%3D%20", string);
}
}