iw4x-client/src/Utils/Cache.cpp

50 lines
1.0 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
{
"https://iw4xcachep26muba.onion.to"/*,
"https://iw4xcachejnetuln.onion.to",
"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 < ARRAY_SIZE(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
}
}