11 #include "dispatcher.h" 12 #include "controller.h" 13 #include "application.h" 21 #include <QCoreApplication> 31 d_ptr(new ContextPrivate(app, app->engine(), app->dispatcher(), app->plugins()))
33 auto req =
new DummyRequest(
this);
40 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;
154 return d->action->controller();
160 return d->dispatcher->controllers().value(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);
234 QUrl uri = d->request->uri();
239 const QString controllerNS = d->action->controller()->ns();
248 if (_path.
compare(u
"/") == 0) {
258 uri.
setPath(_path, QUrl::DecodedMode);
263 if (queryValues.
size()) {
283 localAction = d->action;
289 Action *expandedAction = d->dispatcher->expandAction(
this,
action);
292 && localArgs.
size()) {
297 localCapturesAux.
append(localArgs);
298 localArgs = localCapturesAux;
302 const QString path = d->dispatcher->uriForAction(localAction, localCaptures);
304 qCWarning(CUTELYST_CORE) <<
"Can not find action for" << localAction << localCaptures;
308 uri =
uriFor(path, localArgs, queryValues);
319 qCWarning(CUTELYST_CORE) <<
"Can not find action for" << path;
337 d->dispatcher->forward(
this,
action);
352 if (--d->asyncDetached) {
356 if (Q_UNLIKELY(d->engineRequest->status & EngineRequest::Finalized)) {
357 qCWarning(CUTELYST_ASYNC) <<
"Trying to async attach to a finalized request! Skipping...";
361 if (d->engineRequest->status & EngineRequest::Async) {
362 while (d->asyncAction < d->pendingAsync.size()) {
366 if (d->asyncDetached) {
375 Q_EMIT d->app->afterDispatch(
this);
384 return d->dispatcher->forward(
this,
action);
390 return d->dispatcher->forward(
this,
action);
396 return d->dispatcher->getAction(
action,
ns);
402 return d->dispatcher->getActions(
action,
ns);
414 Q_ASSERT_X(code,
"Context::execute",
"trying to execute a null Cutelyst::Component");
416 static int recursion = qEnvironmentVariableIsSet(
"RECURSION") ? qEnvironmentVariableIntValue(
"RECURSION") : 1000;
417 if (d->stack.size() >= recursion) {
418 QString msg = QStringLiteral(
"Deep recursion detected (stack size %1) calling %2, %3")
429 const QString statsInfo = d->statsStartExecute(code);
435 if (d->stats && !statsInfo.
isEmpty()) {
436 d->statsFinishExecute(statsInfo);
462 return d->app->config(key, defaultValue);
468 return d->app->config();
474 return d->app->translate(d->locale, context, sourceText, disambiguation, n);
481 if (Q_UNLIKELY(d->engineRequest->status & EngineRequest::Finalized)) {
482 qCWarning(CUTELYST_CORE) <<
"Trying to finalize a finalized request! Skipping...";
487 qCDebug(CUTELYST_STATS,
"Response Code: %d; Content-Type: %s; Content-Length: %s",
488 d->response->status(),
489 qPrintable(d->response->headers().header(QStringLiteral(
"CONTENT_TYPE"), QStringLiteral(
"unknown"))),
490 qPrintable(d->response->headers().header(QStringLiteral(
"CONTENT_LENGTH"), QStringLiteral(
"unknown"))));
492 const double enlapsed = d->engineRequest->elapsed.nsecsElapsed() / 1000000000.0;
494 if (enlapsed == 0.0) {
495 average = QStringLiteral(
"??");
500 qCInfo(CUTELYST_STATS) << qPrintable(QStringLiteral(
"Request took: %1s (%2/s)\n%3")
508 d->engineRequest->finalize();
521 if (qobject_cast<Action *>(code)) {
525 if (stack.size() > 2) {
530 stats->profileStart(actionName);
535 void ContextPrivate::statsFinishExecute(
const QString &statsInfo)
537 stats->profileEnd(statsInfo);
543 auto it = unite.constBegin();
544 while (it != unite.constEnd()) {
545 d->stash.insert(it.key(), it.value());
550 #include "moc_context.cpp" 551 #include "moc_context_p.cpp" Context(Application *app)
Constructs a new DUMMY Context object that is child of Application This currently is experimental to ...
void truncate(int position)
bool state() const noexcept
bool error() const noexcept
Returns true if an error was set.
bool setCustomView(const QString &name)
QString & prepend(QChar ch)
Response * res() const noexcept
QMap::const_iterator constBegin() const const
void setStash(const QString &key, const QVariant &value)
QString ns() const noexcept
void detach(Action *action=nullptr)
QString join(const QString &separator) const const
Request * req() const noexcept
QUrl uriFor(const QString &path=QString(), const QStringList &args=QStringList(), const ParamsMultiMap &queryValues=ParamsMultiMap()) const
virtual bool open(QIODevice::OpenMode mode)
The Cutelyst Component base class.
QString actionName() const noexcept
This class represents a Cutelyst Action.
QVector< Action * > getActions(const QString &action, const QString &ns=QString()) const
Headers & defaultHeaders() noexcept
QUrl uriForAction(const QString &path, const QStringList &captures=QStringList(), const QStringList &args=QStringList(), const ParamsMultiMap &queryValues=ParamsMultiMap()) const
void setPath(const QString &path, QUrl::ParsingMode mode)
void addQueryItem(const QString &key, const QString &value)
bool forward(Component *component)
QString number(int n, int base)
Cutelyst Controller base class
void append(const T &value)
QString rightJustified(int width, QChar fill, bool truncate) const const
Action * action() const noexcept
bool detached() const noexcept
bool isEmpty() const const
bool isEmpty() const const
QMap::const_iterator constEnd() const const
Request * request() const noexcept
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
View * view(const QString &name=QString()) const
QVariantMap config() const noexcept
virtual qint8 numberOfCaptures() const noexcept
The Cutelyst namespace holds all public Cutelyst API.
void detachAsync() noexcept
QLocale locale() const noexcept
Action * getAction(const QString &action, const QString &ns=QString()) const
bool execute(Component *code)
QVector< Plugin * > plugins() const
QVariant fromValue(const T &value)
void setLocale(const QLocale &locale)
Application * app() const noexcept
QStack< Component * > stack() const noexcept
QVariant stashTake(const QString &key)
Engine * engine() const noexcept
Cutelyst View abstract view component
Controller * controller() const noexcept
void setQuery(const QString &query, QUrl::ParsingMode mode)
QString fromLatin1(const char *str, int size)
The Cutelyst Application.
bool stashRemove(const QString &key)
bool isEmpty() const const
void setState(bool state) noexcept
Sets the state of the current executed action, setting to false will make the dispatcher skip non pro...
Response * response() const noexcept
int compare(const QString &other, Qt::CaseSensitivity cs) const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
void finalize()
finalize the request right away this is automatically called at the end of the actions chain ...
View * customView() const noexcept
QStringList errors() const noexcept
Returns a list of errors that were defined.
QString controllerName() const
void attachAsync()
attachAsync
Dispatcher * dispatcher() const noexcept