Skip to content

Commit

Permalink
Yieldmo adapter: cut banner bid request parameters in case the url fo…
Browse files Browse the repository at this point in the history
…r bunner request is too long (#6549)

Co-authored-by: Anton Tsymuk <antontsymuk@Antons-MacBook-Pro.local>
  • Loading branch information
2 people authored and idettman committed May 21, 2021
1 parent 2ea92dd commit a4f9750
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
42 changes: 40 additions & 2 deletions modules/yieldmoBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const LOCAL_WINDOW = utils.getWindowTop();
const DEFAULT_PLAYBACK_METHOD = 2;
const DEFAULT_START_DELAY = 0;
const VAST_TIMEOUT = 15000;
const MAX_BANNER_REQUEST_URL_LENGTH = 8000;
const BANNER_REQUEST_PROPERTIES_TO_REDUCE = ['description', 'title', 'pr', 'page_url'];

export const spec = {
code: BIDDER_CODE,
Expand Down Expand Up @@ -51,7 +53,7 @@ export const spec = {
p: [],
page_url: bidderRequest.refererInfo.referer,
bust: new Date().getTime().toString(),
pr: bidderRequest.refererInfo.referer,
pr: (LOCAL_WINDOW.document && LOCAL_WINDOW.document.referrer) || '',
scrd: LOCAL_WINDOW.devicePixelRatio || 0,
dnt: getDNT(),
description: getPageDescription(),
Expand Down Expand Up @@ -92,6 +94,20 @@ export const spec = {
}
});
serverRequest.p = '[' + serverRequest.p.toString() + ']';

// check if url exceeded max length
const url = `${BANNER_SERVER_ENDPOINT}?${utils.parseQueryStringParameters(serverRequest)}`;
let extraCharacters = url.length - MAX_BANNER_REQUEST_URL_LENGTH;
if (extraCharacters > 0) {
for (let i = 0; i < BANNER_REQUEST_PROPERTIES_TO_REDUCE.length; i++) {
extraCharacters = shortcutProperty(extraCharacters, serverRequest, BANNER_REQUEST_PROPERTIES_TO_REDUCE[i]);

if (extraCharacters <= 0) {
break;
}
}
}

serverRequests.push({
method: 'GET',
url: BANNER_SERVER_ENDPOINT,
Expand Down Expand Up @@ -273,7 +289,7 @@ function getPageDescription() {
if (document.querySelector('meta[name="description"]')) {
return document
.querySelector('meta[name="description"]')
.getAttribute('content'); // Value of the description metadata from the publisher's page.
.getAttribute('content') || ''; // Value of the description metadata from the publisher's page.
} else {
return '';
}
Expand Down Expand Up @@ -514,3 +530,25 @@ function validateVideoParams(bid) {
return false;
}
}

/**
* Shortcut object property and check if required characters count was deleted
*
* @param {number} extraCharacters, count of characters to remove
* @param {object} target, object on which string property length should be reduced
* @param {string} propertyName, name of property to reduce
* @return {number} 0 if required characters count was removed otherwise count of how many left
*/
function shortcutProperty(extraCharacters, target, propertyName) {
if (target[propertyName].length > extraCharacters) {
target[propertyName] = target[propertyName].substring(0, target[propertyName].length - extraCharacters);

return 0
}

const charactersLeft = extraCharacters - target[propertyName].length;

target[propertyName] = '';

return charactersLeft;
}
47 changes: 47 additions & 0 deletions test/spec/modules/yieldmoBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,53 @@ describe('YieldmoAdapter', function () {
const placementsData = JSON.parse(buildAndGetPlacementInfo([mockBannerBid()]));
expect(placementsData[0].bidfloor).to.undefined;
});

it('should not exceed max url length', () => {
const longString = new Array(8000).join('a');
const localWindow = utils.getWindowTop();

const originalTitle = localWindow.document.title;
localWindow.document.title = longString;

const request = spec.buildRequests(
[mockBannerBid()],
mockBidderRequest({
refererInfo: {
numIframes: 1,
reachedTop: true,
referer: longString,
},
})
)[0];
const url = `${request.url}?${utils.parseQueryStringParameters(request.data)}`;

expect(url.length).equal(8000);

localWindow.document.title = originalTitle;
});

it('should only shortcut properties rather then completely remove it', () => {
const longString = new Array(7516).join('a');
const localWindow = utils.getWindowTop();

const originalTitle = localWindow.document.title;
localWindow.document.title = `testtitle${longString}`;

const request = spec.buildRequests(
[mockBannerBid()],
mockBidderRequest({
refererInfo: {
numIframes: 1,
reachedTop: true,
referer: longString,
},
})
)[0];

expect(request.data.title.length).greaterThan(0);

localWindow.document.title = originalTitle;
});
});

describe('Instream video:', function () {
Expand Down

0 comments on commit a4f9750

Please sign in to comment.