Handle IPv6 in connection URLs
Previously IPv6 addresses were wrongly split and lead to a parse error. This commit fixes the behavior.
This commit is contained in:
@@ -399,13 +399,19 @@ func parseURLSettings(connString string) (map[string]string, error) {
|
|||||||
var hosts []string
|
var hosts []string
|
||||||
var ports []string
|
var ports []string
|
||||||
for _, host := range strings.Split(url.Host, ",") {
|
for _, host := range strings.Split(url.Host, ",") {
|
||||||
parts := strings.SplitN(host, ":", 2)
|
if host == "" {
|
||||||
if parts[0] != "" {
|
continue
|
||||||
hosts = append(hosts, parts[0])
|
|
||||||
}
|
}
|
||||||
if len(parts) == 2 {
|
if isIPOnly(host) {
|
||||||
ports = append(ports, parts[1])
|
hosts = append(hosts, strings.Trim(host, "[]"))
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
h, p, err := net.SplitHostPort(host)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Errorf("failed to split host:port in '%s', err: %w", host, err)
|
||||||
|
}
|
||||||
|
hosts = append(hosts, h)
|
||||||
|
ports = append(ports, p)
|
||||||
}
|
}
|
||||||
if len(hosts) > 0 {
|
if len(hosts) > 0 {
|
||||||
settings["host"] = strings.Join(hosts, ",")
|
settings["host"] = strings.Join(hosts, ",")
|
||||||
@@ -426,6 +432,10 @@ func parseURLSettings(connString string) (map[string]string, error) {
|
|||||||
return settings, nil
|
return settings, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isIPOnly(host string) bool {
|
||||||
|
return net.ParseIP(strings.Trim(host, "[]")) != nil || !strings.Contains(host, ":")
|
||||||
|
}
|
||||||
|
|
||||||
var asciiSpace = [256]uint8{'\t': 1, '\n': 1, '\v': 1, '\f': 1, '\r': 1, ' ': 1}
|
var asciiSpace = [256]uint8{'\t': 1, '\n': 1, '\v': 1, '\f': 1, '\r': 1, ' ': 1}
|
||||||
|
|
||||||
func parseDSNSettings(s string) (map[string]string, error) {
|
func parseDSNSettings(s string) (map[string]string, error) {
|
||||||
|
|||||||
+41
-5
@@ -127,11 +127,11 @@ func TestParseConfig(t *testing.T) {
|
|||||||
name: "sslmode verify-ca",
|
name: "sslmode verify-ca",
|
||||||
connString: "postgres://jack:secret@localhost:5432/mydb?sslmode=verify-ca",
|
connString: "postgres://jack:secret@localhost:5432/mydb?sslmode=verify-ca",
|
||||||
config: &pgconn.Config{
|
config: &pgconn.Config{
|
||||||
User: "jack",
|
User: "jack",
|
||||||
Password: "secret",
|
Password: "secret",
|
||||||
Host: "localhost",
|
Host: "localhost",
|
||||||
Port: 5432,
|
Port: 5432,
|
||||||
Database: "mydb",
|
Database: "mydb",
|
||||||
TLSConfig: &tls.Config{
|
TLSConfig: &tls.Config{
|
||||||
InsecureSkipVerify: true,
|
InsecureSkipVerify: true,
|
||||||
},
|
},
|
||||||
@@ -228,6 +228,42 @@ func TestParseConfig(t *testing.T) {
|
|||||||
RuntimeParams: map[string]string{},
|
RuntimeParams: map[string]string{},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "database url IPv4 with port",
|
||||||
|
connString: "postgresql://jack@127.0.0.1:5433/mydb?sslmode=disable",
|
||||||
|
config: &pgconn.Config{
|
||||||
|
User: "jack",
|
||||||
|
Host: "127.0.0.1",
|
||||||
|
Port: 5433,
|
||||||
|
Database: "mydb",
|
||||||
|
TLSConfig: nil,
|
||||||
|
RuntimeParams: map[string]string{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "database url IPv6 with port",
|
||||||
|
connString: "postgresql://jack@[2001:db8::1]:5433/mydb?sslmode=disable",
|
||||||
|
config: &pgconn.Config{
|
||||||
|
User: "jack",
|
||||||
|
Host: "2001:db8::1",
|
||||||
|
Port: 5433,
|
||||||
|
Database: "mydb",
|
||||||
|
TLSConfig: nil,
|
||||||
|
RuntimeParams: map[string]string{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "database url IPv6 no port",
|
||||||
|
connString: "postgresql://jack@[2001:db8::1]/mydb?sslmode=disable",
|
||||||
|
config: &pgconn.Config{
|
||||||
|
User: "jack",
|
||||||
|
Host: "2001:db8::1",
|
||||||
|
Port: 5432,
|
||||||
|
Database: "mydb",
|
||||||
|
TLSConfig: nil,
|
||||||
|
RuntimeParams: map[string]string{},
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "DSN everything",
|
name: "DSN everything",
|
||||||
connString: "user=jack password=secret host=localhost port=5432 dbname=mydb sslmode=disable application_name=pgxtest search_path=myschema",
|
connString: "user=jack password=secret host=localhost port=5432 dbname=mydb sslmode=disable application_name=pgxtest search_path=myschema",
|
||||||
|
|||||||
Reference in New Issue
Block a user