forked from cutelyst/cutelyst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdispatchtypechained.cpp
515 lines (430 loc) · 17.6 KB
/
dispatchtypechained.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
* Copyright (C) 2015-2018 Daniel Nicoletti <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "dispatchtypechained_p.h"
#include "common.h"
#include "actionchain.h"
#include "utils.h"
#include "context.h"
#include <QtCore/QUrl>
using namespace Cutelyst;
DispatchTypeChained::DispatchTypeChained(QObject *parent) : DispatchType(parent)
, d_ptr(new DispatchTypeChainedPrivate)
{
}
DispatchTypeChained::~DispatchTypeChained()
{
delete d_ptr;
}
QByteArray DispatchTypeChained::list() const
{
Q_D(const DispatchTypeChained);
QByteArray buffer;
Actions endPoints = d->endPoints;
std::sort(endPoints.begin(), endPoints.end(), [](Action *a, Action *b) -> bool {
return a->reverse() < b->reverse();
});
QVector<QStringList> paths;
QVector<QStringList> unattachedTable;
for (Action *endPoint : endPoints) {
QStringList parts;
if (endPoint->numberOfArgs() == -1) {
parts.append(QLatin1String("..."));
} else {
for (int i = 0; i < endPoint->numberOfArgs(); ++i) {
parts.append(QLatin1String("*"));
}
}
QString parent;
QString extra = DispatchTypeChainedPrivate::listExtraHttpMethods(endPoint);
QString consumes = DispatchTypeChainedPrivate::listExtraConsumes(endPoint);
ActionList parents;
Action *current = endPoint;
while (current) {
for (int i = 0; i < current->numberOfCaptures(); ++i) {
parts.prepend(QLatin1String("*"));
}
const auto attributes = current->attributes();
const QStringList pathParts = attributes.values(QLatin1String("PathPart"));
for (const QString &part : pathParts) {
if (!part.isEmpty()) {
parts.prepend(part);
}
}
parent = attributes.value(QLatin1String("Chained"));
current = d->actions.value(parent);
if (current) {
parents.prepend(current);
}
}
if (parent != QLatin1String("/")) {
QStringList row;
if (parents.isEmpty()) {
row.append(QLatin1Char('/') + endPoint->reverse());
} else {
row.append(QLatin1Char('/') + parents.first()->reverse());
}
row.append(parent);
unattachedTable.append(row);
continue;
}
QVector<QStringList> rows;
for (Action *p : parents) {
QString name = QLatin1Char('/') + p->reverse();
QString extraHttpMethod = DispatchTypeChainedPrivate::listExtraHttpMethods(p);
if (!extraHttpMethod.isEmpty()) {
name.prepend(extraHttpMethod + QLatin1Char(' '));
}
const auto attributes = p->attributes();
auto it = attributes.constFind(QLatin1String("CaptureArgs"));
if (it != attributes.constEnd()) {
name.append(QLatin1String(" (") + it.value() + QLatin1Char(')'));
} else {
name.append(QLatin1String(" (0)"));
}
QString ct = DispatchTypeChainedPrivate::listExtraConsumes(p);
if (!ct.isEmpty()) {
name.append(QLatin1String(" :") + ct);
}
if (p != parents[0]) {
name = QLatin1String("-> ") + name;
}
rows.append({QString(), name});
}
QString line;
if (!rows.isEmpty()) {
line.append(QLatin1String("=> "));
}
if (!extra.isEmpty()) {
line.append(extra + QLatin1Char(' '));
}
line.append(QLatin1Char('/') + endPoint->reverse());
if (endPoint->numberOfArgs() == -1) {
line.append(QLatin1String(" (...)"));
} else {
line.append(QLatin1String(" (") + QString::number(endPoint->numberOfArgs()) + QLatin1Char(')'));
}
if (!consumes.isEmpty()) {
line.append(QLatin1String(" :") + consumes);
}
rows.append({QString(), line});
rows[0][0] = QLatin1Char('/') + parts.join(QLatin1Char('/'));
paths.append(rows);
}
QTextStream out(&buffer, QIODevice::WriteOnly);
if (!paths.isEmpty()) {
out << Utils::buildTable(paths, { QLatin1String("Path Spec"), QLatin1String("Private") },
QLatin1String("Loaded Chained actions:"));
}
if (!unattachedTable.isEmpty()) {
out << Utils::buildTable(unattachedTable, { QLatin1String("Private"), QLatin1String("Missing parent") },
QLatin1String("Unattached Chained actions:"));
}
return buffer;
}
DispatchType::MatchType DispatchTypeChained::match(Context *c, const QString &path, const QStringList &args) const
{
if (!args.isEmpty()) {
return NoMatch;
}
Q_D(const DispatchTypeChained);
const BestActionMatch ret = d->recurseMatch(args.size(), QStringLiteral("/"), path.split(QLatin1Char('/')));
const ActionList chain = ret.actions;
if (ret.isNull || chain.isEmpty()) {
return NoMatch;
}
QStringList decodedArgs;
const QStringList parts = ret.parts;
for (const QString &arg : parts) {
QString aux = arg;
decodedArgs.append(Utils::decodePercentEncoding(&aux));
}
ActionChain *action = new ActionChain(chain, c);
Request *request = c->request();
request->setArguments(decodedArgs);
request->setCaptures(ret.captures);
request->setMatch(QLatin1Char('/') + action->reverse());
setupMatchedAction(c, action);
return ExactMatch;
}
bool DispatchTypeChained::registerAction(Action *action)
{
Q_D(DispatchTypeChained);
auto attributes = action->attributes();
const QStringList chainedList = attributes.values(QLatin1String("Chained"));
if (chainedList.isEmpty()) {
return false;
}
if (chainedList.size() > 1) {
qCCritical(CUTELYST_DISPATCHER_CHAINED)
<< "Multiple Chained attributes not supported registering" << action->reverse();
return false;
}
const QString chainedTo = chainedList.first();
if (chainedTo == QLatin1Char('/') + action->name()) {
qCCritical(CUTELYST_DISPATCHER_CHAINED)
<< "Actions cannot chain to themselves registering /" << action->name();
return false;
}
const QStringList pathPart = attributes.values(QLatin1String("PathPart"));
QString part = action->name();
if (pathPart.size() == 1 && !pathPart[0].isEmpty()) {
part = pathPart[0];
} else if (pathPart.size() > 1) {
qCCritical(CUTELYST_DISPATCHER_CHAINED)
<< "Multiple PathPart attributes not supported registering"
<< action->reverse();
return false;
}
if (part.startsWith(QLatin1Char('/'))) {
qCCritical(CUTELYST_DISPATCHER_CHAINED)
<< "Absolute parameters to PathPart not allowed registering"
<< action->reverse();
return false;
}
attributes.insert(QStringLiteral("PathPart"), part);
action->setAttributes(attributes);
auto &childrenOf = d->childrenOf[chainedTo][part];
childrenOf.insert(childrenOf.begin(), action);
d->actions[QLatin1Char('/') + action->reverse()] = action;
if (!d->checkArgsAttr(action, QLatin1String("Args")) ||
!d->checkArgsAttr(action, QLatin1String("CaptureArgs"))) {
return false;
}
if (attributes.contains(QLatin1String("Args")) && attributes.contains(QLatin1String("CaptureArgs"))) {
qCCritical(CUTELYST_DISPATCHER_CHAINED)
<< "Combining Args and CaptureArgs attributes not supported registering"
<< action->reverse();
return false;
}
if (!attributes.contains(QLatin1String("CaptureArgs"))) {
d->endPoints.push_back(action);
}
return true;
}
QString DispatchTypeChained::uriForAction(Action *action, const QStringList &captures) const
{
Q_D(const DispatchTypeChained);
QString ret;
const QMap<QString, QString> attributes = action->attributes();
if (!(attributes.contains(QStringLiteral("Chained")) &&
!attributes.contains(QStringLiteral("CaptureArgs")))) {
qCWarning(CUTELYST_DISPATCHER_CHAINED) << "uriForAction: action is not an end point" << action;
return ret;
}
QString parent;
QStringList localCaptures = captures;
QStringList parts;
Action *curr = action;
while (curr) {
const QMap<QString, QString> curr_attributes = curr->attributes();
if (curr_attributes.contains(QStringLiteral("CaptureArgs"))) {
if (localCaptures.size() < curr->numberOfCaptures()) {
// Not enough captures
qCWarning(CUTELYST_DISPATCHER_CHAINED) << "uriForAction: not enough captures" << curr->numberOfCaptures() << captures.size();
return ret;
}
parts = localCaptures.mid(localCaptures.size() - curr->numberOfCaptures()) + parts;
localCaptures = localCaptures.mid(0, localCaptures.size() - curr->numberOfCaptures());
}
const QString pp = curr_attributes.value(QStringLiteral("PathPart"));
if (!pp.isEmpty()) {
parts.prepend(pp);
}
parent = curr_attributes.value(QStringLiteral("Chained"));
curr = d->actions.value(parent);
}
if (parent != QLatin1String("/")) {
// fail for dangling action
qCWarning(CUTELYST_DISPATCHER_CHAINED) << "uriForAction: dangling action" << parent;
return ret;
}
if (!localCaptures.isEmpty()) {
// fail for too many captures
qCWarning(CUTELYST_DISPATCHER_CHAINED) << "uriForAction: too many captures" << localCaptures;
return ret;
}
ret = QLatin1Char('/') + parts.join(QLatin1Char('/'));
return ret;
}
Action *DispatchTypeChained::expandAction(const Context *c, Action *action) const
{
Q_D(const DispatchTypeChained);
// Do not expand action if action already is an ActionChain
if (qobject_cast<ActionChain*>(action)) {
return action;
}
// The action must be chained to something
if (!action->attributes().contains(QStringLiteral("Chained"))) {
return nullptr;
}
ActionList chain;
Action *curr = action;
while (curr) {
chain.prepend(curr);
const QString parent = curr->attribute(QStringLiteral("Chained"));
curr = d->actions.value(parent);
}
return new ActionChain(chain, const_cast<Context*>(c));
}
bool DispatchTypeChained::inUse()
{
Q_D(const DispatchTypeChained);
if (d->actions.isEmpty()) {
return false;
}
// Optimize end points
return true;
}
BestActionMatch DispatchTypeChainedPrivate::recurseMatch(int reqArgsSize, const QString &parent, const QStringList &pathParts) const
{
BestActionMatch bestAction;
auto it = childrenOf.constFind(parent);
if (it == childrenOf.constEnd()) {
return bestAction;
}
const StringActionsMap &children = it.value();
QStringList keys = children.keys();
std::sort(keys.begin(), keys.end(), [](const QString &a, const QString &b) -> bool {
// action2 then action1 to try the longest part first
return b.size() < a.size();
});
for (const QString &tryPart : keys) {
QStringList parts = pathParts;
if (!tryPart.isEmpty()) {
// We want to count the number of parts a split would give
// and remove the number of parts from tryPart
int tryPartCount = tryPart.count(QLatin1Char('/')) + 1;
const QStringList possiblePart = parts.mid(0, tryPartCount);
if (tryPart != possiblePart.join(QLatin1Char('/'))) {
continue;
}
parts = parts.mid(tryPartCount);
}
const Actions tryActions = children.value(tryPart);
for (Action *action : tryActions) {
const QMap<QString, QString> attributes = action->attributes();
if (attributes.contains(QStringLiteral("CaptureArgs"))) {
const int captureCount = action->numberOfCaptures();
// Short-circuit if not enough remaining parts
if (parts.size() < captureCount) {
continue;
}
// strip CaptureArgs into list
const QStringList captures = parts.mid(0, captureCount);
// check if the action may fit, depending on a given test by the app
if (!action->matchCaptures(captures.size())) {
continue;
}
const QStringList localParts = parts.mid(captureCount);
// try the remaining parts against children of this action
const BestActionMatch ret = recurseMatch(reqArgsSize, QLatin1Char('/') + action->reverse(), localParts);
// No best action currently
// OR The action has less parts
// OR The action has equal parts but less captured data (ergo more defined)
ActionList actions = ret.actions;
const QStringList actionCaptures = ret.captures;
const QStringList actionParts = ret.parts;
int bestActionParts = bestAction.parts.size();
if (!actions.isEmpty() &&
(bestAction.isNull ||
actionParts.size() < bestActionParts ||
(actionParts.size() == bestActionParts &&
actionCaptures.size() < bestAction.captures.size() &&
ret.n_pathParts > bestAction.n_pathParts))) {
actions.prepend(action);
int pathparts = attributes.value(QStringLiteral("PathPart")).count(QLatin1Char('/')) + 1;
bestAction.actions = actions;
bestAction.captures = captures + actionCaptures;
bestAction.parts = actionParts;
bestAction.n_pathParts = pathparts + ret.n_pathParts;
bestAction.isNull = false;
}
} else {
if (!action->match(reqArgsSize + parts.size())) {
continue;
}
const QString argsAttr = attributes.value(QStringLiteral("Args"));
const int pathparts = attributes.value(QStringLiteral("PathPart")).count(QLatin1Char('/')) + 1;
// No best action currently
// OR This one matches with fewer parts left than the current best action,
// And therefore is a better match
// OR No parts and this expects 0
// The current best action might also be Args(0),
// but we couldn't chose between then anyway so we'll take the last seen
if (bestAction.isNull ||
parts.size() < bestAction.parts.size() ||
(parts.isEmpty() && !argsAttr.isEmpty() && action->numberOfArgs() == 0)) {
bestAction.actions = { action };
bestAction.captures = QStringList();
bestAction.parts = parts;
bestAction.n_pathParts = pathparts;
bestAction.isNull = false;
}
}
}
}
return bestAction;
}
bool DispatchTypeChainedPrivate::checkArgsAttr(Action *action, const QString &name) const
{
const QMap<QString, QString> attributes = action->attributes();
if (!attributes.contains(name)) {
return true;
}
const QStringList values = attributes.values(name);
if (values.size() > 1) {
qCCritical(CUTELYST_DISPATCHER_CHAINED)
<< "Multiple"
<< name
<< "attributes not supported registering"
<< action->reverse();
return false;
}
QString args = values[0];
bool ok;
if (!args.isEmpty() && args.toInt(&ok) < 0 && !ok) {
qCCritical(CUTELYST_DISPATCHER_CHAINED)
<< "Invalid"
<< name << "(" << args << ") for action"
<< action->reverse()
<< "(use '" << name << "' or '" << name << "(<number>)')";
return false;
}
return true;
}
QString DispatchTypeChainedPrivate::listExtraHttpMethods(Action *action)
{
QString ret;
const auto attributes = action->attributes();
if (attributes.contains(QLatin1String("HTTP_METHODS"))) {
const QStringList extra = attributes.values(QLatin1String("HTTP_METHODS"));
ret = extra.join(QLatin1String(", "));
}
return ret;
}
QString DispatchTypeChainedPrivate::listExtraConsumes(Action *action)
{
QString ret;
const auto attributes = action->attributes();
if (attributes.contains(QLatin1String("CONSUMES"))) {
const QStringList extra = attributes.values(QLatin1String("CONSUMES"));
ret = extra.join(QLatin1String(", "));
}
return ret;
}
#include "moc_dispatchtypechained.cpp"