From bc342645ef1e2748265a89210ce28b81d7334e41 Mon Sep 17 00:00:00 2001 From: Anton Date: Wed, 5 Mar 2025 20:59:02 +0800 Subject: [PATCH 1/3] Introduce new TlsException --- src/Connection/DefaultConnectionFactory.php | 7 ++++--- src/TlsException.php | 7 +++++++ 2 files changed, 11 insertions(+), 3 deletions(-) create mode 100755 src/TlsException.php diff --git a/src/Connection/DefaultConnectionFactory.php b/src/Connection/DefaultConnectionFactory.php index 6227c35f..3c68e08f 100644 --- a/src/Connection/DefaultConnectionFactory.php +++ b/src/Connection/DefaultConnectionFactory.php @@ -10,6 +10,7 @@ use Amp\Http\Client\Request; use Amp\Http\Client\SocketException; use Amp\Http\Client\TimeoutException; +use Amp\Http\Client\TlsException; use Amp\Socket; use Amp\Socket\ClientTlsContext; use Amp\Socket\ConnectContext; @@ -123,7 +124,7 @@ public function create(Request $request, Cancellation $cancellation): Connection if ($tlsState !== Socket\TlsState::Disabled) { $socket->close(); - throw new SocketException('Failed to setup TLS connection, connection was in an unexpected TLS state (' . $tlsState->name . ')'); + throw new TlsException('Failed to setup TLS connection, connection was in an unexpected TLS state (' . $tlsState->name . ')'); } $socket->setupTls(new CompositeCancellation( @@ -137,7 +138,7 @@ public function create(Request $request, Cancellation $cancellation): Connection \preg_match('/error:[0-9a-f]*:[^:]*:[^:]*:(.+)$/i', $errorMessage, $matches); $errorMessage = \trim($matches[1] ?? \explode('():', $errorMessage, 2)[1] ?? $errorMessage); - throw new SocketException(\sprintf( + throw new TlsException(\sprintf( "Connection to '%s' @ '%s' closed during TLS handshake: %s", $authority, $socket->getRemoteAddress()->toString(), @@ -161,7 +162,7 @@ public function create(Request $request, Cancellation $cancellation): Connection if ($tlsInfo === null) { $socket->close(); - throw new SocketException(\sprintf( + throw new TlsException(\sprintf( "Socket closed after TLS handshake with '%s' @ '%s'", $authority, $socket->getRemoteAddress()->toString() diff --git a/src/TlsException.php b/src/TlsException.php new file mode 100755 index 00000000..797afdca --- /dev/null +++ b/src/TlsException.php @@ -0,0 +1,7 @@ + Date: Thu, 6 Mar 2025 01:43:28 +0800 Subject: [PATCH 2/3] Extend TlsException from SocketException for backward compatibility --- src/SocketException.php | 2 +- src/TlsException.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SocketException.php b/src/SocketException.php index 88d498ce..8f1c855c 100755 --- a/src/SocketException.php +++ b/src/SocketException.php @@ -2,6 +2,6 @@ namespace Amp\Http\Client; -final class SocketException extends HttpException +class SocketException extends HttpException { } diff --git a/src/TlsException.php b/src/TlsException.php index 797afdca..bd78f415 100755 --- a/src/TlsException.php +++ b/src/TlsException.php @@ -2,6 +2,6 @@ namespace Amp\Http\Client; -final class TlsException extends HttpException +final class TlsException extends SocketException { } From cf48d6201a8838576a1e5dbb010b9995cb507ba4 Mon Sep 17 00:00:00 2001 From: Anton Date: Thu, 6 Mar 2025 17:43:12 +0800 Subject: [PATCH 3/3] Update tests for use new TlsException --- test/ClientBadSslIntegrationTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/ClientBadSslIntegrationTest.php b/test/ClientBadSslIntegrationTest.php index 4f4774f6..62566500 100644 --- a/test/ClientBadSslIntegrationTest.php +++ b/test/ClientBadSslIntegrationTest.php @@ -19,7 +19,7 @@ public function testSelfSignedCertificate(): void { $request = new Request('https://self-signed.badssl.com/'); - $this->expectException(SocketException::class); + $this->expectException(TlsException::class); $this->expectExceptionMessageMatches("/^Connection to 'self-signed.badssl.com:443' @ '.+' closed during TLS handshake: certificate verify failed$/"); $this->client->request($request); @@ -29,7 +29,7 @@ public function testWrongHostCertificate(): void { $request = new Request('https://wrong.host.badssl.com/'); - $this->expectException(SocketException::class); + $this->expectException(TlsException::class); $this->expectExceptionMessageMatches("/^Connection to 'wrong.host.badssl.com:443' @ '.+' closed during TLS handshake: Peer certificate CN=`\*.badssl.com' did not match expected CN=`wrong.host.badssl.com'$/"); $this->client->request($request); @@ -39,7 +39,7 @@ public function testDhKeyTooSmall(): void { $request = new Request('https://dh512.badssl.com/'); - $this->expectException(SocketException::class); + $this->expectException(TlsException::class); $this->expectExceptionMessageMatches("/^Connection to 'dh512.badssl.com:443' @ '.+' closed during TLS handshake: dh key too small$/"); $this->client->request($request);