Skip to content

turn off redirects per request #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 6, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/main/java/com/ning/http/client/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,13 @@ public static interface EntityWriter {
*/
public boolean isRedirectEnabled();

/**
*
* @return <tt>true></tt> if request's redirectEnabled setting
* should be used in place of client's
*/
public boolean isRedirectOverrideSet();

/**
* Return Per request configuration.
*
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/ning/http/client/RequestBuilderBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/ning/http/util/AsyncHttpProviderUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down