Skip to content

Commit

Permalink
Remove extra copy in WebClient headers/cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
yuzawa-san authored and rstoyanchev committed Mar 14, 2023
1 parent d18bcb3 commit cd8955f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,8 @@ public ResponseSpec exchange() {

private ClientRequest.Builder initRequestBuilder() {
ClientRequest.Builder builder = ClientRequest.create(this.httpMethod, initUri())
.headers(headers -> headers.addAll(initHeaders()))
.cookies(cookies -> cookies.addAll(initCookies()))
.headers(this::initHeaders)
.cookies(this::initCookies)
.attributes(attributes -> attributes.putAll(this.attributes));
if (this.httpRequestConsumer != null) {
builder.httpRequest(this.httpRequestConsumer);
Expand All @@ -383,28 +383,21 @@ private URI initUri() {
return (this.uri != null ? this.uri : uriBuilderFactory.expand(""));
}

private HttpHeaders initHeaders() {
if (CollectionUtils.isEmpty(defaultHeaders)) {
return this.headers;
private void initHeaders(HttpHeaders out) {
if (!CollectionUtils.isEmpty(defaultHeaders)) {
out.putAll(defaultHeaders);
}
if (!CollectionUtils.isEmpty(this.headers)) {
out.putAll(this.headers);
}
HttpHeaders result = new HttpHeaders();
result.putAll(defaultHeaders);
result.putAll(this.headers);
return result;
}

private MultiValueMap<String, String> initCookies() {
if (CollectionUtils.isEmpty(this.cookies)) {
return (defaultCookies != null ? defaultCookies : new LinkedMultiValueMap<>());
private void initCookies(MultiValueMap<String, String> out) {
if (!CollectionUtils.isEmpty(defaultCookies)) {
out.putAll(defaultCookies);
}
else if (CollectionUtils.isEmpty(defaultCookies)) {
return this.cookies;
}
else {
MultiValueMap<String, String> result = new LinkedMultiValueMap<>();
result.putAll(defaultCookies);
result.putAll(this.cookies);
return result;
if (!CollectionUtils.isEmpty(this.cookies)) {
out.putAll(this.cookies);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,6 @@ private HttpRequest createRequest() {
return new HttpRequest() {
private final URI uri = initUri();

private final HttpHeaders headers = initHeaders();

@Override
public HttpMethod getMethod() {
return httpMethod;
Expand All @@ -415,7 +413,9 @@ public URI getURI() {

@Override
public HttpHeaders getHeaders() {
return this.headers;
HttpHeaders headers = new HttpHeaders();
initHeaders(headers);
return headers;
}
};
}
Expand Down Expand Up @@ -488,8 +488,8 @@ private ClientRequest.Builder initRequestBuilder() {
defaultRequest.accept(this);
}
ClientRequest.Builder builder = ClientRequest.create(this.httpMethod, initUri())
.headers(headers -> headers.addAll(initHeaders()))
.cookies(cookies -> cookies.addAll(initCookies()))
.headers(this::initHeaders)
.cookies(this::initCookies)
.attributes(attributes -> attributes.putAll(this.attributes));
if (this.httpRequestConsumer != null) {
builder.httpRequest(this.httpRequestConsumer);
Expand All @@ -501,33 +501,21 @@ private URI initUri() {
return (this.uri != null ? this.uri : uriBuilderFactory.expand(""));
}

private HttpHeaders initHeaders() {
if (CollectionUtils.isEmpty(this.headers)) {
return (defaultHeaders != null ? defaultHeaders : new HttpHeaders());
}
else if (CollectionUtils.isEmpty(defaultHeaders)) {
return this.headers;
private void initHeaders(HttpHeaders out) {
if (!CollectionUtils.isEmpty(defaultHeaders)) {
out.putAll(defaultHeaders);
}
else {
HttpHeaders result = new HttpHeaders();
result.putAll(defaultHeaders);
result.putAll(this.headers);
return result;
if (!CollectionUtils.isEmpty(this.headers)) {
out.putAll(this.headers);
}
}

private MultiValueMap<String, String> initCookies() {
if (CollectionUtils.isEmpty(this.cookies)) {
return (defaultCookies != null ? defaultCookies : new LinkedMultiValueMap<>());
}
else if (CollectionUtils.isEmpty(defaultCookies)) {
return this.cookies;
private void initCookies(MultiValueMap<String, String> out) {
if (!CollectionUtils.isEmpty(defaultCookies)) {
out.putAll(defaultCookies);
}
else {
MultiValueMap<String, String> result = new LinkedMultiValueMap<>();
result.putAll(defaultCookies);
result.putAll(this.cookies);
return result;
if (!CollectionUtils.isEmpty(this.cookies)) {
out.putAll(this.cookies);
}
}
}
Expand Down

0 comments on commit cd8955f

Please sign in to comment.