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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2011-03-22
* Description : a MediaWiki C++ interface
*
* SPDX-FileCopyrightText: 2011-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
* SPDX-FileCopyrightText: 2011 by Alexandre Mendes <alex dot mendes1988 at gmail dot com>
* SPDX-FileCopyrightText: 2011 by Hormiere Guillaume <hormiere dot guillaume at gmail dot com>
* SPDX-FileCopyrightText: 2011 by Manuel Campomanes <campomanes dot manuel at gmail dot com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "fakeserver.h"
// C++ includes
#include <iostream>
// Qt includes
#include <QFile>
#include <QRegularExpression>
FakeServer::FakeServer(QObject* const parent)
: QThread (parent),
m_tcpServer (nullptr),
m_clientSocket(nullptr)
{
moveToThread(this);
}
FakeServer::~FakeServer()
{
quit();
wait();
}
void FakeServer::startAndWait()
{
start();
// this will block until the event queue starts
QMetaObject::invokeMethod(this, "started", Qt::BlockingQueuedConnection);
}
void FakeServer::newConnection()
{
QMutexLocker locker(&m_mutex);
m_clientSocket = m_tcpServer->nextPendingConnection();
connect(m_clientSocket, SIGNAL(readyRead()),
this, SLOT(dataAvailable()));
}
void FakeServer::dataAvailable()
{
QMutexLocker locker(&m_mutex);
if (m_clientSocket->canReadLine())
{
QStringList token = QString::fromUtf8(m_clientSocket->readAll()).split(QRegularExpression(QStringLiteral("[ \r\n][ \r\n]*")));
if (token.size() > 2)
{
FakeServer::Request request;
request.type = token[0];
request.value = token[1];
int index = token.indexOf(QString::fromLatin1("User-Agent:"));
if (index < 0)
{
index = token.indexOf(QString::fromLatin1("user-agent:"));
}
if ((index > 0) && ((index + 1) < token.size()))
{
request.agent = token[index + 1];
}
// It might happen that the same request cames through more than once, so you need to check that you are
// counting each different request only once.
//
// For more information, see: qt-project.org/forums/viewthread/25521
if (!m_request.contains(request))
{
m_request << request;
QString retour = m_scenarios.isEmpty() ? QStringLiteral("empty") : m_scenarios.takeFirst();
QString cookie = m_cookie.isEmpty() ? QStringLiteral("empty") : m_cookie.takeFirst();
QString scenario = QStringLiteral("HTTP/1.0 200 Ok\r\nContent-Type: text/html; charset=\"utf-8\"\r\nSet-Cookie:") + cookie + QStringLiteral("\r\n\r\n") + retour;
m_clientSocket->write(scenario.toLocal8Bit());
}
}
}
m_clientSocket->close();
}
void FakeServer::run()
{
m_tcpServer = new QTcpServer();
if (!m_tcpServer->listen(QHostAddress(QHostAddress::LocalHost), 12566))
{
// TODO
}
connect(m_tcpServer, SIGNAL(newConnection()),
this, SLOT(newConnection()));
exec();
delete m_clientSocket;
delete m_tcpServer;
}
void FakeServer::started()
{
// do nothing: this is a dummy slot used by startAndWait()
}
void FakeServer::setScenario(const QString& scenario, const QString& cookie)
{
QMutexLocker locker(&m_mutex);
m_scenarios.clear();
m_scenarios << scenario;
m_cookie.clear();
m_cookie << cookie;
m_request.clear();
}
void FakeServer::addScenario(const QString& scenario, const QString& cookie )
{
QMutexLocker locker(&m_mutex);
m_scenarios << scenario;
m_cookie << cookie;
}
void FakeServer::addScenarioFromFile(const QString& fileName, const QString& cookie )
{
QFile file(fileName);
if (!file.open(QFile::ReadOnly))
{
return;
}
QTextStream in(&file);
QString scenario;
// When loading from files we never have the authentication phase
// force jumping directly to authenticated state.
while (!in.atEnd())
{
scenario.append(in.readLine());
}
file.close();
addScenario(scenario, cookie);
}
bool FakeServer::isScenarioDone( int scenarioNumber ) const
{
QMutexLocker locker(&m_mutex);
if (scenarioNumber < m_scenarios.size())
{
return m_scenarios[scenarioNumber].isEmpty();
}
else
{
return true; // Non existent hence empty, right?
}
}
bool FakeServer::isAllScenarioDone() const
{
QMutexLocker locker(&m_mutex);
for (const QString& scenario : std::as_const(m_scenarios))<--- Consider using std::all_of or std::none_of algorithm instead of a raw loop.
{
if (!scenario.isEmpty())
{ // cppcheck-suppress useStlAlgorithm
return false;
}
}
return true;
}
const QList<FakeServer::Request>& FakeServer::getRequest()
{
return m_request;
}
FakeServer::Request FakeServer::takeLastRequest()
{
return m_request.takeLast();
}
FakeServer::Request FakeServer::takeFirstRequest()
{
return m_request.takeFirst();
}
void FakeServer::clearRequest()
{
m_request.clear();
}
#include "moc_fakeserver.cpp"
|