Skip to content

Commit aff740c

Browse files
committed
Merge pull request AsyncHttpClient#38 from jloomis/master
turn off redirects per request
2 parents 2206977 + 4e6e58f commit aff740c

File tree

5 files changed

+36
-4
lines changed

5 files changed

+36
-4
lines changed

src/main/java/com/ning/http/client/Request.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,13 @@ public static interface EntityWriter {
200200
*/
201201
public boolean isRedirectEnabled();
202202

203+
/**
204+
*
205+
* @return <tt>true></tt> if request's redirectEnabled setting
206+
* should be used in place of client's
207+
*/
208+
public boolean isRedirectOverrideSet();
209+
203210
/**
204211
* Return Per request configuration.
205212
*

src/main/java/com/ning/http/client/RequestBuilderBase.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private static final class RequestImpl implements Request {
6161
public ProxyServer proxyServer;
6262
private Realm realm;
6363
private File file;
64-
private boolean followRedirects;
64+
private Boolean followRedirects;
6565
private PerRequestConfig perRequestConfig;
6666
private long rangeOffset = 0;
6767
public String charset;
@@ -92,7 +92,7 @@ public RequestImpl(Request prototype) {
9292
this.proxyServer = prototype.getProxyServer();
9393
this.realm = prototype.getRealm();
9494
this.file = prototype.getFile();
95-
this.followRedirects = prototype.isRedirectEnabled();
95+
this.followRedirects = prototype.isRedirectOverrideSet()? prototype.isRedirectEnabled() : null;
9696
this.perRequestConfig = prototype.getPerRequestConfig();
9797
this.rangeOffset = prototype.getRangeOffset();
9898
this.charset = prototype.getBodyEncoding();
@@ -259,7 +259,11 @@ public File getFile() {
259259
}
260260

261261
public boolean isRedirectEnabled() {
262-
return followRedirects;
262+
return (followRedirects != null && followRedirects);
263+
}
264+
265+
public boolean isRedirectOverrideSet(){
266+
return followRedirects != null;
263267
}
264268

265269
public PerRequestConfig getPerRequestConfig() {

src/main/java/com/ning/http/client/providers/netty/NettyAsyncHttpProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,7 @@ public Object call() throws Exception {
12291229
return;
12301230
}
12311231

1232-
boolean redirectEnabled = request.isRedirectEnabled() ? true : config.isRedirectEnabled();
1232+
boolean redirectEnabled = request.isRedirectOverrideSet()? request.isRedirectEnabled() : config.isRedirectEnabled();
12331233
if (redirectEnabled && (statusCode == 302 || statusCode == 301 || statusCode == 307)) {
12341234

12351235
if (future.incrementAndGetCurrentRedirectCount() < config.getMaxRedirects()) {

src/main/java/com/ning/http/util/AsyncHttpProviderUtils.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ public final static String getHost(URI uri) {
218218
}
219219

220220
public final static URI getRedirectUri(URI uri, String location) {
221+
if(location == null)
222+
throw new IllegalArgumentException("URI " + uri + " was redirected to null location");
221223
URI newUri = uri.resolve(location);
222224

223225
String scheme = newUri.getScheme();

src/test/java/com/ning/http/client/async/PerRequestRelative302Test.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,25 @@ public void redirected302Test() throws Throwable {
112112
assertTrue(baseUrl.matches(anyMicrosoftPage), "response does not show redirection to " + anyMicrosoftPage);
113113
}
114114

115+
@Test(groups = {"online", "default_provider"})
116+
public void notRedirected302Test() throws Throwable {
117+
isSet.getAndSet(false);
118+
AsyncHttpClientConfig cg = new AsyncHttpClientConfig.Builder().setFollowRedirects(true).build();
119+
AsyncHttpClient c = getAsyncHttpClient(cg);
120+
121+
122+
// once
123+
Response response = c.prepareGet(getTargetUrl())
124+
.setFollowRedirects(false)
125+
.setHeader("X-redirect", "http://www.microsoft.com/")
126+
.execute().get();
127+
128+
assertNotNull(response);
129+
assertEquals(response.getStatusCode(), 302);
130+
131+
c.close();
132+
}
133+
115134
private String getBaseUrl(URI uri) {
116135
String url = uri.toString();
117136
int port = uri.getPort();

0 commit comments

Comments
 (0)