Skip to content

Commit 082c147

Browse files
committedJul 29, 2015
should fix issue where 'read on closed body' error was leaking down
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
1 parent 8ed08ab commit 082c147

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed
 

‎commands/http/client.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package http
22

33
import (
4-
"bytes"
54
"encoding/json"
65
"errors"
76
"fmt"
87
"io"
8+
"io/ioutil"
99
"net/http"
1010
"net/url"
1111
"reflect"
@@ -225,9 +225,11 @@ func getResponse(httpRes *http.Response, req cmds.Request) (cmds.Response, error
225225

226226
case contentType == plainText:
227227
// handle non-marshalled errors
228-
buf := bytes.NewBuffer(nil)
229-
io.Copy(buf, httpRes.Body)
230-
e.Message = string(buf.Bytes())
228+
mes, err := ioutil.ReadAll(rr)
229+
if err != nil {
230+
return nil, err
231+
}
232+
e.Message = string(mes)
231233
e.Code = cmds.ErrNormal
232234

233235
default:
@@ -266,8 +268,7 @@ func readStreamedJson(req cmds.Request, rr io.Reader, out chan<- interface{}) {
266268
for {
267269
v, err := decodeTypedVal(outputType, dec)
268270
if err != nil {
269-
// reading on a closed response body is as good as an io.EOF here
270-
if !(strings.Contains(err.Error(), "read on closed response body") || err == io.EOF) {
271+
if err != io.EOF {
271272
log.Error(err)
272273
}
273274
return
@@ -305,6 +306,11 @@ type httpResponseReader struct {
305306

306307
func (r *httpResponseReader) Read(b []byte) (int, error) {
307308
n, err := r.resp.Body.Read(b)
309+
310+
// reading on a closed response body is as good as an io.EOF here
311+
if err != nil && strings.Contains(err.Error(), "read on closed response body") {
312+
err = io.EOF
313+
}
308314
if err == io.EOF {
309315
_ = r.resp.Body.Close()
310316
trailerErr := r.checkError()

0 commit comments

Comments
 (0)
Please sign in to comment.