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 <boost/regex.hpp>
#include <mastodon-cpp/mastodon-cpp.hpp> #include <mastodon-cpp/mastodon-cpp.hpp>
#include <restclient-cpp/connection.h> #include <restclient-cpp/connection.h>
#include <restclient-cpp/restclient.h>
#include <list> #include <list>
#include <sstream> #include <sstream>
@ -53,11 +52,11 @@ Document::~Document()
RestClient::disable(); 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.SetUserAgent(string("mastorss/").append(version));
connection.FollowRedirects(true, 10); connection.FollowRedirects(false);
RestClient::Response response{connection.get("")}; RestClient::Response response{connection.get("")};
@ -72,9 +71,30 @@ void Document::download()
case 301: case 301:
case 308: case 308:
{ {
// TODO(tastytea): Handle permanent redirections. _data.feedurl = extract_location(response.headers);
throw std::runtime_error{"Permanent redirect, " if (_data.feedurl.empty())
"no solution implemented yet."}; {
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: case -1:
{ {
@ -87,6 +107,11 @@ void Document::download()
} }
} }
void Document::download()
{
download(_data.feedurl);
}
void Document::parse() void Document::parse()
{ {
pt::ptree tree; pt::ptree tree;
@ -185,4 +210,14 @@ string Document::remove_html(string html) const
return html; 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 } // namespace mastorss

View File

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