diff --git a/changes.md b/changes.md
index 06586bdb29..a0baaaaaea 100644
--- a/changes.md
+++ b/changes.md
@@ -103,6 +103,10 @@ Changes log
first scope outermost. `McpRequestContext.transportResponse()` gives the transport
response.
- Bugs fixed
+ - An "If-Range" header carrying a date is now honoured only if that date is a strong
+ validator, as RFC 9110 section 13.1.5 requires: a "Last-Modified" less than one second
+ older than the response is weak, so the range is ignored and the whole entity is sent
+ instead of a partial content (#1742).
- A HEAD request answered 304 (Not Modified) now carries the same headers as the GET
request: the validators and caching headers only, not the other entity headers such as
`Content-Type` and `Last-Modified` (RFC 9110, section 15.4.5) (#1738).
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 aa1890d15d..2e8ea053ca 100644
--- a/org.restlet/src/main/java/org/restlet/data/Conditions.java
+++ b/org.restlet/src/main/java/org/restlet/data/Conditions.java
@@ -81,7 +81,11 @@ public static Conditions ifAbsent() {
* updates within the same second could not be told apart.
*
* Note that {@code new Tag(String)} creates a weak tag, which never matches an "If-Match"
- * condition.
+ * 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
+ * (RFC 9110 section 6.6.1), so the rule is then only as reliable as the clock of the client.
*
* @param read The response to the request that read the resource, with its entity.
* @return The conditions of a request guarded against concurrent updates.
@@ -106,12 +110,8 @@ public static Conditions unchangedSince(Response read) {
return result;
}
- Date modified = entity.getModificationDate();
- Date responseDate = read.getDate();
-
- if (modified != null
- && responseDate != null
- && responseDate.getTime() - modified.getTime() >= 1000) {
+ if (DateUtils.isStrong(entity.getModificationDate(), read.getDate())) {
+ Date modified = entity.getModificationDate();
result.setUnmodifiedSince(modified);
return result;
}
@@ -229,7 +229,12 @@ public Status getRangeStatus(RepresentationInfo representationInfo) {
}
/**
- * Returns the range conditional status of an entity.
+ * Returns the range conditional status of an entity.
+ *
+ * RFC 9110 section 13.1.5: a tag must match with the strong comparison, and a date is honoured
+ * only if it is a strong validator, that is if the modification date is at least one second
+ * older than the response (section 8.8.2.2). A modification date that recent never satisfies
+ * the condition, so the range is ignored and the whole entity is sent.
*
* @param tag The tag of the entity.
* @param modificationDate The modification date of the entity.
@@ -248,8 +253,11 @@ public Status getRangeStatus(Tag tag, Date modificationDate) {
} else if (getRangeDate() != null
&& modificationDate != null
- && DateUtils.equals(getRangeDate(), modificationDate)) {
- // The dates of the headers have a precision of one second
+ && DateUtils.equals(getRangeDate(), modificationDate)
+ && DateUtils.isStrong(modificationDate, new Date())) {
+ // The dates of the headers have a precision of one second. RFC 9110 section 13.1.5: a
+ // date is honoured only if it is a strong validator (section 8.8.2.2). The "Date" of
+ // the response is written after this test and cannot be earlier than now.
result = Status.SUCCESS_OK;
}
diff --git a/org.restlet/src/main/java/org/restlet/engine/util/DateUtils.java b/org.restlet/src/main/java/org/restlet/engine/util/DateUtils.java
index 2ece51cb6c..0eb8041ce6 100644
--- a/org.restlet/src/main/java/org/restlet/engine/util/DateUtils.java
+++ b/org.restlet/src/main/java/org/restlet/engine/util/DateUtils.java
@@ -54,6 +54,23 @@ public final class DateUtils {
/** Remember the often used GMT time zone. */
private static final java.util.TimeZone TIMEZONE_GMT = java.util.TimeZone.getTimeZone("GMT");
+ /**
+ * Indicates if a modification date can be used as a strong validator (RFC 9110 section
+ * 8.8.2.2): it must be at least one second older than the reference date, generally the date of
+ * the message that carries it, otherwise the representation may still change within the same
+ * second without the date changing.
+ *
+ * @param modificationDate The modification date (a "Last-Modified" value).
+ * @param referenceDate The date of the message ("Date" value).
+ * @return True if both dates are known and the modification date is at least one second before
+ * the reference date.
+ */
+ public static boolean isStrong(final Date modificationDate, final Date referenceDate) {
+ return modificationDate != null
+ && referenceDate != null
+ && referenceDate.getTime() - modificationDate.getTime() >= 1000;
+ }
+
/**
* Compares two date with a precision of one second.
*
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 7fceccc9f3..8888208f0d 100644
--- a/org.restlet/src/test/java/org/restlet/data/ConditionsTestCase.java
+++ b/org.restlet/src/test/java/org/restlet/data/ConditionsTestCase.java
@@ -116,6 +116,24 @@ void getRangeStatus_matchingDate_returnsOk() {
assertEquals(Status.SUCCESS_OK, conditions.getRangeStatus(null, date));
}
+ /** RFC 9110 section 13.1.5: an {@code If-Range} date is honoured only if it is strong. */
+ @Test
+ void getRangeStatus_recentlyModified_dateIsWeakAndNeverMatches() {
+ Date justNow = new Date();
+ Conditions conditions = new Conditions();
+ conditions.setRangeDate(justNow);
+ assertEquals(
+ Status.CLIENT_ERROR_PRECONDITION_FAILED, conditions.getRangeStatus(null, justNow));
+ }
+
+ @Test
+ void getRangeStatus_modifiedSeveralSecondsAgo_dateIsStrongAndMatches() {
+ Date earlier = new Date(System.currentTimeMillis() - 3_600_000L);
+ Conditions conditions = new Conditions();
+ conditions.setRangeDate(earlier);
+ assertEquals(Status.SUCCESS_OK, conditions.getRangeStatus(null, earlier));
+ }
+
@Test
void getRangeStatus_noConditionsSet_returnsPreconditionFailed() {
Conditions conditions = new Conditions();
diff --git a/org.restlet/src/test/java/org/restlet/engine/application/RangeFilterTestCase.java b/org.restlet/src/test/java/org/restlet/engine/application/RangeFilterTestCase.java
index 93f95d68ad..61cca4be43 100644
--- a/org.restlet/src/test/java/org/restlet/engine/application/RangeFilterTestCase.java
+++ b/org.restlet/src/test/java/org/restlet/engine/application/RangeFilterTestCase.java
@@ -13,6 +13,7 @@
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import java.util.Date;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -320,4 +321,35 @@ void afterHandle_rangeOnGet_stillGivesPartialContent() {
assertEquals(Status.SUCCESS_PARTIAL_CONTENT, response.getStatus());
}
+
+ /**
+ * RFC 9110 section 13.1.5: a weak {@code If-Range} date is ignored, the whole entity is sent.
+ */
+ @Test
+ void afterHandle_ifRangeDateOfARecentModification_servesTheWholeEntity() {
+ Date justNow = new Date();
+ StringRepresentation entity = new StringRepresentation("0123456789");
+ entity.setModificationDate(justNow);
+ Response response = successResponse(Method.GET, entity);
+ response.getRequest().getRanges().add(new Range(0, 4));
+ response.getRequest().getConditions().setRangeDate(justNow);
+
+ filter.afterHandle(response.getRequest(), response);
+
+ assertEquals(Status.SUCCESS_OK, response.getStatus());
+ }
+
+ @Test
+ void afterHandle_ifRangeDateOfAnOldModification_givesPartialContent() {
+ Date earlier = new Date(System.currentTimeMillis() - 3_600_000L);
+ StringRepresentation entity = new StringRepresentation("0123456789");
+ entity.setModificationDate(earlier);
+ Response response = successResponse(Method.GET, entity);
+ response.getRequest().getRanges().add(new Range(0, 4));
+ response.getRequest().getConditions().setRangeDate(earlier);
+
+ filter.afterHandle(response.getRequest(), response);
+
+ assertEquals(Status.SUCCESS_PARTIAL_CONTENT, response.getStatus());
+ }
}
diff --git a/org.restlet/src/test/java/org/restlet/engine/application/RangeRequestEndToEndTestCase.java b/org.restlet/src/test/java/org/restlet/engine/application/RangeRequestEndToEndTestCase.java
index cd656f3b18..6ce1eafba7 100644
--- a/org.restlet/src/test/java/org/restlet/engine/application/RangeRequestEndToEndTestCase.java
+++ b/org.restlet/src/test/java/org/restlet/engine/application/RangeRequestEndToEndTestCase.java
@@ -16,6 +16,7 @@
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
+import java.util.Date;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -30,6 +31,7 @@
import org.restlet.data.MediaType;
import org.restlet.data.Protocol;
import org.restlet.engine.Engine;
+import org.restlet.engine.util.DateUtils;
import org.restlet.representation.StringRepresentation;
/**
@@ -47,6 +49,9 @@ class RangeRequestEndToEndTestCase {
private URI uri;
+ /** Modification date of the served entity, null for none. */
+ private volatile Date modified;
+
@BeforeEach
void setUpEach() throws Exception {
component = new Component();
@@ -60,9 +65,11 @@ public Restlet createInboundRoot() {
return new Restlet() {
@Override
public void handle(Request request, Response response) {
- response.setEntity(
+ StringRepresentation entity =
new StringRepresentation(
- BODY, MediaType.TEXT_PLAIN));
+ BODY, MediaType.TEXT_PLAIN);
+ entity.setModificationDate(modified);
+ response.setEntity(entity);
}
};
}
@@ -192,4 +199,37 @@ void options_withRange_ignoresTheRange() throws Exception {
void get_withRange_stillReturnsPartialContent() throws Exception {
assertPartial("bytes=0-3", "0123", "bytes 0-3/10");
}
+
+ private HttpResponse getIfRange(Date ifRange) throws Exception {
+ HttpRequest request =
+ HttpRequest.newBuilder(uri)
+ .GET()
+ .header("Range", "bytes=0-3")
+ .header("If-Range", DateUtils.format(ifRange))
+ .build();
+ return client.send(request, BodyHandlers.ofString());
+ }
+
+ /** RFC 9110 section 13.1.5: an {@code If-Range} date that is not strong is ignored (#1742). */
+ @Test
+ void ifRangeDate_ofARecentModification_returnsTheWholeEntity() throws Exception {
+ modified = new Date();
+
+ HttpResponse response = getIfRange(modified);
+
+ assertEquals(200, response.statusCode());
+ assertEquals(BODY, response.body());
+ assertEquals(null, header(response, "Content-Range"));
+ }
+
+ @Test
+ void ifRangeDate_ofAnOldModification_returnsPartialContent() throws Exception {
+ modified = new Date(System.currentTimeMillis() - 3_600_000L);
+
+ HttpResponse response = getIfRange(modified);
+
+ assertEquals(206, response.statusCode());
+ assertEquals("0123", response.body());
+ assertEquals("bytes 0-3/10", header(response, "Content-Range"));
+ }
}
diff --git a/org.restlet/src/test/java/org/restlet/engine/util/DateUtilsTestCase.java b/org.restlet/src/test/java/org/restlet/engine/util/DateUtilsTestCase.java
index 0f76cf71a7..0fae163d0d 100644
--- a/org.restlet/src/test/java/org/restlet/engine/util/DateUtilsTestCase.java
+++ b/org.restlet/src/test/java/org/restlet/engine/util/DateUtilsTestCase.java
@@ -9,6 +9,7 @@
package org.restlet.engine.util;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Calendar;
@@ -112,4 +113,24 @@ void unmodifiableDates() {
assertTrue(DateUtils.unmodifiable(yesterday).before(DateUtils.unmodifiable(now)));
assertTrue(DateUtils.unmodifiable(yesterday).before(now));
}
+
+ @Test
+ void isStrong_requiresTheReferenceToBeAtLeastOneSecondLater() {
+ Date modified = new Date(1_700_000_000_000L);
+
+ assertFalse(DateUtils.isStrong(modified, new Date(modified.getTime())));
+ assertFalse(DateUtils.isStrong(modified, new Date(modified.getTime() + 999)));
+ assertTrue(DateUtils.isStrong(modified, new Date(modified.getTime() + 1000)));
+ assertTrue(DateUtils.isStrong(modified, new Date(modified.getTime() + 3_600_000L)));
+ assertFalse(DateUtils.isStrong(modified, new Date(modified.getTime() - 5000)));
+ }
+
+ @Test
+ void isStrong_withoutADate_isNotStrong() {
+ Date date = new Date();
+
+ assertFalse(DateUtils.isStrong(null, date));
+ assertFalse(DateUtils.isStrong(date, null));
+ assertFalse(DateUtils.isStrong(null, null));
+ }
}