Skip to content

Commit 097b495

Browse files
committed
client: only fetch necessary packages when cache missing
When a httpSource is used without a Cache implementation, only fetch packages which are represented in the index. Previously if there wasn't a Cache implementation every pacakge was enumerated, ignoring the contents of the retrieved index. Change-Id: I8e8e0ce412b3ded188afd6bb109d96efb7e7f27c Reviewed-on: https://go-review.googlesource.com/c/vulndb/+/333455 Trust: Roland Shoemaker <[email protected]> Trust: Zvonimir Pavlinovic <[email protected]> Run-TryBot: Roland Shoemaker <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Zvonimir Pavlinovic <[email protected]>
1 parent 87d38e1 commit 097b495

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

client/client.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,12 @@ func (hs *httpSource) Get(packages []string) ([]*osv.Entry, error) {
158158
}
159159

160160
var stillNeed []string
161-
if hs.cache != nil {
162-
for _, p := range packages {
163-
lastModified, present := index[p]
164-
if !present {
165-
continue
166-
}
161+
for _, p := range packages {
162+
lastModified, present := index[p]
163+
if !present {
164+
continue
165+
}
166+
if hs.cache != nil {
167167
if cached, err := hs.cache.ReadEntries(hs.dbName, p); err != nil {
168168
return nil, err
169169
} else if len(cached) != 0 {
@@ -179,10 +179,8 @@ func (hs *httpSource) Get(packages []string) ([]*osv.Entry, error) {
179179
continue
180180
}
181181
}
182-
stillNeed = append(stillNeed, p)
183182
}
184-
} else {
185-
stillNeed = packages
183+
stillNeed = append(stillNeed, p)
186184
}
187185

188186
for _, p := range stillNeed {

client/client_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
package client
66

77
import (
8+
"encoding/json"
89
"fmt"
910
"io/ioutil"
1011
"net"
1112
"net/http"
13+
"net/http/httptest"
1214
"os"
1315
"path"
16+
"reflect"
1417
"runtime"
1518
"testing"
1619
"time"
@@ -156,3 +159,30 @@ func TestClient(t *testing.T) {
156159
}
157160
}
158161
}
162+
163+
func TestCorrectFetchesNoCache(t *testing.T) {
164+
fetches := map[string]int{}
165+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
166+
fetches[r.URL.Path]++
167+
if r.URL.Path == "/index.json" {
168+
j, _ := json.Marshal(osv.DBIndex{
169+
"a": time.Now(),
170+
"b": time.Now(),
171+
})
172+
w.Write(j)
173+
} else {
174+
w.Write([]byte("[]"))
175+
}
176+
}))
177+
defer ts.Close()
178+
179+
hs := &httpSource{url: ts.URL, c: new(http.Client)}
180+
_, err := hs.Get([]string{"a", "b", "c"})
181+
if err != nil {
182+
t.Fatalf("unexpected error: %s", err)
183+
}
184+
expectedFetches := map[string]int{"/index.json": 1, "/a.json": 1, "/b.json": 1}
185+
if !reflect.DeepEqual(fetches, expectedFetches) {
186+
t.Errorf("unexpected fetches, got %v, want %v", fetches, expectedFetches)
187+
}
188+
}

0 commit comments

Comments
 (0)