From 15cf6bb73408f9568c335be894d34372b5589a1d Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Sun, 15 Mar 2026 14:22:15 +0100 Subject: feat(url): check match canonical --- human.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'human.go') diff --git a/human.go b/human.go index d78d31f..e2ac134 100644 --- a/human.go +++ b/human.go @@ -23,6 +23,25 @@ var ( // URL represents a standard [url.URL] following normalization rules of human.json. type URL url.URL +func NewURL(s string) (*URL, error) { + raw, err := url.Parse(s) + if err != nil { + return nil, err + } + u := URL(*raw) + return &u, nil +} + +// Match returns true if url is within the scope. +// See https://codeberg.org/robida/human.json#url-matching. +func (u *URL) Match(url *URL) bool { + u.normalize() + url.normalize() + return u.Scheme == url.Scheme && + u.Host == url.Host && + (u.Path == url.Path || strings.HasPrefix(url.Path, u.Path+"/")) +} + func (u *URL) normalize() { if strings.Contains(u.Host, ":") { sp := strings.Split(u.Host, ":") -- cgit v1.2.3