From d81689aefcf57cfb692b73161b1a00511a90ee1e Mon Sep 17 00:00:00 2001 From: Alon Albert Date: Thu, 3 Jun 2010 15:43:14 -0700 Subject: [PATCH] Add support for logging curl commands for binary POST data Change-Id: Ibf55b95c2ae11681b18b20ba5643057ea0083f7f --- .../android/net/http/AndroidHttpClient.java | 47 ++++++++++++++++--- 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/core/java/android/net/http/AndroidHttpClient.java b/core/java/android/net/http/AndroidHttpClient.java index e07ee59a39042..915e342b0f29d 100644 --- a/core/java/android/net/http/AndroidHttpClient.java +++ b/core/java/android/net/http/AndroidHttpClient.java @@ -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