Skip to content

Commit ee0a102

Browse files
committed
HTTPCLIENT-2328: Blocking i/o connections to check if the opposite TLS endpoint has been closed by the opposite endpoint while writing out request body
1 parent 10e8a7a commit ee0a102

File tree

6 files changed

+43
-6
lines changed

6 files changed

+43
-6
lines changed

httpclient5/src/main/java/org/apache/hc/client5/http/impl/io/DefaultHttpClientConnectionOperator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ public void connect(
235235
if (LOG.isDebugEnabled()) {
236236
LOG.debug("{} {} upgrading to TLS", ConnPoolSupport.getId(conn), tlsName);
237237
}
238-
final Socket upgradedSocket = tlsSocketStrategy.upgrade(socket, tlsName.getHostName(), tlsName.getPort(), attachment, context);
239-
conn.bind(upgradedSocket);
238+
final SSLSocket sslSocket = tlsSocketStrategy.upgrade(socket, tlsName.getHostName(), tlsName.getPort(), attachment, context);
239+
conn.bind(sslSocket, socket);
240240
onAfterTlsHandshake(context, endpointHost);
241241
if (LOG.isDebugEnabled()) {
242242
LOG.debug("{} {} upgraded to TLS", ConnPoolSupport.getId(conn), tlsName);

httpclient5/src/main/java/org/apache/hc/client5/http/impl/io/DefaultManagedHttpClientConnection.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,14 @@ public void bind(final Socket socket) throws IOException {
184184
socketTimeout = Timeout.ofMilliseconds(socket.getSoTimeout());
185185
}
186186

187+
@Override
188+
public void bind(final SSLSocket sslSocket, final Socket socket) throws IOException {
189+
super.bind(WIRE_LOG.isDebugEnabled() ?
190+
new LoggingSocketHolder(sslSocket, socket, this.id, WIRE_LOG) :
191+
new SocketHolder(sslSocket, socket));
192+
socketTimeout = Timeout.ofMilliseconds(sslSocket.getSoTimeout());
193+
}
194+
187195
@Override
188196
protected void onResponseReceived(final ClassicHttpResponse response) {
189197
if (response != null && HEADER_LOG.isDebugEnabled()) {

httpclient5/src/main/java/org/apache/hc/client5/http/impl/io/LoggingSocketHolder.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import java.io.OutputStream;
3333
import java.net.Socket;
3434

35+
import javax.net.ssl.SSLSocket;
36+
3537
import org.apache.hc.client5.http.impl.Wire;
3638
import org.apache.hc.core5.http.impl.io.SocketHolder;
3739
import org.slf4j.Logger;
@@ -45,6 +47,11 @@ public LoggingSocketHolder(final Socket socket, final String id, final Logger lo
4547
this.wire = new Wire(log, id);
4648
}
4749

50+
LoggingSocketHolder(final SSLSocket sslSocket, final Socket baseSocket, final String id, final Logger log) {
51+
super(sslSocket, baseSocket);
52+
this.wire = new Wire(log, id);
53+
}
54+
4855
@Override
4956
protected InputStream getInputStream(final Socket socket) throws IOException {
5057
return new LoggingInputStream(super.getInputStream(socket), wire);

httpclient5/src/main/java/org/apache/hc/client5/http/io/ManagedHttpClientConnection.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.net.Socket;
3232

3333
import javax.net.ssl.SSLSession;
34+
import javax.net.ssl.SSLSocket;
3435

3536
import org.apache.hc.core5.annotation.Internal;
3637
import org.apache.hc.core5.http.io.HttpClientConnection;
@@ -55,6 +56,21 @@ public interface ManagedHttpClientConnection extends HttpClientConnection {
5556
*/
5657
void bind(Socket socket) throws IOException;
5758

59+
/**
60+
* Binds this connection to the SSL given socket and the underlying network
61+
* socket. The connection is considered open if it is bound, the underlying
62+
* network socket is connection to a remote host and the SSL socket is
63+
* fully initialized (TLS handshake has been successfully executed).
64+
*
65+
* @param sslSocket the SSL socket to bind the connection to.
66+
* @param socket the underlying network socket of the SSL socket.
67+
*
68+
* @since 5.4
69+
*/
70+
default void bind(SSLSocket sslSocket, Socket socket) throws IOException {
71+
bind(sslSocket);
72+
}
73+
5874
/**
5975
* Returns the underlying socket.
6076
*/

httpclient5/src/main/java/org/apache/hc/client5/http/ssl/AbstractClientTlsStrategy.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import org.apache.hc.core5.http2.HttpVersionPolicy;
6363
import org.apache.hc.core5.http2.ssl.ApplicationProtocol;
6464
import org.apache.hc.core5.http2.ssl.H2TlsSupport;
65+
import org.apache.hc.core5.io.Closer;
6566
import org.apache.hc.core5.net.NamedEndpoint;
6667
import org.apache.hc.core5.reactor.ssl.SSLBufferMode;
6768
import org.apache.hc.core5.reactor.ssl.TlsDetails;
@@ -204,9 +205,14 @@ public SSLSocket upgrade(final Socket socket,
204205
socket,
205206
target,
206207
port,
207-
true);
208-
executeHandshake(upgradedSocket, target, attachment);
209-
return upgradedSocket;
208+
false);
209+
try {
210+
executeHandshake(upgradedSocket, target, attachment);
211+
return upgradedSocket;
212+
} catch (IOException | RuntimeException ex) {
213+
Closer.closeQuietly(upgradedSocket);
214+
throw ex;
215+
}
210216
}
211217

212218
private void executeHandshake(

httpclient5/src/test/java/org/apache/hc/client5/http/impl/io/TestHttpClientConnectionOperator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public void testConnectWithTLSUpgrade() throws Exception {
147147
Mockito.verify(socket).connect(new InetSocketAddress(ip1, 443), 123);
148148
Mockito.verify(conn, Mockito.times(2)).bind(socket);
149149
Mockito.verify(tlsSocketStrategy).upgrade(socket, "somehost", -1, tlsConfig, context);
150-
Mockito.verify(conn, Mockito.times(1)).bind(upgradedSocket);
150+
Mockito.verify(conn, Mockito.times(1)).bind(upgradedSocket, socket);
151151
}
152152

153153
@Test

0 commit comments

Comments
 (0)