Skip to content

Commit b979648

Browse files
committed
HttpEntity.EMPTY should be immutable
Fix GH-34806 Signed-off-by: Yanming Zhou <[email protected]>
1 parent 838b4d6 commit b979648

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

spring-web/src/main/java/org/springframework/http/HttpHeaders.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
* @author Josh Long
9393
* @author Sam Brannen
9494
* @author Simon Baslé
95+
* @author Yanming Zhou
9596
* @since 3.0
9697
*/
9798
public class HttpHeaders implements Serializable {
@@ -473,10 +474,7 @@ public HttpHeaders(HttpHeaders httpHeaders) {
473474
this.headers = CollectionUtils.toMultiValueMap(new LinkedCaseInsensitiveMap<>(8, Locale.ENGLISH));
474475
}
475476
else {
476-
while (httpHeaders.headers instanceof HttpHeaders wrapped) {
477-
httpHeaders = wrapped;
478-
}
479-
this.headers = httpHeaders.headers;
477+
this.headers = new LinkedMultiValueMap<>(httpHeaders.headers);
480478
}
481479
}
482480

spring-web/src/test/java/org/springframework/http/HttpEntityTests.java

+8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
/**
2626
* @author Arjen Poutsma
27+
* @author Yanming Zhou
2728
*/
2829
class HttpEntityTests {
2930

@@ -120,4 +121,11 @@ void requestEntity() {
120121
assertThat(requestEntity2).isEqualTo(requestEntity);
121122
}
122123

124+
@Test
125+
void emptyHttpEntityShouldBeImmutable() {
126+
HttpHeaders newHeaders = new HttpHeaders(HttpEntity.EMPTY.getHeaders());
127+
newHeaders.add("Authorization", "Bearer some-token");
128+
assertThat(HttpEntity.EMPTY.getHeaders().headerNames()).isEmpty();
129+
}
130+
123131
}

0 commit comments

Comments
 (0)