5 #include "dispatchtypechained_p.h" 7 #include "actionchain.h" 11 #include <QtCore/QUrl> 16 , d_ptr(new DispatchTypeChainedPrivate)
21 DispatchTypeChained::~DispatchTypeChained()
31 Actions endPoints = d->endPoints;
32 std::sort(endPoints.begin(), endPoints.end(), [](
Action *a,
Action *b) ->
bool {
33 return a->
reverse() < b->reverse();
38 for (
Action *endPoint : 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) {
137 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) 143 if (!paths.isEmpty()) {
148 if (!unattachedTable.
isEmpty()) {
164 const BestActionMatch ret = d->recurseMatch(args.
size(), QStringLiteral(
"/"), path.
split(
QLatin1Char(
'/')));
166 if (ret.isNull || chain.
isEmpty()) {
172 for (
const QString &arg : parts) {
174 decodedArgs.
append(Utils::decodePercentEncoding(&aux));
178 Request *request = c->request();
197 if (chainedList.
size() > 1) {
198 qCCritical(CUTELYST_DISPATCHER_CHAINED)
199 <<
"Multiple Chained attributes not supported registering" << action->
reverse();
205 qCCritical(CUTELYST_DISPATCHER_CHAINED)
206 <<
"Actions cannot chain to themselves registering /" << action->
name();
214 if (pathPart.
size() == 1 && !pathPart[0].
isEmpty()) {
216 }
else if (pathPart.
size() > 1) {
217 qCCritical(CUTELYST_DISPATCHER_CHAINED)
218 <<
"Multiple PathPart attributes not supported registering" 224 qCCritical(CUTELYST_DISPATCHER_CHAINED)
225 <<
"Absolute parameters to PathPart not allowed registering" 230 attributes.
replace(QStringLiteral(
"PathPart"), part);
233 auto &childrenOf = d->childrenOf[chainedTo][part];
234 childrenOf.insert(childrenOf.begin(), action);
244 qCCritical(CUTELYST_DISPATCHER_CHAINED)
245 <<
"Combining Args and CaptureArgs attributes not supported registering" 263 if (!(attributes.
contains(QStringLiteral(
"Chained")) &&
264 !attributes.
contains(QStringLiteral(
"CaptureArgs")))) {
265 qCWarning(CUTELYST_DISPATCHER_CHAINED) <<
"uriForAction: action is not an end point" << action;
275 if (curr_attributes.
contains(QStringLiteral(
"CaptureArgs"))) {
278 qCWarning(CUTELYST_DISPATCHER_CHAINED) <<
"uriForAction: not enough captures" << curr->
numberOfCaptures() << captures.
size();
286 const QString pp = curr_attributes.
value(QStringLiteral(
"PathPart"));
291 parent = curr_attributes.
value(QStringLiteral(
"Chained"));
292 curr = d->actions.value(
parent);
295 if (
parent.compare(u
"/") != 0) {
297 qCWarning(CUTELYST_DISPATCHER_CHAINED) <<
"uriForAction: dangling action" <<
parent;
301 if (!localCaptures.
isEmpty()) {
303 qCWarning(CUTELYST_DISPATCHER_CHAINED) <<
"uriForAction: too many captures" << localCaptures;
316 if (qobject_cast<ActionChain*>(action)) {
331 curr = d->actions.value(
parent);
334 return new ActionChain(chain, const_cast<Context*>(c));
341 if (d->actions.isEmpty()) {
350 BestActionMatch DispatchTypeChainedPrivate::recurseMatch(
int reqArgsSize,
const QString &parent,
const QStringList &pathParts)
const 352 BestActionMatch bestAction;
353 auto it = childrenOf.constFind(parent);
354 if (it == childrenOf.constEnd()) {
358 const StringActionsMap &children = it.value();
362 return b.size() < a.
size();
365 for (
const QString &tryPart : keys) {
367 if (!tryPart.isEmpty()) {
375 parts = parts.
mid(tryPartCount);
378 const Actions tryActions = children.value(tryPart);
379 for (
Action *action : tryActions) {
381 if (attributes.
contains(QStringLiteral(
"CaptureArgs"))) {
382 const int captureCount = action->numberOfCaptures();
384 if (parts.
size() < captureCount) {
392 if (!action->matchCaptures(captures.
size())) {
399 const BestActionMatch ret = recurseMatch(reqArgsSize,
QLatin1Char(
'/') + action->reverse(), localParts);
407 int bestActionParts = bestAction.parts.
size();
409 if (!actions.isEmpty() &&
410 (bestAction.isNull ||
411 actionParts.
size() < bestActionParts ||
412 (actionParts.
size() == bestActionParts &&
413 actionCaptures.
size() < bestAction.captures.size() &&
414 ret.n_pathParts > bestAction.n_pathParts))) {
415 actions.prepend(action);
416 int pathparts = attributes.
value(QStringLiteral(
"PathPart")).count(
QLatin1Char(
'/')) + 1;
417 bestAction.actions = actions;
418 bestAction.captures = captures + actionCaptures;
419 bestAction.parts = actionParts;
420 bestAction.n_pathParts = pathparts + ret.n_pathParts;
421 bestAction.isNull =
false;
424 if (!action->match(reqArgsSize + parts.
size())) {
428 const QString argsAttr = attributes.
value(QStringLiteral(
"Args"));
429 const int pathparts = attributes.
value(QStringLiteral(
"PathPart")).count(
QLatin1Char(
'/')) + 1;
437 if (bestAction.isNull ||
438 parts.
size() < bestAction.parts.size() ||
439 (parts.
isEmpty() && !argsAttr.
isEmpty() && action->numberOfArgs() == 0)) {
440 bestAction.actions = { action };
442 bestAction.parts = parts;
443 bestAction.n_pathParts = pathparts;
444 bestAction.isNull =
false;
453 bool DispatchTypeChainedPrivate::checkArgsAttr(
Action *action,
const QString &name)
const 461 if (values.
size() > 1) {
462 qCCritical(CUTELYST_DISPATCHER_CHAINED)
465 <<
"attributes not supported registering" 473 qCCritical(CUTELYST_DISPATCHER_CHAINED)
475 << name <<
"(" << args <<
") for action" 477 <<
"(use '" << name <<
"' or '" << name <<
"(<number>)')";
484 QString DispatchTypeChainedPrivate::listExtraHttpMethods(
Action *action)
495 QString DispatchTypeChainedPrivate::listExtraConsumes(
Action *action)
506 #include "moc_dispatchtypechained.cpp" ParamsMultiMap attributes() const noexcept
Action * expandAction(const Context *c, Action *action) const final
QString & append(QChar ch)
virtual bool inUse() override
virtual bool registerAction(Action *action) override
registerAction
QList< T > values() const const
void append(const T &value)
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QString & prepend(QChar ch)
Holds a chain of Cutelyst Actions.
void setupMatchedAction(Context *c, Action *action) const
QString join(const QString &separator) const const
void setMatch(const QString &match)
This class represents a Cutelyst Action.
QString number(int n, int base)
int count(const T &value) const const
void append(const T &value)
DispatchTypeChained(QObject *parent=nullptr)
void setAttributes(const ParamsMultiMap &attributes)
int toInt(bool *ok, int base) const const
bool isEmpty() const const
bool isEmpty() const const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
virtual qint8 numberOfCaptures() const noexcept
The Cutelyst namespace holds all public Cutelyst API.
virtual MatchType match(Context *c, const QString &path, const QStringList &args) const override
void setCaptures(const QStringList &captures)
virtual QString uriForAction(Action *action, const QStringList &captures) const override
void setArguments(const QStringList &arguments)
bool contains(const Key &key, const T &value) const const
QString & replace(int position, int n, QChar after)
bool isEmpty() const const
QList< T > mid(int pos, int length) const const
void prepend(const T &value)
virtual QByteArray list() const override
list the registered actions To be implemented by subclasses
QString attribute(const QString &name, const QString &defaultValue={}) const
QObject * parent() const const
const T value(const Key &key, const T &defaultValue) const const