Use regex to extract NodeInfo addresses.

This commit is contained in:
tastytea 2020-01-14 21:53:42 +01:00
parent 223db7b255
commit 0d8b2e8490
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07

View File

@ -77,7 +77,6 @@ uint64_t Instance::get_max_chars() noexcept
answer_type Instance::get_nodeinfo() answer_type Instance::get_nodeinfo()
{ {
debuglog << "Finding location of NodeInfo on " << _hostname << "\n";
auto answer{make_request(http_method::GET, auto answer{make_request(http_method::GET,
_baseuri + "/.well-known/nodeinfo", {})}; _baseuri + "/.well-known/nodeinfo", {})};
if (!answer) if (!answer)
@ -86,15 +85,15 @@ answer_type Instance::get_nodeinfo()
return answer; return answer;
} }
size_t pos{0};
vector<string> hrefs; vector<string> hrefs;
constexpr string_view searchstring{R"("href":")"}; const regex re_href{R"("href"\s*:\s*"([^"]+)\")"};
while ((pos = answer.body.find(searchstring, pos)) != string::npos) smatch match;
string body = answer.body;
while (regex_search(body, match, re_href))
{ {
pos += searchstring.size(); hrefs.push_back(match[1].str());
auto endpos{answer.body.find('"', pos)};
hrefs.push_back(answer.body.substr(pos, endpos - pos));
debuglog << "Found href: " << hrefs.back() << '\n'; debuglog << "Found href: " << hrefs.back() << '\n';
body = match.suffix();
} }
sort(hrefs.begin(), hrefs.end()); // We assume they are sortable strings. sort(hrefs.begin(), hrefs.end()); // We assume they are sortable strings.
debuglog << "Selecting href: " << hrefs.back() << '\n'; debuglog << "Selecting href: " << hrefs.back() << '\n';