1
1
/*
2
- * Copyright (c) 2013 Sonatype, Inc. All rights reserved.
2
+ * Copyright (c) 2013-2014 Sonatype, Inc. All rights reserved.
3
3
*
4
4
* This program is licensed to you under the Apache License Version 2.0,
5
5
* and you may not use this file except in compliance with the Apache License Version 2.0.
21
21
import org .asynchttpclient .ProxyServer ;
22
22
import org .asynchttpclient .Request ;
23
23
import org .asynchttpclient .uri .UriComponents ;
24
- import org .asynchttpclient .util .Base64 ;
25
24
import org .glassfish .grizzly .CompletionHandler ;
26
25
import org .glassfish .grizzly .Connection ;
27
26
import org .glassfish .grizzly .Grizzly ;
30
29
import org .glassfish .grizzly .connectionpool .EndpointKey ;
31
30
import org .glassfish .grizzly .filterchain .FilterChainBuilder ;
32
31
import org .glassfish .grizzly .impl .FutureImpl ;
33
- import org .glassfish .grizzly .ssl .SSLBaseFilter ;
34
- import org .glassfish .grizzly .ssl .SSLFilter ;
35
- import org .glassfish .grizzly .ssl .SSLUtils ;
36
32
import org .glassfish .grizzly .utils .Futures ;
37
33
import org .glassfish .grizzly .utils .IdleTimeoutFilter ;
38
34
import org .slf4j .Logger ;
39
35
import org .slf4j .LoggerFactory ;
40
36
41
37
import javax .net .ssl .HostnameVerifier ;
42
- import javax .net .ssl .SSLSession ;
43
38
44
39
import java .io .IOException ;
45
- import java .net .ConnectException ;
46
40
import java .net .InetAddress ;
47
41
import java .net .InetSocketAddress ;
48
42
import java .net .SocketAddress ;
52
46
import java .util .concurrent .CancellationException ;
53
47
import java .util .concurrent .ExecutionException ;
54
48
import java .util .concurrent .TimeoutException ;
49
+ import org .asynchttpclient .providers .grizzly .filters .SwitchingSSLFilter ;
55
50
56
51
public class ConnectionManager {
57
52
@@ -66,15 +61,13 @@ public class ConnectionManager {
66
61
private final FilterChainBuilder secureBuilder ;
67
62
private final FilterChainBuilder nonSecureBuilder ;
68
63
private final boolean asyncConnect ;
69
- private final SSLFilter sslFilter ;
70
64
71
65
// ------------------------------------------------------------ Constructors
72
66
73
67
ConnectionManager (final GrizzlyAsyncHttpProvider provider ,//
74
68
final ConnectionPool connectionPool ,//
75
69
final FilterChainBuilder secureBuilder ,//
76
- final FilterChainBuilder nonSecureBuilder ,//
77
- final SSLFilter sslFilter ) {
70
+ final FilterChainBuilder nonSecureBuilder ) {
78
71
79
72
this .provider = provider ;
80
73
final AsyncHttpClientConfig config = provider .getClientConfig ();
@@ -95,37 +88,45 @@ public class ConnectionManager {
95
88
AsyncHttpProviderConfig <?, ?> providerConfig = config .getAsyncHttpProviderConfig ();
96
89
asyncConnect = providerConfig instanceof GrizzlyAsyncHttpProviderConfig ? GrizzlyAsyncHttpProviderConfig .class .cast (providerConfig )
97
90
.isAsyncConnectMode () : false ;
98
- this .sslFilter = sslFilter ;
99
91
}
100
92
101
93
// ---------------------------------------------------------- Public Methods
102
94
103
95
public void doTrackedConnection (final Request request ,//
104
96
final GrizzlyResponseFuture requestFuture ,//
105
- final CompletionHandler <Connection > connectHandler ) throws IOException {
97
+ CompletionHandler <Connection > completionHandler ) throws IOException {
106
98
final EndpointKey <SocketAddress > key = getEndPointKey (request , requestFuture .getProxyServer ());
107
- CompletionHandler <Connection > handler = wrapHandler (request , getVerifier (), connectHandler , sslFilter );
99
+
100
+ final HostnameVerifier verifier = getVerifier ();
101
+ final UriComponents uri = request .getURI ();
102
+
103
+ if (Utils .isSecure (uri ) && verifier != null ) {
104
+ completionHandler =
105
+ SwitchingSSLFilter .wrapWithHostnameVerifierHandler (
106
+ completionHandler , verifier , uri .getHost ());
107
+ }
108
+
108
109
if (asyncConnect ) {
109
- connectionPool .take (key , handler );
110
+ connectionPool .take (key , completionHandler );
110
111
} else {
111
112
IOException ioe = null ;
112
113
GrizzlyFuture <Connection > future = connectionPool .take (key );
113
114
try {
114
115
// No explicit timeout when calling get() here as the Grizzly
115
116
// endpoint pool will time it out based on the connect timeout
116
117
// setting.
117
- handler .completed (future .get ());
118
+ completionHandler .completed (future .get ());
118
119
} catch (CancellationException e ) {
119
- handler .cancelled ();
120
+ completionHandler .cancelled ();
120
121
} catch (ExecutionException ee ) {
121
122
final Throwable cause = ee .getCause ();
122
123
if (cause instanceof ConnectionPool .MaxCapacityException ) {
123
124
ioe = (IOException ) cause ;
124
125
} else {
125
- handler .failed (ee .getCause ());
126
+ completionHandler .failed (ee .getCause ());
126
127
}
127
128
} catch (Exception ie ) {
128
- handler .failed (ie );
129
+ completionHandler .failed (ie );
129
130
}
130
131
if (ioe != null ) {
131
132
throw ioe ;
@@ -144,37 +145,6 @@ public Connection obtainConnection(final Request request, final GrizzlyResponseF
144
145
145
146
// --------------------------------------------------Package Private Methods
146
147
147
- static CompletionHandler <Connection > wrapHandler (final Request request , final HostnameVerifier verifier ,
148
- final CompletionHandler <Connection > delegate , final SSLFilter sslFilter ) {
149
- final UriComponents uri = request .getURI ();
150
- if (Utils .isSecure (uri ) && verifier != null ) {
151
- SSLBaseFilter .HandshakeListener handshakeListener = new SSLBaseFilter .HandshakeListener () {
152
- @ Override
153
- public void onStart (Connection connection ) {
154
- // do nothing
155
- LOGGER .debug ("SSL Handshake onStart: " );
156
- }
157
-
158
- @ Override
159
- public void onComplete (Connection connection ) {
160
- sslFilter .removeHandshakeListener (this );
161
-
162
- final String host = uri .getHost ();
163
- final SSLSession session = SSLUtils .getSSLEngine (connection ).getSession ();
164
- LOGGER .debug ("SSL Handshake onComplete: session = {}, id = {}, isValid = {}, host = {}" , session .toString (), Base64 .encode (session .getId ()), session .isValid (), host );
165
-
166
- if (!verifier .verify (host , session )) {
167
- connection .close (); // XXX what's the correct way to kill a connection?
168
- IOException e = new ConnectException ("Host name verification failed for host " + host );
169
- delegate .failed (e );
170
- }
171
- }
172
- };
173
- sslFilter .addHandshakeListener (handshakeListener );
174
- }
175
- return delegate ;
176
- }
177
-
178
148
static void markConnectionAsDoNotCache (final Connection c ) {
179
149
DO_NOT_CACHE .set (c , Boolean .TRUE );
180
150
}
0 commit comments