2019-09-20 17:48:57 +08:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
nurl "net/url"
|
2022-03-28 03:01:39 +08:00
|
|
|
"sort"
|
2019-09-20 17:48:57 +08:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2022-03-28 03:01:39 +08:00
|
|
|
// queryEncodeWithoutEmptyValues is a copy of `values.Encode` but checking if the queryparam
|
|
|
|
// value is empty to prevent sending the = symbol empty which breaks in some servers.
|
|
|
|
func queryEncodeWithoutEmptyValues(v nurl.Values) string {
|
|
|
|
if v == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
var buf strings.Builder
|
|
|
|
keys := make([]string, 0, len(v))
|
|
|
|
for k := range v {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
for _, k := range keys {
|
|
|
|
vs := v[k]
|
|
|
|
keyEscaped := nurl.QueryEscape(k)
|
|
|
|
for _, v := range vs {
|
|
|
|
if buf.Len() > 0 {
|
|
|
|
buf.WriteByte('&')
|
|
|
|
}
|
|
|
|
buf.WriteString(keyEscaped)
|
|
|
|
if v != "" {
|
|
|
|
buf.WriteByte('=')
|
|
|
|
buf.WriteString(nurl.QueryEscape(v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2019-09-20 17:48:57 +08:00
|
|
|
// RemoveUTMParams removes the UTM parameters from URL.
|
|
|
|
func RemoveUTMParams(url string) (string, error) {
|
|
|
|
// Parse string URL
|
|
|
|
tmp, err := nurl.Parse(url)
|
|
|
|
if err != nil || tmp.Scheme == "" || tmp.Hostname() == "" {
|
|
|
|
return url, fmt.Errorf("URL is not valid")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove UTM queries
|
|
|
|
queries := tmp.Query()
|
|
|
|
for key := range queries {
|
|
|
|
if strings.HasPrefix(key, "utm_") {
|
|
|
|
queries.Del(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp.Fragment = ""
|
2022-03-28 03:01:39 +08:00
|
|
|
tmp.RawQuery = queryEncodeWithoutEmptyValues(queries)
|
2019-09-20 17:48:57 +08:00
|
|
|
return tmp.String(), nil
|
|
|
|
}
|