-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
140 lines (109 loc) · 2.99 KB
/
option.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package cors
import (
"fmt"
"net/http"
"strings"
"github.com/go-http-utils/headers"
)
// Option returns a configuration func to configurate the
// CORS middleware.
type Option func(*options) error
type options struct {
allowOrigin bool
allowOriginValidator func(*http.Request) string
allowHeaders []string
exposeHeaders []string
maxAge int
credentials bool
methods []string
}
// SetExposeHeaders configures the Access-Control-Expose-Headers CORS header.
func SetExposeHeaders(exposes []string) Option {
return func(o *options) error {
o.exposeHeaders = []string{}
for _, header := range exposes {
normalized := headers.Normalize(header)
if has(o.exposeHeaders, normalized) {
continue
}
o.exposeHeaders = append(o.exposeHeaders, normalized)
}
return nil
}
}
// SetAllowHeaders configures the Access-Control-Allow-Headers CORS header.
func SetAllowHeaders(allows []string) Option {
return func(o *options) error {
o.allowHeaders = []string{}
for _, header := range allows {
normalized := headers.Normalize(header)
if has(o.allowHeaders, normalized) {
continue
}
o.allowHeaders = append(o.allowHeaders, normalized)
}
return nil
}
}
// SetMethods configures the Access-Control-Allow-Methods CORS header.
func SetMethods(methods []string) Option {
return func(o *options) error {
o.methods = []string{}
for _, method := range methods {
normalized := strings.ToUpper(strings.TrimSpace(method))
if has(o.methods, normalized) {
continue
}
o.methods = append(o.methods, normalized)
}
return nil
}
}
// SetMaxAge configures the Access-Control-Max-Age CORS header (in seconds).
// If the maxAge > 600 (10 minutes), then it will be reset to 600 (10 minutes).
func SetMaxAge(maxAge int) Option {
return func(o *options) error {
if maxAge < 0 {
return fmt.Errorf("cors: maxAge should > 0")
}
if maxAge > 600 {
maxAge = 600
}
o.maxAge = maxAge
return nil
}
}
// SetCredentials configures the Access-Control-Allow-Credentials CORS header.
func SetCredentials(credentials bool) Option {
return func(o *options) error {
o.credentials = credentials
return nil
}
}
// SetAllowOrigin configures the Access-Control-Allow-Origin CORS header.
// Set to true to reflect the request origin. Set to false to disable
// CORS.
func SetAllowOrigin(allow bool) Option {
return func(o *options) error {
o.allowOrigin = allow
return nil
}
}
// SetAllowOriginValidator configures the Access-Control-Allow-Origin CORS
// header by run the validator function `func(*http.Request) string`.
// The validator function accpets an `*http.Request` argument and return
// the Access-Control-Allow-Origin value.
func SetAllowOriginValidator(validator func(*http.Request) string) Option {
return func(o *options) error {
o.allowOriginValidator = validator
return nil
}
}
func has(hs []string, h string) bool {
for _, e := range hs {
if e == h {
return true
}
}
return false
}