mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 16:40:24 +08:00 
			
		
		
		
	refactor postgres connection string building (#27723)
This patchset changes the connection string builder to use net.URL and the host/port parser to use the stdlib function for splitting host from port. It also adds a footnote about a potentially required portnumber for postgres UNIX sockets. Fixes: #24552
This commit is contained in:
		@@ -6,6 +6,7 @@ package setting
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"net"
 | 
			
		||||
	"net/url"
 | 
			
		||||
	"os"
 | 
			
		||||
	"path"
 | 
			
		||||
@@ -135,15 +136,18 @@ func DBConnStr() (string, error) {
 | 
			
		||||
// parsePostgreSQLHostPort parses given input in various forms defined in
 | 
			
		||||
// https://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-CONNSTRING
 | 
			
		||||
// and returns proper host and port number.
 | 
			
		||||
func parsePostgreSQLHostPort(info string) (string, string) {
 | 
			
		||||
	host, port := "127.0.0.1", "5432"
 | 
			
		||||
	if strings.Contains(info, ":") && !strings.HasSuffix(info, "]") {
 | 
			
		||||
		idx := strings.LastIndex(info, ":")
 | 
			
		||||
		host = info[:idx]
 | 
			
		||||
		port = info[idx+1:]
 | 
			
		||||
	} else if len(info) > 0 {
 | 
			
		||||
func parsePostgreSQLHostPort(info string) (host, port string) {
 | 
			
		||||
	if h, p, err := net.SplitHostPort(info); err == nil {
 | 
			
		||||
		host, port = h, p
 | 
			
		||||
	} else {
 | 
			
		||||
		// treat the "info" as "host", if it's an IPv6 address, remove the wrapper
 | 
			
		||||
		host = info
 | 
			
		||||
		if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") {
 | 
			
		||||
			host = host[1 : len(host)-1]
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// set fallback values
 | 
			
		||||
	if host == "" {
 | 
			
		||||
		host = "127.0.0.1"
 | 
			
		||||
	}
 | 
			
		||||
@@ -155,14 +159,22 @@ func parsePostgreSQLHostPort(info string) (string, string) {
 | 
			
		||||
 | 
			
		||||
func getPostgreSQLConnectionString(dbHost, dbUser, dbPasswd, dbName, dbParam, dbsslMode string) (connStr string) {
 | 
			
		||||
	host, port := parsePostgreSQLHostPort(dbHost)
 | 
			
		||||
	if host[0] == '/' { // looks like a unix socket
 | 
			
		||||
		connStr = fmt.Sprintf("postgres://%s:%s@:%s/%s%ssslmode=%s&host=%s",
 | 
			
		||||
			url.PathEscape(dbUser), url.PathEscape(dbPasswd), port, dbName, dbParam, dbsslMode, host)
 | 
			
		||||
	} else {
 | 
			
		||||
		connStr = fmt.Sprintf("postgres://%s:%s@%s:%s/%s%ssslmode=%s",
 | 
			
		||||
			url.PathEscape(dbUser), url.PathEscape(dbPasswd), host, port, dbName, dbParam, dbsslMode)
 | 
			
		||||
	connURL := url.URL{
 | 
			
		||||
		Scheme:   "postgres",
 | 
			
		||||
		User:     url.UserPassword(dbUser, dbPasswd),
 | 
			
		||||
		Host:     net.JoinHostPort(host, port),
 | 
			
		||||
		Path:     dbName,
 | 
			
		||||
		OmitHost: false,
 | 
			
		||||
		RawQuery: dbParam,
 | 
			
		||||
	}
 | 
			
		||||
	return connStr
 | 
			
		||||
	query := connURL.Query()
 | 
			
		||||
	if dbHost[0] == '/' { // looks like a unix socket
 | 
			
		||||
		query.Add("host", dbHost)
 | 
			
		||||
		connURL.Host = ":" + port
 | 
			
		||||
	}
 | 
			
		||||
	query.Set("sslmode", dbsslMode)
 | 
			
		||||
	connURL.RawQuery = query.Encode()
 | 
			
		||||
	return connURL.String()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ParseMSSQLHostPort splits the host into host and port
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user