-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoint.go
129 lines (114 loc) · 2.88 KB
/
endpoint.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package net
import (
"net"
"regexp"
"strings"
"github.com/pkg/errors"
)
//Endpoint endpoint to connect to
type Endpoint struct {
endpointAddress
}
//ParseEndpoint parse endpoint address
func ParseEndpoint(src string) (*Endpoint, error) {
const (
api = "ParseEndpoint"
unix = "unix"
tcp = "tcp"
tcp4 = "tcp4"
tcp6 = "tcp6"
)
parts := reSchemaAndAddress.FindStringSubmatch(src)
if len(parts) < 3 {
return nil, errors.Errorf("%s: address '%s' seems invalid", api, src)
}
ep := new(Endpoint)
schema := strings.ToLower(parts[1])
addr := parts[2]
switch schema {
case unix:
ep.endpointAddress = endpointAddressUnix{socketPath: addr}
case tcp, tcp4, tcp6, "":
h, p, e := net.SplitHostPort(addr)
if e != nil {
return nil, errors.Errorf("%s: the addr '%s' has invalid host:port", api, src)
}
h, p = strings.TrimSpace(h), strings.TrimSpace(p)
if len(p) == 0 {
return nil, errors.Errorf("%s: in addr('%s') port is empty", api, src)
}
for _, s := range []*string{&h, &p} {
if strings.ContainsAny(*s, "/?\\=% #") {
return nil, errors.Errorf("%s: the addr('%s') is invalid", api, src)
}
}
ep.endpointAddress = endpointAddressTCP{host: h, port: p}
default:
return nil, errors.Errorf("%s: the addr '%s' has unsupported schema '%s'", api, src, schema)
}
return ep, nil
}
func (ep *Endpoint) String() string {
s, _ := ep.Address()
return s
}
//Address makes net address
func (ep *Endpoint) Address() (string, error) {
const api = "Endpoint.Address"
switch t := ep.endpointAddress.(type) {
case endpointAddressTCP:
return net.JoinHostPort(t.host, t.port), nil
case endpointAddressUnix:
return t.socketPath, nil
}
return "", errors.Errorf("%s: endpoint is not initialized", api)
}
//HostPort gives host - port if TCP case is
func (ep *Endpoint) HostPort() (host, port string, err error) {
const api = "Endpoint.HostPort"
switch t := ep.endpointAddress.(type) {
case endpointAddressTCP:
host, port = t.host, t.port
default:
err = errors.Errorf("%s: endpoint is not a TCP", api)
}
return
}
//IsUnixDomain returns true when endpoint is unix domain socket
func (ep *Endpoint) IsUnixDomain() bool {
_, ret := ep.endpointAddress.(endpointAddressUnix)
return ret
}
//FQN full qualified name
func (ep *Endpoint) FQN() string {
if a, _ := ep.Address(); len(a) > 0 {
return ep.Network() + "://" + a
}
return ""
}
var (
reSchemaAndAddress = regexp.MustCompile(`^\s*(?:(\w+):(?://)?)?([^\\\s#?=]+)\s*$`)
_ = ParseEndpoint
_ net.Addr = (*Endpoint)(nil)
)
type (
endpointAddress interface {
isEndpointAddress()
Network() string
}
endpointAddressTCP struct {
endpointAddress
host string
port string
}
endpointAddressUnix struct {
endpointAddress
socketPath string
}
)
func (endpointAddressTCP) Network() string {
return "tcp"
}
func (endpointAddressUnix) Network() string {
return "unix"
}