-
-
Notifications
You must be signed in to change notification settings - Fork 145
Handle 301/302 redirects automatically in OpdsRequestManager #1388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
2552691
d8c3af8
1653e14
91b569f
31a19dc
7327fa4
d81b3f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,10 @@ | ||
| #include "opdsrequestmanager.h" | ||
| #include "kiwixapp.h" | ||
| #include <QPointer> | ||
|
|
||
| namespace { | ||
| constexpr int kMaxRedirects = 5; | ||
| } | ||
|
|
||
| OpdsRequestManager::OpdsRequestManager() | ||
| { | ||
|
|
@@ -21,6 +26,44 @@ int OpdsRequestManager::getCatalogPort() | |
| : 443; | ||
| } | ||
|
|
||
| // Helper to handle replies and follow redirects | ||
| void OpdsRequestManager::handleReply(QNetworkReply* reply, std::function<void(QNetworkReply*)> finalHandler, int redirectCount) { | ||
| QVariant redirectAttr = reply->attribute(QNetworkRequest::RedirectionTargetAttribute); | ||
| if (redirectAttr.isValid() && redirectCount < kMaxRedirects) { | ||
| QUrl redirectUrl = redirectAttr.toUrl(); | ||
| // Make absolute if needed | ||
| if (redirectUrl.isRelative()) { | ||
| redirectUrl = reply->url().resolved(redirectUrl); | ||
| } | ||
| qInfo() << "Following redirect" << redirectCount + 1 << "to:" << redirectUrl.toString(); | ||
| reply->deleteLater(); | ||
| QNetworkRequest newRequest(redirectUrl); | ||
| QNetworkReply* newReply = m_networkManager.get(newRequest); | ||
| connect(newReply, &QNetworkReply::finished, this, [=]() { | ||
| handleReply(newReply, finalHandler, redirectCount + 1); | ||
| }); | ||
|
||
| return; | ||
| } | ||
| // No redirect, or max redirects reached | ||
| if (redirectAttr.isValid() && redirectCount >= kMaxRedirects) { | ||
|
||
| qWarning() << "Redirect limit (" << kMaxRedirects << ") reached. Reporting error."; | ||
|
||
| // Set a custom property to indicate redirect limit reached | ||
| reply->setProperty("redirectLimitReached", true); | ||
| // Optionally, abort the reply to set an error | ||
| reply->abort(); | ||
| } | ||
| if (redirectCount > 0) { | ||
| qInfo() << "Completed after" << redirectCount << "redirects"; | ||
| } | ||
| finalHandler(reply); | ||
| } | ||
|
|
||
| // New method to allow requests to arbitrary URLs (for redirects) | ||
| QNetworkReply* OpdsRequestManager::opdsResponseFromUrl(const QUrl &url) { | ||
| QNetworkRequest request(url); | ||
| return m_networkManager.get(request); | ||
| } | ||
|
|
||
|
||
| void OpdsRequestManager::doUpdate(const QString& currentLanguage, const QString& categoryFilter) | ||
| { | ||
| QUrlQuery query; | ||
|
|
@@ -43,7 +86,7 @@ void OpdsRequestManager::doUpdate(const QString& currentLanguage, const QString& | |
|
|
||
| auto mp_reply = opdsResponseFromPath("/catalog/v2/entries", query); | ||
| connect(mp_reply, &QNetworkReply::finished, this, [=]() { | ||
| receiveContent(mp_reply); | ||
| handleReply(mp_reply, [this](QNetworkReply* r) { receiveContent(r); }, 0); | ||
|
||
| }); | ||
| } | ||
|
|
||
|
|
@@ -65,15 +108,15 @@ void OpdsRequestManager::getLanguagesFromOpds() | |
| { | ||
| auto mp_reply = opdsResponseFromPath("/catalog/v2/languages"); | ||
| connect(mp_reply, &QNetworkReply::finished, this, [=]() { | ||
| receiveLanguages(mp_reply); | ||
| handleReply(mp_reply, [this](QNetworkReply* r) { receiveLanguages(r); }, 0); | ||
| }); | ||
| } | ||
|
|
||
| void OpdsRequestManager::getCategoriesFromOpds() | ||
| { | ||
| auto mp_reply = opdsResponseFromPath("/catalog/v2/categories"); | ||
| connect(mp_reply, &QNetworkReply::finished, this, [=]() { | ||
| receiveCategories(mp_reply); | ||
| handleReply(mp_reply, [this](QNetworkReply* r) { receiveCategories(r); }, 0); | ||
| }); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,15 +18,20 @@ class OpdsRequestManager : public QObject | |
| void doUpdate(const QString& currentLanguage, const QString& categoryFilter); | ||
| void getLanguagesFromOpds(); | ||
| void getCategoriesFromOpds(); | ||
| QNetworkReply* opdsResponseFromUrl(const QUrl &url); | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A leftover from earlier experiments (unused and undefined) |
||
| private: | ||
| QNetworkAccessManager m_networkManager; | ||
| QNetworkReply* opdsResponseFromPath(const QString &path, const QUrlQuery &query = QUrlQuery()); | ||
| void handleNetworkReply(QNetworkReply* reply, void (OpdsRequestManager::*finalHandler)(QNetworkReply*), int redirectCount); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A leftover from earlier experiments (unused and undefined) |
||
| void handleReply(QNetworkReply* reply, std::function<void(QNetworkReply*)> finalHandler, int redirectCount); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A leftover from earlier experiments (unused and undefined)
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually I removed all of them both forgot to change in the actual branch(newbie mistake sincere apologies), I will make sure that i will remove all these in the next iteration and come up with two commits |
||
| static constexpr int MAX_REDIRECTS = 5; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A leftover from earlier experiments (unused and undefined) |
||
|
|
||
| signals: | ||
| void requestReceived(const QString&); | ||
| void languagesReceived(const QString&); | ||
| void categoriesReceived(const QString&); | ||
| void requestError(const QString& errorMessage); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unused
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A leftover from earlier experiments (unused and undefined) |
||
|
|
||
| public slots: | ||
| void receiveContent(QNetworkReply*); | ||
|
|
@@ -39,3 +44,5 @@ public slots: | |
| }; | ||
|
|
||
| #endif // OPDSREQUESTMANAGER_H | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to have the URL that was redirected (
reply->url()) in this info message.