aboutsummaryrefslogtreecommitdiff
path: root/human.go
diff options
context:
space:
mode:
Diffstat (limited to 'human.go')
-rw-r--r--human.go19
1 files changed, 19 insertions, 0 deletions
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, ":")