5 #include "localserver.h" 7 #include "protocolfastcgi.h" 8 #include "protocolhttp.h" 9 #include "protocolhttp2.h" 11 #include "serverengine.h" 13 #include "tcpserverbalancer.h" 16 # include "unixfork.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> 40 Q_LOGGING_CATEGORY(CUTELYST_SERVER,
"cutelyst.server", QtWarningMsg)
47 , d_ptr(new ServerPrivate(this))
51 if (!qEnvironmentVariableIsSet(
"QT_MESSAGE_PATTERN")) {
52 if (qEnvironmentVariableIsSet(
"JOURNAL_STREAM")) {
54 qSetMessagePattern(u
"%{category}[%{type}] %{message}"_s);
56 qSetMessagePattern(u
"%{pid}:%{threadid} %{category}[%{type}] %{message}"_s);
61 if (!qEnvironmentVariableIsSet(
"CUTELYST_QT_EVENT_LOOP")) {
62 qCInfo(CUTELYST_SERVER) <<
"Trying to install EPoll event loop";
67 auto cleanUp = [
this]() {
70 d->protoHTTP =
nullptr;
73 d->protoHTTP2 =
nullptr;
76 d->protoFCGI =
nullptr;
81 qDeleteAll(d->servers);
92 std::cout <<
"Cutelyst-Server terminated" << std::endl;
103 qtTrId(
"cutelystd-cli-desc"));
112 qtTrId(
"cutelystd-opt-ini-desc"),
115 qtTrId(
"cutelystd-opt-value-file"));
123 qtTrId(
"cutelystd-opt-json-desc"),
124 qtTrId(
"cutelystd-opt-value-file"));
128 QStringLiteral(
"chdir"),
131 qtTrId(
"cutelystd-opt-chdir-desc"),
134 qtTrId(
"cutelystd-opt-value-directory"));
138 QStringLiteral(
"chdir2"),
141 qtTrId(
"cutelystd-opt-chdir2-desc"),
142 qtTrId(
"cutelystd-opt-value-directory"));
146 QStringLiteral(
"lazy"),
149 qtTrId(
"cutelystd-opt-lazy-desc"));
155 qtTrId(
"cutelystd-opt-application-desc"),
156 qtTrId(
"cutelystd-opt-value-file"));
163 qtTrId(
"cutelystd-opt-threads-desc"),
166 qtTrId(
"cutelystd-opt-threads-value"));
174 qtTrId(
"cutelystd-opt-processes-desc"),
177 qtTrId(
"cutelystd-opt-processes-value"));
184 qtTrId(
"cutelystd-opt-master-desc"));
190 qtTrId(
"cutelystd-opt-listen-desc"),
193 qtTrId(
"cutelystd-opt-value-size"));
199 qtTrId(
"cutelystd-opt-buffer-size-desc"),
202 qtTrId(
"cutelystd-opt-value-bytes"));
210 qtTrId(
"cutelystd-opt-post-buffering-desc"),
211 qtTrId(
"cutelystd-opt-value-bytes"));
215 QStringLiteral(
"post-buffering-bufsize"),
218 qtTrId(
"cutelystd-opt-post-buffering-bufsize-desc"),
219 qtTrId(
"cutelystd-opt-value-bytes"));
222 QCommandLineOption httpSocketOpt({QStringLiteral(
"http-socket"), QStringLiteral(
"h1")},
225 qtTrId(
"cutelystd-opt-http-socket-desc"),
228 qtTrId(
"cutelystd-opt-value-address"));
232 {QStringLiteral(
"http2-socket"), QStringLiteral(
"h2")},
235 qtTrId(
"cutelystd-opt-http2-socket-desc"),
236 qtTrId(
"cutelystd-opt-value-address"));
242 qtTrId(
"cutelystd-opt-http2-header-table-size-desc"),
243 qtTrId(
"cutelystd-opt-value-size"));
244 parser.
addOption(http2HeaderTableSizeOpt);
249 qtTrId(
"cutelystd-opt-upgrade-h2c-desc"));
255 qtTrId(
"cutelystd-opt-https-h2-desc"));
258 QCommandLineOption httpsSocketOpt({QStringLiteral(
"https-socket"), QStringLiteral(
"hs1")},
261 qtTrId(
"cutelystd-opt-https-socket-desc"),
263 qtTrId(
"cutelystd-opt-value-httpsaddress"));
267 QStringLiteral(
"fastcgi-socket"),
270 qtTrId(
"cutelystd-opt-fastcgi-socket-desc"),
271 qtTrId(
"cutelystd-opt-value-address"));
275 QStringLiteral(
"socket-access"),
278 qtTrId(
"cutelystd-opt-socket-access-desc"),
281 qtTrId(
"cutelystd-opt-socket-access-value"));
284 QCommandLineOption socketTimeout({QStringLiteral(
"socket-timeout"), QStringLiteral(
"z")},
287 qtTrId(
"cutelystd-opt-socket-timeout-desc"),
290 qtTrId(
"cutelystd-opt-socket-timeout-value"));
299 qtTrId(
"cutelystd-opt-static-map-desc"),
302 qtTrId(
"cutelystd-opt-value-static-map"));
309 qtTrId(
"cutelystd-opt-static-map2-desc"),
312 qtTrId(
"cutelystd-opt-value-static-map"));
319 qtTrId(
"cutelystd-opt-auto-restart-desc"));
323 QStringLiteral(
"touch-reload"),
327 qtTrId(
"cutelystd-opt-touch-reload-desc"),
328 qtTrId(
"cutelystd-opt-value-file"));
334 qtTrId(
"cutelystd-opt-tcp-nodelay-desc"));
340 qtTrId(
"cutelystd-opt-so-keepalive-desc"));
347 qtTrId(
"cutelystd-opt-socket-sndbuf-desc"),
348 qtTrId(
"cutelystd-opt-value-bytes"));
355 qtTrId(
"cutelystd-opt-socket-rcvbuf-desc"),
356 qtTrId(
"cutelystd-opt-value-bytes"));
363 qtTrId(
"cutelystd-opt-websocket-max-size-desc"),
366 qtTrId(
"cutelystd-opt-websocket-max-size-value"));
372 qtTrId(
"cutelystd-opt-pidfile-desc"),
375 qtTrId(
"cutelystd-opt-value-pidfile"));
381 qtTrId(
"cutelystd-opt-pidfile2-desc"),
382 qtTrId(
"cutelystd-opt-value-pidfile"));
389 qtTrId(
"cutelystd-opt-stop-desc"),
390 qtTrId(
"cutelystd-opt-value-pidfile"));
396 qtTrId(
"cutelystd-opt-uid-desc"),
399 qtTrId(
"cutelystd-opt-uid-value"));
405 qtTrId(
"cutelystd-opt-gid-desc"),
408 qtTrId(
"cutelystd-opt-gid-value"));
414 qtTrId(
"cutelystd-opt-no-init-groups-desc"));
420 qtTrId(
"cutelystd-opt-chown-socket-desc"),
423 qtTrId(
"cutelystd-opt-chown-socket-value"));
429 qtTrId(
"cutelystd-opt-umask-desc"),
432 qtTrId(
"cutelystd-opt-umask-value"));
436 QStringLiteral(
"cpu-affinity"),
439 qtTrId(
"cutelystd-opt-cpu-affinity-desc"),
442 qtTrId(
"cutelystd-opt-cpu-affinity-value"));
450 qtTrId(
"cutelystd-opt-reuse-port-desc"));
455 QStringLiteral(
"experimental-thread-balancer"),
458 qtTrId(
"cutelystd-opt-experimental-thread-balancer-desc"));
464 qtTrId(
"cutelystd-opt-using-frontend-proxy-desc"));
470 setIni(parser.
values(iniOpt));
472 setJson(parser.
values(jsonOpt));
474 if (parser.
isSet(chdir)) {
475 setChdir(parser.
value(chdir));
478 if (parser.
isSet(chdir2)) {
479 setChdir2(parser.
value(chdir2));
486 if (parser.
isSet(socketAccess)) {
487 setSocketAccess(parser.
value(socketAccess));
490 if (parser.
isSet(socketTimeout)) {
492 auto size = parser.
value(socketTimeout).
toInt(&ok);
493 setSocketTimeout(size);
494 if (!ok || size < 0) {
499 if (parser.
isSet(pidfileOpt)) {
500 setPidfile(parser.
value(pidfileOpt));
503 if (parser.
isSet(pidfile2Opt)) {
504 setPidfile2(parser.
value(pidfile2Opt));
508 if (parser.
isSet(stopOption)) {
509 UnixFork::stopWSGI(parser.
value(stopOption));
516 if (parser.
isSet(uidOption)) {
517 setUid(parser.
value(uidOption));
520 if (parser.
isSet(gidOption)) {
521 setGid(parser.
value(gidOption));
524 if (parser.
isSet(noInitgroupsOption)) {
525 setNoInitgroups(
true);
528 if (parser.
isSet(chownSocketOption)) {
529 setChownSocket(parser.
value(chownSocketOption));
532 if (parser.
isSet(umaskOption)) {
533 setUmask(parser.
value(umaskOption));
536 if (parser.
isSet(cpuAffinityOption)) {
538 auto value = parser.
value(cpuAffinityOption).
toInt(&ok);
539 setCpuAffinity(value);
540 if (!ok || value < 0) {
547 if (parser.
isSet(reusePortOption)) {
552 if (parser.
isSet(lazyOption)) {
556 if (parser.
isSet(listenQueue)) {
558 auto size = parser.
value(listenQueue).
toInt(&ok);
559 setListenQueue(size);
560 if (!ok || size < 1) {
565 if (parser.
isSet(bufferSize)) {
567 auto size = parser.
value(bufferSize).
toInt(&ok);
569 if (!ok || size < 1) {
574 if (parser.
isSet(postBuffering)) {
577 setPostBuffering(size);
578 if (!ok || size < 1) {
583 if (parser.
isSet(postBufferingBufsize)) {
586 setPostBufferingBufsize(size);
587 if (!ok || size < 1) {
600 if (parser.
isSet(autoReload)) {
604 if (parser.
isSet(tcpNoDelay)) {
608 if (parser.
isSet(soKeepAlive)) {
609 setSoKeepalive(
true);
612 if (parser.
isSet(upgradeH2cOpt)) {
616 if (parser.
isSet(httpsH2Opt)) {
620 if (parser.
isSet(socketSndbuf)) {
622 auto size = parser.
value(socketSndbuf).
toInt(&ok);
623 setSocketSndbuf(size);
624 if (!ok || size < 1) {
629 if (parser.
isSet(socketRcvbuf)) {
631 auto size = parser.
value(socketRcvbuf).
toInt(&ok);
632 setSocketRcvbuf(size);
633 if (!ok || size < 1) {
638 if (parser.
isSet(wsMaxSize)) {
640 auto size = parser.
value(wsMaxSize).
toInt(&ok);
641 setWebsocketMaxSize(size);
642 if (!ok || size < 1) {
647 if (parser.
isSet(http2HeaderTableSizeOpt)) {
649 auto size = parser.
value(http2HeaderTableSizeOpt).
toUInt(&ok);
650 setHttp2HeaderTableSize(size);
651 if (!ok || size < 1) {
656 if (parser.
isSet(frontendProxy)) {
657 setUsingFrontendProxy(
true);
660 setHttpSocket(httpSocket() + parser.
values(httpSocketOpt));
662 setHttp2Socket(http2Socket() + parser.
values(http2SocketOpt));
664 setHttpsSocket(httpsSocket() + parser.
values(httpsSocketOpt));
666 setFastcgiSocket(fastcgiSocket() + parser.
values(fastcgiSocketOpt));
668 setStaticMap(staticMap() + parser.
values(staticMapOpt));
670 setStaticMap2(staticMap2() + parser.
values(staticMap2Opt));
672 setTouchReload(touchReload() + parser.
values(touchReloadOpt));
674 d->threadBalancer = parser.
isSet(threadBalancerOpt);
680 std::cout <<
"Cutelyst-Server starting" << std::endl;
682 if (!qEnvironmentVariableIsSet(
"CUTELYST_SERVER_IGNORE_MASTER") && !d->master) {
684 <<
"*** WARNING: you are running Cutelyst-Server without its master process manager ***" 689 if (d->processes == -1 && d->threads == -1) {
690 d->processes = UnixFork::idealProcessCount();
691 d->threads = UnixFork::idealThreadCount() / d->processes;
692 }
else if (d->processes == -1) {
693 d->processes = UnixFork::idealThreadCount();
694 }
else if (d->threads == -1) {
695 d->threads = UnixFork::idealThreadCount();
698 if (d->processes == 0 && d->master) {
701 d->genericFork =
new UnixFork(d->processes, qMax(d->threads, 1), !d->userEventLoop,
this);
703 if (d->processes == -1) {
706 if (d->threads == -1) {
717 if (d->master && d->lazy) {
718 if (d->autoReload && !d->application.isEmpty()) {
719 d->touchReload.append(d->application);
721 d->genericFork->setTouchReload(d->touchReload);
725 if (d->master && !d->genericFork->continueMaster(&ret)) {
730 if (systemdNotify::is_systemd_notify_available()) {
732 sd->setWatchdog(
true, systemdNotify::sd_watchdog_enabled(
true));
734 sd->sendStatus(qApp->applicationName().toLatin1() +
" is ready");
737 connect(d, &ServerPrivate::postForked, sd, [sd] { sd->setWatchdog(
false); });
738 qInfo(CUTELYST_SERVER) <<
"systemd notify detected";
746 if (!d->listenTcpSockets()) {
753 if (!d->writePidFile(d->pidfile)) {
759 bool isListeningLocalSockets =
false;
760 if (!d->chownSocket.isEmpty()) {
761 if (!d->listenLocalSockets()) {
766 isListeningLocalSockets =
true;
769 if (!d->umask.isEmpty() && !UnixFork::setUmask(d->umask.toLatin1())) {
773 if (!UnixFork::setGidUid(d->gid, d->uid, d->noInitgroups)) {
779 if (!isListeningLocalSockets) {
781 d->listenLocalSockets();
787 if (!d->listenTcpSockets()) {
793 if (d->servers.empty()) {
794 std::cout <<
"Please specify a socket to listen to" << std::endl;
800 d->writePidFile(d->pidfile2);
802 if (!d->chdir.isEmpty()) {
803 std::cout <<
"Changing directory to: " << d->chdir.toLatin1().constData() << std::endl;
814 if (!d->setupApplication()) {
821 if (d->userEventLoop) {
826 ret = d->genericFork->exec(d->lazy, d->master);
844 d->userEventLoop =
true;
849 qputenv(
"CUTELYST_SERVER_IGNORE_MASTER", QByteArrayLiteral(
"1"));
851 if (
exec(app) == 0) {
861 if (d->userEventLoop) {
866 ServerPrivate::~ServerPrivate()
873 bool ServerPrivate::listenTcpSockets()
875 if (httpSockets.isEmpty() && httpsSockets.isEmpty() && http2Sockets.isEmpty() &&
876 fastcgiSockets.isEmpty()) {
882 for (
const auto &socket : qAsConst(httpSockets)) {
883 if (!listenTcp(socket, getHttpProto(),
false)) {
889 for (
const auto &socket : qAsConst(httpsSockets)) {
890 if (!listenTcp(socket, getHttpProto(),
true)) {
896 for (
const auto &socket : qAsConst(http2Sockets)) {
897 if (!listenTcp(socket, getHttp2Proto(),
false)) {
903 for (
const auto &socket : qAsConst(fastcgiSockets)) {
904 if (!listenTcp(socket, getFastCgiProto(),
false)) {
912 bool ServerPrivate::listenTcp(
const QString &line,
Protocol *protocol,
bool secure)
919 server->setBalancer(threadBalancer);
920 ret = server->listen(line, protocol, secure);
922 if (ret && server->socketDescriptor()) {
923 auto qEnum = protocol->staticMetaObject.enumerator(0);
924 std::cout << qEnum.valueToKey(static_cast<int>(protocol->type())) <<
" socket " 926 <<
" bound to TCP address " << server->serverName().constData() <<
" fd " 935 bool ServerPrivate::listenLocalSockets()
944 std::vector<int> fds = systemdNotify::listenFds();
947 if (server->listen(fd)) {
948 const QString name = server->serverName();
949 const QString fullName = server->fullServerName();
953 protocol = getHttpProto();
955 protocol = getHttp2Proto();
957 protocol = getFastCgiProto();
959 std::cerr <<
"systemd activated socket does not match any configured socket" 963 server->setProtocol(protocol);
964 server->pauseAccepting();
966 auto qEnum = protocol->staticMetaObject.enumerator(0);
967 std::cout << qEnum.valueToKey(static_cast<int>(protocol->type())) <<
" socket " 969 <<
" bound to LOCAL address " << qPrintable(fullName) <<
" fd " 973 std::cerr <<
"Failed to listen on activated LOCAL FD: " 975 << qPrintable(server->errorString()) << std::endl;
982 const auto httpConst = http;
983 for (
const auto &socket : httpConst) {
984 ret |= listenLocal(socket, getHttpProto());
987 const auto http2Const = http2;
988 for (
const auto &socket : http2Const) {
989 ret |= listenLocal(socket, getHttp2Proto());
992 const auto fastcgiConst = fastcgi;
993 for (
const auto &socket : fastcgiConst) {
994 ret |= listenLocal(socket, getFastCgiProto());
1000 bool ServerPrivate::listenLocal(
const QString &line,
Protocol *protocol)
1007 server->setProtocol(protocol);
1008 if (!socketAccess.isEmpty()) {
1010 if (socketAccess.contains(u
'u')) {
1014 if (socketAccess.contains(u
'g')) {
1018 if (socketAccess.contains(u
'o')) {
1021 server->setSocketOptions(options);
1023 server->removeServer(line);
1024 ret = server->listen(line);
1025 server->pauseAccepting();
1027 if (!ret || !server->socket()) {
1028 std::cerr <<
"Failed to listen on LOCAL: " << qPrintable(line) <<
" : " 1029 << qPrintable(server->errorString()) << std::endl;
1034 if (!chownSocket.isEmpty()) {
1035 UnixFork::chownSocket(line, chownSocket);
1038 auto qEnum = protocol->staticMetaObject.enumerator(0);
1039 std::cout << qEnum.valueToKey(static_cast<int>(protocol->type())) <<
" socket " 1041 <<
" bound to LOCAL address " << qPrintable(line) <<
" fd " 1049 void Server::setApplication(
const QString &application)
1054 if (loader.fileName().isEmpty()) {
1055 d->application = application;
1059 d->application = loader.fileName();
1064 QString Server::application()
const 1067 return d->application;
1070 void Server::setThreads(
const QString &threads)
1076 d->threads = qMax(1, threads.
toInt());
1081 QString Server::threads()
const 1084 if (d->threads == -1) {
1085 return QStringLiteral(
"auto");
1090 void Server::setProcesses(
const QString &process)
1097 d->processes = process.
toInt();
1103 QString Server::processes()
const 1106 if (d->processes == -1) {
1107 return QStringLiteral(
"auto");
1112 void Server::setChdir(
const QString &chdir)
1125 void Server::setHttpSocket(
const QStringList &httpSocket)
1128 d->httpSockets = httpSocket;
1135 return d->httpSockets;
1138 void Server::setHttp2Socket(
const QStringList &http2Socket)
1141 d->http2Sockets = http2Socket;
1148 return d->http2Sockets;
1151 void Server::setHttp2HeaderTableSize(quint32 headerTableSize)
1154 d->http2HeaderTableSize = headerTableSize;
1158 quint32 Server::http2HeaderTableSize()
const 1161 return d->http2HeaderTableSize;
1164 void Server::setUpgradeH2c(
bool enable)
1167 d->upgradeH2c = enable;
1171 bool Server::upgradeH2c()
const 1174 return d->upgradeH2c;
1177 void Server::setHttpsH2(
bool enable)
1180 d->httpsH2 = enable;
1184 bool Server::httpsH2()
const 1190 void Server::setHttpsSocket(
const QStringList &httpsSocket)
1193 d->httpsSockets = httpsSocket;
1200 return d->httpsSockets;
1203 void Server::setFastcgiSocket(
const QStringList &fastcgiSocket)
1206 d->fastcgiSockets = fastcgiSocket;
1213 return d->fastcgiSockets;
1216 void Server::setSocketAccess(
const QString &socketAccess)
1219 d->socketAccess = socketAccess;
1223 QString Server::socketAccess()
const 1226 return d->socketAccess;
1229 void Server::setSocketTimeout(
int timeout)
1232 d->socketTimeout = timeout;
1236 int Server::socketTimeout()
const 1239 return d->socketTimeout;
1242 void Server::setChdir2(
const QString &chdir2)
1249 QString Server::chdir2()
const 1258 d->ini.append(files);
1259 d->ini.removeDuplicates();
1262 for (
const QString &file : files) {
1263 if (!d->configLoaded.contains(file)) {
1264 auto fileToLoad = std::make_pair(file, ServerPrivate::ConfigFormat::Ini);
1265 if (!d->configToLoad.contains(fileToLoad)) {
1266 qCDebug(CUTELYST_SERVER) <<
"Enqueue INI config file:" << file;
1267 d->configToLoad.enqueue(fileToLoad);
1284 d->json.append(files);
1285 d->json.removeDuplicates();
1288 for (
const QString &file : files) {
1289 if (!d->configLoaded.contains(file)) {
1290 auto fileToLoad = std::make_pair(file, ServerPrivate::ConfigFormat::Json);
1291 if (!d->configToLoad.contains(fileToLoad)) {
1292 qCDebug(CUTELYST_SERVER) <<
"Enqueue JSON config file:" << file;
1293 d->configToLoad.enqueue(fileToLoad);
1307 void Server::setStaticMap(
const QStringList &staticMap)
1310 d->staticMaps = staticMap;
1317 return d->staticMaps;
1320 void Server::setStaticMap2(
const QStringList &staticMap)
1323 d->staticMaps2 = staticMap;
1330 return d->staticMaps2;
1333 void Server::setMaster(
bool enable)
1336 if (!qEnvironmentVariableIsSet(
"CUTELYST_SERVER_IGNORE_MASTER")) {
1342 bool Server::master()
const 1348 void Server::setAutoReload(
bool enable)
1352 d->autoReload =
true;
1357 bool Server::autoReload()
const 1360 return d->autoReload;
1363 void Server::setTouchReload(
const QStringList &files)
1366 d->touchReload = files;
1373 return d->touchReload;
1376 void Server::setListenQueue(
int size)
1379 d->listenQueue = size;
1383 int Server::listenQueue()
const 1386 return d->listenQueue;
1389 void Server::setBufferSize(
int size)
1393 qCWarning(CUTELYST_SERVER) <<
"Buffer size must be at least 4096 bytes, ignoring";
1396 d->bufferSize = size;
1400 int Server::bufferSize()
const 1403 return d->bufferSize;
1406 void Server::setPostBuffering(qint64 size)
1409 d->postBuffering = size;
1413 qint64 Server::postBuffering()
const 1416 return d->postBuffering;
1419 void Server::setPostBufferingBufsize(qint64 size)
1423 qCWarning(CUTELYST_SERVER) <<
"Post buffer size must be at least 4096 bytes, ignoring";
1426 d->postBufferingBufsize = size;
1430 qint64 Server::postBufferingBufsize()
const 1433 return d->postBufferingBufsize;
1436 void Server::setTcpNodelay(
bool enable)
1439 d->tcpNodelay = enable;
1443 bool Server::tcpNodelay()
const 1446 return d->tcpNodelay;
1449 void Server::setSoKeepalive(
bool enable)
1452 d->soKeepalive = enable;
1456 bool Server::soKeepalive()
const 1459 return d->soKeepalive;
1462 void Server::setSocketSndbuf(
int value)
1465 d->socketSendBuf = value;
1469 int Server::socketSndbuf()
const 1472 return d->socketSendBuf;
1475 void Server::setSocketRcvbuf(
int value)
1478 d->socketReceiveBuf = value;
1482 int Server::socketRcvbuf()
const 1485 return d->socketReceiveBuf;
1488 void Server::setWebsocketMaxSize(
int value)
1491 d->websocketMaxSize = value * 1024;
1495 int Server::websocketMaxSize()
const 1498 return d->websocketMaxSize / 1024;
1501 void Server::setPidfile(
const QString &file)
1508 QString Server::pidfile()
const 1514 void Server::setPidfile2(
const QString &file)
1521 QString Server::pidfile2()
const 1527 void Server::setUid(
const QString &uid)
1542 void Server::setGid(
const QString &gid)
1557 void Server::setNoInitgroups(
bool enable)
1561 d->noInitgroups = enable;
1566 bool Server::noInitgroups()
const 1569 return d->noInitgroups;
1572 void Server::setChownSocket(
const QString &chownSocket)
1576 d->chownSocket = chownSocket;
1581 QString Server::chownSocket()
const 1584 return d->chownSocket;
1587 void Server::setUmask(
const QString &value)
1602 void Server::setCpuAffinity(
int value)
1606 d->cpuAffinity = value;
1611 int Server::cpuAffinity()
const 1614 return d->cpuAffinity;
1617 void Server::setReusePort(
bool enable)
1621 d->reusePort = enable;
1628 bool Server::reusePort()
const 1631 return d->reusePort;
1634 void Server::setLazy(
bool enable)
1641 bool Server::lazy()
const 1647 void Server::setUsingFrontendProxy(
bool enable)
1650 d->usingFrontendProxy = enable;
1654 bool Server::usingFrontendProxy()
const 1657 return d->usingFrontendProxy;
1666 bool ServerPrivate::setupApplication()
1673 std::cout <<
"Loading application: " << application.
toLatin1().
constData() << std::endl;
1676 if (!loader.load()) {
1677 qCCritical(CUTELYST_SERVER) <<
"Could not load application:" << loader.errorString();
1681 QObject *instance = loader.instance();
1683 qCCritical(CUTELYST_SERVER) <<
"Could not get a QObject instance: %s\n" 1684 << loader.errorString();
1690 qCCritical(CUTELYST_SERVER)
1691 <<
"Could not cast Cutelyst::Application from instance: %s\n" 1692 << loader.errorString();
1703 if (!chdir2.isEmpty()) {
1704 std::cout <<
"Changing directory2 to: " << chdir2.toLatin1().constData() << std::endl;
1713 engine = createEngine(localApp, 0);
1714 for (
int i = 1; i < threads; ++i) {
1715 if (createEngine(localApp, i)) {
1716 ++workersNotRunning;
1720 engine = createEngine(localApp, 0);
1721 workersNotRunning = 1;
1725 std::cerr <<
"Application failed to init, cheaping..." << std::endl;
1732 void ServerPrivate::engineShutdown(
ServerEngine *engine)
1734 const auto engineThread = engine->
thread();
1737 engines.erase(std::remove(engines.begin(), engines.end(), engine), engines.end());
1738 checkEngineShutdown();
1740 engineThread->quit();
1742 engines.erase(std::remove(engines.begin(), engines.end(), engine), engines.end());
1745 checkEngineShutdown();
1748 void ServerPrivate::checkEngineShutdown()
1750 if (engines.empty()) {
1751 if (userEventLoop) {
1753 Q_EMIT q->stopped();
1760 void ServerPrivate::workerStarted()
1765 if (--workersNotRunning == 0) {
1770 bool ServerPrivate::postFork(
int workerId)
1775 if (!setupApplication()) {
1776 Q_EMIT q->errorOccured(qtTrId(
"cutelystd-err-fail-setup-app"));
1781 if (engines.size() > 1) {
1782 qCDebug(CUTELYST_SERVER) <<
"Starting threads";
1787 if (thread != qApp->thread()) {
1789 if (!qEnvironmentVariableIsSet(
"CUTELYST_QT_EVENT_LOOP")) {
1798 Q_EMIT postForked(workerId);
1806 qApp->processEvents();
1812 bool ServerPrivate::writePidFile(
const QString &filename)
1818 QFile file(filename);
1820 std::cerr <<
"Failed write pid file " << qPrintable(filename) << std::endl;
1824 std::cout <<
"Writing pidfile to " << qPrintable(filename) << std::endl;
1835 if (workerCore > 0) {
1838 qFatal(
"*** FATAL *** Could not create a NEW instance of your Cutelyst::Application, " 1839 "make sure your constructor has Q_INVOKABLE macro or disable threaded mode.");
1843 auto engine =
new ServerEngine(app, workerCore, opt, q);
1848 &ServerEngine::shutdownCompleted,
1850 &ServerPrivate::engineShutdown,
1856 engine->setServers(servers);
1857 if (!engine->
init()) {
1858 std::cerr <<
"Application failed to init(), cheaping core: " << workerCore << std::endl;
1863 engines.push_back(engine);
1866 if (workerCore > 0) {
1871 auto thread =
new QThread(
this);
1880 void ServerPrivate::loadConfig()
1882 if (loadingConfig) {
1886 loadingConfig =
true;
1888 if (configToLoad.isEmpty()) {
1889 loadingConfig =
false;
1893 auto fileToLoad = configToLoad.dequeue();
1895 if (fileToLoad.first.isEmpty()) {
1896 qCWarning(CUTELYST_SERVER) <<
"Can not load config from empty config file name";
1897 loadingConfig =
false;
1901 if (configLoaded.contains(fileToLoad.first)) {
1902 loadingConfig =
false;
1906 configLoaded.append(fileToLoad.first);
1908 QVariantMap loadedConfig;
1909 switch (fileToLoad.second) {
1910 case ConfigFormat::Ini:
1911 qCInfo(CUTELYST_SERVER) <<
"Loading INI configuratin:" << fileToLoad.first;
1914 case ConfigFormat::Json:
1915 qCInfo(CUTELYST_SERVER) <<
"Loading JSON configuration:" << fileToLoad.first;
1920 auto loadedIt = loadedConfig.cbegin();
1921 while (loadedIt != loadedConfig.cend()) {
1922 if (config.contains(loadedIt.key())) {
1923 QVariantMap currentMap = config.value(loadedIt.key()).toMap();
1924 const QVariantMap loadedMap = loadedIt.value().toMap();
1925 auto loadedMapIt = loadedMap.cbegin();
1926 while (loadedMapIt != loadedMap.cend()) {
1927 currentMap.insert(loadedMapIt.key(), loadedMapIt.value());
1930 config.insert(loadedIt.key(), currentMap);
1932 config.insert(loadedIt.key(), loadedIt.value());
1937 QVariantMap sessionConfig = loadedConfig.value(u
"server"_s).toMap();
1939 applyConfig(sessionConfig);
1941 opt.insert(sessionConfig);
1943 loadingConfig =
false;
1945 if (!configToLoad.empty()) {
1950 void ServerPrivate::applyConfig(
const QVariantMap &config)
1954 auto it = config.constBegin();
1955 while (it != config.constEnd()) {
1967 if (prop.
userType() == value.userType()) {
1970 prop.
write(q, currentValues + value.toStringList());
1972 prop.
write(q, value);
1978 prop.
write(q, value);
1985 Protocol *ServerPrivate::getHttpProto()
2007 Protocol *ServerPrivate::getFastCgiProto()
2016 #include "moc_server.cpp" 2017 #include "moc_server_p.cpp" QFuture< ArgsType< Signal >> connect(Sender *sender, Signal signal)
void setConfig(const QVariantMap &config)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
int exec(Cutelyst::Application *app=nullptr)
void moveToThread(QThread *targetThread)
QCommandLineOption addVersionOption()
virtual bool init() override
QThread * thread() const const
void setEventDispatcher(QAbstractEventDispatcher *eventDispatcher)
static QVariantMap loadIniConfig(const QString &filename)
void addLibraryPath(const QString &path)
QVariantMap config() const noexcept
void start(QThread::Priority priority)
QString number(double n, char format, int precision)
QByteArray number(double n, char format, int precision)
void setEventDispatcher(QAbstractEventDispatcher *eventDispatcher)
bool isSet(const QCommandLineOption &option) const const
QStringList values(const QCommandLineOption &option) const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QString value(const QCommandLineOption &option) const const
int toInt(bool *ok, int base) const const
bool isEmpty() const const
bool start(Cutelyst::Application *app=nullptr)
const char * constData() const const
QCommandLineOption addHelpOption()
bool setCurrent(const QString &path)
void setApplicationDescription(const QString &description)
static QVariantMap loadJsonConfig(const QString &filename)
The Cutelyst namespace holds all public Cutelyst API.
void errorOccured(const QString &error)
void push_back(QByteArrayView str)
void parseCommandLine(const QStringList &args)
void setParent(QObject *parent)
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
QString fromLatin1(QByteArrayView str)
QByteArray toLatin1() const const
QStringList toStringList() const const
void showHelp(int exitCode)
QThread * currentThread()
bool addOption(const QCommandLineOption &option)
The Cutelyst application.
int compare(QLatin1StringView s1, const QString &s2, Qt::CaseSensitivity cs)
void process(const QCoreApplication &app)
bool removeOne(const AT &t)
qlonglong toLongLong(bool *ok, int base) const const
QString applicationName()
uint toUInt(bool *ok, int base) const const
Server(QObject *parent=nullptr)