-
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathDnsResolvingHttpClient.php
More file actions
97 lines (80 loc) · 3.3 KB
/
DnsResolvingHttpClient.php
File metadata and controls
97 lines (80 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpClient;
use Symfony\Component\HttpClient\Internal\FollowRedirectsTrait;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Symfony\Contracts\Service\ResetInterface;
/**
* Decorator that resolves host names using a custom resolver before delegating to the transport.
*
* The resolver is called for the requested host and for the host of every followed redirect. When it
* returns an IP address, the result is injected into the "resolve" option so that the transport connects
* to that IP without performing its own DNS resolution. When it returns null, the transport's default
* DNS resolution is used. Hosts that are already in the "resolve" option or that are IP addresses are
* not passed to it.
*
* Note that using this decorator opts out of the asynchronous and cached DNS resolution that the curl
* and amphp transports provide; the resolver is responsible for any caching it needs.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
final class DnsResolvingHttpClient implements HttpClientInterface, ResetInterface
{
use AsyncDecoratorTrait;
use FollowRedirectsTrait;
use HttpClientTrait;
private array $defaultOptions = self::OPTIONS_DEFAULTS;
/** @var callable(string): ?string */
private $resolver;
/**
* @param callable(string $host): ?string $resolver Returns the IP address the given host name should resolve to,
* or null to let the transport perform its default DNS resolution
*/
public function __construct(
private HttpClientInterface $client,
callable $resolver,
) {
$this->resolver = $resolver;
}
public function request(string $method, string $url, array $options = []): ResponseInterface
{
[$url, $options] = self::prepareRequest($method, $url, $options, $this->defaultOptions, true);
$host = parse_url($url['authority'], \PHP_URL_HOST);
$url = implode('', $url);
$resolver = $this->resolver;
$resolve = static function (string $host, string $url, array &$options) use ($resolver): void {
if (isset($options['resolve'][$host]) || false !== filter_var(trim($host, '[]'), \FILTER_VALIDATE_IP)) {
return;
}
if (null !== $ip = $resolver($host)) {
$options['resolve'][$host] = $ip;
}
};
$resolve($host, $url, $options);
return $this->followRedirects($method, $url, $host, $options, $resolve);
}
public function withOptions(array $options): static
{
$clone = clone $this;
$clone->client = $this->client->withOptions($options);
$clone->defaultOptions = self::mergeDefaultOptions($options, $this->defaultOptions);
return $clone;
}
public function reset(): void
{
if ($this->resolver instanceof ResetInterface) {
$this->resolver->reset();
}
if ($this->client instanceof ResetInterface) {
$this->client->reset();
}
}
}