2019-04-20 04:47:53 +02:00
|
|
|
/* This file is part of gitea2rss.
|
2020-10-24 16:59:41 +02:00
|
|
|
* Copyright © 2019, 2020 tastytea <tastytea@tastytea.de>
|
2019-04-20 04:47:53 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gitea2rss.hpp"
|
2020-10-24 16:59:41 +02:00
|
|
|
#include "version.hpp"
|
|
|
|
#include <curl/curl.h>
|
|
|
|
#include <exception>
|
|
|
|
#include <iostream>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
2019-04-20 04:47:53 +02:00
|
|
|
|
2020-10-27 12:05:50 +01:00
|
|
|
namespace gitea2rss
|
|
|
|
{
|
|
|
|
|
2019-04-20 04:47:53 +02:00
|
|
|
using std::cerr;
|
2020-10-24 16:59:41 +02:00
|
|
|
using std::cout;
|
2019-04-20 04:47:53 +02:00
|
|
|
using std::endl;
|
2020-10-24 16:59:41 +02:00
|
|
|
using std::runtime_error;
|
|
|
|
using std::to_string;
|
|
|
|
|
|
|
|
string buffer_body;
|
|
|
|
|
|
|
|
string get_http(const string &url)
|
2019-04-20 04:47:53 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-10-24 16:59:41 +02:00
|
|
|
curl_global_init(CURL_GLOBAL_ALL); // NOLINT(hicpp-signed-bitwise)
|
|
|
|
CURL *connection{curl_easy_init()};
|
|
|
|
if (connection == nullptr)
|
2019-08-09 22:38:28 +02:00
|
|
|
{
|
2020-10-24 16:59:41 +02:00
|
|
|
throw runtime_error{"Failed to initialize curl."};
|
|
|
|
}
|
2019-08-25 05:05:36 +02:00
|
|
|
|
2020-10-24 16:59:41 +02:00
|
|
|
char buffer_error[256];
|
2019-08-09 22:38:28 +02:00
|
|
|
|
2020-10-24 16:59:41 +02:00
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
|
|
|
curl_easy_setopt(connection, CURLOPT_ERRORBUFFER, buffer_error);
|
2019-08-09 22:38:28 +02:00
|
|
|
|
2020-10-24 16:59:41 +02:00
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
|
|
|
curl_easy_setopt(connection, CURLOPT_WRITEFUNCTION, writer_body);
|
|
|
|
|
2020-10-27 12:05:50 +01:00
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
2020-10-24 16:59:41 +02:00
|
|
|
CURLcode code{curl_easy_setopt(connection, CURLOPT_FOLLOWLOCATION, 1L)};
|
|
|
|
if (code != CURLE_OK)
|
2019-08-09 22:38:28 +02:00
|
|
|
{
|
2020-10-24 16:59:41 +02:00
|
|
|
throw runtime_error{"HTTP is not supported."};
|
2019-08-09 22:38:28 +02:00
|
|
|
}
|
2020-10-24 16:59:41 +02:00
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
|
|
|
curl_easy_setopt(connection, CURLOPT_MAXREDIRS, 5L);
|
2019-04-20 10:00:15 +02:00
|
|
|
|
2020-10-27 12:05:50 +01:00
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
|
|
|
code = curl_easy_setopt(connection, CURLOPT_USERAGENT,
|
|
|
|
(string("gitea2rss/") += version).c_str());
|
2020-10-24 16:59:41 +02:00
|
|
|
if (code != CURLE_OK)
|
2019-08-09 22:38:28 +02:00
|
|
|
{
|
2020-10-24 16:59:41 +02:00
|
|
|
throw runtime_error{"Failed to set User-Agent."};
|
2019-08-09 22:38:28 +02:00
|
|
|
}
|
2020-10-24 16:59:41 +02:00
|
|
|
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
|
|
|
curl_easy_setopt(connection, CURLOPT_HTTPGET, 1L);
|
|
|
|
|
2020-10-27 12:05:50 +01:00
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
2020-10-24 16:59:41 +02:00
|
|
|
code = curl_easy_setopt(connection, CURLOPT_URL, url.c_str());
|
|
|
|
if (code != CURLE_OK)
|
2019-04-20 04:47:53 +02:00
|
|
|
{
|
2020-10-24 16:59:41 +02:00
|
|
|
throw runtime_error{"Couldn't set URL: " + to_string(code)};
|
2019-04-20 04:47:53 +02:00
|
|
|
}
|
2020-10-24 16:59:41 +02:00
|
|
|
|
|
|
|
code = curl_easy_perform(connection);
|
|
|
|
if (code != CURLE_OK)
|
2019-08-09 22:38:28 +02:00
|
|
|
{
|
2020-10-24 16:59:41 +02:00
|
|
|
throw runtime_error{"libcurl error: " + to_string(code)};
|
2019-08-09 22:38:28 +02:00
|
|
|
}
|
|
|
|
|
2020-10-24 16:59:41 +02:00
|
|
|
long http_status{0}; // NOLINT(google-runtime-int)
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
|
|
|
curl_easy_getinfo(connection, CURLINFO_RESPONSE_CODE, &http_status);
|
2019-08-09 22:38:28 +02:00
|
|
|
|
2020-10-24 16:59:41 +02:00
|
|
|
curl_easy_cleanup(connection);
|
2019-08-09 22:38:28 +02:00
|
|
|
|
2020-10-24 16:59:41 +02:00
|
|
|
if (http_status == 200)
|
2019-08-09 22:38:28 +02:00
|
|
|
{
|
2020-10-24 16:59:41 +02:00
|
|
|
return buffer_body;
|
2019-08-09 22:38:28 +02:00
|
|
|
}
|
|
|
|
|
2020-10-24 16:59:41 +02:00
|
|
|
if (cgi)
|
|
|
|
{
|
|
|
|
cout << "Status: " << http_status << endl;
|
2019-04-20 04:47:53 +02:00
|
|
|
}
|
2020-10-24 16:59:41 +02:00
|
|
|
throw runtime_error{"HTTP error: " + to_string(http_status)};
|
2019-04-20 04:47:53 +02:00
|
|
|
}
|
2020-10-24 16:59:41 +02:00
|
|
|
catch (const runtime_error &e)
|
2019-04-20 04:47:53 +02:00
|
|
|
{
|
2020-10-24 16:59:41 +02:00
|
|
|
cerr << "Error: " << e.what() << endl;
|
2019-04-20 04:47:53 +02:00
|
|
|
}
|
|
|
|
|
2019-08-09 22:38:28 +02:00
|
|
|
return "";
|
2019-04-20 04:47:53 +02:00
|
|
|
}
|
2020-10-24 16:59:41 +02:00
|
|
|
|
|
|
|
size_t writer_body(char *data, size_t size, size_t nmemb)
|
|
|
|
{
|
|
|
|
if (data == nullptr)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer_body.append(data, size * nmemb);
|
|
|
|
|
|
|
|
return size * nmemb;
|
|
|
|
}
|
2020-10-27 12:05:50 +01:00
|
|
|
|
|
|
|
} // namespace gitea2rss
|