Skip to content

Commit

Permalink
Extracted vtrack.ModifyVastXmlString to avoid conversions
Browse files Browse the repository at this point in the history
This avoids unnecessary conversions between json and string

ok  	github.com/prebid/prebid-server/endpoints/events	0.034s	coverage: 96.1% of statements
  • Loading branch information
laurb9 committed Dec 1, 2020
1 parent c7448bd commit b829081
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
23 changes: 16 additions & 7 deletions endpoints/events/vtrack.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func (v *vtrackEndpoint) cachePutObjects(ctx context.Context, req *BidCacheReque
}

if _, ok := biddersAllowingVastUpdate[c.Bidder]; ok && nc.Data != nil {
nc.Data = ModifyVastXml(v.Cfg.ExternalURL, nc.Data, c.BidID, c.Bidder, accountId, c.Timestamp)
nc.Data = ModifyVastXmlJSON(v.Cfg.ExternalURL, nc.Data, c.BidID, c.Bidder, accountId, c.Timestamp)
}

cacheables = append(cacheables, *nc)
Expand Down Expand Up @@ -270,25 +270,34 @@ func getAccountId(httpRequest *http.Request) string {
return httpRequest.URL.Query().Get(AccountParameter)
}

// modifyVastXml modifies BidCacheRequest element Vast XML data
func ModifyVastXml(externalUrl string, data json.RawMessage, bidid string, bidder string, accountId string, timestamp int64) json.RawMessage {
c := string(data)
// modifyVastXmlString rewrites and returns the string vastXML or an empty string if it was left unmodified
func ModifyVastXmlString(externalUrl string, c string, bidid string, bidder string, accountId string, timestamp int64) string {
ci := strings.Index(c, ImpressionCloseTag)

// no impression tag - pass it as it is
if ci == -1 {
return data
return ""
}

vastUrlTracking := GetVastUrlTracking(externalUrl, bidid, bidder, accountId, timestamp)
impressionUrl := "<![CDATA[" + vastUrlTracking + "]]>"
oi := strings.Index(c, ImpressionOpenTag)

if ci-oi == len(ImpressionOpenTag) {
return json.RawMessage(strings.Replace(c, ImpressionOpenTag, ImpressionOpenTag+impressionUrl, 1))
return strings.Replace(c, ImpressionOpenTag, ImpressionOpenTag+impressionUrl, 1)
}

return json.RawMessage(strings.Replace(c, ImpressionCloseTag, ImpressionCloseTag+ImpressionOpenTag+impressionUrl+ImpressionCloseTag, 1))
return strings.Replace(c, ImpressionCloseTag, ImpressionCloseTag+ImpressionOpenTag+impressionUrl+ImpressionCloseTag, 1)
}

// modifyVastXml modifies BidCacheRequest element Vast XML data
func ModifyVastXmlJSON(externalUrl string, data json.RawMessage, bidid string, bidder string, accountId string, timestamp int64) json.RawMessage {
c := string(data) // FIXME: should decode json, or escapes like "\u003e" will not be recognized as "<"
c = ModifyVastXmlString(externalUrl, c, bidid, bidder, accountId, timestamp)
if len(c) == 0 {
return data
}
return json.RawMessage(c)
}

func contains(s []string, e string) bool {
Expand Down
2 changes: 1 addition & 1 deletion exchange/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (ev *eventsData) isModifyingVASTXMLAllowed(bidderName string) bool {
// modifyVAST injects event Impression url if needed, otherwise returns original VAST string
func (ev *eventsData) modifyVAST(bid *openrtb.Bid, bidderName openrtb_ext.BidderName, vastXML string) string {
if ev.isModifyingVASTXMLAllowed(bidderName.String()) {
vastXML = string(events.ModifyVastXml(ev.externalURL, json.RawMessage(vastXML), bid.ID, bidderName.String(), ev.accountID, ev.auctionTimestampMs))
vastXML = events.ModifyVastXmlString(ev.externalURL, vastXML, bid.ID, bidderName.String(), ev.accountID, ev.auctionTimestampMs)
}
return vastXML
}
Expand Down

0 comments on commit b829081

Please sign in to comment.