Skip to content

Commit 128689e

Browse files
committed
Use entrySet iterator in getBodyFromServletRequestParameters
Closes gh-27081
1 parent 475396b commit 128689e

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -244,9 +244,10 @@ private static InputStream getBodyFromServletRequestParameters(HttpServletReques
244244
Writer writer = new OutputStreamWriter(bos, FORM_CHARSET);
245245

246246
Map<String, String[]> form = request.getParameterMap();
247-
for (Iterator<String> nameIterator = form.keySet().iterator(); nameIterator.hasNext();) {
248-
String name = nameIterator.next();
249-
List<String> values = Arrays.asList(form.get(name));
247+
for (Iterator<Map.Entry<String, String[]>> entryIterator = form.entrySet().iterator(); entryIterator.hasNext();) {
248+
Map.Entry<String, String[]> entry = entryIterator.next();
249+
String name = entry.getKey();
250+
List<String> values = Arrays.asList(entry.getValue());
250251
for (Iterator<String> valueIterator = values.iterator(); valueIterator.hasNext();) {
251252
String value = valueIterator.next();
252253
writer.write(URLEncoder.encode(name, FORM_CHARSET.name()));
@@ -258,7 +259,7 @@ private static InputStream getBodyFromServletRequestParameters(HttpServletReques
258259
}
259260
}
260261
}
261-
if (nameIterator.hasNext()) {
262+
if (entryIterator.hasNext()) {
262263
writer.append('&');
263264
}
264265
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,14 @@ void getFormBody() throws IOException {
175175
assertThat(result).as("Invalid content returned").isEqualTo(content);
176176
}
177177

178+
@Test
179+
void getEmptyFormBody() throws IOException {
180+
mockRequest.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
181+
mockRequest.setMethod("POST");
182+
183+
byte[] result = FileCopyUtils.copyToByteArray(request.getBody());
184+
byte[] content = "".getBytes(StandardCharsets.UTF_8);
185+
assertThat(result).as("Invalid content returned").isEqualTo(content);
186+
}
187+
178188
}

0 commit comments

Comments
 (0)