|
| 1 | +/*- |
| 2 | + * Copyright (c) 2011, 2022 Oracle and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Universal Permissive License v 1.0 as shown at |
| 5 | + * https://oss.oracle.com/licenses/upl/ |
| 6 | + */ |
| 7 | + |
| 8 | +package oracle.nosql.driver.httpclient; |
| 9 | + |
| 10 | +import static io.netty.handler.logging.LogLevel.DEBUG; |
| 11 | +import static oracle.nosql.driver.util.LogUtil.logFine; |
| 12 | + |
| 13 | +import java.net.SocketAddress; |
| 14 | +import java.util.Objects; |
| 15 | +import java.util.logging.Logger; |
| 16 | + |
| 17 | +import io.netty.channel.ChannelHandlerContext; |
| 18 | +import io.netty.channel.ChannelOutboundHandler; |
| 19 | +import io.netty.channel.ChannelPipeline; |
| 20 | +import io.netty.channel.ChannelPromise; |
| 21 | +import io.netty.handler.codec.http.HttpClientCodec; |
| 22 | +import io.netty.handler.codec.http.HttpMessage; |
| 23 | +import io.netty.handler.codec.http.HttpObjectAggregator; |
| 24 | +import io.netty.handler.codec.http2.DefaultHttp2Connection; |
| 25 | +import io.netty.handler.codec.http2.DelegatingDecompressorFrameListener; |
| 26 | +import io.netty.handler.codec.http2.Http2Connection; |
| 27 | +import io.netty.handler.codec.http2.Http2FrameLogger; |
| 28 | +import io.netty.handler.codec.http2.HttpToHttp2ConnectionHandler; |
| 29 | +import io.netty.handler.codec.http2.HttpToHttp2ConnectionHandlerBuilder; |
| 30 | +import io.netty.handler.codec.http2.InboundHttp2ToHttpAdapterBuilder; |
| 31 | +import io.netty.handler.ssl.ApplicationProtocolNames; |
| 32 | +import io.netty.handler.ssl.ApplicationProtocolNegotiationHandler; |
| 33 | +import io.netty.util.internal.RecyclableArrayList; |
| 34 | + |
| 35 | +/** |
| 36 | + * Handle TLS protocol negotiation result, either Http1.1 or H2 |
| 37 | + * |
| 38 | + * The channel initialization process: |
| 39 | + * 1. Channel aquired from {@link ConnectionPool} after channel is active. |
| 40 | + * 2. SSL negotiation started, pipeline is not ready. |
| 41 | + * 3. {@link HttpProtocolNegotiationHandler} holds all {@link HttpMessage} while waiting for the negotiation result. |
| 42 | + * 4. Negotiation finished, {@link HttpProtocolNegotiationHandler} changes the pipeline according to the protocol selected. |
| 43 | + * 5. {@link HttpProtocolNegotiationHandler} removes itself from the pipeline. Writes any buffered {@link HttpMessage} to the channel. |
| 44 | + */ |
| 45 | +public class HttpProtocolNegotiationHandler extends ApplicationProtocolNegotiationHandler implements ChannelOutboundHandler { |
| 46 | + private static final Http2FrameLogger frameLogger = new Http2FrameLogger(DEBUG, HttpProtocolNegotiationHandler.class); |
| 47 | + |
| 48 | + private static final String CODEC_HANDLER_NAME = "http-codec"; |
| 49 | + private static final String AGG_HANDLER_NAME = "http-aggregator"; |
| 50 | + private static final String HTTP_HANDLER_NAME = "http-client-handler"; |
| 51 | + |
| 52 | + private final Logger logger; |
| 53 | + private final RecyclableArrayList bufferedMessages = RecyclableArrayList.newInstance(); |
| 54 | + private final HttpClientHandler handler; |
| 55 | + private final int maxChunkSize; |
| 56 | + private final int maxContentLength; |
| 57 | + |
| 58 | + public HttpProtocolNegotiationHandler(String fallbackProtocol, HttpClientHandler handler, int maxChunkSize, int maxContentLength, Logger logger) { |
| 59 | + super(fallbackProtocol); |
| 60 | + |
| 61 | + this.logger = logger; |
| 62 | + this.handler = handler; |
| 63 | + this.maxChunkSize = maxChunkSize; |
| 64 | + this.maxContentLength = maxContentLength; |
| 65 | + } |
| 66 | + |
| 67 | + private void writeBufferedMessages(ChannelHandlerContext ctx) { |
| 68 | + if (!this.bufferedMessages.isEmpty()) { |
| 69 | + for(int i = 0; i < this.bufferedMessages.size(); ++i) { |
| 70 | + Pair<Object, ChannelPromise> p = (Pair<Object, ChannelPromise>)this.bufferedMessages.get(i); |
| 71 | + ctx.channel().write(p.first, p.second); |
| 72 | + } |
| 73 | + |
| 74 | + this.bufferedMessages.clear(); |
| 75 | + } |
| 76 | + this.bufferedMessages.recycle(); |
| 77 | + } |
| 78 | + |
| 79 | + private void configureHttp1(ChannelHandlerContext ctx) { |
| 80 | + ChannelPipeline p = ctx.pipeline(); |
| 81 | + |
| 82 | + p.addLast(CODEC_HANDLER_NAME, |
| 83 | + new HttpClientCodec(4096, // initial line |
| 84 | + 8192, // header size |
| 85 | + maxChunkSize)); // chunksize |
| 86 | + p.addLast(AGG_HANDLER_NAME, |
| 87 | + new HttpObjectAggregator(maxContentLength)); |
| 88 | + } |
| 89 | + |
| 90 | + private void configureHttp2(ChannelHandlerContext ctx) { |
| 91 | + ChannelPipeline p = ctx.pipeline(); |
| 92 | + |
| 93 | + Http2Connection connection = new DefaultHttp2Connection(false); |
| 94 | + HttpToHttp2ConnectionHandler connectionHandler = new HttpToHttp2ConnectionHandlerBuilder() |
| 95 | + .frameListener(new DelegatingDecompressorFrameListener( |
| 96 | + connection, |
| 97 | + new InboundHttp2ToHttpAdapterBuilder(connection) |
| 98 | + .maxContentLength(this.maxContentLength) |
| 99 | + .propagateSettings(false) |
| 100 | + .build())) |
| 101 | + .frameLogger(frameLogger) |
| 102 | + .connection(connection) |
| 103 | + .build(); |
| 104 | + |
| 105 | + p.addLast(connectionHandler); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + protected void configurePipeline(ChannelHandlerContext ctx, String protocol) { |
| 110 | + if (ApplicationProtocolNames.HTTP_2.equals(protocol)) { |
| 111 | + configureHttp2(ctx); |
| 112 | + } else if (ApplicationProtocolNames.HTTP_1_1.equals(protocol)) { |
| 113 | + configureHttp1(ctx); |
| 114 | + } else { |
| 115 | + throw new IllegalStateException("unknown http protocol: " + protocol); |
| 116 | + } |
| 117 | + logFine(this.logger, "HTTP protocol selected: " + protocol); |
| 118 | + ctx.pipeline().addLast(HTTP_HANDLER_NAME, handler); |
| 119 | + } |
| 120 | + |
| 121 | + /* |
| 122 | + * User can write requests right after the channel is active, while protocol |
| 123 | + * negotiation is still in progress. At this stage the pipeline is not ready |
| 124 | + * to write http request so we must hold them here. |
| 125 | + */ |
| 126 | + @Override |
| 127 | + public void write(ChannelHandlerContext ctx, Object o, ChannelPromise channelPromise) throws Exception { |
| 128 | + if (o instanceof HttpMessage) { |
| 129 | + Pair<Object, ChannelPromise> p = Pair.of(o, channelPromise); |
| 130 | + this.bufferedMessages.add(p); |
| 131 | + return; |
| 132 | + } |
| 133 | + |
| 134 | + // let non-http message to pass, so the HTTP2 preface and settings frame can be sent |
| 135 | + ctx.write(o, channelPromise); |
| 136 | + } |
| 137 | + |
| 138 | + /* |
| 139 | + * Protocol negotiation finish, handler removed, the pipeline is |
| 140 | + * ready to handle http messages. Write previousely buffered http messages. |
| 141 | + */ |
| 142 | + @Override |
| 143 | + public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { |
| 144 | + super.handlerRemoved(ctx); |
| 145 | + this.writeBufferedMessages(ctx); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public void bind(ChannelHandlerContext ctx, SocketAddress socketAddress, ChannelPromise channelPromise) { |
| 150 | + ctx.bind(socketAddress, channelPromise); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public void connect(ChannelHandlerContext ctx, SocketAddress socketAddress, SocketAddress socketAddress1, ChannelPromise channelPromise) throws Exception { |
| 155 | + ctx.connect(socketAddress, socketAddress1, channelPromise); |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public void disconnect(ChannelHandlerContext ctx, ChannelPromise channelPromise) { |
| 160 | + ctx.disconnect(channelPromise); |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public void close(ChannelHandlerContext ctx, ChannelPromise channelPromise) throws Exception { |
| 165 | + ctx.close(channelPromise); |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public void deregister(ChannelHandlerContext ctx, ChannelPromise channelPromise) { |
| 170 | + ctx.deregister(channelPromise); |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public void read(ChannelHandlerContext ctx) throws Exception { |
| 175 | + ctx.read(); |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public void flush(ChannelHandlerContext ctx) { |
| 180 | + ctx.flush(); |
| 181 | + } |
| 182 | + |
| 183 | + private static class Pair<A, B> { |
| 184 | + |
| 185 | + public final A first; |
| 186 | + public final B second; |
| 187 | + |
| 188 | + public Pair(A fst, B snd) { |
| 189 | + this.first = fst; |
| 190 | + this.second = snd; |
| 191 | + } |
| 192 | + |
| 193 | + public String toString() { |
| 194 | + return "Pair[" + first + "," + second + "]"; |
| 195 | + } |
| 196 | + |
| 197 | + public boolean equals(Object other) { |
| 198 | + if (other instanceof Pair<?, ?>) { |
| 199 | + return Objects.equals(first, ((Pair<?, ?>) other).first) && |
| 200 | + Objects.equals(second, ((Pair<?, ?>) other).second); |
| 201 | + } |
| 202 | + return false; |
| 203 | + } |
| 204 | + |
| 205 | + public int hashCode() { |
| 206 | + if (first == null) |
| 207 | + return (second == null) ? 0 : second.hashCode() + 1; |
| 208 | + else if (second == null) |
| 209 | + return first.hashCode() + 2; |
| 210 | + else |
| 211 | + return first.hashCode() * 17 + second.hashCode(); |
| 212 | + } |
| 213 | + |
| 214 | + public static <A, B> Pair<A, B> of(A a, B b) { |
| 215 | + return new Pair<>(a, b); |
| 216 | + } |
| 217 | + } |
| 218 | +} |
| 219 | + |
0 commit comments