-
Notifications
You must be signed in to change notification settings - Fork 8
/
urlmore.go
55 lines (50 loc) · 1.35 KB
/
urlmore.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
package urlutil
import (
"net/url"
"regexp"
"strconv"
)
// UrlMore provides additional URL parsing and reconstruction capabilties
// above and beyond URL. Specifically it can parse out the port number and
// return URLs that strip off the target fragment as well as the query
// string.
type URLMore struct {
URL *url.URL
URLWoQueryWoFragment string
URLWoFragment string
Port int
}
func NewURLMore() URLMore {
urlMore := URLMore{
URLWoQueryWoFragment: "",
URLWoFragment: ""}
return urlMore
}
// Parse uses `url.Parse()` to create a URL object. When using an already
// created URL object, simply set the `Url` property and then call `Inflate`.
func (urlMore *URLMore) Parse(rawurl string) error {
myURL, err := url.Parse(rawurl)
if err != nil {
return err
}
urlMore.URL = myURL
urlMore.Inflate()
return nil
}
func (urlMore *URLMore) Inflate() {
myURL := urlMore.URL
urlMore.URLWoQueryWoFragment = myURL.Scheme + "://" + myURL.Host + myURL.Path
if len(myURL.RawQuery) > 0 {
urlMore.URLWoFragment = urlMore.URLWoQueryWoFragment + "?" + myURL.RawQuery
} else {
urlMore.URLWoFragment = urlMore.URLWoQueryWoFragment
}
rx := regexp.MustCompile(`:([0-9]+)$`)
rs := rx.FindStringSubmatch(myURL.Host)
if len(rs) > 0 {
port, err := strconv.Atoi(rs[1])
if err == nil {
urlMore.Port = port
}
}
}