Skip to content

Commit f561c4e

Browse files
committed
Use tls:// scheme for URIs in error messages for secure connections
1 parent d77d54a commit f561c4e

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

src/SecureConnector.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ public function connect($uri)
3737
return Promise\reject(new \InvalidArgumentException('Given URI "' . $uri . '" is invalid'));
3838
}
3939

40-
$uri = \str_replace('tls://', '', $uri);
4140
$context = $this->context;
42-
4341
$encryption = $this->streamEncryption;
4442
$connected = false;
45-
$promise = $this->connector->connect($uri)->then(function (ConnectionInterface $connection) use ($context, $encryption, $uri, &$promise, &$connected) {
43+
$promise = $this->connector->connect(
44+
\str_replace('tls://', '', $uri)
45+
)->then(function (ConnectionInterface $connection) use ($context, $encryption, $uri, &$promise, &$connected) {
4646
// (unencrypted) TCP/IP connection succeeded
4747
$connected = true;
4848

@@ -66,6 +66,17 @@ public function connect($uri)
6666
$error->getCode()
6767
);
6868
});
69+
}, function (\Exception $e) use ($uri) {
70+
if ($e instanceof \RuntimeException) {
71+
$message = \preg_replace('/^Connection to [^ ]+/', '', $e->getMessage());
72+
$e = new \RuntimeException(
73+
'Connection to ' . $uri . $message,
74+
$e->getCode(),
75+
$e
76+
);
77+
}
78+
79+
throw $e;
6980
});
7081

7182
return new \React\Promise\Promise(

tests/FunctionalConnectorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ public function testCancelPendingTlsConnectionDuringTlsHandshakeShouldCloseTcpCo
144144
$loop = Factory::create();
145145

146146
$server = new TcpServer(0, $loop);
147-
$uri = str_replace('tcp://', '', $server->getAddress());
147+
$uri = str_replace('tcp://', 'tls://', $server->getAddress());
148148

149149
$connector = new Connector(array(), $loop);
150-
$promise = $connector->connect('tls://' . $uri);
150+
$promise = $connector->connect($uri);
151151

152152
$deferred = new Deferred();
153153
$server->on('connection', function (ConnectionInterface $connection) use ($promise, $deferred, $loop) {

tests/SecureConnectorTest.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,28 @@ public function testConnectionToInvalidSchemeWillReject()
6868
$promise->then(null, $this->expectCallableOnce());
6969
}
7070

71+
public function testConnectWillRejectWithTlsUriWhenUnderlyingConnectorRejects()
72+
{
73+
$this->tcp->expects($this->once())->method('connect')->with('example.com:80')->willReturn(\React\Promise\reject(new \RuntimeException('Connection to tcp://example.com:80 failed: Connection refused', 42)));
74+
75+
$promise = $this->connector->connect('example.com:80');
76+
$promise->cancel();
77+
78+
$this->setExpectedException('RuntimeException', 'Connection to tls://example.com:80 failed: Connection refused', 42);
79+
$this->throwRejection($promise);
80+
}
81+
82+
public function testConnectWillRejectWithOriginalMessageWhenUnderlyingConnectorRejectsWithInvalidArgumentException()
83+
{
84+
$this->tcp->expects($this->once())->method('connect')->with('example.com:80')->willReturn(\React\Promise\reject(new \InvalidArgumentException('Invalid', 42)));
85+
86+
$promise = $this->connector->connect('example.com:80');
87+
$promise->cancel();
88+
89+
$this->setExpectedException('InvalidArgumentException', 'Invalid', 42);
90+
$this->throwRejection($promise);
91+
}
92+
7193
public function testCancelDuringTcpConnectionCancelsTcpConnection()
7294
{
7395
$pending = new Promise\Promise(function () { }, $this->expectCallableOnce());
@@ -79,13 +101,13 @@ public function testCancelDuringTcpConnectionCancelsTcpConnection()
79101

80102
public function testCancelDuringTcpConnectionCancelsTcpConnectionAndRejectsWithTcpRejection()
81103
{
82-
$pending = new Promise\Promise(function () { }, function () { throw new \RuntimeException('Connection cancelled'); });
104+
$pending = new Promise\Promise(function () { }, function () { throw new \RuntimeException('Connection to tcp://example.com:80 cancelled', 42); });
83105
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('example.com:80'))->will($this->returnValue($pending));
84106

85107
$promise = $this->connector->connect('example.com:80');
86108
$promise->cancel();
87109

88-
$this->setExpectedException('RuntimeException', 'Connection cancelled');
110+
$this->setExpectedException('RuntimeException', 'Connection to tls://example.com:80 cancelled', 42);
89111
$this->throwRejection($promise);
90112
}
91113

@@ -139,7 +161,7 @@ public function testConnectionWillBeRejectedIfStreamEncryptionFailsAndClosesConn
139161
try {
140162
$this->throwRejection($promise);
141163
} catch (\RuntimeException $e) {
142-
$this->assertEquals('Connection to example.com:80 failed during TLS handshake: TLS error', $e->getMessage());
164+
$this->assertEquals('Connection to tls://example.com:80 failed during TLS handshake: TLS error', $e->getMessage());
143165
$this->assertEquals(123, $e->getCode());
144166
$this->assertNull($e->getPrevious());
145167
}
@@ -165,7 +187,7 @@ public function testCancelDuringStreamEncryptionCancelsEncryptionAndClosesConnec
165187
$promise = $this->connector->connect('example.com:80');
166188
$promise->cancel();
167189

168-
$this->setExpectedException('RuntimeException', 'Connection to example.com:80 cancelled during TLS handshake');
190+
$this->setExpectedException('RuntimeException', 'Connection to tls://example.com:80 cancelled during TLS handshake');
169191
$this->throwRejection($promise);
170192
}
171193

0 commit comments

Comments
 (0)