forked from goadesign/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcors.go
47 lines (40 loc) · 1.08 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
45
46
47
/*
Package cors provides the means for implementing the server side of CORS,
see https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS.
*/
package cors
import (
"net/http"
"strings"
"golang.org/x/net/context"
"github.com/goadesign/goa"
)
// key is the private type used to key context values.
type key string
// OriginKey is the context key used to store the request origin match
const OriginKey key = "origin"
// MatchOrigin returns true if the given Origin header value matches the
// origin specification.
func MatchOrigin(origin, spec string) bool {
if spec == "*" {
return true
}
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
}
// HandlePreflight returns a simple 200 response. The middleware takes care of handling CORS.
func HandlePreflight() goa.Handler {
return func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
rw.WriteHeader(200)
return nil
}
}