diff --git a/changes.md b/changes.md index ffafb10c96..e4da983bcc 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. + - 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). Issue #1738. - 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. diff --git a/org.restlet/src/main/java/org/restlet/engine/adapter/ServerAdapter.java b/org.restlet/src/main/java/org/restlet/engine/adapter/ServerAdapter.java index d1426f60a2..3385fb9625 100644 --- a/org.restlet/src/main/java/org/restlet/engine/adapter/ServerAdapter.java +++ b/org.restlet/src/main/java/org/restlet/engine/adapter/ServerAdapter.java @@ -88,7 +88,11 @@ protected void addResponseHeaders(HttpResponse response) { */ public void commit(HttpResponse response) { try { - if (Method.HEAD.equals(response.getRequest().getMethod())) { + if (response.getStatus().equals(Status.REDIRECTION_NOT_MODIFIED)) { + // Also for a HEAD request: RFC 9110 section 15.4.5, a 304 carries the + // validators and caching headers only, whatever the method. + handleRedirectionNotNodified(response); + } else if (Method.HEAD.equals(response.getRequest().getMethod())) { addEntityHeaders(response); response.setEntity(null); } else if (Method.GET.equals(response.getRequest().getMethod()) @@ -104,8 +108,6 @@ public void commit(HttpResponse response) { handleNoContentResponse(response); } else if (response.getStatus().equals(Status.SUCCESS_RESET_CONTENT)) { handleResetContent(response); - } else if (response.getStatus().equals(Status.REDIRECTION_NOT_MODIFIED)) { - handleRedirectionNotNodified(response); } else if (response.getStatus().isInformational()) { handleInformationalStatus(response); } else { diff --git a/org.restlet/src/test/java/org/restlet/engine/connector/NotModifiedHeadersTestCase.java b/org.restlet/src/test/java/org/restlet/engine/connector/NotModifiedHeadersTestCase.java index 70b4621abb..d00465fd7c 100644 --- a/org.restlet/src/test/java/org/restlet/engine/connector/NotModifiedHeadersTestCase.java +++ b/org.restlet/src/test/java/org/restlet/engine/connector/NotModifiedHeadersTestCase.java @@ -8,6 +8,7 @@ */ package org.restlet.engine.connector; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -16,7 +17,10 @@ import java.net.Socket; import java.nio.charset.StandardCharsets; import java.util.Date; +import java.util.List; import java.util.Locale; +import java.util.Set; +import java.util.TreeSet; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -156,4 +160,54 @@ void ok_stillCarriesExpires() throws Exception { headersOf(response).contains("\r\nexpires: fri, 15 jan 2027 08:00:00 gmt"), response); } + + /** + * The names of the header fields of a response, without the ones that depend on the connection. + */ + private static Set headerNames(String response) { + Set names = new TreeSet<>(); + String[] lines = headersOf(response).split("\r\n"); + for (int i = 1; i < lines.length; i++) { + String name = lines[i].substring(0, lines[i].indexOf(':')); + if (!List.of("date", "server", "connection", "content-length").contains(name)) { + names.add(name); + } + } + return names; + } + + /** RFC 9110 sections 9.3.2 and 15.4.5: a HEAD answered 304 is answered like the GET (#1738). */ + @Test + void head_notModified_doesNotCarryTheOtherEntityHeaders() throws Exception { + String response = exchange("HEAD", "If-None-Match: \"v1\"\r\n"); + String headers = headersOf(response); + + assertTrue(response.startsWith("HTTP/1.1 304"), response); + assertTrue(headers.contains("\r\netag: \"v1\""), response); + assertTrue(headers.contains("\r\ncache-control: max-age=60"), response); + assertTrue(headers.contains("\r\nvary: accept-language"), response); + assertTrue(headers.contains("\r\nexpires: fri, 15 jan 2027 08:00:00 gmt"), response); + assertFalse(headers.contains("\r\nlast-modified: "), headers); + assertFalse(headers.contains("\r\ncontent-type: "), headers); + } + + @Test + void head_notModified_hasTheHeaderFieldsOfTheGetNotModified() throws Exception { + assertEquals( + headerNames(exchange("GET", "If-None-Match: \"v1\"\r\n")), + headerNames(exchange("HEAD", "If-None-Match: \"v1\"\r\n"))); + } + + @Test + void head_unconditional_stillCarriesTheEntityHeaders() throws Exception { + String response = exchange("HEAD", ""); + String headers = headersOf(response); + + assertTrue(response.startsWith("HTTP/1.1 200"), response); + assertTrue(headers.contains("\r\ncontent-type: text/plain"), response); + assertTrue(headers.contains("\r\nlast-modified: "), response); + assertTrue(headers.contains("\r\netag: \"v1\""), response); + assertTrue(headers.contains("\r\nexpires: fri, 15 jan 2027 08:00:00 gmt"), response); + assertTrue(headers.contains("\r\ncontent-length: 5"), response); + } }