Skip to content
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

Fix go-ipfs#7242: Remove "HEAD" from Allow methods #195

Merged
merged 4 commits into from Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion http/errors_test.go
Expand Up @@ -165,7 +165,7 @@ func TestUnhandledMethod(t *testing.T) {
AllowGet: false,
Code: http.StatusMethodNotAllowed,
ResHeaders: map[string]string{
"Allow": "POST, HEAD, OPTIONS",
"Allow": "OPTIONS, POST",
},
}
tc.test(t)
Expand Down
14 changes: 7 additions & 7 deletions http/handler.go
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"net/http"
"runtime/debug"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -106,7 +107,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// The CORS library handles all other requests.

// Tell the user the allowed methods, and return.
setAllowedHeaders(w, h.cfg.AllowGet)
setAllowHeader(w, h.cfg.AllowGet)
w.WriteHeader(http.StatusNoContent)
return
case http.MethodPost:
Expand All @@ -116,7 +117,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
fallthrough
default:
setAllowedHeaders(w, h.cfg.AllowGet)
setAllowHeader(w, h.cfg.AllowGet)
http.Error(w, "405 - Method Not Allowed", http.StatusMethodNotAllowed)
log.Warnf("The IPFS API does not support %s requests.", r.Method)
return
Expand Down Expand Up @@ -191,11 +192,10 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.root.Call(req, re, h.env)
}

func setAllowedHeaders(w http.ResponseWriter, allowGet bool) {
w.Header().Add("Allow", http.MethodHead)
w.Header().Add("Allow", http.MethodOptions)
w.Header().Add("Allow", http.MethodPost)
func setAllowHeader(w http.ResponseWriter, allowGet bool) {
allowedMethods := []string{http.MethodOptions, http.MethodPost}
if allowGet {
w.Header().Add("Allow", http.MethodGet)
allowedMethods = append(allowedMethods, http.MethodHead, http.MethodGet)
}
w.Header().Set("Allow", strings.Join(allowedMethods, ", "))
}