Skip to content

Commit 97515ba

Browse files
committed
Introduce MvcUtils#decodeQueryString method
Signed-off-by: Yike Xiao <[email protected]>
1 parent cd4f6ce commit 97515ba

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/common/MvcUtils.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.io.InputStream;
2222
import java.io.UncheckedIOException;
2323
import java.net.URI;
24+
import java.net.URLDecoder;
25+
import java.nio.charset.Charset;
2426
import java.nio.charset.StandardCharsets;
2527
import java.util.Arrays;
2628
import java.util.Collection;
@@ -44,6 +46,7 @@
4446
import org.springframework.util.LinkedMultiValueMap;
4547
import org.springframework.util.MultiValueMap;
4648
import org.springframework.util.StreamUtils;
49+
import org.springframework.util.StringUtils;
4750
import org.springframework.web.context.WebApplicationContext;
4851
import org.springframework.web.servlet.function.ServerRequest;
4952
import org.springframework.web.servlet.support.RequestContextUtils;
@@ -283,15 +286,21 @@ public static MultiValueMap<String, String> encodeQueryParams(MultiValueMap<Stri
283286
return CollectionUtils.unmodifiableMultiValueMap(encodedQueryParams);
284287
}
285288

286-
public static MultiValueMap<String, String> decodeQueryParams(MultiValueMap<String, String> params) {
287-
MultiValueMap<String, String> decodedQueryParams = new LinkedMultiValueMap<>(params.size());
288-
for (Map.Entry<String, List<String>> entry : params.entrySet()) {
289-
for (String value : entry.getValue()) {
290-
decodedQueryParams.add(UriUtils.decode(entry.getKey(), StandardCharsets.UTF_8),
291-
UriUtils.decode(value, StandardCharsets.UTF_8));
289+
public static MultiValueMap<String, String> decodeQueryString(String queryString, Charset charset) {
290+
String[] pairs = StringUtils.tokenizeToStringArray(queryString, "&");
291+
MultiValueMap<String, String> result = new LinkedMultiValueMap<>(pairs.length);
292+
for (String pair : pairs) {
293+
int idx = pair.indexOf('=');
294+
if (idx == -1) {
295+
result.add(URLDecoder.decode(pair, charset), null);
296+
}
297+
else {
298+
String name = URLDecoder.decode(pair.substring(0, idx), charset);
299+
String value = URLDecoder.decode(pair.substring(idx + 1), charset);
300+
result.add(name, value);
292301
}
293302
}
294-
return CollectionUtils.unmodifiableMultiValueMap(decodedQueryParams);
303+
return result;
295304
}
296305

297306
private record ByteArrayInputMessage(ServerRequest request, ByteArrayInputStream body) implements HttpInputMessage {

spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/FormFilter.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
import org.springframework.lang.Nullable;
5050
import org.springframework.util.MultiValueMap;
5151
import org.springframework.util.StringUtils;
52-
import org.springframework.web.util.UriComponentsBuilder;
5352

5453
/**
5554
* Filter that rebuilds the body for form urlencoded posts. Serlvets treat query
@@ -114,14 +113,7 @@ static HttpServletRequest getRequestWithBodyFromRequestParameters(HttpServletReq
114113
Writer writer = new OutputStreamWriter(bos, FORM_CHARSET);
115114

116115
Map<String, String[]> form = request.getParameterMap();
117-
String queryString = request.getQueryString();
118-
StringBuffer requestURL = request.getRequestURL();
119-
if (StringUtils.hasText(queryString)) {
120-
requestURL.append('?').append(queryString);
121-
}
122-
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(requestURL.toString());
123-
MultiValueMap<String, String> queryParams = uriComponentsBuilder.build().getQueryParams();
124-
queryParams = MvcUtils.decodeQueryParams(queryParams);
116+
MultiValueMap<String, String> queryParams = MvcUtils.decodeQueryString(request.getQueryString(), FORM_CHARSET);
125117
for (Iterator<Map.Entry<String, String[]>> entryIterator = form.entrySet().iterator(); entryIterator
126118
.hasNext();) {
127119
Map.Entry<String, String[]> entry = entryIterator.next();

0 commit comments

Comments
 (0)