Merge pull request #9432 from nextcloud/backport/9225/ipv6-database-host

[stable13] Allow IPv6 database host
This commit is contained in:
Morris Jobke 2018-05-14 13:36:21 +02:00 committed by GitHub
commit 003cc5acfd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 80 additions and 10 deletions

View File

@ -193,16 +193,7 @@ class ConnectionFactory {
$connectionParams['path'] = $dataDir . '/' . $name . '.db'; $connectionParams['path'] = $dataDir . '/' . $name . '.db';
} else { } else {
$host = $this->config->getValue('dbhost', ''); $host = $this->config->getValue('dbhost', '');
if (strpos($host, ':')) { $connectionParams = array_merge($connectionParams, $this->splitHostFromPortAndSocket($host));
// Host variable may carry a port or socket.
list($host, $portOrSocket) = explode(':', $host, 2);
if (ctype_digit($portOrSocket)) {
$connectionParams['port'] = $portOrSocket;
} else {
$connectionParams['unix_socket'] = $portOrSocket;
}
}
$connectionParams['host'] = $host;
$connectionParams['dbname'] = $name; $connectionParams['dbname'] = $name;
} }
@ -232,4 +223,27 @@ class ConnectionFactory {
return $connectionParams; return $connectionParams;
} }
/**
* @param string $host
* @return array
*/
protected function splitHostFromPortAndSocket($host) {
$params = [
'host' => $host,
];
$matches = [];
if (preg_match('/^(.*):([^\]:]+)$/', $host, $matches)) {
// Host variable carries a port or socket.
$params['host'] = $matches[1];
if (is_numeric($matches[2])) {
$params['port'] = (int) $matches[2];
} else {
$params['unix_socket'] = $matches[2];
}
}
return $params;
}
} }

View File

@ -0,0 +1,56 @@
<?php
/**
* @copyright Copyright (c) 2018 Joas Schilling <coding@schilljs.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace Test\DB;
use OC\DB\ConnectionFactory;
use OC\SystemConfig;
use Test\TestCase;
class ConnectionFactoryTest extends TestCase {
public function splitHostFromPortAndSocketData() {
return [
['127.0.0.1', ['host' => '127.0.0.1']],
['db.example.org', ['host' => 'db.example.org']],
['unix', ['host' => 'unix']],
['[::1]', ['host' => '[::1]']],
['127.0.0.1:3306', ['host' => '127.0.0.1', 'port' => 3306]],
['db.example.org:3306', ['host' => 'db.example.org', 'port' => 3306]],
['unix:3306', ['host' => 'unix', 'port' => 3306]],
['[::1]:3306', ['host' => '[::1]', 'port' => 3306]],
['unix:/socket', ['host' => 'unix', 'unix_socket' => '/socket']],
];
}
/**
* @dataProvider splitHostFromPortAndSocketData
* @param string $host
* @param array $expected
*/
public function testSplitHostFromPortAndSocket($host, array $expected) {
/** @var SystemConfig $config */
$config = $this->createMock(SystemConfig::class);
$factory = new ConnectionFactory($config);
$this->assertEquals($expected, self::invokePrivate($factory, 'splitHostFromPortAndSocket', [$host]));
}
}