forked from Governikus/AusweisApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpServerStatusParser.cpp
80 lines (60 loc) · 1.9 KB
/
HttpServerStatusParser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* Copyright (c) 2014-2024 Governikus GmbH & Co. KG, Germany
*/
#include "HttpServerStatusParser.h"
#include "LogHandler.h"
#include "NetworkManager.h"
#include <QLoggingCategory>
#include <http_parser.h>
using namespace governikus;
Q_DECLARE_LOGGING_CATEGORY(network)
HttpServerStatusParser::HttpServerStatusParser(quint16 pPort, const QHostAddress& pHost)
: QObject()
, mUrl(HttpServerRequestor::createUrl(QStringLiteral("Status"), pPort, pHost))
, mServerHeader()
, mVersionInfo()
, mRequestor()
{
}
bool HttpServerStatusParser::request()
{
// if we were called multiple time we need to clear() here
mVersionInfo = VersionInfo();
mServerHeader.clear();
const auto reply = mRequestor.getRequest(mUrl);
if (!reply.isNull())
{
return parseReply(reply);
}
return false;
}
const QString& HttpServerStatusParser::getServerHeader() const
{
return mServerHeader;
}
const VersionInfo& HttpServerStatusParser::getVersionInfo() const
{
return mVersionInfo;
}
bool HttpServerStatusParser::parseReply(const QSharedPointer<QNetworkReply>& pReply)
{
mServerHeader = pReply->header(QNetworkRequest::KnownHeaders::ServerHeader).toString();
if (mServerHeader.isEmpty())
{
mServerHeader = pReply->header(QNetworkRequest::KnownHeaders::UserAgentHeader).toString();
if (!mServerHeader.isEmpty())
{
qCDebug(network) << "Server header is undefined... using User-Agent";
}
}
const auto statusCode = NetworkManager::getLoggedStatusCode(pReply, spawnMessageLogger(network));
if (statusCode == HTTP_STATUS_OK)
{
Q_ASSERT_X(pReply->isFinished(), "HttpServerStatusParser::parseReply", "Not all data available for reply");
mVersionInfo = VersionInfo::fromText(QString::fromUtf8(pReply->readAll()));
qCDebug(network) << "Found version info" << mVersionInfo;
return !mVersionInfo.isNull();
}
qCDebug(network) << "Cannot get status information! Got bad http status code:" << pReply->error();
return false;
}