Skip to content

Commit

Permalink
Merge pull request square#502 from adriancole/mws-npn
Browse files Browse the repository at this point in the history
Tell MockWebServer which NPN protocols to use.
  • Loading branch information
Adrian Cole committed Feb 1, 2014
2 parents b52e041 + 66b2582 commit f2ef66f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
8 changes: 8 additions & 0 deletions benchmarks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
OkHttp Benchmarks
=======================================

This module allows you to test the performance of HTTP clients.

### Running
1. If you made modifications to `com.squareup.okhttp.benchmarks.Benchmark` run `mvn compile`.
2. Run `mvn exec:exec` to launch a new JVM, which will execute the benchmark.
26 changes: 26 additions & 0 deletions benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,30 @@
<version>4.0.15.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<arguments>
<argument>-Xms512m</argument>
<argument>-Xmx512m</argument>
<commandlineArgs>-Xbootclasspath/p:${settings.localRepository}/org/mortbay/jetty/npn/npn-boot/${npn.version}/npn-boot-${npn.version}.jar</commandlineArgs>
<argument>-classpath</argument>
<classpath/>
<argument>com.squareup.okhttp.benchmarks.Benchmark</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ private MockWebServer startServer() throws IOException {
SSLContext sslContext = SslContextBuilder.localhost();
server.useHttps(sslContext.getSocketFactory(), false);
server.setNpnEnabled(true);
server.setNpnProtocols(protocols);
}

final MockResponse response = newResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public final class MockWebServer {

private int port = -1;
private boolean npnEnabled = true;
private List<Protocol> npnProtocols = Protocol.HTTP2_SPDY3_AND_HTTP;

public int getPort() {
if (port == -1) throw new IllegalStateException("Cannot retrieve port before calling play()");
Expand Down Expand Up @@ -165,6 +166,27 @@ public void setNpnEnabled(boolean npnEnabled) {
this.npnEnabled = npnEnabled;
}

/**
* Indicates the protocols supported by NPN on incoming HTTPS connections.
* This list is ignored when npn is disabled.
*
* @param protocols the protocols to use, in order of preference. The list
* must contain "http/1.1". It must not contain null.
*/
public void setNpnProtocols(List<Protocol> protocols) {
protocols = Util.immutableList(protocols);
if (!protocols.contains(Protocol.HTTP_11)) {
throw new IllegalArgumentException("protocols doesn't contain http/1.1: " + protocols);
}
if (protocols.contains(null)) {
throw new IllegalArgumentException("protocols must not contain null");
}
if (protocols.contains(ByteString.EMPTY)) {
throw new IllegalArgumentException("protocols contains an empty string");
}
this.npnProtocols = Util.immutableList(protocols);
}

/**
* Serve requests with HTTPS rather than otherwise.
* @param tunnelProxy true to expect the HTTP CONNECT method before
Expand Down Expand Up @@ -304,8 +326,7 @@ public void processConnection() throws Exception {
openClientSockets.put(socket, true);

if (npnEnabled) {
// TODO: expose means to select which protocols to advertise.
Platform.get().setNpnProtocols(sslSocket, Protocol.HTTP2_SPDY3_AND_HTTP);
Platform.get().setNpnProtocols(sslSocket, npnProtocols);
}

sslSocket.startHandshake();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2834,10 +2834,11 @@ private static class FakeProxySelector extends ProxySelector {
* -Xbootclasspath/p:/tmp/npn-boot-8.1.2.v20120308.jar}
*/
private void enableNpn(Protocol protocol) {
server.useHttps(sslContext.getSocketFactory(), false);
server.setNpnEnabled(true);
client.setSslSocketFactory(sslContext.getSocketFactory());
client.setHostnameVerifier(new RecordingHostnameVerifier());
client.setProtocols(Arrays.asList(protocol, Protocol.HTTP_11));
server.useHttps(sslContext.getSocketFactory(), false);
server.setNpnEnabled(true);
server.setNpnProtocols(client.getProtocols());
}
}

0 comments on commit f2ef66f

Please sign in to comment.