AndroidHttpClient.java revision d81689aefcf57cfb692b73161b1a00511a90ee1e
1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.net.http;
18
19import com.android.internal.http.HttpDateTime;
20import org.apache.http.Header;
21import org.apache.http.HttpEntity;
22import org.apache.http.HttpEntityEnclosingRequest;
23import org.apache.http.HttpException;
24import org.apache.http.HttpHost;
25import org.apache.http.HttpRequest;
26import org.apache.http.HttpRequestInterceptor;
27import org.apache.http.HttpResponse;
28import org.apache.http.entity.AbstractHttpEntity;
29import org.apache.http.entity.ByteArrayEntity;
30import org.apache.http.client.HttpClient;
31import org.apache.http.client.ResponseHandler;
32import org.apache.http.client.ClientProtocolException;
33import org.apache.http.client.protocol.ClientContext;
34import org.apache.http.client.methods.HttpUriRequest;
35import org.apache.http.client.params.HttpClientParams;
36import org.apache.http.conn.ClientConnectionManager;
37import org.apache.http.conn.scheme.PlainSocketFactory;
38import org.apache.http.conn.scheme.Scheme;
39import org.apache.http.conn.scheme.SchemeRegistry;
40import org.apache.http.impl.client.DefaultHttpClient;
41import org.apache.http.impl.client.RequestWrapper;
42import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
43import org.apache.http.params.BasicHttpParams;
44import org.apache.http.params.HttpConnectionParams;
45import org.apache.http.params.HttpParams;
46import org.apache.http.params.HttpProtocolParams;
47import org.apache.http.protocol.BasicHttpProcessor;
48import org.apache.http.protocol.HttpContext;
49import org.apache.http.protocol.BasicHttpContext;
50
51import java.io.IOException;
52import java.io.InputStream;
53import java.io.ByteArrayOutputStream;
54import java.io.OutputStream;
55import java.util.zip.GZIPInputStream;
56import java.util.zip.GZIPOutputStream;
57import java.net.URI;
58
59import android.content.Context;
60import android.content.ContentResolver;
61import android.net.SSLCertificateSocketFactory;
62import android.net.SSLSessionCache;
63import android.os.Looper;
64import android.util.Base64;
65import android.util.Log;
66
67/**
68 * Subclass of the Apache {@link DefaultHttpClient} that is configured with
69 * reasonable default settings and registered schemes for Android, and
70 * also lets the user add {@link HttpRequestInterceptor} classes.
71 * Don't create this directly, use the {@link #newInstance} factory method.
72 *
73 * <p>This client processes cookies but does not retain them by default.
74 * To retain cookies, simply add a cookie store to the HttpContext:</p>
75 *
76 * <pre>context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);</pre>
77 */
78public final class AndroidHttpClient implements HttpClient {
79
80    // Gzip of data shorter than this probably won't be worthwhile
81    public static long DEFAULT_SYNC_MIN_GZIP_BYTES = 256;
82
83    private static final String TAG = "AndroidHttpClient";
84
85    private static String[] textContentTypes = new String[] {
86            "text/",
87            "application/xml",
88            "application/json"
89    };
90
91    /** Interceptor throws an exception if the executing thread is blocked */
92    private static final HttpRequestInterceptor sThreadCheckInterceptor =
93            new HttpRequestInterceptor() {
94        public void process(HttpRequest request, HttpContext context) {
95            // Prevent the HttpRequest from being sent on the main thread
96            if (Looper.myLooper() != null && Looper.myLooper() == Looper.getMainLooper() ) {
97                throw new RuntimeException("This thread forbids HTTP requests");
98            }
99        }
100    };
101
102    /**
103     * Create a new HttpClient with reasonable defaults (which you can update).
104     *
105     * @param userAgent to report in your HTTP requests
106     * @param context to use for caching SSL sessions (may be null for no caching)
107     * @return AndroidHttpClient for you to use for all your requests.
108     */
109    public static AndroidHttpClient newInstance(String userAgent, Context context) {
110        HttpParams params = new BasicHttpParams();
111
112        // Turn off stale checking.  Our connections break all the time anyway,
113        // and it's not worth it to pay the penalty of checking every time.
114        HttpConnectionParams.setStaleCheckingEnabled(params, false);
115
116        // Default connection and socket timeout of 20 seconds.  Tweak to taste.
117        HttpConnectionParams.setConnectionTimeout(params, 20 * 1000);
118        HttpConnectionParams.setSoTimeout(params, 20 * 1000);
119        HttpConnectionParams.setSocketBufferSize(params, 8192);
120
121        // Don't handle redirects -- return them to the caller.  Our code
122        // often wants to re-POST after a redirect, which we must do ourselves.
123        HttpClientParams.setRedirecting(params, false);
124
125        // Use a session cache for SSL sockets
126        SSLSessionCache sessionCache = context == null ? null : new SSLSessionCache(context);
127
128        // Set the specified user agent and register standard protocols.
129        HttpProtocolParams.setUserAgent(params, userAgent);
130        SchemeRegistry schemeRegistry = new SchemeRegistry();
131        schemeRegistry.register(new Scheme("http",
132                PlainSocketFactory.getSocketFactory(), 80));
133        schemeRegistry.register(new Scheme("https",
134                SSLCertificateSocketFactory.getHttpSocketFactory(30 * 1000, sessionCache), 443));
135
136        ClientConnectionManager manager =
137                new ThreadSafeClientConnManager(params, schemeRegistry);
138
139        // We use a factory method to modify superclass initialization
140        // parameters without the funny call-a-static-method dance.
141        return new AndroidHttpClient(manager, params);
142    }
143
144    /**
145     * Create a new HttpClient with reasonable defaults (which you can update).
146     * @param userAgent to report in your HTTP requests.
147     * @return AndroidHttpClient for you to use for all your requests.
148     */
149    public static AndroidHttpClient newInstance(String userAgent) {
150        return newInstance(userAgent, null /* session cache */);
151    }
152
153    private final HttpClient delegate;
154
155    private RuntimeException mLeakedException = new IllegalStateException(
156            "AndroidHttpClient created and never closed");
157
158    private AndroidHttpClient(ClientConnectionManager ccm, HttpParams params) {
159        this.delegate = new DefaultHttpClient(ccm, params) {
160            @Override
161            protected BasicHttpProcessor createHttpProcessor() {
162                // Add interceptor to prevent making requests from main thread.
163                BasicHttpProcessor processor = super.createHttpProcessor();
164                processor.addRequestInterceptor(sThreadCheckInterceptor);
165                processor.addRequestInterceptor(new CurlLogger());
166
167                return processor;
168            }
169
170            @Override
171            protected HttpContext createHttpContext() {
172                // Same as DefaultHttpClient.createHttpContext() minus the
173                // cookie store.
174                HttpContext context = new BasicHttpContext();
175                context.setAttribute(
176                        ClientContext.AUTHSCHEME_REGISTRY,
177                        getAuthSchemes());
178                context.setAttribute(
179                        ClientContext.COOKIESPEC_REGISTRY,
180                        getCookieSpecs());
181                context.setAttribute(
182                        ClientContext.CREDS_PROVIDER,
183                        getCredentialsProvider());
184                return context;
185            }
186        };
187    }
188
189    @Override
190    protected void finalize() throws Throwable {
191        super.finalize();
192        if (mLeakedException != null) {
193            Log.e(TAG, "Leak found", mLeakedException);
194            mLeakedException = null;
195        }
196    }
197
198    /**
199     * Modifies a request to indicate to the server that we would like a
200     * gzipped response.  (Uses the "Accept-Encoding" HTTP header.)
201     * @param request the request to modify
202     * @see #getUngzippedContent
203     */
204    public static void modifyRequestToAcceptGzipResponse(HttpRequest request) {
205        request.addHeader("Accept-Encoding", "gzip");
206    }
207
208    /**
209     * Gets the input stream from a response entity.  If the entity is gzipped
210     * then this will get a stream over the uncompressed data.
211     *
212     * @param entity the entity whose content should be read
213     * @return the input stream to read from
214     * @throws IOException
215     */
216    public static InputStream getUngzippedContent(HttpEntity entity)
217            throws IOException {
218        InputStream responseStream = entity.getContent();
219        if (responseStream == null) return responseStream;
220        Header header = entity.getContentEncoding();
221        if (header == null) return responseStream;
222        String contentEncoding = header.getValue();
223        if (contentEncoding == null) return responseStream;
224        if (contentEncoding.contains("gzip")) responseStream
225                = new GZIPInputStream(responseStream);
226        return responseStream;
227    }
228
229    /**
230     * Release resources associated with this client.  You must call this,
231     * or significant resources (sockets and memory) may be leaked.
232     */
233    public void close() {
234        if (mLeakedException != null) {
235            getConnectionManager().shutdown();
236            mLeakedException = null;
237        }
238    }
239
240    public HttpParams getParams() {
241        return delegate.getParams();
242    }
243
244    public ClientConnectionManager getConnectionManager() {
245        return delegate.getConnectionManager();
246    }
247
248    public HttpResponse execute(HttpUriRequest request) throws IOException {
249        return delegate.execute(request);
250    }
251
252    public HttpResponse execute(HttpUriRequest request, HttpContext context)
253            throws IOException {
254        return delegate.execute(request, context);
255    }
256
257    public HttpResponse execute(HttpHost target, HttpRequest request)
258            throws IOException {
259        return delegate.execute(target, request);
260    }
261
262    public HttpResponse execute(HttpHost target, HttpRequest request,
263            HttpContext context) throws IOException {
264        return delegate.execute(target, request, context);
265    }
266
267    public <T> T execute(HttpUriRequest request,
268            ResponseHandler<? extends T> responseHandler)
269            throws IOException, ClientProtocolException {
270        return delegate.execute(request, responseHandler);
271    }
272
273    public <T> T execute(HttpUriRequest request,
274            ResponseHandler<? extends T> responseHandler, HttpContext context)
275            throws IOException, ClientProtocolException {
276        return delegate.execute(request, responseHandler, context);
277    }
278
279    public <T> T execute(HttpHost target, HttpRequest request,
280            ResponseHandler<? extends T> responseHandler) throws IOException,
281            ClientProtocolException {
282        return delegate.execute(target, request, responseHandler);
283    }
284
285    public <T> T execute(HttpHost target, HttpRequest request,
286            ResponseHandler<? extends T> responseHandler, HttpContext context)
287            throws IOException, ClientProtocolException {
288        return delegate.execute(target, request, responseHandler, context);
289    }
290
291    /**
292     * Compress data to send to server.
293     * Creates a Http Entity holding the gzipped data.
294     * The data will not be compressed if it is too short.
295     * @param data The bytes to compress
296     * @return Entity holding the data
297     */
298    public static AbstractHttpEntity getCompressedEntity(byte data[], ContentResolver resolver)
299            throws IOException {
300        AbstractHttpEntity entity;
301        if (data.length < getMinGzipSize(resolver)) {
302            entity = new ByteArrayEntity(data);
303        } else {
304            ByteArrayOutputStream arr = new ByteArrayOutputStream();
305            OutputStream zipper = new GZIPOutputStream(arr);
306            zipper.write(data);
307            zipper.close();
308            entity = new ByteArrayEntity(arr.toByteArray());
309            entity.setContentEncoding("gzip");
310        }
311        return entity;
312    }
313
314    /**
315     * Retrieves the minimum size for compressing data.
316     * Shorter data will not be compressed.
317     */
318    public static long getMinGzipSize(ContentResolver resolver) {
319        return DEFAULT_SYNC_MIN_GZIP_BYTES;  // For now, this is just a constant.
320    }
321
322    /* cURL logging support. */
323
324    /**
325     * Logging tag and level.
326     */
327    private static class LoggingConfiguration {
328
329        private final String tag;
330        private final int level;
331
332        private LoggingConfiguration(String tag, int level) {
333            this.tag = tag;
334            this.level = level;
335        }
336
337        /**
338         * Returns true if logging is turned on for this configuration.
339         */
340        private boolean isLoggable() {
341            return Log.isLoggable(tag, level);
342        }
343
344        /**
345         * Prints a message using this configuration.
346         */
347        private void println(String message) {
348            Log.println(level, tag, message);
349        }
350    }
351
352    /** cURL logging configuration. */
353    private volatile LoggingConfiguration curlConfiguration;
354
355    /**
356     * Enables cURL request logging for this client.
357     *
358     * @param name to log messages with
359     * @param level at which to log messages (see {@link android.util.Log})
360     */
361    public void enableCurlLogging(String name, int level) {
362        if (name == null) {
363            throw new NullPointerException("name");
364        }
365        if (level < Log.VERBOSE || level > Log.ASSERT) {
366            throw new IllegalArgumentException("Level is out of range ["
367                + Log.VERBOSE + ".." + Log.ASSERT + "]");
368        }
369
370        curlConfiguration = new LoggingConfiguration(name, level);
371    }
372
373    /**
374     * Disables cURL logging for this client.
375     */
376    public void disableCurlLogging() {
377        curlConfiguration = null;
378    }
379
380    /**
381     * Logs cURL commands equivalent to requests.
382     */
383    private class CurlLogger implements HttpRequestInterceptor {
384        public void process(HttpRequest request, HttpContext context)
385                throws HttpException, IOException {
386            LoggingConfiguration configuration = curlConfiguration;
387            if (configuration != null
388                    && configuration.isLoggable()
389                    && request instanceof HttpUriRequest) {
390                // Never print auth token -- we used to check ro.secure=0 to
391                // enable that, but can't do that in unbundled code.
392                configuration.println(toCurl((HttpUriRequest) request, false));
393            }
394        }
395    }
396
397    /**
398     * Generates a cURL command equivalent to the given request.
399     */
400    private static String toCurl(HttpUriRequest request, boolean logAuthToken) throws IOException {
401        StringBuilder builder = new StringBuilder();
402
403        builder.append("curl ");
404
405        for (Header header: request.getAllHeaders()) {
406            if (!logAuthToken
407                    && (header.getName().equals("Authorization") ||
408                        header.getName().equals("Cookie"))) {
409                continue;
410            }
411            builder.append("--header \"");
412            builder.append(header.toString().trim());
413            builder.append("\" ");
414        }
415
416        URI uri = request.getURI();
417
418        // If this is a wrapped request, use the URI from the original
419        // request instead. getURI() on the wrapper seems to return a
420        // relative URI. We want an absolute URI.
421        if (request instanceof RequestWrapper) {
422            HttpRequest original = ((RequestWrapper) request).getOriginal();
423            if (original instanceof HttpUriRequest) {
424                uri = ((HttpUriRequest) original).getURI();
425            }
426        }
427
428        builder.append("\"");
429        builder.append(uri);
430        builder.append("\"");
431
432        if (request instanceof HttpEntityEnclosingRequest) {
433            HttpEntityEnclosingRequest entityRequest =
434                    (HttpEntityEnclosingRequest) request;
435            HttpEntity entity = entityRequest.getEntity();
436            if (entity != null && entity.isRepeatable()) {
437                if (entity.getContentLength() < 1024) {
438                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
439                    entity.writeTo(stream);
440
441                    if (isBinaryContent(request)) {
442                        String base64 = Base64.encodeToString(stream.toByteArray(), Base64.NO_WRAP);
443                        builder.insert(0, "echo '" + base64 + "' | base64 -d > /tmp/$$.bin; ");
444                        builder.append(" --data-binary @/tmp/$$.bin");
445                    } else {
446                        String entityString = stream.toString();
447                        builder.append(" --data-ascii \"")
448                                .append(entityString)
449                                .append("\"");
450                    }
451                } else {
452                    builder.append(" [TOO MUCH DATA TO INCLUDE]");
453                }
454            }
455        }
456
457        return builder.toString();
458    }
459
460    private static boolean isBinaryContent(HttpUriRequest request) {
461        Header[] headers;
462        headers = request.getHeaders(Headers.CONTENT_ENCODING);
463        if (headers != null) {
464            for (Header header : headers) {
465                if ("gzip".equalsIgnoreCase(header.getValue())) {
466                    return true;
467                }
468            }
469        }
470
471        headers = request.getHeaders(Headers.CONTENT_TYPE);
472        if (headers != null) {
473            for (Header header : headers) {
474                for (String contentType : textContentTypes) {
475                    if (header.getValue().startsWith(contentType)) {
476                        return false;
477                    }
478                }
479            }
480        }
481        return true;
482    }
483
484    /**
485     * Returns the date of the given HTTP date string. This method can identify
486     * and parse the date formats emitted by common HTTP servers, such as
487     * <a href="http://www.ietf.org/rfc/rfc0822.txt">RFC 822</a>,
488     * <a href="http://www.ietf.org/rfc/rfc0850.txt">RFC 850</a>,
489     * <a href="http://www.ietf.org/rfc/rfc1036.txt">RFC 1036</a>,
490     * <a href="http://www.ietf.org/rfc/rfc1123.txt">RFC 1123</a> and
491     * <a href="http://www.opengroup.org/onlinepubs/007908799/xsh/asctime.html">ANSI
492     * C's asctime()</a>.
493     *
494     * @return the number of milliseconds since Jan. 1, 1970, midnight GMT.
495     * @throws IllegalArgumentException if {@code dateString} is not a date or
496     *     of an unsupported format.
497     */
498    public static long parseDate(String dateString) {
499        return HttpDateTime.parse(dateString);
500    }
501}
502