|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace React\Socket; |
| 4 | + |
| 5 | +use React\Dns\Model\Message; |
| 6 | +use React\Dns\Resolver\Resolver; |
| 7 | +use React\EventLoop\LoopInterface; |
| 8 | +use React\EventLoop\TimerInterface; |
| 9 | +use React\Promise; |
| 10 | +use React\Promise\CancellablePromiseInterface; |
| 11 | +use InvalidArgumentException; |
| 12 | +use RuntimeException; |
| 13 | + |
| 14 | +/** |
| 15 | + * @internal |
| 16 | + */ |
| 17 | +final class HappyEyeBallsConnectionBuilder |
| 18 | +{ |
| 19 | + public $loop; |
| 20 | + public $connector; |
| 21 | + public $resolver; |
| 22 | + public $uri; |
| 23 | + public $host; |
| 24 | + public $resolved = array(); |
| 25 | + public $resolverPromises = array(); |
| 26 | + public $connectionPromises = array(); |
| 27 | + public $connectQueue = array(); |
| 28 | + public $timer; |
| 29 | + public $parts; |
| 30 | + public $ipsCount = 0; |
| 31 | + public $failureCount = 0; |
| 32 | + |
| 33 | + public function __construct(LoopInterface $loop, ConnectorInterface $connector, Resolver $resolver, $uri) |
| 34 | + { |
| 35 | + $this->loop = $loop; |
| 36 | + $this->connector = $connector; |
| 37 | + $this->resolver = $resolver; |
| 38 | + $this->uri = $uri; |
| 39 | + |
| 40 | + if (\strpos($uri, '://') === false) { |
| 41 | + $this->parts = \parse_url('tcp://' . $uri); |
| 42 | + unset($this->parts['scheme']); |
| 43 | + } else { |
| 44 | + $this->parts = \parse_url($uri); |
| 45 | + } |
| 46 | + |
| 47 | + if (!$this->parts || !isset($this->parts['host'])) { |
| 48 | + throw new \InvalidArgumentException('Given URI "' . $uri . '" is invalid'); |
| 49 | + } |
| 50 | + |
| 51 | + $this->host = \trim($this->parts['host'], '[]'); |
| 52 | + } |
| 53 | + |
| 54 | + public function connect() |
| 55 | + { |
| 56 | + // skip DNS lookup / URI manipulation if this URI already contains an IP |
| 57 | + if (false !== \filter_var($this->host, \FILTER_VALIDATE_IP)) { |
| 58 | + return $this->connector->connect($this->uri); |
| 59 | + } |
| 60 | + |
| 61 | + $that = $this; |
| 62 | + return new Promise\Promise(function ($resolve, $reject) use ($that) { |
| 63 | + foreach (array(Message::TYPE_AAAA, Message::TYPE_A) as $type) { |
| 64 | + $that->resolved[$type] = false; |
| 65 | + $that->resolverPromises[$type] = $that->resolver->resolveAll($that->host, $type)->then(function (array $ips) use ($that, $resolve, $reject, $type) { |
| 66 | + unset($that->resolverPromises[$type]); |
| 67 | + $that->resolved[$type] = true; |
| 68 | + $that->ipsCount += \count($ips); |
| 69 | + $connectQueueStash = $that->connectQueue; |
| 70 | + $that->connectQueue = array(); |
| 71 | + while (\count($connectQueueStash) > 0 || \count($ips) > 0) { |
| 72 | + if (\count($ips) > 0) { |
| 73 | + $that->connectQueue[] = \array_shift($ips); |
| 74 | + } |
| 75 | + if (\count($connectQueueStash) > 0) { |
| 76 | + $that->connectQueue[] = \array_shift($connectQueueStash); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + $that->check($resolve, $reject); |
| 81 | + }, function () use ($type, $reject, $that) { |
| 82 | + unset($that->resolverPromises[$type]); |
| 83 | + $that->resolved[$type] = true; |
| 84 | + |
| 85 | + foreach ($that->resolved as $typeHasBeenResolved) { |
| 86 | + if ($typeHasBeenResolved === false) { |
| 87 | + return; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + if ($that->ipsCount === 0) { |
| 92 | + $that->resolved = null; |
| 93 | + $that->resolverPromises = null; |
| 94 | + $reject(new \RuntimeException('Connection to ' . $that->uri . ' failed during DNS lookup: DNS error')); |
| 95 | + } |
| 96 | + }); |
| 97 | + } |
| 98 | + }, function ($_, $reject) use ($that) { |
| 99 | + if ($that->timer instanceof TimerInterface) { |
| 100 | + $that->loop->cancelTimer($that->timer); |
| 101 | + $that->timer = null; |
| 102 | + } |
| 103 | + |
| 104 | + foreach ($that->resolverPromises as $index => $resolverPromise) { |
| 105 | + if ($resolverPromise instanceof CancellablePromiseInterface) { |
| 106 | + $resolverPromise->cancel(); |
| 107 | + unset($that->resolverPromises[$index]); |
| 108 | + $resolverPromise = null; |
| 109 | + } |
| 110 | + } |
| 111 | + $that->resolverPromises = null; |
| 112 | + |
| 113 | + foreach ($that->connectionPromises as $index => $connectionPromise) { |
| 114 | + if ($connectionPromise instanceof CancellablePromiseInterface) { |
| 115 | + $connectionPromise->cancel(); |
| 116 | + unset($that->connectionPromises[$index]); |
| 117 | + $connectionPromise = null; |
| 118 | + } |
| 119 | + } |
| 120 | + $that->connectionPromises = null; |
| 121 | + |
| 122 | + $reject(new \RuntimeException('Connection to ' . $that->uri . ' cancelled during DNS lookup')); |
| 123 | + |
| 124 | + $_ = $reject = null; |
| 125 | + }); |
| 126 | + } |
| 127 | + |
| 128 | + public function check($resolve, $reject) |
| 129 | + { |
| 130 | + $hasBeenResolved = true; |
| 131 | + foreach ($this->resolved as $typeHasBeenResolved) { |
| 132 | + if ($typeHasBeenResolved === false) { |
| 133 | + $hasBeenResolved = false; |
| 134 | + |
| 135 | + break; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + if ($hasBeenResolved === true && \count($this->connectQueue) === 0 && $this->timer instanceof TimerInterface) { |
| 140 | + $this->loop->cancelTimer($this->timer); |
| 141 | + $this->timer = null; |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + if (\count($this->connectQueue) === 0 && $this->timer instanceof TimerInterface) { |
| 146 | + $this->loop->cancelTimer($this->timer); |
| 147 | + $this->timer = null; |
| 148 | + return; |
| 149 | + } |
| 150 | + |
| 151 | + $ip = \array_shift($this->connectQueue); |
| 152 | + |
| 153 | + if (\count($this->connectQueue) === 0 && $this->timer instanceof TimerInterface) { |
| 154 | + $this->loop->cancelTimer($this->timer); |
| 155 | + $this->timer = null; |
| 156 | + } |
| 157 | + |
| 158 | + $that = $this; |
| 159 | + $that->connectionPromises[$ip] = $this->attemptConnection($ip)->then(function ($connection) use ($that, $ip, $resolve) { |
| 160 | + unset($that->connectionPromises[$ip]); |
| 161 | + |
| 162 | + /** @var CancellablePromiseInterface $promise */ |
| 163 | + foreach ($that->connectionPromises as $index => $connectionPromise) { |
| 164 | + if ($connectionPromise instanceof CancellablePromiseInterface) { |
| 165 | + $connectionPromise->cancel(); |
| 166 | + } |
| 167 | + unset($that->connectionPromises[$index]); |
| 168 | + $connectionPromise = null; |
| 169 | + } |
| 170 | + $that->connectionPromises = array(); |
| 171 | + |
| 172 | + /** @var CancellablePromiseInterface $promise */ |
| 173 | + foreach ($that->resolverPromises as $index => $resolverPromise) { |
| 174 | + if ($resolverPromise instanceof CancellablePromiseInterface) { |
| 175 | + $resolverPromise->cancel(); |
| 176 | + } |
| 177 | + unset($that->resolverPromises[$index]); |
| 178 | + $resolverPromise = null; |
| 179 | + } |
| 180 | + $that->resolverPromises = array(); |
| 181 | + |
| 182 | + if ($that->timer instanceof TimerInterface) { |
| 183 | + $that->loop->cancelTimer($that->timer); |
| 184 | + $that->timer = null; |
| 185 | + } |
| 186 | + |
| 187 | + $resolve($connection); |
| 188 | + }, function () use ($that, $ip, $resolve, $reject) { |
| 189 | + unset($that->connectionPromises[$ip]); |
| 190 | + |
| 191 | + $that->failureCount++; |
| 192 | + |
| 193 | + foreach ($that->resolved as $typeHasBeenResolved) { |
| 194 | + if ($typeHasBeenResolved === false) { |
| 195 | + return; |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + if ($that->ipsCount === $that->failureCount) { |
| 200 | + if ($that->timer instanceof TimerInterface) { |
| 201 | + $that->loop->cancelTimer($that->timer); |
| 202 | + $that->timer = null; |
| 203 | + } |
| 204 | + $reject(new \RuntimeException('All attempts to connect to "' . $that->host . '" have failed')); |
| 205 | + } |
| 206 | + }); |
| 207 | + |
| 208 | + if (\count($this->connectQueue) > 0 && $this->timer === null) { |
| 209 | + $this->timer = $this->loop->addPeriodicTimer(0.05, function () use ($that, $resolve, $reject) { |
| 210 | + $that->check($resolve, $reject); |
| 211 | + }); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + public function attemptConnection($ip) |
| 216 | + { |
| 217 | + $resolved = null; |
| 218 | + $promise = null; |
| 219 | + $that = $this; |
| 220 | + |
| 221 | + return new Promise\Promise( |
| 222 | + function ($resolve, $reject) use (&$promise, &$resolved, $that, $ip) { |
| 223 | + $resolved = $ip; |
| 224 | + $uri = ''; |
| 225 | + |
| 226 | + // prepend original scheme if known |
| 227 | + if (isset($that->parts['scheme'])) { |
| 228 | + $uri .= $that->parts['scheme'] . '://'; |
| 229 | + } |
| 230 | + |
| 231 | + if (\strpos($ip, ':') !== false) { |
| 232 | + // enclose IPv6 addresses in square brackets before appending port |
| 233 | + $uri .= '[' . $ip . ']'; |
| 234 | + } else { |
| 235 | + $uri .= $ip; |
| 236 | + } |
| 237 | + |
| 238 | + // append original port if known |
| 239 | + if (isset($that->parts['port'])) { |
| 240 | + $uri .= ':' . $that->parts['port']; |
| 241 | + } |
| 242 | + |
| 243 | + // append orignal path if known |
| 244 | + if (isset($that->parts['path'])) { |
| 245 | + $uri .= $that->parts['path']; |
| 246 | + } |
| 247 | + |
| 248 | + // append original query if known |
| 249 | + if (isset($that->parts['query'])) { |
| 250 | + $uri .= '?' . $that->parts['query']; |
| 251 | + } |
| 252 | + |
| 253 | + // append original hostname as query if resolved via DNS and if |
| 254 | + // destination URI does not contain "hostname" query param already |
| 255 | + $args = array(); |
| 256 | + \parse_str(isset($that->parts['query']) ? $that->parts['query'] : '', $args); |
| 257 | + if ($that->host !== $ip && !isset($args['hostname'])) { |
| 258 | + $uri .= (isset($that->parts['query']) ? '&' : '?') . 'hostname=' . \rawurlencode($that->host); |
| 259 | + } |
| 260 | + |
| 261 | + // append original fragment if known |
| 262 | + if (isset($that->parts['fragment'])) { |
| 263 | + $uri .= '#' . $that->parts['fragment']; |
| 264 | + } |
| 265 | + |
| 266 | + $promise = $that->connector->connect($uri); |
| 267 | + $promise->then($resolve, $reject); |
| 268 | + }, |
| 269 | + function ($_, $reject) use (&$promise, &$resolved, $that) { |
| 270 | + // cancellation should reject connection attempt |
| 271 | + // (try to) cancel pending connection attempt |
| 272 | + if ($resolved === null) { |
| 273 | + $reject(new \RuntimeException('Connection to ' . $that->uri . ' cancelled during connection attempt')); |
| 274 | + } |
| 275 | + |
| 276 | + if ($promise instanceof CancellablePromiseInterface) { |
| 277 | + // overwrite callback arguments for PHP7+ only, so they do not show |
| 278 | + // up in the Exception trace and do not cause a possible cyclic reference. |
| 279 | + $_ = $reject = null; |
| 280 | + |
| 281 | + $promise->cancel(); |
| 282 | + $promise = null; |
| 283 | + } |
| 284 | + } |
| 285 | + ); |
| 286 | + } |
| 287 | +} |
0 commit comments