5 #include "actionchain.h" 8 #include "dispatchtypechained_p.h" 11 #include <QtCore/QUrl> 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);
74 if (
parent.compare(u
"/") != 0) {
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) {
141 if (!paths.isEmpty()) {
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" 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);
341 return new ActionChain(chain, const_cast<Context *>(c));
348 if (d->actions.isEmpty()) {
357 BestActionMatch 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()) {
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) {
401 if (!action->matchCaptures(captures.
size())) {
408 const BestActionMatch ret =
409 recurseMatch(reqArgsSize,
QLatin1Char(
'/') + action->reverse(), localParts);
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);
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 =
448 if (bestAction.isNull || parts.
size() < bestAction.parts.size() ||
449 (parts.
isEmpty() && !argsAttr.
isEmpty() && action->numberOfArgs() == 0)) {
450 bestAction.actions = {action};
452 bestAction.parts = parts;
453 bestAction.n_pathParts = pathparts;
454 bestAction.isNull =
false;
463 bool DispatchTypeChainedPrivate::checkArgsAttr(
Action *action,
const QString &name)
const 471 if (values.
size() > 1) {
472 qCCritical(CUTELYST_DISPATCHER_CHAINED)
473 <<
"Multiple" << name <<
"attributes not supported registering" << action->
reverse();
480 qCCritical(CUTELYST_DISPATCHER_CHAINED)
481 <<
"Invalid" << name <<
"(" << args <<
") for action" << action->
reverse() <<
"(use '" 482 << name <<
"' or '" << name <<
"(<number>)')";
489 QString DispatchTypeChainedPrivate::listExtraHttpMethods(
Action *action)
500 QString DispatchTypeChainedPrivate::listExtraConsumes(
Action *action)
511 #include "moc_dispatchtypechained.cpp" ParamsMultiMap attributes() const noexcept
Action * expandAction(const Context *c, Action *action) const final
QString & append(QChar ch)
MatchType match(Context *c, QStringView path, const QStringList &args) const override
bool registerAction(Action *action) override
virtual qint8 numberOfCaptures() const
QString & prepend(QChar ch)
qsizetype count() const const
qsizetype size() const const
QStringView mid(qsizetype start, qsizetype length) const const
Holds a chain of Cutelyst actions.
QString join(QChar separator) const const
void setupMatchedAction(Context *c, Action *action) const
void setMatch(const QString &match)
QString reverse() const noexcept
qsizetype size() const const
This class represents a Cutelyst Action.
QString number(double n, char format, int precision)
DispatchTypeChained(QObject *parent=nullptr)
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
void setAttributes(const ParamsMultiMap &attributes)
int toInt(bool *ok, int base) const const
bool isEmpty() const const
bool isEmpty() const const
QString name() const noexcept
The Cutelyst namespace holds all public Cutelyst API.
void setCaptures(const QStringList &captures)
QString uriForAction(Action *action, const QStringList &captures) const override
void setArguments(const QStringList &arguments)
void prepend(parameter_type value)
bool contains(const Key &key) const const
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
QString toString() const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
Describes a chained dispatch type.
void append(QList< T > &&value)
Abstract class to described a dispatch type.
QList< T > mid(qsizetype pos, qsizetype length) const const
QByteArray list() const override
QString attribute(const QString &name, const QString &defaultValue={}) const
QObject * parent() const const
~DispatchTypeChained() override
T value(const Key &key, const T &defaultValue) const const
QList< T > values() const const