blob: ef9a56b4ebe55e8947782177101631bed48e1ad5 [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
Tobias Thierer75144442017-10-25 01:01:28 +010018package com.squareup.okhttp;
Narayan Kamathc3f6f162012-08-09 11:57:05 +010019
Chad Brubaker36e73c92016-01-27 10:42:38 -080020import com.squareup.okhttp.internal.URLFilter;
Alex Klyubind2167392015-03-20 19:52:23 -070021import libcore.net.NetworkSecurityPolicy;
Narayan Kamathc3f6f162012-08-09 11:57:05 +010022import java.io.IOException;
Neil Fullerb701c072015-11-30 21:47:09 +000023import java.net.HttpURLConnection;
Narayan Kamathc3f6f162012-08-09 11:57:05 +010024import java.net.Proxy;
Neil Fuller3c938a32014-02-19 09:40:26 +000025import java.net.ResponseCache;
Narayan Kamathc3f6f162012-08-09 11:57:05 +010026import java.net.URL;
27import java.net.URLConnection;
28import java.net.URLStreamHandler;
Neil Fuller2ab42192015-01-21 17:47:59 +000029import java.util.Collections;
30import java.util.List;
Neil Fullerf4a606d2015-06-29 16:02:23 +010031import java.util.concurrent.TimeUnit;
Narayan Kamathc3f6f162012-08-09 11:57:05 +010032
Narayan Kamathbf0942e2013-06-13 12:06:06 +010033public class HttpHandler extends URLStreamHandler {
Neil Fuller8bced3e2014-09-11 10:45:54 +010034
Neil Fuller2ab42192015-01-21 17:47:59 +000035 private final static List<ConnectionSpec> CLEARTEXT_ONLY =
36 Collections.singletonList(ConnectionSpec.CLEARTEXT);
37
Chad Brubaker36e73c92016-01-27 10:42:38 -080038 private static final CleartextURLFilter CLEARTEXT_FILTER = new CleartextURLFilter();
39
Neil Fuller8bced3e2014-09-11 10:45:54 +010040 private final ConfigAwareConnectionPool configAwareConnectionPool =
41 ConfigAwareConnectionPool.getInstance();
Neil Fuller84f674f2014-09-03 17:01:55 +010042
Narayan Kamathc3f6f162012-08-09 11:57:05 +010043 @Override protected URLConnection openConnection(URL url) throws IOException {
Neil Fullere78f1172015-01-20 09:39:41 +000044 return newOkUrlFactory(null /* proxy */).open(url);
Narayan Kamathc3f6f162012-08-09 11:57:05 +010045 }
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 Fullere78f1172015-01-20 09:39:41 +000051 return newOkUrlFactory(proxy).open(url);
Narayan Kamathc3f6f162012-08-09 11:57:05 +010052 }
53
54 @Override protected int getDefaultPort() {
jwilson2231db32012-12-26 11:37:43 -070055 return 80;
Narayan Kamathc3f6f162012-08-09 11:57:05 +010056 }
Narayan Kamathbf0942e2013-06-13 12:06:06 +010057
Neil Fullere78f1172015-01-20 09:39:41 +000058 protected OkUrlFactory newOkUrlFactory(Proxy proxy) {
59 OkUrlFactory okUrlFactory = createHttpOkUrlFactory(proxy);
Neil Fuller2ab42192015-01-21 17:47:59 +000060 // 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 Fullere78f1172015-01-20 09:39:41 +000063 okUrlFactory.client().setConnectionPool(configAwareConnectionPool.get());
64 return okUrlFactory;
Neil Fullerc19427f2014-09-09 16:46:57 +010065 }
66
67 /**
68 * Creates an OkHttpClient suitable for creating {@link java.net.HttpURLConnection} instances on
69 * Android.
70 */
Neil Fuller2ab42192015-01-21 17:47:59 +000071 // Visible for android.net.Network.
Neil Fullere78f1172015-01-20 09:39:41 +000072 public static OkUrlFactory createHttpOkUrlFactory(Proxy proxy) {
Narayan Kamathbf0942e2013-06-13 12:06:06 +010073 OkHttpClient client = new OkHttpClient();
Neil Fuller2ab42192015-01-21 17:47:59 +000074
Neil Fullerf4a606d2015-06-29 16:02:23 +010075 // 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 Fullerb701c072015-11-30 21:47:09 +000080 // 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 Fuller2ab42192015-01-21 17:47:59 +000084 // Do not permit http -> https and https -> http redirects.
Neil Fullere78f1172015-01-20 09:39:41 +000085 client.setFollowSslRedirects(false);
Alex Klyubind2167392015-03-20 19:52:23 -070086
Chad Brubaker36e73c92016-01-27 10:42:38 -080087 // Permit cleartext traffic only (this is a handler for HTTP, not for HTTPS).
88 client.setConnectionSpecs(CLEARTEXT_ONLY);
Neil Fuller2ab42192015-01-21 17:47:59 +000089
90 // When we do not set the Proxy explicitly OkHttp picks up a ProxySelector using
91 // ProxySelector.getDefault().
Narayan Kamathbf0942e2013-06-13 12:06:06 +010092 if (proxy != null) {
93 client.setProxy(proxy);
94 }
95
Neil Fuller2ab42192015-01-21 17:47:59 +000096 // OkHttp requires that we explicitly set the response cache.
Neil Fullere78f1172015-01-20 09:39:41 +000097 OkUrlFactory okUrlFactory = new OkUrlFactory(client);
Chad Brubaker36e73c92016-01-27 10:42:38 -080098
99 // Use the installed NetworkSecurityPolicy to determine which requests are permitted over
100 // http.
Tobias Thiererfd275412017-04-11 21:01:50 +0100101 OkUrlFactories.setUrlFilter(okUrlFactory, CLEARTEXT_FILTER);
Chad Brubaker36e73c92016-01-27 10:42:38 -0800102
Neil Fuller3c938a32014-02-19 09:40:26 +0000103 ResponseCache responseCache = ResponseCache.getDefault();
104 if (responseCache != null) {
Neil Fullere78f1172015-01-20 09:39:41 +0000105 AndroidInternal.setResponseCache(okUrlFactory, responseCache);
Neil Fuller3c938a32014-02-19 09:40:26 +0000106 }
Neil Fullere78f1172015-01-20 09:39:41 +0000107 return okUrlFactory;
Narayan Kamathbf0942e2013-06-13 12:06:06 +0100108 }
Neil Fuller84f674f2014-09-03 17:01:55 +0100109
Chad Brubaker36e73c92016-01-27 10:42:38 -0800110 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 Kamathc3f6f162012-08-09 11:57:05 +0100119}