fix ipv6 database host

This commit is contained in:
Robin Appelman 2026-03-04 15:03:57 +01:00
commit 70543db580
3 changed files with 54 additions and 9 deletions

View file

@ -348,14 +348,12 @@ fn split_host(host: &str) -> (&str, Option<u16>, Option<&str>) {
if host.starts_with('/') { if host.starts_with('/') {
return ("localhost", None, Some(host)); return ("localhost", None, Some(host));
} }
let mut parts = host.split(':'); let (host, port_or_socket) = host.rsplit_once(':').unwrap_or((host, ""));
let host = parts.next().unwrap(); if port_or_socket.is_empty() {
match parts return (host, None, None);
.next() }
.map(|port_or_socket| u16::from_str(port_or_socket).map_err(|_| port_or_socket)) match u16::from_str(port_or_socket) {
{ Ok(port) => (host, Some(port), None),
Some(Ok(port)) => (host, Some(port), None), Err(_) => (host, None, Some(port_or_socket)),
Some(Err(socket)) => (host, None, Some(socket)),
None => (host, None, None),
} }
} }

View file

@ -721,3 +721,35 @@ fn test_parse_postgres_escaped_credentials() {
PgConnectOptions::from_str(&config.database.url()).unwrap(), PgConnectOptions::from_str(&config.database.url()).unwrap(),
); );
} }
#[test]
fn test_parse_ipv6_db() {
let config = config_from_file("tests/configs/ipv6_db.php");
assert_debug_equal(
&Database::MySql {
database: "nextcloud".to_string(),
username: "nextcloud".to_string(),
password: "secret".to_string(),
connect: DbConnect::Tcp {
host: "[::1]".to_string(),
port: 123,
},
ssl_options: SslOptions::Default,
},
&config.database,
);
assert_eq!(
config.database.url(),
"mysql://nextcloud:secret@[::1]:123/nextcloud"
);
assert_debug_equal(
MySqlConnectOptions::new()
.host("[::1]")
.username("nextcloud")
.password("secret")
.port(123)
.database("nextcloud"),
MySqlConnectOptions::from_str(&config.database.url()).unwrap(),
);
}

15
tests/configs/ipv6_db.php Normal file
View file

@ -0,0 +1,15 @@
<?php
$CONFIG = [
'overwrite.cli.url' => 'https://cloud.example.com',
'dbtype' => 'mysql',
'dbname' => 'nextcloud',
'dbhost' => '[::1]:123',
'dbport' => '',
'dbtableprefix' => 'oc_',
'dbuser' => 'nextcloud',
'dbpassword' => 'secret',
'redis' => [
'host' => 'localhost'
]
];