From e8ac4869b1e7cfa74daa0d8d015b918f294b6246 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 29 Dec 2019 00:41:14 +0100 Subject: [PATCH] Treat permanent redirects after temporary redirects as temporary. Example: http://example.com/ returns 302 with Location: http://example.com/1 http://example.com/1 returns 301 with Location: http://example.com/2 http://example.com/ will not be overwritten in the config file. --- src/document.cpp | 9 +++++++-- src/document.hpp | 15 +++++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/document.cpp b/src/document.cpp index 4674c7b..5f40068 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -61,7 +61,7 @@ Document::~Document() RestClient::disable(); } -void Document::download(const string &uri) +void Document::download(const string &uri, const bool temp_redirect) { RestClient::Connection connection{uri}; connection.SetUserAgent(string("mastorss/").append(version)); @@ -80,6 +80,10 @@ void Document::download(const string &uri) case 301: case 308: { + if (temp_redirect) + { + goto temporary_redirect; + } _profiledata.feedurl = extract_location(response.headers); if (_profiledata.feedurl.empty()) { @@ -96,6 +100,7 @@ void Document::download(const string &uri) case 303: case 307: { + temporary_redirect: const string newuri{extract_location(response.headers)}; if (newuri.empty()) { @@ -104,7 +109,7 @@ void Document::download(const string &uri) BOOST_LOG_TRIVIAL(debug) << "Feed has new location (temporary): " << _profiledata.feedurl; - download(newuri); + download(newuri, true); break; } case -1: diff --git a/src/document.hpp b/src/document.hpp index b39a975..608dbcc 100644 --- a/src/document.hpp +++ b/src/document.hpp @@ -63,8 +63,6 @@ public: list new_items; - void download(); - void download(const string &uri); void parse(); private: @@ -73,6 +71,19 @@ private: string _raw_doc; list _watchwords; + void download(); + /*! + * @brief Download document. + * + * @param uri The URI to download. + * @param temp_redirect `true` if this is an temporary redirect. + * + * The argument `temp_redirect` is there for cases where the original URI + * is redirected temporarily, and the new URI is redirected permanently. + * + * @since 0.10.0 + */ + void download(const string &uri, const bool temp_redirect = false); void parse_rss(const pt::ptree &tree); [[nodiscard]] string remove_html(string html) const;