Skip to content

Commit 22c4e68

Browse files
authored
Merge pull request #16394 from omoerbeek/rec-pubsuffix-eod
rec: don't use a vector of string for internal pubsuffixlist
2 parents 22b7a74 + 9e6769f commit 22c4e68

File tree

3 files changed

+48
-42
lines changed

3 files changed

+48
-42
lines changed

pdns/recursordist/mkpubsuffixcc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ trap "rm -f $temp" 0 1 2 3 15
1010
set -e
1111
curl -o $temp -s -S https://publicsuffix.org/list/public_suffix_list.dat
1212
(echo "#include \"pubsuffix.hh\""
13-
echo "const std::vector<std::string> g_pubsuffix = {";
13+
echo "const std::string g_pubsuffix = ";
1414
for a in $(grep -v "//" "$temp" | grep \\. | egrep "^[.0-9a-z-]*$")
1515
do
16-
echo \"$a\",
16+
echo \"$a\\\\n\"
1717
done
18-
echo "};") > "$1"
18+
echo ";") > "$1"

pdns/recursordist/pubsuffix.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include <vector>
2626

2727
extern std::vector<std::vector<std::string>> g_pubs;
28-
extern const std::vector<std::string> g_pubsuffix;
28+
extern const std::string g_pubsuffix;
2929

3030
/* initialize the g_pubs variable with the public suffix list,
3131
using the file passed in parameter if any, or the built-in

pdns/recursordist/pubsuffixloader.cc

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -30,44 +30,58 @@
3030

3131
std::vector<std::vector<std::string>> g_pubs;
3232

33-
void initPublicSuffixList(const std::string& file)
33+
static bool initPublicSuffixList(const std::string& file, std::istream& stream, std::vector<std::vector<std::string>>& pbList)
3434
{
35-
std::vector<std::vector<std::string>> pbList;
3635

37-
bool loaded = false;
38-
if (!file.empty()) {
39-
try {
40-
Regex reg("^[.0-9a-z-]*$");
41-
std::ifstream suffixFile(file);
42-
if (!suffixFile.is_open()) {
43-
throw std::runtime_error("Error opening the public suffix list file");
44-
}
36+
try {
37+
Regex reg("^[.0-9a-z-]*$");
4538

46-
std::string line;
47-
while (std::getline(suffixFile, line)) {
48-
if (line.empty() || (line.rfind("//", 0) == 0)) {
49-
/* skip empty and commented lines */
39+
std::string line;
40+
while (std::getline(stream, line)) {
41+
if (line.empty() || (line.rfind("//", 0) == 0)) {
42+
/* skip empty and commented lines */
43+
continue;
44+
}
45+
try {
46+
line = toLower(line);
47+
if (!reg.match(line)) {
5048
continue;
5149
}
52-
try {
53-
line = toLower(line);
54-
if (!reg.match(line)) {
55-
continue;
56-
}
57-
DNSName name(toLower(line));
58-
if (name.countLabels() < 2) {
59-
continue;
60-
}
61-
pbList.emplace_back(name.labelReverse().getRawLabels());
62-
}
63-
catch (...) {
64-
/* not a DNS name, ignoring */
50+
DNSName name(line);
51+
if (name.countLabels() < 2) {
6552
continue;
6653
}
54+
pbList.emplace_back(name.labelReverse().getRawLabels());
55+
}
56+
catch (...) {
57+
/* not a DNS name, ignoring */
58+
continue;
6759
}
60+
}
6861

62+
if (file != "internal") {
6963
g_slog->withName("runtime")->info(Logr::Info, "Loaded the Public Suffix List", "file", Logging::Loggable(file));
70-
loaded = true;
64+
}
65+
return true;
66+
}
67+
catch (const std::exception& e) {
68+
g_slog->withName("runtime")->error(Logr::Error, e.what(), "Error while loading the Public Suffix List", "file", Logging::Loggable(file));
69+
}
70+
return false;
71+
}
72+
73+
void initPublicSuffixList(const std::string& file)
74+
{
75+
bool loaded = false;
76+
std::vector<std::vector<std::string>> pbList;
77+
78+
if (!file.empty()) {
79+
try {
80+
std::ifstream suffixFile(file);
81+
if (!suffixFile.is_open()) {
82+
throw std::runtime_error("Error opening the public suffix list file");
83+
}
84+
loaded = initPublicSuffixList(file, suffixFile, pbList);
7185
}
7286
catch (const std::exception& e) {
7387
g_slog->withName("runtime")->error(Logr::Error, e.what(), "Error while loading the Public Suffix List", "file", Logging::Loggable(file));
@@ -76,17 +90,9 @@ void initPublicSuffixList(const std::string& file)
7690

7791
if (!loaded) {
7892
pbList.clear();
79-
80-
for (const auto& entry : g_pubsuffix) {
81-
const auto low = toLower(entry);
82-
std::vector<std::string> parts;
83-
stringtok(parts, low, ".");
84-
std::reverse(parts.begin(), parts.end());
85-
parts.shrink_to_fit();
86-
pbList.emplace_back(std::move(parts));
87-
}
93+
std::istringstream stream(g_pubsuffix);
94+
initPublicSuffixList("internal", stream, pbList);
8895
}
89-
9096
std::sort(pbList.begin(), pbList.end());
9197
g_pubs = std::move(pbList);
9298
}

0 commit comments

Comments
 (0)