blob: 22a0b158b5fa94ec53dcd5364ab527d2a4a5b1c2 [file] [log] [blame]
Narayan Kamathc3f6f162012-08-09 11:57:05 +01001/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
jwilson2231db32012-12-26 11:37:43 -070018package com.squareup.okhttp;
Narayan Kamathc3f6f162012-08-09 11:57:05 +010019
Alex Klyubind2167392015-03-20 19:52:23 -070020import libcore.net.NetworkSecurityPolicy;
Narayan Kamathc3f6f162012-08-09 11:57:05 +010021import java.io.IOException;
22import java.net.Proxy;
Neil Fuller3c938a32014-02-19 09:40:26 +000023import java.net.ResponseCache;
Narayan Kamathc3f6f162012-08-09 11:57:05 +010024import java.net.URL;
25import java.net.URLConnection;
26import java.net.URLStreamHandler;
Neil Fuller2ab42192015-01-21 17:47:59 +000027import java.util.Collections;
28import java.util.List;
Neil Fullerf4a606d2015-06-29 16:02:23 +010029import java.util.concurrent.TimeUnit;
Narayan Kamathc3f6f162012-08-09 11:57:05 +010030
Narayan Kamathbf0942e2013-06-13 12:06:06 +010031public class HttpHandler extends URLStreamHandler {
Neil Fuller8bced3e2014-09-11 10:45:54 +010032
Neil Fuller2ab42192015-01-21 17:47:59 +000033 private final static List<ConnectionSpec> CLEARTEXT_ONLY =
34 Collections.singletonList(ConnectionSpec.CLEARTEXT);
35
Neil Fuller8bced3e2014-09-11 10:45:54 +010036 private final ConfigAwareConnectionPool configAwareConnectionPool =
37 ConfigAwareConnectionPool.getInstance();
Neil Fuller84f674f2014-09-03 17:01:55 +010038
Narayan Kamathc3f6f162012-08-09 11:57:05 +010039 @Override protected URLConnection openConnection(URL url) throws IOException {
Neil Fullere78f1172015-01-20 09:39:41 +000040 return newOkUrlFactory(null /* proxy */).open(url);
Narayan Kamathc3f6f162012-08-09 11:57:05 +010041 }
42
43 @Override protected URLConnection openConnection(URL url, Proxy proxy) throws IOException {
44 if (url == null || proxy == null) {
45 throw new IllegalArgumentException("url == null || proxy == null");
46 }
Neil Fullere78f1172015-01-20 09:39:41 +000047 return newOkUrlFactory(proxy).open(url);
Narayan Kamathc3f6f162012-08-09 11:57:05 +010048 }
49
50 @Override protected int getDefaultPort() {
jwilson2231db32012-12-26 11:37:43 -070051 return 80;
Narayan Kamathc3f6f162012-08-09 11:57:05 +010052 }
Narayan Kamathbf0942e2013-06-13 12:06:06 +010053
Neil Fullere78f1172015-01-20 09:39:41 +000054 protected OkUrlFactory newOkUrlFactory(Proxy proxy) {
55 OkUrlFactory okUrlFactory = createHttpOkUrlFactory(proxy);
Neil Fuller2ab42192015-01-21 17:47:59 +000056 // For HttpURLConnections created through java.net.URL Android uses a connection pool that
57 // is aware when the default network changes so that pooled connections are not re-used when
58 // the default network changes.
Neil Fullere78f1172015-01-20 09:39:41 +000059 okUrlFactory.client().setConnectionPool(configAwareConnectionPool.get());
60 return okUrlFactory;
Neil Fullerc19427f2014-09-09 16:46:57 +010061 }
62
63 /**
64 * Creates an OkHttpClient suitable for creating {@link java.net.HttpURLConnection} instances on
65 * Android.
66 */
Neil Fuller2ab42192015-01-21 17:47:59 +000067 // Visible for android.net.Network.
Neil Fullere78f1172015-01-20 09:39:41 +000068 public static OkUrlFactory createHttpOkUrlFactory(Proxy proxy) {
Narayan Kamathbf0942e2013-06-13 12:06:06 +010069 OkHttpClient client = new OkHttpClient();
Neil Fuller2ab42192015-01-21 17:47:59 +000070
Neil Fullerf4a606d2015-06-29 16:02:23 +010071 // Explicitly set the timeouts to infinity.
72 client.setConnectTimeout(0, TimeUnit.MILLISECONDS);
73 client.setReadTimeout(0, TimeUnit.MILLISECONDS);
74 client.setWriteTimeout(0, TimeUnit.MILLISECONDS);
75
Neil Fuller2ab42192015-01-21 17:47:59 +000076 // Do not permit http -> https and https -> http redirects.
Neil Fullere78f1172015-01-20 09:39:41 +000077 client.setFollowSslRedirects(false);
Alex Klyubind2167392015-03-20 19:52:23 -070078
79 if (NetworkSecurityPolicy.isCleartextTrafficPermitted()) {
80 // Permit cleartext traffic only (this is a handler for HTTP, not for HTTPS).
81 client.setConnectionSpecs(CLEARTEXT_ONLY);
82 } else {
83 // Cleartext HTTP denied by policy. Make okhttp deny cleartext HTTP attempts using the
84 // only mechanism it currently provides -- pretend there are no suitable routes.
85 client.setConnectionSpecs(Collections.<ConnectionSpec>emptyList());
86 }
Neil Fuller2ab42192015-01-21 17:47:59 +000087
88 // When we do not set the Proxy explicitly OkHttp picks up a ProxySelector using
89 // ProxySelector.getDefault().
Narayan Kamathbf0942e2013-06-13 12:06:06 +010090 if (proxy != null) {
91 client.setProxy(proxy);
92 }
93
Neil Fuller2ab42192015-01-21 17:47:59 +000094 // OkHttp requires that we explicitly set the response cache.
Neil Fullere78f1172015-01-20 09:39:41 +000095 OkUrlFactory okUrlFactory = new OkUrlFactory(client);
Neil Fuller3c938a32014-02-19 09:40:26 +000096 ResponseCache responseCache = ResponseCache.getDefault();
97 if (responseCache != null) {
Neil Fullere78f1172015-01-20 09:39:41 +000098 AndroidInternal.setResponseCache(okUrlFactory, responseCache);
Neil Fuller3c938a32014-02-19 09:40:26 +000099 }
Neil Fullere78f1172015-01-20 09:39:41 +0000100 return okUrlFactory;
Narayan Kamathbf0942e2013-06-13 12:06:06 +0100101 }
Neil Fuller84f674f2014-09-03 17:01:55 +0100102
Narayan Kamathc3f6f162012-08-09 11:57:05 +0100103}