5 #include "application_p.h"
9 #include "controller.h"
10 #include "controller_p.h"
11 #include "dispatchtype.h"
14 #include "request_p.h"
16 #include "response_p.h"
21 #include <QJsonDocument>
22 #include <QtCore/QCoreApplication>
23 #include <QtCore/QDataStream>
24 #include <QtCore/QDir>
25 #include <QtCore/QFileInfo>
26 #include <QtCore/QLocale>
27 #include <QtCore/QPluginLoader>
28 #include <QtCore/QStringList>
29 #include <QtCore/QTranslator>
31 Q_LOGGING_CATEGORY(CUTELYST_DISPATCHER,
"cutelyst.dispatcher", QtWarningMsg)
32 Q_LOGGING_CATEGORY(CUTELYST_DISPATCHER_PATH,
"cutelyst.dispatcher.path", QtWarningMsg)
33 Q_LOGGING_CATEGORY(CUTELYST_DISPATCHER_CHAINED,
"cutelyst.dispatcher.chained", QtWarningMsg)
34 Q_LOGGING_CATEGORY(CUTELYST_CONTROLLER,
"cutelyst.controller", QtWarningMsg)
35 Q_LOGGING_CATEGORY(CUTELYST_CORE,
"cutelyst.core", QtWarningMsg)
36 Q_LOGGING_CATEGORY(CUTELYST_ENGINE,
"cutelyst.engine", QtWarningMsg)
37 Q_LOGGING_CATEGORY(CUTELYST_UPLOAD,
"cutelyst.upload", QtWarningMsg)
38 Q_LOGGING_CATEGORY(CUTELYST_MULTIPART,
"cutelyst.multipart", QtWarningMsg)
39 Q_LOGGING_CATEGORY(CUTELYST_VIEW,
"cutelyst.view", QtWarningMsg)
40 Q_LOGGING_CATEGORY(CUTELYST_REQUEST,
"cutelyst.request", QtWarningMsg)
41 Q_LOGGING_CATEGORY(CUTELYST_RESPONSE,
"cutelyst.response", QtWarningMsg)
42 Q_LOGGING_CATEGORY(CUTELYST_STATS,
"cutelyst.stats", QtWarningMsg)
43 Q_LOGGING_CATEGORY(CUTELYST_COMPONENT,
"cutelyst.component", QtWarningMsg)
49 , d_ptr(new ApplicationPrivate)
55 qRegisterMetaType<ParamsMultiMap>();
69 qCDebug(CUTELYST_CORE) <<
"Default Application::init called on pid:"
76 qCDebug(CUTELYST_CORE) <<
"Default Application::postFork called on pid:"
90 d->headers.setHeader(
"X-Cutelyst"_qba, QByteArrayLiteral(CUTELYST_VERSION));
96 if (d->plugins.contains(
plugin)) {
107 if (d->controllersHash.contains(name)) {
110 d->controllersHash.insert(name, controller);
111 d->controllers.append(controller);
118 if (d->views.contains(
view->
name())) {
120 <<
"There is already a view with this name:" <<
view->
name();
141 auto it = d->factories.constFind(name);
142 if (it != d->factories.constEnd()) {
152 qgetenv(
"CUTELYST_PLUGINS_DIR").split(
';');
159 qCDebug(CUTELYST_CORE) <<
"Did not find plugin" << name <<
"on" << dirs <<
"for" <<
parent;
166 return CUTELYST_VERSION;
172 return d->controllers;
178 return d->views.value(name);
184 auto it = d->config.constFind(key);
185 if (it != d->config.constEnd()) {
194 return d->dispatcher;
200 return d->dispatcher->dispatchers();
223 QDir home = config(u
"home"_qs).toString();
242 d->config.insert(key, value);
254 d->useStats = CUTELYST_STATS().isDebugEnabled();
268 const auto plugins = d->plugins;
270 if (
plugin->objectName().isEmpty()) {
278 if (zeroCore && !tablePlugins.
isEmpty()) {
279 qCDebug(CUTELYST_CORE)
286 tableDataHandlers.
append({u
"application/x-www-form-urlencoded"_qs});
287 tableDataHandlers.
append({u
"application/json"_qs});
288 tableDataHandlers.
append({u
"multipart/form-data"_qs});
289 qCDebug(CUTELYST_CORE)
290 << Utils::buildTable(tableDataHandlers,
295 qCDebug(CUTELYST_CORE) <<
"Loaded dispatcher"
297 qCDebug(CUTELYST_CORE)
301 QString home = d->config.value(u
"home"_qs).toString();
304 qCDebug(CUTELYST_CORE) <<
"Couldn't find home";
308 if (homeInfo.
isDir()) {
310 qCDebug(CUTELYST_CORE) <<
"Found home" << home;
314 qCDebug(CUTELYST_CORE) <<
"Home" << home <<
"doesn't exist";
320 QStringList controllerNames = d->controllersHash.keys();
321 controllerNames.
sort();
322 for (
const QString &controller : controllerNames) {
326 const auto views = d->views;
336 if (zeroCore && !table.
isEmpty()) {
337 qCDebug(CUTELYST_CORE)
338 << Utils::buildTable(table,
346 controller->d_ptr->init(
this, d->dispatcher);
349 d->dispatcher->setupActions(d->controllers, d->dispatchers, d->engine->workerCore() == 0);
352 qCInfo(CUTELYST_CORE) << qPrintable(
373 auto priv =
new ContextPrivate(
this,
engine, d->dispatcher, d->plugins);
377 priv->engineRequest = request;
378 priv->response =
new Response(d->headers, request);
379 priv->request =
new Request(request);
380 priv->locale = d->defaultLocale;
383 priv->stats =
new Stats(request);
387 bool skipMethod =
false;
391 static bool log = CUTELYST_REQUEST().isEnabled(QtDebugMsg);
393 d->logRequest(priv->request);
396 d->dispatcher->prepareAction(c);
400 d->dispatcher->dispatch(c);
402 if (request->
status & EngineRequest::Async) {
422 if (!controller->postFork(
this)) {
435 Q_ASSERT_X(translator,
"add translator to application",
"invalid QTranslator object");
436 auto it = d->translators.find(locale);
437 if (it != d->translators.end()) {
438 it.value().prepend(translator);
452 Q_ASSERT_X(!translators.
empty(),
"add translators to application",
"empty translators vector");
453 auto transIt = d->translators.find(locale);
454 if (transIt != d->translators.end()) {
455 for (
auto it = translators.
crbegin(); it != translators.
crend(); ++it) {
456 transIt.value().prepend(*it);
459 d->translators.insert(locale, translators);
463 static void replacePercentN(
QString *result,
int n)
468 while ((percentPos = result->
indexOf(u
'%', percentPos + len)) != -1) {
471 if (result->
at(percentPos + len) == u
'L') {
473 fmt = QStringLiteral(
"%L1");
475 fmt = QStringLiteral(
"%1");
477 if (result->
at(percentPos + len) == u
'n') {
480 result->
replace(percentPos, len, fmt);
489 const char *sourceText,
490 const char *disambiguation,
502 if (translators.
empty()) {
504 replacePercentN(&result, n);
509 result = translator->translate(context, sourceText, disambiguation, n);
519 replacePercentN(&result, n);
538 if (Q_LIKELY(!filename.
isEmpty())) {
539 const QString _dir = directory.
isEmpty() ? QStringLiteral(CUTELYST_I18N_DIR) : directory;
540 const QDir i18nDir(_dir);
541 if (Q_LIKELY(i18nDir.
exists())) {
542 const QString _prefix = prefix.
isEmpty() ? QStringLiteral(
".") : prefix;
543 const QString _suffix = suffix.
isEmpty() ? QStringLiteral(
".qm") : suffix;
547 if (Q_LIKELY(!tsFiles.empty())) {
548 locales.
reserve(tsFiles.size());
550 const QString fn = ts.fileName();
551 const int prefIdx = fn.
indexOf(_prefix);
558 if (Q_LIKELY(trans->load(loc, filename, _prefix, _dir))) {
561 qCDebug(CUTELYST_CORE) <<
"Loaded translations for" << loc <<
"from"
562 << ts.absoluteFilePath();
565 qCWarning(CUTELYST_CORE) <<
"Can not load translations for" << loc
566 <<
"from" << ts.absoluteFilePath();
569 qCWarning(CUTELYST_CORE)
570 <<
"Can not load translations for invalid locale string" << locString;
575 qCWarning(CUTELYST_CORE)
576 <<
"Can not find translation files for" << filename <<
"in directory" << _dir;
579 qCWarning(CUTELYST_CORE)
580 <<
"Can not load translations from not existing directory:" << _dir;
583 qCWarning(CUTELYST_CORE) <<
"Can not load translations for empty file name.";
595 const QDir dir(directory);
596 if (Q_LIKELY(dir.
exists())) {
598 if (Q_LIKELY(!dirs.empty())) {
600 for (
const QString &subDir : dirs) {
601 const QString relFn = subDir + u
'/' + filename;
607 if (Q_LIKELY(trans->load(
611 qCDebug(CUTELYST_CORE) <<
"Loaded translations for" << l <<
"from"
615 qCWarning(CUTELYST_CORE) <<
"Can not load translations for" << l
619 qCWarning(CUTELYST_CORE)
620 <<
"Can not load translations for invalid locale string:" << subDir;
626 qCWarning(CUTELYST_CORE) <<
"Can not find locale dirs under" << directory;
629 qCWarning(CUTELYST_CORE)
630 <<
"Can not load translations from not existing directory:" << directory;
633 qCWarning(CUTELYST_CORE)
634 <<
"Can not load translations for empty file name or directory name";
643 return d->defaultLocale;
649 d->defaultLocale = locale;
652 void Cutelyst::ApplicationPrivate::setupHome()
665 void ApplicationPrivate::setupChildren(
const QObjectList &children)
668 for (
QObject *child : children) {
669 auto controller = qobject_cast<Controller *>(child);
671 q->registerController(controller);
675 auto plugin = qobject_cast<Plugin *>(child);
677 q->registerPlugin(plugin);
681 auto view = qobject_cast<View *>(child);
683 q->registerView(view);
687 auto dispatchType = qobject_cast<DispatchType *>(child);
689 q->registerDispatcher(dispatchType);
695 void Cutelyst::ApplicationPrivate::logRequest(
Request *req)
699 path = QStringLiteral(
"/");
701 qCDebug(CUTELYST_REQUEST) << req->method() <<
"request for" << path <<
"from"
706 logRequestParameters(params,
QLatin1String(
"Query Parameters are:"));
711 logRequestParameters(params,
QLatin1String(
"Body Parameters are:"));
714 const auto bodyData = req->bodyData();
717 qCDebug(CUTELYST_REQUEST).noquote() <<
"JSON body:\n"
721 const auto uploads = req->
uploads();
722 if (!uploads.isEmpty()) {
723 logRequestUploads(uploads);
727 void Cutelyst::ApplicationPrivate::logRequestParameters(
const ParamsMultiMap ¶ms,
733 table.
append({it.key(), it.value()});
736 qCDebug(CUTELYST_REQUEST) << Utils::buildTable(table,
748 for (
Upload *upload : uploads) {
749 table.
append({upload->name(),
754 qCDebug(CUTELYST_REQUEST) << Utils::buildTable(table,
772 auto matchMetadata = [name](
const QJsonObject &metadata) {
773 const QJsonObject json = metadata[u
"MetaData"].toObject();
774 qCDebug(CUTELYST_CORE) <<
"Found plugin metadata" << json;
775 return json[u
"name"].toString() == name;
778 auto createComponent = [name, parent, &factory](
QObject *plugin) ->
Component * {
779 factory = qobject_cast<ComponentFactory *>(plugin);
789 if (matchMetadata(plugin.metaData())) {
790 component = createComponent(plugin.instance());
795 qCCritical(CUTELYST_CORE)
796 <<
"Could not create a component for static plugin" << plugin.metaData();
800 if (factory && component) {
801 factories.insert(name, factory);
805 QDir pluginsDir(directory);
807 const auto plugins = pluginsDir.entryList(
QDir::Files);
808 for (
const QString &fileName : plugins) {
809 loader.
setFileName(pluginsDir.absoluteFilePath(fileName));
811 if (matchMetadata(loader.
metaData())) {
812 component = createComponent(loader.
instance());
817 qCCritical(CUTELYST_CORE)
818 <<
"Could not create a component for plugin" << fileName << loader.
metaData();
823 factories.
insert(name, factory);
829 #include "moc_application.cpp"
The Cutelyst application.
Engine * engine() const noexcept
QVector< Controller * > controllers() const noexcept
void afterDispatch(Cutelyst::Context *c)
bool setup(Engine *engine)
void handleRequest(Cutelyst::EngineRequest *request)
void beforePrepareAction(Cutelyst::Context *c, bool *skipMethod)
Application(QObject *parent=nullptr)
void setConfig(const QString &key, const QVariant &value)
QVariantMap config() const noexcept
Dispatcher * dispatcher() const noexcept
bool registerPlugin(Plugin *plugin)
bool registerController(Controller *controller)
bool registerDispatcher(DispatchType *dispatcher)
QString pathTo(const QString &path) const
Headers & defaultHeaders() noexcept
QString translate(const QLocale &locale, const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
View * view(QStringView name={}) const
bool inited() const noexcept
void beforeDispatch(Cutelyst::Context *c)
void preForked(Cutelyst::Application *app)
void setDefaultLocale(const QLocale &locale)
QVector< Plugin * > plugins() const noexcept
void addTranslator(const QLocale &locale, QTranslator *translator)
QLocale defaultLocale() const noexcept
bool registerView(View *view)
void addTranslators(const QLocale &locale, const QVector< QTranslator * > &translators)
QVector< DispatchType * > dispatchers() const noexcept
static const char * cutelystVersion() noexcept
QVector< QLocale > loadTranslationsFromDirs(const QString &directory, const QString &filename)
void addXCutelystVersionHeader()
Component * createComponentPlugin(const QString &name, QObject *parent=nullptr)
void loadTranslations(const QString &filename, const QString &directory={}, const QString &prefix={}, const QString &suffix={})
QVector< QLocale > loadTranslationsFromDir(const QString &filename, const QString &directory=QString(), const QString &prefix=QStringLiteral("."), const QString &suffix=QStringLiteral(".qm"))
void postForked(Cutelyst::Application *app)
virtual Component * createComponent(QObject *parent=nullptr)=0
The Cutelyst Component base class.
void setReverse(const QString &reverse)
QString reverse() const noexcept
QString name() const noexcept
Cutelyst Controller base class.
Abstract class to described a dispatch type.
QVariantMap config(const QString &entity) const
Base class for Cutelyst Plugins.
QString addressString() const
QVector< Upload * > uploads() const
ParamsMultiMap bodyParameters() const
ParamsMultiMap queryParameters() const
Cutelyst Upload handles file upload requests.
Abstract View component for Cutelyst.
The Cutelyst namespace holds all public Cutelyst API.
QString absoluteFilePath(const QString &fileName) const const
QFileInfoList entryInfoList(QDir::Filters filters, QDir::SortFlags sort) const const
QStringList entryList(QDir::Filters filters, QDir::SortFlags sort) const const
bool exists() const const
QString absoluteFilePath() const const
QString absolutePath() const const
QString baseName() const const
QString suffix() const const
QByteArray toJson(QJsonDocument::JsonFormat format) const const
QJsonObject::iterator insert(QLatin1StringView key, const QJsonValue &value)
void append(QList::parameter_type value)
QList::const_reverse_iterator crbegin() const const
QList::const_reverse_iterator crend() const const
bool isEmpty() const const
void reserve(qsizetype size)
T value(qsizetype i) const const
QLocale::Language language() const const
QMultiMap::const_iterator constBegin() const const
QMultiMap::const_iterator constEnd() const const
bool isEmpty() const const
const QObjectList & children() const const
QObject * parent() const const
void setFileName(const QString &fileName)
QList< QStaticPlugin > staticPlugins()
QString arg(Args &&... args) const const
const QChar at(qsizetype position) const const
QString fromLatin1(QByteArrayView str)
QString fromLocal8Bit(QByteArrayView str)
QString fromUtf8(QByteArrayView str)
qsizetype indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const const
QString & insert(qsizetype position, QChar ch)
bool isEmpty() const const
qsizetype length() const const
QString mid(qsizetype position, qsizetype n) const const
QString number(double n, char format, int precision)
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
QString join(QChar separator) const const
void sort(Qt::CaseSensitivity cs)
QJsonDocument toJsonDocument() const const