snapshot of https://github.com/centrifugal/centrifugo.git (master, 2026-09-16; blobs > 131072B omitted)
Some checks failed
test / Lint (push) Failing after 3s
test / Test with Go 1.26.8 (push) Failing after 2s

This commit is contained in:
upstream-mirror
2026-09-16 01:17:21 +05:00
commit b352a9a30d
472 changed files with 108375 additions and 0 deletions

46
internal/origin/origin.go Normal file
View File

@@ -0,0 +1,46 @@
package origin
import (
"fmt"
"net/http"
"strings"
"github.com/gobwas/glob"
)
type PatternChecker struct {
allowedOrigins []*glob.Pattern
}
func NewPatternChecker(allowedOrigins []string) (*PatternChecker, error) {
var globs []*glob.Pattern
for _, pattern := range allowedOrigins {
// Origin is matched in lower case (scheme and host are case-insensitive),
// so patterns must be lower-cased too – otherwise a pattern containing
// upper-case letters could never match.
g, err := glob.Compile(strings.ToLower(pattern))
if err != nil {
return nil, fmt.Errorf("malformed origin pattern: %w", err)
}
globs = append(globs, g)
}
return &PatternChecker{
allowedOrigins: globs,
}, nil
}
func (a *PatternChecker) Check(r *http.Request) bool {
origin := r.Header.Get("Origin")
if origin == "" {
return true
}
origin = strings.ToLower(origin)
for _, pattern := range a.allowedOrigins {
if pattern.Match(origin) {
return true
}
}
return false
}