Skip to content

Commit a32ebfa

Browse files
praswicaksononicolas-grekas
authored andcommitted
[HttpClient] fix amphp http client v5 unix socket
1 parent 3360eab commit a32ebfa

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

src/Symfony/Component/HttpClient/Internal/AmpClientStateV5.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Amp\Socket\ClientTlsContext;
2929
use Amp\Socket\ConnectContext;
3030
use Amp\Socket\DnsSocketConnector;
31+
use Amp\Socket\InternetAddress;
3132
use Amp\Socket\Socket;
3233
use Amp\Socket\SocketAddress;
3334
use Amp\Socket\SocketConnector;
@@ -160,7 +161,7 @@ public function connect(SocketAddress|string $uri, ?ConnectContext $context = nu
160161

161162
if ($options['proxy']) {
162163
$proxyUrl = parse_url($options['proxy']['url']);
163-
$proxySocket = new SocketAddress($proxyUrl['host'], $proxyUrl['port']);
164+
$proxySocket = new InternetAddress($proxyUrl['host'], $proxyUrl['port']);
164165
$proxyHeaders = $options['proxy']['auth'] ? ['Proxy-Authorization' => $options['proxy']['auth']] : [];
165166

166167
if ('ssl' === $proxyUrl['scheme']) {

src/Symfony/Component/HttpClient/Internal/AmpListenerV5.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Amp\Http\Client\NetworkInterceptor;
1919
use Amp\Http\Client\Request;
2020
use Amp\Http\Client\Response;
21+
use Amp\Socket\InternetAddress;
2122
use Symfony\Component\HttpClient\Exception\TransportException;
2223

2324
/**
@@ -66,14 +67,18 @@ public function connectionAcquired(Request $request, Connection $connection, int
6667

6768
public function requestHeaderStart(Request $request, Stream $stream): void
6869
{
69-
$host = $stream->getRemoteAddress()->getAddress();
70+
$host = $stream->getRemoteAddress()->toString();
71+
if ($stream->getRemoteAddress() instanceof InternetAddress) {
72+
$host = $stream->getRemoteAddress()->getAddress();
73+
$this->info['primary_port'] = $stream->getRemoteAddress()->getPort();
74+
}
75+
7076
$this->info['primary_ip'] = $host;
7177

7278
if (str_contains($host, ':')) {
7379
$host = '['.$host.']';
7480
}
7581

76-
$this->info['primary_port'] = $stream->getRemoteAddress()->getPort();
7782
$this->info['pretransfer_time'] = microtime(true) - $this->info['start_time'];
7883
$this->info['debug'] .= \sprintf("* Connected to %s (%s) port %d\n", $request->getUri()->getHost(), $host, $this->info['primary_port']);
7984

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

+20
Original file line numberDiff line numberDiff line change
@@ -700,4 +700,24 @@ public function testPostToGetRedirect(int $status)
700700
$this->assertSame('GET', $body['REQUEST_METHOD']);
701701
$this->assertSame('/', $body['REQUEST_URI']);
702702
}
703+
704+
public function testUnixSocket()
705+
{
706+
if (!file_exists('/var/run/docker.sock')) {
707+
$this->markTestSkipped('Docker socket not found.');
708+
}
709+
710+
$client = $this->getHttpClient(__FUNCTION__)
711+
->withOptions([
712+
'base_uri' => 'http://docker',
713+
'bindto' => '/run/docker.sock',
714+
]);
715+
716+
$response = $client->request('GET', '/info');
717+
718+
$this->assertSame(200, $response->getStatusCode());
719+
720+
$info = $response->getInfo();
721+
$this->assertSame('/run/docker.sock', $info['primary_ip']);
722+
}
703723
}

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

+5
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,11 @@ public function testHttp2PushVulcainWithUnusedResponse()
505505
$this->markTestSkipped('MockHttpClient doesn\'t support HTTP/2 PUSH.');
506506
}
507507

508+
public function testUnixSocket()
509+
{
510+
$this->markTestSkipped('MockHttpClient doesn\'t support binding to unix sockets.');
511+
}
512+
508513
public function testChangeResponseFactory()
509514
{
510515
/* @var MockHttpClient $client */

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

+5
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,9 @@ public function testHttp2PushVulcainWithUnusedResponse()
4848
{
4949
$this->markTestSkipped('NativeHttpClient doesn\'t support HTTP/2.');
5050
}
51+
52+
public function testUnixSocket()
53+
{
54+
$this->markTestSkipped('NativeHttpClient doesn\'t support binding to unix sockets.');
55+
}
5156
}

0 commit comments

Comments
 (0)