Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(options): Added availability to set 200/204 for OPTIONS request status #129

Merged
merged 1 commit into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
36 changes: 21 additions & 15 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
"net/http"
"strings"

"github.com/gin-gonic/gin"

Check failure on line 7 in config.go

View workflow job for this annotation

GitHub Actions / lint

import 'github.com/gin-gonic/gin' is not allowed from list 'Main' (depguard)
)

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
optionsResponseStatusCode int
}

var (
Expand Down Expand Up @@ -48,14 +49,19 @@
}
}

if config.OptionsResponseStatusCode == 0 {
config.OptionsResponseStatusCode = 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(),
optionsResponseStatusCode: config.OptionsResponseStatusCode,
}
}

Expand All @@ -80,7 +86,7 @@

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(cors.optionsResponseStatusCode)
} else {
cors.handleNormal(c)
}
Expand Down
3 changes: 3 additions & 0 deletions cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"strings"
"time"

"github.com/gin-gonic/gin"

Check failure on line 8 in cors.go

View workflow job for this annotation

GitHub Actions / lint

import 'github.com/gin-gonic/gin' is not allowed from list 'Main' (depguard)
)

// Config represents all available options for the middleware.
Expand Down Expand Up @@ -53,6 +53,9 @@

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

// Allows to pass custom OPTIONS response status code for old browsers / clients
OptionsResponseStatusCode int
}

// AddAllowMethods is allowed to add custom methods
Expand Down