From d28e408ebbd2892ac9292c2263b18c0250ca0e39 Mon Sep 17 00:00:00 2001 From: Aayush Tiwari Date: Fri, 4 Dec 2026 09:05:59 +0530 Subject: [PATCH] Close proxy connection when error response consumption fails --- .../http/impl/classic/ProxyClient.java | 7 ++-- .../http/impl/classic/TestProxyClient.java | 35 ++++++++++++++++++- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ProxyClient.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ProxyClient.java index 651f352bb0..710499e9e2 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ProxyClient.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ProxyClient.java @@ -202,8 +202,11 @@ public Socket tunnel( final int status = response.getCode(); if (status > 299) { - EntityUtils.consume(response.getEntity()); - conn.close(); + try { + EntityUtils.consume(response.getEntity()); + } finally { + conn.close(); + } throw new HttpException("Tunnel refused: " + new StatusLine(response)); } return conn.getSocket(); diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestProxyClient.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestProxyClient.java index 4c14e2c5eb..b049554b32 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestProxyClient.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestProxyClient.java @@ -29,13 +29,17 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.io.IOException; +import java.io.InputStream; import java.net.Socket; import org.apache.hc.client5.http.auth.Credentials; @@ -43,6 +47,7 @@ import org.apache.hc.client5.http.config.RequestConfig; import org.apache.hc.client5.http.io.ManagedHttpClientConnection; import org.apache.hc.core5.http.ClassicHttpResponse; +import org.apache.hc.core5.http.HttpEntity; import org.apache.hc.core5.http.HttpException; import org.apache.hc.core5.http.HttpHost; import org.apache.hc.core5.http.impl.io.HttpRequestExecutor; @@ -110,4 +115,32 @@ void testSuccessfulTunnel() throws IOException, HttpException { assertEquals(mockSocket, resultSocket, "Expected the mock socket to be returned"); } -} \ No newline at end of file + @Test + void testTunnelClosesConnectionWhenConsumingErrorResponseFails() throws IOException, HttpException { + final HttpConnectionFactory connFactory = mock(HttpConnectionFactory.class); + final ManagedHttpClientConnection managedConnection = mock(ManagedHttpClientConnection.class); + when(managedConnection.isOpen()).thenReturn(true); + when(connFactory.createConnection(null)).thenReturn(managedConnection); + + final IOException consumeFailure = new IOException("Failed to consume proxy response"); + final InputStream content = mock(InputStream.class); + doThrow(consumeFailure).when(content).close(); + final HttpEntity entity = mock(HttpEntity.class); + when(entity.isStreaming()).thenReturn(true); + when(entity.getContent()).thenReturn(content); + final ClassicHttpResponse response = mock(ClassicHttpResponse.class); + when(response.getCode()).thenReturn(500); + when(response.getEntity()).thenReturn(entity); + when(managedConnection.receiveResponseHeader()).thenReturn(response); + + final ProxyClient client = new ProxyClient(connFactory, null, null, RequestConfig.DEFAULT); + final HttpHost proxy = new HttpHost("proxy.example.com", 8080); + final HttpHost target = new HttpHost("target.example.com", 80); + final Credentials credentials = new UsernamePasswordCredentials("user", "pass".toCharArray()); + + assertSame(consumeFailure, assertThrows(IOException.class, + () -> client.tunnel(proxy, target, credentials))); + verify(managedConnection).close(); + } + +}