Skip to content

Commit 8fa428f

Browse files
committed
Skip reconstructing body from request params if query present
This allows restoring optimization in StringHttpMessageConverter that was undone in 23162b for 6.0.x. Closes gh-31327
1 parent a6ab636 commit 8fa428f

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

spring-web/src/main/java/org/springframework/http/converter/StringHttpMessageConverter.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ public boolean supports(Class<?> clazz) {
9393
@Override
9494
protected String readInternal(Class<? extends String> clazz, HttpInputMessage inputMessage) throws IOException {
9595
Charset charset = getContentTypeCharset(inputMessage.getHeaders().getContentType());
96-
return StreamUtils.copyToString(inputMessage.getBody(), charset);
96+
long length = inputMessage.getHeaders().getContentLength();
97+
byte[] bytes = (length >= 0 && length <= Integer.MAX_VALUE ?
98+
inputMessage.getBody().readNBytes((int) length) :
99+
inputMessage.getBody().readAllBytes());
100+
return new String(bytes, charset);
97101
}
98102

99103
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public InetSocketAddress getRemoteAddress() {
209209

210210
@Override
211211
public InputStream getBody() throws IOException {
212-
if (isFormPost(this.servletRequest)) {
212+
if (isFormPost(this.servletRequest) && this.servletRequest.getQueryString() == null) {
213213
return getBodyFromServletRequestParameters(this.servletRequest);
214214
}
215215
else {

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -167,18 +167,16 @@ void getBody() throws IOException {
167167
assertThat(result).as("Invalid content returned").isEqualTo(content);
168168
}
169169

170-
@Test
170+
@Test // gh-13318
171171
void getFormBody() throws IOException {
172-
// Charset (SPR-8676)
173172
mockRequest.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
174173
mockRequest.setMethod("POST");
175174
mockRequest.addParameter("name 1", "value 1");
176175
mockRequest.addParameter("name 2", "value 2+1", "value 2+2");
177176
mockRequest.addParameter("name 3", (String) null);
178177

179178
byte[] result = FileCopyUtils.copyToByteArray(request.getBody());
180-
byte[] content = "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3".getBytes(
181-
StandardCharsets.UTF_8);
179+
byte[] content = "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3".getBytes(StandardCharsets.UTF_8);
182180
assertThat(result).as("Invalid content returned").isEqualTo(content);
183181
}
184182

@@ -192,4 +190,18 @@ void getEmptyFormBody() throws IOException {
192190
assertThat(result).as("Invalid content returned").isEqualTo(content);
193191
}
194192

193+
@Test // gh-31327
194+
void getFormBodyWhenQueryParamsAlsoPresent() throws IOException {
195+
mockRequest.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
196+
mockRequest.setMethod("POST");
197+
mockRequest.setQueryString("q=1");
198+
mockRequest.addParameter("q", "1");
199+
mockRequest.setContent("foo=bar".getBytes(StandardCharsets.UTF_8));
200+
mockRequest.addHeader("Content-Length", 7);
201+
202+
byte[] result = FileCopyUtils.copyToByteArray(request.getBody());
203+
byte[] content = "foo=bar".getBytes(StandardCharsets.UTF_8);
204+
assertThat(result).as("Invalid content returned").isEqualTo(content);
205+
}
206+
195207
}

0 commit comments

Comments
 (0)