-
Notifications
You must be signed in to change notification settings - Fork 441
/
Ads.cpp
630 lines (555 loc) · 21.2 KB
/
Ads.cpp
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
#include "gb-include.h"
#include "Ads.h"
#include "HttpServer.h"
#include "Xml.h"
#include "Pages.h"
#include "Titledb.h"
// . lets get feeds from:
// 1. overture
// 2. findwhat
// 3. looksmart
// 4. about
// 5. ah-ha (http://www.ah-ha.com/partners/partners_xmlv4_integration.asp)
static void gotDocWrapper( void *state , TcpSocket *s ) ;
char *removeCDATA ( char *buf, int32_t *bufLen ) {
if(*bufLen >= 12 && strncasecmp(buf, "<![CDATA[", 9) == 0 ) {
buf += 9;
*bufLen -= 12;
}
return buf;
}
// . Gigablast Ad Server feed implementation
// Sends a search request on the ad collection to a remote/separate ad server
// Ad server sends back XML encoded results.
// . gotDoc parses and places into arrays for use when printing.
// . printAd is used to write the ads into html, called from PageResults.cpp.
Ads::Ads(){
m_indexPIAds = PI_PRIMARY;
m_indexSSAds = SS_PRIMARY;
m_adSSSameasPI = false;
m_adBSSSameasBPI = false;
memset(m_bufLen , 0, sizeof(m_bufLen) );
memset(m_buf , 0, sizeof(m_buf) );
memset(m_numAds , 0, sizeof(m_numAds) );
memset(m_urls , 0, sizeof(m_urls) );
memset(m_urlsLen , 0, sizeof(m_urlsLen) );
memset(m_titles , 0, sizeof(m_titles) );
memset(m_titlesLen, 0, sizeof(m_titlesLen));
memset(m_desc , 0, sizeof(m_desc) );
memset(m_descLen , 0, sizeof(m_descLen) );
memset(m_sites , 0, sizeof(m_sites) );
memset(m_sitesLen , 0, sizeof(m_sitesLen) );
}
Ads::~Ads() {
for(int32_t i = 0; i < MAX_FEEDS; i++) {
if( m_buf[i] )
mfree( m_buf[i], m_bufLen[i], "Ads-fSktXml" );
m_bufLen[i] = 0 ;
m_buf [i] = NULL;
}
}
// . This gets ads from the Gigablast Ad Server
// . Request Parameters (* = required):
// URL: http://<adserverIP>.<adserverPort>/search
// *q=<query>
// *n=<num results>
// *s=<start number>
// *raw=<what format, should always be 11>
// . Fromatted Response:
// <response>
// <hits>LONG LONG</hits>
// <result>
// <title><![CDATA[STRING]]></title>
// <body>
// <text><![CDATA[STRING]]></text>
// <url><![CDATA[STRING]]></url>
// </body>
// <link><![CDATA[STRING]]</link>
// </result>
// ...
// </response>
//
// . This gets ads from 3rd party ad provider: searchfeed.com
// formerly miva
// . Request Parameters (* = required):
// URL: http://www.searchfeed.com/rd/feed/XMLFeed.jsp
// *trackID=<unique to acct>
// *pID=<partner id>
// cat=<url encoded query>
// nl=<num results>
// page=<page results, like our s/n>
// *ip=<querying IP>
// excID=<hides ads from this advertiserID> (N/A currently)
// . Formatted Response:
// <Listings>
// <Page></Page>
// <Count></Count>
// <Listing>
// <Title></Title>
// <URL></URL>
// <URI></URI>
// <Description></Description>
// <Bid></Bid>
// </Listing>
// <Listing>
// <Title></Title>
// <URL></URL>
// <URI></URI>
// <Description> </Description>
// <Bid></Bid>
// </Listing>
// ...
// </Listings>
//
// . This gets ads from 3rd party ad provider: 7search.com
// . Request Parameters (* = required):
// URL: http://meta.7search.com/feed/xml.aspx
// *affiliate=<acct ID>
// *qu=<url encoded query>
// pn=<page results, like our s>
// r=<num results>
// filter=<adult filter on(yes)/off(no)>
// *ip_address=<querying IP>
// *st=<search type, our queries are "typein", can do "link">
// *rid=<full URL where search came from>
// . Formatted Response:
// <?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
// <results>
// <site index="1">
// <url><![CDATA[link to end website]]></url>
// <name><![CDATA[title of above link]]></name>
// <description><![CDATA[advertiser description]]></description>
// <bid><![CDATA[total bid amount before revenue share]]></bid>
// <httplink><![CDATA[advertisers actual url, NO LINK]]></httplink>
// </site>
// <site index="2">
// ...
// </site>
// </results>
//
bool Ads::getAds ( char *q ,
int32_t qlen ,
int32_t pageNum ,
int32_t queryIP ,
char *coll ,
void *state ,
void (*callback)(void *state) ){
// bail if no collection specified
if(!coll) {
log("query: Collection for ads is empty.");
return true;
}
// bail if query too big
if ( qlen > MAX_AD_QUERY_LEN) {
log("query: Query length of %"INT32" is too long to get "
"ads for.",qlen);
return true;
}
// bail if empty, too
if ( ! q || qlen <= 0 ) return true;
// save stuff
strncpy(m_q, q, MAX_AD_QUERY_LEN);
m_qlen = qlen;
m_coll = coll;
m_state = state;
m_callback = callback;
m_pageAds = pageNum;
m_queryIP = queryIP;
m_cr = g_collectiondb.getRec(m_coll);
// . this means that it was not specifically mentioned, choose next
// in round robin
/*
if(!feedIndex)
m_feedIndex = getAdFeedIndex(g_collectiondb.getCollnum(m_coll));
else
// . an ad feed was specified, so use this one instead.
m_feedIndex = feedIndex - 1;
*/
int32_t numPIAds = m_cr->m_adPINumAds;
int32_t numBPIAds = m_cr->m_adPINumAds;
if(!m_cr->m_adSSSameasPI) {
size_t size = gbstrlen(m_cr->m_adCGI[0]);
if((int32_t)size == gbstrlen(m_cr->m_adCGI[2]))
if(!memcmp(m_cr->m_adCGI[0], m_cr->m_adCGI[2], size))
m_adSSSameasPI = true;
}
else
m_adSSSameasPI = true;
if(!m_cr->m_adBSSSameasBPI) {
size_t size = gbstrlen(m_cr->m_adCGI[1]);
if((int32_t)size == gbstrlen(m_cr->m_adCGI[3]))
if(!memcmp(m_cr->m_adCGI[1], m_cr->m_adCGI[3], size))
m_adBSSSameasBPI = true;
}
else
m_adBSSSameasBPI = true;
if(m_adSSSameasPI && m_cr->m_adPIEnable && m_cr->m_adSSEnable) {
numPIAds += m_cr->m_adSSNumAds;
m_indexSSAds = PI_PRIMARY;
}
else
m_adSSSameasPI = false;
if(m_adBSSSameasBPI && m_cr->m_adPIEnable && m_cr->m_adSSEnable) {
numBPIAds += m_cr->m_adSSNumAds;
}
else
m_adBSSSameasBPI = false;
// get paid inclusion ad and its backup
m_numGotAds = 0;
if(m_cr->m_adPIEnable) {
if(getAd(PI_PRIMARY, m_cr->m_adCGI[PI_PRIMARY], numPIAds))
m_numGotAds++;
if(getAd(PI_BACKUP, m_cr->m_adCGI[PI_BACKUP], numBPIAds))
m_numGotAds++;
}
else
m_numGotAds += 2;
// get skyskraper ad and its backup
if(m_cr->m_adSSEnable) {
// if skyscraper is not using the same feed as paid inclusion
// get ads
if(!m_adSSSameasPI) {
if(getAd(SS_PRIMARY, m_cr->m_adCGI[SS_PRIMARY],
m_cr->m_adSSNumAds))
m_numGotAds++;
}
else
m_numGotAds++;
// if backup skyscraper is not using the same feed as backup
// paid inclusion get ads
if(!m_adBSSSameasBPI) {
if(getAd(SS_BACKUP, m_cr->m_adCGI[SS_BACKUP],
m_cr->m_adSSNumAds))
m_numGotAds++;
}
else
m_numGotAds++;
}
else
m_numGotAds += 2;
// if we got all of them back already, return true...we failed
if(m_numGotAds >= 4)
return true;
return false;
}
bool Ads::getAd ( int32_t index ,
char *cgi ,
int32_t numAds ) {
char tmp[1024] = {0};
int32_t clen = gbstrlen(cgi);
// if there is no cgi string, bail
if(clen <= 0)
return true;
// if string is greater than buffer, bail
if(clen > 1024) {
log("query: Ad Feed CGI string %"INT32" is > 1024.", index);
return true;
}
char *p = tmp;
int32_t plen = htmlDecode(tmp, cgi, clen,false,0);
SafeBuf sb(1024);
// . replace standard characters with our search values
while(*p) {
char *beg = p;
p = strstr(p, "%");
if(p) {
p++;
if( beg != p )
sb.safeMemcpy(beg, (int32_t)(p-beg-1));
switch(*p) {
// insert url encoded query
case 'q':
sb.urlEncode(m_q, m_qlen);
break;
// insert page number
case 'p':
sb.safePrintf("%"INT32"", m_pageAds);
break;
// insert number of ads to return
case 'n':
sb.safePrintf("%"INT32"", numAds);
break;
// insert querying IP
case 'i':
sb.safePrintf("%s", iptoa(m_queryIP));
break;
// insert %
case '%':
sb.safePrintf("%%");
break;
default :
break;
}
p++;
}
else {
p = tmp;
int32_t len = plen - (beg-p);
sb.safeMemcpy(beg, len);
sb.pushChar('\0');
break;
}
}
log(LOG_DEBUG, "query: Ad feed request[%"INT32"] url is: %s",
index, sb.getBufStart());
// make it a url
//m_url[index].set(sb.getBufStart(), sb.length(), false /*addWWW?*/);
// get the url
char *url = sb.getBufStart();
AdFeed *af = (AdFeed *)mmalloc(sizeof(AdFeed), "AdFeed");
if(!af) {
log("query: Ad feed malloc failed.");
return true;
}
af->m_ads = this;
af->m_index = index;
// get the xml
if (!g_httpServer.getDoc( url , // &m_url[index] ,
0 , // ip
0 , // offset
-1 , // size
0 , // if mod since
af , // state
gotDocWrapper,
m_cr->m_adFeedTimeOut, // declared in collRec
0 , // proxyIp --> none
0 , // proxyPort
20*1024 , // 20k maxTextDocLen
20*1024 ))// 20k maxOhterDocLen
return false;
// we got it w/o blocking, MUST be an error!! g_errno should be set
return true;
}
void gotDocWrapper ( void *state , TcpSocket *s ) {
AdFeed *af = (AdFeed *)state;
// extract our class
Ads *ads = af->m_ads;
ads->gotDoc(s, af->m_index);
mfree(af, sizeof(AdFeed), "AdFeed");
if(!ads->gotAllRequests())
return;
ads->selectDisplayAds();
// call callback
ads->m_callback ( ads->m_state );
}
void Ads::gotDoc ( TcpSocket *ts, int32_t index ) {
m_numGotAds++;
// return if g_errno set
if ( g_errno ) {
log("query: Ad feed returned an error: %s", mstrerror(g_errno));
return;
}
// this is guaranteed now to be NULL terminated
char *p = ts->m_readBuf;
int32_t plen = ts->m_readBufSize;
int32_t len = ts->m_readOffset;
if ( ! p || plen <= 0 ) {
log("query: Ad feed gave empty reply.");
return;
}
HttpMime mime;
mime.set(p, len, NULL);//&m_url[index]);
if(mime.getHttpStatus() != 200) {
log("query: Ad feed returned %"INT32" status, bailing.",
mime.getHttpStatus());
return;
}
char *content = p + mime.getMimeLen();
int32_t contentLen = mime.getContentLen();
if((contentLen == -1) && (len != 0))
contentLen = len - mime.getMimeLen();
log(LOG_DEBUG, "query: Ad feed response: %s", content);
int16_t charset = get_iana_charset( mime.getCharset(),
mime.getCharsetLen() );
// sanity check
if ( charset != csUTF8 && charset != csASCII ) { char *xx=NULL;*xx=0; }
Xml *xml = &m_xml[index];
xml->set( content, contentLen,
false, 0, true, TITLEREC_CURRENT_VERSION);
char *rsltStr = m_cr->m_adResultXml[index];
char *titleStr = m_cr->m_adTitleXml [index];
char *urlStr = m_cr->m_adUrlXml [index];
char *descStr = m_cr->m_adDescXml [index];
char *linkStr = m_cr->m_adLinkXml [index];
int32_t rsltStrLen = gbstrlen(rsltStr);
int32_t begPtr = 0;
int32_t endPtr = xml->getNumNodes();
char **titles = m_titles [index];
int32_t *titlesLen = m_titlesLen[index];
char **desc = m_desc [index];
int32_t *descLen = m_descLen [index];
char **sites = m_sites [index];
int32_t *sitesLen = m_sitesLen [index];
char **urls = m_urls [index];
int32_t *urlsLen = m_urlsLen [index];
int32_t numAds = 0;
while(begPtr < endPtr) {
// . We will loop until we are out of nodes and should break
// . Search for result tag
begPtr = xml->getNodeNum(begPtr, endPtr, rsltStr, rsltStrLen);
// . If there is no response.result, consider parsing finished
if( begPtr == -1 ) break;
// . We don't want to use the back result tags, so skip it
if( xml->isBackTag( begPtr++ ) ) continue;
// . save pointer to ad title
titles[numAds] = xml->getString(begPtr, endPtr, titleStr,
&titlesLen[numAds]);
titles[numAds] =removeCDATA(titles[numAds],&titlesLen[numAds]);
// . save pointers for summary lines
desc[numAds] = xml->getString(begPtr, endPtr, descStr,
&descLen[numAds]);
desc[numAds] = removeCDATA(desc[numAds], &descLen[numAds]);
// . save pointer for site
sites[numAds] = xml->getString(begPtr, endPtr, linkStr,
&sitesLen[numAds]);
sites[numAds] = removeCDATA(sites[numAds], &sitesLen[numAds]);
// . save pointer to click url
urls[numAds] = xml->getString(begPtr, endPtr, urlStr,
&urlsLen[numAds]);
urls[numAds] = removeCDATA(urls[numAds], &urlsLen[numAds]);
// . increment number of ads found
numAds++;
}
log(LOG_DEBUG, "query: Ad feed[%"INT32"] returned %"INT32" ads on page %"INT32"",
index, numAds, m_pageAds);
// . let mem table know we stole the buffer as well
if( !relabel( p, plen, "Ads-sktXml" ) ) {
log("query: Could not relabel ad memory from socket!");
return;
}
// . we are now going to free the socket buffer when we are done,
// so let socket know
m_buf [index] = p;
m_bufLen [index] = plen;
m_numAds [index] = numAds;
ts->m_readBuf = NULL;
}
void Ads::selectDisplayAds( ) {
if(m_adSSSameasPI) {
// Same primary AND backup AND backup has MORE
if(m_adBSSSameasBPI &&
m_numAds[PI_PRIMARY] < m_numAds[PI_BACKUP]) {
m_indexPIAds = PI_BACKUP;
m_indexSSAds = PI_BACKUP;
}
// Same primary AND different backup AND backup has MORE
else if(!m_adBSSSameasBPI &&
(m_numAds[PI_PRIMARY] <
(m_numAds[PI_BACKUP] + m_numAds[SS_BACKUP]))) {
m_indexPIAds = PI_BACKUP;
m_indexSSAds = SS_BACKUP;
}
}
else if(!m_adSSSameasPI && m_adBSSSameasBPI) {
// Different primary AND same backup AND backup has MORE
if(m_numAds[PI_BACKUP] >
(m_numAds[PI_PRIMARY] + m_numAds[SS_PRIMARY])) {
m_indexPIAds = PI_BACKUP;
m_indexSSAds = PI_BACKUP;
}
}
else {
// Different primary AND different backup
// AND backup has MORE
if(!m_numAds[PI_PRIMARY] && m_numAds[PI_BACKUP])
m_indexPIAds = PI_BACKUP;
// AND backup has MORE
if(!m_numAds[SS_PRIMARY] && m_numAds[SS_BACKUP])
m_indexSSAds = SS_BACKUP;
}
}
void Ads::printAd( SafeBuf *sb ,
char *url , int32_t urlLen,
char *title , int32_t titleLen,
char *desc , int32_t descLen,
char *site , int32_t siteLen,
int32_t numCharPerLine ) {
sb->safePrintf( "<a href=" );
sb->safeMemcpy (url, urlLen);
sb->safePrintf( "><b>" );
sb->safeMemcpy (title, titleLen);
sb->safePrintf( "</b></a><br>\n" );
sb->safeMemcpy (desc, descLen);
sb->safePrintf( "<br>\n" );
sb->safePrintf( "<b>" );
int32_t len = siteLen;
int32_t numToPrint = numCharPerLine;
int32_t numSplits = len / numCharPerLine;
int32_t index = 0;
if(len % numCharPerLine)
numSplits++;
if(numToPrint > len)
numToPrint = len;
sb->safeMemcpy (site, numToPrint);
for(int32_t i = 1; i < numSplits; i++) {
sb->safePrintf("<br>");
index += numCharPerLine;
if(numCharPerLine*(i+1) > len) {
numToPrint = len % numCharPerLine;
}
sb->safeMemcpy (site+index, numToPrint);
}
sb->safePrintf( "</b><br>\n" );
}
void Ads::printPaidInclusionAds(SafeBuf *sb, int32_t numCharPerLine) {
int32_t end = m_numAds[m_indexPIAds];
if((m_indexPIAds == PI_PRIMARY && m_adSSSameasPI ) ||
(m_indexPIAds == PI_BACKUP && m_adBSSSameasBPI) )
end = m_cr->m_adPINumAds;
for( int32_t i = 0; i < end; i++) {
printAd(sb,
m_urls [m_indexPIAds][i], m_urlsLen [m_indexPIAds][i],
m_titles[m_indexPIAds][i], m_titlesLen[m_indexPIAds][i],
m_desc [m_indexPIAds][i], m_descLen [m_indexPIAds][i],
m_sites [m_indexPIAds][i], m_sitesLen [m_indexPIAds][i], numCharPerLine );
if(i != end-1)
sb->safePrintf("<br>\n");
}
}
void Ads::printSkyscraperAds (SafeBuf *sb, int32_t numCharPerLine) {
int32_t i = 0;
int32_t end = m_numAds[m_indexSSAds];
if(m_indexSSAds == PI_PRIMARY || m_indexSSAds == PI_BACKUP) {
i = m_cr->m_adPINumAds;
end += i;
}
for( ; i < end; i++) {
printAd(sb,
m_urls [m_indexSSAds][i], m_urlsLen [m_indexSSAds][i],
m_titles[m_indexSSAds][i], m_titlesLen[m_indexSSAds][i],
m_desc [m_indexSSAds][i], m_descLen [m_indexSSAds][i],
m_sites [m_indexSSAds][i], m_sitesLen [m_indexSSAds][i],
numCharPerLine);
if(i != end-1)
sb->safePrintf("<br>\n");
}
}
/*
int32_t Ads::s_availableAds[16][MAX_AD_FEEDS] = {{0}};
int32_t Ads::s_numAvailableAds[16] = {0} ;
void Ads::initCollAvailAds ( ) {
for(collnum_t cn = g_collectiondb.getFirstCollnum();
cn >= 0;
cn = g_collectiondb.getNextCollnum(cn) )
setAvailableAds(g_collectiondb.getCollName(cn));
}
void Ads::setAvailableAds ( char *coll ) {
CollectionRec *cr = g_collectiondb.getRec(coll);
s_numAvailableAds[cr->m_collnum] = 0;
for(int32_t i = 0; i < MAX_AD_FEEDS; i++) {
if(cr->m_adFeedEnable[i]) {
if(cr->m_adCGI[i][0] != '\0')
s_availableAds[cr->m_collnum]
[s_numAvailableAds[cr->m_collnum]++]=i;
}
}
}
int32_t Ads::getAdFeedIndex ( collnum_t cn ) {
static int32_t count = 0;
if(!s_numAvailableAds[cn])
return 0;
count++;
count %= s_numAvailableAds[cn];
return s_availableAds[cn][count];
}
*/