Add SelectedProtocolQuerier to get protocol selected by NPN on Android.

Fixes #467

Manually tested by using NPN on Android 4.3 (api level 18) talk to GFE.
This commit is contained in:
Xudong Ma 2015-05-27 14:35:39 -07:00
parent 1eb16d94fc
commit 2fe279f7c2
2 changed files with 87 additions and 11 deletions

View File

@ -34,6 +34,7 @@ package com.squareup.okhttp;
import com.google.common.base.Preconditions;
import com.squareup.okhttp.internal.Platform;
import com.squareup.okhttp.internal.SelectedProtocolQuerier;
import java.io.IOException;
import java.net.InetSocketAddress;
@ -75,22 +76,15 @@ public final class OkHttpTlsUpgrader {
Platform platform = Platform.get();
String negotiatedProtocol = null;
try {
// It's possible that the user provided SSLSocketFactory has already done the handshake
// when creates the SSLSocket.
negotiatedProtocol = platform.getSelectedProtocol(sslSocket);
} catch (Exception e) {
// In some implementations, querying selected protocol before the handshake will fail with
// exception.
}
String negotiatedProtocol = SelectedProtocolQuerier.getSelectedProtocol(sslSocket);
if (negotiatedProtocol == null) {
try {
// Force handshake.
sslSocket.startHandshake();
negotiatedProtocol = platform.getSelectedProtocol(sslSocket);
negotiatedProtocol = SelectedProtocolQuerier.getSelectedProtocol(sslSocket);
if (negotiatedProtocol == null) {
throw new RuntimeException("protocol negotiation failed");
}

View File

@ -0,0 +1,82 @@
/*
* Copyright 2014, Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.squareup.okhttp.internal;
import java.net.Socket;
import javax.net.ssl.SSLSocket;
/**
* A helper class located in package com.squareup.okhttp.internal for getting protocol
* selected by NPN on Android.
*/
public class SelectedProtocolQuerier {
private static Platform platform = Platform.get();
// byte[] getNpnSelectedProtocol()
private static final OptionalMethod<Socket> GET_NPN_SELECTED_PROTOCOL =
new OptionalMethod<Socket>(byte[].class, "getNpnSelectedProtocol");
private static boolean android;
/** Returns the negotiated protocol, or null if no protocol was negotiated. */
public static String getSelectedProtocol(SSLSocket socket) {
String protocol = null;
try {
protocol = platform.getSelectedProtocol(socket);
} catch (Exception e) {
// In some implementations, querying selected protocol before the handshake will fail with
// exception.
}
if (protocol == null && android && GET_NPN_SELECTED_PROTOCOL.isSupported(socket)) {
byte[] result = (byte[]) GET_NPN_SELECTED_PROTOCOL.invokeWithoutCheckedException(socket);
if (result != null) {
protocol = new String(result, Util.UTF_8);
}
}
return protocol;
}
static {
android = true;
try {
// Attempt to find Android 2.3+ APIs.
Class.forName("com.android.org.conscrypt.OpenSSLSocketImpl");
} catch (ClassNotFoundException ignored) {
try {
// Older platform before being unbundled.
Class.forName("org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl");
} catch (ClassNotFoundException ignored2) {
android = false;
}
}
}
}