Details
-
Bug
-
Status: Closed
-
Minor
-
Resolution: Fixed
-
4.5.2
-
None
-
None
Description
We have some legacy library code that uses the http client.
We pass in a well configured http client (with connection and socket timeouts) to the library.
The library calls the deprecated org.apache.http.client.params.HttpClientParams. This causes the timeouts to be set to 0 when the clients tries to connect to a socket.
The library calls:
HttpClientParams.setRedirecting(get.getParams(), true);
This makes the org.apache.http.impl.client.InternalHttpClient#doExecute call HttpClientParamConfig.getRequestConfig(params). If HttpClientParamConfig does not find a value for the timeouts in params, it defaults to 0. Perhaps it should get those default values from the configured default request config?
There is a junit test that demonstrates the behaviour:
import org.apache.http.HttpHost; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.params.HttpClientParams; import org.apache.http.config.SocketConfig; import org.apache.http.conn.socket.LayeredConnectionSocketFactory; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.protocol.HttpContext; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.net.InetSocketAddress; import java.net.Socket; import java.net.UnknownHostException; import static org.junit.Assert.assertEquals; public final class HttpClientTest { CloseableHttpClient client; @Before public void setUp() throws Exception { final int timeout = 100; SocketConfig socketConfig = SocketConfig.custom() .setSoTimeout(timeout) .build(); RequestConfig requestConfig = RequestConfig.custom() .setConnectionRequestTimeout(timeout) .setConnectTimeout(timeout) .setSocketTimeout(timeout) .build(); client = HttpClientBuilder.create() .setDefaultSocketConfig(socketConfig) .setDefaultRequestConfig(requestConfig) .setSSLSocketFactory(new LayeredConnectionSocketFactory() { @Override public Socket createLayeredSocket(Socket socket, String target, int port, HttpContext context) throws IOException, UnknownHostException { return SSLConnectionSocketFactory.getSystemSocketFactory() .createLayeredSocket(socket, target, port, context); } @Override public Socket createSocket(HttpContext context) throws IOException { return SSLConnectionSocketFactory.getSystemSocketFactory() .createSocket(context); } @Override public Socket connectSocket(int connectTimeout, Socket sock, HttpHost host, InetSocketAddress remoteAddress, InetSocketAddress localAddress, HttpContext context) throws IOException { assertEquals(connectTimeout, timeout); throw new RuntimeException("expected"); } }) .build(); } @After public void tearDown() throws Exception { client.close(); } @Test(expected = RuntimeException.class) public void timeoutsIsCleared() throws Exception { HttpGet get = new HttpGet("https://google.com"); HttpClientParams.setRedirecting(get.getParams(), true); try(CloseableHttpResponse response = client.execute(get)){ } } @Test(expected = RuntimeException.class) public void timeoutsIsRetained() throws Exception { HttpGet get = new HttpGet("https://google.com"); try(CloseableHttpResponse response = client.execute(get)){ } } }