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 | |
jwilson | 2231db3 | 2012-12-26 11:37:43 -0700 | [diff] [blame] | 18 | package com.squareup.okhttp; |
Narayan Kamath | c3f6f16 | 2012-08-09 11:57:05 +0100 | [diff] [blame] | 19 | |
Alex Klyubin | d216739 | 2015-03-20 19:52:23 -0700 | [diff] [blame] | 20 | import libcore.net.NetworkSecurityPolicy; |
Narayan Kamath | c3f6f16 | 2012-08-09 11:57:05 +0100 | [diff] [blame] | 21 | import java.io.IOException; |
| 22 | import java.net.Proxy; |
Neil Fuller | 3c938a3 | 2014-02-19 09:40:26 +0000 | [diff] [blame] | 23 | import java.net.ResponseCache; |
Narayan Kamath | c3f6f16 | 2012-08-09 11:57:05 +0100 | [diff] [blame] | 24 | import java.net.URL; |
| 25 | import java.net.URLConnection; |
| 26 | import java.net.URLStreamHandler; |
Neil Fuller | 2ab4219 | 2015-01-21 17:47:59 +0000 | [diff] [blame] | 27 | import java.util.Collections; |
| 28 | import java.util.List; |
Neil Fuller | f4a606d | 2015-06-29 16:02:23 +0100 | [diff] [blame^] | 29 | import java.util.concurrent.TimeUnit; |
Narayan Kamath | c3f6f16 | 2012-08-09 11:57:05 +0100 | [diff] [blame] | 30 | |
Narayan Kamath | bf0942e | 2013-06-13 12:06:06 +0100 | [diff] [blame] | 31 | public class HttpHandler extends URLStreamHandler { |
Neil Fuller | 8bced3e | 2014-09-11 10:45:54 +0100 | [diff] [blame] | 32 | |
Neil Fuller | 2ab4219 | 2015-01-21 17:47:59 +0000 | [diff] [blame] | 33 | private final static List<ConnectionSpec> CLEARTEXT_ONLY = |
| 34 | Collections.singletonList(ConnectionSpec.CLEARTEXT); |
| 35 | |
Neil Fuller | 8bced3e | 2014-09-11 10:45:54 +0100 | [diff] [blame] | 36 | private final ConfigAwareConnectionPool configAwareConnectionPool = |
| 37 | ConfigAwareConnectionPool.getInstance(); |
Neil Fuller | 84f674f | 2014-09-03 17:01:55 +0100 | [diff] [blame] | 38 | |
Narayan Kamath | c3f6f16 | 2012-08-09 11:57:05 +0100 | [diff] [blame] | 39 | @Override protected URLConnection openConnection(URL url) throws IOException { |
Neil Fuller | e78f117 | 2015-01-20 09:39:41 +0000 | [diff] [blame] | 40 | return newOkUrlFactory(null /* proxy */).open(url); |
Narayan Kamath | c3f6f16 | 2012-08-09 11:57:05 +0100 | [diff] [blame] | 41 | } |
| 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 Fuller | e78f117 | 2015-01-20 09:39:41 +0000 | [diff] [blame] | 47 | return newOkUrlFactory(proxy).open(url); |
Narayan Kamath | c3f6f16 | 2012-08-09 11:57:05 +0100 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | @Override protected int getDefaultPort() { |
jwilson | 2231db3 | 2012-12-26 11:37:43 -0700 | [diff] [blame] | 51 | return 80; |
Narayan Kamath | c3f6f16 | 2012-08-09 11:57:05 +0100 | [diff] [blame] | 52 | } |
Narayan Kamath | bf0942e | 2013-06-13 12:06:06 +0100 | [diff] [blame] | 53 | |
Neil Fuller | e78f117 | 2015-01-20 09:39:41 +0000 | [diff] [blame] | 54 | protected OkUrlFactory newOkUrlFactory(Proxy proxy) { |
| 55 | OkUrlFactory okUrlFactory = createHttpOkUrlFactory(proxy); |
Neil Fuller | 2ab4219 | 2015-01-21 17:47:59 +0000 | [diff] [blame] | 56 | // 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 Fuller | e78f117 | 2015-01-20 09:39:41 +0000 | [diff] [blame] | 59 | okUrlFactory.client().setConnectionPool(configAwareConnectionPool.get()); |
| 60 | return okUrlFactory; |
Neil Fuller | c19427f | 2014-09-09 16:46:57 +0100 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Creates an OkHttpClient suitable for creating {@link java.net.HttpURLConnection} instances on |
| 65 | * Android. |
| 66 | */ |
Neil Fuller | 2ab4219 | 2015-01-21 17:47:59 +0000 | [diff] [blame] | 67 | // Visible for android.net.Network. |
Neil Fuller | e78f117 | 2015-01-20 09:39:41 +0000 | [diff] [blame] | 68 | public static OkUrlFactory createHttpOkUrlFactory(Proxy proxy) { |
Narayan Kamath | bf0942e | 2013-06-13 12:06:06 +0100 | [diff] [blame] | 69 | OkHttpClient client = new OkHttpClient(); |
Neil Fuller | 2ab4219 | 2015-01-21 17:47:59 +0000 | [diff] [blame] | 70 | |
Neil Fuller | f4a606d | 2015-06-29 16:02:23 +0100 | [diff] [blame^] | 71 | // 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 Fuller | 2ab4219 | 2015-01-21 17:47:59 +0000 | [diff] [blame] | 76 | // Do not permit http -> https and https -> http redirects. |
Neil Fuller | e78f117 | 2015-01-20 09:39:41 +0000 | [diff] [blame] | 77 | client.setFollowSslRedirects(false); |
Alex Klyubin | d216739 | 2015-03-20 19:52:23 -0700 | [diff] [blame] | 78 | |
| 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 Fuller | 2ab4219 | 2015-01-21 17:47:59 +0000 | [diff] [blame] | 87 | |
| 88 | // When we do not set the Proxy explicitly OkHttp picks up a ProxySelector using |
| 89 | // ProxySelector.getDefault(). |
Narayan Kamath | bf0942e | 2013-06-13 12:06:06 +0100 | [diff] [blame] | 90 | if (proxy != null) { |
| 91 | client.setProxy(proxy); |
| 92 | } |
| 93 | |
Neil Fuller | 2ab4219 | 2015-01-21 17:47:59 +0000 | [diff] [blame] | 94 | // OkHttp requires that we explicitly set the response cache. |
Neil Fuller | e78f117 | 2015-01-20 09:39:41 +0000 | [diff] [blame] | 95 | OkUrlFactory okUrlFactory = new OkUrlFactory(client); |
Neil Fuller | 3c938a3 | 2014-02-19 09:40:26 +0000 | [diff] [blame] | 96 | ResponseCache responseCache = ResponseCache.getDefault(); |
| 97 | if (responseCache != null) { |
Neil Fuller | e78f117 | 2015-01-20 09:39:41 +0000 | [diff] [blame] | 98 | AndroidInternal.setResponseCache(okUrlFactory, responseCache); |
Neil Fuller | 3c938a3 | 2014-02-19 09:40:26 +0000 | [diff] [blame] | 99 | } |
Neil Fuller | e78f117 | 2015-01-20 09:39:41 +0000 | [diff] [blame] | 100 | return okUrlFactory; |
Narayan Kamath | bf0942e | 2013-06-13 12:06:06 +0100 | [diff] [blame] | 101 | } |
Neil Fuller | 84f674f | 2014-09-03 17:01:55 +0100 | [diff] [blame] | 102 | |
Narayan Kamath | c3f6f16 | 2012-08-09 11:57:05 +0100 | [diff] [blame] | 103 | } |