Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3e9f770

Browse files
bug #39211 [HttpClient] fix binding to network interfaces (nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- [HttpClient] fix binding to network interfaces | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #38979 | License | MIT | Doc PR | - Fixes support for binding to local interfaces by adding support for [curl's `if!` prefix](https://curl.se/libcurl/c/CURLOPT_INTERFACE.html). Commits ------- faa1fd3 [HttpClient] fix binding to network interfaces
2 parents fe36f35 + faa1fd3 commit 3e9f770

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/Symfony/Component/HttpClient/CurlHttpClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ public function request(string $method, string $url, array $options = []): Respo
269269
if ($options['bindto']) {
270270
if (file_exists($options['bindto'])) {
271271
$curlopts[\CURLOPT_UNIX_SOCKET_PATH] = $options['bindto'];
272-
} elseif (preg_match('/^(.*):(\d+)$/', $options['bindto'], $matches)) {
272+
} elseif (0 !== strpos($options['bindto'], 'if!') && preg_match('/^(.*):(\d+)$/', $options['bindto'], $matches)) {
273273
$curlopts[\CURLOPT_INTERFACE] = $matches[1];
274274
$curlopts[\CURLOPT_LOCALPORT] = $matches[2];
275275
} else {

src/Symfony/Component/HttpClient/NativeHttpClient.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,16 @@ public function request(string $method, string $url, array $options = []): Respo
6767
{
6868
[$url, $options] = self::prepareRequest($method, $url, $options, $this->defaultOptions);
6969

70-
if ($options['bindto'] && file_exists($options['bindto'])) {
71-
throw new TransportException(__CLASS__.' cannot bind to local Unix sockets, use e.g. CurlHttpClient instead.');
70+
if ($options['bindto']) {
71+
if (file_exists($options['bindto'])) {
72+
throw new TransportException(__CLASS__.' cannot bind to local Unix sockets, use e.g. CurlHttpClient instead.');
73+
}
74+
if (0 === strpos($options['bindto'], 'if!')) {
75+
throw new TransportException(__CLASS__.' cannot bind to network interfaces, use e.g. CurlHttpClient instead.');
76+
}
77+
if (0 === strpos($options['bindto'], 'host!')) {
78+
$options['bindto'] = substr($options['bindto'], 5);
79+
}
7280
}
7381

7482
$options['body'] = self::getBodyAsString($options['body']);

0 commit comments

Comments
 (0)