forked from quic-go/quic-go
make utils an internal package
This commit is contained in:
27
internal/utils/host.go
Normal file
27
internal/utils/host.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// HostnameFromAddr determines the hostname in an address string
|
||||
func HostnameFromAddr(addr string) (string, error) {
|
||||
p, err := url.Parse(addr)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
h := p.Host
|
||||
|
||||
// copied from https://golang.org/src/net/http/transport.go
|
||||
if hasPort(h) {
|
||||
h = h[:strings.LastIndex(h, ":")]
|
||||
}
|
||||
|
||||
return h, nil
|
||||
}
|
||||
|
||||
// copied from https://golang.org/src/net/http/http.go
|
||||
func hasPort(s string) bool {
|
||||
return strings.LastIndex(s, ":") > strings.LastIndex(s, "]")
|
||||
}
|
||||
Reference in New Issue
Block a user