3333import io .netty .channel .ChannelFutureListener ;
3434import io .netty .channel .ChannelInitializer ;
3535import io .netty .channel .EventLoopGroup ;
36- import io .netty .handler .codec .http .HttpClientCodec ;
3736import io .netty .handler .codec .http2 .Http2SecurityUtil ;
3837import io .netty .handler .proxy .ProxyHandler ;
3938import io .netty .handler .ssl .ApplicationProtocolConfig ;
5554
5655import static io .clientcore .core .utils .ServerSentEventUtils .attemptRetry ;
5756import static io .clientcore .core .utils .ServerSentEventUtils .processTextEventStream ;
57+ import static io .clientcore .http .netty4 .implementation .Netty4HandlerNames .HTTP_RESPONSE ;
58+ import static io .clientcore .http .netty4 .implementation .Netty4HandlerNames .PROGRESS_AND_TIMEOUT ;
5859import static io .clientcore .http .netty4 .implementation .Netty4Utility .awaitLatch ;
5960import static io .clientcore .http .netty4 .implementation .Netty4Utility .createCodec ;
6061import static io .clientcore .http .netty4 .implementation .Netty4Utility .sendHttp11Request ;
@@ -166,11 +167,10 @@ protected void initChannel(Channel channel) throws SSLException {
166167 channel .pipeline ().addLast (Netty4HandlerNames .SSL , ssl .newHandler (channel .alloc (), host , port ));
167168 channel .pipeline ()
168169 .addLast (Netty4HandlerNames .SSL_INITIALIZER , new Netty4SslInitializationHandler ());
169- }
170170
171- if (isHttps ) {
172171 channel .pipeline ()
173- .addLast (new Netty4AlpnHandler (request , addProgressAndTimeoutHandler , errorReference , latch ));
172+ .addLast (Netty4HandlerNames .ALPN ,
173+ new Netty4AlpnHandler (request , responseReference , errorReference , latch ));
174174 }
175175 }
176176 });
@@ -198,14 +198,10 @@ protected void initChannel(Channel channel) throws SSLException {
198198 // effectively be a no-op.
199199 if (addProgressAndTimeoutHandler ) {
200200 channel .pipeline ()
201- .addLast (Netty4HandlerNames . PROGRESS_AND_TIMEOUT , new Netty4ProgressAndTimeoutHandler (
202- progressReporter , writeTimeoutMillis , responseTimeoutMillis , readTimeoutMillis ));
201+ .addLast (PROGRESS_AND_TIMEOUT , new Netty4ProgressAndTimeoutHandler (progressReporter ,
202+ writeTimeoutMillis , responseTimeoutMillis , readTimeoutMillis ));
203203 }
204204
205- Netty4ResponseHandler responseHandler
206- = new Netty4ResponseHandler (request , responseReference , errorReference , latch );
207- channel .pipeline ().addLast (Netty4HandlerNames .RESPONSE , responseHandler );
208-
209205 Throwable earlyError = errorReference .get ();
210206 if (earlyError != null ) {
211207 // If an error occurred between the connect and the request being sent, don't proceed with sending
@@ -237,15 +233,14 @@ protected void initChannel(Channel channel) throws SSLException {
237233 } else {
238234 // If there isn't an SslHandler, we can send the request immediately.
239235 // Add the HTTP/1.1 codec, as we only support HTTP/2 when using SSL ALPN.
240- HttpClientCodec codec = createCodec ();
241- if (addProgressAndTimeoutHandler ) {
242- channel .pipeline ()
243- .addBefore (Netty4HandlerNames .PROGRESS_AND_TIMEOUT , Netty4HandlerNames .HTTP_1_1_CODEC , codec );
244- } else {
245- channel .pipeline ().addBefore (Netty4HandlerNames .RESPONSE , Netty4HandlerNames .HTTP_1_1_CODEC , codec );
246- }
236+ Netty4ResponseHandler responseHandler
237+ = new Netty4ResponseHandler (request , responseReference , errorReference , latch );
238+ channel .pipeline ().addLast (HTTP_RESPONSE , responseHandler );
239+
240+ String addBefore = addProgressAndTimeoutHandler ? PROGRESS_AND_TIMEOUT : HTTP_RESPONSE ;
241+ channel .pipeline ().addBefore (addBefore , Netty4HandlerNames .HTTP_CODEC , createCodec ());
247242
248- sendHttp11Request (request , channel , addProgressAndTimeoutHandler , errorReference )
243+ sendHttp11Request (request , channel , errorReference )
249244 .addListener ((ChannelFutureListener ) sendListener -> {
250245 if (!sendListener .isSuccess ()) {
251246 setOrSuppressError (errorReference , sendListener .cause ());
@@ -288,7 +283,7 @@ protected void initChannel(Channel channel) throws SSLException {
288283 // We're ignoring the response content.
289284 CountDownLatch drainLatch = new CountDownLatch (1 );
290285 channel .pipeline ().addLast (new Netty4EagerConsumeChannelHandler (drainLatch , ignored -> {
291- }));
286+ }, info . isHttp2 () ));
292287 channel .config ().setAutoRead (true );
293288 awaitLatch (drainLatch );
294289 } else if (bodyHandling == ResponseBodyHandling .STREAM ) {
@@ -306,7 +301,7 @@ protected void initChannel(Channel channel) throws SSLException {
306301 }
307302 }
308303
309- body = new Netty4ChannelBinaryData (info .getEagerContent (), channel , length );
304+ body = new Netty4ChannelBinaryData (info .getEagerContent (), channel , length , info . isHttp2 () );
310305 } else {
311306 // All cases otherwise assume BUFFER.
312307 CountDownLatch drainLatch = new CountDownLatch (1 );
@@ -316,7 +311,7 @@ protected void initChannel(Channel channel) throws SSLException {
316311 } catch (IOException ex ) {
317312 throw LOGGER .throwableAtError ().log (ex , CoreException ::from );
318313 }
319- }));
314+ }, info . isHttp2 () ));
320315 channel .config ().setAutoRead (true );
321316 awaitLatch (drainLatch );
322317
0 commit comments