forked from brave-experiments/ad-block
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
103 lines (91 loc) · 3.32 KB
/
main.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
/* Copyright (c) 2015 Brian R. Bondy. Distributed under the MPL2 license.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <algorithm>
#include <cerrno>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include "./ABPFilterParser.h"
using std::cout;
using std::endl;
using std::string;
string getFileContents(const char *filename) {
std::ifstream in(filename, std::ios::in);
if (in) {
std::ostringstream contents;
contents << in.rdbuf();
in.close();
return(contents.str());
}
throw(errno);
}
void writeFile(const char *filename, const char *buffer, int length) {
std::ofstream outFile(filename, std::ios::out | std::ios::binary);
if (outFile) {
outFile.write(buffer, length);
outFile.close();
return;
}
throw(errno);
}
int main(int argc, char**argv) {
std::string && easyListTxt = getFileContents("./test/data/easylist.txt");
std::string && ublockUnblockTxt =
getFileContents("./test/data/ublock-unbreak.txt");
std::string && braveUnblockTxt =
getFileContents("./test/data/brave-unbreak.txt");
const char *urlsToCheck[] = {
// ||pagead2.googlesyndication.com^$~object-subrequest
"http://pagead2.googlesyndication.com/pagead/show_ads.js",
// Should be blocked by: ||googlesyndication.com/safeframe/$third-party
"http://tpc.googlesyndication.com/safeframe/1-0-2/html/container.html",
// Should be blocked by: ||googletagservices.com/tag/js/gpt_$third-party
"http://www.googletagservices.com/tag/js/gpt_mobile.js",
// Shouldn't be blocked
"http://www.brianbondy.com"
};
// This is the site who's URLs are being checked, not the domain of the
// URL being checked.
const char *currentPageDomain = "slashdot.org";
// Parse filter lists
ABPFilterParser parser;
parser.parse(easyListTxt.c_str());
parser.parse(ublockUnblockTxt.c_str());
parser.parse(braveUnblockTxt.c_str());
// Do the checks
std::for_each(urlsToCheck, urlsToCheck + sizeof(urlsToCheck)
/ sizeof(urlsToCheck[0]),
[&parser, currentPageDomain](std::string const &urlToCheck) {
if (parser.matches(urlToCheck.c_str(),
FONoFilterOption, currentPageDomain)) {
cout << urlToCheck << ": You should block this URL!" << endl;
} else {
cout << urlToCheck << ": You should NOT block this URL!" << endl;
}
});
int size;
// This buffer is allocate on the heap, you must call delete[] when
// you're done using it.
char *buffer = parser.serialize(&size);
writeFile("./ABPFilterParserData.dat", buffer, size);
ABPFilterParser parser2;
// Deserialize uses the buffer directly for subsequent matches, do not free
// until all matches are done.
parser2.deserialize(buffer);
// Prints the same as parser.matches would
std::for_each(urlsToCheck, urlsToCheck + sizeof(urlsToCheck)
/ sizeof(urlsToCheck[0]),
[&parser2, currentPageDomain](std::string const &urlToCheck) {
if (parser2.matches(urlToCheck.c_str(),
FONoFilterOption, currentPageDomain)) {
cout << urlToCheck << ": You should block this URL!" << endl;
} else {
cout << urlToCheck << ": You should NOT block this URL!" << endl;
}
});
delete[] buffer;
return 0;
}