Added HTTP status support, added curlpp initialization

This commit is contained in:
tastytea 2018-02-10 12:58:21 +01:00
parent f0c6004f30
commit 7d0a2d0af6
Signed by: tastytea
GPG Key ID: 59346E0EA35C67E5
3 changed files with 26 additions and 4 deletions

View File

@ -24,6 +24,7 @@
#include <curlpp/Easy.hpp> #include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp> #include <curlpp/Options.hpp>
#include <curlpp/Exception.hpp> #include <curlpp/Exception.hpp>
#include <curlpp/Infos.hpp>
#include "mastorss.hpp" #include "mastorss.hpp"
using std::string; using std::string;
@ -31,6 +32,11 @@ using std::cerr;
namespace curlopts = curlpp::options; namespace curlopts = curlpp::options;
void curlpp_init()
{
curlpp::initialize();
}
const std::uint16_t http_get(const string &feedurl, string &answer, const string &useragent) const std::uint16_t http_get(const string &feedurl, string &answer, const string &useragent)
{ {
try try
@ -43,10 +49,24 @@ const std::uint16_t http_get(const string &feedurl, string &answer, const string
{ {
"Connection: close", "Connection: close",
}); });
request.setOpt<curlopts::FollowLocation>(true);
request.setOpt<curlopts::WriteStream>(&oss);
request.perform();
oss << request; std::uint16_t ret = curlpp::infos::ResponseCode::get(request);
answer = oss.str(); if (ret == 200 || ret == 302 || ret == 307)
{ // OK or Found or Temporary Redirect
answer = oss.str();
}
else if (ret == 301 || ret == 308)
{ // Moved Permanently or Permanent Redirect
// FIXME: The new URL should be passed back somehow
answer = oss.str();
}
else
{
return ret;
}
return 0; return 0;
} }

View File

@ -242,11 +242,12 @@ int main(int argc, char *argv[])
std::uint16_t ret; std::uint16_t ret;
read_config(config, profile, instance, access_token, feedurl); read_config(config, profile, instance, access_token, feedurl);
curlpp_init();
string answer; string answer;
string last_entry = config.get(profile + ".last_entry", ""); string last_entry = config.get(profile + ".last_entry", "");
std::vector<string> entries; std::vector<string> entries;
//FIXME: User-Agent
ret = http_get(feedurl, answer, "mastorss/" + (string)global::version); ret = http_get(feedurl, answer, "mastorss/" + (string)global::version);
if (ret != 0) if (ret != 0)
{ {

View File

@ -15,5 +15,6 @@ std::vector<string> parse_website(const string &profile, const string &xml);
// http.cpp // http.cpp
const std::uint16_t http_get(const string &feedurl, const std::uint16_t http_get(const string &feedurl,
string &answer, const string &useragent = ""); string &answer, const string &useragent = "");
void curlpp_init();
#endif // mastorss_HPP #endif // mastorss_HPP