-
Notifications
You must be signed in to change notification settings - Fork 229
Description
I'm not sure how general this is, but in the framework I'm using (gin, which is very similar to martini), when I use c.HandlerFunc I get 404s for OPTIONS requests, even with OptionsPassthrough set to false.
I was able to fix this in the short term by always setting the status code to 200 for valid OPTIONS requests.
Here's what I had before that failed with a 404
# Fails with 404 on OPTIONS request
r := gin.Default()
r.Use(gin.WrapF(c.HandlerFunc))
Here's what works for OPTIONS requests
makeCorsHandler := func(c *cors.Cors) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
c.HandlerFunc(w, r)
// Allow it to return to avoid a 404
if r.Method == "OPTIONS" && w.Header().Get("Access-Control-Allow-Origin") == r.Header.Get("Origin") {
w.WriteHeader(http.StatusOK)
}
}
}
Really I should be checking if optionPassthrough is false, but I don't have access to that property here. Also, checking that Access-Control-Allow-Origin is set to the correct value is kind of a hack that just works because that header value isn't set until the request has made it past all the relevant cross domain checks.
However you want to handle it, making the OptionsPassthrough option make more sense when using HandlerFunc would be helpful. That may mean just documenting this case, or providing an option to handle this automatically by always returning 200s. I couldn't think of a very clean way to do it, hence the bug report and no PR. :)
Please let me know if there is any testing I can do to help. I certainly appreciate your work on this library!