forked from couchbase/gocb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnspec.go
208 lines (178 loc) · 4.84 KB
/
connspec.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package gocb
import (
"errors"
"fmt"
"net"
"net/url"
"regexp"
"strconv"
)
type connSpecScheme int
const (
csPlainMcd connSpecScheme = 11210
csPlainHttp connSpecScheme = 8091
csSslMcd connSpecScheme = 11207
csSslHttp connSpecScheme = 18091
csInvalid connSpecScheme = 0
)
// Gets the implicit port for the scheme
func (scheme connSpecScheme) DefaultPort() int {
return int(scheme)
}
func (scheme connSpecScheme) IsMCD() bool {
return scheme == csPlainMcd || scheme == csSslMcd
}
func (scheme connSpecScheme) IsHTTP() bool {
return scheme == csPlainHttp || scheme == csSslHttp
}
func (scheme connSpecScheme) IsSSL() bool {
return scheme == csSslMcd || scheme == csSslHttp
}
func (scheme connSpecScheme) String() string {
switch scheme {
case csPlainHttp:
return "http"
case csPlainMcd:
return "couchbase"
case csSslHttp:
return "https"
case csSslMcd:
return "couchbases"
default:
return ""
}
}
func (scheme *connSpecScheme) load(s string) {
switch s {
case "couchbase":
*scheme = csPlainMcd
case "couchbases":
*scheme = csSslMcd
case "http":
*scheme = csPlainHttp
case "https":
*scheme = csSslHttp
}
}
// A single address stored within a connection string
type connSpecAddr struct {
Host string
Port uint16
}
func (a *connSpecAddr) HostPort() string {
return fmt.Sprintf("%s:%d", a.Host, a.Port)
}
// A parsed connection string
type connSpec struct {
Scheme connSpecScheme
MemcachedHosts []*connSpecAddr
HttpHosts []*connSpecAddr
Bucket string
Options url.Values
hasExplicitPort bool
hasExplicitScheme bool
}
// Loads a raw host definition into the spec
// Host is the raw hostname (without the port) and port is the port used. May be 0
func (cs *connSpec) addRawHost(host string, port int) error {
// The port is "implicit", so we can derive the neighboring port
if port != 0 && port != csPlainHttp.DefaultPort() && !cs.hasExplicitScheme {
return errors.New("Ambiguous port without scheme")
}
if cs.hasExplicitScheme && cs.Scheme.IsMCD() && port == csPlainHttp.DefaultPort() {
return errors.New("couchbase://host:8091 not supported for couchbase:// scheme. Use couchbase://host")
}
if port == 0 || port == cs.Scheme.DefaultPort() || port == csPlainHttp.DefaultPort() {
tmpHtHost := &connSpecAddr{Host: host}
tmpMcHost := &connSpecAddr{Host: host}
if cs.Scheme.IsSSL() {
tmpHtHost.Port = uint16(csSslHttp.DefaultPort())
tmpMcHost.Port = uint16(csSslMcd.DefaultPort())
} else {
tmpHtHost.Port = uint16(csPlainHttp.DefaultPort())
tmpMcHost.Port = uint16(csPlainMcd.DefaultPort())
}
cs.HttpHosts = append(cs.HttpHosts, tmpHtHost)
cs.MemcachedHosts = append(cs.MemcachedHosts, tmpMcHost)
} else {
// Explicit non-standard port. Just add the port without anything funny
tmpHost := &connSpecAddr{Host: host, Port: uint16(port)}
if cs.Scheme.IsMCD() {
cs.MemcachedHosts = append(cs.MemcachedHosts, tmpHost)
} else {
cs.HttpHosts = append(cs.HttpHosts, tmpHost)
}
}
return nil
}
// Parses a connection string into a structure more easily consumed by the library.
func parseConnSpec(connStr string) (out connSpec, err error) {
partMatcher := regexp.MustCompile(`((.*):\/\/)?(([^\/?:]*)(:([^\/?:@]*))?@)?([^\/?]*)(\/([^\?]*))?(\?(.*))?`)
hostMatcher := regexp.MustCompile(`([^;\,\:]+)(:([0-9]*))?(;\,)?`)
parts := partMatcher.FindStringSubmatch(connStr)
if parts[2] != "" {
(&out.Scheme).load(parts[2])
if out.Scheme == csInvalid {
err = fmt.Errorf("Unknown scheme '%s'", parts[2])
return
}
out.hasExplicitScheme = true
} else {
out.Scheme = csPlainMcd
}
if parts[7] != "" {
hosts := hostMatcher.FindAllStringSubmatch(parts[7], -1)
for _, hostInfo := range hosts {
port := 0
if hostInfo[3] != "" {
port, _ = strconv.Atoi(hostInfo[3])
out.hasExplicitPort = true
}
err = out.addRawHost(hostInfo[1], port)
if err != nil {
return
}
}
}
if len(out.HttpHosts) == 0 && len(out.MemcachedHosts) == 0 {
out.addRawHost("127.0.0.1", 0)
}
if parts[9] != "" {
out.Bucket, err = url.QueryUnescape(parts[9])
if err != nil {
return
}
}
if parts[11] != "" {
out.Options, err = url.ParseQuery(parts[11])
if err != nil {
return
}
}
return
}
func csResolveDnsSrv(spec *connSpec) bool {
if len(spec.MemcachedHosts) > 1 || len(spec.HttpHosts) > 1 {
return false
}
if spec.hasExplicitPort || spec.Scheme.IsHTTP() {
return false
}
srvHostname := spec.HttpHosts[0].Host
_, addrs, err := net.LookupSRV(spec.Scheme.String(), "tcp", srvHostname)
if err != nil || len(addrs) == 0 {
return false
}
var hostList []*connSpecAddr
for _, srvRecord := range addrs {
hostList = append(hostList, &connSpecAddr{srvRecord.Target, srvRecord.Port})
}
spec.HttpHosts = nil
spec.MemcachedHosts = nil
if spec.Scheme.IsHTTP() {
spec.HttpHosts = hostList
} else {
spec.MemcachedHosts = hostList
}
return true
}