Get rid of restclient-cpp dependency.

Use curl_wrapper instead.
This commit is contained in:
tastytea 2020-11-21 22:30:24 +01:00
parent 4de5c4e685
commit 3955cd95a1
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
3 changed files with 33 additions and 72 deletions

View File

@ -4,23 +4,10 @@ include(GNUInstallDirs)
find_package(Boost 1.62 REQUIRED COMPONENTS filesystem log regex) find_package(Boost 1.62 REQUIRED COMPONENTS filesystem log regex)
find_package(PkgConfig REQUIRED) find_package(PkgConfig REQUIRED)
pkg_check_modules(jsoncpp REQUIRED IMPORTED_TARGET jsoncpp) pkg_check_modules(jsoncpp REQUIRED IMPORTED_TARGET jsoncpp)
find_package(CURL 7.52 REQUIRED)
find_package(Threads REQUIRED) find_package(Threads REQUIRED)
find_package(restclient-cpp 0.5 CONFIG)
find_package(mastodonpp 0.5.6 REQUIRED CONFIG) find_package(mastodonpp 0.5.6 REQUIRED CONFIG)
if(NOT ${restclient-cpp_FOUND}) add_subdirectory(curl_wrapper)
find_file(restclient_h NAMES "restclient-cpp/restclient.h"
PATHS "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}")
if("${restclient_h}" STREQUAL "restclient_h-NOTFOUND")
message(FATAL_ERROR "Could not find restclient-cpp.")
else()
message(WARNING
"Your distribution of restclient-cpp doesn't contain the *Config.cmake "
"recipes, but the files seem to be in the standard directories. "
"Let's hope this works.")
endif()
endif()
# Write version in header. # Write version in header.
configure_file ("${PROJECT_SOURCE_DIR}/src/version.hpp.in" configure_file ("${PROJECT_SOURCE_DIR}/src/version.hpp.in"
@ -32,7 +19,7 @@ file(GLOB sources *.cpp)
add_executable(mastorss ${sources}) add_executable(mastorss ${sources})
target_link_libraries(mastorss target_link_libraries(mastorss
PRIVATE PRIVATE
PkgConfig::jsoncpp restclient-cpp mastodonpp::mastodonpp PkgConfig::jsoncpp curl_wrapper mastodonpp::mastodonpp
Boost::filesystem Boost::log Boost::regex) Boost::filesystem Boost::log Boost::regex)
if(BUILD_SHARED_LIBS) if(BUILD_SHARED_LIBS)
target_compile_definitions(mastorss PRIVATE "BOOST_ALL_DYN_LINK=1") target_compile_definitions(mastorss PRIVATE "BOOST_ALL_DYN_LINK=1")

View File

@ -16,6 +16,7 @@
#include "document.hpp" #include "document.hpp"
#include "curl_wrapper.hpp"
#include "exceptions.hpp" #include "exceptions.hpp"
#include "version.hpp" #include "version.hpp"
@ -24,7 +25,6 @@
#include <boost/regex.hpp> #include <boost/regex.hpp>
#include <json/json.h> #include <json/json.h>
#include <mastodonpp/mastodonpp.hpp> #include <mastodonpp/mastodonpp.hpp>
#include <restclient-cpp/connection.h>
#include <algorithm> #include <algorithm>
#include <fstream> #include <fstream>
@ -54,39 +54,29 @@ Document::Document(Config &cfg)
: _cfg{cfg} : _cfg{cfg}
, _profiledata{_cfg.profiledata} , _profiledata{_cfg.profiledata}
{ {
RestClient::init();
BOOST_LOG_TRIVIAL(debug) << "Initialized RestClient.";
download(); download();
} }
Document::~Document()
{
RestClient::disable();
}
void Document::download(const string &uri, const bool temp_redirect) void Document::download(const string &uri, const bool temp_redirect)
{ {
namespace cw = curl_wrapper;
BOOST_LOG_TRIVIAL(debug) << "Downloading <" << uri << "> …"; BOOST_LOG_TRIVIAL(debug) << "Downloading <" << uri << "> …";
RestClient::Connection connection{uri}; cw::CURLWrapper curl;
connection.SetUserAgent(string("mastorss/").append(version)); curl.set_useragent(string("mastorss/") += version);
connection.FollowRedirects(false); curl.set_maxredirs(0);
RestClient::Response response{connection.get("")}; const auto answer{curl.make_http_request(cw::http_method::GET, uri)};
BOOST_LOG_TRIVIAL(debug) << "Got response: " << response.code; BOOST_LOG_TRIVIAL(debug) << "Got response: " << answer.status;
BOOST_LOG_TRIVIAL(debug) << "Got Headers:"; BOOST_LOG_TRIVIAL(debug) << "Got Headers:";
for (const auto &header : response.headers) BOOST_LOG_TRIVIAL(debug) << answer.headers;
{
BOOST_LOG_TRIVIAL(debug)
<< " " << header.first << ": " << header.second;
}
switch (response.code) switch (answer.status)
{ {
case 200: case 200:
{ {
_raw_doc = response.body; _raw_doc = answer.body;
BOOST_LOG_TRIVIAL(debug) << "Downloaded feed: " << _profiledata.feedurl; BOOST_LOG_TRIVIAL(debug) << "Downloaded feed: " << _profiledata.feedurl;
break; break;
} }
@ -97,10 +87,10 @@ void Document::download(const string &uri, const bool temp_redirect)
{ {
goto temporary_redirect; // NOLINT(cppcoreguidelines-avoid-goto) goto temporary_redirect; // NOLINT(cppcoreguidelines-avoid-goto)
} }
_profiledata.feedurl = extract_location(response.headers); _profiledata.feedurl = extract_location(answer);
if (_profiledata.feedurl.empty()) if (_profiledata.feedurl.empty())
{ {
throw HTTPException{response.code}; throw HTTPException{answer.status};
} }
// clang-format off // clang-format off
@ -116,10 +106,10 @@ void Document::download(const string &uri, const bool temp_redirect)
case 307: case 307:
{ {
temporary_redirect: temporary_redirect:
const string newuri{extract_location(response.headers)}; const string newuri{extract_location(answer)};
if (newuri.empty()) if (newuri.empty())
{ {
throw HTTPException{response.code}; throw HTTPException{answer.status};
} }
// clang-format off // clang-format off
@ -129,13 +119,9 @@ temporary_redirect:
download(newuri, true); download(newuri, true);
break; break;
} }
case -1:
{
throw CURLException{errno};
}
default: default:
{ {
throw HTTPException{response.code}; throw HTTPException{answer.status};
} }
} }
} }
@ -274,23 +260,13 @@ string Document::remove_html(string html) const
return html; return html;
} }
string Document::extract_location(const RestClient::HeaderFields &headers) const string Document::extract_location(const curl_wrapper::answer &answer)
{ {
string location; const string location = answer.get_header("Location");
try
if (location.empty())
{ {
location = headers.at("Location"); throw std::runtime_error{"Could not extract new feed location."};
}
catch (const std::out_of_range &)
{
try
{
location = headers.at("location");
}
catch (const std::out_of_range &)
{
throw std::runtime_error{"Could not extract new feed location."};
}
} }
return location; return location;

View File

@ -1,5 +1,5 @@
/* This file is part of mastorss. /* This file is part of mastorss.
* Copyright © 2019 tastytea <tastytea@tastytea.de> * Copyright © 2019, 2020 tastytea <tastytea@tastytea.de>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -18,18 +18,18 @@
#define MASTORSS_DOCUMENT_HPP #define MASTORSS_DOCUMENT_HPP
#include "config.hpp" #include "config.hpp"
#include "curl_wrapper.hpp"
#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/ptree.hpp>
#include <restclient-cpp/restclient.h>
#include <string>
#include <list> #include <list>
#include <string>
namespace mastorss namespace mastorss
{ {
namespace pt = boost::property_tree; namespace pt = boost::property_tree;
using std::string;
using std::list; using std::list;
using std::string;
/*! /*!
* @brief An Item of a feed. * @brief An Item of a feed.
@ -43,7 +43,7 @@ struct Item
string link; string link;
string title; string title;
friend bool operator !=(const Item &a, const Item &b); friend bool operator!=(const Item &a, const Item &b);
}; };
/*! /*!
@ -55,7 +55,6 @@ class Document
{ {
public: public:
explicit Document(Config &cfg); explicit Document(Config &cfg);
~Document();
Document(const Document &other) = default; Document(const Document &other) = default;
Document &operator=(const Document &other) = delete; Document &operator=(const Document &other) = delete;
Document(Document &&other) = default; Document(Document &&other) = default;
@ -83,15 +82,14 @@ private:
* *
* @since 0.10.0 * @since 0.10.0
*/ */
void download(const string &uri, const bool temp_redirect = false); void download(const string &uri, bool temp_redirect = false);
void parse_rss(const pt::ptree &tree); void parse_rss(const pt::ptree &tree);
[[nodiscard]] [[nodiscard]] string remove_html(string html) const;
string remove_html(string html) const; [[nodiscard]] static string
[[nodiscard]] extract_location(const curl_wrapper::answer &answer);
string extract_location(const RestClient::HeaderFields &headers) const;
string add_hashtags(const string &text); string add_hashtags(const string &text);
void parse_watchwords(); void parse_watchwords();
}; };
} // namespace mastorss } // namespace mastorss
#endif // MASTORSS_DOCUMENT_HPP #endif // MASTORSS_DOCUMENT_HPP