Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d81689ae authored by Alon Albert's avatar Alon Albert
Browse files

Add support for logging curl commands for binary POST data

Change-Id: Ibf55b95c2ae11681b18b20ba5643057ea0083f7f
parent 8fd2913a
Loading
Loading
Loading
Loading
+41 −6
Original line number Diff line number Diff line
@@ -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 =
@@ -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.
                    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