fix curl requirement
parent
6cdfab39cf
commit
90cf177c57
|
@ -1,6 +1,6 @@
|
|||
cmake_minimum_required(VERSION 3.20)
|
||||
include(cmake/color.cmake)
|
||||
set(BLT_VERSION 5.3.3)
|
||||
set(BLT_VERSION 5.3.4)
|
||||
|
||||
set(BLT_TARGET BLT)
|
||||
|
||||
|
|
|
@ -29,62 +29,63 @@
|
|||
|
||||
namespace blt::requests
|
||||
{
|
||||
#ifdef BLT_HAS_CURL
|
||||
struct curl_init_t
|
||||
{
|
||||
curl_init_t()
|
||||
{
|
||||
const auto version_data = curl_version_info(CURLVERSION_NOW);
|
||||
if (!(version_data->features & CURL_VERSION_THREADSAFE))
|
||||
{
|
||||
thread_safe = false;
|
||||
}
|
||||
curl_global_init(CURL_GLOBAL_ALL);
|
||||
}
|
||||
#ifdef BLT_HAS_CURL
|
||||
struct curl_init_t
|
||||
{
|
||||
curl_init_t()
|
||||
{
|
||||
const auto version_data = curl_version_info(CURLVERSION_NOW);
|
||||
if (!(version_data->features & CURL_VERSION_THREADSAFE))
|
||||
{
|
||||
thread_safe = false;
|
||||
}
|
||||
curl_global_init(CURL_GLOBAL_ALL);
|
||||
}
|
||||
|
||||
~curl_init_t()
|
||||
{
|
||||
curl_global_cleanup();
|
||||
}
|
||||
~curl_init_t()
|
||||
{
|
||||
curl_global_cleanup();
|
||||
}
|
||||
|
||||
bool thread_safe = true;
|
||||
};
|
||||
bool thread_safe = true;
|
||||
};
|
||||
|
||||
struct curl_easy_init_t
|
||||
{
|
||||
curl_easy_init_t(): curl(curl_easy_init())
|
||||
{
|
||||
}
|
||||
struct curl_easy_init_t
|
||||
{
|
||||
curl_easy_init_t(): curl(curl_easy_init())
|
||||
{
|
||||
}
|
||||
|
||||
~curl_easy_init_t()
|
||||
{
|
||||
curl_easy_cleanup(curl);
|
||||
}
|
||||
~curl_easy_init_t()
|
||||
{
|
||||
curl_easy_cleanup(curl);
|
||||
}
|
||||
|
||||
CURL* curl;
|
||||
};
|
||||
CURL* curl;
|
||||
};
|
||||
|
||||
void init()
|
||||
{
|
||||
static curl_init_t curl_init_obj;
|
||||
}
|
||||
void init()
|
||||
{
|
||||
static curl_init_t curl_init_obj;
|
||||
}
|
||||
|
||||
CURL* easy_init()
|
||||
{
|
||||
thread_local curl_easy_init_t curl_easy_init_obj;
|
||||
return curl_easy_init_obj.curl;
|
||||
}
|
||||
CURL* easy_init()
|
||||
{
|
||||
thread_local curl_easy_init_t curl_easy_init_obj;
|
||||
return curl_easy_init_obj.curl;
|
||||
}
|
||||
|
||||
size_t write_to_string_func(const void* data, const size_t size, const size_t nmemb, void* user_data) {
|
||||
auto& str = *static_cast<std::string*>(user_data);
|
||||
str.append(static_cast<const char*>(data), size * nmemb);
|
||||
return size * nmemb;
|
||||
};
|
||||
#endif
|
||||
size_t write_to_string_func(const void* data, const size_t size, const size_t nmemb, void* user_data)
|
||||
{
|
||||
auto& str = *static_cast<std::string*>(user_data);
|
||||
str.append(static_cast<const char*>(data), size * nmemb);
|
||||
return size * nmemb;
|
||||
};
|
||||
#endif
|
||||
|
||||
std::string send_get_request(const std::string& url)
|
||||
{
|
||||
#ifdef __EMSCRIPTEN__
|
||||
std::string send_get_request(const std::string& url)
|
||||
{
|
||||
#ifdef __EMSCRIPTEN__
|
||||
auto* str = static_cast<char*>(EM_ASM_PTR({
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("GET", $0);
|
||||
|
@ -94,23 +95,27 @@ namespace blt::requests
|
|||
std::string str_obj{str};
|
||||
free(str);
|
||||
return str_obj;
|
||||
#else
|
||||
init();
|
||||
auto curl = easy_init();
|
||||
if (!curl)
|
||||
throw std::runtime_error("Failed to initialize curl");
|
||||
std::string response_string;
|
||||
#else
|
||||
#ifdef BLT_HAS_CURL
|
||||
init();
|
||||
auto curl = easy_init();
|
||||
if (!curl)
|
||||
throw std::runtime_error("Failed to initialize curl");
|
||||
std::string response_string;
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, static_cast<void*>(&response_string));
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_to_string_func);
|
||||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, static_cast<void*>(&response_string));
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_to_string_func);
|
||||
|
||||
const auto res = curl_easy_perform(curl);
|
||||
if(res != CURLE_OK)
|
||||
throw std::runtime_error(curl_easy_strerror(res));
|
||||
const auto res = curl_easy_perform(curl);
|
||||
if (res != CURLE_OK)
|
||||
throw std::runtime_error(curl_easy_strerror(res));
|
||||
|
||||
return response_string;
|
||||
#endif
|
||||
}
|
||||
return response_string;
|
||||
#else
|
||||
return "Missing cURL! Unable to fetch URL: '" + url + "'";
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue