From 1ce8e79c63f8e913def6fdf71bfae0e0085ac163 Mon Sep 17 00:00:00 2001 From: smallboyc Date: Wed, 24 Jul 2024 13:07:31 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A8Fix=20:=20use=20httplib=20and=20rem?= =?UTF-8?q?ove=20curl?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 15 ++----- src/download.cpp | 115 +++++++++++++++++++++++------------------------ src/release.cpp | 40 ++++++++--------- 3 files changed, 77 insertions(+), 93 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d55868d..3d2ec6a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,8 +32,6 @@ include(FetchContent) # Make sure we link the static version set(BUILD_SHARED_LIBS OFF CACHE BOOL "Build static library" FORCE) set(BULD_STATIC_LIBS ON CACHE BOOL "" FORCE) -set(CURL_STATICLIB ON CACHE BOOL "" FORCE) -set(CURL_ZLIB OFF CACHE BOOL "" FORCE) # JSON FetchContent_Declare( @@ -42,12 +40,6 @@ FetchContent_Declare( GIT_TAG v3.11.3 ) -# CURL -FetchContent_Declare( - curl - URL https://curl.se/download/curl-7.80.0.tar.gz -) - # MINIZ # Configurer FetchContent pour miniz FetchContent_Declare( @@ -59,9 +51,6 @@ FetchContent_Declare( # Get and build JSON FetchContent_MakeAvailable(json) -# Get and build CURL -FetchContent_MakeAvailable(curl) - FetchContent_MakeAvailable(miniz) # # Grab all the source files @@ -69,10 +58,12 @@ file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS src/*) target_sources(${PROJECT_NAME} PRIVATE ${SOURCES}) target_include_directories(${PROJECT_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/src) -target_link_libraries(${PROJECT_NAME} PRIVATE nlohmann_json::nlohmann_json CURL::libcurl miniz) +target_link_libraries(${PROJECT_NAME} PRIVATE nlohmann_json::nlohmann_json miniz) add_subdirectory(Lab/Cool/lib/stringify/lib/fmt) target_link_libraries(${PROJECT_NAME} PRIVATE fmt::fmt) +add_subdirectory(Lab/lib/cpp-httplib) +target_link_libraries(${PROJECT_NAME} PRIVATE httplib) target_include_directories(${PROJECT_NAME} SYSTEM PRIVATE Lab/Cool/lib/expected/include) # --------------------- diff --git a/src/download.cpp b/src/download.cpp index 4ce5139..92b4d9d 100644 --- a/src/download.cpp +++ b/src/download.cpp @@ -1,79 +1,76 @@ #include "download.hpp" -#include "release.hpp" -#include "utils.hpp" #include -#include #include #include #include +#include "httplib.h" +#include "release.hpp" +#include "utils.hpp" namespace fs = std::filesystem; // download file from url -auto download_zip(nlohmann::basic_json<> const &release) - -> tl::expected, std::string> { - auto const url = get_coollab_download_url(release); - std::cout << url << std::endl; - CURL *curl; - curl = curl_easy_init(); - std::vector out_data; - if (curl) { - curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); - curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_memory_callback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &out_data); - CURLcode res; - res = curl_easy_perform(curl); - curl_easy_cleanup(curl); - if (res == CURLE_OK) - return out_data; - else { - std::cerr << "Failed to download zip file from URL: " << url << std::endl; - std::cerr << curl_easy_strerror(res) << std::endl; - return tl::make_unexpected("error"); // TODO : set a correct error message +auto download_zip(nlohmann::json const& release) -> tl::expected, std::string> +{ + std::string url = "https://github.com"; + httplib::Client cli(url); + auto const path = get_coollab_download_url(release); + + cli.set_follow_location(true); // Allow the client to follow redirects + + auto res = cli.Get(path); + if (res && res->status == 200) + { + // Copy the response body into a vector + std::vector zip_data(res->body.begin(), res->body.end()); + return zip_data; } - } else { - std::cerr << "Failed to initialize curl." << std::endl; - return tl::make_unexpected("error"); // TODO : set a correct error message - } + return tl::unexpected("Failed to download the file. Status code: " + std::to_string(res->status)); } -auto install_macos_dependencies_if_necessary() -> void { - // Check if Homebrew is already installed - if (std::system("command -v brew >/dev/null 2>&1")) - install_homebrew(); - else - std::cout << "Homebrew is already installed." << std::endl; +auto install_macos_dependencies_if_necessary() -> void +{ + // Check if Homebrew is already installed + if (std::system("command -v brew >/dev/null 2>&1")) + install_homebrew(); + else + std::cout << "Homebrew is already installed." << std::endl; - // Check if FFmpeg is already installed - if (std::system("command -v ffmpeg >/dev/null 2>&1")) - install_ffmpeg(); - else - std::cout << "FFmpeg is already installed." << std::endl; + // Check if FFmpeg is already installed + if (std::system("command -v ffmpeg >/dev/null 2>&1")) + install_ffmpeg(); + else + std::cout << "FFmpeg is already installed." << std::endl; } -auto install_homebrew() -> void { - std::cout << "Installing Homebrew..." << std::endl; - int result = std::system( - "/bin/bash -c \"$(curl -fsSL " - "https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\""); - if (result != 0) { - std::cerr << "Homebrew installation failed!" << std::endl; - exit(result); - } - std::cout << "Homebrew successfully installed." << std::endl; +auto install_homebrew() -> void +{ + std::cout << "Installing Homebrew..." << std::endl; + int result = std::system( + "/bin/bash -c \"$(curl -fsSL " + "https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\"" + ); + if (result != 0) + { + std::cerr << "Homebrew installation failed!" << std::endl; + exit(result); + } + std::cout << "Homebrew successfully installed." << std::endl; } -auto install_ffmpeg() -> void { - std::cout << "Installing FFmpeg via Homebrew..." << std::endl; - int result = std::system("brew install ffmpeg"); - if (result != 0) { - std::cerr << "FFmpeg installation failed!" << std::endl; - exit(result); - } - std::cout << "FFmpeg successfully installed." << std::endl; +auto install_ffmpeg() -> void +{ + std::cout << "Installing FFmpeg via Homebrew..." << std::endl; + int result = std::system("brew install ffmpeg"); + if (result != 0) + { + std::cerr << "FFmpeg installation failed!" << std::endl; + exit(result); + } + std::cout << "FFmpeg successfully installed." << std::endl; } -auto coollab_version_is_installed(std::string_view const &version) -> bool { - return fs::exists(get_PATH() + std::string(version)); +auto coollab_version_is_installed(std::string_view const& version) -> bool +{ + return fs::exists(get_PATH() + std::string(version)); } \ No newline at end of file diff --git a/src/release.cpp b/src/release.cpp index 3baf61c..512adc5 100644 --- a/src/release.cpp +++ b/src/release.cpp @@ -1,39 +1,34 @@ -#include -#include - #include "release.hpp" +#include #include "fmt/core.h" +#include "httplib.h" #include "utils.hpp" -auto get_release(std::string_view const& version) -> tl::expected, std::string> +auto get_release(std::string_view const& version) -> tl::expected { - CURL* curl = curl_easy_init(); - if (!curl) - return tl::make_unexpected("Failed to initialize curl"); + std::string url = fmt::format("https://api.github.com/repos/CoolLibs/Lab/releases/tags/{}", version); - std::string const url = fmt::format("https://api.github.com/repos/CoolLibs/Lab/releases/tags/{}", version); // TODO(Launcher) Don't hardcode the url to the release - curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); - curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); - curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/7.68.0"); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, string_write_callback); - std::string readBuffer; - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); - CURLcode res = curl_easy_perform(curl); - curl_easy_cleanup(curl); + httplib::Client cli("https://api.github.com"); + cli.set_follow_location(true); - if (res != CURLE_OK) - return tl::make_unexpected(fmt::format("Failed to fetch release info: {}", curl_easy_strerror(res))); + auto res = cli.Get(url.c_str()); + + if (!res || res->status != 200) + { + return tl::make_unexpected(fmt::format("Failed to fetch release info: {}", res ? res->status : -1)); + } - // Parse JSON response try { - auto const jsonResponse = json::parse(readBuffer); + auto jsonResponse = nlohmann::json::parse(res->body); if (!jsonResponse.contains("assets") || jsonResponse["assets"].empty()) + { return tl::make_unexpected("No assets found in the release."); - nlohmann::basic_json<> const& assets = jsonResponse["assets"]; + } + nlohmann::json const& assets = jsonResponse["assets"]; return assets; } - catch (json::parse_error const& e) + catch (nlohmann::json::parse_error const& e) { return tl::make_unexpected(fmt::format("JSON parse error: {}", e.what())); } @@ -42,6 +37,7 @@ auto get_release(std::string_view const& version) -> tl::expected const& release) -> std::string { auto os_path = get_OS();