diff --git a/src/main/java/com/ning/http/client/Request.java b/src/main/java/com/ning/http/client/Request.java index ad90b9e7bb..52ef0cbb72 100644 --- a/src/main/java/com/ning/http/client/Request.java +++ b/src/main/java/com/ning/http/client/Request.java @@ -200,6 +200,13 @@ public static interface EntityWriter { */ public boolean isRedirectEnabled(); + /** + * + * @return true> if request's redirectEnabled setting + * should be used in place of client's + */ + public boolean isRedirectOverrideSet(); + /** * Return Per request configuration. * diff --git a/src/main/java/com/ning/http/client/RequestBuilderBase.java b/src/main/java/com/ning/http/client/RequestBuilderBase.java index 1e7efe1e5f..2a80fd409a 100644 --- a/src/main/java/com/ning/http/client/RequestBuilderBase.java +++ b/src/main/java/com/ning/http/client/RequestBuilderBase.java @@ -61,7 +61,7 @@ private static final class RequestImpl implements Request { public ProxyServer proxyServer; private Realm realm; private File file; - private boolean followRedirects; + private Boolean followRedirects; private PerRequestConfig perRequestConfig; private long rangeOffset = 0; public String charset; @@ -92,7 +92,7 @@ public RequestImpl(Request prototype) { this.proxyServer = prototype.getProxyServer(); this.realm = prototype.getRealm(); this.file = prototype.getFile(); - this.followRedirects = prototype.isRedirectEnabled(); + this.followRedirects = prototype.isRedirectOverrideSet()? prototype.isRedirectEnabled() : null; this.perRequestConfig = prototype.getPerRequestConfig(); this.rangeOffset = prototype.getRangeOffset(); this.charset = prototype.getBodyEncoding(); @@ -259,7 +259,11 @@ public File getFile() { } public boolean isRedirectEnabled() { - return followRedirects; + return (followRedirects != null && followRedirects); + } + + public boolean isRedirectOverrideSet(){ + return followRedirects != null; } public PerRequestConfig getPerRequestConfig() { diff --git a/src/main/java/com/ning/http/client/providers/netty/NettyAsyncHttpProvider.java b/src/main/java/com/ning/http/client/providers/netty/NettyAsyncHttpProvider.java index 0863718573..c07046f67a 100644 --- a/src/main/java/com/ning/http/client/providers/netty/NettyAsyncHttpProvider.java +++ b/src/main/java/com/ning/http/client/providers/netty/NettyAsyncHttpProvider.java @@ -1229,7 +1229,7 @@ public Object call() throws Exception { return; } - boolean redirectEnabled = request.isRedirectEnabled() ? true : config.isRedirectEnabled(); + boolean redirectEnabled = request.isRedirectOverrideSet()? request.isRedirectEnabled() : config.isRedirectEnabled(); if (redirectEnabled && (statusCode == 302 || statusCode == 301 || statusCode == 307)) { if (future.incrementAndGetCurrentRedirectCount() < config.getMaxRedirects()) { diff --git a/src/main/java/com/ning/http/util/AsyncHttpProviderUtils.java b/src/main/java/com/ning/http/util/AsyncHttpProviderUtils.java index 8f85e2f967..867a60b01f 100644 --- a/src/main/java/com/ning/http/util/AsyncHttpProviderUtils.java +++ b/src/main/java/com/ning/http/util/AsyncHttpProviderUtils.java @@ -218,6 +218,8 @@ public final static String getHost(URI uri) { } public final static URI getRedirectUri(URI uri, String location) { + if(location == null) + throw new IllegalArgumentException("URI " + uri + " was redirected to null location"); URI newUri = uri.resolve(location); String scheme = newUri.getScheme(); diff --git a/src/test/java/com/ning/http/client/async/PerRequestRelative302Test.java b/src/test/java/com/ning/http/client/async/PerRequestRelative302Test.java index 85c6629477..756a258339 100644 --- a/src/test/java/com/ning/http/client/async/PerRequestRelative302Test.java +++ b/src/test/java/com/ning/http/client/async/PerRequestRelative302Test.java @@ -112,6 +112,25 @@ public void redirected302Test() throws Throwable { assertTrue(baseUrl.matches(anyMicrosoftPage), "response does not show redirection to " + anyMicrosoftPage); } + @Test(groups = {"online", "default_provider"}) + public void notRedirected302Test() throws Throwable { + isSet.getAndSet(false); + AsyncHttpClientConfig cg = new AsyncHttpClientConfig.Builder().setFollowRedirects(true).build(); + AsyncHttpClient c = getAsyncHttpClient(cg); + + + // once + Response response = c.prepareGet(getTargetUrl()) + .setFollowRedirects(false) + .setHeader("X-redirect", "http://www.microsoft.com/") + .execute().get(); + + assertNotNull(response); + assertEquals(response.getStatusCode(), 302); + + c.close(); + } + private String getBaseUrl(URI uri) { String url = uri.toString(); int port = uri.getPort();