4
4
import com .github .dockerjava .transport .NamedPipeSocket ;
5
5
import com .github .dockerjava .transport .SSLConfig ;
6
6
import com .github .dockerjava .transport .UnixSocket ;
7
+
8
+ import org .apache .hc .client5 .http .SystemDefaultDnsResolver ;
7
9
import org .apache .hc .client5 .http .classic .methods .HttpUriRequestBase ;
10
+ import org .apache .hc .client5 .http .config .ConnectionConfig ;
8
11
import org .apache .hc .client5 .http .config .RequestConfig ;
12
+ import org .apache .hc .client5 .http .impl .DefaultSchemePortResolver ;
9
13
import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
10
- import org .apache .hc .client5 .http .impl .classic .CloseableHttpResponse ;
11
14
import org .apache .hc .client5 .http .impl .classic .HttpClients ;
15
+ import org .apache .hc .client5 .http .impl .io .DefaultHttpClientConnectionOperator ;
12
16
import org .apache .hc .client5 .http .impl .io .ManagedHttpClientConnectionFactory ;
13
17
import org .apache .hc .client5 .http .impl .io .PoolingHttpClientConnectionManager ;
14
- import org .apache .hc .client5 .http .socket .ConnectionSocketFactory ;
15
- import org .apache .hc .client5 .http .socket .PlainConnectionSocketFactory ;
16
- import org .apache .hc .client5 .http .ssl .SSLConnectionSocketFactory ;
18
+ import org .apache .hc .client5 .http .io .HttpClientConnectionOperator ;
19
+ import org .apache .hc .client5 .http .ssl .DefaultClientTlsStrategy ;
20
+ import org .apache .hc .client5 .http .ssl .TlsSocketStrategy ;
21
+ import org .apache .hc .core5 .http .ClassicHttpResponse ;
17
22
import org .apache .hc .core5 .http .ConnectionClosedException ;
18
23
import org .apache .hc .core5 .http .ContentLengthStrategy ;
19
24
import org .apache .hc .core5 .http .Header ;
20
25
import org .apache .hc .core5 .http .HttpHeaders ;
21
26
import org .apache .hc .core5 .http .HttpHost ;
22
27
import org .apache .hc .core5 .http .NameValuePair ;
23
- import org .apache .hc .core5 .http .config .Registry ;
24
- import org .apache .hc .core5 .http .config .RegistryBuilder ;
25
28
import org .apache .hc .core5 .http .impl .DefaultContentLengthStrategy ;
26
- import org .apache .hc .core5 .http .impl .io .EmptyInputStream ;
27
29
import org .apache .hc .core5 .http .io .SocketConfig ;
28
30
import org .apache .hc .core5 .http .io .entity .ByteArrayEntity ;
31
+ import org .apache .hc .core5 .http .io .entity .EmptyInputStream ;
29
32
import org .apache .hc .core5 .http .io .entity .InputStreamEntity ;
30
- import org .apache .hc .core5 .http .protocol .BasicHttpContext ;
31
33
import org .apache .hc .core5 .http .protocol .HttpContext ;
34
+ import org .apache .hc .core5 .http .protocol .HttpCoreContext ;
32
35
import org .apache .hc .core5 .net .URIAuthority ;
33
36
import org .apache .hc .core5 .util .TimeValue ;
34
37
import org .apache .hc .core5 .util .Timeout ;
38
41
import javax .net .ssl .SSLContext ;
39
42
import java .io .IOException ;
40
43
import java .io .InputStream ;
41
- import java .net .InetSocketAddress ;
42
44
import java .net .Socket ;
43
45
import java .net .URI ;
44
46
import java .time .Duration ;
@@ -61,7 +63,13 @@ protected ApacheDockerHttpClientImpl(
61
63
Duration connectionTimeout ,
62
64
Duration responseTimeout
63
65
) {
64
- Registry <ConnectionSocketFactory > socketFactoryRegistry = createConnectionSocketFactoryRegistry (sslConfig , dockerHost );
66
+ SSLContext sslContext ;
67
+ try {
68
+ sslContext = sslConfig != null ? sslConfig .getSSLContext () : null ;
69
+ } catch (Exception e ) {
70
+ throw new RuntimeException (e );
71
+ }
72
+ HttpClientConnectionOperator connectionOperator = createConnectionOperator (dockerHost , sslContext );
65
73
66
74
switch (dockerHost .getScheme ()) {
67
75
case "unix" :
@@ -75,7 +83,7 @@ protected ApacheDockerHttpClientImpl(
75
83
? rawPath .substring (0 , rawPath .length () - 1 )
76
84
: rawPath ;
77
85
host = new HttpHost (
78
- socketFactoryRegistry . lookup ( "https" ) != null ? "https" : "http" ,
86
+ sslContext != null ? "https" : "http" ,
79
87
dockerHost .getHost (),
80
88
dockerHost .getPort ()
81
89
);
@@ -85,7 +93,10 @@ protected ApacheDockerHttpClientImpl(
85
93
}
86
94
87
95
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager (
88
- socketFactoryRegistry ,
96
+ connectionOperator ,
97
+ null ,
98
+ null ,
99
+ null ,
89
100
new ManagedHttpClientConnectionFactory (
90
101
null ,
91
102
null ,
@@ -109,77 +120,49 @@ protected ApacheDockerHttpClientImpl(
109
120
.setSoTimeout (Timeout .ZERO_MILLISECONDS )
110
121
.build ()
111
122
);
112
- connectionManager .setValidateAfterInactivity (TimeValue .NEG_ONE_SECOND );
113
123
connectionManager .setMaxTotal (maxConnections );
114
124
connectionManager .setDefaultMaxPerRoute (maxConnections );
115
- RequestConfig .Builder defaultRequest = RequestConfig .custom ();
116
- if (connectionTimeout != null ) {
117
- defaultRequest .setConnectTimeout (connectionTimeout .toNanos (), TimeUnit .NANOSECONDS );
118
- }
119
- if (responseTimeout != null ) {
120
- defaultRequest .setResponseTimeout (responseTimeout .toNanos (), TimeUnit .NANOSECONDS );
121
- }
125
+ connectionManager .setDefaultConnectionConfig (ConnectionConfig .custom ()
126
+ .setValidateAfterInactivity (TimeValue .NEG_ONE_SECOND )
127
+ .setConnectTimeout (connectionTimeout != null ? Timeout .of (connectionTimeout .toNanos (), TimeUnit .NANOSECONDS ) : null )
128
+ .build ());
122
129
123
130
httpClient = HttpClients .custom ()
124
131
.setRequestExecutor (new HijackingHttpRequestExecutor (null ))
125
132
.setConnectionManager (connectionManager )
126
- .setDefaultRequestConfig (defaultRequest .build ())
133
+ .setDefaultRequestConfig (RequestConfig .custom ()
134
+ .setResponseTimeout (responseTimeout != null ? Timeout .of (responseTimeout .toNanos (), TimeUnit .NANOSECONDS ) : null )
135
+ .build ())
127
136
.disableConnectionState ()
128
137
.build ();
129
138
}
130
139
131
- private Registry < ConnectionSocketFactory > createConnectionSocketFactoryRegistry (
132
- SSLConfig sslConfig ,
133
- URI dockerHost
140
+ private HttpClientConnectionOperator createConnectionOperator (
141
+ URI dockerHost ,
142
+ SSLContext sslContext
134
143
) {
135
- RegistryBuilder <ConnectionSocketFactory > socketFactoryRegistryBuilder = RegistryBuilder .create ();
136
-
137
- if (sslConfig != null ) {
138
- try {
139
- SSLContext sslContext = sslConfig .getSSLContext ();
140
- if (sslContext != null ) {
141
- socketFactoryRegistryBuilder .register ("https" , new SSLConnectionSocketFactory (sslContext ));
142
- }
143
- } catch (Exception e ) {
144
- throw new RuntimeException (e );
145
- }
146
- }
147
-
148
- return socketFactoryRegistryBuilder
149
- .register ("tcp" , PlainConnectionSocketFactory .INSTANCE )
150
- .register ("http" , PlainConnectionSocketFactory .INSTANCE )
151
- .register ("unix" , new ConnectionSocketFactory () {
152
- @ Override
153
- public Socket createSocket (HttpContext context ) throws IOException {
154
- return UnixSocket .get (dockerHost .getPath ());
144
+ String dockerHostScheme = dockerHost .getScheme ();
145
+ String dockerHostPath = dockerHost .getPath ();
146
+ TlsSocketStrategy tlsSocketStrategy = sslContext != null ?
147
+ new DefaultClientTlsStrategy (sslContext ) : DefaultClientTlsStrategy .createSystemDefault ();
148
+ return new DefaultHttpClientConnectionOperator (
149
+ socksProxy -> {
150
+ if ("unix" .equalsIgnoreCase (dockerHostScheme )) {
151
+ return UnixSocket .get (dockerHostPath );
152
+ } else if ("npipe" .equalsIgnoreCase (dockerHostScheme )) {
153
+ return new NamedPipeSocket (dockerHostPath );
154
+ } else {
155
+ return socksProxy == null ? new Socket () : new Socket (socksProxy );
155
156
}
156
-
157
- @ Override
158
- public Socket connectSocket (TimeValue timeValue , Socket socket , HttpHost httpHost , InetSocketAddress inetSocketAddress ,
159
- InetSocketAddress inetSocketAddress1 , HttpContext httpContext ) throws IOException {
160
- return PlainConnectionSocketFactory .INSTANCE .connectSocket (timeValue , socket , httpHost , inetSocketAddress ,
161
- inetSocketAddress1 , httpContext );
162
- }
163
- })
164
- .register ("npipe" , new ConnectionSocketFactory () {
165
- @ Override
166
- public Socket createSocket (HttpContext context ) {
167
- return new NamedPipeSocket (dockerHost .getPath ());
168
- }
169
-
170
- @ Override
171
- public Socket connectSocket (TimeValue timeValue , Socket socket , HttpHost httpHost , InetSocketAddress inetSocketAddress ,
172
- InetSocketAddress inetSocketAddress1 , HttpContext httpContext ) throws IOException {
173
- return PlainConnectionSocketFactory .INSTANCE .connectSocket (timeValue , socket , httpHost , inetSocketAddress ,
174
- inetSocketAddress1 , httpContext );
175
- }
176
- })
177
- .build ();
157
+ },
158
+ DefaultSchemePortResolver .INSTANCE ,
159
+ SystemDefaultDnsResolver .INSTANCE ,
160
+ name -> "https" .equalsIgnoreCase (name ) ? tlsSocketStrategy : null );
178
161
}
179
162
180
163
@ Override
181
164
public Response execute (Request request ) {
182
- HttpContext context = new BasicHttpContext ();
165
+ HttpContext context = new HttpCoreContext ();
183
166
HttpUriRequestBase httpUriRequest = new HttpUriRequestBase (request .method (), URI .create (pathPrefix + request .path ()));
184
167
httpUriRequest .setScheme (host .getSchemeName ());
185
168
httpUriRequest .setAuthority (new URIAuthority (host .getHostName (), host .getPort ()));
@@ -203,7 +186,7 @@ public Response execute(Request request) {
203
186
}
204
187
205
188
try {
206
- CloseableHttpResponse response = httpClient .execute (host , httpUriRequest , context );
189
+ ClassicHttpResponse response = httpClient .executeOpen (host , httpUriRequest , context );
207
190
208
191
return new ApacheResponse (httpUriRequest , response );
209
192
} catch (IOException e ) {
@@ -222,9 +205,9 @@ static class ApacheResponse implements Response {
222
205
223
206
private final HttpUriRequestBase request ;
224
207
225
- private final CloseableHttpResponse response ;
208
+ private final ClassicHttpResponse response ;
226
209
227
- ApacheResponse (HttpUriRequestBase httpUriRequest , CloseableHttpResponse response ) {
210
+ ApacheResponse (HttpUriRequestBase httpUriRequest , ClassicHttpResponse response ) {
228
211
this .request = httpUriRequest ;
229
212
this .response = response ;
230
213
}
0 commit comments