-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnormalizers.go
51 lines (47 loc) · 1.22 KB
/
normalizers.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
package main
import (
"net/url"
"strings"
"golang.org/x/net/idna"
)
func NormalizeDomain(domain string) string {
domain = strings.Replace(domain, ",", ".", -1)
domain = strings.Replace(domain, " ", "", -1)
if c := strings.IndexByte(domain, '/'); c >= 0 {
domain = domain[:c]
}
if c := strings.IndexByte(domain, '\\'); c >= 0 {
domain = domain[:c]
}
domain = strings.TrimPrefix(domain, "*.")
domain = strings.TrimSuffix(domain, ".")
domain, _ = idna.ToASCII(domain)
domain = strings.ToLower(domain)
return domain
}
func NormalizeUrl(u string) (nurl, host string) {
u = strings.Replace(u, "\\", "/", -1)
_url, err := url.Parse(u)
if err == nil && _url.Scheme == "" {
_url, err = url.Parse("https://" + u) // shit for resolve bogus URL problem
}
if err != nil {
Error.Printf("URL parse error: %s\n", err.Error())
// add as is
return u, ""
} else {
port := ""
domain := _url.Hostname()
colon := strings.LastIndexByte(domain, ':')
if colon != -1 && validOptionalPort(domain[colon:]) {
domain, port = domain[:colon], domain[colon+1:]
}
domain = NormalizeDomain(domain)
_url.Host = domain
if port != "" {
_url.Host = _url.Host + ":" + port
}
_url.Fragment = ""
return _url.String(), domain
}
}