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);
33 req->
body =
new QBuffer(
this);
34 req->
body->open(QBuffer::ReadWrite);
38 d_ptr->request =
new Request(req);
39 d_ptr->request->d_ptr->engine = d_ptr->engine;
44 delete d_ptr->request;
45 delete d_ptr->response;
52 return !d->error.isEmpty();
58 if (
error.isEmpty()) {
62 qCCritical(CUTELYST_CORE) <<
error;
72bool Context::state() const noexcept
114QString Context::actionName() const noexcept
117 return d->action->name();
120QString Context::ns() const noexcept
123 return d->action->ns();
141 return d->dispatcher;
144QString Cutelyst::Context::controllerName()
const
147 return QString::fromLatin1(d->action->controller()->metaObject()->className());
153 return d->action->controller();
159 return d->dispatcher->controllers().value(name);
171 return d->app->view(name);
177 return d->app->view(name);
183 d->view = d->app->view(name);
196 return d->stash.value(key);
202 return d->stash.value(key, defaultValue);
208 return d->stash.take(key);
214 return d->stash.remove(key);
220 d->stash.insert(key, value);
226 d->stash.insert(key, QVariant::fromValue(map));
236 const QStringList &args,
241 QUrl uri = d->request->uri();
244 if (path.isEmpty()) {
246 const QString controllerNS = d->action->controller()->ns();
247 if (!controllerNS.isEmpty()) {
248 _path.prepend(controllerNS);
254 if (!args.isEmpty()) {
255 if (_path.compare(u
"/") == 0) {
256 _path += args.join(u
'/');
258 _path = _path + u
'/' + args.join(u
'/');
262 if (!_path.startsWith(u
'/')) {
265 uri.setPath(_path, QUrl::DecodedMode);
268 if (!queryValues.isEmpty()) {
270 if (queryValues.size()) {
271 auto it = queryValues.constEnd();
272 while (it != queryValues.constBegin()) {
274 query.addQueryItem(it.key(), it.value());
284 const QStringList &captures,
285 const QStringList &args,
291 Action *localAction = action;
293 localAction = d->action;
296 QStringList localArgs = args;
297 QStringList localCaptures = captures;
299 Action *expandedAction = d->dispatcher->expandAction(
this, action);
301 while (localCaptures.size() < expandedAction->
numberOfCaptures() && localArgs.size()) {
302 localCaptures.append(localArgs.takeFirst());
305 QStringList localCapturesAux = localCaptures;
306 localCapturesAux.append(localArgs);
307 localArgs = localCapturesAux;
308 localCaptures = QStringList();
311 const QString path = d->dispatcher->uriForAction(localAction, localCaptures);
312 if (path.isEmpty()) {
313 qCWarning(CUTELYST_CORE) <<
"Can not find action for" << localAction << localCaptures;
317 uri =
uriFor(path, localArgs, queryValues);
322 const QStringList &captures,
323 const QStringList &args,
329 Action *action = d->dispatcher->getActionByPath(path);
331 qCWarning(CUTELYST_CORE) <<
"Can not find action for" << path;
335 uri =
uriFor(action, captures, args, queryValues);
349 d->dispatcher->forward(
this, action);
364 if (--d->actionRefCount) {
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->asyncAction < d->pendingAsync.size()) {
375 Component *action = d->pendingAsync[d->asyncAction++];
376 const bool ret =
execute(action);
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")
432 .arg(QString::number(d->stack.size()), code->
reverse(), code->
name());
442 const QString statsInfo = d->statsStartExecute(code);
448 if (d->stats && !statsInfo.isEmpty()) {
449 d->statsFinishExecute(statsInfo);
472QVariant Context::config(
const QString &key,
const QVariant &defaultValue)
const
475 return d->app->config(key, defaultValue);
478QVariantMap Context::config() const noexcept
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 qPrintable(d->response->headers().header(QStringLiteral(
"CONTENT_TYPE"),
507 QStringLiteral(
"unknown"))),
508 qPrintable(d->response->headers().header(QStringLiteral(
"CONTENT_LENGTH"),
509 QStringLiteral(
"unknown"))));
511 const double enlapsed = d->engineRequest->elapsed.nsecsElapsed() / 1000000000.0;
513 if (enlapsed == 0.0) {
514 average = QStringLiteral(
"??");
516 average = QString::number(1.0 / enlapsed,
'f');
517 average.truncate(average.size() - 3);
519 qCInfo(CUTELYST_STATS) << qPrintable(QStringLiteral(
"Request took: %1s (%2/s)\n%3")
520 .arg(QString::number(enlapsed,
'f'),
522 QString::fromLatin1(d->stats->report())));
527 d->engineRequest->finalize();
530QString ContextPrivate::statsStartExecute(
Component *code)
534 if (code->
name().startsWith(u
'_')) {
540 if (qobject_cast<Action *>(code)) {
541 actionName.prepend(u
'/');
544 if (stack.size() > 2) {
545 actionName = u
"-> " + actionName;
547 actionName.rightJustified(actionName.size() + stack.size() - 2, QLatin1Char(
' '));
550 stats->profileStart(actionName);
555void ContextPrivate::statsFinishExecute(
const QString &statsInfo)
557 stats->profileEnd(statsInfo);
563 auto it = unite.constBegin();
564 while (it != unite.constEnd()) {
565 d->stash.insert(it.key(), it.value());
570#include "moc_context.cpp"
571#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