This commit is contained in:
@ -1,4 +1,8 @@
|
||||
#include "utils.hpp"
|
||||
#include "httplib.hpp"
|
||||
#include "json.hpp"
|
||||
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
@ -10,6 +14,7 @@
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
#include <cctype>
|
||||
#include <sstream>
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
@ -445,4 +450,183 @@ std::string tolower(const std::string& str) {
|
||||
return result;
|
||||
}
|
||||
|
||||
// Common utility function to make HTTP requests
|
||||
struct HttpResult {
|
||||
bool success;
|
||||
int status;
|
||||
std::string body;
|
||||
std::string error;
|
||||
};
|
||||
|
||||
HttpResult make_http_request(const std::string& url) {
|
||||
try {
|
||||
// Parse the URL to get host and path
|
||||
std::string host;
|
||||
std::string path;
|
||||
size_t protocol_end = url.find("://");
|
||||
if (protocol_end != std::string::npos) {
|
||||
size_t host_start = protocol_end + 3;
|
||||
size_t path_start = url.find('/', host_start);
|
||||
if (path_start != std::string::npos) {
|
||||
host = url.substr(host_start, path_start - host_start);
|
||||
path = url.substr(path_start);
|
||||
} else {
|
||||
host = url.substr(host_start);
|
||||
path = "/";
|
||||
}
|
||||
} else {
|
||||
return {false, 0, "", "Invalid URL format"};
|
||||
}
|
||||
|
||||
// Create HTTP client
|
||||
httplib::Client cli(host);
|
||||
cli.set_connection_timeout(10); // 10 second timeout
|
||||
|
||||
// Make GET request
|
||||
auto res = cli.Get(path);
|
||||
if (!res) {
|
||||
return {false, 0, "", "Failed to connect to server"};
|
||||
}
|
||||
if (res->status != 200) {
|
||||
return {false, res->status, "", "HTTP request failed with status " + std::to_string(res->status)};
|
||||
}
|
||||
|
||||
return {true, res->status, res->body, ""};
|
||||
} catch (const std::exception& e) {
|
||||
return {false, 0, "", std::string("Exception: ") + e.what()};
|
||||
}
|
||||
}
|
||||
|
||||
bool download_file(const std::string &url, const std::string &destination) {
|
||||
auto result = make_http_request(url);
|
||||
if (!result.success) {
|
||||
warning << "Failed to download file from URL: " << url << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
std::ofstream out_file(destination, std::ios::binary);
|
||||
if (!out_file) {
|
||||
warning << "Failed to open file for writing: " << destination << std::endl;
|
||||
return false;
|
||||
}
|
||||
out_file.write(result.body.c_str(), result.body.size());
|
||||
out_file.close();
|
||||
return true;
|
||||
} catch (const std::exception& e) {
|
||||
warning << "Failed to download file from URL: " << url << std::endl;
|
||||
warning << "Exception: " << e.what() << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
nlohmann::json get_json_from_url(const std::string &url) {
|
||||
auto result = make_http_request(url);
|
||||
if (!result.success) {
|
||||
warning << "Failed to get JSON from URL: " << url << std::endl;
|
||||
return nlohmann::json();
|
||||
}
|
||||
|
||||
try {
|
||||
return nlohmann::json::parse(result.body);
|
||||
} catch (const nlohmann::json::parse_error& e) {
|
||||
warning << "Failed to parse JSON from URL: " << url << std::endl;
|
||||
warning << "JSON: " << result.body << std::endl;
|
||||
return nlohmann::json();
|
||||
}
|
||||
}
|
||||
|
||||
std::string get_string_from_url(const std::string &url) {
|
||||
auto result = make_http_request(url);
|
||||
if (!result.success) {
|
||||
warning << "Failed to get string from URL: " << url << std::endl;
|
||||
return std::string();
|
||||
}
|
||||
return result.body;
|
||||
}
|
||||
|
||||
bool match_line(const std::string &line, const std::string &pattern) {
|
||||
return trim(line) == trim(pattern);
|
||||
}
|
||||
|
||||
// replace or append a block of text to a file, matching first and last lines if replacing.
|
||||
// edits file in-place.
|
||||
bool file_replace_or_add_segment(std::string filepath, std::string segment)
|
||||
{
|
||||
std::string first_line = segment.substr(0, segment.find("\n"));
|
||||
|
||||
// look backwards until we get a non-empty line.
|
||||
size_t last_line_pos = segment.rfind("\n");
|
||||
while (last_line_pos != std::string::npos) {
|
||||
std::string last_line = segment.substr(last_line_pos + 1);
|
||||
if (!trim(last_line).empty()) {
|
||||
break;
|
||||
}
|
||||
last_line_pos = segment.rfind("\n", last_line_pos - 1);
|
||||
}
|
||||
std::string last_line = segment.substr(last_line_pos + 1);
|
||||
|
||||
// Read the entire file into memory
|
||||
std::ifstream input_file(filepath);
|
||||
if (!input_file.is_open()) {
|
||||
std::cerr << "Error: Unable to open file: " << filepath << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<std::string> file_lines;
|
||||
std::string line;
|
||||
while (std::getline(input_file, line)) {
|
||||
file_lines.push_back(line);
|
||||
}
|
||||
input_file.close();
|
||||
|
||||
// Try to find the matching block
|
||||
bool found_match = false;
|
||||
for (size_t i = 0; i < file_lines.size(); i++) {
|
||||
if (match_line(file_lines[i], first_line)) {
|
||||
// Found potential start, look for end
|
||||
for (size_t j = i + 1; j < file_lines.size(); j++) {
|
||||
if (match_line(file_lines[j], last_line)) {
|
||||
// Found matching block, replace it
|
||||
file_lines.erase(file_lines.begin() + i, file_lines.begin() + j + 1);
|
||||
|
||||
// Split segment into lines and insert them
|
||||
std::vector<std::string> segment_lines;
|
||||
std::istringstream segment_stream(segment);
|
||||
while (std::getline(segment_stream, line)) {
|
||||
segment_lines.push_back(line);
|
||||
}
|
||||
file_lines.insert(file_lines.begin() + i, segment_lines.begin(), segment_lines.end());
|
||||
|
||||
found_match = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (found_match) break;
|
||||
}
|
||||
}
|
||||
|
||||
// If no match found, append the segment
|
||||
if (!found_match) {
|
||||
std::istringstream segment_stream(segment);
|
||||
while (std::getline(segment_stream, line)) {
|
||||
file_lines.push_back(line);
|
||||
}
|
||||
}
|
||||
|
||||
// Write back to file
|
||||
std::ofstream output_file(filepath);
|
||||
if (!output_file.is_open()) {
|
||||
std::cerr << "Error: Unable to open file for writing: " << filepath << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const auto& line : file_lines) {
|
||||
output_file << line << "\n";
|
||||
}
|
||||
output_file.close();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
@ -5,6 +5,7 @@
|
||||
#include <map>
|
||||
|
||||
#include "output.hpp"
|
||||
#include "json.hpp"
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
@ -61,4 +62,11 @@ std::string get_line_wrap(std::string & src, int maxchars);
|
||||
|
||||
std::string tolower(const std::string& str);
|
||||
|
||||
bool download_file(const std::string& url, const std::string& destination);
|
||||
nlohmann::json get_json_from_url(const std::string& url);
|
||||
std::string get_string_from_url(const std::string& url);
|
||||
|
||||
// replace or append a block of text to a file, matching first and last lines if replacing.
|
||||
bool file_replace_or_add_segment(std::string filepath, std::string segment);
|
||||
|
||||
} // namespace dropshell
|
Reference in New Issue
Block a user