Skip to content

Commit 25f3576

Browse files
authored
okhttp: Convert to internal ConnectionSpec eagerly
This allows ProGuard to remove OkHttp's ConnectionSpec in most cases, saving about 40 methods. The savings won't be realized until DEFAULT_CONNECTION_SPEC is removed.
1 parent 6b753bd commit 25f3576

File tree

3 files changed

+44
-13
lines changed

3 files changed

+44
-13
lines changed

interop-testing/src/test/java/io/grpc/testing/integration/Http2OkHttpTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import com.google.common.base.Throwables;
2424
import com.google.protobuf.EmptyProtos.Empty;
2525
import com.squareup.okhttp.ConnectionSpec;
26-
import com.squareup.okhttp.TlsVersion;
2726
import io.grpc.ManagedChannel;
2827
import io.grpc.internal.AbstractServerImplBuilder;
2928
import io.grpc.internal.GrpcUtil;
@@ -94,9 +93,8 @@ protected ManagedChannel createChannel() {
9493
private OkHttpChannelBuilder createChannelBuilder() {
9594
OkHttpChannelBuilder builder = OkHttpChannelBuilder.forAddress("localhost", getPort())
9695
.maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE)
97-
.connectionSpec(new ConnectionSpec.Builder(OkHttpChannelBuilder.DEFAULT_CONNECTION_SPEC)
96+
.connectionSpec(new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
9897
.cipherSuites(TestUtils.preferredTestCiphers().toArray(new String[0]))
99-
.tlsVersions(ConnectionSpec.MODERN_TLS.tlsVersions().toArray(new TlsVersion[0]))
10098
.build())
10199
.overrideAuthority(GrpcUtil.authorityFromHostAndPort(
102100
TestUtils.TEST_SERVER_HOST, getPort()));

okhttp/src/main/java/io/grpc/okhttp/OkHttpChannelBuilder.java

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323

2424
import com.google.common.annotations.VisibleForTesting;
2525
import com.google.common.base.Preconditions;
26-
import com.squareup.okhttp.CipherSuite;
27-
import com.squareup.okhttp.ConnectionSpec;
28-
import com.squareup.okhttp.TlsVersion;
2926
import io.grpc.Attributes;
3027
import io.grpc.ExperimentalApi;
3128
import io.grpc.Internal;
@@ -40,7 +37,10 @@
4037
import io.grpc.internal.SharedResourceHolder;
4138
import io.grpc.internal.SharedResourceHolder.Resource;
4239
import io.grpc.internal.TransportTracer;
40+
import io.grpc.okhttp.internal.CipherSuite;
41+
import io.grpc.okhttp.internal.ConnectionSpec;
4342
import io.grpc.okhttp.internal.Platform;
43+
import io.grpc.okhttp.internal.TlsVersion;
4444
import java.net.InetSocketAddress;
4545
import java.net.SocketAddress;
4646
import java.security.GeneralSecurityException;
@@ -62,7 +62,39 @@
6262
public class OkHttpChannelBuilder extends
6363
AbstractManagedChannelImplBuilder<OkHttpChannelBuilder> {
6464

65-
public static final ConnectionSpec DEFAULT_CONNECTION_SPEC =
65+
/**
66+
* ConnectionSpec closely matching the default configuration that could be used as a basis for
67+
* modification.
68+
*
69+
* <p>Since this field is the only reference in gRPC to ConnectionSpec that may not be ProGuarded,
70+
* we are removing the field to reduce method count. We've been unable to find any existing users
71+
* of the field, and any such user would highly likely at least be changing the cipher suites,
72+
* which is sort of the only part that's non-obvious. Any existing user should instead create
73+
* their own spec from scratch or base it off ConnectionSpec.MODERN_TLS if believed to be
74+
* necessary. If this was providing you with value and don't want to see it removed, open a GitHub
75+
* issue to discuss keeping it.
76+
*
77+
* @deprecated Deemed of little benefit and users weren't using it. Just define one yourself
78+
*/
79+
@Deprecated
80+
public static final com.squareup.okhttp.ConnectionSpec DEFAULT_CONNECTION_SPEC =
81+
new com.squareup.okhttp.ConnectionSpec.Builder(com.squareup.okhttp.ConnectionSpec.MODERN_TLS)
82+
.cipherSuites(
83+
// The following items should be sync with Netty's Http2SecurityUtil.CIPHERS.
84+
com.squareup.okhttp.CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
85+
com.squareup.okhttp.CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
86+
com.squareup.okhttp.CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
87+
com.squareup.okhttp.CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
88+
com.squareup.okhttp.CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
89+
com.squareup.okhttp.CipherSuite.TLS_DHE_DSS_WITH_AES_128_GCM_SHA256,
90+
com.squareup.okhttp.CipherSuite.TLS_DHE_RSA_WITH_AES_256_GCM_SHA384,
91+
com.squareup.okhttp.CipherSuite.TLS_DHE_DSS_WITH_AES_256_GCM_SHA384)
92+
.tlsVersions(com.squareup.okhttp.TlsVersion.TLS_1_2)
93+
.supportsTlsExtensions(true)
94+
.build();
95+
96+
@VisibleForTesting
97+
static final ConnectionSpec INTERNAL_DEFAULT_CONNECTION_SPEC =
6698
new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
6799
.cipherSuites(
68100
// The following items should be sync with Netty's Http2SecurityUtil.CIPHERS.
@@ -110,7 +142,7 @@ public static OkHttpChannelBuilder forTarget(String target) {
110142

111143
private SSLSocketFactory sslSocketFactory;
112144
private HostnameVerifier hostnameVerifier;
113-
private ConnectionSpec connectionSpec = DEFAULT_CONNECTION_SPEC;
145+
private ConnectionSpec connectionSpec = INTERNAL_DEFAULT_CONNECTION_SPEC;
114146
private NegotiationType negotiationType = NegotiationType.TLS;
115147
private long keepAliveTimeNanos = KEEPALIVE_TIME_NANOS_DISABLED;
116148
private long keepAliveTimeoutNanos = DEFAULT_KEEPALIVE_TIMEOUT_NANOS;
@@ -272,17 +304,18 @@ public final OkHttpChannelBuilder hostnameVerifier(@Nullable HostnameVerifier ho
272304
* For secure connection, provides a ConnectionSpec to specify Cipher suite and
273305
* TLS versions.
274306
*
275-
* <p>By default {@link #DEFAULT_CONNECTION_SPEC} will be used.
307+
* <p>By default a modern, HTTP/2-compatible spec will be used.
276308
*
277309
* <p>This method is only used when building a secure connection. For plaintext
278310
* connection, use {@link #usePlaintext()} instead.
279311
*
280312
* @throws IllegalArgumentException
281313
* If {@code connectionSpec} is not with TLS
282314
*/
283-
public final OkHttpChannelBuilder connectionSpec(ConnectionSpec connectionSpec) {
315+
public final OkHttpChannelBuilder connectionSpec(
316+
com.squareup.okhttp.ConnectionSpec connectionSpec) {
284317
Preconditions.checkArgument(connectionSpec.isTls(), "plaintext ConnectionSpec is not accepted");
285-
this.connectionSpec = connectionSpec;
318+
this.connectionSpec = Utils.convertSpec(connectionSpec);
286319
return this;
287320
}
288321

@@ -481,7 +514,7 @@ public void run() {
481514
executor,
482515
socketFactory,
483516
hostnameVerifier,
484-
Utils.convertSpec(connectionSpec),
517+
connectionSpec,
485518
maxMessageSize,
486519
proxy,
487520
tooManyPingsRunnable,

okhttp/src/test/java/io/grpc/okhttp/OkHttpClientTransportTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public void testToString() throws Exception {
228228
executor,
229229
sslSocketFactory,
230230
hostnameVerifier,
231-
Utils.convertSpec(OkHttpChannelBuilder.DEFAULT_CONNECTION_SPEC),
231+
OkHttpChannelBuilder.INTERNAL_DEFAULT_CONNECTION_SPEC,
232232
DEFAULT_MAX_MESSAGE_SIZE,
233233
NO_PROXY,
234234
tooManyPingsRunnable,

0 commit comments

Comments
 (0)