-
Notifications
You must be signed in to change notification settings - Fork 8
/
cors.go
111 lines (97 loc) · 3.39 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package filter
import (
"net/http"
"strconv"
"strings"
"github.com/cuigh/auxo/net/web"
)
// CORS represents a Cross-Origin Resource Sharing (CORS) filter.
type CORS struct {
// AllowOrigin defines a list of origins that may access the resource.
// Optional. Default value []string{"*"}.
AllowOrigins []string
// AllowMethods defines a list methods allowed when accessing the resource.
// This is used in response to a preflight request.
// Optional. Default value DefaultCORSConfig.AllowMethods.
AllowMethods []string
// AllowHeaders defines a list of request headers that can be used when
// making the actual request. This in response to a preflight request.
// Optional. Default value []string{}.
AllowHeaders []string
// AllowCredentials indicates whether or not the response to the request
// can be exposed when the credentials flag is true. When used as part of
// a response to a preflight request, this indicates whether or not the
// actual request can be made using credentials.
// Optional. Default value false.
AllowCredentials bool
// ExposeHeaders defines a whitelist headers that clients are allowed to
// access.
// Optional. Default value []string{}.
ExposeHeaders []string
// MaxAge indicates how long (in seconds) the results of a preflight request
// can be cached.
// Optional. Default value 0.
MaxAge int
}
// Apply implements `web.Filter` interface.
func (c *CORS) Apply(next web.HandlerFunc) web.HandlerFunc {
if len(c.AllowOrigins) == 0 {
c.AllowOrigins = []string{"*"}
}
if len(c.AllowMethods) == 0 {
c.AllowMethods = []string{http.MethodGet, http.MethodPost, http.MethodHead, http.MethodPut, http.MethodPatch, http.MethodDelete}
}
allowMethods := strings.Join(c.AllowMethods, ",")
allowHeaders := strings.Join(c.AllowHeaders, ",")
exposeHeaders := strings.Join(c.ExposeHeaders, ",")
maxAge := strconv.Itoa(c.MaxAge)
return func(ctx web.Context) error {
r := ctx.Request()
w := ctx.Response()
origin := r.Header.Get(web.HeaderOrigin)
if origin == "" {
return next(ctx)
}
// Check allowed origins
var allowOrigin string
for _, o := range c.AllowOrigins {
if o == "*" || o == origin {
allowOrigin = o
break
}
}
// Simple request
if r.Method != http.MethodOptions {
w.Header().Add(web.HeaderVary, web.HeaderOrigin)
w.Header().Set(web.HeaderAccessControlAllowOrigin, allowOrigin)
if c.AllowCredentials {
w.Header().Set(web.HeaderAccessControlAllowCredentials, "true")
}
if exposeHeaders != "" {
w.Header().Set(web.HeaderAccessControlExposeHeaders, exposeHeaders)
}
return next(ctx)
}
// Preflight request
w.Header().Add(web.HeaderVary, web.HeaderOrigin)
w.Header().Add(web.HeaderVary, web.HeaderAccessControlRequestMethod)
w.Header().Add(web.HeaderVary, web.HeaderAccessControlRequestHeaders)
w.Header().Set(web.HeaderAccessControlAllowOrigin, allowOrigin)
w.Header().Set(web.HeaderAccessControlAllowMethods, allowMethods)
if c.AllowCredentials {
w.Header().Set(web.HeaderAccessControlAllowCredentials, "true")
}
if allowHeaders != "" {
w.Header().Set(web.HeaderAccessControlAllowHeaders, allowHeaders)
} else {
h := r.Header.Get(web.HeaderAccessControlRequestHeaders)
if h != "" {
w.Header().Set(web.HeaderAccessControlAllowHeaders, h)
}
}
if c.MaxAge > 0 {
w.Header().Set(web.HeaderAccessControlMaxAge, maxAge)
}
return ctx.Status(http.StatusNoContent).Empty()
}
}