diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:38:42 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:38:42 +0000 |
commit | c3ca98e1b35123f226c7f4c596b5dee78caa4223 (patch) | |
tree | 9b6eb109283da55e7d9064baa9fac795a40264cb /vendor/react/socket/src/HappyEyeBallsConnector.php | |
parent | Initial commit. (diff) | |
download | icinga-php-thirdparty-c3ca98e1b35123f226c7f4c596b5dee78caa4223.tar.xz icinga-php-thirdparty-c3ca98e1b35123f226c7f4c596b5dee78caa4223.zip |
Adding upstream version 0.11.0.upstream/0.11.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/react/socket/src/HappyEyeBallsConnector.php')
-rw-r--r-- | vendor/react/socket/src/HappyEyeBallsConnector.php | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/vendor/react/socket/src/HappyEyeBallsConnector.php b/vendor/react/socket/src/HappyEyeBallsConnector.php new file mode 100644 index 0000000..4b04f77 --- /dev/null +++ b/vendor/react/socket/src/HappyEyeBallsConnector.php @@ -0,0 +1,69 @@ +<?php + +namespace React\Socket; + +use React\Dns\Resolver\ResolverInterface; +use React\EventLoop\Loop; +use React\EventLoop\LoopInterface; +use React\Promise; + +final class HappyEyeBallsConnector implements ConnectorInterface +{ + private $loop; + private $connector; + private $resolver; + + public function __construct(LoopInterface $loop = null, ConnectorInterface $connector = null, ResolverInterface $resolver = null) + { + // $connector and $resolver arguments are actually required, marked + // optional for technical reasons only. Nullable $loop without default + // requires PHP 7.1, null default is also supported in legacy PHP + // versions, but required parameters are not allowed after arguments + // with null default. Mark all parameters optional and check accordingly. + if ($connector === null || $resolver === null) { + throw new \InvalidArgumentException('Missing required $connector or $resolver argument'); + } + + $this->loop = $loop ?: Loop::get(); + $this->connector = $connector; + $this->resolver = $resolver; + } + + public function connect($uri) + { + $original = $uri; + if (\strpos($uri, '://') === false) { + $uri = 'tcp://' . $uri; + $parts = \parse_url($uri); + if (isset($parts['scheme'])) { + unset($parts['scheme']); + } + } else { + $parts = \parse_url($uri); + } + + if (!$parts || !isset($parts['host'])) { + return Promise\reject(new \InvalidArgumentException( + 'Given URI "' . $original . '" is invalid (EINVAL)', + \defined('SOCKET_EINVAL') ? \SOCKET_EINVAL : 22 + )); + } + + $host = \trim($parts['host'], '[]'); + + // skip DNS lookup / URI manipulation if this URI already contains an IP + if (@\inet_pton($host) !== false) { + return $this->connector->connect($original); + } + + $builder = new HappyEyeBallsConnectionBuilder( + $this->loop, + $this->connector, + $this->resolver, + $uri, + $host, + $parts + ); + return $builder->connect(); + } +} |