forked from goadesign/plugins
/
cors.go
44 lines (39 loc) · 1.09 KB
/
cors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package cors
import (
"regexp"
"strings"
)
// MatchOrigin returns true if the given Origin header value matches the
// origin specification.
// Spec can be one of:
// - a plain string identifying an origin. eg http://swagger.goa.design
// - a plain string containing a wildcard. eg *.goa.design
// - the special string * that matches every host
func MatchOrigin(origin, spec string) bool {
if spec == "*" {
return true
}
// Check regular expression
if strings.HasPrefix(spec, "/") && strings.HasSuffix(spec, "/") {
stripped := strings.Trim(spec, "/")
r := regexp.MustCompile(stripped)
return MatchOriginRegexp(origin, r)
}
if !strings.Contains(spec, "*") {
return origin == spec
}
parts := strings.SplitN(spec, "*", 2)
if !strings.HasPrefix(origin, parts[0]) {
return false
}
if !strings.HasSuffix(origin, parts[1]) {
return false
}
return true
}
// MatchOriginRegexp returns true if the given Origin header value matches the
// origin specification.
// Spec must be a valid regex
func MatchOriginRegexp(origin string, spec *regexp.Regexp) bool {
return spec.Match([]byte(origin))
}