Skip to content

Commit 7d41709

Browse files
[HttpClient] Fix option "resolve" with IPv6 addresses
1 parent b815547 commit 7d41709

File tree

6 files changed

+56
-13
lines changed

6 files changed

+56
-13
lines changed

src/Symfony/Component/HttpClient/CurlHttpClient.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ private function validateExtraCurlOptions(array $options): void
490490
\CURLOPT_INFILESIZE => 'body',
491491
\CURLOPT_POSTFIELDS => 'body',
492492
\CURLOPT_UPLOAD => 'body',
493+
\CURLOPT_IPRESOLVE => 'bindto',
493494
\CURLOPT_INTERFACE => 'bindto',
494495
\CURLOPT_TIMEOUT_MS => 'max_duration',
495496
\CURLOPT_TIMEOUT => 'max_duration',

src/Symfony/Component/HttpClient/HttpClientTrait.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,14 @@ private static function mergeDefaultOptions(array $options, array $defaultOption
197197
if ($resolve = $options['resolve'] ?? false) {
198198
$options['resolve'] = [];
199199
foreach ($resolve as $k => $v) {
200-
$options['resolve'][substr(self::parseUrl('http://'.$k)['authority'], 2)] = (string) $v;
200+
if ('' === $v = (string) $v) {
201+
throw new InvalidArgumentException(sprintf('Option "resolve" for host "%s" cannot be empty.', $k));
202+
}
203+
if ('[' === $v[0] && ']' === substr($v, -1) && str_contains($v, ':')) {
204+
$v = substr($v, 1, -1);
205+
}
206+
207+
$options['resolve'][substr(self::parseUrl('http://'.$k)['authority'], 2)] = $v;
201208
}
202209
}
203210

@@ -220,7 +227,14 @@ private static function mergeDefaultOptions(array $options, array $defaultOption
220227

221228
if ($resolve = $defaultOptions['resolve'] ?? false) {
222229
foreach ($resolve as $k => $v) {
223-
$options['resolve'] += [substr(self::parseUrl('http://'.$k)['authority'], 2) => (string) $v];
230+
if ('' === $v = (string) $v) {
231+
throw new InvalidArgumentException(sprintf('Option "resolve" for host "%s" cannot be empty.', $k));
232+
}
233+
if ('[' === $v[0] && ']' === substr($v, -1) && str_contains($v, ':')) {
234+
$v = substr($v, 1, -1);
235+
}
236+
237+
$options['resolve'] += [substr(self::parseUrl('http://'.$k)['authority'], 2) => $v];
224238
}
225239
}
226240

src/Symfony/Component/HttpClient/Internal/AmpListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ public function startTlsNegotiation(Request $request): Promise
8080
public function startSendingRequest(Request $request, Stream $stream): Promise
8181
{
8282
$host = $stream->getRemoteAddress()->getHost();
83+
$this->info['primary_ip'] = $host;
8384

8485
if (false !== strpos($host, ':')) {
8586
$host = '['.$host.']';
8687
}
8788

88-
$this->info['primary_ip'] = $host;
8989
$this->info['primary_port'] = $stream->getRemoteAddress()->getPort();
9090
$this->info['pretransfer_time'] = microtime(true) - $this->info['start_time'];
9191
$this->info['debug'] .= sprintf("* Connected to %s (%s) port %d\n", $request->getUri()->getHost(), $host, $this->info['primary_port']);

src/Symfony/Component/HttpClient/Internal/AmpResolver.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,31 @@ public function __construct(array &$dnsMap)
3434

3535
public function resolve(string $name, ?int $typeRestriction = null): Promise
3636
{
37-
if (!isset($this->dnsMap[$name]) || !\in_array($typeRestriction, [Record::A, null], true)) {
37+
$recordType = Record::A;
38+
$ip = $this->dnsMap[$name] ?? null;
39+
40+
if (null !== $ip && str_contains($ip, ':')) {
41+
$recordType = Record::AAAA;
42+
}
43+
if (null === $ip || $recordType !== ($typeRestriction ?? $recordType)) {
3844
return Dns\resolver()->resolve($name, $typeRestriction);
3945
}
4046

41-
return new Success([new Record($this->dnsMap[$name], Record::A, null)]);
47+
return new Success([new Record($ip, $recordType, null)]);
4248
}
4349

4450
public function query(string $name, int $type): Promise
4551
{
46-
if (!isset($this->dnsMap[$name]) || Record::A !== $type) {
52+
$recordType = Record::A;
53+
$ip = $this->dnsMap[$name] ?? null;
54+
55+
if (null !== $ip && str_contains($ip, ':')) {
56+
$recordType = Record::AAAA;
57+
}
58+
if (null === $ip || $recordType !== $type) {
4759
return Dns\resolver()->query($name, $type);
4860
}
4961

50-
return new Success([new Record($this->dnsMap[$name], Record::A, null)]);
62+
return new Success([new Record($ip, $recordType, null)]);
5163
}
5264
}

src/Symfony/Component/HttpClient/NativeHttpClient.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -330,31 +330,35 @@ private static function parseHostPort(array $url, array &$info): array
330330
*/
331331
private static function dnsResolve($host, NativeClientState $multi, array &$info, ?\Closure $onProgress): string
332332
{
333-
if (null === $ip = $multi->dnsCache[$host] ?? null) {
333+
$flag = '' !== $host && '[' === $host[0] && ']' === $host[-1] && str_contains($host, ':') ? \FILTER_FLAG_IPV6 : \FILTER_FLAG_IPV4;
334+
$ip = \FILTER_FLAG_IPV6 === $flag ? substr($host, 1, -1) : $host;
335+
336+
if (filter_var($ip, \FILTER_VALIDATE_IP, $flag)) {
337+
// The host is already an IP address
338+
} elseif (null === $ip = $multi->dnsCache[$host] ?? null) {
334339
$info['debug'] .= "* Hostname was NOT found in DNS cache\n";
335340
$now = microtime(true);
336341

337-
if ('[' === $host[0] && ']' === $host[-1] && filter_var(substr($host, 1, -1), \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) {
338-
$ip = [$host];
339-
} elseif (!$ip = gethostbynamel($host)) {
342+
if (!$ip = gethostbynamel($host)) {
340343
throw new TransportException(sprintf('Could not resolve host "%s".', $host));
341344
}
342345

343-
$info['namelookup_time'] = microtime(true) - ($info['start_time'] ?: $now);
344346
$multi->dnsCache[$host] = $ip = $ip[0];
345347
$info['debug'] .= "* Added {$host}:0:{$ip} to DNS cache\n";
346348
} else {
347349
$info['debug'] .= "* Hostname was found in DNS cache\n";
350+
$host = str_contains($ip, ':') ? "[$ip]" : $ip;
348351
}
349352

353+
$info['namelookup_time'] = microtime(true) - ($info['start_time'] ?: $now);
350354
$info['primary_ip'] = $ip;
351355

352356
if ($onProgress) {
353357
// Notify DNS resolution
354358
$onProgress();
355359
}
356360

357-
return $ip;
361+
return $host;
358362
}
359363

360364
/**

src/Symfony/Contracts/HttpClient/Test/HttpClientTestCase.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,18 @@ public function testIdnResolve()
735735
$this->assertSame(200, $response->getStatusCode());
736736
}
737737

738+
public function testIPv6Resolve()
739+
{
740+
TestHttpServer::start(8087, '[::1]');
741+
742+
$client = $this->getHttpClient(__FUNCTION__);
743+
$response = $client->request('GET', 'http://symfony.com:8087/', [
744+
'resolve' => ['symfony.com' => '::1'],
745+
]);
746+
747+
$this->assertSame(200, $response->getStatusCode());
748+
}
749+
738750
public function testNotATimeout()
739751
{
740752
$client = $this->getHttpClient(__FUNCTION__);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy