-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathurl.go
125 lines (103 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
121
122
123
124
125
package utils
import (
"bytes"
"errors"
"net/url"
"strings"
)
var (
GooglePlay = []byte("play.google")
ITunesApple = []byte("itunes.apple")
HTTPS = []byte("https://")
HTTP = []byte("http://")
HTTPSProtocol = []byte("https:")
HTTPProtocol = []byte("http:")
ErrInvalidUrl = errors.New("invalid url")
)
func TrimUrlForScylla(fullUrl string) (scyllaUrl string, hostName string, err error) {
trimmedUrl := strings.TrimSpace(fullUrl)
if len(trimmedUrl) == 0 {
return "", "", ErrInvalidUrl
}
// remove everything behind ? or #
if index := strings.Index(trimmedUrl, "?"); index > 0 {
trimmedUrl = trimmedUrl[0:index]
}
if index := strings.Index(trimmedUrl, "#"); index > 0 {
trimmedUrl = trimmedUrl[0:index]
}
trimmedUrl = strings.ToValidUTF8(trimmedUrl, "")
if trimmedUrl[len(trimmedUrl)-1:] != "/" {
trimmedUrl = trimmedUrl + "/"
}
if strings.Index(trimmedUrl, "http") != 0 {
trimmedUrl = "https://" + trimmedUrl
} else {
trimmedUrl = strings.Replace(trimmedUrl, "http://", "https://", 1)
}
if len(trimmedUrl) < 11 {
return "", "", ErrInvalidUrl
}
host := trimmedUrl[8:]
if index := strings.Index(host, "/"); index > 0 {
host = host[0:index]
}
return trimmedUrl, host, nil
}
func TrimUrlForScyllaOld(fullUrl string) (scyllaUrl string, hostName string, err error) {
var data strings.Builder
trimmedUrl := strings.TrimSpace(fullUrl)
urlObject, err := url.Parse(trimmedUrl)
if err != nil {
// remove everything behind ? or #
if index := strings.Index(trimmedUrl, "?"); index > 0 {
trimmedUrl = trimmedUrl[0:index]
}
if index := strings.Index(trimmedUrl, "#"); index > 0 {
trimmedUrl = trimmedUrl[0:index]
}
trimmedUrl = strings.ToValidUTF8(trimmedUrl, "")
urlObject, err = url.Parse(trimmedUrl)
if err != nil {
return "", "", err
}
}
data.Grow(len(urlObject.Host) + len(urlObject.Path) + len("https://") + 1)
data.WriteString("https://")
data.WriteString(urlObject.Host)
data.WriteString(urlObject.Path)
scyllaUrl = data.String()
if scyllaUrl[len(scyllaUrl)-1:] != "/" {
data.WriteString("/")
}
scyllaUrl = data.String()
scyllaUrl = strings.ToValidUTF8(scyllaUrl, "")
return scyllaUrl, urlObject.Host, nil
}
func GetDomainFromUrl(fullUrl string) (string, error) {
fullUrlObject, err := url.Parse(fullUrl)
if err != nil {
return "", err
}
return fullUrlObject.Hostname(), nil
}
// Truncates a given url
func TruncateUrl(value []byte) []byte {
if bytes.Contains(value, GooglePlay) || bytes.Contains(value, ITunesApple) {
return value
}
n := bytes.IndexAny(value, "?#")
if n != -1 {
value = value[0:n]
}
if value[len(value)-1] != byte('/') {
value = append(value, byte('/'))
}
if !bytes.Equal(value[0:4], []byte("http")) {
value = append(HTTPS, value...)
}
if bytes.Equal(value[0:5], HTTPProtocol) {
value = append(HTTPSProtocol, value[5:]...)
}
return value
}