Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[HttpClient] fix using proxies with NativeHttpClient #38375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 42 additions & 17 deletions 59 src/Symfony/Component/HttpClient/NativeHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public function request(string $method, string $url, array $options = []): Respo

$this->logger && $this->logger->info(sprintf('Request: "%s %s"', $method, implode('', $url)));

[$host, $port, $url['authority']] = self::dnsResolve($url, $this->multi, $info, $onProgress);
[$host, $port] = self::parseHostPort($url, $info);

if (!isset($options['normalized_headers']['host'])) {
$options['headers'][] = 'Host: '.$host.$port;
Expand All @@ -198,7 +198,6 @@ public function request(string $method, string $url, array $options = []): Respo
'follow_location' => false, // We follow redirects ourselves - the native logic is too limited
],
'ssl' => array_filter([
'peer_name' => $host,
'verify_peer' => $options['verify_peer'],
'verify_peer_name' => $options['verify_host'],
'cafile' => $options['cafile'],
Expand All @@ -225,7 +224,11 @@ public function request(string $method, string $url, array $options = []): Respo

$resolveRedirect = self::createRedirectResolver($options, $host, $proxy, $noProxy, $info, $onProgress);
$context = stream_context_create($context, ['notification' => $notification]);
self::configureHeadersAndProxy($context, $host, $options['headers'], $proxy, $noProxy, 'https:' === $url['scheme']);

if (!self::configureHeadersAndProxy($context, $host, $options['headers'], $proxy, $noProxy, 'https:' === $url['scheme'])) {
$ip = self::dnsResolve($host, $this->multi, $info, $onProgress);
$url['authority'] = substr_replace($url['authority'], $ip, -\strlen($host) - \strlen($port), \strlen($host));
}

return new NativeResponse($this->multi, $context, implode('', $url), $options, $info, $resolveRedirect, $onProgress, $this->logger);
}
Expand Down Expand Up @@ -306,9 +309,9 @@ private static function getProxy(?string $proxy, array $url): ?array
}

/**
* Resolves the IP of the host using the local DNS cache if possible.
* Extracts the host and the port from the URL.
*/
private static function dnsResolve(array $url, NativeClientState $multi, array &$info, ?\Closure $onProgress): array
private static function parseHostPort(array $url, array &$info): array
{
if ($port = parse_url($url['authority'], \PHP_URL_PORT) ?: '') {
$info['primary_port'] = $port;
Expand All @@ -317,8 +320,14 @@ private static function dnsResolve(array $url, NativeClientState $multi, array &
$info['primary_port'] = 'http:' === $url['scheme'] ? 80 : 443;
}

$host = parse_url($url['authority'], \PHP_URL_HOST);
return [parse_url($url['authority'], \PHP_URL_HOST), $port];
}

/**
* Resolves the IP of the host using the local DNS cache if possible.
*/
private static function dnsResolve($host, NativeClientState $multi, array &$info, ?\Closure $onProgress): string
{
if (null === $ip = $multi->dnsCache[$host] ?? null) {
$info['debug'] .= "* Hostname was NOT found in DNS cache\n";
$now = microtime(true);
Expand All @@ -341,7 +350,7 @@ private static function dnsResolve(array $url, NativeClientState $multi, array &
$onProgress();
}

return [$host, $port, substr_replace($url['authority'], $ip, -\strlen($host) - \strlen($port), \strlen($host))];
return $ip;
}

/**
Expand Down Expand Up @@ -404,24 +413,33 @@ private static function createRedirectResolver(array $options, string $host, ?ar
}
}

[$host, $port, $url['authority']] = self::dnsResolve($url, $multi, $info, $onProgress);
stream_context_set_option($context, 'ssl', 'peer_name', $host);
[$host, $port] = self::parseHostPort($url, $info);

if (false !== (parse_url($location, \PHP_URL_HOST) ?? false)) {
// Authorization and Cookie headers MUST NOT follow except for the initial host name
$requestHeaders = $redirectHeaders['host'] === $host ? $redirectHeaders['with_auth'] : $redirectHeaders['no_auth'];
$requestHeaders[] = 'Host: '.$host.$port;
self::configureHeadersAndProxy($context, $host, $requestHeaders, $proxy, $noProxy, 'https:' === $url['scheme']);
$dnsResolve = !self::configureHeadersAndProxy($context, $host, $requestHeaders, $proxy, $noProxy, 'https:' === $url['scheme']);
} else {
$dnsResolve = isset(stream_context_get_options($context)['ssl']['peer_name']);
}

if ($dnsResolve) {
$ip = self::dnsResolve($host, $multi, $info, $onProgress);
$url['authority'] = substr_replace($url['authority'], $ip, -\strlen($host) - \strlen($port), \strlen($host));
}

return implode('', $url);
};
}

private static function configureHeadersAndProxy($context, string $host, array $requestHeaders, ?array $proxy, array $noProxy, bool $isSsl)
private static function configureHeadersAndProxy($context, string $host, array $requestHeaders, ?array $proxy, array $noProxy, bool $isSsl): bool
{
if (null === $proxy) {
return stream_context_set_option($context, 'http', 'header', $requestHeaders);
stream_context_set_option($context, 'http', 'header', $requestHeaders);
stream_context_set_option($context, 'ssl', 'peer_name', $host);

return false;
}

// Matching "no_proxy" should follow the behavior of curl
Expand All @@ -430,17 +448,24 @@ private static function configureHeadersAndProxy($context, string $host, array $
$dotRule = '.'.ltrim($rule, '.');

if ('*' === $rule || $host === $rule || substr($host, -\strlen($dotRule)) === $dotRule) {
return stream_context_set_option($context, 'http', 'header', $requestHeaders);
stream_context_set_option($context, 'http', 'proxy', null);
stream_context_set_option($context, 'http', 'request_fulluri', false);
stream_context_set_option($context, 'http', 'header', $requestHeaders);
stream_context_set_option($context, 'ssl', 'peer_name', $host);

return false;
}
}

stream_context_set_option($context, 'http', 'proxy', $proxy['url']);
stream_context_set_option($context, 'http', 'request_fulluri', !$isSsl);

if (null !== $proxy['auth']) {
$requestHeaders[] = 'Proxy-Authorization: '.$proxy['auth'];
}

return stream_context_set_option($context, 'http', 'header', $requestHeaders);
stream_context_set_option($context, 'http', 'proxy', $proxy['url']);
stream_context_set_option($context, 'http', 'request_fulluri', !$isSsl);
stream_context_set_option($context, 'http', 'header', $requestHeaders);
stream_context_set_option($context, 'ssl', 'peer_name', null);

return true;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.