You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

matchlist.go 901B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package matchlist
  5. import (
  6. "strings"
  7. "github.com/gobwas/glob"
  8. )
  9. // Matchlist represents a block or allow list
  10. type Matchlist struct {
  11. ruleGlobs []glob.Glob
  12. }
  13. // NewMatchlist creates a new block or allow list
  14. func NewMatchlist(rules ...string) (*Matchlist, error) {
  15. for i := range rules {
  16. rules[i] = strings.ToLower(rules[i])
  17. }
  18. list := Matchlist{
  19. ruleGlobs: make([]glob.Glob, 0, len(rules)),
  20. }
  21. for _, rule := range rules {
  22. rg, err := glob.Compile(rule)
  23. if err != nil {
  24. return nil, err
  25. }
  26. list.ruleGlobs = append(list.ruleGlobs, rg)
  27. }
  28. return &list, nil
  29. }
  30. // Match will matches
  31. func (b *Matchlist) Match(u string) bool {
  32. for _, r := range b.ruleGlobs {
  33. if r.Match(u) {
  34. return true
  35. }
  36. }
  37. return false
  38. }