gin icon indicating copy to clipboard operation
gin copied to clipboard

Issue with custom CORS header middleware

Open mattatcha opened this issue 12 years ago • 29 comments

I have created a the following middleware function but It doesn't seem to be working properly. When I do an options request I get a 404 and I get http: multiple response.WriteHeader calls in my console.

func CORSMiddleware() gin.HandlerFunc {
    return func(c *gin.Context) {

        c.Writer.Header().Set("Content-Type", "application/json")
        c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
        c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
        if c.Req.Method == "OPTIONS" {
            fmt.Println("options")
            c.Abort(200)
            return
        }
        // c.Next()
    }
}

I have mounted it as the before anything else with the following.

r.Use(CORSMiddleware())

mattatcha avatar Jul 03 '14 11:07 mattatcha

What gin/go versions are you on? Can you please post a complete, runnable example? This[0] works fine for me.

[0] https://gist.github.com/alexandernyquist/94580423363d3ec95fa8

pinscript avatar Jul 03 '14 11:07 pinscript

@alexandernyquist I am using the newest version of gin and I am on go1.3 darwin/amd64. (I just did go get -u . before sending this)

Also I have given you and @manucorporat read permissions to the repo. I think there may be too hard to get a runnable example without the rest of the app.

I think the only dependency the app has is Rethinkdb.

Routes: https://github.com/Lanciv/GoGradeAPI/blob/master/handlers/routes.go Main: https://github.com/Lanciv/GoGradeAPI/blob/master/main.go

mattatcha avatar Jul 03 '14 11:07 mattatcha

Thanks for the access, however I only have access to a Windows machine at the moment.

I'll see if I can get a VirtualBox running this weekend.

pinscript avatar Jul 03 '14 11:07 pinscript

"http: multiple response.WriteHeader calls" will be fixed soon with this: https://github.com/gin-gonic/gin/pull/16 Anyway I think that message is just a warning, it guess it should work.

It's incredible that the standard library do not provide any way to know the current status code.

manucorporat avatar Jul 03 '14 12:07 manucorporat

@alexandernyquist I'll get a VM up for you real quick.

mattatcha avatar Jul 03 '14 12:07 mattatcha

@manucorporat Well I get a 404 and the headers aren't set at all :/

mattatcha avatar Jul 03 '14 12:07 mattatcha

Can you guys do a quick review of this: https://github.com/gin-gonic/gin/issues/24 ? once this is merged, your bug can be fixed easily.

manucorporat avatar Jul 03 '14 12:07 manucorporat

@manucorporat I switched to that commit but I am getting the same error.

mattatcha avatar Jul 03 '14 12:07 mattatcha

Of course! that commit do not fix it, but I need to merge that first :)

manucorporat avatar Jul 03 '14 12:07 manucorporat

@manucorporat Do you know when you will have it done?

mattatcha avatar Jul 03 '14 12:07 mattatcha

@alexandernyquist Would you still like me to get you a vm to test with?

mattatcha avatar Jul 03 '14 13:07 mattatcha

@alexandernyquist I just tested your gist and it is returning a 404 to OPTIONS.

mattatcha avatar Jul 03 '14 14:07 mattatcha

it will be fixed today!

manucorporat avatar Jul 03 '14 14:07 manucorporat

@MattAitchison Have you added route for OPTIONS? Unfortunately, there is no OPTIONS-method in the current master branch (PR coming).

Patching gin.go with a OPTIONS-method:

func (group *RouterGroup) OPTIONS(path string, handlers ...HandlerFunc) {
    group.Handle("OPTIONS", path, handlers)
}

And registering the route:

r.OPTIONS("", func (g *gin.Context) {
    g.JSON(200, gin.H{"foo":"bar"})
})

Will work.

pinscript avatar Jul 03 '14 14:07 pinscript

@alexandernyquist Well I would like to handle OPTIONS inside my middleware. I don't believe I should have to mount the middleware and add a route for OPTIONS. I have added the following route which has fixed the problem for now. I hope it's only temporary.

r.Handle("OPTIONS", "/*cors", []gin.HandlerFunc{CORSMiddleware()})

It looks like the 404 handler sends a 404 before my OPTIONS middleware can write.

mattatcha avatar Jul 03 '14 14:07 mattatcha

Ah, I was a bit too quick there. I'll check it out when I get home.

pinscript avatar Jul 03 '14 14:07 pinscript

Just an update, this is already fixed in my local repo

manucorporat avatar Jul 03 '14 16:07 manucorporat

Can you guys try the develop branch? https://github.com/gin-gonic/gin/commit/e1781e2db10b1c3580837491198ea2f7ef3c0ab4

manucorporat avatar Jul 03 '14 22:07 manucorporat

@manucorporat that works like a charm. Nice!

pinscript avatar Jul 04 '14 06:07 pinscript

Works now! Thanks!

mattatcha avatar Jul 04 '14 06:07 mattatcha