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;
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.URL;
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}
*
@ -53,27 +68,30 @@ public class URLFetchServiceClient extends BaseHttpFutureCommandClient {
public void submit(HttpFutureCommand<?> command) {
HttpRequest request = command.getRequest();
HTTPResponse gaeResponse = null;
try {
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 (; ;) {
for (;;) {
logger.trace("%1$s - converting request %2$s", target, request);
HTTPRequest gaeRequest = convert(request);
if (logger.isTraceEnabled())
logger.trace(
"%1$s - submitting request %2$s, headers: %3$s",
target, gaeRequest.getURL(),
headersAsString(gaeRequest.getHeaders()));
logger.trace("%1$s - submitting request %2$s, headers: %3$s", target, gaeRequest
.getURL(), headersAsString(gaeRequest.getHeaders()));
gaeResponse = this.urlFetchService.fetch(gaeRequest);
if (logger.isTraceEnabled())
logger
.trace(
"%1$s - received response code %2$s, headers: %3$s",
target, gaeResponse.getResponseCode(),
headersAsString(gaeResponse.getHeaders()));
logger.info("%1$s - received response code %2$s, headers: %3$s", target, gaeResponse
.getResponseCode(), headersAsString(gaeResponse.getHeaders()));
response = convert(gaeResponse);
int statusCode = response.getStatusCode();
if (statusCode >= 500 && httpRetryHandler.retryRequest(command, response))
@ -83,9 +101,8 @@ public class URLFetchServiceClient extends BaseHttpFutureCommandClient {
handleResponse(command, response);
} catch (Exception e) {
if (gaeResponse != null && gaeResponse.getContent() != null) {
logger.error(e,
"error encountered during the execution: %1$s%n%2$s",
gaeResponse, new String(gaeResponse.getContent()));
logger.error(e, "error encountered during the execution: %1$s%n%2$s", gaeResponse,
new String(gaeResponse.getContent()));
}
command.setException(e);
}
@ -94,14 +111,14 @@ public class URLFetchServiceClient extends BaseHttpFutureCommandClient {
String headersAsString(List<HTTPHeader> headers) {
StringBuilder builder = new StringBuilder("");
for (HTTPHeader header : headers)
builder.append("[").append(header.getName()).append("=").append(
header.getValue()).append("],");
builder.append("[").append(header.getName()).append("=").append(header.getValue()).append(
"],");
return builder.toString();
}
/**
* byte [] content is replayable and the only content type supportable by
* GAE. As such, we convert the original request content to a byte array.
* byte [] content is replayable and the only content type supportable by GAE. As such, we
* convert the original request content to a byte array.
*/
@VisibleForTesting
void changeRequestContentToBytes(HttpRequest request) throws IOException {
@ -120,8 +137,7 @@ public class URLFetchServiceClient extends BaseHttpFutureCommandClient {
IOUtils.closeQuietly(i);
}
} else {
throw new UnsupportedOperationException("Content not supported "
+ content.getClass());
throw new UnsupportedOperationException("Content not supported " + content.getClass());
}
}
@ -134,8 +150,7 @@ public class URLFetchServiceClient extends BaseHttpFutureCommandClient {
response.getHeaders().put(header.getName(), header.getValue());
}
if (gaeResponse.getContent() != null) {
response.setContent(new ByteArrayInputStream(gaeResponse
.getContent()));
response.setContent(new ByteArrayInputStream(gaeResponse.getContent()));
}
return response;
}
@ -143,8 +158,8 @@ public class URLFetchServiceClient extends BaseHttpFutureCommandClient {
@VisibleForTesting
HTTPRequest convert(HttpRequest request) throws IOException {
URL url = new URL(target, request.getUri());
HTTPRequest gaeRequest = new HTTPRequest(url, HTTPMethod
.valueOf(request.getMethod()), disallowTruncate());
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));

View File

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