/
url_codec.go
146 lines (130 loc) · 2.84 KB
/
url_codec.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
package mgoc
import (
"github.com/civet148/log"
"net/url"
"strings"
)
const (
URL_SCHEME_SEP = "://"
URL_QUERY_SLAVE = "slave"
URL_QUERY_MAX = "max"
URL_QUERY_IDLE = "idle"
URL_QUERY_CHARSET = "charset"
)
type UrlInfo struct {
Scheme string
Host string // host name and port like '127.0.0.1:3306'
User string
Password string
Path string
Fragment string
Opaque string
ForceQuery bool
Queries map[string]string
Database string
}
//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
// scheme://[userinfo@]host:port/path[?query][#fragment]
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]
}
ui.Database = getDatabaseName(ui.Path)
return
}
func getDatabaseName(strPath string) string {
idx := strings.LastIndex(strPath, "/")
return strPath[idx+1:]
}
func getHostPort(strHost string) (ip, port string) {
if strHost == "" {
log.Panic("host is empty")
}
ipport := strings.Split(strHost, ":")
if len(ipport) == 2 {
ip = ipport[0]
port = ipport[1]
} else {
ip = ipport[0]
port = "27017"
}
return
}