Skip to content

Commit

Permalink
Added availability to set 200/204 for OPTIONS request status
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmytro Kishchenko committed Nov 10, 2023
1 parent ee4df80 commit 31e8bd1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ _testmain.go
*.prof

coverage.out

.idea
38 changes: 22 additions & 16 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
)

type cors struct {
allowAllOrigins bool
allowCredentials bool
allowOriginFunc func(string) bool
allowOrigins []string
normalHeaders http.Header
preflightHeaders http.Header
wildcardOrigins [][]string
allowAllOrigins bool
allowCredentials bool
allowOriginFunc func(string) bool
allowOrigins []string
normalHeaders http.Header
preflightHeaders http.Header
wildcardOrigins [][]string
optionsRequestStatus int
}

var (
Expand Down Expand Up @@ -48,18 +49,23 @@ func newCors(config Config) *cors {
}
}

if config.OptionsRequestStatus == 0 {
config.OptionsRequestStatus = http.StatusNoContent
}

return &cors{
allowOriginFunc: config.AllowOriginFunc,
allowAllOrigins: config.AllowAllOrigins,
allowCredentials: config.AllowCredentials,
allowOrigins: normalize(config.AllowOrigins),
normalHeaders: generateNormalHeaders(config),
preflightHeaders: generatePreflightHeaders(config),
wildcardOrigins: config.parseWildcardRules(),
allowOriginFunc: config.AllowOriginFunc,
allowAllOrigins: config.AllowAllOrigins,
allowCredentials: config.AllowCredentials,
allowOrigins: normalize(config.AllowOrigins),
normalHeaders: generateNormalHeaders(config),
preflightHeaders: generatePreflightHeaders(config),
wildcardOrigins: config.parseWildcardRules(),
optionsRequestStatus: config.OptionsRequestStatus,
}
}

func (cors *cors) applyCors(c *gin.Context) {
func (cors *cors) applyCors(c *gin.Context, optionsRequestStatus *int) {
origin := c.Request.Header.Get("Origin")
if len(origin) == 0 {
// request is not a CORS request
Expand All @@ -80,7 +86,7 @@ func (cors *cors) applyCors(c *gin.Context) {

if c.Request.Method == "OPTIONS" {
cors.handlePreflight(c)
defer c.AbortWithStatus(http.StatusNoContent) // Using 204 is better than 200 when the request status is OPTIONS
defer c.AbortWithStatus(*optionsRequestStatus) // Added availability to set 200/204 for OPTIONS request status
} else {
cors.handleNormal(c)
}
Expand Down
5 changes: 4 additions & 1 deletion cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ type Config struct {

// Allows usage of file:// schema (dangerous!) use it only when you 100% sure it's needed
AllowFiles bool

// Allows to pass custom OPTIONS request status code for old browsers / clients
OptionsRequestStatus int
}

// AddAllowMethods is allowed to add custom methods
Expand Down Expand Up @@ -163,6 +166,6 @@ func Default() gin.HandlerFunc {
func New(config Config) gin.HandlerFunc {
cors := newCors(config)
return func(c *gin.Context) {
cors.applyCors(c)
cors.applyCors(c, &cors.optionsRequestStatus)
}
}

0 comments on commit 31e8bd1

Please sign in to comment.