Skip to content

Commit 3dd4a83

Browse files
committed
Update more places to use quoteETagIfNecessary
Closes gh-33412
1 parent 1b26122 commit 3dd4a83

File tree

3 files changed

+21
-38
lines changed

3 files changed

+21
-38
lines changed

spring-web/src/main/java/org/springframework/http/HttpHeaders.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,11 +1063,11 @@ public long getDate() {
10631063
/**
10641064
* Set the (new) entity tag of the body, as specified by the {@code ETag} header.
10651065
*/
1066-
public void setETag(@Nullable String etag) {
1067-
if (etag != null) {
1068-
Assert.isTrue(etag.startsWith("\"") || etag.startsWith("W/\""), "ETag does not start with W/\" or \"");
1069-
Assert.isTrue(etag.endsWith("\""), "ETag does not end with \"");
1070-
set(ETAG, etag);
1066+
public void setETag(@Nullable String eTag) {
1067+
if (eTag != null) {
1068+
Assert.isTrue(eTag.startsWith("\"") || eTag.startsWith("W/\""), "ETag does not start with W/\" or \"");
1069+
Assert.isTrue(eTag.endsWith("\""), "ETag does not end with \"");
1070+
set(ETAG, eTag);
10711071
}
10721072
else {
10731073
remove(ETAG);

spring-web/src/main/java/org/springframework/web/context/request/ServletWebRequest.java

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -247,23 +247,25 @@ private boolean validateIfNoneMatch(@Nullable String etag) {
247247
return true;
248248
}
249249

250-
private boolean matchRequestedETags(Enumeration<String> requestedETags, @Nullable String etag, boolean weakCompare) {
251-
etag = padEtagIfNecessary(etag);
250+
private boolean matchRequestedETags(Enumeration<String> requestedETags, @Nullable String eTag, boolean weakCompare) {
251+
if (StringUtils.hasLength(eTag)) {
252+
eTag = ETag.quoteETagIfNecessary(eTag);
253+
}
252254
while (requestedETags.hasMoreElements()) {
253255
// Compare weak/strong ETags as per https://datatracker.ietf.org/doc/html/rfc9110#section-8.8.3
254256
for (ETag requestedETag : ETag.parse(requestedETags.nextElement())) {
255257
// only consider "lost updates" checks for unsafe HTTP methods
256-
if (requestedETag.isWildcard() && StringUtils.hasLength(etag)
258+
if (requestedETag.isWildcard() && StringUtils.hasLength(eTag)
257259
&& !SAFE_METHODS.contains(getRequest().getMethod())) {
258260
return false;
259261
}
260262
if (weakCompare) {
261-
if (etagWeakMatch(etag, requestedETag.formattedTag())) {
263+
if (etagWeakMatch(eTag, requestedETag.formattedTag())) {
262264
return false;
263265
}
264266
}
265267
else {
266-
if (etagStrongMatch(etag, requestedETag.formattedTag())) {
268+
if (etagStrongMatch(eTag, requestedETag.formattedTag())) {
267269
return false;
268270
}
269271
}
@@ -272,17 +274,6 @@ private boolean matchRequestedETags(Enumeration<String> requestedETags, @Nullabl
272274
return true;
273275
}
274276

275-
@Nullable
276-
private String padEtagIfNecessary(@Nullable String etag) {
277-
if (!StringUtils.hasLength(etag)) {
278-
return etag;
279-
}
280-
if ((etag.startsWith("\"") || etag.startsWith("W/\"")) && etag.endsWith("\"")) {
281-
return etag;
282-
}
283-
return "\"" + etag + "\"";
284-
}
285-
286277
private boolean etagStrongMatch(@Nullable String first, @Nullable String second) {
287278
if (!StringUtils.hasLength(first) || first.startsWith("W/")) {
288279
return false;
@@ -346,13 +337,13 @@ private void updateResponseIdempotent(@Nullable String etag, long lastModifiedTi
346337
}
347338
}
348339

349-
private void addCachingResponseHeaders(@Nullable String etag, long lastModifiedTimestamp) {
340+
private void addCachingResponseHeaders(@Nullable String eTag, long lastModifiedTimestamp) {
350341
if (getResponse() != null && SAFE_METHODS.contains(getRequest().getMethod())) {
351342
if (lastModifiedTimestamp > 0 && parseDateValue(getResponse().getHeader(HttpHeaders.LAST_MODIFIED)) == -1) {
352343
getResponse().setDateHeader(HttpHeaders.LAST_MODIFIED, lastModifiedTimestamp);
353344
}
354-
if (StringUtils.hasLength(etag) && getResponse().getHeader(HttpHeaders.ETAG) == null) {
355-
getResponse().setHeader(HttpHeaders.ETAG, padEtagIfNecessary(etag));
345+
if (StringUtils.hasLength(eTag) && getResponse().getHeader(HttpHeaders.ETAG) == null) {
346+
getResponse().setHeader(HttpHeaders.ETAG, ETag.quoteETagIfNecessary(eTag));
356347
}
357348
}
358349
}

spring-web/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@
3333
import org.springframework.context.i18n.LocaleContext;
3434
import org.springframework.core.ResolvableType;
3535
import org.springframework.core.codec.Hints;
36+
import org.springframework.http.ETag;
3637
import org.springframework.http.HttpHeaders;
3738
import org.springframework.http.HttpMethod;
3839
import org.springframework.http.HttpStatus;
@@ -341,7 +342,9 @@ private boolean validateIfMatch(@Nullable String eTag) {
341342
}
342343

343344
private boolean matchRequestedETags(List<String> requestedETags, @Nullable String eTag, boolean weakCompare) {
344-
eTag = padEtagIfNecessary(eTag);
345+
if (StringUtils.hasLength(eTag)) {
346+
eTag = ETag.quoteETagIfNecessary(eTag);
347+
}
345348
for (String clientEtag : requestedETags) {
346349
// only consider "lost updates" checks for unsafe HTTP methods
347350
if ("*".equals(clientEtag) && StringUtils.hasLength(eTag)
@@ -363,17 +366,6 @@ private boolean matchRequestedETags(List<String> requestedETags, @Nullable Strin
363366
return true;
364367
}
365368

366-
@Nullable
367-
private String padEtagIfNecessary(@Nullable String etag) {
368-
if (!StringUtils.hasLength(etag)) {
369-
return etag;
370-
}
371-
if ((etag.startsWith("\"") || etag.startsWith("W/\"")) && etag.endsWith("\"")) {
372-
return etag;
373-
}
374-
return "\"" + etag + "\"";
375-
}
376-
377369
private boolean eTagStrongMatch(@Nullable String first, @Nullable String second) {
378370
if (!StringUtils.hasLength(first) || first.startsWith("W/")) {
379371
return false;
@@ -431,7 +423,7 @@ private void addCachingResponseHeaders(@Nullable String eTag, Instant lastModifi
431423
getResponseHeaders().setLastModified(lastModified.toEpochMilli());
432424
}
433425
if (StringUtils.hasLength(eTag) && getResponseHeaders().getETag() == null) {
434-
getResponseHeaders().setETag(padEtagIfNecessary(eTag));
426+
getResponseHeaders().setETag(ETag.quoteETagIfNecessary(eTag));
435427
}
436428
}
437429
}

0 commit comments

Comments
 (0)