Unverified Commit 6cc6af69 authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #167 from ipfs/fix/http-errors

fix: use golang's http.Error to send errors
parents 1995f5cb 6978cc1e
# Please see https://help.github.com/articles/about-codeowners/ for more information
# Global owner
* @keks
# Subsystem specific owners
...@@ -92,7 +92,7 @@ func TestErrors(t *testing.T) { ...@@ -92,7 +92,7 @@ func TestErrors(t *testing.T) {
cmds.EncLong: "foobar", cmds.EncLong: "foobar",
}, },
status: "400 Bad Request", status: "400 Bad Request",
bodyStr: `invalid encoding: foobar`, bodyStr: "invalid encoding: foobar\n",
}, },
{ {
......
...@@ -96,8 +96,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ...@@ -96,8 +96,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}() }()
if !allowOrigin(r, h.cfg) || !allowReferer(r, h.cfg) { if !allowOrigin(r, h.cfg) || !allowReferer(r, h.cfg) {
w.WriteHeader(http.StatusForbidden) http.Error(w, "403 - Forbidden", http.StatusForbidden)
w.Write([]byte("403 - Forbidden"))
log.Warningf("API blocked request to %s. (possible CSRF)", r.URL) log.Warningf("API blocked request to %s. (possible CSRF)", r.URL)
return return
} }
...@@ -122,12 +121,12 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ...@@ -122,12 +121,12 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
req, err := parseRequest(r, h.root) req, err := parseRequest(r, h.root)
if err != nil { if err != nil {
status := http.StatusBadRequest
if err == ErrNotFound { if err == ErrNotFound {
w.WriteHeader(http.StatusNotFound) status = http.StatusNotFound
} else {
w.WriteHeader(http.StatusBadRequest)
} }
w.Write([]byte(err.Error()))
http.Error(w, err.Error(), status)
return return
} }
...@@ -146,8 +145,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ...@@ -146,8 +145,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
re, err := NewResponseEmitter(w, r.Method, req, withRequestBodyEOFChan(bodyEOFChan)) re, err := NewResponseEmitter(w, r.Method, req, withRequestBodyEOFChan(bodyEOFChan))
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
w.Write([]byte(err.Error()))
return return
} }
......
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