diff --git a/resource/http.go b/resource/http.go index bbd82009..6ea4e385 100644 --- a/resource/http.go +++ b/resource/http.go @@ -78,6 +78,7 @@ func (u *HTTP) Validate(sys *system.System) []TestResult { RequestHeader: u.RequestHeader, RequestBody: u.RequestBody, Method: u.Method}) sysHTTP.SetAllowInsecure(u.AllowInsecure) sysHTTP.SetNoFollowRedirects(u.NoFollowRedirects) + defer sysHTTP.Close() var results []TestResult results = append(results, ValidateValue(u, "status", u.Status, sysHTTP.Status, skip)) diff --git a/system/http.go b/system/http.go index d5e5abec..e3053b3d 100644 --- a/system/http.go +++ b/system/http.go @@ -27,6 +27,7 @@ type HTTP interface { Exists() (bool, error) SetAllowInsecure(bool) SetNoFollowRedirects(bool) + Close() error } type DefHTTP struct { @@ -46,6 +47,7 @@ type DefHTTP struct { KeyFile string Method string Proxy string + bodyConsumed bool } func NewDefHTTP(_ context.Context, httpStr string, system *System, config util.Config) HTTP { @@ -214,10 +216,18 @@ func (u *DefHTTP) Body() (io.Reader, error) { if err := u.setup(); err != nil { return nil, err } + u.bodyConsumed = true return u.resp.Body, nil } +func (u *DefHTTP) Close() error { + if u.resp != nil && u.resp.Body != nil && !u.bodyConsumed { + return u.resp.Body.Close() + } + return nil +} + func hasUserAgentHeader(headers []string) bool { for _, header := range headers { if strings.HasPrefix(strings.ToLower(header), USER_AGENT_HEADER_PREFIX) {