8000 bug #38215 [HttpClient] Support for CURLOPT_LOCALPORT (derrabus) · symfony/symfony@ad22618 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit ad22618

Browse files
committed
bug #38215 [HttpClient] Support for CURLOPT_LOCALPORT (derrabus)
This PR was merged into the 4.4 branch. Discussion ---------- [HttpClient] Support for CURLOPT_LOCALPORT | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | #38081 (comment) | License | MIT | Doc PR | N/A Commits ------- 45fa6b8 [HttpClient] Support for CURLOPT_LOCALPORT.
2 parents c965be8 + 45fa6b8 commit ad22618

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/Symfony/Component/HttpClient/CurlHttpClient.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,14 @@ public function request(string $method, string $url, array $options = []): Respo
267267
}
268268

269269
if ($options['bindto']) {
270-
$curlopts[file_exists($options['bindto']) ? \CURLOPT_UNIX_SOCKET_PATH : \CURLOPT_INTERFACE] = $options['bindto'];
270+
if (file_exists($options['bindto'])) {
271+
$curlopts[\CURLOPT_UNIX_SOCKET_PATH] = $options['bindto'];
272+
} elseif (preg_match('/^(.*):(\d+)$/', $options['bindto'], $matches)) {
273+
$curlopts[\CURLOPT_INTERFACE] = $matches[1];
274+
$curlopts[\CURLOPT_LOCALPORT] = $matches[2];
275+
} else {
276+
$curlopts[\CURLOPT_INTERFACE] = $options['bindto'];
277+
}
271278
}
272279

273280
if (0 < $options['max_duration']) {

src/Symfony/Component/HttpClient/Tests/CurlHttpClientTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ protected function getHttpClient(string $testCase): HttpClientInterface
3535
return new CurlHttpClient();
3636
}
3737

38+
public function testBindToPort()
39+
{
40+
$client = $this->getHttpClient(__FUNCTION__);
41+
$response = $client->request('GET', 'http://localhost:8057', ['bindto' => '127.0.0.1:9876']);
42+
$response->getStatusCode();
43+
44+
$r = new \ReflectionProperty($response, 'handle');
45+
$r->setAccessible(true);
46+
47+
$curlInfo = curl_getinfo($r->getValue($response));
48+
49+
self::assertSame('127.0.0.1', $curlInfo['local_ip']);
50+
self::assertSame(9876, $curlInfo['local_port']);
51+
}
52+
3853
/**
3954
* @requires PHP 7.2.17
4055
*/

0 commit comments

Comments
 (0)
0