Skip to content

Commit 645e349

Browse files
committed
Use URLDecoder for query params in WebFlux
Issue: SPR-15860
1 parent 8b7a670 commit 645e349

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

spring-web/src/main/java/org/springframework/http/server/reactive/AbstractServerHttpRequest.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616

1717
package org.springframework.http.server.reactive;
1818

19+
import java.io.UnsupportedEncodingException;
1920
import java.net.URI;
20-
import java.nio.charset.StandardCharsets;
21+
import java.net.URLDecoder;
2122
import java.util.regex.Matcher;
2223
import java.util.regex.Pattern;
2324

25+
import org.apache.commons.logging.Log;
26+
import org.apache.commons.logging.LogFactory;
27+
2428
import org.springframework.http.HttpCookie;
2529
import org.springframework.http.HttpHeaders;
2630
import org.springframework.http.server.RequestPath;
@@ -38,6 +42,8 @@
3842
*/
3943
public abstract class AbstractServerHttpRequest implements ServerHttpRequest {
4044

45+
private static final Log logger = LogFactory.getLog(ServerHttpRequest.class);
46+
4147
private static final Pattern QUERY_PATTERN = Pattern.compile("([^&=]+)(=?)([^&]+)?");
4248

4349

@@ -113,8 +119,18 @@ protected MultiValueMap<String, String> initQueryParams() {
113119
return queryParams;
114120
}
115121

122+
@SuppressWarnings("deprecation")
116123
private String decodeQueryParam(String value) {
117-
return StringUtils.uriDecode(value, StandardCharsets.UTF_8);
124+
try {
125+
return URLDecoder.decode(value, "UTF-8");
126+
}
127+
catch (UnsupportedEncodingException ex) {
128+
if (logger.isWarnEnabled()) {
129+
logger.warn("Could not decode query param [" + value + "] as 'UTF-8'. " +
130+
"Falling back on default encoding; exception message: " + ex.getMessage());
131+
}
132+
return URLDecoder.decode(value);
133+
}
118134
}
119135

120136
@Override

spring-web/src/test/java/org/springframework/http/server/reactive/ServerHttpRequestTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -68,7 +68,7 @@ public void queryParamsWithMulitpleValues() throws Exception {
6868
public void queryParamsWithEncodedValue() throws Exception {
6969
MultiValueMap<String, String> params = createHttpRequest("/path?a=%20%2B+%C3%A0").getQueryParams();
7070
assertEquals(1, params.size());
71-
assertEquals(Collections.singletonList(" ++\u00e0"), params.get("a"));
71+
assertEquals(Collections.singletonList(" + \u00e0"), params.get("a"));
7272
}
7373

7474
@Test

spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestMappingIntegrationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void handleWithParam() throws Exception {
6363

6464
@Test // SPR-15140
6565
public void handleWithEncodedParam() throws Exception {
66-
String expected = "Hello ++\u00e0!";
66+
String expected = "Hello + \u00e0!";
6767
assertEquals(expected, performGet("/param?name=%20%2B+%C3%A0", new HttpHeaders(), String.class).getBody());
6868
}
6969

0 commit comments

Comments
 (0)