You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by mi...@apache.org on 2020/02/22 13:46:05 UTC
[httpcomponents-client] 01/01: [HTTPCLIENT-2053] Add
SC_PERMANENT_REDIRECT (408) to DefaultRedirectStrategy
This is an automated email from the ASF dual-hosted git repository.
michaelo pushed a commit to branch HTTPCLIENT-2053
in repository https://gitbox.apache.org/repos/asf/httpcomponents-client.git
commit 55bd1654fc58ee4fb7a97007a2840b4f74550bc5
Author: Michael Osipov <mi...@apache.org>
AuthorDate: Sat Feb 22 14:45:13 2020 +0100
[HTTPCLIENT-2053] Add SC_PERMANENT_REDIRECT (408) to DefaultRedirectStrategy
This closes #215
---
.../http/impl/client/DefaultRedirectStrategy.java | 5 +++-
.../impl/client/TestDefaultRedirectStrategy.java | 33 ++++++++++++++++++++++
2 files changed, 37 insertions(+), 1 deletion(-)
diff --git a/httpclient/src/main/java/org/apache/http/impl/client/DefaultRedirectStrategy.java b/httpclient/src/main/java/org/apache/http/impl/client/DefaultRedirectStrategy.java
index d874601..c2635fa 100644
--- a/httpclient/src/main/java/org/apache/http/impl/client/DefaultRedirectStrategy.java
+++ b/httpclient/src/main/java/org/apache/http/impl/client/DefaultRedirectStrategy.java
@@ -74,6 +74,8 @@ public class DefaultRedirectStrategy implements RedirectStrategy {
private final Log log = LogFactory.getLog(getClass());
+ public static final int SC_PERMANENT_REDIRECT = 308;
+
/**
* @deprecated (4.3) use {@link org.apache.http.client.protocol.HttpClientContext#REDIRECT_LOCATIONS}.
*/
@@ -120,6 +122,7 @@ public class DefaultRedirectStrategy implements RedirectStrategy {
return isRedirectable(method) && locationHeader != null;
case HttpStatus.SC_MOVED_PERMANENTLY:
case HttpStatus.SC_TEMPORARY_REDIRECT:
+ case SC_PERMANENT_REDIRECT:
return isRedirectable(method);
case HttpStatus.SC_SEE_OTHER:
return true;
@@ -225,7 +228,7 @@ public class DefaultRedirectStrategy implements RedirectStrategy {
return new HttpGet(uri);
} else {
final int status = response.getStatusLine().getStatusCode();
- return status == HttpStatus.SC_TEMPORARY_REDIRECT
+ return (status == HttpStatus.SC_TEMPORARY_REDIRECT || status == SC_PERMANENT_REDIRECT)
? RequestBuilder.copy(request).setUri(uri).build()
: new HttpGet(uri);
}
diff --git a/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultRedirectStrategy.java b/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultRedirectStrategy.java
index 78e09b5..e46ca82 100644
--- a/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultRedirectStrategy.java
+++ b/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultRedirectStrategy.java
@@ -113,6 +113,18 @@ public class TestDefaultRedirectStrategy {
}
@Test
+ public void testIsRedirectedPermanentRedirect() throws Exception {
+ final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
+ final HttpClientContext context = HttpClientContext.create();
+ final HttpGet httpget = new HttpGet("http://localhost/");
+ final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
+ DefaultRedirectStrategy.SC_PERMANENT_REDIRECT, "Redirect");
+ Assert.assertTrue(redirectStrategy.isRedirected(httpget, response, context));
+ final HttpPost httppost = new HttpPost("http://localhost/");
+ Assert.assertFalse(redirectStrategy.isRedirected(httppost, response, context));
+ }
+
+ @Test
public void testIsRedirectedSeeOther() throws Exception {
final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
final HttpClientContext context = HttpClientContext.create();
@@ -385,6 +397,27 @@ public class TestDefaultRedirectStrategy {
Assert.assertSame(entity, ((HttpEntityEnclosingRequest) redirect2).getEntity());
}
+ @Test
+ public void testGetRedirectRequestForPermanentRedirect() throws Exception {
+ final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
+ final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
+ DefaultRedirectStrategy.SC_PERMANENT_REDIRECT, "Permanent Redirect");
+ response.addHeader("Location", "http://localhost/stuff");
+ final HttpContext context1 = new BasicHttpContext();
+ final HttpUriRequest redirect1 = redirectStrategy.getRedirect(
+ new HttpTrace("http://localhost/"), response, context1);
+ Assert.assertEquals("TRACE", redirect1.getMethod());
+ final HttpContext context2 = new BasicHttpContext();
+ final HttpPost httppost = new HttpPost("http://localhost/");
+ final HttpEntity entity = new BasicHttpEntity();
+ httppost.setEntity(entity);
+ final HttpUriRequest redirect2 = redirectStrategy.getRedirect(
+ httppost, response, context2);
+ Assert.assertEquals("POST", redirect2.getMethod());
+ Assert.assertTrue(redirect2 instanceof HttpEntityEnclosingRequest);
+ Assert.assertSame(entity, ((HttpEntityEnclosingRequest) redirect2).getEntity());
+ }
+
@Test(expected=ProtocolException.class)
public void testCreateLocationURIInvalid() throws Exception {
final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();