Skip to content

Commit e2ee779

Browse files
authored
Add more specific TlsException to extend SocketException for TLS handshake errors (#384)
1 parent 5dedc81 commit e2ee779

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed

src/Connection/DefaultConnectionFactory.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Amp\Http\Client\Request;
1111
use Amp\Http\Client\SocketException;
1212
use Amp\Http\Client\TimeoutException;
13+
use Amp\Http\Client\TlsException;
1314
use Amp\Socket;
1415
use Amp\Socket\ClientTlsContext;
1516
use Amp\Socket\ConnectContext;
@@ -123,7 +124,7 @@ public function create(Request $request, Cancellation $cancellation): Connection
123124
if ($tlsState !== Socket\TlsState::Disabled) {
124125
$socket->close();
125126

126-
throw new SocketException('Failed to setup TLS connection, connection was in an unexpected TLS state (' . $tlsState->name . ')');
127+
throw new TlsException('Failed to setup TLS connection, connection was in an unexpected TLS state (' . $tlsState->name . ')');
127128
}
128129

129130
$socket->setupTls(new CompositeCancellation(
@@ -137,7 +138,7 @@ public function create(Request $request, Cancellation $cancellation): Connection
137138
\preg_match('/error:[0-9a-f]*:[^:]*:[^:]*:(.+)$/i', $errorMessage, $matches);
138139
$errorMessage = \trim($matches[1] ?? \explode('():', $errorMessage, 2)[1] ?? $errorMessage);
139140

140-
throw new SocketException(\sprintf(
141+
throw new TlsException(\sprintf(
141142
"Connection to '%s' @ '%s' closed during TLS handshake: %s",
142143
$authority,
143144
$socket->getRemoteAddress()->toString(),
@@ -161,7 +162,7 @@ public function create(Request $request, Cancellation $cancellation): Connection
161162
if ($tlsInfo === null) {
162163
$socket->close();
163164

164-
throw new SocketException(\sprintf(
165+
throw new TlsException(\sprintf(
165166
"Socket closed after TLS handshake with '%s' @ '%s'",
166167
$authority,
167168
$socket->getRemoteAddress()->toString()

src/SocketException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
namespace Amp\Http\Client;
44

5-
final class SocketException extends HttpException
5+
class SocketException extends HttpException
66
{
77
}

src/TlsException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Amp\Http\Client;
4+
5+
final class TlsException extends SocketException
6+
{
7+
}

test/ClientBadSslIntegrationTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function testSelfSignedCertificate(): void
1919
{
2020
$request = new Request('https://self-signed.badssl.com/');
2121

22-
$this->expectException(SocketException::class);
22+
$this->expectException(TlsException::class);
2323
$this->expectExceptionMessageMatches("/^Connection to 'self-signed.badssl.com:443' @ '.+' closed during TLS handshake: certificate verify failed$/");
2424

2525
$this->client->request($request);
@@ -29,7 +29,7 @@ public function testWrongHostCertificate(): void
2929
{
3030
$request = new Request('https://wrong.host.badssl.com/');
3131

32-
$this->expectException(SocketException::class);
32+
$this->expectException(TlsException::class);
3333
$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'$/");
3434

3535
$this->client->request($request);
@@ -39,7 +39,7 @@ public function testDhKeyTooSmall(): void
3939
{
4040
$request = new Request('https://dh512.badssl.com/');
4141

42-
$this->expectException(SocketException::class);
42+
$this->expectException(TlsException::class);
4343
$this->expectExceptionMessageMatches("/^Connection to 'dh512.badssl.com:443' @ '.+' closed during TLS handshake: dh key too small$/");
4444

4545
$this->client->request($request);

0 commit comments

Comments
 (0)