Avoided repeated creation of ReadOnlyHttpHeaders wrapper
See gh-24680
This commit is contained in:
@@ -1769,7 +1769,9 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
|
||||
|
||||
|
||||
/**
|
||||
* Apply a read-only {@code HttpHeaders} wrapper around the given headers.
|
||||
* Apply a read-only {@code HttpHeaders} wrapper around the given headers
|
||||
* that also caches the parsed representations of the "Accept" and
|
||||
* "Content-Type" headers.
|
||||
*/
|
||||
public static HttpHeaders readOnlyHttpHeaders(MultiValueMap<String, String> headers) {
|
||||
Assert.notNull(headers, "HttpHeaders must not be null");
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -20,6 +20,7 @@ import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
@@ -35,10 +36,22 @@ public abstract class AbstractClientHttpRequest implements ClientHttpRequest {
|
||||
|
||||
private boolean executed = false;
|
||||
|
||||
@Nullable
|
||||
private HttpHeaders readOnlyHeaders;
|
||||
|
||||
|
||||
@Override
|
||||
public final HttpHeaders getHeaders() {
|
||||
return (this.executed ? HttpHeaders.readOnlyHttpHeaders(this.headers) : this.headers);
|
||||
if (this.readOnlyHeaders != null) {
|
||||
return this.readOnlyHeaders;
|
||||
}
|
||||
else if (this.executed) {
|
||||
this.readOnlyHeaders = HttpHeaders.readOnlyHttpHeaders(this.headers);
|
||||
return this.readOnlyHeaders;
|
||||
}
|
||||
else {
|
||||
return this.headers;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -60,6 +60,9 @@ public abstract class AbstractClientHttpRequest implements ClientHttpRequest {
|
||||
|
||||
private final List<Supplier<? extends Publisher<Void>>> commitActions = new ArrayList<>(4);
|
||||
|
||||
@Nullable
|
||||
private HttpHeaders readOnlyHeaders;
|
||||
|
||||
|
||||
public AbstractClientHttpRequest() {
|
||||
this(new HttpHeaders());
|
||||
@@ -74,10 +77,16 @@ public abstract class AbstractClientHttpRequest implements ClientHttpRequest {
|
||||
|
||||
@Override
|
||||
public HttpHeaders getHeaders() {
|
||||
if (State.COMMITTED.equals(this.state.get())) {
|
||||
return HttpHeaders.readOnlyHttpHeaders(this.headers);
|
||||
if (this.readOnlyHeaders != null) {
|
||||
return this.readOnlyHeaders;
|
||||
}
|
||||
else if (State.COMMITTED.equals(this.state.get())) {
|
||||
this.readOnlyHeaders = HttpHeaders.readOnlyHttpHeaders(this.headers);
|
||||
return this.readOnlyHeaders;
|
||||
}
|
||||
else {
|
||||
return this.headers;
|
||||
}
|
||||
return this.headers;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -47,6 +47,9 @@ public class ServletServerHttpResponse implements ServerHttpResponse {
|
||||
|
||||
private boolean bodyUsed = false;
|
||||
|
||||
@Nullable
|
||||
private HttpHeaders readOnlyHeaders;
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new instance of the ServletServerHttpResponse based on the given {@link HttpServletResponse}.
|
||||
@@ -74,7 +77,16 @@ public class ServletServerHttpResponse implements ServerHttpResponse {
|
||||
|
||||
@Override
|
||||
public HttpHeaders getHeaders() {
|
||||
return (this.headersWritten ? HttpHeaders.readOnlyHttpHeaders(this.headers) : this.headers);
|
||||
if (this.readOnlyHeaders != null) {
|
||||
return this.readOnlyHeaders;
|
||||
}
|
||||
else if (this.headersWritten) {
|
||||
this.readOnlyHeaders = HttpHeaders.readOnlyHttpHeaders(this.headers);
|
||||
return this.readOnlyHeaders;
|
||||
}
|
||||
else {
|
||||
return this.headers;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -75,6 +75,9 @@ public abstract class AbstractServerHttpResponse implements ServerHttpResponse {
|
||||
|
||||
private final List<Supplier<? extends Mono<Void>>> commitActions = new ArrayList<>(4);
|
||||
|
||||
@Nullable
|
||||
private HttpHeaders readOnlyHeaders;
|
||||
|
||||
|
||||
public AbstractServerHttpResponse(DataBufferFactory dataBufferFactory) {
|
||||
this(dataBufferFactory, new HttpHeaders());
|
||||
@@ -155,8 +158,16 @@ public abstract class AbstractServerHttpResponse implements ServerHttpResponse {
|
||||
|
||||
@Override
|
||||
public HttpHeaders getHeaders() {
|
||||
return (this.state.get() == State.COMMITTED ?
|
||||
HttpHeaders.readOnlyHttpHeaders(this.headers) : this.headers);
|
||||
if (this.readOnlyHeaders != null) {
|
||||
return this.readOnlyHeaders;
|
||||
}
|
||||
else if (this.state.get() == State.COMMITTED) {
|
||||
this.readOnlyHeaders = HttpHeaders.readOnlyHttpHeaders(this.headers);
|
||||
return this.readOnlyHeaders;
|
||||
}
|
||||
else {
|
||||
return this.headers;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user