Skip to content

Commit bb640db

Browse files
committed
Improve https configuration layer.
1 parent 330498f commit bb640db

32 files changed

Lines changed: 721 additions & 706 deletions

vertx-core/src/main/asciidoc/http.adoc

Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,64 +2029,31 @@ on the same port value will share this random port.
20292029

20302030
Vert.x http servers and clients can be configured to use HTTPS in exactly the same way as net servers.
20312031

2032-
==== HTTPS on the server
2032+
Please see <<ssl, configuring net servers to use SSL>> for more information.
20332033

2034-
HTTPS is enabled with the `HttpServerOptions` {@link io.vertx.core.http.HttpServerOptions#setSsl(boolean) ssl} setting.
2035-
2036-
By default, it is disabled.
2037-
2038-
[source,$lang]
2039-
----
2040-
{@link examples.HTTPExamples#sslServerConfiguration}
2041-
----
2042-
2043-
You can read more about <<server_ssl,SSL server configuration>>
2044-
2045-
==== HTTPS on the client
2046-
2047-
Client SSL/TLS is enabled with the `HttpClientOptions` {@link io.vertx.core.http.HttpClientOptions#setSsl(boolean) ssl} property or {@link io.vertx.core.http.RequestOptions#setSsl(java.lang.Boolean) ssl} property.
2048-
2049-
{@link io.vertx.core.http.HttpClientOptions#setSsl(boolean)} setting acts as the default client setting.
2034+
SSL can also be enabled/disabled per request with {@link io.vertx.core.http.RequestOptions} or when
2035+
specifying a scheme with {@link io.vertx.core.http.RequestOptions#setAbsoluteURI(java.lang.String)}
2036+
method.
20502037

20512038
[source,$lang]
20522039
----
2053-
{@link examples.HTTPExamples#sslClientConfiguration}
2040+
{@link examples.HTTPExamples#setSSLPerRequest(io.vertx.core.http.HttpClient)}
20542041
----
20552042

2056-
SSL can also be enabled/disabled per request with {@link io.vertx.core.http.RequestOptions} or when
2057-
specifying a scheme with {@link io.vertx.core.http.RequestOptions#setAbsoluteURI(java.lang.String)}
2058-
method.
2043+
The {@link io.vertx.core.http.HttpClientOptions#setSsl(boolean)} setting acts as the default client setting.
20592044

2060-
{@link io.vertx.core.http.RequestOptions#setSsl(Boolean)} overrides the default client setting.
2045+
The {@link io.vertx.core.http.RequestOptions#setSsl(Boolean)} overrides the default client setting
20612046

20622047
* setting the value to `false` will disable SSL/TLS even if the client is configured to use SSL/TLS
2063-
* setting the value to `true` will enable SSL/TLS even if the client is configured to not use SSL/TLS, the actual client SSL/TLS (such as trust, key/certificate, ciphers, ALPN, ...) will be reused
2048+
* setting the value to `true` will enable SSL/TLS even if the client is configured to not use SSL/TLS, the actual
2049+
client SSL/TLS (such as trust, key/certificate, ciphers, ALPN, ...) will be reused
20642050

20652051
Likewise {@link io.vertx.core.http.RequestOptions#setAbsoluteURI(java.lang.String)} scheme
20662052
also overrides the default client setting.
20672053

2068-
[source,$lang]
2069-
----
2070-
{@link examples.HTTPExamples#sslClientRequestConfiguration}
2071-
----
2072-
2073-
You can also set {@link io.vertx.core.net.ClientSSLOptions} at request time.
2074-
2075-
[source,$lang]
2076-
----
2077-
{@link examples.HTTPExamples#sslClientRequestConfiguration2}
2078-
----
2079-
2080-
You can read more about <<client_ssl,SSL client configuration>>.
2081-
20822054
==== Server Name Indication (SNI)
20832055

2084-
Vert.x http servers can be configured to use SNI.
2085-
2086-
[source,$lang]
2087-
----
2088-
{@link examples.HTTPExamples#serverSNIConfig}
2089-
----
2056+
Vert.x http servers can be configured to use SNI in exactly the same way as {@linkplain io.vertx.core.net net servers}.
20902057

20912058
Vert.x http client will present the actual hostname as _server name_ during the TLS handshake.
20922059

vertx-core/src/main/java/examples/HTTPExamples.java

Lines changed: 1 addition & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,9 @@
5151
import io.vertx.core.http.WebSocketConnectOptions;
5252
import io.vertx.core.http.WebSocketFrame;
5353
import io.vertx.core.json.JsonObject;
54-
import io.vertx.core.net.ClientSSLOptions;
55-
import io.vertx.core.net.ConnectOptions;
56-
import io.vertx.core.net.JksOptions;
57-
import io.vertx.core.net.NetClient;
58-
import io.vertx.core.net.NetClientOptions;
59-
import io.vertx.core.net.NetServer;
60-
import io.vertx.core.net.NetServerOptions;
6154
import io.vertx.core.net.NetSocket;
6255
import io.vertx.core.net.ProxyOptions;
6356
import io.vertx.core.net.ProxyType;
64-
import io.vertx.core.net.ServerSSLOptions;
6557
import io.vertx.core.net.endpoint.LoadBalancer;
6658
import io.vertx.core.net.endpoint.ServerEndpoint;
6759
import io.vertx.core.streams.Pipe;
@@ -1301,54 +1293,7 @@ public void randomServersharing(Vertx vertx) {
13011293
}).listen(-1);
13021294
}
13031295

1304-
public void sslServerConfiguration(Vertx vertx) {
1305-
ServerSSLOptions sslOptions = new ServerSSLOptions()
1306-
.setKeyCertOptions(
1307-
new JksOptions().
1308-
setPath("/path/to/your/server-keystore.jks").
1309-
setPassword("password-of-your-keystore")
1310-
);
1311-
1312-
HttpServerOptions options = new HttpServerOptions()
1313-
.setSsl(true)
1314-
.setSslOptions(sslOptions);
1315-
1316-
HttpServer server = vertx.createHttpServer(options);
1317-
}
1318-
1319-
public void sslClientConfiguration(Vertx vertx) {
1320-
ClientSSLOptions sslOptions = new ClientSSLOptions()
1321-
.setTrustOptions(new JksOptions().
1322-
setPath("/path/to/your/truststore.jks").
1323-
setPassword("password-of-your-truststore")
1324-
);
1325-
1326-
HttpClientOptions options = new HttpClientOptions()
1327-
.setSsl(true)
1328-
.setSslOptions(sslOptions);
1329-
1330-
HttpClientAgent client = vertx.createHttpClient(options);
1331-
}
1332-
1333-
public void serverSNIConfig(Vertx vertx) {
1334-
ServerSSLOptions sslOptions = new ServerSSLOptions()
1335-
.setKeyCertOptions(new JksOptions().
1336-
setPath("/path/to/your/server-keystore.jks").
1337-
setPassword("password-of-your-keystore"))
1338-
.setSni(true);
1339-
}
1340-
1341-
public void sslClientRequestConfiguration(Vertx vertx, int port, String host) {
1342-
ClientSSLOptions sslOptions = new ClientSSLOptions()
1343-
.setTrustOptions(new JksOptions().
1344-
setPath("/path/to/your/truststore.jks").
1345-
setPassword("password-of-your-truststore")
1346-
);
1347-
1348-
HttpClientOptions options = new HttpClientOptions().setSslOptions(sslOptions);
1349-
1350-
HttpClientAgent client = vertx.createHttpClient(options);
1351-
1296+
public void setSSLPerRequest(HttpClient client) {
13521297
client
13531298
.request(new RequestOptions()
13541299
.setHost("localhost")
@@ -1361,28 +1306,6 @@ public void sslClientRequestConfiguration(Vertx vertx, int port, String host) {
13611306
});
13621307
}
13631308

1364-
public void sslClientRequestConfiguration2(Vertx vertx, int port, String host) {
1365-
HttpClientAgent client = vertx.createHttpClient();
1366-
1367-
ClientSSLOptions sslOptions = new ClientSSLOptions()
1368-
.setTrustOptions(new JksOptions().
1369-
setPath("/path/to/your/truststore.jks").
1370-
setPassword("password-of-your-truststore")
1371-
);
1372-
1373-
client
1374-
.request(new RequestOptions()
1375-
.setHost("localhost")
1376-
.setPort(8080)
1377-
.setURI("/")
1378-
.setSsl(true)
1379-
.setSslOptions(sslOptions))
1380-
.compose(request -> request.send())
1381-
.onSuccess(response -> {
1382-
System.out.println("Received response with status code " + response.statusCode());
1383-
});
1384-
}
1385-
13861309
public static void setIdentityContentEncodingHeader(HttpServerRequest request) {
13871310
// Disable compression and send an image
13881311
request.response()

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,13 @@
2525
public class Http2ServerConfig {
2626

2727
private Http2Settings initialSettings;
28-
private boolean clearTextEnabled;
2928
private int connectionWindowSize;
3029
private boolean multiplexImplementation;
3130
private int rstFloodMaxRstFramePerWindow;
3231
private Duration rstFloodWindowDuration;
3332

3433
public Http2ServerConfig() {
3534
initialSettings = new Http2Settings().setMaxConcurrentStreams(DEFAULT_INITIAL_SETTINGS_MAX_CONCURRENT_STREAMS);
36-
clearTextEnabled = DEFAULT_HTTP2_CLEAR_TEXT_ENABLED;
3735
connectionWindowSize = DEFAULT_HTTP2_CONNECTION_WINDOW_SIZE;
3836
rstFloodMaxRstFramePerWindow = DEFAULT_HTTP2_RST_FLOOD_MAX_RST_FRAME_PER_WINDOW;
3937
rstFloodWindowDuration = Duration.of(DEFAULT_HTTP2_RST_FLOOD_WINDOW_DURATION, DEFAULT_HTTP2_RST_FLOOD_WINDOW_DURATION_TIME_UNIT.toChronoUnit());
@@ -42,7 +40,6 @@ public Http2ServerConfig() {
4240

4341
public Http2ServerConfig(Http2ServerConfig other) {
4442
this.initialSettings = other.initialSettings != null ? new Http2Settings(other.initialSettings) : null;
45-
this.clearTextEnabled = other.clearTextEnabled;
4643
this.connectionWindowSize = other.connectionWindowSize;
4744
this.rstFloodMaxRstFramePerWindow = other.rstFloodMaxRstFramePerWindow;
4845
this.rstFloodWindowDuration = other.rstFloodWindowDuration;
@@ -87,24 +84,6 @@ public Http2ServerConfig setRstFloodWindowDuration(Duration rstFloodWindowDurati
8784
return this;
8885
}
8986

90-
/**
91-
* @return whether the server accepts HTTP/2 over clear text connections
92-
*/
93-
public boolean isClearTextEnabled() {
94-
return clearTextEnabled;
95-
}
96-
97-
/**
98-
* Set whether HTTP/2 over clear text is enabled or disabled, default is enabled.
99-
*
100-
* @param clearTextEnabled whether to accept HTTP/2 over clear text
101-
* @return a reference to this, so the API can be used fluently
102-
*/
103-
public Http2ServerConfig setClearTextEnabled(boolean clearTextEnabled) {
104-
this.clearTextEnabled = clearTextEnabled;
105-
return this;
106-
}
107-
10887
/**
10988
* @return the default HTTP/2 connection window size
11089
*/

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private static List<HttpVersion> toSupportedVersion(HttpVersion version) {
3434
case HTTP_1_0:
3535
return List.of(HttpVersion.HTTP_1_0);
3636
case HTTP_1_1:
37-
return List.of(HttpVersion.HTTP_1_1, HttpVersion.HTTP_2);
37+
return List.of(HttpVersion.HTTP_1_1);
3838
case HTTP_2:
3939
return List.of(HttpVersion.HTTP_2, HttpVersion.HTTP_1_1);
4040
default:

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

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,6 @@ protected ClientSSLOptions createSSLOptions() {
276276
return super.createSSLOptions().setApplicationLayerProtocols(HttpUtils.fromHttpAlpnVersions(DEFAULT_ALPN_VERSIONS));
277277
}
278278

279-
@Override
280-
public HttpClientOptions setSslOptions(ClientSSLOptions sslOptions) {
281-
return (HttpClientOptions) super.setSslOptions(sslOptions);
282-
}
283-
284279
@Override
285280
public HttpClientOptions setSendBufferSize(int sendBufferSize) {
286281
super.setSendBufferSize(sendBufferSize);
@@ -812,21 +807,9 @@ public HttpClientOptions setInitialSettings(Http2Settings settings) {
812807
return this;
813808
}
814809

815-
@Override
816-
public boolean isUseAlpn() {
817-
return protocolVersion == HttpVersion.HTTP_2;
818-
}
819-
820-
/**
821-
* Alpn supported is automatically managed by the HTTP client depending on the client supported protocols.
822-
*
823-
* @param useAlpn ignored
824-
* @return this object
825-
*/
826-
@Deprecated(forRemoval = true)
827810
@Override
828811
public HttpClientOptions setUseAlpn(boolean useAlpn) {
829-
return this;
812+
return (HttpClientOptions) super.setUseAlpn(useAlpn);
830813
}
831814

832815
@Override
@@ -835,22 +818,33 @@ public HttpClientOptions setSslEngineOptions(SSLEngineOptions sslEngineOptions)
835818
}
836819

837820
/**
838-
* @return {@code null}
821+
* @return the list of protocol versions to provide during the Application-Layer Protocol Negotiation. When
822+
* the list is empty, the client provides a best effort list according to {@link #setProtocolVersion}
839823
*/
840824
public List<HttpVersion> getAlpnVersions() {
841-
return null;
825+
List<String> applicationLayerProtocols = getOrCreateSSLOptions().getApplicationLayerProtocols();
826+
return applicationLayerProtocols != null ? HttpUtils.toHttpAlpnVersions(applicationLayerProtocols ) : null;
842827
}
843828

844829
/**
845-
* Does nothing, the list of supported alpn versions is managed by the HTTP client depending on the
846-
* client supported HTTP versions.
830+
* Set the list of protocol versions to provide to the server during the Application-Layer Protocol Negotiation.
831+
* When the list is empty, the client makes a best effort list according to {@link #setProtocolVersion}:
832+
*
833+
* <ul>
834+
* <li>{@link HttpVersion#HTTP_2}: [ "h2", "http/1.1" ]</li>
835+
* <li>otherwise: [{@link #getProtocolVersion()}]</li>
836+
* </ul>
847837
*
848-
* @param alpnVersions ignored
838+
* @param alpnVersions the versions
849839
* @return a reference to this, so the API can be used fluently
850-
* @deprecated this should not be used anymore
851840
*/
852-
@Deprecated(forRemoval = true)
853841
public HttpClientOptions setAlpnVersions(List<HttpVersion> alpnVersions) {
842+
ClientSSLOptions sslOptions = getOrCreateSSLOptions();
843+
if (alpnVersions != null) {
844+
sslOptions.setApplicationLayerProtocols(HttpUtils.fromHttpAlpnVersions(alpnVersions));
845+
} else {
846+
sslOptions.setApplicationLayerProtocols(null);
847+
}
854848
return this;
855849
}
856850

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,20 @@ public HttpServerConfig(HttpServerOptions options) {
9999
compression = null;
100100
}
101101

102-
this.versions = EnumSet.copyOf(DEFAULT_VERSIONS);
102+
Set<HttpVersion> versions;
103+
if (options.isSsl()) {
104+
if (options.isUseAlpn()) {
105+
versions = EnumSet.copyOf(options.getAlpnVersions());
106+
} else {
107+
versions = EnumSet.of(HttpVersion.HTTP_1_1);
108+
}
109+
} else if (options.isHttp2ClearTextEnabled()) {
110+
versions = EnumSet.copyOf(DEFAULT_VERSIONS);
111+
} else {
112+
versions = EnumSet.of(HttpVersion.HTTP_1_1);
113+
}
114+
115+
this.versions = versions;
103116
this.maxFormAttributeSize = options.getMaxFormAttributeSize();
104117
this.maxFormFields = options.getMaxFormFields();
105118
this.maxFormBufferedBytes = options.getMaxFormBufferedBytes();
@@ -150,9 +163,9 @@ public HttpServerConfig(HttpServerConfig other) {
150163
this.strictThreadMode = other.strictThreadMode;
151164
this.metricsName = other.metricsName;
152165
this.tracingPolicy = other.tracingPolicy;
153-
this.http1Config = other.http1Config != null ? new Http1ServerConfig(other.http1Config) : new Http1ServerConfig();
154-
this.http2Config = other.http2Config != null ? new Http2ServerConfig(other.http2Config) : new Http2ServerConfig();
155-
this.http3Config = other.http3Config != null ? new Http3ServerConfig(other.http3Config) : new Http3ServerConfig();
166+
this.http1Config = other.http1Config != null ? new Http1ServerConfig(other.http1Config) : null;
167+
this.http2Config = other.http2Config != null ? new Http2ServerConfig(other.http2Config) : null;
168+
this.http3Config = other.http3Config != null ? new Http3ServerConfig(other.http3Config) : null;
156169
this.webSocketConfig = other.webSocketConfig != null ? new WebSocketServerConfig(other.webSocketConfig) : new WebSocketServerConfig();
157170
this.compression = other.compression != null ? new HttpCompressionConfig(other.compression) : new HttpCompressionConfig();
158171
this.tcpConfig = other.tcpConfig != null ? new TcpServerConfig(other.tcpConfig) : defaultTcpServerConfig();

0 commit comments

Comments
 (0)