Polishing and minor refactoring

Update checks whether quoting is needed to be more complete
than what we've used so far, making sure the there is both
opening and closing quotes independent of each other.

See gh-33412
This commit is contained in:
rstoyanchev
2024-09-09 18:19:59 +01:00
parent 80b264ba82
commit 1b26122e64
6 changed files with 69 additions and 33 deletions

View File

@@ -134,16 +134,27 @@ public record ETag(String tag, boolean weak) {
return result;
}
public static String format(String etag) {
if (!etag.startsWith("\"") && !etag.startsWith("W/\"")) {
etag = "\"" + etag;
/**
* Add quotes around the ETag value if not present already.
* @param tag the ETag value
* @return the resulting, quoted value
* @since 6.2
*/
public static String quoteETagIfNecessary(String tag) {
if (tag.startsWith("W/\"")) {
if (tag.length() > 3 && tag.endsWith("\"")) {
return tag;
}
}
if (!etag.endsWith("\"")) {
etag = etag + "\"";
else if (tag.startsWith("\"")) {
if (tag.length() > 1 && tag.endsWith("\"")) {
return tag;
}
}
return etag;
return ("\"" + tag + "\"");
}
private enum State {
BEFORE_QUOTES, IN_QUOTES, AFTER_QUOTES

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
@@ -568,11 +568,9 @@ public class ResponseEntity<T> extends HttpEntity<T> {
}
@Override
public BodyBuilder eTag(@Nullable String etag) {
if (etag != null) {
etag = ETag.format(etag);
}
this.headers.setETag(etag);
public BodyBuilder eTag(@Nullable String eTag) {
eTag = (eTag != null ? ETag.quoteETagIfNecessary(eTag) : eTag);
this.headers.setETag(eTag);
return this;
}