Handle HTTP redirects.
continuous-integration/drone/push Build is failing Details

This commit is contained in:
tastytea 2019-12-25 06:28:31 +01:00
parent d08bb95fbd
commit 4491af53db
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
2 changed files with 46 additions and 7 deletions

View File

@ -23,7 +23,6 @@
#include <boost/regex.hpp>
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <restclient-cpp/connection.h>
#include <restclient-cpp/restclient.h>
#include <list>
#include <sstream>
@ -53,11 +52,11 @@ Document::~Document()
RestClient::disable();
}
void Document::download()
void Document::download(const string &uri)
{
RestClient::Connection connection(_data.feedurl);
RestClient::Connection connection{uri};
connection.SetUserAgent(string("mastorss/").append(version));
connection.FollowRedirects(true, 10);
connection.FollowRedirects(false);
RestClient::Response response{connection.get("")};
@ -72,9 +71,30 @@ void Document::download()
case 301:
case 308:
{
// TODO(tastytea): Handle permanent redirections.
throw std::runtime_error{"Permanent redirect, "
"no solution implemented yet."};
_data.feedurl = extract_location(response.headers);
if (_data.feedurl.empty())
{
throw HTTPException{response.code};
}
BOOST_LOG_TRIVIAL(debug) << "Feed has new location: " << _data.feedurl;
_cfg.write();
download();
break;
}
case 302:
case 303:
case 307:
{
string newuri{extract_location(response.headers)};
if (newuri.empty())
{
throw HTTPException{response.code};
}
BOOST_LOG_TRIVIAL(debug) << "Feed redirect: " << _data.feedurl;
download(move(newuri));
break;
}
case -1:
{
@ -87,6 +107,11 @@ void Document::download()
}
}
void Document::download()
{
download(_data.feedurl);
}
void Document::parse()
{
pt::ptree tree;
@ -185,4 +210,14 @@ string Document::remove_html(string html) const
return html;
}
string Document::extract_location(const RestClient::HeaderFields &headers) const
{
string location{headers.at("Location")};
if (location.empty())
{
location = headers.at("location");
}
return location;
}
} // namespace mastorss

View File

@ -20,6 +20,7 @@
#include "config.hpp"
#include <boost/property_tree/ptree.hpp>
#include <restclient-cpp/restclient.h>
#include <string>
#include <vector>
@ -61,6 +62,7 @@ public:
vector<Item> new_items;
void download();
void download(const string &uri);
void parse();
private:
@ -71,6 +73,8 @@ private:
void parse_rss(const pt::ptree &tree);
[[nodiscard]]
string remove_html(string html) const;
[[nodiscard]]
string extract_location(const RestClient::HeaderFields &headers) const;
};
} // namespace mastorss