-
Notifications
You must be signed in to change notification settings - Fork 1
/
uri.go
270 lines (251 loc) · 4.92 KB
/
uri.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package sip
import (
"net"
"net/url"
"strconv"
"strings"
)
type (
Uri struct {
IsEncrypted bool
HasProtocol bool
User string
Password string
Host string
Port int
Params Map //params
Queries Map //queries
}
Map map[string]string
)
func NewUri(user, addr string, ps Map) *Uri {
uri := &Uri{
IsEncrypted: false,
User: user,
Params: ps,
Queries: Map{},
}
uri.SetAddress(addr)
return uri
}
func (uri *Uri) EnableProtocol() *Uri {
uri.HasProtocol = true
return uri
}
func (uri *Uri) SetParams(m Map) *Uri {
uri.Params = m
return uri
}
func (uri *Uri) SetAddress(addr string) *Uri {
if strings.Index(addr, ":") > -1 {
if host, port, err := net.SplitHostPort(addr); err == nil {
uri.Host = host
uri.Port, _ = strconv.Atoi(port)
}
} else {
uri.Host = addr
}
return uri
}
func (uri *Uri) Address() string {
return net.JoinHostPort(uri.Host, strconv.Itoa(uri.Port))
}
func (m *Map) Set(k, v string) {
if m == nil || len(*m) == 0 {
*m = make(map[string]string)
}
mp := *m
mp[k] = v
}
func (m Map) Get(k string) string {
if m == nil {
return ""
}
return m[k]
}
func (m Map) ToString(sep string) string {
var (
i int
length int
sb strings.Builder
)
length = len(m)
for k, v := range m {
sb.WriteString(k)
if len(v) > 0 {
sb.WriteString("=")
if strings.ContainsAny(v, " \t") {
sb.WriteString("\"")
sb.WriteString(v)
sb.WriteString("\"")
} else {
sb.WriteString(v)
}
}
i++
if i < length {
sb.WriteString(sep)
}
}
return sb.String()
}
func (m Map) String() string {
return m.ToString(";")
}
func (m Map) Clone() Map {
mm := make(map[string]string)
for k, v := range m {
mm[k] = v
}
return mm
}
func (uri *Uri) ParamValue(name string) string {
return uri.Params.Get(name)
}
func (uri *Uri) FormValue(name string) string {
return uri.Queries.Get(name)
}
func (uri *Uri) Clone() *Uri {
u := &Uri{
HasProtocol: uri.HasProtocol,
IsEncrypted: uri.IsEncrypted,
User: uri.User,
Password: uri.Password,
Host: uri.Host,
Port: uri.Port,
}
if uri.Params != nil {
u.Params = uri.Params.Clone()
}
if uri.Queries != nil {
u.Queries = uri.Queries.Clone()
}
return u
}
func (uri *Uri) String() string {
var sb strings.Builder
// Compulsory protocol identifier.
if uri.HasProtocol {
if uri.IsEncrypted {
sb.WriteString("sips")
sb.WriteString(":")
} else {
sb.WriteString("sip")
sb.WriteString(":")
}
}
if uri.User != "" {
sb.WriteString(uri.User)
if uri.Password != "" {
sb.WriteString(":" + uri.Password)
}
sb.WriteString("@")
}
// Compulsory hostname.
sb.WriteString(uri.Host)
// Optional port number.
if uri.Port != 0 {
sb.WriteString(":")
sb.WriteString(strconv.Itoa(int(uri.Port)))
}
if (uri.Params != nil) && len(uri.Params) > 0 {
sb.WriteString(";")
sb.WriteString(uri.Params.String())
}
if (uri.Queries != nil) && len(uri.Queries) > 0 {
sb.WriteString("?")
sb.WriteString(uri.Queries.ToString("&"))
}
return sb.String()
}
func parseMap(s string) (m Map, err error) {
for s != "" {
key := s
if i := strings.IndexAny(key, "&;"); i >= 0 {
key, s = key[:i], key[i+1:]
} else {
s = ""
}
if key == "" {
continue
}
value := ""
if i := strings.Index(key, "="); i >= 0 {
key, value = key[:i], key[i+1:]
}
key, err1 := url.QueryUnescape(key)
if err1 != nil {
if err == nil {
err = err1
}
continue
}
value, err1 = url.QueryUnescape(value)
if err1 != nil {
if err == nil {
err = err1
}
continue
}
if len(value) > 0 {
value = strings.Trim(value, "\"")
value = strings.TrimSpace(value)
}
m.Set(key, value)
}
return
}
//parseUri parse uri from string
func parseUri(s string) (uri *Uri, err error) {
var (
pos int
endOfUserPos int
netAddrStr string
netSplitPos int
p string
)
uri = &Uri{}
p = s
//sip or sips
if pos = strings.Index(p, ":"); pos > -1 && pos < 4 {
uri.HasProtocol = true
if strings.ToLower(p[:pos]) == "sips" {
uri.IsEncrypted = true
}
p = p[pos+1:]
}
if pos = strings.Index(p, "@"); pos != -1 {
if endOfUserPos = strings.Index(p[:pos], ":"); endOfUserPos == -1 {
uri.User = p[:pos]
} else {
uri.User = p[:endOfUserPos]
uri.Password = p[endOfUserPos+1 : pos]
}
p = p[pos+1:]
}
if pos = strings.IndexAny(p, ";?"); pos == -1 {
netAddrStr = p
} else {
netAddrStr = p[:pos]
}
if netSplitPos = strings.Index(netAddrStr, ":"); netSplitPos == -1 {
uri.Host = netAddrStr
} else {
uri.Host = netAddrStr[:netSplitPos]
uri.Port, _ = strconv.Atoi(netAddrStr[netSplitPos+1:])
}
if pos > -1 {
p = p[pos:]
if p[0] == '?' { //queries
uri.Queries, err = parseMap(p[1:])
} else { //params
if pos = strings.Index(p, "?"); pos == -1 {
uri.Params, err = parseMap(p[1:])
} else {
uri.Params, err = parseMap(p[1:pos])
uri.Queries, err = parseMap(p[pos+1:])
}
}
}
return
}