fix: one decoded path form for crawl keys and CMS lookups

Crawl keys, the rendered comparison and the link graph use the decoded
path (classes.PathKey), the form the resolver, the descriptors and the
SEO entries use; the request URL is percent-encoded again
(classes.EscapeKeyPath). A non-ASCII address now matches its CMS record
and its stored links. A path that does not decode to text keeps its
encoded form.
This commit is contained in:
Jakub Zych
2026-11-04 22:59:17 +01:00
parent 3fdb44bce2
commit e2af5b44c8
5 changed files with 152 additions and 18 deletions

View File

@@ -239,25 +239,51 @@ func (s *Service) LinkSuggestions(ctx context.Context, db *gorm.DB, path string,
return out, nil
}
// sitePath is the path of a path or an absolute URL; ErrBadURL otherwise.
// sitePath is the crawl key of a path or an absolute URL (PathKey with
// the raw query); ErrBadURL otherwise. A path and the absolute URL of the
// same page give the same answer, written decoded or percent-encoded.
func sitePath(raw string) (string, error) {
raw = strings.TrimSpace(raw)
if strings.HasPrefix(raw, "/") && !strings.HasPrefix(raw, "//") {
path, _, _ := strings.Cut(raw, "#")
return path, nil
u, err := url.Parse(raw)
if err != nil {
return "", ErrBadURL
}
return joinQuery(PathKey(u), u.RawQuery), nil
}
u, err := url.Parse(raw)
if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" {
return "", ErrBadURL
}
path := u.EscapedPath()
if path == "" {
path = "/"
return joinQuery(PathKey(u), u.RawQuery), nil
}
// PathKey is the path form of the crawl's addresses (P33-61), the form
// the resolver, the descriptors and the SEO entries use: u's decoded path
// ("/" when empty). A path whose decoded form is not valid UTF-8 without
// NUL, or holds "%" or "?" (which would read as an escape or a query),
// keeps its percent-encoded form.
func PathKey(u *url.URL) string {
if u == nil {
return "/"
}
if u.RawQuery != "" {
path += "?" + u.RawQuery
p := u.Path
if !safeText(p) || strings.ContainsAny(p, "%?") {
p = u.EscapedPath()
}
return path, nil
if p == "" {
p = "/"
}
return p
}
// EscapeKeyPath is the request form of a PathKey path: a decoded path is
// percent-encoded, an encoded one (it holds "%") is kept.
func EscapeKeyPath(p string) string {
if strings.Contains(p, "%") {
return p
}
return (&url.URL{Path: p}).EscapedPath()
}
// LinksOfURL is the internal links of a page addressed by URL (a path or