HTTPCLIENT-1248: Default and lax redirect strategies should not convert requests redirected with 307 status to GET method
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1396793 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
15301ffbba
commit
46aca9def4
|
@ -1,6 +1,10 @@
|
|||
Changes since 4.2.1
|
||||
-------------------
|
||||
|
||||
* [HTTPCLIENT-1248]: Default and lax redirect strategies should not convert requests redirected
|
||||
with 307 status to GET method.
|
||||
Contributed by Oleg Kalnichevski <olegk at apache.org>
|
||||
|
||||
* [HTTPCLIENT-1215] BasicAuthCache does not take default ports into consideration when
|
||||
looking up cached authentication details by HttpHost key.
|
||||
Contributed by Oleg Kalnichevski <olegk at apache.org>
|
||||
|
|
|
@ -35,6 +35,7 @@ import org.apache.http.annotation.Immutable;
|
|||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpEntityEnclosingRequest;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.HttpRequest;
|
||||
import org.apache.http.HttpResponse;
|
||||
|
@ -42,8 +43,15 @@ import org.apache.http.HttpStatus;
|
|||
import org.apache.http.ProtocolException;
|
||||
import org.apache.http.client.CircularRedirectException;
|
||||
import org.apache.http.client.RedirectStrategy;
|
||||
import org.apache.http.client.methods.HttpDelete;
|
||||
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpHead;
|
||||
import org.apache.http.client.methods.HttpOptions;
|
||||
import org.apache.http.client.methods.HttpPatch;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.client.methods.HttpPut;
|
||||
import org.apache.http.client.methods.HttpTrace;
|
||||
import org.apache.http.client.methods.HttpUriRequest;
|
||||
import org.apache.http.client.params.ClientPNames;
|
||||
import org.apache.http.client.utils.URIUtils;
|
||||
|
@ -210,9 +218,35 @@ public class DefaultRedirectStrategy implements RedirectStrategy {
|
|||
String method = request.getRequestLine().getMethod();
|
||||
if (method.equalsIgnoreCase(HttpHead.METHOD_NAME)) {
|
||||
return new HttpHead(uri);
|
||||
} else if (method.equalsIgnoreCase(HttpGet.METHOD_NAME)) {
|
||||
return new HttpGet(uri);
|
||||
} else {
|
||||
int status = response.getStatusLine().getStatusCode();
|
||||
if (status == HttpStatus.SC_TEMPORARY_REDIRECT) {
|
||||
if (method.equalsIgnoreCase(HttpPost.METHOD_NAME)) {
|
||||
return copyEntity(new HttpPost(uri), request);
|
||||
} else if (method.equalsIgnoreCase(HttpPut.METHOD_NAME)) {
|
||||
return copyEntity(new HttpPut(uri), request);
|
||||
} else if (method.equalsIgnoreCase(HttpDelete.METHOD_NAME)) {
|
||||
return new HttpDelete(uri);
|
||||
} else if (method.equalsIgnoreCase(HttpTrace.METHOD_NAME)) {
|
||||
return new HttpTrace(uri);
|
||||
} else if (method.equalsIgnoreCase(HttpOptions.METHOD_NAME)) {
|
||||
return new HttpOptions(uri);
|
||||
} else if (method.equalsIgnoreCase(HttpPatch.METHOD_NAME)) {
|
||||
return copyEntity(new HttpPatch(uri), request);
|
||||
}
|
||||
}
|
||||
return new HttpGet(uri);
|
||||
}
|
||||
}
|
||||
|
||||
private HttpUriRequest copyEntity(
|
||||
final HttpEntityEnclosingRequestBase redirect, final HttpRequest original) {
|
||||
if (original instanceof HttpEntityEnclosingRequest) {
|
||||
redirect.setEntity(((HttpEntityEnclosingRequest) original).getEntity());
|
||||
}
|
||||
return redirect;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -28,6 +28,8 @@ package org.apache.http.impl.client;
|
|||
import java.net.URI;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpEntityEnclosingRequest;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.HttpStatus;
|
||||
|
@ -38,8 +40,10 @@ import org.apache.http.client.methods.HttpGet;
|
|||
import org.apache.http.client.methods.HttpHead;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.client.methods.HttpPut;
|
||||
import org.apache.http.client.methods.HttpTrace;
|
||||
import org.apache.http.client.methods.HttpUriRequest;
|
||||
import org.apache.http.client.params.ClientPNames;
|
||||
import org.apache.http.entity.BasicHttpEntity;
|
||||
import org.apache.http.message.BasicHttpResponse;
|
||||
import org.apache.http.protocol.BasicHttpContext;
|
||||
import org.apache.http.protocol.ExecutionContext;
|
||||
|
@ -345,4 +349,25 @@ public class TestDefaultRedirectStrategy {
|
|||
Assert.assertEquals("HEAD", redirect3.getMethod());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetRedirectRequestForTemporaryRedirect() throws Exception {
|
||||
DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
|
||||
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
|
||||
HttpStatus.SC_TEMPORARY_REDIRECT, "Temporary Redirect");
|
||||
response.addHeader("Location", "http://localhost/stuff");
|
||||
HttpContext context1 = new BasicHttpContext();
|
||||
HttpUriRequest redirect1 = redirectStrategy.getRedirect(
|
||||
new HttpTrace("http://localhost/"), response, context1);
|
||||
Assert.assertEquals("TRACE", redirect1.getMethod());
|
||||
HttpContext context2 = new BasicHttpContext();
|
||||
HttpPost httppost = new HttpPost("http://localhost/");
|
||||
HttpEntity entity = new BasicHttpEntity();
|
||||
httppost.setEntity(entity);
|
||||
HttpUriRequest redirect2 = redirectStrategy.getRedirect(
|
||||
httppost, response, context2);
|
||||
Assert.assertEquals("POST", redirect2.getMethod());
|
||||
Assert.assertTrue(redirect2 instanceof HttpEntityEnclosingRequest);
|
||||
Assert.assertSame(entity, ((HttpEntityEnclosingRequest) redirect2).getEntity());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue