Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 4 additions & 16 deletions src/Internal/Capabilities/Sockets/Clients.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<Stream> */
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));
}

Expand Down
21 changes: 21 additions & 0 deletions src/Sockets/Internet/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
}
}
Loading