diff --git a/changes.md b/changes.md index 3281ed71ba..6ab060d9f3 100644 --- a/changes.md +++ b/changes.md @@ -6,6 +6,7 @@ Changes log - `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. + - The `Range` header is now applied to GET requests only, as RFC 9110 section 14.2 requires: a HEAD request with a range was answered "206 Partial Content" (and so was OPTIONS), it is now answered as without the range. Issue #1734. - A `CacheDirective` created by a factory, such as `CacheDirective.maxAge(60)`, now equals the same directive read from a header: `equals` and `hashCode` use the name and the value only, no longer the `digit` attribute, which has no effect on the written header. `isDigit()`, `setDigit(boolean)` and the constructor `CacheDirective(String, String, boolean)` are deprecated and will be removed in Restlet 3.0. Issue #1730. - A 304 (Not Modified) response now carries the `Expires` header of the entity, as RFC 9110 section 15.4.5 requires, so that a revalidating cache refreshes it. Issue #1712. - Misc diff --git a/org.restlet.java/org.restlet/src/main/java/org/restlet/engine/application/RangeFilter.java b/org.restlet.java/org.restlet/src/main/java/org/restlet/engine/application/RangeFilter.java index 720f7e8c0d..6f0c9607cd 100644 --- a/org.restlet.java/org.restlet/src/main/java/org/restlet/engine/application/RangeFilter.java +++ b/org.restlet.java/org.restlet/src/main/java/org/restlet/engine/application/RangeFilter.java @@ -12,6 +12,7 @@ import org.restlet.Context; import org.restlet.Request; import org.restlet.Response; +import org.restlet.data.Method; import org.restlet.data.Range; import org.restlet.data.Status; import org.restlet.routing.Filter; @@ -42,7 +43,10 @@ protected void afterHandle(Request request, Response response) { if (getRangeService().isEnabled()) { response.getServerInfo().setAcceptingRanges(true); - if (request.getMethod().isSafe() && response.isEntityAvailable()) { + // RFC 9110 section 14.2: GET is the only method for which range handling is + // defined, the Range header of any other method (HEAD, OPTIONS...) must be + // ignored. + if (Method.GET.equals(request.getMethod()) && response.isEntityAvailable()) { Range responseRange = response.getEntity().getRange(); boolean rangedEntity = responseRange != null && isBytesRange(responseRange); diff --git a/org.restlet.java/org.restlet/src/test/java/org/restlet/engine/application/RangeFilterTestCase.java b/org.restlet.java/org.restlet/src/test/java/org/restlet/engine/application/RangeFilterTestCase.java index 466a0e9ef0..9012a7c827 100644 --- a/org.restlet.java/org.restlet/src/test/java/org/restlet/engine/application/RangeFilterTestCase.java +++ b/org.restlet.java/org.restlet/src/test/java/org/restlet/engine/application/RangeFilterTestCase.java @@ -17,6 +17,8 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.restlet.Application; import org.restlet.Context; import org.restlet.Request; @@ -292,4 +294,31 @@ public void write(java.io.OutputStream outputStream) void getRangeService_returnsApplicationRangeService() { assertEquals(application.getRangeService(), filter.getRangeService()); } + + /** + * RFC 9110 section 14.2: GET is the only method for which range handling is defined (#1734). + */ + @ParameterizedTest + @ValueSource(strings = {"HEAD", "OPTIONS", "TRACE"}) + void afterHandle_rangeOnAnotherSafeMethod_isIgnored(String method) { + Response response = + successResponse(Method.valueOf(method), new StringRepresentation("0123456789")); + Representation original = response.getEntity(); + response.getRequest().getRanges().add(new Range(0, 4)); + + filter.afterHandle(response.getRequest(), response); + + assertEquals(Status.SUCCESS_OK, response.getStatus(), method); + assertEquals(original, response.getEntity(), method); + } + + @Test + void afterHandle_rangeOnGet_stillGivesPartialContent() { + Response response = successResponse(Method.GET, new StringRepresentation("0123456789")); + response.getRequest().getRanges().add(new Range(0, 4)); + + filter.afterHandle(response.getRequest(), response); + + assertEquals(Status.SUCCESS_PARTIAL_CONTENT, response.getStatus()); + } } diff --git a/org.restlet.java/org.restlet/src/test/java/org/restlet/engine/application/RangeRequestEndToEndTestCase.java b/org.restlet.java/org.restlet/src/test/java/org/restlet/engine/application/RangeRequestEndToEndTestCase.java index 17fdcfd352..7dfccad006 100644 --- a/org.restlet.java/org.restlet/src/test/java/org/restlet/engine/application/RangeRequestEndToEndTestCase.java +++ b/org.restlet.java/org.restlet/src/test/java/org/restlet/engine/application/RangeRequestEndToEndTestCase.java @@ -10,6 +10,7 @@ package org.restlet.engine.application; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import java.net.URI; import java.net.http.HttpClient; @@ -157,4 +158,39 @@ void unsatisfiableRange_returns416WithTheCompleteLength(String range) throws Exc assertEquals(416, response.statusCode(), range); assertEquals("bytes */10", header(response, "Content-Range"), range); } + + private HttpResponse send(String method, String range) throws Exception { + HttpRequest request = + HttpRequest.newBuilder(uri) + .method(method, HttpRequest.BodyPublishers.noBody()) + .header("Range", range) + .build(); + return client.send(request, BodyHandlers.ofString()); + } + + /** + * RFC 9110 section 14.2: the Range of a HEAD request is ignored, so it describes the full + * entity (#1734). + */ + @Test + void head_withRange_ignoresTheRange() throws Exception { + HttpResponse response = send("HEAD", "bytes=0-3"); + + assertEquals(200, response.statusCode()); + assertEquals("10", header(response, "Content-Length")); + assertEquals(null, header(response, "Content-Range")); + } + + @Test + void options_withRange_ignoresTheRange() throws Exception { + HttpResponse response = send("OPTIONS", "bytes=0-3"); + + assertNotEquals(206, response.statusCode()); + assertEquals(null, header(response, "Content-Range")); + } + + @Test + void get_withRange_stillReturnsPartialContent() throws Exception { + assertPartial("bytes=0-3", "0123", "bytes 0-3/10"); + } }