Skip to content

Commit

Permalink
Merge pull request #2082 from heavycrystal/url-parse-err-fix
Browse files Browse the repository at this point in the history
don't print URL when url.Parse returns an error
  • Loading branch information
jackc authored Jul 12, 2024
2 parents 3563a2b + 13e2124 commit 96791c8
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions pgconn/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,22 +467,25 @@ func parseEnvSettings() map[string]string {
func parseURLSettings(connString string) (map[string]string, error) {
settings := make(map[string]string)

url, err := url.Parse(connString)
parsedURL, err := url.Parse(connString)
if err != nil {
if urlErr := new(url.Error); errors.As(err, &urlErr) {
return nil, urlErr.Err
}
return nil, err
}

if url.User != nil {
settings["user"] = url.User.Username()
if password, present := url.User.Password(); present {
if parsedURL.User != nil {
settings["user"] = parsedURL.User.Username()
if password, present := parsedURL.User.Password(); present {
settings["password"] = password
}
}

// Handle multiple host:port's in url.Host by splitting them into host,host,host and port,port,port.
var hosts []string
var ports []string
for _, host := range strings.Split(url.Host, ",") {
for _, host := range strings.Split(parsedURL.Host, ",") {
if host == "" {
continue
}
Expand All @@ -508,7 +511,7 @@ func parseURLSettings(connString string) (map[string]string, error) {
settings["port"] = strings.Join(ports, ",")
}

database := strings.TrimLeft(url.Path, "/")
database := strings.TrimLeft(parsedURL.Path, "/")
if database != "" {
settings["database"] = database
}
Expand All @@ -517,7 +520,7 @@ func parseURLSettings(connString string) (map[string]string, error) {
"dbname": "database",
}

for k, v := range url.Query() {
for k, v := range parsedURL.Query() {
if k2, present := nameMap[k]; present {
k = k2
}
Expand Down

0 comments on commit 96791c8

Please sign in to comment.