diff --git a/changes.md b/changes.md
index d4b5aa66d4..26352cf6b6 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
- Upgraded Jetty library to version 12.1.14. Issue #1515.
- Upgraded Jackson library to version 2.22.4.
diff --git a/org.restlet/src/main/java/org/restlet/Response.java b/org.restlet/src/main/java/org/restlet/Response.java
index 3790d3b65d..b1dd2c3667 100644
--- a/org.restlet/src/main/java/org/restlet/Response.java
+++ b/org.restlet/src/main/java/org/restlet/Response.java
@@ -552,8 +552,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.
*/
@@ -563,8 +564,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 {@link Request#getResourceRef()} and {@link
@@ -577,6 +579,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 a POST-activated script to
diff --git a/org.restlet/src/main/java/org/restlet/resource/ServerResource.java b/org.restlet/src/main/java/org/restlet/resource/ServerResource.java
index 3741a0afdb..7aeda49410 100644
--- a/org.restlet/src/main/java/org/restlet/resource/ServerResource.java
+++ b/org.restlet/src/main/java/org/restlet/resource/ServerResource.java
@@ -1410,8 +1410,8 @@ protected Representation put(Representation representation, Variant variant)
}
/**
- * 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.
*/
@@ -1422,8 +1422,8 @@ 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 {@link Request#getResourceRef()} and {@link
@@ -1437,6 +1437,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 a POST-activated script to
diff --git a/org.restlet/src/main/java/org/restlet/routing/Redirector.java b/org.restlet/src/main/java/org/restlet/routing/Redirector.java
index 9a7b7425bf..d475d5c7a8 100644
--- a/org.restlet/src/main/java/org/restlet/routing/Redirector.java
+++ b/org.restlet/src/main/java/org/restlet/routing/Redirector.java
@@ -24,9 +24,9 @@
/**
* 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 server-side redirections, similar to a reverse proxy ( {@link #MODE_SERVER_OUTBOUND} and
- * {@link #MODE_SERVER_INBOUND}).
+ * {@link #MODE_CLIENT_PERMANENT}, {@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}).
*
* When setting the redirection URIs, you can also use special URI variables to reuse most
* properties from the original request as well as URI template variables. For a complete list of
@@ -59,11 +59,24 @@ 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. Note: this is a client-side
@@ -260,6 +273,18 @@ public void handle(Request request, Response response) {
response.redirectPermanent(targetRef);
break;
+ case MODE_CLIENT_PERMANENT_STRICT:
+ if (request.isLoggable()) {
+ getLogger()
+ .fine(
+ () ->
+ "Permanently redirecting client, same method, to: "
+ + targetRef);
+ }
+
+ response.redirectPermanentStrict(targetRef);
+ break;
+
case MODE_CLIENT_FOUND:
if (request.isLoggable()) {
getLogger().fine(() -> "Redirecting client to found location: " + targetRef);
diff --git a/org.restlet/src/main/java/org/restlet/routing/Router.java b/org.restlet/src/main/java/org/restlet/routing/Router.java
index 71fc2ca677..445cb71bd6 100644
--- a/org.restlet/src/main/java/org/restlet/routing/Router.java
+++ b/org.restlet/src/main/java/org/restlet/routing/Router.java
@@ -561,8 +561,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.
@@ -575,6 +576,21 @@ public TemplateRoute redirectPermanent(String pathTemplate, String targetUri) {
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 method on that resource. This method
diff --git a/org.restlet/src/main/java/org/restlet/util/WrapperResponse.java b/org.restlet/src/main/java/org/restlet/util/WrapperResponse.java
index 1495fa3b9b..f522d70aa9 100644
--- a/org.restlet/src/main/java/org/restlet/util/WrapperResponse.java
+++ b/org.restlet/src/main/java/org/restlet/util/WrapperResponse.java
@@ -253,8 +253,8 @@ 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 +264,8 @@ 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 +274,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 a POST-activated script to
diff --git a/org.restlet/src/test/java/org/restlet/ResponseRedirectionTestCase.java b/org.restlet/src/test/java/org/restlet/ResponseRedirectionTestCase.java
new file mode 100644
index 0000000000..4ab0b31172
--- /dev/null
+++ b/org.restlet/src/test/java/org/restlet/ResponseRedirectionTestCase.java
@@ -0,0 +1,62 @@
+/**
+ * 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 org.junit.jupiter.api.Test; +import org.restlet.data.Method; +import org.restlet.data.Reference; +import org.restlet.data.Status; + +/** Tests the redirection helpers of {@link Response}, including the 308 one (#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()); + } +} diff --git a/org.restlet/src/test/java/org/restlet/engine/connector/PermanentRedirectWireTestCase.java b/org.restlet/src/test/java/org/restlet/engine/connector/PermanentRedirectWireTestCase.java new file mode 100644 index 0000000000..e5e464441b --- /dev/null +++ b/org.restlet/src/test/java/org/restlet/engine/connector/PermanentRedirectWireTestCase.java @@ -0,0 +1,90 @@ +/** + * 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.Context; +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 Server server; + + @BeforeEach + void setUp() throws Exception { + Engine.clearThreadLocalVariables(); + Engine.register(false); + Engine.getInstance().getRegisteredServers().add(new HttpServerHelper(null)); + + Context context = new Context(); + Router router = new Router(context); + router.redirectPermanent("/moved", "http://example.com/new"); + router.redirectPermanentStrict("/strict", "http://example.com/new"); + server = new Server(context, Protocol.HTTP, 0, router); + server.start(); + } + + @AfterEach + void tearDown() throws Exception { + if (server != null) { + server.stop(); + } + Engine.clearThreadLocalVariables(); + } + + private String post(String path) throws IOException { + try (Socket socket = new Socket("localhost", server.getActualPort())) { + 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/src/test/java/org/restlet/resource/ServerResourceTestCase.java b/org.restlet/src/test/java/org/restlet/resource/ServerResourceTestCase.java index f6058b2831..ede379de87 100644 --- a/org.restlet/src/test/java/org/restlet/resource/ServerResourceTestCase.java +++ b/org.restlet/src/test/java/org/restlet/resource/ServerResourceTestCase.java @@ -488,4 +488,23 @@ void getOnSentAndSetOnSent_roundTrip() { assertEquals(callback, resource.getOnSent()); } + + @Test + void redirectPermanentStrict_withStringUri_setsLocationAnd308() { + EchoServerResource resource = createInitializedResource(Method.GET); + + resource.redirectPermanentStrict("http://localhost/target"); + + assertEquals(Status.REDIRECTION_PERMANENT_STRICT, resource.getStatus()); + assertEquals("http://localhost/target", resource.getLocationRef().toString()); + } + + @Test + void redirectPermanentStrict_withReference_setsLocationAnd308() { + EchoServerResource resource = createInitializedResource(Method.GET); + + resource.redirectPermanentStrict(new org.restlet.data.Reference("http://localhost/target")); + + assertEquals(Status.REDIRECTION_PERMANENT_STRICT, resource.getStatus()); + } } diff --git a/org.restlet/src/test/java/org/restlet/routing/RedirectorTestCase.java b/org.restlet/src/test/java/org/restlet/routing/RedirectorTestCase.java index 2b15449f38..8da5de38d9 100644 --- a/org.restlet/src/test/java/org/restlet/routing/RedirectorTestCase.java +++ b/org.restlet/src/test/java/org/restlet/routing/RedirectorTestCase.java @@ -375,4 +375,35 @@ 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()); + } + + @Test + void clientPermanentStrictMode_isDistinctFromTheOtherModes() { + int[] others = { + Redirector.MODE_CLIENT_PERMANENT, + Redirector.MODE_CLIENT_FOUND, + Redirector.MODE_CLIENT_SEE_OTHER, + Redirector.MODE_CLIENT_TEMPORARY, + Redirector.MODE_SERVER_INBOUND, + Redirector.MODE_SERVER_OUTBOUND + }; + for (int other : others) { + org.junit.jupiter.api.Assertions.assertNotEquals( + Redirector.MODE_CLIENT_PERMANENT_STRICT, other); + } + } } diff --git a/org.restlet/src/test/java/org/restlet/routing/RouterTestCase.java b/org.restlet/src/test/java/org/restlet/routing/RouterTestCase.java index 58127e7c24..d3f55c066f 100644 --- a/org.restlet/src/test/java/org/restlet/routing/RouterTestCase.java +++ b/org.restlet/src/test/java/org/restlet/routing/RouterTestCase.java @@ -363,4 +363,15 @@ void settersRoundTrip() { router.setRoutes(routes); assertSame(routes, router.getRoutes()); } + + @Test + void 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/src/test/java/org/restlet/util/WrapperResponseTestCase.java b/org.restlet/src/test/java/org/restlet/util/WrapperResponseTestCase.java index 0ec3d80d7c..386805bccf 100644 --- a/org.restlet/src/test/java/org/restlet/util/WrapperResponseTestCase.java +++ b/org.restlet/src/test/java/org/restlet/util/WrapperResponseTestCase.java @@ -138,6 +138,12 @@ void redirectMethods_delegateToWrappedResponse() { wrapper.redirectPermanent(new Reference("http://localhost/permanent2")); assertEquals(Status.REDIRECTION_PERMANENT, wrapped.getStatus()); + 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()); + wrapper.redirectSeeOther("http://localhost/other"); assertEquals(Status.REDIRECTION_SEE_OTHER, wrapped.getStatus());