5#include "actionchain.h"
8#include "dispatchtypechained_p.h"
17 , d_ptr(new DispatchTypeChainedPrivate)
21DispatchTypeChained::~DispatchTypeChained()
31 Actions endPoints = d->endPoints;
32 std::sort(endPoints.begin(), endPoints.end(), [](
Action *a,
Action *b) ->
bool {
33 return a->reverse() < b->reverse();
36 QVector<QStringList> paths;
37 QVector<QStringList> unattachedTable;
38 for (
Action *endPoint : endPoints) {
40 if (endPoint->numberOfArgs() == -1) {
41 parts.append(QLatin1String(
"..."));
43 for (
int i = 0; i < endPoint->numberOfArgs(); ++i) {
44 parts.append(QLatin1String(
"*"));
49 QString extra = DispatchTypeChainedPrivate::listExtraHttpMethods(endPoint);
50 QString consumes = DispatchTypeChainedPrivate::listExtraConsumes(endPoint);
52 Action *current = endPoint;
55 parts.prepend(QLatin1String(
"*"));
59 const QStringList pathParts = attributes.values(QLatin1String(
"PathPart"));
60 for (
const QString &part : pathParts) {
61 if (!part.isEmpty()) {
66 parent = attributes.value(QLatin1String(
"Chained"));
67 current = d->actions.value(parent);
69 parents.prepend(current);
73 if (parent.compare(u
"/") != 0) {
75 if (parents.isEmpty()) {
76 row.append(QLatin1Char(
'/') + endPoint->reverse());
78 row.append(QLatin1Char(
'/') + parents.first()->reverse());
81 unattachedTable.append(row);
85 QVector<QStringList> rows;
86 for (
Action *p : parents) {
87 QString name = QLatin1Char(
'/') + p->reverse();
89 QString extraHttpMethod = DispatchTypeChainedPrivate::listExtraHttpMethods(p);
90 if (!extraHttpMethod.isEmpty()) {
91 name.prepend(extraHttpMethod + QLatin1Char(
' '));
94 const auto attributes = p->attributes();
95 auto it = attributes.constFind(QLatin1String(
"CaptureArgs"));
96 if (it != attributes.constEnd()) {
97 name.append(QLatin1String(
" (") + it.value() + QLatin1Char(
')'));
99 name.append(QLatin1String(
" (0)"));
102 QString ct = DispatchTypeChainedPrivate::listExtraConsumes(p);
104 name.append(QLatin1String(
" :") + ct);
107 if (p != parents[0]) {
108 name = QLatin1String(
"-> ") + name;
111 rows.append({QString(), name});
115 if (!rows.isEmpty()) {
116 line.append(QLatin1String(
"=> "));
118 if (!extra.isEmpty()) {
119 line.append(extra + QLatin1Char(
' '));
121 line.append(QLatin1Char(
'/') + endPoint->reverse());
122 if (endPoint->numberOfArgs() == -1) {
123 line.append(QLatin1String(
" (...)"));
125 line.append(QLatin1String(
" (") + QString::number(endPoint->numberOfArgs()) +
129 if (!consumes.isEmpty()) {
130 line.append(QLatin1String(
" :") + consumes);
132 rows.append({QString(), line});
134 rows[0][0] = QLatin1Char(
'/') + parts.join(QLatin1Char(
'/'));
138#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
139 QTextStream out(&buffer, QTextStream::WriteOnly);
141 QTextStream out(&buffer, QIODevice::WriteOnly);
144 if (!paths.isEmpty()) {
145 out << Utils::buildTable(paths,
146 {QLatin1String(
"Path Spec"), QLatin1String(
"Private")},
147 QLatin1String(
"Loaded Chained actions:"));
150 if (!unattachedTable.isEmpty()) {
151 out << Utils::buildTable(unattachedTable,
152 {QLatin1String(
"Private"), QLatin1String(
"Missing parent")},
153 QLatin1String(
"Unattached Chained actions:"));
162 if (!args.isEmpty()) {
168 const BestActionMatch ret =
169 d->recurseMatch(args.size(), QStringLiteral(
"/"), path.split(QLatin1Char(
'/')));
171 if (ret.isNull || chain.isEmpty()) {
175 QStringList decodedArgs;
176 const QStringList parts = ret.parts;
177 for (
const QString &arg : parts) {
179 decodedArgs.append(Utils::decodePercentEncoding(&aux));
183 Request *request = c->request();
197 const QStringList chainedList = attributes.values(QLatin1String(
"Chained"));
198 if (chainedList.isEmpty()) {
202 if (chainedList.size() > 1) {
203 qCCritical(CUTELYST_DISPATCHER_CHAINED)
204 <<
"Multiple Chained attributes not supported registering" << action->
reverse();
208 const QString chainedTo = chainedList.first();
209 if (chainedTo == u
'/' + action->
name()) {
210 qCCritical(CUTELYST_DISPATCHER_CHAINED)
211 <<
"Actions cannot chain to themselves registering /" << action->
name();
215 const QStringList pathPart = attributes.values(QLatin1String(
"PathPart"));
217 QString part = action->
name();
219 if (pathPart.size() == 1 && !pathPart[0].isEmpty()) {
221 }
else if (pathPart.size() > 1) {
222 qCCritical(CUTELYST_DISPATCHER_CHAINED)
223 <<
"Multiple PathPart attributes not supported registering" << action->
reverse();
227 if (part.startsWith(QLatin1Char(
'/'))) {
228 qCCritical(CUTELYST_DISPATCHER_CHAINED)
229 <<
"Absolute parameters to PathPart not allowed registering" << action->
reverse();
233 attributes.replace(QStringLiteral(
"PathPart"), part);
236 auto &childrenOf = d->childrenOf[chainedTo][part];
237 childrenOf.insert(childrenOf.begin(), action);
239 d->actions[QLatin1Char(
'/') + action->
reverse()] = action;
241 if (!d->checkArgsAttr(action, QLatin1String(
"Args")) ||
242 !d->checkArgsAttr(action, QLatin1String(
"CaptureArgs"))) {
246 if (attributes.contains(QLatin1String(
"Args")) &&
247 attributes.contains(QLatin1String(
"CaptureArgs"))) {
248 qCCritical(CUTELYST_DISPATCHER_CHAINED)
249 <<
"Combining Args and CaptureArgs attributes not supported registering"
254 if (!attributes.contains(QLatin1String(
"CaptureArgs"))) {
255 d->endPoints.push_back(action);
267 if (!(attributes.contains(QStringLiteral(
"Chained")) &&
268 !attributes.contains(QStringLiteral(
"CaptureArgs")))) {
269 qCWarning(CUTELYST_DISPATCHER_CHAINED)
270 <<
"uriForAction: action is not an end point" << action;
275 QStringList localCaptures = captures;
280 if (curr_attributes.contains(QStringLiteral(
"CaptureArgs"))) {
283 qCWarning(CUTELYST_DISPATCHER_CHAINED)
289 parts = localCaptures.mid(localCaptures.size() - curr->
numberOfCaptures()) + parts;
290 localCaptures = localCaptures.mid(0, localCaptures.size() - curr->
numberOfCaptures());
293 const QString pp = curr_attributes.value(QStringLiteral(
"PathPart"));
298 parent = curr_attributes.value(QStringLiteral(
"Chained"));
299 curr = d->actions.value(parent);
302 if (parent.compare(u
"/") != 0) {
304 qCWarning(CUTELYST_DISPATCHER_CHAINED) <<
"uriForAction: dangling action" << parent;
308 if (!localCaptures.isEmpty()) {
310 qCWarning(CUTELYST_DISPATCHER_CHAINED)
311 <<
"uriForAction: too many captures" << localCaptures;
315 ret = QLatin1Char(
'/') + parts.join(QLatin1Char(
'/'));
324 if (qobject_cast<ActionChain *>(action)) {
329 if (!action->
attributes().contains(QStringLiteral(
"Chained"))) {
338 const QString parent = curr->
attribute(QStringLiteral(
"Chained"));
339 curr = d->actions.value(parent);
349 if (d->actions.isEmpty()) {
358BestActionMatch DispatchTypeChainedPrivate::recurseMatch(
int reqArgsSize,
359 const QString &parent,
360 const QStringList &pathParts)
const
362 BestActionMatch bestAction;
363 auto it = childrenOf.constFind(parent);
364 if (it == childrenOf.constEnd()) {
368 const StringActionsMap &children = it.value();
369 QStringList keys = children.keys();
370 std::sort(keys.begin(), keys.end(), [](
const QString &a,
const QString &b) ->
bool {
372 return b.size() < a.size();
375 for (
const QString &tryPart : keys) {
376 QStringList parts = pathParts;
377 if (!tryPart.isEmpty()) {
380 int tryPartCount = tryPart.count(QLatin1Char(
'/')) + 1;
381 const QStringList possiblePart = parts.mid(0, tryPartCount);
382 if (tryPart != possiblePart.join(QLatin1Char(
'/'))) {
385 parts = parts.mid(tryPartCount);
388 const Actions tryActions = children.value(tryPart);
389 for (
Action *action : tryActions) {
391 if (attributes.contains(QStringLiteral(
"CaptureArgs"))) {
392 const int captureCount = action->numberOfCaptures();
394 if (parts.size() < captureCount) {
399 const QStringList captures = parts.mid(0, captureCount);
402 if (!action->matchCaptures(captures.size())) {
406 const QStringList localParts = parts.mid(captureCount);
409 const BestActionMatch ret =
410 recurseMatch(reqArgsSize, QLatin1Char(
'/') + action->reverse(), localParts);
416 const QStringList actionCaptures = ret.captures;
417 const QStringList actionParts = ret.parts;
418 int bestActionParts = bestAction.parts.size();
420 if (!actions.isEmpty() &&
421 (bestAction.isNull || actionParts.size() < bestActionParts ||
422 (actionParts.size() == bestActionParts &&
423 actionCaptures.size() < bestAction.captures.size() &&
424 ret.n_pathParts > bestAction.n_pathParts))) {
425 actions.prepend(action);
427 attributes.value(QStringLiteral(
"PathPart")).count(QLatin1Char(
'/')) + 1;
428 bestAction.actions = actions;
429 bestAction.captures = captures + actionCaptures;
430 bestAction.parts = actionParts;
431 bestAction.n_pathParts = pathparts + ret.n_pathParts;
432 bestAction.isNull =
false;
435 if (!action->match(reqArgsSize + parts.size())) {
439 const QString argsAttr = attributes.value(QStringLiteral(
"Args"));
440 const int pathparts =
441 attributes.value(QStringLiteral(
"PathPart")).count(QLatin1Char(
'/')) + 1;
449 if (bestAction.isNull || parts.size() < bestAction.parts.size() ||
450 (parts.isEmpty() && !argsAttr.isEmpty() && action->numberOfArgs() == 0)) {
451 bestAction.actions = {action};
452 bestAction.captures = QStringList();
453 bestAction.parts = parts;
454 bestAction.n_pathParts = pathparts;
455 bestAction.isNull =
false;
464bool DispatchTypeChainedPrivate::checkArgsAttr(
Action *action,
const QString &name)
const
467 if (!attributes.contains(name)) {
471 const QStringList values = attributes.values(name);
472 if (values.size() > 1) {
473 qCCritical(CUTELYST_DISPATCHER_CHAINED)
474 <<
"Multiple" << name <<
"attributes not supported registering" << action->
reverse();
478 QString args = values[0];
480 if (!args.isEmpty() && args.toInt(&ok) < 0 && !ok) {
481 qCCritical(CUTELYST_DISPATCHER_CHAINED)
482 <<
"Invalid" << name <<
"(" << args <<
") for action" << action->
reverse() <<
"(use '"
483 << name <<
"' or '" << name <<
"(<number>)')";
490QString DispatchTypeChainedPrivate::listExtraHttpMethods(
Action *action)
494 if (attributes.contains(QLatin1String(
"HTTP_METHODS"))) {
495 const QStringList extra = attributes.values(QLatin1String(
"HTTP_METHODS"));
496 ret = extra.join(QLatin1String(
", "));
501QString DispatchTypeChainedPrivate::listExtraConsumes(
Action *action)
505 if (attributes.contains(QLatin1String(
"CONSUMES"))) {
506 const QStringList extra = attributes.values(QLatin1String(
"CONSUMES"));
507 ret = extra.join(QLatin1String(
", "));
512#include "moc_dispatchtypechained.cpp"
Holds a chain of Cutelyst Actions.
This class represents a Cutelyst Action.
void setAttributes(const ParamsMultiMap &attributes)
virtual qint8 numberOfCaptures() const noexcept
ParamsMultiMap attributes() const noexcept
QString attribute(const QString &name, const QString &defaultValue={}) const
virtual bool inUse() override
virtual QString uriForAction(Action *action, const QStringList &captures) const override
virtual QByteArray list() const override
list the registered actions To be implemented by subclasses
virtual MatchType match(Context *c, const QString &path, const QStringList &args) const override
virtual bool registerAction(Action *action) override
registerAction
Action * expandAction(const Context *c, Action *action) const final
DispatchTypeChained(QObject *parent=nullptr)
DispatchType(QObject *parent=nullptr)
void setupMatchedAction(Context *c, Action *action) const
void setCaptures(const QStringList &captures)
void setArguments(const QStringList &arguments)
void setMatch(const QString &match)
The Cutelyst namespace holds all public Cutelyst API.
QMultiMap< QString, QString > ParamsMultiMap
QVector< Action * > ActionList