Quote ETag if not quoted in HttpHeaders#setETag

This aligns HttpHeaders with other places like ServletWebRequest and
DefaultWebExchange where an ETag is accepted as input.

It also allows us to remove quoting from places that delegate to
HttpHeaders#setETag since it now does that internally.

Closes gh-33412
This commit is contained in:
rstoyanchev
2024-09-10 09:01:30 +01:00
parent 3dd4a8350a
commit 19700d07b1
9 changed files with 24 additions and 34 deletions

View File

@@ -1063,11 +1063,9 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
/**
* Set the (new) entity tag of the body, as specified by the {@code ETag} header.
*/
public void setETag(@Nullable String eTag) {
if (eTag != null) {
Assert.isTrue(eTag.startsWith("\"") || eTag.startsWith("W/\""), "ETag does not start with W/\" or \"");
Assert.isTrue(eTag.endsWith("\""), "ETag does not end with \"");
set(ETAG, eTag);
public void setETag(@Nullable String tag) {
if (tag != null) {
set(ETAG, ETag.quoteETagIfNecessary(tag));
}
else {
remove(ETAG);

View File

@@ -568,9 +568,8 @@ public class ResponseEntity<T> extends HttpEntity<T> {
}
@Override
public BodyBuilder eTag(@Nullable String eTag) {
eTag = (eTag != null ? ETag.quoteETagIfNecessary(eTag) : eTag);
this.headers.setETag(eTag);
public BodyBuilder eTag(@Nullable String tag) {
this.headers.setETag(tag);
return this;
}

View File

@@ -417,13 +417,13 @@ public class DefaultServerWebExchange implements ServerWebExchange {
addCachingResponseHeaders(eTag, lastModified);
}
private void addCachingResponseHeaders(@Nullable String eTag, Instant lastModified) {
private void addCachingResponseHeaders(@Nullable String tag, Instant lastModified) {
if (SAFE_METHODS.contains(getRequest().getMethod())) {
if (lastModified.isAfter(Instant.EPOCH) && getResponseHeaders().getLastModified() == -1) {
getResponseHeaders().setLastModified(lastModified.toEpochMilli());
}
if (StringUtils.hasLength(eTag) && getResponseHeaders().getETag() == null) {
getResponseHeaders().setETag(ETag.quoteETagIfNecessary(eTag));
if (StringUtils.hasLength(tag) && getResponseHeaders().getETag() == null) {
getResponseHeaders().setETag(tag);
}
}
}