Skip to content

Include Allow header for 405 Method Not Allowed responses #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package bone

import (
"fmt"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -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
}