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