-
Notifications
You must be signed in to change notification settings - Fork 334
/
addr.go
39 lines (35 loc) · 944 Bytes
/
addr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package netutil
import (
"golang.org/x/net/idna"
"net"
"net/url"
"strings"
)
func AuthorityKey(u *url.URL) string {
return u.Scheme + "://" + AuthorityAddr(u.Scheme, u.Host)
}
// AuthorityAddr returns a given authority (a host/IP, or host:port / ip:port)
// and returns a host:port. The port 443 is added if needed.
func AuthorityAddr(scheme, authority string) (addr string) {
host, port := AuthorityHostPort(scheme, authority)
// IPv6 address literal, without a port:
if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") {
return host + ":" + port
}
addr = net.JoinHostPort(host, port)
return
}
func AuthorityHostPort(scheme, authority string) (host, port string) {
host, port, err := net.SplitHostPort(authority)
if err != nil { // authority didn't have a port
port = "443"
if scheme == "http" {
port = "80"
}
host = authority
}
if a, err := idna.ToASCII(host); err == nil {
host = a
}
return
}