-
Notifications
You must be signed in to change notification settings - Fork 0
/
url.go
61 lines (50 loc) · 1.18 KB
/
url.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
package go_sdk
import (
"fmt"
"strings"
)
// url
// each url contains of the following parts:
// - host
// - port
type url struct {
address string
auth string
}
// urlUnpack
// manages to create url struct from url string.
func urlUnpack(inputUrl string) (*url, error) {
// check the input url protocol
protocolSplit := strings.Split(inputUrl, "://")
if len(protocolSplit) < 2 {
return nil, fmt.Errorf("invalid uri")
}
// protocol must be 'st'
if protocolSplit[0] != "st" {
return nil, fmt.Errorf("not using stallion protocol (st://...)")
}
var (
address string
auth string
)
// exporting the user:pass@host:port with @
if len(strings.Split(protocolSplit[1], "@")) < 2 {
auth = " : "
// exporting the host:port pair
if len(strings.Split(protocolSplit[1], ":")) < 2 {
return nil, fmt.Errorf("server ip or port is not given")
}
address = protocolSplit[1]
} else {
authAndAddress := strings.Split(protocolSplit[1], "@")
if len(strings.Split(authAndAddress[0], ":")) < 2 {
return nil, fmt.Errorf("auth user or pass is not given")
}
auth = authAndAddress[0]
address = authAndAddress[1]
}
return &url{
address: address,
auth: auth,
}, nil
}