Update source/src/utils/utils.cpp
All checks were successful
Build-Test-Publish / build (linux/amd64) (push) Successful in 1m5s
Build-Test-Publish / build (linux/arm64) (push) Successful in 1m44s

This commit is contained in:
j842
2025-08-17 22:16:25 +12:00
parent e1c002568a
commit 36e9a3fce7

View File

@@ -2,10 +2,11 @@
#include "httplib.hpp" #include "httplib.hpp"
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <memory>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <fstream> #include <fstream>
#include <cstdlib>
#include <vector> #include <vector>
#include <algorithm> #include <algorithm>
#include <filesystem> #include <filesystem>
@@ -479,12 +480,24 @@ HttpResult make_http_request(const std::string& url) {
return {false, 0, "", "Invalid URL format"}; return {false, 0, "", "Invalid URL format"};
} }
// Create HTTP client // Create HTTP or HTTPS client based on protocol
httplib::Client cli(host); std::string protocol = url.substr(0, protocol_end);
cli.set_connection_timeout(10); // 10 second timeout std::unique_ptr<httplib::Client> cli;
if (protocol == "https") {
// For HTTPS, include the full scheme and host
cli = std::make_unique<httplib::Client>("https://" + host);
} else if (protocol == "http") {
cli = std::make_unique<httplib::Client>("http://" + host);
} else {
return {false, 0, "", "Unsupported protocol: " + protocol};
}
cli->set_connection_timeout(10); // 10 second timeout
cli->set_read_timeout(30); // 30 second read timeout
// Make GET request // Make GET request
auto res = cli.Get(path); auto res = cli->Get(path);
if (!res) { if (!res) {
return {false, 0, "", "Failed to connect to server"}; return {false, 0, "", "Failed to connect to server"};
} }
@@ -499,6 +512,20 @@ HttpResult make_http_request(const std::string& url) {
} }
bool download_file(const std::string &url, const std::string &destination) { bool download_file(const std::string &url, const std::string &destination) {
// For HTTPS URLs, use curl as it handles SSL properly in static builds
if (url.substr(0, 8) == "https://") {
std::string cmd = "curl -fsSL " + quote(url) + " -o " + quote(destination) + " 2>/dev/null";
int result = system(cmd.c_str());
if (result != 0) {
warning << "Failed to download file from URL: " << url << std::endl;
return false;
}
// Check if file was actually created
std::ifstream check_file(destination);
return check_file.good();
}
// For HTTP URLs, use the built-in httplib
auto result = make_http_request(url); auto result = make_http_request(url);
if (!result.success) { if (!result.success) {
warning << "Failed to download file from URL: " << url << std::endl; warning << "Failed to download file from URL: " << url << std::endl;