1#ifndef CPR_CURL_HOLDER_H
2#define CPR_CURL_HOLDER_H
3
4#include <array>
5#include <mutex>
6#include <string>
7
8#include <curl/curl.h>
9
10namespace cpr {
11struct CurlHolder {
12 private:
13 /**
14 * Mutex for curl_easy_init().
15 * curl_easy_init() is not thread save.
16 * References:
17 * https://curl.haxx.se/libcurl/c/curl_easy_init.html
18 * https://curl.haxx.se/libcurl/c/threadsafe.html
19 **/
20
21 // Avoids initalization order problems in a static build
22 static std::mutex& curl_easy_init_mutex_() {
23 static std::mutex curl_easy_init_mutex_;
24 return curl_easy_init_mutex_;
25 }
26
27 public:
28 CURL* handle{nullptr};
29 struct curl_slist* chunk{nullptr};
30 struct curl_slist* resolveCurlList{nullptr};
31 curl_mime* multipart{nullptr};
32 std::array<char, CURL_ERROR_SIZE> error{};
33
34 CurlHolder();
35 CurlHolder(const CurlHolder& other) = default;
36 CurlHolder(CurlHolder&& old) noexcept = default;
37 ~CurlHolder();
38
39 CurlHolder& operator=(CurlHolder&& old) noexcept = default;
40 CurlHolder& operator=(const CurlHolder& other) = default;
41
42 /**
43 * Uses curl_easy_escape(...) for escaping the given string.
44 **/
45 std::string urlEncode(const std::string& s) const;
46
47 /**
48 * Uses curl_easy_unescape(...) for unescaping the given string.
49 **/
50 std::string urlDecode(const std::string& s) const;
51};
52} // namespace cpr
53
54#endif
55