Add support for logging curl commands for binary POST data

Change-Id: Ibf55b95c2ae11681b18b20ba5643057ea0083f7f
This commit is contained in:
Alon Albert
2010-06-03 15:43:14 -07:00
parent 8fd2913a7d
commit d81689aefc

View File

@@ -61,6 +61,7 @@ import android.content.ContentResolver;
import android.net.SSLCertificateSocketFactory;
import android.net.SSLSessionCache;
import android.os.Looper;
import android.util.Base64;
import android.util.Log;
/**
@@ -81,6 +82,11 @@ public final class AndroidHttpClient implements HttpClient {
private static final String TAG = "AndroidHttpClient";
private static String[] textContentTypes = new String[] {
"text/",
"application/xml",
"application/json"
};
/** Interceptor throws an exception if the executing thread is blocked */
private static final HttpRequestInterceptor sThreadCheckInterceptor =
@@ -358,7 +364,7 @@ public final class AndroidHttpClient implements HttpClient {
}
if (level < Log.VERBOSE || level > Log.ASSERT) {
throw new IllegalArgumentException("Level is out of range ["
+ Log.VERBOSE + ".." + Log.ASSERT + "]");
+ Log.VERBOSE + ".." + Log.ASSERT + "]");
}
curlConfiguration = new LoggingConfiguration(name, level);
@@ -431,12 +437,17 @@ public final class AndroidHttpClient implements HttpClient {
if (entity.getContentLength() < 1024) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
entity.writeTo(stream);
String entityString = stream.toString();
// TODO: Check the content type, too.
builder.append(" --data-ascii \"")
.append(entityString)
.append("\"");
if (isBinaryContent(request)) {
String base64 = Base64.encodeToString(stream.toByteArray(), Base64.NO_WRAP);
builder.insert(0, "echo '" + base64 + "' | base64 -d > /tmp/$$.bin; ");
builder.append(" --data-binary @/tmp/$$.bin");
} else {
String entityString = stream.toString();
builder.append(" --data-ascii \"")
.append(entityString)
.append("\"");
}
} else {
builder.append(" [TOO MUCH DATA TO INCLUDE]");
}
@@ -446,6 +457,30 @@ public final class AndroidHttpClient implements HttpClient {
return builder.toString();
}
private static boolean isBinaryContent(HttpUriRequest request) {
Header[] headers;
headers = request.getHeaders(Headers.CONTENT_ENCODING);
if (headers != null) {
for (Header header : headers) {
if ("gzip".equalsIgnoreCase(header.getValue())) {
return true;
}
}
}
headers = request.getHeaders(Headers.CONTENT_TYPE);
if (headers != null) {
for (Header header : headers) {
for (String contentType : textContentTypes) {
if (header.getValue().startsWith(contentType)) {
return false;
}
}
}
}
return true;
}
/**
* Returns the date of the given HTTP date string. This method can identify
* and parse the date formats emitted by common HTTP servers, such as