iw4x-client/src/Utils/Cache.cpp

58 lines
1.2 KiB
C++
Raw Normal View History

2016-11-21 04:50:55 -05:00
#include "STDInclude.hpp"
namespace Utils
{
const char* Cache::Urls[] =
2016-11-21 04:50:55 -05:00
{
2017-01-15 15:07:06 -05:00
"https://iw4xcachep26muba.onion.to",
"https://iw4xcachep26muba.onion.link",
2017-03-10 08:59:33 -05:00
"https://iw4xcachep26muba.onion.rip",
2017-01-15 15:07:06 -05:00
/*"https://iw4xcachejnetuln.onion.to",
2016-11-21 04:50:55 -05:00
"https://iw4xcachedjodc4y.onion.to",
*/
};
std::string Cache::ValidUrl;
2016-11-21 05:09:42 -05:00
std::mutex Cache::CacheMutex;
2016-11-21 04:50:55 -05:00
std::string Cache::GetStaticUrl(std::string path)
2016-11-21 04:50:55 -05:00
{
return Cache::Urls[0] + path;
2016-11-21 04:50:55 -05:00
}
std::string Cache::GetUrl(std::string url, std::string path)
{
return url + path;
}
std::string Cache::GetFile(std::string path, int timeout, std::string useragent)
2016-11-21 04:50:55 -05:00
{
std::lock_guard<std::mutex> _(Cache::CacheMutex);
if (Cache::ValidUrl.empty())
2016-11-21 04:50:55 -05:00
{
for (int i = 0; i < ARRAYSIZE(Cache::Urls); i++)
2016-11-21 04:50:55 -05:00
{
std::string result = Utils::WebIO(useragent, Cache::GetUrl(Cache::Urls[i], path)).setTimeout(timeout)->get();
2016-11-21 05:09:42 -05:00
if (!result.empty())
{
Cache::ValidUrl = Cache::Urls[i];
2016-11-21 05:09:42 -05:00
return result;
}
2016-11-21 04:50:55 -05:00
}
return "";
2016-11-21 04:50:55 -05:00
}
2016-11-21 05:09:42 -05:00
else
{
return Utils::WebIO(useragent, Cache::GetUrl(Cache::ValidUrl, path)).setTimeout(timeout)->get();
2016-11-21 05:09:42 -05:00
}
2016-11-21 04:50:55 -05:00
}
void Cache::Uninitialize()
{
std::lock_guard<std::mutex> _(Cache::CacheMutex);
Cache::ValidUrl.clear();
}
2016-11-21 04:50:55 -05:00
}