Skip to content

Commit 2d17fdf

Browse files
authored
Merge pull request #195 from ipfs/fix/go-ipfs-7242
Fix go-ipfs#7242: Remove "HEAD" from Allow methods
2 parents 90c723d + 31eee8c commit 2d17fdf

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

http/errors_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func TestUnhandledMethod(t *testing.T) {
165165
AllowGet: false,
166166
Code: http.StatusMethodNotAllowed,
167167
ResHeaders: map[string]string{
168-
"Allow": "POST, HEAD, OPTIONS",
168+
"Allow": "OPTIONS, POST",
169169
},
170170
}
171171
tc.test(t)

http/handler.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"net/http"
77
"runtime/debug"
8+
"strings"
89
"sync"
910
"time"
1011

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

108109
// Tell the user the allowed methods, and return.
109-
setAllowedHeaders(w, h.cfg.AllowGet)
110+
setAllowHeader(w, h.cfg.AllowGet)
110111
w.WriteHeader(http.StatusNoContent)
111112
return
112113
case http.MethodPost:
@@ -116,7 +117,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
116117
}
117118
fallthrough
118119
default:
119-
setAllowedHeaders(w, h.cfg.AllowGet)
120+
setAllowHeader(w, h.cfg.AllowGet)
120121
http.Error(w, "405 - Method Not Allowed", http.StatusMethodNotAllowed)
121122
log.Warnf("The IPFS API does not support %s requests.", r.Method)
122123
return
@@ -191,11 +192,10 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
191192
h.root.Call(req, re, h.env)
192193
}
193194

194-
func setAllowedHeaders(w http.ResponseWriter, allowGet bool) {
195-
w.Header().Add("Allow", http.MethodHead)
196-
w.Header().Add("Allow", http.MethodOptions)
197-
w.Header().Add("Allow", http.MethodPost)
195+
func setAllowHeader(w http.ResponseWriter, allowGet bool) {
196+
allowedMethods := []string{http.MethodOptions, http.MethodPost}
198197
if allowGet {
199-
w.Header().Add("Allow", http.MethodGet)
198+
allowedMethods = append(allowedMethods, http.MethodHead, http.MethodGet)
200199
}
200+
w.Header().Set("Allow", strings.Join(allowedMethods, ", "))
201201
}

0 commit comments

Comments
 (0)