3030
3131std::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