From a8583997a8dceac5b9efcfcf81c113c20182fff8 Mon Sep 17 00:00:00 2001 From: Thierry Boileau Date: Sat, 28 Nov 2026 10:52:28 +0100 Subject: [PATCH] Apply the Range header to GET requests only (#1734) RFC 9110 section 14.2: GET is the only method for which range handling is defined, the Range of any other method must be ignored. A HEAD or OPTIONS request with a range was answered 206. Co-Authored-By: Claude Sonnet 5.5 --- changes.md | 1 + .../engine/application/RangeFilter.java | 5 ++- .../application/RangeFilterTestCase.java | 29 +++++++++++++++ .../RangeRequestEndToEndTestCase.java | 36 +++++++++++++++++++ 4 files changed, 70 insertions(+), 1 deletion(-) diff --git a/changes.md b/changes.md index 1aef6de526..ffafb10c96 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/src/main/java/org/restlet/engine/application/RangeFilter.java b/org.restlet/src/main/java/org/restlet/engine/application/RangeFilter.java index 3b8853f068..7b5dd4cc33 100644 --- a/org.restlet/src/main/java/org/restlet/engine/application/RangeFilter.java +++ b/org.restlet/src/main/java/org/restlet/engine/application/RangeFilter.java @@ -14,6 +14,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,9 @@ protected void afterHandle(Request request, Response response) { if (!getRangeService().isEnabled()) { return; } - 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()) { return; } if (!response.getStatus().isSuccess()) { 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 55da6c113c..93f95d68ad 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 @@ -16,6 +16,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; @@ -291,4 +293,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/src/test/java/org/restlet/engine/application/RangeRequestEndToEndTestCase.java b/org.restlet/src/test/java/org/restlet/engine/application/RangeRequestEndToEndTestCase.java index bb649407b8..cd656f3b18 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 @@ -9,6 +9,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; @@ -156,4 +157,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"); + } }