include/punk/punk_route.h

filter: Statementstmt 98.7% 230 / 233 Branchbran 83.4% 189 / 236 (12 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 91.0% 419 / 469 (12 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_plural.h ↑ Summary xs/csrf.xs »
1 /* punk_route.h - the C web router.
2 *
3 * Compiled once at to_app from the frozen Perl route table. The whole match
4 * path now lives here: an exact-match table for static routes, typed-segment
5 * records for dynamic ones (:param / *splat, held in one malloc'd arena and
6 * walked with memcmp - no regex engine), and the 405 Allow computation on a
7 * miss. lib/Punk/Router.pm is documentation only; t/0111 pins this router's
8 * behaviour. The dynamic walk is linear in declaration order (first match
9 * wins) and costs about 6 ns per record scanned, measured on dyn50.
10 *
11 * Every record carries `idx`, its position in the one Perl array of compiled
12 * records (code + guards) the dispatcher indexes into; a static hit, a
13 * dynamic hit and the reference tier all speak that same index.
14 *
15 * Semantics mirror the Perl exactly: :param captures one non-empty segment,
16 * *splat captures the non-empty remainder including slashes, HEAD matches GET,
17 * ANY matches every method, and Allow lists the methods a path answers under.
18 */
19  
20 #ifndef PUNK_ROUTE_H
21 #define PUNK_ROUTE_H
22  
23 #define PR_LIT 0
24 #define PR_PARAM 1
25 #define PR_SPLAT 2
26  
27 #define PR_MAX_SEGS 64
28  
29 typedef struct {
30 int type;
31 char *lit; /* PR_LIT: the bytes to match */
32 STRLEN litlen;
33 SV *name; /* PR_PARAM / PR_SPLAT: capture name (owned) */
34 } pr_seg;
35  
36 typedef struct {
37 char *method; /* uppercase (kept even for ANY, for Allow) */
38 STRLEN mlen;
39 int is_any; /* matches every method */
40 int is_get; /* HEAD falls back onto these */
41 int nsegs;
42 int splat; /* last segment is a splat */
43 pr_seg *segs;
44 IV idx; /* position in the Perl @recs array */
45 } pr_rec;
46  
47 typedef struct {
48 AV *raw; /* accumulation: { method, path, target, guards }
49 * hashrefs pushed by add(), unresolved */
50 int compiled;
51 int n;
52 pr_rec *recs; /* dynamic records */
53 int nrecs; /* entries in `records` (and in dyn_of) */
54 IV *dyn_of; /* record index -> its position in recs, or -1 for
55 * a static route. `recs` is indexed by dynamic
56 * position and `records` by record index, and
57 * named routes (punk_url.h) arrive holding the
58 * second and needing the first. Filled in the one
59 * loop that fills recs; nothing per request. */
60 HV *statics; /* "METHOD path" -> IV record index */
61 HV *static_paths; /* path -> AV of uppercase method SVs (for Allow) */
62 AV *records; /* per-route { code, guards, method, path } hashrefs,
63 * indexed by the record index match returns */
64 } pr_router;
65  
66 1429 static pr_router *pr_new(pTHX) {
67 pr_router *rt;
68 1429 Newxz(rt, 1, pr_router);
69 1429 rt->raw = newAV();
70 1429 return rt;
71 }
72  
73 /* the methods an ANY route answers */
74 static const char *const PR_ANY_METHODS[] =
75 { "GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS" };
76 #define PR_N_ANY 7
77  
78 /* Split a path on '/' (skipping the leading one) into borrowed segment
79 * pointers/lengths. Returns the count, or -1 if it does not start with '/'
80 * or overflows PR_MAX_SEGS. */
81 107952 static int pr_split(const char *path, STRLEN plen,
82 const char **seg_p, STRLEN *seg_l) {
83 107952 int nsegs = 0;
84 STRLEN j;
85 const char *start;
86 /*
87 # uncoverable branch true
88 # uncoverable condition left count:1 class:ignore_covered_err
89 # uncoverable condition left count:2 class:ignore_covered_err */
90 107952 ▶ if (!plen || path[0] != '/') return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (!plen || path[0] != '/') return -1; 107952 0 107952
if (!plen || path[0] != '/') return -1; 0 107952 107952
91 107952 start = path + 1;
92 1074008 ▶ for (j = 1; j <= plen; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 1; j <= plen; j++) { 966056 107952 1074008
93 966056 ▶ if (j == plen || path[j] == '/') {
Branch: 2 branches
BranchTrueFalseTotal
if (j == plen || path[j] == '/') { 858104 107952 966056
if (j == plen || path[j] == '/') { 105429 752675 858104
94 /*
95 # uncoverable branch true
96 # uncoverable condition left count:1 class:ignore_covered_err */
97 213381 ▶ if (nsegs == PR_MAX_SEGS) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (nsegs == PR_MAX_SEGS) return -1; 0 213381 213381
98 213381 seg_p[nsegs] = start;
99 213381 seg_l[nsegs] = (STRLEN)((path + j) - start);
100 213381 nsegs++;
101 213381 start = path + j + 1;
102 }
103 }
104 107952 return nsegs;
105 }
106  
107 /* Do a record's segments match these request segments? Method-agnostic, no
108 * captures - the shared core of hit matching and the Allow scan. */
109 107753 static int pr_segs_match(pr_rec *r, const char *path, STRLEN plen,
110 const char **seg_p, STRLEN *seg_l, int nsegs) {
111 int k;
112 107753 ▶ if (r->splat ? (nsegs < r->nsegs) : (nsegs != r->nsegs)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (r->splat ? (nsegs < r->nsegs) : (nsegs != r->nsegs)) return 0; 32 107721 107753
if (r->splat ? (nsegs < r->nsegs) : (nsegs != r->nsegs)) return 0; 364 107389 107753
113 319965 ▶ for (k = 0; k < r->nsegs; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < r->nsegs; k++) { 212688 107277 319965
114 212688 pr_seg *s = &r->segs[k];
115 212688 ▶ if (s->type == PR_LIT) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->type == PR_LIT) { 105384 107304 212688
116 105384 ▶ if (seg_l[k] != s->litlen /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
if (seg_l[k] != s->litlen /* # uncoverable condition right count:2 class:ignore_covered_err */ 105321 63 105384
117 105321 ▶ || (s->litlen && memNE(seg_p[k], s->lit, s->litlen)))
Branch: 2 branches
BranchTrueFalseTotal
|| (s->litlen && memNE(seg_p[k], s->lit, s->litlen))) 105321 0 105321
|| (s->litlen && memNE(seg_p[k], s->lit, s->litlen))) 25 105296 105321
118 88 return 0;
119 }
120 107304 ▶ else if (s->type == PR_PARAM) {
Branch: 1 branch
BranchTrueFalseTotal
else if (s->type == PR_PARAM) { 107297 7 107304
121 107297 ▶ if (!seg_l[k]) return 0; /* [^/]+ needs one char */
Branch: 1 branch
BranchTrueFalseTotal
if (!seg_l[k]) return 0; /* [^/]+ needs one char */ 17 107280 107297
122 }
123 else { /* PR_SPLAT (always last): non-empty remainder */
124 7 return ((STRLEN)((path + plen) - seg_p[k])) ? 1 : 0;
125 }
126 }
127 107277 return 1;
128 }
129  
130 107694 static int pr_method_ok(pr_rec *r, const char *method, STRLEN mlen, int is_head) {
131 /*
132 # uncoverable branch true
133 # uncoverable condition left count:1 class:ignore_covered_err */
134 107694 ▶ if (r->is_any) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (r->is_any) return 1; 0 107694 107694
135 107694 ▶ if (mlen == r->mlen && memEQ(method, r->method, mlen)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (mlen == r->mlen && memEQ(method, r->method, mlen)) return 1; 107533 161 107694
if (mlen == r->mlen && memEQ(method, r->method, mlen)) return 1; 107414 119 107533
136 280 ▶ if (is_head && r->is_get) return 1; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (is_head && r->is_get) return 1; /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 278 280
if (is_head && r->is_get) return 1; /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 0 2
137 278 return 0;
138 }
139  
140 /* Parse one route path into a record's typed segments. */
141 144 static void pr_parse_path(pTHX_ pr_rec *r, const char *p, STRLEN pl) {
142 const char *seg_start;
143 STRLEN j;
144 144 int ns = 0, k;
145 1611 ▶ for (j = 1; j <= pl; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 1; j <= pl; j++) 1467 144 1611
146 1467 ▶ if (j == pl || p[j] == '/') ns++;
Branch: 2 branches
BranchTrueFalseTotal
if (j == pl || p[j] == '/') ns++; 1323 144 1467
if (j == pl || p[j] == '/') ns++; 151 1172 1323
147 /*
148 # uncoverable branch true
149 # uncoverable condition left count:1 class:ignore_covered_err */
150 144 ▶ if (ns > PR_MAX_SEGS)
Branch: 1 branch
BranchTrueFalseTotal
if (ns > PR_MAX_SEGS) 0 144 144
151 0 croak("punk router: '%.*s' has more than %d segments", /* # uncoverable statement */
152 (int)pl, p, PR_MAX_SEGS); /* # uncoverable statement */
153 144 r->nsegs = ns;
154 144 ▶ Newxz(r->segs, ns ? ns : 1, pr_seg); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
Newxz(r->segs, ns ? ns : 1, pr_seg); /* # uncoverable condition right count:1 class:ignore_covered_err */ 144 0 144
155 144 seg_start = p + 1;
156 1611 ▶ for (k = 0, j = 1; j <= pl; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0, j = 1; j <= pl; j++) { 1467 144 1611
157 1467 ▶ if (j == pl || p[j] == '/') {
Branch: 2 branches
BranchTrueFalseTotal
if (j == pl || p[j] == '/') { 1323 144 1467
if (j == pl || p[j] == '/') { 151 1172 1323
158 295 STRLEN slen = (STRLEN)((p + j) - seg_start);
159 295 pr_seg *s = &r->segs[k];
160 295 ▶ if (slen > 1 && seg_start[0] == ':') {
Branch: 2 branches
BranchTrueFalseTotal
if (slen > 1 && seg_start[0] == ':') { 279 16 295
if (slen > 1 && seg_start[0] == ':') { 144 135 279
161 144 s->type = PR_PARAM;
162 144 s->name = newSVpvn(seg_start + 1, slen - 1);
163 }
164 151 ▶ else if (slen > 1 && seg_start[0] == '*') {
Branch: 2 branches
BranchTrueFalseTotal
else if (slen > 1 && seg_start[0] == '*') { 135 16 151
else if (slen > 1 && seg_start[0] == '*') { 4 131 135
165 4 s->type = PR_SPLAT;
166 4 s->name = newSVpvn(seg_start + 1, slen - 1);
167 /*
168 # uncoverable branch true
169 # uncoverable condition left count:1 class:ignore_covered_err */
170 4 ▶ if (k != ns - 1)
Branch: 1 branch
BranchTrueFalseTotal
if (k != ns - 1) 0 4 4
171 0 croak("punk router: *splat must be the last segment " /* # uncoverable statement */
172 "in '%.*s'", (int)pl, p); /* # uncoverable statement */
173 4 r->splat = 1;
174 }
175 else {
176 147 s->type = PR_LIT;
177 147 s->lit = savepvn(seg_start, slen);
178 147 s->litlen = slen;
179 }
180 295 k++;
181 295 seg_start = p + j + 1;
182 }
183 }
184 144 }
185  
186 /* Insert one static "METHOD path" -> idx into the exact table and the
187 * per-path Allow list, croaking on a duplicate. */
188 61839 static void pr_add_static(pTHX_ pr_router *rt, HV *seen,
189 const char *m, STRLEN ml,
190 const char *p, STRLEN pl, IV idx) {
191 61839 STRLEN kl = ml + 1 + pl;
192 61839 SV *key = sv_2mortal(newSV(kl + 1));
193 char *kbuf;
194 SV **slot;
195 61839 SvPOK_on(key);
196 61839 kbuf = SvPVX(key);
197 61839 memcpy(kbuf, m, ml); kbuf[ml] = ' ';
198 61839 memcpy(kbuf + ml + 1, p, pl); kbuf[kl] = '\0';
199 61839 SvCUR_set(key, kl);
200 61839 ▶ if (hv_exists_ent(seen, key, 0))
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists_ent(seen, key, 0)) 2 61837 61839
201 2 croak("Punk::Router: duplicate route %.*s %.*s",
202 (int)ml, m, (int)pl, p);
203 61837 (void)hv_store_ent(seen, key, PUNK_SET_TRUE, 0);
204 61837 (void)hv_store_ent(rt->statics, key, newSViv(idx), 0);
205 61837 slot = hv_fetch(rt->static_paths, p, (I32)pl, 1);
206 /*
207 # uncoverable condition left count:1 class:ignore_covered_err
208 # uncoverable condition left count:3 class:ignore_covered_err */
209 61837 ▶ if (!(*slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV))
Branch: 3 branches
BranchTrueFalseTotal
if (!(*slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV)) 61837 0 61837
if (!(*slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV)) 175 61662 61837
if (!(*slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV)) 0 175 175
210 61662 sv_setsv(*slot, sv_2mortal(newRV_noinc((SV *)newAV())));
211 61837 av_push((AV *)SvRV(*slot), newSVpvn(m, ml));
212 61837 }
213  
214 /* Compile the whole route table in C from one AV of resolved routes -
215 * each [ METHOD, path, code, guards ]. Classifies static vs dynamic (a ':'
216 * or '*' anywhere makes it dynamic), expands ANY static routes per method,
217 * rejects duplicates and a misplaced *splat, parses dynamic paths into typed
218 * segments, and builds the exact table, the per-path Allow lists, the
219 * dynamic records and the per-route { code, guards, method, path } record
220 * hashrefs the dispatcher indexes into. The only work left in Perl is
221 * resolving the targets to those coderefs. */
222 /* Call the app's Perl $resolve->($target, $desc) to turn a route target or
223 * guard into a coderef. Returns the resolved SV (+1). A bad target croaks
224 * inside $resolve, unwinding to_app - the boot-time contract. */
225 61883 static SV *pr_resolve(pTHX_ SV *resolve, SV *target, SV *desc) {
226 61883 dSP;
227 SV *ret;
228 int count;
229 61883 ENTER; SAVETMPS;
230 61883 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 61883 61883
231 61883 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 61883 61883
232 61883 PUSHs(target);
233 61883 PUSHs(desc);
234 61883 PUTBACK;
235 61883 count = call_sv(resolve, G_SCALAR);
236 61869 SPAGAIN;
237 61869 ▶ ret = count > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
ret = count > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ 61869 0 61869
238 61869 PUTBACK;
239 61869 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 61869 0 61869
240 61869 return ret;
241 }
242  
243 /* One { method, path, target, guards } record -> a resolved
244 * [ method, path, code, \@guards ] route pushed onto `routes`. */
245 61835 static void pr_add_route(pTHX_ AV *routes, SV *resolve, HV *rec) {
246 61835 SV **m = hv_fetchs(rec, "method", 0);
247 61835 SV **p = hv_fetchs(rec, "path", 0);
248 61835 SV **t = hv_fetchs(rec, "target", 0);
249 61835 SV **g = hv_fetchs(rec, "guards", 0);
250 /*
251 # uncoverable condition right count:1 class:ignore_covered_err
252 # uncoverable condition right count:2 class:ignore_covered_err */
253 61835 ▶ SV *mv = (m && *m) ? *m : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
SV *mv = (m && *m) ? *m : &PL_sv_undef; 61835 0 61835
SV *mv = (m && *m) ? *m : &PL_sv_undef; 61835 0 61835
254 /*
255 # uncoverable condition right count:1 class:ignore_covered_err
256 # uncoverable condition right count:2 class:ignore_covered_err */
257 61835 ▶ SV *pv = (p && *p) ? *p : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
SV *pv = (p && *p) ? *p : &PL_sv_undef; 61835 0 61835
SV *pv = (p && *p) ? *p : &PL_sv_undef; 61835 0 61835
258 61835 AV *route = newAV();
259 61835 AV *gav = newAV();
260 123670 ▶ SV *desc = sv_2mortal(newSVpvf("route %s %s",
Branch: 1 branch
BranchTrueFalseTotal
SV *desc = sv_2mortal(newSVpvf("route %s %s", 61835 0 61835
261 SvPV_nolen(mv), SvPV_nolen(pv)));
262 61835 av_push(route, newSVsv(mv));
263 61835 av_push(route, newSVsv(pv));
264 /*
265 # uncoverable condition right count:1 class:ignore_covered_err
266 # uncoverable condition right count:2 class:ignore_covered_err */
267 61835 ▶ av_push(route, pr_resolve(aTHX_ resolve, (t && *t) ? *t : &PL_sv_undef, desc));
Branch: 2 branches
BranchTrueFalseTotal
av_push(route, pr_resolve(aTHX_ resolve, (t && *t) ? *t : &PL_sv_undef, desc)); 61835 0 61835
av_push(route, pr_resolve(aTHX_ resolve, (t && *t) ? *t : &PL_sv_undef, desc)); 61835 0 61835
268 /*
269 # uncoverable condition right count:2 class:ignore_covered_err
270 # uncoverable condition right count:3 class:ignore_covered_err
271 # uncoverable condition right count:4 class:ignore_covered_err */
272 61821 ▶ if (g && *g && SvROK(*g) && SvTYPE(SvRV(*g)) == SVt_PVAV) {
Branch: 4 branches
BranchTrueFalseTotal
if (g && *g && SvROK(*g) && SvTYPE(SvRV(*g)) == SVt_PVAV) { 61813 8 61821
if (g && *g && SvROK(*g) && SvTYPE(SvRV(*g)) == SVt_PVAV) { 61813 0 61813
if (g && *g && SvROK(*g) && SvTYPE(SvRV(*g)) == SVt_PVAV) { 61813 0 61813
if (g && *g && SvROK(*g) && SvTYPE(SvRV(*g)) == SVt_PVAV) { 61813 0 61813
273 61813 AV *gs = (AV *)SvRV(*g);
274 61813 SSize_t i, n = av_len(gs) + 1;
275 61813 SV *gdesc = sv_2mortal(newSVpvf("guard on %s", SvPV_nolen(pv)));
276 61861 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 48 61813 61861
277 48 SV **gp = av_fetch(gs, i, 0);
278 48 ▶ av_push(gav, pr_resolve(aTHX_ resolve,
Branch: 2 branches
BranchTrueFalseTotal
av_push(gav, pr_resolve(aTHX_ resolve, 48 0 48
av_push(gav, pr_resolve(aTHX_ resolve, 48 0 48
279 (gp && *gp) ? *gp : &PL_sv_undef, gdesc));
280 }
281 }
282 61821 av_push(route, newRV_noinc((SV *)gav));
283 61821 av_push(routes, newRV_noinc((SV *)route));
284 61821 }
285  
286 861 static void pr_build(pTHX_ pr_router *rt, AV *routes) {
287 861 int n = (int)(av_len(routes) + 1), i, dn = 0, di = 0;
288 861 HV *seen = (HV *)sv_2mortal((SV *)newHV());
289 861 rt->statics = newHV();
290 861 rt->static_paths = newHV();
291 861 rt->records = newAV();
292 861 rt->nrecs = n;
293 861 ▶ if (n > 0) av_extend(rt->records, n - 1);
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) av_extend(rt->records, n - 1); 722 139 861
294 861 ▶ Newx(rt->dyn_of, n ? n : 1, IV);
Branch: 1 branch
BranchTrueFalseTotal
Newx(rt->dyn_of, n ? n : 1, IV); 722 139 861
295 62682 ▶ for (i = 0; i < n; i++) rt->dyn_of[i] = -1;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) rt->dyn_of[i] = -1; 61821 861 62682
296  
297 62682 ▶ for (i = 0; i < n; i++) { /* count dynamics for the record array */
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { /* count dynamics for the record array */ 61821 861 62682
298 61821 SV **e = av_fetch(routes, i, 0);
299 AV *t; SV **psv; STRLEN pl; const char *p;
300 /*
301 # uncoverable branch true
302 # uncoverable condition left count:1 class:ignore_covered_err
303 # uncoverable condition left count:2 class:ignore_covered_err
304 # uncoverable condition left count:3 class:ignore_covered_err
305 # uncoverable condition left count:4 class:ignore_covered_err */
306 61821 ▶ if (!e || !*e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVAV)
Branch: 4 branches
BranchTrueFalseTotal
if (!e || !*e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVAV) 61821 0 61821
if (!e || !*e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVAV) 61821 0 61821
if (!e || !*e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVAV) 61821 0 61821
if (!e || !*e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVAV) 0 61821 61821
307 0 croak("Punk::Router::_compile: route %d is not an arrayref", i); /* # uncoverable statement */
308 61821 t = (AV *)SvRV(*e);
309 61821 psv = av_fetch(t, 1, 0);
310 61821 p = SvPV_const(*psv, pl);
311 61821 ▶ if (memchr(p, ':', pl) || memchr(p, '*', pl)) dn++;
Branch: 2 branches
BranchTrueFalseTotal
if (memchr(p, ':', pl) || memchr(p, '*', pl)) dn++; 61681 140 61821
if (memchr(p, ':', pl) || memchr(p, '*', pl)) dn++; 4 61677 61681
312 }
313 861 ▶ Newxz(rt->recs, dn ? dn : 1, pr_rec);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(rt->recs, dn ? dn : 1, pr_rec); 100 761 861
314  
315 62680 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 61821 859 62680
316 61821 SV **e = av_fetch(routes, i, 0);
317 61821 AV *t = (AV *)SvRV(*e);
318 61821 SV **msv = av_fetch(t, 0, 0), **psv = av_fetch(t, 1, 0);
319 61821 SV **csv = av_fetch(t, 2, 0), **gsv = av_fetch(t, 3, 0);
320 STRLEN ml, pl;
321 61821 const char *m = SvPV_const(*msv, ml);
322 61821 ▶ const char *p = SvPV_const(*psv, pl);
Branch: 1 branch
BranchTrueFalseTotal
const char *p = SvPV_const(*psv, pl); 61821 0 61821
323 61821 ▶ int is_any = (ml == 3 && memEQ(m, "ANY", 3));
Branch: 2 branches
BranchTrueFalseTotal
int is_any = (ml == 3 && memEQ(m, "ANY", 3)); 61562 259 61821
int is_any = (ml == 3 && memEQ(m, "ANY", 3)); 27 61535 61562
324 61821 HV *rec = newHV();
325  
326 /*
327 # uncoverable condition right count:1 class:ignore_covered_err
328 # uncoverable condition right count:2 class:ignore_covered_err */
329 61821 ▶ (void)hv_stores(rec, "code", csv && *csv ? SvREFCNT_inc(*csv) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_stores(rec, "code", csv && *csv ? SvREFCNT_inc(*csv) : newSV(0)); 61821 0 61821
(void)hv_stores(rec, "code", csv && *csv ? SvREFCNT_inc(*csv) : newSV(0)); 61821 0 61821
330 /*
331 # uncoverable condition right count:1 class:ignore_covered_err
332 # uncoverable condition right count:2 class:ignore_covered_err */
333 61821 ▶ (void)hv_stores(rec, "guards", gsv && *gsv ? SvREFCNT_inc(*gsv) : newRV_noinc((SV *)newAV()));
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_stores(rec, "guards", gsv && *gsv ? SvREFCNT_inc(*gsv) : newRV_noinc((SV *)newAV())); 61821 0 61821
(void)hv_stores(rec, "guards", gsv && *gsv ? SvREFCNT_inc(*gsv) : newRV_noinc((SV *)newAV())); 61821 0 61821
334 61821 (void)hv_stores(rec, "method", newSVpvn(m, ml));
335 61821 (void)hv_stores(rec, "path", newSVpvn(p, pl));
336 61821 (void)av_store(rt->records, i, newRV_noinc((SV *)rec));
337  
338 61821 ▶ if (!memchr(p, ':', pl) && !memchr(p, '*', pl)) { /* static */
Branch: 2 branches
BranchTrueFalseTotal
if (!memchr(p, ':', pl) && !memchr(p, '*', pl)) { /* static */ 61681 140 61821
if (!memchr(p, ':', pl) && !memchr(p, '*', pl)) { /* static */ 61677 4 61681
339 61677 ▶ if (is_any) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_any) { 27 61650 61677
340 int k;
341 216 ▶ for (k = 0; k < PR_N_ANY; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < PR_N_ANY; k++) 189 27 216
342 189 pr_add_static(aTHX_ rt, seen, PR_ANY_METHODS[k],
343 189 strlen(PR_ANY_METHODS[k]), p, pl, (IV)i);
344 }
345 61650 else pr_add_static(aTHX_ rt, seen, m, ml, p, pl, (IV)i);
346 }
347 else { /* dynamic */
348 144 pr_rec *r = &rt->recs[di];
349 144 rt->dyn_of[i] = (IV)di;
350 144 di++;
351 144 r->method = savepvn(m, ml);
352 144 r->mlen = ml;
353 144 r->is_any = is_any;
354 144 ▶ r->is_get = (ml == 3 && memEQ(m, "GET", 3));
Branch: 2 branches
BranchTrueFalseTotal
r->is_get = (ml == 3 && memEQ(m, "GET", 3)); 131 13 144
r->is_get = (ml == 3 && memEQ(m, "GET", 3)); 129 2 131
355 144 r->idx = (IV)i;
356 144 pr_parse_path(aTHX_ r, p, pl); /* croaks on a misplaced *splat */
357 }
358 }
359 859 rt->n = di;
360 859 rt->compiled = 1;
361 859 }
362  
363 1429 static void pr_free(pTHX_ pr_router *rt) {
364 int i, k;
365 /*
366 # uncoverable branch true
367 # uncoverable condition left count:1 class:ignore_covered_err */
368 1429 ▶ if (!rt) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!rt) return; 0 1429 1429
369 /*
370 # uncoverable branch false
371 # uncoverable condition right count:1 class:ignore_covered_err */
372 1429 ▶ if (rt->raw) SvREFCNT_dec((SV *)rt->raw);
Branch: 1 branch
BranchTrueFalseTotal
if (rt->raw) SvREFCNT_dec((SV *)rt->raw); 1429 0 1429
373 1573 ▶ for (i = 0; i < rt->n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < rt->n; i++) { 144 1429 1573
374 144 pr_rec *r = &rt->recs[i];
375 439 ▶ for (k = 0; k < r->nsegs; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < r->nsegs; k++) { 295 144 439
376 295 ▶ if (r->segs[k].lit) Safefree(r->segs[k].lit);
Branch: 1 branch
BranchTrueFalseTotal
if (r->segs[k].lit) Safefree(r->segs[k].lit); 147 148 295
377 295 ▶ if (r->segs[k].name) SvREFCNT_dec(r->segs[k].name);
Branch: 1 branch
BranchTrueFalseTotal
if (r->segs[k].name) SvREFCNT_dec(r->segs[k].name); 148 147 295
378 }
379 144 Safefree(r->segs);
380 /*
381 # uncoverable branch false
382 # uncoverable condition right count:1 class:ignore_covered_err */
383 144 ▶ if (r->method) Safefree(r->method);
Branch: 1 branch
BranchTrueFalseTotal
if (r->method) Safefree(r->method); 144 0 144
384 }
385 1429 Safefree(rt->recs);
386 1429 Safefree(rt->dyn_of);
387 1429 ▶ if (rt->statics) SvREFCNT_dec((SV *)rt->statics);
Branch: 1 branch
BranchTrueFalseTotal
if (rt->statics) SvREFCNT_dec((SV *)rt->statics); 861 568 1429
388 1429 ▶ if (rt->static_paths) SvREFCNT_dec((SV *)rt->static_paths);
Branch: 1 branch
BranchTrueFalseTotal
if (rt->static_paths) SvREFCNT_dec((SV *)rt->static_paths); 861 568 1429
389 1429 ▶ if (rt->records) SvREFCNT_dec((SV *)rt->records);
Branch: 1 branch
BranchTrueFalseTotal
if (rt->records) SvREFCNT_dec((SV *)rt->records); 861 568 1429
390 1429 Safefree(rt);
391 }
392  
393 /* Static exact match: the record index, or -1. HEAD falls back to GET. */
394 221551 static IV pr_static_match(pTHX_ pr_router *rt,
395 const char *method, STRLEN mlen,
396 const char *path, STRLEN plen) {
397 char buf[256], *kbuf;
398 221551 STRLEN kl = mlen + 1 + plen;
399 SV **e;
400 221551 IV idx = -1;
401 221551 ▶ kbuf = kl + 1 <= sizeof buf ? buf : (char *)safemalloc(kl + 1);
Branch: 1 branch
BranchTrueFalseTotal
kbuf = kl + 1 <= sizeof buf ? buf : (char *)safemalloc(kl + 1); 221550 1 221551
402 221551 memcpy(kbuf, method, mlen); kbuf[mlen] = ' ';
403 221551 memcpy(kbuf + mlen + 1, path, plen);
404 221551 e = hv_fetch(rt->statics, kbuf, (I32)kl, 0);
405 221551 ▶ if (!e && mlen == 4 && memEQ(method, "HEAD", 4)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!e && mlen == 4 && memEQ(method, "HEAD", 4)) { 107934 113617 221551
if (!e && mlen == 4 && memEQ(method, "HEAD", 4)) { 160 107774 107934
if (!e && mlen == 4 && memEQ(method, "HEAD", 4)) { 23 137 160
406 23 memcpy(kbuf, "GET", 3); kbuf[3] = ' ';
407 23 memcpy(kbuf + 4, path, plen);
408 23 e = hv_fetch(rt->statics, kbuf, (I32)(4 + plen), 0);
409 }
410 221551 ▶ if (e && *e) idx = SvIV(*e); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (e && *e) idx = SvIV(*e); /* # uncoverable condition right count:2 class:ignore_covered_err */ 113636 107915 221551
if (e && *e) idx = SvIV(*e); /* # uncoverable condition right count:2 class:ignore_covered_err */ 113636 0 113636
411 221551 ▶ if (kbuf != buf) safefree(kbuf);
Branch: 1 branch
BranchTrueFalseTotal
if (kbuf != buf) safefree(kbuf); 1 221550 221551
412 221551 return idx;
413 }
414  
415 /* Dynamic hit: the record index (its @recs idx) and fills caps, or -1. */
416 107609 static IV pr_match(pTHX_ pr_router *rt,
417 const char *method, STRLEN mlen,
418 const char *path, STRLEN plen, HV *caps) {
419 const char *seg_p[PR_MAX_SEGS];
420 STRLEN seg_l[PR_MAX_SEGS];
421 107609 int nsegs = pr_split(path, plen, seg_p, seg_l);
422 107609 ▶ int is_head = (mlen == 4 && memEQ(method, "HEAD", 4));
Branch: 2 branches
BranchTrueFalseTotal
int is_head = (mlen == 4 && memEQ(method, "HEAD", 4)); 120 107489 107609
int is_head = (mlen == 4 && memEQ(method, "HEAD", 4)); 2 118 120
423 int i, c;
424 /*
425 # uncoverable branch true
426 # uncoverable condition left count:1 class:ignore_covered_err */
427 107609 ▶ if (nsegs < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (nsegs < 0) return -1; 0 107609 107609
428 108030 ▶ for (i = 0; i < rt->n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < rt->n; i++) { 107694 336 108030
429 107694 pr_rec *r = &rt->recs[i];
430 107694 ▶ if (!pr_method_ok(r, method, mlen, is_head)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!pr_method_ok(r, method, mlen, is_head)) continue; 278 107416 107694
431 107416 ▶ if (!pr_segs_match(r, path, plen, seg_p, seg_l, nsegs)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!pr_segs_match(r, path, plen, seg_p, seg_l, nsegs)) continue; 143 107273 107416
432 319820 ▶ for (c = 0; c < r->nsegs; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < r->nsegs; c++) { 212547 107273 319820
433 212547 pr_seg *s = &r->segs[c];
434 212547 ▶ if (s->type == PR_PARAM)
Branch: 1 branch
BranchTrueFalseTotal
if (s->type == PR_PARAM) 107271 105276 212547
435 107271 (void)hv_store_ent(caps, s->name,
436 newSVpvn(seg_p[c], seg_l[c]), 0);
437 105276 ▶ else if (s->type == PR_SPLAT)
Branch: 1 branch
BranchTrueFalseTotal
else if (s->type == PR_SPLAT) 5 105271 105276
438 5 (void)hv_store_ent(caps, s->name,
439 newSVpvn(seg_p[c], (STRLEN)((path + plen) - seg_p[c])), 0);
440 }
441 107273 return r->idx;
442 }
443 336 return -1;
444 }
445  
446 /* The 405 Allow set for a path the request method missed: every method the
447 * path answers under (static exact + dynamic, method-agnostic), minus the
448 * request method. Pushes sorted method SVs into `allow`; returns the count. */
449 343 static int pr_allow(pTHX_ pr_router *rt,
450 const char *method, STRLEN mlen,
451 const char *path, STRLEN plen, AV *allow) {
452 const char *seg_p[PR_MAX_SEGS];
453 STRLEN seg_l[PR_MAX_SEGS];
454 343 int nsegs = pr_split(path, plen, seg_p, seg_l);
455 343 HV *seen = (HV *)sv_2mortal((SV *)newHV());
456 SV **sp;
457 int i;
458  
459 343 sp = hv_fetch(rt->static_paths, path, (I32)plen, 0);
460 /*
461 # uncoverable condition right count:2 class:ignore_covered_err
462 # uncoverable condition right count:3 class:ignore_covered_err
463 # uncoverable condition right count:4 class:ignore_covered_err */
464 343 ▶ if (sp && *sp && SvROK(*sp) && SvTYPE(SvRV(*sp)) == SVt_PVAV) {
Branch: 4 branches
BranchTrueFalseTotal
if (sp && *sp && SvROK(*sp) && SvTYPE(SvRV(*sp)) == SVt_PVAV) { 38 305 343
if (sp && *sp && SvROK(*sp) && SvTYPE(SvRV(*sp)) == SVt_PVAV) { 38 0 38
if (sp && *sp && SvROK(*sp) && SvTYPE(SvRV(*sp)) == SVt_PVAV) { 38 0 38
if (sp && *sp && SvROK(*sp) && SvTYPE(SvRV(*sp)) == SVt_PVAV) { 38 0 38
465 38 AV *ms = (AV *)SvRV(*sp);
466 38 SSize_t j, n = av_len(ms) + 1;
467 85 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 47 38 85
468 47 SV **mv = av_fetch(ms, j, 0);
469 /*
470 # uncoverable branch false
471 # uncoverable condition right count:1 class:ignore_covered_err
472 # uncoverable condition right count:2 class:ignore_covered_err */
473 47 ▶ if (mv && *mv) (void)hv_store_ent(seen, *mv, PUNK_SET_TRUE, 0);
Branch: 2 branches
BranchTrueFalseTotal
if (mv && *mv) (void)hv_store_ent(seen, *mv, PUNK_SET_TRUE, 0); 47 0 47
if (mv && *mv) (void)hv_store_ent(seen, *mv, PUNK_SET_TRUE, 0); 47 0 47
474 }
475 }
476 /*
477 # uncoverable branch false
478 # uncoverable condition right count:1 class:ignore_covered_err */
479 343 ▶ if (nsegs >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nsegs >= 0) { 343 0 343
480 680 ▶ for (i = 0; i < rt->n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < rt->n; i++) { 337 343 680
481 337 pr_rec *r = &rt->recs[i];
482 337 ▶ if (!pr_segs_match(r, path, plen, seg_p, seg_l, nsegs)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!pr_segs_match(r, path, plen, seg_p, seg_l, nsegs)) continue; 328 9 337
483 9 (void)hv_store(seen, r->method, (I32)r->mlen, PUNK_SET_TRUE, 0);
484 }
485 }
486 /* the request method is not "other" */
487 343 (void)hv_delete(seen, method, (I32)mlen, 0);
488  
489 {
490 343 SSize_t na = 0;
491 HE *he;
492 343 hv_iterinit(seen);
493 394 ▶ while ((he = hv_iternext(seen))) { av_push(allow, newSVsv(hv_iterkeysv(he))); na++; }
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(seen))) { av_push(allow, newSVsv(hv_iterkeysv(he))); na++; } 51 343 394
494 /* sort the Allow list, matching the Perl `sort keys %allow` */
495 343 ▶ if (na > 1) sortsv(AvARRAY(allow), (STRLEN)na, Perl_sv_cmp);
Branch: 1 branch
BranchTrueFalseTotal
if (na > 1) sortsv(AvARRAY(allow), (STRLEN)na, Perl_sv_cmp); 10 333 343
496 343 return (int)na;
497 }
498 }
499  
500 #endif /* PUNK_ROUTE_H */
« include/punk/punk_plural.h ↑ Summary xs/csrf.xs »