summaryrefslogtreecommitdiffstats
path: root/src/curl_wrapper.cpp
blob: 6746fe15bf4007151119387d2c148c91f0bd8061 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*  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"
#include "log.hpp"
#include "version.hpp"

#include <algorithm>
#include <array>
#include <atomic>
#include <cstdint>
#include <cstring>

namespace mastodonpp
{

using std::get;
using std::holds_alternative;
using std::any_of;
using std::array;
using std::atomic;
using std::uint8_t;
using std::uint16_t;

// No one will ever need more than 65535 connections. 😉
static atomic<uint16_t> curlwrapper_instances{0};

CURLWrapper::CURLWrapper()
    : _curl_buffer_error{}
    , _stream_cancelled(false)
{
    if (curlwrapper_instances == 0)
    {
        curl_global_init(CURL_GLOBAL_ALL); // NOLINT(hicpp-signed-bitwise)
    }
    ++curlwrapper_instances;
    debuglog << "CURLWrapper instances: " << curlwrapper_instances << " (+1)\n";

    _connection = curl_easy_init();
    setup_curl();
}
CURLWrapper::~CURLWrapper() noexcept
{
    curl_easy_cleanup(_connection);

    --curlwrapper_instances;
    debuglog << "CURLWrapper instances: " << curlwrapper_instances << " (-1)\n";
    if (curlwrapper_instances == 0)
    {
        curl_global_cleanup();
    }
}

void CURLWrapper::set_proxy(const string_view proxy)
{
    // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
    CURLcode code{curl_easy_setopt(_connection, CURLOPT_PROXY, proxy)};
    if (code != CURLE_OK)
    {
        throw CURLException{code, "Failed to set proxy", _curl_buffer_error};
    }
}

void CURLWrapper::cancel_stream()
{
    _stream_cancelled = true;
}

answer_type CURLWrapper::make_request(const http_method &method, string uri,
                                      const parametermap &parameters)
{
    _stream_cancelled = false;
    _curl_buffer_headers.clear();
    _curl_buffer_body.clear();

    CURLcode code;
    switch (method)
    {
    case http_method::GET:
    {
        // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
        code = curl_easy_setopt(_connection, CURLOPT_HTTPGET, 1L);

        add_parameters_to_uri(uri, parameters);

        break;
    }
    case http_method::POST:
    {
        curl_mime *mime{parameters_to_curl_mime(uri, parameters)};

        // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
        code = curl_easy_setopt(_connection, CURLOPT_MIMEPOST, mime);

        break;
    }
    case http_method::PATCH:
    {
        // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
        code = curl_easy_setopt(_connection, CURLOPT_CUSTOMREQUEST, "PATCH");
        break;
    }
    case http_method::PUT:
    {
        // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
        code = curl_easy_setopt(_connection, CURLOPT_UPLOAD, 1L);
        break;
    }
    case http_method::DELETE:
    {
        // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
        code = curl_easy_setopt(_connection, CURLOPT_CUSTOMREQUEST, "DELETE");
        break;
    }
    }
    if (code != CURLE_OK)
    {
        throw CURLException{code, "Failed to set HTTP method",
                _curl_buffer_error};
    }
    debuglog << "Making request to: " << uri << '\n';

    // 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};
    }

    answer_type answer;
    code = curl_easy_perform(_connection);
    if (code == CURLE_OK
        || (code == CURLE_ABORTED_BY_CALLBACK && _stream_cancelled))
    {
        long http_status;       // NOLINT(google-runtime-int)
        // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
        curl_easy_getinfo(_connection, CURLINFO_RESPONSE_CODE, &http_status);
        answer.http_status = static_cast<uint16_t>(http_status);
        debuglog << "HTTP status code: " << http_status << '\n';

        answer.headers = _curl_buffer_headers;
        answer.body = _curl_buffer_body;
    }
    else
    {
        answer.curl_error_code = static_cast<uint8_t>(code);
        answer.error_message = _curl_buffer_error;
        debuglog << "libcurl error: " << code << '\n';
        debuglog << _curl_buffer_error << '\n';
    }

    return answer;
}

size_t CURLWrapper::writer_body(char *data, size_t size, size_t nmemb)
{
    if(data == nullptr)
    {
        return 0;
    }

    buffer_mutex.lock();
    _curl_buffer_body.append(data, size * nmemb);
    buffer_mutex.unlock();

    return size * nmemb;
}

size_t CURLWrapper::writer_header(char *data, size_t size, size_t nmemb)
{
    if(data == nullptr)
    {
        return 0;
    }

    _curl_buffer_headers.append(data, size * nmemb);

    return size * nmemb;
}

int CURLWrapper::progress(void *, curl_off_t , curl_off_t ,
                          curl_off_t , curl_off_t )
{
    if (_stream_cancelled)
    {
        return 1;
    }
    return 0;
}

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

    // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
    curl_easy_setopt(_connection, CURLOPT_ERRORBUFFER, _curl_buffer_error);

    // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
    curl_easy_setopt(_connection, CURLOPT_WRITEFUNCTION, writer_body_wrapper);
    // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
    curl_easy_setopt(_connection, CURLOPT_WRITEDATA, this);

    // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
    curl_easy_setopt(_connection, CURLOPT_HEADERFUNCTION,
                     writer_header_wrapper);
    // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
    curl_easy_setopt(_connection, CURLOPT_HEADERDATA, this);

    // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
    curl_easy_setopt(_connection, CURLOPT_XFERINFOFUNCTION, progress_wrapper);
    // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
    curl_easy_setopt(_connection, CURLOPT_XFERINFODATA, this);
    // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
    curl_easy_setopt(_connection, CURLOPT_NOPROGRESS, 0L);

    // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
    CURLcode code{curl_easy_setopt(_connection, CURLOPT_USERAGENT,
                                   (string("mastorss/") += version).c_str())};
    if (code != CURLE_OK)
    {
        throw CURLException{code, "Failed to set User-Agent",
                _curl_buffer_error};
    }

    // The next 2 only fail if HTTP is not supported.
    // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
    code = curl_easy_setopt(_connection, CURLOPT_FOLLOWLOCATION, 1L);
    if (code != CURLE_OK)
    {
        throw CURLException{code, "HTTP is not supported.", _curl_buffer_error};
    }
    // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
    curl_easy_setopt(_connection, CURLOPT_MAXREDIRS, 10L);
}

bool CURLWrapper::replace_parameter_in_uri(string &uri,
                                           const parameterpair &parameter)
{
    static constexpr array replace
        {
            "id", "nickname", "nickname_or_id",
            "hashtag", "permission_group"
        };
    if (any_of(replace.begin(), replace.end(),
               [&parameter](const auto &s) { return s == parameter.first; }))
    {
        const auto pos{uri.find('<')};
        if (pos != string::npos)
        {
            uri.replace(pos, parameter.first.size() + 2,
                        get<string_view>(parameter.second));
            return true;
        }
    }

    return false;
}

void CURLWrapper::add_parameters_to_uri(string &uri,
                                        const parametermap &parameters)
{
    // Replace <ID> with the value of parameter “id” and so on.
    for (const auto &param : parameters)
    {
        if (replace_parameter_in_uri(uri, param))
        {
            continue;
        }

        static bool first{true};
        if (first)
        {
            uri += "?";
            first = false;
        }
        else
        {
            uri += "&";
        }
        if (holds_alternative<string_view>(param.second))
        {
            ((uri += param.first) += "=") += get<string_view>(param.second);
        }
        else
        {
            for (const auto &arg : get<vector<string_view>>(param.second))
            {
                ((uri += param.first) += "[]=") += arg;
                if (arg != *get<vector<string_view>>(param.second).rbegin())
                {
                    uri += "&";
                }
            }
        }
    }
}

curl_mime *CURLWrapper::parameters_to_curl_mime(string &uri,
                                                const parametermap &parameters)
{
    debuglog << "Building HTTP form.\n";

    curl_mime *mime{curl_mime_init(_connection)};
    for (const auto &param : parameters)
    {
        if (replace_parameter_in_uri(uri, param))
        {
            continue;
        }

        curl_mimepart *part{curl_mime_addpart(mime)};
        if (part == nullptr)
        {
            throw CURLException{"Could not build HTTP form."};
        }

        CURLcode code;
        if (holds_alternative<string_view>(param.second))
        {
            code = curl_mime_name(part, param.first.data());
            if (code != CURLE_OK)
            {
                throw CURLException{code, "Could not build HTTP form."};
            }

            code = curl_mime_data(part, get<string_view>(param.second).data(),
                                  CURL_ZERO_TERMINATED);
            if (code != CURLE_OK)
            {
                throw CURLException{code, "Could not build HTTP form."};
            }
            debuglog << "Set form part: " << param.first << " = "
                     << get<string_view>(param.second) << '\n';
        }
        else
        {
            for (const auto &arg : get<vector<string_view>>(param.second))
            {
                const string name{string(param.first) += "[]"};
                code = curl_mime_name(part, name.c_str());
                if (code != CURLE_OK)
                {
                    throw CURLException{code, "Could not build HTTP form."};
                }
                code = curl_mime_data(part, arg.data(), CURL_ZERO_TERMINATED);
                if (code != CURLE_OK)
                {
                    throw CURLException{code, "Could not build HTTP form."};
                }
                debuglog << "Set form part: " << name << " = " << arg << '\n';
            }
        }
    }

    return mime;
}

} // namespace mastodonpp