Fix #5590 QueryStringDecoder#path should decode the path info

Motivation:

Currently, QueryStringDecoder#path simply returns the path info as is, without decoding it as the Javadoc states.

Modifications:

* Make QueryStringDecoder#path decode the path info.
* Add tests to QueryStringDecoderTest.

Result:

QueryStringDecoder#path now decodes the path info as expected.
This commit is contained in:
Ngoc Dao 2016-07-27 15:19:10 +10:00 committed by Norman Maurer
parent 2bc902e71b
commit b548579a3d
2 changed files with 28 additions and 13 deletions

View File

@ -178,14 +178,10 @@ public class QueryStringDecoder {
public String path() { public String path() {
if (path == null) { if (path == null) {
if (!hasPath) { if (!hasPath) {
return path = ""; path = "";
}
int pathEndPos = uri.indexOf('?');
if (pathEndPos < 0) {
path = uri;
} else { } else {
return path = uri.substring(0, pathEndPos); int pathEndPos = uri.indexOf('?');
path = decodeComponent(pathEndPos < 0 ? uri : uri.substring(0, pathEndPos), this.charset);
} }
} }
return path; return path;
@ -197,16 +193,18 @@ public class QueryStringDecoder {
public Map<String, List<String>> parameters() { public Map<String, List<String>> parameters() {
if (params == null) { if (params == null) {
if (hasPath) { if (hasPath) {
int pathLength = path().length(); int pathEndPos = uri.indexOf('?');
if (uri.length() == pathLength) { if (pathEndPos >= 0 && pathEndPos < uri.length() - 1) {
return Collections.emptyMap(); decodeParams(uri.substring(pathEndPos + 1));
} else {
params = Collections.emptyMap();
} }
decodeParams(uri.substring(pathLength + 1));
} else { } else {
if (uri.isEmpty()) { if (uri.isEmpty()) {
return Collections.emptyMap(); params = Collections.emptyMap();
} else {
decodeParams(uri);
} }
decodeParams(uri);
} }
} }
return params; return params;

View File

@ -38,6 +38,14 @@ public class QueryStringDecoderTest {
public void testBasic() throws Exception { public void testBasic() throws Exception {
QueryStringDecoder d; QueryStringDecoder d;
d = new QueryStringDecoder("/foo");
Assert.assertEquals("/foo", d.path());
Assert.assertEquals(0, d.parameters().size());
d = new QueryStringDecoder("/foo%20bar");
Assert.assertEquals("/foo bar", d.path());
Assert.assertEquals(0, d.parameters().size());
d = new QueryStringDecoder("/foo?a=b=c"); d = new QueryStringDecoder("/foo?a=b=c");
Assert.assertEquals("/foo", d.path()); Assert.assertEquals("/foo", d.path());
Assert.assertEquals(1, d.parameters().size()); Assert.assertEquals(1, d.parameters().size());
@ -51,6 +59,13 @@ public class QueryStringDecoderTest {
Assert.assertEquals("1", d.parameters().get("a").get(0)); Assert.assertEquals("1", d.parameters().get("a").get(0));
Assert.assertEquals("2", d.parameters().get("a").get(1)); Assert.assertEquals("2", d.parameters().get("a").get(1));
d = new QueryStringDecoder("/foo%20bar?a=1&a=2");
Assert.assertEquals("/foo bar", d.path());
Assert.assertEquals(1, d.parameters().size());
Assert.assertEquals(2, d.parameters().get("a").size());
Assert.assertEquals("1", d.parameters().get("a").get(0));
Assert.assertEquals("2", d.parameters().get("a").get(1));
d = new QueryStringDecoder("/foo?a=&a=2"); d = new QueryStringDecoder("/foo?a=&a=2");
Assert.assertEquals("/foo", d.path()); Assert.assertEquals("/foo", d.path());
Assert.assertEquals(1, d.parameters().size()); Assert.assertEquals(1, d.parameters().size());
@ -85,6 +100,8 @@ public class QueryStringDecoderTest {
public void testExotic() throws Exception { public void testExotic() throws Exception {
assertQueryString("", ""); assertQueryString("", "");
assertQueryString("foo", "foo"); assertQueryString("foo", "foo");
assertQueryString("foo", "foo?");
assertQueryString("/foo", "/foo?");
assertQueryString("/foo", "/foo"); assertQueryString("/foo", "/foo");
assertQueryString("?a=", "?a"); assertQueryString("?a=", "?a");
assertQueryString("foo?a=", "foo?a"); assertQueryString("foo?a=", "foo?a");