Update source/src/utils/utils.cpp
This commit is contained in:
@@ -2,10 +2,11 @@
|
||||
#include "httplib.hpp"
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
|
||||
#include <memory>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <cstdlib>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
@@ -479,12 +480,24 @@ HttpResult make_http_request(const std::string& url) {
|
||||
return {false, 0, "", "Invalid URL format"};
|
||||
}
|
||||
|
||||
// Create HTTP client
|
||||
httplib::Client cli(host);
|
||||
cli.set_connection_timeout(10); // 10 second timeout
|
||||
// Create HTTP or HTTPS client based on protocol
|
||||
std::string protocol = url.substr(0, protocol_end);
|
||||
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
|
||||
auto res = cli.Get(path);
|
||||
auto res = cli->Get(path);
|
||||
if (!res) {
|
||||
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) {
|
||||
// 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);
|
||||
if (!result.success) {
|
||||
warning << "Failed to download file from URL: " << url << std::endl;
|
||||
|
Reference in New Issue
Block a user