-
Notifications
You must be signed in to change notification settings - Fork 15
/
cors.go
156 lines (129 loc) · 3.58 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package middleware
import (
"math"
"net/http"
"strconv"
"strings"
"time"
"github.com/avenga/couper/config"
"github.com/avenga/couper/errors"
"github.com/avenga/couper/internal/seetie"
)
var _ http.Handler = &CORS{}
type CORS struct {
options *CORSOptions
nextHandler http.Handler
}
type CORSOptions struct {
AllowedOrigins []string
AllowCredentials bool
MaxAge string
methodAllowed methodAllowedFunc
}
func NewCORSOptions(cors *config.CORS, methodAllowed methodAllowedFunc) (*CORSOptions, error) {
if cors == nil {
return nil, nil
}
var corsMaxAge string
if cors.MaxAge != "" {
dur, err := time.ParseDuration(cors.MaxAge)
if err != nil {
return nil, errors.Configuration.With(err).Message("cors max_age")
}
corsMaxAge = strconv.Itoa(int(math.Floor(dur.Seconds())))
}
allowedOrigins := seetie.ValueToStringSlice(cors.AllowedOrigins)
for i, a := range allowedOrigins {
allowedOrigins[i] = strings.ToLower(a)
}
return &CORSOptions{
AllowedOrigins: allowedOrigins,
AllowCredentials: cors.AllowCredentials,
MaxAge: corsMaxAge,
methodAllowed: methodAllowed,
}, nil
}
func (c *CORSOptions) AllowsOrigin(origin string) bool {
if c == nil {
return false
}
for _, a := range c.AllowedOrigins {
if a == strings.ToLower(origin) || a == "*" {
return true
}
}
return false
}
func NewCORSHandler(opts *CORSOptions, nextHandler http.Handler) http.Handler {
if opts == nil {
return nextHandler
}
return &CORS{
options: opts,
nextHandler: nextHandler,
}
}
func (c *CORS) ServeNextHTTP(rw http.ResponseWriter, nextHandler http.Handler, req *http.Request) {
c.setCorsRespHeaders(rw.Header(), req)
if c.isCorsPreflightRequest(req) {
rw.WriteHeader(http.StatusNoContent)
return
}
nextHandler.ServeHTTP(rw, req)
}
func (c *CORS) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
c.ServeNextHTTP(rw, c.nextHandler, req)
}
func (c *CORS) isCorsPreflightRequest(req *http.Request) bool {
return req.Method == http.MethodOptions &&
(req.Header.Get("Access-Control-Request-Method") != "" ||
req.Header.Get("Access-Control-Request-Headers") != "")
}
func (c *CORS) setCorsRespHeaders(headers http.Header, req *http.Request) {
// see https://fetch.spec.whatwg.org/#http-responses
allowSpecificOrigin := false
if c.options.AllowsOrigin("*") && !c.options.AllowCredentials {
headers.Set("Access-Control-Allow-Origin", "*")
} else {
headers.Add("Vary", "Origin")
allowSpecificOrigin = true
}
if !c.isCorsRequest(req) {
return
}
requestOrigin := req.Header.Get("Origin")
if !c.options.AllowsOrigin(requestOrigin) {
return
}
if allowSpecificOrigin {
headers.Set("Access-Control-Allow-Origin", requestOrigin)
}
if c.options.AllowCredentials {
headers.Set("Access-Control-Allow-Credentials", "true")
}
if c.isCorsPreflightRequest(req) {
// Reflect request header value
acrm := req.Header.Get("Access-Control-Request-Method")
if acrm != "" {
if c.options.methodAllowed == nil || c.options.methodAllowed(acrm) {
headers.Set("Access-Control-Allow-Methods", acrm)
}
headers.Add("Vary", "Access-Control-Request-Method")
}
// Reflect request header value
acrh := req.Header.Get("Access-Control-Request-Headers")
if acrh != "" {
headers.Set("Access-Control-Allow-Headers", acrh)
headers.Add("Vary", "Access-Control-Request-Headers")
}
if c.options.MaxAge != "" {
headers.Set("Access-Control-Max-Age", c.options.MaxAge)
}
}
}
func (c *CORS) isCorsRequest(req *http.Request) bool {
return req.Header.Get("Origin") != ""
}
func (c *CORS) Child() http.Handler {
return c.nextHandler
}