1212import java .net .SocketAddress ;
1313import java .util .Objects ;
1414
15+ import io .netty .channel .Channel ;
1516import io .netty .channel .ChannelDuplexHandler ;
1617import io .netty .channel .ChannelHandlerContext ;
1718import io .netty .channel .ChannelPipeline ;
2425import io .netty .handler .codec .http2 .DelegatingDecompressorFrameListener ;
2526import io .netty .handler .codec .http2 .Http2ClientUpgradeCodec ;
2627import io .netty .handler .codec .http2 .Http2Connection ;
28+ import io .netty .handler .codec .http2 .Http2ConnectionHandler ;
2729import io .netty .handler .codec .http2 .Http2FrameLogger ;
2830import io .netty .handler .codec .http2 .HttpToHttp2ConnectionHandler ;
2931import io .netty .handler .codec .http2 .HttpToHttp2ConnectionHandlerBuilder ;
@@ -46,22 +48,19 @@ public static void configureHttp1(ChannelPipeline p, int maxChunkSize, int maxCo
4648 }
4749
4850 public static void configureHttp2 (ChannelPipeline p , int maxContentLength ) {
49- Http2Connection connection = new DefaultHttp2Connection (false );
50- HttpToHttp2ConnectionHandler connectionHandler = new HttpToHttp2ConnectionHandlerBuilder ()
51- .frameListener (new DelegatingDecompressorFrameListener (
52- connection ,
53- new InboundHttp2ToHttpAdapterBuilder (connection )
54- .maxContentLength (maxContentLength )
55- .propagateSettings (false )
56- .build ()))
57- .frameLogger (frameLogger )
58- .connection (connection )
59- .build ();
60-
61- p .addLast (connectionHandler );
51+ p .addLast (createHttp2ConnectionHandler (maxContentLength ));
6252 }
6353
6454 public static void configureH2C (ChannelPipeline p , int maxChunkSize , int maxContentLength ) {
55+ HttpClientCodec sourceCodec = new HttpClientCodec (4096 , 8192 , maxChunkSize );
56+ Http2ClientUpgradeCodec upgradeCodec = new Http2ClientUpgradeCodec (createHttp2ConnectionHandler (maxContentLength ));
57+ HttpClientUpgradeHandler upgradeHandler = new UpgradeHandler (sourceCodec , upgradeCodec , maxContentLength );
58+ p .addLast (sourceCodec ,
59+ upgradeHandler ,
60+ new UpgradeRequestHandler (maxContentLength ));
61+ }
62+
63+ private static Http2ConnectionHandler createHttp2ConnectionHandler (int maxContentLength ) {
6564 Http2Connection connection = new DefaultHttp2Connection (false );
6665 HttpToHttp2ConnectionHandler connectionHandler = new HttpToHttp2ConnectionHandlerBuilder ()
6766 .frameListener (new DelegatingDecompressorFrameListener (
@@ -73,20 +72,14 @@ public static void configureH2C(ChannelPipeline p, int maxChunkSize, int maxCont
7372 .frameLogger (frameLogger )
7473 .connection (connection )
7574 .build ();
76-
77- HttpClientCodec sourceCodec = new HttpClientCodec (4096 , 8192 , maxChunkSize );
78- Http2ClientUpgradeCodec upgradeCodec = new Http2ClientUpgradeCodec (connectionHandler );
79- HttpClientUpgradeHandler upgradeHandler = new UpgradeHandler (sourceCodec , upgradeCodec , maxContentLength );
80- p .addLast (sourceCodec ,
81- upgradeHandler ,
82- new UpgradeRequestHandler (maxContentLength ));
75+ return connectionHandler ;
8376 }
8477
85- public static void writeBufferedMessages (ChannelHandlerContext ctx , RecyclableArrayList bufferedMessages ) {
78+ public static void writeBufferedMessages (Channel ch , RecyclableArrayList bufferedMessages ) {
8679 if (!bufferedMessages .isEmpty ()) {
8780 for (int i = 0 ; i < bufferedMessages .size (); ++i ) {
8881 Pair <Object , ChannelPromise > p = (Pair <Object , ChannelPromise >)bufferedMessages .get (i );
89- ctx . channel () .write (p .first , p .second );
82+ ch .write (p .first , p .second );
9083 }
9184
9285 bufferedMessages .clear ();
@@ -107,12 +100,12 @@ public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
107100 }
108101
109102 /**
110- * A handler that triggers the cleartext upgrade to HTTP/2 by sending an initial HTTP request.
103+ * A handler that holds HttpMessages (Except the first one)
104+ * So the upgrade handler can have time to finish clear text protocol upgrade
111105 */
112106 private static final class UpgradeRequestHandler extends ChannelDuplexHandler {
113107 private final int maxContentLength ;
114108 private final RecyclableArrayList bufferedMessages = RecyclableArrayList .newInstance ();
115- private boolean upgradeTried = false ;
116109 private boolean upgrading = false ;
117110
118111 public UpgradeRequestHandler (int maxContentLength ) {
@@ -122,7 +115,7 @@ public UpgradeRequestHandler(int maxContentLength) {
122115 @ Override
123116 public void handlerRemoved (ChannelHandlerContext ctx ) throws Exception {
124117 super .handlerRemoved (ctx );
125- writeBufferedMessages (ctx , this .bufferedMessages );
118+ writeBufferedMessages (ctx . channel () , this .bufferedMessages );
126119 }
127120
128121 @ Override
0 commit comments