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
3 changes: 3 additions & 0 deletions changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,9 @@ 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)) {
prepareNotModifiedEntity(response);
} else if (Method.HEAD.equals(method)) {
HeaderUtils.addEntityHeaders(response.getEntity(), responseHeaders);
response.setEntity(null);
} else if (Method.GET.equals(method)
Expand All @@ -601,11 +603,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()) {
Expand All @@ -627,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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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<String> headerNames(String response) {
Set<String> 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);
}
}