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

Commit 92d543f6 authored by Dan Egnor's avatar Dan Egnor
Browse files

API CHANGE: Add SSLSessionCache public API to allow unbundled SSL session caching.

Generally clean up the associated SSLCertificateSocketFactory API as well,
change AndroidHttpClient to use this new thing, and make the android-common
library build SDK-clean (woo hoo).

Bug: 2362543
Bug: 2357311
parent aa5be64f
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := android-common
LOCAL_SDK_VERSION := current
LOCAL_SRC_FILES := $(call all-java-files-under, java)
include $(BUILD_STATIC_JAVA_LIBRARY)

+15 −53
Original line number Diff line number Diff line
@@ -47,8 +47,6 @@ import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.BasicHttpProcessor;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.harmony.xnet.provider.jsse.SSLClientSessionCache;
import org.apache.harmony.xnet.provider.jsse.SSLContextImpl;

import java.io.IOException;
import java.io.InputStream;
@@ -59,11 +57,11 @@ import java.util.zip.GZIPOutputStream;
import java.net.URI;
import java.security.KeyManagementException;

import android.content.Context;
import android.content.ContentResolver;
import android.net.SSLCertificateSocketFactory;
import android.net.SSLSessionCache;
import android.os.Looper;
import android.os.SystemProperties;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;

/**
@@ -76,8 +74,6 @@ import android.util.Log;
 * To retain cookies, simply add a cookie store to the HttpContext:</p>
 *
 * <pre>context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);</pre>
 * 
 * {@hide}
 */
public final class AndroidHttpClient implements HttpClient {

@@ -101,12 +97,11 @@ public final class AndroidHttpClient implements HttpClient {
    /**
     * Create a new HttpClient with reasonable defaults (which you can update).
     *
     * @param userAgent to report in your HTTP requests.
     * @param sessionCache persistent session cache
     * @param userAgent to report in your HTTP requests
     * @param context to use for caching SSL sessions (may be null for no caching)
     * @return AndroidHttpClient for you to use for all your requests.
     */
    public static AndroidHttpClient newInstance(String userAgent,
            SSLClientSessionCache sessionCache) {
    public static AndroidHttpClient newInstance(String userAgent, Context context) {
        HttpParams params = new BasicHttpParams();

        // Turn off stale checking.  Our connections break all the time anyway,
@@ -122,13 +117,16 @@ public final class AndroidHttpClient implements HttpClient {
        // often wants to re-POST after a redirect, which we must do ourselves.
        HttpClientParams.setRedirecting(params, false);

        // Use a session cache for SSL sockets
        SSLSessionCache sessionCache = context == null ? null : new SSLSessionCache(context);

        // Set the specified user agent and register standard protocols.
        HttpProtocolParams.setUserAgent(params, userAgent);
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http",
                PlainSocketFactory.getSocketFactory(), 80));
        schemeRegistry.register(new Scheme("https",
                socketFactoryWithCache(sessionCache), 443));
                SSLCertificateSocketFactory.getHttpSocketFactory(30 * 1000, sessionCache), 443));

        ClientConnectionManager manager =
                new ThreadSafeClientConnManager(params, schemeRegistry);
@@ -138,32 +136,6 @@ public final class AndroidHttpClient implements HttpClient {
        return new AndroidHttpClient(manager, params);
    }

    /**
     * Returns a socket factory backed by the given persistent session cache.
     *
     * @param sessionCache to retrieve sessions from, null for no cache
     */
    private static SSLSocketFactory socketFactoryWithCache(
            SSLClientSessionCache sessionCache) {
        if (sessionCache == null) {
            // Use the default factory which doesn't support persistent
            // caching.
            return SSLSocketFactory.getSocketFactory();
        }

        // Create a new SSL context backed by the cache.
        // TODO: Keep a weak *identity* hash map of caches to engines. In the
        // mean time, if we have two engines for the same cache, they'll still
        // share sessions but will have to do so through the persistent cache.
        SSLContextImpl sslContext = new SSLContextImpl();
        try {
            sslContext.engineInit(null, null, null, sessionCache, null);
        } catch (KeyManagementException e) {
            throw new AssertionError(e);
        }
        return new SSLSocketFactory(sslContext.engineGetSocketFactory());
    }

    /**
     * Create a new HttpClient with reasonable defaults (which you can update).
     * @param userAgent to report in your HTTP requests.
@@ -339,9 +311,7 @@ public final class AndroidHttpClient implements HttpClient {
     * Shorter data will not be compressed.
     */
    public static long getMinGzipSize(ContentResolver resolver) {
        return Settings.Secure.getLong(resolver,
                                       Settings.Secure.SYNC_MIN_GZIP_BYTES,
                                       DEFAULT_SYNC_MIN_GZIP_BYTES);
        return DEFAULT_SYNC_MIN_GZIP_BYTES;  // For now, this is just a constant.
    }

    /* cURL logging support. */
@@ -366,15 +336,6 @@ public final class AndroidHttpClient implements HttpClient {
            return Log.isLoggable(tag, level);
        }

        /**
         * Returns true if auth logging is turned on for this configuration.  Can only be set on
         * insecure devices.
         */
        private boolean isAuthLoggable() {
            String secure = SystemProperties.get("ro.secure");
            return "0".equals(secure) && Log.isLoggable(tag + "-auth", level);
        }

        /**
         * Prints a message using this configuration.
         */
@@ -421,8 +382,9 @@ public final class AndroidHttpClient implements HttpClient {
            if (configuration != null
                    && configuration.isLoggable()
                    && request instanceof HttpUriRequest) {
                configuration.println(toCurl((HttpUriRequest) request,
                        configuration.isAuthLoggable()));
                // Never print auth token -- we used to check ro.secure=0 to
                // enable that, but can't do that in unbundled code.
                configuration.println(toCurl((HttpUriRequest) request, false));
            }
        }
    }
+0 −5
Original line number Diff line number Diff line
@@ -114,11 +114,6 @@ public class ArrayListCursor extends AbstractCursor {
        return mRows.length;
    }

    @Override
    public boolean deleteRow() {
        return false;
    }

    @Override
    public String[] getColumnNames() {
        return mColumnNames;