use net.JoinHostPort instead of fmt.Sprintf (#2791)

This commit is contained in:
fatedier
2022-02-09 15:19:35 +08:00
committed by GitHub
parent b2311e55e7
commit 6194273615
17 changed files with 61 additions and 49 deletions

View File

@@ -34,17 +34,6 @@ func OkResponse() *http.Response {
return res
}
// TODO: use "CanonicalHost" func to replace all "GetHostFromAddr" func.
func GetHostFromAddr(addr string) (host string) {
strs := strings.Split(addr, ":")
if len(strs) > 1 {
host = strs[0]
} else {
host = addr
}
return
}
// canonicalHost strips port from host if present and returns the canonicalized
// host name.
func CanonicalHost(host string) (string, error) {

View File

@@ -19,6 +19,7 @@ import (
"crypto/rand"
"encoding/hex"
"fmt"
"net"
"strconv"
"strings"
)
@@ -52,7 +53,7 @@ func CanonicalAddr(host string, port int) (addr string) {
if port == 80 || port == 443 {
addr = host
} else {
addr = fmt.Sprintf("%s:%d", host, port)
addr = net.JoinHostPort(host, strconv.Itoa(port))
}
return
}