Checkpoint
This commit is contained in:
138 files changed
+1898
-57607
No files matched your search
@@ -0,0 +1,56 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// JoinHostPort is like net.JoinHostPort but accepts a default host and port.
|
||||
func JoinHostPort(host, port, defaultHost, defaultPort string) string {
|
||||
if host == "" {
|
||||
host = defaultHost
|
||||
}
|
||||
if port == "" {
|
||||
port = defaultPort
|
||||
}
|
||||
return net.JoinHostPort(host, port)
|
||||
}
|
||||
|
||||
// MakeAddr accepts an empty string, just a hostname or just a port number, or just a port name prefixed by a colon and complements the missing parts from the default address.
|
||||
//
|
||||
// MakeAddr("", "localhost:80") -> "localhost:80"
|
||||
// MakeAddr("1.1.1.1", "localhost:80") -> "1.1.1.1:80"
|
||||
// MakeAddr("81", "localhost:80") -> "localhost:81"
|
||||
// MakeAddr("foo", "localhost:80") -> "foo:80"
|
||||
// MakeAddr(":https", "localhost:80") -> "localhost:https"
|
||||
func MakeAddr(addr, defaultAddr string) string {
|
||||
if addr == "" {
|
||||
// Fast case
|
||||
return defaultAddr
|
||||
}
|
||||
|
||||
defaultHost, defaultPort, err := net.SplitHostPort(defaultAddr)
|
||||
if err != nil {
|
||||
panic(err) // default was malformed, error in code!
|
||||
}
|
||||
|
||||
i := strings.LastIndexByte(addr, ':')
|
||||
if i < 0 {
|
||||
if _, err = strconv.ParseInt(addr, 10, 16); err == nil {
|
||||
// Just a port number
|
||||
return net.JoinHostPort(defaultHost, addr)
|
||||
}
|
||||
// Just a host
|
||||
return net.JoinHostPort(addr, defaultPort)
|
||||
} else if i == 0 {
|
||||
// Just a port name
|
||||
return net.JoinHostPort(defaultHost, addr[1:])
|
||||
} else if i == len(addr)-1 {
|
||||
// Just a host with a colon
|
||||
return net.JoinHostPort(addr[:i], defaultPort)
|
||||
}
|
||||
|
||||
// Assume already full qualified
|
||||
return addr
|
||||
}
|
||||
Reference in new issue
Block a user