From 88e379c4461297498f82ebb1f338e31c8c1dee98 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sun, 16 Aug 2026 15:22:36 +0200 Subject: [PATCH] fix applying socket options before connecting --- CHANGELOG.md | 1 + src/Internal/Capabilities/Sockets/Clients.php | 20 ++++-------------- src/Sockets/Internet/Transport.php | 21 +++++++++++++++++++ 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8eb32c6..be5aa0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - Deleting a link pointing to a directory now delete the link instead of the directory content - Flagging a stream as blocking was set as non-blocking - Timeout periods above the minute were not taking into account +- Socket client options are applied when connecting, not after ## 4.1.2 - 2026-08-01 diff --git a/src/Internal/Capabilities/Sockets/Clients.php b/src/Internal/Capabilities/Sockets/Clients.php index d490655..1412740 100644 --- a/src/Internal/Capabilities/Sockets/Clients.php +++ b/src/Internal/Capabilities/Sockets/Clients.php @@ -39,28 +39,16 @@ public function internet(Transport $transport, Authority $authority): Attempt $transport->toString(), $authority->toString(), ); - $socket = @\stream_socket_client($address); + $socket = @\stream_socket_client( + $address, + context: $transport->asContext(), + ); if ($socket === false) { /** @var Attempt */ return Attempt::error(new RuntimeException("Failed to open socket at '$address'")); } - /** - * @psalm-suppress MissingClosureReturnType - * @var resource - */ - $socket = $transport - ->options() - ->reduce( - $socket, - static function($socket, string $key, int|bool|float|string|array $value) use ($transport) { - \stream_context_set_option($socket, $transport->toString(), $key, $value); - - return $socket; - }, - ); - return Attempt::result(Stream::of($socket)); } diff --git a/src/Sockets/Internet/Transport.php b/src/Sockets/Internet/Transport.php index 4b8ae81..876b864 100644 --- a/src/Sockets/Internet/Transport.php +++ b/src/Sockets/Internet/Transport.php @@ -99,4 +99,25 @@ public function toString(): string { return $this->transport; } + + /** + * @internal + * + * @return resource + */ + #[\NoDiscard] + public function asContext() + { + $options = \array_merge( + ...$this + ->options + ->map(static fn($key, $value) => [$key => $value]) + ->values() + ->toList(), + ); + + return \stream_context_create([ + $this->transport => $options, + ]); + } }