Make sure to close request body in HTTP client

This commit is contained in:
Frédéric Guillot 2018-04-29 23:11:10 -07:00
parent 5cacae6cf2
commit 2f4cd59ad9
1 changed files with 8 additions and 1 deletions

View File

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