1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-30 01:52:26 +08:00

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

License: MIT
Signed-off-by: Jeromy <jeromyj@gmail.com>
This commit is contained in:
Jeromy
2015-07-29 13:26:39 -07:00
parent 8ed08ab0fc
commit 082c147bbe

View File

@ -1,11 +1,11 @@
package http package http
import ( import (
"bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"reflect" "reflect"
@ -225,9 +225,11 @@ func getResponse(httpRes *http.Response, req cmds.Request) (cmds.Response, error
case contentType == plainText: case contentType == plainText:
// handle non-marshalled errors // handle non-marshalled errors
buf := bytes.NewBuffer(nil) mes, err := ioutil.ReadAll(rr)
io.Copy(buf, httpRes.Body) if err != nil {
e.Message = string(buf.Bytes()) return nil, err
}
e.Message = string(mes)
e.Code = cmds.ErrNormal e.Code = cmds.ErrNormal
default: default:
@ -266,8 +268,7 @@ func readStreamedJson(req cmds.Request, rr io.Reader, out chan<- interface{}) {
for { for {
v, err := decodeTypedVal(outputType, dec) v, err := decodeTypedVal(outputType, dec)
if err != nil { if err != nil {
// reading on a closed response body is as good as an io.EOF here if err != io.EOF {
if !(strings.Contains(err.Error(), "read on closed response body") || err == io.EOF) {
log.Error(err) log.Error(err)
} }
return return
@ -305,6 +306,11 @@ type httpResponseReader struct {
func (r *httpResponseReader) Read(b []byte) (int, error) { func (r *httpResponseReader) Read(b []byte) (int, error) {
n, err := r.resp.Body.Read(b) 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 { if err == io.EOF {
_ = r.resp.Body.Close() _ = r.resp.Body.Close()
trailerErr := r.checkError() trailerErr := r.checkError()