1
0
mirror of https://github.com/square/okhttp.git synced 2026-01-24 04:02:07 +03:00

fix #447: Fix JettyNpnProvider.invoke() to pick the right protocol

This commit is contained in:
Adrian Cole
2014-01-18 09:49:04 -08:00
parent bfa2788032
commit 5843bd35bd
3 changed files with 19 additions and 5 deletions

View File

@@ -360,7 +360,7 @@ public class Platform {
private final List<String> 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<String> 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;

View File

@@ -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<String> 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;

View File

@@ -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<String> 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"));