Skip to content

Commit ea580e0

Browse files
committed
Remove HTTP options ALPN version field.
Motivation: HTTP options classes define a List<HttpVersion> as ALPN versions to use. Since the encapsulation of SSL options in SSLOptions, the HTTP options ALPN field mirrors the state of the underlying nested SSL options field. Updating HTTP ALPN fields is not correlated to the nested ALPN generic field and thus the TCP client/server code needs to calculate it to reflect it. Changes: Remove the HTTP options ALPN version field and make the getter/setter delegate to the SSL options generic ALPN field.
1 parent 7e8402e commit ea580e0

File tree

10 files changed

+84
-55
lines changed

10 files changed

+84
-55
lines changed

vertx-core/src/main/java/io/vertx/core/eventbus/EventBusOptions.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
@JsonGen(publicConverter = false)
3232
public class EventBusOptions extends TCPSSLOptions {
3333

34-
35-
3634
/**
3735
* The default cluster host = null which means use the same as the cluster manager, if possible.
3836
*/

vertx-core/src/main/java/io/vertx/core/http/HttpClientOptions.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313

1414
import io.netty.handler.logging.ByteBufFormat;
1515
import io.vertx.codegen.annotations.DataObject;
16+
import io.vertx.codegen.annotations.GenIgnore;
1617
import io.vertx.codegen.json.annotations.JsonGen;
1718
import io.vertx.core.buffer.Buffer;
19+
import io.vertx.core.http.impl.HttpUtils;
1820
import io.vertx.core.impl.Arguments;
1921
import io.vertx.core.json.JsonObject;
2022
import io.vertx.core.net.*;
@@ -181,7 +183,6 @@ public class HttpClientOptions extends ClientOptionsBase {
181183
private int maxInitialLineLength;
182184
private int maxHeaderSize;
183185
private Http2Settings initialSettings;
184-
private List<HttpVersion> alpnVersions;
185186
private boolean http2ClearTextUpgrade;
186187
private boolean http2ClearTextUpgradeWithPreflightRequest;
187188
private int maxRedirects;
@@ -236,7 +237,6 @@ public HttpClientOptions(HttpClientOptions other) {
236237
this.maxInitialLineLength = other.getMaxInitialLineLength();
237238
this.maxHeaderSize = other.getMaxHeaderSize();
238239
this.initialSettings = other.initialSettings != null ? new Http2Settings(other.initialSettings) : null;
239-
this.alpnVersions = other.alpnVersions != null ? new ArrayList<>(other.alpnVersions) : null;
240240
this.http2ClearTextUpgrade = other.http2ClearTextUpgrade;
241241
this.http2ClearTextUpgradeWithPreflightRequest = other.http2ClearTextUpgradeWithPreflightRequest;
242242
this.maxRedirects = other.maxRedirects;
@@ -288,7 +288,6 @@ private void init() {
288288
maxInitialLineLength = DEFAULT_MAX_INITIAL_LINE_LENGTH;
289289
maxHeaderSize = DEFAULT_MAX_HEADER_SIZE;
290290
initialSettings = new Http2Settings();
291-
alpnVersions = new ArrayList<>(DEFAULT_ALPN_VERSIONS);
292291
http2ClearTextUpgrade = DEFAULT_HTTP2_CLEAR_TEXT_UPGRADE;
293292
http2ClearTextUpgradeWithPreflightRequest = DEFAULT_HTTP2_CLEAR_TEXT_UPGRADE_WITH_PREFLIGHT_REQUEST;
294293
maxRedirects = DEFAULT_MAX_REDIRECTS;
@@ -299,6 +298,11 @@ private void init() {
299298
name = DEFAULT_NAME;
300299
}
301300

301+
@Override
302+
protected ClientSSLOptions createSSLOptions() {
303+
return super.createSSLOptions().setApplicationLayerProtocols(HttpUtils.fromHttpAlpnVersions(DEFAULT_ALPN_VERSIONS));
304+
}
305+
302306
@Override
303307
public HttpClientOptions setSendBufferSize(int sendBufferSize) {
304308
super.setSendBufferSize(sendBufferSize);
@@ -857,12 +861,13 @@ public HttpClientOptions setSslEngineOptions(SSLEngineOptions sslEngineOptions)
857861
* the list is empty, the client provides a best effort list according to {@link #setProtocolVersion}
858862
*/
859863
public List<HttpVersion> getAlpnVersions() {
860-
return alpnVersions;
864+
List<String> applicationLayerProtocols = getOrCreateSSLOptions().getApplicationLayerProtocols();
865+
return applicationLayerProtocols != null ? HttpUtils.toHttpAlpnVersions(applicationLayerProtocols ) : null;
861866
}
862867

863868
/**
864869
* Set the list of protocol versions to provide to the server during the Application-Layer Protocol Negotiation.
865-
* When the list is empty, the client provides a best effort list according to {@link #setProtocolVersion}:
870+
* When the list is empty, the client makes a best effort list according to {@link #setProtocolVersion}:
866871
*
867872
* <ul>
868873
* <li>{@link HttpVersion#HTTP_2}: [ "h2", "http/1.1" ]</li>
@@ -873,7 +878,12 @@ public List<HttpVersion> getAlpnVersions() {
873878
* @return a reference to this, so the API can be used fluently
874879
*/
875880
public HttpClientOptions setAlpnVersions(List<HttpVersion> alpnVersions) {
876-
this.alpnVersions = alpnVersions;
881+
ClientSSLOptions sslOptions = getOrCreateSSLOptions();
882+
if (alpnVersions != null) {
883+
sslOptions.setApplicationLayerProtocols(HttpUtils.fromHttpAlpnVersions(alpnVersions));
884+
} else {
885+
sslOptions.setApplicationLayerProtocols(null);
886+
}
877887
return this;
878888
}
879889

vertx-core/src/main/java/io/vertx/core/http/HttpServerOptions.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,10 @@
1919
import io.vertx.codegen.json.annotations.JsonGen;
2020
import io.vertx.core.Handler;
2121
import io.vertx.core.buffer.Buffer;
22+
import io.vertx.core.http.impl.HttpUtils;
2223
import io.vertx.core.impl.Arguments;
2324
import io.vertx.core.json.JsonObject;
24-
import io.vertx.core.net.KeyCertOptions;
25-
import io.vertx.core.net.NetServerOptions;
26-
import io.vertx.core.net.SSLEngineOptions;
27-
import io.vertx.core.net.TrafficShapingOptions;
28-
import io.vertx.core.net.TrustOptions;
25+
import io.vertx.core.net.*;
2926
import io.vertx.core.tracing.TracingPolicy;
3027

3128
import java.util.ArrayList;
@@ -35,6 +32,7 @@
3532
import java.util.Objects;
3633
import java.util.Set;
3734
import java.util.concurrent.TimeUnit;
35+
import java.util.stream.Collectors;
3836

3937
/**
4038
* Represents options used by an {@link io.vertx.core.http.HttpServer} instance
@@ -226,7 +224,6 @@ public class HttpServerOptions extends NetServerOptions {
226224
private int maxFormFields;
227225
private int maxFormBufferedBytes;
228226
private Http2Settings initialSettings;
229-
private List<HttpVersion> alpnVersions;
230227
private boolean http2ClearTextEnabled;
231228
private int http2ConnectionWindowSize;
232229
private boolean decompressionSupported;
@@ -277,7 +274,6 @@ public HttpServerOptions(HttpServerOptions other) {
277274
this.maxFormFields = other.getMaxFormFields();
278275
this.maxFormBufferedBytes = other.getMaxFormBufferedBytes();
279276
this.initialSettings = other.initialSettings != null ? new Http2Settings(other.initialSettings) : null;
280-
this.alpnVersions = other.alpnVersions != null ? new ArrayList<>(other.alpnVersions) : null;
281277
this.http2ClearTextEnabled = other.http2ClearTextEnabled;
282278
this.http2ConnectionWindowSize = other.http2ConnectionWindowSize;
283279
this.decompressionSupported = other.isDecompressionSupported();
@@ -336,7 +332,6 @@ private void init() {
336332
maxFormFields = DEFAULT_MAX_FORM_FIELDS;
337333
maxFormBufferedBytes = DEFAULT_MAX_FORM_BUFFERED_SIZE;
338334
initialSettings = new Http2Settings().setMaxConcurrentStreams(DEFAULT_INITIAL_SETTINGS_MAX_CONCURRENT_STREAMS);
339-
alpnVersions = new ArrayList<>(DEFAULT_ALPN_VERSIONS);
340335
http2ClearTextEnabled = DEFAULT_HTTP2_CLEAR_TEXT_ENABLED;
341336
http2ConnectionWindowSize = DEFAULT_HTTP2_CONNECTION_WINDOW_SIZE;
342337
decompressionSupported = DEFAULT_DECOMPRESSION_SUPPORTED;
@@ -365,6 +360,11 @@ public HttpServerOptions copy() {
365360
return new HttpServerOptions(this);
366361
}
367362

363+
@Override
364+
protected ServerSSLOptions createSSLOptions() {
365+
return super.createSSLOptions().setApplicationLayerProtocols(HttpUtils.fromHttpAlpnVersions(DEFAULT_ALPN_VERSIONS));
366+
}
367+
368368
@Override
369369
public HttpServerOptions setSendBufferSize(int sendBufferSize) {
370370
super.setSendBufferSize(sendBufferSize);
@@ -896,20 +896,26 @@ public HttpServerOptions setInitialSettings(Http2Settings settings) {
896896
}
897897

898898
/**
899-
* @return the list of protocol versions to provide during the Application-Layer Protocol Negotiatiation
899+
* @return the list of protocol versions to provide during the Application-Layer Protocol Negotiation
900900
*/
901901
public List<HttpVersion> getAlpnVersions() {
902-
return alpnVersions;
902+
List<String> applicationLayerProtocols = getOrCreateSSLOptions().getApplicationLayerProtocols();
903+
return applicationLayerProtocols != null ? HttpUtils.toHttpAlpnVersions(applicationLayerProtocols ) : null;
903904
}
904905

905906
/**
906-
* Set the list of protocol versions to provide to the server during the Application-Layer Protocol Negotiatiation.
907+
* Set the list of protocol versions to provide to the server during the Application-Layer Protocol Negotiation.
907908
*
908909
* @param alpnVersions the versions
909910
* @return a reference to this, so the API can be used fluently
910911
*/
911912
public HttpServerOptions setAlpnVersions(List<HttpVersion> alpnVersions) {
912-
this.alpnVersions = alpnVersions;
913+
ServerSSLOptions sslOptions = getOrCreateSSLOptions();
914+
if (alpnVersions != null) {
915+
sslOptions.setApplicationLayerProtocols(HttpUtils.fromHttpAlpnVersions(alpnVersions));
916+
} else {
917+
sslOptions.setApplicationLayerProtocols(null);
918+
}
913919
return this;
914920
}
915921

vertx-core/src/main/java/io/vertx/core/http/HttpVersion.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,18 @@ public enum HttpVersion {
3434
public String alpnName() {
3535
return alpnName;
3636
}
37+
38+
public static HttpVersion fromAlpnName(String alpnName) {
39+
switch (alpnName) {
40+
case "http/1.0":
41+
return HTTP_1_0;
42+
case "http/1.1":
43+
return HTTP_1_1;
44+
case "h2":
45+
return HTTP_2;
46+
default:
47+
return null;
48+
}
49+
}
50+
3751
}

vertx-core/src/main/java/io/vertx/core/http/impl/HttpClientBase.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.util.*;
2727
import java.util.concurrent.TimeUnit;
2828
import java.util.function.Predicate;
29-
import java.util.stream.Collectors;
3029

3130
/**
3231
* @author <a href="http://tfox.org">Tim Fox</a>
@@ -36,7 +35,6 @@ public class HttpClientBase implements MetricsProvider, Closeable {
3635
protected final VertxInternal vertx;
3736
public final HttpClientOptions options;
3837
protected final NetClientInternal netClient;
39-
protected final List<String> alpnVersions;
4038
protected final HttpClientMetrics metrics;
4139
protected final CloseSequence closeSequence;
4240
private volatile ClientSSLOptions defaultSslOptions;
@@ -48,23 +46,18 @@ public HttpClientBase(VertxInternal vertx, HttpClientOptions options) {
4846
if (!options.isKeepAlive() && options.isPipelining()) {
4947
throw new IllegalStateException("Cannot have pipelining with no keep alive");
5048
}
49+
options = new HttpClientOptions(options);
5150
List<HttpVersion> alpnVersions = options.getAlpnVersions();
5251
if (alpnVersions == null || alpnVersions.isEmpty()) {
53-
switch (options.getProtocolVersion()) {
54-
case HTTP_2:
55-
alpnVersions = Arrays.asList(HttpVersion.HTTP_2, HttpVersion.HTTP_1_1);
56-
break;
57-
default:
58-
alpnVersions = Collections.singletonList(options.getProtocolVersion());
59-
break;
52+
if (options.getProtocolVersion() == HttpVersion.HTTP_2) {
53+
options.setAlpnVersions(List.of(HttpVersion.HTTP_2, HttpVersion.HTTP_1_1));
54+
} else {
55+
options.setAlpnVersions(List.of(options.getProtocolVersion()));
6056
}
61-
} else {
62-
alpnVersions = new ArrayList<>(alpnVersions);
6357
}
64-
this.alpnVersions = alpnVersions.stream().map(HttpVersion::alpnName).collect(Collectors.toUnmodifiableList());
6558
this.vertx = vertx;
6659
this.metrics = vertx.metrics() != null ? vertx.metrics().createHttpClientMetrics(options) : null;
67-
this.options = new HttpClientOptions(options);
60+
this.options = options;
6861
this.closeSequence = new CloseSequence(p -> doClose(p), p1 -> doShutdown(p1));
6962
this.proxyFilter = options.getNonProxyHosts() != null ? ProxyFilter.nonProxyHosts(options.getNonProxyHosts()) : ProxyFilter.DEFAULT_PROXY_FILTER;
7063
this.netClient = new NetClientBuilder(vertx, new NetClientOptions(options).setProxyOptions(null)).metrics(metrics).build();
@@ -80,9 +73,6 @@ private void configureSSLOptions(ClientSSLOptions sslOptions) {
8073
if (sslOptions.getHostnameVerificationAlgorithm() == null) {
8174
sslOptions.setHostnameVerificationAlgorithm(options.isVerifyHost() ? "HTTPS" : "");
8275
}
83-
if (sslOptions.getApplicationLayerProtocols() == null) {
84-
sslOptions.setApplicationLayerProtocols(alpnVersions);
85-
}
8676
}
8777

8878
public NetClientInternal netClient() {

vertx-core/src/main/java/io/vertx/core/http/impl/HttpServerImpl.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@
2626
import io.vertx.core.spi.metrics.Metrics;
2727
import io.vertx.core.spi.metrics.MetricsProvider;
2828

29-
import java.util.List;
3029
import java.util.concurrent.TimeUnit;
3130
import java.util.function.Supplier;
32-
import java.util.stream.Collectors;
3331

3432
/**
3533
* @author <a href="http://tfox.org">Tim Fox</a>
@@ -70,7 +68,6 @@ public Future<Boolean> updateSSLOptions(ServerSSLOptions options, boolean force)
7068
throw new IllegalStateException("Not listening");
7169
}
7270
options = options.copy();
73-
configureApplicationLayerProtocols(options);
7471
return s.updateSSLOptions(options, force);
7572
}
7673

@@ -180,9 +177,6 @@ public synchronized Future<HttpServer> listen(SocketAddress address) {
180177
}
181178
HttpServerOptions options = this.options;
182179
HttpServerOptions tcpOptions = new HttpServerOptions(options);
183-
if (tcpOptions.getSslOptions() != null) {
184-
configureApplicationLayerProtocols(tcpOptions.getSslOptions());
185-
}
186180
ContextInternal context = vertx.getOrCreateContext();
187181
ContextInternal listenContext;
188182
// Not sure of this
@@ -271,18 +265,6 @@ public Future<Void> shutdown(long timeout, TimeUnit unit) {
271265
}
272266
}
273267

274-
/**
275-
* Configure the {@code options} to match the server configured HTTP versions.
276-
*/
277-
private void configureApplicationLayerProtocols(ServerSSLOptions options) {
278-
List<String> applicationProtocols = this.options
279-
.getAlpnVersions()
280-
.stream()
281-
.map(HttpVersion::alpnName)
282-
.collect(Collectors.toList());
283-
options.setApplicationLayerProtocols(applicationProtocols);
284-
}
285-
286268
private boolean isListening() {
287269
return tcpServer != null;
288270
}

vertx-core/src/main/java/io/vertx/core/http/impl/HttpUtils.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import java.util.List;
5252
import java.util.Map;
5353
import java.util.function.Consumer;
54+
import java.util.stream.Collectors;
5455

5556
import static io.netty.handler.codec.http.HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED;
5657
import static io.netty.handler.codec.http.HttpHeaderValues.MULTIPART_FORM_DATA;
@@ -996,4 +997,18 @@ public static String positiveLongToString(long value) {
996997
}
997998
return str;
998999
}
1000+
1001+
public static List<String> fromHttpAlpnVersions(List<io.vertx.core.http.HttpVersion> alpnVersions) {
1002+
return alpnVersions
1003+
.stream()
1004+
.map(io.vertx.core.http.HttpVersion::alpnName)
1005+
.collect(Collectors.toList());
1006+
}
1007+
1008+
public static List<io.vertx.core.http.HttpVersion> toHttpAlpnVersions(List<String> alpnVersions) {
1009+
return alpnVersions
1010+
.stream()
1011+
.map(io.vertx.core.http.HttpVersion::fromAlpnName)
1012+
.collect(Collectors.toList());
1013+
}
9991014
}

vertx-core/src/main/java/io/vertx/core/net/ClientOptionsBase.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ protected ClientSSLOptions getOrCreateSSLOptions() {
109109
return (ClientSSLOptions) super.getOrCreateSSLOptions();
110110
}
111111

112+
@Override
113+
protected ClientSSLOptions createSSLOptions() {
114+
return new ClientSSLOptions();
115+
}
116+
112117
/**
113118
*
114119
* @return true if all server certificates should be trusted

vertx-core/src/main/java/io/vertx/core/net/NetServerOptions.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ protected ServerSSLOptions getOrCreateSSLOptions() {
146146
return (ServerSSLOptions) super.getOrCreateSSLOptions();
147147
}
148148

149+
@Override
150+
protected ServerSSLOptions createSSLOptions() {
151+
return new ServerSSLOptions();
152+
}
153+
149154
@Override
150155
public NetServerOptions setSendBufferSize(int sendBufferSize) {
151156
super.setSendBufferSize(sendBufferSize);

vertx-core/src/main/java/io/vertx/core/net/TCPSSLOptions.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ private void init() {
227227

228228
protected SSLOptions getOrCreateSSLOptions() {
229229
if (sslOptions == null) {
230-
sslOptions = this instanceof ClientOptionsBase ? new ClientSSLOptions() : new ServerSSLOptions();
230+
sslOptions = createSSLOptions();
231231
// Necessary hacks because we return lazy created collections so we need to care about that
232232
if (enabledCipherSuites != null) {
233233
sslOptions.enabledCipherSuites = enabledCipherSuites;
@@ -248,6 +248,10 @@ protected SSLOptions getOrCreateSSLOptions() {
248248
return sslOptions;
249249
}
250250

251+
protected SSLOptions createSSLOptions() {
252+
return new SSLOptions();
253+
}
254+
251255
@GenIgnore
252256
public TcpOptions getTransportOptions() {
253257
return transportOptions;

0 commit comments

Comments
 (0)