From 27742dd4d23eef1d043355a7ebe209847b02caac Mon Sep 17 00:00:00 2001 From: Scott Kay Date: Wed, 7 Feb 2024 16:33:48 -0500 Subject: [PATCH] Wrapper Refactor --- endpoints/cookie_sync_test.go | 1 + endpoints/openrtb2/auction.go | 7 ++++--- endpoints/openrtb2/auction_test.go | 17 ++++++++++++----- exchange/bidder.go | 14 +++++++------- openrtb_ext/request_wrapper.go | 10 +++++----- openrtb_ext/request_wrapper_test.go | 10 +++++----- privacy/rule_condition.go | 7 +++++-- privacy/rule_condition_test.go | 10 ++++++++++ util/httputil/headers.go | 2 +- 9 files changed, 50 insertions(+), 28 deletions(-) diff --git a/endpoints/cookie_sync_test.go b/endpoints/cookie_sync_test.go index 8feb1fa6318..ea73d435e8e 100644 --- a/endpoints/cookie_sync_test.go +++ b/endpoints/cookie_sync_test.go @@ -427,6 +427,7 @@ func TestExtractGDPRSignal(t *testing.T) { } } +// todo func TestExtractPrivacyPolicies(t *testing.T) { type testInput struct { request cookieSyncRequest diff --git a/endpoints/openrtb2/auction.go b/endpoints/openrtb2/auction.go index 2b32d9836c6..7af4b8cb2e7 100644 --- a/endpoints/openrtb2/auction.go +++ b/endpoints/openrtb2/auction.go @@ -2045,9 +2045,10 @@ func setRegsImplicitly(httpReq *http.Request, r *openrtb_ext.RequestWrapper) err return fmt.Errorf("request.regs.ext is invalid: %v", err) } - if len(regExt.GetGPC()) == 0 { - secGPC := httpReq.Header.Get(httputil.HeaderSecGPC) - regExt.SetGPC(secGPC) + if regExt.GetGPC() == nil { + if secGPC := httpReq.Header.Get(httputil.HeaderSecGPC); len(secGPC) > 0 { + regExt.SetGPC(&secGPC) + } } return nil } diff --git a/endpoints/openrtb2/auction_test.go b/endpoints/openrtb2/auction_test.go index b31a544ee53..ddeb32b533c 100644 --- a/endpoints/openrtb2/auction_test.go +++ b/endpoints/openrtb2/auction_test.go @@ -682,28 +682,28 @@ func TestSetRegsImplicitly(t *testing.T) { expectedError string }{ { - name: "nil", + name: "regs-nil", gpcHeader: "", regs: nil, expectedRegs: nil, expectedError: "", }, { - name: "nil-gpc", + name: "regs-nil-fromheader", gpcHeader: "1", regs: nil, expectedRegs: &openrtb2.Regs{Ext: json.RawMessage(`{"gpc":"1"}`)}, expectedError: "", }, { - name: "empty", + name: "regs-empty", gpcHeader: "", regs: &openrtb2.Regs{}, expectedRegs: &openrtb2.Regs{}, expectedError: "", }, { - name: "empty-gpc", + name: "regs-empty-fromheader", gpcHeader: "1", regs: &openrtb2.Regs{}, expectedRegs: &openrtb2.Regs{Ext: json.RawMessage(`{"gpc":"1"}`)}, @@ -711,11 +711,18 @@ func TestSetRegsImplicitly(t *testing.T) { }, { name: "existing", - gpcHeader: "", + gpcHeader: "1", regs: &openrtb2.Regs{Ext: json.RawMessage(`{"gpc":"0"}`)}, expectedRegs: &openrtb2.Regs{Ext: json.RawMessage(`{"gpc":"0"}`)}, expectedError: "", }, + { + name: "existing-empty", + gpcHeader: "1", + regs: &openrtb2.Regs{Ext: json.RawMessage(`{"gpc":""}`)}, + expectedRegs: &openrtb2.Regs{Ext: json.RawMessage(`{"gpc":""}`)}, + expectedError: "", + }, { name: "empty-gpc", gpcHeader: "1", diff --git a/exchange/bidder.go b/exchange/bidder.go index a5cfa425707..b5b1392446e 100644 --- a/exchange/bidder.go +++ b/exchange/bidder.go @@ -16,13 +16,6 @@ import ( "sync" "time" - "github.com/golang/glog" - "github.com/prebid/openrtb/v20/adcom1" - nativeRequests "github.com/prebid/openrtb/v20/native1/request" - nativeResponse "github.com/prebid/openrtb/v20/native1/response" - "github.com/prebid/openrtb/v20/openrtb2" - "golang.org/x/net/context/ctxhttp" - "github.com/prebid/prebid-server/v2/adapters" "github.com/prebid/prebid-server/v2/bidadjustment" "github.com/prebid/prebid-server/v2/config" @@ -37,6 +30,13 @@ import ( "github.com/prebid/prebid-server/v2/util/httputil" "github.com/prebid/prebid-server/v2/util/jsonutil" "github.com/prebid/prebid-server/v2/version" + + "github.com/golang/glog" + "github.com/prebid/openrtb/v20/adcom1" + nativeRequests "github.com/prebid/openrtb/v20/native1/request" + nativeResponse "github.com/prebid/openrtb/v20/native1/response" + "github.com/prebid/openrtb/v20/openrtb2" + "golang.org/x/net/context/ctxhttp" ) // AdaptedBidder defines the contract needed to participate in an Auction within an Exchange. diff --git a/openrtb_ext/request_wrapper.go b/openrtb_ext/request_wrapper.go index c1aee7d1a61..5aa9662a657 100644 --- a/openrtb_ext/request_wrapper.go +++ b/openrtb_ext/request_wrapper.go @@ -1168,7 +1168,7 @@ type RegExt struct { extDirty bool gdpr *int8 gdprDirty bool - gpc string + gpc *string gpcDirty bool usPrivacy string usPrivacyDirty bool @@ -1228,8 +1228,8 @@ func (re *RegExt) marshal() (json.RawMessage, error) { } if re.gpcDirty { - if len(re.gpc) > 0 { - rawjson, err := json.Marshal(re.gpc) + if re.gpc != nil { + rawjson, err := json.Marshal(*re.gpc) if err != nil { return nil, err } @@ -1287,12 +1287,12 @@ func (re *RegExt) SetGDPR(gdpr *int8) { re.gdprDirty = true } -func (re *RegExt) GetGPC() string { +func (re *RegExt) GetGPC() *string { gpc := re.gpc return gpc } -func (re *RegExt) SetGPC(gpc string) { +func (re *RegExt) SetGPC(gpc *string) { re.gpc = gpc re.gpcDirty = true } diff --git a/openrtb_ext/request_wrapper_test.go b/openrtb_ext/request_wrapper_test.go index f7d6886766f..5b9558fb503 100644 --- a/openrtb_ext/request_wrapper_test.go +++ b/openrtb_ext/request_wrapper_test.go @@ -1366,7 +1366,7 @@ func TestCloneRegExt(t *testing.T) { ext: map[string]json.RawMessage{"A": json.RawMessage(`X`), "B": json.RawMessage(`Y`)}, extDirty: true, gdpr: ptrutil.ToPtr[int8](1), - gpc: "anyGPC", + gpc: ptrutil.ToPtr("anyGPC"), gpcDirty: true, usPrivacy: "priv", usPrivacyDirty: true, @@ -1375,7 +1375,7 @@ func TestCloneRegExt(t *testing.T) { ext: map[string]json.RawMessage{"A": json.RawMessage(`X`), "B": json.RawMessage(`Y`)}, extDirty: true, gdpr: ptrutil.ToPtr[int8](1), - gpc: "anyGPC", + gpc: ptrutil.ToPtr("anyGPC"), gpcDirty: true, usPrivacy: "priv", usPrivacyDirty: true, @@ -1388,7 +1388,7 @@ func TestCloneRegExt(t *testing.T) { ext: map[string]json.RawMessage{"A": json.RawMessage(`X`), "B": json.RawMessage(`Y`)}, extDirty: true, gdpr: ptrutil.ToPtr[int8](1), - gpc: "anyGPC", + gpc: ptrutil.ToPtr("anyGPC"), gpcDirty: true, usPrivacy: "priv", usPrivacyDirty: true, @@ -1397,7 +1397,7 @@ func TestCloneRegExt(t *testing.T) { ext: map[string]json.RawMessage{"A": json.RawMessage(`X`), "B": json.RawMessage(`Y`)}, extDirty: true, gdpr: ptrutil.ToPtr[int8](1), - gpc: "anyGPC", + gpc: ptrutil.ToPtr("anyGPC"), gpcDirty: true, usPrivacy: "priv", usPrivacyDirty: true, @@ -1408,7 +1408,7 @@ func TestCloneRegExt(t *testing.T) { regExt.extDirty = false regExt.gdpr = nil regExt.gdprDirty = true - regExt.gpc = "mutated" + regExt.gpc = ptrutil.ToPtr("mutated") regExt.gpcDirty = false regExt.usPrivacy = "Other" }, diff --git a/privacy/rule_condition.go b/privacy/rule_condition.go index 9bc42a206ea..9472ba21b28 100644 --- a/privacy/rule_condition.go +++ b/privacy/rule_condition.go @@ -102,8 +102,11 @@ func getGPC(request ActivityRequest) string { } if request.IsBidRequest() && request.bidRequest.Regs != nil { - regExt, _ := request.bidRequest.GetRegExt() - return regExt.GetGPC() + if regExt, err := request.bidRequest.GetRegExt(); err == nil { + if v := regExt.GetGPC(); v != nil { + return *v + } + } } return "" diff --git a/privacy/rule_condition_test.go b/privacy/rule_condition_test.go index 2360f7aa34e..ec8ad7a5565 100644 --- a/privacy/rule_condition_test.go +++ b/privacy/rule_condition_test.go @@ -441,6 +441,16 @@ func TestGetGPC(t *testing.T) { request: ActivityRequest{bidRequest: &openrtb_ext.RequestWrapper{BidRequest: &openrtb2.BidRequest{Regs: &openrtb2.Regs{Ext: json.RawMessage(`{"gpc":"1"}`)}}}}, expected: "1", }, + { + name: "request-regs-missing", + request: ActivityRequest{bidRequest: &openrtb_ext.RequestWrapper{BidRequest: &openrtb2.BidRequest{Regs: &openrtb2.Regs{Ext: json.RawMessage(`{}`)}}}}, + expected: "", + }, + { + name: "request-regs-malformed", + request: ActivityRequest{bidRequest: &openrtb_ext.RequestWrapper{BidRequest: &openrtb2.BidRequest{Regs: &openrtb2.Regs{Ext: json.RawMessage(`malformed`)}}}}, + expected: "", + }, { name: "request-regs-nil", request: ActivityRequest{bidRequest: &openrtb_ext.RequestWrapper{BidRequest: &openrtb2.BidRequest{Regs: nil}}}, diff --git a/util/httputil/headers.go b/util/httputil/headers.go index f4090a59559..22a5f049284 100644 --- a/util/httputil/headers.go +++ b/util/httputil/headers.go @@ -1,5 +1,5 @@ package httputil const ( - HeaderSecGPC = "Sec-GPC" + HeaderSecGPC = "Sec-Gpc" )