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 ;
810import org .apache .hc .client5 .http .config .RequestConfig ;
11+ import org .apache .hc .client5 .http .impl .DefaultSchemePortResolver ;
912import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
1013import 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 ;
1721import org .apache .hc .core5 .http .ConnectionClosedException ;
1822import org .apache .hc .core5 .http .ContentLengthStrategy ;
1923import org .apache .hc .core5 .http .Header ;
2024import org .apache .hc .core5 .http .HttpHeaders ;
2125import org .apache .hc .core5 .http .HttpHost ;
2226import org .apache .hc .core5 .http .NameValuePair ;
23- import org .apache .hc .core5 .http .config .Registry ;
24- import org .apache .hc .core5 .http .config .RegistryBuilder ;
2527import org .apache .hc .core5 .http .impl .DefaultContentLengthStrategy ;
2628import org .apache .hc .core5 .http .impl .io .EmptyInputStream ;
2729import org .apache .hc .core5 .http .io .SocketConfig ;
3840import javax .net .ssl .SSLContext ;
3941import java .io .IOException ;
4042import java .io .InputStream ;
41- import java .net .InetSocketAddress ;
4243import java .net .Socket ;
4344import java .net .URI ;
4445import java .time .Duration ;
@@ -61,7 +62,13 @@ protected ApacheDockerHttpClientImpl(
6162 Duration connectionTimeout ,
6263 Duration responseTimeout
6364 ) {
64- Registry <ConnectionSocketFactory > socketFactoryRegistry = createConnectionSocketFactoryRegistry (sslConfig , dockerHost );
65+ SSLContext sslContext ;
66+ try {
67+ sslContext = sslConfig != null ? sslConfig .getSSLContext () : null ;
68+ } catch (Exception e ) {
69+ throw new RuntimeException (e );
70+ }
71+ HttpClientConnectionOperator connectionOperator = createConnectionOperator (dockerHost , sslContext );
6572
6673 switch (dockerHost .getScheme ()) {
6774 case "unix" :
@@ -75,7 +82,7 @@ protected ApacheDockerHttpClientImpl(
7582 ? rawPath .substring (0 , rawPath .length () - 1 )
7683 : rawPath ;
7784 host = new HttpHost (
78- socketFactoryRegistry . lookup ( "https" ) != null ? "https" : "http" ,
85+ sslContext != null ? "https" : "http" ,
7986 dockerHost .getHost (),
8087 dockerHost .getPort ()
8188 );
@@ -85,7 +92,10 @@ protected ApacheDockerHttpClientImpl(
8592 }
8693
8794 PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager (
88- socketFactoryRegistry ,
95+ connectionOperator ,
96+ null ,
97+ null ,
98+ null ,
8999 new ManagedHttpClientConnectionFactory (
90100 null ,
91101 null ,
@@ -128,53 +138,27 @@ protected ApacheDockerHttpClientImpl(
128138 .build ();
129139 }
130140
131- private Registry < ConnectionSocketFactory > createConnectionSocketFactoryRegistry (
132- SSLConfig sslConfig ,
133- URI dockerHost
141+ private HttpClientConnectionOperator createConnectionOperator (
142+ URI dockerHost ,
143+ SSLContext sslContext
134144 ) {
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 ());
155- }
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 ());
145+ String dockerHostScheme = dockerHost .getScheme ();
146+ String dockerHostPath = dockerHost .getPath ();
147+ TlsSocketStrategy tlsSocketStrategy = sslContext != null ?
148+ new DefaultClientTlsStrategy (sslContext ) : DefaultClientTlsStrategy .createSystemDefault ();
149+ return new DefaultHttpClientConnectionOperator (
150+ socksProxy -> {
151+ if ("unix" .equalsIgnoreCase (dockerHostScheme )) {
152+ return UnixSocket .get (dockerHostPath );
153+ } else if ("npipe" .equalsIgnoreCase (dockerHostScheme )) {
154+ return new NamedPipeSocket (dockerHostPath );
155+ } else {
156+ return socksProxy == null ? new Socket () : new Socket (socksProxy );
168157 }
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 ();
158+ },
159+ DefaultSchemePortResolver .INSTANCE ,
160+ SystemDefaultDnsResolver .INSTANCE ,
161+ name -> "https" .equalsIgnoreCase (name ) ? tlsSocketStrategy : null );
178162 }
179163
180164 @ Override
0 commit comments