5#include "actionchain.h"
8#include "dispatchtypechained_p.h"
18 , d_ptr(new DispatchTypeChainedPrivate)
32 Actions endPoints = d->endPoints;
33 std::ranges::sort(endPoints,
38 for (
Action *endPoint : std::as_const(endPoints)) {
40 if (endPoint->numberOfArgs() == -1) {
43 for (
int i = 0; i < endPoint->numberOfArgs(); ++i) {
49 QString extra = DispatchTypeChainedPrivate::listExtraHttpMethods(endPoint);
50 QString consumes = DispatchTypeChainedPrivate::listExtraConsumes(endPoint);
52 Action *current = endPoint;
60 for (
const QString &part : pathParts) {
61 if (!part.isEmpty()) {
67 current = d->actions.value(
parent);
73 if (
parent.compare(u
"/") != 0) {
81 unattachedTable.
append(row);
86 for (
Action *p : parents) {
89 QString extraHttpMethod = DispatchTypeChainedPrivate::listExtraHttpMethods(p);
90 if (!extraHttpMethod.
isEmpty()) {
94 const auto attributes = p->attributes();
96 if (it != attributes.constEnd()) {
102 QString ct = DispatchTypeChainedPrivate::listExtraConsumes(p);
107 if (p != parents[0]) {
122 if (endPoint->numberOfArgs() == -1) {
140 if (!paths.isEmpty()) {
141 out << Utils::buildTable(paths,
146 if (!unattachedTable.
isEmpty()) {
147 out << Utils::buildTable(unattachedTable,
164 const BestActionMatch ret = d->recurseMatch(args.
size(), u
"/"_s, path.
mid(1).
split(u
'/'));
166 if (ret.isNull || chain.
isEmpty()) {
171 const auto parts = ret.parts;
172 for (
const auto &arg : parts) {
174 decodedArgs.
append(Utils::decodePercentEncoding(&aux));
181 for (
const auto a : ret.captures) {
182 captures.
append(a.toString());
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"
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;
323 if (qobject_cast<ActionChain *>(action)) {
338 curr = d->actions.value(
parent);
348 if (d->actions.isEmpty()) {
357BestActionMatch DispatchTypeChainedPrivate::recurseMatch(
int reqArgsSize,
361 BestActionMatch bestAction;
362 const auto it = childrenOf.constFind(parent);
363 if (it == childrenOf.constEnd()) {
367 const StringActionsMap &children = it.value();
369 std::ranges::sort(keys, [](
const QString &a,
const QString &b) ->
bool {
374 for (
const QString &tryPart : std::as_const(keys)) {
375 auto parts = pathParts;
376 if (!tryPart.isEmpty()) {
379 int tryPartCount = tryPart.
count(u
'/') + 1;
380 const auto possibleParts = parts.mid(0, tryPartCount);
384 for (
const auto part : possibleParts) {
386 possiblePartsString = part.toString();
389 possiblePartsString.
append(u
'/' + part);
393 if (tryPart != possiblePartsString) {
396 parts = parts.
mid(tryPartCount);
399 const Actions tryActions = children.value(tryPart);
400 for (
Action *action : tryActions) {
402 if (attributes.
contains(u
"CaptureArgs"_s)) {
403 const auto captureCount = action->numberOfCaptures();
405 if (parts.size() < captureCount) {
410 const auto captures = parts.mid(0, captureCount);
413 if (!action->matchCaptures(captures.size())) {
417 const auto localParts = parts.mid(captureCount);
420 const BestActionMatch ret =
421 recurseMatch(reqArgsSize,
QLatin1Char(
'/') + action->reverse(), localParts);
427 const auto actionCaptures = ret.captures;
428 const auto actionParts = ret.parts;
429 int bestActionParts = bestAction.parts.
size();
432 (bestAction.isNull || actionParts.size() < bestActionParts ||
433 (actionParts.size() == bestActionParts &&
434 actionCaptures.size() < bestAction.captures.size() &&
435 ret.n_pathParts > bestAction.n_pathParts))) {
437 int pathparts = attributes.
value(u
"PathPart"_s).count(u
'/') + 1;
438 bestAction.actions = actions;
439 bestAction.captures = captures + actionCaptures;
440 bestAction.parts = actionParts;
441 bestAction.n_pathParts = pathparts + ret.n_pathParts;
442 bestAction.isNull =
false;
445 if (!action->match(reqArgsSize + parts.size())) {
450 const int pathparts = attributes.
value(u
"PathPart"_s).count(u
'/') + 1;
458 if (bestAction.isNull || parts.size() < bestAction.parts.size() ||
459 (parts.isEmpty() && !argsAttr.
isEmpty() && action->numberOfArgs() == 0)) {
460 bestAction.actions = {action};
461 bestAction.captures = {};
462 bestAction.parts = parts;
463 bestAction.n_pathParts = pathparts;
464 bestAction.isNull =
false;
473bool DispatchTypeChainedPrivate::checkArgsAttr(
Action *action,
const QString &name)
const
481 if (values.
size() > 1) {
482 qCCritical(CUTELYST_DISPATCHER_CHAINED)
483 <<
"Multiple" << name <<
"attributes not supported registering" << action->
reverse();
490 qCCritical(CUTELYST_DISPATCHER_CHAINED)
491 <<
"Invalid" << name <<
"(" << args <<
") for action" << action->
reverse() <<
"(use '"
492 << name <<
"' or '" << name <<
"(<number>)')";
499QString DispatchTypeChainedPrivate::listExtraHttpMethods(
Action *action)
510QString DispatchTypeChainedPrivate::listExtraConsumes(
Action *action)
521#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
Describes a chained dispatch type.
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)
Abstract class to described a dispatch type.
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.
void append(QList::parameter_type value)
qsizetype count() const const
bool isEmpty() const const
QList< T > mid(qsizetype pos, qsizetype length) const const
void prepend(QList::parameter_type value)
qsizetype size() 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
QString & append(QChar ch)
bool isEmpty() const const
QString mid(qsizetype position, qsizetype n) const const
QString number(double n, char format, int precision)
QString & prepend(QChar ch)
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
qsizetype size() 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
QList< QStringView > split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const