forked from prebid/prebid-server
/
pbsrequest.go
262 lines (219 loc) · 6.25 KB
/
pbsrequest.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
254
255
256
257
258
259
260
261
262
package pbs
import (
"encoding/json"
"fmt"
"net"
"net/http"
"net/url"
"strings"
"time"
"github.com/prebid/prebid-server/cache"
"github.com/golang/glog"
"github.com/prebid/openrtb"
"github.com/spf13/viper"
"golang.org/x/net/publicsuffix"
)
const MAX_BIDDERS = 8
type ConfigCache interface {
LoadConfig(string) ([]Bids, error)
}
type Bids struct {
BidderCode string `json:"bidder"`
BidID string `json:"bid_id"`
Params json.RawMessage `json:"params"`
}
type AdUnit struct {
Code string `json:"code"`
TopFrame int8 `json:"is_top_frame"`
Sizes []openrtb.Format `json:"sizes"`
Bids []Bids `json:"bids"`
ConfigID string `json:"config_id"`
}
type PBSAdUnit struct {
Sizes []openrtb.Format
TopFrame int8
Code string
BidID string
Params json.RawMessage
}
type PBSBidder struct {
BidderCode string `json:"bidder"`
AdUnitCode string `json:"ad_unit,omitempty"` // for index to dedup responses
ResponseTime int `json:"response_time_ms,omitempty"`
NumBids int `json:"num_bids,omitempty"`
Error string `json:"error,omitempty"`
NoCookie bool `json:"no_cookie,omitempty"`
NoBid bool `json:"no_bid,omitempty"`
UsersyncInfo *UsersyncInfo `json:"usersync,omitempty"`
Debug []*BidderDebug `json:"debug,omitempty"`
AdUnits []PBSAdUnit `json:"-"`
}
func (bidder *PBSBidder) LookupBidID(Code string) string {
for _, unit := range bidder.AdUnits {
if unit.Code == Code {
return unit.BidID
}
}
return ""
}
type PBSRequest struct {
AccountID string `json:"account_id"`
Tid string `json:"tid"`
CacheMarkup int8 `json:"cache_markup"`
Secure int8 `json:"secure"`
TimeoutMillis uint64 `json:"timeout_millis"`
AdUnits []AdUnit `json:"ad_units"`
IsDebug bool `json:"is_debug"`
App *openrtb.App `json:"app"`
Device *openrtb.Device `json:"device"`
// internal
Bidders []*PBSBidder `json:"-"`
UserIDs map[string]string `json:"-"`
Url string `json:"-"`
Domain string `json:"-"`
Start time.Time
}
func ConfigGet(cache cache.Cache, id string) ([]Bids, error) {
conf, err := cache.Config().Get(id)
if err != nil {
return nil, err
}
bids := make([]Bids, 0)
err = json.Unmarshal([]byte(conf), &bids)
if err != nil {
return nil, err
}
return bids, nil
}
func ParsePBSRequest(r *http.Request, cache cache.Cache) (*PBSRequest, error) {
defer r.Body.Close()
pbsReq := &PBSRequest{}
err := json.NewDecoder(r.Body).Decode(&pbsReq)
if err != nil {
return nil, err
}
pbsReq.Start = time.Now()
if len(pbsReq.AdUnits) == 0 {
return nil, fmt.Errorf("No ad units specified")
}
if pbsReq.TimeoutMillis == 0 || pbsReq.TimeoutMillis > 2000 {
pbsReq.TimeoutMillis = uint64(viper.GetInt("default_timeout_ms"))
}
if pbsReq.Device == nil {
pbsReq.Device = &openrtb.Device{}
}
// use client-side data for web requests
if pbsReq.App == nil {
pc := ParseUIDCookie(r)
pbsReq.UserIDs = pc.UIDs
// this would be for the shared adnxs.com domain
if anid, err := r.Cookie("uuid2"); err == nil {
pbsReq.UserIDs["adnxs"] = anid.Value
}
pbsReq.Device.UA = r.Header.Get("User-Agent")
if r.Header.Get("X-Real-Ip") != "" {
pbsReq.Device.IP = r.Header.Get("X-Real-Ip")
} else {
ip, _, uerr := net.SplitHostPort(r.RemoteAddr)
if uerr == nil && net.ParseIP(ip) != nil {
pbsReq.Device.IP = ip
}
}
pbsReq.Url = r.Header.Get("Referer") // must be specified in the header
// TODO: this should explicitly put us in test mode
if r.FormValue("url_override") != "" {
pbsReq.Url = r.FormValue("url_override")
}
if strings.Index(pbsReq.Url, "http") == -1 {
pbsReq.Url = fmt.Sprintf("http://%s", pbsReq.Url)
}
url, err := url.Parse(pbsReq.Url)
if err != nil {
return nil, fmt.Errorf("Invalid URL '%s': %v", pbsReq.Url, err)
}
if url.Host == "" {
return nil, fmt.Errorf("Host not found from URL '%v'", url)
}
pbsReq.Domain, err = publicsuffix.EffectiveTLDPlusOne(url.Host)
if err != nil {
return nil, fmt.Errorf("Invalid URL '%s': %v", url.Host, err)
}
// all domains must be in the whitelist
_, err = cache.Domains().Get(pbsReq.Domain)
if err != nil {
return nil, fmt.Errorf("Invalid URL %s", pbsReq.Domain)
}
} else {
_, err = cache.Apps().Get(pbsReq.App.Bundle)
if err != nil {
return nil, fmt.Errorf("Invalid app bundle %s", pbsReq.App.Bundle)
}
}
if r.FormValue("debug") == "1" {
pbsReq.IsDebug = true
}
if pbsReq.Secure == 0 {
if r.Header.Get("X-Forwarded-Proto") != "" {
if r.Header.Get("X-Forwarded-Proto") == "https" {
pbsReq.Secure = 1
}
} else if r.TLS != nil {
pbsReq.Secure = 1
}
}
pbsReq.Bidders = make([]*PBSBidder, 0, MAX_BIDDERS)
for _, unit := range pbsReq.AdUnits {
bidders := unit.Bids
if unit.ConfigID != "" {
bidders, err = ConfigGet(cache, unit.ConfigID)
if err != nil {
// proceed with other ad units
glog.Infof("Unable to load config '%s': %v", unit.ConfigID, err)
continue
}
}
if glog.V(2) {
glog.Infof("Ad unit %s has %d bidders for %d sizes", unit.Code, len(bidders), len(unit.Sizes))
}
for _, b := range bidders {
var bidder *PBSBidder
// index requires a different request for each ad unit
if b.BidderCode != "indexExchange" {
for _, pb := range pbsReq.Bidders {
if pb.BidderCode == b.BidderCode {
bidder = pb
}
}
}
if bidder == nil {
bidder = &PBSBidder{BidderCode: b.BidderCode}
if b.BidderCode == "indexExchange" {
bidder.AdUnitCode = unit.Code
}
pbsReq.Bidders = append(pbsReq.Bidders, bidder)
}
pau := PBSAdUnit{
Sizes: unit.Sizes,
TopFrame: unit.TopFrame,
Code: unit.Code,
Params: b.Params,
BidID: b.BidID,
}
bidder.AdUnits = append(bidder.AdUnits, pau)
}
}
return pbsReq, nil
}
func (req PBSRequest) Elapsed() int {
return int(time.Since(req.Start) / 1000000)
}
func (req PBSRequest) GetUserID(BidderCode string) string {
if uid, ok := req.UserIDs[BidderCode]; ok {
return uid
}
return ""
}
func (p PBSRequest) String() string {
b, _ := json.MarshalIndent(p, "", " ")
return string(b)
}