Skip to content

Commit

Permalink
Moved the cookie parsing into the actual ParseCookie function. (#584)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbemiller authored Jun 25, 2018
1 parent c6578cd commit bf61a0f
Show file tree
Hide file tree
Showing 22 changed files with 110 additions and 108 deletions.
4 changes: 2 additions & 2 deletions adapters/adform/adform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func preparePrebidRequest(serverUrl string, t *testing.T) *pbs.PBSRequest {
prebidHttpRequest.Header.Add("Referer", adformTestData.referrer)
prebidHttpRequest.Header.Add("X-Real-IP", adformTestData.deviceIP)

pbsCookie := usersync.ParsePBSCookieFromRequest(prebidHttpRequest, &config.Cookie{})
pbsCookie := usersync.ParsePBSCookieFromRequest(prebidHttpRequest, &config.HostCookie{})
pbsCookie.TrySync("adform", adformTestData.buyerUID)
fakeWriter := httptest.NewRecorder()
pbsCookie.SetCookieOnResponse(fakeWriter, "", time.Minute)
Expand All @@ -203,7 +203,7 @@ func preparePrebidRequest(serverUrl string, t *testing.T) *pbs.PBSRequest {
r, err := pbs.ParsePBSRequest(prebidHttpRequest, &config.AuctionTimeouts{
Default: 2000,
Max: 2000,
}, cacheClient, &pbs.HostCookieSettings{})
}, cacheClient, &config.HostCookie{})
if err != nil {
t.Fatalf("ParsePBSRequest failed: %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions adapters/appnexus/appnexus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,19 +360,19 @@ func TestAppNexusBasicResponse(t *testing.T) {
req.Header.Add("User-Agent", andata.deviceUA)
req.Header.Add("X-Real-IP", andata.deviceIP)

pc := usersync.ParsePBSCookieFromRequest(req, &config.Cookie{})
pc := usersync.ParsePBSCookieFromRequest(req, &config.HostCookie{})
pc.TrySync("adnxs", andata.buyerUID)
fakewriter := httptest.NewRecorder()
pc.SetCookieOnResponse(fakewriter, "", 90*24*time.Hour)
req.Header.Add("Cookie", fakewriter.Header().Get("Set-Cookie"))

cacheClient, _ := dummycache.New()
hcs := pbs.HostCookieSettings{}
hcc := config.HostCookie{}

pbReq, err := pbs.ParsePBSRequest(req, &config.AuctionTimeouts{
Default: 2000,
Max: 2000,
}, cacheClient, &hcs)
}, cacheClient, &hcc)
if err != nil {
t.Fatalf("ParsePBSRequest failed: %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions adapters/audienceNetwork/facebook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,19 +206,19 @@ func GenerateBidRequestForTestData(fbdata bidInfo, url string) (*pbs.PBSRequest,
req.Header.Add("User-Agent", fbdata.deviceUA)
req.Header.Add("X-Real-IP", fbdata.deviceIP)

pc := usersync.ParsePBSCookieFromRequest(req, &config.Cookie{})
pc := usersync.ParsePBSCookieFromRequest(req, &config.HostCookie{})
pc.TrySync("audienceNetwork", fbdata.buyerUID)
fakewriter := httptest.NewRecorder()
pc.SetCookieOnResponse(fakewriter, "", 90*24*time.Hour)
req.Header.Add("Cookie", fakewriter.Header().Get("Set-Cookie"))

cacheClient, _ := dummycache.New()
hcs := pbs.HostCookieSettings{}
hcc := config.HostCookie{}

pbReq, err := pbs.ParsePBSRequest(req, &config.AuctionTimeouts{
Default: 2000,
Max: 2000,
}, cacheClient, &hcs)
}, cacheClient, &hcc)
return pbReq, err
}

Expand Down
4 changes: 2 additions & 2 deletions adapters/conversant/conversant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,12 +613,12 @@ func ParseRequest(req *pbs.PBSRequest) (*pbs.PBSRequest, error) {
httpReq.Header.Set("Cookie", cookie.ToHTTPCookie(90*24*time.Hour).String())
httpReq.Header.Add("Referer", "http://example.com")
cache, _ := dummycache.New()
hcs := pbs.HostCookieSettings{}
hcc := config.HostCookie{}

parsedReq, err := pbs.ParsePBSRequest(httpReq, &config.AuctionTimeouts{
Default: 2000,
Max: 2000,
}, cache, &hcs)
}, cache, &hcc)

return parsedReq, err
}
Expand Down
6 changes: 3 additions & 3 deletions adapters/lifestreet/lifestreet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,17 +225,17 @@ func TestLifestreetBasicResponse(t *testing.T) {
req.Header.Add("Referer", lsdata.referrer)
req.Header.Add("X-Real-IP", lsdata.deviceIP)

pc := usersync.ParsePBSCookieFromRequest(req, &config.Cookie{})
pc := usersync.ParsePBSCookieFromRequest(req, &config.HostCookie{})
fakewriter := httptest.NewRecorder()
pc.SetCookieOnResponse(fakewriter, "", 90*24*time.Hour)
req.Header.Add("Cookie", fakewriter.Header().Get("Set-Cookie"))

cacheClient, _ := dummycache.New()
hcs := pbs.HostCookieSettings{}
hcc := config.HostCookie{}
pbReq, err := pbs.ParsePBSRequest(req, &config.AuctionTimeouts{
Default: 2000,
Max: 2000,
}, cacheClient, &hcs)
}, cacheClient, &hcc)
if err != nil {
t.Fatalf("ParsePBSRequest failed: %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions adapters/pubmatic/pubmatic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,19 +626,19 @@ func TestPubmaticSampleRequest(t *testing.T) {

httpReq := httptest.NewRequest("POST", server.URL, body)
httpReq.Header.Add("Referer", "http://test.com/sports")
pc := usersync.ParsePBSCookieFromRequest(httpReq, &config.Cookie{})
pc := usersync.ParsePBSCookieFromRequest(httpReq, &config.HostCookie{})
pc.TrySync("pubmatic", "12345")
fakewriter := httptest.NewRecorder()
pc.SetCookieOnResponse(fakewriter, "", 90*24*time.Hour)
httpReq.Header.Add("Cookie", fakewriter.Header().Get("Set-Cookie"))

cacheClient, _ := dummycache.New()
hcs := pbs.HostCookieSettings{}
hcc := config.HostCookie{}

_, err = pbs.ParsePBSRequest(httpReq, &config.AuctionTimeouts{
Default: 2000,
Max: 2000,
}, cacheClient, &hcs)
}, cacheClient, &hcc)
if err != nil {
t.Fatalf("Error when parsing request: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions adapters/pulsepoint/pulsepoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,14 @@ func SampleRequest(numberOfImpressions int, t *testing.T) *pbs.PBSRequest {
// setup a http request
httpReq := httptest.NewRequest("POST", CreateService(adapterstest.BidOnTags("")).Server.URL, body)
httpReq.Header.Add("Referer", "http://news.pub/topnews")
pc := usersync.ParsePBSCookieFromRequest(httpReq, &config.Cookie{})
pc := usersync.ParsePBSCookieFromRequest(httpReq, &config.HostCookie{})
pc.TrySync("pulsepoint", "pulsepointUser123")
fakewriter := httptest.NewRecorder()
pc.SetCookieOnResponse(fakewriter, "", 90*24*time.Hour)
httpReq.Header.Add("Cookie", fakewriter.Header().Get("Set-Cookie"))
// parse the http request
cacheClient, _ := dummycache.New()
hcs := pbs.HostCookieSettings{}
hcs := config.HostCookie{}

parsedReq, err := pbs.ParsePBSRequest(httpReq, &config.AuctionTimeouts{
Default: 2000,
Expand Down
6 changes: 3 additions & 3 deletions adapters/rubicon/rubicon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -946,19 +946,19 @@ func CreatePrebidRequest(server *httptest.Server, t *testing.T) (an *RubiconAdap
req.Header.Add("User-Agent", rubidata.deviceUA)
req.Header.Add("X-Real-IP", rubidata.deviceIP)

pc := usersync.ParsePBSCookieFromRequest(req, &config.Cookie{})
pc := usersync.ParsePBSCookieFromRequest(req, &config.HostCookie{})
pc.TrySync("rubicon", rubidata.buyerUID)
fakewriter := httptest.NewRecorder()
pc.SetCookieOnResponse(fakewriter, "", 90*24*time.Hour)
req.Header.Add("Cookie", fakewriter.Header().Get("Set-Cookie"))

cacheClient, _ := dummycache.New()
hcs := pbs.HostCookieSettings{}
hcc := config.HostCookie{}

pbReq, err = pbs.ParsePBSRequest(req, &config.AuctionTimeouts{
Default: 2000,
Max: 2000,
}, cacheClient, &hcs)
}, cacheClient, &hcc)
pbReq.IsDebug = true
if err != nil {
t.Fatalf("ParsePBSRequest failed: %v", err)
Expand Down
6 changes: 3 additions & 3 deletions adapters/sovrn/sovrn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,19 +183,19 @@ func SampleSovrnRequest(numberOfImpressions int, t *testing.T) *pbs.PBSRequest {
httpReq.Header.Add("Referer", testUrl)
httpReq.Header.Add("User-Agent", testUserAgent)
httpReq.Header.Add("X-Forwarded-For", testIp)
pc := usersync.ParsePBSCookieFromRequest(httpReq, &config.Cookie{})
pc := usersync.ParsePBSCookieFromRequest(httpReq, &config.HostCookie{})
pc.TrySync("sovrn", testSovrnUserId)
fakewriter := httptest.NewRecorder()
pc.SetCookieOnResponse(fakewriter, "", 90*24*time.Hour)
httpReq.Header.Add("Cookie", fakewriter.Header().Get("Set-Cookie"))
// parse the http request
cacheClient, _ := dummycache.New()
hcs := pbs.HostCookieSettings{}
hcc := config.HostCookie{}

parsedReq, err := pbs.ParsePBSRequest(httpReq, &config.AuctionTimeouts{
Default: 2000,
Max: 2000,
}, cacheClient, &hcs)
}, cacheClient, &hcc)
if err != nil {
t.Fatalf("Error when parsing request: %v", err)
}
Expand Down
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ type HostCookie struct {
TTL int64 `mapstructure:"ttl_days"`
}

func (cfg *HostCookie) TTLDuration() time.Duration {
return time.Duration(cfg.TTL) * time.Hour * 24
}

type Adapter struct {
Endpoint string `mapstructure:"endpoint"` // Required
UserSyncURL string `mapstructure:"usersync_url"`
Expand Down
8 changes: 4 additions & 4 deletions endpoints/cookie_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import (
"github.com/prebid/prebid-server/usersync"
)

func NewCookieSyncEndpoint(syncers map[openrtb_ext.BidderName]usersync.Usersyncer, optOutCookie *config.Cookie, syncPermissions gdpr.Permissions, metrics pbsmetrics.MetricsEngine, pbsAnalytics analytics.PBSAnalyticsModule) httprouter.Handle {
func NewCookieSyncEndpoint(syncers map[openrtb_ext.BidderName]usersync.Usersyncer, hostCookie *config.HostCookie, syncPermissions gdpr.Permissions, metrics pbsmetrics.MetricsEngine, pbsAnalytics analytics.PBSAnalyticsModule) httprouter.Handle {
deps := &cookieSyncDeps{
syncers: syncers,
optOutCookie: optOutCookie,
hostCookie: hostCookie,
syncPermissions: syncPermissions,
metrics: metrics,
pbsAnalytics: pbsAnalytics,
Expand All @@ -33,7 +33,7 @@ func NewCookieSyncEndpoint(syncers map[openrtb_ext.BidderName]usersync.Usersynce

type cookieSyncDeps struct {
syncers map[openrtb_ext.BidderName]usersync.Usersyncer
optOutCookie *config.Cookie
hostCookie *config.HostCookie
syncPermissions gdpr.Permissions
metrics pbsmetrics.MetricsEngine
pbsAnalytics analytics.PBSAnalyticsModule
Expand All @@ -50,7 +50,7 @@ func (deps *cookieSyncDeps) Endpoint(w http.ResponseWriter, r *http.Request, _ h
defer deps.pbsAnalytics.LogCookieSyncObject(&co)

deps.metrics.RecordCookieSync(pbsmetrics.Labels{})
userSyncCookie := usersync.ParsePBSCookieFromRequest(r, deps.optOutCookie)
userSyncCookie := usersync.ParsePBSCookieFromRequest(r, deps.hostCookie)
if !userSyncCookie.AllowSyncs() {
http.Error(w, "User has opted out", http.StatusUnauthorized)
co.Status = http.StatusUnauthorized
Expand Down
2 changes: 1 addition & 1 deletion endpoints/cookie_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func doPost(body string, existingSyncs map[string]string, gdprHostConsent bool,
}

func testableEndpoint(perms gdpr.Permissions) httprouter.Handle {
return NewCookieSyncEndpoint(syncersForTest(), &config.Cookie{}, perms, &metricsConf.DummyMetricsEngine{}, analyticsConf.NewPBSAnalytics(&config.Analytics{}))
return NewCookieSyncEndpoint(syncersForTest(), &config.HostCookie{}, perms, &metricsConf.DummyMetricsEngine{}, analyticsConf.NewPBSAnalytics(&config.Analytics{}))
}

func syncersForTest() map[openrtb_ext.BidderName]usersync.Usersyncer {
Expand Down
2 changes: 1 addition & 1 deletion endpoints/openrtb2/amp_auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (deps *endpointDeps) AmpAuction(w http.ResponseWriter, r *http.Request, _ h
}
defer cancel()

usersyncs := usersync.ParsePBSCookieFromRequest(r, &(deps.cfg.HostCookie.OptOutCookie))
usersyncs := usersync.ParsePBSCookieFromRequest(r, &(deps.cfg.HostCookie))
if req.App != nil {
labels.Source = pbsmetrics.DemandApp
} else {
Expand Down
2 changes: 1 addition & 1 deletion endpoints/openrtb2/auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (deps *endpointDeps) Auction(w http.ResponseWriter, r *http.Request, _ http
}
defer cancel()

usersyncs := usersync.ParsePBSCookieFromRequest(r, &(deps.cfg.HostCookie.OptOutCookie))
usersyncs := usersync.ParsePBSCookieFromRequest(r, &(deps.cfg.HostCookie))
if req.App != nil {
labels.Source = pbsmetrics.DemandApp
} else {
Expand Down
2 changes: 1 addition & 1 deletion endpoints/setuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewSetUIDEndpoint(cfg config.HostCookie, perms gdpr.Permissions, pbsanalyti

defer pbsanalytics.LogSetUIDObject(&so)

pc := usersync.ParsePBSCookieFromRequest(r, &cfg.OptOutCookie)
pc := usersync.ParsePBSCookieFromRequest(r, &cfg)
if !pc.AllowSyncs() {
w.WriteHeader(http.StatusUnauthorized)
metrics.RecordUserIDSet(pbsmetrics.UserLabels{Action: pbsmetrics.RequestActionOptOut})
Expand Down
11 changes: 2 additions & 9 deletions pbs/pbsrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func ParseMediaTypes(types []string) []MediaType {
return mtypes
}

func ParsePBSRequest(r *http.Request, cfg *config.AuctionTimeouts, cache cache.Cache, hostCookieSettings *HostCookieSettings) (*PBSRequest, error) {
func ParsePBSRequest(r *http.Request, cfg *config.AuctionTimeouts, cache cache.Cache, hostCookieConfig *config.HostCookie) (*PBSRequest, error) {
defer r.Body.Close()

pbsReq := &PBSRequest{}
Expand Down Expand Up @@ -263,14 +263,7 @@ func ParsePBSRequest(r *http.Request, cfg *config.AuctionTimeouts, cache cache.C

// use client-side data for web requests
if pbsReq.App == nil {
pbsReq.Cookie = usersync.ParsePBSCookieFromRequest(r, &(hostCookieSettings.OptOutCookie))

// Host has right to leverage private cookie store for user ID
if uid, _, _ := pbsReq.Cookie.GetUID(hostCookieSettings.Family); uid == "" && hostCookieSettings.CookieName != "" {
if hostCookie, err := r.Cookie(hostCookieSettings.CookieName); err == nil {
pbsReq.Cookie.TrySync(hostCookieSettings.Family, hostCookie.Value)
}
}
pbsReq.Cookie = usersync.ParsePBSCookieFromRequest(r, hostCookieConfig)

pbsReq.Device.UA = r.Header.Get("User-Agent")

Expand Down
Loading

0 comments on commit bf61a0f

Please sign in to comment.