include/oa_route.h

filter: Statementstmt 100% 63 / 63 Branchbran 79.7% 67 / 84 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 88.4% 130 / 147 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/oa_normalize.h ↑ Summary blib/lib/Open/API.pm »
1 #ifndef OA_ROUTE_H
2 #define OA_ROUTE_H
3  
4 /* The router: match (method, path) against the compiled operation table.
5 * Per-method linear scan over pre-split templates with literal-segment
6 * rejects - specs are tens of routes; a trie is a later optimization behind
7 * the same functions. Distinguishes the two miss cases so callers can send
8 * 404 (no path) vs 405 + Allow (path exists, method does not). */
9  
10 #define OA_MAXSEGS 64
11  
12 typedef struct oa_pseg { const char *p; STRLEN l; } oa_pseg;
13  
14 782 static int oa_split_path(const char *path, STRLEN pl, oa_pseg *out, int max) {
15 782 int n = 0;
16 782 STRLEN s = 0;
17 2249 ▶ while (s < pl) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < pl) { 1476 773 2249
18 STRLEN e;
19 2954 ▶ while (s < pl && path[s] == '/') s++;
Branch: 2 branches
BranchTrueFalseTotal
while (s < pl && path[s] == '/') s++; 2945 9 2954
while (s < pl && path[s] == '/') s++; 1478 1467 2945
20 1476 ▶ if (s >= pl) break;
Branch: 1 branch
BranchTrueFalseTotal
if (s >= pl) break; 9 1467 1476
21 1467 e = s;
22 5533 ▶ while (e < pl && path[e] != '/') e++;
Branch: 2 branches
BranchTrueFalseTotal
while (e < pl && path[e] != '/') e++; 4760 773 5533
while (e < pl && path[e] != '/') e++; 4066 694 4760
23 1467 ▶ if (n == max) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (n == max) return -1; 0 1467 1467
24 1467 out[n].p = path + s;
25 1467 out[n].l = e - s;
26 1467 n++;
27 1467 s = e;
28 }
29 782 return n;
30 }
31  
32 /* does this op's template fit these path segments? (method not considered) */
33 1659 static int oa_segs_fit(pTHX_ oa_op *o, oa_pseg *segs, int nsegs) {
34 int i;
35 1659 ▶ if (o->nsegs != nsegs) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (o->nsegs != nsegs) return 0; 755 904 1659
36 2323 ▶ for (i = 0; i < nsegs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nsegs; i++) { 1592 731 2323
37 1592 ▶ if (o->segs[i].lit) {
Branch: 1 branch
BranchTrueFalseTotal
if (o->segs[i].lit) { 1006 586 1592
38 1006 STRLEN ll; const char *lp = SvPV_const(o->segs[i].lit, ll);
39 1006 ▶ if (ll != segs[i].l || memNE(lp, segs[i].p, ll)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (ll != segs[i].l || memNE(lp, segs[i].p, ll)) return 0; 967 39 1006
if (ll != segs[i].l || memNE(lp, segs[i].p, ll)) return 0; 134 833 967
40 }
41 }
42 731 return 1;
43 }
44  
45 /* Match. On success returns the op and fills `captures` (raw, undecoded
46 * segment bytes keyed by template parameter name). On failure returns NULL;
47 * when `allow` is given it is filled with the uppercased methods that DO
48 * match the path - empty means 404, non-empty means 405. */
49 780 static oa_op *oa_route(pTHX_ oa_api *a, const char *method, STRLEN ml,
50 const char *path, STRLEN pl,
51 HV *captures, AV *allow) {
52 780 oa_ops *t = (oa_ops *)a->ops;
53 oa_pseg segs[OA_MAXSEGS];
54 char m[16];
55 int nsegs, i;
56 STRLEN k;
57  
58 780 ▶ if (!t) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!t) return NULL; 0 780 780
59  
60 /* A server URL's path prefix, when the caller opted in. Stripped BEFORE
61 * the split: oa_segs_fit compares segment counts, so a prefix left in
62 * place makes every count wrong by its depth. Every surface routes
63 * through here - the Plack app, the match XSUB and the C ABI - so they
64 * cannot disagree about it. */
65 780 ▶ if (a->prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if (a->prefix) { 6 774 780
66 6 STRLEN xl; const char *xp = SvPV_const(a->prefix, xl);
67 6 ▶ if (pl >= xl && memEQ(path, xp, xl) && (pl == xl || path[xl] == '/')) {
Branch: 4 branches
BranchTrueFalseTotal
if (pl >= xl && memEQ(path, xp, xl) && (pl == xl || path[xl] == '/')) { 5 1 6
if (pl >= xl && memEQ(path, xp, xl) && (pl == xl || path[xl] == '/')) { 5 0 5
if (pl >= xl && memEQ(path, xp, xl) && (pl == xl || path[xl] == '/')) { 5 0 5
if (pl >= xl && memEQ(path, xp, xl) && (pl == xl || path[xl] == '/')) { 4 1 5
68 4 path += xl;
69 4 pl -= xl;
70 }
71 }
72  
73 780 ▶ if (ml >= sizeof m) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (ml >= sizeof m) return NULL; 0 780 780
74 3223 ▶ for (k = 0; k < ml; k++) m[k] = (char)toLOWER((U8)method[k]);
Branch: 2 branches
BranchTrueFalseTotal
for (k = 0; k < ml; k++) m[k] = (char)toLOWER((U8)method[k]); 2439 4 2443
for (k = 0; k < ml; k++) m[k] = (char)toLOWER((U8)method[k]); 2443 780 3223
75  
76 780 nsegs = oa_split_path(path, pl, segs, OA_MAXSEGS);
77 780 ▶ if (nsegs < 0) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (nsegs < 0) return NULL; 0 780 780
78  
79 /* t->order, not 0..n: the table is in `paths` hash order, which perl
80 * randomises, so an unordered scan routes a request that two templates
81 * both fit to a different operation between runs. See oa_order_routes. */
82 2743 ▶ for (i = 0; i < t->n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->n; i++) { 2682 61 2743
83 2682 ▶ oa_op *o = &t->ops[t->order ? t->order[i] : i];
Branch: 1 branch
BranchTrueFalseTotal
oa_op *o = &t->ops[t->order ? t->order[i] : i]; 2682 0 2682
84 2682 STRLEN oml; const char *omp = SvPV_const(o->method, oml);
85 oa_pseg osegs[OA_MAXSEGS];
86 2682 oa_pseg *use = segs;
87 2682 int unum = nsegs;
88 3399 ▶ if (oml != ml || memNE(omp, m, ml)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (oml != ml || memNE(omp, m, ml)) continue; 1436 1246 2682
if (oml != ml || memNE(omp, m, ml)) continue; 0 1436 1436
89 /* An operation (or its path item) may carry its own server prefix,
90 * which is more specific than the document's. Only such an operation
91 * pays for a second split, and the strip is tolerant in the same way
92 * the document-level one is: a path that already lacks the prefix
93 * still matches. */
94 1436 ▶ if (o->prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if (o->prefix) { 2 1434 1436
95 2 STRLEN xl; const char *xp = SvPV_const(o->prefix, xl);
96 2 ▶ if (pl >= xl && memEQ(path, xp, xl)
Branch: 2 branches
BranchTrueFalseTotal
if (pl >= xl && memEQ(path, xp, xl) 2 0 2
if (pl >= xl && memEQ(path, xp, xl) 2 0 2
97 2 ▶ && (pl == xl || path[xl] == '/')) {
Branch: 2 branches
BranchTrueFalseTotal
&& (pl == xl || path[xl] == '/')) { 2 0 2
&& (pl == xl || path[xl] == '/')) { 2 0 2
98 2 unum = oa_split_path(path + xl, pl - xl, osegs, OA_MAXSEGS);
99 2 ▶ if (unum < 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (unum < 0) continue; 0 2 2
100 2 use = osegs;
101 }
102 }
103 1436 ▶ if (!oa_segs_fit(aTHX_ o, use, unum)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!oa_segs_fit(aTHX_ o, use, unum)) continue; 717 719 1436
104 719 ▶ if (captures) {
Branch: 1 branch
BranchTrueFalseTotal
if (captures) { 719 0 719
105 int s;
106 2098 ▶ for (s = 0; s < o->nsegs; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = 0; s < o->nsegs; s++) { 1379 719 2098
107 1379 ▶ if (o->segs[s].pname) {
Branch: 1 branch
BranchTrueFalseTotal
if (o->segs[s].pname) { 584 795 1379
108 584 STRLEN nl; const char *np = SvPV_const(o->segs[s].pname, nl);
109 584 (void)hv_store(captures, np, (I32)nl,
110 newSVpvn(use[s].p, use[s].l), 0);
111 }
112 }
113 }
114 719 return o;
115 }
116  
117 61 ▶ if (allow) { /* any other method on this path? -> 405 material */
Branch: 1 branch
BranchTrueFalseTotal
if (allow) { /* any other method on this path? -> 405 material */ 61 0 61
118 284 ▶ for (i = 0; i < t->n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->n; i++) { 223 61 284
119 223 ▶ oa_op *o = &t->ops[t->order ? t->order[i] : i];
Branch: 1 branch
BranchTrueFalseTotal
oa_op *o = &t->ops[t->order ? t->order[i] : i]; 223 0 223
120 223 ▶ if (oa_segs_fit(aTHX_ o, segs, nsegs)) {
Branch: 1 branch
BranchTrueFalseTotal
if (oa_segs_fit(aTHX_ o, segs, nsegs)) { 12 211 223
121 12 STRLEN oml; const char *omp = SvPV_const(o->method, oml);
122 12 SV *up = newSVpvn(omp, oml);
123 12 STRLEN j; char *pv = SvPVX(up);
124 56 ▶ for (j = 0; j < oml; j++) pv[j] = (char)toUPPER((U8)pv[j]);
Branch: 2 branches
BranchTrueFalseTotal
for (j = 0; j < oml; j++) pv[j] = (char)toUPPER((U8)pv[j]); 44 0 44
for (j = 0; j < oml; j++) pv[j] = (char)toUPPER((U8)pv[j]); 44 12 56
125 12 av_push(allow, up);
126 }
127 }
128 }
129 61 return NULL;
130 }
131  
132 #endif /* OA_ROUTE_H */
« include/oa_normalize.h ↑ Summary blib/lib/Open/API.pm »