Skip to content

Commit b067963

Browse files
committed
Map Netty protocol names to NoSQL HttpConstants
1 parent eaf3f3c commit b067963

File tree

7 files changed

+41
-45
lines changed

7 files changed

+41
-45
lines changed

driver/src/main/java/oracle/nosql/driver/NoSQLHandleConfig.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import oracle.nosql.driver.Region.RegionProvider;
2323
import oracle.nosql.driver.iam.SignatureProvider;
24-
import io.netty.handler.ssl.ApplicationProtocolNames;
24+
import oracle.nosql.driver.util.HttpConstants;
2525
import io.netty.handler.ssl.SslContext;
2626

2727
/**
@@ -148,7 +148,7 @@ public class NoSQLHandleConfig implements Cloneable {
148148
*
149149
* Default: prefer H2 but fallback to Http1.1
150150
*/
151-
private List<String> httpProtocols = new ArrayList<>(Arrays.asList(ApplicationProtocolNames.HTTP_2, ApplicationProtocolNames.HTTP_1_1));
151+
private List<String> httpProtocols = new ArrayList<>(Arrays.asList(HttpConstants.HTTP_2, HttpConstants.HTTP_1_1));
152152

153153
/**
154154
* A RetryHandler, or null if not configured by the user.
@@ -569,15 +569,6 @@ public List<String> getHttpProtocols() {
569569
return httpProtocols;
570570
}
571571

572-
/**
573-
* Check if "h2" is in the protocols list
574-
*
575-
* @return true if "h2" is in the protocols list
576-
*/
577-
public boolean useHttp2() {
578-
return this.httpProtocols.contains(ApplicationProtocolNames.HTTP_2);
579-
}
580-
581572
/**
582573
* Returns the configured table request timeout value, in milliseconds.
583574
* The table request timeout default can be specified independently to allow

driver/src/main/java/oracle/nosql/driver/http/NoSQLHandleImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import oracle.nosql.driver.ops.TableUsageResult;
5151
import oracle.nosql.driver.ops.WriteMultipleRequest;
5252
import oracle.nosql.driver.ops.WriteMultipleResult;
53+
import oracle.nosql.driver.util.HttpConstants;
5354
import oracle.nosql.driver.values.FieldValue;
5455
import oracle.nosql.driver.values.JsonUtils;
5556
import oracle.nosql.driver.values.MapValue;
@@ -128,7 +129,7 @@ private void configSslContext(NoSQLHandleConfig config) {
128129
}
129130
builder.sessionTimeout(config.getSSLSessionTimeout());
130131
builder.sessionCacheSize(config.getSSLSessionCacheSize());
131-
if (config.useHttp2()) {
132+
if (config.getHttpProtocols().contains(HttpConstants.HTTP_2)) {
132133
builder.ciphers(Http2SecurityUtil.CIPHERS, SupportedCipherSuiteFilter.INSTANCE);
133134
}
134135
builder.applicationProtocolConfig(

driver/src/main/java/oracle/nosql/driver/httpclient/HttpClient.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import io.netty.channel.socket.nio.NioSocketChannel;
3535
import io.netty.handler.codec.http.DefaultFullHttpRequest;
3636
import io.netty.handler.codec.http.HttpRequest;
37-
import io.netty.handler.ssl.ApplicationProtocolNames;
3837
import io.netty.handler.ssl.SslContext;
3938
import io.netty.util.AttributeKey;
4039
import io.netty.util.concurrent.Future;
@@ -43,6 +42,7 @@
4342
* from this config needs to be be abstracted to a generic class.
4443
*/
4544
import oracle.nosql.driver.NoSQLHandleConfig;
45+
import oracle.nosql.driver.util.HttpConstants;
4646

4747
/**
4848
* Netty HTTP client. Initialization process:
@@ -231,12 +231,12 @@ private HttpClient(String host,
231231

232232
this.httpProtocols = httpProtocols.size() > 0 ?
233233
httpProtocols :
234-
new ArrayList<>(Arrays.asList(ApplicationProtocolNames.HTTP_2, ApplicationProtocolNames.HTTP_1_1));
234+
new ArrayList<>(Arrays.asList(HttpConstants.HTTP_2, HttpConstants.HTTP_1_1));
235235

236236
// If Http1.1 is in the httpProtocols list, we prefer use it as the fallback
237237
// Else we use the last protocol in the httpProtocols list.
238-
this.httpFallbackProtocol = this.httpProtocols.contains(ApplicationProtocolNames.HTTP_1_1) ?
239-
ApplicationProtocolNames.HTTP_1_1 : this.httpProtocols.get(this.httpProtocols.size() - 1);
238+
this.httpFallbackProtocol = this.httpProtocols.contains(HttpConstants.HTTP_1_1) ?
239+
HttpConstants.HTTP_1_1 : this.httpProtocols.get(this.httpProtocols.size() - 1);
240240

241241
this.maxContentLength = (maxContentLength == 0 ?
242242
DEFAULT_MAX_CONTENT_LENGTH : maxContentLength);
@@ -382,15 +382,6 @@ public int getFreeChannelCount() {
382382
return pool.getFreeChannels();
383383
}
384384

385-
/**
386-
* Check if "h2" is in the protocols list
387-
*
388-
* @return true if "h2" is in the protocols list
389-
*/
390-
public boolean useHttp2() {
391-
return this.httpProtocols.contains(ApplicationProtocolNames.HTTP_2);
392-
}
393-
394385
/* available for testing */
395386
ConnectionPool getConnectionPool() {
396387
return pool;

driver/src/main/java/oracle/nosql/driver/httpclient/HttpClientChannelPoolHandler.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
import io.netty.channel.pool.ChannelHealthChecker;
2424
import io.netty.channel.pool.ChannelPoolHandler;
2525
import io.netty.handler.proxy.HttpProxyHandler;
26-
import io.netty.handler.ssl.ApplicationProtocolNames;
2726
import io.netty.handler.ssl.SslHandler;
2827
import io.netty.handler.ssl.SslHandshakeCompletionEvent;
2928
import io.netty.util.concurrent.Future;
29+
import oracle.nosql.driver.util.HttpConstants;
3030

3131
/**
3232
* This is an instance of Netty's ChannelPoolHandler used to initialize
@@ -71,11 +71,12 @@ private void configureSSL(Channel ch) {
7171
private void configureClearText(Channel ch) {
7272
ChannelPipeline p = ch.pipeline();
7373
HttpClientHandler handler = new HttpClientHandler(client.getLogger());
74+
boolean useHttp2 = client.getHttpProtocols().contains(HttpConstants.HTTP_2);
7475

7576
// Only true when HTTP_2 is the only protocol, as if user set:
76-
// config.setHttpProtocols(ApplicationProtocolNames.HTTP_2);
77-
if (client.useHttp2() &&
78-
ApplicationProtocolNames.HTTP_2.equals(client.getHttpFallbackProtocol())) {
77+
// config.setHttpProtocols(HttpConstants.HTTP_2);
78+
if (useHttp2 &&
79+
HttpConstants.HTTP_2.equals(client.getHttpFallbackProtocol())) {
7980
// If choose to use H2 and fallback is also H2
8081
// Then there is no need to upgrade from Http1.1 to H2C
8182
// Directly connects with H2 protocol, so called Http2-prior-knowledge
@@ -85,19 +86,19 @@ private void configureClearText(Channel ch) {
8586
}
8687

8788
// Only true when HTTP_1_1 is the only protocol, as if user set:
88-
// config.setHttpProtocols(ApplicationProtocolNames.HTTP_1_1);
89-
if (!client.useHttp2() &&
90-
ApplicationProtocolNames.HTTP_1_1.equals(client.getHttpFallbackProtocol())) {
89+
// config.setHttpProtocols(HttpConstants.HTTP_1_1);
90+
if (!useHttp2 &&
91+
HttpConstants.HTTP_1_1.equals(client.getHttpFallbackProtocol())) {
9192
HttpUtil.configureHttp1(ch.pipeline(), client.getMaxChunkSize(), client.getMaxContentLength());
9293
p.addLast(handler);
9394
return;
9495
}
9596

9697
// Only true when both HTTP_2 and HTTP_1_1 are available, the default option:
97-
// config.setHttpProtocols(ApplicationProtocolNames.HTTP_2,
98-
// ApplicationProtocolNames.HTTP_1_1)
99-
if (client.useHttp2() &&
100-
ApplicationProtocolNames.HTTP_1_1.equals(client.getHttpFallbackProtocol())) {
98+
// config.setHttpProtocols(HttpConstants.HTTP_2,
99+
// HttpConstants.HTTP_1_1)
100+
if (useHttp2 &&
101+
HttpConstants.HTTP_1_1.equals(client.getHttpFallbackProtocol())) {
101102
HttpUtil.configureH2C(ch.pipeline(), client.getMaxChunkSize(), client.getMaxContentLength());
102103
p.addLast(handler);
103104
return;

driver/src/main/java/oracle/nosql/driver/httpclient/HttpProtocolNegotiationHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
import io.netty.channel.ChannelOutboundHandler;
1717
import io.netty.channel.ChannelPromise;
1818
import io.netty.handler.codec.http.HttpMessage;
19-
import io.netty.handler.ssl.ApplicationProtocolNames;
2019
import io.netty.handler.ssl.ApplicationProtocolNegotiationHandler;
2120
import io.netty.util.internal.RecyclableArrayList;
21+
import oracle.nosql.driver.util.HttpConstants;
2222

2323
/**
2424
* Handle TLS protocol negotiation result, either Http1.1 or H2
@@ -50,9 +50,9 @@ public HttpProtocolNegotiationHandler(String fallbackProtocol, HttpClientHandler
5050

5151
@Override
5252
protected void configurePipeline(ChannelHandlerContext ctx, String protocol) {
53-
if (ApplicationProtocolNames.HTTP_2.equals(protocol)) {
53+
if (HttpConstants.HTTP_2.equals(protocol)) {
5454
HttpUtil.configureHttp2(ctx.pipeline(), this.maxContentLength);
55-
} else if (ApplicationProtocolNames.HTTP_1_1.equals(protocol)) {
55+
} else if (HttpConstants.HTTP_1_1.equals(protocol)) {
5656
HttpUtil.configureHttp1(ctx.pipeline(), this.maxChunkSize, this.maxContentLength);
5757
} else {
5858
throw new IllegalStateException("unknown http protocol: " + protocol);

driver/src/main/java/oracle/nosql/driver/httpclient/HttpUtil.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,24 @@
99

1010
import static io.netty.handler.logging.LogLevel.DEBUG;
1111

12+
import java.net.InetSocketAddress;
1213
import java.net.SocketAddress;
1314
import java.util.Objects;
1415

16+
import io.netty.buffer.Unpooled;
1517
import io.netty.channel.Channel;
1618
import io.netty.channel.ChannelDuplexHandler;
1719
import io.netty.channel.ChannelHandlerContext;
1820
import io.netty.channel.ChannelPipeline;
1921
import io.netty.channel.ChannelPromise;
22+
import io.netty.handler.codec.http.DefaultFullHttpRequest;
2023
import io.netty.handler.codec.http.HttpClientCodec;
2124
import io.netty.handler.codec.http.HttpClientUpgradeHandler;
25+
import io.netty.handler.codec.http.HttpHeaderNames;
2226
import io.netty.handler.codec.http.HttpMessage;
27+
import io.netty.handler.codec.http.HttpMethod;
2328
import io.netty.handler.codec.http.HttpObjectAggregator;
29+
import io.netty.handler.codec.http.HttpVersion;
2430
import io.netty.handler.codec.http2.DefaultHttp2Connection;
2531
import io.netty.handler.codec.http2.DelegatingDecompressorFrameListener;
2632
import io.netty.handler.codec.http2.Http2ClientUpgradeCodec;
@@ -30,6 +36,7 @@
3036
import io.netty.handler.codec.http2.HttpToHttp2ConnectionHandler;
3137
import io.netty.handler.codec.http2.HttpToHttp2ConnectionHandlerBuilder;
3238
import io.netty.handler.codec.http2.InboundHttp2ToHttpAdapterBuilder;
39+
import io.netty.util.ReferenceCountUtil;
3340
import io.netty.util.internal.RecyclableArrayList;
3441

3542
public class HttpUtil {
@@ -100,8 +107,7 @@ public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
100107
}
101108

102109
/**
103-
* A handler that holds HttpMessages (Except the first one)
104-
* So the upgrade handler can have time to finish clear text protocol upgrade
110+
* A handler that triggers the cleartext upgrade to HTTP/2 by sending an initial HTTP request.
105111
*/
106112
private static final class UpgradeRequestHandler extends ChannelDuplexHandler {
107113
private final int maxContentLength;
@@ -139,10 +145,6 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc
139145
@Override
140146
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
141147
if (msg instanceof HttpMessage) {
142-
// Only let the very first HttpMessage pass through
143-
// The H2C upgrade handler modifies the first message,
144-
// and tries to negotiate a new protocol with the server
145-
// This process takes one round trip
146148
if (upgrading) {
147149
Pair<Object, ChannelPromise> p = Pair.of(msg, promise);
148150
this.bufferedMessages.add(p);

driver/src/main/java/oracle/nosql/driver/util/HttpConstants.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@
1414
*/
1515
public class HttpConstants {
1616

17+
/**
18+
* {@code "h2"}: HTTP version 2
19+
*/
20+
public static final String HTTP_2 = "h2";
21+
22+
/**
23+
* {@code "http/1.1"}: HTTP version 1.1
24+
*/
25+
public static final String HTTP_1_1 = "http/1.1";
26+
1727
/**
1828
* The http header that identifies the client scoped unique request id
1929
* associated with each request. The request header is returned by the

0 commit comments

Comments
 (0)