Fix QueryStringEncoder encodes tilde (#11590)

Motivation:

In this issue(#11578 ) discussed that tilde should not be encoded in QueryStringEncoder, this pr is to fix this problem

Modification:

Modified QueryStringEncoder so that it does not encode tilde, and added a test case

Result:

Fixes #11578
This commit is contained in:
skyguard1 2021-08-27 03:53:07 +08:00 committed by NiteshKant
parent 445f747ce3
commit 3fc44b0a8e
3 changed files with 10 additions and 2 deletions

View File

@ -240,13 +240,13 @@ public class QueryStringEncoder {
* unreserved characters do not need to be encoded, and include uppercase and lowercase * unreserved characters do not need to be encoded, and include uppercase and lowercase
* letters, decimal digits, hyphen, period, underscore, and tilde. * letters, decimal digits, hyphen, period, underscore, and tilde.
* <p> * <p>
* unreserved = ALPHA / DIGIT / "-" / "_" / "." / "*" * unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" / "*"
* *
* @param ch the char to be judged whether it need to be encode * @param ch the char to be judged whether it need to be encode
* @return true or false * @return true or false
*/ */
private static boolean dontNeedEncoding(char ch) { private static boolean dontNeedEncoding(char ch) {
return ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch >= '0' && ch <= '9' return ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch >= '0' && ch <= '9'
|| ch == '-' || ch == '_' || ch == '.' || ch == '*'; || ch == '-' || ch == '_' || ch == '.' || ch == '*' || ch == '~';
} }
} }

View File

@ -105,6 +105,9 @@ public class QueryStringDecoderTest {
assertEquals(2, d.parameters().get("abc").size()); assertEquals(2, d.parameters().get("abc").size());
assertEquals("1 23", d.parameters().get("abc").get(0)); assertEquals("1 23", d.parameters().get("abc").get(0));
assertEquals("124 ", d.parameters().get("abc").get(1)); assertEquals("124 ", d.parameters().get("abc").get(1));
d = new QueryStringDecoder("/foo?abc=%7E");
assertEquals("~", d.parameters().get("abc").get(0));
} }
@Test @Test

View File

@ -56,6 +56,11 @@ public class QueryStringEncoderTest {
e.addParam("d", null); e.addParam("d", null);
assertEquals("/foo?a=1&b=&c&d", e.toString()); assertEquals("/foo?a=1&b=&c&d", e.toString());
assertEquals(new URI("/foo?a=1&b=&c&d"), e.toUri()); assertEquals(new URI("/foo?a=1&b=&c&d"), e.toUri());
e = new QueryStringEncoder("/foo");
e.addParam("test", "a~b");
assertEquals("/foo?test=a~b", e.toString());
assertEquals(new URI("/foo?test=a~b"), e.toUri());
} }
@Test @Test