-
Notifications
You must be signed in to change notification settings - Fork 2
/
url_parser.go
205 lines (179 loc) · 4.1 KB
/
url_parser.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
package parser
import (
"net/url"
"strconv"
"strings"
)
const (
URL_SCHEME_HTTPS = "https"
URL_SCHEME_HTTP = "http"
URL_SCHEME_MYSQL = "mysql"
URL_SCHEME_POSTGRES = "postgres"
URL_SCHEME_REDIS = "redis"
URL_SCHEME_ETCD = "etcd"
URL_SCHEME_KAFKA = "kafka"
URL_SCHEME_MSSQL = "mssql"
URL_SCHEME_ZOOKEEPER = "zookeeper"
URL_SCHEME_SEP = "://"
)
type UrlInfo struct {
Scheme string // http...
Host string // host name and port eg. '127.0.0.1:3306'
User string // user
Password string // password
Path string // path
Fragment string // fragment
Opaque string // opaque
ForceQuery bool // force query
Queries map[string]string // queries
}
//URL have some special characters in password(支持URL中密码包含特殊字符)
func ParseUrl(strUrl string) (ui *UrlInfo) {
ui = &UrlInfo{Queries: make(map[string]string, 1)}
var encodes = map[string]string{
"`": "%60",
"#": "%23",
"?": "%3f",
"<": "%3c",
">": "%3e",
"[": "%5b",
"]": "%5d",
"{": "%7b",
"}": "%7d",
"/": "%2f",
"|": "%7c",
"\\": "%5c",
"%": "%25",
"^": "%5e",
}
var decodes = map[string]string{
"%60": "`",
"%23": "#",
"%3f": "?",
"%3c": "<",
"%3e": ">",
"%5b": "[",
"%5d": "]",
"%7b": "{",
"%7d": "}",
"%2f": "/",
"%7c": "|",
"%5c": "\\",
"%25": "%",
"%5e": "^",
}
_ = decodes
strUrl = strings.TrimSpace(strUrl)
if strings.Contains(strUrl, "@") { // if a url have user+password, there must be have '@'
// find first '://'
var strScheme string
_ = strScheme
index := strings.LastIndex(strUrl, URL_SCHEME_SEP)
if index > 0 {
strScheme = strUrl[:index]
strUrl = strUrl[index+3:]
}
// find last '@'
index = strings.LastIndex(strUrl, "@")
if index > 0 {
strPrefix := strUrl[:index]
strSuffix := strUrl[index:]
for k, v := range encodes {
//encode user and password special character(s) to url encode
strPrefix = strings.ReplaceAll(strPrefix, k, v)
}
if strScheme != "" {
strUrl = strScheme + URL_SCHEME_SEP
}
strUrl += strPrefix + strSuffix
}
}
u, err := url.Parse(strUrl)
if err != nil {
return
}
ui.Path = u.Path
ui.Host = u.Host
ui.Scheme = u.Scheme
ui.Fragment = u.Fragment
ui.Opaque = u.Opaque
ui.ForceQuery = u.ForceQuery
if u.User != nil {
ui.User = u.User.Username()
ui.Password, _ = u.User.Password()
for k, v := range decodes {
//decode password from url encode to special character(s)
ui.Password = strings.ReplaceAll(ui.Password, k, v)
}
}
vs, _ := url.ParseQuery(u.RawQuery)
for k, v := range vs {
ui.Queries[k] = v[0]
}
return
}
func (u *UrlInfo) GetScheme() string {
return u.Scheme
}
func (u *UrlInfo) GetHost() string {
return u.Host
}
func (u *UrlInfo) GetIPAndPort() (ip string, port int) {
ss := strings.Split(u.Host, ":")
if len(ss) == 0 {
return
}
if len(ss) == 1 {
port = u.getSchemePort()
} else {
port, _ = strconv.Atoi(ss[1])
}
ip = ss[0]
return
}
func (u *UrlInfo) GetPath() string {
return u.Path
}
func (u *UrlInfo) GetUser() string {
return u.User
}
func (u *UrlInfo) GetPassword() string {
return u.Password
}
func (u *UrlInfo) GetOpaque() string {
return u.Opaque
}
func (u *UrlInfo) GetFragment() string {
return u.Fragment
}
func (u *UrlInfo) GetQueries() map[string]string {
return u.Queries
}
func (u *UrlInfo) GetForceQuery() bool {
return u.ForceQuery
}
func (u *UrlInfo) getSchemePort() (port int) {
switch u.Scheme {
case URL_SCHEME_HTTPS:
port = 443
case URL_SCHEME_HTTP:
port = 80
case URL_SCHEME_MYSQL:
port = 3306
case URL_SCHEME_POSTGRES:
port = 5432
case URL_SCHEME_REDIS:
port = 6379
case URL_SCHEME_ETCD:
port = 2379
case URL_SCHEME_KAFKA:
port = 9092
case URL_SCHEME_MSSQL:
port = 1433
case URL_SCHEME_ZOOKEEPER:
port = 2181
default:
port = 80
}
return
}