feat: 添加cqjson处理和url处理

This commit is contained in:
lixiangwuxian
2024-10-13 15:18:43 +08:00
parent 22434ae5dd
commit 97234edda8
3 changed files with 72 additions and 3 deletions

33
util/url.go Normal file
View File

@@ -0,0 +1,33 @@
package util
import (
"net/url"
"strings"
)
// isEquivalentURL 判断两个 URL 是否在规范化后相同
func IsEquivalentURL(url1, url2 string) bool {
norm1 := normalizeURL(url1)
norm2 := normalizeURL(url2)
return norm1 == norm2
}
// normalizeURL 规范化 URL移除末尾斜杠、协议标准化、移除 index.html
func normalizeURL(rawURL string) string {
u, err := url.Parse(rawURL)
if err != nil {
return rawURL
}
// 将 http 和 https 视为同一种协议
u.Scheme = "https"
// 移除尾部的 /index.html 或 .html
u.Path = strings.TrimSuffix(u.Path, "/index.html")
u.Path = strings.TrimSuffix(u.Path, ".html")
// 移除末尾的 /
u.Path = strings.TrimRight(u.Path, "/")
return u.String()
}