AusweisApp2/src/network/HttpServerRequestor.cpp

65 lines
1.3 KiB
C++
Raw Normal View History

2017-07-03 09:30:10 +02:00
/*
2019-05-22 10:08:38 +02:00
* \copyright Copyright (c) 2014-2019 Governikus GmbH & Co. KG, Germany
2017-07-03 09:30:10 +02:00
*/
#include "HttpServerRequestor.h"
#include "NetworkManager.h"
#include <QCoreApplication>
#include <QLoggingCategory>
#include <QNetworkRequest>
using namespace governikus;
Q_DECLARE_LOGGING_CATEGORY(network)
HttpServerRequestor::HttpServerRequestor()
: QObject()
, mEventLoop()
, mTimer()
, mReply(nullptr)
{
connect(&mTimer, &QTimer::timeout, &mEventLoop, &QEventLoop::quit);
mTimer.setSingleShot(true);
}
HttpServerRequestor::~HttpServerRequestor()
{
}
2019-01-03 15:06:22 +01:00
QUrl HttpServerRequestor::createUrl(const QString& pQuery, quint16 pPort, const QHostAddress& pHost, const QString& pPath)
2017-07-03 09:30:10 +02:00
{
QUrl url;
url.setScheme(QStringLiteral("http"));
url.setHost(pHost.toString());
url.setPort(pPort);
url.setPath(pPath);
url.setQuery(pQuery);
return url;
}
QPointer<QNetworkReply> HttpServerRequestor::request(const QUrl& pUrl, int pTimeOut)
{
qCDebug(network) << "Request URL:" << pUrl;
QNetworkRequest getRequest(pUrl);
mTimer.start(pTimeOut);
2017-12-20 14:54:05 +01:00
mReply.reset(Env::getSingleton<NetworkManager>()->get(getRequest));
2017-07-03 09:30:10 +02:00
connect(mReply.data(), &QNetworkReply::finished, this, &HttpServerRequestor::finished);
mEventLoop.exec();
return mReply->isFinished() ? mReply.data() : nullptr;
}
void HttpServerRequestor::finished()
{
mTimer.stop();
mEventLoop.quit();
}