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
1 change: 1 addition & 0 deletions changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,18 @@
*
* @param response The high-level response.
*/
public void commit(HttpResponse response) {

Check failure on line 84 in org.restlet.java/org.restlet/src/main/java/org/restlet/engine/adapter/ServerAdapter.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 31 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIhtgsP5O2KWGZu72fW&open=AaIhtgsP5O2KWGZu72fW&pullRequest=1741
try {
if ((response.getRequest().getMethod() != null) && response.getRequest().getMethod().equals(Method.HEAD)) {
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.
if (response.getEntity() != null) {
HeaderUtils.addNotModifiedEntityHeaders(response.getEntity(),
response.getHttpCall().getResponseHeaders());
response.setEntity(null);
}
} else if ((response.getRequest().getMethod() != null)
&& response.getRequest().getMethod().equals(Method.HEAD)) {
addEntityHeaders(response);
response.setEntity(null);
} else if (Method.GET.equals(response.getRequest().getMethod())
Expand All @@ -109,12 +118,6 @@
+ response.getRequest().getResourceRef() + "\".");
response.setEntity(null);
}
} else if (response.getStatus().equals(Status.REDIRECTION_NOT_MODIFIED)) {
if (response.getEntity() != null) {
HeaderUtils.addNotModifiedEntityHeaders(response.getEntity(),
response.getHttpCall().getResponseHeaders());
response.setEntity(null);
}
} else if (response.getStatus().isInformational()) {
if (response.isEntityAvailable()) {
getLogger().warning(
Expand All @@ -141,11 +144,11 @@

// Send the response to the client
response.getHttpCall().sendResponse(response);
} catch (Throwable t) {

Check warning on line 147 in org.restlet.java/org.restlet/src/main/java/org/restlet/engine/adapter/ServerAdapter.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Catch Exception instead of Throwable.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIhtgsP5O2KWGZu72fX&open=AaIhtgsP5O2KWGZu72fX&pullRequest=1741
if (response.getHttpCall().isConnectionBroken(t)) {
// output a single log line for this common case to avoid filling server logs
getLogger().log(Level.INFO,
"The connection was broken. It was probably closed by the client. Reason: " + t.getMessage());

Check warning on line 151 in org.restlet.java/org.restlet/src/main/java/org/restlet/engine/adapter/ServerAdapter.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Format specifiers or lambda should be used instead of string concatenation.

See more on https://sonarcloud.io/project/issues?id=restlet_restlet-framework-java&issues=AaIhtgsP5O2KWGZu72fY&open=AaIhtgsP5O2KWGZu72fY&pullRequest=1741
} else {
getLogger().log(Level.SEVERE, "An exception occurred writing the response entity", t);
response.getHttpCall().setStatusCode(Status.SERVER_ERROR_INTERNAL.getCode());
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);
}
}
Loading