1
1
package org .asynchttpclient .providers .netty4 ;
2
2
3
- import static org .asynchttpclient .providers .netty4 .util .HttpUtil .HTTP ;
4
- import static org .asynchttpclient .providers .netty4 .util .HttpUtil .WEBSOCKET ;
5
- import static org .asynchttpclient .providers .netty4 .util .HttpUtil .isSecure ;
3
+ import static org .asynchttpclient .providers .netty4 .util .HttpUtil .*;
6
4
import io .netty .bootstrap .Bootstrap ;
7
5
import io .netty .channel .Channel ;
8
6
import io .netty .channel .ChannelHandlerContext ;
45
43
import org .asynchttpclient .ConnectionsPool ;
46
44
import org .asynchttpclient .providers .netty4 .future .NettyResponseFuture ;
47
45
import org .asynchttpclient .providers .netty4 .pool .NettyConnectionsPool ;
46
+ import org .asynchttpclient .providers .netty4 .pool .NonConnectionsPool ;
48
47
import org .asynchttpclient .providers .netty4 .util .CleanupChannelGroup ;
49
48
import org .asynchttpclient .util .SslUtils ;
50
49
import org .slf4j .Logger ;
@@ -91,8 +90,6 @@ public boolean remove(Object o) {
91
90
}
92
91
};
93
92
94
- private NettyChannelHandler httpProcessor ;
95
-
96
93
public Channels (final AsyncHttpClientConfig config , NettyAsyncHttpProviderConfig asyncHttpProviderConfig ) {
97
94
98
95
this .config = config ;
@@ -177,21 +174,23 @@ public Channels(final AsyncHttpClientConfig config, NettyAsyncHttpProviderConfig
177
174
webSocketBootstrap .option (ChannelOption .CONNECT_TIMEOUT_MILLIS , config .getConnectionTimeoutInMs ());
178
175
secureBootstrap .option (ChannelOption .CONNECT_TIMEOUT_MILLIS , config .getConnectionTimeoutInMs ());
179
176
secureWebSocketBootstrap .option (ChannelOption .CONNECT_TIMEOUT_MILLIS , config .getConnectionTimeoutInMs ());
177
+ }
180
178
181
- // FIXME What was the meaning of this and what is it still a matter with
182
- // Netty4
183
- // DefaultChannelFuture.setUseDeadLockChecker(false);
179
+ private SSLEngine createSSLEngine () throws IOException , GeneralSecurityException {
180
+ SSLEngine sslEngine = config .getSSLEngineFactory ().newSSLEngine ();
181
+ if (sslEngine == null ) {
182
+ sslEngine = SslUtils .getSSLEngine ();
183
+ }
184
+ return sslEngine ;
184
185
}
185
186
186
187
public void configure (final NettyChannelHandler httpProcessor ) {
187
- this .httpProcessor = httpProcessor ;
188
-
189
- ChannelInitializer <Channel > httpChannelInitializer = new ChannelInitializer <Channel >() {
190
188
189
+ plainBootstrap .handler (new ChannelInitializer <Channel >() {
191
190
@ Override
192
191
protected void initChannel (Channel ch ) throws Exception {
193
192
ChannelPipeline pipeline = ch .pipeline ()//
194
- .addLast (HTTP_HANDLER , newHttpClientCodec ());
193
+ .addLast (HTTP_HANDLER , newHttpClientCodec ());
195
194
196
195
if (config .getRequestCompressionLevel () > 0 ) {
197
196
pipeline .addLast (DEFLATER_HANDLER , new HttpContentCompressor (config .getRequestCompressionLevel ()));
@@ -201,75 +200,41 @@ protected void initChannel(Channel ch) throws Exception {
201
200
pipeline .addLast (INFLATER_HANDLER , new HttpContentDecompressor ());
202
201
}
203
202
pipeline .addLast (CHUNKED_WRITER_HANDLER , new ChunkedWriteHandler ())//
204
- .addLast (AHC_HANDLER , httpProcessor );
203
+ .addLast (AHC_HANDLER , httpProcessor );
205
204
206
205
if (asyncHttpProviderConfig .getHttpAdditionalChannelInitializer () != null ) {
207
206
asyncHttpProviderConfig .getHttpAdditionalChannelInitializer ().initChannel (ch );
208
207
}
209
208
}
210
- };
209
+ }) ;
211
210
212
- ChannelInitializer < Channel > webSocketChannelInitializer = new ChannelInitializer <Channel >() {
211
+ webSocketBootstrap . handler ( new ChannelInitializer <Channel >() {
213
212
@ Override
214
213
protected void initChannel (Channel ch ) throws Exception {
215
214
ch .pipeline ()//
216
- .addLast (HTTP_DECODER_HANDLER , new HttpResponseDecoder ())//
217
- .addLast (HTTP_ENCODER_HANDLER , new HttpRequestEncoder ())//
218
- .addLast (AHC_HANDLER , httpProcessor );
215
+ .addLast (HTTP_DECODER_HANDLER , new HttpResponseDecoder ())//
216
+ .addLast (HTTP_ENCODER_HANDLER , new HttpRequestEncoder ())//
217
+ .addLast (AHC_HANDLER , httpProcessor );
219
218
220
219
if (asyncHttpProviderConfig .getWsAdditionalChannelInitializer () != null ) {
221
220
asyncHttpProviderConfig .getWsAdditionalChannelInitializer ().initChannel (ch );
222
221
}
223
222
}
224
- };
225
-
226
- plainBootstrap .handler (httpChannelInitializer );
227
- webSocketBootstrap .handler (webSocketChannelInitializer );
228
- }
229
-
230
- public Bootstrap getBootstrap (String url , boolean useSSl ) {
231
- Bootstrap bootstrap = url .startsWith (WEBSOCKET ) ? (useSSl ? secureWebSocketBootstrap : webSocketBootstrap ) : (useSSl ? secureBootstrap : plainBootstrap );
232
-
233
- return bootstrap ;
234
- }
235
-
236
- public void close () {
237
- connectionsPool .destroy ();
238
- for (Channel channel : openChannels ) {
239
- Object attribute = getDefaultAttribute (channel );
240
- if (attribute instanceof NettyResponseFuture <?>) {
241
- NettyResponseFuture <?> future = (NettyResponseFuture <?>) attribute ;
242
- future .setReaperFuture (null );
243
- }
244
- }
245
- openChannels .close ();
246
- if (allowReleaseEventLoopGroup ) {
247
- eventLoopGroup .shutdownGracefully ();
248
- }
249
- }
250
-
251
- void constructSSLPipeline (final NettyResponseFuture <?> future ) {
223
+ });
252
224
253
225
secureBootstrap .handler (new ChannelInitializer <Channel >() {
254
226
255
227
@ Override
256
228
protected void initChannel (Channel ch ) throws Exception {
257
- ChannelPipeline pipeline = ch .pipeline ();
258
-
259
- try {
260
- pipeline .addLast (SSL_HANDLER , new SslHandler (createSSLEngine ()));
261
- } catch (Throwable ex ) {
262
- LOGGER .error ("Channel {} could not add SslHandler {}" , ch , ex );
263
- abort (future , ex );
264
- }
265
-
266
- pipeline .addLast (HTTP_HANDLER , newHttpClientCodec ());
229
+ ChannelPipeline pipeline = ch .pipeline ()//
230
+ .addLast (SSL_HANDLER , new SslHandler (createSSLEngine ()))//
231
+ .addLast (HTTP_HANDLER , newHttpClientCodec ());
267
232
268
233
if (config .isCompressionEnabled ()) {
269
234
pipeline .addLast (INFLATER_HANDLER , new HttpContentDecompressor ());
270
235
}
271
236
pipeline .addLast (CHUNKED_WRITER_HANDLER , new ChunkedWriteHandler ())//
272
- .addLast (AHC_HANDLER , httpProcessor );
237
+ .addLast (AHC_HANDLER , httpProcessor );
273
238
274
239
if (asyncHttpProviderConfig .getHttpsAdditionalChannelInitializer () != null ) {
275
240
asyncHttpProviderConfig .getHttpsAdditionalChannelInitializer ().initChannel (ch );
@@ -281,18 +246,11 @@ protected void initChannel(Channel ch) throws Exception {
281
246
282
247
@ Override
283
248
protected void initChannel (Channel ch ) throws Exception {
284
- ChannelPipeline pipeline = ch .pipeline ();
285
-
286
- try {
287
- pipeline .addLast (SSL_HANDLER , new SslHandler (createSSLEngine ()));
288
- } catch (Throwable ex ) {
289
- LOGGER .error ("Channel {} could not add SslHandler {}" , ch , ex );
290
- abort (future , ex );
291
- }
292
-
293
- pipeline .addLast (HTTP_DECODER_HANDLER , new HttpResponseDecoder ())//
294
- .addLast (HTTP_ENCODER_HANDLER , new HttpRequestEncoder ())//
295
- .addLast (AHC_HANDLER , httpProcessor );
249
+ ch .pipeline ()//
250
+ .addLast (SSL_HANDLER , new SslHandler (createSSLEngine ()))//
251
+ .addLast (HTTP_DECODER_HANDLER , new HttpResponseDecoder ())//
252
+ .addLast (HTTP_ENCODER_HANDLER , new HttpRequestEncoder ())//
253
+ .addLast (AHC_HANDLER , httpProcessor );
296
254
297
255
if (asyncHttpProviderConfig .getWssAdditionalChannelInitializer () != null ) {
298
256
asyncHttpProviderConfig .getWssAdditionalChannelInitializer ().initChannel (ch );
@@ -301,15 +259,26 @@ protected void initChannel(Channel ch) throws Exception {
301
259
});
302
260
}
303
261
304
- private SSLEngine createSSLEngine () throws IOException , GeneralSecurityException {
305
- SSLEngine sslEngine = config .getSSLEngineFactory ().newSSLEngine ();
306
- if (sslEngine == null ) {
307
- sslEngine = SslUtils .getSSLEngine ();
262
+ public Bootstrap getBootstrap (String url , boolean useSSl ) {
263
+ return url .startsWith (WEBSOCKET ) ? (useSSl ? secureWebSocketBootstrap : webSocketBootstrap ) : (useSSl ? secureBootstrap : plainBootstrap );
264
+ }
265
+
266
+ public void close () {
267
+ connectionsPool .destroy ();
268
+ for (Channel channel : openChannels ) {
269
+ Object attribute = getDefaultAttribute (channel );
270
+ if (attribute instanceof NettyResponseFuture <?>) {
271
+ NettyResponseFuture <?> future = (NettyResponseFuture <?>) attribute ;
272
+ future .setReaperFuture (null );
273
+ }
274
+ }
275
+ openChannels .close ();
276
+ if (allowReleaseEventLoopGroup ) {
277
+ eventLoopGroup .shutdownGracefully ();
308
278
}
309
- return sslEngine ;
310
279
}
311
280
312
- // FIXME what for?
281
+ // some servers can use the same port for HTTP and HTTPS
313
282
public Channel verifyChannelPipeline (Channel channel , String scheme ) throws IOException , GeneralSecurityException {
314
283
315
284
if (channel .pipeline ().get (SSL_HANDLER ) != null && HTTP .equalsIgnoreCase (scheme )) {
@@ -509,26 +478,4 @@ public static void setDefaultAttribute(Channel channel, Object o) {
509
478
public static void setDefaultAttribute (ChannelHandlerContext ctx , Object o ) {
510
479
ctx .attr (DEFAULT_ATTRIBUTE ).set (o );
511
480
}
512
-
513
- private static class NonConnectionsPool implements ConnectionsPool <String , Channel > {
514
-
515
- public boolean offer (String uri , Channel connection ) {
516
- return false ;
517
- }
518
-
519
- public Channel poll (String uri ) {
520
- return null ;
521
- }
522
-
523
- public boolean removeAll (Channel connection ) {
524
- return false ;
525
- }
526
-
527
- public boolean canCacheConnection () {
528
- return true ;
529
- }
530
-
531
- public void destroy () {
532
- }
533
- }
534
481
}
0 commit comments