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 option "bindto" with IPv6 addresses #58914

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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion 2 src/Symfony/Component/HttpClient/CurlHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public function request(string $method, string $url, array $options = []): Respo
if (file_exists($options['bindto'])) {
$curlopts[\CURLOPT_UNIX_SOCKET_PATH] = $options['bindto'];
} elseif (!str_starts_with($options['bindto'], 'if!') && preg_match('/^(.*):(\d+)$/', $options['bindto'], $matches)) {
$curlopts[\CURLOPT_INTERFACE] = $matches[1];
$curlopts[\CURLOPT_INTERFACE] = trim($matches[1], '[]');
$curlopts[\CURLOPT_LOCALPORT] = $matches[2];
} else {
$curlopts[\CURLOPT_INTERFACE] = $options['bindto'];
Expand Down
4 changes: 3 additions & 1 deletion 4 src/Symfony/Component/HttpClient/NativeHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,9 @@ private static function dnsResolve($host, NativeClientState $multi, array &$info
$info['debug'] .= "* Hostname was NOT found in DNS cache\n";
$now = microtime(true);

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

Expand Down
15 changes: 0 additions & 15 deletions 15 src/Symfony/Component/HttpClient/Tests/CurlHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,6 @@ protected function getHttpClient(string $testCase): HttpClientInterface
return new CurlHttpClient(['verify_peer' => false, 'verify_host' => false], 6, 50);
}

public function testBindToPort()
{
$client = $this->getHttpClient(__FUNCTION__);
$response = $client->request('GET', 'http://localhost:8057', ['bindto' => '127.0.0.1:9876']);
$response->getStatusCode();

$r = new \ReflectionProperty($response, 'handle');
$r->setAccessible(true);

$curlInfo = curl_getinfo($r->getValue($response));

self::assertSame('127.0.0.1', $curlInfo['local_ip']);
self::assertSame(9876, $curlInfo['local_port']);
}

public function testTimeoutIsNotAFatalError()
{
if ('\\' === \DIRECTORY_SEPARATOR) {
Expand Down
32 changes: 19 additions & 13 deletions 32 src/Symfony/Contracts/HttpClient/Test/Fixtures/web/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,26 @@
$_POST['content-type'] = $_SERVER['HTTP_CONTENT_TYPE'] ?? '?';
}

$headers = [
'SERVER_PROTOCOL',
'SERVER_NAME',
'REQUEST_URI',
'REQUEST_METHOD',
'PHP_AUTH_USER',
'PHP_AUTH_PW',
'REMOTE_ADDR',
'REMOTE_PORT',
];

foreach ($headers as $k) {
if (isset($_SERVER[$k])) {
$vars[$k] = $_SERVER[$k];
}
}

foreach ($_SERVER as $k => $v) {
switch ($k) {
default:
if (0 !== strpos($k, 'HTTP_')) {
continue 2;
}
// no break
case 'SERVER_NAME':
case 'SERVER_PROTOCOL':
case 'REQUEST_URI':
case 'REQUEST_METHOD':
case 'PHP_AUTH_USER':
case 'PHP_AUTH_PW':
$vars[$k] = $v;
if (0 === strpos($k, 'HTTP_')) {
$vars[$k] = $v;
}
}

Expand Down
27 changes: 27 additions & 0 deletions 27 src/Symfony/Contracts/HttpClient/Test/HttpClientTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static function tearDownAfterClass(): void
{
TestHttpServer::stop(8067);
TestHttpServer::stop(8077);
TestHttpServer::stop(8087);
}

abstract protected function getHttpClient(string $testCase): HttpClientInterface;
Expand Down Expand Up @@ -1152,4 +1153,30 @@ public function testWithOptions()
$response = $client2->request('GET', '/');
$this->assertSame(200, $response->getStatusCode());
}

public function testBindToPort()
{
$client = $this->getHttpClient(__FUNCTION__);
$response = $client->request('GET', 'http://localhost:8057', ['bindto' => '127.0.0.1:9876']);
$response->getStatusCode();

$vars = $response->toArray();

self::assertSame('127.0.0.1', $vars['REMOTE_ADDR']);
self::assertSame('9876', $vars['REMOTE_PORT']);
}

public function testBindToPortV6()
{
TestHttpServer::start(8087, '[::1]');

$client = $this->getHttpClient(__FUNCTION__);
$response = $client->request('GET', 'http://[::1]:8087', ['bindto' => '[::1]:9876']);
$response->getStatusCode();

$vars = $response->toArray();

self::assertSame('::1', $vars['REMOTE_ADDR']);
self::assertSame('9876', $vars['REMOTE_PORT']);
}
}
6 changes: 3 additions & 3 deletions 6 src/Symfony/Contracts/HttpClient/Test/TestHttpServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TestHttpServer
/**
* @return Process
*/
public static function start(int $port = 8057)
public static function start(int $port = 8057, $ip = '127.0.0.1')
{
if (isset(self::$process[$port])) {
self::$process[$port]->stop();
Expand All @@ -32,14 +32,14 @@ public static function start(int $port = 8057)
}

$finder = new PhpExecutableFinder();
$process = new Process(array_merge([$finder->find(false)], $finder->findArguments(), ['-dopcache.enable=0', '-dvariables_order=EGPCS', '-S', '127.0.0.1:'.$port]));
$process = new Process(array_merge([$finder->find(false)], $finder->findArguments(), ['-dopcache.enable=0', '-dvariables_order=EGPCS', '-S', $ip.':'.$port]));
$process->setWorkingDirectory(__DIR__.'/Fixtures/web');
$process->start();
self::$process[$port] = $process;

do {
usleep(50000);
} while (!@fopen('http://127.0.0.1:'.$port, 'r'));
} while (!@fopen('http://'.$ip.':'.$port, 'r'));

return $process;
}
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.