@@ -11,35 +11,37 @@ import (
11
11
"time"
12
12
13
13
"github.com/golang/glog"
14
+ "github.com/prebid/go-gdpr/api"
14
15
"github.com/prebid/go-gdpr/vendorlist"
16
+ "github.com/prebid/go-gdpr/vendorlist2"
15
17
"github.com/prebid/prebid-server/config"
16
18
"golang.org/x/net/context/ctxhttp"
17
19
)
18
20
19
- type saveVendors func (uint16 , vendorlist .VendorList )
21
+ type saveVendors func (uint16 , api .VendorList )
20
22
21
23
// This file provides the vendorlist-fetching function for Prebid Server.
22
24
//
23
25
// For more info, see https://github.com/prebid/prebid-server/issues/504
24
26
//
25
27
// Nothing in this file is exported. Public APIs can be found in gdpr.go
26
28
27
- func newVendorListFetcher (initCtx context.Context , cfg config.GDPR , client * http.Client , urlMaker func (uint16 ) string ) func (ctx context.Context , id uint16 ) (vendorlist.VendorList , error ) {
29
+ func newVendorListFetcher (initCtx context.Context , cfg config.GDPR , client * http.Client , urlMaker func (uint16 , uint8 ) string , TCFVer uint8 ) func (ctx context.Context , id uint16 ) (vendorlist.VendorList , error ) {
28
30
// These save and load functions can be used to store & retrieve lists from our cache.
29
31
save , load := newVendorListCache ()
30
32
31
33
withTimeout , cancel := context .WithTimeout (initCtx , cfg .Timeouts .InitTimeout ())
32
34
defer cancel ()
33
- populateCache (withTimeout , client , urlMaker , save )
35
+ populateCache (withTimeout , client , urlMaker , save , TCFVer )
34
36
35
- saveOneSometimes := newOccasionalSaver (cfg .Timeouts .ActiveTimeout ())
37
+ saveOneSometimes := newOccasionalSaver (cfg .Timeouts .ActiveTimeout (), TCFVer )
36
38
37
39
return func (ctx context.Context , id uint16 ) (vendorlist.VendorList , error ) {
38
40
list := load (id )
39
41
if list != nil {
40
42
return list , nil
41
43
}
42
- saveOneSometimes (ctx , client , urlMaker (id ), save )
44
+ saveOneSometimes (ctx , client , urlMaker (id , TCFVer ), save )
43
45
list = load (id )
44
46
if list != nil {
45
47
return list , nil
@@ -49,17 +51,23 @@ func newVendorListFetcher(initCtx context.Context, cfg config.GDPR, client *http
49
51
}
50
52
51
53
// populateCache saves all the known versions of the vendor list for future use.
52
- func populateCache (ctx context.Context , client * http.Client , urlMaker func (uint16 ) string , saver saveVendors ) {
53
- latestVersion := saveOne (ctx , client , urlMaker (0 ), saver )
54
+ func populateCache (ctx context.Context , client * http.Client , urlMaker func (uint16 , uint8 ) string , saver saveVendors , TCFVer uint8 ) {
55
+ latestVersion := saveOne (ctx , client , urlMaker (0 , TCFVer ), saver , TCFVer )
54
56
55
57
for i := uint16 (1 ); i < latestVersion ; i ++ {
56
- saveOne (ctx , client , urlMaker (i ), saver )
58
+ saveOne (ctx , client , urlMaker (i , TCFVer ), saver , TCFVer )
57
59
}
58
60
}
59
61
60
62
// Make a URL which can be used to fetch a given version of the Global Vendor List. If the version is 0,
61
63
// this will fetch the latest version.
62
- func vendorListURLMaker (version uint16 ) string {
64
+ func vendorListURLMaker (version uint16 , TCFVer uint8 ) string {
65
+ if TCFVer == 2 {
66
+ if version == 0 {
67
+ return "https://vendorlist.consensu.org/v2/vendor-list.json"
68
+ }
69
+ return "https://vendorlist.consensu.org/v2/archives/vendor-list-v" + strconv .Itoa (int (version )) + ".json"
70
+ }
63
71
if version == 0 {
64
72
return "https://vendorlist.consensu.org/vendorlist.json"
65
73
}
@@ -71,7 +79,7 @@ func vendorListURLMaker(version uint16) string {
71
79
// The goal here is to update quickly when new versions of the VendorList are released, but not wreck
72
80
// server performance if a bad CMP starts sending us malformed consent strings that advertize a version
73
81
// that doesn't exist yet.
74
- func newOccasionalSaver (timeout time.Duration ) func (ctx context.Context , client * http.Client , url string , saver saveVendors ) {
82
+ func newOccasionalSaver (timeout time.Duration , TCFVer uint8 ) func (ctx context.Context , client * http.Client , url string , saver saveVendors ) {
75
83
lastSaved := & atomic.Value {}
76
84
lastSaved .Store (time.Time {})
77
85
@@ -80,13 +88,13 @@ func newOccasionalSaver(timeout time.Duration) func(ctx context.Context, client
80
88
if now .Sub (lastSaved .Load ().(time.Time )).Minutes () > 10 {
81
89
withTimeout , cancel := context .WithTimeout (ctx , timeout )
82
90
defer cancel ()
83
- saveOne (withTimeout , client , url , saver )
91
+ saveOne (withTimeout , client , url , saver , TCFVer )
84
92
lastSaved .Store (now )
85
93
}
86
94
}
87
95
}
88
96
89
- func saveOne (ctx context.Context , client * http.Client , url string , saver saveVendors ) uint16 {
97
+ func saveOne (ctx context.Context , client * http.Client , url string , saver saveVendors , cTFVer uint8 ) uint16 {
90
98
req , err := http .NewRequest ("GET" , url , nil )
91
99
if err != nil {
92
100
glog .Errorf ("Failed to build GET %s request. Cookie syncs may be affected: %v" , url , err )
@@ -109,8 +117,12 @@ func saveOne(ctx context.Context, client *http.Client, url string, saver saveVen
109
117
glog .Errorf ("GET %s returned %d. Cookie syncs may be affected." , url , resp .StatusCode )
110
118
return 0
111
119
}
112
-
113
- newList , err := vendorlist .ParseEagerly (respBody )
120
+ var newList api.VendorList
121
+ if cTFVer == 2 {
122
+ newList , err = vendorlist2 .ParseEagerly (respBody )
123
+ } else {
124
+ newList , err = vendorlist .ParseEagerly (respBody )
125
+ }
114
126
if err != nil {
115
127
glog .Errorf ("GET %s returned malformed JSON. Cookie syncs may be affected. Error was %v. Body was %s" , url , err , string (respBody ))
116
128
return 0
@@ -120,13 +132,13 @@ func saveOne(ctx context.Context, client *http.Client, url string, saver saveVen
120
132
return newList .Version ()
121
133
}
122
134
123
- func newVendorListCache () (save func (id uint16 , list vendorlist .VendorList ), load func (id uint16 ) vendorlist .VendorList ) {
135
+ func newVendorListCache () (save func (id uint16 , list api .VendorList ), load func (id uint16 ) api .VendorList ) {
124
136
cache := & sync.Map {}
125
137
126
- save = func (id uint16 , list vendorlist .VendorList ) {
138
+ save = func (id uint16 , list api .VendorList ) {
127
139
cache .Store (id , list )
128
140
}
129
- load = func (id uint16 ) vendorlist .VendorList {
141
+ load = func (id uint16 ) api .VendorList {
130
142
list , ok := cache .Load (id )
131
143
if ok {
132
144
return list .(vendorlist.VendorList )
0 commit comments