forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
info.go
253 lines (221 loc) · 8.39 KB
/
info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package adapters
import (
"fmt"
"io/ioutil"
"strings"
"github.com/golang/glog"
"github.com/mxmCherry/openrtb"
"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/openrtb_ext"
"github.com/prebid/prebid-server/pbsmetrics"
yaml "gopkg.in/yaml.v2"
)
// EnforceBidderInfo decorates the input Bidder by making sure that all the requests
// to it are in sync with its static/bidder-info/{bidder}.yaml file.
//
// It adjusts incoming requests in the following ways:
// 1. If App or Site traffic is not supported by the info file, then requests from
// those sources will be rejected before the delegate is called.
// 2. If a given MediaType is not supported for the platform, then it will be set
// to nil before the request is forwarded to the delegate.
// 3. Any Imps which have no MediaTypes left will be removed.
// 4. If there are no valid Imps left, the delegate won't be called at all.
func EnforceBidderInfo(bidder Bidder, info BidderInfo) Bidder {
return &InfoAwareBidder{
Bidder: bidder,
info: parseBidderInfo(info),
}
}
type InfoAwareBidder struct {
Bidder
info parsedBidderInfo
}
func (i *InfoAwareBidder) MakeRequests(request *openrtb.BidRequest, reqInfo *ExtraRequestInfo) ([]*RequestData, []error) {
var allowedMediaTypes parsedSupports
if request.Site != nil {
if !i.info.site.enabled {
return nil, []error{BadInput("this bidder does not support site requests")}
}
allowedMediaTypes = i.info.site
}
if request.App != nil {
if !i.info.app.enabled {
return nil, []error{BadInput("this bidder does not support app requests")}
}
allowedMediaTypes = i.info.app
}
// Filtering imps is quite expensive (array filter with large, non-pointer elements)... but should be rare,
// because it only happens if the publisher makes a really bad request.
//
// To avoid allocating new arrays and copying in the normal case, we'll make one pass to
// see if any imps need to be removed, and another to do the removing if necessary.
numToFilter, errs := i.pruneImps(request.Imp, allowedMediaTypes)
if numToFilter != 0 {
filteredImps, newErrs := i.filterImps(request.Imp, numToFilter)
request.Imp = filteredImps
errs = append(errs, newErrs...)
}
reqs, delegateErrs := i.Bidder.MakeRequests(request, reqInfo)
return reqs, append(errs, delegateErrs...)
}
// pruneImps trims invalid media types from each imp, and returns true if any of the
// Imps have _no_ valid Media Types left.
func (i *InfoAwareBidder) pruneImps(imps []openrtb.Imp, allowedTypes parsedSupports) (int, []error) {
numToFilter := 0
var errs []error
for i := 0; i < len(imps); i++ {
if !allowedTypes.banner && imps[i].Banner != nil {
imps[i].Banner = nil
errs = append(errs, BadInput(fmt.Sprintf("request.imp[%d] uses banner, but this bidder doesn't support it", i)))
}
if !allowedTypes.video && imps[i].Video != nil {
imps[i].Video = nil
errs = append(errs, BadInput(fmt.Sprintf("request.imp[%d] uses video, but this bidder doesn't support it", i)))
}
if !allowedTypes.audio && imps[i].Audio != nil {
imps[i].Audio = nil
errs = append(errs, BadInput(fmt.Sprintf("request.imp[%d] uses audio, but this bidder doesn't support it", i)))
}
if !allowedTypes.native && imps[i].Native != nil {
imps[i].Native = nil
errs = append(errs, BadInput(fmt.Sprintf("request.imp[%d] uses native, but this bidder doesn't support it", i)))
}
if !hasAnyTypes(&imps[i]) {
numToFilter = numToFilter + 1
}
}
return numToFilter, errs
}
func parseAllowedTypes(allowedTypes []openrtb_ext.BidType) (allowBanner bool, allowVideo bool, allowAudio bool, allowNative bool) {
for _, allowedType := range allowedTypes {
switch allowedType {
case openrtb_ext.BidTypeBanner:
allowBanner = true
case openrtb_ext.BidTypeVideo:
allowVideo = true
case openrtb_ext.BidTypeAudio:
allowAudio = true
case openrtb_ext.BidTypeNative:
allowNative = true
}
}
return
}
func hasAnyTypes(imp *openrtb.Imp) bool {
return imp.Banner != nil || imp.Video != nil || imp.Audio != nil || imp.Native != nil
}
func (i *InfoAwareBidder) filterImps(imps []openrtb.Imp, numToFilter int) ([]openrtb.Imp, []error) {
newImps := make([]openrtb.Imp, 0, len(imps)-numToFilter)
errs := make([]error, 0, numToFilter)
for i := 0; i < len(imps); i++ {
if hasAnyTypes(&imps[i]) {
newImps = append(newImps, imps[i])
} else {
errs = append(errs, BadInput(fmt.Sprintf("request.imp[%d] has no supported MediaTypes. It will be ignored", i)))
}
}
return newImps, errs
}
type BidderInfos map[string]BidderInfo
// ParseBidderInfos reads all the static/bidder-info/{bidder}.yaml files from the filesystem.
// The map it returns will have a key for every element of the bidders array.
// If a {bidder}.yaml file does not exist for some bidder, it will panic.
func ParseBidderInfos(cfg map[string]config.Adapter, infoDir string, bidders []openrtb_ext.BidderName) BidderInfos {
bidderInfos := make(map[string]BidderInfo, len(bidders))
for _, bidderName := range bidders {
bidderString := string(bidderName)
fileData, err := ioutil.ReadFile(infoDir + "/" + bidderString + ".yaml")
if err != nil {
glog.Fatalf("error reading from file %s: %v", infoDir+"/"+bidderString+".yaml", err)
}
var parsedInfo BidderInfo
if err := yaml.Unmarshal(fileData, &parsedInfo); err != nil {
glog.Fatalf("error parsing yaml in file %s: %v", infoDir+"/"+bidderString+".yaml", err)
}
if isEnabledBidder(cfg, bidderString) {
parsedInfo.Status = StatusActive
} else {
parsedInfo.Status = StatusDisabled
}
bidderInfos[bidderString] = parsedInfo
}
return bidderInfos
}
func (infos BidderInfos) IsActive(bidder openrtb_ext.BidderName) bool {
return infos[string(bidder)].Status == StatusActive
}
func (infos BidderInfos) HasAppSupport(bidder openrtb_ext.BidderName) bool {
return infos[string(bidder)].Capabilities.App != nil
}
func (infos BidderInfos) HasSiteSupport(bidder openrtb_ext.BidderName) bool {
return infos[string(bidder)].Capabilities.Site != nil
}
func (infos BidderInfos) SupportsAppMediaType(bidder openrtb_ext.BidderName, mediaType openrtb_ext.BidType) bool {
return containsMediaType(infos[string(bidder)].Capabilities.App.MediaTypes, mediaType)
}
func (infos BidderInfos) SupportsWebMediaType(bidder openrtb_ext.BidderName, mediaType openrtb_ext.BidType) bool {
return containsMediaType(infos[string(bidder)].Capabilities.Site.MediaTypes, mediaType)
}
// isEnabledBidder Checks that a bidder config exists and is not disabled
func isEnabledBidder(cfg map[string]config.Adapter, bidder string) bool {
a, ok := cfg[strings.ToLower(bidder)]
return ok && !a.Disabled
}
// BidderStatus represents a bidder status in PBS, can be either active or disabled
type BidderStatus string
const (
StatusUnknown BidderStatus = ""
StatusActive BidderStatus = "ACTIVE"
StatusDisabled BidderStatus = "DISABLED"
)
type BidderInfo struct {
Status BidderStatus `yaml:"status" json:"status"`
Maintainer *MaintainerInfo `yaml:"maintainer" json:"maintainer"`
Capabilities *CapabilitiesInfo `yaml:"capabilities" json:"capabilities"`
AliasOf string `json:"aliasOf,omitempty"`
}
type MaintainerInfo struct {
Email string `yaml:"email" json:"email"`
}
type CapabilitiesInfo struct {
App *PlatformInfo `yaml:"app" json:"app"`
Site *PlatformInfo `yaml:"site" json:"site"`
}
type PlatformInfo struct {
MediaTypes []openrtb_ext.BidType `yaml:"mediaTypes" json:"mediaTypes"`
}
func containsMediaType(haystack []openrtb_ext.BidType, needle openrtb_ext.BidType) bool {
for i := 0; i < len(haystack); i++ {
if needle == haystack[i] {
return true
}
}
return false
}
// Structs to handle parsed bidder info, so we aren't reparsing every request
type parsedBidderInfo struct {
app parsedSupports
site parsedSupports
}
type parsedSupports struct {
enabled bool
banner bool
video bool
audio bool
native bool
}
func parseBidderInfo(info BidderInfo) parsedBidderInfo {
var parsedInfo parsedBidderInfo
if info.Capabilities.App != nil {
parsedInfo.app.enabled = true
parsedInfo.app.banner, parsedInfo.app.video, parsedInfo.app.audio, parsedInfo.app.native = parseAllowedTypes(info.Capabilities.App.MediaTypes)
}
if info.Capabilities.Site != nil {
parsedInfo.site.enabled = true
parsedInfo.site.banner, parsedInfo.site.video, parsedInfo.site.audio, parsedInfo.site.native = parseAllowedTypes(info.Capabilities.Site.MediaTypes)
}
return parsedInfo
}
type ExtraRequestInfo struct {
PbsEntryPoint pbsmetrics.RequestType
}