Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Changes log

- 2.6.2 (??-??-2026)
- 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. Issue #1742.
- `DefaultSslContextFactory` now uses the JVM default algorithms for the trust manager (`TrustManagerFactory.getDefaultAlgorithm()`, `PKIX`) and the key manager (`KeyManagerFactory.getDefaultAlgorithm()`) instead of the obsolete `SunX509`, unless the `ssl.TrustManagerFactory.algorithm` or `ssl.KeyManagerFactory.algorithm` system property, the `trustManagerAlgorithm` or `keyManagerAlgorithm` parameter, or the setters say otherwise. A valid certificate chain that is not in order is no longer rejected with a "Certificate chaining error". The `PKIX` validation is the standard one, and stricter than `SunX509` on unusual chains: set the trust manager algorithm to `SunX509` to get the previous behavior. Issue #1397.
- `Redirector.getTargetRef()` now removes the dot segments (`.` and `..`) of the target URI (RFC 3986, section 5.2.4), so the `Location` header of a client redirection and the URI of a server redirection are normalized. Relative-path targets such as `../x` are left untouched, as they need their base reference. A reverse proxy built on `http://backend/root/{rr}` now resolves a `..` found in the remaining part itself, which it did not before the request reached the backend. Issue #238.
- The Jackson, Gson and JSON (org.json) converters now handle the `application/*+json` media types, such as `application/vnd.example+json`. They were ignored, so an incoming `+json` entity was not converted, and a `+json` response was left to another converter (such as JAXB) when it scored higher than the 0.5 given by Jackson. A custom converter that won for these media types may now lose to the JSON converters. Issue #1185.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@
* updates within the same second could not be told apart.<br>
* <br>
* Note that {@code new Tag(String)} creates a weak tag, which never matches an "If-Match"
* condition.
* condition.<br>
* <br>
* 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.
Expand All @@ -119,12 +124,8 @@
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;
}
Expand Down Expand Up @@ -160,7 +161,7 @@
/**
* Constructor.
*/
public Conditions() {

Check failure on line 164 in org.restlet.java/org.restlet/src/main/java/org/restlet/data/Conditions.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Add a nested comment explaining why this method is empty, throw an UnsupportedOperationException or complete the implementation.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiVKxHQKu3y-8RWBn8&open=AaIiVKxHQKu3y-8RWBn8&pullRequest=1745
}

/**
Expand Down Expand Up @@ -247,7 +248,13 @@
}

/**
* Returns the range conditional status of an entity.
* Returns the range conditional status of an entity.<br>
* <br>
* 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.
Expand All @@ -261,14 +268,18 @@
// If a tag exists. RFC 9110 section 13.1.5: strong comparison, a weak
// tag never matches.
if (tag != null) {
if (all || getRangeTag().strongMatch(tag)) {

Check warning on line 271 in org.restlet.java/org.restlet/src/main/java/org/restlet/data/Conditions.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Merge this if statement with the enclosing one.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiVKxHQKu3y-8RWBn6&open=AaIiVKxHQKu3y-8RWBn6&pullRequest=1745
result = Status.SUCCESS_OK;
}
}
} else if (getRangeDate() != null) {
// If a modification date exists. The dates of the headers have a
// precision of one second.
if (modificationDate != null && DateUtils.equals(getRangeDate(), modificationDate)) {
if (modificationDate != null && DateUtils.equals(getRangeDate(), modificationDate)

Check warning on line 278 in org.restlet.java/org.restlet/src/main/java/org/restlet/data/Conditions.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Merge this if statement with the enclosing one.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiVKxHQKu3y-8RWBn7&open=AaIiVKxHQKu3y-8RWBn7&pullRequest=1745
&& DateUtils.isStrong(modificationDate, new Date())) {
// 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;
}
}
Expand Down Expand Up @@ -315,7 +326,7 @@
* @return Null if the requested method can be performed, the status of the
* response otherwise.
*/
public Status getStatus(Method method, boolean entityExists, Tag tag, Date modificationDate) {

Check failure on line 329 in org.restlet.java/org.restlet/src/main/java/org/restlet/data/Conditions.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 62 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiVKxHQKu3y-8RWBn9&open=AaIiVKxHQKu3y-8RWBn9&pullRequest=1745
Status result = null;

// Is the "if-Match" rule followed or not?
Expand Down Expand Up @@ -385,7 +396,7 @@
// Check if the current representation has been updated
// since the "if-modified-since" date. In this case, the
// rule is followed.
Date modifiedSince = getModifiedSince();

Check warning on line 399 in org.restlet.java/org.restlet/src/main/java/org/restlet/data/Conditions.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename "modifiedSince" which hides the field declared at line 59.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiVKxHQKu3y-8RWBn-&open=AaIiVKxHQKu3y-8RWBn-&pullRequest=1745
boolean isModifiedSince = (modifiedSince != null) && (DateUtils.after(new Date(), modifiedSince)
|| (modificationDate == null) || DateUtils.after(modifiedSince, modificationDate));
matched = !isModifiedSince;
Expand All @@ -404,7 +415,7 @@

// Is the "if-Modified-Since" rule followed or not?
if ((result == null) && (getModifiedSince() != null)) {
Date modifiedSince = getModifiedSince();

Check warning on line 418 in org.restlet.java/org.restlet/src/main/java/org/restlet/data/Conditions.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename "modifiedSince" which hides the field declared at line 59.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiVKxHQKu3y-8RWBn_&open=AaIiVKxHQKu3y-8RWBn_&pullRequest=1745
boolean isModifiedSince = (DateUtils.after(new Date(), modifiedSince) || (modificationDate == null)
|| DateUtils.after(modifiedSince, modificationDate));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,25 @@ 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,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;
Expand Down Expand Up @@ -321,4 +322,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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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;
Expand All @@ -31,6 +32,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;

/**
Expand All @@ -48,6 +50,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();
Expand All @@ -61,9 +66,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);
}
};
}
Expand Down Expand Up @@ -193,4 +200,37 @@ void options_withRange_ignoresTheRange() throws Exception {
void get_withRange_stillReturnsPartialContent() throws Exception {
assertPartial("bytes=0-3", "0123", "bytes 0-3/10");
}

private HttpResponse<String> 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<String> 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<String> response = getIfRange(modified);

assertEquals(206, response.statusCode());
assertEquals("0123", response.body());
assertEquals("bytes 0-3/10", header(response, "Content-Range"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.GregorianCalendar;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
Expand All @@ -23,25 +24,25 @@
*
* @author Jerome Louvel
*/
public class DateUtilsTestCase {

Check warning on line 27 in org.restlet.java/org.restlet/src/test/java/org/restlet/engine/util/DateUtilsTestCase.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove redundant visibility modifiers from the methods of this test class.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiVK8yQKu3y-8RWBoJ&open=AaIiVK8yQKu3y-8RWBoJ&pullRequest=1745

private final String DATE_RFC3339_1 = "1985-04-12T23:20:50.52Z";

Check warning on line 29 in org.restlet.java/org.restlet/src/test/java/org/restlet/engine/util/DateUtilsTestCase.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this field "DATE_RFC3339_1" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiVK8yQKu3y-8RWBoA&open=AaIiVK8yQKu3y-8RWBoA&pullRequest=1745

private final String DATE_RFC3339_2 = "1996-12-19T16:39:57-08:00";

Check warning on line 31 in org.restlet.java/org.restlet/src/test/java/org/restlet/engine/util/DateUtilsTestCase.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this field "DATE_RFC3339_2" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiVK8yQKu3y-8RWBoB&open=AaIiVK8yQKu3y-8RWBoB&pullRequest=1745

private final String DATE_RFC3339_3 = "1990-12-31T23:59:60Z";

Check warning on line 33 in org.restlet.java/org.restlet/src/test/java/org/restlet/engine/util/DateUtilsTestCase.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this field "DATE_RFC3339_3" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiVK8yQKu3y-8RWBoC&open=AaIiVK8yQKu3y-8RWBoC&pullRequest=1745

private final String DATE_RFC3339_4 = "1990-12-31T15:59:60-08:00";

Check warning on line 35 in org.restlet.java/org.restlet/src/test/java/org/restlet/engine/util/DateUtilsTestCase.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this field "DATE_RFC3339_4" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiVK8yQKu3y-8RWBoD&open=AaIiVK8yQKu3y-8RWBoD&pullRequest=1745

private final String DATE_RFC3339_5 = "1937-01-01T12:00:27.87+00:20";

Check warning on line 37 in org.restlet.java/org.restlet/src/test/java/org/restlet/engine/util/DateUtilsTestCase.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this field "DATE_RFC3339_5" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiVK8yQKu3y-8RWBoE&open=AaIiVK8yQKu3y-8RWBoE&pullRequest=1745

private final String DATE_ASC_1 = "Fri Apr 12 23:20:50 1985";

Check warning on line 39 in org.restlet.java/org.restlet/src/test/java/org/restlet/engine/util/DateUtilsTestCase.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this field "DATE_ASC_1" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiVK8yQKu3y-8RWBoF&open=AaIiVK8yQKu3y-8RWBoF&pullRequest=1745

private final String DATE_RFC1036_1 = "Friday, 12-Apr-85 23:20:50 GMT";

Check warning on line 41 in org.restlet.java/org.restlet/src/test/java/org/restlet/engine/util/DateUtilsTestCase.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this field "DATE_RFC1036_1" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiVK8yQKu3y-8RWBoG&open=AaIiVK8yQKu3y-8RWBoG&pullRequest=1745

private final String DATE_RFC1123_1 = "Fri, 12 Apr 1985 23:20:50 GMT";

Check warning on line 43 in org.restlet.java/org.restlet/src/test/java/org/restlet/engine/util/DateUtilsTestCase.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this field "DATE_RFC1123_1" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiVK8yQKu3y-8RWBoH&open=AaIiVK8yQKu3y-8RWBoH&pullRequest=1745

private final String DATE_RFC822_1 = "Fri, 12 Apr 85 23:20:50 GMT";

Check warning on line 45 in org.restlet.java/org.restlet/src/test/java/org/restlet/engine/util/DateUtilsTestCase.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this field "DATE_RFC822_1" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIiVK8yQKu3y-8RWBoI&open=AaIiVK8yQKu3y-8RWBoI&pullRequest=1745

/**
* Tests for dates in the RFC 822 format.
Expand Down Expand Up @@ -143,4 +144,24 @@
DateUtils.unmodifiable(now)));
assertTrue(DateUtils.unmodifiable(yesterday).before(now));
}

@Test
public 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
public void isStrong_withoutADate_isNotStrong() {
Date date = new Date();

assertFalse(DateUtils.isStrong(null, date));
assertFalse(DateUtils.isStrong(date, null));
assertFalse(DateUtils.isStrong(null, null));
}
}
Loading