xs/app.xs

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