6#include "application.h"
10#include "controller.h"
11#include "dispatcher.h"
18#include <QCoreApplication>
30 : d_ptr(new ContextPrivate(app, app->engine(), app->dispatcher(), app->plugins()))
32 auto req =
new DummyRequest(
this);
39 d_ptr->request->d_ptr->engine = d_ptr->engine;
45 delete d_ptr->request;
46 delete d_ptr->response;
53 return !d->error.isEmpty();
59 if (
error.isEmpty()) {
63 qCCritical(CUTELYST_CORE) <<
error;
118 return d->action->name();
124 return d->action->ns();
142 return d->dispatcher;
148 return d->action->className();
154 return d->action->controller();
160 return d->dispatcher->controller(name);
172 return d->app->view(name);
178 d->view = d->app->view(name);
191 return d->stash.value(key);
197 return d->stash.value(key, defaultValue);
203 return d->stash.take(key);
209 return d->stash.remove(key);
215 d->stash.insert(key, value);
236 QUrl uri = d->request->uri();
241 const QString controllerNS = d->action->controller()->ns();
250 if (_path.
compare(u
"/") == 0) {
251 _path += args.
join(u
'/');
253 _path = _path + u
'/' + args.
join(u
'/');
265 if (queryValues.
size()) {
293 Action *expandedAction = d->dispatcher->expandAction(
this,
action);
300 localCapturesAux.
append(localArgs);
301 localArgs = localCapturesAux;
305 const QString path = d->dispatcher->uriForAction(
action, localCaptures);
307 qCWarning(CUTELYST_CORE) <<
"Can not find action for" <<
action << localCaptures;
311 uri =
uriFor(path, localArgs, queryValues);
325 qCWarning(CUTELYST_CORE) <<
"Can not find action for" << path;
343 d->dispatcher->forward(
this,
action);
364 if (--d->actionRefCount || !d->stack.isEmpty()) {
368 if (Q_UNLIKELY(d->engineRequest->status & EngineRequest::Finalized)) {
369 qCWarning(CUTELYST_ASYNC) <<
"Trying to async attach to a finalized request! Skipping...";
373 if (d->engineRequest->status & EngineRequest::Async) {
374 while (!d->pendingAsync.isEmpty()) {
378 if (d->actionRefCount) {
387 Q_EMIT d->app->afterDispatch(
this);
396 return d->dispatcher->forward(
this,
action);
402 return d->dispatcher->forward(
this,
action);
408 return d->dispatcher->getAction(
action,
ns);
414 return d->dispatcher->getActions(
action,
ns);
426 Q_ASSERT_X(code,
"Context::execute",
"trying to execute a null Cutelyst::Component");
428 static int recursion =
429 qEnvironmentVariableIsSet(
"RECURSION") ? qEnvironmentVariableIntValue(
"RECURSION") : 1000;
430 if (d->stack.size() >= recursion) {
431 QString msg = QStringLiteral(
"Deep recursion detected (stack size %1) calling %2, %3")
442 const QString statsInfo = d->statsStartExecute(code);
448 if (d->stats && !statsInfo.
isEmpty()) {
449 d->statsFinishExecute(statsInfo);
475 return d->app->config(key, defaultValue);
481 return d->app->config();
485 const char *sourceText,
486 const char *disambiguation,
490 return d->app->translate(d->locale, context, sourceText, disambiguation, n);
497 if (Q_UNLIKELY(d->engineRequest->status & EngineRequest::Finalized)) {
498 qCWarning(CUTELYST_CORE) <<
"Trying to finalize a finalized request! Skipping...";
503 qCDebug(CUTELYST_STATS,
504 "Response Code: %d; Content-Type: %s; Content-Length: %s",
505 d->response->status(),
506 d->response->headers().header(
"Content-Type"_qba,
"unknown"_qba).constData(),
507 d->response->headers()
511 const std::chrono::duration<double> duration =
512 std::chrono::steady_clock::now() - d->engineRequest->startOfRequest;
515 if (duration.count() == 0.0) {
516 average = QStringLiteral(
"??");
521 qCInfo(CUTELYST_STATS) << qPrintable(QStringLiteral(
"Request took: %1s (%2/s)\n%3")
529 d->engineRequest->finalize();
542 if (qobject_cast<Action *>(code)) {
546 if (stack.size() > 2) {
547 actionName = u
"-> " + actionName;
552 stats->profileStart(actionName);
557void ContextPrivate::statsFinishExecute(
const QString &statsInfo)
559 stats->profileEnd(statsInfo);
565 auto it = unite.constBegin();
566 while (it != unite.constEnd()) {
567 d->stash.insert(it.key(), it.value());
572#include "moc_context.cpp"
573#include "moc_context_p.cpp"
This class represents a Cutelyst Action.
virtual qint8 numberOfCaptures() const
The Cutelyst application.
Headers & defaultHeaders() noexcept
QLocale defaultLocale() const noexcept
The Cutelyst Component base class.
QString reverse() const noexcept
QString name() const noexcept
QStringList errors() const noexcept
bool forward(Component *component)
QVector< Plugin * > plugins() const
Context(Application *app)
void detach(Action *action=nullptr)
QStack< Component * > stack() const noexcept
QLocale locale() const noexcept
void setState(bool state) noexcept
QUrl uriFor(const QString &path={}, const QStringList &args={}, const ParamsMultiMap &queryValues={}) const
Response * res() const noexcept
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
void setStash(const QString &key, const QVariant &value)
bool stashRemove(const QString &key)
QVariant stashTake(const QString &key)
bool setCustomView(QStringView name)
void setLocale(const QLocale &locale)
View * customView() const noexcept
QVector< Action * > getActions(QStringView action, QStringView ns={}) const
bool detached() const noexcept
Action * getAction(QStringView action, QStringView ns={}) const
Dispatcher * dispatcher() const noexcept
Application * app() const noexcept
View * view(QStringView name={}) const
QUrl uriForAction(QStringView path, const QStringList &captures={}, const QStringList &args={}, const ParamsMultiMap &queryValues={}) const
void detachAsync() noexcept
bool execute(Component *code)
void appendError(const QString &error)
Engine * engine() const noexcept
bool error() const noexcept
Response * response() const noexcept
Cutelyst Controller base class.
QIODevice * body() const noexcept
Abstract View component for Cutelyst.
The Cutelyst namespace holds all public Cutelyst API.
QByteArray number(double n, char format, int precision)
virtual bool open(QIODeviceBase::OpenMode mode)
void append(QList::parameter_type value)
bool isEmpty() const const
qsizetype size() const const
QList::value_type takeFirst()
QMultiMap::const_iterator constBegin() const const
QMultiMap::const_iterator constEnd() const const
bool isEmpty() const const
QMultiMap::size_type size() const const
QString arg(Args &&... args) 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 & prepend(QChar ch)
QString rightJustified(qsizetype width, QChar fill, bool truncate) const const
qsizetype size() const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
void truncate(qsizetype position)
QString join(QChar separator) const const
void setPath(const QString &path, QUrl::ParsingMode mode)
void setQuery(const QString &query, QUrl::ParsingMode mode)
void addQueryItem(const QString &key, const QString &value)
QVariant fromValue(const T &value)