From ae5dfd8d0dd610083428ed9ac326ca7fc26a8302 Mon Sep 17 00:00:00 2001 From: Alex Edwards Date: Wed, 25 Jul 2018 11:44:48 +0200 Subject: [PATCH] include Allow header for 405 Method Not Allowed responses --- helper.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/helper.go b/helper.go index af855b0..46b239f 100644 --- a/helper.go +++ b/helper.go @@ -8,6 +8,7 @@ package bone import ( + "fmt" "net/http" "net/url" "strings" @@ -155,16 +156,27 @@ func extractQueries(req *http.Request) (bool, map[string][]string) { } func (m *Mux) otherMethods(rw http.ResponseWriter, req *http.Request) bool { + allowed := []string{} for _, met := range method { if met != req.Method { for _, r := range m.Routes[met] { ok := r.exists(rw, req) if ok { - rw.WriteHeader(http.StatusMethodNotAllowed) - return true + allowed = append(allowed, r.Method) } } } } + if len(allowed) > 0 { + // Build the Allow header. Append HEAD to the list of allowed methods if necessary. + allowHeader := strings.Join(allowed, ", ") + if strings.Contains(allowHeader, "GET") && !strings.Contains(allowHeader, "HEAD") { + allowHeader = fmt.Sprintf("%s, HEAD", allowHeader) + } + + rw.Header().Set("Allow", allowHeader) + http.Error(rw, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) + return true + } return false }