mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2024-11-08 16:57:16 +01:00
4a6bfc086c
Switched to POCO library for unified platform/library interface. Deprecated the external module API. It was creating more problems than solving. Removed most built-in libraries in favor of system libraries for easier maintenance. Cleaned and secured code with help from static analyzers.
55 lines
1.5 KiB
C++
55 lines
1.5 KiB
C++
#include "cpr/curlholder.h"
|
|
#include <cassert>
|
|
|
|
namespace cpr {
|
|
// It does not make sense to make a std::mutex const.
|
|
// NOLINTNEXTLINE (cppcoreguidelines-avoid-non-const-global-variables)
|
|
std::mutex CurlHolder::curl_easy_init_mutex_{};
|
|
|
|
#ifndef _WIN32 // There is no thread sanitizer on windows
|
|
__attribute__((no_sanitize("thread")))
|
|
#endif
|
|
CurlHolder::CurlHolder() {
|
|
/**
|
|
* Allow multithreaded access to CPR by locking curl_easy_init().
|
|
* curl_easy_init() is not thread safe.
|
|
* References:
|
|
* https://curl.haxx.se/libcurl/c/curl_easy_init.html
|
|
* https://curl.haxx.se/libcurl/c/threadsafe.html
|
|
**/
|
|
curl_easy_init_mutex_.lock();
|
|
handle = curl_easy_init();
|
|
curl_easy_init_mutex_.unlock();
|
|
|
|
assert(handle);
|
|
} // namespace cpr
|
|
|
|
CurlHolder::~CurlHolder() {
|
|
curl_easy_cleanup(handle);
|
|
curl_slist_free_all(chunk);
|
|
curl_formfree(formpost);
|
|
}
|
|
|
|
std::string CurlHolder::urlEncode(const std::string& s) const {
|
|
assert(handle);
|
|
char* output = curl_easy_escape(handle, s.c_str(), s.length());
|
|
if (output) {
|
|
std::string result = output;
|
|
curl_free(output);
|
|
return result;
|
|
}
|
|
return "";
|
|
}
|
|
|
|
std::string CurlHolder::urlDecode(const std::string& s) const {
|
|
assert(handle);
|
|
char* output = curl_easy_unescape(handle, s.c_str(), s.length(), nullptr);
|
|
if (output) {
|
|
std::string result = output;
|
|
curl_free(output);
|
|
return result;
|
|
}
|
|
return "";
|
|
}
|
|
} // namespace cpr
|