2
0

Merge pull request #38 from lukedirtwalker/fixIPv6

Handle IPv6 in connection URLs
This commit is contained in:
Jack Christensen
2020-05-13 08:50:38 -05:00
committed by GitHub
2 changed files with 51 additions and 5 deletions
+15 -5
View File
@@ -401,13 +401,19 @@ func parseURLSettings(connString string) (map[string]string, error) {
var hosts []string
var ports []string
for _, host := range strings.Split(url.Host, ",") {
parts := strings.SplitN(host, ":", 2)
if parts[0] != "" {
hosts = append(hosts, parts[0])
if host == "" {
continue
}
if len(parts) == 2 {
ports = append(ports, parts[1])
if isIPOnly(host) {
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 {
settings["host"] = strings.Join(hosts, ",")
@@ -428,6 +434,10 @@ func parseURLSettings(connString string) (map[string]string, error) {
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}
func parseDSNSettings(s string) (map[string]string, error) {
+36
View File
@@ -230,6 +230,42 @@ func TestParseConfig(t *testing.T) {
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",
connString: "user=jack password=secret host=localhost port=5432 dbname=mydb sslmode=disable application_name=pgxtest search_path=myschema connect_timeout=5",