18 #include "dispatchtypechained_p.h"
20 #include "actionchain.h"
24 #include <QtCore/QUrl>
29 , d_ptr(new DispatchTypeChainedPrivate)
34 DispatchTypeChained::~DispatchTypeChained()
44 Actions endPoints = d->endPoints;
45 std::sort(endPoints.begin(), endPoints.end(), [](
Action *a,
Action *b) ->
bool {
46 return a->reverse() < b->reverse();
51 for (
Action *endPoint : endPoints) {
53 if (endPoint->numberOfArgs() == -1) {
56 for (
int i = 0; i < endPoint->numberOfArgs(); ++i) {
62 QString extra = DispatchTypeChainedPrivate::listExtraHttpMethods(endPoint);
63 QString consumes = DispatchTypeChainedPrivate::listExtraConsumes(endPoint);
65 Action *current = endPoint;
73 for (
const QString &part : pathParts) {
74 if (!part.isEmpty()) {
80 current = d->actions.value(
parent);
94 unattachedTable.
append(row);
99 for (
Action *p : parents) {
102 QString extraHttpMethod = DispatchTypeChainedPrivate::listExtraHttpMethods(p);
103 if (!extraHttpMethod.
isEmpty()) {
107 const auto attributes = p->attributes();
108 auto it = attributes.constFind(
QLatin1String(
"CaptureArgs"));
109 if (it != attributes.constEnd()) {
115 QString ct = DispatchTypeChainedPrivate::listExtraConsumes(p);
120 if (p != parents[0]) {
135 if (endPoint->numberOfArgs() == -1) {
150 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
161 if (!unattachedTable.
isEmpty()) {
177 const BestActionMatch ret = d->recurseMatch(args.
size(), QStringLiteral(
"/"), path.
split(
QLatin1Char(
'/')));
179 if (ret.isNull || chain.
isEmpty()) {
185 for (
const QString &arg : parts) {
187 decodedArgs.
append(Utils::decodePercentEncoding(&aux));
191 Request *request = c->request();
210 if (chainedList.
size() > 1) {
211 qCCritical(CUTELYST_DISPATCHER_CHAINED)
212 <<
"Multiple Chained attributes not supported registering" << action->
reverse();
218 qCCritical(CUTELYST_DISPATCHER_CHAINED)
219 <<
"Actions cannot chain to themselves registering /" << action->
name();
227 if (pathPart.
size() == 1 && !pathPart[0].
isEmpty()) {
229 }
else if (pathPart.
size() > 1) {
230 qCCritical(CUTELYST_DISPATCHER_CHAINED)
231 <<
"Multiple PathPart attributes not supported registering"
237 qCCritical(CUTELYST_DISPATCHER_CHAINED)
238 <<
"Absolute parameters to PathPart not allowed registering"
243 attributes.
insert(QStringLiteral(
"PathPart"), part);
246 auto &childrenOf = d->childrenOf[chainedTo][part];
247 childrenOf.insert(childrenOf.begin(), action);
257 qCCritical(CUTELYST_DISPATCHER_CHAINED)
258 <<
"Combining Args and CaptureArgs attributes not supported registering"
276 if (!(attributes.
contains(QStringLiteral(
"Chained")) &&
277 !attributes.
contains(QStringLiteral(
"CaptureArgs")))) {
278 qCWarning(CUTELYST_DISPATCHER_CHAINED) <<
"uriForAction: action is not an end point" << action;
288 if (curr_attributes.
contains(QStringLiteral(
"CaptureArgs"))) {
291 qCWarning(CUTELYST_DISPATCHER_CHAINED) <<
"uriForAction: not enough captures" << curr->
numberOfCaptures() << captures.
size();
299 const QString pp = curr_attributes.
value(QStringLiteral(
"PathPart"));
304 parent = curr_attributes.
value(QStringLiteral(
"Chained"));
305 curr = d->actions.value(
parent);
310 qCWarning(CUTELYST_DISPATCHER_CHAINED) <<
"uriForAction: dangling action" <<
parent;
314 if (!localCaptures.
isEmpty()) {
316 qCWarning(CUTELYST_DISPATCHER_CHAINED) <<
"uriForAction: too many captures" << localCaptures;
329 if (qobject_cast<ActionChain*>(action)) {
344 curr = d->actions.value(
parent);
354 if (d->actions.isEmpty()) {
363 BestActionMatch DispatchTypeChainedPrivate::recurseMatch(
int reqArgsSize,
const QString &parent,
const QStringList &pathParts)
const
365 BestActionMatch bestAction;
366 auto it = childrenOf.constFind(parent);
367 if (it == childrenOf.constEnd()) {
371 const StringActionsMap &children = it.value();
375 return b.size() < a.size();
378 for (
const QString &tryPart : keys) {
380 if (!tryPart.isEmpty()) {
388 parts = parts.
mid(tryPartCount);
391 const Actions tryActions = children.value(tryPart);
392 for (
Action *action : tryActions) {
394 if (attributes.
contains(QStringLiteral(
"CaptureArgs"))) {
395 const int captureCount = action->numberOfCaptures();
397 if (parts.
size() < captureCount) {
405 if (!action->matchCaptures(captures.
size())) {
412 const BestActionMatch ret = recurseMatch(reqArgsSize,
QLatin1Char(
'/') + action->reverse(), localParts);
420 int bestActionParts = bestAction.parts.
size();
423 (bestAction.isNull ||
424 actionParts.
size() < bestActionParts ||
425 (actionParts.
size() == bestActionParts &&
426 actionCaptures.
size() < bestAction.captures.size() &&
427 ret.n_pathParts > bestAction.n_pathParts))) {
429 int pathparts = attributes.
value(QStringLiteral(
"PathPart")).count(
QLatin1Char(
'/')) + 1;
430 bestAction.actions = actions;
431 bestAction.captures = captures + actionCaptures;
432 bestAction.parts = actionParts;
433 bestAction.n_pathParts = pathparts + ret.n_pathParts;
434 bestAction.isNull =
false;
437 if (!action->match(reqArgsSize + parts.
size())) {
441 const QString argsAttr = attributes.
value(QStringLiteral(
"Args"));
442 const int pathparts = attributes.
value(QStringLiteral(
"PathPart")).count(
QLatin1Char(
'/')) + 1;
450 if (bestAction.isNull ||
451 parts.
size() < bestAction.parts.size() ||
452 (parts.
isEmpty() && !argsAttr.
isEmpty() && action->numberOfArgs() == 0)) {
453 bestAction.actions = { action };
455 bestAction.parts = parts;
456 bestAction.n_pathParts = pathparts;
457 bestAction.isNull =
false;
466 bool DispatchTypeChainedPrivate::checkArgsAttr(
Action *action,
const QString &name)
const
474 if (values.
size() > 1) {
475 qCCritical(CUTELYST_DISPATCHER_CHAINED)
478 <<
"attributes not supported registering"
486 qCCritical(CUTELYST_DISPATCHER_CHAINED)
488 << name <<
"(" << args <<
") for action"
490 <<
"(use '" << name <<
"' or '" << name <<
"(<number>)')";
497 QString DispatchTypeChainedPrivate::listExtraHttpMethods(
Action *action)
508 QString DispatchTypeChainedPrivate::listExtraConsumes(
Action *action)
519 #include "moc_dispatchtypechained.cpp"
Holds a chain of Cutelyst Actions.
This class represents a Cutelyst Action.
void setAttributes(const ParamsMultiMap &attributes)
QString attribute(const QString &name, const QString &defaultValue=QString()) const
ParamsMultiMap attributes() const
virtual qint8 numberOfCaptures() 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)
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(const T &value)
int count(const T &value) const const
bool isEmpty() const const
QList< T > mid(int pos, int length) const const
void prepend(const T &value)
const T value(const Key &key, const T &defaultValue) const const
bool contains(const Key &key, const T &value) const const
QList< T > values(const Key &key) const const
QObject * parent() const const
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QString & append(QChar ch)
QString & insert(int position, QChar ch)
bool isEmpty() const const
QString number(int n, int base)
QString & prepend(QChar ch)
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
int toInt(bool *ok, int base) const const
QString join(const QString &separator) const const
void append(const T &value)
bool isEmpty() const const