/
uri.go
273 lines (257 loc) · 6.13 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
271
272
273
// Copyright 2011, Shelby Ramsey. All rights reserved.
// Copyright 2018, Eugen Biegler. All rights reserved.
// Use of this code is governed by a BSD license that can be
// found in the LICENSE.txt file.
package sipparser
// Imports from the go standard library
import (
"fmt"
"strconv"
"strings"
)
const (
SIP_SCHEME = "sip"
SIPS_SCHEME = "sips"
TEL_SCHEME = "tel"
)
// uriStateFn is just a type used by the parse method
type uriStateFn func(*URI) uriStateFn
// URI is a struct that holds an error (hopefully nil), the raw value,
// and the parsed uri.
// Fields are as follows:
// -- Error is the error (or nil)
// -- Scheme is the scheme (i.e. sip)
// -- Raw is the raw value of the uri
// -- UserInfo is the user:password;userparams=foo;
// -- User is the user (i.e. the phone number)
// -- UserPassword is the user password
// -- HostInfo is the host:port combination
// -- Host is the host
// -- Port is the port (if any)
// -- UriParams are the uri's parameters
// -- Secure is if the scheme is "sips"
// -- atPos is just used by the parser to identify where the
// "@" char is in the .Raw field (or 0 if not present)
type URI struct {
Error error // error if any
Scheme string // scheme .. i.e. tel, sip, sips,etc.
Raw string // this is the actual raw uri unparsed
UserInfo string // this is everything before the "@"
User string // this is the actual called party
UserPassword string // this is the password (i.e. alice:passwd@bob.com)
HostInfo string // this is everything after the @ or the entire uri
Host string // the host in the uri
Port string // the port
PortInt int
//UriParams []*Param
Secure bool // Indicates SIP-URI or SIPS-URI (true for SIPS-URI)
atPos int
}
// NewURI is a convenience function that creates a *URI for you
func NewURI(s string) *URI {
u := new(URI)
u.Raw = s
return u
}
// ParseURI is NewURI ... but also parses it
func ParseURI(s string) *URI {
u := NewURI(s)
u.Parse()
return u
}
// Parse parses the .Raw field
func (u *URI) Parse() {
for state := parseUri; state != nil; {
state = state(u)
}
}
// parseUri is the for loop that does the actual parsing
func parseUri(u *URI) uriStateFn {
if u.Error == nil {
return parseUriGetScheme
}
return nil
}
// parseUriGetAt determines if there is an "@" character in the
// .Raw field
func parseUriGetAt(u *URI) uriStateFn {
for i := range u.Raw {
if u.Raw[i] == '@' {
u.atPos = i
return parseUriUser
}
}
if u.atPos == 0 && u.Scheme == TEL_SCHEME {
return parseUriUser
}
return parseUriHost
}
func parseUriGetScheme(u *URI) uriStateFn {
sLen := len(u.Raw)
switch {
case sLen > 4:
if u.Raw[0:4] == "sip:" {
u.Raw = u.Raw[4:]
u.Scheme = SIP_SCHEME
return parseUriGetAt
}
if u.Raw[0:4] == "tel:" {
u.Raw = u.Raw[4:]
u.Scheme = TEL_SCHEME
return parseUriGetAt
}
if sLen > 5 && u.Raw[0:5] == "sips:" {
u.Raw = u.Raw[5:]
u.Scheme = SIPS_SCHEME
return parseUriGetAt
}
default:
return parseUriGetAt
}
return parseUriGetAt
}
func parseUriUser(u *URI) uriStateFn {
if u.atPos == 0 && u.Scheme != TEL_SCHEME {
return parseUriHost
}
firstSemi := strings.IndexRune(u.Raw[0:u.atPos], ';')
colon := 0
/*
if firstSemi != -1 {
if u.UriParams == nil {
u.UriParams = make([]*Param, 0)
}
if len(u.Raw[0:u.atPos])-1 > firstSemi+1 {
params := strings.Split(u.Raw[firstSemi+1:], ";")
for i := range params {
u.UriParams = append(u.UriParams, getParam(params[i]))
}
}
}
*/
switch {
case firstSemi != -1:
for i := range u.Raw[0:firstSemi] {
if u.Raw[i] == ':' {
if u.atPos > i+1 {
u.UserPassword = u.Raw[0:i]
colon = i
break
}
}
}
default:
for i := range u.Raw[0:u.atPos] {
if u.Raw[i] == ':' {
if u.atPos > i+1 {
u.UserPassword = u.Raw[i+1 : u.atPos]
colon = i
break
}
}
}
}
switch {
case colon != 0:
u.User = u.Raw[0:colon]
default:
switch {
case firstSemi != -1:
u.User = u.Raw[0:firstSemi]
default:
if u.Scheme == TEL_SCHEME && u.atPos == 0 {
u.User = u.Raw[0:len(u.Raw)]
if e := strings.IndexRune(u.User, ';'); e > -1 {
u.User = u.Raw[0:e]
}
} else {
u.User = u.Raw[0:u.atPos]
}
}
}
return parseUriHost
}
func parseUriHost(u *URI) uriStateFn {
if len(u.Raw) <= u.atPos {
u.Error = fmt.Errorf("malformed host part inside URI: %s", u.Raw)
return nil
}
firstSemi := strings.IndexRune(u.Raw[u.atPos:], ';')
/*
if firstSemi != -1 {
if u.UriParams == nil {
u.UriParams = make([]*Param, 0)
}
if len(u.Raw[u.atPos:])-1 > firstSemi+1 {
uriparams := strings.Split(u.Raw[u.atPos:][firstSemi+1:], ";")
for i := range uriparams {
u.UriParams = append(u.UriParams, getParam(uriparams[i]))
}
}
}
*/
colon := 0
if firstSemi != -1 {
for i := range u.Raw[u.atPos : u.atPos+firstSemi] {
if i != len(u.Raw[u.atPos+1:u.atPos+firstSemi]) {
if u.Raw[u.atPos+1 : u.atPos+firstSemi][i] == ':' {
//u.Port = cleanWs(u.Raw[u.atPos+1 : u.atPos+firstSemi][i+1:])
u.Port = u.Raw[u.atPos+1 : u.atPos+firstSemi][i+1:]
if len(u.Port) >= 2 {
u.PortInt, _ = strconv.Atoi(u.Port)
}
colon = i
}
if colon != 0 {
break
}
}
}
switch {
case colon == 0:
if u.atPos != 0 {
u.Host = u.Raw[u.atPos+1 : u.atPos+firstSemi]
} else {
u.Host = u.Raw[u.atPos : u.atPos+firstSemi]
}
default:
if u.atPos != 0 {
u.Host = u.Raw[u.atPos+1 : u.atPos+colon+1]
} else {
u.Host = u.Raw[u.atPos : u.atPos+colon+1]
}
}
}
if firstSemi == -1 {
for i := range u.Raw[u.atPos+1:] {
if i != len(u.Raw[u.atPos+1:]) {
if u.Raw[u.atPos+1:][i] == ':' {
//u.Port = cleanWs(u.Raw[u.atPos+1:][i+1:])
u.Port = u.Raw[u.atPos+1:][i+1:]
if len(u.Port) >= 2 {
u.PortInt, _ = strconv.Atoi(u.Port)
}
colon = i
}
if colon != 0 {
break
}
}
}
switch {
case colon == 0:
if u.atPos != 0 {
u.Host = u.Raw[u.atPos+1:]
} else {
u.Host = u.Raw[u.atPos:]
}
default:
if u.atPos != 0 {
u.Host = u.Raw[u.atPos+1 : u.atPos+colon+1]
} else {
u.Host = u.Raw[u.atPos : u.atPos+colon+1]
}
}
}
return nil
}