1
0
mirror of https://github.com/square/okhttp.git synced 2026-01-22 15:42:00 +03:00

Merge pull request #502 from adriancole/mws-npn

Tell MockWebServer which NPN protocols to use.
This commit is contained in:
Adrian Cole
2014-02-01 11:23:29 -08:00
5 changed files with 61 additions and 4 deletions

8
benchmarks/README.md Normal file
View File

@@ -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.

View File

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

View File

@@ -138,6 +138,7 @@ public class Benchmark {
SSLContext sslContext = SslContextBuilder.localhost();
server.useHttps(sslContext.getSocketFactory(), false);
server.setNpnEnabled(true);
server.setNpnProtocols(protocols);
}
final MockResponse response = newResponse();

View File

@@ -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()");
@@ -165,6 +166,27 @@ public final class MockWebServer {
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
@@ -304,8 +326,7 @@ public final class MockWebServer {
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();

View File

@@ -2834,10 +2834,11 @@ public final class URLConnectionTest {
* -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());
}
}