xs/app.xs

filter: Statementstmt 98.0% 1178 / 1201 Branchbran 74.1% 1002 / 1350 (3 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 85.4% 2180 / 2551 (3 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/static.xs ↑ Summary xs/computed.xs »
1 MODULE = Punk PACKAGE = Punk::App
2  
3 PROTOTYPES: DISABLE
4  
5 # The registrar surface, in C (punk_app.h). The app is a blessed hash the DSL
6 # keywords push into; these methods are XSUBs. The boot orchestration that
7 # drives the other subsystems (compile, the config loader, target/context
8 # resolution) stays Perl in Punk::App.pm.
9  
10 SV *
11 new(class, ...)
12 SV *class
13 CODE:
14 {
15 1428 HV *h = newHV();
16 1428 HV *hooks = newHV();
17 SV *rc, *router;
18 int i;
19 2835 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 1407 1428 2835
20 1407 STRLEN kl; const char *k = SvPV_const(ST(i), kl);
21 1407 ▶ # uncoverable branch false
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch false 1407 0 1407
# uncoverable branch false 1407 0 1407
22 1407 # uncoverable condition right count:1 class:ignore_covered_err
23 # uncoverable condition right count:2 class:ignore_covered_err
24 1428 ▶ if (kl == 6 && memEQ(k, K_CALLER, 6))
Branch: 1 branch
BranchTrueFalseTotal
if (kl == 6 && memEQ(k, K_CALLER, 6)) 21 1407 1428
25 1428 (void)hv_stores(h, K_CALLER, newSVsv(ST(i + 1)));
26 1428 }
27 1428 ▶ if (!hv_exists(h, K_CALLER, 6)) (void)hv_stores(h, K_CALLER, newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_exists(h, K_CALLER, 6)) (void)hv_stores(h, K_CALLER, newSV(0)); 0 1428 1428
28 1428 rc = sv_2mortal(newSVpvs(PK_ROUTER));
29 1428 router = pcx_call_meth(aTHX_ rc, "new", NULL, 0, 1);
30 1428 (void)hv_stores(h, K_ROUTER, router ? router : newSV(0)); /* # uncoverable condition left count:1 class:ignore_covered_err */
31 1428 (void)hv_stores(h, K_MOUNTS, newRV_noinc((SV *)newAV()));
32 1428 (void)hv_stores(h, K_VIEWS, newRV_noinc((SV *)newAV()));
33 1428 (void)hv_stores(h, K_DATABASES, newRV_noinc((SV *)newHV()));
34 1428 (void)hv_stores(h, K_MODELS, newRV_noinc((SV *)newAV()));
35 1428 (void)hv_stores(hooks, K_BEFORE_R, newRV_noinc((SV *)newAV()));
36 1428 (void)hv_stores(hooks, K_BEFORE_D, newRV_noinc((SV *)newAV()));
37 1428 (void)hv_stores(hooks, K_AFTER_D, newRV_noinc((SV *)newAV()));
38 1428 (void)hv_stores(hooks, K_BEFORE_RN, newRV_noinc((SV *)newAV()));
39 1428 (void)hv_stores(hooks, K_AFTER_RES, newRV_noinc((SV *)newAV()));
40 1428 (void)hv_stores(h, K_HOOKS, newRV_noinc((SV *)hooks));
41 1428 (void)hv_stores(h, K_MIDDLEWARE, newRV_noinc((SV *)newAV()));
42 1428 (void)hv_stores(h, K_HELPERS, newRV_noinc((SV *)newHV()));
43 (void)hv_stores(h, K_ON_ERROR, newSV(0));
44 (void)hv_stores(h, K_COMPILED, newSViv(0));
45 RETVAL = sv_bless(newRV_noinc((SV *)h), gv_stashsv(class, GV_ADD));
46 }
47 OUTPUT:
48 RETVAL
49  
50 SV *
51 caller_class(self)
52 SV *self
53 CODE:
54 {
55 32 SV *c = app_get(aTHX_ app_hv(aTHX_ self), K_CALLER);
56 32 ▶ RETVAL = c ? newSVsv(c) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = c ? newSVsv(c) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ 32 0 32
57 }
58 OUTPUT:
59 RETVAL
60  
61 SV *
62 config_object(self)
63 SV *self
64 CODE:
65 {
66 3157 SV *c = app_get(aTHX_ app_hv(aTHX_ self), K_CONFIG);
67 3157 ▶ RETVAL = c ? newSVsv(c) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = c ? newSVsv(c) : &PL_sv_undef; 49 3108 3157
68 }
69 OUTPUT:
70 RETVAL
71  
72 # route($method, $path, $target, $guards, $opts?): $self->{router}->add(...)
73 # plus, for a `validate` option, a record in validate_routes for
74 # compile_extras to compile once at boot. Unknown option keys croak. Chains.
75 SV *
76 route(self, method, path, target, guards = &PL_sv_undef, opts = &PL_sv_undef)
77 SV *self
78 SV *method
79 SV *path
80 SV *target
81 SV *guards
82 SV *opts
83 CODE:
84 {
85 61851 HV *h = app_hv(aTHX_ self);
86 61851 SV *router = app_get(aTHX_ h, K_ROUTER);
87 SV *argv[8], *r;
88 /* the one-hashref form: post '/x' => { cb => ..., max_body => N }.
89 * Split here rather than in the keyword layer, because scoped verbs
90 * ($scope->post) reach this same method (xs/scope.xs) and get it for
91 * free. Everything below is untouched by which spelling was used. */
92 {
93 61851 SV *wm = sv_2mortal(newSVpvs("route "));
94 61851 sv_catsv(wm, method);
95 61851 pk_spec_split(aTHX_ SvPV_nolen(wm), path, &target, &opts);
96 }
97 61848 ▶ if (SvOK(opts)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(opts)) { 425 61423 61848
98 HV *oh; HE *he; SV **vp;
99 425 ▶ if (!(SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)) /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)) /* # uncoverable condition left count:2 class:ignore_covered_err */ 424 1 425
if (!(SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)) /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 424 424
100 1 croak("Punk: route options must be a hashref");
101 424 oh = (HV *)SvRV(opts);
102 424 hv_iterinit(oh);
103 849 ▶ while ((he = hv_iternext(oh))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(oh))) { 436 413 849
104 436 ▶ STRLEN kl; const char *k = HePV(he, kl);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl; const char *k = HePV(he, kl); 0 436 436
105 436 ▶ if (!strEQ(k, K_VALIDATE) && !strEQ(k, K_COMPRESS)
Branch: 2 branches
BranchTrueFalseTotal
if (!strEQ(k, K_VALIDATE) && !strEQ(k, K_COMPRESS) 425 11 436
if (!strEQ(k, K_VALIDATE) && !strEQ(k, K_COMPRESS) 417 8 425
106 417 ▶ && !strEQ(k, K_MAX_BODY) && !strEQ(k, K_SITEMAP)
Branch: 2 branches
BranchTrueFalseTotal
&& !strEQ(k, K_MAX_BODY) && !strEQ(k, K_SITEMAP) 379 38 417
&& !strEQ(k, K_MAX_BODY) && !strEQ(k, K_SITEMAP) 231 148 379
107 231 ▶ && !strEQ(k, K_ETAG) && !strEQ(k, K_IDEMPOTENT)
Branch: 2 branches
BranchTrueFalseTotal
&& !strEQ(k, K_ETAG) && !strEQ(k, K_IDEMPOTENT) 185 46 231
&& !strEQ(k, K_ETAG) && !strEQ(k, K_IDEMPOTENT) 144 41 185
108 144 ▶ && !strEQ(k, K_LAST_MODIFIED)
Branch: 1 branch
BranchTrueFalseTotal
&& !strEQ(k, K_LAST_MODIFIED) 134 10 144
109 134 ▶ && !strEQ(k, K_RATE_LIMIT)
Branch: 1 branch
BranchTrueFalseTotal
&& !strEQ(k, K_RATE_LIMIT) 106 28 134
110 106 ▶ && !strEQ(k, K_NAME))
Branch: 1 branch
BranchTrueFalseTotal
&& !strEQ(k, K_NAME)) 11 95 106
111 11 croak("Punk: unknown route option '%s'", k);
112 }
113 /* max_body: this route's ceiling on CONTENT_LENGTH, overriding
114 * the app-wide keyword. 0 means "do not check here", which is
115 * legitimate - unlike the server's ceiling, this one is policy
116 * and not the memory backstop. Recorded now, stamped onto the
117 * compiled record at to_app (the ws/sse/compress pattern). */
118 413 vp = hv_fetchs(oh, K_MAX_BODY, 0);
119 413 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 38 375 413
# uncoverable condition right count:2 class:ignore_covered_err 38 0 38
# uncoverable condition right count:2 class:ignore_covered_err 38 0 38
120 38 # uncoverable condition right count:3 class:ignore_covered_err
121 38 if (vp && *vp && SvOK(*vp)) {
122 38 HV *rec = newHV();
123 38 (void)hv_stores(rec, K_METHOD, newSVsv(method));
124 38 (void)hv_stores(rec, K_PATH, newSVsv(path));
125 (void)hv_stores(rec, K_MAX_BODY, newSViv(SvIV(*vp)));
126 av_push(app_av(aTHX_ h, K_MAXBODY_ROUTES),
127 newRV_noinc((SV *)rec));
128 }
129 /* compress => 0 opts this route out of the server's response
130 * compression. Recorded now, stamped onto the compiled record
131 * at to_app (the ws/sse pattern), and spelled on the way out as
132 * `Content-Encoding: identity` - a plain response header, which
133 * is the documented contract Hyperman honours and any other
134 413 * PSGI server could. There is no compress => 1: compressing is
135 413 ▶ * the server's default answer for a route that says nothing. */
Branch: 3 branches
BranchTrueFalseTotal
* the server's default answer for a route that says nothing. */ 8 405 413
* the server's default answer for a route that says nothing. */ 8 0 8
* the server's default answer for a route that says nothing. */ 6 2 8
136 6 vp = hv_fetchs(oh, K_COMPRESS, 0);
137 6 if (vp && *vp && !SvTRUE(*vp)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
138 6 HV *rec = newHV();
139 6 (void)hv_stores(rec, K_METHOD, newSVsv(method));
140 (void)hv_stores(rec, K_PATH, newSVsv(path));
141 av_push(app_av(aTHX_ h, K_NOCOMPRESS_ROUTES),
142 newRV_noinc((SV *)rec));
143 }
144 /* sitemap => 0 keeps this route out of Punk::Plugin::Sitemap's
145 * generated sitemap.xml, and disallows it in robots.txt.
146 *
147 * sitemap => 1 is the other direction and is NOT the opt-in half
148 * of a pair: inclusion is already the default for a route that
149 * says nothing. It overrides an exclusion the plugin inferred -
150 * in practice a guard, because a scope guard may be an
151 * authentication check or may be an ordinary filter, and Punk
152 * cannot read its intent. The plugin assumes the first, which is
153 413 * the safe direction, and this is how an application says the
154 413 ▶ * page is public anyway. */
Branch: 3 branches
BranchTrueFalseTotal
* page is public anyway. */ 148 265 413
* page is public anyway. */ 148 0 148
* page is public anyway. */ 148 0 148
155 148 vp = hv_fetchs(oh, K_SITEMAP, 0);
156 148 # uncoverable condition right count:2 class:ignore_covered_err
157 148 # uncoverable condition right count:3 class:ignore_covered_err
158 148 if (vp && *vp && SvOK(*vp)) {
159 148 HV *rec = newHV();
160 (void)hv_stores(rec, K_METHOD, newSVsv(method));
161 (void)hv_stores(rec, K_PATH, newSVsv(path));
162 (void)hv_stores(rec, K_SITEMAP, newSViv(SvTRUE(*vp) ? 1 : 0));
163 av_push(app_av(aTHX_ h, K_SITEMAP_ROUTES),
164 newRV_noinc((SV *)rec));
165 }
166 /* etag: the conditional-GET validator for this route, inert
167 * unless Punk::Plugin::ConditionalGet is registered - the
168 * `sitemap` arrangement, and for the same reason: the option
169 * belongs to the route, and only the application knows whether
170 * it wants the behaviour at all.
171 *
172 * A coderef is the STRONG validator: it is called before the
173 * handler and what it returns identifies the entity, so an
174 * unchanged one answers 304 without the handler running. The
175 413 * body ETag (`etag => 1`) is not implemented and croaks here
176 413 ▶ * rather than being accepted and quietly doing nothing - an
Branch: 3 branches
BranchTrueFalseTotal
* rather than being accepted and quietly doing nothing - an 46 367 413
* rather than being accepted and quietly doing nothing - an 46 0 46
* rather than being accepted and quietly doing nothing - an 46 0 46
177 * option that looks like it worked is worse than one that
178 46 ▶ * failed. */
Branch: 2 branches
BranchTrueFalseTotal
* failed. */ 24 22 46
* failed. */ 23 1 24
179 46 ▶ vp = hv_fetchs(oh, K_ETAG, 0);
Branch: 2 branches
BranchTrueFalseTotal
vp = hv_fetchs(oh, K_ETAG, 0); 23 23 46
vp = hv_fetchs(oh, K_ETAG, 0); 1 22 23
180 3 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 1 0 1
181 # uncoverable condition right count:3 class:ignore_covered_err
182 if (vp && *vp && SvOK(*vp)) {
183 HV *rec;
184 int is_cv = (SvROK(*vp) && SvTYPE(SvRV(*vp)) == SVt_PVCV);
185 if (!is_cv && SvROK(*vp))
186 45 ▶ croak("Punk: etag on %s %s takes a coderef (the strong "
Branch: 2 branches
BranchTrueFalseTotal
croak("Punk: etag on %s %s takes a coderef (the strong " 22 23 45
croak("Punk: etag on %s %s takes a coderef (the strong " 1 21 22
187 44 "validator) or 1 (the body ETag), not a %s",
188 44 SvPV_nolen(method), SvPV_nolen(path),
189 44 sv_reftype(SvRV(*vp), 0));
190 44 ▶ /* etag => 0 is "no", which is what a route says by saying
Branch: 1 branch
BranchTrueFalseTotal
/* etag => 0 is "no", which is what a route says by saying 23 21 44
191 * nothing: recorded as absent rather than as a third state */
192 44 if (!is_cv && !SvTRUE(*vp)) goto etag_done;
193 rec = newHV();
194 45 (void)hv_stores(rec, K_METHOD, newSVsv(method));
195 (void)hv_stores(rec, K_PATH, newSVsv(path));
196 (void)hv_stores(rec, K_ETAG,
197 is_cv ? newSVsv(*vp) : newSViv(1));
198 av_push(app_av(aTHX_ h, K_ETAG_ROUTES),
199 newRV_noinc((SV *)rec));
200 etag_done: ;
201 }
202 /* last_modified: the other dynamic validator, same arrangement
203 * as `etag` and inert without the same plugin. The coderef
204 * returns an epoch the route knows cheaply - a row's updated_at,
205 412 * a feed's newest entry - answered before the handler runs.
206 412 ▶ *
Branch: 3 branches
BranchTrueFalseTotal
* 10 402 412
* 10 0 10
* 10 0 10
207 * There is deliberately no `last_modified => 1`: the body form
208 10 ▶ * of `etag` hashes rendered bytes, and a rendered body has no
Branch: 2 branches
BranchTrueFalseTotal
* of `etag` hashes rendered bytes, and a rendered body has no 7 3 10
* of `etag` hashes rendered bytes, and a rendered body has no 7 0 7
209 10 ▶ * timestamp to derive - inventing one (render time, boot time)
Branch: 2 branches
BranchTrueFalseTotal
* timestamp to derive - inventing one (render time, boot time) 3 7 10
* timestamp to derive - inventing one (render time, boot time) 2 1 3
210 4 ▶ * would manufacture false freshness. */
Branch: 1 branch
BranchTrueFalseTotal
* would manufacture false freshness. */ 2 0 2
211 vp = hv_fetchs(oh, K_LAST_MODIFIED, 0);
212 # uncoverable condition right count:2 class:ignore_covered_err
213 # uncoverable condition right count:3 class:ignore_covered_err
214 8 ▶ if (vp && *vp && SvOK(*vp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (vp && *vp && SvOK(*vp)) { 7 1 8
215 7 HV *rec;
216 7 int is_cv = (SvROK(*vp) && SvTYPE(SvRV(*vp)) == SVt_PVCV); /* # uncoverable condition right count:2 class:ignore_covered_err */
217 7 if (!is_cv && SvTRUE(*vp))
218 7 croak("Punk: last_modified on %s %s takes a coderef "
219 7 "returning an epoch; there is no body form - a "
220 "rendered body has no timestamp to derive",
221 SvPV_nolen(method), SvPV_nolen(path));
222 if (is_cv) {
223 rec = newHV();
224 (void)hv_stores(rec, K_METHOD, newSVsv(method));
225 (void)hv_stores(rec, K_PATH, newSVsv(path));
226 (void)hv_stores(rec, K_LAST_MODIFIED, newSVsv(*vp));
227 av_push(app_av(aTHX_ h, K_LM_ROUTES),
228 410 newRV_noinc((SV *)rec));
229 410 ▶ }
Branch: 4 branches
BranchTrueFalseTotal
} 41 369 410
} 41 0 41
} 41 0 41
} 41 0 41
230 41 }
231 41 /* idempotent: honour an Idempotency-Key on this route, inert
232 41 * unless Punk::Plugin::Idempotency is registered - the `sitemap`
233 41 * and `etag` arrangement. Opt in per route because a key
234 * honoured on every POST means a cache write on every POST, and
235 * most POSTs do not need one. */
236 vp = hv_fetchs(oh, K_IDEMPOTENT, 0);
237 # uncoverable condition right count:2 class:ignore_covered_err
238 # uncoverable condition right count:3 class:ignore_covered_err
239 # uncoverable condition right count:4 class:ignore_covered_err
240 if (vp && *vp && SvOK(*vp) && SvTRUE(*vp)) {
241 410 HV *rec = newHV();
242 410 ▶ (void)hv_stores(rec, K_METHOD, newSVsv(method));
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_stores(rec, K_METHOD, newSVsv(method)); 95 315 410
(void)hv_stores(rec, K_METHOD, newSVsv(method)); 95 0 95
243 (void)hv_stores(rec, K_PATH, newSVsv(path));
244 95 av_push(app_av(aTHX_ h, K_IDEM_ROUTES),
245 87 newRV_noinc((SV *)rec));
246 87 }
247 87 /* name: this route's name, for $c->url_for and {% url.name %}.
248 87 * Recorded now, stamped onto the compiled record at to_app and
249 87 * resolved there into one name -> record index table, where a
250 * duplicate croaks naming both routes. What a name may be is
251 * checked HERE, so a bad one fails on the line that wrote it. */
252 402 vp = hv_fetchs(oh, K_NAME, 0);
253 402 ▶ if (vp && *vp) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (vp && *vp) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 10 392 402
if (vp && *vp) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 10 0 10
if (vp && *vp) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 10 0 10
254 10 HV *rec;
255 10 pk_url_check_name(aTHX_ *vp, "route", method, path);
256 10 rec = newHV();
257 10 (void)hv_stores(rec, K_METHOD, newSVsv(method));
258 10 (void)hv_stores(rec, K_PATH, newSVsv(path));
259 (void)hv_stores(rec, K_NAME, newSVsv(*vp));
260 av_push(app_av(aTHX_ h, K_NAMED_ROUTES),
261 newRV_noinc((SV *)rec));
262 }
263 vp = hv_fetchs(oh, K_VALIDATE, 0);
264 # uncoverable condition right count:2 class:ignore_covered_err
265 # uncoverable condition right count:3 class:ignore_covered_err
266 if (vp && *vp && SvOK(*vp)) {
267 HV *rec = newHV();
268 (void)hv_stores(rec, K_METHOD, newSVsv(method));
269 (void)hv_stores(rec, K_PATH, newSVsv(path));
270 (void)hv_stores(rec, K_VALIDATE, newSVsv(*vp));
271 402 av_push(app_av(aTHX_ h, K_VALIDATE_ROUTES),
272 402 ▶ newRV_noinc((SV *)rec));
Branch: 3 branches
BranchTrueFalseTotal
newRV_noinc((SV *)rec)); 28 374 402
newRV_noinc((SV *)rec)); 28 0 28
newRV_noinc((SV *)rec)); 28 0 28
273 28 }
274 45 ▶ /* rate_limit: this route's own budget, on the machinery the
Branch: 2 branches
BranchTrueFalseTotal
/* rate_limit: this route's own budget, on the machinery the 23 5 28
/* rate_limit: this route's own budget, on the machinery the 21 2 23
275 21 * `rate_limit` keyword already uses. The keyword is one policy
276 * for a path prefix, which is the right shape for "the whole
277 21 * API" and the wrong one for "/login gets five tries a minute" -
278 59 ▶ * the routes an attacker retries are exactly the ones that need
Branch: 1 branch
BranchTrueFalseTotal
* the routes an attacker retries are exactly the ones that need 42 17 59
279 42 ▶ * a budget of their own.
Branch: 1 branch
BranchTrueFalseTotal
* a budget of their own. 0 42 42
280 42 ▶ *
Branch: 1 branch
BranchTrueFalseTotal
* 2 40 42
281 4 ▶ * The spec is checked here so a typo fails on the line that
Branch: 1 branch
BranchTrueFalseTotal
* The spec is checked here so a typo fails on the line that 2 0 2
282 * wrote it; it is compiled into a guard at to_app, before the
283 * `validate` guard, so a refused request never parses a body. */
284 40 ▶ vp = hv_fetchs(oh, K_RATE_LIMIT, 0);
Branch: 2 branches
BranchTrueFalseTotal
vp = hv_fetchs(oh, K_RATE_LIMIT, 0); 23 17 40
vp = hv_fetchs(oh, K_RATE_LIMIT, 0); 13 10 23
285 13 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 6 7 13
# uncoverable condition right count:2 class:ignore_covered_err 2 4 6
286 4 ▶ # uncoverable condition right count:3 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:3 class:ignore_covered_err 2 0 2
287 if (vp && *vp && SvOK(*vp)) {
288 HV *spec = NULL;
289 if (SvROK(*vp) && SvTYPE(SvRV(*vp)) == SVt_PVHV) {
290 17 HV *in = (HV *)SvRV(*vp);
291 HE *rhe;
292 7 ▶ hv_iterinit(in);
Branch: 1 branch
BranchTrueFalseTotal
hv_iterinit(in); 2 5 7
293 6 ▶ while ((rhe = hv_iternext(in))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((rhe = hv_iternext(in))) { 2 0 2
294 STRLEN rkl; const char *rk = HePV(rhe, rkl);
295 if (strEQ(rk, "for"))
296 croak("Punk: rate_limit on %s %s takes no `for` "
297 5 ▶ "- the route is the scope",
Branch: 1 branch
BranchTrueFalseTotal
"- the route is the scope", 4 1 5
298 SvPV_nolen(method), SvPV_nolen(path));
299 if (!(strEQ(rk, "limit") || strEQ(rk, "window")
300 4 ▶ || strEQ(rk, "by") || strEQ(rk, "tag")))
Branch: 2 branches
BranchTrueFalseTotal
|| strEQ(rk, "by") || strEQ(rk, "tag"))) 2 2 4
|| strEQ(rk, "by") || strEQ(rk, "tag"))) 1 1 2
301 12 ▶ croak("Punk: unknown rate_limit option '%s' on "
Branch: 2 branches
BranchTrueFalseTotal
croak("Punk: unknown rate_limit option '%s' on " 3 0 3
croak("Punk: unknown rate_limit option '%s' on " 3 0 3
302 "%s %s", rk, SvPV_nolen(method),
303 SvPV_nolen(path));
304 }
305 1 spec = newHVhv(in);
306 1 }
307 else if (SvROK(*vp))
308 croak("Punk: rate_limit on %s %s takes a hashref of "
309 19 ▶ "options or a count, not a %s",
Branch: 1 branch
BranchTrueFalseTotal
"options or a count, not a %s", 18 1 19
310 18 SvPV_nolen(method), SvPV_nolen(path),
311 18 sv_reftype(SvRV(*vp), 0));
312 18 # uncoverable branch false class:ignore_covered_err
313 18 # uncoverable condition right count:1 class:ignore_covered_err
314 else if (SvTRUE(*vp)) {
315 18 /* the count shorthand: `rate_limit => 5` is five a
316 * minute, the window every other spelling defaults to */
317 # uncoverable branch false class:ignore_covered_err
318 # uncoverable condition right count:1 class:ignore_covered_err
319 # uncoverable condition right count:2 class:ignore_covered_err
320 61816 if (!looks_like_number(*vp) || SvIV(*vp) <= 0)
321 61816 croak("Punk: rate_limit on %s %s takes a positive "
322 61816 "count or a hashref of options, not '%s'",
323 61816 SvPV_nolen(method), SvPV_nolen(path),
324 123632 SvPV_nolen(*vp));
325 61816 ▶ spec = newHV(); /* # uncoverable statement class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
spec = newHV(); /* # uncoverable statement class:ignore_covered_err */ 185 61631 61816
326 61816 ▶ (void)hv_stores(spec, "limit", newSViv(SvIV(*vp))); /* # uncoverable statement class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(spec, "limit", newSViv(SvIV(*vp))); /* # uncoverable statement class:ignore_covered_err */ 61816 0 61816
327 61815 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 61815 0 61815
328 61815 /* rate_limit => 0 is what a route says by saying nothing */
329 # uncoverable branch false class:ignore_covered_err
330 # uncoverable condition right count:1 class:ignore_covered_err
331 if (spec) {
332 HV *rec = newHV();
333 (void)hv_stores(rec, K_METHOD, newSVsv(method));
334 (void)hv_stores(rec, K_PATH, newSVsv(path));
335 (void)hv_stores(rec, K_RATE_LIMIT,
336 newRV_noinc((SV *)spec));
337 av_push(app_av(aTHX_ h, K_RL_ROUTES),
338 newRV_noinc((SV *)rec));
339 }
340 }
341 }
342 argv[0] = sv_2mortal(newSVpvs(K_METHOD)); argv[1] = method;
343 argv[2] = sv_2mortal(newSVpvs(K_PATH)); argv[3] = path;
344 argv[4] = sv_2mortal(newSVpvs(K_TARGET)); argv[5] = target;
345 argv[6] = sv_2mortal(newSVpvs(K_GUARDS));
346 argv[7] = SvOK(guards) ? guards
347 : sv_2mortal(newRV_noinc((SV *)newAV()));
348 r = pcx_call_meth(aTHX_ router ? router : &PL_sv_undef, "add", argv, 8, 1); /* # uncoverable condition left count:1 class:ignore_covered_err */
349 # uncoverable branch false class:ignore_covered_err
350 # uncoverable condition right count:1 class:ignore_covered_err
351 if (r) SvREFCNT_dec(r);
352 RETVAL = newSVsv(self);
353 }
354 OUTPUT:
355 RETVAL
356  
357 # under($prefix, $guard?) -> a Punk::Router::Scope.
358 SV *
359 under(self, prefix, guard = &PL_sv_undef)
360 SV *self
361 SV *prefix
362 SV *guard
363 CODE:
364 {
365 67 AV *g = newAV();
366 SV *argv[6];
367 67 ▶ if (SvOK(guard)) av_push(g, newSVsv(guard));
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(guard)) av_push(g, newSVsv(guard)); 43 24 67
368 67 argv[0] = sv_2mortal(newSVpvs(K_APP)); argv[1] = self;
369 67 argv[2] = sv_2mortal(newSVpvs(K_PREFIX)); argv[3] = prefix;
370 67 argv[4] = sv_2mortal(newSVpvs(K_GUARDS));
371 67 argv[5] = sv_2mortal(newRV_noinc((SV *)g));
372 67 RETVAL = pcx_call_meth(aTHX_ sv_2mortal(newSVpvs(PK_SCOPE)),
373 "new", argv, 6, 1);
374 67 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 67 67
375 # uncoverable condition left count:1 class:ignore_covered_err
376 if (!RETVAL) RETVAL = &PL_sv_undef;
377 }
378 OUTPUT:
379 RETVAL
380  
381 # websocket($path, $target, $opts?, $guards?): a GET route plus recorded opts.
382 SV *
383 websocket(self, path, target, opts = &PL_sv_undef, guards = &PL_sv_undef)
384 SV *self
385 SV *path
386 SV *target
387 SV *opts
388 SV *guards
389 CODE:
390 {
391 103 HV *h = app_hv(aTHX_ self);
392 static const char *known[] =
393 { "protocols", "max_message_size", "write_buffer_limit", K_BLOCKING,
394 K_NAME, "origin" };
395 HV *oh;
396 SV *argv[8], *r; HE *he; HV *rec;
397 103 SV *origin = NULL;
398 103 pk_spec_split(aTHX_ "websocket", path, &target, &opts);
399 66 ▶ oh = (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
oh = (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 66 0 66
400 168 ▶ ? (HV *)SvRV(opts) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(opts) : NULL; 66 36 102
401 102 ▶ if (oh) {
Branch: 1 branch
BranchTrueFalseTotal
if (oh) { 66 36 102
402 66 hv_iterinit(oh);
403 133 ▶ while ((he = hv_iternext(oh))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(oh))) { 69 64 133
404 69 ▶ STRLEN kl; const char *k = HePV(he, kl); int ok = 0;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl; const char *k = HePV(he, kl); int ok = 0; 0 69 69
405 size_t i;
406 294 ▶ for (i = 0; i < sizeof known / sizeof *known; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sizeof known / sizeof *known; i++) 292 2 294
407 292 ▶ if (strEQ(k, known[i])) { ok = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, known[i])) { ok = 1; break; } 67 225 292
408 69 ▶ if (!ok) croak("Punk: unknown websocket option(s) %s", k);
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) croak("Punk: unknown websocket option(s) %s", k); 2 67 69
409 }
410 }
411 /* `origin` is normalised at boot into the one shape the handshake
412 * reads: a false scalar (checked off), a coderef, or an arrayref of
413 * lowercased allow entries. undef is refused rather than read as off,
414 * because the value usually arrives from configuration and a missing
415 * key must not quietly take the check away. */
416 {
417 100 ▶ SV **ov = oh ? hv_fetchs(oh, "origin", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **ov = oh ? hv_fetchs(oh, "origin", 0) : NULL; 64 36 100
418 100 ▶ if (ov && *ov) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (ov && *ov) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 13 87 100
if (ov && *ov) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 13 0 13
419 13 SV *v = *ov;
420 13 ▶ if (!SvOK(v))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(v)) 1 12 13
421 1 croak("Punk: websocket origin is undef - spell it 0 to "
422 "serve any origin, or name the ones you answer as");
423 12 ▶ else if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
else if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVCV) 3 9 12
else if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVCV) 1 2 3
424 1 origin = newSVsv(v);
425 11 ▶ else if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
else if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 9 11
else if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 0 2
426 2 origin = newRV_noinc(
427 (SV *)pk_host_allow_av(aTHX_ v, "websocket origin"));
428 9 ▶ else if (!SvTRUE(v))
Branch: 1 branch
BranchTrueFalseTotal
else if (!SvTRUE(v)) 7 2 9
429 7 origin = newSViv(0);
430 else {
431 2 AV *one = (AV *)sv_2mortal((SV *)newAV());
432 SV *wrap;
433 2 av_push(one, newSVsv(v));
434 2 wrap = sv_2mortal(newRV_inc((SV *)one));
435 2 origin = newRV_noinc(
436 (SV *)pk_host_allow_av(aTHX_ wrap, "websocket origin"));
437 }
438 }
439 }
440 /* $self->route('GET', $path, $target, $guards || []) */
441 /* `name` is a ROUTE option: forwarding it to route() means phase
442 * 0's recording, stamping and duplicate croak all run unchanged,
443 * and a named websocket route is a named GET route like any other. */
444 97 argv[0] = sv_2mortal(newSVpvs("GET")); argv[1] = path;
445 97 argv[2] = target;
446 97 ▶ argv[3] = SvOK(guards) ? guards : sv_2mortal(newRV_noinc((SV *)newAV()));
Branch: 1 branch
BranchTrueFalseTotal
argv[3] = SvOK(guards) ? guards : sv_2mortal(newRV_noinc((SV *)newAV())); 89 8 97
447 {
448 97 ▶ SV **nv = oh ? hv_fetchs(oh, K_NAME, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **nv = oh ? hv_fetchs(oh, K_NAME, 0) : NULL; 61 36 97
449 97 int nargs = 4;
450 97 ▶ if (nv && *nv) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (nv && *nv) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 93 97
if (nv && *nv) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 0 4
451 4 HV *ro = newHV();
452 4 pk_url_check_name(aTHX_ *nv, "websocket", argv[0], path);
453 3 (void)hv_stores(ro, K_NAME, newSVsv(*nv));
454 3 argv[4] = sv_2mortal(newRV_noinc((SV *)ro));
455 3 nargs = 5;
456 }
457 96 r = pcx_call_meth(aTHX_ self, "route", argv, nargs, 1);
458 }
459 96 ▶ # uncoverable branch false class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false class:ignore_covered_err 96 0 96
460 96 # uncoverable condition right count:1 class:ignore_covered_err
461 96 if (r) SvREFCNT_dec(r);
462 rec = newHV();
463 (void)hv_stores(rec, K_PATH, newSVsv(path));
464 { /* The options copy is what reaches Punk::WebSocket as its
465 * configuration, and it does not know `name` - so the key is
466 96 ▶ * dropped from the COPY, never from the caller's hashref, which
Branch: 1 branch
BranchTrueFalseTotal
* dropped from the COPY, never from the caller's hashref, which 60 36 96
467 96 * an application may be reusing between declarations. */
468 96 ▶ HV *copy = oh ? newHVhv(oh) : newHV();
Branch: 1 branch
BranchTrueFalseTotal
HV *copy = oh ? newHVhv(oh) : newHV(); 10 86 96
469 96 (void)hv_delete(copy, K_NAME, (I32)strlen(K_NAME), G_DISCARD);
470 if (origin) (void)hv_stores(copy, "origin", origin);
471 96 (void)hv_stores(rec, K_OPTS, newRV_noinc((SV *)copy));
472 96 }
473 av_push(app_av(aTHX_ h, K_WS_ROUTES), newRV_noinc((SV *)rec));
474 RETVAL = newSVsv(self);
475 }
476 OUTPUT:
477 RETVAL
478  
479 # sse($path, $target, $opts?, $guards?): a Server-Sent Events route - a GET
480 # route the dispatcher hands to Punk::SSE with the socket taken over. Chains.
481 SV *
482 sse(self, path, target, opts = &PL_sv_undef, guards = &PL_sv_undef)
483 SV *self
484 SV *path
485 SV *target
486 SV *opts
487 SV *guards
488 CODE:
489 {
490 38 HV *h = app_hv(aTHX_ self);
491 static const char *known[] =
492 { "heartbeat", "retry", "write_buffer_limit", K_BLOCKING,
493 K_NAME };
494 HV *oh;
495 SV *argv[8], *r; HE *he; HV *rec;
496 38 pk_spec_split(aTHX_ "sse", path, &target, &opts);
497 14 ▶ oh = (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
oh = (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 14 0 14
498 52 ▶ ? (HV *)SvRV(opts) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(opts) : NULL; 14 24 38
499 38 ▶ if (oh) {
Branch: 1 branch
BranchTrueFalseTotal
if (oh) { 14 24 38
500 14 hv_iterinit(oh);
501 28 ▶ while ((he = hv_iternext(oh))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(oh))) { 16 12 28
502 16 ▶ STRLEN kl; const char *k = HePV(he, kl); int ok = 0, i;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl; const char *k = HePV(he, kl); int ok = 0, i; 0 16 16
503 54 ▶ for (i = 0; i < 5; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 5; i++) 52 2 54
504 52 ▶ if (strEQ(k, known[i])) { ok = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, known[i])) { ok = 1; break; } 14 38 52
505 16 ▶ if (!ok) croak("Punk: unknown sse option(s) %s", k);
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) croak("Punk: unknown sse option(s) %s", k); 2 14 16
506 }
507 }
508 /* `name` is a ROUTE option: forwarding it to route() means phase
509 * 0's recording, stamping and duplicate croak all run unchanged,
510 * and a named sse route is a named GET route like any other. */
511 36 argv[0] = sv_2mortal(newSVpvs("GET")); argv[1] = path;
512 36 argv[2] = target;
513 36 ▶ argv[3] = SvOK(guards) ? guards : sv_2mortal(newRV_noinc((SV *)newAV())); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
argv[3] = SvOK(guards) ? guards : sv_2mortal(newRV_noinc((SV *)newAV())); /* # uncoverable condition right count:1 class:ignore_covered_err */ 36 0 36
514 {
515 36 ▶ SV **nv = oh ? hv_fetchs(oh, K_NAME, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **nv = oh ? hv_fetchs(oh, K_NAME, 0) : NULL; 12 24 36
516 36 int nargs = 4;
517 36 ▶ if (nv && *nv) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (nv && *nv) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 32 36
if (nv && *nv) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 0 4
518 4 HV *ro = newHV();
519 4 pk_url_check_name(aTHX_ *nv, "sse", argv[0], path);
520 3 (void)hv_stores(ro, K_NAME, newSVsv(*nv));
521 3 argv[4] = sv_2mortal(newRV_noinc((SV *)ro));
522 3 nargs = 5;
523 }
524 35 r = pcx_call_meth(aTHX_ self, "route", argv, nargs, 1);
525 }
526 35 ▶ # uncoverable branch false class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false class:ignore_covered_err 35 0 35
527 35 # uncoverable condition right count:1 class:ignore_covered_err
528 35 if (r) SvREFCNT_dec(r);
529 rec = newHV();
530 (void)hv_stores(rec, K_PATH, newSVsv(path));
531 { /* The options copy is what reaches Punk::SSE as its
532 * configuration, and it does not know `name` - so the key is
533 35 ▶ * dropped from the COPY, never from the caller's hashref, which
Branch: 1 branch
BranchTrueFalseTotal
* dropped from the COPY, never from the caller's hashref, which 11 24 35
534 35 * an application may be reusing between declarations. */
535 35 HV *copy = oh ? newHVhv(oh) : newHV();
536 (void)hv_delete(copy, K_NAME, (I32)strlen(K_NAME), G_DISCARD);
537 35 (void)hv_stores(rec, K_OPTS, newRV_noinc((SV *)copy));
538 35 }
539 av_push(app_av(aTHX_ h, K_SSE_ROUTES), newRV_noinc((SV *)rec));
540 RETVAL = newSVsv(self);
541 }
542 OUTPUT:
543 RETVAL
544  
545 # cors(origins => [...], credentials => 1, ...) or cors(\%opts): freeze the
546 # CORS policy on the app. Bare `cors` is '*' with no credentials - right for a
547 # public API, and safe, since browsers refuse '*' with credentials anyway.
548 # Chains.
549 SV *
550 cors(self, ...)
551 SV *self
552 CODE:
553 {
554 23 HV *h = app_hv(aTHX_ self);
555 23 HV *cfg = newHV();
556 int i;
557 24 ▶ if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 2 21 23
if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 1 1 2
if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 1 0 1
558 1 HE *e; HV *given = (HV *)SvRV(ST(1));
559 1 hv_iterinit(given);
560 2 ▶ while ((e = hv_iternext(given))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hv_iternext(given))) { 1 1 2
561 1 ▶ STRLEN kl; const char *k = HePV(e, kl);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl; const char *k = HePV(e, kl); 0 1 1
562 1 (void)hv_store(cfg, k, (I32)kl,
563 newSVsv(hv_iterval(given, e)), 0);
564 }
565 }
566 47 ▶ else for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
else for (i = 1; i + 1 < items; i += 2) { 25 22 47
567 25 STRLEN kl; const char *k = SvPV_const(ST(i), kl);
568 25 (void)hv_store(cfg, k, (I32)kl, newSVsv(ST(i + 1)), 0);
569 }
570 23 ▶ if (items == 2 && !SvROK(ST(1)) && !SvTRUE(ST(1))) { /* # uncoverable condition right count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (items == 2 && !SvROK(ST(1)) && !SvTRUE(ST(1))) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 2 21 23
if (items == 2 && !SvROK(ST(1)) && !SvTRUE(ST(1))) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 1 1 2
if (items == 2 && !SvROK(ST(1)) && !SvTRUE(ST(1))) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 1 0 1
571 1 SvREFCNT_dec((SV *)cfg);
572 1 (void)hv_delete(h, K_CORS, (I32)strlen(K_CORS), G_DISCARD);
573 }
574 else {
575 /* Allow-Origin: * with credentials is refused by every browser,
576 * and reflecting any origin with credentials is the classic
577 * misconfiguration. Naming origins is the only safe spelling. */
578 22 SV **cr = hv_fetchs(cfg, "credentials", 0);
579 22 SV **og = hv_fetchs(cfg, "origins", 0);
580 22 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 8 14 22
# uncoverable condition right count:2 class:ignore_covered_err 8 0 8
# uncoverable condition right count:2 class:ignore_covered_err 8 0 8
581 8 ▶ # uncoverable condition right count:3 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition right count:3 class:ignore_covered_err 6 2 8
# uncoverable condition right count:3 class:ignore_covered_err 6 0 6
# uncoverable condition right count:3 class:ignore_covered_err 6 0 6
582 8 ▶ if (cr && *cr && SvTRUE(*cr)) {
Branch: 2 branches
BranchTrueFalseTotal
if (cr && *cr && SvTRUE(*cr)) { 6 2 8
if (cr && *cr && SvTRUE(*cr)) { 3 3 6
583 3 # uncoverable condition right count:2 class:ignore_covered_err
584 3 ▶ # uncoverable condition right count:3 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:3 class:ignore_covered_err 2 1 3
# uncoverable condition right count:3 class:ignore_covered_err 2 0 2
585 int named = og && *og && SvOK(*og);
586 8 ▶ if (named && !SvROK(*og)) {
Branch: 1 branch
BranchTrueFalseTotal
if (named && !SvROK(*og)) { 4 4 8
587 4 STRLEN ol; const char *o = SvPV_const(*og, ol);
588 4 if (ol == 1 && *o == '*') named = 0; /* # uncoverable condition right count:2 class:ignore_covered_err */
589 }
590 if (!named) {
591 SvREFCNT_dec((SV *)cfg);
592 croak("Punk: `cors credentials => 1` needs explicit "
593 "origins - a browser refuses credentials with '*', "
594 18 "and reflecting any origin with them lets every "
595 "site read authenticated responses");
596 }
597 19 }
598 (void)hv_store(h, K_CORS, (I32)strlen(K_CORS),
599 newRV_noinc((SV *)cfg), 0);
600 }
601 RETVAL = newSVsv(self);
602 }
603 OUTPUT:
604 RETVAL
605  
606 # headers('Content-Security-Policy' => "default-src 'self'", ...) or
607 # headers(\%opts): freeze the security-header policy on the app. Bare
608 # `headers` is the safe default set; a header name with an undef value drops
609 # that default; every value else is a literal header string. Chains.
610 SV *
611 headers(self, ...)
612 SV *self
613 CODE:
614 {
615 16 HV *h = app_hv(aTHX_ self);
616 16 HV *cfg = newHV();
617 int i;
618 17 ▶ if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 3 13 16
if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 1 2 3
if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 1 0 1
619 1 HE *e; HV *given = (HV *)SvRV(ST(1));
620 1 hv_iterinit(given);
621 2 ▶ while ((e = hv_iternext(given))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hv_iternext(given))) { 1 1 2
622 1 ▶ STRLEN kl; const char *k = HePV(e, kl);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl; const char *k = HePV(e, kl); 0 1 1
623 1 (void)hv_store(cfg, k, (I32)kl,
624 newSVsv(hv_iterval(given, e)), 0);
625 }
626 }
627 25 ▶ else for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
else for (i = 1; i + 1 < items; i += 2) { 10 15 25
628 10 STRLEN kl; const char *k = SvPV_const(ST(i), kl);
629 10 (void)hv_store(cfg, k, (I32)kl, newSVsv(ST(i + 1)), 0);
630 }
631 16 ▶ if (items == 2 && !SvROK(ST(1)) && !SvTRUE(ST(1))) { /* # uncoverable condition right count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (items == 2 && !SvROK(ST(1)) && !SvTRUE(ST(1))) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 3 13 16
if (items == 2 && !SvROK(ST(1)) && !SvTRUE(ST(1))) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 2 1 3
if (items == 2 && !SvROK(ST(1)) && !SvTRUE(ST(1))) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 2 0 2
632 2 SvREFCNT_dec((SV *)cfg);
633 2 (void)hv_delete(h, K_HEADERS, (I32)strlen(K_HEADERS), G_DISCARD);
634 }
635 else {
636 /* a value must be a literal header string (or undef to drop a
637 * default) - a reference here is always a mistake */
638 HE *e;
639 14 hv_iterinit(cfg);
640 22 ▶ while ((e = hv_iternext(cfg))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hv_iternext(cfg))) { 11 11 22
641 11 ▶ STRLEN kl; const char *k = HePV(e, kl);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl; const char *k = HePV(e, kl); 0 11 11
642 11 SV *v = hv_iterval(cfg, e);
643 11 ▶ if (!kl || v == NULL) { /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!kl || v == NULL) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 10 1 11
if (!kl || v == NULL) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 10 10
644 1 SvREFCNT_dec((SV *)cfg);
645 1 croak("Punk: headers needs header names as keys");
646 }
647 10 ▶ if (SvROK(v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(v)) { 2 8 10
648 2 SV *msg = sv_2mortal(newSVpvf(
649 "Punk: headers value for '%.*s' must be a string "
650 "(or undef to drop a default), not a reference",
651 (int)kl, k));
652 2 SvREFCNT_dec((SV *)cfg);
653 2 croak("%s", SvPV_nolen(msg));
654 }
655 }
656 11 (void)hv_store(h, K_HEADERS, (I32)strlen(K_HEADERS),
657 newRV_noinc((SV *)cfg), 0);
658 }
659 13 RETVAL = newSVsv(self);
660 }
661 OUTPUT:
662 RETVAL
663  
664 # max_body($bytes): the app-wide ceiling on a request's declared
665 # CONTENT_LENGTH, overridable per route. This is POLICY, not memory
666 # protection - by the time Punk runs, the body is already resident in the
667 # server's buffer. What it buys is the parse, the guards, the handler, and
668 # an honest 413 instead of a mysterious success. The memory bound is the
669 # server's (Hyperman's own `max_body`). Chains.
670 SV *
671 max_body(self, bytes = &PL_sv_undef)
672 SV *self
673 SV *bytes
674 CODE:
675 {
676 5 HV *h = app_hv(aTHX_ self);
677 5 ▶ if (!SvOK(bytes))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(bytes)) 1 4 5
678 1 croak("Punk: max_body needs a byte count");
679 4 ▶ if (SvROK(bytes))
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(bytes)) 1 3 4
680 1 croak("Punk: max_body takes a byte count, not a reference");
681 {
682 3 IV n = SvIV(bytes);
683 3 ▶ if (n < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) 1 2 3
684 1 croak("Punk: max_body must not be negative");
685 2 (void)hv_store(h, K_MAX_BODY, (I32)strlen(K_MAX_BODY),
686 newSViv(n), 0);
687 }
688 2 RETVAL = newSVsv(self);
689 }
690 OUTPUT:
691 RETVAL
692  
693 # proxy() / proxy(trust => 1, ...) / proxy(\%opts): declare that this
694 # application sits behind a reverse proxy, so the real client can be
695 # recovered from the forwarded headers. Bare `proxy` means one hop, which is
696 # the common case. Unknown options croak at keyword time; the values are
697 # validated at to_app, where the CIDRs are parsed and 'all' is refused
698 # outside development. Declaring it twice croaks: a second policy silently
699 # replacing the first is how a staging config leaks into production. Chains.
700 SV *
701 proxy(self, ...)
702 SV *self
703 CODE:
704 {
705 26 HV *h = app_hv(aTHX_ self);
706 26 HV *cfg = newHV();
707 int i;
708 static const char *const PP_OPTS[] = {
709 "trust", "for_header", "proto_header", "host_header",
710 "port_header", NULL
711 };
712 27 ▶ if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 3 23 26
if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 1 2 3
if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 1 0 1
713 1 HE *e; HV *given = (HV *)SvRV(ST(1));
714 1 hv_iterinit(given);
715 2 ▶ while ((e = hv_iternext(given))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hv_iternext(given))) { 1 1 2
716 1 ▶ STRLEN kl; const char *k = HePV(e, kl);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl; const char *k = HePV(e, kl); 0 1 1
717 1 (void)hv_store(cfg, k, (I32)kl,
718 newSVsv(hv_iterval(given, e)), 0);
719 }
720 }
721 43 ▶ else for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
else for (i = 1; i + 1 < items; i += 2) { 18 25 43
722 18 STRLEN kl; const char *k = SvPV_const(ST(i), kl);
723 18 (void)hv_store(cfg, k, (I32)kl, newSVsv(ST(i + 1)), 0);
724 }
725  
726 26 ▶ if (items == 2 && !SvROK(ST(1)) && !SvTRUE(ST(1))) { /* # uncoverable condition right count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (items == 2 && !SvROK(ST(1)) && !SvTRUE(ST(1))) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 3 23 26
if (items == 2 && !SvROK(ST(1)) && !SvTRUE(ST(1))) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 2 1 3
if (items == 2 && !SvROK(ST(1)) && !SvTRUE(ST(1))) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 2 0 2
727 2 SvREFCNT_dec((SV *)cfg);
728 2 (void)hv_delete(h, K_PROXY, (I32)strlen(K_PROXY), G_DISCARD);
729 }
730 else {
731 HE *e;
732 24 ▶ if (hv_exists(h, K_PROXY, (I32)strlen(K_PROXY))) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(h, K_PROXY, (I32)strlen(K_PROXY))) { 2 22 24
733 2 SvREFCNT_dec((SV *)cfg);
734 2 croak("Punk: `proxy` is already declared - one trust policy "
735 "per application, or a second one silently replaces "
736 "the first");
737 }
738 22 hv_iterinit(cfg);
739 38 ▶ while ((e = hv_iternext(cfg))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hv_iternext(cfg))) { 17 21 38
740 17 ▶ STRLEN kl; const char *k = HePV(e, kl);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl; const char *k = HePV(e, kl); 0 17 17
741 17 int j, known = 0;
742 23 ▶ for (j = 0; PP_OPTS[j]; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; PP_OPTS[j]; j++) 22 1 23
743 22 ▶ if (kl == strlen(PP_OPTS[j]) && memEQ(k, PP_OPTS[j], kl)) /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (kl == strlen(PP_OPTS[j]) && memEQ(k, PP_OPTS[j], kl)) /* # uncoverable condition right count:2 class:ignore_covered_err */ 16 6 22
if (kl == strlen(PP_OPTS[j]) && memEQ(k, PP_OPTS[j], kl)) /* # uncoverable condition right count:2 class:ignore_covered_err */ 16 0 16
744 16 { known = 1; break; }
745 17 ▶ if (!known) {
Branch: 1 branch
BranchTrueFalseTotal
if (!known) { 1 16 17
746 1 SV *msg = sv_2mortal(newSVpvf(
747 "Punk: proxy: unknown option '%.*s' (known: trust, "
748 "for_header, proto_header, host_header, port_header)",
749 (int)kl, k));
750 1 SvREFCNT_dec((SV *)cfg);
751 1 croak("%s", SvPV_nolen(msg));
752 }
753 }
754 21 (void)hv_store(h, K_PROXY, (I32)strlen(K_PROXY),
755 newRV_noinc((SV *)cfg), 0);
756 }
757 23 RETVAL = newSVsv(self);
758 }
759 OUTPUT:
760 RETVAL
761  
762 # auth(model => 'User', ...) or auth(\%opts): freeze the authentication
763 # config on the app. Unknown keys croak at keyword time - a typo in an auth
764 # option must not become a silently-open door. `auth 0` turns it off. Chains.
765 SV *
766 auth(self, ...)
767 SV *self
768 CODE:
769 {
770 32 HV *h = app_hv(aTHX_ self);
771 32 HV *cfg = newHV();
772 32 HV *fields = newHV();
773 int i;
774 static const char *known[] =
775 { "model", "token_model", "session_key", "login_path",
776 "iterations", "roles", "rank", "fields", "sqitch" };
777 static const char *fknown[] =
778 { "id", "email", "password", "verified" };
779 32 (void)hv_stores(cfg, "session_key", newSVpvs("user_id"));
780 32 (void)hv_stores(cfg, "login_path", newSVpvs("/login"));
781 32 (void)hv_stores(fields, "id", newSVpvs("id"));
782 32 (void)hv_stores(fields, "email", newSVpvs("email"));
783 32 (void)hv_stores(fields, "password", newSVpvs("password_hash"));
784 32 (void)hv_stores(fields, "verified", newSVpvs("verified"));
785 32 (void)hv_stores(cfg, "fields", newRV_noinc((SV *)fields));
786 33 ▶ if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 2 30 32
if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 1 1 2
if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 1 0 1
787 1 HE *e; HV *given = (HV *)SvRV(ST(1));
788 1 hv_iterinit(given);
789 3 ▶ while ((e = hv_iternext(given))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hv_iternext(given))) { 2 1 3
790 2 ▶ STRLEN kl; const char *k = HePV(e, kl);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl; const char *k = HePV(e, kl); 0 2 2
791 2 (void)hv_store(cfg, k, (I32)kl,
792 newSVsv(hv_iterval(given, e)), 0);
793 }
794 }
795 73 ▶ else for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
else for (i = 1; i + 1 < items; i += 2) { 42 31 73
796 42 STRLEN kl; const char *k = SvPV_const(ST(i), kl);
797 42 (void)hv_store(cfg, k, (I32)kl, newSVsv(ST(i + 1)), 0);
798 }
799 32 ▶ if (items == 2 && !SvROK(ST(1)) && !SvTRUE(ST(1))) { /* # uncoverable condition right count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (items == 2 && !SvROK(ST(1)) && !SvTRUE(ST(1))) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 2 30 32
if (items == 2 && !SvROK(ST(1)) && !SvTRUE(ST(1))) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 1 1 2
if (items == 2 && !SvROK(ST(1)) && !SvTRUE(ST(1))) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 1 0 1
800 1 SvREFCNT_dec((SV *)cfg);
801 1 (void)hv_delete(h, K_AUTH, (I32)strlen(K_AUTH), G_DISCARD);
802 }
803 else {
804 HE *e;
805 31 hv_iterinit(cfg);
806 158 ▶ while ((e = hv_iternext(cfg))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hv_iternext(cfg))) { 129 29 158
807 129 ▶ STRLEN kl; const char *k = HePV(e, kl);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl; const char *k = HePV(e, kl); 0 129 129
808 129 int ok = 0, j;
809 586 ▶ for (j = 0; j < 9; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < 9; j++) 584 2 586
810 584 ▶ if (strEQ(k, known[j])) { ok = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, known[j])) { ok = 1; break; } 127 457 584
811 129 ▶ if (!ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { 2 127 129
812 2 SV *msg = sv_2mortal(newSVpvf(
813 "Punk: unknown auth option '%.*s'", (int)kl, k));
814 2 SvREFCNT_dec((SV *)cfg);
815 2 croak("%s", SvPV_nolen(msg));
816 }
817 }
818 { /* a fields hashref merges over the defaults */
819 29 SV **f = hv_fetchs(cfg, "fields", 0);
820 29 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 29 0 29
# uncoverable condition right count:1 class:ignore_covered_err 29 0 29
# uncoverable condition right count:1 class:ignore_covered_err 6 23 29
821 6 # uncoverable condition right count:2 class:ignore_covered_err
822 if (f && *f && SvRV(*f) != (SV *)fields) {
823 HV *merged = newHV(), *given;
824 6 ▶ HE *fe;
Branch: 2 branches
BranchTrueFalseTotal
HE *fe; 5 1 6
HE *fe; 0 5 5
825 1 int j;
826 1 if (!(SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVHV)) { /* # uncoverable condition left count:2 class:ignore_covered_err */
827 SvREFCNT_dec((SV *)cfg);
828 5 croak("Punk: auth fields must be a hashref");
829 5 }
830 5 given = (HV *)SvRV(*f);
831 5 (void)hv_stores(merged, "id", newSVpvs("id"));
832 5 (void)hv_stores(merged, "email", newSVpvs("email"));
833 5 (void)hv_stores(merged, "password", newSVpvs("password_hash"));
834 8 ▶ (void)hv_stores(merged, "verified", newSVpvs("verified"));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(merged, "verified", newSVpvs("verified")); 5 3 8
835 5 ▶ hv_iterinit(given);
Branch: 1 branch
BranchTrueFalseTotal
hv_iterinit(given); 0 5 5
836 5 while ((fe = hv_iternext(given))) {
837 16 ▶ STRLEN kl; const char *k = HePV(fe, kl);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl; const char *k = HePV(fe, kl); 14 2 16
838 14 ▶ int ok = 0;
Branch: 1 branch
BranchTrueFalseTotal
int ok = 0; 3 11 14
839 5 ▶ for (j = 0; j < 4; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < 4; j++) 2 3 5
840 2 if (strEQ(k, fknown[j])) { ok = 1; break; }
841 if (!ok) {
842 SV *msg = sv_2mortal(newSVpvf(
843 2 "Punk: unknown auth field '%.*s'",
844 2 (int)kl, k));
845 2 SvREFCNT_dec((SV *)merged);
846 SvREFCNT_dec((SV *)cfg);
847 3 croak("%s", SvPV_nolen(msg));
848 }
849 (void)hv_store(merged, k, (I32)kl,
850 3 newSVsv(hv_iterval(given, fe)), 0);
851 }
852 (void)hv_stores(cfg, "fields",
853 newRV_noinc((SV *)merged));
854 }
855 26 }
856 26 ▶ {
Branch: 4 branches
BranchTrueFalseTotal
{ 2 24 26
{ 2 0 2
{ 2 0 2
{ 1 1 2
857 1 SV **it = hv_fetchs(cfg, "iterations", 0);
858 1 # uncoverable condition right count:2 class:ignore_covered_err
859 # uncoverable condition right count:3 class:ignore_covered_err
860 25 ▶ if (it && *it && SvOK(*it) && SvIV(*it) <= 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (it && *it && SvOK(*it) && SvIV(*it) <= 0) { 1 24 25
if (it && *it && SvOK(*it) && SvIV(*it) <= 0) { 1 0 1
if (it && *it && SvOK(*it) && SvIV(*it) <= 0) { 1 0 1
861 1 ▶ SvREFCNT_dec((SV *)cfg);
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_dec((SV *)cfg); 0 1 1
862 0 ▶ croak("Punk: auth iterations must be positive");
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk: auth iterations must be positive"); 0 0 0
863 1 }
864 1 # uncoverable condition right count:2 class:ignore_covered_err
865 # uncoverable condition right count:3 class:ignore_covered_err
866 # uncoverable condition right count:4 class:ignore_covered_err
867 # uncoverable condition left count:5 class:ignore_covered_err
868 # uncoverable condition right count:5 class:ignore_covered_err
869 if (it && *it && SvOK(*it)
870 && (SvIV(*it) < PWD_MIN_ITERATIONS
871 || SvIV(*it) > PWD_MAX_ITERATIONS)) { /* # uncoverable statement class:ignore_covered_err */
872 SvREFCNT_dec((SV *)cfg);
873 croak("Punk: auth iterations must be between %d and %d",
874 PWD_MIN_ITERATIONS, PWD_MAX_ITERATIONS);
875 }
876 24 }
877 24 ▶ /* sqitch => 1: the schema the defaults expect, shipped as the
Branch: 3 branches
BranchTrueFalseTotal
/* sqitch => 1: the schema the defaults expect, shipped as the 2 22 24
/* sqitch => 1: the schema the defaults expect, shipped as the 2 0 2
/* sqitch => 1: the schema the defaults expect, shipped as the 2 0 2
878 * Sqitch project `punk_auth` under lib/Punk/Auth/sqitch and
879 2 * registered with Punk-Sqitch, which deploys it before the
880 2 ▶ * application's own. Opt-in because `fields` exists precisely
Branch: 1 branch
BranchTrueFalseTotal
* application's own. Opt-in because `fields` exists precisely 1 1 2
881 1 * for schemas with other spellings, and the project creates
882 2 ▶ * the defaults. Asking for it without Punk-Sqitch installed is
Branch: 1 branch
BranchTrueFalseTotal
* the defaults. Asking for it without Punk-Sqitch installed is 1 0 1
883 * an error, not a silence. */
884 {
885 SV **sq = hv_fetchs(cfg, "sqitch", 0);
886 1 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 1 0 1
# uncoverable condition right count:2 class:ignore_covered_err 1 0 1
# uncoverable condition right count:2 class:ignore_covered_err 0 1 1
887 0 # uncoverable condition right count:3 class:ignore_covered_err
888 0 if (sq && *sq && SvTRUE(*sq)) {
889 SV *dir, *argv[4], *r;
890 SV **punkpm = hv_fetchs(GvHV(PL_incgv), "Punk.pm", 0);
891 if (!pk_require_once(aTHX_ "Punk::Plugin::Sqitch", FALSE)) { /* # uncoverable branch true class:ignore_covered_err */
892 1 SvREFCNT_dec((SV *)cfg); /* # uncoverable statement class:ignore_covered_err */
893 croak("Punk: auth sqitch => 1 needs Punk-Sqitch " /* # uncoverable statement class:ignore_covered_err */
894 1 "(Punk::Plugin::Sqitch) installed: %s",
895 1 ▶ SvPV_nolen(ERRSV)); /* # uncoverable statement class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
SvPV_nolen(ERRSV)); /* # uncoverable statement class:ignore_covered_err */ 1 0 1
SvPV_nolen(ERRSV)); /* # uncoverable statement class:ignore_covered_err */ 1 0 1
896 1 }
897 # uncoverable branch true
898 1 # uncoverable condition left count:1 class:ignore_covered_err
899 1 # uncoverable condition left count:2 class:ignore_covered_err
900 1 # uncoverable condition left count:3 class:ignore_covered_err
901 1 if (!(punkpm && *punkpm && SvOK(*punkpm))) {
902 1 SvREFCNT_dec((SV *)cfg); /* # uncoverable statement */
903 croak("Punk: cannot find Punk.pm in %%INC to locate " /* # uncoverable statement */
904 1 "the punk_auth Sqitch project");
905 1 }
906 1 /* .../Punk.pm -> .../Punk/Auth/sqitch */
907 1 dir = sv_2mortal(newSVsv(*punkpm));
908 {
909 STRLEN dl; const char *dp = SvPV_const(dir, dl);
910 1 # uncoverable branch false
911 1 # uncoverable condition right count:1 class:ignore_covered_err
912 1 # uncoverable condition right count:2 class:ignore_covered_err
913 1 if (dl >= 3 && memEQ(dp + dl - 3, ".pm", 3))
914 SvCUR_set(dir, dl - 3);
915 }
916 sv_catpvs(dir, "/Auth/sqitch");
917 argv[0] = self;
918 1 ▶ argv[1] = sv_2mortal(newSVpvs("punk_auth"));
Branch: 1 branch
BranchTrueFalseTotal
argv[1] = sv_2mortal(newSVpvs("punk_auth")); 1 0 1
919 1 argv[2] = dir;
920 argv[3] = sv_2mortal(newSVpvs("engines"));
921 {
922 23 AV *eng = newAV();
923 av_push(eng, newSVpvs("sqlite"));
924 av_push(eng, newSVpvs("pg"));
925 24 av_push(eng, newSVpvs("mysql"));
926 {
927 SV *argv5[5];
928 argv5[0] = argv[0]; argv5[1] = argv[1];
929 argv5[2] = argv[2]; argv5[3] = argv[3];
930 argv5[4] = sv_2mortal(newRV_noinc((SV *)eng));
931 r = pcx_call_meth(aTHX_
932 sv_2mortal(newSVpvs("Punk::Plugin::Sqitch")),
933 "project", argv5, 5, 1);
934 }
935 }
936 # uncoverable branch false
937 # uncoverable condition right count:1 class:ignore_covered_err
938 if (r) SvREFCNT_dec(r);
939 (void)hv_delete(cfg, "sqitch", 6, G_DISCARD);
940 }
941 }
942 (void)hv_store(h, K_AUTH, (I32)strlen(K_AUTH),
943 newRV_noinc((SV *)cfg), 0);
944 }
945 RETVAL = newSVsv(self);
946 }
947 OUTPUT:
948 RETVAL
949  
950 # auth_guard() / auth_guard(role => 'admin', verified => 1, on_denied =>
951 # '404'|'403'|coderef): a guard coderef for `under`. The bare form runs
952 # entirely in C. Croaks on unknown options.
953 SV *
954 auth_guard(self, ...)
955 SV *self
956 CODE:
957 {
958 23 HV *opts = newHV();
959 AV *cap;
960 int i;
961 static const char *known[] = { "role", "verified", "on_denied" };
962 24 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 1 22 23
# uncoverable condition right count:2 class:ignore_covered_err 1 0 1
# uncoverable condition right count:2 class:ignore_covered_err 1 0 1
963 1 # uncoverable condition right count:3 class:ignore_covered_err
964 1 if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) {
965 2 ▶ HE *e; HV *given = (HV *)SvRV(ST(1));
Branch: 1 branch
BranchTrueFalseTotal
HE *e; HV *given = (HV *)SvRV(ST(1)); 1 1 2
966 1 ▶ hv_iterinit(given);
Branch: 1 branch
BranchTrueFalseTotal
hv_iterinit(given); 0 1 1
967 1 while ((e = hv_iternext(given))) {
968 STRLEN kl; const char *k = HePV(e, kl);
969 (void)hv_store(opts, k, (I32)kl,
970 newSVsv(hv_iterval(given, e)), 0);
971 41 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 19 22 41
972 19 }
973 19 else for (i = 1; i + 1 < items; i += 2) {
974 STRLEN kl; const char *k = SvPV_const(ST(i), kl);
975 (void)hv_store(opts, k, (I32)kl, newSVsv(ST(i + 1)), 0);
976 }
977 23 {
978 41 ▶ HE *e;
Branch: 1 branch
BranchTrueFalseTotal
HE *e; 20 21 41
979 20 ▶ hv_iterinit(opts);
Branch: 1 branch
BranchTrueFalseTotal
hv_iterinit(opts); 0 20 20
980 20 while ((e = hv_iternext(opts))) {
981 43 ▶ STRLEN kl; const char *k = HePV(e, kl);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl; const char *k = HePV(e, kl); 41 2 43
982 41 ▶ int ok = 0, j;
Branch: 1 branch
BranchTrueFalseTotal
int ok = 0, j; 18 23 41
983 20 ▶ for (j = 0; j < 3; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < 3; j++) 2 18 20
984 2 if (strEQ(k, known[j])) { ok = 1; break; }
985 if (!ok) {
986 SV *msg = sv_2mortal(newSVpvf(
987 2 "Punk: unknown auth_guard option '%.*s'",
988 2 (int)kl, k));
989 SvREFCNT_dec((SV *)opts);
990 croak("%s", SvPV_nolen(msg));
991 }
992 }
993 21 { /* on_denied is '403', '404' or a coderef - anything else is
994 21 ▶ * a typo croaking now, not a request-time surprise */
Branch: 3 branches
BranchTrueFalseTotal
* a typo croaking now, not a request-time surprise */ 8 13 21
* a typo croaking now, not a request-time surprise */ 8 0 8
* a typo croaking now, not a request-time surprise */ 8 0 8
995 16 SV **od = hv_fetchs(opts, "on_denied", 0);
996 8 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 3 5 8
# uncoverable condition right count:2 class:ignore_covered_err 2 1 3
997 8 ▶ # uncoverable condition right count:3 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:3 class:ignore_covered_err 6 2 8
# uncoverable condition right count:3 class:ignore_covered_err 5 1 6
998 5 if (od && *od && SvOK(*od)) {
999 5 int fine = SvROK(*od)
1000 5 ▶ && SvTYPE(SvRV(*od)) == SVt_PVCV;
Branch: 3 branches
BranchTrueFalseTotal
&& SvTYPE(SvRV(*od)) == SVt_PVCV; 4 1 5
&& SvTYPE(SvRV(*od)) == SVt_PVCV; 4 0 4
&& SvTYPE(SvRV(*od)) == SVt_PVCV; 3 1 4
1001 if (!fine && !SvROK(*od)) {
1002 8 ▶ STRLEN ol; const char *o = SvPV_const(*od, ol);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN ol; const char *o = SvPV_const(*od, ol); 3 5 8
1003 3 fine = ol == 3
1004 3 && (memEQ(o, "403", 3) || memEQ(o, "404", 3)); /* # uncoverable condition left count:2 class:ignore_covered_err */
1005 }
1006 if (!fine) {
1007 SvREFCNT_dec((SV *)opts);
1008 croak("Punk: auth_guard on_denied takes '403', "
1009 "'404' or a coderef");
1010 18 }
1011 18 }
1012 18 }
1013 18 }
1014 cap = newAV();
1015 av_push(cap, newSVsv(self));
1016 av_push(cap, newRV_noinc((SV *)opts));
1017 RETVAL = punk_closure(aTHX_ pauth_guard_cb, cap);
1018 }
1019 OUTPUT:
1020 RETVAL
1021  
1022 # headers_scoped($prefix, \%pairs): the scope form of `headers`, reached via
1023 # $scope->headers(...) rather than a DSL keyword. Recorded with the scope's
1024 # accumulated prefix; to_app freezes the records longest-prefix-first, and
1025 # the dispatcher applies them ahead of the application-wide policy for
1026 # requests under the prefix. Chains.
1027 SV *
1028 headers_scoped(self, prefix, pairs)
1029 SV *self
1030 SV *prefix
1031 SV *pairs
1032 CODE:
1033 {
1034 4 HV *h = app_hv(aTHX_ self);
1035 4 HV *cfg = newHV();
1036 HV *rec;
1037 HE *e; HV *given;
1038 4 ▶ if (!(SvROK(pairs) && SvTYPE(SvRV(pairs)) == SVt_PVHV)) { /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(pairs) && SvTYPE(SvRV(pairs)) == SVt_PVHV)) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 3 1 4
if (!(SvROK(pairs) && SvTYPE(SvRV(pairs)) == SVt_PVHV)) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 3 3
1039 1 SvREFCNT_dec((SV *)cfg);
1040 1 croak("Punk: headers_scoped takes a prefix and a hashref");
1041 }
1042 3 given = (HV *)SvRV(pairs);
1043 3 hv_iterinit(given);
1044 8 ▶ while ((e = hv_iternext(given))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hv_iternext(given))) { 5 3 8
1045 5 ▶ STRLEN kl; const char *k = HePV(e, kl);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl; const char *k = HePV(e, kl); 0 5 5
1046 5 SV *v = hv_iterval(given, e);
1047 5 ▶ # uncoverable branch true
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch true 5 0 5
# uncoverable branch true 0 5 5
1048 0 # uncoverable condition left count:1 class:ignore_covered_err
1049 0 # uncoverable condition left count:2 class:ignore_covered_err
1050 if (!kl || v == NULL) {
1051 5 ▶ SvREFCNT_dec((SV *)cfg); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_dec((SV *)cfg); /* # uncoverable statement */ 0 5 5
1052 0 croak("Punk: headers needs header names as keys"); /* # uncoverable statement */
1053 }
1054 # uncoverable branch true
1055 # uncoverable condition left count:1 class:ignore_covered_err
1056 0 if (SvROK(v)) {
1057 0 SV *msg = sv_2mortal(newSVpvf( /* # uncoverable statement */
1058 "Punk: headers value for '%.*s' must be a string "
1059 5 "(or undef to drop it here), not a reference",
1060 (int)kl, k));
1061 3 SvREFCNT_dec((SV *)cfg); /* # uncoverable statement */
1062 3 ▶ croak("%s", SvPV_nolen(msg)); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
croak("%s", SvPV_nolen(msg)); /* # uncoverable statement */ 3 0 3
1063 }
1064 3 (void)hv_store(cfg, k, (I32)kl, newSVsv(v), 0);
1065 3 }
1066 3 rec = newHV();
1067 (void)hv_stores(rec, K_PREFIX,
1068 SvOK(prefix) ? newSVsv(prefix) : newSVpvs(""));
1069 (void)hv_stores(rec, K_HEADERS, newRV_noinc((SV *)cfg));
1070 av_push(app_av(aTHX_ h, K_HEADERS_SCOPED), newRV_noinc((SV *)rec));
1071 RETVAL = newSVsv(self);
1072 }
1073 OUTPUT:
1074 RETVAL
1075  
1076 # csrf(field => '_csrf', header => ..., keep => 1, ...) or csrf(\%opts):
1077 # freeze the CSRF config on the app. The compiler installs the check and the
1078 # mirror write-back, and croaks if there is no session for the token to live
1079 # in. Chains.
1080 SV *
1081 csrf(self, ...)
1082 SV *self
1083 CODE:
1084 {
1085 28 HV *h = app_hv(aTHX_ self);
1086 28 HV *cfg = newHV();
1087 int i;
1088 28 (void)hv_stores(cfg, "field", newSVpvs("_csrf"));
1089 28 (void)hv_stores(cfg, "header", newSVpvs("X-CSRF-Token"));
1090 28 (void)hv_stores(cfg, "cookie", newSVpvs("csrf"));
1091 28 (void)hv_stores(cfg, "keep", newSViv(1));
1092 28 (void)hv_stores(cfg, "max_body", newSViv(65536));
1093 29 ▶ if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 2 26 28
if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 1 1 2
if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 1 0 1
1094 1 HE *e; HV *given = (HV *)SvRV(ST(1));
1095 1 hv_iterinit(given);
1096 2 ▶ while ((e = hv_iternext(given))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hv_iternext(given))) { 1 1 2
1097 1 ▶ STRLEN kl; const char *k = HePV(e, kl);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl; const char *k = HePV(e, kl); 0 1 1
1098 1 (void)hv_store(cfg, k, (I32)kl,
1099 newSVsv(hv_iterval(given, e)), 0);
1100 }
1101 }
1102 40 ▶ else for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
else for (i = 1; i + 1 < items; i += 2) { 13 27 40
1103 13 STRLEN kl; const char *k = SvPV_const(ST(i), kl);
1104 13 (void)hv_store(cfg, k, (I32)kl, newSVsv(ST(i + 1)), 0);
1105 }
1106 /* `csrf 0` turns it back off */
1107 28 ▶ if (items == 2 && !SvROK(ST(1)) && !SvTRUE(ST(1))) { /* # uncoverable condition right count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (items == 2 && !SvROK(ST(1)) && !SvTRUE(ST(1))) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 2 26 28
if (items == 2 && !SvROK(ST(1)) && !SvTRUE(ST(1))) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 1 1 2
if (items == 2 && !SvROK(ST(1)) && !SvTRUE(ST(1))) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 1 0 1
1108 1 SvREFCNT_dec((SV *)cfg);
1109 1 (void)hv_delete(h, K_CSRF, (I32)strlen(K_CSRF), G_DISCARD);
1110 }
1111 else {
1112 /* The mirror cookie is written where a croak is trapped, so a
1113 * name the cookie builder would refuse is refused here; and the
1114 * header name has a fixed-size env key, so a long one is refused
1115 * rather than silently matching nothing. */
1116 27 SV **ck = hv_fetchs(cfg, "cookie", 0);
1117 27 SV **hd = hv_fetchs(cfg, "header", 0);
1118 27 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 4 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 27 0 27
# uncoverable condition right count:1 class:ignore_covered_err 27 0 27
# uncoverable condition right count:1 class:ignore_covered_err 27 0 27
# uncoverable condition right count:1 class:ignore_covered_err 2 25 27
1119 2 # uncoverable condition right count:2 class:ignore_covered_err
1120 2 # uncoverable condition right count:3 class:ignore_covered_err
1121 if (ck && *ck && SvOK(*ck) && !pk_cookie_safe(aTHX_ *ck)) {
1122 SvREFCNT_dec((SV *)cfg);
1123 25 ▶ croak("Punk: csrf `cookie` may not contain ';' or a control "
Branch: 4 branches
BranchTrueFalseTotal
croak("Punk: csrf `cookie` may not contain ';' or a control " 25 0 25
croak("Punk: csrf `cookie` may not contain ';' or a control " 25 0 25
croak("Punk: csrf `cookie` may not contain ';' or a control " 25 0 25
croak("Punk: csrf `cookie` may not contain ';' or a control " 2 23 25
1124 2 "character");
1125 2 }
1126 # uncoverable condition right count:1 class:ignore_covered_err
1127 23 # uncoverable condition right count:2 class:ignore_covered_err
1128 # uncoverable condition right count:3 class:ignore_covered_err
1129 if (hd && *hd && SvOK(*hd) && SvCUR(*hd) > 120) {
1130 24 SvREFCNT_dec((SV *)cfg);
1131 croak("Punk: csrf `header` name is longer than 120 bytes");
1132 }
1133 (void)hv_store(h, K_CSRF, (I32)strlen(K_CSRF),
1134 newRV_noinc((SV *)cfg), 0);
1135 }
1136 RETVAL = newSVsv(self);
1137 }
1138 OUTPUT:
1139 RETVAL
1140  
1141 # ua(timeout => 10, ...) freezes the default agent's options; ua(name => \%opts)
1142 # freezes a named one, the same shape `database` uses for a second database.
1143 # Every key is passed to Fetch->new as given, so this is Fetch's own
1144 # constructor surface and not a second vocabulary; the loop is supplied by
1145 # punk_ua.h at build time. Purely declarative - no agent is built here, because
1146 # one built before the fork would put every worker on the same sockets. Chains.
1147 SV *
1148 ua(self, ...)
1149 SV *self
1150 CODE:
1151 {
1152 17 HV *h = app_hv(aTHX_ self);
1153 17 HV *uas = app_hash(aTHX_ h, K_UA);
1154 22 ▶ if (items == 3 && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (items == 3 && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 11 6 17
if (items == 3 && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 5 6 11
if (items == 3 && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 5 0 5
1155 5 STRLEN nl; const char *n = SvPV_const(ST(1), nl);
1156 5 (void)hv_store(uas, n, (I32)nl,
1157 newRV_noinc((SV *)newHVhv((HV *)SvRV(ST(2)))), 0);
1158 }
1159 12 ▶ else if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
else if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 2 10 12
else if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 1 1 2
else if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 1 0 1
1160 1 (void)hv_stores(uas, K_DEFAULT,
1161 newRV_noinc((SV *)newHVhv((HV *)SvRV(ST(1)))));
1162 }
1163 else {
1164 11 HV *opts = newHV(); int i;
1165 11 ▶ if (!(items % 2))
Branch: 1 branch
BranchTrueFalseTotal
if (!(items % 2)) 2 9 11
1166 2 croak("Punk: ua takes a hashref, a list of pairs, or "
1167 "a name and a hashref");
1168 21 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 12 9 21
1169 12 STRLEN kl; const char *k = SvPV_const(ST(i), kl);
1170 12 (void)hv_store(opts, k, (I32)kl, newSVsv(ST(i + 1)), 0);
1171 }
1172 9 (void)hv_stores(uas, K_DEFAULT, newRV_noinc((SV *)opts));
1173 }
1174 15 RETVAL = newSVsv(self);
1175 }
1176 OUTPUT:
1177 RETVAL
1178  
1179 # session(secret => ..., expires => '7d', ...) or session(\%opts): freeze the
1180 # session config on the app (the compiler installs the write-back hook). Chains.
1181 SV *
1182 session(self, ...)
1183 SV *self
1184 CODE:
1185 {
1186 144 HV *h = app_hv(aTHX_ self);
1187 144 HV *cfg = newHV();
1188 144 HV *given = NULL;
1189 int i;
1190 144 (void)hv_stores(cfg, "cookie", newSVpvs("punk.sid"));
1191 144 (void)hv_stores(cfg, "path", newSVpvs("/"));
1192 144 (void)hv_stores(cfg, "httponly", newSViv(1));
1193 144 (void)hv_stores(cfg, "samesite", newSVpvs("Lax"));
1194 148 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 4 140 144
# uncoverable condition right count:2 class:ignore_covered_err 4 0 4
# uncoverable condition right count:2 class:ignore_covered_err 4 0 4
1195 4 # uncoverable condition right count:3 class:ignore_covered_err
1196 4 if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) {
1197 15 ▶ HE *e; given = (HV *)SvRV(ST(1));
Branch: 1 branch
BranchTrueFalseTotal
HE *e; given = (HV *)SvRV(ST(1)); 11 4 15
1198 11 ▶ hv_iterinit(given);
Branch: 1 branch
BranchTrueFalseTotal
hv_iterinit(given); 0 11 11
1199 11 while ((e = hv_iternext(given))) {
1200 11 ▶ STRLEN kl; const char *k = HePV(e, kl); SV *v = hv_iterval(given, e);
Branch: 2 branches
BranchTrueFalseTotal
STRLEN kl; const char *k = HePV(e, kl); SV *v = hv_iterval(given, e); 2 9 11
STRLEN kl; const char *k = HePV(e, kl); SV *v = hv_iterval(given, e); 2 0 2
1201 2 ps_check_key(aTHX_ k, kl, cfg);
1202 if (kl == 7 && memEQ(k, "expires", 7)) /* # uncoverable condition right count:2 class:ignore_covered_err */
1203 9 (void)hv_stores(cfg, "max_age", newSViv(ps_parse_duration(aTHX_ v)));
1204 else
1205 (void)hv_store(cfg, k, (I32)kl, newSVsv(v), 0);
1206 382 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 244 138 382
1207 244 }
1208 244 else for (i = 1; i + 1 < items; i += 2) {
1209 242 ▶ STRLEN kl; const char *k = SvPV_const(ST(i), kl);
Branch: 2 branches
BranchTrueFalseTotal
STRLEN kl; const char *k = SvPV_const(ST(i), kl); 38 204 242
STRLEN kl; const char *k = SvPV_const(ST(i), kl); 37 1 38
1210 37 ps_check_key(aTHX_ k, kl, cfg);
1211 if (kl == 7 && memEQ(k, "expires", 7))
1212 (void)hv_stores(cfg, "max_age",
1213 205 newSViv(ps_parse_duration(aTHX_ ST(i + 1))));
1214 else
1215 (void)hv_store(cfg, k, (I32)kl, newSVsv(ST(i + 1)), 0);
1216 }
1217 /* A session cookie is only as good as the key it is signed with, and
1218 * an absent one used to mean an empty HMAC key: well-formed cookies
1219 * that round-tripped perfectly and that anyone who knew the format
1220 * could mint offline, carrying any user id or role the session holds.
1221 * Nothing about it looked wrong at runtime, which is what made it
1222 * worth failing on. So the keyword refuses to freeze a config it
1223 * cannot sign with, at boot, the way Punk::Session::_seal already
1224 142 * refused to seal one. */
1225 142 ▶ {
Branch: 4 branches
BranchTrueFalseTotal
{ 139 3 142
{ 139 0 139
{ 138 1 139
{ 2 136 138
1226 6 SV **sp = hv_fetchs(cfg, "secret", 0);
1227 6 if (!sp || !*sp || !SvOK(*sp) || !SvCUR(*sp)) { /* # uncoverable condition left count:2 class:ignore_covered_err */
1228 SvREFCNT_dec((SV *)cfg);
1229 croak("Punk: `session` needs a non-empty secret - without one "
1230 "the cookie is signed with an empty key and anyone can "
1231 "forge it. Try `session secret => secret('session_key')"
1232 "`, which fails closed when the key is missing");
1233 }
1234 }
1235 { /* the write-back runs where a croak is trapped, so a cookie
1236 * attribute it would refuse is refused here instead */
1237 670 ▶ static const char *const attrs[] = { "cookie", "path", "domain",
Branch: 1 branch
BranchTrueFalseTotal
static const char *const attrs[] = { "cookie", "path", "domain", 538 132 670
1238 538 "samesite", NULL };
1239 538 ▶ for (i = 0; attrs[i]; i++) {
Branch: 4 branches
BranchTrueFalseTotal
for (i = 0; attrs[i]; i++) { 406 132 538
for (i = 0; attrs[i]; i++) { 406 0 406
for (i = 0; attrs[i]; i++) { 406 0 406
for (i = 0; attrs[i]; i++) { 4 402 406
1240 4 SV **ap = hv_fetch(cfg, attrs[i], (I32)strlen(attrs[i]), 0);
1241 4 # uncoverable condition right count:2 class:ignore_covered_err
1242 # uncoverable condition right count:3 class:ignore_covered_err
1243 if (ap && *ap && SvOK(*ap) && !pk_cookie_safe(aTHX_ *ap)) {
1244 SvREFCNT_dec((SV *)cfg);
1245 croak("Punk: session `%s` may not contain ';' or a "
1246 132 "control character", attrs[i]);
1247 132 }
1248 }
1249 }
1250 (void)hv_stores(h, "session", newRV_noinc((SV *)cfg));
1251 RETVAL = newSVsv(self);
1252 }
1253 OUTPUT:
1254 RETVAL
1255  
1256 # logging(level => 'debug', format => 'json', to => ...) or logging(\%opts):
1257 # freeze the logger config on the app (level/format/to). Chains.
1258 SV *
1259 logging(self, ...)
1260 SV *self
1261 CODE:
1262 {
1263 40 HV *h = app_hv(aTHX_ self);
1264 40 HV *cfg = newHV();
1265 int i;
1266 40 ▶ # uncoverable branch true
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable branch true 0 40 40
# uncoverable branch true 0 0 0
# uncoverable branch true 0 0 0
1267 0 # uncoverable condition left count:1 class:ignore_covered_err
1268 0 # uncoverable condition left count:2 class:ignore_covered_err
1269 0 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 0 0 0
1270 0 ▶ # uncoverable condition left count:3 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:3 class:ignore_covered_err 0 0 0
1271 0 # uncoverable condition right count:3 class:ignore_covered_err
1272 if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) {
1273 HE *e; HV *g = (HV *)SvRV(ST(1)); /* # uncoverable statement */
1274 93 ▶ hv_iterinit(g); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
hv_iterinit(g); /* # uncoverable statement */ 53 40 93
1275 53 # uncoverable statement
1276 53 # uncoverable branch true
1277 # uncoverable branch false
1278 # uncoverable condition left count:1
1279 # uncoverable condition right count:1
1280 while ((e = hv_iternext(g))) {
1281 STRLEN kl; const char *k = HePV(e, kl); /* # uncoverable statement */
1282 40 (void)hv_store(cfg, k, (I32)kl, newSVsv(hv_iterval(g, e)), 0); /* # uncoverable statement */
1283 40 }
1284 40 ▶ }
Branch: 4 branches
BranchTrueFalseTotal
} 39 1 40
} 39 0 39
} 39 0 39
} 4 35 39
1285 4 ▶ else for (i = 1; i + 1 < items; i += 2) {
Branch: 2 branches
BranchTrueFalseTotal
else for (i = 1; i + 1 < items; i += 2) { 4 0 4
else for (i = 1; i + 1 < items; i += 2) { 4 0 4
1286 STRLEN kl; const char *k = SvPV_const(ST(i), kl);
1287 (void)hv_store(cfg, k, (I32)kl, newSVsv(ST(i + 1)), 0);
1288 4 }
1289 4 ▶ { /* `to` as a path: what a YAML file can say, since it cannot hold
Branch: 11 branches
BranchTrueFalseTotal
{ /* `to` as a path: what a YAML file can say, since it cannot hold 1 3 4
{ /* `to` as a path: what a YAML file can say, since it cannot hold 1 0 1
{ /* `to` as a path: what a YAML file can say, since it cannot hold 0 1 1
{ /* `to` as a path: what a YAML file can say, since it cannot hold 0 0 0
{ /* `to` as a path: what a YAML file can say, since it cannot hold 1 0 1
{ /* `to` as a path: what a YAML file can say, since it cannot hold 1 0 1
{ /* `to` as a path: what a YAML file can say, since it cannot hold 1 0 1
{ /* `to` as a path: what a YAML file can say, since it cannot hold 0 1 1
{ /* `to` as a path: what a YAML file can say, since it cannot hold 0 0 0
{ /* `to` as a path: what a YAML file can say, since it cannot hold 1 0 1
{ /* `to` as a path: what a YAML file can say, since it cannot hold 1 0 1
1290 * a handle. Opened here, for append, so a path that cannot be
1291 40 ▶ * written is a boot error and not a 500 on the first line that
Branch: 1 branch
BranchTrueFalseTotal
* written is a boot error and not a 500 on the first line that 1 39 40
1292 1 * somebody tries to log. */
1293 39 ▶ SV **to = hv_fetchs(cfg, "to", 0);
Branch: 4 branches
BranchTrueFalseTotal
SV **to = hv_fetchs(cfg, "to", 0); 38 1 39
SV **to = hv_fetchs(cfg, "to", 0); 38 0 38
SV **to = hv_fetchs(cfg, "to", 0); 38 0 38
SV **to = hv_fetchs(cfg, "to", 0); 3 35 38
1294 3 ▶ GV *named = NULL;
Branch: 2 branches
BranchTrueFalseTotal
GV *named = NULL; 3 0 3
GV *named = NULL; 3 0 3
1295 6 # uncoverable condition right count:2 class:ignore_covered_err
1296 # uncoverable condition right count:3 class:ignore_covered_err
1297 # uncoverable condition right count:5 class:ignore_covered_err
1298 3 ▶ # uncoverable condition right count:6 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:6 class:ignore_covered_err 1 2 3
1299 1 if (to && *to && SvOK(*to) && !SvROK(*to)
1300 && SvTYPE(*to) != SVt_PVGV && SvCUR(*to)) {
1301 /* the NAME of an open handle first - `to: STDERR` is what
1302 1 * a YAML file says for the default - and a path otherwise */
1303 1 GV *g = gv_fetchsv(*to, 0, SVt_PVIO);
1304 if (g && GvIO(g) && IoOFP(GvIO(g))) named = g;
1305 2 }
1306 2 if (named)
1307 2 ▶ (void)hv_stores(cfg, "to", newRV_inc((SV *)named));
Branch: 5 branches
BranchTrueFalseTotal
(void)hv_stores(cfg, "to", newRV_inc((SV *)named)); 2 0 2
(void)hv_stores(cfg, "to", newRV_inc((SV *)named)); 0 2 2
(void)hv_stores(cfg, "to", newRV_inc((SV *)named)); 0 0 0
(void)hv_stores(cfg, "to", newRV_inc((SV *)named)); 2 0 2
(void)hv_stores(cfg, "to", newRV_inc((SV *)named)); 0 2 2
1308 2 # uncoverable condition right count:2 class:ignore_covered_err
1309 2 # uncoverable condition right count:3 class:ignore_covered_err
1310 2 # uncoverable condition right count:5 class:ignore_covered_err
1311 # uncoverable condition right count:6 class:ignore_covered_err
1312 else if (to && *to && SvOK(*to) && !SvROK(*to)
1313 39 && SvTYPE(*to) != SVt_PVGV && SvCUR(*to)) {
1314 39 PerlIO *fp = PerlIO_open(SvPV_nolen(*to), "a");
1315 GV *gv;
1316 IO *io;
1317 if (!fp) {
1318 SV *msg = sv_2mortal(newSVpvf(
1319 "Punk: logging cannot open '%" SVf "' for append: %s",
1320 SVfARG(*to), Strerror(errno)));
1321 SvREFCNT_dec((SV *)cfg);
1322 croak_sv(msg);
1323 }
1324 PerlIO_setlinebuf(fp);
1325 gv = newGVgen("Punk::Logger");
1326 io = GvIOn(gv);
1327 IoIFP(io) = IoOFP(io) = fp;
1328 IoTYPE(io) = IoTYPE_APPEND;
1329 (void)hv_stores(cfg, "to", newRV_noinc((SV *)gv));
1330 }
1331 }
1332 (void)hv_stores(h, "logging", newRV_noinc((SV *)cfg));
1333 RETVAL = newSVsv(self);
1334 }
1335 OUTPUT:
1336 RETVAL
1337  
1338 # api($spec, $opts?, $scope?) -> a Punk::Mount::OpenAPI.
1339 SV *
1340 api(self, spec, opts = &PL_sv_undef, scope = &PL_sv_undef)
1341 SV *self
1342 SV *spec
1343 SV *opts
1344 SV *scope
1345 CODE:
1346 {
1347 34 HV *h = app_hv(aTHX_ self);
1348 SV *argv[8], *mount, *pfx, *grd;
1349 34 (void)pk_require_once(aTHX_ PK_MOUNT_OA, TRUE);
1350 34 ▶ if (SvOK(scope)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(scope)) { 21 13 34
1351 21 pfx = pcx_call_meth(aTHX_ scope, K_PREFIX, NULL, 0, 1);
1352 21 grd = pcx_call_meth(aTHX_ scope, K_GUARDS, NULL, 0, 1);
1353 13 } else { pfx = newSVpvs(""); grd = newRV_noinc((SV *)newAV()); }
1354 34 argv[0] = sv_2mortal(newSVpvs("spec")); argv[1] = spec;
1355 34 argv[2] = sv_2mortal(newSVpvs(K_OPTS));
1356 34 ▶ argv[3] = SvOK(opts) ? opts : sv_2mortal(newRV_noinc((SV *)newHV()));
Branch: 1 branch
BranchTrueFalseTotal
argv[3] = SvOK(opts) ? opts : sv_2mortal(newRV_noinc((SV *)newHV())); 5 29 34
1357 34 argv[4] = sv_2mortal(newSVpvs(K_PREFIX)); argv[5] = sv_2mortal(pfx);
1358 34 argv[6] = sv_2mortal(newSVpvs(K_GUARDS)); argv[7] = sv_2mortal(grd);
1359 34 mount = pcx_call_meth(aTHX_ sv_2mortal(newSVpvs(PK_MOUNT_OA)),
1360 "new", argv, 8, 1);
1361 33 ▶ av_push(app_av(aTHX_ h, K_API_MOUNTS), mount ? mount : newSV(0)); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
av_push(app_av(aTHX_ h, K_API_MOUNTS), mount ? mount : newSV(0)); /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 33 33
1362 33 ▶ RETVAL = mount ? newSVsv(mount) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = mount ? newSVsv(mount) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ 33 0 33
1363 }
1364 OUTPUT:
1365 RETVAL
1366  
1367 # docs($path, $mount?, $opts?): record a docs UI mount. Chains.
1368 SV *
1369 docs(self, path, mount = &PL_sv_undef, opts = &PL_sv_undef)
1370 SV *self
1371 SV *path
1372 SV *mount
1373 SV *opts
1374 CODE:
1375 {
1376 14 HV *h = app_hv(aTHX_ self);
1377 14 HV *rec = newHV();
1378 /* (mount, opts) = (undef, mount) if mount is a plain hashref and !opts
1379 * - a blessed mount object (ref ne 'HASH') is never the opts slot */
1380 14 ▶ # uncoverable branch true
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch true 9 5 14
# uncoverable branch true 9 0 9
1381 9 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 0 9 9
# uncoverable condition left count:1 class:ignore_covered_err 0 0 0
1382 0 # uncoverable condition left count:2 class:ignore_covered_err
1383 # uncoverable condition right count:2 class:ignore_covered_err
1384 14 # uncoverable condition left count:3 class:ignore_covered_err
1385 14 # uncoverable condition left count:4 class:ignore_covered_err
1386 14 ▶ # uncoverable condition right count:4 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:4 class:ignore_covered_err 1 13 14
# uncoverable condition right count:4 class:ignore_covered_err 1 0 1
1387 if (SvROK(mount) && SvTYPE(SvRV(mount)) == SVt_PVHV
1388 && !SvOBJECT(SvRV(mount)) && !SvOK(opts)) {
1389 opts = mount; mount = &PL_sv_undef; /* # uncoverable statement */
1390 14 }
1391 14 (void)hv_stores(rec, K_PATH, newSVsv(path));
1392 (void)hv_stores(rec, K_MOUNT, newSVsv(mount));
1393 (void)hv_stores(rec, K_OPTS,
1394 (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)
1395 ? newRV_noinc((SV *)newHVhv((HV *)SvRV(opts)))
1396 : newRV_noinc((SV *)newHV()));
1397 av_push(app_av(aTHX_ h, K_DOCS), newRV_noinc((SV *)rec));
1398 RETVAL = newSVsv(self);
1399 }
1400 OUTPUT:
1401 RETVAL
1402  
1403 # static($prefix, $dir, %opts): serve files. The options are the mount's
1404 # cache policy - max_age, cache_control, fingerprint - and reach
1405 # Punk::Static->app at compile. Chains.
1406 SV *
1407 static(self, prefix, dir, ...)
1408 SV *self
1409 SV *prefix
1410 SV *dir
1411 CODE:
1412 {
1413 52 HV *h = app_hv(aTHX_ self);
1414 52 HV *rec = newHV();
1415 52 SV *pf = app_strip_slash(aTHX_ prefix);
1416 52 HV *opts = newHV();
1417 int i;
1418 52 ▶ if ((items - 3) % 2)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 3) % 2) 1 51 52
1419 1 croak("Punk: static takes a prefix, a directory and an "
1420 "even-sized option list");
1421 63 ▶ for (i = 3; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 3; i + 1 < items; i += 2) { 12 51 63
1422 STRLEN kl;
1423 12 const char *kp = SvPV_const(ST(i), kl);
1424 12 (void)hv_store(opts, kp, (I32)kl, newSVsv(ST(i + 1)), 0);
1425 }
1426 51 (void)hv_stores(rec, K_PREFIX, pf);
1427 51 (void)hv_stores(rec, K_LEN, newSViv((IV)SvCUR(pf)));
1428 51 (void)hv_stores(rec, K_DIR, newSVsv(dir));
1429 51 (void)hv_stores(rec, K_OPTS, newRV_noinc((SV *)opts));
1430 51 av_push(app_av(aTHX_ h, K_MOUNTS), newRV_noinc((SV *)rec));
1431 51 RETVAL = newSVsv(self);
1432 }
1433 OUTPUT:
1434 RETVAL
1435  
1436 # markdown($prefix, $dir, %opts): serve a directory of markdown as a
1437 # documentation site. Rides the same mount table static does, so prefix
1438 # dispatch and longest-prefix ordering come free; compile turns the record
1439 # into a Punk::Mount::Markdown app. Chains.
1440 SV *
1441 markdown(self, prefix, dir, ...)
1442 SV *self
1443 SV *prefix
1444 SV *dir
1445 CODE:
1446 {
1447 87 HV *h = app_hv(aTHX_ self);
1448 87 HV *rec = newHV();
1449 87 SV *pf = app_strip_slash(aTHX_ prefix);
1450 87 HV *opts = newHV();
1451 int i;
1452 87 ▶ if ((items - 3) % 2)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 3) % 2) 1 86 87
1453 1 croak("Punk: markdown takes a prefix, a directory and "
1454 "an even-sized option list");
1455 211 ▶ for (i = 3; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 3; i + 1 < items; i += 2) { 125 86 211
1456 STRLEN kl;
1457 125 const char *kp = SvPV_const(ST(i), kl);
1458 125 (void)hv_store(opts, kp, (I32)kl, newSVsv(ST(i + 1)), 0);
1459 }
1460 86 (void)hv_stores(rec, K_PREFIX, pf);
1461 86 (void)hv_stores(rec, K_LEN, newSViv((IV)SvCUR(pf)));
1462 86 (void)hv_stores(rec, K_MD_DIR, newSVsv(dir));
1463 86 (void)hv_stores(rec, K_OPTS, newRV_noinc((SV *)opts));
1464 86 av_push(app_av(aTHX_ h, K_MOUNTS), newRV_noinc((SV *)rec));
1465 86 RETVAL = newSVsv(self);
1466 }
1467 OUTPUT:
1468 RETVAL
1469  
1470 # markdown_pages($prefix): the pages of the markdown mount at that prefix,
1471 # in its order, as fresh hashrefs (punk_markdown.h, pmd_pages_av). The site
1472 # is built by compile, so before to_app there is nothing to list.
1473 SV *
1474 markdown_pages(self, prefix)
1475 SV *self
1476 SV *prefix
1477 CODE:
1478 16 RETVAL = newRV_noinc((SV *)pmd_pages_av(aTHX_
1479 pmd_mount_cap(aTHX_ self, prefix, "Punk: markdown_pages")));
1480 OUTPUT:
1481 RETVAL
1482  
1483 # mount($prefix, $psgi_app): mount a PSGI coderef. Chains.
1484 SV *
1485 mount(self, prefix, app)
1486 SV *self
1487 SV *prefix
1488 SV *app
1489 CODE:
1490 {
1491 21 HV *h = app_hv(aTHX_ self);
1492 HV *rec; SV *pf;
1493 21 ▶ if (!(SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVCV))
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVCV)) 19 2 21
if (!(SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVCV)) 1 18 19
1494 3 croak("Punk: mount needs a PSGI coderef");
1495 18 rec = newHV();
1496 18 pf = app_strip_slash(aTHX_ prefix);
1497 18 (void)hv_stores(rec, K_PREFIX, pf);
1498 18 (void)hv_stores(rec, K_LEN, newSViv((IV)SvCUR(pf)));
1499 18 (void)hv_stores(rec, K_APP, newSVsv(app));
1500 18 av_push(app_av(aTHX_ h, K_MOUNTS), newRV_noinc((SV *)rec));
1501 18 RETVAL = newSVsv(self);
1502 }
1503 OUTPUT:
1504 RETVAL
1505  
1506 # views($name, $opts?): register a view engine. Chains.
1507 SV *
1508 views(self, name, opts = &PL_sv_undef)
1509 SV *self
1510 SV *name
1511 SV *opts
1512 CODE:
1513 {
1514 82 HV *h = app_hv(aTHX_ self);
1515 82 AV *pair = newAV();
1516 82 av_push(pair, newSVsv(name));
1517 82 ▶ av_push(pair, (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
av_push(pair, (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) /* # uncoverable condition right count:2 class:ignore_covered_err */ 77 5 82
av_push(pair, (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) /* # uncoverable condition right count:2 class:ignore_covered_err */ 77 0 77
1518 ? newRV_noinc((SV *)newHVhv((HV *)SvRV(opts)))
1519 : newRV_noinc((SV *)newHV()));
1520 82 av_push(app_av(aTHX_ h, K_VIEWS), newRV_noinc((SV *)pair));
1521 82 RETVAL = newSVsv(self);
1522 }
1523 OUTPUT:
1524 RETVAL
1525  
1526 # database(dsn => ... | $name => \%opts): configure a database. Chains.
1527 SV *
1528 database(self, ...)
1529 SV *self
1530 CODE:
1531 {
1532 49 HV *h = app_hv(aTHX_ self);
1533 49 HV *db = app_hash(aTHX_ h, K_DATABASES);
1534 70 ▶ if (items == 3 && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (items == 3 && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 42 7 49
if (items == 3 && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 21 21 42
if (items == 3 && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 21 0 21
1535 21 STRLEN nl; const char *n = SvPV_const(ST(1), nl);
1536 21 (void)hv_store(db, n, (I32)nl,
1537 newRV_noinc((SV *)newHVhv((HV *)SvRV(ST(2)))), 0);
1538 }
1539 else {
1540 28 HV *opts = newHV(); int i;
1541 66 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 38 28 66
1542 38 STRLEN kl; const char *k = SvPV_const(ST(i), kl);
1543 38 (void)hv_store(opts, k, (I32)kl, newSVsv(ST(i + 1)), 0);
1544 }
1545 28 (void)hv_stores(db, K_DEFAULT, newRV_noinc((SV *)opts));
1546 }
1547 49 RETVAL = newSVsv(self);
1548 }
1549 OUTPUT:
1550 RETVAL
1551  
1552 # databases(): the configured databases read back - a deep copy, keyed by
1553 # name with "default" for the unnamed one. Copies, because a plugin must
1554 # not be able to edit the connection options of the application it is
1555 # installed in; everything else on the registrar records, this reads.
1556 SV *
1557 databases(self)
1558 SV *self
1559 CODE:
1560 {
1561 6 HV *h = app_hv(aTHX_ self);
1562 6 HV *db = app_hash(aTHX_ h, K_DATABASES);
1563 6 RETVAL = app_deep_copy(aTHX_ sv_2mortal(newRV_inc((SV *)db)));
1564 }
1565 OUTPUT:
1566 RETVAL
1567  
1568 # auth_config(): the frozen auth configuration read back - a deep copy,
1569 # and undef when no `auth` keyword ran. (Not "# undef when ..." on a line of
1570 # its own: xsubpp reads a comment beginning with a directive name as the
1571 # directive, and `# undef` becomes an #undef in the generated C.)
1572 # The keyword is write-only otherwise, and
1573 # a plugin that answers "may this user act on this row"
1574 # (Punk::Plugin::Authorization) needs two things out of it: the `rank`
1575 # ladder, so a rank name it is given can be checked against the one
1576 # auth_guard uses rather than a second copy, and the `roles` hook, so it can
1577 # ask outside a guard - $c->stash->{auth}{roles} is only there when a guard
1578 # ran. The `databases` read-back next to it is the precedent.
1579 SV *
1580 auth_config(self)
1581 SV *self
1582 CODE:
1583 {
1584 4 HV *h = app_hv(aTHX_ self);
1585 4 SV **a = hv_fetchs(h, K_AUTH, 0);
1586 7 ▶ RETVAL = (a && *a && SvROK(*a))
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (a && *a && SvROK(*a)) 3 0 3
RETVAL = (a && *a && SvROK(*a)) 3 0 3
1587 7 ▶ ? app_deep_copy(aTHX_ *a) : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
? app_deep_copy(aTHX_ *a) : newSV(0); 3 1 4
1588 }
1589 OUTPUT:
1590 RETVAL
1591  
1592 # on_compile($code, $owner?): a callback for to_app, run before anything is
1593 # compiled and after every keyword has recorded - the moment a plugin that
1594 # registered above a `database` line needs, and the one Punk::Plugin::Queue
1595 # faked with a middleware whose constructor runs once at compile. The
1596 # callback gets the registrar; a die is a boot croak naming the owner. Not
1597 # a hook phase: those are request phases. Croaks once the app is compiled.
1598 SV *
1599 on_compile(self, code, owner = &PL_sv_undef)
1600 SV *self
1601 SV *code
1602 SV *owner
1603 CODE:
1604 {
1605 9 HV *h = app_hv(aTHX_ self);
1606 AV *rec;
1607 9 SV **cp = hv_fetchs(h, K_COMPILED, 0);
1608 9 ▶ if (!(SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVCV)) /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVCV)) /* # uncoverable condition left count:2 class:ignore_covered_err */ 8 1 9
if (!(SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVCV)) /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 8 8
1609 1 croak("Punk: on_compile takes a coderef");
1610 8 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 4 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 8 0 8
# uncoverable condition right count:1 class:ignore_covered_err 8 0 8
# uncoverable condition right count:1 class:ignore_covered_err 8 0 8
# uncoverable condition right count:1 class:ignore_covered_err 1 7 8
1611 1 # uncoverable condition right count:2 class:ignore_covered_err
1612 # uncoverable condition right count:3 class:ignore_covered_err
1613 7 if (cp && *cp && SvOK(*cp) && SvIV(*cp) > 0)
1614 7 croak("Punk: on_compile after to_app - the application is already "
1615 7 ▶ "compiled, so the callback would never run");
Branch: 8 branches
BranchTrueFalseTotal
"compiled, so the callback would never run"); 4 3 7
"compiled, so the callback would never run"); 3 0 3
"compiled, so the callback would never run"); 3 0 3
"compiled, so the callback would never run"); 3 0 3
"compiled, so the callback would never run"); 0 3 3
"compiled, so the callback would never run"); 0 0 0
"compiled, so the callback would never run"); 3 0 3
"compiled, so the callback would never run"); 0 3 3
1616 rec = newAV();
1617 7 av_push(rec, newSVsv(code));
1618 7 # uncoverable condition right count:2 class:ignore_covered_err
1619 # uncoverable condition left count:3 class:ignore_covered_err
1620 av_push(rec, SvOK(owner) ? newSVsv(owner)
1621 : newSVpv(CopSTASHPV(PL_curcop), 0));
1622 av_push(app_av(aTHX_ h, K_ON_COMPILE), newRV_noinc((SV *)rec));
1623 RETVAL = newSVsv(self);
1624 }
1625 OUTPUT:
1626 RETVAL
1627  
1628 # model_class(@names): register model names. Bare `model;` turns
1629 # auto-discovery on explicitly - load everything under ${caller}::Model:: -
1630 # which also survives alongside named registrations (naming a model
1631 # normally switches auto off). Chains.
1632 SV *
1633 model_class(self, ...)
1634 SV *self
1635 CODE:
1636 {
1637 30 HV *h = app_hv(aTHX_ self);
1638 30 AV *m = app_av(aTHX_ h, K_MODELS);
1639 int i;
1640 30 ▶ if (items == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (items == 1) 2 28 30
1641 2 (void)hv_stores(h, K_MODEL_AUTO, newSViv(1));
1642 else
1643 58 ▶ for (i = 1; i < items; i++) av_push(m, newSVsv(ST(i)));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) av_push(m, newSVsv(ST(i))); 30 28 58
1644 30 RETVAL = newSVsv(self);
1645 }
1646 OUTPUT:
1647 RETVAL
1648  
1649 # model_auto($on?): get/set the auto-discovery flag.
1650 SV *
1651 model_auto(self, on = NULL)
1652 SV *self
1653 SV *on
1654 CODE:
1655 {
1656 4 HV *h = app_hv(aTHX_ self);
1657 4 ▶ if (!on) {
Branch: 1 branch
BranchTrueFalseTotal
if (!on) { 3 1 4
1658 3 SV *v = app_get(aTHX_ h, K_MODEL_AUTO);
1659 3 ▶ RETVAL = newSViv((v && SvOK(v)) ? (SvTRUE(v) ? 1 : 0) : 1);
Branch: 3 branches
BranchTrueFalseTotal
RETVAL = newSViv((v && SvOK(v)) ? (SvTRUE(v) ? 1 : 0) : 1); 2 1 3
RETVAL = newSViv((v && SvOK(v)) ? (SvTRUE(v) ? 1 : 0) : 1); 2 0 2
RETVAL = newSViv((v && SvOK(v)) ? (SvTRUE(v) ? 1 : 0) : 1); 1 1 2
1660 }
1661 else {
1662 1 (void)hv_stores(h, K_MODEL_AUTO, newSViv(SvTRUE(on) ? 1 : 0));
1663 1 RETVAL = newSVsv(self);
1664 }
1665 }
1666 OUTPUT:
1667 RETVAL
1668  
1669 # hook($name, $code): add a before/after_dispatch hook. Chains.
1670 SV *
1671 hook(self, name, code)
1672 SV *self
1673 SV *name
1674 SV *code
1675 CODE:
1676 {
1677 48 HV *h = app_hv(aTHX_ self);
1678 48 HV *hooks = app_hash(aTHX_ h, K_HOOKS);
1679 48 STRLEN nl; const char *n = SvPV_const(name, nl);
1680 48 SV **slot = hv_fetch(hooks, n, (I32)nl, 0);
1681 48 ▶ # uncoverable condition left count:2 class:ignore_covered_err
Branch: 4 branches
BranchTrueFalseTotal
# uncoverable condition left count:2 class:ignore_covered_err 44 4 48
# uncoverable condition left count:2 class:ignore_covered_err 44 0 44
# uncoverable condition left count:2 class:ignore_covered_err 44 0 44
# uncoverable condition left count:2 class:ignore_covered_err 0 44 44
1682 4 # uncoverable condition left count:3 class:ignore_covered_err
1683 # uncoverable condition left count:4 class:ignore_covered_err
1684 44 if (!(slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV))
1685 44 croak("Punk: unknown hook '%s' (before_request, before_dispatch, "
1686 "after_dispatch, after_response, before_render)", n);
1687 av_push((AV *)SvRV(*slot), newSVsv(code));
1688 RETVAL = newSVsv(self);
1689 }
1690 OUTPUT:
1691 RETVAL
1692  
1693 # middleware($code): add an outer PSGI wrap. Chains.
1694 SV *
1695 middleware(self, code)
1696 SV *self
1697 SV *code
1698 CODE:
1699 53 av_push(app_av(aTHX_ app_hv(aTHX_ self), K_MIDDLEWARE), newSVsv(code));
1700 53 RETVAL = newSVsv(self);
1701 OUTPUT:
1702 RETVAL
1703  
1704 # on_error($code): set the error handler. Chains.
1705 SV *
1706 on_error(self, code)
1707 SV *self
1708 SV *code
1709 CODE:
1710 16 (void)hv_stores(app_hv(aTHX_ self), K_ON_ERROR, newSVsv(code));
1711 16 RETVAL = newSVsv(self);
1712 OUTPUT:
1713 RETVAL
1714  
1715 # on_not_found($code): set the 404 handler - same contract as on_error, a
1716 # reference return becomes the response. Chains.
1717 SV *
1718 on_not_found(self, code)
1719 SV *self
1720 SV *code
1721 CODE:
1722 6 (void)hv_stores(app_hv(aTHX_ self), K_ON_NOT_FOUND, newSVsv(code));
1723 6 RETVAL = newSVsv(self);
1724 OUTPUT:
1725 RETVAL
1726  
1727 # helper($name, $code, $owner?): install a context helper. Chains.
1728 SV *
1729 helper(self, name, code, owner = &PL_sv_undef)
1730 SV *self
1731 SV *name
1732 SV *code
1733 SV *owner
1734 CODE:
1735 {
1736 380 HV *h = app_hv(aTHX_ self);
1737 380 HV *helpers = app_hash(aTHX_ h, K_HELPERS);
1738 380 STRLEN nl; const char *n = SvPV_const(name, nl);
1739 380 HV *ctxstash = gv_stashpvs(PK_CONTEXT, 0);
1740 SV **have; HV *rec;
1741 380 ▶ if (ctxstash && gv_fetchmethod_autoload(ctxstash, n, 0)) /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (ctxstash && gv_fetchmethod_autoload(ctxstash, n, 0)) /* # uncoverable condition right count:1 class:ignore_covered_err */ 380 0 380
if (ctxstash && gv_fetchmethod_autoload(ctxstash, n, 0)) /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 378 380
1742 2 croak("Punk: helper '%s' collides with a Punk::Context method", n);
1743 378 have = hv_fetch(helpers, n, (I32)nl, 0);
1744 378 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 1 377 378
# uncoverable condition right count:2 class:ignore_covered_err 1 0 1
# uncoverable condition right count:2 class:ignore_covered_err 1 0 1
1745 1 # uncoverable condition right count:3 class:ignore_covered_err
1746 3 ▶ if (have && *have && SvROK(*have)) {
Branch: 10 branches
BranchTrueFalseTotal
if (have && *have && SvROK(*have)) { 1 0 1
if (have && *have && SvROK(*have)) { 0 0 0
if (have && *have && SvROK(*have)) { 0 0 0
if (have && *have && SvROK(*have)) { 0 0 0
if (have && *have && SvROK(*have)) { 0 0 0
if (have && *have && SvROK(*have)) { 0 0 0
if (have && *have && SvROK(*have)) { 0 0 0
if (have && *have && SvROK(*have)) { 0 0 0
if (have && *have && SvROK(*have)) { 1 0 1
if (have && *have && SvROK(*have)) { 1 0 1
1747 SV **o = hv_fetchs((HV *)SvRV(*have), K_OWNER, 0);
1748 croak("Punk: helper '%s' is registered by both %s and %s", n,
1749 (o && *o) ? SvPV_nolen(*o) : "?",
1750 SvOK(owner) ? SvPV_nolen(owner) /* # uncoverable condition right count:1 class:ignore_covered_err */
1751 377 : CopSTASHPV(PL_curcop)); /* # uncoverable statement */
1752 377 }
1753 377 ▶ rec = newHV();
Branch: 8 branches
BranchTrueFalseTotal
rec = newHV(); 198 179 377
rec = newHV(); 179 0 179
rec = newHV(); 179 0 179
rec = newHV(); 179 0 179
rec = newHV(); 0 179 179
rec = newHV(); 0 0 0
rec = newHV(); 179 0 179
rec = newHV(); 0 179 179
1754 (void)hv_stores(rec, K_CODE, newSVsv(code));
1755 377 (void)hv_stores(rec, K_OWNER,
1756 377 SvOK(owner) ? newSVsv(owner) : newSVpv(CopSTASHPV(PL_curcop), 0));
1757 (void)hv_store(helpers, n, (I32)nl, newRV_noinc((SV *)rec), 0);
1758 RETVAL = newSVsv(self);
1759 }
1760 OUTPUT:
1761 RETVAL
1762  
1763 # install_kw($name, $code, $owner?): install a declaration keyword into the
1764 # application class - the supported way for a plugin to add to the DSL, so
1765 # nothing has to assign to a glob. Chains.
1766 #
1767 # The keyword is a magic CV forwarding to $code in the caller's context
1768 # (punk_import.h). Installing the same name from the same owner twice is a
1769 # no-op - a plugin that installs from both its import and its register is
1770 # doing the ordinary thing - and two owners claiming one name croak, naming
1771 # both, exactly as helper does.
1772 SV *
1773 install_kw(self, name, code, owner = &PL_sv_undef)
1774 SV *self
1775 SV *name
1776 SV *code
1777 SV *owner
1778 CODE:
1779 {
1780 70 HV *h = app_hv(aTHX_ self);
1781 70 HV *kws = app_hash(aTHX_ h, K_KEYWORDS);
1782 70 SV *caller = app_get(aTHX_ h, K_CALLER);
1783 70 STRLEN nl; const char *n = SvPV_const(name, nl);
1784 140 SV *own = SvOK(owner) ? owner
1785 70 ▶ : sv_2mortal(newSVpv(CopSTASHPV(PL_curcop), 0));
Branch: 8 branches
BranchTrueFalseTotal
: sv_2mortal(newSVpv(CopSTASHPV(PL_curcop), 0)); 1 69 70
: sv_2mortal(newSVpv(CopSTASHPV(PL_curcop), 0)); 1 0 1
: sv_2mortal(newSVpv(CopSTASHPV(PL_curcop), 0)); 1 0 1
: sv_2mortal(newSVpv(CopSTASHPV(PL_curcop), 0)); 1 0 1
: sv_2mortal(newSVpv(CopSTASHPV(PL_curcop), 0)); 0 1 1
: sv_2mortal(newSVpv(CopSTASHPV(PL_curcop), 0)); 0 0 0
: sv_2mortal(newSVpv(CopSTASHPV(PL_curcop), 0)); 1 0 1
: sv_2mortal(newSVpv(CopSTASHPV(PL_curcop), 0)); 0 1 1
1786 70 SV **have; HV *rec; int done = 0;
1787  
1788 70 ▶ # uncoverable branch true
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch true 70 0 70
# uncoverable branch true 0 70 70
1789 0 # uncoverable condition left count:1 class:ignore_covered_err
1790 70 ▶ # uncoverable condition left count:2 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition left count:2 class:ignore_covered_err 69 1 70
# uncoverable condition left count:2 class:ignore_covered_err 68 1 69
# uncoverable condition left count:2 class:ignore_covered_err 0 68 68
1791 2 if (!caller || !SvOK(caller))
1792 68 ▶ croak("Punk: install_kw('%s') needs an application class", n); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk: install_kw('%s') needs an application class", n); /* # uncoverable statement */ 1 67 68
1793 1 if (!nl || !SvROK(code) || SvTYPE(SvRV(code)) != SVt_PVCV) /* # uncoverable condition left count:3 class:ignore_covered_err */
1794 croak("Punk: install_kw needs a name and a code reference");
1795 67 if (pki_is_dsl(n))
1796 67 ▶ croak("Punk: keyword '%s' is part of the Punk DSL", n);
Branch: 3 branches
BranchTrueFalseTotal
croak("Punk: keyword '%s' is part of the Punk DSL", n); 17 50 67
croak("Punk: keyword '%s' is part of the Punk DSL", n); 17 0 17
croak("Punk: keyword '%s' is part of the Punk DSL", n); 17 0 17
1797 17  
1798 17 ▶ have = hv_fetch(kws, n, (I32)nl, 0);
Branch: 3 branches
BranchTrueFalseTotal
have = hv_fetch(kws, n, (I32)nl, 0); 17 0 17
have = hv_fetch(kws, n, (I32)nl, 0); 17 0 17
have = hv_fetch(kws, n, (I32)nl, 0); 15 2 17
1799 4 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 2 0 2
# uncoverable condition right count:2 class:ignore_covered_err 2 0 2
1800 # uncoverable condition right count:3 class:ignore_covered_err
1801 if (have && *have && SvROK(*have)) {
1802 65 ▶ SV **o = hv_fetchs((HV *)SvRV(*have), K_OWNER, 0);
Branch: 1 branch
BranchTrueFalseTotal
SV **o = hv_fetchs((HV *)SvRV(*have), K_OWNER, 0); 50 15 65
1803 50 # uncoverable condition right count:1 class:ignore_covered_err
1804 50 # uncoverable condition right count:2 class:ignore_covered_err
1805 50 if (o && *o && sv_eq(*o, own)) done = 1; /* the same owner */
1806 50 else croak("Punk: keyword '%s' is installed by both %s and %s", n,
1807 50 (o && *o) ? SvPV_nolen(*o) : "?", SvPV_nolen(own));
1808 }
1809 65 if (!done) {
1810 rec = newHV();
1811 (void)hv_stores(rec, K_CODE, newSVsv(code));
1812 (void)hv_stores(rec, K_OWNER, newSVsv(own));
1813 (void)hv_store(kws, n, (I32)nl, newRV_noinc((SV *)rec), 0);
1814 pki_install_kw(aTHX_ caller, name, code);
1815 }
1816 RETVAL = newSVsv(self);
1817 }
1818 OUTPUT:
1819 RETVAL
1820  
1821 # keyword($name, @args): call a keyword some plugin installed, from code
1822 # rather than from the application's package body - how a plugin consumes a
1823 # sibling's declaration ($app->keyword(sitemap => blog => sub {...})) without
1824 # reaching into the application class by name or waiting for a method the
1825 # providing plugin never grew. The installed code runs with @args in the
1826 # caller's context and its return is passed through, exactly as the keyword
1827 # in the class would have done.
1828 #
1829 # A name nothing installed croaks naming it: the plugin that provides it is
1830 # not loaded, and silently doing nothing would be a sitemap with no section.
1831 # A core DSL word croaks too - those are the registrar's own methods and this
1832 # is not the door to them.
1833 void
1834 keyword(self, name, ...)
1835 SV *self
1836 SV *name
1837 PPCODE:
1838 {
1839 10 HV *h = app_hv(aTHX_ self);
1840 10 HV *kws = app_hash(aTHX_ h, K_KEYWORDS);
1841 10 STRLEN nl; const char *n = SvPV_const(name, nl);
1842 10 SV **have, **code = NULL;
1843 10 I32 want = GIMME_V;
1844 int count, i;
1845  
1846 10 ▶ if (!nl) croak("Punk: keyword needs a name");
Branch: 1 branch
BranchTrueFalseTotal
if (!nl) croak("Punk: keyword needs a name"); 1 9 10
1847 9 ▶ if (pki_is_dsl(n))
Branch: 1 branch
BranchTrueFalseTotal
if (pki_is_dsl(n)) 1 8 9
1848 1 croak("Punk: keyword '%s' is part of the Punk DSL - call the "
1849 "registrar method", n);
1850 8 have = hv_fetch(kws, n, (I32)nl, 0);
1851 8 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 6 2 8
# uncoverable condition right count:2 class:ignore_covered_err 6 0 6
# uncoverable condition right count:2 class:ignore_covered_err 6 0 6
1852 6 # uncoverable condition right count:3 class:ignore_covered_err
1853 8 ▶ if (have && *have && SvROK(*have))
Branch: 3 branches
BranchTrueFalseTotal
if (have && *have && SvROK(*have)) 6 2 8
if (have && *have && SvROK(*have)) 6 0 6
if (have && *have && SvROK(*have)) 6 0 6
1854 6 ▶ code = hv_fetchs((HV *)SvRV(*have), K_CODE, 0);
Branch: 1 branch
BranchTrueFalseTotal
code = hv_fetchs((HV *)SvRV(*have), K_CODE, 0); 0 6 6
1855 2 # uncoverable condition left count:2 class:ignore_covered_err
1856 # uncoverable condition left count:3 class:ignore_covered_err
1857 # uncoverable condition left count:4 class:ignore_covered_err
1858 if (!(code && *code && SvROK(*code)
1859 && SvTYPE(SvRV(*code)) == SVt_PVCV))
1860 croak("Punk: no keyword '%s' is installed on this application - "
1861 6 ▶ "is the plugin that provides it loaded?", n);
Branch: 1 branch
BranchTrueFalseTotal
"is the plugin that provides it loaded?", n); 0 6 6
1862 6 ▶ 
Branch: 2 branches
BranchTrueFalseTotal
6 0 6
0 6 6
1863 10 ▶ /* SP sits at the mark below our own arguments. Push @args over
Branch: 1 branch
BranchTrueFalseTotal
/* SP sits at the mark below our own arguments. Push @args over 4 6 10
1864 6 * them - each source is ahead of its destination, so the copy
1865 6 * down is safe - and the callee's results land at ST(0). */
1866 6 PUSHMARK(SP);
1867 6 ▶ EXTEND(SP, items - 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, items - 2); 4 2 6
1868 2 for (i = 2; i < items; i++) PUSHs(ST(i));
1869 PUTBACK;
1870 count = call_sv(*code, want == G_VOID ? G_VOID : want);
1871 SPAGAIN;
1872 if (want == G_VOID) XSRETURN_EMPTY;
1873 XSRETURN(count);
1874 }
1875  
1876 # has_keyword($name): the owner of an installed keyword, or undef - how a
1877 # plugin asks "did this application load Sitemap?" without sniffing %INC,
1878 # which says a module was loaded somewhere and not that THIS app registered
1879 # it. Core DSL words are not keywords in this sense and answer undef.
1880 SV *
1881 has_keyword(self, name)
1882 SV *self
1883 SV *name
1884 CODE:
1885 {
1886 7 HV *h = app_hv(aTHX_ self);
1887 7 HV *kws = app_hash(aTHX_ h, K_KEYWORDS);
1888 7 STRLEN nl; const char *n = SvPV_const(name, nl);
1889 7 ▶ SV **have = nl ? hv_fetch(kws, n, (I32)nl, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **have = nl ? hv_fetch(kws, n, (I32)nl, 0) : NULL; 6 1 7
1890 2 ▶ SV **o = (have && *have && SvROK(*have))
Branch: 2 branches
BranchTrueFalseTotal
SV **o = (have && *have && SvROK(*have)) 2 0 2
SV **o = (have && *have && SvROK(*have)) 2 0 2
1891 9 ▶ ? hv_fetchs((HV *)SvRV(*have), K_OWNER, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? hv_fetchs((HV *)SvRV(*have), K_OWNER, 0) : NULL; 2 5 7
1892 7 ▶ # uncoverable condition left count:2 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition left count:2 class:ignore_covered_err 2 5 7
# uncoverable condition left count:2 class:ignore_covered_err 2 0 2
# uncoverable condition left count:2 class:ignore_covered_err 0 2 2
1893 2 # uncoverable condition left count:3 class:ignore_covered_err
1894 if (!(o && *o && SvOK(*o))) XSRETURN_UNDEF;
1895 RETVAL = newSVsv(*o);
1896 }
1897 OUTPUT:
1898 RETVAL
1899  
1900 # plugin($name, $opts?): load and register a plugin. Chains.
1901 SV *
1902 plugin(self, name, opts = &PL_sv_undef)
1903 SV *self
1904 SV *name
1905 SV *opts
1906 CODE:
1907 {
1908 570 STRLEN nl; const char *n = SvPV_const(name, nl);
1909 570 ▶ int plus = (nl > 0 && n[0] == '+'); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
int plus = (nl > 0 && n[0] == '+'); /* # uncoverable condition right count:1 class:ignore_covered_err */ 570 0 570
int plus = (nl > 0 && n[0] == '+'); /* # uncoverable condition right count:1 class:ignore_covered_err */ 12 558 570
1910 570 SV *classsv = plus ? newSVpvn(n + 1, nl - 1)
1911 570 ▶ : newSVpvf("Punk::Plugin::%.*s", (int)nl, n);
Branch: 1 branch
BranchTrueFalseTotal
: newSVpvf("Punk::Plugin::%.*s", (int)nl, n); 12 558 570
1912 570 STRLEN cl; const char *class = SvPV_const(classsv, cl);
1913 570 HV *stash = gv_stashpvn(class, cl, 0);
1914 SV *p, *argv[2], *r;
1915 570 sv_2mortal(classsv);
1916 570 ▶ if (!(stash && gv_fetchmethod_autoload(stash, "register", 0))) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(stash && gv_fetchmethod_autoload(stash, "register", 0))) { 561 9 570
if (!(stash && gv_fetchmethod_autoload(stash, "register", 0))) { 23 538 561
1917 32 SV *req = pk_require_src(aTHX_ class);
1918 23 eval_sv(req, G_SCALAR);
1919 23 ▶ if (SvTRUE(ERRSV)) /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) /* # uncoverable condition right count:1 class:ignore_covered_err */ 23 0 23
if (SvTRUE(ERRSV)) /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 22 23
1920 2 ▶ croak("Punk: plugin '%s' (%s) failed to load: %s",
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk: plugin '%s' (%s) failed to load: %s", 1 0 1
1921 n, class, SvPV_nolen(ERRSV));
1922 22 stash = gv_stashpvn(class, cl, 0);
1923 }
1924 560 ▶ p = (stash && gv_fetchmethod_autoload(stash, "new", 0))
Branch: 1 branch
BranchTrueFalseTotal
p = (stash && gv_fetchmethod_autoload(stash, "new", 0)) 152 408 560
1925 152 ? sv_2mortal(pcx_call_meth(aTHX_ classsv, "new", NULL, 0, 1))
1926 968 ▶ : classsv;
Branch: 1 branch
BranchTrueFalseTotal
: classsv; 560 0 560
1927 560 argv[0] = self;
1928 560 ▶ argv[1] = SvOK(opts) ? opts : sv_2mortal(newRV_noinc((SV *)newHV()));
Branch: 1 branch
BranchTrueFalseTotal
argv[1] = SvOK(opts) ? opts : sv_2mortal(newRV_noinc((SV *)newHV())); 81 479 560
1929 560 r = pcx_call_meth(aTHX_ p, "register", argv, 2, 1);
1930 279 ▶ # uncoverable branch false class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false class:ignore_covered_err 279 0 279
1931 279 # uncoverable condition right count:1 class:ignore_covered_err
1932 if (r) SvREFCNT_dec(r);
1933 RETVAL = newSVsv(self);
1934 }
1935 OUTPUT:
1936 RETVAL
1937  
1938 # config(): with no args, the loaded config data (or undef). With args -
1939 # a file and/or options - load Punk::Config and apply the DSL blocks now.
1940 SV *
1941 config(self, ...)
1942 SV *self
1943 CODE:
1944 {
1945 39 HV *h = app_hv(aTHX_ self);
1946 39 ▶ if (items <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items <= 1) { 3 36 39
1947 3 SV *cfg = app_get(aTHX_ h, K_CONFIG);
1948 3 ▶ if (cfg && SvOK(cfg)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (cfg && SvOK(cfg)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 1 3
if (cfg && SvOK(cfg)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 0 2
1949 2 SV *r = pcx_call_meth(aTHX_ cfg, K_CONFIG, NULL, 0, 1);
1950 2 ▶ RETVAL = r ? r : &PL_sv_undef; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = r ? r : &PL_sv_undef; /* # uncoverable condition left count:1 class:ignore_covered_err */ 2 0 2
1951 }
1952 1 else RETVAL = &PL_sv_undef;
1953 }
1954 else {
1955 36 int argc = items - 1, odd = argc % 2, i, m;
1956 SV **av2, *cfg, *aa[1];
1957 36 (void)pk_require_once(aTHX_ PK_CONFIG, TRUE);
1958 /* config $file => %opts ==> (file => $file, %opts) */
1959 36 ▶ m = odd ? argc + 1 : argc;
Branch: 1 branch
BranchTrueFalseTotal
m = odd ? argc + 1 : argc; 35 1 36
1960 36 Newx(av2, m, SV *);
1961 36 ▶ if (odd) {
Branch: 1 branch
BranchTrueFalseTotal
if (odd) { 35 1 36
1962 35 av2[0] = sv_2mortal(newSVpvs("file"));
1963 74 ▶ for (i = 1; i < items; i++) av2[i] = ST(i);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) av2[i] = ST(i); 39 35 74
1964 }
1965 5 ▶ else for (i = 1; i < items; i++) av2[i - 1] = ST(i);
Branch: 1 branch
BranchTrueFalseTotal
else for (i = 1; i < items; i++) av2[i - 1] = ST(i); 4 1 5
1966 36 cfg = pcx_call_meth(aTHX_ sv_2mortal(newSVpvs(PK_CONFIG)),
1967 "load", av2, m, 1);
1968 36 Safefree(av2);
1969 36 ▶ (void)hv_stores(h, K_CONFIG, cfg ? cfg : newSV(0)); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(h, K_CONFIG, cfg ? cfg : newSV(0)); /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 36 36
1970 36 ▶ aa[0] = cfg ? cfg : &PL_sv_undef; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
aa[0] = cfg ? cfg : &PL_sv_undef; /* # uncoverable condition left count:1 class:ignore_covered_err */ 36 0 36
1971 36 { SV *r = pcx_call_meth(aTHX_ self, "_apply_config", aa, 1, 0);
1972 34 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 34 34
1973 34 # uncoverable condition left count:1 class:ignore_covered_err
1974 if (r) SvREFCNT_dec(r); }
1975 RETVAL = newSVsv(self);
1976 }
1977 }
1978 OUTPUT:
1979 RETVAL
1980  
1981 # secret($path): a resolved secret from the loaded config.
1982 SV *
1983 secret(self, path)
1984 SV *self
1985 SV *path
1986 CODE:
1987 {
1988 2 SV *cfg = app_get(aTHX_ app_hv(aTHX_ self), K_CONFIG);
1989 SV *argv[1], *r;
1990 2 ▶ if (!(cfg && SvOK(cfg))) /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(cfg && SvOK(cfg))) /* # uncoverable condition left count:2 class:ignore_covered_err */ 1 1 2
if (!(cfg && SvOK(cfg))) /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 1 1
1991 1 croak("Punk: no configuration loaded (add a config keyword)");
1992 1 argv[0] = path;
1993 1 r = pcx_call_meth(aTHX_ cfg, "secret", argv, 1, 1);
1994 1 ▶ RETVAL = r ? r : &PL_sv_undef; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = r ? r : &PL_sv_undef; /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 0 1
1995 }
1996 OUTPUT:
1997 RETVAL
1998  
1999 # _apply_config($cfg): register the DSL blocks a config file carries
2000 # (views, database, models, plugins, static), in that order.
2001 SV *
2002 _apply_config(self, cfg)
2003 SV *self
2004 SV *cfg
2005 CODE:
2006 {
2007 80 HV *h = app_hv(aTHX_ self);
2008 80 SV *resolved = pcx_call_meth(aTHX_ cfg, "resolved", NULL, 0, 1);
2009 HV *c;
2010 80 ▶ # uncoverable branch false
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false 80 0 80
2011 80 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 79 1 80
# uncoverable condition right count:1 class:ignore_covered_err 79 0 79
2012 160 ▶ if (resolved) sv_2mortal(resolved);
Branch: 1 branch
BranchTrueFalseTotal
if (resolved) sv_2mortal(resolved); 80 0 80
2013 80 ▶ c = (resolved && SvROK(resolved) && SvTYPE(SvRV(resolved)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
c = (resolved && SvROK(resolved) && SvTYPE(SvRV(resolved)) == SVt_PVHV) 79 1 80
2014 79 ? (HV *)SvRV(resolved) : NULL;
2015 79 if (c) {
2016 79 SV **vp = hv_fetchs(c, K_VIEWS, 0);
2017 79 SV **dp = hv_fetchs(c, "database", 0);
2018 79 SV **mp = hv_fetchs(c, K_MODELS, 0);
2019 79 SV **pp = hv_fetchs(c, "plugins", 0);
2020 79 SV **sp = hv_fetchs(c, "static", 0);
2021 SV **hp = hv_fetchs(c, "host", 0);
2022 SV **fvp = hv_fetchs(c, "favicon", 0);
2023 SSize_t i, n;
2024 79 ▶ 
Branch: 3 branches
BranchTrueFalseTotal
19 60 79
19 0 19
19 0 19
2025 19 /* views: engine => opts, in declaration order */
2026 19 # uncoverable condition right count:2 class:ignore_covered_err
2027 38 ▶ # uncoverable condition right count:3 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:3 class:ignore_covered_err 19 19 38
2028 19 if (vp && *vp && SvTRUE(*vp)) {
2029 19 AV *pairs = app_pairs(aTHX_ *vp, K_VIEWS);
2030 n = av_len(pairs) + 1;
2031 for (i = 0; i < n; i++) {
2032 SV **pr = av_fetch(pairs, i, 0);
2033 app_call_list(aTHX_ self, K_VIEWS, (AV *)SvRV(*pr));
2034 79 ▶ }
Branch: 3 branches
BranchTrueFalseTotal
} 5 74 79
} 5 0 5
} 5 0 5
2035 5 }
2036 5 ▶ 
Branch: 2 branches
BranchTrueFalseTotal
4 1 5
0 4 4
2037 1 /* database: one default block, or a mapping of named blocks */
2038 4 # uncoverable condition right count:2 class:ignore_covered_err
2039 4 # uncoverable condition right count:3 class:ignore_covered_err
2040 8 ▶ if (dp && *dp && SvTRUE(*dp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (dp && *dp && SvTRUE(*dp)) { 6 2 8
2041 6 HV *db; int named = 0; HE *e;
2042 6 ▶ if (!(SvROK(*dp) && SvTYPE(SvRV(*dp)) == SVt_PVHV)) /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(*dp) && SvTYPE(SvRV(*dp)) == SVt_PVHV)) /* # uncoverable condition left count:2 class:ignore_covered_err */ 2 4 6
if (!(SvROK(*dp) && SvTYPE(SvRV(*dp)) == SVt_PVHV)) /* # uncoverable condition left count:2 class:ignore_covered_err */ 2 0 2
2043 2 ▶ croak("Punk: the config database block must be a mapping");
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk: the config database block must be a mapping"); 2 0 2
2044 db = (HV *)SvRV(*dp);
2045 4 ▶ hv_iterinit(db);
Branch: 1 branch
BranchTrueFalseTotal
hv_iterinit(db); 2 2 4
2046 2 while ((e = hv_iternext(db))) {
2047 2 SV *v = hv_iterval(db, e);
2048 6 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 4 2 6
2049 4 # uncoverable condition right count:3 class:ignore_covered_err
2050 4 if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV
2051 4 && !SvOBJECT(SvRV(v))) { named = 1; break; }
2052 4 ▶ }
Branch: 2 branches
BranchTrueFalseTotal
} 4 0 4
} 3 1 4
2053 8 ▶ if (named) {
Branch: 1 branch
BranchTrueFalseTotal
if (named) { 4 0 4
2054 6 ▶ AV *keys = app_sorted_keys(aTHX_ db);
Branch: 2 branches
BranchTrueFalseTotal
AV *keys = app_sorted_keys(aTHX_ db); 2 2 4
AV *keys = app_sorted_keys(aTHX_ db); 2 0 2
2055 2 n = av_len(keys) + 1;
2056 2 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 1 1 2
2057 1 SV **kp = av_fetch(keys, i, 0);
2058 2 ▶ STRLEN kl; const char *k = SvPV_const(*kp, kl);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl; const char *k = SvPV_const(*kp, kl); 1 1 2
2059 1 SV **vp2 = hv_fetch(db, k, (I32)kl, 0);
2060 1 HV *opt = (vp2 && *vp2 && SvROK(*vp2)) ?
2061 (HV *)SvRV(*vp2) : NULL;
2062 if (kl == 7 && memEQ(k, K_DEFAULT, 7)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
2063 2 AV *args = (AV *)sv_2mortal((SV *)newAV());
2064 if (opt) {
2065 HE *oe; hv_iterinit(opt);
2066 2 while ((oe = hv_iternext(opt))) {
2067 2 av_push(args, newSVsv(hv_iterkeysv(oe)));
2068 2 av_push(args, newSVsv(hv_iterval(opt, oe)));
2069 2 }
2070 }
2071 app_call_list(aTHX_ self, "database", args);
2072 }
2073 else {
2074 2 AV *args = (AV *)sv_2mortal((SV *)newAV());
2075 2 av_push(args, newSVsv(*kp));
2076 5 ▶ av_push(args, newSVsv(*vp2));
Branch: 1 branch
BranchTrueFalseTotal
av_push(args, newSVsv(*vp2)); 3 2 5
2077 3 app_call_list(aTHX_ self, "database", args);
2078 3 }
2079 }
2080 2 }
2081 else {
2082 AV *args = (AV *)sv_2mortal((SV *)newAV());
2083 HE *oe; hv_iterinit(db);
2084 while ((oe = hv_iternext(db))) {
2085 78 ▶ av_push(args, newSVsv(hv_iterkeysv(oe)));
Branch: 3 branches
BranchTrueFalseTotal
av_push(args, newSVsv(hv_iterkeysv(oe))); 10 68 78
av_push(args, newSVsv(hv_iterkeysv(oe))); 10 0 10
av_push(args, newSVsv(hv_iterkeysv(oe))); 10 0 10
2086 10 av_push(args, newSVsv(hv_iterval(db, oe)));
2087 10 ▶ }
Branch: 2 branches
BranchTrueFalseTotal
} 6 4 10
} 5 1 6
2088 9 ▶ app_call_list(aTHX_ self, "database", args);
Branch: 1 branch
BranchTrueFalseTotal
app_call_list(aTHX_ self, "database", args); 5 0 5
2089 5 }
2090 5 }
2091 5  
2092 5 ▶ /* models: auto | none | [names] | { auto=>?, only=>[names] } */
Branch: 1 branch
BranchTrueFalseTotal
/* models: auto | none | [names] | { auto=>?, only=>[names] } */ 2 3 5
2093 2 # uncoverable condition right count:2 class:ignore_covered_err
2094 # uncoverable condition right count:3 class:ignore_covered_err
2095 5 ▶ if (mp && *mp && SvOK(*mp)) {
Branch: 3 branches
BranchTrueFalseTotal
if (mp && *mp && SvOK(*mp)) { 4 1 5
if (mp && *mp && SvOK(*mp)) { 4 0 4
if (mp && *mp && SvOK(*mp)) { 4 0 4
2096 4 SV *m = *mp;
2097 if (SvROK(m) && SvTYPE(SvRV(m)) == SVt_PVHV /* # uncoverable condition right count:3 class:ignore_covered_err */
2098 && !SvOBJECT(SvRV(m))) {
2099 5 ▶ HV *mh = (HV *)SvRV(m);
Branch: 1 branch
BranchTrueFalseTotal
HV *mh = (HV *)SvRV(m); 4 1 5
2100 4 SV **au = hv_fetchs(mh, K_AUTO, 0);
2101 4 ▶ SV **only = hv_fetchs(mh, K_ONLY, 0);
Branch: 2 branches
BranchTrueFalseTotal
SV **only = hv_fetchs(mh, K_ONLY, 0); 4 0 4
SV **only = hv_fetchs(mh, K_ONLY, 0); 1 3 4
2102 1 if (au)
2103 3 ▶ (void)hv_stores(h, K_MODEL_AUTO,
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_stores(h, K_MODEL_AUTO, 3 0 3
(void)hv_stores(h, K_MODEL_AUTO, 2 1 3
2104 2 newSViv(SvTRUE(*au) ? 1 : 0));
2105 # uncoverable branch false class:ignore_covered_err
2106 1 # uncoverable condition right count:2 class:ignore_covered_err
2107 # uncoverable condition right count:3 class:ignore_covered_err
2108 if (only && *only && SvTRUE(*only))
2109 app_call_list(aTHX_ self, "model_class",
2110 1 app_list(aTHX_ *only, "models.only"));
2111 }
2112 else if (!SvROK(m)) {
2113 STRLEN ml; const char *ms = SvPV_const(m, ml);
2114 if (ml == 4 && strncasecmp(ms, K_AUTO, 4) == 0) /* # uncoverable condition right count:1 class:ignore_covered_err */
2115 (void)hv_stores(h, K_MODEL_AUTO, newSViv(1));
2116 else if (ml == 4 && strncasecmp(ms, "none", 4) == 0) /* # uncoverable condition right count:1 class:ignore_covered_err */
2117 (void)hv_stores(h, K_MODEL_AUTO, newSViv(0));
2118 77 ▶ else
Branch: 3 branches
BranchTrueFalseTotal
else 13 64 77
else 13 0 13
else 13 0 13
2119 13 app_call_list(aTHX_ self, "model_class",
2120 18 ▶ app_list(aTHX_ m, K_MODELS));
Branch: 2 branches
BranchTrueFalseTotal
app_list(aTHX_ m, K_MODELS)); 9 4 13
app_list(aTHX_ m, K_MODELS)); 8 1 9
2121 8 }
2122 8 else {
2123 8 app_call_list(aTHX_ self, "model_class",
2124 8 ▶ app_list(aTHX_ m, K_MODELS));
Branch: 4 branches
BranchTrueFalseTotal
app_list(aTHX_ m, K_MODELS)); 6 2 8
app_list(aTHX_ m, K_MODELS)); 6 0 6
app_list(aTHX_ m, K_MODELS)); 6 0 6
app_list(aTHX_ m, K_MODELS)); 5 1 6
2125 5 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 0 5 5
2126 3 }
2127 5  
2128 5 ▶ /* host: one origin, or a mapping of origin + allow. Applied
Branch: 3 branches
BranchTrueFalseTotal
/* host: one origin, or a mapping of origin + allow. Applied 4 1 5
/* host: one origin, or a mapping of origin + allow. Applied 4 0 4
/* host: one origin, or a mapping of origin + allow. Applied 4 0 4
2129 4 * before plugins so a plugin registered from the same file
2130 4 ▶ * already sees it. */
Branch: 1 branch
BranchTrueFalseTotal
* already sees it. */ 2 2 4
2131 # uncoverable condition right count:2 class:ignore_covered_err
2132 2 # uncoverable condition right count:3 class:ignore_covered_err
2133 2 if (hp && *hp && SvOK(*hp)) {
2134 2 AV *args = (AV *)sv_2mortal((SV *)newAV());
2135 if (SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVHV) {
2136 HV *hh = (HV *)SvRV(*hp);
2137 SV **org = hv_fetchs(hh, "origin", 0);
2138 5 ▶ SV **al = hv_fetchs(hh, "allow", 0);
Branch: 1 branch
BranchTrueFalseTotal
SV **al = hv_fetchs(hh, "allow", 0); 4 1 5
2139 1 # uncoverable condition left count:2 class:ignore_covered_err
2140 9 # uncoverable condition left count:3 class:ignore_covered_err
2141 # uncoverable condition left count:5 class:ignore_covered_err
2142 if (!(org && *org && SvOK(*org) && !SvROK(*org)
2143 && SvCUR(*org)))
2144 73 ▶ croak("Punk: the config host block needs an origin");
Branch: 3 branches
BranchTrueFalseTotal
croak("Punk: the config host block needs an origin"); 8 65 73
croak("Punk: the config host block needs an origin"); 8 0 8
croak("Punk: the config host block needs an origin"); 8 0 8
2145 8 av_push(args, newSVsv(*org));
2146 11 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 6 2 8
# uncoverable condition right count:2 class:ignore_covered_err 5 1 6
2147 5 # uncoverable condition right count:3 class:ignore_covered_err
2148 5 if (al && *al && SvOK(*al)) {
2149 5 av_push(args, newSVpvs("allow"));
2150 5 ▶ if (SvROK(*al)) av_push(args, newSVsv(*al));
Branch: 4 branches
BranchTrueFalseTotal
if (SvROK(*al)) av_push(args, newSVsv(*al)); 3 2 5
if (SvROK(*al)) av_push(args, newSVsv(*al)); 3 0 3
if (SvROK(*al)) av_push(args, newSVsv(*al)); 3 0 3
if (SvROK(*al)) av_push(args, newSVsv(*al)); 0 3 3
2151 2 else { /* one host, written bare */
2152 3 AV *one = newAV();
2153 3 ▶ av_push(one, newSVsv(*al));
Branch: 3 branches
BranchTrueFalseTotal
av_push(one, newSVsv(*al)); 2 1 3
av_push(one, newSVsv(*al)); 2 0 2
av_push(one, newSVsv(*al)); 2 0 2
2154 2 av_push(args, newRV_noinc((SV *)one));
2155 2 }
2156 }
2157 }
2158 3 ▶ else if (!SvROK(*hp)) av_push(args, newSVsv(*hp));
Branch: 1 branch
BranchTrueFalseTotal
else if (!SvROK(*hp)) av_push(args, newSVsv(*hp)); 2 1 3
2159 1 else croak("Punk: the config host block needs an origin");
2160 5 app_call_list(aTHX_ self, "host", args);
2161 }
2162  
2163 /* favicon: a path, or a mapping of path + max_age */
2164 70 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 5 65 70
# uncoverable condition right count:2 class:ignore_covered_err 5 0 5
# uncoverable condition right count:2 class:ignore_covered_err 5 0 5
2165 5 # uncoverable condition right count:3 class:ignore_covered_err
2166 4 if (fvp && *fvp && SvOK(*fvp)) {
2167 10 ▶ AV *args = (AV *)sv_2mortal((SV *)newAV());
Branch: 1 branch
BranchTrueFalseTotal
AV *args = (AV *)sv_2mortal((SV *)newAV()); 6 4 10
2168 6 if (SvROK(*fvp) && SvTYPE(SvRV(*fvp)) == SVt_PVHV) {
2169 6 HV *fh = (HV *)SvRV(*fvp);
2170 SV **fpath = hv_fetchs(fh, "path", 0);
2171 SV **fma = hv_fetchs(fh, "max_age", 0);
2172 # uncoverable condition left count:2 class:ignore_covered_err
2173 # uncoverable condition left count:3 class:ignore_covered_err
2174 69 ▶ # uncoverable condition left count:4 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition left count:4 class:ignore_covered_err 19 50 69
# uncoverable condition left count:4 class:ignore_covered_err 19 0 19
# uncoverable condition left count:4 class:ignore_covered_err 19 0 19
2175 if (!(fpath && *fpath && SvOK(*fpath) && SvCUR(*fpath)))
2176 19 ▶ croak("Punk: the config favicon block needs a path");
Branch: 2 branches
BranchTrueFalseTotal
croak("Punk: the config favicon block needs a path"); 18 1 19
croak("Punk: the config favicon block needs a path"); 0 18 18
2177 1 av_push(args, newSVsv(*fpath));
2178 # uncoverable condition right count:2 class:ignore_covered_err
2179 18 # uncoverable condition right count:3 class:ignore_covered_err
2180 18 if (fma && *fma && SvOK(*fma)) {
2181 18 av_push(args, newSVpvs("max_age"));
2182 36 ▶ av_push(args, newSVsv(*fma));
Branch: 1 branch
BranchTrueFalseTotal
av_push(args, newSVsv(*fma)); 18 18 36
2183 18 }
2184 18 }
2185 18 else if (!SvROK(*fvp)) av_push(args, newSVsv(*fvp));
2186 18 else croak("Punk: the config favicon block needs a path");
2187 18 app_call_list(aTHX_ self, "favicon", args);
2188 18 ▶ }
Branch: 2 branches
BranchTrueFalseTotal
} 18 0 18
} 18 0 18
2189 18  
2190 /* plugins: name => opts */
2191 # uncoverable condition right count:2 class:ignore_covered_err
2192 # uncoverable condition right count:3 class:ignore_covered_err
2193 if (pp && *pp && SvTRUE(*pp)) {
2194 AV *pairs = app_pairs(aTHX_ *pp, "plugins");
2195 68 n = av_len(pairs) + 1;
2196 68 ▶ for (i = 0; i < n; i++) {
Branch: 3 branches
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 3 65 68
for (i = 0; i < n; i++) { 3 0 3
for (i = 0; i < n; i++) { 3 0 3
2197 3 ▶ SV **pr = av_fetch(pairs, i, 0);
Branch: 1 branch
BranchTrueFalseTotal
SV **pr = av_fetch(pairs, i, 0); 3 0 3
2198 3 app_call_list(aTHX_ self, "plugin", (AV *)SvRV(*pr));
2199 3 }
2200 3 }
2201 6 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
3 3 6
2202 3 /* static: url prefix => directory */
2203 3 # uncoverable condition right count:2 class:ignore_covered_err
2204 # uncoverable condition right count:3 class:ignore_covered_err
2205 3 if (sp && *sp && SvTRUE(*sp)) {
2206 HV *st; AV *keys;
2207 if (!(SvROK(*sp) && SvTYPE(SvRV(*sp)) == SVt_PVHV)) /* # uncoverable condition left count:2 class:ignore_covered_err */
2208 croak("Punk: the config static block must be a mapping "
2209 "of url prefix => directory");
2210 st = (HV *)SvRV(*sp);
2211 keys = app_sorted_keys(aTHX_ st);
2212 n = av_len(keys) + 1;
2213 for (i = 0; i < n; i++) {
2214 SV **kp = av_fetch(keys, i, 0);
2215 68 STRLEN kl; const char *k = SvPV_const(*kp, kl);
2216 68 ▶ SV **dv = hv_fetch(st, k, (I32)kl, 0);
Branch: 4 branches
BranchTrueFalseTotal
SV **dv = hv_fetch(st, k, (I32)kl, 0); 2 66 68
SV **dv = hv_fetch(st, k, (I32)kl, 0); 2 0 2
SV **dv = hv_fetch(st, k, (I32)kl, 0); 2 0 2
SV **dv = hv_fetch(st, k, (I32)kl, 0); 2 0 2
2217 2 AV *args = (AV *)sv_2mortal((SV *)newAV());
2218 2 av_push(args, newSVsv(*kp));
2219 2 # uncoverable condition right count:1 class:ignore_covered_err
2220 2 # uncoverable condition right count:2 class:ignore_covered_err
2221 5 ▶ av_push(args, newSVsv((dv && *dv) ? *dv : &PL_sv_undef));
Branch: 1 branch
BranchTrueFalseTotal
av_push(args, newSVsv((dv && *dv) ? *dv : &PL_sv_undef)); 3 2 5
2222 3 app_call_list(aTHX_ self, "static", args);
2223 3 }
2224 3 }
2225 3 ▶ 
Branch: 2 branches
BranchTrueFalseTotal
2 1 3
2 0 2
2226 2 ▶ /* session: a mapping of options -> the session keyword */
Branch: 1 branch
BranchTrueFalseTotal
/* session: a mapping of options -> the session keyword */ 2 0 2
2227 2 {
2228 2 SV **sess = hv_fetchs(c, "session", 0);
2229 # uncoverable condition right count:2 class:ignore_covered_err
2230 # uncoverable condition right count:3 class:ignore_covered_err
2231 1 # uncoverable condition right count:4 class:ignore_covered_err
2232 1 if (sess && *sess && SvROK(*sess)
2233 && SvTYPE(SvRV(*sess)) == SVt_PVHV) {
2234 HV *sh = (HV *)SvRV(*sess); HE *e;
2235 2 ▶ AV *args = (AV *)sv_2mortal((SV *)newAV());
Branch: 1 branch
BranchTrueFalseTotal
AV *args = (AV *)sv_2mortal((SV *)newAV()); 1 1 2
2236 1 hv_iterinit(sh);
2237 while ((e = hv_iternext(sh))) {
2238 2 av_push(args, newSVsv(hv_iterkeysv(e)));
2239 4 ▶ av_push(args, newSVsv(hv_iterval(sh, e)));
Branch: 1 branch
BranchTrueFalseTotal
av_push(args, newSVsv(hv_iterval(sh, e))); 2 2 4
2240 2 }
2241 2 app_call_list(aTHX_ self, "session", args);
2242 2 }
2243 2 }
2244 2  
2245 2 /* ua: a mapping of options -> the ua keyword, so an outbound
2246 * timeout or a base header set is deployment configuration and
2247 * a token can arrive as { $env: ... } like any other secret.
2248 * A value that is itself a mapping names a second agent, the way
2249 * `databases` does; a scalar is an option on the default one. */
2250 {
2251 SV **u = hv_fetchs(c, K_UA, 0);
2252 # uncoverable condition right count:2 class:ignore_covered_err
2253 # uncoverable condition right count:3 class:ignore_covered_err
2254 68 # uncoverable condition right count:4 class:ignore_covered_err
2255 68 ▶ if (u && *u && SvROK(*u) && SvTYPE(SvRV(*u)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (u && *u && SvROK(*u) && SvTYPE(SvRV(*u)) == SVt_PVHV) { 6 62 68
if (u && *u && SvROK(*u) && SvTYPE(SvRV(*u)) == SVt_PVHV) { 6 0 6
if (u && *u && SvROK(*u) && SvTYPE(SvRV(*u)) == SVt_PVHV) { 5 1 6
2256 8 ▶ HV *uh = (HV *)SvRV(*u); HE *e;
Branch: 1 branch
BranchTrueFalseTotal
HV *uh = (HV *)SvRV(*u); HE *e; 3 2 5
2257 3 AV *dflt = (AV *)sv_2mortal((SV *)newAV());
2258 3 AV *named = (AV *)sv_2mortal((SV *)newAV());
2259 3 hv_iterinit(uh);
2260 5 ▶ while ((e = hv_iternext(uh))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hv_iternext(uh))) { 2 3 5
2261 2 SV *k = hv_iterkeysv(e);
2262 2 SV *v = hv_iterval(uh, e);
2263 STRLEN kl; const char *kp = SvPV_const(k, kl);
2264 3 # uncoverable condition right count:2 class:ignore_covered_err
2265 # uncoverable condition right count:3 class:ignore_covered_err
2266 65 ▶ if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV
Branch: 3 branches
BranchTrueFalseTotal
if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV 3 62 65
if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV 3 0 3
if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV 2 1 3
2267 2 && !pua_is_known_opt(aTHX_ kp, kl)) {
2268 2 av_push(named, newSVsv(k));
2269 av_push(named, newSVsv(v));
2270 }
2271 else {
2272 av_push(dflt, newSVsv(k));
2273 av_push(dflt, newSVsv(v));
2274 68 }
2275 68 ▶ }
Branch: 3 branches
BranchTrueFalseTotal
} 4 64 68
} 4 0 4
} 3 1 4
2276 4 ▶ if (av_len(dflt) >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(dflt) >= 0) 1 2 3
2277 1 app_call_list(aTHX_ self, "ua", dflt);
2278 1 {
2279 1 SSize_t j, nn = av_len(named) + 1;
2280 2 ▶ for (j = 0; j + 1 < nn; j += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j + 1 < nn; j += 2) { 1 1 2
2281 1 AV *one = (AV *)sv_2mortal((SV *)newAV());
2282 1 SV **kk = av_fetch(named, j, 0);
2283 SV **vv = av_fetch(named, j + 1, 0);
2284 1 av_push(one, newSVsv(*kk));
2285 av_push(one, newSVsv(*vv));
2286 67 ▶ app_call_list(aTHX_ self, "ua", one);
Branch: 3 branches
BranchTrueFalseTotal
app_call_list(aTHX_ self, "ua", one); 3 64 67
app_call_list(aTHX_ self, "ua", one); 3 0 3
app_call_list(aTHX_ self, "ua", one); 2 1 3
2287 2 }
2288 2 }
2289 }
2290 }
2291  
2292 /* csrf: a mapping of options -> the csrf keyword. `csrf: false`
2293 * (or absent) leaves it off. */
2294 {
2295 68 SV **cs = hv_fetchs(c, K_CSRF, 0);
2296 68 ▶ if (cs && *cs && SvROK(*cs) /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (cs && *cs && SvROK(*cs) /* # uncoverable condition right count:2 class:ignore_covered_err */ 5 63 68
if (cs && *cs && SvROK(*cs) /* # uncoverable condition right count:2 class:ignore_covered_err */ 5 0 5
if (cs && *cs && SvROK(*cs) /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 1 5
2297 6 ▶ && SvTYPE(SvRV(*cs)) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(*cs)) == SVt_PVHV) { 2 2 4
2298 2 HV *ch = (HV *)SvRV(*cs); HE *e;
2299 2 AV *args = (AV *)sv_2mortal((SV *)newAV());
2300 2 hv_iterinit(ch);
2301 5 ▶ while ((e = hv_iternext(ch))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hv_iternext(ch))) { 3 2 5
2302 3 av_push(args, newSVsv(hv_iterkeysv(e)));
2303 3 av_push(args, newSVsv(hv_iterval(ch, e)));
2304 }
2305 2 app_call_list(aTHX_ self, K_CSRF, args);
2306 }
2307 66 ▶ else if (cs && *cs && app_cfg_on(aTHX_ *cs)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
else if (cs && *cs && app_cfg_on(aTHX_ *cs)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 3 63 66
else if (cs && *cs && app_cfg_on(aTHX_ *cs)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 3 0 3
else if (cs && *cs && app_cfg_on(aTHX_ *cs)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 1 3
2308 2 app_call_list(aTHX_ self, K_CSRF,
2309 2 (AV *)sv_2mortal((SV *)newAV()));
2310 }
2311 }
2312  
2313 /* cors: a mapping of options, or `cors: true` for the bare form */
2314 {
2315 68 SV **co = hv_fetchs(c, K_CORS, 0);
2316 68 ▶ if (co && *co && SvROK(*co) /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (co && *co && SvROK(*co) /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 67 68
if (co && *co && SvROK(*co) /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
if (co && *co && SvROK(*co) /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
2317 1 ▶ && SvTYPE(SvRV(*co)) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(*co)) == SVt_PVHV) { 1 0 1
2318 1 HV *oh = (HV *)SvRV(*co); HE *e;
2319 1 AV *args = (AV *)sv_2mortal((SV *)newAV());
2320 1 hv_iterinit(oh);
2321 2 ▶ while ((e = hv_iternext(oh))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hv_iternext(oh))) { 1 1 2
2322 1 av_push(args, newSVsv(hv_iterkeysv(e)));
2323 1 av_push(args, newSVsv(hv_iterval(oh, e)));
2324 }
2325 1 app_call_list(aTHX_ self, K_CORS, args);
2326 }
2327 else if (co && *co && app_cfg_on(aTHX_ *co)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
2328 app_call_list(aTHX_ self, K_CORS,
2329 (AV *)sv_2mortal((SV *)newAV()));
2330 }
2331 68 }
2332 68 ▶ 
Branch: 4 branches
BranchTrueFalseTotal
1 67 68
1 0 1
1 0 1
1 0 1
2333 1 /* headers: a mapping of header names, or `headers: true` for the
2334 1 * bare default set */
2335 1 {
2336 2 ▶ SV **hd = hv_fetchs(c, K_HEADERS, 0);
Branch: 1 branch
BranchTrueFalseTotal
SV **hd = hv_fetchs(c, K_HEADERS, 0); 1 1 2
2337 1 if (hd && *hd && SvROK(*hd) /* # uncoverable condition right count:2 class:ignore_covered_err */
2338 1 && SvTYPE(SvRV(*hd)) == SVt_PVHV) {
2339 HV *oh = (HV *)SvRV(*hd); HE *e;
2340 1 AV *args = (AV *)sv_2mortal((SV *)newAV());
2341 hv_iterinit(oh);
2342 while ((e = hv_iternext(oh))) {
2343 av_push(args, newSVsv(hv_iterkeysv(e)));
2344 69 av_push(args, newSVsv(hv_iterval(oh, e)));
2345 }
2346 app_call_list(aTHX_ self, K_HEADERS, args);
2347 }
2348 else if (hd && *hd && app_cfg_on(aTHX_ *hd)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
2349 app_call_list(aTHX_ self, K_HEADERS,
2350 (AV *)sv_2mortal((SV *)newAV()));
2351 }
2352 }
2353  
2354 /* auth: a mapping of options -> the auth keyword */
2355 {
2356 SV **at = hv_fetchs(c, K_AUTH, 0);
2357 # uncoverable condition right count:2 class:ignore_covered_err
2358 # uncoverable condition right count:3 class:ignore_covered_err
2359 # uncoverable condition right count:4 class:ignore_covered_err
2360 if (at && *at && SvROK(*at)
2361 && SvTYPE(SvRV(*at)) == SVt_PVHV) {
2362 HV *oh = (HV *)SvRV(*at); HE *e;
2363 AV *args = (AV *)sv_2mortal((SV *)newAV());
2364 hv_iterinit(oh);
2365 while ((e = hv_iternext(oh))) {
2366 av_push(args, newSVsv(hv_iterkeysv(e)));
2367 av_push(args, newSVsv(hv_iterval(oh, e)));
2368 }
2369 app_call_list(aTHX_ self, K_AUTH, args);
2370 }
2371 }
2372  
2373 /* logging: a mapping of options -> the logging keyword */
2374 {
2375 SV **lgc = hv_fetchs(c, "logging", 0);
2376 # uncoverable condition right count:2 class:ignore_covered_err
2377 # uncoverable condition right count:3 class:ignore_covered_err
2378 # uncoverable condition right count:4 class:ignore_covered_err
2379 if (lgc && *lgc && SvROK(*lgc) && SvTYPE(SvRV(*lgc)) == SVt_PVHV) {
2380 HV *gh = (HV *)SvRV(*lgc); HE *e;
2381 AV *args = (AV *)sv_2mortal((SV *)newAV());
2382 hv_iterinit(gh);
2383 while ((e = hv_iternext(gh))) {
2384 av_push(args, newSVsv(hv_iterkeysv(e)));
2385 av_push(args, newSVsv(hv_iterval(gh, e)));
2386 }
2387 app_call_list(aTHX_ self, "logging", args);
2388 }
2389 }
2390 }
2391 RETVAL = newSVsv(self);
2392 }
2393 OUTPUT:
2394 RETVAL
2395  
2396 # cache - the store, or stores, this application uses.
2397 #
2398 # cache 'file', dir => '/var/cache/app'; # the default store
2399 # cache sessions => { backend => 'memory', max_bytes => '64M' };
2400 #
2401 # A NAME with a hashref declares a named store, exactly as `ua` and `views`
2402 # do; anything else configures the default one. Named stores matter because a
2403 # session cache and a rendered-page cache want different budgets, different
2404 # backends and different lifetimes, and sharing one store means the big cold
2405 # thing evicts the small hot thing.
2406 #
2407 # Recorded here and built at to_app, where a bad backend name, an unparseable
2408 # max_bytes or an unwritable directory croak at BOOT. A cache that fails on
2409 # its first miss fails at three in the morning; one that fails at boot fails
2410 # in front of whoever deployed it.
2411 SV *
2412 cache(self, ...)
2413 SV *self
2414 CODE:
2415 {
2416 72 HV *h = app_hv(aTHX_ self);
2417 72 HV *stores = app_hash(aTHX_ h, "cache_spec");
2418 72 AV *rec = newAV();
2419 72 HV *opt = newHV();
2420 72 const char *name = K_DEFAULT;
2421 72 STRLEN nlen = sizeof(K_DEFAULT) - 1;
2422 72 int i, first = 1;
2423  
2424 72 ▶ if (items >= 3 && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (items >= 3 && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 68 4 72
if (items >= 3 && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 7 61 68
if (items >= 3 && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVHV) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 7 0 7
2425 /* cache $name => \%opts */
2426 7 HV *given = (HV *)SvRV(ST(2));
2427 HE *e;
2428 7 name = SvPV_const(ST(1), nlen);
2429 7 hv_iterinit(given);
2430 23 ▶ while ((e = hv_iternext(given)))
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hv_iternext(given))) 16 7 23
2431 16 (void)hv_store_ent(opt, hv_iterkeysv(e), newSVsv(HeVAL(e)), 0);
2432 7 first = items; /* nothing left to scan */
2433 }
2434 65 ▶ # uncoverable branch false
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false 65 0 65
2435 # uncoverable condition right count:1 class:ignore_covered_err
2436 else if (items >= 2) {
2437 /* The backend: a name, or a ready-made object. Taking a ref here
2438 * as well matters - the earlier form ignored one, so `cache
2439 65 * $store` recorded no backend at all and quietly built a file
2440 65 * cache instead of the object it was handed. */
2441 av_push(rec, newSVsv(ST(1)));
2442 first = 2;
2443 161 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 89 72 161
2444  
2445 89 for (i = first; i + 1 < items; i += 2) {
2446 89 STRLEN kl;
2447 const char *k = SvPV_const(ST(i), kl);
2448 (void)hv_store(opt, k, (I32)kl, newSVsv(ST(i + 1)), 0);
2449 72 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 7 65 72
2450  
2451 7 if (av_len(rec) < 0) { /* empty: av_len is -1, not 0 */
2452 7 ▶ /* a named store names its backend inside the hashref */
Branch: 3 branches
BranchTrueFalseTotal
/* a named store names its backend inside the hashref */ 7 0 7
/* a named store names its backend inside the hashref */ 7 0 7
/* a named store names its backend inside the hashref */ 7 0 7
2453 SV **b = hv_fetchs(opt, "backend", 0);
2454 # uncoverable condition right count:1 class:ignore_covered_err
2455 72 # uncoverable condition right count:2 class:ignore_covered_err
2456 72 # uncoverable condition right count:3 class:ignore_covered_err
2457 72 av_push(rec, (b && *b && SvOK(*b)) ? newSVsv(*b)
2458 : newSVpvs("file"));
2459 }
2460 av_push(rec, newRV_noinc((SV *)opt));
2461 (void)hv_store(stores, name, (I32)nlen, newRV_noinc((SV *)rec), 0);
2462 RETVAL = newSVsv(self);
2463 }
2464 OUTPUT:
2465 RETVAL
2466  
2467 # upload_dir($path): where a large multipart part is spilled while the request
2468 # runs.
2469 #
2470 # Worth naming rather than defaulting, for two reasons that are not obvious:
2471 # it decides the FILESYSTEM, which decides whether Punk::Upload::save is a
2472 # rename or another whole copy of a large file; and it decides what shares a
2473 # filesystem with attacker-controlled bytes.
2474 SV *
2475 upload_dir(self, path)
2476 SV *self
2477 SV *path
2478 CODE:
2479 {
2480 6 HV *h = app_hv(aTHX_ self);
2481 6 ▶ if (!SvOK(path) || !SvCUR(path)) /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(path) || !SvCUR(path)) /* # uncoverable condition left count:1 class:ignore_covered_err */ 6 0 6
if (!SvOK(path) || !SvCUR(path)) /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 5 6
2482 1 croak("Punk: upload_dir needs a directory");
2483 5 (void)hv_stores(h, "upload_dir", newSVsv(path));
2484 5 RETVAL = newSVsv(self);
2485 }
2486 OUTPUT:
2487 RETVAL
2488  
2489 # host($origin?, allow => \@hosts?): the application's canonical origin,
2490 # declared once.
2491 #
2492 # Plugins that need an absolute base URL - a sitemap, an OAuth redirect -
2493 # default to this when their own option is not passed, so one fact about the
2494 # application is stated in one place. It is CONFIGURATION, never the request's
2495 # Host header: a request-derived origin is attacker-supplied bytes, which is
2496 # the whole reason those plugins refuse to guess.
2497 #
2498 # `allow` is the multi-tenant half: the request hosts that may stand in for
2499 # the canonical one, so $c->origin can name a tenant without ever reflecting
2500 # a header nobody configured (punk_host.h). Entries are lowercased and
2501 # checked at the keyword - a typo fails here rather than never matching.
2502 #
2503 # With no argument it reads back the stored value, so a plugin reaches it as
2504 # $app->host and an older Punk is detected with $app->can('host').
2505 SV *
2506 host(self, ...)
2507 SV *self
2508 CODE:
2509 {
2510 59 HV *h = app_hv(aTHX_ self);
2511 59 ▶ if (items == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 1) { 8 51 59
2512 8 SV *v = app_get(aTHX_ h, "host");
2513 8 ▶ RETVAL = (v && SvOK(v)) ? newSVsv(v) : newSV(0); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (v && SvOK(v)) ? newSVsv(v) : newSV(0); /* # uncoverable condition right count:2 class:ignore_covered_err */ 7 1 8
RETVAL = (v && SvOK(v)) ? newSVsv(v) : newSV(0); /* # uncoverable condition right count:2 class:ignore_covered_err */ 7 0 7
2514 }
2515 else {
2516 51 SV *val = ST(1);
2517 51 STRLEN vl, scheme = 0, i;
2518 const char *vp;
2519 SSize_t oi;
2520 51 ▶ if (!SvOK(val) || !SvCUR(val)) /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(val) || !SvCUR(val)) /* # uncoverable condition left count:1 class:ignore_covered_err */ 51 0 51
if (!SvOK(val) || !SvCUR(val)) /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 50 51
2521 1 croak("Punk: host needs an absolute origin "
2522 "(https://example.com)");
2523 50 vp = SvPV_const(val, vl);
2524 50 ▶ if (vl > 8 && memEQ(vp, "https://", 8)) scheme = 8;
Branch: 2 branches
BranchTrueFalseTotal
if (vl > 8 && memEQ(vp, "https://", 8)) scheme = 8; 49 1 50
if (vl > 8 && memEQ(vp, "https://", 8)) scheme = 8; 48 1 49
2525 2 ▶ # uncoverable branch true
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch true 2 0 2
# uncoverable branch true 0 2 2
2526 50 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 48 2 50
# uncoverable condition left count:1 class:ignore_covered_err 0 48 48
2527 2 # uncoverable condition right count:1 class:ignore_covered_err
2528 # uncoverable condition left count:2 class:ignore_covered_err
2529 else if (vl > 7 && memEQ(vp, "http://", 7)) scheme = 7;
2530 if (!scheme || vp[scheme] == '/') /* # uncoverable condition left count:2 class:ignore_covered_err */
2531 croak("Punk: host needs an absolute origin "
2532 "(https://example.com), not '%.*s'", (int)vl, vp);
2533 981 ▶ /* A query, a fragment, whitespace or a control byte in an origin
Branch: 1 branch
BranchTrueFalseTotal
/* A query, a fragment, whitespace or a control byte in an origin 937 44 981
2534 937 * is always a mistake, and this value is joined onto by code that
2535 937 ▶ * must be able to trust it. A backslash is refused for the same
Branch: 3 branches
BranchTrueFalseTotal
* must be able to trust it. A backslash is refused for the same 936 1 937
* must be able to trust it. A backslash is refused for the same 935 1 936
* must be able to trust it. A backslash is refused for the same 934 1 935
2536 934 ▶ * reason $c->safe_path refuses one: browsers fold it to a slash. */
Branch: 2 branches
BranchTrueFalseTotal
* reason $c->safe_path refuses one: browsers fold it to a slash. */ 933 1 934
* reason $c->safe_path refuses one: browsers fold it to a slash. */ 0 933 933
2537 4 for (i = 0; i < vl; i++) {
2538 const unsigned char ch = (unsigned char)vp[i];
2539 if (ch == '?' || ch == '#' || ch == '\\' /* # uncoverable condition left count:5 class:ignore_covered_err */
2540 44 ▶ || ch <= 0x20 || ch == 0x7f)
Branch: 1 branch
BranchTrueFalseTotal
|| ch <= 0x20 || ch == 0x7f) 1 43 44
2541 1 croak("Punk: host may not carry '%c' - an origin has no "
2542 "query, fragment or whitespace", (int)ch);
2543 }
2544 43 if ((items - 2) % 2)
2545 52 ▶ croak("Punk: host takes key => value options");
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk: host takes key => value options"); 15 37 52
2546 /* last write wins wholly: a second declaration without `allow`
2547 15 * has no allowlist */
2548 15 (void)hv_delete(h, "host_allow", 10, G_DISCARD);
2549 24 ▶ for (oi = 2; oi + 1 < items; oi += 2) {
Branch: 2 branches
BranchTrueFalseTotal
for (oi = 2; oi + 1 < items; oi += 2) { 14 1 15
for (oi = 2; oi + 1 < items; oi += 2) { 14 0 14
2550 14 STRLEN kl;
2551 9 const char *k = SvPV_const(ST(oi), kl);
2552 SV *v = ST(oi + 1);
2553 1 if (kl == 5 && memEQ(k, "allow", 5)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
2554 AV *out = pk_host_allow_av(aTHX_ v, "host allow");
2555 (void)hv_stores(h, "host_allow", newRV_noinc((SV *)out));
2556 }
2557 else croak("Punk: host does not understand '%.*s'",
2558 44 ▶ (int)kl, k);
Branch: 2 branches
BranchTrueFalseTotal
(int)kl, k); 44 0 44
(int)kl, k); 7 37 44
2559 37 }
2560 37 /* trailing slashes trimmed on store, so every consumer joining a
2561 * rooted path onto it produces one slash, not two */
2562 while (vl > scheme && vp[vl - 1] == '/') vl--; /* # uncoverable condition right count:1 class:ignore_covered_err */
2563 (void)hv_stores(h, "host", newSVpvn(vp, vl));
2564 RETVAL = newSVsv(self);
2565 }
2566 }
2567 OUTPUT:
2568 RETVAL
2569  
2570 # $app->url_for($name, %args): the URL of a named route, with NO request.
2571 #
2572 # The context method is the one an application uses; this is for the callers
2573 # that have no context to reach it through - a mail built in a queue worker,
2574 # a test naming a route instead of typing its path. Same names, same
2575 # captures, same croaks.
2576 #
2577 # Two differences follow from there being no request, and both are the safe
2578 # direction: the prefix is the path on `host` alone (SCRIPT_NAME belongs to a
2579 # request, and there is not one), and `absolute` builds on the DECLARED host
2580 # rather than negotiating the allowlist - a job has no Host header to be
2581 # allowed or refused.
2582 SV *
2583 url_for(self, name, ...)
2584 SV *self
2585 SV *name
2586 CODE:
2587 {
2588 11 HV *h = app_hv(aTHX_ self);
2589 11 HV *args, *query = NULL;
2590 11 SV *val, *prefix, *origin = NULL, *host;
2591 11 IV absolute = 0;
2592 int i;
2593  
2594 11 ▶ if ((items - 2) % 2)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 2) % 2) 1 10 11
2595 1 croak("Punk: url_for takes a route name then key => value pairs");
2596 10 ▶ if (!SvOK(name) || !SvCUR(name)) /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(name) || !SvCUR(name)) /* # uncoverable condition left count:1 class:ignore_covered_err */ 10 0 10
if (!SvOK(name) || !SvCUR(name)) /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 9 10
2597 1 croak("Punk: url_for needs a route name");
2598  
2599 9 args = (HV *)sv_2mortal((SV *)newHV());
2600 17 ▶ for (i = 2; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i + 1 < items; i += 2) { 9 8 17
2601 STRLEN kl;
2602 9 const char *k = SvPV_const(ST(i), kl);
2603 9 ▶ if (kl == 8 && memEQ(k, "absolute", 8)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 8 && memEQ(k, "absolute", 8)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 7 9
if (kl == 8 && memEQ(k, "absolute", 8)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 0 2
2604 2 absolute = SvTRUE(ST(i + 1)) ? 1 : 0;
2605 3 continue;
2606 }
2607 7 ▶ if (kl == 5 && memEQ(k, "query", 5)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 5 && memEQ(k, "query", 5)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 5 7
if (kl == 5 && memEQ(k, "query", 5)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 0 2
2608 2 SV *q = ST(i + 1);
2609 2 ▶ if (!(SvROK(q) && SvTYPE(SvRV(q)) == SVt_PVHV)) /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(q) && SvTYPE(SvRV(q)) == SVt_PVHV)) /* # uncoverable condition left count:2 class:ignore_covered_err */ 1 1 2
if (!(SvROK(q) && SvTYPE(SvRV(q)) == SVt_PVHV)) /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 1 1
2610 1 croak("Punk: url_for('%s'): `query` takes a hashref",
2611 SvPV_nolen(name));
2612 1 query = (HV *)SvRV(q);
2613 1 continue;
2614 }
2615 5 (void)hv_store(args, k, (I32)kl, newSVsv(ST(i + 1)), 0);
2616 }
2617  
2618 8 val = pk_url_target(aTHX_ h, name);
2619 8 ▶ if (!val)
Branch: 1 branch
BranchTrueFalseTotal
if (!val) 2 6 8
2620 2 croak("Punk: url_for: no route is named '%s' - a name is "
2621 "declared with { name => '...' } on the route, and it is "
2622 "one namespace for the whole application",
2623 SvPV_nolen(name));
2624  
2625 6 prefix = pk_url_prefix(aTHX_ h, NULL);
2626 6 host = app_get(aTHX_ h, "host");
2627 6 ▶ if (absolute) {
Branch: 1 branch
BranchTrueFalseTotal
if (absolute) { 1 5 6
2628 STRLEN hl, hoff, hlen;
2629 const char *hp;
2630 1 ▶ # uncoverable branch true
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable branch true 1 0 1
# uncoverable branch true 1 0 1
# uncoverable branch true 0 1 1
2631 0 # uncoverable condition left count:1 class:ignore_covered_err
2632 # uncoverable condition left count:2 class:ignore_covered_err
2633 # uncoverable condition left count:3 class:ignore_covered_err
2634 1 if (!(host && SvOK(host) && SvCUR(host)))
2635 1 croak("Punk: url_for('%s', absolute => 1): the application " /* # uncoverable statement */
2636 "declared no `host` - declare "
2637 "host 'https://example.com'", SvPV_nolen(name)); /* # uncoverable statement */
2638 6 ▶ hp = SvPV_const(host, hl);
Branch: 2 branches
BranchTrueFalseTotal
hp = SvPV_const(host, hl); 0 6 6
hp = SvPV_const(host, hl); 0 0 0
2639 0 origin = sv_2mortal(pk_origin_bare(aTHX_ hp, hl, &hoff, &hlen));
2640 0 }
2641 0  
2642 0 # uncoverable branch true
2643 0 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 0 0 0
2644 0 ▶ # uncoverable condition left count:2 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:2 class:ignore_covered_err 0 0 0
2645 # uncoverable condition right count:2 class:ignore_covered_err
2646 if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV) {
2647 6 AV *pair = (AV *)SvRV(val); /* # uncoverable statement */
2648 6 ▶ SV **mp = av_fetch(pair, 0, 0); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
SV **mp = av_fetch(pair, 0, 0); /* # uncoverable statement */ 0 6 6
2649 0 SV **tm = av_fetch(pair, 1, 0); /* # uncoverable statement */
2650 6 RETVAL = pk_url_build_op(aTHX_ (AV *)SvRV(*tm), /* # uncoverable statement */
2651 6 (mp && *mp) ? *mp : NULL, args, query, prefix, /* # uncoverable statement */
2652 origin, SvPV_nolen(name)); /* # uncoverable statement */
2653 }
2654 else {
2655 pr_router *rt = pk_url_router(aTHX_ h);
2656 # uncoverable branch true
2657 # uncoverable condition left count:1 class:ignore_covered_err
2658 if (!rt)
2659 croak("Punk: url_for: this application is not compiled"); /* # uncoverable statement */
2660 RETVAL = pk_url_build(aTHX_ rt, SvIV(val), args, query,
2661 prefix, origin, SvPV_nolen(name));
2662 }
2663 }
2664 OUTPUT:
2665 RETVAL
2666  
2667 # favicon($path, max_age => $secs?): GET /favicon.ico from this file.
2668 #
2669 # The route is registered here, once; the bytes are frozen at to_app
2670 # (pfav_freeze, from the compiler), where a missing file croaks at boot. The
2671 # route opts itself out of the sitemap - an icon is not a page.
2672 SV *
2673 favicon(self, path, ...)
2674 SV *self
2675 SV *path
2676 CODE:
2677 {
2678 16 HV *h = app_hv(aTHX_ self);
2679 16 int had = app_get(aTHX_ h, "favicon_path") != NULL;
2680 SSize_t i;
2681 16 ▶ if (!SvOK(path) || !SvCUR(path)) /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(path) || !SvCUR(path)) /* # uncoverable condition left count:1 class:ignore_covered_err */ 16 0 16
if (!SvOK(path) || !SvCUR(path)) /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 15 16
2682 1 croak("Punk: favicon needs a file path");
2683 15 ▶ if ((items - 2) % 2)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 2) % 2) 1 14 15
2684 1 croak("Punk: favicon takes key => value options");
2685 14 (void)hv_delete(h, "favicon_max_age", 15, G_DISCARD);
2686 18 ▶ for (i = 2; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i + 1 < items; i += 2) { 6 12 18
2687 STRLEN kl;
2688 6 const char *k = SvPV_const(ST(i), kl);
2689 6 SV *v = ST(i + 1);
2690 6 ▶ if (kl == 7 && memEQ(k, "max_age", 7)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 7 && memEQ(k, "max_age", 7)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 5 1 6
if (kl == 7 && memEQ(k, "max_age", 7)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 5 0 5
2691 5 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 5 0 5
# uncoverable condition left count:1 class:ignore_covered_err 5 0 5
# uncoverable condition left count:1 class:ignore_covered_err 1 4 5
2692 1 # uncoverable condition left count:2 class:ignore_covered_err
2693 if (!SvOK(v) || !looks_like_number(v) || SvIV(v) < 0)
2694 4 croak("Punk: favicon max_age needs a non-negative "
2695 "number of seconds");
2696 1 (void)hv_stores(h, "favicon_max_age", newSViv(SvIV(v)));
2697 }
2698 else croak("Punk: favicon does not understand '%.*s'",
2699 12 (int)kl, k);
2700 12 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 10 2 12
2701 (void)hv_stores(h, "favicon_path", newSVsv(path));
2702 10 if (!had) {
2703 10 SV *argv[5], *r;
2704 10 AV *cap = newAV();
2705 10 av_push(cap, newSVsv(self));
2706 10 argv[0] = sv_2mortal(newSVpvs("GET"));
2707 10 argv[1] = sv_2mortal(newSVpvs("/favicon.ico"));
2708 argv[2] = sv_2mortal(punk_closure(aTHX_ pfav_serve_cb, cap));
2709 10 argv[3] = &PL_sv_undef;
2710 10 {
2711 10 HV *o = newHV();
2712 (void)hv_stores(o, K_SITEMAP, newSViv(0));
2713 10 argv[4] = sv_2mortal(newRV_noinc((SV *)o));
2714 10 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 10 0 10
2715 r = pcx_call_meth(aTHX_ self, "route", argv, 5, 1);
2716 12 # uncoverable branch false
2717 # uncoverable condition right count:1 class:ignore_covered_err
2718 if (r) SvREFCNT_dec(r);
2719 }
2720 RETVAL = newSVsv(self);
2721 }
2722 OUTPUT:
2723 RETVAL
2724  
2725 # ---- the cross-worker bus, at the application level -------------------------
2726 #
2727 # The same two calls the context has. They live here as well because the place
2728 # to REGISTER a subscription is where the application is built - at boot, in
2729 # the parent, before the server forks - and at that point there is no context
2730 # to hang them off. A subscription made inside a request would land in one
2731 # worker and last as long as that process, which is the fault the bus exists
2732 # to fix.
2733  
2734 IV
2735 publish(self, topic, payload)
2736 SV *self
2737 SV *topic
2738 SV *payload
2739 CODE:
2740 PERL_UNUSED_VAR(self);
2741 1 ▶ RETVAL = punk_bus_app_publish(aTHX_ topic, payload);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = punk_bus_app_publish(aTHX_ topic, payload); 0 1 1
2742 OUTPUT:
2743 RETVAL
2744  
2745 IV
2746 subscribe(self, topic, cb, ...)
2747 SV *self
2748 SV *topic
2749 SV *cb
2750 CODE:
2751 {
2752 3 SV *group = &PL_sv_undef;
2753 int i;
2754 PERL_UNUSED_VAR(self);
2755 4 ▶ for (i = 3; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 3; i + 1 < items; i += 2) { 1 3 4
2756 1 const char *k = SvPV_nolen(ST(i));
2757 1 ▶ # uncoverable branch false
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false 1 0 1
2758 # uncoverable condition right count:1 class:ignore_covered_err
2759 3 ▶ if (strEQ(k, "group")) group = ST(i + 1);
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "group")) group = ST(i + 1); 0 3 3
2760 }
2761 RETVAL = punk_bus_app_subscribe(aTHX_ topic, cb, group);
2762 }
2763 OUTPUT:
2764 RETVAL
« xs/static.xs ↑ Summary xs/computed.xs »