mirror of
https://github.com/BeamMP/BeamMP-Launcher.git
synced 2025-07-02 07:56:26 +00:00
dos2unix, cache http connections for reuse
This commit is contained in:
parent
60b6d6f9ac
commit
29b4fa9b81
51
src/Http.cpp
51
src/Http.cpp
@ -1,20 +1,30 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include "Http.h"
|
||||
#include <mutex>
|
||||
#include <cmath>
|
||||
#include <fstream>
|
||||
#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;
|
||||
std::string HTTP::Get(const std::string &IP) {
|
||||
std::string HTTP::Get(const std::string& host_and_target) {
|
||||
static std::mutex 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_follow_location(true);
|
||||
|
||||
@ -22,13 +32,14 @@ std::string HTTP::Get(const std::string &IP) {
|
||||
{ "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;
|
||||
|
||||
if (res) {
|
||||
if (res->status == 200) {
|
||||
Ret = res->body;
|
||||
}else spdlog::error(res->reason);
|
||||
} else
|
||||
spdlog::error(res->reason);
|
||||
|
||||
} else {
|
||||
if (isDownload) {
|
||||
@ -40,18 +51,18 @@ std::string HTTP::Get(const std::string &IP) {
|
||||
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;
|
||||
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));
|
||||
std::string Ret;
|
||||
|
||||
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->status != 200) {
|
||||
@ -62,7 +73,7 @@ std::string HTTP::Post(const std::string& IP, const std::string& Fields) {
|
||||
spdlog::error("HTTP Post failed on " + to_string(res.error()));
|
||||
}
|
||||
} 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->status != 200) {
|
||||
spdlog::error(res->reason);
|
||||
@ -73,11 +84,12 @@ std::string HTTP::Post(const std::string& IP, const std::string& Fields) {
|
||||
}
|
||||
}
|
||||
|
||||
if(Ret.empty())return "-1";
|
||||
else return Ret;
|
||||
if (Ret.empty())
|
||||
return "-1";
|
||||
else
|
||||
return Ret;
|
||||
}
|
||||
|
||||
|
||||
bool HTTP::Download(const std::string& IP, const std::string& Path) {
|
||||
static std::mutex Lock;
|
||||
std::scoped_lock Guard(Lock);
|
||||
@ -86,7 +98,8 @@ bool HTTP::Download(const std::string &IP, const std::string &Path) {
|
||||
std::string Ret = Get(IP);
|
||||
isDownload = false;
|
||||
|
||||
if(Ret.empty())return false;
|
||||
if (Ret.empty())
|
||||
return false;
|
||||
|
||||
std::ofstream File(Path, std::ios::binary);
|
||||
if (File.is_open()) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user