diff options
Diffstat (limited to 'human.go')
| -rw-r--r-- | human.go | 19 |
1 files changed, 19 insertions, 0 deletions
@@ -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, ":") |
