2020-01-04 18:11:24 +01:00
|
|
|
/* 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"
|
2020-01-05 12:22:26 +01:00
|
|
|
#include "log.hpp"
|
2020-01-06 15:33:48 +01:00
|
|
|
#include "version.hpp"
|
2020-01-05 12:22:26 +01:00
|
|
|
|
2020-01-08 14:04:53 +01:00
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
2020-01-08 11:27:19 +01:00
|
|
|
#include <atomic>
|
2020-01-05 12:22:26 +01:00
|
|
|
#include <cstdint>
|
2020-01-04 18:11:24 +01:00
|
|
|
|
|
|
|
namespace mastodonpp
|
|
|
|
{
|
|
|
|
|
2020-01-08 12:56:16 +01:00
|
|
|
using std::get;
|
|
|
|
using std::holds_alternative;
|
2020-01-08 14:04:53 +01:00
|
|
|
using std::any_of;
|
2020-01-14 23:46:42 +01:00
|
|
|
using std::array; // NOLINT(misc-unused-using-decls)
|
2020-01-08 11:27:19 +01:00
|
|
|
using std::atomic;
|
2020-01-05 12:22:26 +01:00
|
|
|
using std::uint8_t;
|
|
|
|
using std::uint16_t;
|
|
|
|
|
2020-01-09 11:23:30 +01:00
|
|
|
// No one will ever need more than 65535 connections. 😉
|
2020-01-08 11:27:19 +01:00
|
|
|
static atomic<uint16_t> curlwrapper_instances{0};
|
2020-01-05 19:07:07 +01:00
|
|
|
|
2020-01-04 18:11:24 +01:00
|
|
|
CURLWrapper::CURLWrapper()
|
|
|
|
: _curl_buffer_error{}
|
2020-01-08 21:27:27 +01:00
|
|
|
, _stream_cancelled(false)
|
2020-01-04 18:11:24 +01:00
|
|
|
{
|
2020-01-05 19:07:07 +01:00
|
|
|
if (curlwrapper_instances == 0)
|
|
|
|
{
|
|
|
|
curl_global_init(CURL_GLOBAL_ALL); // NOLINT(hicpp-signed-bitwise)
|
|
|
|
}
|
|
|
|
++curlwrapper_instances;
|
|
|
|
debuglog << "CURLWrapper instances: " << curlwrapper_instances << " (+1)\n";
|
|
|
|
|
2020-01-04 18:11:24 +01:00
|
|
|
_connection = curl_easy_init();
|
|
|
|
setup_curl();
|
|
|
|
}
|
|
|
|
CURLWrapper::~CURLWrapper() noexcept
|
|
|
|
{
|
|
|
|
curl_easy_cleanup(_connection);
|
2020-01-05 19:07:07 +01:00
|
|
|
|
|
|
|
--curlwrapper_instances;
|
|
|
|
debuglog << "CURLWrapper instances: " << curlwrapper_instances << " (-1)\n";
|
|
|
|
if (curlwrapper_instances == 0)
|
|
|
|
{
|
|
|
|
curl_global_cleanup();
|
|
|
|
}
|
2020-01-04 18:11:24 +01:00
|
|
|
}
|
2020-01-08 17:16:15 +01:00
|
|
|
|
2020-01-08 12:56:16 +01:00
|
|
|
answer_type CURLWrapper::make_request(const http_method &method, string uri,
|
|
|
|
const parametermap ¶meters)
|
2020-01-04 19:04:50 +01:00
|
|
|
{
|
2020-01-08 21:27:27 +01:00
|
|
|
_stream_cancelled = false;
|
2020-01-09 13:13:49 +01:00
|
|
|
_curl_buffer_headers.clear();
|
|
|
|
_curl_buffer_body.clear();
|
2020-01-08 21:27:27 +01:00
|
|
|
|
2020-01-04 19:04:50 +01:00
|
|
|
CURLcode code;
|
2020-01-05 12:21:58 +01:00
|
|
|
switch (method)
|
2020-01-04 19:04:50 +01:00
|
|
|
{
|
|
|
|
case http_method::GET:
|
|
|
|
{
|
2020-01-08 17:51:53 +01:00
|
|
|
add_parameters_to_uri(uri, parameters);
|
2020-01-11 18:06:06 +01:00
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
|
|
|
curl_easy_setopt(_connection, CURLOPT_HTTPGET, 1L);
|
2020-01-08 12:56:16 +01:00
|
|
|
|
2020-01-04 19:04:50 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case http_method::POST:
|
|
|
|
{
|
2020-01-10 14:33:33 +01:00
|
|
|
if (parameters.empty())
|
|
|
|
{
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
2020-01-11 18:06:06 +01:00
|
|
|
curl_easy_setopt(_connection, CURLOPT_POST, 1L);
|
2020-01-10 14:33:33 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
curl_mime *mime{parameters_to_curl_mime(uri, parameters)};
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
2020-01-11 18:06:06 +01:00
|
|
|
curl_easy_setopt(_connection, CURLOPT_MIMEPOST, mime);
|
2020-01-10 14:33:33 +01:00
|
|
|
}
|
2020-01-10 13:08:34 +01:00
|
|
|
|
2020-01-04 19:04:50 +01:00
|
|
|
break;
|
|
|
|
}
|
2020-01-05 13:59:43 +01:00
|
|
|
case http_method::PATCH:
|
|
|
|
{
|
2020-01-11 16:07:40 +01:00
|
|
|
if (!parameters.empty())
|
|
|
|
{
|
|
|
|
curl_mime *mime{parameters_to_curl_mime(uri, parameters)};
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
2020-01-11 18:06:06 +01:00
|
|
|
curl_easy_setopt(_connection, CURLOPT_MIMEPOST, mime);
|
2020-01-11 16:07:40 +01:00
|
|
|
}
|
|
|
|
|
2020-01-05 13:59:43 +01:00
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
|
|
|
code = curl_easy_setopt(_connection, CURLOPT_CUSTOMREQUEST, "PATCH");
|
2020-01-11 18:06:06 +01:00
|
|
|
if (code != CURLE_OK)
|
|
|
|
{
|
|
|
|
throw CURLException{code, "Failed to set URI", _curl_buffer_error};
|
|
|
|
}
|
2020-01-11 16:07:40 +01:00
|
|
|
|
2020-01-05 13:59:43 +01:00
|
|
|
break;
|
|
|
|
}
|
2020-01-05 16:16:22 +01:00
|
|
|
case http_method::PUT:
|
|
|
|
{
|
2020-01-11 16:07:40 +01:00
|
|
|
if (!parameters.empty())
|
|
|
|
{
|
|
|
|
curl_mime *mime{parameters_to_curl_mime(uri, parameters)};
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
2020-01-11 18:06:06 +01:00
|
|
|
curl_easy_setopt(_connection, CURLOPT_MIMEPOST, mime);
|
2020-01-11 16:07:40 +01:00
|
|
|
}
|
|
|
|
|
2020-01-05 16:16:22 +01:00
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
2020-01-11 16:07:40 +01:00
|
|
|
code = curl_easy_setopt(_connection, CURLOPT_CUSTOMREQUEST, "PUT");
|
2020-01-11 18:06:06 +01:00
|
|
|
if (code != CURLE_OK)
|
|
|
|
{
|
|
|
|
throw CURLException{code, "Failed to set URI", _curl_buffer_error};
|
|
|
|
}
|
2020-01-11 16:07:40 +01:00
|
|
|
|
2020-01-05 16:16:22 +01:00
|
|
|
break;
|
|
|
|
}
|
2020-01-05 13:59:43 +01:00
|
|
|
case http_method::DELETE:
|
|
|
|
{
|
2020-01-11 16:23:28 +01:00
|
|
|
if (!parameters.empty())
|
|
|
|
{
|
|
|
|
curl_mime *mime{parameters_to_curl_mime(uri, parameters)};
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
2020-01-11 18:06:06 +01:00
|
|
|
curl_easy_setopt(_connection, CURLOPT_MIMEPOST, mime);
|
2020-01-11 16:23:28 +01:00
|
|
|
}
|
|
|
|
|
2020-01-05 13:59:43 +01:00
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
|
|
|
code = curl_easy_setopt(_connection, CURLOPT_CUSTOMREQUEST, "DELETE");
|
2020-01-11 18:06:06 +01:00
|
|
|
if (code != CURLE_OK)
|
|
|
|
{
|
|
|
|
throw CURLException{code, "Failed to set URI", _curl_buffer_error};
|
|
|
|
}
|
2020-01-11 16:23:28 +01:00
|
|
|
|
2020-01-05 13:59:43 +01:00
|
|
|
break;
|
|
|
|
}
|
2020-01-04 19:04:50 +01:00
|
|
|
}
|
2020-01-08 12:56:16 +01:00
|
|
|
debuglog << "Making request to: " << uri << '\n';
|
2020-01-04 19:04:50 +01:00
|
|
|
|
|
|
|
// 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};
|
|
|
|
}
|
|
|
|
|
2020-01-05 12:22:26 +01:00
|
|
|
answer_type answer;
|
2020-01-04 19:04:50 +01:00
|
|
|
code = curl_easy_perform(_connection);
|
2020-01-08 21:49:48 +01:00
|
|
|
if (code == CURLE_OK
|
|
|
|
|| (code == CURLE_ABORTED_BY_CALLBACK && _stream_cancelled))
|
2020-01-04 19:04:50 +01:00
|
|
|
{
|
2020-01-05 12:22:26 +01:00
|
|
|
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';
|
2020-01-04 19:04:50 +01:00
|
|
|
}
|
|
|
|
|
2020-01-05 10:35:38 +01:00
|
|
|
return answer;
|
2020-01-04 19:04:50 +01:00
|
|
|
}
|
|
|
|
|
2020-01-12 17:35:11 +01:00
|
|
|
void CURLWrapper::setup_connection_properties(const string_view proxy,
|
|
|
|
const string_view access_token,
|
|
|
|
const string_view cainfo,
|
|
|
|
const string_view useragent)
|
2020-01-12 17:21:58 +01:00
|
|
|
{
|
|
|
|
if (!proxy.empty())
|
|
|
|
{
|
|
|
|
set_proxy(proxy);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!access_token.empty())
|
|
|
|
{
|
|
|
|
set_access_token(access_token);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cainfo.empty())
|
|
|
|
{
|
|
|
|
set_cainfo(cainfo);
|
|
|
|
}
|
2020-01-12 17:35:11 +01:00
|
|
|
|
|
|
|
if (!useragent.empty())
|
|
|
|
{
|
|
|
|
set_useragent(useragent);
|
|
|
|
}
|
2020-01-12 17:21:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CURLWrapper::set_proxy(const string_view proxy)
|
|
|
|
{
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
|
|
|
CURLcode code{curl_easy_setopt(_connection, CURLOPT_PROXY, proxy.data())};
|
|
|
|
if (code != CURLE_OK)
|
|
|
|
{
|
|
|
|
throw CURLException{code, "Failed to set proxy", _curl_buffer_error};
|
|
|
|
}
|
2020-01-16 18:17:29 +01:00
|
|
|
debuglog << "Set proxy to: " << proxy << '\n';
|
2020-01-12 17:21:58 +01:00
|
|
|
}
|
|
|
|
|
2020-01-10 14:26:50 +01:00
|
|
|
void CURLWrapper::set_access_token(const string_view access_token)
|
|
|
|
{
|
2020-01-10 14:35:44 +01:00
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-signed-bitwise)
|
2020-01-10 14:26:50 +01:00
|
|
|
CURLcode code{curl_easy_setopt(_connection, CURLOPT_XOAUTH2_BEARER,
|
|
|
|
access_token.data())};
|
|
|
|
if (code != CURLE_OK)
|
|
|
|
{
|
|
|
|
throw CURLException{code, "Could not set authorization token.",
|
|
|
|
_curl_buffer_error};
|
|
|
|
}
|
|
|
|
|
2020-01-10 17:43:50 +01:00
|
|
|
#if (LIBCURL_VERSION_NUM < 0x073d00) // libcurl < 7.61.0.
|
|
|
|
#define CURLAUTH_BEARER CURLAUTH_ANY
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-signed-bitwise)
|
|
|
|
code = curl_easy_setopt(_connection, CURLOPT_HTTPAUTH, CURLAUTH_BEARER);
|
2020-01-10 14:26:50 +01:00
|
|
|
if (code != CURLE_OK)
|
|
|
|
{
|
|
|
|
throw CURLException{code, "Could not set authorization token.",
|
|
|
|
_curl_buffer_error};
|
|
|
|
}
|
|
|
|
|
|
|
|
debuglog << "Set authorization token.\n";
|
|
|
|
}
|
|
|
|
|
2020-01-12 17:27:40 +01:00
|
|
|
void CURLWrapper::set_cainfo(const string_view path)
|
2020-01-12 13:37:53 +01:00
|
|
|
{
|
2020-01-14 23:46:42 +01:00
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
2020-01-12 13:37:53 +01:00
|
|
|
CURLcode code{curl_easy_setopt(_connection, CURLOPT_CAINFO, path.data())};
|
|
|
|
if (code != CURLE_OK)
|
|
|
|
{
|
|
|
|
throw CURLException{code, "Could not set CA info.", _curl_buffer_error};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-12 17:35:11 +01:00
|
|
|
void CURLWrapper::set_useragent(const string_view useragent)
|
|
|
|
{
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
|
|
|
CURLcode code{curl_easy_setopt(_connection, CURLOPT_USERAGENT,
|
|
|
|
useragent.data())};
|
|
|
|
if (code != CURLE_OK)
|
|
|
|
{
|
|
|
|
throw CURLException{code, "Failed to set User-Agent",
|
|
|
|
_curl_buffer_error};
|
|
|
|
}
|
|
|
|
debuglog << "Set User-Agent to: " << useragent << '\n';
|
|
|
|
}
|
|
|
|
|
2020-01-08 19:55:34 +01:00
|
|
|
size_t CURLWrapper::writer_body(char *data, size_t size, size_t nmemb)
|
2020-01-04 18:11:24 +01:00
|
|
|
{
|
2020-01-08 19:55:34 +01:00
|
|
|
if(data == nullptr)
|
2020-01-04 18:11:24 +01:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-08 21:27:27 +01:00
|
|
|
buffer_mutex.lock();
|
2020-01-08 19:55:34 +01:00
|
|
|
_curl_buffer_body.append(data, size * nmemb);
|
2020-01-08 21:27:27 +01:00
|
|
|
buffer_mutex.unlock();
|
2020-01-04 18:11:24 +01:00
|
|
|
|
2020-01-08 19:55:34 +01:00
|
|
|
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;
|
2020-01-04 18:11:24 +01:00
|
|
|
}
|
|
|
|
|
2020-01-08 21:27:27 +01:00
|
|
|
int CURLWrapper::progress(void *, curl_off_t , curl_off_t ,
|
|
|
|
curl_off_t , curl_off_t )
|
|
|
|
{
|
|
|
|
if (_stream_cancelled)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-04 18:11:24 +01:00
|
|
|
void CURLWrapper::setup_curl()
|
|
|
|
{
|
|
|
|
if (_connection == nullptr)
|
|
|
|
{
|
|
|
|
throw CURLException{CURLE_FAILED_INIT, "Failed to initialize curl."};
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
2020-01-08 21:34:57 +01:00
|
|
|
curl_easy_setopt(_connection, CURLOPT_ERRORBUFFER, _curl_buffer_error);
|
2020-01-04 18:11:24 +01:00
|
|
|
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
2020-01-08 21:34:57 +01:00
|
|
|
curl_easy_setopt(_connection, CURLOPT_WRITEFUNCTION, writer_body_wrapper);
|
2020-01-04 18:11:24 +01:00
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
2020-01-08 21:34:57 +01:00
|
|
|
curl_easy_setopt(_connection, CURLOPT_WRITEDATA, this);
|
2020-01-05 12:03:23 +01:00
|
|
|
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
2020-01-08 21:34:57 +01:00
|
|
|
curl_easy_setopt(_connection, CURLOPT_HEADERFUNCTION,
|
|
|
|
writer_header_wrapper);
|
2020-01-08 19:55:34 +01:00
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
2020-01-08 21:34:57 +01:00
|
|
|
curl_easy_setopt(_connection, CURLOPT_HEADERDATA, this);
|
2020-01-06 15:33:48 +01:00
|
|
|
|
2020-01-08 21:27:27 +01:00
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
2020-01-08 21:34:57 +01:00
|
|
|
curl_easy_setopt(_connection, CURLOPT_XFERINFOFUNCTION, progress_wrapper);
|
2020-01-08 21:27:27 +01:00
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
|
|
|
curl_easy_setopt(_connection, CURLOPT_XFERINFODATA, this);
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
|
|
|
curl_easy_setopt(_connection, CURLOPT_NOPROGRESS, 0L);
|
|
|
|
|
2020-01-12 17:35:11 +01:00
|
|
|
set_useragent((string("mastodonpp/") += version));
|
2020-01-08 11:53:01 +01:00
|
|
|
|
|
|
|
// The next 2 only fail if HTTP is not supported.
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
2020-01-12 17:35:11 +01:00
|
|
|
CURLcode code{curl_easy_setopt(_connection, CURLOPT_FOLLOWLOCATION, 1L)};
|
2020-01-08 11:53:01 +01:00
|
|
|
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);
|
2020-01-04 18:11:24 +01:00
|
|
|
}
|
|
|
|
|
2020-01-10 13:08:34 +01:00
|
|
|
bool CURLWrapper::replace_parameter_in_uri(string &uri,
|
|
|
|
const parameterpair ¶meter)
|
|
|
|
{
|
|
|
|
static constexpr array replace
|
|
|
|
{
|
2020-01-14 22:50:14 +01:00
|
|
|
"id", "nickname", "nickname_or_id", "account_id",
|
|
|
|
"list_id", "hashtag", "permission_group"
|
2020-01-10 13:08:34 +01:00
|
|
|
};
|
|
|
|
if (any_of(replace.begin(), replace.end(),
|
|
|
|
[¶meter](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));
|
2020-01-11 16:42:01 +01:00
|
|
|
debuglog << "Replaced :" << parameter.first << " in URI with "
|
|
|
|
<< get<string_view>(parameter.second) << '\n';
|
2020-01-10 13:08:34 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-01-08 17:51:53 +01:00
|
|
|
void CURLWrapper::add_parameters_to_uri(string &uri,
|
|
|
|
const parametermap ¶meters)
|
|
|
|
{
|
|
|
|
// Replace <ID> with the value of parameter “id” and so on.
|
|
|
|
for (const auto ¶m : parameters)
|
|
|
|
{
|
2020-01-10 13:08:34 +01:00
|
|
|
if (replace_parameter_in_uri(uri, param))
|
2020-01-08 17:51:53 +01:00
|
|
|
{
|
2020-01-10 13:08:34 +01:00
|
|
|
continue;
|
2020-01-08 17:51:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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 += "&";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-11 13:17:35 +01:00
|
|
|
void CURLWrapper::add_mime_part(curl_mime *mime,
|
|
|
|
string_view name, string_view data) const
|
|
|
|
{
|
|
|
|
curl_mimepart *part{curl_mime_addpart(mime)};
|
|
|
|
if (part == nullptr)
|
|
|
|
{
|
|
|
|
throw CURLException{"Could not build HTTP form."};
|
|
|
|
}
|
|
|
|
|
|
|
|
CURLcode code{curl_mime_name(part, name.data())};
|
|
|
|
if (code != CURLE_OK)
|
|
|
|
{
|
|
|
|
throw CURLException{code, "Could not build HTTP form."};
|
|
|
|
}
|
|
|
|
|
2020-01-11 14:27:10 +01:00
|
|
|
if (data.substr(0, 6) == "@file:")
|
|
|
|
{
|
|
|
|
const string_view filename{data.substr(6)};
|
|
|
|
code = curl_mime_filedata(part, filename.data());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
code = curl_mime_data(part, data.data(), CURL_ZERO_TERMINATED);
|
|
|
|
}
|
2020-01-11 13:17:35 +01:00
|
|
|
if (code != CURLE_OK)
|
|
|
|
{
|
|
|
|
throw CURLException{code, "Could not build HTTP form."};
|
|
|
|
}
|
|
|
|
|
|
|
|
debuglog << "Set form part: " << name << " = " << data << '\n';
|
|
|
|
}
|
|
|
|
|
2020-01-10 13:08:34 +01:00
|
|
|
curl_mime *CURLWrapper::parameters_to_curl_mime(string &uri,
|
|
|
|
const parametermap ¶meters)
|
|
|
|
{
|
|
|
|
debuglog << "Building HTTP form.\n";
|
|
|
|
|
|
|
|
curl_mime *mime{curl_mime_init(_connection)};
|
2020-01-11 13:17:35 +01:00
|
|
|
|
2020-01-10 13:08:34 +01:00
|
|
|
for (const auto ¶m : parameters)
|
|
|
|
{
|
|
|
|
if (replace_parameter_in_uri(uri, param))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (holds_alternative<string_view>(param.second))
|
|
|
|
{
|
2020-01-11 13:17:35 +01:00
|
|
|
add_mime_part(mime, param.first, get<string_view>(param.second));
|
2020-01-10 13:08:34 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (const auto &arg : get<vector<string_view>>(param.second))
|
|
|
|
{
|
2020-01-11 13:17:35 +01:00
|
|
|
const string_view name{string(param.first) += "[]"};
|
|
|
|
add_mime_part(mime, name, arg);
|
2020-01-10 13:08:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mime;
|
|
|
|
}
|
|
|
|
|
2020-01-04 18:11:24 +01:00
|
|
|
} // namespace mastodonpp
|