Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Upgrade Apache HttpClient to version 5.4 #2364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docker-java-transport-httpclient5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.0.3</version>
<version>5.4.2</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,34 @@
import com.github.dockerjava.transport.NamedPipeSocket;
import com.github.dockerjava.transport.SSLConfig;
import com.github.dockerjava.transport.UnixSocket;

import org.apache.hc.client5.http.SystemDefaultDnsResolver;
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
import org.apache.hc.client5.http.config.ConnectionConfig;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.DefaultSchemePortResolver;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.DefaultHttpClientConnectionOperator;
import org.apache.hc.client5.http.impl.io.ManagedHttpClientConnectionFactory;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.client5.http.socket.ConnectionSocketFactory;
import org.apache.hc.client5.http.socket.PlainConnectionSocketFactory;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
import org.apache.hc.client5.http.io.HttpClientConnectionOperator;
import org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy;
import org.apache.hc.client5.http.ssl.TlsSocketStrategy;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.ConnectionClosedException;
import org.apache.hc.core5.http.ContentLengthStrategy;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.config.Registry;
import org.apache.hc.core5.http.config.RegistryBuilder;
import org.apache.hc.core5.http.impl.DefaultContentLengthStrategy;
import org.apache.hc.core5.http.impl.io.EmptyInputStream;
import org.apache.hc.core5.http.io.SocketConfig;
import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
import org.apache.hc.core5.http.io.entity.EmptyInputStream;
import org.apache.hc.core5.http.io.entity.InputStreamEntity;
import org.apache.hc.core5.http.protocol.BasicHttpContext;
import org.apache.hc.core5.http.protocol.HttpContext;
import org.apache.hc.core5.http.protocol.HttpCoreContext;
import org.apache.hc.core5.net.URIAuthority;
import org.apache.hc.core5.util.TimeValue;
import org.apache.hc.core5.util.Timeout;
Expand All @@ -38,7 +41,6 @@
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URI;
import java.time.Duration;
Expand All @@ -61,7 +63,13 @@ protected ApacheDockerHttpClientImpl(
Duration connectionTimeout,
Duration responseTimeout
) {
Registry<ConnectionSocketFactory> socketFactoryRegistry = createConnectionSocketFactoryRegistry(sslConfig, dockerHost);
SSLContext sslContext;
try {
sslContext = sslConfig != null ? sslConfig.getSSLContext() : null;
} catch (Exception e) {
throw new RuntimeException(e);
}
HttpClientConnectionOperator connectionOperator = createConnectionOperator(dockerHost, sslContext);

switch (dockerHost.getScheme()) {
case "unix":
Expand All @@ -75,7 +83,7 @@ protected ApacheDockerHttpClientImpl(
? rawPath.substring(0, rawPath.length() - 1)
: rawPath;
host = new HttpHost(
socketFactoryRegistry.lookup("https") != null ? "https" : "http",
sslContext != null ? "https" : "http",
dockerHost.getHost(),
dockerHost.getPort()
);
Expand All @@ -85,7 +93,10 @@ protected ApacheDockerHttpClientImpl(
}

PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(
socketFactoryRegistry,
connectionOperator,
null,
null,
null,
new ManagedHttpClientConnectionFactory(
null,
null,
Expand All @@ -109,77 +120,49 @@ protected ApacheDockerHttpClientImpl(
.setSoTimeout(Timeout.ZERO_MILLISECONDS)
.build()
);
connectionManager.setValidateAfterInactivity(TimeValue.NEG_ONE_SECOND);
connectionManager.setMaxTotal(maxConnections);
connectionManager.setDefaultMaxPerRoute(maxConnections);
RequestConfig.Builder defaultRequest = RequestConfig.custom();
if (connectionTimeout != null) {
defaultRequest.setConnectTimeout(connectionTimeout.toNanos(), TimeUnit.NANOSECONDS);
}
if (responseTimeout != null) {
defaultRequest.setResponseTimeout(responseTimeout.toNanos(), TimeUnit.NANOSECONDS);
}
connectionManager.setDefaultConnectionConfig(ConnectionConfig.custom()
.setValidateAfterInactivity(TimeValue.NEG_ONE_SECOND)
.setConnectTimeout(connectionTimeout != null ? Timeout.of(connectionTimeout.toNanos(), TimeUnit.NANOSECONDS) : null)
.build());

httpClient = HttpClients.custom()
.setRequestExecutor(new HijackingHttpRequestExecutor(null))
.setConnectionManager(connectionManager)
.setDefaultRequestConfig(defaultRequest.build())
.setDefaultRequestConfig(RequestConfig.custom()
.setResponseTimeout(responseTimeout != null ? Timeout.of(responseTimeout.toNanos(), TimeUnit.NANOSECONDS) : null)
.build())
.disableConnectionState()
.build();
}

private Registry<ConnectionSocketFactory> createConnectionSocketFactoryRegistry(
SSLConfig sslConfig,
URI dockerHost
private HttpClientConnectionOperator createConnectionOperator(
URI dockerHost,
SSLContext sslContext
) {
RegistryBuilder<ConnectionSocketFactory> socketFactoryRegistryBuilder = RegistryBuilder.create();

if (sslConfig != null) {
try {
SSLContext sslContext = sslConfig.getSSLContext();
if (sslContext != null) {
socketFactoryRegistryBuilder.register("https", new SSLConnectionSocketFactory(sslContext));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}

return socketFactoryRegistryBuilder
.register("tcp", PlainConnectionSocketFactory.INSTANCE)
.register("http", PlainConnectionSocketFactory.INSTANCE)
.register("unix", new ConnectionSocketFactory() {
@Override
public Socket createSocket(HttpContext context) throws IOException {
return UnixSocket.get(dockerHost.getPath());
String dockerHostScheme = dockerHost.getScheme();
String dockerHostPath = dockerHost.getPath();
TlsSocketStrategy tlsSocketStrategy = sslContext != null ?
new DefaultClientTlsStrategy(sslContext) : DefaultClientTlsStrategy.createSystemDefault();
return new DefaultHttpClientConnectionOperator(
socksProxy -> {
if ("unix".equalsIgnoreCase(dockerHostScheme)) {
return UnixSocket.get(dockerHostPath);
} else if ("npipe".equalsIgnoreCase(dockerHostScheme)) {
return new NamedPipeSocket(dockerHostPath);
} else {
return socksProxy == null ? new Socket() : new Socket(socksProxy);
}

@Override
public Socket connectSocket(TimeValue timeValue, Socket socket, HttpHost httpHost, InetSocketAddress inetSocketAddress,
InetSocketAddress inetSocketAddress1, HttpContext httpContext) throws IOException {
return PlainConnectionSocketFactory.INSTANCE.connectSocket(timeValue, socket, httpHost, inetSocketAddress,
inetSocketAddress1, httpContext);
}
})
.register("npipe", new ConnectionSocketFactory() {
@Override
public Socket createSocket(HttpContext context) {
return new NamedPipeSocket(dockerHost.getPath());
}

@Override
public Socket connectSocket(TimeValue timeValue, Socket socket, HttpHost httpHost, InetSocketAddress inetSocketAddress,
InetSocketAddress inetSocketAddress1, HttpContext httpContext) throws IOException {
return PlainConnectionSocketFactory.INSTANCE.connectSocket(timeValue, socket, httpHost, inetSocketAddress,
inetSocketAddress1, httpContext);
}
})
.build();
},
DefaultSchemePortResolver.INSTANCE,
SystemDefaultDnsResolver.INSTANCE,
name -> "https".equalsIgnoreCase(name) ? tlsSocketStrategy : null);
}

@Override
public Response execute(Request request) {
HttpContext context = new BasicHttpContext();
HttpContext context = new HttpCoreContext();
HttpUriRequestBase httpUriRequest = new HttpUriRequestBase(request.method(), URI.create(pathPrefix + request.path()));
httpUriRequest.setScheme(host.getSchemeName());
httpUriRequest.setAuthority(new URIAuthority(host.getHostName(), host.getPort()));
Expand All @@ -203,7 +186,7 @@ public Response execute(Request request) {
}

try {
CloseableHttpResponse response = httpClient.execute(host, httpUriRequest, context);
ClassicHttpResponse response = httpClient.executeOpen(host, httpUriRequest, context);

return new ApacheResponse(httpUriRequest, response);
} catch (IOException e) {
Expand All @@ -222,9 +205,9 @@ static class ApacheResponse implements Response {

private final HttpUriRequestBase request;

private final CloseableHttpResponse response;
private final ClassicHttpResponse response;

ApacheResponse(HttpUriRequestBase httpUriRequest, CloseableHttpResponse response) {
ApacheResponse(HttpUriRequestBase httpUriRequest, ClassicHttpResponse response) {
this.request = httpUriRequest;
this.response = response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public ClassicHttpResponse execute(

InputStream hijackedInput = (InputStream) context.getAttribute(HIJACKED_INPUT_ATTRIBUTE);
if (hijackedInput != null) {
return executeHijacked(request, conn, context, hijackedInput);
return executeHijacked(request, conn, (HttpCoreContext) context, hijackedInput);
}

return super.execute(request, conn, informationCallback, context);
Expand All @@ -53,12 +53,12 @@ public ClassicHttpResponse execute(
private ClassicHttpResponse executeHijacked(
ClassicHttpRequest request,
HttpClientConnection conn,
HttpContext context,
HttpCoreContext context,
InputStream hijackedInput
) throws HttpException, IOException {
try {
context.setAttribute(HttpCoreContext.SSL_SESSION, conn.getSSLSession());
context.setAttribute(HttpCoreContext.CONNECTION_ENDPOINT, conn.getEndpointDetails());
context.setSSLSession(conn.getSSLSession());
context.setEndpointDetails(conn.getEndpointDetails());
final ProtocolVersion transportVersion = request.getVersion();
if (transportVersion != null) {
context.setProtocolVersion(transportVersion);
Expand Down