// corsWithConfig returns a CORS middleware with config.
// See: `CORS()`.
corsWithConfig returns a CORS middleware with config.
// corsWithConfig returns a CORS middleware with config . // See : ` CORS () ` .
func corsWithConfig(config corsConfig) func(fasthttp.RequestHandler) fasthttp.RequestHandler {
	if len(config.allowOrigins) == 0 {
		config.allowOrigins = defaultCORSConfig.allowOrigins
	}
	if len(config.allowMethods) == 0 {
		config.allowMethods = defaultCORSConfig.allowMethods
	}
	allowMethods := strings.Join(config.allowMethods, ",")
	allowHeaders := strings.Join(config.allowHeaders, ",")
	exposeHeaders := strings.Join(config.exposeHeaders, ",")
	maxAge := strconv.Itoa(config.maxAge)
	return func(next fasthttp.RequestHandler) fasthttp.RequestHandler {
		fn := func(c *fasthttp.RequestCtx) {
			origin := string(c.Request.Header.Peek("Origin"))
			allowOrigin := ""
			for _, o := range config.allowOrigins {
				if o == "*" && config.allowCredentials {
					allowOrigin = origin
					break
				}
				if o == "*" || o == origin {
					allowOrigin = o
					break
				}
				if matchSubdomain(origin, o) {
					allowOrigin = origin
					break
				}
			}
			if string(c.Method()) != http.MethodOptions {
				c.Response.Header.Add("Vary", "Origin")
				c.Response.Header.Set("Access-Control-Allow-Origin", allowOrigin)
				if config.allowCredentials {
					c.Response.Header.Set("Access-Control-Allow-Credentials", "true")
				}
				if exposeHeaders != "" {
					c.Response.Header.Set("Access-Control-Expose-Headers", exposeHeaders)
				}
				next(c)
				return
			}
			c.Response.Header.Add("Vary", "Origin")
			c.Response.Header.Add("Vary", "Access-Control-Request-Method")
			c.Response.Header.Add("Vary", "Access-Control-Request-Headers")
			c.Response.Header.Set("Access-Control-Allow-Origin", allowOrigin)
			c.Response.Header.Set("Access-Control-Allow-Methods", allowMethods)
			if config.allowCredentials {
				c.Response.Header.Set("Access-Control-Allow-Credentials", "true")
			}
			if allowHeaders != "" {
				c.Response.Header.Set("Access-Control-Allow-Headers", allowHeaders)
			} else {
				h := string(c.Response.Header.Peek("Access-Control-Request-Headers"))
				if h != "" {
					c.Response.Header.Set("Access-Control-Allow-Headers", h)
				}
			}
			if config.maxAge > 0 {
				c.Response.Header.Set("Access-Control-Max-Age", maxAge)
			}
			c.Response.SetStatusCode(http.StatusNoContent)
		}
		return fasthttp.RequestHandler(fn)
	}
}
func corsWithConfig ( config corsConfig ) func ( fasthttp . RequestHandler ) fasthttp . RequestHandler { if len ( config . allowOrigins ) == 0 { config . allowOrigins = defaultCORSConfig . allowOrigins } if len ( config . allowMethods ) == 0 { config . allowMethods = defaultCORSConfig . allowMethods } allowMethods := strings . Join ( config . allowMethods , " " ) allowHeaders := strings . Join ( config . allowHeaders , " " ) exposeHeaders := strings . Join ( config . exposeHeaders , " " ) maxAge := strconv . Itoa ( config . maxAge ) return func ( next fasthttp . RequestHandler ) fasthttp . RequestHandler { fn := func ( c * fasthttp . RequestCtx ) { origin := string ( c . Request . Header . Peek ( " " ) ) allowOrigin := " " for _ , o := range config . allowOrigins { if o == " " && config . allowCredentials { allowOrigin = origin break } if o == " " || o == origin { allowOrigin = o break } if matchSubdomain ( origin , o ) { allowOrigin = origin break } } if string ( c . Method ( ) ) != http . MethodOptions { c . Response . Header . Add ( " " , " " ) c . Response . Header . Set ( " " , allowOrigin ) if config . allowCredentials { c . Response . Header . Set ( " " , " " ) } if exposeHeaders != " " { c . Response . Header . Set ( " " , exposeHeaders ) } next ( c ) return } c . Response . Header . Add ( " " , " " ) c . Response . Header . Add ( " " , " " ) c . Response . Header . Add ( " " , " " ) c . Response . Header . Set ( " " , allowOrigin ) c . Response . Header . Set ( " " , allowMethods ) if config . allowCredentials { c . Response . Header . Set ( " " , " " ) } if allowHeaders != " " { c . Response . Header . Set ( " " , allowHeaders ) } else { h := string ( c . Response . Header . Peek ( " " ) ) if h != " " { c . Response . Header . Set ( " " , h ) } } if config . maxAge > 0 { c . Response . Header . Set ( " " , maxAge ) } c . Response . SetStatusCode ( http . StatusNoContent ) } return fasthttp . RequestHandler ( fn ) } }
corsWithConfig returns a CORS middleware with config.
corsWithConfig returns a CORS middleware with config .
// Check allowed origins // Simple request // Preflight request