Skip to content

Commit 474b87c

Browse files
Merge branch '5.1' into 5.2
* 5.1: [HttpClient] fix binding to network interfaces [HttpClient] fix binding to network interfaces
2 parents dda8496 + 3717cbb commit 474b87c

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

AmpHttpClient.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ public function request(string $method, string $url, array $options = []): Respo
8282
throw new \LogicException('You cannot use the "proxy" option as the "amphp/http-tunnel" package is not installed. Try running "composer require amphp/http-tunnel".');
8383
}
8484

85+
if ($options['bindto']) {
86+
if (0 === strpos($options['bindto'], 'if!')) {
87+
throw new TransportException(__CLASS__.' cannot bind to network interfaces, use e.g. CurlHttpClient instead.');
88+
}
89+
if (0 === strpos($options['bindto'], 'host!')) {
90+
$options['bindto'] = substr($options['bindto'], 5);
91+
}
92+
}
93+
8594
if ('' !== $options['body'] && 'POST' === $method && !isset($options['normalized_headers']['content-type'])) {
8695
$options['headers'][] = 'Content-Type: application/x-www-form-urlencoded';
8796
}
@@ -141,7 +150,7 @@ public function stream($responses, float $timeout = null): ResponseStreamInterfa
141150
if ($responses instanceof AmpResponse) {
142151
$responses = [$responses];
143152
} elseif (!is_iterable($responses)) {
144-
throw new \TypeError(sprintf('%s() expects parameter 1 to be an iterable of AmpResponse objects, %s given.', __METHOD__, get_debug_type($responses)));
153+
throw new \TypeError(sprintf('"%s()" expects parameter 1 to be an iterable of AmpResponse objects, "%s" given.', __METHOD__, get_debug_type($responses)));
145154
}
146155

147156
return new ResponseStream(AmpResponse::stream($responses, $timeout));

CurlHttpClient.php

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

Internal/AmpClientState.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function request(array $options, Request $request, CancellationToken $can
119119
private function getClient(array $options): array
120120
{
121121
$options = [
122-
'bindto' => $options['bindto'] ?: '0',
122+
'bindto' => $options['bindto'] ?: '0:0',
123123
'verify_peer' => $options['verify_peer'],
124124
'capath' => $options['capath'],
125125
'cafile' => $options['cafile'],

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)