diff --git a/httptransport/client/indexer.go b/httptransport/client/indexer.go index fc1e40f393..8fdc546e29 100644 --- a/httptransport/client/indexer.go +++ b/httptransport/client/indexer.go @@ -38,12 +38,10 @@ func (s *HTTP) AffectedManifests(ctx context.Context, v []claircore.Vulnerabilit return nil, fmt.Errorf("failed to create request: %v", err) } resp, err := s.c.Do(req) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return nil, &clairerror.ErrRequestFail{Code: resp.StatusCode, Status: resp.Status} } + defer resp.Body.Close() err = json.NewDecoder(resp.Body).Decode(&affected) if err != nil { return nil, &clairerror.ErrBadAffectedManifests{err} @@ -73,12 +71,10 @@ func (s *HTTP) Index(ctx context.Context, manifest *claircore.Manifest) (*clairc return nil, fmt.Errorf("failed to create request: %v", err) } resp, err := s.c.Do(req) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return nil, fmt.Errorf("failed to do request: %v", err) } + defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return nil, &clairerror.ErrRequestFail{Code: resp.StatusCode, Status: resp.Status} } @@ -103,12 +99,10 @@ func (s *HTTP) IndexReport(ctx context.Context, manifest claircore.Digest) (*cla return nil, false, fmt.Errorf("failed to create request: %v", err) } resp, err := s.c.Do(req) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return nil, false, fmt.Errorf("failed to do request: %v", err) } + defer resp.Body.Close() if resp.StatusCode == http.StatusNotFound { return nil, false, nil } @@ -135,13 +129,10 @@ func (s *HTTP) State(ctx context.Context) (string, error) { return "", fmt.Errorf("failed to create request: %v", err) } resp, err := s.c.Do(req) - if resp != nil { - defer resp.Body.Close() - } - if err != nil { return "", fmt.Errorf("failed to do request: %v", err) } + defer resp.Body.Close() buf := &bytes.Buffer{} if _, err := buf.ReadFrom(resp.Body); err != nil { return "", err diff --git a/httptransport/client/matcher.go b/httptransport/client/matcher.go index 49f3ac72bb..9c52c461f9 100644 --- a/httptransport/client/matcher.go +++ b/httptransport/client/matcher.go @@ -38,12 +38,10 @@ func (c *HTTP) Scan(ctx context.Context, ir *claircore.IndexReport) (*claircore. } resp, err := c.c.Do(req) - if resp != nil { - defer resp.Body.Close() - } if err != nil { return nil, err } + defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("%v: unexpected status: %s", u.Path, resp.Status) } @@ -89,13 +87,11 @@ func (c *HTTP) DeleteUpdateOperations(ctx context.Context, ref ...uuid.UUID) (in return } res, err := c.c.Do(req) - if res != nil { - defer res.Body.Close() - } if err != nil { errs[i] = err return } + defer res.Body.Close() if got, want := res.StatusCode, http.StatusOK; got != want { errs[i] = fmt.Errorf("%v: unexpected status: %s", u.Path, res.Status) } @@ -176,12 +172,10 @@ func (c *HTTP) LatestUpdateOperations(ctx context.Context) (map[string][]driver. // if a subsequent response provides a StatusNotModified status, the map of UpdateOprations is served from cache. func (c *HTTP) updateOperations(ctx context.Context, req *http.Request, cache *uoCache) (map[string][]driver.UpdateOperation, error) { res, err := c.c.Do(req) - if res != nil { - defer res.Body.Close() - } if err != nil { return nil, err } + defer res.Body.Close() switch res.StatusCode { case http.StatusOK: m := make(map[string][]driver.UpdateOperation) @@ -223,12 +217,10 @@ func (c *HTTP) UpdateDiff(ctx context.Context, prev, cur uuid.UUID) (*driver.Upd req.URL.RawQuery = v.Encode() res, err := c.c.Do(req) - if res != nil { - defer res.Body.Close() - } if err != nil { return nil, err } + defer res.Body.Close() if res.StatusCode != http.StatusOK { return nil, fmt.Errorf("%v: unexpected status: %s", u.Path, res.Status) }