@cryptotaxi247 / kubo / commits / 082c147bb

should fix issue where 'read on closed body' error was leaking down

License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>

Jeromy committed Jul 29, 2015 at 13:26 UTC 082c147bbe09744e7a681f24c81c8cca913d63b4
1 file changed +12 -6
commands/http/client.go
+12 -6
@@ -1,11 +1,11 @@
1 package http
2
3 import (
4 - "bytes"
4 "encoding/json"
5 "errors"
6 "fmt"
7 "io"
8 + "io/ioutil"
9 "net/http"
10 "net/url"
11 "reflect"
@@ -225,9 +225,11 @@ func getResponse(httpRes *http.Response, req cmds.Request) (cmds.Response, error
225
226 case contentType == plainText:
227 // 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)
233 e.Code = cmds.ErrNormal
234
235 default:
@@ -266,8 +268,7 @@ func readStreamedJson(req cmds.Request, rr io.Reader, out chan<- interface{}) {
268 for {
269 v, err := decodeTypedVal(outputType, dec)
270 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 {
272 log.Error(err)
273 }
274 return
@@ -305,6 +306,11 @@ type httpResponseReader struct {
306
307 func (r *httpResponseReader) Read(b []byte) (int, error) {
308 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 + }
314 if err == io.EOF {
315 _ = r.resp.Body.Close()
316 trailerErr := r.checkError()