8000 Upgraded Apache HttpClient to version 5.4.1; Fixed connection initial… · docker-java/docker-java@b1aa544 · GitHub
[go: up one dir, main page]

Skip to content

Commit b1aa544

Browse files
committed
Upgraded Apache HttpClient to version 5.4.1; Fixed connection initialization for non-HTTP protocol schemes
1 parent a1393bf commit b1aa544

File tree

2 files changed

+39
-55
lines changed

2 files changed

+39
-55
lines changed

docker-java-transport-httpclient5/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<dependency>
3030
<groupId>org.apache.httpcomponents.client5</groupId>
3131
<artifactId>httpclient5</artifactId>
32-
<version>5.0.3</version>
32+
<version>5.4.1</version>
3333
</dependency>
3434

3535
<dependency>

docker-java-transport-httpclient5/src/main/java/com/github/dockerjava/httpclient5/ApacheDockerHttpClientImpl.java

Lines changed: 38 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,26 @@
44
import com.github.dockerjava.transport.NamedPipeSocket;
55
import com.github.dockerjava.transport.SSLConfig;
66
import com.github.dockerjava.transport.UnixSocket;
7+
8+
import org.apache.hc.client5.http.SystemDefaultDnsResolver;
79
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
810
import org.apache.hc.client5.http.config.RequestConfig;
11+
import org.apache.hc.client5.http.impl.DefaultSchemePortResolver;
912
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
1013
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
1114
import org.apache.hc.client5.http.impl.classic.HttpClients;
15+
import org.apache.hc.client5.http.impl.io.DefaultHttpClientConnectionOperator;
1216
import org.apache.hc.client5.http.impl.io.ManagedHttpClientConnectionFactory;
1317
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;
1721
import org.apache.hc.core5.http.ConnectionClosedException;
1822
import org.apache.hc.core5.http.ContentLengthStrategy;
1923
import org.apache.hc.core5.http.Header;
2024
import org.apache.hc.core5.http.HttpHeaders;
2125
import org.apache.hc.core5.http.HttpHost;
2226
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;
2527
import org.apache.hc.core5.http.impl.DefaultContentLengthStrategy;
2628
import org.apache.hc.core5.http.impl.io.EmptyInputStream;
2729
import org.apache.hc.core5.http.io.SocketConfig;
@@ -38,7 +40,6 @@
3840
import javax.net.ssl.SSLContext;
3941
import java.io.IOException;
4042
import java.io.InputStream;
41-
import java.net.InetSocketAddress;
4243
import java.net.Socket;
4344
import java.net.URI;
4445
import 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

Comments
 (0)
0