Skip to content

Commit

Permalink
Drop ALPN support.
Browse files Browse the repository at this point in the history
#666
(cherry picked from commit 4c86085)
  • Loading branch information
swankjesse committed Apr 15, 2014
1 parent 4bee81e commit 36efdad
Showing 1 changed file with 9 additions and 29 deletions.
38 changes: 9 additions & 29 deletions okhttp/src/main/java/com/squareup/okhttp/internal/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,16 @@
*
* <p>NPN (Next Protocol Negotiation) was developed for SPDY. It is widely
* available and we support it on both Android (4.1+) and OpenJDK 7 (via the
* Jetty NPN-boot library).
* Jetty NPN-boot library). NPN is not yet available on Java 8.
*
* <p>ALPN (Application Layer Protocol Negotiation) is the successor to NPN. It
* has some technical advantages over NPN. We support it on Android (4.4+) only.
* has some technical advantages over NPN. ALPN first arrived in Android 4.4,
* but that release suffers a <a href="http://goo.gl/y5izPP">concurrency bug</a>
* so we don't use it. ALPN will be supported in the future.
*
* <p>On platforms that support both extensions, OkHttp will use both,
* preferring ALPN's result. Future versions of OkHttp will drop support NPN.
* preferring ALPN's result. Future versions of OkHttp will drop support for
* NPN.
*
* <h3>Deflater Sync Flush</h3>
* SPDY header compression requires a recent version of {@code
Expand Down Expand Up @@ -170,21 +173,14 @@ private static Platform findPlatform() {
// Attempt to find Android 4.1+ APIs.
Method setNpnProtocols = null;
Method getNpnSelectedProtocol = null;
Method setAlpnProtocols = null;
Method getAlpnSelectedProtocol = null;
try {
setNpnProtocols = openSslSocketClass.getMethod("setNpnProtocols", byte[].class);
getNpnSelectedProtocol = openSslSocketClass.getMethod("getNpnSelectedProtocol");
try {
setAlpnProtocols = openSslSocketClass.getMethod("setAlpnProtocols", byte[].class);
getAlpnSelectedProtocol = openSslSocketClass.getMethod("getAlpnSelectedProtocol");
} catch (NoSuchMethodException ignored) {
}
} catch (NoSuchMethodException ignored) {
}

return new Android(openSslSocketClass, setUseSessionTickets, setHostname, setNpnProtocols,
getNpnSelectedProtocol, setAlpnProtocols, getAlpnSelectedProtocol);
getNpnSelectedProtocol);
} catch (ClassNotFoundException ignored) {
// This isn't an Android runtime.
} catch (NoSuchMethodException ignored) {
Expand Down Expand Up @@ -225,21 +221,13 @@ private static class Android extends Platform {
private final Method setNpnProtocols;
private final Method getNpnSelectedProtocol;

// Non-null on Android 4.4+.
private final Method setAlpnProtocols;
private final Method getAlpnSelectedProtocol;

private Android(
Class<?> openSslSocketClass, Method setUseSessionTickets, Method setHostname,
Method setNpnProtocols, Method getNpnSelectedProtocol, Method setAlpnProtocols,
Method getAlpnSelectedProtocol) {
private Android(Class<?> openSslSocketClass, Method setUseSessionTickets, Method setHostname,
Method setNpnProtocols, Method getNpnSelectedProtocol) {
this.openSslSocketClass = openSslSocketClass;
this.setUseSessionTickets = setUseSessionTickets;
this.setHostname = setHostname;
this.setNpnProtocols = setNpnProtocols;
this.getNpnSelectedProtocol = getNpnSelectedProtocol;
this.setAlpnProtocols = setAlpnProtocols;
this.getAlpnSelectedProtocol = getAlpnSelectedProtocol;
}

@Override public void connectSocket(Socket socket, InetSocketAddress address,
Expand Down Expand Up @@ -273,9 +261,6 @@ private Android(
if (!openSslSocketClass.isInstance(socket)) return;
try {
Object[] parameters = { concatLengthPrefixed(npnProtocols) };
if (setAlpnProtocols != null) {
setAlpnProtocols.invoke(socket, parameters);
}
setNpnProtocols.invoke(socket, parameters);
} catch (IllegalAccessException e) {
throw new AssertionError(e);
Expand All @@ -288,11 +273,6 @@ private Android(
if (getNpnSelectedProtocol == null) return null;
if (!openSslSocketClass.isInstance(socket)) return null;
try {
if (getAlpnSelectedProtocol != null) {
// Prefer ALPN's result if it is present.
byte[] alpnResult = (byte[]) getAlpnSelectedProtocol.invoke(socket);
if (alpnResult != null) return ByteString.of(alpnResult);
}
byte[] npnResult = (byte[]) getNpnSelectedProtocol.invoke(socket);
if (npnResult == null) return null;
return ByteString.of(npnResult);
Expand Down

0 comments on commit 36efdad

Please sign in to comment.