forked from bluesky-social/indigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl.go
61 lines (55 loc) · 1.14 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
package search
import (
"net/url"
"github.com/PuerkitoBio/purell"
)
var trackingParams = []string{
"__s",
"_ga",
"campaign_id",
"ceid",
"emci",
"emdi",
"fbclid",
"gclid",
"hootPostID",
"mc_eid",
"mkclid",
"mkt_tok",
"msclkid",
"pk_campaign",
"pk_kwd",
"sessionid",
"sourceid",
"utm_campaign",
"utm_content",
"utm_id",
"utm_medium",
"utm_source",
"utm_term",
"xpid",
}
// aggressively normalizes URL, for search indexing and matching. it is possible the URL won't be directly functional after this normalization
func NormalizeLossyURL(raw string) string {
clean, err := purell.NormalizeURLString(raw, purell.FlagsUsuallySafeGreedy|purell.FlagRemoveDirectoryIndex|purell.FlagRemoveFragment|purell.FlagRemoveDuplicateSlashes|purell.FlagRemoveWWW|purell.FlagSortQuery)
if err != nil {
return raw
}
// remove tracking params
u, err := url.Parse(clean)
if err != nil {
return clean
}
if u.RawQuery == "" {
return clean
}
params := u.Query()
// there is probably a more efficient way to do this
for _, p := range trackingParams {
if params.Has(p) {
params.Del(p)
}
}
u.RawQuery = params.Encode()
return u.String()
}