Issue 52: worked around host header issue in gae

git-svn-id: http://jclouds.googlecode.com/svn/trunk@1088 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-06-07 23:13:38 +00:00
parent 498c1dca20
commit bf3ba3e855
2 changed files with 25 additions and 13 deletions

View File

@ -50,6 +50,7 @@ import com.google.appengine.api.urlfetch.HTTPResponse;
import com.google.appengine.api.urlfetch.URLFetchService; import com.google.appengine.api.urlfetch.URLFetchService;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.name.Named;
/** /**
* Google App Engine version of {@link HttpFutureCommandClient} * Google App Engine version of {@link HttpFutureCommandClient}
@ -58,12 +59,17 @@ import com.google.inject.Inject;
*/ */
public class URLFetchServiceClient extends BaseHttpFutureCommandClient { public class URLFetchServiceClient extends BaseHttpFutureCommandClient {
private final URLFetchService urlFetchService; private final URLFetchService urlFetchService;
private final int port;
private final boolean isSecure;
@Inject @Inject
public URLFetchServiceClient(URL target, URLFetchService urlFetchService) public URLFetchServiceClient(@Named(HttpConstants.PROPERTY_HTTP_PORT) int port,
throws MalformedURLException { @Named(HttpConstants.PROPERTY_HTTP_SECURE) boolean isSecure, URL target,
URLFetchService urlFetchService) throws MalformedURLException {
super(target); super(target);
this.urlFetchService = urlFetchService; this.urlFetchService = urlFetchService;
this.port = port;
this.isSecure = isSecure;
} }
public void submit(HttpFutureCommand<?> command) { public void submit(HttpFutureCommand<?> command) {
@ -74,13 +80,6 @@ public class URLFetchServiceClient extends BaseHttpFutureCommandClient {
for (HttpRequestFilter filter : requestFilters) { for (HttpRequestFilter filter : requestFilters) {
filter.filter(request); filter.filter(request);
} }
String hostHeader = request.getFirstHeaderOrNull(HttpConstants.HOST);
if (hostHeader != null) {
logger
.warn(
"Note that as of GAE SDK version 1.2.1, host headers are stripped. you passed %1$s",
hostHeader);
}
HttpResponse response = null; HttpResponse response = null;
for (;;) { for (;;) {
logger.trace("%1$s - converting request %2$s", target, request); logger.trace("%1$s - converting request %2$s", target, request);
@ -157,13 +156,26 @@ public class URLFetchServiceClient extends BaseHttpFutureCommandClient {
@VisibleForTesting @VisibleForTesting
HTTPRequest convert(HttpRequest request) throws IOException { HTTPRequest convert(HttpRequest request) throws IOException {
URL url = new URL(target, request.getUri()); String hostHeader = request.getFirstHeaderOrNull(HttpConstants.HOST);
URL url;
// As host headers are not supported in GAE/J v1.2.1, we'll change the
// hostname of the destination to the same value as the host header
if (hostHeader != null) {
url = new URL(new URL(isSecure ? "https" : "http", hostHeader, port, "/"), request
.getUri());
} else {
url = new URL(target, request.getUri());
}
HTTPRequest gaeRequest = new HTTPRequest(url, HTTPMethod.valueOf(request.getMethod()), HTTPRequest gaeRequest = new HTTPRequest(url, HTTPMethod.valueOf(request.getMethod()),
disallowTruncate().doNotFollowRedirects()); disallowTruncate().doNotFollowRedirects());
for (String header : request.getHeaders().keySet()) { for (String header : request.getHeaders().keySet()) {
for (String value : request.getHeaders().get(header)) // GAE/J v1.2.1 re-writes the host header, so we'll skip it.
if (!header.equals(HttpConstants.HOST)) {
for (String value : request.getHeaders().get(header)) {
gaeRequest.addHeader(new HTTPHeader(header, value)); gaeRequest.addHeader(new HTTPHeader(header, value));
} }
}
}
if (request.getPayload() != null) { if (request.getPayload() != null) {
changeRequestContentToBytes(request); changeRequestContentToBytes(request);
gaeRequest.setPayload((byte[]) request.getPayload()); gaeRequest.setPayload((byte[]) request.getPayload());

View File

@ -63,7 +63,7 @@ public class URLFetchServiceClientTest {
@BeforeTest @BeforeTest
void setupClient() throws MalformedURLException { void setupClient() throws MalformedURLException {
url = new URL("http://localhost:80"); url = new URL("http://localhost:80");
client = new URLFetchServiceClient(url, createNiceMock(URLFetchService.class)); client = new URLFetchServiceClient(80, false, url, createNiceMock(URLFetchService.class));
} }
@Test @Test