From bf3ba3e8555750a2c9abea630272f0740611f44e Mon Sep 17 00:00:00 2001 From: "adrian.f.cole" Date: Sun, 7 Jun 2009 23:13:38 +0000 Subject: [PATCH] Issue 52: worked around host header issue in gae git-svn-id: http://jclouds.googlecode.com/svn/trunk@1088 3d8758e0-26b5-11de-8745-db77d3ebf521 --- .../jclouds/gae/URLFetchServiceClient.java | 36 ++++++++++++------- .../gae/URLFetchServiceClientTest.java | 2 +- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/extensions/gae/src/main/java/org/jclouds/gae/URLFetchServiceClient.java b/extensions/gae/src/main/java/org/jclouds/gae/URLFetchServiceClient.java index 455df26cc5..4e1042e6c0 100644 --- a/extensions/gae/src/main/java/org/jclouds/gae/URLFetchServiceClient.java +++ b/extensions/gae/src/main/java/org/jclouds/gae/URLFetchServiceClient.java @@ -50,6 +50,7 @@ import com.google.appengine.api.urlfetch.HTTPResponse; import com.google.appengine.api.urlfetch.URLFetchService; import com.google.common.annotations.VisibleForTesting; import com.google.inject.Inject; +import com.google.inject.name.Named; /** * Google App Engine version of {@link HttpFutureCommandClient} @@ -58,12 +59,17 @@ import com.google.inject.Inject; */ public class URLFetchServiceClient extends BaseHttpFutureCommandClient { private final URLFetchService urlFetchService; + private final int port; + private final boolean isSecure; @Inject - public URLFetchServiceClient(URL target, URLFetchService urlFetchService) - throws MalformedURLException { + public URLFetchServiceClient(@Named(HttpConstants.PROPERTY_HTTP_PORT) int port, + @Named(HttpConstants.PROPERTY_HTTP_SECURE) boolean isSecure, URL target, + URLFetchService urlFetchService) throws MalformedURLException { super(target); this.urlFetchService = urlFetchService; + this.port = port; + this.isSecure = isSecure; } public void submit(HttpFutureCommand command) { @@ -74,13 +80,6 @@ public class URLFetchServiceClient extends BaseHttpFutureCommandClient { for (HttpRequestFilter filter : requestFilters) { 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; for (;;) { logger.trace("%1$s - converting request %2$s", target, request); @@ -157,12 +156,25 @@ public class URLFetchServiceClient extends BaseHttpFutureCommandClient { @VisibleForTesting 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()), disallowTruncate().doNotFollowRedirects()); for (String header : request.getHeaders().keySet()) { - for (String value : request.getHeaders().get(header)) - gaeRequest.addHeader(new HTTPHeader(header, value)); + // 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)); + } + } } if (request.getPayload() != null) { changeRequestContentToBytes(request); diff --git a/extensions/gae/src/test/java/org/jclouds/gae/URLFetchServiceClientTest.java b/extensions/gae/src/test/java/org/jclouds/gae/URLFetchServiceClientTest.java index 4a4a24493b..c3302ea57b 100644 --- a/extensions/gae/src/test/java/org/jclouds/gae/URLFetchServiceClientTest.java +++ b/extensions/gae/src/test/java/org/jclouds/gae/URLFetchServiceClientTest.java @@ -63,7 +63,7 @@ public class URLFetchServiceClientTest { @BeforeTest void setupClient() throws MalformedURLException { url = new URL("http://localhost:80"); - client = new URLFetchServiceClient(url, createNiceMock(URLFetchService.class)); + client = new URLFetchServiceClient(80, false, url, createNiceMock(URLFetchService.class)); } @Test