From 2231db3e6bb54447a9b14cf004a6cb03c373651c Mon Sep 17 00:00:00 2001 From: jwilson Date: Wed, 26 Dec 2012 11:37:43 -0700 Subject: Upgrade to the latest OkHttp. Note the internal package name has changed from libcore.net.http to com.squareup.okhttp. Change-Id: Ib0eb4bdd69f24f2a255975460bd001e59657ddcc --- .../main/java/com/squareup/okhttp/HttpHandler.java | 41 +++++ .../java/com/squareup/okhttp/HttpsHandler.java | 41 +++++ .../com/squareup/okhttp/internal/Platform.java | 97 ++++++++++ android/main/java/libcore/util/Libcore.java | 200 --------------------- 4 files changed, 179 insertions(+), 200 deletions(-) create mode 100644 android/main/java/com/squareup/okhttp/HttpHandler.java create mode 100644 android/main/java/com/squareup/okhttp/HttpsHandler.java create mode 100644 android/main/java/com/squareup/okhttp/internal/Platform.java delete mode 100644 android/main/java/libcore/util/Libcore.java (limited to 'android') diff --git a/android/main/java/com/squareup/okhttp/HttpHandler.java b/android/main/java/com/squareup/okhttp/HttpHandler.java new file mode 100644 index 0000000..c960160 --- /dev/null +++ b/android/main/java/com/squareup/okhttp/HttpHandler.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 java.io.IOException; +import java.net.Proxy; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLStreamHandler; + +public final class HttpHandler extends URLStreamHandler { + @Override protected URLConnection openConnection(URL url) throws IOException { + return new OkHttpClient().open(url); + } + + @Override protected URLConnection openConnection(URL url, Proxy proxy) throws IOException { + if (url == null || proxy == null) { + throw new IllegalArgumentException("url == null || proxy == null"); + } + return new OkHttpClient().setProxy(proxy).open(url); + } + + @Override protected int getDefaultPort() { + return 80; + } +} diff --git a/android/main/java/com/squareup/okhttp/HttpsHandler.java b/android/main/java/com/squareup/okhttp/HttpsHandler.java new file mode 100644 index 0000000..1dc3826 --- /dev/null +++ b/android/main/java/com/squareup/okhttp/HttpsHandler.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 java.io.IOException; +import java.net.Proxy; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLStreamHandler; + +public final class HttpsHandler extends URLStreamHandler { + @Override protected URLConnection openConnection(URL url) throws IOException { + return new OkHttpClient().open(url); + } + + @Override protected URLConnection openConnection(URL url, Proxy proxy) throws IOException { + if (url == null || proxy == null) { + throw new IllegalArgumentException("url == null || proxy == null"); + } + return new OkHttpClient().setProxy(proxy).open(url); + } + + @Override protected int getDefaultPort() { + return 443; + } +} diff --git a/android/main/java/com/squareup/okhttp/internal/Platform.java b/android/main/java/com/squareup/okhttp/internal/Platform.java new file mode 100644 index 0000000..7479de0 --- /dev/null +++ b/android/main/java/com/squareup/okhttp/internal/Platform.java @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2012 Square, Inc. + * Copyright (C) 2012 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.internal; + +import dalvik.system.SocketTagger; +import java.io.OutputStream; +import java.net.Socket; +import java.net.SocketException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.zip.Deflater; +import java.util.zip.DeflaterOutputStream; +import javax.net.ssl.SSLSocket; +import org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl; + +/** + * Access to proprietary Android APIs. Doesn't use reflection. + */ +public final class Platform { + private static final Platform PLATFORM = new Platform(); + + public static Platform get() { + return PLATFORM; + } + + public void logW(String warning) { + System.logW(warning); + } + + public void tagSocket(Socket socket) throws SocketException { + SocketTagger.get().tag(socket); + } + + public void untagSocket(Socket socket) throws SocketException { + SocketTagger.get().untag(socket); + } + + public URI toUriLenient(URL url) throws URISyntaxException { + return url.toURILenient(); + } + + public void enableTlsExtensions(SSLSocket socket, String uriHost) { + if (socket instanceof OpenSSLSocketImpl) { + OpenSSLSocketImpl openSSLSocket = (OpenSSLSocketImpl) socket; + openSSLSocket.setUseSessionTickets(true); + openSSLSocket.setHostname(uriHost); + } + } + + public void supportTlsIntolerantServer(SSLSocket socket) { + socket.setEnabledProtocols(new String[]{"SSLv3"}); + } + + /** + * Returns the negotiated protocol, or null if no protocol was negotiated. + */ + public byte[] getNpnSelectedProtocol(SSLSocket socket) { + return socket instanceof OpenSSLSocketImpl + ? ((OpenSSLSocketImpl) socket).getNpnSelectedProtocol() + : null; + } + + /** + * Sets client-supported protocols on a socket to send to a server. The + * protocols are only sent if the socket implementation supports NPN. + */ + public void setNpnProtocols(SSLSocket socket, byte[] npnProtocols) { + if (socket instanceof OpenSSLSocketImpl) { + ((OpenSSLSocketImpl) socket).setNpnProtocols(npnProtocols); + } + } + + /** + * Returns a deflater output stream that supports SYNC_FLUSH for SPDY name + * value blocks. This throws an {@link UnsupportedOperationException} on + * Java 6 and earlier where there is no built-in API to do SYNC_FLUSH. + */ + public OutputStream newDeflaterOutputStream( + OutputStream out, Deflater deflater, boolean syncFlush) { + return new DeflaterOutputStream(out, deflater, syncFlush); + } +} diff --git a/android/main/java/libcore/util/Libcore.java b/android/main/java/libcore/util/Libcore.java deleted file mode 100644 index c1cf069..0000000 --- a/android/main/java/libcore/util/Libcore.java +++ /dev/null @@ -1,200 +0,0 @@ -/* - * Copyright (C) 2012 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.util; - -import javax.net.ssl.SSLSocket; -import java.io.File; -import java.io.IOException; -import java.io.OutputStream; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.net.Socket; -import java.net.SocketException; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; -import java.nio.ByteOrder; -import java.util.zip.Deflater; -import java.util.zip.DeflaterOutputStream; - -/** - * APIs for interacting with Android's core library. The main purpose of this - * class is to access hidden methods on - * - * org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl - * - * via reflection. - */ -public final class Libcore { - - private Libcore() { - } - - private static final Class openSslSocketClass; - private static final Method setUseSessionTickets; - private static final Method setHostname; - private static final Method setNpnProtocols; - private static final Method getNpnSelectedProtocol; - private static final Constructor deflaterOutputStreamConstructor; - - static { - try { - openSslSocketClass = Class.forName( - "org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl"); - setUseSessionTickets = openSslSocketClass.getMethod( - "setUseSessionTickets", boolean.class); - setHostname = openSslSocketClass.getMethod("setHostname", String.class); - setNpnProtocols = openSslSocketClass.getMethod("setNpnProtocols", byte[].class); - getNpnSelectedProtocol = openSslSocketClass.getMethod("getNpnSelectedProtocol"); - deflaterOutputStreamConstructor = DeflaterOutputStream.class.getConstructor( - new Class[] { OutputStream.class, Deflater.class, boolean.class }); - } catch (ClassNotFoundException cnfe) { - throw new RuntimeException(cnfe); - } catch (NoSuchMethodException nsme) { - throw new RuntimeException(nsme); - } - } - - public static DeflaterOutputStream newDeflaterOutputStream( - OutputStream os, Deflater deflater, boolean syncFlush) { - try { - return deflaterOutputStreamConstructor.newInstance(os, deflater, syncFlush); - } catch (InstantiationException e) { - throw new RuntimeException("Unknown DeflaterOutputStream implementation."); - } catch (IllegalAccessException e) { - throw new RuntimeException("Unknown DeflaterOutputStream implementation."); - } catch (InvocationTargetException e) { - throw new RuntimeException("Unknown DeflaterOutputStream implementation."); - } - } - - public static void makeTlsTolerant(SSLSocket socket, String socketHost, boolean tlsTolerant) { - if (!tlsTolerant) { - socket.setEnabledProtocols(new String[] {"SSLv3"}); - return; - } - - if (openSslSocketClass.isInstance(socket)) { - try { - setUseSessionTickets.invoke(socket, true); - setHostname.invoke(socket, socketHost); - } catch (InvocationTargetException e) { - throw new RuntimeException(e); - } catch (IllegalAccessException e) { - throw new AssertionError(e); - } - } else { - throw new RuntimeException("Unknown socket implementation."); - } - } - - /** - * Returns the negotiated protocol, or null if no protocol was negotiated. - */ - public static byte[] getNpnSelectedProtocol(SSLSocket socket) { - if (openSslSocketClass.isInstance(socket)) { - try { - return (byte[]) getNpnSelectedProtocol.invoke(socket); - } catch (InvocationTargetException e) { - throw new RuntimeException(e); - } catch (IllegalAccessException e) { - throw new AssertionError(e); - } - } else { - throw new RuntimeException("Unknown socket implementation."); - } - } - - public static void setNpnProtocols(SSLSocket socket, byte[] npnProtocols) { - if (openSslSocketClass.isInstance(socket)) { - try { - setNpnProtocols.invoke(socket, new Object[] {npnProtocols}); - } catch (IllegalAccessException e) { - throw new AssertionError(e); - } catch (InvocationTargetException e) { - throw new RuntimeException(e); - } - } else { - throw new RuntimeException("Unknown socket implementation."); - } - } - - public static void deleteIfExists(File file) throws IOException { - // okhttp-changed: was Libcore.os.remove() in a try/catch block - file.delete(); - } - - public static void logW(String warning) { - // okhttp-changed: was System.logw() - System.out.println(warning); - } - - public static int getEffectivePort(URI uri) { - return getEffectivePort(uri.getScheme(), uri.getPort()); - } - - public static int getEffectivePort(URL url) { - return getEffectivePort(url.getProtocol(), url.getPort()); - } - - private static int getEffectivePort(String scheme, int specifiedPort) { - return specifiedPort != -1 - ? specifiedPort - : getDefaultPort(scheme); - } - - public static int getDefaultPort(String scheme) { - if ("http".equalsIgnoreCase(scheme)) { - return 80; - } else if ("https".equalsIgnoreCase(scheme)) { - return 443; - } else { - return -1; - } - } - - public static void checkOffsetAndCount(int arrayLength, int offset, int count) { - if ((offset | count) < 0 || offset > arrayLength || arrayLength - offset < count) { - throw new ArrayIndexOutOfBoundsException(); - } - } - - public static void tagSocket(Socket socket) { - } - - public static void untagSocket(Socket socket) throws SocketException { - } - - public static URI toUriLenient(URL url) throws URISyntaxException { - return url.toURI(); // this isn't as good as the built-in toUriLenient - } - - public static void pokeInt(byte[] dst, int offset, int value, ByteOrder order) { - if (order == ByteOrder.BIG_ENDIAN) { - dst[offset++] = (byte) ((value >> 24) & 0xff); - dst[offset++] = (byte) ((value >> 16) & 0xff); - dst[offset++] = (byte) ((value >> 8) & 0xff); - dst[offset ] = (byte) ((value >> 0) & 0xff); - } else { - dst[offset++] = (byte) ((value >> 0) & 0xff); - dst[offset++] = (byte) ((value >> 8) & 0xff); - dst[offset++] = (byte) ((value >> 16) & 0xff); - dst[offset ] = (byte) ((value >> 24) & 0xff); - } - } -} -- cgit v1.2.3