Merge "Isolate OkHttp from frameworks/base." am: b1ccf7d392 am: f3436b166e am: 0ca4cc946c am: c6e6977bba
am: 6fcf83724c
Change-Id: I89ca8fb597c23c2608be31bb15999a8c8c2c1635
diff --git a/android/main/java/com/squareup/okhttp/ConnectionSpecs.java b/android/main/java/com/squareup/okhttp/ConnectionSpecs.java
new file mode 100644
index 0000000..08cf86a
--- /dev/null
+++ b/android/main/java/com/squareup/okhttp/ConnectionSpecs.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.squareup.okhttp;
+
+/**
+ * Exposes nonpublic {@link ConnectionSpec} API for internal use by libcore.
+ *
+ * @hide
+ */
+public class ConnectionSpecs {
+ /** uninstantiable */
+ private ConnectionSpecs() {
+ }
+
+ public static ConnectionSpec.Builder builder(boolean tls) {
+ return new ConnectionSpec.Builder(tls);
+ }
+
+}
diff --git a/android/main/java/com/squareup/okhttp/OkUrlFactories.java b/android/main/java/com/squareup/okhttp/OkUrlFactories.java
new file mode 100644
index 0000000..f520dce
--- /dev/null
+++ b/android/main/java/com/squareup/okhttp/OkUrlFactories.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.squareup.okhttp;
+
+import com.squareup.okhttp.internal.URLFilter;
+
+import java.net.HttpURLConnection;
+import java.net.Proxy;
+import java.net.URL;
+
+/**
+ * Exposes nonpublic {@link OkUrlFactory} API for internal use by libcore.
+ *
+ * @hide
+ */
+public class OkUrlFactories {
+
+ /** uninstantiable */
+ private OkUrlFactories() {
+ }
+
+ public static HttpURLConnection open(OkUrlFactory okUrlFactory, URL url, Proxy proxy) {
+ return okUrlFactory.open(url, proxy);
+ }
+
+ public static void setUrlFilter(OkUrlFactory okUrlFactory, URLFilter urlFilter) {
+ okUrlFactory.setUrlFilter(urlFilter);
+ }
+
+}
diff --git a/android/main/java/com/squareup/okhttp/ConfigAwareConnectionPool.java b/android/main/java/libcore/net/http/ConfigAwareConnectionPool.java
similarity index 97%
rename from android/main/java/com/squareup/okhttp/ConfigAwareConnectionPool.java
rename to android/main/java/libcore/net/http/ConfigAwareConnectionPool.java
index 36c3101..a50c0d0 100644
--- a/android/main/java/com/squareup/okhttp/ConfigAwareConnectionPool.java
+++ b/android/main/java/libcore/net/http/ConfigAwareConnectionPool.java
@@ -15,7 +15,9 @@
* limitations under the License.
*/
-package com.squareup.okhttp;
+package libcore.net.http;
+
+import com.squareup.okhttp.ConnectionPool;
import libcore.net.event.NetworkEventDispatcher;
import libcore.net.event.NetworkEventListener;
diff --git a/android/main/java/libcore/net/http/Dns.java b/android/main/java/libcore/net/http/Dns.java
new file mode 100644
index 0000000..e42c349
--- /dev/null
+++ b/android/main/java/libcore/net/http/Dns.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package libcore.net.http;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.List;
+
+/**
+ * A domain name service that resolves IP addresses for host names.
+ */
+public interface Dns {
+ /**
+ * Returns the IP addresses of {@code hostname}, in the order they should
+ * be attempted.
+ */
+ List<InetAddress> lookup(String hostname) throws UnknownHostException;
+}
diff --git a/android/main/java/com/squareup/okhttp/HttpHandler.java b/android/main/java/libcore/net/http/HttpHandler.java
similarity index 93%
rename from android/main/java/com/squareup/okhttp/HttpHandler.java
rename to android/main/java/libcore/net/http/HttpHandler.java
index 38eecb4..4c9a9a8 100644
--- a/android/main/java/com/squareup/okhttp/HttpHandler.java
+++ b/android/main/java/libcore/net/http/HttpHandler.java
@@ -15,8 +15,13 @@
* limitations under the License.
*/
-package com.squareup.okhttp;
+package libcore.net.http;
+import com.squareup.okhttp.AndroidInternal;
+import com.squareup.okhttp.ConnectionSpec;
+import com.squareup.okhttp.OkHttpClient;
+import com.squareup.okhttp.OkUrlFactories;
+import com.squareup.okhttp.OkUrlFactory;
import com.squareup.okhttp.internal.URLFilter;
import libcore.net.NetworkSecurityPolicy;
import java.io.IOException;
@@ -98,7 +103,7 @@
// Use the installed NetworkSecurityPolicy to determine which requests are permitted over
// http.
- okUrlFactory.setUrlFilter(CLEARTEXT_FILTER);
+ OkUrlFactories.setUrlFilter(okUrlFactory, CLEARTEXT_FILTER);
ResponseCache responseCache = ResponseCache.getDefault();
if (responseCache != null) {
diff --git a/android/main/java/libcore/net/http/HttpURLConnectionFactory.java b/android/main/java/libcore/net/http/HttpURLConnectionFactory.java
new file mode 100644
index 0000000..050405c
--- /dev/null
+++ b/android/main/java/libcore/net/http/HttpURLConnectionFactory.java
@@ -0,0 +1,173 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package libcore.net.http;
+
+import com.squareup.okhttp.ConnectionPool;
+import com.squareup.okhttp.OkHttpClient;
+import com.squareup.okhttp.OkUrlFactories;
+import com.squareup.okhttp.OkUrlFactory;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.MalformedURLException;
+import java.net.Proxy;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.UnknownHostException;
+import java.util.List;
+import java.util.Objects;
+import java.util.concurrent.TimeUnit;
+import javax.net.SocketFactory;
+
+/**
+ * A way to construct {@link java.net.HttpURLConnection}s that supports some
+ * configuration on a per-factory or per-connection basis rather than only via
+ * global static state such as {@link CookieHandler#setDefault(CookieHandler)}.
+ * The per-factory configuration is <b>optional</b>; if not set, global
+ * configuration or default behavior is used.
+ *
+ * This facade prevents tight coupling with the underlying implementation (on
+ * top of a particular version of OkHttp). Android code outside of libcore
+ * should never depend directly on OkHttp.
+ *
+ * This abstraction is not suitable for general use. Talk to the maintainers of
+ * this class before modifying it or adding additional dependencies.
+ *
+ * @hide
+ */
+public final class HttpURLConnectionFactory {
+
+ private ConnectionPool connectionPool;
+ private com.squareup.okhttp.Dns dns;
+
+ /**
+ * Sets a new ConnectionPool, specific to this URLFactory and not shared with
+ * any other connections, with the given configuration.
+ */
+ public void setNewConnectionPool(int maxIdleConnections, long keepAliveDuration,
+ TimeUnit timeUnit) {
+ this.connectionPool = new ConnectionPool(maxIdleConnections, keepAliveDuration, timeUnit);
+ }
+
+ public void setDns(Dns dns) {
+ Objects.requireNonNull(dns);
+ this.dns = new DnsAdapter(dns);
+ }
+
+ /**
+ * Opens a connection that uses the system default proxy settings and SocketFactory.
+ */
+ public URLConnection openConnection(URL url) throws IOException {
+ return internalOpenConnection(url, null /* socketFactory */, null /* proxy */);
+ }
+
+ /**
+ * Opens a connection that uses the system default SocketFactory and the specified
+ * proxy settings.
+ */
+ public URLConnection openConnection(URL url, Proxy proxy) throws IOException {
+ Objects.requireNonNull(proxy);
+ return internalOpenConnection(url, null /* socketFactory */, proxy);
+ }
+
+ /**
+ * Opens a connection that uses the specified SocketFactory and the system default
+ * proxy settings.
+ */
+ public URLConnection openConnection(URL url, SocketFactory socketFactory) throws IOException {
+ Objects.requireNonNull(socketFactory);
+ return internalOpenConnection(url, socketFactory, null /* proxy */);
+ }
+
+ /**
+ * Opens a connection using the specified SocketFactory and the specified proxy
+ * settings, overriding any system wide configuration.
+ */
+ public URLConnection openConnection(URL url, SocketFactory socketFactory, Proxy proxy)
+ throws IOException {
+ Objects.requireNonNull(socketFactory);
+ Objects.requireNonNull(proxy);
+ return internalOpenConnection(url, socketFactory, proxy);
+ }
+
+ private URLConnection internalOpenConnection(URL url, SocketFactory socketFactoryOrNull,
+ Proxy proxyOrNull) throws IOException {
+ String protocol = url.getProtocol();
+ OkUrlFactory okUrlFactory;
+ // TODO: HttpHandler creates OkUrlFactory instances that share the default ResponseCache.
+ // Could this cause unexpected behavior?
+ if (protocol.equals("http")) {
+ okUrlFactory = HttpHandler.createHttpOkUrlFactory(proxyOrNull);
+ } else if (protocol.equals("https")) {
+ okUrlFactory = HttpsHandler.createHttpsOkUrlFactory(proxyOrNull);
+ } else {
+ // OkHttp only supports HTTP and HTTPS.
+ throw new MalformedURLException("Invalid URL or unrecognized protocol " + protocol);
+ }
+
+ OkHttpClient client = okUrlFactory.client();
+ if (connectionPool != null) {
+ client.setConnectionPool(connectionPool);
+ }
+ if (dns != null) {
+ client.setDns(dns);
+ }
+ if (socketFactoryOrNull != null) {
+ client.setSocketFactory(socketFactoryOrNull);
+ }
+ if (proxyOrNull == null) {
+ return okUrlFactory.open(url);
+ } else {
+ return OkUrlFactories.open(okUrlFactory, url, proxyOrNull);
+ }
+ }
+
+ /**
+ * Adapts a {@link Dns} as a {@link com.squareup.okhttp.Dns}.
+ */
+ static final class DnsAdapter implements com.squareup.okhttp.Dns {
+ private final Dns adaptee;
+
+ DnsAdapter(Dns adaptee) {
+ this.adaptee = Objects.requireNonNull(adaptee);
+ }
+
+ @Override
+ public List<InetAddress> lookup(String hostname) throws UnknownHostException {
+ return adaptee.lookup(hostname);
+ }
+
+ @Override
+ public int hashCode() {
+ return 31 * DnsAdapter.class.hashCode() + adaptee.hashCode();
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (!(obj instanceof DnsAdapter)) {
+ return false;
+ }
+ return adaptee.equals(((DnsAdapter) obj).adaptee);
+ }
+
+ @Override
+ public String toString() {
+ return adaptee.toString();
+ }
+ }
+
+}
diff --git a/android/main/java/com/squareup/okhttp/HttpsHandler.java b/android/main/java/libcore/net/http/HttpsHandler.java
similarity index 89%
rename from android/main/java/com/squareup/okhttp/HttpsHandler.java
rename to android/main/java/libcore/net/http/HttpsHandler.java
index 3915df1..3dd870e 100644
--- a/android/main/java/com/squareup/okhttp/HttpsHandler.java
+++ b/android/main/java/libcore/net/http/HttpsHandler.java
@@ -15,7 +15,15 @@
* limitations under the License.
*/
-package com.squareup.okhttp;
+package libcore.net.http;
+
+import com.squareup.okhttp.CertificatePinner;
+import com.squareup.okhttp.ConnectionSpec;
+import com.squareup.okhttp.ConnectionSpecs;
+import com.squareup.okhttp.OkHttpClient;
+import com.squareup.okhttp.OkUrlFactories;
+import com.squareup.okhttp.OkUrlFactory;
+import com.squareup.okhttp.Protocol;
import java.net.Proxy;
import java.util.Collections;
@@ -32,7 +40,7 @@
* override the enabled ciphers or TLS versions set on the sockets it produces with a
* list hardcoded at release time. This is deliberate.
*/
- private static final ConnectionSpec TLS_CONNECTION_SPEC = new ConnectionSpec.Builder(true)
+ private static final ConnectionSpec TLS_CONNECTION_SPEC = ConnectionSpecs.builder(true)
.allEnabledCipherSuites()
.allEnabledTlsVersions()
.supportsTlsExtensions(true)
@@ -68,7 +76,7 @@
OkUrlFactory okUrlFactory = HttpHandler.createHttpOkUrlFactory(proxy);
// All HTTPS requests are allowed.
- okUrlFactory.setUrlFilter(null);
+ OkUrlFactories.setUrlFilter(okUrlFactory, null);
OkHttpClient okHttpClient = okUrlFactory.client();
diff --git a/android/test/java/com/squareup/okhttp/ConfigAwareConnectionPoolTest.java b/android/test/java/libcore/net/http/ConfigAwareConnectionPoolTest.java
similarity index 95%
rename from android/test/java/com/squareup/okhttp/ConfigAwareConnectionPoolTest.java
rename to android/test/java/libcore/net/http/ConfigAwareConnectionPoolTest.java
index 825f980..73bfd98 100644
--- a/android/test/java/com/squareup/okhttp/ConfigAwareConnectionPoolTest.java
+++ b/android/test/java/libcore/net/http/ConfigAwareConnectionPoolTest.java
@@ -15,7 +15,9 @@
* limitations under the License.
*/
-package com.squareup.okhttp;
+package libcore.net.http;
+
+import com.squareup.okhttp.ConnectionPool;
import org.junit.Test;