Avoid copying in DefaultServerHttpRequestBuilder

This commit avoids copying HTTP headers when mutating an HTTP request.
Instead, we're now unwrapping the `ReadOnlyHttpHeaders` (which is most
likely backed by the native request headers).

Issue: SPR-17250
This commit is contained in:
Brian Clozel 2018-10-09 22:58:00 +02:00
parent ce7278aaf4
commit f12c28e402
2 changed files with 14 additions and 2 deletions

View File

@ -1617,4 +1617,17 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
} }
} }
/**
* 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;
}
}
} }

View File

@ -72,8 +72,7 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
this.httpMethodValue = original.getMethodValue(); this.httpMethodValue = original.getMethodValue();
this.body = original.getBody(); this.body = original.getBody();
this.httpHeaders = new HttpHeaders(); this.httpHeaders = HttpHeaders.writableHttpHeaders(original.getHeaders());
copyMultiValueMap(original.getHeaders(), this.httpHeaders);
this.cookies = new LinkedMultiValueMap<>(original.getCookies().size()); this.cookies = new LinkedMultiValueMap<>(original.getCookies().size());
copyMultiValueMap(original.getCookies(), this.cookies); copyMultiValueMap(original.getCookies(), this.cookies);