From 71ea54df315be8bcf4c34358a03cbe30641a5a83 Mon Sep 17 00:00:00 2001 From: Thierry Boileau Date: Sat, 28 Nov 2026 11:26:52 +0100 Subject: [PATCH 1/2] Answer a HEAD request with a 304 like the GET request (#1738) RFC 9110 sections 9.3.2 and 15.4.5: a 304 carries the validators and caching headers only. The HEAD branch came first and wrote all the entity headers (Content-Type, Last-Modified...) on a HEAD answered 304. Co-Authored-By: Claude Sonnet 5.5 --- changes.md | 3 ++ .../restlet/engine/netty/NettyServerCall.java | 14 ++--- .../connector/NotModifiedHeadersTestCase.java | 54 +++++++++++++++++++ 3 files changed, 65 insertions(+), 6 deletions(-) diff --git a/changes.md b/changes.md index 7c05662663..06586bdb29 100644 --- a/changes.md +++ b/changes.md @@ -103,6 +103,9 @@ Changes log first scope outermost. `McpRequestContext.transportResponse()` gives the transport response. - Bugs fixed + - 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). - 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 (#1734). diff --git a/org.restlet/src/main/java/org/restlet/engine/netty/NettyServerCall.java b/org.restlet/src/main/java/org/restlet/engine/netty/NettyServerCall.java index b2b9e51233..0e81a05f3b 100644 --- a/org.restlet/src/main/java/org/restlet/engine/netty/NettyServerCall.java +++ b/org.restlet/src/main/java/org/restlet/engine/netty/NettyServerCall.java @@ -584,7 +584,14 @@ private void prepareEntity(Response response) { Status status = response.getStatus(); Method method = response.getRequest().getMethod(); - if (Method.HEAD.equals(method)) { + if (Status.REDIRECTION_NOT_MODIFIED.equals(status)) { + // Also for a HEAD request: RFC 9110 section 15.4.5, a 304 carries the validators and + // caching headers only, whatever the method. + if (response.getEntity() != null) { + HeaderUtils.addNotModifiedEntityHeaders(response.getEntity(), responseHeaders); + response.setEntity(null); + } + } else if (Method.HEAD.equals(method)) { HeaderUtils.addEntityHeaders(response.getEntity(), responseHeaders); response.setEntity(null); } else if (Method.GET.equals(method) @@ -601,11 +608,6 @@ private void prepareEntity(Response response) { } else if (Status.SUCCESS_NO_CONTENT.equals(status)) { HeaderUtils.addEntityHeaders(response.getEntity(), responseHeaders); dropEntity(response, Level.FINE, "204 (No content)"); - } else if (Status.REDIRECTION_NOT_MODIFIED.equals(status)) { - if (response.getEntity() != null) { - HeaderUtils.addNotModifiedEntityHeaders(response.getEntity(), responseHeaders); - response.setEntity(null); - } } else if (Status.SUCCESS_RESET_CONTENT.equals(status)) { dropEntity(response, Level.WARNING, "205 (Reset content)"); } else if (status.isInformational()) { 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 a66da0f7db..d8d4f4fb27 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); + } } From 84ab4bad61bf21bb27ba9bd84963f6790185e9dd Mon Sep 17 00:00:00 2001 From: Thierry Boileau Date: Sat, 28 Nov 2026 12:35:56 +0100 Subject: [PATCH 2/2] Extract the 304 branch of NettyServerCall.prepareEntity (Sonar S3776) Reduces the cognitive complexity of the method from 16 to under 15; no behavior change. Co-Authored-By: Claude Sonnet 5.5 --- .../restlet/engine/netty/NettyServerCall.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/org.restlet/src/main/java/org/restlet/engine/netty/NettyServerCall.java b/org.restlet/src/main/java/org/restlet/engine/netty/NettyServerCall.java index 0e81a05f3b..ff68170c58 100644 --- a/org.restlet/src/main/java/org/restlet/engine/netty/NettyServerCall.java +++ b/org.restlet/src/main/java/org/restlet/engine/netty/NettyServerCall.java @@ -585,12 +585,7 @@ private void prepareEntity(Response response) { Method method = response.getRequest().getMethod(); if (Status.REDIRECTION_NOT_MODIFIED.equals(status)) { - // Also for a HEAD request: RFC 9110 section 15.4.5, a 304 carries the validators and - // caching headers only, whatever the method. - if (response.getEntity() != null) { - HeaderUtils.addNotModifiedEntityHeaders(response.getEntity(), responseHeaders); - response.setEntity(null); - } + prepareNotModifiedEntity(response); } else if (Method.HEAD.equals(method)) { HeaderUtils.addEntityHeaders(response.getEntity(), responseHeaders); response.setEntity(null); @@ -629,6 +624,18 @@ private void prepareEntity(Response response) { } } + /** + * Adds the entity headers of a 304 response and drops the entity. This is also done for a HEAD + * request: RFC 9110 section 15.4.5, a 304 carries the validators and caching headers only, + * whatever the method. + */ + private void prepareNotModifiedEntity(Response response) { + if (response.getEntity() != null) { + HeaderUtils.addNotModifiedEntityHeaders(response.getEntity(), responseHeaders); + response.setEntity(null); + } + } + private void dropEntity(Response response, Level level, String status) { if (response.isEntityAvailable()) { getLogger()