Skip to content

Commit

Permalink
Avoid copying in DefaultServerHttpRequestBuilder
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bclozel committed Oct 11, 2018
1 parent ce7278a commit f12c28e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
13 changes: 13 additions & 0 deletions spring-web/src/main/java/org/springframework/http/HttpHeaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f12c28e

Please sign in to comment.