Issue 52: warn when host header is used

git-svn-id: http://jclouds.googlecode.com/svn/trunk@1083 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-06-07 11:06:29 +00:00
parent 9e9d8ecd1c
commit 0c9b3accda
2 changed files with 233 additions and 224 deletions

View File

@ -24,18 +24,33 @@
package org.jclouds.gae; package org.jclouds.gae;
import static com.google.appengine.api.urlfetch.FetchOptions.Builder.disallowTruncate; import static com.google.appengine.api.urlfetch.FetchOptions.Builder.disallowTruncate;
import com.google.appengine.api.urlfetch.*;
import com.google.common.annotations.VisibleForTesting;
import com.google.inject.Inject;
import org.apache.commons.io.IOUtils;
import org.jclouds.http.*;
import org.jclouds.http.internal.BaseHttpFutureCommandClient;
import java.io.*; import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.List; import java.util.List;
import org.apache.commons.io.IOUtils;
import org.jclouds.http.HttpConstants;
import org.jclouds.http.HttpFutureCommand;
import org.jclouds.http.HttpFutureCommandClient;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpRequestFilter;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.internal.BaseHttpFutureCommandClient;
import com.google.appengine.api.urlfetch.HTTPHeader;
import com.google.appengine.api.urlfetch.HTTPMethod;
import com.google.appengine.api.urlfetch.HTTPRequest;
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;
/** /**
* Google App Engine version of {@link HttpFutureCommandClient} * Google App Engine version of {@link HttpFutureCommandClient}
* *
@ -53,27 +68,30 @@ public class URLFetchServiceClient extends BaseHttpFutureCommandClient {
public void submit(HttpFutureCommand<?> command) { public void submit(HttpFutureCommand<?> command) {
HttpRequest request = command.getRequest(); HttpRequest request = command.getRequest();
HTTPResponse gaeResponse = null; HTTPResponse gaeResponse = null;
try { try {
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);
HTTPRequest gaeRequest = convert(request); HTTPRequest gaeRequest = convert(request);
if (logger.isTraceEnabled()) if (logger.isTraceEnabled())
logger.trace( logger.trace("%1$s - submitting request %2$s, headers: %3$s", target, gaeRequest
"%1$s - submitting request %2$s, headers: %3$s", .getURL(), headersAsString(gaeRequest.getHeaders()));
target, gaeRequest.getURL(),
headersAsString(gaeRequest.getHeaders()));
gaeResponse = this.urlFetchService.fetch(gaeRequest); gaeResponse = this.urlFetchService.fetch(gaeRequest);
if (logger.isTraceEnabled()) if (logger.isTraceEnabled())
logger logger.info("%1$s - received response code %2$s, headers: %3$s", target, gaeResponse
.trace( .getResponseCode(), headersAsString(gaeResponse.getHeaders()));
"%1$s - received response code %2$s, headers: %3$s",
target, gaeResponse.getResponseCode(),
headersAsString(gaeResponse.getHeaders()));
response = convert(gaeResponse); response = convert(gaeResponse);
int statusCode = response.getStatusCode(); int statusCode = response.getStatusCode();
if (statusCode >= 500 && httpRetryHandler.retryRequest(command, response)) if (statusCode >= 500 && httpRetryHandler.retryRequest(command, response))
@ -83,9 +101,8 @@ public class URLFetchServiceClient extends BaseHttpFutureCommandClient {
handleResponse(command, response); handleResponse(command, response);
} catch (Exception e) { } catch (Exception e) {
if (gaeResponse != null && gaeResponse.getContent() != null) { if (gaeResponse != null && gaeResponse.getContent() != null) {
logger.error(e, logger.error(e, "error encountered during the execution: %1$s%n%2$s", gaeResponse,
"error encountered during the execution: %1$s%n%2$s", new String(gaeResponse.getContent()));
gaeResponse, new String(gaeResponse.getContent()));
} }
command.setException(e); command.setException(e);
} }
@ -94,14 +111,14 @@ public class URLFetchServiceClient extends BaseHttpFutureCommandClient {
String headersAsString(List<HTTPHeader> headers) { String headersAsString(List<HTTPHeader> headers) {
StringBuilder builder = new StringBuilder(""); StringBuilder builder = new StringBuilder("");
for (HTTPHeader header : headers) for (HTTPHeader header : headers)
builder.append("[").append(header.getName()).append("=").append( builder.append("[").append(header.getName()).append("=").append(header.getValue()).append(
header.getValue()).append("],"); "],");
return builder.toString(); return builder.toString();
} }
/** /**
* byte [] content is replayable and the only content type supportable by * byte [] content is replayable and the only content type supportable by GAE. As such, we
* GAE. As such, we convert the original request content to a byte array. * convert the original request content to a byte array.
*/ */
@VisibleForTesting @VisibleForTesting
void changeRequestContentToBytes(HttpRequest request) throws IOException { void changeRequestContentToBytes(HttpRequest request) throws IOException {
@ -120,8 +137,7 @@ public class URLFetchServiceClient extends BaseHttpFutureCommandClient {
IOUtils.closeQuietly(i); IOUtils.closeQuietly(i);
} }
} else { } else {
throw new UnsupportedOperationException("Content not supported " throw new UnsupportedOperationException("Content not supported " + content.getClass());
+ content.getClass());
} }
} }
@ -134,8 +150,7 @@ public class URLFetchServiceClient extends BaseHttpFutureCommandClient {
response.getHeaders().put(header.getName(), header.getValue()); response.getHeaders().put(header.getName(), header.getValue());
} }
if (gaeResponse.getContent() != null) { if (gaeResponse.getContent() != null) {
response.setContent(new ByteArrayInputStream(gaeResponse response.setContent(new ByteArrayInputStream(gaeResponse.getContent()));
.getContent()));
} }
return response; return response;
} }
@ -143,8 +158,8 @@ 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()); URL url = new URL(target, request.getUri());
HTTPRequest gaeRequest = new HTTPRequest(url, HTTPMethod HTTPRequest gaeRequest = new HTTPRequest(url, HTTPMethod.valueOf(request.getMethod()),
.valueOf(request.getMethod()), disallowTruncate()); disallowTruncate().doNotFollowRedirects());
for (String header : request.getHeaders().keySet()) { for (String header : request.getHeaders().keySet()) {
for (String value : request.getHeaders().get(header)) for (String value : request.getHeaders().get(header))
gaeRequest.addHeader(new HTTPHeader(header, value)); gaeRequest.addHeader(new HTTPHeader(header, value));

View File

@ -63,8 +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, client = new URLFetchServiceClient(url, createNiceMock(URLFetchService.class));
createNiceMock(URLFetchService.class));
} }
@Test @Test
@ -80,8 +79,7 @@ public class URLFetchServiceClientTest {
assertEquals(response.getStatusCode(), 200); assertEquals(response.getStatusCode(), 200);
assertEquals(response.getContent(), null); assertEquals(response.getContent(), null);
assertEquals(response.getHeaders().size(), 1); assertEquals(response.getHeaders().size(), 1);
assertEquals(response.getFirstHeaderOrNull(HttpHeaders.CONTENT_TYPE), assertEquals(response.getFirstHeaderOrNull(HttpHeaders.CONTENT_TYPE), "text/xml");
"text/xml");
} }
@Test @Test
@ -91,15 +89,13 @@ public class URLFetchServiceClientTest {
List<HTTPHeader> headers = new ArrayList<HTTPHeader>(); List<HTTPHeader> headers = new ArrayList<HTTPHeader>();
headers.add(new HTTPHeader(HttpHeaders.CONTENT_TYPE, "text/xml")); headers.add(new HTTPHeader(HttpHeaders.CONTENT_TYPE, "text/xml"));
expect(gaeResponse.getHeaders()).andReturn(headers); expect(gaeResponse.getHeaders()).andReturn(headers);
expect(gaeResponse.getContent()).andReturn("hello".getBytes()) expect(gaeResponse.getContent()).andReturn("hello".getBytes()).atLeastOnce();
.atLeastOnce();
replay(gaeResponse); replay(gaeResponse);
HttpResponse response = client.convert(gaeResponse); HttpResponse response = client.convert(gaeResponse);
assertEquals(response.getStatusCode(), 200); assertEquals(response.getStatusCode(), 200);
assertEquals(IOUtils.toString(response.getContent()), "hello"); assertEquals(IOUtils.toString(response.getContent()), "hello");
assertEquals(response.getHeaders().size(), 1); assertEquals(response.getHeaders().size(), 1);
assertEquals(response.getFirstHeaderOrNull(HttpHeaders.CONTENT_TYPE), assertEquals(response.getFirstHeaderOrNull(HttpHeaders.CONTENT_TYPE), "text/xml");
"text/xml");
} }
@Test @Test
@ -174,12 +170,10 @@ public class URLFetchServiceClientTest {
} }
private void testHoot(HttpRequest request) throws IOException { private void testHoot(HttpRequest request) throws IOException {
request.getHeaders().put(HttpHeaders.CONTENT_TYPE,"text/plain"); request.getHeaders().put(HttpHeaders.CONTENT_TYPE, "text/plain");
HTTPRequest gaeRequest = client.convert(request); HTTPRequest gaeRequest = client.convert(request);
assertEquals(gaeRequest.getHeaders().get(0).getName(), assertEquals(gaeRequest.getHeaders().get(0).getName(), HttpHeaders.CONTENT_TYPE);
HttpHeaders.CONTENT_TYPE);
assertEquals(gaeRequest.getHeaders().get(0).getValue(), "text/plain"); assertEquals(gaeRequest.getHeaders().get(0).getValue(), "text/plain");
assertEquals(new String(gaeRequest.getPayload()), "hoot!"); assertEquals(new String(gaeRequest.getPayload()), "hoot!");
} }
} }