source: src/testing/webserverprocess.cpp@ 1ebaa4f

Last change on this file since 1ebaa4f was 1ebaa4f, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago

Changed fixed PHP binary path to the determined one from CMake

  • Property mode set to 100644
File size: 6.4 KB
Line 
1/*
2 Copyright (C) 2015 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 MA 02110-1301 USA.
18*/
19
20#include "webserverprocess.h"
21#include "moc_webserverprocess.cpp"
22
23#include "sitestingconfig.h"
24
25#include <QTcpSocket>
26#include <QRegExp>
27#include <QNetworkAccessManager>
28#include <QNetworkRequest>
29#include <QNetworkReply>
30#include <QTime>
31#include <QCoreApplication>
32#include <QTest>
33#include <QDir>
34
35WebserverProcess::WebserverProcess(QObject* parent)
36 : QObject(parent)
37{
38 _process = new QProcess(this);
39 connect(_process, SIGNAL(error(QProcess::ProcessError)),
40 this, SLOT(processError(QProcess::ProcessError)));
41 connect(_process, SIGNAL(finished(int,QProcess::ExitStatus)),
42 this, SLOT(processFinished(int,QProcess::ExitStatus)));
43 connect(_process, SIGNAL(readyRead()),
44 this, SLOT(processReadyRead()));
45}
46
47WebserverProcess::~WebserverProcess()
48{
49 if(_process && _process->state() == QProcess::Running){
50 _process->kill();
51 _process->waitForFinished(5000);
52 }
53 cleanupDataDirectory();
54}
55
56QString WebserverProcess::webserviceBaseUrl()
57{
58 return appendSlashToUrl(QString(WEBSERVICE_BASE_URL));
59}
60
61QString WebserverProcess::webserviceApiVersion()
62{
63 return QString(WEBSERVICE_API_VERSION);
64}
65
66
67bool WebserverProcess::isRunning() const
68{
69 if(!_process)
70 return false;
71
72 QRegExp regex("http:\\/\\/([^:]+)+:([0-9]+)");
73 if(regex.indexIn(webserviceBaseUrl()) < 0){
74 qDebug() << QString("Regex %1 to find host and port not matching in %2").arg(regex.pattern()).arg(webserviceBaseUrl());
75 return false;
76 }
77 QStringList capturedTexts = regex.capturedTexts();
78 if(capturedTexts.size() != 3){
79 qDebug() << QString("Regex %1 does not matches enough items in %2").arg(regex.pattern()).arg(webserviceBaseUrl());
80 return false;
81 }
82 QTcpSocket *socket = new QTcpSocket();
83 socket->connectToHost(capturedTexts[1], capturedTexts[2].toInt(), QIODevice::ReadOnly);
84 if (!socket->waitForConnected()){
85 qDebug() << QString("Webservice at %1:%2 not reachable").arg(capturedTexts[1]).arg(capturedTexts[2]);
86 delete socket;
87 return false;
88 }
89 socket->close();
90 delete socket;
91 return true;
92}
93
94bool WebserverProcess::start()
95{
96 if(!_process)
97 return false;
98
99 QStringList arguments;
100 arguments << QString("-S %1:%2").arg(QString(WEBSERVICE_SERVER_HOST)).arg(QString(WEBSERVICE_SERVER_PORT));
101 arguments << QString("-t %1").arg(QString(WEBSERVICE_SERVER_DOCROOT));
102
103 connect(_process, SIGNAL(error(QProcess::ProcessError)),
104 this, SLOT(processError(QProcess::ProcessError)));
105 connect(_process, SIGNAL(finished(int,QProcess::ExitStatus)),
106 this, SLOT(processFinished(int,QProcess::ExitStatus)));
107 connect(_process, SIGNAL(readyRead()),
108 this, SLOT(processReadyRead()));
109
110 _process->setReadChannelMode(QProcess::ForwardedChannels);
111 _process->start(QString("%1 %2").arg(QString(PHP_EXECUTABLE)).arg(arguments.join(" ")));
112 // give the php webserver a few seconds to start
113 QTest::qWait(5000);
114 if(!isRunning())
115 return false;
116 return true;
117}
118
119bool WebserverProcess::stop()
120{
121 if(!_process)
122 return true;
123 _process->terminate();
124 return _process->waitForFinished(5000);
125}
126
127
128bool WebserverProcess::setup()
129{
130 if(!_process)
131 return false;
132
133 QRegExp regex("http:\\/\\/([^:]+)+:([0-9]+)");
134 if(regex.indexIn(webserviceBaseUrl()) < 0){
135 qDebug() << QString("Regex %1 to find host and port not matching in %2").arg(regex.pattern()).arg(webserviceBaseUrl());
136 return false;
137 }
138 QStringList capturedTexts = regex.capturedTexts();
139 if(capturedTexts.size() != 3){
140 qDebug() << QString("Regex %1 does not matches enough items in %2").arg(regex.pattern()).arg(webserviceBaseUrl());
141 return false;
142 }
143 QNetworkAccessManager *manager = new QNetworkAccessManager(this);
144 QNetworkReply *reply = manager->get(QNetworkRequest(QUrl(QString("%1%2").arg(webserviceBaseUrl()).arg("index.php/setup"))));
145 QTime dieTime = QTime::currentTime().addSecs(30);
146 while(QTime::currentTime() < dieTime){
147 if(reply->isFinished())
148 break;
149 QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents, 100);
150 }
151 bool result = reply->isFinished();
152 reply->deleteLater();
153 manager->deleteLater();
154 return result;
155}
156
157bool WebserverProcess::cleanupDataDirectory()
158{
159 QDir dir(QString(WEBSERVICE_SERVER_DOCROOT));
160 if(!dir.exists())
161 return true;
162 if(!dir.removeRecursively())
163 return false;
164 return true;
165}
166
167void WebserverProcess::processError(QProcess::ProcessError error)
168{
169 QString errorString;
170 switch(error){
171 case QProcess::FailedToStart:
172 errorString = "FailedToStart";
173 break;
174 case QProcess::Crashed:
175 errorString = "Crashed";
176 break;
177 case QProcess::Timedout:
178 errorString = "Timedout";
179 break;
180 case QProcess::WriteError:
181 errorString = "WriteError";
182 break;
183 case QProcess::ReadError:
184 errorString = "ReadError";
185 break;
186 default:
187 errorString = "Unknown";
188 }
189
190 qDebug() << QString("%1: Error %2 (%3)")
191 .arg(metaObject()->className()).arg(errorString).arg(int(error));
192}
193
194void WebserverProcess::processFinished(int exitCode, QProcess::ExitStatus exitStatus)
195{
196 QString exitString;
197 switch(exitStatus){
198 case QProcess::NormalExit:
199 exitString = "NormalExit";
200 break;
201 case QProcess::CrashExit:
202 exitString = "CrashExit";
203 break;
204 default:
205 exitString = "Unknown";
206 }
207 qDebug() << QString("%1: Finished with exit code %2 and staus %3 (%4)")
208 .arg(metaObject()->className()).arg(exitCode).arg(exitString).arg(int(exitStatus));
209}
210
211void WebserverProcess::processReadyRead()
212{
213 QProcess *process = qobject_cast<QProcess*>(sender());
214 if(!process)
215 return;
216 qDebug() << QString("%1: Read %2")
217 .arg(metaObject()->className()).arg(QString::fromUtf8(process->readAll()));
218}
219
220QString WebserverProcess::appendSlashToUrl(QString url)
221{
222 if(url.right(1) != "/")
223 url += "/";
224 return url;
225}
Note: See TracBrowser for help on using the repository browser.