target based upstream options

This commit is contained in:
2025-08-14 14:52:17 +08:00
parent f1fe70386d
commit 6681522678
9 changed files with 245 additions and 5 deletions

38
internal/util/hostport.go Normal file
View File

@@ -0,0 +1,38 @@
package util
import "strings"
// ValidOptionalPort reports whether port is either an empty string
// or matches /^:\d*$/
func ValidOptionalPort(port string) bool {
if port == "" {
return true
}
if port[0] != ':' {
return false
}
for _, b := range port[1:] {
if b < '0' || b > '9' {
return false
}
}
return true
}
// SplitHostPort separates host and port. If the port is not valid, it returns
// the entire input as host, and it doesn't check the validity of the host.
// Unlike net.SplitHostPort, but per RFC 3986, it requires ports to be numeric.
func SplitHostPort(hostPort string) (host, port string) {
host = hostPort
colon := strings.LastIndexByte(host, ':')
if colon != -1 && ValidOptionalPort(host[colon:]) {
host, port = host[:colon], host[colon+1:]
}
if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") {
host = host[1 : len(host)-1]
}
return
}