Skip to content

Commit a7ad4bf

Browse files
committed
Use parse_url() to check for port
1 parent 496d392 commit a7ad4bf

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

src/React/Dns/Resolver/Factory.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,15 @@ protected function createCachedExecutor(LoopInterface $loop)
4646

4747
protected function addPortToServerIfMissing($nameserver)
4848
{
49-
$colon = strrpos($nameserver, ':');
50-
51-
// there is no colon at all or the last one does not have a closing IPv6 bracket right before it
52-
if ($colon === false || (strpos($nameserver, ':') !== $colon && strpos($nameserver, ']') !== ($colon - 1))) {
53-
if (strpos($nameserver, ':') !== $colon) {
54-
// several colons => enclose IPv6 address in square brackets
55-
$nameserver = '[' . $nameserver . ']';
56-
}
49+
if (strpos($nameserver, '[') === false && substr_count($nameserver, ':') >= 2) {
50+
// several colons, but not enclosed in square brackets => enclose IPv6 address in square brackets
51+
$nameserver = '[' . $nameserver . ']';
52+
}
53+
// assume a dummy scheme when checking for the port, otherwise parse_url() fails
54+
if (parse_url('dummy://' . $nameserver, PHP_URL_PORT) === null) {
5755
$nameserver .= ':53';
5856
}
57+
5958
return $nameserver;
6059
}
6160
}

0 commit comments

Comments
 (0)