Cutelyst  2.3.0
context.cpp
1 /*
2  * Copyright (C) 2013-2017 Daniel Nicoletti <dantti12@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 #include "context_p.h"
19 
20 #include "common.h"
21 #include "request.h"
22 #include "response.h"
23 #include "action.h"
24 #include "dispatcher.h"
25 #include "controller.h"
26 #include "application.h"
27 #include "stats.h"
28 #include "enginerequest.h"
29 
30 #include "config.h"
31 
32 #include <QUrl>
33 #include <QUrlQuery>
34 #include <QCoreApplication>
35 #include <QBuffer>
36 
37 using namespace Cutelyst;
38 
39 Context::Context(ContextPrivate *priv) : d_ptr(priv)
40 {
41 }
42 
44  d_ptr(new ContextPrivate(app, app->engine(), app->dispatcher(), app->plugins()))
45 {
46  auto req = new DummyRequest(this);
47  req->body = new QBuffer(this);
48  req->body->open(QBuffer::ReadWrite);
49  req->context = this;
50 
51  d_ptr->response = new Response(app->defaultHeaders(), req);
52  d_ptr->request = new Request(req);
53  d_ptr->request->d_ptr->engine = d_ptr->engine;
54 }
55 
56 Context::~Context()
57 {
58  delete d_ptr->request;
59  delete d_ptr->response;
60  delete d_ptr;
61 }
62 
63 bool Context::error() const
64 {
65  Q_D(const Context);
66  return !d->error.isEmpty();
67 }
68 
69 void Context::error(const QString &error)
70 {
71  Q_D(Context);
72  if (error.isEmpty()) {
73  d->error.clear();
74  } else {
75  d->error << error;
76  qCCritical(CUTELYST_CORE) << error;
77  }
78 }
79 
80 QStringList Context::errors() const
81 {
82  Q_D(const Context);
83  return d->error;
84 }
85 
86 bool Context::state() const
87 {
88  Q_D(const Context);
89  return d->state;
90 }
91 
93 {
94  Q_D(Context);
95  d->state = state;
96 }
97 
99 {
100  Q_D(const Context);
101  return d->engine;
102 }
103 
105 {
106  Q_D(const Context);
107  return d->app;
108 }
109 
111 {
112  Q_D(const Context);
113  return d->response;
114 }
115 
117 {
118  Q_D(const Context);
119  return d->response;
120 }
121 
122 Action *Context::action() const
123 {
124  Q_D(const Context);
125  return d->action;
126 }
127 
128 QString Context::actionName() const
129 {
130  Q_D(const Context);
131  return d->action->name();
132 }
133 
134 QString Context::ns() const
135 {
136  Q_D(const Context);
137  return d->action->ns();
138 }
139 
140 Request *Context::request() const
141 {
142  Q_D(const Context);
143  return d->request;
144 }
145 
146 Request *Context::req() const
147 {
148  Q_D(const Context);
149  return d->request;
150 }
151 
153 {
154  Q_D(const Context);
155  return d->dispatcher;
156 }
157 
158 QString Cutelyst::Context::controllerName() const
159 {
160  Q_D(const Context);
161  return QString::fromLatin1(d->action->controller()->metaObject()->className());
162 }
163 
165 {
166  Q_D(const Context);
167  return d->action->controller();
168 }
169 
170 Controller *Context::controller(const QString &name) const
171 {
172  Q_D(const Context);
173  return d->dispatcher->controllers().value(name);
174 }
175 
177 {
178  Q_D(const Context);
179  return d->view;
180 }
181 
182 View *Context::view(const QString &name) const
183 {
184  Q_D(const Context);
185  return d->app->view(name);
186 }
187 
188 bool Context::setCustomView(const QString &name)
189 {
190  Q_D(Context);
191  d->view = d->app->view(name);
192  return d->view;
193 }
194 
195 QVariantHash &Context::stash()
196 {
197  Q_D(Context);
198  return d->stash;
199 }
200 
201 QVariant Context::stash(const QString &key) const
202 {
203  Q_D(const Context);
204  return d->stash.value(key);
205 }
206 
207 void Context::setStash(const QString &key, const QVariant &value)
208 {
209  Q_D(Context);
210  d->stash.insert(key, value);
211 }
212 
213 void Context::setStash(const QString &key, const ParamsMultiMap &map)
214 {
215  Q_D(Context);
216  d->stash.insert(key, QVariant::fromValue(map));
217 }
218 
219 QStack<Component *> Context::stack() const
220 {
221  Q_D(const Context);
222  return d->stack;
223 }
224 
225 QUrl Context::uriFor(const QString &path, const QStringList &args, const ParamsMultiMap &queryValues) const
226 {
227  Q_D(const Context);
228 
229  QUrl uri = d->request->uri();
230 
231  QString _path = path;
232  if (_path.isEmpty()) {
233  // ns must NOT return a leading slash
234  _path = QLatin1Char('/') + d->action->controller()->ns();
235  } else if (!_path.startsWith(QLatin1Char('/'))) {
236  _path.prepend(QLatin1Char('/'));
237  }
238 
239  if (!args.isEmpty()) {
240  _path = _path + QLatin1Char('/') + args.join(QLatin1Char('/'));
241  }
242  uri.setPath(_path, QUrl::DecodedMode);
243 
244  QUrlQuery query;
245  if (!queryValues.isEmpty()) {
246  // Avoid a trailing '?'
247  if (queryValues.size()) {
248  auto it = queryValues.constEnd();
249  while (it != queryValues.constBegin()) {
250  --it;
251  query.addQueryItem(it.key(), it.value());
252  }
253  }
254  }
255  uri.setQuery(query);
256 
257  return uri;
258 }
259 
260 QUrl Context::uriFor(Action *action, const QStringList &captures, const QStringList &args, const ParamsMultiMap &queryValues) const
261 {
262  Q_D(const Context);
263 
264  QUrl uri;
265  Action *localAction = action;
266  if (!localAction) {
267  localAction = d->action;
268  }
269 
270  QStringList localArgs = args;
271  QStringList localCaptures = captures;
272 
273  Action *expandedAction = d->dispatcher->expandAction(this, action);
274  if (expandedAction->numberOfCaptures() > 0) {
275  while (localCaptures.size() < expandedAction->numberOfCaptures()
276  && localArgs.size()) {
277  localCaptures.append(localArgs.takeFirst());
278  }
279  } else {
280  QStringList localCapturesAux = localCaptures;
281  localCapturesAux.append(localArgs);
282  localArgs = localCapturesAux;
283  localCaptures = QStringList();
284  }
285 
286  const QString path = d->dispatcher->uriForAction(localAction, localCaptures);
287  if (path.isEmpty()) {
288  qCWarning(CUTELYST_CORE) << "Can not find action for" << localAction << localCaptures;
289  return uri;
290  }
291 
292  uri = uriFor(path, localArgs, queryValues);
293  return uri;
294 }
295 
296 QUrl Context::uriForAction(const QString &path, const QStringList &captures, const QStringList &args, const ParamsMultiMap &queryValues) const
297 {
298  Q_D(const Context);
299 
300  QUrl uri;
301  Action *action = d->dispatcher->getActionByPath(path);
302  if (!action) {
303  qCWarning(CUTELYST_CORE) << "Can not find action for" << path;
304  return uri;
305  }
306 
307  uri = uriFor(action, captures, args, queryValues);
308  return uri;
309 }
310 
311 bool Context::detached() const
312 {
313  Q_D(const Context);
314  return d->detached;
315 }
316 
318 {
319  Q_D(Context);
320  if (action) {
321  d->dispatcher->forward(this, action);
322  }
323  d->detached = true;
324 }
325 
327 {
328  Q_D(Context);
329  return d->dispatcher->forward(this, action);
330 }
331 
332 bool Context::forward(const QString &action)
333 {
334  Q_D(Context);
335  return d->dispatcher->forward(this, action);
336 }
337 
338 Action *Context::getAction(const QString &action, const QString &ns) const
339 {
340  Q_D(const Context);
341  return d->dispatcher->getAction(action, ns);
342 }
343 
344 QVector<Action *> Context::getActions(const QString &action, const QString &ns) const
345 {
346  Q_D(const Context);
347  return d->dispatcher->getActions(action, ns);
348 }
349 
350 QVector<Cutelyst::Plugin *> Context::plugins() const
351 {
352  Q_D(const Context);
353  return d->plugins;
354 }
355 
357 {
358  Q_D(Context);
359  Q_ASSERT_X(code, "Context::execute", "trying to execute a null Cutelyst::Component");
360 
361  static int recursion = qEnvironmentVariableIsSet("RECURSION") ? qEnvironmentVariableIntValue("RECURSION") : 1000;
362  if (d->stack.size() >= recursion) {
363  QString msg = QStringLiteral("Deep recursion detected (stack size %1) calling %2, %3")
364  .arg(QString::number(d->stack.size()), code->reverse(), code->name());
365  error(msg);
366  setState(false);
367  return false;
368  }
369 
370  bool ret;
371  d->stack.push(code);
372 
373  if (d->stats) {
374  const QString statsInfo = d->statsStartExecute(code);
375 
376  ret = code->execute(this);
377 
378  if (!statsInfo.isEmpty()) {
379  d->statsFinishExecute(statsInfo);
380  }
381  } else {
382  ret = code->execute(this);
383  }
384 
385  d->stack.pop();
386 
387  return ret;
388 }
389 
390 QLocale Context::locale() const
391 {
392  Q_D(const Context);
393  return d->locale;
394 }
395 
396 void Context::setLocale(const QLocale &locale)
397 {
398  Q_D(Context);
399  d->locale = locale;
400 }
401 
402 QVariant Context::config(const QString &key, const QVariant &defaultValue) const
403 {
404  Q_D(const Context);
405  return d->app->config(key, defaultValue);
406 }
407 
408 QVariantMap Context::config() const
409 {
410  Q_D(const Context);
411  return d->app->config();
412 }
413 
414 QString Context::translate(const char *context, const char *sourceText, const char *disambiguation, int n) const
415 {
416  Q_D(const Context);
417  return d->app->translate(d->locale, context, sourceText, disambiguation, n);
418 }
419 
420 bool Context::wait(uint count)
421 {
422  Q_UNUSED(count)
423 // Q_D(Context);
424 // if (d->loop) {
425 // d->loopWait += count;
426 // return false;
427 // }
428 
429 // if (count) {
430 // d->loopWait = count;
431 // d->loop = new QEventLoop(this);
432 // d->loop->exec();
433 // return true;
434 // }
435  return false;
436 }
437 
438 void Context::next(bool force)
439 {
440 // Q_D(Context);
441  Q_UNUSED(force)
442 // if (!d->loop || (--d->loopWait && !force)) {
443 // return;
444 // }
445 
446 // d->loop->quit();
447 // d->loop->deleteLater();
448 // d->loop = nullptr;
449 }
450 
451 QString ContextPrivate::statsStartExecute(Component *code)
452 {
453  QString actionName;
454  // Skip internal actions
455  if (code->name().startsWith(QLatin1Char('_'))) {
456  return actionName;
457  }
458 
459  actionName = code->reverse();
460 
461  if (qobject_cast<Action *>(code)) {
462  actionName.prepend(QLatin1Char('/'));
463  }
464 
465  if (stack.size() > 2) {
466  actionName = QLatin1String("-> ") + actionName;
467  actionName = actionName.rightJustified(actionName.size() + stack.size() - 2, QLatin1Char(' '));
468  }
469 
470  stats->profileStart(actionName);
471 
472  return actionName;
473 }
474 
475 void ContextPrivate::statsFinishExecute(const QString &statsInfo)
476 {
477  stats->profileEnd(statsInfo);
478 }
479 
480 #include "moc_context.cpp"
481 #include "moc_context_p.cpp"
Context(Application *app)
Constructs a new DUMMY Context object that is child of Application This currently is experimental to ...
Definition: context.cpp:43
bool execute(Context *c)
Definition: component.cpp:70
Application * app() const
Definition: context.cpp:104
QMap< QString, QString > ParamsMultiMap
QString actionName() const
Request * request() const
QIODevice * body() const
Definition: request.cpp:189
Engine * engine() const
Definition: context.cpp:98
virtual qint8 numberOfCaptures() const
Definition: action.cpp:143
bool setCustomView(const QString &name)
Definition: context.cpp:188
Dispatcher * dispatcher() const
Definition: context.cpp:152
Action * action() const
Response * res() const
Definition: context.cpp:116
void setStash(const QString &key, const QVariant &value)
Definition: context.cpp:207
void detach(Action *action=nullptr)
Definition: context.cpp:317
Request * req() const
QUrl uriFor(const QString &path=QString(), const QStringList &args=QStringList(), const ParamsMultiMap &queryValues=ParamsMultiMap()) const
Definition: context.cpp:225
The Cutelyst Component base class.
Definition: component.h:38
QString ns() const
This class represents a Cutelyst Action.
Definition: action.h:47
QVector< Action * > getActions(const QString &action, const QString &ns=QString()) const
Definition: context.cpp:344
QUrl uriForAction(const QString &path, const QStringList &captures=QStringList(), const QStringList &args=QStringList(), const ParamsMultiMap &queryValues=ParamsMultiMap()) const
Definition: context.cpp:296
QStack< Component * > stack() const
Definition: context.cpp:219
QLocale locale() const
Definition: context.cpp:390
Headers & defaultHeaders()
Definition: application.cpp:94
The Cutelyst Context.
Definition: context.h:50
bool forward(Component *component)
Definition: context.cpp:326
Cutelyst Controller base class
Definition: controller.h:102
QString name() const
Definition: component.cpp:39
void next(bool force=false)
This method is deprecated and no longer works, creating local event loops leads to crashes...
Definition: context.cpp:438
QStringList errors() const
Returns a list of errors that were defined.
Definition: context.cpp:80
Response * response() const
Definition: context.cpp:110
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:414
View * view(const QString &name=QString()) const
Definition: context.cpp:182
QVariantMap config() const
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
QString reverse() const
Definition: component.cpp:51
Action * getAction(const QString &action, const QString &ns=QString()) const
Definition: context.cpp:338
bool execute(Component *code)
Definition: context.cpp:356
void setState(bool state)
Sets the state of the current executed action, setting to false will make the dispatcher skip non pro...
Definition: context.cpp:92
QVector< Plugin * > plugins() const
Definition: context.cpp:350
QVariantHash & stash()
Definition: context.cpp:195
void setLocale(const QLocale &locale)
Definition: context.cpp:396
bool detached() const
Definition: context.cpp:311
Cutelyst View abstract view component
Definition: view.h:33
Controller * controller() const
View * customView() const
Definition: context.cpp:176
The Cutelyst Application.
Definition: application.h:55
The Cutelyst Engine.
Definition: engine.h:33
The Cutelyst Dispatcher.
Definition: dispatcher.h:40
Q_DECL_DEPRECATED bool wait(uint count=1)
This method is deprecated and no longer works, creating local event loops leads to crashes...
Definition: context.cpp:420
bool state() const
bool error() const
Returns true if an error was set.
Definition: context.cpp:63
QString controllerName() const