Browse Source

Add OPTIONS to the list of allowed methods

Carl Jackson 12 years ago
parent
commit
fb3ce04ee4
1 changed files with 12 additions and 0 deletions
  1. +12
    -0
      web/middleware/options.go

+ 12
- 0
web/middleware/options.go View File

@ -36,6 +36,7 @@ func AutomaticOptions(c *web.C, h http.Handler) http.Handler {
methods := getValidMethods(*c) methods := getValidMethods(*c)
if fw.Code == http.StatusNotFound && methods != nil { if fw.Code == http.StatusNotFound && methods != nil {
methods = addMethod(methods, "OPTIONS")
w.Header().Set("Allow", strings.Join(methods, ", ")) w.Header().Set("Allow", strings.Join(methods, ", "))
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} else { } else {
@ -61,3 +62,14 @@ func getValidMethods(c web.C) []string {
return nil return nil
} }
} }
// Assumption: the list of methods is teensy, and that anything we could
// possibly want to do here is going to be fast.
func addMethod(methods []string, method string) []string {
for _, m := range methods {
if m == method {
return methods
}
}
return append(methods, method)
}

Loading…
Cancel
Save