-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
181 lines (154 loc) · 3.6 KB
/
main.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
package path_parser
import (
"net/url"
"regexp"
"strconv"
"strings"
)
type Output struct {
Protocols []string
Protocol string
Port *int
Resource string
User string
Pathname string
Hash string
Search string
Href string
Query url.Values
}
// ParsePath parse paths (local paths, urls: ssh/git/etc)
func ParsePath(input string) Output {
input = strings.TrimSpace(input)
var re = regexp.MustCompile(`\r\n|\r`)
input = string(re.ReplaceAll([]byte(input), []byte("")))
var output = Output{
Protocols: Protocols(input),
Protocol: "",
Port: nil,
Resource: "",
User: "",
Pathname: "",
Hash: "",
Search: "",
Href: input,
Query: nil,
}
if strings.HasPrefix(input, ".") {
if strings.HasPrefix(input, "./") {
input = input[2:]
}
output.Pathname = input
output.Protocol = "file"
}
protocolIdx := strings.Index(input, "://")
fc := input[1]
if len(output.Protocol) == 0 {
if len(output.Protocols) > 0 {
output.Protocol = output.Protocols[0]
}
if len(output.Protocol) == 0 {
if IsSsh(input) {
output.Protocol = "ssh"
} else if fc == '/' || fc == '~' {
input = input[2:]
output.Protocol = "file"
} else {
output.Protocol = "file"
}
}
}
if protocolIdx != -1 {
input = input[protocolIdx+3:]
}
re = regexp.MustCompile(`\/|\\`)
parts := re.Split(input, -1)
if output.Protocol != "file" {
output.Resource = parts[0]
parts = parts[1:]
} else {
output.Resource = ""
}
splits := strings.Split(output.Resource, "@")
if len(splits) == 2 {
output.User = splits[0]
output.Resource = splits[1]
}
splits = strings.Split(output.Resource, ":")
if len(splits) == 2 {
output.Resource = splits[0]
port := splits[1]
if len(port) != 0 {
p, err := strconv.Atoi(port)
if err != nil || p == 0 {
parts = append(parts, parts[len(parts)-1])
parts[0] = port
} else {
output.Port = &p
}
}
}
var filtered []string
for i := range parts {
if parts[i] != "" {
filtered = append(filtered, parts[i])
}
}
parts = filtered
if output.Protocol == "file" {
output.Pathname = output.Href
} else if len(output.Pathname) == 0 {
pathname := ""
if output.Protocol != "file" || output.Href[0] == '/' {
pathname = "/"
}
output.Pathname = pathname + strings.Join(parts, "/")
}
splits = strings.Split(output.Pathname, "#")
if len(splits) == 2 {
output.Pathname = splits[0]
output.Hash = splits[1]
}
splits = strings.Split(output.Pathname, "?")
if len(splits) == 2 {
output.Pathname = splits[0]
output.Search = splits[1]
}
re = regexp.MustCompile(`\/$`)
q, err := url.ParseQuery(output.Search)
if err != nil {
panic(err)
}
output.Query = q
output.Href = re.ReplaceAllString(output.Href, "")
output.Pathname = re.ReplaceAllString(output.Pathname, "")
return output
}
// IsSsh check if an input value is a ssh url or not
func IsSsh(input string) bool {
protocols := Protocols(input)
input = input[strings.Index(input, "://")+3:]
for _, p := range protocols {
if strings.HasPrefix(p, "ssh") || strings.HasPrefix(p, "rsync") {
return true
}
}
var re = regexp.MustCompile(`\.([a-zA-Z\\d]+):(\d+)\/`)
return !re.Match([]byte(input)) && strings.Index(input, "@") < strings.Index(input, ":")
}
// Protocols returns the protocols of an input url.
func Protocols(input string) []string {
idx := strings.Index(input, "://")
if idx == -1 {
return []string{}
}
input = input[0:idx]
splits := strings.Split(input, "+")
var filtered []string
for i := range splits {
if splits[i] != "" {
filtered = append(filtered, splits[i])
}
}
return filtered
}