From 082c147bbe09744e7a681f24c81c8cca913d63b4 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Wed, 29 Jul 2015 13:26:39 -0700 Subject: [PATCH] should fix issue where 'read on closed body' error was leaking down License: MIT Signed-off-by: Jeromy --- commands/http/client.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/commands/http/client.go b/commands/http/client.go index b36299153..5cb2be1b3 100644 --- a/commands/http/client.go +++ b/commands/http/client.go @@ -1,11 +1,11 @@ package http import ( - "bytes" "encoding/json" "errors" "fmt" "io" + "io/ioutil" "net/http" "net/url" "reflect" @@ -225,9 +225,11 @@ func getResponse(httpRes *http.Response, req cmds.Request) (cmds.Response, error case contentType == plainText: // handle non-marshalled errors - buf := bytes.NewBuffer(nil) - io.Copy(buf, httpRes.Body) - e.Message = string(buf.Bytes()) + mes, err := ioutil.ReadAll(rr) + if err != nil { + return nil, err + } + e.Message = string(mes) e.Code = cmds.ErrNormal default: @@ -266,8 +268,7 @@ func readStreamedJson(req cmds.Request, rr io.Reader, out chan<- interface{}) { for { v, err := decodeTypedVal(outputType, dec) if err != nil { - // reading on a closed response body is as good as an io.EOF here - if !(strings.Contains(err.Error(), "read on closed response body") || err == io.EOF) { + if err != io.EOF { log.Error(err) } return @@ -305,6 +306,11 @@ type httpResponseReader struct { func (r *httpResponseReader) Read(b []byte) (int, error) { n, err := r.resp.Body.Read(b) + + // reading on a closed response body is as good as an io.EOF here + if err != nil && strings.Contains(err.Error(), "read on closed response body") { + err = io.EOF + } if err == io.EOF { _ = r.resp.Body.Close() trailerErr := r.checkError()