From bea5438260411a008111747862b4e51879b1b65b Mon Sep 17 00:00:00 2001 From: Yoteichi Date: Sat, 17 Jun 2023 22:40:18 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=AA=E3=83=80=E3=82=A4=E3=83=AC=E3=82=AF?= =?UTF-8?q?=E3=83=88=E3=81=AB=E5=AF=BE=E5=BF=9C=E3=81=97=E3=81=9F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/common/chandir.cpp | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/core/common/chandir.cpp b/core/common/chandir.cpp index 8a158139..b475063b 100644 --- a/core/common/chandir.cpp +++ b/core/common/chandir.cpp @@ -81,6 +81,15 @@ int ChannelDirectory::numFeeds() const // が入っている場合がある。 static bool getFeed(std::string url, std::vector& out) { + const std::string originalUrl = url; + int retryCount = 0; + +Retry: + if (retryCount > 1) { + LOG_ERROR("Too many redirections. Giving up ..."); + return false; + } + out.clear(); URI feed(url); @@ -125,13 +134,25 @@ static bool getFeed(std::string url, std::vector& out) }); HTTPResponse res = rhttp.send(req); - if (res.statusCode != 200) { + if (res.statusCode == 301 || res.statusCode == 302 || res.statusCode == 307 || res.statusCode == 308) { + const auto loc = res.headers.get("Location"); + /* redirect */ + if (loc != "") { + LOG_TRACE("Status code %d. Redirecting to %s ...", res.statusCode, loc.c_str()); + url = loc; + retryCount++; + goto Retry; + }else { + LOG_ERROR("Status code %d. No Location header. Giving up ...", res.statusCode); + return false; + } + }else if (res.statusCode != 200) { LOG_ERROR("%s: status code %d", feed.host().c_str(), res.statusCode); return false; } std::vector errors; - out = ChannelEntry::textToChannelEntries(res.body, url, errors); + out = ChannelEntry::textToChannelEntries(res.body, originalUrl, errors); for (auto& message : errors) { LOG_ERROR("%s", message.c_str());