Skip to content

Commit 50de334

Browse files
committed
[HttpClient] Simplify control flow and make sure resolved records are stored to cache
1 parent 6edd2a9 commit 50de334

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

src/Symfony/Component/HttpClient/NativeHttpClient.php

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -340,30 +340,26 @@ private static function dnsResolve(string $host, NativeClientState $multi, array
340340
$now = microtime(true);
341341

342342
if ($ip = gethostbynamel($host)) {
343-
$multi->dnsCache[$host] = $ip = $ip[0];
344-
$info['debug'] .= "* Added {$host}:0:{$ip} to DNS cache\n";
345-
$host = $ip;
346-
} elseif (\defined('STREAM_PF_INET6')) {
347-
if ($ip = dns_get_record($host, \DNS_AAAA)) {
348-
$multi->dnsCache[$host] = $ip = $ip[0]['ipv6'];
349-
$info['debug'] .= "* Added {$host}:0:{$ip} to DNS cache\n";
350-
$host = "[$ip]";
351-
} elseif (\extension_loaded('sockets')) {
352-
if (!$addrInfo = socket_addrinfo_lookup($host, 0, ['ai_socktype' => \SOCK_STREAM, 'ai_family' => \AF_INET6])) {
353-
throw new TransportException(\sprintf('Could not resolve host "%s".', $host));
354-
}
355-
356-
$ip = socket_addrinfo_explain($addrInfo[0])['ai_addr']['sin6_addr'];
357-
$host = "[$ip]";
358-
} elseif ('localhost' === $host || 'localhost.' === $host) {
359-
$ip = '::1';
360-
$host = "[$ip]";
361-
} else {
343+
$ip = $ip[0];
344+
} elseif (!\defined('STREAM_PF_INET6')) {
345+
throw new TransportException(\sprintf('Could not resolve host "%s".', $host));
346+
} elseif ($ip = dns_get_record($host, \DNS_AAAA)) {
347+
$ip = $ip[0]['ipv6'];
348+
} elseif (\extension_loaded('sockets')) {
349+
if (!$addrInfo = socket_addrinfo_lookup($host, 0, ['ai_socktype' => \SOCK_STREAM, 'ai_family' => \AF_INET6])) {
362350
throw new TransportException(\sprintf('Could not resolve host "%s".', $host));
363351
}
352+
353+
$ip = socket_addrinfo_explain($addrInfo[0])['ai_addr']['sin6_addr'];
354+
} elseif ('localhost' === $host || 'localhost.' === $host) {
355+
$ip = '::1';
364356
} else {
365357
throw new TransportException(\sprintf('Could not resolve host "%s".', $host));
366358
}
359+
360+
$multi->dnsCache[$host] = $ip;
361+
$info['debug'] .= "* Added {$host}:0:{$ip} to DNS cache\n";
362+
$host = str_contains($ip, ':') ? "[$ip]" : $ip;
367363
} else {
368364
$info['debug'] .= "* Hostname was found in DNS cache\n";
369365
$host = str_contains($ip, ':') ? "[$ip]" : $ip;

0 commit comments

Comments
 (0)