Skip to content

Commit f12c28e

Browse files
committedOct 11, 2018
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
1 parent ce7278a commit f12c28e

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed
 

Diff for: ‎spring-web/src/main/java/org/springframework/http/HttpHeaders.java

+13
Original file line numberDiff line numberDiff line change
@@ -1617,4 +1617,17 @@ public static HttpHeaders readOnlyHttpHeaders(HttpHeaders headers) {
16171617
}
16181618
}
16191619

1620+
/**
1621+
* Return a {@code HttpHeaders} object that can read and written to.
1622+
*/
1623+
public static HttpHeaders writableHttpHeaders(HttpHeaders headers) {
1624+
Assert.notNull(headers, "HttpHeaders must not be null");
1625+
if (headers instanceof ReadOnlyHttpHeaders) {
1626+
return new HttpHeaders(headers.headers);
1627+
}
1628+
else {
1629+
return headers;
1630+
}
1631+
}
1632+
16201633
}

Diff for: ‎spring-web/src/main/java/org/springframework/http/server/reactive/DefaultServerHttpRequestBuilder.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ public DefaultServerHttpRequestBuilder(ServerHttpRequest original) {
7272
this.httpMethodValue = original.getMethodValue();
7373
this.body = original.getBody();
7474

75-
this.httpHeaders = new HttpHeaders();
76-
copyMultiValueMap(original.getHeaders(), this.httpHeaders);
75+
this.httpHeaders = HttpHeaders.writableHttpHeaders(original.getHeaders());
7776

7877
this.cookies = new LinkedMultiValueMap<>(original.getCookies().size());
7978
copyMultiValueMap(original.getCookies(), this.cookies);

0 commit comments

Comments
 (0)
Please sign in to comment.