| 1 |
/* ============================================================ |
| 2 |
* |
| 3 |
* This file is a part of digiKam project |
| 4 |
* https://www.digikam.org |
| 5 |
* |
| 6 |
* Date : 2011-03-22 |
| 7 |
* Description : a MediaWiki C++ interface |
| 8 |
* |
| 9 |
* SPDX-FileCopyrightText: 2011-2026 by Gilles Caulier <caulier dot gilles at gmail dot com> |
| 10 |
* SPDX-FileCopyrightText: 2011 by Alexandre Mendes <alex dot mendes1988 at gmail dot com> |
| 11 |
* SPDX-FileCopyrightText: 2011 by Hormiere Guillaume <hormiere dot guillaume at gmail dot com> |
| 12 |
* SPDX-FileCopyrightText: 2011 by Manuel Campomanes <campomanes dot manuel at gmail dot com> |
| 13 |
* |
| 14 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 15 |
* |
| 16 |
* ============================================================ */ |
| 17 |
|
| 18 |
#include "fakeserver.h" |
| 19 |
|
| 20 |
// C++ includes |
| 21 |
|
| 22 |
#include <iostream> |
| 23 |
|
| 24 |
// Qt includes |
| 25 |
|
| 26 |
#include <QFile> |
| 27 |
#include <QRegularExpression> |
| 28 |
|
| 29 |
// Local includes |
| 30 |
|
| 31 |
#include "actionthreadbase.h" |
| 32 |
|
| 33 |
using namespace Digikam; |
| 34 |
|
| 35 |
FakeServer::FakeServer(QObject* const parent) |
| 36 |
: QThread (parent), |
| 37 |
m_tcpServer (nullptr), |
| 38 |
m_clientSocket(nullptr) |
| 39 |
{ |
| 40 |
moveToThread(this); |
| 41 |
} |
| 42 |
|
| 43 |
FakeServer::~FakeServer() |
| 44 |
{ |
| 45 |
quit(); |
| 46 |
wait(); |
| 47 |
} |
| 48 |
|
| 49 |
void FakeServer::startAndWait() |
| 50 |
{ |
| 51 |
start(); |
| 52 |
|
| 53 |
// this will block until the event queue starts |
| 54 |
|
| 55 |
QMetaObject::invokeMethod(this, "started", Qt::BlockingQueuedConnection); |
| 56 |
} |
| 57 |
|
| 58 |
void FakeServer::newConnection() |
| 59 |
{ |
| 60 |
QMutexLocker locker(&m_mutex); |
| 61 |
m_clientSocket = m_tcpServer->nextPendingConnection(); |
| 62 |
|
| 63 |
connect(m_clientSocket, SIGNAL(readyRead()), |
| 64 |
this, SLOT(dataAvailable())); |
| 65 |
} |
| 66 |
|
| 67 |
void FakeServer::dataAvailable() |
| 68 |
{ |
| 69 |
QMutexLocker locker(&m_mutex); |
| 70 |
|
| 71 |
if (m_clientSocket->canReadLine()) |
| 72 |
{ |
| 73 |
QStringList token = QString::fromUtf8(m_clientSocket->readAll()).split(QRegularExpression(QStringLiteral("[ \r\n][ \r\n]*"))); |
| 74 |
|
| 75 |
if (token.size() > 2) |
| 76 |
{ |
| 77 |
FakeServer::Request request; |
| 78 |
request.type = token[0]; |
| 79 |
request.value = token[1]; |
| 80 |
int index = token.indexOf(QString::fromLatin1("User-Agent:")); |
| 81 |
|
| 82 |
if (index < 0) |
| 83 |
{ |
| 84 |
index = token.indexOf(QString::fromLatin1("user-agent:")); |
| 85 |
} |
| 86 |
|
| 87 |
if ((index > 0) && ((index + 1) < token.size())) |
| 88 |
{ |
| 89 |
request.agent = token[index + 1]; |
| 90 |
} |
| 91 |
|
| 92 |
// It might happen that the same request cames through more than once, so you need to check that you are |
| 93 |
// counting each different request only once. |
| 94 |
// |
| 95 |
// For more information, see: qt-project.org/forums/viewthread/25521 |
| 96 |
|
| 97 |
if (!m_request.contains(request)) |
| 98 |
{ |
| 99 |
m_request << request; |
| 100 |
|
| 101 |
QString retour = m_scenarios.isEmpty() ? QStringLiteral("empty") : m_scenarios.takeFirst(); |
| 102 |
QString cookie = m_cookie.isEmpty() ? QStringLiteral("empty") : m_cookie.takeFirst(); |
| 103 |
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; |
| 104 |
m_clientSocket->write(scenario.toLocal8Bit()); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
m_clientSocket->close(); |
| 110 |
} |
| 111 |
|
| 112 |
void FakeServer::run() |
| 113 |
{ |
| 114 |
ActionThreadBase::setCurrentThreadName(QLatin1String("FakeServer")); // To customize thread name |
| 115 |
|
| 116 |
m_tcpServer = new QTcpServer(); |
| 117 |
|
| 118 |
if (!m_tcpServer->listen(QHostAddress(QHostAddress::LocalHost), 12566)) |
| 119 |
{ |
| 120 |
// TODO |
| 121 |
} |
| 122 |
|
| 123 |
connect(m_tcpServer, SIGNAL(newConnection()), |
| 124 |
this, SLOT(newConnection())); |
| 125 |
|
| 126 |
exec(); |
| 127 |
|
| 128 |
delete m_clientSocket; |
| 129 |
delete m_tcpServer; |
| 130 |
} |
| 131 |
|
| 132 |
void FakeServer::started() |
| 133 |
{ |
| 134 |
// do nothing: this is a dummy slot used by startAndWait() |
| 135 |
} |
| 136 |
|
| 137 |
void FakeServer::setScenario(const QString& scenario, const QString& cookie) |
| 138 |
{ |
| 139 |
QMutexLocker locker(&m_mutex); |
| 140 |
|
| 141 |
m_scenarios.clear(); |
| 142 |
m_scenarios << scenario; |
| 143 |
m_cookie.clear(); |
| 144 |
m_cookie << cookie; |
| 145 |
m_request.clear(); |
| 146 |
} |
| 147 |
|
| 148 |
void FakeServer::addScenario(const QString& scenario, const QString& cookie ) |
| 149 |
{ |
| 150 |
QMutexLocker locker(&m_mutex); |
| 151 |
|
| 152 |
m_scenarios << scenario; |
| 153 |
m_cookie << cookie; |
| 154 |
} |
| 155 |
|
| 156 |
void FakeServer::addScenarioFromFile(const QString& fileName, const QString& cookie ) |
| 157 |
{ |
| 158 |
QFile file(fileName); |
| 159 |
|
| 160 |
if (!file.open(QFile::ReadOnly)) |
| 161 |
{ |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
QTextStream in(&file); |
| 166 |
QString scenario; |
| 167 |
|
| 168 |
// When loading from files we never have the authentication phase |
| 169 |
// force jumping directly to authenticated state. |
| 170 |
|
| 171 |
while (!in.atEnd()) |
| 172 |
{ |
| 173 |
scenario.append(in.readLine()); |
| 174 |
} |
| 175 |
|
| 176 |
file.close(); |
| 177 |
|
| 178 |
addScenario(scenario, cookie); |
| 179 |
} |
| 180 |
|
| 181 |
bool FakeServer::isScenarioDone( int scenarioNumber ) const |
| 182 |
{ |
| 183 |
QMutexLocker locker(&m_mutex); |
| 184 |
|
| 185 |
if (scenarioNumber < m_scenarios.size()) |
| 186 |
{ |
| 187 |
return m_scenarios[scenarioNumber].isEmpty(); |
| 188 |
} |
| 189 |
else |
| 190 |
{ |
| 191 |
return true; // Non existent hence empty, right? |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
bool FakeServer::isAllScenarioDone() const |
| 196 |
{ |
| 197 |
QMutexLocker locker(&m_mutex); |
| 198 |
|
| 199 |
for (const QString& scenario : std::as_const(m_scenarios)) |
| 200 |
{ |
| 201 |
if (!scenario.isEmpty()) |
| 202 |
{ // cppcheck-suppress useStlAlgorithm |
| 203 |
return false; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
return true; |
| 208 |
} |
| 209 |
|
| 210 |
const QList<FakeServer::Request>& FakeServer::getRequest() |
| 211 |
{ |
| 212 |
return m_request; |
| 213 |
} |
| 214 |
|
| 215 |
FakeServer::Request FakeServer::takeLastRequest() |
| 216 |
{ |
| 217 |
return m_request.takeLast(); |
| 218 |
} |
| 219 |
|
| 220 |
FakeServer::Request FakeServer::takeFirstRequest() |
| 221 |
{ |
| 222 |
return m_request.takeFirst(); |
| 223 |
} |
| 224 |
|
| 225 |
void FakeServer::clearRequest() |
| 226 |
{ |
| 227 |
m_request.clear(); |
| 228 |
} |
| 229 |
|
| 230 |
#include "moc_fakeserver.cpp" |