[HttpClient] Support for CURLOPT_LOCALPORT.

This commit is contained in:
Alexander M. Turek 2020-09-17 00:10:08 +02:00
parent cae08a0279
commit 45fa6b8f24
2 changed files with 23 additions and 1 deletions

View File

@ -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']) {

View File

@ -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
*/