forked from cutelyst/cutelyst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.cpp
370 lines (327 loc) · 9.92 KB
/
engine.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
* Copyright (C) 2013-2018 Daniel Nicoletti <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "engine_p.h"
#include "context_p.h"
#include "common.h"
#include "request_p.h"
#include "application.h"
#include "response_p.h"
#include "context_p.h"
#include <QUrl>
#include <QSettings>
#include <QDir>
#include <QThread>
#include <QByteArray>
#include <QJsonDocument>
using namespace Cutelyst;
/*!
\class Cutelyst::Engine
\brief The Cutelyst Engine
This class is responsible receiving the request
and sending the response. It must be reimplemented
by real HTTP engines due some pure virtual methods.
The subclass must create an Engine per thread (worker core),
if the Application passed to the constructor has a worker core
greater than 0 it will issue a new Application instance, failing
to do so a fatal error is generated (usually indicating that
the Application does not have a Q_INVOKABLE constructor).
*/
/*!
@param app The application loaded
@param workerCore The thread number
@param opts The configuation options
*/
Engine::Engine(Cutelyst::Application *app, int workerCore, const QVariantMap &opts)
: d_ptr(new EnginePrivate)
{
Q_D(Engine);
connect(this, &Engine::processRequestAsync, this, &Engine::processRequest, Qt::QueuedConnection);
d->opts = opts;
d->workerCore = workerCore;
// If workerCore is greater than 0 we need a new application instance
if (workerCore) {
auto newApp = qobject_cast<Application *>(app->metaObject()->newInstance());
if (!newApp) {
qFatal("*** FATAL *** Could not create a NEW instance of your Cutelyst::Application, "
"make sure your constructor has Q_INVOKABLE macro or disable threaded mode.");
}
d->app = newApp;
} else {
d->app = app;
}
// To make easier for engines to clean up
// the app must be a child of it
d->app->setParent(this);
}
Engine::~Engine()
{
delete d_ptr;
}
/**
* @brief application
* @return the Application object we are dealing with
*/
Application *Engine::app() const
{
Q_D(const Engine);
Q_ASSERT(d->app);
return d->app;
}
/*! \fn virtual int Engine::workerId() const = 0
The id is the number of the spawned engine process,
a single process workerId = 0, two process 0 for the first
1 for the second.
\note the value returned from this function is
only valid when postFork() is issued.
\returns the worker id (process)
*/
/*!
Each worker process migth have a number of worker cores (threads),
a single process with two worker threads will return 0 and 1 for
each of the thread respectively.
\returns the worker core (thread)
*/
int Engine::workerCore() const
{
Q_D(const Engine);
return d->workerCore;
}
bool Engine::initApplication()
{
Q_D(Engine);
if (thread() != QThread::currentThread()) {
qCCritical(CUTELYST_ENGINE) << "Cannot init application on a different thread";
return false;
}
if (!d->app->setup(this)) {
qCCritical(CUTELYST_ENGINE) << "Failed to setup application";
return false;
}
return true;
}
bool Engine::postForkApplication()
{
Q_D(Engine);
if (!d->app) {
qCCritical(CUTELYST_ENGINE) << "Failed to postForkApplication on a null application";
return false;
}
QThread::currentThread()->setObjectName(QString::number(d->workerCore));
return d->app->enginePostFork();
}
quint64 Engine::time()
{
return quint64(QDateTime::currentMSecsSinceEpoch() * 1000);
}
const char *Engine::httpStatusMessage(quint16 status, int *len)
{
const char *ret;
switch (status) {
case Response::OK:
ret = "HTTP/1.1 200 OK";
break;
case Response::Found:
ret = "HTTP/1.1 302 Found";
break;
case Response::NotFound:
ret = "HTTP/1.1 404 Not Found";
break;
case Response::InternalServerError:
ret = "HTTP/1.1 500 Internal Server Error";
break;
case Response::MovedPermanently:
ret = "HTTP/1.1 301 Moved Permanently";
break;
case Response::NotModified:
ret = "HTTP/1.1 304 Not Modified";
break;
case Response::SeeOther:
ret = "HTTP/1.1 303 See Other";
break;
case Response::Forbidden:
ret = "HTTP/1.1 403 Forbidden";
break;
case Response::TemporaryRedirect:
ret = "HTTP/1.1 307 Temporary Redirect";
break;
case Response::Unauthorized:
ret = "HTTP/1.1 401 Unauthorized";
break;
case Response::BadRequest:
ret = "HTTP/1.1 400 Bad Request";
break;
case Response::MethodNotAllowed:
ret = "HTTP/1.1 405 Method Not Allowed";
break;
case Response::RequestTimeout:
ret = "HTTP/1.1 408 Request Timeout";
break;
case Response::Continue:
ret = "HTTP/1.1 100 Continue";
break;
case Response::SwitchingProtocols:
ret = "HTTP/1.1 101 Switching Protocols";
break;
case Response::Created:
ret = "HTTP/1.1 201 Created";
break;
case Response::Accepted:
ret = "HTTP/1.1 202 Accepted";
break;
case Response::NonAuthoritativeInformation:
ret = "HTTP/1.1 203 Non-Authoritative Information";
break;
case Response::NoContent:
ret = "HTTP/1.1 204 No Content";
break;
case Response::ResetContent:
ret = "HTTP/1.1 205 Reset Content";
break;
case Response::PartialContent:
ret = "HTTP/1.1 206 Partial Content";
break;
case Response::MultipleChoices:
ret = "HTTP/1.1 300 Multiple Choices";
break;
case Response::UseProxy:
ret = "HTTP/1.1 305 Use Proxy";
break;
case Response::PaymentRequired:
ret = "HTTP/1.1 402 Payment Required";
break;
case Response::NotAcceptable:
ret = "HTTP/1.1 406 Not Acceptable";
break;
case Response::ProxyAuthenticationRequired:
ret = "HTTP/1.1 407 Proxy Authentication Required";
break;
case Response::Conflict:
ret = "HTTP/1.1 409 Conflict";
break;
case Response::Gone:
ret = "HTTP/1.1 410 Gone";
break;
case Response::LengthRequired:
ret = "HTTP/1.1 411 Length Required";
break;
case Response::PreconditionFailed:
ret = "HTTP/1.1 412 Precondition Failed";
break;
case Response::RequestEntityTooLarge:
ret = "HTTP/1.1 413 Request Entity Too Large";
break;
case Response::RequestURITooLong:
ret = "HTTP/1.1 414 Request-URI Too Long";
break;
case Response::UnsupportedMediaType:
ret = "HTTP/1.1 415 Unsupported Media Type";
break;
case Response::RequestedRangeNotSatisfiable:
ret = "HTTP/1.1 416 Requested Range Not Satisfiable";
break;
case Response::ExpectationFailed:
ret = "HTTP/1.1 417 Expectation Failed";
break;
case Response::NotImplemented:
ret = "HTTP/1.1 501 Not Implemented";
break;
case Response::BadGateway:
ret = "HTTP/1.1 502 Bad Gateway";
break;
case Response::ServiceUnavailable:
ret = "HTTP/1.1 503 Service Unavailable";
break;
case Response::MultiStatus:
ret = "HTTP/1.1 207 Multi-Status";
break;
case Response::GatewayTimeout:
ret = "HTTP/1.1 504 Gateway Timeout";
break;
case Response::HTTPVersionNotSupported:
ret = "HTTP/1.1 505 HTTP Version Not Supported";
break;
case Response::BandwidthLimitExceeded:
ret = "HTTP/1.1 509 Bandwidth Limit Exceeded";
break;
default:
ret = QByteArrayLiteral("HTTP/1.1 ").append(QByteArray::number(status)).constData();
break;
}
if (len) {
*len = int(strlen(ret));
}
return ret;
}
Headers &Engine::defaultHeaders()
{
Q_D(Engine);
return d->app->defaultHeaders();
}
void Engine::processRequest(EngineRequest *request)
{
Q_D(Engine);
d->app->handleRequest(request);
}
QVariantMap Engine::opts() const
{
Q_D(const Engine);
return d->opts;
}
QVariantMap Engine::config(const QString &entity) const
{
Q_D(const Engine);
return d->config.value(entity).toMap();
}
void Engine::setConfig(const QVariantMap &config)
{
Q_D(Engine);
d->config = config;
}
QVariantMap Engine::loadIniConfig(const QString &filename)
{
QVariantMap ret;
QSettings settings(filename, QSettings::IniFormat);
if (settings.status() != QSettings::NoError) {
qCWarning(CUTELYST_ENGINE) << "Failed to load INI file:" << settings.status();
return ret;
}
const auto groups = settings.childGroups();
for (const QString &group : groups) {
QVariantMap configGroup;
settings.beginGroup(group);
const auto child = settings.childKeys();
for (const QString &key : child) {
configGroup.insert(key, settings.value(key));
}
settings.endGroup();
ret.insert(group, configGroup);
}
return ret;
}
QVariantMap Engine::loadJsonConfig(const QString &filename)
{
QVariantMap ret;
QFile file(filename);
if (!file.open(QIODevice::ReadOnly)) {
return ret;
}
QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
ret = doc.toVariant().toMap();
return ret;
}
#include "moc_engine.cpp"