diff --git a/changes.md b/changes.md index 8bf0afac48..443d6e3c8f 100644 --- a/changes.md +++ b/changes.md @@ -11,6 +11,8 @@ Changes log - 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. + - Enhancements + - `Redirector.MODE_CLIENT_PERMANENT_STRICT` answers 308 (Permanent Redirect), which makes the client repeat the request with the same method and content, unlike `MODE_CLIENT_PERMANENT` (301) that lets a user agent turn a POST into a GET. It comes with `Response.redirectPermanentStrict(...)`, `ServerResource.redirectPermanentStrict(...)` and `Router.redirectPermanentStrict(...)`. The Javadoc of the 301 helpers no longer promises that the method is kept. Issue #1746. - Misc - Added conditional writes on the client side: `ClientResource` gets per-call overloads `put`, `patch` and `delete` taking `Conditions`; they apply to that call only and are never retried. `Conditions.unchangedSince(Response)` builds an "If-Match" condition from the strong entity tag that was read, or an "If-Unmodified-Since" condition if the modification date can be deduced to be strong (the response date is at least one second after it, RFC 9110 section 8.8.2.2), and refuses to build an unconditional write otherwise. `Conditions.ifAbsent()` builds "If-None-Match: *" to create a resource only if it does not exist. A failed condition is reported as a 412 `ResourceException`. Issue #100. - Added an optional origin matcher to CORS (`CorsService`, `CorsFilter`, `setAllowedOriginMatcher(Predicate)`) to accept origins that are not listed, for example any sub-domain. The response always contains the exact request origin; it has no effect while the allowed origins contain `*`. Issue #1611. diff --git a/org.restlet.java/org.restlet/src/main/java/org/restlet/Response.java b/org.restlet.java/org.restlet/src/main/java/org/restlet/Response.java index 7d85e33162..74107568e3 100644 --- a/org.restlet.java/org.restlet/src/main/java/org/restlet/Response.java +++ b/org.restlet.java/org.restlet/src/main/java/org/restlet/Response.java @@ -568,8 +568,9 @@ public boolean isProvisional() { } /** - * Permanently redirects the client to a target URI. The client is expected to - * reuse the same method for the new request. + * Permanently redirects the client to a target URI (301). A user agent may + * change the method of the new request, for example a POST into a GET: use + * {@link #redirectPermanentStrict(Reference)} to have the same method reused. * * @param targetRef The target URI reference. */ @@ -579,8 +580,9 @@ public void redirectPermanent(Reference targetRef) { } /** - * Permanently redirects the client to a target URI. The client is expected to - * reuse the same method for the new request.
+ * Permanently redirects the client to a target URI (301). A user agent may + * change the method of the new request, for example a POST into a GET: use + * {@link #redirectPermanentStrict(String)} to have the same method reused.
*
* If you pass a relative target URI, it will be resolved with the current base * reference of the request's resource reference (see @@ -593,6 +595,36 @@ public void redirectPermanent(String targetUri) { setStatus(Status.REDIRECTION_PERMANENT); } + /** + * Permanently redirects the client to a target URI (308). The client must + * repeat the request to the new URI with the same method and content, unlike + * with {@link #redirectPermanent(Reference)}. + * + * @param targetRef The target URI reference. + * @see Status#REDIRECTION_PERMANENT_STRICT + */ + public void redirectPermanentStrict(Reference targetRef) { + setLocationRef(targetRef); + setStatus(Status.REDIRECTION_PERMANENT_STRICT); + } + + /** + * Permanently redirects the client to a target URI (308). The client must + * repeat the request to the new URI with the same method and content, unlike + * with {@link #redirectPermanent(String)}.
+ *
+ * If you pass a relative target URI, it will be resolved with the current base + * reference of the request's resource reference (see + * {@link Request#getResourceRef()} and {@link Reference#getBaseRef()}. + * + * @param targetUri The target URI. + * @see Status#REDIRECTION_PERMANENT_STRICT + */ + public void redirectPermanentStrict(String targetUri) { + setLocationRef(targetUri); + setStatus(Status.REDIRECTION_PERMANENT_STRICT); + } + /** * Redirects the client to a different URI that SHOULD be retrieved using a GET * method on that resource. This method exists primarily to allow the output of diff --git a/org.restlet.java/org.restlet/src/main/java/org/restlet/resource/ServerResource.java b/org.restlet.java/org.restlet/src/main/java/org/restlet/resource/ServerResource.java index ceb963a237..9c628c6fb5 100644 --- a/org.restlet.java/org.restlet/src/main/java/org/restlet/resource/ServerResource.java +++ b/org.restlet.java/org.restlet/src/main/java/org/restlet/resource/ServerResource.java @@ -1443,8 +1443,9 @@ protected Representation put(Representation representation, Variant variant) thr } /** - * Permanently redirects the client to a target URI. The client is expected to - * reuse the same method for the new request. + * Permanently redirects the client to a target URI (301). A user agent may + * change the method of the new request: see + * {@link #redirectPermanentStrict(Reference)}. * * @param targetRef The target URI reference. */ @@ -1455,8 +1456,9 @@ public void redirectPermanent(Reference targetRef) { } /** - * Permanently redirects the client to a target URI. The client is expected to - * reuse the same method for the new request.
+ * Permanently redirects the client to a target URI (301). A user agent may + * change the method of the new request: see + * {@link #redirectPermanentStrict(String)}.
*
* If you pass a relative target URI, it will be resolved with the current base * reference of the request's resource reference (see @@ -1470,6 +1472,34 @@ public void redirectPermanent(String targetUri) { } } + /** + * Permanently redirects the client to a target URI (308). The client must + * repeat the request to the new URI with the same method and content. + * + * @param targetRef The target URI reference. + */ + public void redirectPermanentStrict(Reference targetRef) { + if (getResponse() != null) { + getResponse().redirectPermanentStrict(targetRef); + } + } + + /** + * Permanently redirects the client to a target URI (308). The client must + * repeat the request to the new URI with the same method and content.
+ *
+ * If you pass a relative target URI, it will be resolved with the current base + * reference of the request's resource reference (see + * {@link Request#getResourceRef()} and {@link Reference#getBaseRef()}. + * + * @param targetUri The target URI. + */ + public void redirectPermanentStrict(String targetUri) { + if (getResponse() != null) { + getResponse().redirectPermanentStrict(targetUri); + } + } + /** * Redirects the client to a different URI that SHOULD be retrieved using a GET * method on that resource. This method exists primarily to allow the output of diff --git a/org.restlet.java/org.restlet/src/main/java/org/restlet/routing/Redirector.java b/org.restlet.java/org.restlet/src/main/java/org/restlet/routing/Redirector.java index 73f2de036b..d6a63ff279 100644 --- a/org.restlet.java/org.restlet/src/main/java/org/restlet/routing/Redirector.java +++ b/org.restlet.java/org.restlet/src/main/java/org/restlet/routing/Redirector.java @@ -23,7 +23,8 @@ * Rewrites URIs then redirects the call or the client to a new destination. * There are various redirection modes that you can choose from: client-side * redirections ({@link #MODE_CLIENT_FOUND}, {@link #MODE_CLIENT_PERMANENT}, - * {@link #MODE_CLIENT_SEE_OTHER}, {@link #MODE_CLIENT_TEMPORARY}) or + * {@link #MODE_CLIENT_PERMANENT_STRICT}, {@link #MODE_CLIENT_SEE_OTHER}, + * {@link #MODE_CLIENT_TEMPORARY}) or * server-side redirections, similar to a reverse proxy ( * {@link #MODE_SERVER_OUTBOUND} and {@link #MODE_SERVER_INBOUND}).
*
@@ -60,11 +61,27 @@ public class Redirector extends Restlet { * In this mode, the client is permanently redirected to the URI generated from * the target URI pattern, using the {@link Status#REDIRECTION_PERMANENT} * status. Note: this is a client-side redirection.
+ *
+ * With the 301 status, a user agent may change the method of the new request + * (a POST can become a GET): use {@link #MODE_CLIENT_PERMANENT_STRICT} to + * keep it. * * @see Status#REDIRECTION_PERMANENT */ public static final int MODE_CLIENT_PERMANENT = 1; + /** + * In this mode, the client is permanently redirected to the URI generated from + * the target URI pattern, using the + * {@link Status#REDIRECTION_PERMANENT_STRICT} status (308): the client must + * repeat the request to the new URI with the same method and content, unlike + * with {@link #MODE_CLIENT_PERMANENT}. Note: this is a client-side + * redirection.
+ * + * @see Status#REDIRECTION_PERMANENT_STRICT + */ + public static final int MODE_CLIENT_PERMANENT_STRICT = 5; + /** * In this mode, the client is simply redirected to the URI generated from the * target URI pattern using the {@link Status#REDIRECTION_SEE_OTHER} status. @@ -268,6 +285,14 @@ public void handle(Request request, Response response) { response.redirectPermanent(targetRef); break; + case MODE_CLIENT_PERMANENT_STRICT: + if (request.isLoggable()) { + getLogger().log(Level.FINE, "Permanently redirecting client, same method, to: " + targetRef); + } + + response.redirectPermanentStrict(targetRef); + break; + case MODE_CLIENT_FOUND: if (request.isLoggable()) { getLogger().log(Level.FINE, "Redirecting client to found location: " + targetRef); diff --git a/org.restlet.java/org.restlet/src/main/java/org/restlet/routing/Router.java b/org.restlet.java/org.restlet/src/main/java/org/restlet/routing/Router.java index ac3159808b..c4209ef76b 100644 --- a/org.restlet.java/org.restlet/src/main/java/org/restlet/routing/Router.java +++ b/org.restlet.java/org.restlet/src/main/java/org/restlet/routing/Router.java @@ -590,8 +590,9 @@ protected void logRoute(Route route) { } /** - * Attaches a permanent redirection to this router based on a given URI pattern. - * The client is expected to reuse the same method for the new request. + * Attaches a permanent redirection (301) to this router based on a given URI + * pattern. A user agent may change the method of the new request: see + * {@link #redirectPermanentStrict(String, String)}. * * @param pathTemplate The URI path template that must match the relative part * of the resource URI. @@ -602,6 +603,21 @@ public TemplateRoute redirectPermanent(String pathTemplate, String targetUri) { return attach(pathTemplate, new Redirector(getContext(), targetUri, Redirector.MODE_CLIENT_PERMANENT)); } + /** + * Attaches a permanent redirection (308) to this router based on a given URI + * pattern. The client must repeat the request to the new URI with the same + * method and content. + * + * @param pathTemplate The URI path template that must match the relative part + * of the resource URI. + * @param targetUri The target URI. + * @return The created route. + */ + public TemplateRoute redirectPermanentStrict(String pathTemplate, String targetUri) { + return attach(pathTemplate, + new Redirector(getContext(), targetUri, Redirector.MODE_CLIENT_PERMANENT_STRICT)); + } + /** * Attaches a redirection to this router based on a given URI pattern. It * redirects the client to a different URI that SHOULD be retrieved using a GET diff --git a/org.restlet.java/org.restlet/src/main/java/org/restlet/util/WrapperResponse.java b/org.restlet.java/org.restlet/src/main/java/org/restlet/util/WrapperResponse.java index e4486124bf..4545c05b65 100644 --- a/org.restlet.java/org.restlet/src/main/java/org/restlet/util/WrapperResponse.java +++ b/org.restlet.java/org.restlet/src/main/java/org/restlet/util/WrapperResponse.java @@ -253,8 +253,9 @@ public boolean isEntityAvailable() { } /** - * Permanently redirects the client to a target URI. The client is expected to - * reuse the same method for the new request. + * Permanently redirects the client to a target URI (301). A user agent may + * change the method of the new request: see + * {@link #redirectPermanentStrict(Reference)}. * * @param targetRef The target URI reference. */ @@ -264,8 +265,9 @@ public void redirectPermanent(Reference targetRef) { } /** - * Permanently redirects the client to a target URI. The client is expected to - * reuse the same method for the new request. + * Permanently redirects the client to a target URI (301). A user agent may + * change the method of the new request: see + * {@link #redirectPermanentStrict(String)}. * * @param targetUri The target URI. */ @@ -274,6 +276,28 @@ public void redirectPermanent(String targetUri) { getWrappedResponse().redirectPermanent(targetUri); } + /** + * Permanently redirects the client to a target URI (308). The client must + * repeat the request to the new URI with the same method and content. + * + * @param targetRef The target URI reference. + */ + @Override + public void redirectPermanentStrict(Reference targetRef) { + getWrappedResponse().redirectPermanentStrict(targetRef); + } + + /** + * Permanently redirects the client to a target URI (308). The client must + * repeat the request to the new URI with the same method and content. + * + * @param targetUri The target URI. + */ + @Override + public void redirectPermanentStrict(String targetUri) { + getWrappedResponse().redirectPermanentStrict(targetUri); + } + /** * Redirects the client to a different URI that SHOULD be retrieved using a GET * method on that resource. This method exists primarily to allow the output of diff --git a/org.restlet.java/org.restlet/src/test/java/org/restlet/ResponseRedirectionTestCase.java b/org.restlet.java/org.restlet/src/test/java/org/restlet/ResponseRedirectionTestCase.java new file mode 100644 index 0000000000..7ff53a624d --- /dev/null +++ b/org.restlet.java/org.restlet/src/test/java/org/restlet/ResponseRedirectionTestCase.java @@ -0,0 +1,110 @@ +/** + * Copyright 2005-2026 Qlik + *

+ * The content of this file is subject to the terms of the Apache 2.0 open + * source license available at https://www.opensource.org/licenses/apache-2.0 + *

+ * Restlet is a registered trademark of QlikTech International AB. + */ +package org.restlet; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; +import org.restlet.data.Method; +import org.restlet.data.Reference; +import org.restlet.data.Status; +import org.restlet.resource.ServerResource; +import org.restlet.routing.Redirector; +import org.restlet.routing.Router; +import org.restlet.routing.TemplateRoute; +import org.restlet.util.WrapperResponse; + +/** + * Tests the redirection helpers of {@link Response}, {@link WrapperResponse}, + * {@link ServerResource} and {@link Router}, including the 308 ones (#1746). + */ +class ResponseRedirectionTestCase { + + private static Response newResponse() { + return new Response(new Request(Method.POST, "http://localhost/old")); + } + + @Test + void redirectPermanent_answers301() { + Response response = newResponse(); + + response.redirectPermanent("http://localhost/new"); + + assertEquals(Status.REDIRECTION_PERMANENT, response.getStatus()); + } + + @Test + void redirectPermanentStrict_withUri_answers308WithTheLocation() { + Response response = newResponse(); + + response.redirectPermanentStrict("http://localhost/new"); + + assertEquals(Status.REDIRECTION_PERMANENT_STRICT, response.getStatus()); + assertEquals("http://localhost/new", response.getLocationRef().toString()); + } + + @Test + void redirectPermanentStrict_withReference_answers308WithTheLocation() { + Response response = newResponse(); + + response.redirectPermanentStrict(new Reference("http://localhost/new")); + + assertEquals(Status.REDIRECTION_PERMANENT_STRICT, response.getStatus()); + assertEquals("http://localhost/new", response.getLocationRef().toString()); + } + + @Test + void redirectPermanentStrict_withRelativeUri_isResolvedAgainstTheRequest() { + Response response = newResponse(); + + response.redirectPermanentStrict("/new"); + + assertEquals("http://localhost/new", response.getLocationRef().getTargetRef().toString()); + } + + @Test + void wrapperResponse_delegatesRedirectPermanentStrict() { + Response wrapped = newResponse(); + WrapperResponse wrapper = new WrapperResponse(wrapped); + + wrapper.redirectPermanentStrict("http://localhost/strict"); + assertEquals(Status.REDIRECTION_PERMANENT_STRICT, wrapped.getStatus()); + + wrapper.redirectPermanentStrict(new Reference("http://localhost/strict2")); + assertEquals(Status.REDIRECTION_PERMANENT_STRICT, wrapped.getStatus()); + assertEquals("http://localhost/strict2", wrapped.getLocationRef().toString()); + } + + @Test + void serverResource_redirectPermanentStrict_setsLocationAnd308() { + ServerResource resource = new ServerResource() { + }; + Response response = newResponse(); + resource.init(new Context(), response.getRequest(), response); + + resource.redirectPermanentStrict("http://localhost/target"); + assertEquals(Status.REDIRECTION_PERMANENT_STRICT, response.getStatus()); + assertEquals("http://localhost/target", response.getLocationRef().toString()); + + resource.redirectPermanentStrict(new Reference("http://localhost/target2")); + assertEquals("http://localhost/target2", response.getLocationRef().toString()); + } + + @Test + void router_redirectPermanentStrict_attachesRedirectorWithPermanentStrictMode() { + Router router = new Router(new Context()); + + TemplateRoute route = router.redirectPermanentStrict("/old", "/new"); + + assertTrue(route.getNext() instanceof Redirector); + assertEquals(Redirector.MODE_CLIENT_PERMANENT_STRICT, + ((Redirector) route.getNext()).getMode()); + } +} diff --git a/org.restlet.java/org.restlet/src/test/java/org/restlet/engine/connector/PermanentRedirectWireTestCase.java b/org.restlet.java/org.restlet/src/test/java/org/restlet/engine/connector/PermanentRedirectWireTestCase.java new file mode 100644 index 0000000000..45ee742008 --- /dev/null +++ b/org.restlet.java/org.restlet/src/test/java/org/restlet/engine/connector/PermanentRedirectWireTestCase.java @@ -0,0 +1,89 @@ +/** + * Copyright 2005-2026 Qlik + *

+ * The content of this file is subject to the terms of the Apache 2.0 open + * source license available at https://www.opensource.org/licenses/apache-2.0 + *

+ * Restlet is a registered trademark of QlikTech International AB. + */ +package org.restlet.engine.connector; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.Socket; +import java.nio.charset.StandardCharsets; +import java.util.Locale; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.restlet.Component; +import org.restlet.Server; +import org.restlet.data.Protocol; +import org.restlet.engine.Engine; +import org.restlet.routing.Router; + +/** + * Tests, on the wire, the permanent redirections of a router: 301 may change the method of the + * request, 308 must keep it (RFC 9110 sections 15.4.2 and 15.4.9, #1746). + */ +class PermanentRedirectWireTestCase { + + private static final int TIMEOUT_MS = 10_000; + + private Component component; + + private Server server; + + @BeforeEach + void setUp() throws Exception { + component = new Component(); + server = component.getServers().add(Protocol.HTTP, 0); + Router router = new Router(component.getContext().createChildContext()); + router.redirectPermanent("/moved", "http://example.com/new"); + router.redirectPermanentStrict("/strict", "http://example.com/new"); + component.getDefaultHost().attachDefault(router); + component.start(); + } + + @AfterEach + void tearDown() throws Exception { + if (component != null) { + component.stop(); + } + Engine.clearThreadLocalVariables(); + } + + private String post(String path) throws IOException { + try (Socket socket = new Socket("localhost", server.getEphemeralPort())) { + socket.setSoTimeout(TIMEOUT_MS); + OutputStream out = socket.getOutputStream(); + out.write( + ("POST " + + path + + " HTTP/1.1\r\nHost: localhost\r\nContent-Length: 4\r\n" + + "Content-Type: text/plain\r\nConnection: close\r\n\r\nbody") + .getBytes(StandardCharsets.ISO_8859_1)); + out.flush(); + return new String(socket.getInputStream().readAllBytes(), StandardCharsets.ISO_8859_1); + } + } + + @Test + void post_toAStrictPermanentRedirection_answers308WithTheLocation() throws Exception { + String response = post("/strict"); + + assertTrue(response.startsWith("HTTP/1.1 308"), response); + assertTrue( + response.toLowerCase(Locale.ROOT).contains("\r\nlocation: http://example.com/new"), + response); + } + + @Test + void post_toAPermanentRedirection_stillAnswers301() throws Exception { + String response = post("/moved"); + + assertTrue(response.startsWith("HTTP/1.1 301"), response); + } +} diff --git a/org.restlet.java/org.restlet/src/test/java/org/restlet/routing/RedirectorTestCase.java b/org.restlet.java/org.restlet/src/test/java/org/restlet/routing/RedirectorTestCase.java index a26830406d..3cb8e84e4f 100644 --- a/org.restlet.java/org.restlet/src/test/java/org/restlet/routing/RedirectorTestCase.java +++ b/org.restlet.java/org.restlet/src/test/java/org/restlet/routing/RedirectorTestCase.java @@ -123,4 +123,19 @@ public void handle(Request req, Response resp) { assertEquals(List.of("http://backend/a/c/d"), seen); assertEquals("http://localhost/old", request.getResourceRef().toString()); } + + /** #1746: 308 keeps the method of the request, unlike 301. */ + @Test + void handle_clientPermanentStrictMode_redirectsWith308() { + Redirector redirector = + new Redirector(new Context(), "/new", Redirector.MODE_CLIENT_PERMANENT_STRICT); + Request request = new Request(Method.POST, "http://localhost/old"); + Response response = new Response(request); + + redirector.handle(request, response); + + assertEquals(Status.REDIRECTION_PERMANENT_STRICT, response.getStatus()); + assertEquals("http://localhost/new", response.getLocationRef().getTargetRef().toString()); + assertEquals(Method.POST, request.getMethod()); + } }