include/oa_route.h

filter: Statementstmt 100% 63 / 63 Branchbran 80.9% 68 / 84 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 89.1% 131 / 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 827 static int oa_split_path(const char *path, STRLEN pl, oa_pseg *out, int max) {
15 827 int n = 0;
16 827 STRLEN s = 0;
17 2356 ▶ while (s < pl) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < pl) { 1539 817 2356
18 STRLEN e;
19 3080 ▶ while (s < pl && path[s] == '/') s++;
Branch: 2 branches
BranchTrueFalseTotal
while (s < pl && path[s] == '/') s++; 3070 10 3080
while (s < pl && path[s] == '/') s++; 1541 1529 3070
20 1539 ▶ if (s >= pl) break;
Branch: 1 branch
BranchTrueFalseTotal
if (s >= pl) break; 10 1529 1539
21 1529 e = s;
22 5822 ▶ while (e < pl && path[e] != '/') e++;
Branch: 2 branches
BranchTrueFalseTotal
while (e < pl && path[e] != '/') e++; 5005 817 5822
while (e < pl && path[e] != '/') e++; 4293 712 5005
23 1529 ▶ if (n == max) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (n == max) return -1; 0 1529 1529
24 1529 out[n].p = path + s;
25 1529 out[n].l = e - s;
26 1529 n++;
27 1529 s = e;
28 }
29 827 return n;
30 }
31  
32 /* does this op's template fit these path segments? (method not considered) */
33 1752 static int oa_segs_fit(pTHX_ oa_op *o, oa_pseg *segs, int nsegs) {
34 int i;
35 1752 ▶ if (o->nsegs != nsegs) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (o->nsegs != nsegs) return 0; 786 966 1752
36 2442 ▶ for (i = 0; i < nsegs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nsegs; i++) { 1668 774 2442
37 1668 ▶ if (o->segs[i].lit) {
Branch: 1 branch
BranchTrueFalseTotal
if (o->segs[i].lit) { 1070 598 1668
38 1070 STRLEN ll; const char *lp = SvPV_const(o->segs[i].lit, ll);
39 1070 ▶ 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; 1012 58 1070
if (ll != segs[i].l || memNE(lp, segs[i].p, ll)) return 0; 134 878 1012
40 }
41 }
42 774 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 825 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 825 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 825 ▶ if (!t) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!t) return NULL; 0 825 825
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 825 ▶ if (a->prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if (a->prefix) { 9 816 825
66 9 STRLEN xl; const char *xp = SvPV_const(a->prefix, xl);
67 9 ▶ 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] == '/')) { 8 1 9
if (pl >= xl && memEQ(path, xp, xl) && (pl == xl || path[xl] == '/')) { 7 1 8
if (pl >= xl && memEQ(path, xp, xl) && (pl == xl || path[xl] == '/')) { 7 0 7
if (pl >= xl && memEQ(path, xp, xl) && (pl == xl || path[xl] == '/')) { 6 1 7
68 6 path += xl;
69 6 pl -= xl;
70 }
71 }
72  
73 825 ▶ if (ml >= sizeof m) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (ml >= sizeof m) return NULL; 0 825 825
74 3424 ▶ 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]); 2595 4 2599
for (k = 0; k < ml; k++) m[k] = (char)toLOWER((U8)method[k]); 2599 825 3424
75  
76 825 nsegs = oa_split_path(path, pl, segs, OA_MAXSEGS);
77 825 ▶ if (nsegs < 0) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (nsegs < 0) return NULL; 0 825 825
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 2864 ▶ for (i = 0; i < t->n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->n; i++) { 2796 68 2864
83 2796 ▶ 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]; 2796 0 2796
84 2796 STRLEN oml; const char *omp = SvPV_const(o->method, oml);
85 oa_pseg osegs[OA_MAXSEGS];
86 2796 oa_pseg *use = segs;
87 2796 int unum = nsegs;
88 3548 ▶ if (oml != ml || memNE(omp, m, ml)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (oml != ml || memNE(omp, m, ml)) continue; 1509 1287 2796
if (oml != ml || memNE(omp, m, ml)) continue; 0 1509 1509
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 1509 ▶ if (o->prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if (o->prefix) { 2 1507 1509
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 1509 ▶ if (!oa_segs_fit(aTHX_ o, use, unum)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!oa_segs_fit(aTHX_ o, use, unum)) continue; 752 757 1509
104 757 ▶ if (captures) {
Branch: 1 branch
BranchTrueFalseTotal
if (captures) { 757 0 757
105 int s;
106 2187 ▶ for (s = 0; s < o->nsegs; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = 0; s < o->nsegs; s++) { 1430 757 2187
107 1430 ▶ if (o->segs[s].pname) {
Branch: 1 branch
BranchTrueFalseTotal
if (o->segs[s].pname) { 596 834 1430
108 596 STRLEN nl; const char *np = SvPV_const(o->segs[s].pname, nl);
109 596 (void)hv_store(captures, np, (I32)nl,
110 newSVpvn(use[s].p, use[s].l), 0);
111 }
112 }
113 }
114 757 return o;
115 }
116  
117 68 ▶ if (allow) { /* any other method on this path? -> 405 material */
Branch: 1 branch
BranchTrueFalseTotal
if (allow) { /* any other method on this path? -> 405 material */ 68 0 68
118 311 ▶ for (i = 0; i < t->n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->n; i++) { 243 68 311
119 243 ▶ 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]; 243 0 243
120 243 ▶ if (oa_segs_fit(aTHX_ o, segs, nsegs)) {
Branch: 1 branch
BranchTrueFalseTotal
if (oa_segs_fit(aTHX_ o, segs, nsegs)) { 17 226 243
121 17 STRLEN oml; const char *omp = SvPV_const(o->method, oml);
122 17 SV *up = newSVpvn(omp, oml);
123 17 STRLEN j; char *pv = SvPVX(up);
124 78 ▶ 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]); 61 0 61
for (j = 0; j < oml; j++) pv[j] = (char)toUPPER((U8)pv[j]); 61 17 78
125 17 av_push(allow, up);
126 }
127 }
128 }
129 68 return NULL;
130 }
131  
132 #endif /* OA_ROUTE_H */
« include/oa_normalize.h ↑ Summary blib/lib/Open/API.pm »