Skip to content

Commit

Permalink
Origin wildcard
Browse files Browse the repository at this point in the history
  • Loading branch information
cv65kr committed Nov 4, 2023
1 parent e22a7c2 commit dbef1f6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ type Config struct {
// CORSConfig headers configuration.
type CORSConfig struct {
// AllowedOrigin: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
AllowedOrigin string `mapstructure:"allowed_origin"`
AllowedOrigin string `mapstructure:"allowed_origin"`
AllowedOriginWildcard string `mapstructure:"allowed_origin_wildcard"`
// AllowedHeaders: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers
AllowedHeaders string `mapstructure:"allowed_headers"`
// AllowedMethods: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Methods
Expand Down
8 changes: 8 additions & 0 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package headers
import (
"fmt"
"net/http"
"regexp"
"strings"

"github.com/roadrunner-server/errors"
Expand Down Expand Up @@ -70,6 +71,13 @@ func (p *Plugin) Init(cfg Configurer) error {
opts.AllowedOrigins = strings.Split(p.cfg.CORS.AllowedOrigin, ",")
}

// if this option is set, the content of `AllowedOrigins` is ignored
if p.cfg.CORS.AllowedOriginWildcard != "" {
opts.AllowOriginFunc = func(origin string) bool {
return regexp.MustCompile(p.cfg.CORS.AllowedOriginWildcard).MatchString(origin)
}
}

if p.cfg.CORS.AllowedMethods != "" {
// trim all spaces
p.cfg.CORS.AllowedMethods = strings.Trim(p.cfg.CORS.AllowedMethods, " ")
Expand Down

0 comments on commit dbef1f6

Please sign in to comment.