fix(report): WordPress(WPVULNDB API) 429 Too Many Requests (#826)

* fix(report): WordPress(WPVULNDB API) 429 Too Many Requests

* fix(report): WordPress(WPVULNDB API) 429 Too Many Requests
This commit is contained in:
Shigechika AIKAWA
2019-06-04 12:11:40 +09:00
committed by Kota Kanbe
parent 3bb650cb77
commit 64cdd5aedc

View File

@@ -23,6 +23,7 @@ import (
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/future-architect/vuls/models"
"github.com/future-architect/vuls/util"
@@ -245,12 +246,14 @@ func extractToVulnInfos(pkgName string, cves []WpCveInfo) (vinfos []models.VulnI
}
func httpRequest(url, token string) (string, error) {
retry := 1
util.Log.Debugf("%s", url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
req.Header.Set("Authorization", fmt.Sprintf("Token token=%s", token))
loop:
resp, err := new(http.Client).Do(req)
if err != nil {
return "", err
@@ -260,11 +263,17 @@ func httpRequest(url, token string) (string, error) {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != 200 && resp.StatusCode != 404 {
return "", xerrors.Errorf("status: %s", resp.Status)
if resp.StatusCode == 200 {
return string(body), nil
} else if resp.StatusCode == 404 {
// This package is not in WPVulnDB
return "", nil
} else if resp.StatusCode == 429 && retry <= 3 {
// 429 Too Many Requests
util.Log.Debugf("sleep %d min(s): %s", retry, resp.Status)
time.Sleep(time.Duration(retry) * time.Minute)
retry++
goto loop
}
return string(body), nil
return "", err
}