Unverified Commit 2d17fdf5 authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #195 from ipfs/fix/go-ipfs-7242

Fix go-ipfs#7242: Remove "HEAD" from Allow methods
parents 90c723d6 31eee8c7
...@@ -165,7 +165,7 @@ func TestUnhandledMethod(t *testing.T) { ...@@ -165,7 +165,7 @@ func TestUnhandledMethod(t *testing.T) {
AllowGet: false, AllowGet: false,
Code: http.StatusMethodNotAllowed, Code: http.StatusMethodNotAllowed,
ResHeaders: map[string]string{ ResHeaders: map[string]string{
"Allow": "POST, HEAD, OPTIONS", "Allow": "OPTIONS, POST",
}, },
} }
tc.test(t) tc.test(t)
......
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"errors" "errors"
"net/http" "net/http"
"runtime/debug" "runtime/debug"
"strings"
"sync" "sync"
"time" "time"
...@@ -106,7 +107,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ...@@ -106,7 +107,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// The CORS library handles all other requests. // The CORS library handles all other requests.
// Tell the user the allowed methods, and return. // Tell the user the allowed methods, and return.
setAllowedHeaders(w, h.cfg.AllowGet) setAllowHeader(w, h.cfg.AllowGet)
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
return return
case http.MethodPost: case http.MethodPost:
...@@ -116,7 +117,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ...@@ -116,7 +117,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
fallthrough fallthrough
default: default:
setAllowedHeaders(w, h.cfg.AllowGet) setAllowHeader(w, h.cfg.AllowGet)
http.Error(w, "405 - Method Not Allowed", http.StatusMethodNotAllowed) http.Error(w, "405 - Method Not Allowed", http.StatusMethodNotAllowed)
log.Warnf("The IPFS API does not support %s requests.", r.Method) log.Warnf("The IPFS API does not support %s requests.", r.Method)
return return
...@@ -191,11 +192,10 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ...@@ -191,11 +192,10 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.root.Call(req, re, h.env) h.root.Call(req, re, h.env)
} }
func setAllowedHeaders(w http.ResponseWriter, allowGet bool) { func setAllowHeader(w http.ResponseWriter, allowGet bool) {
w.Header().Add("Allow", http.MethodHead) allowedMethods := []string{http.MethodOptions, http.MethodPost}
w.Header().Add("Allow", http.MethodOptions)
w.Header().Add("Allow", http.MethodPost)
if allowGet { if allowGet {
w.Header().Add("Allow", http.MethodGet) allowedMethods = append(allowedMethods, http.MethodHead, http.MethodGet)
} }
w.Header().Set("Allow", strings.Join(allowedMethods, ", "))
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment