dos2unix, cache http connections for reuse

This commit is contained in:
Lion Kortlepel 2024-03-10 12:21:53 +01:00
parent 60b6d6f9ac
commit 29b4fa9b81
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B
2 changed files with 157 additions and 144 deletions

View File

@ -1,20 +1,30 @@
#include <iostream>
#include <fstream>
#include <spdlog/spdlog.h>
#include "Http.h" #include "Http.h"
#include <mutex>
#include <cmath> #include <cmath>
#include <fstream>
#include <httplib.h> #include <httplib.h>
#include <iostream>
#include <mutex>
#include <spdlog/spdlog.h>
static httplib::Client& get_client(const std::string& host) {
static thread_local std::unordered_map<std::string /* host */, httplib::Client> s_clients_cache {};
if (!s_clients_cache.contains(host)) {
spdlog::debug("Caching connection to {}", host);
s_clients_cache.emplace(host, httplib::Client(host));
} else {
spdlog::debug("Reusing cached connection to {}", host);
}
return s_clients_cache.at(host);
}
bool HTTP::isDownload = false; bool HTTP::isDownload = false;
std::string HTTP::Get(const std::string &IP) { std::string HTTP::Get(const std::string& host_and_target) {
static std::mutex Lock; static std::mutex Lock;
std::scoped_lock Guard(Lock); std::scoped_lock Guard(Lock);
auto pos = IP.find('/',10); auto pos = host_and_target.find('/', 10);
auto& cli = get_client(host_and_target.substr(0, pos).c_str());
httplib::Client cli(IP.substr(0, pos).c_str());
cli.set_connection_timeout(std::chrono::seconds(10)); cli.set_connection_timeout(std::chrono::seconds(10));
cli.set_follow_location(true); cli.set_follow_location(true);
@ -22,16 +32,17 @@ std::string HTTP::Get(const std::string &IP) {
{ "Accept-Encoding", "gzip" } { "Accept-Encoding", "gzip" }
}; };
auto res = cli.Get(IP.substr(pos).c_str(), headers); auto res = cli.Get(host_and_target.substr(pos).c_str(), headers);
std::string Ret; std::string Ret;
if(res){ if (res) {
if(res->status == 200){ if (res->status == 200) {
Ret = res->body; Ret = res->body;
}else spdlog::error(res->reason); } else
spdlog::error(res->reason);
}else{ } else {
if(isDownload) { if (isDownload) {
std::cout << "\n"; std::cout << "\n";
} }
spdlog::error("HTTP Get failed on " + to_string(res.error())); spdlog::error("HTTP Get failed on " + to_string(res.error()));
@ -40,45 +51,46 @@ std::string HTTP::Get(const std::string &IP) {
return Ret; return Ret;
} }
std::string HTTP::Post(const std::string& IP, const std::string& Fields) { std::string HTTP::Post(const std::string& host_and_target, const std::string& Fields) {
static std::mutex Lock; static std::mutex Lock;
std::scoped_lock Guard(Lock); std::scoped_lock Guard(Lock);
auto pos = IP.find('/',10); auto pos = host_and_target.find('/', 10);
httplib::Client cli(IP.substr(0, pos).c_str()); auto& cli = get_client(host_and_target.substr(0, pos).c_str());
cli.set_connection_timeout(std::chrono::seconds(10)); cli.set_connection_timeout(std::chrono::seconds(10));
std::string Ret; std::string Ret;
if(!Fields.empty()) { if (!Fields.empty()) {
httplib::Result res = cli.Post(IP.substr(pos).c_str(), Fields, "application/json"); httplib::Result res = cli.Post(host_and_target.substr(pos).c_str(), Fields, "application/json");
if(res) { if (res) {
if (res->status != 200) { if (res->status != 200) {
spdlog::error(res->reason); spdlog::error(res->reason);
} }
Ret = res->body; Ret = res->body;
}else{ } else {
spdlog::error("HTTP Post failed on " + to_string(res.error())); spdlog::error("HTTP Post failed on " + to_string(res.error()));
} }
}else{ } else {
httplib::Result res = cli.Post(IP.substr(pos).c_str()); httplib::Result res = cli.Post(host_and_target.substr(pos).c_str());
if(res) { if (res) {
if (res->status != 200) { if (res->status != 200) {
spdlog::error(res->reason); spdlog::error(res->reason);
} }
Ret = res->body; Ret = res->body;
}else{ } else {
spdlog::error("HTTP Post failed on " + to_string(res.error())); spdlog::error("HTTP Post failed on " + to_string(res.error()));
} }
} }
if(Ret.empty())return "-1"; if (Ret.empty())
else return Ret; return "-1";
else
return Ret;
} }
bool HTTP::Download(const std::string& IP, const std::string& Path) {
bool HTTP::Download(const std::string &IP, const std::string &Path) {
static std::mutex Lock; static std::mutex Lock;
std::scoped_lock Guard(Lock); std::scoped_lock Guard(Lock);
@ -86,15 +98,16 @@ bool HTTP::Download(const std::string &IP, const std::string &Path) {
std::string Ret = Get(IP); std::string Ret = Get(IP);
isDownload = false; isDownload = false;
if(Ret.empty())return false; if (Ret.empty())
return false;
std::ofstream File(Path, std::ios::binary); std::ofstream File(Path, std::ios::binary);
if(File.is_open()) { if (File.is_open()) {
File << Ret; File << Ret;
File.close(); File.close();
std::cout << "\n"; std::cout << "\n";
spdlog::info("Download Complete!"); spdlog::info("Download Complete!");
}else{ } else {
spdlog::error("Failed to open file directory: " + Path); spdlog::error("Failed to open file directory: " + Path);
return false; return false;
} }