diff --git a/changes.md b/changes.md index 1d319e5d76..82bb39b298 100644 --- a/changes.md +++ b/changes.md @@ -3,6 +3,13 @@ Changes log - 3.0 Milestone 3 (month-day-2026) - Breaking changes + - `new Tag()` and `new Tag(String)` now create a **strong** entity tag, as RFC 9110 section + 8.8.3 defines it (a tag is strong unless it carries the `W/` prefix): they created a weak + tag, so a server doing `representation.setTag(new Tag("v1"))` sent `ETag: W/"v1"`, which + can never match an `If-Match` or `If-Range` condition (strong comparison). The `ETag` + header written by such a server loses its `W/` prefix. A tag that is really weak must be + created with `new Tag(value, true)` or the new `Tag.weak(String)`; `Tag.strong(String)` is + the explicit counterpart (#1694). - `CacheDirective` no longer has the `digit` attribute: `isDigit()`, `setDigit(boolean)` and the constructor `CacheDirective(String, String, boolean)` are removed (use `new CacheDirective(name, value)`). The attribute had no effect on the header that is diff --git a/org.restlet/src/main/java/org/restlet/data/Conditions.java b/org.restlet/src/main/java/org/restlet/data/Conditions.java index 2e8ea053ca..c5ab0b4e5b 100644 --- a/org.restlet/src/main/java/org/restlet/data/Conditions.java +++ b/org.restlet/src/main/java/org/restlet/data/Conditions.java @@ -22,11 +22,10 @@ *
* The entity tags are compared as RFC 9110 section 13.1 requires: "If-Match" and "If-Range" use the * strong comparison ({@link Tag#strongMatch(Tag)}, a weak tag never matches), "If-None-Match" uses - * the weak comparison ({@link Tag#weakMatch(Tag)}). Note that {@code new Tag(String)} creates a - * weak tag: use {@code new Tag(String, false)} for a resource that clients must be able to - * update with "If-Match" or to resume with "If-Range". The dates have a precision of one second, - * and an "If-Modified-Since" date later than the current time is ignored (RFC 2616 section 14.25, - * kept although RFC 9110 does not require it). + * the weak comparison ({@link Tag#weakMatch(Tag)}). A tag created with {@code new Tag(String)} is + * strong; a weak tag ({@link Tag#weak(String)}) never matches an "If-Match" or "If-Range" + * condition. The dates have a precision of one second, and an "If-Modified-Since" date later than + * the current time is ignored (RFC 2616 section 14.25, kept although RFC 9110 does not require it). * * @see If-Match * @see *
- * Note that {@code new Tag(String)} creates a weak tag, which never matches an "If-Match" - * condition.
+ * Note that a weak tag ({@link Tag#weak(String)}) never matches an "If-Match" condition.
*
* The date rule compares the modification date with the {@link Response#getDate() date of the * response}. If the origin server sent an invalid "Date" header, the time of reception is used diff --git a/org.restlet/src/main/java/org/restlet/data/Tag.java b/org.restlet/src/main/java/org/restlet/data/Tag.java index 95f391b58f..8cf77030ba 100644 --- a/org.restlet/src/main/java/org/restlet/data/Tag.java +++ b/org.restlet/src/main/java/org/restlet/data/Tag.java @@ -63,6 +63,28 @@ public static Tag parse(String httpTag) { return result; } + /** + * Creates a strong tag: two representations with the same strong tag are identical byte for + * byte. + * + * @param opaqueTag The tag value. + * @return A new strong tag. + */ + public static Tag strong(String opaqueTag) { + return new Tag(opaqueTag, false); + } + + /** + * Creates a weak tag: two representations with the same weak tag are only semantically + * equivalent. A weak tag never matches an "If-Match" or "If-Range" condition. + * + * @param opaqueTag The tag value. + * @return A new weak tag. + */ + public static Tag weak(String opaqueTag) { + return new Tag(opaqueTag, true); + } + /** The name. */ private final String name; @@ -70,20 +92,24 @@ public static Tag parse(String httpTag) { private final boolean weak; /** - * Default constructor. The opaque tag is set to null, and the weakness indicator is set to - * true. + * Default constructor. The opaque tag is set to null, and the tag is strong (RFC 9110 section + * 8.8.3: an entity tag is strong unless it carries the "W/" prefix). */ public Tag() { - this(null, true); + this(null, false); } /** - * Constructor of weak tags. + * Constructor of strong tags. Use {@link #Tag(String, boolean)} or {@link #weak(String)} to + * create a weak tag.
+ *
+ * Note that before Restlet 3.0, this constructor created a weak tag, which never matches + * an "If-Match" or "If-Range" condition (strong comparison). * * @param opaqueTag The tag value. */ public Tag(String opaqueTag) { - this(opaqueTag, true); + this(opaqueTag, false); } /** diff --git a/org.restlet/src/test/java/org/restlet/data/ConditionsTestCase.java b/org.restlet/src/test/java/org/restlet/data/ConditionsTestCase.java index 8888208f0d..ab44481634 100644 --- a/org.restlet/src/test/java/org/restlet/data/ConditionsTestCase.java +++ b/org.restlet/src/test/java/org/restlet/data/ConditionsTestCase.java @@ -441,4 +441,13 @@ void getStatus_modifiedAfterIfModifiedSince_proceeds() { conditions.setModifiedSince(T0); assertNull(conditions.getStatus(Method.GET, true, null, AFTER)); } + + /** A tag built with the default constructor is strong, so "If-Match" can match it (#1694). */ + @Test + void getStatus_ifMatchWithTheDefaultTag_succeedsOnPut() { + Conditions conditions = new Conditions(); + conditions.setMatch(Collections.singletonList(new Tag("v1"))); + + assertNull(conditions.getStatus(Method.PUT, true, new Tag("v1"), null)); + } } diff --git a/org.restlet/src/test/java/org/restlet/data/TagTestCase.java b/org.restlet/src/test/java/org/restlet/data/TagTestCase.java index 59c00af6cc..a4d7653ce7 100644 --- a/org.restlet/src/test/java/org/restlet/data/TagTestCase.java +++ b/org.restlet/src/test/java/org/restlet/data/TagTestCase.java @@ -141,4 +141,38 @@ void hashSet_distinguishesWeakAndStrongTags() { assertTrue(tags.contains(new Tag("1", false))); assertFalse(tags.contains(WEAK_1)); } + + /** + * RFC 9110 section 8.8.3: an entity tag is strong unless it carries the "W/" prefix (#1694). + */ + @Test + void newTag_isStrongByDefault() { + assertFalse(new Tag("v1").isWeak()); + assertFalse(new Tag().isWeak()); + assertEquals("\"v1\"", new Tag("v1").format()); + assertTrue(new Tag("v1").strongMatch(new Tag("v1"))); + } + + @Test + void explicitWeakness_isKept() { + assertTrue(new Tag("v1", true).isWeak()); + assertFalse(new Tag("v1", false).isWeak()); + assertEquals("W/\"v1\"", new Tag("v1", true).format()); + } + + @Test + void factories_createStrongAndWeakTags() { + assertFalse(Tag.strong("v1").isWeak()); + assertTrue(Tag.weak("v1").isWeak()); + assertEquals(new Tag("v1", false), Tag.strong("v1")); + assertEquals(new Tag("v1", true), Tag.weak("v1")); + assertFalse(Tag.strong("v1").strongMatch(Tag.weak("v1"))); + assertTrue(Tag.strong("v1").weakMatch(Tag.weak("v1"))); + } + + @Test + void parse_andFormat_roundTripWithTheDefaultTag() { + assertEquals(new Tag("v1"), Tag.parse(new Tag("v1").format())); + assertEquals(Tag.weak("v1"), Tag.parse(Tag.weak("v1").format())); + } } diff --git a/org.restlet/src/test/java/org/restlet/engine/connector/StrongTagWireTestCase.java b/org.restlet/src/test/java/org/restlet/engine/connector/StrongTagWireTestCase.java new file mode 100644 index 0000000000..67ebc6a0e5 --- /dev/null +++ b/org.restlet/src/test/java/org/restlet/engine/connector/StrongTagWireTestCase.java @@ -0,0 +1,110 @@ +/** + * Copyright 2005-2026 Qlik + *

+ * The content of this file is subject to the terms of the Apache 2.0 open + * source license available at https://www.opensource.org/licenses/apache-2.0 + *

+ * Restlet is a registered trademark of QlikTech International AB. + */ +package org.restlet.engine.connector; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.Socket; +import java.nio.charset.StandardCharsets; +import java.util.Locale; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.restlet.Context; +import org.restlet.Server; +import org.restlet.data.Protocol; +import org.restlet.data.Tag; +import org.restlet.engine.Engine; +import org.restlet.representation.Representation; +import org.restlet.representation.StringRepresentation; +import org.restlet.resource.Finder; +import org.restlet.resource.Get; +import org.restlet.resource.Put; +import org.restlet.resource.ServerResource; + +/** + * Tests, on the wire, that a tag created with {@code new Tag(String)} is written as a strong entity + * tag, so that the concurrency control of "If-Match" works (RFC 9110 sections 8.8.3 and 13.1.1, + * #1694). + */ +class StrongTagWireTestCase { + + private static final int TIMEOUT_MS = 10_000; + + /** Resource whose representation has a tag made with the default constructor. */ + public static class VersionedResource extends ServerResource { + + @Get + public Representation read() { + StringRepresentation result = new StringRepresentation("body"); + result.setTag(new Tag("v1")); + return result; + } + + @Put + public void replace(Representation entity) { + // Accepted. + } + } + + private Server server; + + @BeforeEach + void setUp() throws Exception { + Engine.clearThreadLocalVariables(); + Engine.register(); + Engine.getInstance().getRegisteredServers().add(new HttpServerHelper(null)); + + Context context = new Context(); + server = + new Server(context, Protocol.HTTP, 0, new Finder(context, VersionedResource.class)); + server.start(); + } + + @AfterEach + void tearDown() throws Exception { + if (server != null) { + server.stop(); + } + Engine.clearThreadLocalVariables(); + } + + private String send(String method, String extraHeaders) throws IOException { + try (Socket socket = new Socket("localhost", server.getActualPort())) { + socket.setSoTimeout(TIMEOUT_MS); + OutputStream out = socket.getOutputStream(); + out.write( + (method + + " / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 4\r\n" + + "Content-Type: text/plain\r\n" + + extraHeaders + + "Connection: close\r\n\r\nbody") + .getBytes(StandardCharsets.ISO_8859_1)); + out.flush(); + return new String(socket.getInputStream().readAllBytes(), StandardCharsets.ISO_8859_1); + } + } + + @Test + void theEtagHeader_ofADefaultTag_isStrong() throws Exception { + String response = send("GET", ""); + + String lower = response.toLowerCase(Locale.ROOT); + assertTrue(lower.contains("\r\netag: \"v1\"\r\n"), response); + } + + @Test + void ifMatch_withTheTagOfADefaultTag_letsTheWriteThrough() throws Exception { + String response = send("PUT", "If-Match: \"v1\"\r\n"); + + assertTrue(response.startsWith("HTTP/1.1 2"), response); + } +}