Skip to content

Commit 76eee28

Browse files
authored
fix: throw exception on stream_socket_enable_crypto() warning (#1158)
1 parent 0ed3ecc commit 76eee28

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

PhpAmqpLib/Wire/IO/StreamIO.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public function connect()
139139

140140
$options = stream_context_get_options($context);
141141
if (isset($options['ssl']['crypto_method'])) {
142-
$this->enable_crypto();
142+
$this->enableCrypto();
143143
}
144144

145145
$this->heartbeat = $this->initial_heartbeat;
@@ -432,16 +432,31 @@ protected function extract_error_code($message)
432432
return 0;
433433
}
434434

435-
private function enable_crypto(): void
435+
/**
436+
* @throws AMQPIOException
437+
*/
438+
private function enableCrypto(): void
436439
{
437440
$timeout_at = time() + ($this->read_timeout + $this->write_timeout) * 2; // 2 round-trips during handshake
438441

439-
do {
440-
$enabled = stream_socket_enable_crypto($this->sock, true);
441-
} while ($enabled !== true && time() < $timeout_at);
442+
try {
443+
$this->setErrorHandler();
444+
do {
445+
$enabled = stream_socket_enable_crypto($this->sock, true);
446+
if ($enabled === true) {
447+
return;
448+
}
449+
$this->throwOnError();
450+
usleep(1e3);
451+
} while ($enabled === 0 && time() < $timeout_at);
452+
} catch (\ErrorException $exception) {
453+
throw new AMQPIOException($exception->getMessage(), $exception->getCode(), $exception);
454+
} finally {
455+
$this->restoreErrorHandler();
456+
}
442457

443458
if ($enabled !== true) {
444-
throw new AMQPIOException('Can not enable crypto');
459+
throw new AMQPIOException('Could not enable socket crypto');
445460
}
446461
}
447462
}

tests/Unit/Connection/AMQPConnectionConfigTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public function external_auth_with_user_credentials()
5050
public function secure_with_incorrect_crypto_method()
5151
{
5252
$this->expectException(AMQPIOException::class);
53-
$this->expectExceptionMessage('Can not enable crypto');
5453

5554
$cert_dir = realpath(__DIR__ . "/../../certs");
5655
$config = new AMQPConnectionConfig();

0 commit comments

Comments
 (0)