diff --git a/okhttp-protocols/src/main/java/com/squareup/okhttp/internal/Platform.java b/okhttp-protocols/src/main/java/com/squareup/okhttp/internal/Platform.java index 6bdc540d1..f02a2ca60 100644 --- a/okhttp-protocols/src/main/java/com/squareup/okhttp/internal/Platform.java +++ b/okhttp-protocols/src/main/java/com/squareup/okhttp/internal/Platform.java @@ -360,7 +360,7 @@ public class Platform { private final List protocols; /** Set when remote peer notifies NPN is unsupported. */ private boolean unsupported; - /** When server, this is the protocol the client selected. */ + /** The protocol the client selected. */ private String selected; public JettyNpnProvider(List protocols) { @@ -388,11 +388,11 @@ public class Platform { // Pick the first protocol the server advertises and client knows. for (int i = 0, size = serverProtocols.size(); i < size; i++) { if (protocols.contains(serverProtocols.get(i))) { - return serverProtocols.get(i); + return selected = serverProtocols.get(i); } } // On no intersection, try client's first protocol. - return protocols.get(0); + return selected = protocols.get(0); } else if (methodName.equals("protocolSelected") && args.length == 1) { this.selected = (String) args[0]; // Client selected this protocol. return null; diff --git a/okhttp/src/test/java/com/squareup/okhttp/internal/http/ExternalHttp2Example.java b/okhttp/src/test/java/com/squareup/okhttp/internal/http/ExternalHttp2Example.java index 36d54668e..a7b85e353 100644 --- a/okhttp/src/test/java/com/squareup/okhttp/internal/http/ExternalHttp2Example.java +++ b/okhttp/src/test/java/com/squareup/okhttp/internal/http/ExternalHttp2Example.java @@ -20,10 +20,13 @@ import com.squareup.okhttp.OkHttpClient; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; +import java.util.List; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLSession; +import static com.squareup.okhttp.internal.http.OkHeaders.SELECTED_PROTOCOL; + public final class ExternalHttp2Example { public static void main(String[] args) throws Exception { URL url = new URL("https://twitter.com/"); @@ -38,8 +41,11 @@ public final class ExternalHttp2Example { int responseCode = connection.getResponseCode(); System.out.println(responseCode); - System.out.println(connection.getHeaderFields()); - + List protocolValues = connection.getHeaderFields().get(SELECTED_PROTOCOL); + // If null, probably you didn't add jetty's npn jar to your boot classpath! + if (protocolValues != null && !protocolValues.isEmpty()) { + System.out.println("PROTOCOL " + protocolValues.get(0)); + } BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); String line; diff --git a/okhttp/src/test/java/com/squareup/okhttp/internal/http/ExternalSpdyExample.java b/okhttp/src/test/java/com/squareup/okhttp/internal/http/ExternalSpdyExample.java index 11d723905..e43fbb065 100644 --- a/okhttp/src/test/java/com/squareup/okhttp/internal/http/ExternalSpdyExample.java +++ b/okhttp/src/test/java/com/squareup/okhttp/internal/http/ExternalSpdyExample.java @@ -20,10 +20,13 @@ import com.squareup.okhttp.OkHttpClient; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; +import java.util.List; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLSession; +import static com.squareup.okhttp.internal.http.OkHeaders.SELECTED_PROTOCOL; + public final class ExternalSpdyExample { public static void main(String[] args) throws Exception { URL url = new URL("https://www.google.ca/"); @@ -38,6 +41,11 @@ public final class ExternalSpdyExample { int responseCode = connection.getResponseCode(); System.out.println(responseCode); + List protocolValues = connection.getHeaderFields().get(SELECTED_PROTOCOL); + // If null, probably you didn't add jetty's npn jar to your boot classpath! + if (protocolValues != null && !protocolValues.isEmpty()) { + System.out.println("PROTOCOL " + protocolValues.get(0)); + } BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));