Issue 177: support scheme-only redirects

This commit is contained in:
Adrian Cole 2010-02-21 10:43:00 -08:00
parent 55c8ce03ad
commit a72efaea6d
8 changed files with 46 additions and 11 deletions

View File

@ -66,7 +66,8 @@ public class AWSRedirectionRetryHandler extends RedirectionRetryHandler {
// http://developer.amazonwebservices.com/connect/thread.jspa?messageID=72287&#72287 // http://developer.amazonwebservices.com/connect/thread.jspa?messageID=72287&#72287
return backoffHandler.shouldRetryRequest(command, response); return backoffHandler.shouldRetryRequest(command, response);
} else { } else {
command.changeHostAndPortTo(host, command.getRequest().getEndpoint().getPort()); command.changeSchemeHostAndPortTo(command.getRequest().getEndpoint()
.getScheme(), host, command.getRequest().getEndpoint().getPort());
} }
return true; return true;
} else { } else {

View File

@ -94,7 +94,7 @@ public class S3UtilsTest {
return false; return false;
} }
public void changeHostAndPortTo(String host, int port) { public void changeSchemeHostAndPortTo(String scheme, String host, int port) {
} }
public void changeToGETRequest() { public void changeToGETRequest() {

View File

@ -441,7 +441,7 @@ public class StubAsyncBlobStore extends BaseAsyncBlobStore {
return false; return false;
} }
public void changeHostAndPortTo(String host, int port) { public void changeSchemeHostAndPortTo(String scheme, String host, int port) {
} }
public void changeToGETRequest() { public void changeToGETRequest() {

View File

@ -18,7 +18,6 @@
*/ */
package org.jclouds.http; package org.jclouds.http;
/** /**
* Command whose endpoint is an http service. * Command whose endpoint is an http service.
* *
@ -48,8 +47,10 @@ public interface HttpCommand {
/** /**
* change the destination of the current http command. typically used in handling redirects. * change the destination of the current http command. typically used in handling redirects.
*
* @param string
*/ */
void changeHostAndPortTo(String host, int port); void changeSchemeHostAndPortTo(String scheme, String host, int port);
/** /**
* change method from GET to HEAD. typically used in handling redirects. * change method from GET to HEAD. typically used in handling redirects.

View File

@ -84,8 +84,9 @@ public class TransformingHttpCommandImpl<T> implements TransformingHttpCommand<T
* This also removes the Host header in order to avoid ssl problems. * This also removes the Host header in order to avoid ssl problems.
*/ */
@Override @Override
public void changeHostAndPortTo(String host, int port) { public void changeSchemeHostAndPortTo(String scheme, String host, int port) {
UriBuilder builder = UriBuilder.fromUri(request.getEndpoint()); UriBuilder builder = UriBuilder.fromUri(request.getEndpoint());
builder.scheme(scheme);
builder.host(host); builder.host(host);
builder.port(port); builder.port(port);
request.setEndpoint(builder.build()); request.setEndpoint(builder.build());

View File

@ -64,7 +64,8 @@ public class RedirectionRetryHandler implements HttpRetryHandler {
String hostHeader = response.getFirstHeaderOrNull(HttpHeaders.LOCATION); String hostHeader = response.getFirstHeaderOrNull(HttpHeaders.LOCATION);
if (hostHeader != null && command.incrementRedirectCount() < retryCountLimit) { if (hostHeader != null && command.incrementRedirectCount() < retryCountLimit) {
URI redirectionUrl = UriBuilder.fromUri(hostHeader).build(); URI redirectionUrl = UriBuilder.fromUri(hostHeader).build();
if (redirectionUrl.getHost().equals(command.getRequest().getEndpoint().getHost()) if (redirectionUrl.getScheme().equals(command.getRequest().getEndpoint().getScheme())
&& redirectionUrl.getHost().equals(command.getRequest().getEndpoint().getHost())
&& redirectionUrl.getPort() == command.getRequest().getEndpoint().getPort()) { && redirectionUrl.getPort() == command.getRequest().getEndpoint().getPort()) {
if (!redirectionUrl.getPath().equals(command.getRequest().getEndpoint().getPath())) { if (!redirectionUrl.getPath().equals(command.getRequest().getEndpoint().getPath())) {
command.changePathTo(redirectionUrl.getPath()); command.changePathTo(redirectionUrl.getPath());
@ -72,7 +73,8 @@ public class RedirectionRetryHandler implements HttpRetryHandler {
return backoffHandler.shouldRetryRequest(command, response); return backoffHandler.shouldRetryRequest(command, response);
} }
} else { } else {
command.changeHostAndPortTo(redirectionUrl.getHost(), redirectionUrl.getPort()); command.changeSchemeHostAndPortTo(redirectionUrl.getScheme(), redirectionUrl.getHost(),
redirectionUrl.getPort());
} }
return true; return true;
} else { } else {

View File

@ -18,17 +18,46 @@
*/ */
package org.jclouds.http; package org.jclouds.http;
import static org.easymock.EasyMock.expect;
import static org.easymock.classextension.EasyMock.createMock;
import static org.easymock.classextension.EasyMock.replay;
import static org.testng.Assert.assertEquals;
import java.net.URI;
import java.util.Collections;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.ext.RuntimeDelegate;
import org.jclouds.rest.internal.GeneratedHttpRequest;
import org.jclouds.rest.internal.RuntimeDelegateImpl;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
/** /**
* *
* @author Adrian Cole * @author Adrian Cole
*/ */
public class TransformingHttpCommandImplTest { public class TransformingHttpCommandImplTest {
@SuppressWarnings("unchecked")
@Test @Test
public void testToString() { public void testChangeSchemeHostAndPortTo() {
// TODO RuntimeDelegate.setInstance(new RuntimeDelegateImpl());
GeneratedHttpRequest<?> request = createMock(GeneratedHttpRequest.class);
TransformingHttpCommandImpl<?> command = new TransformingHttpCommandImpl(null, request,
null);
expect(request.getEndpoint()).andReturn(URI.create("http://localhost/mypath"));
request.setEndpoint(URI.create("https://remotehost:443/mypath"));
Multimap<String, String> headers = HashMultimap.create();
expect(request.getHeaders()).andReturn(headers);
replay(request);
command.changeSchemeHostAndPortTo("https", "remotehost", 443);
assertEquals(headers.get(HttpHeaders.HOST), Collections.singletonList("remotehost"));
} }
@Test @Test

View File

@ -160,7 +160,8 @@ public class GaeHttpCommandExecutorService extends BaseHttpCommandExecutorServic
HttpRequest request = command.getRequest(); HttpRequest request = command.getRequest();
String hostHeader = request.getFirstHeaderOrNull(HttpHeaders.HOST); String hostHeader = request.getFirstHeaderOrNull(HttpHeaders.HOST);
if (hostHeader != null) { if (hostHeader != null) {
command.changeHostAndPortTo(hostHeader, request.getEndpoint().getPort()); command.changeSchemeHostAndPortTo(request.getEndpoint().getScheme(), hostHeader, request
.getEndpoint().getPort());
} }
} }