Initial commit: web_hghl_server
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
package webpath
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// LegacyPathRemap 旧版 app 导航路径 → 当前 h5 SSR 实际路由。
|
||||
var LegacyPathRemap = map[string]string{
|
||||
"/avtopic/": "/video/all",
|
||||
"/avtopic": "/video/all",
|
||||
"/dh/": "/nav",
|
||||
"/dh": "/nav",
|
||||
}
|
||||
|
||||
// SkipPublicPaths 当前 web 无对应页面、或不应对外暴露的路径。
|
||||
var SkipPublicPaths = map[string]struct{}{
|
||||
"/topic/": {},
|
||||
"/topic": {},
|
||||
"/history/": {},
|
||||
"/history": {},
|
||||
"/tag/all/": {},
|
||||
"/tag/all": {},
|
||||
"/useragreement/": {},
|
||||
"/useragreement": {},
|
||||
"/privacypolicy/": {},
|
||||
"/privacypolicy": {},
|
||||
"/dmca/": {},
|
||||
"/dmca": {},
|
||||
}
|
||||
|
||||
// NormalizePublicURL 将 URL 规范为 web 可访问地址;第二个返回值 false 表示应跳过。
|
||||
func NormalizePublicURL(raw string) (string, bool) {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return "", false
|
||||
}
|
||||
|
||||
parsed, err := url.Parse(raw)
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
|
||||
path := parsed.Path
|
||||
if path == "" {
|
||||
path = "/"
|
||||
}
|
||||
if !strings.HasPrefix(path, "/") {
|
||||
path = "/" + path
|
||||
}
|
||||
|
||||
normalizedPath := normalizePathKey(path)
|
||||
if _, skip := SkipPublicPaths[normalizedPath]; skip {
|
||||
return "", false
|
||||
}
|
||||
if mapped, ok := LegacyPathRemap[normalizedPath]; ok {
|
||||
path = mapped
|
||||
parsed.Path = path
|
||||
}
|
||||
|
||||
if parsed.Path != "/" {
|
||||
parsed.Path = strings.TrimRight(parsed.Path, "/")
|
||||
if parsed.Path == "" {
|
||||
parsed.Path = "/"
|
||||
}
|
||||
}
|
||||
|
||||
return parsed.String(), true
|
||||
}
|
||||
|
||||
// NormalizePublicPath 规范单条站内 path(不含域名)。
|
||||
func NormalizePublicPath(path string) (string, bool) {
|
||||
path = strings.TrimSpace(path)
|
||||
if path == "" {
|
||||
return "", false
|
||||
}
|
||||
if strings.Contains(path, "://") {
|
||||
return NormalizePublicURL(path)
|
||||
}
|
||||
base := "https://example.com"
|
||||
normalized, ok := NormalizePublicURL(base + path)
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
parsed, err := url.Parse(normalized)
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
return parsed.Path, true
|
||||
}
|
||||
|
||||
func normalizePathKey(path string) string {
|
||||
path = strings.TrimSpace(path)
|
||||
if path == "" {
|
||||
return "/"
|
||||
}
|
||||
if !strings.HasPrefix(path, "/") {
|
||||
path = "/" + path
|
||||
}
|
||||
lower := strings.ToLower(path)
|
||||
if lower != "/" && !strings.HasSuffix(lower, "/") {
|
||||
lower += "/"
|
||||
}
|
||||
return lower
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package webpath
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestNormalizePublicURL(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
raw string
|
||||
want string
|
||||
allow bool
|
||||
}{
|
||||
{"https://91jxporn.com/avtopic/", "https://91jxporn.com/video/all", true},
|
||||
{"https://91jxporn.com/dh/", "https://91jxporn.com/nav", true},
|
||||
{"https://91jxporn.com/richtext?title=test", "https://91jxporn.com/richtext?title=test", true},
|
||||
{"https://91jxporn.com/topic/", "", false},
|
||||
{"https://91jxporn.com/tag/all/", "", false},
|
||||
{"https://91jxporn.com/useragreement/", "", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
got, ok := NormalizePublicURL(tt.raw)
|
||||
if ok != tt.allow {
|
||||
t.Fatalf("NormalizePublicURL(%q) allow = %v, want %v", tt.raw, ok, tt.allow)
|
||||
}
|
||||
if !tt.allow {
|
||||
continue
|
||||
}
|
||||
if got != tt.want {
|
||||
t.Fatalf("NormalizePublicURL(%q) = %q, want %q", tt.raw, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizePublicPath(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
got, ok := NormalizePublicPath("/avtopic/")
|
||||
if !ok || got != "/video/all" {
|
||||
t.Fatalf("NormalizePublicPath(/avtopic/) = %q, %v", got, ok)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user