-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurls.go
109 lines (96 loc) · 2.59 KB
/
urls.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
package ohauth
import (
"encoding/json"
"errors"
"net/url"
"strings"
)
// ErrNotAbsoluteURL is returned when a parsed URL is not absolute i.e. does not
// have scheme or host
var ErrNotAbsoluteURL = errors.New("absolute urls with host are required")
// StrictURL is similar to the standard net/url.URL type except that it can be
// json marshalled and unmarshalled and forces all parsed urls to https protocol
type StrictURL url.URL
// ParseURL parses a string url and coerces the scheme to https, clears the
// querystring, sets fragment to '_=_' to create a StrictURL instance. The raw
// url must be absolute (host and scheme must be set)
func ParseURL(raw string) (*StrictURL, error) {
u, err := url.Parse(raw)
if err != nil {
return nil, err
}
u.Host = strings.TrimSpace(u.Host)
u.Scheme = "https"
u.RawQuery = ""
u.Fragment = "_=_"
if u.Host == "" || !u.IsAbs() {
return nil, ErrNotAbsoluteURL
}
return (*StrictURL)(u), err
}
// MustParseURL is the same as ParseURL but panic on error instead
func MustParseURL(raw string) *StrictURL {
u, err := ParseURL(raw)
if err != nil {
panic(err)
}
return u
}
// MarshalJSON implements the json.Marshaler interface
func (u *StrictURL) MarshalJSON() ([]byte, error) {
c := (*url.URL)(u)
return json.Marshal(c.String())
}
// UnmarshalJSON implements the json.Unmarshaler that correctly parses a
// StrictURL
func (u *StrictURL) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
in, err := url.Parse(s)
if err != nil {
return err
}
*u = StrictURL(*in)
return nil
}
func (u *StrictURL) String() string {
if u == nil {
return ""
}
unbox := (*url.URL)(u)
return unbox.String()
}
// Compare determines if two StrictURL's are the same using simple string
// comparison. If either instance is nil the result is false.
func (u *StrictURL) Compare(u2 *StrictURL) bool {
if u == nil || u2 == nil {
return false
}
s1 := u.String()
s2 := u2.String()
return s1 != "" && s2 != "" && s1 == s2
}
// Clone creates a new copy of the StrictURL
func (u *StrictURL) Clone() *StrictURL {
c, err := ParseURL(u.String())
if err != nil {
panic(err)
}
return c
}
// StringWithParams returns a string representation of a StrictURL with
// the specified query parameters
func (u *StrictURL) StringWithParams(v url.Values) string {
c := u.Clone()
c.RawQuery = v.Encode()
return c.String()
}
// StringWithFragment returns a string representation of a StrictURL with
// the specified fragment
func (u *StrictURL) StringWithFragment(v url.Values) string {
c := u.Clone()
c.Fragment = v.Encode()
return c.String()
}