[HttpClient] fix binding to network interfaces

This commit is contained in:
Nicolas Grekas 2020-11-28 14:14:15 +01:00
parent fe36f35d9b
commit faa1fd32f9
2 changed files with 11 additions and 3 deletions

View File

@ -269,7 +269,7 @@ final class CurlHttpClient implements HttpClientInterface, LoggerAwareInterface,
if ($options['bindto']) {
if (file_exists($options['bindto'])) {
$curlopts[\CURLOPT_UNIX_SOCKET_PATH] = $options['bindto'];
} elseif (preg_match('/^(.*):(\d+)$/', $options['bindto'], $matches)) {
} elseif (0 !== strpos($options['bindto'], 'if!') && preg_match('/^(.*):(\d+)$/', $options['bindto'], $matches)) {
$curlopts[\CURLOPT_INTERFACE] = $matches[1];
$curlopts[\CURLOPT_LOCALPORT] = $matches[2];
} else {

View File

@ -67,8 +67,16 @@ final class NativeHttpClient implements HttpClientInterface, LoggerAwareInterfac
{
[$url, $options] = self::prepareRequest($method, $url, $options, $this->defaultOptions);
if ($options['bindto'] && file_exists($options['bindto'])) {
throw new TransportException(__CLASS__.' cannot bind to local Unix sockets, use e.g. CurlHttpClient instead.');
if ($options['bindto']) {
if (file_exists($options['bindto'])) {
throw new TransportException(__CLASS__.' cannot bind to local Unix sockets, use e.g. CurlHttpClient instead.');
}
if (0 === strpos($options['bindto'], 'if!')) {
throw new TransportException(__CLASS__.' cannot bind to network interfaces, use e.g. CurlHttpClient instead.');
}
if (0 === strpos($options['bindto'], 'host!')) {
$options['bindto'] = substr($options['bindto'], 5);
}
}
$options['body'] = self::getBodyAsString($options['body']);