diff --git a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java index 4756017d3691..ca4954d13b8d 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java +++ b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java @@ -1617,4 +1617,17 @@ public static HttpHeaders readOnlyHttpHeaders(HttpHeaders headers) { } } + /** + * Return a {@code HttpHeaders} object that can read and written to. + */ + public static HttpHeaders writableHttpHeaders(HttpHeaders headers) { + Assert.notNull(headers, "HttpHeaders must not be null"); + if (headers instanceof ReadOnlyHttpHeaders) { + return new HttpHeaders(headers.headers); + } + else { + return headers; + } + } + } diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/DefaultServerHttpRequestBuilder.java b/spring-web/src/main/java/org/springframework/http/server/reactive/DefaultServerHttpRequestBuilder.java index 66c38439db34..6d5b670d3209 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/DefaultServerHttpRequestBuilder.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/DefaultServerHttpRequestBuilder.java @@ -72,8 +72,7 @@ public DefaultServerHttpRequestBuilder(ServerHttpRequest original) { this.httpMethodValue = original.getMethodValue(); this.body = original.getBody(); - this.httpHeaders = new HttpHeaders(); - copyMultiValueMap(original.getHeaders(), this.httpHeaders); + this.httpHeaders = HttpHeaders.writableHttpHeaders(original.getHeaders()); this.cookies = new LinkedMultiValueMap<>(original.getCookies().size()); copyMultiValueMap(original.getCookies(), this.cookies);