-
Notifications
You must be signed in to change notification settings - Fork 3
/
url.go
120 lines (104 loc) · 2.84 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
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
package util
import (
"net/url"
"time"
)
// URL wrap url.URL.
type URL struct {
Scheme string
Opaque string // encoded opaque data
User *url.Userinfo // username and password information
Host string // host or host:port
Path string // path (relative paths may omit leading slash)
RawPath string // encoded path hint (see EscapedPath method)
ForceQuery bool // append a query ('?') even if RawQuery is empty
RawQuery string // encoded query values, without '?'
Fragment string // fragment for references, without '#'
HostName string
Port string
params url.Values
}
// ParseURLRaw parses raw into URL.
func ParseURLRaw(raw string) (*URL, error) {
u, e := url.Parse(raw)
if e != nil {
return nil, e
}
return &URL{
Scheme: u.Scheme,
Opaque: u.Opaque,
User: u.User,
Host: u.Host,
Path: u.Path,
RawPath: u.RawPath,
ForceQuery: u.ForceQuery,
RawQuery: u.RawQuery,
Fragment: u.Fragment,
HostName: u.Hostname(),
Port: u.Port(),
params: u.Query(),
}, nil
}
// Password gets password from URL.
func (u *URL) Password() (string, bool) {
if u.User != nil {
return u.User.Password()
}
return "", false
}
// Username gets username from URL.
func (u *URL) Username() string {
return u.User.Username()
}
// QueryInt returns provided field's value in int type.
// if value is empty, expect returns
func (u *URL) QueryInt(field string, expect int) (ret int) {
ret, err := ToIntE(u.Query().Get(field))
if err != nil {
return expect
}
return ret
}
// QueryInt64 returns provided field's value in int64 type.
// if value is empty, expect returns
func (u *URL) QueryInt64(field string, expect int64) (ret int64) {
ret, err := ToInt64E(u.Query().Get(field))
if err != nil {
return expect
}
return ret
}
// QueryString returns provided field's value in string type.
// if value is empty, expect returns
func (u *URL) QueryString(field string, expect string) (ret string) {
ret = expect
if mi := u.Query().Get(field); mi != "" {
ret = mi
}
return
}
// QueryDuration returns provided field's value in duration type.
// if value is empty, expect returns
func (u *URL) QueryDuration(field string, expect time.Duration) (ret time.Duration) {
ret, err := ToDurationE(u.Query().Get(field))
if err != nil {
return expect
}
return ret
}
// QueryBool returns provided field's value in bool
// if value is empty, expect returns
func (u *URL) QueryBool(field string, expect bool) (ret bool) {
ret, err := ToBoolE(u.Query().Get(field))
if err != nil {
return expect
}
return ret
}
// Query parses RawQuery and returns the corresponding values.
// It silently discards malformed value pairs.
// To check errors use ParseQuery.
func (u *URL) Query() url.Values {
v, _ := url.ParseQuery(u.RawQuery)
return v
}