@@ -80,6 +80,9 @@ public function request(string $method, string $url, array $options = []): Respo
80
80
if (str_starts_with ($ options ['bindto ' ], 'host! ' )) {
81
81
$ options ['bindto ' ] = substr ($ options ['bindto ' ], 5 );
82
82
}
83
+ if ((\PHP_VERSION_ID < 80223 || 80300 <= \PHP_VERSION_ID && 80311 < \PHP_VERSION_ID ) && '\\' === \DIRECTORY_SEPARATOR && '[ ' === $ options ['bindto ' ][0 ]) {
84
+ $ options ['bindto ' ] = preg_replace ('{^\[[^\]]++\]} ' , '[$0] ' , $ options ['bindto ' ]);
85
+ }
83
86
}
84
87
85
88
$ hasContentLength = isset ($ options ['normalized_headers ' ]['content-length ' ]);
@@ -334,29 +337,35 @@ private static function parseHostPort(array $url, array &$info): array
334
337
*/
335
338
private static function dnsResolve (string $ host , NativeClientState $ multi , array &$ info , ?\Closure $ onProgress ): string
336
339
{
337
- if (null === $ ip = $ multi ->dnsCache [$ host ] ?? null ) {
340
+ $ flag = '' !== $ host && '[ ' === $ host [0 ] && '] ' === $ host [-1 ] && str_contains ($ host , ': ' ) ? \FILTER_FLAG_IPV6 : \FILTER_FLAG_IPV4 ;
341
+ $ ip = \FILTER_FLAG_IPV6 === $ flag ? substr ($ host , 1 , -1 ) : $ host ;
342
+
343
+ if (filter_var ($ ip , \FILTER_VALIDATE_IP , $ flag )) {
344
+ // The host is already an IP address
345
+ } elseif (null === $ ip = $ multi ->dnsCache [$ host ] ?? null ) {
338
346
$ info ['debug ' ] .= "* Hostname was NOT found in DNS cache \n" ;
339
347
$ now = microtime (true );
340
348
341
349
if (!$ ip = gethostbynamel ($ host )) {
342
350
throw new TransportException (sprintf ('Could not resolve host "%s". ' , $ host ));
343
351
}
344
352
345
- $ info ['namelookup_time ' ] = microtime (true ) - ($ info ['start_time ' ] ?: $ now );
346
353
$ multi ->dnsCache [$ host ] = $ ip = $ ip [0 ];
347
354
$ info ['debug ' ] .= "* Added {$ host }:0: {$ ip } to DNS cache \n" ;
348
355
} else {
349
356
$ info ['debug ' ] .= "* Hostname was found in DNS cache \n" ;
357
+ $ host = str_contains ($ ip , ': ' ) ? "[ $ ip] " : $ ip ;
350
358
}
351
359
360
+ $ info ['namelookup_time ' ] = microtime (true ) - ($ info ['start_time ' ] ?: $ now );
352
361
$ info ['primary_ip ' ] = $ ip ;
353
362
354
363
if ($ onProgress ) {
355
364
// Notify DNS resolution
356
365
$ onProgress ();
357
366
}
358
367
359
- return $ ip ;
368
+ return $ host ;
360
369
}
361
370
362
371
/**
0 commit comments