67 lines
1.5 KiB
Go
67 lines
1.5 KiB
Go
package rules
|
|
|
|
import (
|
|
"strings"
|
|
)
|
|
|
|
// matchesPattern checks if a value matches a pattern
|
|
// Supports wildcards: "*" matches any sequence of characters
|
|
// Examples:
|
|
// - "application/*" matches "application/pdf", "application/json"
|
|
// - "*.github.com" matches "api.github.com", "www.github.com"
|
|
// - "example.com" matches only "example.com"
|
|
func matchesPattern(pattern, value string) bool {
|
|
// Exact match
|
|
if pattern == value {
|
|
return true
|
|
}
|
|
|
|
// Wildcard matching
|
|
if strings.Contains(pattern, "*") {
|
|
// Convert pattern to regex-like matching
|
|
parts := strings.Split(pattern, "*")
|
|
if len(parts) == 0 {
|
|
return false
|
|
}
|
|
|
|
// Pattern must start with first part
|
|
if parts[0] != "" && !strings.HasPrefix(value, parts[0]) {
|
|
return false
|
|
}
|
|
|
|
// Pattern must end with last part
|
|
if len(parts) > 1 && parts[len(parts)-1] != "" {
|
|
if !strings.HasSuffix(value, parts[len(parts)-1]) {
|
|
return false
|
|
}
|
|
}
|
|
|
|
// Check all parts are present in order
|
|
remaining := value
|
|
if parts[0] != "" {
|
|
remaining = strings.TrimPrefix(remaining, parts[0])
|
|
}
|
|
|
|
for i := 1; i < len(parts)-1; i++ {
|
|
if parts[i] == "" {
|
|
continue
|
|
}
|
|
idx := strings.Index(remaining, parts[i])
|
|
if idx == -1 {
|
|
return false
|
|
}
|
|
remaining = remaining[idx+len(parts[i]):]
|
|
}
|
|
|
|
if len(parts) > 1 && parts[len(parts)-1] != "" {
|
|
if !strings.HasSuffix(remaining, parts[len(parts)-1]) {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
// Partial match (contains)
|
|
return strings.Contains(value, pattern)
|
|
}
|