mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
The Gin middleware is being used if one or more Origins are specified. Default setup for each Origin is as follows: - GET,POST, PUT, HEAD methods allowed - Credentials share disabled - Preflight requests cached for 12 hours Which are the defaults gin-contrib/cors comes with out of the box. Gin-cors will return a 403 if it gets a request with an Origin header that isn't on its' list. If no Origin header is specified then it will just return the servers response. Start fn with CORS enabled: `API_CORS="http://localhost:4000, http://localhost:3000" make run`
86 lines
2.3 KiB
Go
86 lines
2.3 KiB
Go
package cors
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type converter func(string) string
|
|
|
|
func generateNormalHeaders(c Config) http.Header {
|
|
headers := make(http.Header)
|
|
if c.AllowCredentials {
|
|
headers.Set("Access-Control-Allow-Credentials", "true")
|
|
}
|
|
if len(c.ExposeHeaders) > 0 {
|
|
exposeHeaders := convert(normalize(c.ExposeHeaders), http.CanonicalHeaderKey)
|
|
headers.Set("Access-Control-Expose-Headers", strings.Join(exposeHeaders, ","))
|
|
}
|
|
if c.AllowAllOrigins {
|
|
headers.Set("Access-Control-Allow-Origin", "*")
|
|
} else {
|
|
headers.Set("Vary", "Origin")
|
|
}
|
|
return headers
|
|
}
|
|
|
|
func generatePreflightHeaders(c Config) http.Header {
|
|
headers := make(http.Header)
|
|
if c.AllowCredentials {
|
|
headers.Set("Access-Control-Allow-Credentials", "true")
|
|
}
|
|
if len(c.AllowMethods) > 0 {
|
|
allowMethods := convert(normalize(c.AllowMethods), strings.ToUpper)
|
|
value := strings.Join(allowMethods, ",")
|
|
headers.Set("Access-Control-Allow-Methods", value)
|
|
}
|
|
if len(c.AllowHeaders) > 0 {
|
|
allowHeaders := convert(normalize(c.AllowHeaders), http.CanonicalHeaderKey)
|
|
value := strings.Join(allowHeaders, ",")
|
|
headers.Set("Access-Control-Allow-Headers", value)
|
|
}
|
|
if c.MaxAge > time.Duration(0) {
|
|
value := strconv.FormatInt(int64(c.MaxAge/time.Second), 10)
|
|
headers.Set("Access-Control-Max-Age", value)
|
|
}
|
|
if c.AllowAllOrigins {
|
|
headers.Set("Access-Control-Allow-Origin", "*")
|
|
} else {
|
|
// Always set Vary headers
|
|
// see https://github.com/rs/cors/issues/10,
|
|
// https://github.com/rs/cors/commit/dbdca4d95feaa7511a46e6f1efb3b3aa505bc43f#commitcomment-12352001
|
|
|
|
headers.Add("Vary", "Origin")
|
|
headers.Add("Vary", "Access-Control-Request-Method")
|
|
headers.Add("Vary", "Access-Control-Request-Headers")
|
|
}
|
|
return headers
|
|
}
|
|
|
|
func normalize(values []string) []string {
|
|
if values == nil {
|
|
return nil
|
|
}
|
|
distinctMap := make(map[string]bool, len(values))
|
|
normalized := make([]string, 0, len(values))
|
|
for _, value := range values {
|
|
value = strings.TrimSpace(value)
|
|
value = strings.ToLower(value)
|
|
if _, seen := distinctMap[value]; !seen {
|
|
normalized = append(normalized, value)
|
|
distinctMap[value] = true
|
|
}
|
|
}
|
|
return normalized
|
|
}
|
|
|
|
func convert(s []string, c converter) []string {
|
|
var out []string
|
|
for _, i := range s {
|
|
out = append(out, c(i))
|
|
}
|
|
return out
|
|
}
|