Upgrade Apache HttpClient to version 5.4 (#2364) · docker-java/docker-java@ece155b · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

Commit ece155b

Browse files
authored
Upgrade Apache HttpClient to version 5.4 (#2364)
* Upgraded Apache HttpClient to version 5.4.2; Fixed connection initialization for non-HTTP protocol * Remove usage of deprecated API
1 parent 6ae1eb4 commit ece155b

File tree

3 files changed

+58
-75
lines changed

3 files changed

+58
-75
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.2</version>
3333
</dependency>
3434

3535
<dependency>

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

Lines changed: 53 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,34 @@
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;
10+
import org.apache.hc.client5.http.config.ConnectionConfig;
811
import org.apache.hc.client5.http.config.RequestConfig;
12+
import org.apache.hc.client5.http.impl.DefaultSchemePortResolver;
913
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
10-
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;
21+
import org.apache.hc.core5.http.ClassicHttpResponse;
1722
import org.apache.hc.core5.http.ConnectionClosedException;
1823
import org.apache.hc.core5.http.ContentLengthStrategy;
1924
import org.apache.hc.core5.http.Header;
2025
import org.apache.hc.core5.http.HttpHeaders;
2126
import org.apache.hc.core5.http.HttpHost;
2227
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;
2528
import org.apache.hc.core5.http.impl.DefaultContentLengthStrategy;
26-
import org.apache.hc.core5.http.impl.io.EmptyInputStream;
2729
import org.apache.hc.core5.http.io.SocketConfig;
2830
import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
31+
import org.apache.hc.core5.http.io.entity.EmptyInputStream;
2932
import org.apache.hc.core5.http.io.entity.InputStreamEntity;
30-
import org.apache.hc.core5.http.protocol.BasicHttpContext;
3133
import org.apache.hc.core5.http.protocol.HttpContext;
34+
import org.apache.hc.core5.http.protocol.HttpCoreContext;
3235
import org.apache.hc.core5.net.URIAuthority;
3336
import org.apache.hc.core5.util.TimeValue;
3437
import org.apache.hc.core5.util.Timeout;
@@ -38,7 +41,6 @@
3841
import javax.net.ssl.SSLContext;
3942
import java.io.IOException;
4043
import java.io.InputStream;
41-
import java.net.InetSocketAddress;
4244
import java.net.Socket;
4345
import java.net.URI;
4446
import 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
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public ClassicHttpResponse execute(
4444

4545
InputStream hijackedInput = (InputStream) context.getAttribute(HIJACKED_INPUT_ATTRIBUTE);
4646
if (hijackedInput != null) {
47-
return executeHijacked(request, conn, context, hijackedInput);
47+
return executeHijacked(request, conn, (HttpCoreContext) context, hijackedInput);
4848
}
4949

5050
return super.execute(request, conn, informationCallback, context);
@@ -53,12 +53,12 @@ public ClassicHttpResponse execute(
5353
private ClassicHttpResponse executeHijacked(
5454
ClassicHttpRequest request,
5555
HttpClientConnection conn,
56-
HttpContext context,
56+
HttpCoreContext context,
5757
InputStream hijackedInput
5858
) throws HttpException, IOException {
5959
try {
60-
context.setAttribute(HttpCoreContext.SSL_SESSION, conn.getSSLSession());
61-
context.setAttribute(HttpCoreContext.CONNECTION_ENDPOINT, conn.getEndpointDetails());
60+
context.setSSLSession(conn.getSSLSession());
61+
context.setEndpointDetails(conn.getEndpointDetails());
6262
final ProtocolVersion transportVersion = request.getVersion();
6363
if (transportVersion != null) {
6464
context.setProtocolVersion(transportVersion);

0 commit comments

Comments
 (0)

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.