From 45fa6b8f244e6d399fd2082761436cb905e878e3 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Thu, 17 Sep 2020 00:10:08 +0200 Subject: [PATCH] [HttpClient] Support for CURLOPT_LOCALPORT. --- .../Component/HttpClient/CurlHttpClient.php | 9 ++++++++- .../HttpClient/Tests/CurlHttpClientTest.php | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/HttpClient/CurlHttpClient.php b/src/Symfony/Component/HttpClient/CurlHttpClient.php index 8a87e29d3f..9cb7fb5295 100644 --- a/src/Symfony/Component/HttpClient/CurlHttpClient.php +++ b/src/Symfony/Component/HttpClient/CurlHttpClient.php @@ -267,7 +267,14 @@ final class CurlHttpClient implements HttpClientInterface, LoggerAwareInterface, } if ($options['bindto']) { - $curlopts[file_exists($options['bindto']) ? \CURLOPT_UNIX_SOCKET_PATH : \CURLOPT_INTERFACE] = $options['bindto']; + if (file_exists($options['bindto'])) { + $curlopts[\CURLOPT_UNIX_SOCKET_PATH] = $options['bindto']; + } elseif (preg_match('/^(.*):(\d+)$/', $options['bindto'], $matches)) { + $curlopts[\CURLOPT_INTERFACE] = $matches[1]; + $curlopts[\CURLOPT_LOCALPORT] = $matches[2]; + } else { + $curlopts[\CURLOPT_INTERFACE] = $options['bindto']; + } } if (0 < $options['max_duration']) { diff --git a/src/Symfony/Component/HttpClient/Tests/CurlHttpClientTest.php b/src/Symfony/Component/HttpClient/Tests/CurlHttpClientTest.php index 45362b1195..6793818d55 100644 --- a/src/Symfony/Component/HttpClient/Tests/CurlHttpClientTest.php +++ b/src/Symfony/Component/HttpClient/Tests/CurlHttpClientTest.php @@ -35,6 +35,21 @@ class CurlHttpClientTest extends HttpClientTestCase return new CurlHttpClient(); } + 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']); + } + /** * @requires PHP 7.2.17 */