Skip to content

Support IPv6 in host config #2595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,17 @@ protected function getHostDsn(array $config): string
$hosts = is_array($config['host']) ? $config['host'] : [$config['host']];

foreach ($hosts as &$host) {
// Check if we need to add a port to the host
if (strpos($host, ':') === false && ! empty($config['port'])) {
$host = $host.':'.$config['port'];
// ipv6
if (filter_var($host, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) {
$host = '['.$host.']';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to check that libmongoc supports this notation, and it is handled in _mongoc_host_list_from_string_with_err.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did test before doing the PR 😉

if (! empty($config['port'])) {
$host = $host.':'.$config['port'];
}
} else {
// Check if we need to add a port to the host
if (! str_contains($host, ':') && ! empty($config['port'])) {
$host = $host.':'.$config['port'];
}
}
}

Expand Down
48 changes: 48 additions & 0 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,54 @@ public function dataConnectionConfig(): Generator
],
];

yield 'IPv4' => [
'expectedUri' => 'mongodb://1.2.3.4',
'expectedDatabaseName' => 'tests',
'config' => [
'host' => '1.2.3.4',
'database' => 'tests',
],
];

yield 'IPv4 and port' => [
'expectedUri' => 'mongodb://1.2.3.4:1234',
'expectedDatabaseName' => 'tests',
'config' => [
'host' => '1.2.3.4',
'port' => 1234,
'database' => 'tests',
],
];

yield 'IPv6' => [
'expectedUri' => 'mongodb://[2001:db8:3333:4444:5555:6666:7777:8888]',
'expectedDatabaseName' => 'tests',
'config' => [
'host' => '2001:db8:3333:4444:5555:6666:7777:8888',
'database' => 'tests',
],
];

yield 'IPv6 and port' => [
'expectedUri' => 'mongodb://[2001:db8:3333:4444:5555:6666:7777:8888]:1234',
'expectedDatabaseName' => 'tests',
'config' => [
'host' => '2001:db8:3333:4444:5555:6666:7777:8888',
'port' => 1234,
'database' => 'tests',
],
];

yield 'multiple IPv6' => [
'expectedUri' => 'mongodb://[::1],[2001:db8::1:0:0:1]',
'expectedDatabaseName' => 'tests',
'config' => [
'host' => ['::1', '2001:db8::1:0:0:1'],
'port' => null,
'database' => 'tests',
],
];

yield 'Port in host name takes precedence' => [
'expectedUri' => 'mongodb://some-host:12345',
'expectedDatabaseName' => 'tests',
Expand Down