5#include "actionchain.h"
8#include "dispatchtypechained_p.h"
18 , d_ptr(new DispatchTypeChainedPrivate)
32 Actions endPoints = d->endPoints;
33 std::sort(endPoints.begin(), endPoints.end(), [](
Action *a,
Action *b) ->
bool {
34 return a->reverse() < b->reverse();
39 for (
Action *endPoint : endPoints) {
41 if (endPoint->numberOfArgs() == -1) {
44 for (
int i = 0; i < endPoint->numberOfArgs(); ++i) {
50 QString extra = DispatchTypeChainedPrivate::listExtraHttpMethods(endPoint);
51 QString consumes = DispatchTypeChainedPrivate::listExtraConsumes(endPoint);
53 Action *current = endPoint;
61 for (
const QString &part : pathParts) {
62 if (!part.isEmpty()) {
68 current = d->actions.value(
parent);
70 parents.prepend(current);
74 if (
parent.compare(u
"/") != 0) {
76 if (parents.isEmpty()) {
82 unattachedTable.
append(row);
87 for (
Action *p : parents) {
90 QString extraHttpMethod = DispatchTypeChainedPrivate::listExtraHttpMethods(p);
91 if (!extraHttpMethod.
isEmpty()) {
95 const auto attributes = p->attributes();
97 if (it != attributes.constEnd()) {
103 QString ct = DispatchTypeChainedPrivate::listExtraConsumes(p);
108 if (p != parents[0]) {
123 if (endPoint->numberOfArgs() == -1) {
142 out << Utils::buildTable(paths,
147 if (!unattachedTable.
isEmpty()) {
148 out << Utils::buildTable(unattachedTable,
167 const BestActionMatch ret =
170 if (ret.isNull || chain.isEmpty()) {
176 for (
const QString &arg : parts) {
178 decodedArgs.
append(Utils::decodePercentEncoding(&aux));
201 if (chainedList.
size() > 1) {
202 qCCritical(CUTELYST_DISPATCHER_CHAINED)
203 <<
"Multiple Chained attributes not supported registering" << action->
reverse();
208 if (chainedTo == u
'/' + action->
name()) {
209 qCCritical(CUTELYST_DISPATCHER_CHAINED)
210 <<
"Actions cannot chain to themselves registering /" << action->
name();
218 if (pathPart.
size() == 1 && !pathPart[0].
isEmpty()) {
220 }
else if (pathPart.
size() > 1) {
221 qCCritical(CUTELYST_DISPATCHER_CHAINED)
222 <<
"Multiple PathPart attributes not supported registering" << action->
reverse();
227 qCCritical(CUTELYST_DISPATCHER_CHAINED)
228 <<
"Absolute parameters to PathPart not allowed registering" << action->
reverse();
232 attributes.
replace(QStringLiteral(
"PathPart"), part);
235 auto &childrenOf = d->childrenOf[chainedTo][part];
236 childrenOf.insert(childrenOf.begin(), action);
247 qCCritical(CUTELYST_DISPATCHER_CHAINED)
248 <<
"Combining Args and CaptureArgs attributes not supported registering"
254 d->endPoints.push_back(action);
266 if (!(attributes.
contains(QStringLiteral(
"Chained")) &&
267 !attributes.
contains(QStringLiteral(
"CaptureArgs")))) {
268 qCWarning(CUTELYST_DISPATCHER_CHAINED)
269 <<
"uriForAction: action is not an end point" << action;
279 if (curr_attributes.
contains(QStringLiteral(
"CaptureArgs"))) {
282 qCWarning(CUTELYST_DISPATCHER_CHAINED)
292 const QString pp = curr_attributes.
value(QStringLiteral(
"PathPart"));
297 parent = curr_attributes.
value(QStringLiteral(
"Chained"));
298 curr = d->actions.value(
parent);
301 if (
parent.compare(u
"/") != 0) {
303 qCWarning(CUTELYST_DISPATCHER_CHAINED) <<
"uriForAction: dangling action" <<
parent;
307 if (!localCaptures.
isEmpty()) {
309 qCWarning(CUTELYST_DISPATCHER_CHAINED)
310 <<
"uriForAction: too many captures" << localCaptures;
338 curr = d->actions.value(
parent);
348 if (d->actions.isEmpty()) {
357BestActionMatch DispatchTypeChainedPrivate::recurseMatch(
int reqArgsSize,
361 BestActionMatch bestAction;
362 auto it = childrenOf.constFind(parent);
363 if (it == childrenOf.constEnd()) {
367 const StringActionsMap &children = it.value();
371 return b.size() < a.size();
374 for (
const QString &tryPart : keys) {
376 if (!tryPart.isEmpty()) {
379 int tryPartCount = tryPart.count(
QLatin1Char(
'/')) + 1;
384 parts = parts.
mid(tryPartCount);
387 const Actions tryActions = children.
value(tryPart);
388 for (
Action *action : tryActions) {
390 if (attributes.
contains(QStringLiteral(
"CaptureArgs"))) {
391 const int captureCount = action->numberOfCaptures();
393 if (parts.
size() < captureCount) {
398 const QStringList captures = parts.
mid(0, captureCount);
401 if (!action->matchCaptures(captures.
size())) {
405 const QStringList localParts = parts.
mid(captureCount);
408 const BestActionMatch ret =
409 recurseMatch(reqArgsSize, QLatin1Char(
'/') + action->reverse(), localParts);
415 const QStringList actionCaptures = ret.captures;
416 const QStringList actionParts = ret.parts;
417 int bestActionParts = bestAction.parts.size();
419 if (!actions.isEmpty() &&
420 (bestAction.isNull || actionParts.
size() < bestActionParts ||
421 (actionParts.
size() == bestActionParts &&
422 actionCaptures.
size() < bestAction.captures.size() &&
423 ret.n_pathParts > bestAction.n_pathParts))) {
424 actions.prepend(action);
426 attributes.
value(QStringLiteral(
"PathPart")).count(QLatin1Char(
'/')) + 1;
427 bestAction.actions = actions;
428 bestAction.captures = captures + actionCaptures;
429 bestAction.parts = actionParts;
430 bestAction.n_pathParts = pathparts + ret.n_pathParts;
431 bestAction.isNull =
false;
434 if (!action->match(reqArgsSize + parts.
size())) {
438 const QString argsAttr = attributes.
value(QStringLiteral(
"Args"));
439 const int pathparts =
440 attributes.
value(QStringLiteral(
"PathPart")).count(QLatin1Char(
'/')) + 1;
448 if (bestAction.isNull || parts.
size() < bestAction.parts.size() ||
449 (parts.
isEmpty() && !argsAttr.
isEmpty() && action->numberOfArgs() == 0)) {
450 bestAction.actions = {action};
451 bestAction.captures = QStringList();
452 bestAction.parts = parts;
453 bestAction.n_pathParts = pathparts;
454 bestAction.isNull =
false;
463bool DispatchTypeChainedPrivate::checkArgsAttr(
Action *action,
const QString &name)
const
470 const QStringList values = attributes.
values(name);
471 if (values.
size() > 1) {
472 qCCritical(CUTELYST_DISPATCHER_CHAINED)
473 <<
"Multiple" << name <<
"attributes not supported registering" << action->
reverse();
477 QString args = values[0];
480 qCCritical(CUTELYST_DISPATCHER_CHAINED)
481 <<
"Invalid" << name <<
"(" << args <<
") for action" << action->
reverse() <<
"(use '"
482 << name <<
"' or '" << name <<
"(<number>)')";
489QString DispatchTypeChainedPrivate::listExtraHttpMethods(
Action *action)
493 if (attributes.
contains(QLatin1String(
"HTTP_METHODS"))) {
494 const QStringList extra = attributes.
values(QLatin1String(
"HTTP_METHODS"));
495 ret = extra.
join(QLatin1String(
", "));
500QString DispatchTypeChainedPrivate::listExtraConsumes(
Action *action)
504 if (attributes.
contains(QLatin1String(
"CONSUMES"))) {
505 const QStringList extra = attributes.
values(QLatin1String(
"CONSUMES"));
506 ret = extra.
join(QLatin1String(
", "));
511#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
ParamsMultiMap attributes() const noexcept
QString attribute(const QString &name, const QString &defaultValue={}) const
QString reverse() const noexcept
QString name() const noexcept
MatchType match(Context *c, QStringView path, const QStringList &args) const override
QString uriForAction(Action *action, const QStringList &captures) const override
QByteArray list() const override
bool registerAction(Action *action) override
Action * expandAction(const Context *c, Action *action) const final
~DispatchTypeChained() override
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)
QMultiMap< QString, QString > ParamsMultiMap
The Cutelyst namespace holds all public Cutelyst API.
QVector< Action * > ActionList
void append(QList< T > &&value)
QList< T >::iterator begin()
QList< T >::iterator end()
bool isEmpty() const const
QList< T > mid(qsizetype pos, qsizetype length) const const
void prepend(QList< T >::parameter_type value)
qsizetype size() const const
T value(qsizetype i) const const
bool contains(const Key &key) const const
T value(const Key &key, const T &defaultValue) const const
QList< T > values() const const
QObject * parent() const const
T qobject_cast(QObject *object)
QString & append(QChar ch)
bool isEmpty() const const
QString number(double n, char format, int precision)
QString & prepend(QChar ch)
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
int toInt(bool *ok, int base) const const
QString join(QChar separator) const const
QStringView mid(qsizetype start, qsizetype length) const const
QString toString() const const