summaryrefslogtreecommitdiffstats
path: root/src/curl_wrapper.cpp
blob: ac534bbd13ba386cbf0190927b8f7455c4deb1f9 (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*  This file is part of mastodonpp.
 *  Copyright © 2020 tastytea <tastytea@tastytea.de>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, version 3.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "curl_wrapper.hpp"
#include "exceptions.hpp"

namespace mastodonpp
{

CURLWrapper::CURLWrapper()
    : _curl_buffer_error{}
{
    curl_global_init(CURL_GLOBAL_ALL); // NOLINT(hicpp-signed-bitwise)
    _connection = curl_easy_init();
    setup_curl();
}
CURLWrapper::~CURLWrapper() noexcept
{
    curl_easy_cleanup(_connection);
    curl_global_cleanup();
}

answer_type CURLWrapper::make_request(const http_method &meth,
                                 const string_view &uri)
{
    CURLcode code;
    switch (meth)
    {
    case http_method::GET:
    {
        // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
        code = curl_easy_setopt(_connection, CURLOPT_HTTPGET, 1L);
        break;
    }
    case http_method::POST:
    {
        // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
        code = curl_easy_setopt(_connection, CURLOPT_POST, 1L);
        break;
    }
    }
    if (code != CURLE_OK)
    {
        throw CURLException{code, "Failed to set HTTP method",
                _curl_buffer_error};
    }

    // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
    code = curl_easy_setopt(_connection, CURLOPT_URL, uri.data());
    if (code != CURLE_OK)
    {
        throw CURLException{code, "Failed to set URI", _curl_buffer_error};
    }

    code = curl_easy_perform(_connection);
    if (code != CURLE_OK)
    {
        throw CURLException{code, "Failed to perform request",
                _curl_buffer_error};
    }

    answer_type answer;
    answer.body = _curl_buffer;
    return answer;
}

int CURLWrapper::writer(char *data, size_t size, size_t nmemb,
                        string *writerData)
{
    if(writerData == nullptr)
    {
        return 0;
    }

    writerData->append(data, size*nmemb);

    return static_cast<int>(size * nmemb);
}

void CURLWrapper::setup_curl()
{
    if (_connection == nullptr)
    {
        throw CURLException{CURLE_FAILED_INIT, "Failed to initialize curl."};
    }

    // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
    CURLcode code{curl_easy_setopt(_connection, CURLOPT_ERRORBUFFER,
                                   _curl_buffer_error)};
    if (code != CURLE_OK)
    {
        throw CURLException{code, "Failed to set error buffer."};
    }

    // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
    code = curl_easy_setopt(_connection, CURLOPT_WRITEFUNCTION, writer);
    if (code != CURLE_OK)
    {
        throw CURLException{code, "Failed to set writer", _curl_buffer_error};
    }

    // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
    code = curl_easy_setopt(_connection, CURLOPT_WRITEDATA, &_curl_buffer);
    if (code != CURLE_OK)
    {
        throw CURLException{code, "Failed to set write data",
                _curl_buffer_error};
    }
}

} // namespace mastodonpp