3636import org .apache .hc .client5 .http .impl .auth .DigestSchemeFactory ;
3737import org .apache .hc .client5 .http .impl .auth .SPNegoSchemeFactory ;
3838import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
39- import org .apache .hc .client5 .http .impl .classic .CloseableHttpResponse ;
4039import org .apache .hc .client5 .http .impl .classic .HttpClientBuilder ;
4140import org .apache .hc .client5 .http .impl .classic .HttpClients ;
4241import org .apache .hc .client5 .http .impl .io .PoolingHttpClientConnectionManager ;
4342import org .apache .hc .client5 .http .protocol .HttpClientContext ;
44- import org .apache .hc .client5 .http .socket .ConnectionSocketFactory ;
43+ import org .apache .hc .client5 .http .routing .RoutingSupport ;
44+ import org .apache .hc .core5 .http .ClassicHttpResponse ;
4545import org .apache .hc .core5 .http .ContentType ;
46+ import org .apache .hc .core5 .http .HttpException ;
47+ import org .apache .hc .core5 .http .HttpHost ;
4648import org .apache .hc .core5 .http .NoHttpResponseException ;
4749import org .apache .hc .core5 .http .config .Lookup ;
48- import org .apache .hc .core5 .http .config .Registry ;
4950import org .apache .hc .core5 .http .config .RegistryBuilder ;
5051import org .apache .hc .core5 .http .io .entity .ByteArrayEntity ;
5152import org .apache .hc .core5 .http .io .entity .EntityUtils ;
@@ -84,9 +85,9 @@ public class AvaticaCommonsHttpClientImpl implements AvaticaHttpClient, HttpClie
8485 private static AuthScope anyAuthScope = new AuthScope (null , -1 );
8586
8687 protected final URI uri ;
88+ protected HttpHost httpHost ;
8789 protected BasicAuthCache authCache ;
8890 protected CloseableHttpClient client ;
89- protected Registry <ConnectionSocketFactory > socketFactoryRegistry ;
9091 protected PoolingHttpClientConnectionManager pool ;
9192
9293 protected UsernamePasswordCredentials credentials = null ;
@@ -130,6 +131,8 @@ protected void initializeClient(PoolingHttpClientConnectionManager pool,
130131 private RequestConfig createRequestConfig () {
131132 RequestConfig .Builder requestConfigBuilder = RequestConfig .custom ();
132133 requestConfigBuilder
134+ // We cannot avoid this. If the timeout were defined on the pool, then
135+ // it couldn't be overridden later
133136 .setConnectTimeout (this .connectTimeout , TimeUnit .MILLISECONDS )
134137 .setResponseTimeout (this .responseTimeout , TimeUnit .MILLISECONDS );
135138 List <String > preferredSchemes = new ArrayList <>();
@@ -153,12 +156,19 @@ private RequestConfig createRequestConfig() {
153156 @ Override public byte [] send (byte [] request ) {
154157 while (true ) {
155158 ByteArrayEntity entity = new ByteArrayEntity (request , ContentType .APPLICATION_OCTET_STREAM );
156-
157- // Create the client with the AuthSchemeRegistry and manager
158159 HttpPost post = new HttpPost (uri );
159160 post .setEntity (entity );
160161
161- try (CloseableHttpResponse response = execute (post , context )) {
162+ if (httpHost == null ) {
163+ try {
164+ httpHost = RoutingSupport .determineHost (post );
165+ } catch (HttpException e ) {
166+ LOG .debug ("Failed to execute HTTP request" , e );
167+ throw new RuntimeException ("Could not determine Http Host from URI" , e );
168+ }
169+ }
170+
171+ try (ClassicHttpResponse response = executeOpen (httpHost , post , context )) {
162172 final int statusCode = response .getCode ();
163173 if (HttpURLConnection .HTTP_OK == statusCode
164174 || HttpURLConnection .HTTP_INTERNAL_ERROR == statusCode ) {
@@ -185,9 +195,9 @@ private RequestConfig createRequestConfig() {
185195 }
186196
187197 // Visible for testing
188- CloseableHttpResponse execute ( HttpPost post , HttpClientContext context )
198+ ClassicHttpResponse executeOpen ( HttpHost httpHost , HttpPost post , HttpClientContext context )
189199 throws IOException , ClientProtocolException {
190- return client .execute ( post , context );
200+ return client .executeOpen ( httpHost , post , context );
191201 }
192202
193203 @ Override public void setUsernamePassword (AuthenticationType authType , String username ,
0 commit comments