-
-
Notifications
You must be signed in to change notification settings - Fork 143
- 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
base: main
Are you sure you want to change the base?
Changes from all commits
2552691
d8c3af8
1653e14
91b569f
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); | ||
| }); | ||
|
Comment on lines
+33
to
+44
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. I would extract this piece of code into a function of its own |
||
| return; | ||
| } | ||
| // No redirect, or max redirects reached | ||
| if (redirectAttr.isValid() && redirectCount >= kMaxRedirects) { | ||
|
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. With current code the |
||
| qWarning() << "Redirect limit (" << kMaxRedirects << ") reached. Reporting error."; | ||
|
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. Please delete the "Reporting error." part |
||
| // 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); | ||
| } | ||
|
|
||
|
Comment on lines
+61
to
+66
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. Seems to be unused
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. Thank you for pointing this out!
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. Undoubtedly there is much more stuff that can be added to support potential future use cases, but since |
||
| 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); | ||
|
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. If you give up some generality of
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. Thank you for the suggestion! |
||
| }); | ||
| } | ||
|
|
||
|
|
@@ -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); | ||
|
|
||
| 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.
|
||
| void handleReply(QNetworkReply* reply, std::function<void(QNetworkReply*)> finalHandler, int redirectCount); | ||
| 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.
|
||
|
|
||
| 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 |
||
|
|
||
| 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.