44import com .github .dockerjava .transport .NamedPipeSocket ;
55import com .github .dockerjava .transport .SSLConfig ;
66import com .github .dockerjava .transport .UnixSocket ;
7+
8+ import org .apache .hc .client5 .http .SystemDefaultDnsResolver ;
79import org .apache .hc .client5 .http .classic .methods .HttpUriRequestBase ;
10+ import org .apache .hc .client5 .http .config .ConnectionConfig ;
811import org .apache .hc .client5 .http .config .RequestConfig ;
12+ import org .apache .hc .client5 .http .impl .DefaultSchemePortResolver ;
913import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
10- import org .apache .hc .client5 .http .impl .classic .CloseableHttpResponse ;
1114import org .apache .hc .client5 .http .impl .classic .HttpClients ;
15+ import org .apache .hc .client5 .http .impl .io .DefaultHttpClientConnectionOperator ;
1216import org .apache .hc .client5 .http .impl .io .ManagedHttpClientConnectionFactory ;
1317import 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 ;
1722import org .apache .hc .core5 .http .ConnectionClosedException ;
1823import org .apache .hc .core5 .http .ContentLengthStrategy ;
1924import org .apache .hc .core5 .http .Header ;
2025import org .apache .hc .core5 .http .HttpHeaders ;
2126import org .apache .hc .core5 .http .HttpHost ;
2227import org .apache .hc .core5 .http .NameValuePair ;
23- import org .apache .hc .core5 .http .config .Registry ;
24- import org .apache .hc .core5 .http .config .RegistryBuilder ;
2528import org .apache .hc .core5 .http .impl .DefaultContentLengthStrategy ;
26- import org .apache .hc .core5 .http .impl .io .EmptyInputStream ;
2729import org .apache .hc .core5 .http .io .SocketConfig ;
2830import org .apache .hc .core5 .http .io .entity .ByteArrayEntity ;
31+ import org .apache .hc .core5 .http .io .entity .EmptyInputStream ;
2932import org .apache .hc .core5 .http .io .entity .InputStreamEntity ;
30- import org .apache .hc .core5 .http .protocol .BasicHttpContext ;
3133import org .apache .hc .core5 .http .protocol .HttpContext ;
34+ import org .apache .hc .core5 .http .protocol .HttpCoreContext ;
3235import org .apache .hc .core5 .net .URIAuthority ;
3336import org .apache .hc .core5 .util .TimeValue ;
3437import org .apache .hc .core5 .util .Timeout ;
3841import javax .net .ssl .SSLContext ;
3942import java .io .IOException ;
4043import java .io .InputStream ;
41- import java .net .InetSocketAddress ;
4244import java .net .Socket ;
4345import java .net .URI ;
4446import java .time .Duration ;
@@ -61,7 +63,13 @@ protected ApacheDockerHttpClientImpl(
6163 Duration connectionTimeout ,
6264 Duration responseTimeout
6365 ) {
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 );
6573
6674 switch (dockerHost .getScheme ()) {
6775 case "unix" :
@@ -75,7 +83,7 @@ protected ApacheDockerHttpClientImpl(
7583 ? rawPath .substring (0 , rawPath .length () - 1 )
7684 : rawPath ;
7785 host = new HttpHost (
78- socketFactoryRegistry . lookup ( "https" ) != null ? "https" : "http" ,
86+ sslContext != null ? "https" : "http" ,
7987 dockerHost .getHost (),
8088 dockerHost .getPort ()
8189 );
@@ -85,7 +93,10 @@ protected ApacheDockerHttpClientImpl(
8593 }
8694
8795 PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager (
88- socketFactoryRegistry ,
96+ connectionOperator ,
97+ null ,
98+ null ,
99+ null ,
89100 new ManagedHttpClientConnectionFactory (
90101 null ,
91102 null ,
@@ -109,77 +120,49 @@ protected ApacheDockerHttpClientImpl(
109120 .setSoTimeout (Timeout .ZERO_MILLISECONDS )
110121 .build ()
111122 );
112- connectionManager .setValidateAfterInactivity (TimeValue .NEG_ONE_SECOND );
113123 connectionManager .setMaxTotal (maxConnections );
114124 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 ());
122129
123130 httpClient = HttpClients .custom ()
124131 .setRequestExecutor (new HijackingHttpRequestExecutor (null ))
125132 .setConnectionManager (connectionManager )
126- .setDefaultRequestConfig (defaultRequest .build ())
133+ .setDefaultRequestConfig (RequestConfig .custom ()
134+ .setResponseTimeout (responseTimeout != null ? Timeout .of (responseTimeout .toNanos (), TimeUnit .NANOSECONDS ) : null )
135+ .build ())
127136 .disableConnectionState ()
128137 .build ();
129138 }
130139
131- private Registry < ConnectionSocketFactory > createConnectionSocketFactoryRegistry (
132- SSLConfig sslConfig ,
133- URI dockerHost
140+ private HttpClientConnectionOperator createConnectionOperator (
141+ URI dockerHost ,
142+ SSLContext sslContext
134143 ) {
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 );
155156 }
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 );
178161 }
179162
180163 @ Override
181164 public Response execute (Request request ) {
182- HttpContext context = new BasicHttpContext ();
165+ HttpContext context = new HttpCoreContext ();
183166 HttpUriRequestBase httpUriRequest = new HttpUriRequestBase (request .method (), URI .create (pathPrefix + request .path ()));
184167 httpUriRequest .setScheme (host .getSchemeName ());
185168 httpUriRequest .setAuthority (new URIAuthority (host .getHostName (), host .getPort ()));
@@ -203,7 +186,7 @@ public Response execute(Request request) {
203186 }
204187
205188 try {
206- CloseableHttpResponse response = httpClient .execute (host , httpUriRequest , context );
189+ ClassicHttpResponse response = httpClient .executeOpen (host , httpUriRequest , context );
207190
208191 return new ApacheResponse (httpUriRequest , response );
209192 } catch (IOException e ) {
@@ -222,9 +205,9 @@ static class ApacheResponse implements Response {
222205
223206 private final HttpUriRequestBase request ;
224207
225- private final CloseableHttpResponse response ;
208+ private final ClassicHttpResponse response ;
226209
227- ApacheResponse (HttpUriRequestBase httpUriRequest , CloseableHttpResponse response ) {
210+ ApacheResponse (HttpUriRequestBase httpUriRequest , ClassicHttpResponse response ) {
228211 this .request = httpUriRequest ;
229212 this .response = response ;
230213 }
0 commit comments