18 #include "context_p.h" 24 #include "dispatcher.h" 25 #include "controller.h" 26 #include "application.h" 28 #include "enginerequest.h" 34 #include <QCoreApplication> 44 d_ptr(new ContextPrivate(app, app->engine(), app->dispatcher(), app->plugins()))
46 auto req =
new DummyRequest(
this);
53 d_ptr->request->d_ptr->engine = d_ptr->engine;
58 delete d_ptr->request;
59 delete d_ptr->response;
66 return !d->error.isEmpty();
72 if (
error.isEmpty()) {
76 qCCritical(CUTELYST_CORE) <<
error;
131 return d->action->name();
137 return d->action->ns();
155 return d->dispatcher;
167 return d->action->controller();
173 return d->dispatcher->controllers().value(name);
185 return d->app->view(name);
191 d->view = d->app->view(name);
204 return d->stash.value(key);
210 return d->stash.value(key, defaultValue);
216 return d->stash.take(key);
222 return d->stash.remove(key);
228 d->stash.insert(key, value);
247 QUrl uri = d->request->uri();
252 const QString controllerNS = d->action->controller()->ns();
271 uri.
setPath(_path, QUrl::DecodedMode);
276 if (queryValues.
size()) {
296 localAction = d->action;
302 Action *expandedAction = d->dispatcher->expandAction(
this,
action);
305 && localArgs.
size()) {
310 localCapturesAux.
append(localArgs);
311 localArgs = localCapturesAux;
315 const QString path = d->dispatcher->uriForAction(localAction, localCaptures);
317 qCWarning(CUTELYST_CORE) <<
"Can not find action for" << localAction << localCaptures;
321 uri =
uriFor(path, localArgs, queryValues);
332 qCWarning(CUTELYST_CORE) <<
"Can not find action for" << path;
350 d->dispatcher->forward(
this,
action);
360 d->engineRequest->status |= EngineRequest::Async;
366 if (--d->asyncDetached) {
370 if (Q_UNLIKELY(d->engineRequest->status & EngineRequest::Finalized)) {
371 qCWarning(CUTELYST_ASYNC) <<
"Trying to async attach to a finalized request! Skipping...";
375 while (d->asyncAction < d->pendingAsync.size()) {
379 }
else if (d->asyncDetached) {
384 if (d->engineRequest->status & EngineRequest::Async) {
385 Q_EMIT d->app->afterDispatch(
this);
394 return d->dispatcher->forward(
this,
action);
400 return d->dispatcher->forward(
this,
action);
406 return d->dispatcher->getAction(
action,
ns);
412 return d->dispatcher->getActions(
action,
ns);
424 Q_ASSERT_X(code,
"Context::execute",
"trying to execute a null Cutelyst::Component");
426 static int recursion = qEnvironmentVariableIsSet(
"RECURSION") ? qEnvironmentVariableIntValue(
"RECURSION") : 1000;
427 if (d->stack.size() >= recursion) {
428 QString msg = QStringLiteral(
"Deep recursion detected (stack size %1) calling %2, %3")
439 const QString statsInfo = d->statsStartExecute(code);
445 if (d->stats && !statsInfo.
isEmpty()) {
446 d->statsFinishExecute(statsInfo);
472 return d->app->config(key, defaultValue);
478 return d->app->config();
484 return d->app->translate(d->locale, context, sourceText, disambiguation, n);
509 if (Q_UNLIKELY(d->engineRequest->status & EngineRequest::Finalized)) {
510 qCWarning(CUTELYST_CORE) <<
"Trying to finalize a finalized request! Skipping...";
515 qCDebug(CUTELYST_STATS,
"Response Code: %d; Content-Type: %s; Content-Length: %s",
516 d->response->status(),
517 qPrintable(d->response->headers().header(QStringLiteral(
"CONTENT_TYPE"), QStringLiteral(
"unknown"))),
518 qPrintable(d->response->headers().header(QStringLiteral(
"CONTENT_LENGTH"), QStringLiteral(
"unknown"))));
520 const double enlapsed = d->engineRequest->elapsed.nsecsElapsed() / 1000000000.0;
522 if (enlapsed == 0.0) {
523 average = QStringLiteral(
"??");
528 qCInfo(CUTELYST_STATS) << qPrintable(QStringLiteral(
"Request took: %1s (%2/s)\n%3")
536 d->engineRequest->finalize();
562 if (qobject_cast<Action *>(code)) {
566 if (stack.size() > 2) {
571 stats->profileStart(actionName);
576 void ContextPrivate::statsFinishExecute(
const QString &statsInfo)
578 stats->profileEnd(statsInfo);
581 #include "moc_context.cpp" 582 #include "moc_context_p.cpp" Context(Application *app)
Constructs a new DUMMY Context object that is child of Application This currently is experimental to ...
Application * app() const
QString actionName() const
Request * request() const
void truncate(int position)
virtual qint8 numberOfCaptures() const
bool setCustomView(const QString &name)
QString & prepend(QChar ch)
const_iterator constBegin() const
Dispatcher * dispatcher() const
void setStash(const QString &key, const QVariant &value)
void detach(Action *action=nullptr)
QString join(const QString &separator) const
QUrl uriFor(const QString &path=QString(), const QStringList &args=QStringList(), const ParamsMultiMap &queryValues=ParamsMultiMap()) const
virtual bool open(OpenMode mode)
The Cutelyst Component base class.
This class represents a Cutelyst Action.
QVector< Action * > getActions(const QString &action, const QString &ns=QString()) const
QUrl uriForAction(const QString &path, const QStringList &captures=QStringList(), const QStringList &args=QStringList(), const ParamsMultiMap &queryValues=ParamsMultiMap()) const
void setPath(const QString &path, ParsingMode mode)
QStack< Component * > stack() const
void addQueryItem(const QString &key, const QString &value)
Headers & defaultHeaders()
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
void next(bool force=false)
This method is deprecated and no longer works, creating local event loops leads to crashes...
QStringList errors() const
Returns a list of errors that were defined.
const_iterator constEnd() const
Response * response() const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) 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
The Cutelyst namespace holds all public Cutelyst API.
Action * getAction(const QString &action, const QString &ns=QString()) const
bool execute(Component *code)
void setState(bool state)
Sets the state of the current executed action, setting to false will make the dispatcher skip non pro...
QVector< Plugin * > plugins() const
QVariant fromValue(const T &value)
void setLocale(const QLocale &locale)
QVariant stashTake(const QString &key)
Cutelyst View abstract view component
void setQuery(const QString &query, ParsingMode mode)
Controller * controller() const
QString fromLatin1(const char *str, int size)
View * customView() const
The Cutelyst Application.
bool stashRemove(const QString &key)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const
Q_DECL_DEPRECATED bool wait(uint count=1)
This method is deprecated and no longer works, creating local event loops leads to crashes...
void finalize()
finalize the request right away this is automatically called at the end of the actions chain ...
bool error() const
Returns true if an error was set.
QString controllerName() const
void attachAsync()
attachAsync