Sfoglia il codice sorgente

Make sure to close request body in HTTP client

Frédéric Guillot 8 anni fa
parent
commit
2f4cd59ad9
1 ha cambiato i file con 8 aggiunte e 1 eliminazioni
  1. 8 1
      http/client/client.go

+ 8 - 1
http/client/client.go

@@ -11,6 +11,7 @@ import (
 	"encoding/json"
 	"fmt"
 	"io"
+	"io/ioutil"
 	"net"
 	"net/http"
 	"net/url"
@@ -134,13 +135,19 @@ func (c *Client) executeRequest(request *http.Request) (*Response, error) {
 
 		return nil, err
 	}
+	defer resp.Body.Close()
 
 	if resp.ContentLength > maxBodySize {
 		return nil, fmt.Errorf("client: response too large (%d bytes)", resp.ContentLength)
 	}
 
+	buf, err := ioutil.ReadAll(resp.Body)
+	if err != nil {
+		return nil, fmt.Errorf("client: error while reading body %v", err)
+	}
+
 	response := &Response{
-		Body:          resp.Body,
+		Body:          bytes.NewReader(buf),
 		StatusCode:    resp.StatusCode,
 		EffectiveURL:  resp.Request.URL.String(),
 		LastModified:  resp.Header.Get("Last-Modified"),