Skip to content

Commit

Permalink
Skip reconstructing body from request params if query present
Browse files Browse the repository at this point in the history
This allows restoring optimization in StringHttpMessageConverter
that was undone in 23162b for 6.0.x.

Closes gh-31327
  • Loading branch information
rstoyanchev committed Sep 29, 2023
1 parent a6ab636 commit 8fa428f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ public boolean supports(Class<?> clazz) {
@Override
protected String readInternal(Class<? extends String> clazz, HttpInputMessage inputMessage) throws IOException {
Charset charset = getContentTypeCharset(inputMessage.getHeaders().getContentType());
return StreamUtils.copyToString(inputMessage.getBody(), charset);
long length = inputMessage.getHeaders().getContentLength();
byte[] bytes = (length >= 0 && length <= Integer.MAX_VALUE ?
inputMessage.getBody().readNBytes((int) length) :
inputMessage.getBody().readAllBytes());
return new String(bytes, charset);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public InetSocketAddress getRemoteAddress() {

@Override
public InputStream getBody() throws IOException {
if (isFormPost(this.servletRequest)) {
if (isFormPost(this.servletRequest) && this.servletRequest.getQueryString() == null) {
return getBodyFromServletRequestParameters(this.servletRequest);
}
else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -167,18 +167,16 @@ void getBody() throws IOException {
assertThat(result).as("Invalid content returned").isEqualTo(content);
}

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

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

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

@Test // gh-31327
void getFormBodyWhenQueryParamsAlsoPresent() throws IOException {
mockRequest.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
mockRequest.setMethod("POST");
mockRequest.setQueryString("q=1");
mockRequest.addParameter("q", "1");
mockRequest.setContent("foo=bar".getBytes(StandardCharsets.UTF_8));
mockRequest.addHeader("Content-Length", 7);

byte[] result = FileCopyUtils.copyToByteArray(request.getBody());
byte[] content = "foo=bar".getBytes(StandardCharsets.UTF_8);
assertThat(result).as("Invalid content returned").isEqualTo(content);
}

}

0 comments on commit 8fa428f

Please sign in to comment.