/// /// Created by Anonymous275 on 1/17/22 /// Copyright (c) 2021-present Anonymous275 read the LICENSE file for more info. /// #define CPPHTTPLIB_OPENSSL_SUPPORT #include #include #include "Logger.h" #include #include "Http.h" #include #include bool HTTP::isDownload = false; std::string HTTP::Get(const std::string &IP) { static std::mutex Lock; std::scoped_lock Guard(Lock); auto pos = IP.find('/',10); httplib::Client cli(IP.substr(0, pos)); cli.set_connection_timeout(std::chrono::seconds(10)); auto res = cli.Get(IP.substr(pos).c_str(), ProgressBar); std::string Ret; if(res.error() == httplib::Error::Success){ if(res->status == 200){ Ret = res->body; }else LOG(ERROR) << res->reason; }else{ if(isDownload) { std::cout << "\n"; } LOG(ERROR) << "HTTP Get failed on " << httplib::to_string(res.error()); } return Ret; } std::string HTTP::Post(const std::string& IP, const std::string& Fields) { static std::mutex Lock; std::scoped_lock Guard(Lock); auto pos = IP.find('/',10); httplib::Client cli(IP.substr(0, pos)); cli.set_connection_timeout(std::chrono::seconds(10)); std::string Ret; if(!Fields.empty()) { httplib::Result res = cli.Post(IP.substr(pos).c_str(), Fields, "application/json"); if(res.error() == httplib::Error::Success) { if(res->status != 200) { LOG(ERROR) << res->reason; } Ret = res->body; } else{ LOG(ERROR) << "HTTP Post failed on " << httplib::to_string(res.error()); } } else { httplib::Result res = cli.Post(IP.substr(pos).c_str()); if(res.error() == httplib::Error::Success) { if (res->status != 200) { LOG(ERROR) << res->reason; } Ret = res->body; } else { LOG(ERROR) << "HTTP Post failed on " << httplib::to_string(res.error()); } } if(Ret.empty())return "-1"; else return Ret; } bool HTTP::ProgressBar(size_t c, size_t t){ if(isDownload) { static double progress_bar_adv; progress_bar_adv = round(double(c) / double(t) * 25); std::cout << "\r"; std::cout << "Progress : [ "; std::cout << round(double(c) / double(t) * 100); std::cout << "% ] ["; int i; for (i = 0; i <= progress_bar_adv; i++)std::cout << "#"; for (i = 0; i < 25 - progress_bar_adv; i++)std::cout << "."; std::cout << "]"; } return true; } bool HTTP::Download(const std::string &IP, const std::string &Path) { static std::mutex Lock; std::scoped_lock Guard(Lock); isDownload = true; std::string Ret = Get(IP); isDownload = false; if(Ret.empty())return false; std::ofstream File(Path, std::ios::binary); if(File.is_open()) { File << Ret; File.close(); std::cout << "\n"; LOG(INFO) << "Download Complete!"; } else { LOG(INFO) << "Failed to open file directory: " << Path; return false; } return true; }