1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <kooling/http/http.h>
#include <kooling/http/http-detail.h>

#include <curl/curl.h>

#include <cstddef>

namespace kooling::http {

std::string url_encode(std::string_view data)
{
    CURL *curl{ curl_easy_init() };
    std::string result;
    if (char *encoded{ curl_easy_escape(curl, data.data(), static_cast<int>(data.length())) })
    {
        result = encoded;
        curl_free(encoded);
    }
    curl_easy_cleanup(curl);
    return result;
}

namespace detail {

struct curl_global_holder
{
    curl_global_holder()
    {
        curl_global_init(CURL_GLOBAL_NOTHING);
    }

    ~curl_global_holder()
    {
        curl_global_cleanup();
    }
};

void initialize_curl()<--- The function 'initialize_curl' is never used.
{
    [[maybe_unused]] static curl_global_holder cgh{};
}

std::size_t write_callback(char* ptr, std::size_t, std::size_t nmemb, void *userdata)
{
    std::string& s{ *static_cast<std::string*>(userdata) };
    s.append(ptr, nmemb);
    return nmemb;
}

} // namespace detail
} // namespace kooling::http