5#include "localserver.h"
7#include "protocolfastcgi.h"
8#include "protocolhttp.h"
9#include "protocolhttp2.h"
11#include "serverengine.h"
13#include "tcpserverbalancer.h"
18# include "windowsfork.h"
22# include "../EventLoopEPoll/eventdispatcher_epoll.h"
23# include "systemdnotify.h"
28#include <QCommandLineParser>
29#include <QCoreApplication>
31#include <QLoggingCategory>
32#include <QMetaProperty>
33#include <QPluginLoader>
35#include <QSocketNotifier>
40Q_LOGGING_CATEGORY(CUTELYST_SERVER,
"cutelyst.server", QtWarningMsg)
46 , d_ptr(new ServerPrivate(this))
50 if (!qEnvironmentVariableIsSet(
"QT_MESSAGE_PATTERN")) {
51 if (qEnvironmentVariableIsSet(
"JOURNAL_STREAM")) {
53 qSetMessagePattern(u
"%{category}[%{type}] %{message}"_qs);
55 qSetMessagePattern(u
"%{pid}:%{threadid} %{category}[%{type}] %{message}"_qs);
60 if (!qEnvironmentVariableIsSet(
"CUTELYST_QT_EVENT_LOOP")) {
61 qCInfo(CUTELYST_SERVER) <<
"Trying to install EPoll event loop";
66 auto cleanUp = [
this]() {
69 d->protoHTTP =
nullptr;
72 d->protoHTTP2 =
nullptr;
75 d->protoFCGI =
nullptr;
80 qDeleteAll(d->servers);
91 std::cout <<
"Cutelyst-Server terminated" << std::endl;
102 qtTrId(
"cutelystd-cli-desc"));
111 qtTrId(
"cutelystd-opt-ini-desc"),
114 qtTrId(
"cutelystd-opt-value-file"));
122 qtTrId(
"cutelystd-opt-json-desc"),
123 qtTrId(
"cutelystd-opt-value-file"));
127 QStringLiteral(
"chdir"),
130 qtTrId(
"cutelystd-opt-chdir-desc"),
133 qtTrId(
"cutelystd-opt-value-directory"));
137 QStringLiteral(
"chdir2"),
140 qtTrId(
"cutelystd-opt-chdir2-desc"),
141 qtTrId(
"cutelystd-opt-value-directory"));
145 QStringLiteral(
"lazy"),
148 qtTrId(
"cutelystd-opt-lazy-desc"));
154 qtTrId(
"cutelystd-opt-application-desc"),
155 qtTrId(
"cutelystd-opt-value-file"));
162 qtTrId(
"cutelystd-opt-threads-desc"),
165 qtTrId(
"cutelystd-opt-threads-value"));
173 qtTrId(
"cutelystd-opt-processes-desc"),
176 qtTrId(
"cutelystd-opt-processes-value"));
183 qtTrId(
"cutelystd-opt-master-desc"));
189 qtTrId(
"cutelystd-opt-listen-desc"),
192 qtTrId(
"cutelystd-opt-value-size"));
198 qtTrId(
"cutelystd-opt-buffer-size-desc"),
201 qtTrId(
"cutelystd-opt-value-bytes"));
209 qtTrId(
"cutelystd-opt-post-buffering-desc"),
210 qtTrId(
"cutelystd-opt-value-bytes"));
214 QStringLiteral(
"post-buffering-bufsize"),
217 qtTrId(
"cutelystd-opt-post-buffering-bufsize-desc"),
218 qtTrId(
"cutelystd-opt-value-bytes"));
221 QCommandLineOption httpSocketOpt({QStringLiteral(
"http-socket"), QStringLiteral(
"h1")},
224 qtTrId(
"cutelystd-opt-http-socket-desc"),
227 qtTrId(
"cutelystd-opt-value-address"));
231 {QStringLiteral(
"http2-socket"), QStringLiteral(
"h2")},
234 qtTrId(
"cutelystd-opt-http2-socket-desc"),
235 qtTrId(
"cutelystd-opt-value-address"));
241 qtTrId(
"cutelystd-opt-http2-header-table-size-desc"),
242 qtTrId(
"cutelystd-opt-value-size"));
243 parser.
addOption(http2HeaderTableSizeOpt);
248 qtTrId(
"cutelystd-opt-upgrade-h2c-desc"));
254 qtTrId(
"cutelystd-opt-https-h2-desc"));
257 QCommandLineOption httpsSocketOpt({QStringLiteral(
"https-socket"), QStringLiteral(
"hs1")},
260 qtTrId(
"cutelystd-opt-https-socket-desc"),
262 qtTrId(
"cutelystd-opt-value-httpsaddress"));
266 QStringLiteral(
"fastcgi-socket"),
269 qtTrId(
"cutelystd-opt-fastcgi-socket-desc"),
270 qtTrId(
"cutelystd-opt-value-address"));
274 QStringLiteral(
"socket-access"),
277 qtTrId(
"cutelystd-opt-socket-access-desc"),
280 qtTrId(
"cutelystd-opt-socket-access-value"));
283 QCommandLineOption socketTimeout({QStringLiteral(
"socket-timeout"), QStringLiteral(
"z")},
286 qtTrId(
"cutelystd-opt-socket-timeout-desc"),
289 qtTrId(
"cutelystd-opt-socket-timeout-value"));
298 qtTrId(
"cutelystd-opt-static-map-desc"),
301 qtTrId(
"cutelystd-opt-value-static-map"));
308 qtTrId(
"cutelystd-opt-static-map2-desc"),
311 qtTrId(
"cutelystd-opt-value-static-map"));
318 qtTrId(
"cutelystd-opt-auto-restart-desc"));
322 QStringLiteral(
"touch-reload"),
326 qtTrId(
"cutelystd-opt-touch-reload-desc"),
327 qtTrId(
"cutelystd-opt-value-file"));
333 qtTrId(
"cutelystd-opt-tcp-nodelay-desc"));
339 qtTrId(
"cutelystd-opt-so-keepalive-desc"));
346 qtTrId(
"cutelystd-opt-socket-sndbuf-desc"),
347 qtTrId(
"cutelystd-opt-value-bytes"));
354 qtTrId(
"cutelystd-opt-socket-rcvbuf-desc"),
355 qtTrId(
"cutelystd-opt-value-bytes"));
362 qtTrId(
"cutelystd-opt-websocket-max-size-desc"),
365 qtTrId(
"cutelystd-opt-websocket-max-size-value"));
371 qtTrId(
"cutelystd-opt-pidfile-desc"),
374 qtTrId(
"cutelystd-opt-value-pidfile"));
380 qtTrId(
"cutelystd-opt-pidfile2-desc"),
381 qtTrId(
"cutelystd-opt-value-pidfile"));
388 qtTrId(
"cutelystd-opt-stop-desc"),
389 qtTrId(
"cutelystd-opt-value-pidfile"));
395 qtTrId(
"cutelystd-opt-uid-desc"),
398 qtTrId(
"cutelystd-opt-uid-value"));
404 qtTrId(
"cutelystd-opt-gid-desc"),
407 qtTrId(
"cutelystd-opt-gid-value"));
413 qtTrId(
"cutelystd-opt-no-init-groups-desc"));
419 qtTrId(
"cutelystd-opt-chown-socket-desc"),
422 qtTrId(
"cutelystd-opt-chown-socket-value"));
428 qtTrId(
"cutelystd-opt-umask-desc"),
431 qtTrId(
"cutelystd-opt-umask-value"));
435 QStringLiteral(
"cpu-affinity"),
438 qtTrId(
"cutelystd-opt-cpu-affinity-desc"),
441 qtTrId(
"cutelystd-opt-cpu-affinity-value"));
449 qtTrId(
"cutelystd-opt-reuse-port-desc"));
454 QStringLiteral(
"experimental-thread-balancer"),
457 qtTrId(
"cutelystd-opt-experimental-thread-balancer-desc"));
463 qtTrId(
"cutelystd-opt-using-frontend-proxy-desc"));
469 setIni(parser.
values(iniOpt));
471 setJson(parser.
values(jsonOpt));
485 if (parser.
isSet(socketAccess)) {
486 setSocketAccess(parser.
value(socketAccess));
489 if (parser.
isSet(socketTimeout)) {
491 auto size = parser.
value(socketTimeout).
toInt(&ok);
492 setSocketTimeout(size);
493 if (!ok || size < 0) {
498 if (parser.
isSet(pidfileOpt)) {
499 setPidfile(parser.
value(pidfileOpt));
502 if (parser.
isSet(pidfile2Opt)) {
503 setPidfile2(parser.
value(pidfile2Opt));
507 if (parser.
isSet(stopOption)) {
508 UnixFork::stopWSGI(parser.
value(stopOption));
515 if (parser.
isSet(uidOption)) {
516 setUid(parser.
value(uidOption));
519 if (parser.
isSet(gidOption)) {
520 setGid(parser.
value(gidOption));
523 if (parser.
isSet(noInitgroupsOption)) {
524 setNoInitgroups(
true);
527 if (parser.
isSet(chownSocketOption)) {
528 setChownSocket(parser.
value(chownSocketOption));
531 if (parser.
isSet(umaskOption)) {
532 setUmask(parser.
value(umaskOption));
535 if (parser.
isSet(cpuAffinityOption)) {
537 auto value = parser.
value(cpuAffinityOption).
toInt(&ok);
538 setCpuAffinity(value);
539 if (!ok || value < 0) {
546 if (parser.
isSet(reusePortOption)) {
551 if (parser.
isSet(lazyOption)) {
555 if (parser.
isSet(listenQueue)) {
557 auto size = parser.
value(listenQueue).
toInt(&ok);
558 setListenQueue(size);
559 if (!ok || size < 1) {
564 if (parser.
isSet(bufferSize)) {
566 auto size = parser.
value(bufferSize).
toInt(&ok);
568 if (!ok || size < 1) {
573 if (parser.
isSet(postBuffering)) {
576 setPostBuffering(size);
577 if (!ok || size < 1) {
582 if (parser.
isSet(postBufferingBufsize)) {
585 setPostBufferingBufsize(size);
586 if (!ok || size < 1) {
599 if (parser.
isSet(autoReload)) {
603 if (parser.
isSet(tcpNoDelay)) {
607 if (parser.
isSet(soKeepAlive)) {
608 setSoKeepalive(
true);
611 if (parser.
isSet(upgradeH2cOpt)) {
615 if (parser.
isSet(httpsH2Opt)) {
619 if (parser.
isSet(socketSndbuf)) {
621 auto size = parser.
value(socketSndbuf).
toInt(&ok);
622 setSocketSndbuf(size);
623 if (!ok || size < 1) {
628 if (parser.
isSet(socketRcvbuf)) {
630 auto size = parser.
value(socketRcvbuf).
toInt(&ok);
631 setSocketRcvbuf(size);
632 if (!ok || size < 1) {
637 if (parser.
isSet(wsMaxSize)) {
639 auto size = parser.
value(wsMaxSize).
toInt(&ok);
640 setWebsocketMaxSize(size);
641 if (!ok || size < 1) {
646 if (parser.
isSet(http2HeaderTableSizeOpt)) {
648 auto size = parser.
value(http2HeaderTableSizeOpt).
toUInt(&ok);
649 setHttp2HeaderTableSize(size);
650 if (!ok || size < 1) {
655 if (parser.
isSet(frontendProxy)) {
656 setUsingFrontendProxy(
true);
659 setHttpSocket(httpSocket() + parser.
values(httpSocketOpt));
661 setHttp2Socket(http2Socket() + parser.
values(http2SocketOpt));
663 setHttpsSocket(httpsSocket() + parser.
values(httpsSocketOpt));
665 setFastcgiSocket(fastcgiSocket() + parser.
values(fastcgiSocketOpt));
667 setStaticMap(staticMap() + parser.
values(staticMapOpt));
669 setStaticMap2(staticMap2() + parser.
values(staticMap2Opt));
671 setTouchReload(touchReload() + parser.
values(touchReloadOpt));
673 d->threadBalancer = parser.
isSet(threadBalancerOpt);
679 std::cout <<
"Cutelyst-Server starting" << std::endl;
681 if (!qEnvironmentVariableIsSet(
"CUTELYST_SERVER_IGNORE_MASTER") && !d->master) {
683 <<
"*** WARNING: you are running Cutelyst-Server without its master process manager ***"
688 if (d->processes == -1 && d->threads == -1) {
689 d->processes = UnixFork::idealProcessCount();
690 d->threads = UnixFork::idealThreadCount() / d->processes;
691 }
else if (d->processes == -1) {
692 d->processes = UnixFork::idealThreadCount();
693 }
else if (d->threads == -1) {
694 d->threads = UnixFork::idealThreadCount();
697 if (d->processes == 0 && d->master) {
700 d->genericFork =
new UnixFork(d->processes, qMax(d->threads, 1), !d->userEventLoop,
this);
702 if (d->processes == -1) {
705 if (d->threads == -1) {
716 if (d->master && d->lazy) {
717 if (d->autoReload && !d->application.isEmpty()) {
718 d->touchReload.append(d->application);
720 d->genericFork->setTouchReload(d->touchReload);
724 if (d->master && !d->genericFork->continueMaster(&ret)) {
729 if (systemdNotify::is_systemd_notify_available()) {
731 sd->setWatchdog(
true, systemdNotify::sd_watchdog_enabled(
true));
733 sd->sendStatus(qApp->applicationName().toLatin1() +
" is ready");
736 connect(d, &ServerPrivate::postForked, sd, [sd] { sd->setWatchdog(
false); });
737 qInfo(CUTELYST_SERVER) <<
"systemd notify detected";
745 if (!d->listenTcpSockets()) {
752 if (!d->writePidFile(d->pidfile)) {
758 bool isListeningLocalSockets =
false;
759 if (!d->chownSocket.isEmpty()) {
760 if (!d->listenLocalSockets()) {
765 isListeningLocalSockets =
true;
768 if (!d->umask.isEmpty() && !UnixFork::setUmask(d->umask.toLatin1())) {
772 if (!UnixFork::setGidUid(d->gid, d->uid, d->noInitgroups)) {
778 if (!isListeningLocalSockets) {
780 d->listenLocalSockets();
786 if (!d->listenTcpSockets()) {
792 if (d->servers.empty()) {
793 std::cout <<
"Please specify a socket to listen to" << std::endl;
799 d->writePidFile(d->pidfile2);
801 if (!d->chdir.isEmpty()) {
802 std::cout <<
"Changing directory to: " << d->chdir.toLatin1().constData() << std::endl;
813 if (!d->setupApplication()) {
820 if (d->userEventLoop) {
825 ret = d->genericFork->exec(d->lazy, d->master);
843 d->userEventLoop =
true;
848 qputenv(
"CUTELYST_SERVER_IGNORE_MASTER", QByteArrayLiteral(
"1"));
850 if (
exec(app) == 0) {
860 if (d->userEventLoop) {
865ServerPrivate::~ServerPrivate()
872bool ServerPrivate::listenTcpSockets()
874 if (httpSockets.isEmpty() && httpsSockets.isEmpty() && http2Sockets.isEmpty() &&
875 fastcgiSockets.isEmpty()) {
881 for (
const auto &socket : qAsConst(httpSockets)) {
882 if (!listenTcp(socket, getHttpProto(),
false)) {
888 for (
const auto &socket : qAsConst(httpsSockets)) {
889 if (!listenTcp(socket, getHttpProto(),
true)) {
895 for (
const auto &socket : qAsConst(http2Sockets)) {
896 if (!listenTcp(socket, getHttp2Proto(),
false)) {
902 for (
const auto &socket : qAsConst(fastcgiSockets)) {
903 if (!listenTcp(socket, getFastCgiProto(),
false)) {
911bool ServerPrivate::listenTcp(
const QString &line,
Protocol *protocol,
bool secure)
918 server->setBalancer(threadBalancer);
919 ret = server->listen(line, protocol, secure);
921 if (ret && server->socketDescriptor()) {
922 auto qEnum = protocol->staticMetaObject.enumerator(0);
923 std::cout << qEnum.valueToKey(
static_cast<int>(protocol->type())) <<
" socket "
925 <<
" bound to TCP address " << server->serverName().constData() <<
" fd "
927 servers.push_back(server);
934bool ServerPrivate::listenLocalSockets()
943 std::vector<int> fds = systemdNotify::listenFds();
946 if (server->listen(fd)) {
947 const QString name = server->serverName();
948 const QString fullName = server->fullServerName();
952 protocol = getHttpProto();
954 protocol = getHttp2Proto();
956 protocol = getFastCgiProto();
958 std::cerr <<
"systemd activated socket does not match any configured socket"
962 server->setProtocol(protocol);
963 server->pauseAccepting();
965 auto qEnum = protocol->staticMetaObject.enumerator(0);
966 std::cout << qEnum.valueToKey(
static_cast<int>(protocol->type())) <<
" socket "
968 <<
" bound to LOCAL address " << qPrintable(fullName) <<
" fd "
970 servers.push_back(server);
972 std::cerr <<
"Failed to listen on activated LOCAL FD: "
974 << qPrintable(server->errorString()) << std::endl;
981 const auto httpConst = http;
982 for (
const auto &socket : httpConst) {
983 ret |= listenLocal(socket, getHttpProto());
986 const auto http2Const = http2;
987 for (
const auto &socket : http2Const) {
988 ret |= listenLocal(socket, getHttp2Proto());
991 const auto fastcgiConst = fastcgi;
992 for (
const auto &socket : fastcgiConst) {
993 ret |= listenLocal(socket, getFastCgiProto());
999bool ServerPrivate::listenLocal(
const QString &line,
Protocol *protocol)
1006 server->setProtocol(protocol);
1007 if (!socketAccess.isEmpty()) {
1009 if (socketAccess.contains(u
'u')) {
1013 if (socketAccess.contains(u
'g')) {
1017 if (socketAccess.contains(u
'o')) {
1020 server->setSocketOptions(options);
1022 server->removeServer(line);
1023 ret = server->listen(line);
1024 server->pauseAccepting();
1026 if (!ret || !server->socket()) {
1027 std::cerr <<
"Failed to listen on LOCAL: " << qPrintable(line) <<
" : "
1028 << qPrintable(server->errorString()) << std::endl;
1033 if (!chownSocket.isEmpty()) {
1034 UnixFork::chownSocket(line, chownSocket);
1037 auto qEnum = protocol->staticMetaObject.enumerator(0);
1038 std::cout << qEnum.valueToKey(
static_cast<int>(protocol->type())) <<
" socket "
1040 <<
" bound to LOCAL address " << qPrintable(line) <<
" fd "
1042 servers.push_back(server);
1048void Server::setApplication(
const QString &application)
1053 if (loader.fileName().isEmpty()) {
1058 d->application = loader.fileName();
1066 return d->application;
1069void Server::setThreads(
const QString &threads)
1083 if (d->threads == -1) {
1084 return QStringLiteral(
"auto");
1089void Server::setProcesses(
const QString &process)
1096 d->processes = process.
toInt();
1105 if (d->processes == -1) {
1106 return QStringLiteral(
"auto");
1111void Server::setChdir(
const QString &chdir)
1124void Server::setHttpSocket(
const QStringList &httpSocket)
1127 d->httpSockets = httpSocket;
1134 return d->httpSockets;
1137void Server::setHttp2Socket(
const QStringList &http2Socket)
1140 d->http2Sockets = http2Socket;
1147 return d->http2Sockets;
1150void Server::setHttp2HeaderTableSize(quint32 headerTableSize)
1153 d->http2HeaderTableSize = headerTableSize;
1157quint32 Server::http2HeaderTableSize()
const
1160 return d->http2HeaderTableSize;
1163void Server::setUpgradeH2c(
bool enable)
1166 d->upgradeH2c = enable;
1170bool Server::upgradeH2c()
const
1173 return d->upgradeH2c;
1176void Server::setHttpsH2(
bool enable)
1179 d->httpsH2 = enable;
1183bool Server::httpsH2()
const
1189void Server::setHttpsSocket(
const QStringList &httpsSocket)
1192 d->httpsSockets = httpsSocket;
1199 return d->httpsSockets;
1202void Server::setFastcgiSocket(
const QStringList &fastcgiSocket)
1205 d->fastcgiSockets = fastcgiSocket;
1212 return d->fastcgiSockets;
1215void Server::setSocketAccess(
const QString &socketAccess)
1218 d->socketAccess = socketAccess;
1222QString Server::socketAccess()
const
1225 return d->socketAccess;
1228void Server::setSocketTimeout(
int timeout)
1231 d->socketTimeout = timeout;
1235int Server::socketTimeout()
const
1238 return d->socketTimeout;
1241void Server::setChdir2(
const QString &chdir2)
1257 d->ini.append(files);
1258 d->ini.removeDuplicates();
1261 for (
const QString &file : files) {
1262 if (!d->configLoaded.contains(file)) {
1263 auto fileToLoad = std::make_pair(file, ServerPrivate::ConfigFormat::Ini);
1264 if (!d->configToLoad.contains(fileToLoad)) {
1265 qCDebug(CUTELYST_SERVER) <<
"Enqueue INI config file:" << file;
1266 d->configToLoad.enqueue(fileToLoad);
1283 d->json.append(files);
1284 d->json.removeDuplicates();
1287 for (
const QString &file : files) {
1288 if (!d->configLoaded.contains(file)) {
1289 auto fileToLoad = std::make_pair(file, ServerPrivate::ConfigFormat::Json);
1290 if (!d->configToLoad.contains(fileToLoad)) {
1291 qCDebug(CUTELYST_SERVER) <<
"Enqueue JSON config file:" << file;
1292 d->configToLoad.enqueue(fileToLoad);
1306void Server::setStaticMap(
const QStringList &staticMap)
1309 d->staticMaps = staticMap;
1316 return d->staticMaps;
1319void Server::setStaticMap2(
const QStringList &staticMap)
1322 d->staticMaps2 = staticMap;
1329 return d->staticMaps2;
1332void Server::setMaster(
bool enable)
1335 if (!qEnvironmentVariableIsSet(
"CUTELYST_SERVER_IGNORE_MASTER")) {
1347void Server::setAutoReload(
bool enable)
1351 d->autoReload =
true;
1356bool Server::autoReload()
const
1359 return d->autoReload;
1362void Server::setTouchReload(
const QStringList &files)
1365 d->touchReload = files;
1372 return d->touchReload;
1375void Server::setListenQueue(
int size)
1378 d->listenQueue = size;
1382int Server::listenQueue()
const
1385 return d->listenQueue;
1388void Server::setBufferSize(
int size)
1392 qCWarning(CUTELYST_SERVER) <<
"Buffer size must be at least 4096 bytes, ignoring";
1395 d->bufferSize = size;
1399int Server::bufferSize()
const
1402 return d->bufferSize;
1405void Server::setPostBuffering(qint64 size)
1408 d->postBuffering = size;
1412qint64 Server::postBuffering()
const
1415 return d->postBuffering;
1418void Server::setPostBufferingBufsize(qint64 size)
1422 qCWarning(CUTELYST_SERVER) <<
"Post buffer size must be at least 4096 bytes, ignoring";
1425 d->postBufferingBufsize = size;
1429qint64 Server::postBufferingBufsize()
const
1432 return d->postBufferingBufsize;
1435void Server::setTcpNodelay(
bool enable)
1438 d->tcpNodelay = enable;
1442bool Server::tcpNodelay()
const
1445 return d->tcpNodelay;
1448void Server::setSoKeepalive(
bool enable)
1451 d->soKeepalive = enable;
1455bool Server::soKeepalive()
const
1458 return d->soKeepalive;
1461void Server::setSocketSndbuf(
int value)
1464 d->socketSendBuf = value;
1468int Server::socketSndbuf()
const
1471 return d->socketSendBuf;
1474void Server::setSocketRcvbuf(
int value)
1477 d->socketReceiveBuf = value;
1481int Server::socketRcvbuf()
const
1484 return d->socketReceiveBuf;
1487void Server::setWebsocketMaxSize(
int value)
1490 d->websocketMaxSize = value * 1024;
1494int Server::websocketMaxSize()
const
1497 return d->websocketMaxSize / 1024;
1500void Server::setPidfile(
const QString &file)
1513void Server::setPidfile2(
const QString &file)
1526void Server::setUid(
const QString &uid)
1541void Server::setGid(
const QString &gid)
1556void Server::setNoInitgroups(
bool enable)
1560 d->noInitgroups = enable;
1565bool Server::noInitgroups()
const
1568 return d->noInitgroups;
1571void Server::setChownSocket(
const QString &chownSocket)
1575 d->chownSocket = chownSocket;
1580QString Server::chownSocket()
const
1583 return d->chownSocket;
1586void Server::setUmask(
const QString &value)
1601void Server::setCpuAffinity(
int value)
1605 d->cpuAffinity = value;
1610int Server::cpuAffinity()
const
1613 return d->cpuAffinity;
1616void Server::setReusePort(
bool enable)
1620 d->reusePort = enable;
1627bool Server::reusePort()
const
1630 return d->reusePort;
1633void Server::setLazy(
bool enable)
1646void Server::setUsingFrontendProxy(
bool enable)
1649 d->usingFrontendProxy = enable;
1653bool Server::usingFrontendProxy()
const
1656 return d->usingFrontendProxy;
1665bool ServerPrivate::setupApplication()
1672 std::cout <<
"Loading application: " << application.
toLatin1().
constData() << std::endl;
1675 if (!loader.load()) {
1676 qCCritical(CUTELYST_SERVER) <<
"Could not load application:" << loader.errorString();
1680 QObject *instance = loader.instance();
1682 qCCritical(CUTELYST_SERVER) <<
"Could not get a QObject instance: %s\n"
1683 << loader.errorString();
1687 localApp = qobject_cast<Cutelyst::Application *>(instance);
1689 qCCritical(CUTELYST_SERVER)
1690 <<
"Could not cast Cutelyst::Application from instance: %s\n"
1691 << loader.errorString();
1702 if (!chdir2.isEmpty()) {
1703 std::cout <<
"Changing directory2 to: " << chdir2.toLatin1().constData() << std::endl;
1712 engine = createEngine(localApp, 0);
1713 for (
int i = 1; i < threads; ++i) {
1714 if (createEngine(localApp, i)) {
1715 ++workersNotRunning;
1719 engine = createEngine(localApp, 0);
1720 workersNotRunning = 1;
1724 std::cerr <<
"Application failed to init, cheaping..." << std::endl;
1731void ServerPrivate::engineShutdown(
ServerEngine *engine)
1733 const auto engineThread = engine->
thread();
1736 engines.erase(std::remove(engines.begin(), engines.end(), engine), engines.end());
1737 checkEngineShutdown();
1739 engineThread->quit();
1741 engines.erase(std::remove(engines.begin(), engines.end(), engine), engines.end());
1744 checkEngineShutdown();
1747void ServerPrivate::checkEngineShutdown()
1749 if (engines.empty()) {
1750 if (userEventLoop) {
1752 Q_EMIT q->stopped();
1759void ServerPrivate::workerStarted()
1764 if (--workersNotRunning == 0) {
1769bool ServerPrivate::postFork(
int workerId)
1774 if (!setupApplication()) {
1775 Q_EMIT q->errorOccured(qtTrId(
"cutelystd-err-fail-setup-app"));
1780 if (engines.size() > 1) {
1781 qCDebug(CUTELYST_SERVER) <<
"Starting threads";
1786 if (thread != qApp->thread()) {
1788 if (!qEnvironmentVariableIsSet(
"CUTELYST_QT_EVENT_LOOP")) {
1797 Q_EMIT postForked(workerId);
1805 qApp->processEvents();
1811bool ServerPrivate::writePidFile(
const QString &filename)
1817 QFile file(filename);
1819 std::cerr <<
"Failed write pid file " << qPrintable(filename) << std::endl;
1823 std::cout <<
"Writing pidfile to " << qPrintable(filename) << std::endl;
1834 if (workerCore > 0) {
1837 qFatal(
"*** FATAL *** Could not create a NEW instance of your Cutelyst::Application, "
1838 "make sure your constructor has Q_INVOKABLE macro or disable threaded mode.");
1842 auto engine =
new ServerEngine(app, workerCore, opt, q);
1847 &ServerEngine::shutdownCompleted,
1849 &ServerPrivate::engineShutdown,
1855 engine->setServers(servers);
1856 if (!engine->
init()) {
1857 std::cerr <<
"Application failed to init(), cheaping core: " << workerCore << std::endl;
1862 engines.push_back(engine);
1865 if (workerCore > 0) {
1870 auto thread =
new QThread(
this);
1879void ServerPrivate::loadConfig()
1881 if (loadingConfig) {
1885 loadingConfig =
true;
1887 if (configToLoad.isEmpty()) {
1888 loadingConfig =
false;
1892 auto fileToLoad = configToLoad.dequeue();
1894 if (fileToLoad.first.isEmpty()) {
1895 qCWarning(CUTELYST_SERVER) <<
"Can not load config from empty config file name";
1896 loadingConfig =
false;
1900 if (configLoaded.contains(fileToLoad.first)) {
1901 loadingConfig =
false;
1905 configLoaded.append(fileToLoad.first);
1907 QVariantMap loadedConfig;
1908 switch (fileToLoad.second) {
1909 case ConfigFormat::Ini:
1910 qCInfo(CUTELYST_SERVER) <<
"Loading INI configuratin:" << fileToLoad.first;
1913 case ConfigFormat::Json:
1914 qCInfo(CUTELYST_SERVER) <<
"Loading JSON configuration:" << fileToLoad.first;
1919 auto loadedIt = loadedConfig.cbegin();
1920 while (loadedIt != loadedConfig.cend()) {
1921 if (config.contains(loadedIt.key())) {
1922 QVariantMap currentMap = config.value(loadedIt.key()).toMap();
1923 const QVariantMap loadedMap = loadedIt.value().toMap();
1924 auto loadedMapIt = loadedMap.cbegin();
1925 while (loadedMapIt != loadedMap.cend()) {
1926 currentMap.insert(loadedMapIt.key(), loadedMapIt.value());
1929 config.insert(loadedIt.key(), currentMap);
1931 config.insert(loadedIt.key(), loadedIt.value());
1936 QVariantMap sessionConfig = loadedConfig.value(u
"server"_qs).toMap();
1938 applyConfig(sessionConfig);
1940 opt.insert(sessionConfig);
1942 loadingConfig =
false;
1944 if (!configToLoad.empty()) {
1949void ServerPrivate::applyConfig(
const QVariantMap &config)
1953 auto it = config.constBegin();
1954 while (it != config.constEnd()) {
1966 if (prop.
userType() == value.userType()) {
1969 prop.
write(q, currentValues + value.toStringList());
1971 prop.
write(q, value);
1977 prop.
write(q, value);
1984Protocol *ServerPrivate::getHttpProto()
2006Protocol *ServerPrivate::getFastCgiProto()
2015#include "moc_server.cpp"
2016#include "moc_server_p.cpp"
The Cutelyst application.
static QVariantMap loadJsonConfig(const QString &filename)
void setConfig(const QVariantMap &config)
static QVariantMap loadIniConfig(const QString &filename)
virtual bool init() override
void errorOccured(const QString &error)
bool start(Cutelyst::Application *app=nullptr)
int exec(Cutelyst::Application *app=nullptr)
void parseCommandLine(const QStringList &args)
Server(QObject *parent=nullptr)
QVariantMap config() const noexcept
The Cutelyst namespace holds all public Cutelyst API.
const char * constData() const const
QByteArray number(double n, char format, int precision)
QCommandLineOption addHelpOption()
bool addOption(const QCommandLineOption &option)
QCommandLineOption addVersionOption()
bool isSet(const QCommandLineOption &option) const const
void process(const QCoreApplication &app)
void setApplicationDescription(const QString &description)
void showHelp(int exitCode)
QString value(const QCommandLineOption &option) const const
QStringList values(const QCommandLineOption &option) const const
void addLibraryPath(const QString &path)
void setEventDispatcher(QAbstractEventDispatcher *eventDispatcher)
bool setCurrent(const QString &path)
bool removeOne(const AT &t)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void moveToThread(QThread *targetThread)
void setParent(QObject *parent)
QThread * thread() const const
int compare(QLatin1StringView s1, const QString &s2, Qt::CaseSensitivity cs)
QString fromLatin1(QByteArrayView str)
bool isEmpty() const const
QString number(double n, char format, int precision)
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
int toInt(bool *ok, int base) const const
QByteArray toLatin1() const const
qlonglong toLongLong(bool *ok, int base) const const
uint toUInt(bool *ok, int base) const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QThread * currentThread()
void setEventDispatcher(QAbstractEventDispatcher *eventDispatcher)
void start(QThread::Priority priority)
QStringList toStringList() const const