Skip to content

Commit 41fa8d8

Browse files
authored
feat(fetcher): don't do concurrency fetch by htcat and set User-Agent (#337)
* feat(fetcher): don't do concurrency fetch by htcat * chore(fetcher/suse): fetch the gzip compressed file * feat(fetcher): set meaningful User-Agent
1 parent a975cd9 commit 41fa8d8

File tree

8 files changed

+30
-97
lines changed

8 files changed

+30
-97
lines changed

fetcher/amazon/amazon.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,9 @@ func fetchUpdateInfoURL(mirrors []string) (updateInfoURLs []string, err error) {
148148
}
149149
u.Path = path.Join(u.Path, "/repodata/repomd.xml")
150150
reqs = append(reqs, util.FetchRequest{
151-
Target: mirror, // base URL of the mirror site
152-
URL: u.String(),
153-
Concurrently: true,
154-
MIMEType: util.MIMETypeXML,
151+
Target: mirror, // base URL of the mirror site
152+
URL: u.String(),
153+
MIMEType: util.MIMETypeXML,
155154
})
156155
}
157156

fetcher/debian/debian.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ func newFetchRequests(target []string) (reqs []util.FetchRequest) {
2020
continue
2121
}
2222
reqs = append(reqs, util.FetchRequest{
23-
Target: v,
24-
URL: fmt.Sprintf(t, name),
25-
Concurrently: true,
26-
MIMEType: util.MIMETypeBzip2,
23+
Target: v,
24+
URL: fmt.Sprintf(t, name),
25+
MIMEType: util.MIMETypeBzip2,
2726
})
2827
}
2928
return

fetcher/fedora/fedora.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,14 @@ func newFedoraFetchRequests(target []string, arch string) (reqs []util.FetchRequ
9797
}
9898

9999
reqs = append(reqs, util.FetchRequest{
100-
Target: v,
101-
URL: fmt.Sprintf(updateURL, v, arch),
102-
MIMEType: util.MIMETypeXML,
103-
Concurrently: true,
100+
Target: v,
101+
URL: fmt.Sprintf(updateURL, v, arch),
102+
MIMEType: util.MIMETypeXML,
104103
})
105104
moduleReqs = append(moduleReqs, util.FetchRequest{
106-
Target: v,
107-
URL: fmt.Sprintf(moduleURL, v, arch),
108-
MIMEType: util.MIMETypeXML,
109-
Concurrently: true,
105+
Target: v,
106+
URL: fmt.Sprintf(moduleURL, v, arch),
107+
MIMEType: util.MIMETypeXML,
110108
})
111109
}
112110
return
@@ -350,7 +348,6 @@ func fetchCveIDsFromBugzilla(id string) ([]string, error) {
350348
for i, v := range b.Blocked {
351349
reqs[i] = util.FetchRequest{
352350
URL: fmt.Sprintf(bugZillaURL, v),
353-
Concurrently: true,
354351
LogSuppressed: true,
355352
MIMEType: util.MIMETypeXML,
356353
}
@@ -395,10 +392,9 @@ func extractInfoFromRepoMd(results []util.FetchResult, rt string, mt util.MIMETy
395392
}
396393
u.Path = strings.Replace(u.Path, "repodata/repomd.xml", repo.Location.Href, 1)
397394
req := util.FetchRequest{
398-
URL: u.String(),
399-
Target: r.Target,
400-
MIMEType: mt,
401-
Concurrently: true,
395+
URL: u.String(),
396+
Target: r.Target,
397+
MIMEType: mt,
402398
}
403399
updateInfoReqs = append(updateInfoReqs, req)
404400
break

fetcher/suse/suse.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,17 @@ import (
88
"github.com/vulsio/goval-dictionary/fetcher/util"
99
)
1010

11-
// https://ftp.suse.com/pub/projects/security/oval/opensuse.leap.42.2.xml
12-
// https://ftp.suse.com/pub/projects/security/oval/opensuse.13.2.xml
13-
// https://ftp.suse.com/pub/projects/security/oval/suse.linux.enterprise.desktop.12.xml"
14-
// https://ftp.suse.com/pub/projects/security/oval/suse.linux.enterprise.server.12.xml
11+
// https://ftp.suse.com/pub/projects/security/oval/opensuse.leap.42.2.xml.gz
12+
// https://ftp.suse.com/pub/projects/security/oval/opensuse.13.2.xml.gz
13+
// https://ftp.suse.com/pub/projects/security/oval/suse.linux.enterprise.desktop.12.xml.gz
14+
// https://ftp.suse.com/pub/projects/security/oval/suse.linux.enterprise.server.12.xml.gz
1515
func newFetchRequests(suseType string, target []string) (reqs []util.FetchRequest) {
16-
const t = "https://ftp.suse.com/pub/projects/security/oval/%s.%s.xml"
16+
const t = "https://ftp.suse.com/pub/projects/security/oval/%s.%s.xml.gz"
1717
for _, v := range target {
1818
reqs = append(reqs, util.FetchRequest{
19-
Target: v,
20-
URL: fmt.Sprintf(t, suseType, v),
21-
Concurrently: true,
22-
MIMEType: util.MIMETypeXML,
19+
Target: v,
20+
URL: fmt.Sprintf(t, suseType, v),
21+
MIMEType: util.MIMETypeGzip,
2322
})
2423
}
2524
return

fetcher/ubuntu/ubuntu.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@ func newFetchRequests(target []string) (reqs []util.FetchRequest) {
2121
log15.Warn("See https://wiki.ubuntu.com/Releases for supported versions")
2222
default:
2323
reqs = append(reqs, util.FetchRequest{
24-
Target: v,
25-
URL: url,
26-
Concurrently: true,
27-
MIMEType: util.MIMETypeBzip2,
24+
Target: v,
25+
URL: url,
26+
MIMEType: util.MIMETypeBzip2,
2827
})
2928
}
3029
}

fetcher/util/fetcher.go

Lines changed: 4 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ import (
1111
"sync"
1212
"time"
1313

14-
"github.com/htcat/htcat"
1514
"github.com/inconshreveable/log15"
1615
"github.com/spf13/viper"
1716
"github.com/ulikunitz/xz"
1817
"golang.org/x/xerrors"
18+
19+
"github.com/vulsio/goval-dictionary/config"
1920
)
2021

2122
// MIMEType :
@@ -68,7 +69,6 @@ type FetchRequest struct {
6869
Target string
6970
URL string
7071
MIMEType MIMEType
71-
Concurrently bool
7272
LogSuppressed bool
7373
}
7474

@@ -122,12 +122,7 @@ func FetchFeedFiles(reqs []FetchRequest) (results []FetchResult, err error) {
122122
tasks <- func() {
123123
select {
124124
case req := <-reqChan:
125-
var body []byte
126-
if req.Concurrently {
127-
body, err = fetchFileConcurrently(req, 20/len(reqs))
128-
} else {
129-
body, err = fetchFileWithUA(req)
130-
}
125+
body, err := fetchFileWithUA(req)
131126
wg.Done()
132127
if err != nil {
133128
errChan <- err
@@ -163,57 +158,6 @@ func FetchFeedFiles(reqs []FetchRequest) (results []FetchResult, err error) {
163158
return results, nil
164159
}
165160

166-
func fetchFileConcurrently(req FetchRequest, concurrency int) (body []byte, err error) {
167-
var proxyURL *url.URL
168-
httpClient := &http.Client{}
169-
httpProxy := viper.GetString("http-proxy")
170-
if httpProxy != "" {
171-
if proxyURL, err = url.Parse(httpProxy); err != nil {
172-
return nil, xerrors.Errorf("Failed to parse proxy url. err: %w", err)
173-
}
174-
httpClient = &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)}}
175-
}
176-
177-
u, err := url.Parse(req.URL)
178-
if err != nil {
179-
return nil, xerrors.Errorf("Failed to parse given URL: %w", err)
180-
}
181-
182-
buf := bytes.Buffer{}
183-
htc := htcat.New(httpClient, u, concurrency)
184-
if _, err := htc.WriteTo(&buf); err != nil {
185-
return nil, xerrors.Errorf("Failed to write to output stream: %w", err)
186-
}
187-
188-
var b bytes.Buffer
189-
switch req.MIMEType {
190-
case MIMETypeXML, MIMETypeTxt, MIMETypeJSON, MIMETypeYml, MIMETypeHTML:
191-
b = buf
192-
case MIMETypeBzip2:
193-
if _, err := b.ReadFrom(bzip2.NewReader(bytes.NewReader(buf.Bytes()))); err != nil {
194-
return nil, xerrors.Errorf("Failed to open bzip2 file. err: %w", err)
195-
}
196-
case MIMETypeXz:
197-
r, err := xz.NewReader(bytes.NewReader(buf.Bytes()))
198-
if err != nil {
199-
return nil, xerrors.Errorf("Failed to open xz file. err: %w", err)
200-
}
201-
if _, err := b.ReadFrom(r); err != nil {
202-
return nil, xerrors.Errorf("Failed to read xz file. err: %w", err)
203-
}
204-
case MIMETypeGzip:
205-
r, err := gzip.NewReader(bytes.NewReader(buf.Bytes()))
206-
if err != nil {
207-
return nil, xerrors.Errorf("Failed to open gzip file. err: %w", err)
208-
}
209-
if _, err := b.ReadFrom(r); err != nil {
210-
return nil, xerrors.Errorf("Failed to read gzip file. err: %w", err)
211-
}
212-
}
213-
214-
return b.Bytes(), nil
215-
}
216-
217161
func fetchFileWithUA(req FetchRequest) (body []byte, err error) {
218162
var proxyURL *url.URL
219163
var resp *http.Response
@@ -232,7 +176,7 @@ func fetchFileWithUA(req FetchRequest) (body []byte, err error) {
232176
return nil, xerrors.Errorf("Failed to download. err: %w", err)
233177
}
234178

235-
httpreq.Header.Set("User-Agent", "curl/7.37.0")
179+
httpreq.Header.Set("User-Agent", fmt.Sprintf("goval-dictionary/%s.%s", config.Version, config.Revision))
236180
resp, err = httpClient.Do(httpreq)
237181
if err != nil {
238182
return nil, xerrors.Errorf("Failed to download. err: %w", err)

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ require (
88
github.com/go-redis/redis/v8 v8.11.5
99
github.com/google/go-cmp v0.5.9
1010
github.com/hashicorp/go-version v1.6.0
11-
github.com/htcat/htcat v1.0.2
1211
github.com/inconshreveable/log15 v3.0.0-testing.5+incompatible
1312
github.com/k0kubun/pp v3.0.1+incompatible
1413
github.com/knqyf263/go-rpm-version v0.0.0-20220614171824-631e686d1075

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,6 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
152152
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
153153
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
154154
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
155-
github.com/htcat/htcat v1.0.2 h1:zro95dGwkKDeZOgq9ei+9szd5qurGxBGfHY8hRehA7k=
156-
github.com/htcat/htcat v1.0.2/go.mod h1:i8ViQbjSi2+lJzM6Lx20FIxHENCz6mzJglK3HH06W3s=
157155
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
158156
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
159157
github.com/inconshreveable/log15 v3.0.0-testing.5+incompatible h1:VryeOTiaZfAzwx8xBcID1KlJCeoWSIpsNbSk+/D2LNk=

0 commit comments

Comments
 (0)