File tree 2 files changed +21
-3
lines changed
main/java/org/springframework/http/server
test/java/org/springframework/http/server
2 files changed +21
-3
lines changed Original file line number Diff line number Diff line change 1
1
/*
2
- * Copyright 2002-2023 the original author or authors.
2
+ * Copyright 2002-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -242,7 +242,7 @@ private static boolean isFormPost(HttpServletRequest request) {
242
242
* from the body, which can fail if any other code has used the ServletRequest
243
243
* to access a parameter, thus causing the input stream to be "consumed".
244
244
*/
245
- private static InputStream getBodyFromServletRequestParameters (HttpServletRequest request ) throws IOException {
245
+ private InputStream getBodyFromServletRequestParameters (HttpServletRequest request ) throws IOException {
246
246
ByteArrayOutputStream bos = new ByteArrayOutputStream (1024 );
247
247
Writer writer = new OutputStreamWriter (bos , FORM_CHARSET );
248
248
@@ -268,7 +268,12 @@ private static InputStream getBodyFromServletRequestParameters(HttpServletReques
268
268
}
269
269
writer .flush ();
270
270
271
- return new ByteArrayInputStream (bos .toByteArray ());
271
+ byte [] bytes = bos .toByteArray ();
272
+ if (bytes .length > 0 && getHeaders ().containsKey (HttpHeaders .CONTENT_LENGTH )) {
273
+ getHeaders ().setContentLength (bytes .length );
274
+ }
275
+
276
+ return new ByteArrayInputStream (bytes );
272
277
}
273
278
274
279
}
Original file line number Diff line number Diff line change @@ -203,4 +203,17 @@ void getFormBodyWhenQueryParamsAlsoPresent() throws IOException {
203
203
assertThat (result ).as ("Invalid content returned" ).isEqualTo (content );
204
204
}
205
205
206
+ @ Test // gh-32471
207
+ void getFormBodyWhenNotEncodedCharactersPresent () throws IOException {
208
+ mockRequest .setContentType ("application/x-www-form-urlencoded; charset=UTF-8" );
209
+ mockRequest .setMethod ("POST" );
210
+ mockRequest .addParameter ("name" , "Test" );
211
+ mockRequest .addParameter ("lastName" , "Test@er" );
212
+ mockRequest .addHeader ("Content-Length" , 26 );
213
+
214
+ byte [] result = FileCopyUtils .copyToByteArray (request .getBody ());
215
+ assertThat (result ).isEqualTo ("name=Test&lastName=Test%40er" .getBytes (StandardCharsets .UTF_8 ));
216
+ assertThat (request .getHeaders ().getContentLength ()).isEqualTo (result .length );
217
+ }
218
+
206
219
}
You can’t perform that action at this time.
0 commit comments