Narayan Kamath | c3f6f16 | 2012-08-09 11:57:05 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
Tobias Thierer | 7514444 | 2017-10-25 01:01:28 +0100 | [diff] [blame^] | 18 | package com.squareup.okhttp; |
Narayan Kamath | c3f6f16 | 2012-08-09 11:57:05 +0100 | [diff] [blame] | 19 | |
Chad Brubaker | 36e73c9 | 2016-01-27 10:42:38 -0800 | [diff] [blame] | 20 | import com.squareup.okhttp.internal.URLFilter; |
Alex Klyubin | d216739 | 2015-03-20 19:52:23 -0700 | [diff] [blame] | 21 | import libcore.net.NetworkSecurityPolicy; |
Narayan Kamath | c3f6f16 | 2012-08-09 11:57:05 +0100 | [diff] [blame] | 22 | import java.io.IOException; |
Neil Fuller | b701c07 | 2015-11-30 21:47:09 +0000 | [diff] [blame] | 23 | import java.net.HttpURLConnection; |
Narayan Kamath | c3f6f16 | 2012-08-09 11:57:05 +0100 | [diff] [blame] | 24 | import java.net.Proxy; |
Neil Fuller | 3c938a3 | 2014-02-19 09:40:26 +0000 | [diff] [blame] | 25 | import java.net.ResponseCache; |
Narayan Kamath | c3f6f16 | 2012-08-09 11:57:05 +0100 | [diff] [blame] | 26 | import java.net.URL; |
| 27 | import java.net.URLConnection; |
| 28 | import java.net.URLStreamHandler; |
Neil Fuller | 2ab4219 | 2015-01-21 17:47:59 +0000 | [diff] [blame] | 29 | import java.util.Collections; |
| 30 | import java.util.List; |
Neil Fuller | f4a606d | 2015-06-29 16:02:23 +0100 | [diff] [blame] | 31 | import java.util.concurrent.TimeUnit; |
Narayan Kamath | c3f6f16 | 2012-08-09 11:57:05 +0100 | [diff] [blame] | 32 | |
Narayan Kamath | bf0942e | 2013-06-13 12:06:06 +0100 | [diff] [blame] | 33 | public class HttpHandler extends URLStreamHandler { |
Neil Fuller | 8bced3e | 2014-09-11 10:45:54 +0100 | [diff] [blame] | 34 | |
Neil Fuller | 2ab4219 | 2015-01-21 17:47:59 +0000 | [diff] [blame] | 35 | private final static List<ConnectionSpec> CLEARTEXT_ONLY = |
| 36 | Collections.singletonList(ConnectionSpec.CLEARTEXT); |
| 37 | |
Chad Brubaker | 36e73c9 | 2016-01-27 10:42:38 -0800 | [diff] [blame] | 38 | private static final CleartextURLFilter CLEARTEXT_FILTER = new CleartextURLFilter(); |
| 39 | |
Neil Fuller | 8bced3e | 2014-09-11 10:45:54 +0100 | [diff] [blame] | 40 | private final ConfigAwareConnectionPool configAwareConnectionPool = |
| 41 | ConfigAwareConnectionPool.getInstance(); |
Neil Fuller | 84f674f | 2014-09-03 17:01:55 +0100 | [diff] [blame] | 42 | |
Narayan Kamath | c3f6f16 | 2012-08-09 11:57:05 +0100 | [diff] [blame] | 43 | @Override protected URLConnection openConnection(URL url) throws IOException { |
Neil Fuller | e78f117 | 2015-01-20 09:39:41 +0000 | [diff] [blame] | 44 | return newOkUrlFactory(null /* proxy */).open(url); |
Narayan Kamath | c3f6f16 | 2012-08-09 11:57:05 +0100 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | @Override protected URLConnection openConnection(URL url, Proxy proxy) throws IOException { |
| 48 | if (url == null || proxy == null) { |
| 49 | throw new IllegalArgumentException("url == null || proxy == null"); |
| 50 | } |
Neil Fuller | e78f117 | 2015-01-20 09:39:41 +0000 | [diff] [blame] | 51 | return newOkUrlFactory(proxy).open(url); |
Narayan Kamath | c3f6f16 | 2012-08-09 11:57:05 +0100 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | @Override protected int getDefaultPort() { |
jwilson | 2231db3 | 2012-12-26 11:37:43 -0700 | [diff] [blame] | 55 | return 80; |
Narayan Kamath | c3f6f16 | 2012-08-09 11:57:05 +0100 | [diff] [blame] | 56 | } |
Narayan Kamath | bf0942e | 2013-06-13 12:06:06 +0100 | [diff] [blame] | 57 | |
Neil Fuller | e78f117 | 2015-01-20 09:39:41 +0000 | [diff] [blame] | 58 | protected OkUrlFactory newOkUrlFactory(Proxy proxy) { |
| 59 | OkUrlFactory okUrlFactory = createHttpOkUrlFactory(proxy); |
Neil Fuller | 2ab4219 | 2015-01-21 17:47:59 +0000 | [diff] [blame] | 60 | // For HttpURLConnections created through java.net.URL Android uses a connection pool that |
| 61 | // is aware when the default network changes so that pooled connections are not re-used when |
| 62 | // the default network changes. |
Neil Fuller | e78f117 | 2015-01-20 09:39:41 +0000 | [diff] [blame] | 63 | okUrlFactory.client().setConnectionPool(configAwareConnectionPool.get()); |
| 64 | return okUrlFactory; |
Neil Fuller | c19427f | 2014-09-09 16:46:57 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Creates an OkHttpClient suitable for creating {@link java.net.HttpURLConnection} instances on |
| 69 | * Android. |
| 70 | */ |
Neil Fuller | 2ab4219 | 2015-01-21 17:47:59 +0000 | [diff] [blame] | 71 | // Visible for android.net.Network. |
Neil Fuller | e78f117 | 2015-01-20 09:39:41 +0000 | [diff] [blame] | 72 | public static OkUrlFactory createHttpOkUrlFactory(Proxy proxy) { |
Narayan Kamath | bf0942e | 2013-06-13 12:06:06 +0100 | [diff] [blame] | 73 | OkHttpClient client = new OkHttpClient(); |
Neil Fuller | 2ab4219 | 2015-01-21 17:47:59 +0000 | [diff] [blame] | 74 | |
Neil Fuller | f4a606d | 2015-06-29 16:02:23 +0100 | [diff] [blame] | 75 | // Explicitly set the timeouts to infinity. |
| 76 | client.setConnectTimeout(0, TimeUnit.MILLISECONDS); |
| 77 | client.setReadTimeout(0, TimeUnit.MILLISECONDS); |
| 78 | client.setWriteTimeout(0, TimeUnit.MILLISECONDS); |
| 79 | |
Neil Fuller | b701c07 | 2015-11-30 21:47:09 +0000 | [diff] [blame] | 80 | // Set the default (same protocol) redirect behavior. The default can be overridden for |
| 81 | // each instance using HttpURLConnection.setInstanceFollowRedirects(). |
| 82 | client.setFollowRedirects(HttpURLConnection.getFollowRedirects()); |
| 83 | |
Neil Fuller | 2ab4219 | 2015-01-21 17:47:59 +0000 | [diff] [blame] | 84 | // Do not permit http -> https and https -> http redirects. |
Neil Fuller | e78f117 | 2015-01-20 09:39:41 +0000 | [diff] [blame] | 85 | client.setFollowSslRedirects(false); |
Alex Klyubin | d216739 | 2015-03-20 19:52:23 -0700 | [diff] [blame] | 86 | |
Chad Brubaker | 36e73c9 | 2016-01-27 10:42:38 -0800 | [diff] [blame] | 87 | // Permit cleartext traffic only (this is a handler for HTTP, not for HTTPS). |
| 88 | client.setConnectionSpecs(CLEARTEXT_ONLY); |
Neil Fuller | 2ab4219 | 2015-01-21 17:47:59 +0000 | [diff] [blame] | 89 | |
| 90 | // When we do not set the Proxy explicitly OkHttp picks up a ProxySelector using |
| 91 | // ProxySelector.getDefault(). |
Narayan Kamath | bf0942e | 2013-06-13 12:06:06 +0100 | [diff] [blame] | 92 | if (proxy != null) { |
| 93 | client.setProxy(proxy); |
| 94 | } |
| 95 | |
Neil Fuller | 2ab4219 | 2015-01-21 17:47:59 +0000 | [diff] [blame] | 96 | // OkHttp requires that we explicitly set the response cache. |
Neil Fuller | e78f117 | 2015-01-20 09:39:41 +0000 | [diff] [blame] | 97 | OkUrlFactory okUrlFactory = new OkUrlFactory(client); |
Chad Brubaker | 36e73c9 | 2016-01-27 10:42:38 -0800 | [diff] [blame] | 98 | |
| 99 | // Use the installed NetworkSecurityPolicy to determine which requests are permitted over |
| 100 | // http. |
Tobias Thierer | fd27541 | 2017-04-11 21:01:50 +0100 | [diff] [blame] | 101 | OkUrlFactories.setUrlFilter(okUrlFactory, CLEARTEXT_FILTER); |
Chad Brubaker | 36e73c9 | 2016-01-27 10:42:38 -0800 | [diff] [blame] | 102 | |
Neil Fuller | 3c938a3 | 2014-02-19 09:40:26 +0000 | [diff] [blame] | 103 | ResponseCache responseCache = ResponseCache.getDefault(); |
| 104 | if (responseCache != null) { |
Neil Fuller | e78f117 | 2015-01-20 09:39:41 +0000 | [diff] [blame] | 105 | AndroidInternal.setResponseCache(okUrlFactory, responseCache); |
Neil Fuller | 3c938a3 | 2014-02-19 09:40:26 +0000 | [diff] [blame] | 106 | } |
Neil Fuller | e78f117 | 2015-01-20 09:39:41 +0000 | [diff] [blame] | 107 | return okUrlFactory; |
Narayan Kamath | bf0942e | 2013-06-13 12:06:06 +0100 | [diff] [blame] | 108 | } |
Neil Fuller | 84f674f | 2014-09-03 17:01:55 +0100 | [diff] [blame] | 109 | |
Chad Brubaker | 36e73c9 | 2016-01-27 10:42:38 -0800 | [diff] [blame] | 110 | private static final class CleartextURLFilter implements URLFilter { |
| 111 | @Override |
| 112 | public void checkURLPermitted(URL url) throws IOException { |
| 113 | String host = url.getHost(); |
| 114 | if (!NetworkSecurityPolicy.getInstance().isCleartextTrafficPermitted(host)) { |
| 115 | throw new IOException("Cleartext HTTP traffic to " + host + " not permitted"); |
| 116 | } |
| 117 | } |
| 118 | } |
Narayan Kamath | c3f6f16 | 2012-08-09 11:57:05 +0100 | [diff] [blame] | 119 | } |