Commit e947dae0 authored by Andrea Scarpino's avatar Andrea Scarpino

Refactoring: share CSRF logic

parent 41c0b4ca
......@@ -207,21 +207,9 @@ QString HNManager::getSubmitCSRF() const
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
QTextStream stream(reply->readAll(), QIODevice::ReadOnly);
const QRegularExpression regexp("<input type=\"hidden\" name=\"fnid\" value=\"([^\"]+)\">");
QString line;
while (!stream.atEnd()) {
line = stream.readLine();
QRegularExpressionMatch match = regexp.match(line);
if (match.hasMatch()) {
return match.captured(1);
}
}
return QString();
return getCSRF(reply, regexp);
}
QString HNManager::getCommentCSRF(const int itemId) const
......@@ -239,10 +227,15 @@ QString HNManager::getCommentCSRF(const int itemId) const
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
QTextStream stream(reply->readAll(), QIODevice::ReadOnly);
const QRegularExpression regexp("<input type=\"hidden\" name=\"hmac\" value=\"([^\"]+)\">");
return getCSRF(reply, regexp);
}
QString HNManager::getCSRF(QNetworkReply *reply, const QRegularExpression &regexp) const
{
QTextStream stream(reply->readAll(), QIODevice::ReadOnly);
QString line;
while (!stream.atEnd()) {
line = stream.readLine();
......@@ -253,5 +246,7 @@ QString HNManager::getCommentCSRF(const int itemId) const
}
}
reply->deleteLater();
return QString();
}
......@@ -32,6 +32,8 @@
class HackerNewsAPI;
class QNetworkAccessManager;
class QNetworkReply;
class QRegularExpression;
class HNManager : public QObject
{
......@@ -62,6 +64,7 @@ private:
QString getSubmitCSRF() const;
QString getCommentCSRF(const int itemId) const;
QString getCSRF(QNetworkReply *reply, const QRegularExpression &regexp) const;
HackerNewsAPI *api;
QNetworkAccessManager *network;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment