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);
34 req->
body =
new QBuffer(
this);
35 req->
body->open(QBuffer::ReadWrite);
39 d_ptr->request =
new Request(req);
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;
73 bool Context::state() const noexcept
115 QString Context::actionName() const noexcept
118 return d->action->name();
121 QString Context::ns() const noexcept
124 return d->action->ns();
142 return d->dispatcher;
145 QString Cutelyst::Context::controllerName()
const
148 return QString::fromLatin1(d->action->controller()->metaObject()->className());
154 return d->action->controller();
160 return d->dispatcher->controllers().value(name);
172 return d->app->view(name);
178 return d->app->view(name);
184 d->view = d->app->view(name);
197 return d->stash.value(key);
203 return d->stash.value(key, defaultValue);
209 return d->stash.take(key);
215 return d->stash.remove(key);
221 d->stash.insert(key, value);
227 d->stash.insert(key, QVariant::fromValue(map));
240 QUrl uri = d->request->uri();
243 if (path.isEmpty()) {
245 const QString controllerNS = d->action->controller()->ns();
246 if (!controllerNS.isEmpty()) {
247 _path.prepend(controllerNS);
253 if (!args.isEmpty()) {
254 if (_path.compare(u
"/") == 0) {
255 _path += args.join(u
'/');
257 _path = _path + u
'/' + args.join(u
'/');
261 if (!_path.startsWith(u
'/')) {
264 uri.setPath(_path, QUrl::DecodedMode);
267 if (!queryValues.isEmpty()) {
269 if (queryValues.size()) {
270 auto it = queryValues.constEnd();
271 while (it != queryValues.constBegin()) {
273 query.addQueryItem(it.key(), it.value());
287 Action *localAction = action;
289 localAction = d->action;
292 QStringList localArgs = args;
293 QStringList localCaptures = captures;
295 Action *expandedAction = d->dispatcher->expandAction(
this, action);
298 && localArgs.size()) {
299 localCaptures.append(localArgs.takeFirst());
302 QStringList localCapturesAux = localCaptures;
303 localCapturesAux.append(localArgs);
304 localArgs = localCapturesAux;
305 localCaptures = QStringList();
308 const QString path = d->dispatcher->uriForAction(localAction, localCaptures);
309 if (path.isEmpty()) {
310 qCWarning(CUTELYST_CORE) <<
"Can not find action for" << localAction << localCaptures;
314 uri =
uriFor(path, localArgs, queryValues);
323 Action *action = d->dispatcher->getActionByPath(path);
325 qCWarning(CUTELYST_CORE) <<
"Can not find action for" << path;
329 uri =
uriFor(action, captures, args, queryValues);
343 d->dispatcher->forward(
this, action);
358 if (--d->actionRefCount) {
362 if (Q_UNLIKELY(d->engineRequest->status & EngineRequest::Finalized)) {
363 qCWarning(CUTELYST_ASYNC) <<
"Trying to async attach to a finalized request! Skipping...";
367 if (d->engineRequest->status & EngineRequest::Async) {
368 while (d->asyncAction < d->pendingAsync.size()) {
369 Component *action = d->pendingAsync[d->asyncAction++];
370 const bool ret =
execute(action);
372 if (d->actionRefCount) {
381 Q_EMIT d->app->afterDispatch(
this);
390 return d->dispatcher->forward(
this, action);
396 return d->dispatcher->forward(
this, action);
402 return d->dispatcher->getAction(action, ns);
408 return d->dispatcher->getActions(action, ns);
420 Q_ASSERT_X(code,
"Context::execute",
"trying to execute a null Cutelyst::Component");
422 static int recursion = qEnvironmentVariableIsSet(
"RECURSION") ? qEnvironmentVariableIntValue(
"RECURSION") : 1000;
423 if (d->stack.size() >= recursion) {
424 QString msg = QStringLiteral(
"Deep recursion detected (stack size %1) calling %2, %3")
425 .arg(QString::number(d->stack.size()), code->
reverse(), code->
name());
435 const QString statsInfo = d->statsStartExecute(code);
441 if (d->stats && !statsInfo.isEmpty()) {
442 d->statsFinishExecute(statsInfo);
465 QVariant Context::config(
const QString &key,
const QVariant &defaultValue)
const
468 return d->app->config(key, defaultValue);
471 QVariantMap Context::config() const noexcept
474 return d->app->config();
477 QString
Context::translate(
const char *context,
const char *sourceText,
const char *disambiguation,
int n)
const
480 return d->app->translate(d->locale, context, sourceText, disambiguation, n);
487 if (Q_UNLIKELY(d->engineRequest->status & EngineRequest::Finalized)) {
488 qCWarning(CUTELYST_CORE) <<
"Trying to finalize a finalized request! Skipping...";
493 qCDebug(CUTELYST_STATS,
"Response Code: %d; Content-Type: %s; Content-Length: %s",
494 d->response->status(),
495 qPrintable(d->response->headers().header(QStringLiteral(
"CONTENT_TYPE"), QStringLiteral(
"unknown"))),
496 qPrintable(d->response->headers().header(QStringLiteral(
"CONTENT_LENGTH"), QStringLiteral(
"unknown"))));
498 const double enlapsed = d->engineRequest->elapsed.nsecsElapsed() / 1000000000.0;
500 if (enlapsed == 0.0) {
501 average = QStringLiteral(
"??");
503 average = QString::number(1.0 / enlapsed,
'f');
504 average.truncate(average.size() - 3);
506 qCInfo(CUTELYST_STATS) << qPrintable(QStringLiteral(
"Request took: %1s (%2/s)\n%3")
507 .arg(QString::number(enlapsed,
'f'),
509 QString::fromLatin1(d->stats->report())));
514 d->engineRequest->finalize();
517 QString ContextPrivate::statsStartExecute(
Component *code)
521 if (code->
name().startsWith(u
'_')) {
527 if (qobject_cast<Action *>(code)) {
528 actionName.prepend(u
'/');
531 if (stack.size() > 2) {
532 actionName = u
"-> " + actionName;
533 actionName = actionName.rightJustified(actionName.size() + stack.size() - 2, QLatin1Char(
' '));
536 stats->profileStart(actionName);
541 void ContextPrivate::statsFinishExecute(
const QString &statsInfo)
543 stats->profileEnd(statsInfo);
549 auto it = unite.constBegin();
550 while (it != unite.constEnd()) {
551 d->stash.insert(it.key(), it.value());
556 #include "moc_context.cpp"
557 #include "moc_context_p.cpp"
This class represents a Cutelyst Action.
virtual qint8 numberOfCaptures() const noexcept
The Cutelyst Application.
Headers & defaultHeaders() noexcept
The Cutelyst Component base class.
QVector< Action * > getActions(const QString &action, const QString &ns={}) const
QStringList errors() const noexcept
Returns a list of errors that were defined.
bool forward(Component *component)
QUrl uriFor(const QString &path=QString(), const QStringList &args=QStringList(), const ParamsMultiMap &queryValues=ParamsMultiMap()) const
QVector< Plugin * > plugins() const
Context(Application *app)
Constructs a new DUMMY Context object that is child of Application This currently is experimental to ...
Action * getAction(const QString &action, const QString &ns={}) const
void detach(Action *action=nullptr)
QStack< Component * > stack() const noexcept
QLocale locale() const noexcept
void setState(bool state) noexcept
Sets the state of the current executed action, setting to false will make the dispatcher skip non pro...
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)
void finalize()
finalize the request right away this is automatically called at the end of the actions chain
bool stashRemove(const QString &key)
QVariant stashTake(const QString &key)
void attachAsync()
attachAsync
void setLocale(const QLocale &locale)
View * customView() const noexcept
bool detached() const noexcept
View * view(const QString &name) const
QUrl uriForAction(const QString &path, const QStringList &captures=QStringList(), const QStringList &args=QStringList(), const ParamsMultiMap &queryValues=ParamsMultiMap()) const
Dispatcher * dispatcher() const noexcept
Application * app() const noexcept
void detachAsync() noexcept
bool execute(Component *code)
bool setCustomView(const QString &name)
Engine * engine() const noexcept
bool error() const noexcept
Returns true if an error was set.
Response * response() const noexcept
Cutelyst Controller base class
Cutelyst View abstract view component
The Cutelyst namespace holds all public Cutelyst API.
QMultiMap< QString, QString > ParamsMultiMap