mirror of
https://github.com/kataras/iris.git
synced 2025-02-09 02:34:55 +01:00
cors: add vary header
looking for a CORS middleware with more options? Take a look at iris-contrib/middleware/cors instead
This commit is contained in:
parent
da50af4ffd
commit
f397c30caf
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
# News
|
# News
|
||||||
|
|
||||||
> This is the under-**development branch**. Stay tuned for the upcoming release [v12.2.0](HISTORY.md#Next). Looking for a stable release? Head over to the [v12.1.8 branch](https://github.com/kataras/iris/tree/v12.1.8) instead.
|
> This is the under-**development branch** - contains the latest and greatest features. Stay tuned for the upcoming release [v12.2.0](HISTORY.md#Next). Looking for a more stable release? Head over to the [v12.1.8 branch](https://github.com/kataras/iris/tree/v12.1.8) instead.
|
||||||
>
|
>
|
||||||
> ![](https://iris-go.com/images/cli.png) Try the official [Iris Command Line Interface](https://github.com/kataras/iris-cli) today!
|
> ![](https://iris-go.com/images/cli.png) Try the official [Iris Command Line Interface](https://github.com/kataras/iris-cli) today!
|
||||||
|
|
||||||
|
|
|
@ -267,17 +267,30 @@ const (
|
||||||
allowCredentialsHeader = "Access-Control-Allow-Credentials"
|
allowCredentialsHeader = "Access-Control-Allow-Credentials"
|
||||||
referrerPolicyHeader = "Referrer-Policy"
|
referrerPolicyHeader = "Referrer-Policy"
|
||||||
exposeHeadersHeader = "Access-Control-Expose-Headers"
|
exposeHeadersHeader = "Access-Control-Expose-Headers"
|
||||||
|
requestMethodHeader = "Access-Control-Request-Method"
|
||||||
allowMethodsHeader = "Access-Control-Allow-Methods"
|
requestHeadersHeader = "Access-Control-Request-Headers"
|
||||||
allowAllMethodsValue = "*"
|
allowMethodsHeader = "Access-Control-Allow-Methods"
|
||||||
allowHeadersHeader = "Access-Control-Allow-Headers"
|
allowAllMethodsValue = "*"
|
||||||
maxAgeHeader = "Access-Control-Max-Age"
|
allowHeadersHeader = "Access-Control-Allow-Headers"
|
||||||
|
maxAgeHeader = "Access-Control-Max-Age"
|
||||||
|
varyHeader = "Vary"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (c *CORS) addVaryHeaders(ctx *context.Context) {
|
||||||
|
ctx.Header(varyHeader, originRequestHeader)
|
||||||
|
|
||||||
|
if ctx.Method() == http.MethodOptions {
|
||||||
|
ctx.Header(varyHeader, requestMethodHeader)
|
||||||
|
ctx.Header(varyHeader, requestHeadersHeader)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Handler method returns the Iris CORS Handler with basic features.
|
// Handler method returns the Iris CORS Handler with basic features.
|
||||||
// Note that the caller should NOT modify any of the CORS instance fields afterwards.
|
// Note that the caller should NOT modify any of the CORS instance fields afterwards.
|
||||||
func (c *CORS) Handler() context.Handler {
|
func (c *CORS) Handler() context.Handler {
|
||||||
return func(ctx *context.Context) {
|
return func(ctx *context.Context) {
|
||||||
|
c.addVaryHeaders(ctx) // add vary headers at any case.
|
||||||
|
|
||||||
origin, ok := c.extractOriginFunc(ctx)
|
origin, ok := c.extractOriginFunc(ctx)
|
||||||
if !ok || !c.allowOriginFunc(ctx, origin) {
|
if !ok || !c.allowOriginFunc(ctx, origin) {
|
||||||
c.errorHandler(ctx, ErrOriginNotAllowed)
|
c.errorHandler(ctx, ErrOriginNotAllowed)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user