xs/context.xs

filter: Statementstmt 98.8% 523 / 529 (2 uncoverable) Branchbran 71.3% 443 / 614 (9 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.0% 966 / 1143 (11 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_headers.h ↑ Summary include/punk/punk_dispatch.h »
1 MODULE = Punk PACKAGE = Punk::Context
2  
3 PROTOTYPES: DISABLE
4  
5 # The per-request context, in C (punk_context.h): the whole class lives here -
6 # the constructor, the plain slot accessors and the methods. lib/Punk/Context.pm
7 # is documentation only.
8  
9 # Fast per-request constructor: bless an AV with just the slots the dispatcher
10 # fills (env, app, match); req/res/stash/openapi stay unset (lazy).
11 SV *
12 _build(class, env, app, match)
13 SV *class
14 SV *env
15 SV *app
16 SV *match
17 CODE:
18 {
19 8 AV *av = newAV();
20 8 av_extend(av, PCX_MATCH);
21 8 (void)av_store(av, PCX_ENV, newSVsv(env));
22 8 (void)av_store(av, PCX_APP, newSVsv(app));
23 8 (void)av_store(av, PCX_MATCH, newSVsv(match));
24 8 RETVAL = sv_bless(newRV_noinc((SV *)av), gv_stashsv(class, GV_ADD));
25 }
26 OUTPUT:
27 RETVAL
28  
29 # ---- slot accessors ----------------------------------------------------------
30  
31 # The plain read/write accessors. The ALIAS index is the position in pcx_slots
32 # below, not the slot number itself - keeping the mapping explicit means the
33 # enum in punk_context.h can be reordered without silently rewiring these.
34 SV *
35 env(self, ...)
36 SV *self
37 ALIAS:
38 app = 1
39 _req = 2
40 _res = 3
41 stash_hv = 4
42 openapi_params = 5
43 match = 6
44 CODE:
45 {
46 static const I32 pcx_slots[] = {
47 PCX_ENV, PCX_APP, PCX_REQ, PCX_RES,
48 PCX_STASH, PCX_OPENAPI, PCX_MATCH
49 };
50 161 AV *av = pcx_av(aTHX_ self);
51 160 I32 slot = pcx_slots[ix];
52 SV **e;
53 160 ▶ if (items > 1) (void)av_store(av, slot, newSVsv(ST(1)));
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) (void)av_store(av, slot, newSVsv(ST(1))); 7 153 160
54 160 e = av_fetch(av, slot, 0);
55 160 ▶ RETVAL = (e && *e) ? newSVsv(*e) : newSV(0); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (e && *e) ? newSVsv(*e) : newSV(0); /* # uncoverable condition right count:2 class:ignore_covered_err */ 158 2 160
RETVAL = (e && *e) ? newSVsv(*e) : newSV(0); /* # uncoverable condition right count:2 class:ignore_covered_err */ 158 0 158
56 }
57 OUTPUT:
58 RETVAL
59  
60 # ---- lazy sub-objects --------------------------------------------------------
61  
62 SV *
63 req(self)
64 SV *self
65 CODE:
66 {
67 4533 AV *av = pcx_av(aTHX_ self);
68 4533 RETVAL = newSVsv(pcx_force(aTHX_ av, PCX_REQ, "Punk::Request",
69 pcx_get(aTHX_ av, PCX_ENV)));
70 }
71 OUTPUT:
72 RETVAL
73  
74 SV *
75 res(self)
76 SV *self
77 CODE:
78 {
79 20 AV *av = pcx_av(aTHX_ self);
80 20 RETVAL = newSVsv(pcx_force(aTHX_ av, PCX_RES, "Punk::Response", NULL));
81 }
82 OUTPUT:
83 RETVAL
84  
85 # The outbound user agent: one Fetch per worker, shared by every request it
86 # serves, so the keep-alive pool and DNS state survive between them. The slot
87 # here only memoises the lookup for this request; punk_ua.h owns the agent and
88 # its pid check. Bound to the worker's loop, so a call made from a handler does
89 # not stop the worker answering others.
90 SV *
91 ua(self, name = NULL)
92 SV *self
93 SV *name
94 CODE:
95 {
96 34 AV *av = pcx_av(aTHX_ self);
97 STRLEN nl;
98 14 ▶ const char *n = (name && SvOK(name)) ? SvPV_const(name, nl)
Branch: 1 branch
BranchTrueFalseTotal
const char *n = (name && SvOK(name)) ? SvPV_const(name, nl) 7 0 7
99 34 ▶ : (nl = sizeof(K_DEFAULT) - 1,
Branch: 1 branch
BranchTrueFalseTotal
: (nl = sizeof(K_DEFAULT) - 1, 7 27 34
100 K_DEFAULT);
101 /* Memoised per name, so a handler naming the same agent twice gets
102 * one agent - and one jar, when the jar is per request. */
103 34 SV *memo = pcx_get(aTHX_ av, PCX_UA);
104 HV *seen;
105 SV **e;
106 34 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 4 30 34
# uncoverable condition right count:2 class:ignore_covered_err 4 0 4
# uncoverable condition right count:2 class:ignore_covered_err 4 0 4
107 4 # uncoverable condition right count:3 class:ignore_covered_err
108 if (memo && SvROK(memo) && SvTYPE(SvRV(memo)) == SVt_PVHV)
109 30 seen = (HV *)SvRV(memo);
110 30 else {
111 seen = newHV();
112 34 (void)av_store(av, PCX_UA, newRV_noinc((SV *)seen));
113 34 ▶ }
Branch: 3 branches
BranchTrueFalseTotal
} 4 30 34
} 4 0 4
} 4 0 4
114 e = hv_fetch(seen, n, (I32)nl, 0);
115 # uncoverable condition right count:2 class:ignore_covered_err
116 30 # uncoverable condition right count:3 class:ignore_covered_err
117 29 if (e && *e && SvROK(*e)) RETVAL = newSVsv(*e);
118 29 else {
119 /* +1 from pua_agent; the cache takes that reference */
120 SV *u = pua_agent(aTHX_ pcx_get(aTHX_ av, PCX_APP), n, nl);
121 (void)hv_store(seen, n, (I32)nl, u, 0);
122 RETVAL = newSVsv(u);
123 }
124 }
125 OUTPUT:
126 RETVAL
127  
128 SV *
129 stash(self)
130 SV *self
131 CODE:
132 {
133 44 AV *av = pcx_av(aTHX_ self);
134 44 SV *s = pcx_get(aTHX_ av, PCX_STASH);
135 44 ▶ if (!s) {
Branch: 1 branch
BranchTrueFalseTotal
if (!s) { 22 22 44
136 22 s = newRV_noinc((SV *)newHV());
137 22 (void)av_store(av, PCX_STASH, s);
138 }
139 44 RETVAL = newSVsv(s);
140 }
141 OUTPUT:
142 RETVAL
143  
144 SV *
145 openapi(self)
146 SV *self
147 CODE:
148 {
149 13 SV *o = pcx_get(aTHX_ pcx_av(aTHX_ self), PCX_OPENAPI);
150 13 ▶ RETVAL = o ? newSVsv(o) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = o ? newSVsv(o) : &PL_sv_undef; 12 1 13
151 }
152 OUTPUT:
153 RETVAL
154  
155 # Validated OpenAPI params win (path, then query), then web route captures,
156 # then the request (query, then form body).
157 SV *
158 param(self, name)
159 SV *self
160 SV *name
161 CODE:
162 {
163 107499 SV *v = pcx_param(aTHX_ pcx_av(aTHX_ self), name);
164 107499 ▶ RETVAL = v ? v : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = v ? v : newSV(0); 65 107434 107499
165 }
166 OUTPUT:
167 RETVAL
168  
169 # params -> all of them merged, in that same precedence
170 # params(@k) -> just those, as a list of values in list context (undef
171 # for a name no layer has) or a hashref of the ones that
172 # are there in scalar context, which is the filter-hash
173 # shape: my %f = %{ $c->params(qw(state queue)) };
174 #
175 # Results are written back over the argument slots, one behind the name
176 # being read, so no key is overwritten before it has been looked up.
177 void
178 params(self, ...)
179 SV *self
180 PPCODE:
181 {
182 19 AV *av = pcx_av(aTHX_ self);
183 int i;
184 19 ▶ if (items < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) { 14 5 19
185 14 ST(0) = sv_2mortal(newRV_noinc((SV *)pcx_params_merged(aTHX_ av)));
186 14 XSRETURN(1);
187 }
188 5 ▶ if (GIMME_V == G_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_ARRAY) { 4 1 5
189 10 ▶ for (i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) { 6 4 10
190 6 SV *v = pcx_param(aTHX_ av, ST(i));
191 6 ▶ ST(i - 1) = sv_2mortal(v ? v : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
ST(i - 1) = sv_2mortal(v ? v : newSV(0)); 3 3 6
192 }
193 4 XSRETURN(items - 1);
194 }
195 else {
196 1 HV *out = newHV();
197 5 ▶ for (i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) { 4 1 5
198 4 SV *v = pcx_param(aTHX_ av, ST(i));
199 4 ▶ if (v) (void)hv_store_ent(out, ST(i), v, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (v) (void)hv_store_ent(out, ST(i), v, 0); 3 1 4
200 }
201 1 ST(0) = sv_2mortal(newRV_noinc((SV *)out));
202 1 XSRETURN(1);
203 }
204 }
205  
206 SV *
207 model(self, name)
208 SV *self
209 SV *name
210 CODE:
211 {
212 100 SV *app = pcx_get(aTHX_ pcx_av(aTHX_ self), PCX_APP);
213 100 dSP; int count;
214 100 ENTER; SAVETMPS;
215 100 ▶ PUSHMARK(SP); EXTEND(SP, 2);
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 2); 0 100 100
PUSHMARK(SP); EXTEND(SP, 2); 0 100 100
216 100 ▶ PUSHs(app ? app : &PL_sv_undef); PUSHs(name); PUTBACK; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(app ? app : &PL_sv_undef); PUSHs(name); PUTBACK; /* # uncoverable condition left count:1 class:ignore_covered_err */ 100 0 100
217 100 count = call_method("model_instance", G_SCALAR);
218 100 SPAGAIN;
219 100 ▶ RETVAL = count > 0 ? newSVsv(POPs) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = count > 0 ? newSVsv(POPs) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ 100 0 100
220 100 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 100 0 100
221 }
222 OUTPUT:
223 RETVAL
224  
225 # $app->render_view($self, @_)
226 SV *
227 render(self, ...)
228 SV *self
229 CODE:
230 {
231 2171 AV *av = pcx_av(aTHX_ self);
232 2171 SV *app = pcx_get(aTHX_ av, PCX_APP);
233 2171 int nargs = items - 1, i;
234 SV **argv, *r;
235 2171 Newx(argv, nargs + 1, SV *); /* capture args before any stack work */
236 2171 argv[0] = self;
237 4438 ▶ for (i = 0; i < nargs; i++) argv[i + 1] = ST(i + 1);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nargs; i++) argv[i + 1] = ST(i + 1); 2267 2171 4438
238 2171 ▶ r = pcx_call_meth(aTHX_ app ? app : &PL_sv_undef, "render_view", /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
r = pcx_call_meth(aTHX_ app ? app : &PL_sv_undef, "render_view", /* # uncoverable condition left count:1 class:ignore_covered_err */ 2171 0 2171
239 argv, nargs + 1, 1);
240 2161 Safefree(argv);
241 2161 ▶ RETVAL = r ? r : newSV(0); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = r ? r : newSV(0); /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 2161 2161
242 }
243 OUTPUT:
244 RETVAL
245  
246 # fragment($template, \%data?, %over): render with no layout, and
247 # `Cache-Control: private, no-store` on the finished response.
248 #
249 # A fragment is one user's data swapped into one user's page - the panel
250 # PDFMake-Site rendered through a private engine - and a shared cache handing
251 # it to the next visitor is a leak, so the header is the default and not a
252 # reminder. The public, cacheable partial is `render` with layout => undef and
253 # a Cache-Control of its own. A `layout` override here croaks: a fragment with
254 # a layout is a page, and the caller wanted `render`.
255 SV *
256 fragment(self, ...)
257 SV *self
258 CODE:
259 {
260 7 AV *av = pcx_av(aTHX_ self);
261 7 SV *app = pcx_get(aTHX_ av, PCX_APP);
262 7 int nargs = items - 1, i, n = 0;
263 SV **argv, *r, *lay;
264 7 ▶ if (nargs < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (nargs < 1) 1 6 7
265 1 croak("Punk::Context::fragment: a template name is required");
266 /* ST(1) template, ST(2) data, ST(3..) overrides - every override
267 * pair is checked for `layout` before anything is called */
268 7 ▶ for (i = 3; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 3; i + 1 < items; i += 2) { 2 5 7
269 2 STRLEN kl; const char *k = SvPV_const(ST(i), kl);
270 2 ▶ if (kl == 6 && memEQ(k, "layout", 6)) /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 6 && memEQ(k, "layout", 6)) /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 0 2
if (kl == 6 && memEQ(k, "layout", 6)) /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 1 2
271 1 croak("Punk::Context::fragment: a fragment has no layout - "
272 "to render inside one, use render(..., layout => ...)");
273 }
274 /* self, template, data (undef if not given), overrides, layout, undef */
275 5 Newx(argv, nargs + 4, SV *);
276 5 argv[n++] = self;
277 5 argv[n++] = ST(1);
278 5 ▶ argv[n++] = items > 2 ? ST(2) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
argv[n++] = items > 2 ? ST(2) : &PL_sv_undef; 4 1 5
279 7 ▶ for (i = 3; i < items; i++) argv[n++] = ST(i);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 3; i < items; i++) argv[n++] = ST(i); 2 5 7
280 5 lay = sv_2mortal(newSVpvs("layout"));
281 5 argv[n++] = lay;
282 5 argv[n++] = &PL_sv_undef;
283 5 ▶ r = pcx_call_meth(aTHX_ app ? app : &PL_sv_undef, "render_view", /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
r = pcx_call_meth(aTHX_ app ? app : &PL_sv_undef, "render_view", /* # uncoverable condition left count:1 class:ignore_covered_err */ 5 0 5
284 argv, n, 1);
285 5 Safefree(argv);
286 5 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 5 5
287 # uncoverable condition left count:1 class:ignore_covered_err
288 5 ▶ if (!r) r = newSV(0);
Branch: 2 branches
BranchTrueFalseTotal
if (!r) r = newSV(0); 5 0 5
if (!r) r = newSV(0); 5 0 5
289 5 /* the header: replace an existing Cache-Control, else append one */
290 5 ▶ # uncoverable branch false
Branch: 4 branches
BranchTrueFalseTotal
# uncoverable branch false 5 0 5
# uncoverable branch false 5 0 5
# uncoverable branch false 5 0 5
# uncoverable branch false 5 0 5
291 5 # uncoverable condition right count:1 class:ignore_covered_err
292 5 # uncoverable condition right count:2 class:ignore_covered_err
293 5 if (SvROK(r) && SvTYPE(SvRV(r)) == SVt_PVAV) {
294 17 ▶ SV **hp = av_fetch((AV *)SvRV(r), 1, 0);
Branch: 1 branch
BranchTrueFalseTotal
SV **hp = av_fetch((AV *)SvRV(r), 1, 0); 12 5 17
295 12 # uncoverable branch false
296 # uncoverable condition right count:1 class:ignore_covered_err
297 12 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 12 0 12
# uncoverable condition right count:2 class:ignore_covered_err 12 0 12
# uncoverable condition right count:2 class:ignore_covered_err 0 12 12
298 12 # uncoverable condition right count:3 class:ignore_covered_err
299 12 ▶ # uncoverable condition right count:4 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:4 class:ignore_covered_err 1 11 12
# uncoverable condition right count:4 class:ignore_covered_err 1 0 1
300 1 if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) {
301 1 ▶ AV *hd = (AV *)SvRV(*hp);
Branch: 2 branches
BranchTrueFalseTotal
AV *hd = (AV *)SvRV(*hp); 1 0 1
AV *hd = (AV *)SvRV(*hp); 1 0 1
302 1 SSize_t j, hn = av_len(hd) + 1;
303 int found = 0;
304 for (j = 0; j + 1 < hn; j += 2) {
305 5 ▶ SV **nm = av_fetch(hd, j, 0);
Branch: 1 branch
BranchTrueFalseTotal
SV **nm = av_fetch(hd, j, 0); 4 1 5
306 4 STRLEN nl; const char *np;
307 4 # uncoverable branch true
308 # uncoverable condition left count:1 class:ignore_covered_err
309 # uncoverable condition left count:2 class:ignore_covered_err
310 # uncoverable condition left count:3 class:ignore_covered_err
311 5 if (!(nm && *nm && SvOK(*nm))) continue;
312 np = SvPV_const(*nm, nl);
313 if (nl == 13 && foldEQ(np, "Cache-Control", (I32)13)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
314 SV **vp = av_fetch(hd, j + 1, 1);
315 # uncoverable branch false
316 # uncoverable condition right count:1 class:ignore_covered_err
317 # uncoverable condition right count:2 class:ignore_covered_err
318 if (vp && *vp) sv_setpvs(*vp, "private, no-store");
319 found = 1;
320 }
321 }
322 if (!found) {
323 av_push(hd, newSVpvs("Cache-Control"));
324 av_push(hd, newSVpvs("private, no-store"));
325 }
326 }
327 }
328 RETVAL = r;
329 }
330 OUTPUT:
331 RETVAL
332  
333 # ---- finished responses ------------------------------------------------------
334  
335 SV *
336 json(self, data, status = &PL_sv_undef)
337 SV *self
338 SV *data
339 SV *status
340 CODE:
341 {
342 941 AV *av = pcx_av(aTHX_ self);
343 941 AV *res = pcx_res_av(aTHX_ av);
344 941 IV st = ps_status_arg(aTHX_ status);
345 932 SV *bytes = pcx_json(aTHX_ av, data);
346 932 ▶ if (!st) st = pcx_res_status(aTHX_ res);
Branch: 1 branch
BranchTrueFalseTotal
if (!st) st = pcx_res_status(aTHX_ res); 888 44 932
347 932 ▶ if (!st) st = 200;
Branch: 1 branch
BranchTrueFalseTotal
if (!st) st = 200; 883 49 932
348 959 ▶ RETVAL = punk_triplet_ct(aTHX_ st, "application/json", 16,
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = punk_triplet_ct(aTHX_ st, "application/json", 16, 27 905 932
349 27 bytes, res ? pcx_res_headers(aTHX_ res) : NULL);
350 }
351 OUTPUT:
352 RETVAL
353  
354 # A File::Raw::XML::Document or ::Node is serialised; a string is markup the
355 # caller already built and goes out as it stands. Anything else is refused
356 # rather than stringified, which is the difference between an empty element
357 # and a body reading HASH(0x7f...).
358 SV *
359 xml(self, data, status = &PL_sv_undef)
360 SV *self
361 SV *data
362 SV *status
363 CODE:
364 {
365 7 AV *av = pcx_av(aTHX_ self);
366 7 AV *res = pcx_res_av(aTHX_ av);
367 7 IV st = ps_status_arg(aTHX_ status);
368 SV *bytes;
369 7 ▶ if (SvROK(data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(data)) { 6 1 7
370 6 SV *err = NULL;
371 6 ▶ if (!punk_xml_is(aTHX_ data))
Branch: 1 branch
BranchTrueFalseTotal
if (!punk_xml_is(aTHX_ data)) 1 5 6
372 1 croak("Punk: xml() takes a File::Raw::XML::Document, a "
373 "::Node, or a string of markup");
374 5 bytes = punk_xml_bytes(aTHX_ data, &err);
375 5 ▶ # uncoverable branch true
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch true 0 5 5
# uncoverable branch true 0 0 0
376 # uncoverable condition left count:1 class:ignore_covered_err
377 1 # uncoverable condition left count:2 class:ignore_covered_err
378 6 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 5 1 6
379 6 ▶ if (!bytes) croak("%s", err ? SvPV_nolen(err) : "Punk: xml");
Branch: 1 branch
BranchTrueFalseTotal
if (!bytes) croak("%s", err ? SvPV_nolen(err) : "Punk: xml"); 5 1 6
380 6 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 0 6 6
381 else bytes = newSVsv(data);
382 0 if (!st) st = pcx_res_status(aTHX_ res);
383 if (!st) st = 200;
384 RETVAL = punk_triplet(aTHX_ st,
385 sv_2mortal(newSVpvs(PK_XML_CT)),
386 bytes, res ? pcx_res_headers(aTHX_ res) : NULL); /* # uncoverable statement class:ignore_covered_err */
387 }
388 OUTPUT:
389 RETVAL
390  
391 SV *
392 text(self, body, status = &PL_sv_undef)
393 SV *self
394 SV *body
395 SV *status
396 ALIAS:
397 html = 1
398 CODE:
399 {
400 109928 AV *av = pcx_av(aTHX_ self);
401 109928 AV *res = pcx_res_av(aTHX_ av);
402 109928 IV st = ps_status_arg(aTHX_ status);
403 109912 ▶ if (!st) st = pcx_res_status(aTHX_ res);
Branch: 1 branch
BranchTrueFalseTotal
if (!st) st = pcx_res_status(aTHX_ res); 109872 40 109912
404 109912 ▶ if (!st) st = 200;
Branch: 1 branch
BranchTrueFalseTotal
if (!st) st = 200; 109867 45 109912
405 109912 RETVAL = ix == 1
406 30 ▶ ? punk_triplet_ct(aTHX_ st, "text/html; charset=utf-8", 24,
Branch: 1 branch
BranchTrueFalseTotal
? punk_triplet_ct(aTHX_ st, "text/html; charset=utf-8", 24, 3 24 27
407 3 newSVsv(body), res ? pcx_res_headers(aTHX_ res) : NULL)
408 109991 ▶ : punk_triplet_ct(aTHX_ st, "text/plain; charset=utf-8", 25,
Branch: 2 branches
BranchTrueFalseTotal
: punk_triplet_ct(aTHX_ st, "text/plain; charset=utf-8", 25, 27 109885 109912
: punk_triplet_ct(aTHX_ st, "text/plain; charset=utf-8", 25, 79 109806 109885
409 79 newSVsv(body), res ? pcx_res_headers(aTHX_ res) : NULL);
410 }
411 OUTPUT:
412 RETVAL
413  
414 # safe_path($path, $fallback?): $path when it is a same-origin relative path,
415 # otherwise the fallback (undef by default). The guard for anything that
416 # redirects to a destination the request supplied - ?to=, ?return=, ?next=.
417 # (Do not start a line in here with the word after "#" being if/else/endif:
418 # xsubpp reads it as a preprocessor directive.)
419 SV *
420 safe_path(self, path, fallback = &PL_sv_undef)
421 SV *self
422 SV *path
423 SV *fallback
424 CODE:
425 {
426 PERL_UNUSED_VAR(self);
427 38 RETVAL = pk_same_origin_path(aTHX_ path)
428 19 ▶ ? newSVsv(path) : newSVsv(fallback);
Branch: 1 branch
BranchTrueFalseTotal
? newSVsv(path) : newSVsv(fallback); 6 13 19
429 }
430 OUTPUT:
431 RETVAL
432  
433 # $c->origin - the request's scheme and host, when that host is the
434 # application's canonical one or on its allowlist; the canonical origin when
435 # it is anything else; undef when no `host` was declared. The raw header is
436 # never returned (punk_host.h).
437 SV *
438 origin(self)
439 SV *self
440 CODE:
441 {
442 220026 int st = 0;
443 220026 SV *o = pk_origin_of(aTHX_ self, &st);
444 220026 ▶ RETVAL = o ? o : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = o ? o : newSV(0); 1 220025 220026
445 }
446 OUTPUT:
447 RETVAL
448  
449 # $c->url_for($name, %args) - the URL of a named route.
450 #
451 # $c->url_for('book', id => 42) # /books/42
452 # $c->url_for('book', id => 42, page => 2) # /books/42?page=2
453 # $c->url_for('book', id => 42, absolute => 1) # https://example.com/books/42
454 #
455 # An argument naming a capture fills that segment; anything left over is the
456 # query string, keys sorted. `absolute` and `query` are reserved words rather
457 # than captures - they are refused as route names for that reason.
458 #
459 # Every result carries the application's prefix: the path on `host` and then
460 # SCRIPT_NAME, which are layers rather than alternatives (a proxy strips one,
461 # a PSGI mount adds the other, and the browser sees both). A relative URL is
462 # an href resolved against a page that is already under the prefix, so it
463 # needs the prefix exactly as much as an absolute one does.
464 #
465 # `absolute` joins $c->origin, which is the canonical origin unless the
466 # request's Host is on the allowlist and is never the raw header - so a link
467 # built here and mailed cannot be poisoned by a crafted Host.
468 SV *
469 url_for(self, name, ...)
470 SV *self
471 SV *name
472 CODE:
473 {
474 87 AV *av = pcx_av(aTHX_ self);
475 87 SV *appsv = pcx_get(aTHX_ av, PCX_APP);
476 87 HV *h, *args, *query = NULL;
477 87 SV *namesv, *routersv, *prefix = NULL, *origin = NULL;
478 HE *he;
479 SV *val;
480 87 IV absolute = 0;
481 int i;
482  
483 87 ▶ if ((items - 2) % 2)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 2) % 2) 1 86 87
484 1 croak("Punk: url_for takes a route name then key => value pairs");
485 86 ▶ if (!SvOK(name) || !SvCUR(name)) /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(name) || !SvCUR(name)) /* # uncoverable condition left count:1 class:ignore_covered_err */ 86 0 86
if (!SvOK(name) || !SvCUR(name)) /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 85 86
486 1 croak("Punk: url_for needs a route name");
487 85 ▶ # uncoverable branch true
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable branch true 85 0 85
# uncoverable branch true 85 0 85
# uncoverable branch true 0 85 85
488 0 # uncoverable condition left count:1 class:ignore_covered_err
489 85 # uncoverable condition left count:2 class:ignore_covered_err
490 # uncoverable condition left count:3 class:ignore_covered_err
491 85 if (!(appsv && SvROK(appsv) && SvTYPE(SvRV(appsv)) == SVt_PVHV))
492 166 ▶ croak("Punk: url_for: no application on this context"); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk: url_for: no application on this context"); /* # uncoverable statement */ 82 84 166
493 h = (HV *)SvRV(appsv);
494 82  
495 82 ▶ args = (HV *)sv_2mortal((SV *)newHV());
Branch: 2 branches
BranchTrueFalseTotal
args = (HV *)sv_2mortal((SV *)newHV()); 8 74 82
args = (HV *)sv_2mortal((SV *)newHV()); 8 0 8
496 8 for (i = 2; i + 1 < items; i += 2) {
497 10 STRLEN kl;
498 const char *k = SvPV_const(ST(i), kl);
499 74 ▶ if (kl == 8 && memEQ(k, "absolute", 8)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 8 && memEQ(k, "absolute", 8)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 3 71 74
if (kl == 8 && memEQ(k, "absolute", 8)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 3 0 3
500 3 absolute = SvTRUE(ST(i + 1)) ? 1 : 0;
501 3 ▶ continue;
Branch: 2 branches
BranchTrueFalseTotal
continue; 2 1 3
continue; 0 2 2
502 1 }
503 if (kl == 5 && memEQ(k, "query", 5)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
504 2 SV *q = ST(i + 1);
505 2 if (!(SvROK(q) && SvTYPE(SvRV(q)) == SVt_PVHV)) /* # uncoverable condition left count:2 class:ignore_covered_err */
506 croak("Punk: url_for('%s'): `query` takes a hashref",
507 71 SvPV_nolen(name));
508 query = (HV *)SvRV(q);
509 continue;
510 84 }
511 84 ▶ (void)hv_store(args, k, (I32)kl, newSVsv(ST(i + 1)), 0);
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_store(args, k, (I32)kl, newSVsv(ST(i + 1)), 0); 84 0 84
(void)hv_store(args, k, (I32)kl, newSVsv(ST(i + 1)), 0); 84 0 84
512 168 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 84 0 84
513 84 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
3 81 84
514 3 namesv = app_get(aTHX_ h, K_NAMES_C);
515 he = (namesv && SvROK(namesv) && SvTYPE(SvRV(namesv)) == SVt_PVHV)
516 ? hv_fetch_ent((HV *)SvRV(namesv), name, 0, 0) : NULL;
517 if (!he)
518 81 croak("Punk: url_for: no route is named '%s' - a name is "
519 "declared with { name => '...' } on the route, and it is "
520 81 "one namespace for the whole application",
521 81 ▶ SvPV_nolen(name));
Branch: 2 branches
BranchTrueFalseTotal
SvPV_nolen(name)); 81 0 81
SvPV_nolen(name)); 0 81 81
522 0 val = HeVAL(he);
523  
524 routersv = app_get(aTHX_ h, K_ROUTER);
525 81 # uncoverable branch true
526 81 # uncoverable condition left count:1 class:ignore_covered_err
527 81 ▶ # uncoverable condition left count:2 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition left count:2 class:ignore_covered_err 81 0 81
# uncoverable condition left count:2 class:ignore_covered_err 81 0 81
528 162 ▶ if (!(routersv && SvROK(routersv)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(routersv && SvROK(routersv))) 81 0 81
529 81 ▶ croak("Punk: url_for: this application is not compiled"); /* # uncoverable statement */
Branch: 3 branches
BranchTrueFalseTotal
croak("Punk: url_for: this application is not compiled"); /* # uncoverable statement */ 3 78 81
croak("Punk: url_for: this application is not compiled"); /* # uncoverable statement */ 3 0 3
croak("Punk: url_for: this application is not compiled"); /* # uncoverable statement */ 3 0 3
530 81 ▶ 
Branch: 4 branches
BranchTrueFalseTotal
4 77 81
4 0 4
4 0 4
4 0 4
531 81 ▶ { /* prefix = the path on `host`, then SCRIPT_NAME */
Branch: 2 branches
BranchTrueFalseTotal
{ /* prefix = the path on `host`, then SCRIPT_NAME */ 78 3 81
{ /* prefix = the path on `host`, then SCRIPT_NAME */ 3 75 78
532 6 SV *hp = app_get(aTHX_ h, K_HOST_PATH_C);
533 6 ▶ SV *envsv = pcx_get(aTHX_ av, PCX_ENV);
Branch: 1 branch
BranchTrueFalseTotal
SV *envsv = pcx_get(aTHX_ av, PCX_ENV); 3 3 6
534 6 ▶ SV **sn = (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
SV **sn = (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV) 4 2 6
535 ? hv_fetchs((HV *)SvRV(envsv), "SCRIPT_NAME", 0) : NULL;
536 # uncoverable condition right count:2 class:ignore_covered_err
537 # uncoverable condition right count:3 class:ignore_covered_err
538 81 ▶ int have_hp = (hp && SvOK(hp) && SvCUR(hp));
Branch: 1 branch
BranchTrueFalseTotal
int have_hp = (hp && SvOK(hp) && SvCUR(hp)); 8 73 81
539 8 # uncoverable condition right count:2 class:ignore_covered_err
540 8 # uncoverable condition right count:3 class:ignore_covered_err
541 8 ▶ # uncoverable condition right count:4 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:4 class:ignore_covered_err 1 7 8
542 1 int have_sn = (sn && *sn && SvOK(*sn) && SvCUR(*sn));
543 if (have_hp || have_sn) {
544 prefix = sv_2mortal(newSVpvs(""));
545 if (have_hp) sv_catsv(prefix, hp);
546 7 if (have_sn) sv_catsv(prefix, *sn);
547 }
548 }
549  
550 if (absolute) {
551 int st = 0;
552 91 ▶ origin = pk_origin_of(aTHX_ self, &st);
Branch: 2 branches
BranchTrueFalseTotal
origin = pk_origin_of(aTHX_ self, &st); 13 67 80
origin = pk_origin_of(aTHX_ self, &st); 13 0 13
553 13 if (!origin)
554 13 croak("Punk: url_for('%s', absolute => 1): the application "
555 13 "declared no `host`, and the request's Host is not "
556 13 "something a link may be built on - declare "
557 26 ▶ "host 'https://example.com'", SvPV_nolen(name));
Branch: 1 branch
BranchTrueFalseTotal
"host 'https://example.com'", SvPV_nolen(name)); 13 0 13
558 13 ▶ sv_2mortal(origin);
Branch: 1 branch
BranchTrueFalseTotal
sv_2mortal(origin); 13 0 13
559 }
560  
561 67 /* A route's entry is a record index; an API operation's is a
562 * reference to [ mount prefix, parsed template ]. One table, two
563 67 * kinds of route, and the shape says which. */
564 if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV) { /* # uncoverable condition right count:2 class:ignore_covered_err */
565 AV *pair = (AV *)SvRV(val);
566 SV **mp = av_fetch(pair, 0, 0);
567 SV **tm = av_fetch(pair, 1, 0);
568 RETVAL = pk_url_build_op(aTHX_
569 # uncoverable condition right count:1 class:ignore_covered_err
570 # uncoverable condition right count:2 class:ignore_covered_err
571 (AV *)SvRV(*tm), (mp && *mp) ? *mp : NULL,
572 args, query, prefix, origin, SvPV_nolen(name));
573 }
574 else
575 RETVAL = pk_url_build(aTHX_ punk_router_of(aTHX_ routersv),
576 SvIV(val), args, query, prefix, origin,
577 SvPV_nolen(name));
578 }
579 OUTPUT:
580 RETVAL
581  
582 # $c->host_allowed - true when the request's Host is one the application
583 # declared: the canonical host, or a match on the allowlist. The signal an
584 # application needs to refuse an unknown host rather than answer for it.
585 IV
586 host_allowed(self)
587 SV *self
588 CODE:
589 {
590 20 int st = 0;
591 20 SV *o = pk_origin_of(aTHX_ self, &st);
592 20 ▶ if (o) SvREFCNT_dec(o);
Branch: 1 branch
BranchTrueFalseTotal
if (o) SvREFCNT_dec(o); 19 1 20
593 20 ▶ RETVAL = st > 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = st > 0 ? 1 : 0; 17 3 20
594 }
595 OUTPUT:
596 RETVAL
597  
598 # $c->asset('/static/app.css') - the content-addressed URL for a file under
599 # a static mount: '/static/app.9f3a1c2b0d4e5f60.css', which serves with a
600 # year and `immutable` because that URL cannot come to mean anything else.
601 # A URL under no static mount, under one with fingerprinting off, or naming
602 # a file that cannot be read comes back exactly as it went in - the page
603 # still works, it just revalidates.
604 SV *
605 asset(self, url)
606 SV *self
607 SV *url
608 CODE:
609 {
610 9 AV *av = pcx_av(aTHX_ self);
611 9 SV *app = pcx_get(aTHX_ av, PCX_APP);
612 9 SV *out = NULL;
613 9 ▶ # uncoverable branch false
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable branch false 9 0 9
# uncoverable branch false 9 0 9
# uncoverable branch false 9 0 9
614 9 # uncoverable condition right count:1 class:ignore_covered_err
615 9 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 9 0 9
# uncoverable condition right count:2 class:ignore_covered_err 9 0 9
# uncoverable condition right count:2 class:ignore_covered_err 9 0 9
616 9 # uncoverable condition right count:3 class:ignore_covered_err
617 if (app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV) {
618 9 ▶ SV *mp = app_get(aTHX_ (HV *)SvRV(app), K_MOUNTS_C);
Branch: 1 branch
BranchTrueFalseTotal
SV *mp = app_get(aTHX_ (HV *)SvRV(app), K_MOUNTS_C); 5 4 9
619 # uncoverable branch false
620 # uncoverable condition right count:1 class:ignore_covered_err
621 # uncoverable condition right count:2 class:ignore_covered_err
622 # uncoverable condition right count:3 class:ignore_covered_err
623 if (mp && SvROK(mp) && SvTYPE(SvRV(mp)) == SVt_PVAV)
624 out = pa_asset_for(aTHX_ (AV *)SvRV(mp), url);
625 }
626 RETVAL = out ? out : newSVsv(url);
627 }
628 OUTPUT:
629 RETVAL
630  
631 # $c->escape_html($text) - the five HTML characters escaped (punk_escape.h),
632 # the escape Punk writes its own markup with; the empty string for an
633 # undefined value, the UTF-8 flag kept.
634 SV *
635 escape_html(self, text)
636 SV *self
637 SV *text
638 CODE:
639 {
640 STRLEN len;
641 const char *p;
642 PERL_UNUSED_VAR(self);
643 13 RETVAL = newSVpvs("");
644 13 ▶ if (SvOK(text)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(text)) { 12 1 13
645 12 p = SvPV_const(text, len);
646 12 ▶ SvGROW(RETVAL, len + 1);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(RETVAL, len + 1); 12 0 12
SvGROW(RETVAL, len + 1); 3 9 12
647 12 punk_html_escape(aTHX_ RETVAL, p, len);
648 12 ▶ if (SvUTF8(text)) SvUTF8_on(RETVAL);
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(text)) SvUTF8_on(RETVAL); 1 11 12
649 }
650 }
651 OUTPUT:
652 RETVAL
653  
654 SV *
655 redirect(self, url, status = &PL_sv_undef)
656 SV *self
657 SV *url
658 SV *status
659 CODE:
660 {
661 25 AV *av = pcx_av(aTHX_ self);
662 25 AV *res = pcx_res_av(aTHX_ av);
663 25 ▶ AV *rh = res ? pcx_res_headers(aTHX_ res) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
AV *rh = res ? pcx_res_headers(aTHX_ res) : NULL; 1 24 25
664 25 AV *hdr = newAV();
665 25 AV *body = newAV();
666 25 AV *resp = newAV();
667 25 IV st = ps_status_arg(aTHX_ status);
668 { /* a Location is one header line: a CR, LF or NUL in a URL built
669 * from the request would end it and start another */
670 STRLEN ul, ui;
671 48 ▶ const char *up = SvOK(url) ? SvPV_const(url, ul) : ""; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
const char *up = SvOK(url) ? SvPV_const(url, ul) : ""; /* # uncoverable condition right count:1 class:ignore_covered_err */ 24 0 24
672 24 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 24 24
673 134 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 114 20 134
674 114 ▶ if (!SvOK(url)) ul = 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!SvOK(url)) ul = 0; 112 2 114
if (!SvOK(url)) ul = 0; 111 1 112
if (!SvOK(url)) ul = 0; 1 110 111
675 4 for (ui = 0; ui < ul; ui++)
676 if (up[ui] == '\r' || up[ui] == '\n' || up[ui] == '\0')
677 croak("Punk: a redirect location may not contain CR, LF "
678 20 ▶ "or NUL");
Branch: 1 branch
BranchTrueFalseTotal
"or NUL"); 18 2 20
679 20 }
680 20 if (!st) st = 302;
681 20 av_push(hdr, newSVpvs("Location"));
682 20 av_push(hdr, newSVsv(url));
683 20 ▶ av_push(hdr, newSVpvs("Content-Length"));
Branch: 1 branch
BranchTrueFalseTotal
av_push(hdr, newSVpvs("Content-Length")); 1 19 20
684 1 av_push(hdr, newSViv(0));
685 3 ▶ if (rh) {
Branch: 1 branch
BranchTrueFalseTotal
if (rh) { 2 1 3
686 2 SSize_t i, n = av_len(rh) + 1;
687 2 ▶ for (i = 0; i < n; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2 0 2
for (i = 0; i < n; i++) { 2 0 2
688 SV **e = av_fetch(rh, i, 0);
689 # uncoverable condition right count:1 class:ignore_covered_err
690 20 # uncoverable condition right count:2 class:ignore_covered_err
691 20 av_push(hdr, e && *e ? newSVsv(*e) : newSV(0));
692 20 }
693 20 }
694 20 av_push(body, newSVpvs(""));
695 20 av_extend(resp, 2);
696 av_push(resp, newSViv(st));
697 av_push(resp, newRV_noinc((SV *)hdr));
698 av_push(resp, newRV_noinc((SV *)body));
699 RETVAL = newRV_noinc((SV *)resp);
700 }
701 OUTPUT:
702 RETVAL
703  
704 SV *
705 not_found(self)
706 SV *self
707 CODE:
708 PERL_UNUSED_VAR(self);
709 11 RETVAL = punk_triplet(aTHX_ 404,
710 sv_2mortal(newSVpvs("application/json")),
711 newSVpvs("{\"errors\":[{\"message\":\"Not Found\"}]}"),
712 NULL);
713 OUTPUT:
714 RETVAL
715  
716 # respond_to(json => sub {...}, html => sub {...}, any => sub {...}):
717 # Accept negotiation (punk_accept.h). The most acceptable offered format's
718 # coderef is called with $c and its return is the response; `any` catches a
719 # request nothing else fits, and without it that request is a 406. A client
720 # that is indifferent (no Accept, or only a wildcard match) gets the format
721 # its own Content-Type names if that is offered, else the first registered.
722 # Every outcome carries Vary: Accept.
723 SV *
724 respond_to(self, ...)
725 SV *self
726 CODE:
727 {
728 #define PRT_MAX 16
729 28 AV *av = pcx_av(aTHX_ self);
730 SV *cbs[PRT_MAX];
731 const char *bt[PRT_MAX], *bs[PRT_MAX];
732 STRLEN btl[PRT_MAX], bsl[PRT_MAX];
733 int bq[PRT_MAX], bspec[PRT_MAX];
734 28 SV *any_cb = NULL, *accept_sv = NULL, *ctype_sv = NULL;
735 pa_range ranges[PA_RANGE_MAX];
736 28 int nb = 0, nr = 0, have_accept = 0, i;
737 28 int best = -1, best_q = 0, best_spec = -1, best_order = 0;
738  
739 28 ▶ if (items < 3 || !(items % 2))
Branch: 2 branches
BranchTrueFalseTotal
if (items < 3 || !(items % 2)) 27 1 28
if (items < 3 || !(items % 2)) 1 26 27
740 2 croak("Punk: respond_to takes format => coderef pairs");
741 87 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 64 23 87
742 64 STRLEN nl; const char *nm = SvPV_const(ST(i), nl);
743 64 SV *cb = ST(i + 1);
744 64 ▶ if (!(SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) /* # uncoverable condition left count:2 class:ignore_covered_err */ 63 1 64
if (!(SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 63 63
745 1 croak("Punk: respond_to format '%.*s' needs a coderef",
746 (int)nl, nm);
747 63 ▶ if (nl == 3 && memEQ(nm, "any", 3)) { any_cb = cb; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (nl == 3 && memEQ(nm, "any", 3)) { any_cb = cb; continue; } 8 55 63
if (nl == 3 && memEQ(nm, "any", 3)) { any_cb = cb; continue; } 1 7 8
748 62 ▶ if (nb >= PRT_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (nb >= PRT_MAX) 1 61 62
749 1 croak("Punk: respond_to takes at most %d formats", PRT_MAX);
750 61 ▶ if (!pa_fmt_mime(nm, nl, &bt[nb], &btl[nb], &bs[nb], &bsl[nb]))
Branch: 1 branch
BranchTrueFalseTotal
if (!pa_fmt_mime(nm, nl, &bt[nb], &btl[nb], &bs[nb], &bsl[nb])) 1 60 61
751 1 croak("Punk: respond_to does not know format '%.*s' - "
752 "name a full media type", (int)nl, nm);
753 60 cbs[nb] = cb;
754 60 nb++;
755 }
756  
757 { /* the request's Accept and Content-Type, straight off the env */
758 23 SV *env = pcx_get(aTHX_ av, PCX_ENV);
759 23 ▶ # uncoverable branch false class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable branch false class:ignore_covered_err 23 0 23
# uncoverable branch false class:ignore_covered_err 23 0 23
# uncoverable branch false class:ignore_covered_err 23 0 23
760 23 # uncoverable condition right count:1 class:ignore_covered_err
761 23 # uncoverable condition right count:2 class:ignore_covered_err
762 23 ▶ # uncoverable condition right count:3 class:ignore_covered_err
Branch: 4 branches
BranchTrueFalseTotal
# uncoverable condition right count:3 class:ignore_covered_err 22 1 23
# uncoverable condition right count:3 class:ignore_covered_err 22 0 22
# uncoverable condition right count:3 class:ignore_covered_err 22 0 22
# uncoverable condition right count:3 class:ignore_covered_err 22 0 22
763 23 if (env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV) {
764 23 ▶ HV *eh = (HV *)SvRV(env);
Branch: 4 branches
BranchTrueFalseTotal
HV *eh = (HV *)SvRV(env); 4 19 23
HV *eh = (HV *)SvRV(env); 4 0 4
HV *eh = (HV *)SvRV(env); 4 0 4
HV *eh = (HV *)SvRV(env); 1 3 4
765 SV **e = hv_fetchs(eh, "HTTP_ACCEPT", 0);
766 # uncoverable condition right count:2 class:ignore_covered_err
767 23 ▶ # uncoverable condition right count:3 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:3 class:ignore_covered_err 22 1 23
768 22 # uncoverable condition right count:4 class:ignore_covered_err
769 22 if (e && *e && SvOK(*e) && SvCUR(*e)) accept_sv = *e;
770 e = hv_fetchs(eh, "CONTENT_TYPE", 0);
771 22 # uncoverable condition right count:2 class:ignore_covered_err
772 # uncoverable condition right count:3 class:ignore_covered_err
773 if (e && *e && SvOK(*e) && SvCUR(*e)) ctype_sv = *e;
774 67 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 44 23 67
775 44 }
776 44 ▶ if (accept_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (accept_sv) { 42 2 44
777 42 STRLEN al; const char *a = SvPV_const(accept_sv, al);
778 nr = pa_parse(a, al, ranges, PA_RANGE_MAX);
779 42 ▶ /* unparseable garbage is indifference, not an error */
Branch: 2 branches
BranchTrueFalseTotal
/* unparseable garbage is indifference, not an error */ 26 16 42
/* unparseable garbage is indifference, not an error */ 1 25 26
780 have_accept = nr > 0;
781 27 }
782 27 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
6 21 27
783 6 ▶ for (i = 0; i < nb; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nb; i++) { 4 2 6
784 4 ▶ int q = 1000, order = 0, spec = 0;
Branch: 2 branches
BranchTrueFalseTotal
int q = 1000, order = 0, spec = 0; 3 1 4
int q = 1000, order = 0, spec = 0; 3 0 3
785 4 ▶ if (have_accept) {
Branch: 3 branches
BranchTrueFalseTotal
if (have_accept) { 3 1 4
if (have_accept) { 3 0 3
if (have_accept) { 0 3 3
786 23 spec = pa_match(aTHX_ ranges, nr, bt[i], btl[i],
787 bs[i], bsl[i], &q, &order);
788 if (spec < 0 || q <= 0) { bq[i] = -1; bspec[i] = -1; continue; }
789 }
790 bq[i] = q; bspec[i] = spec;
791 # uncoverable condition left count:3 class:ignore_covered_err
792 # uncoverable condition left count:4 class:ignore_covered_err
793 # uncoverable condition left count:5 class:ignore_covered_err
794 23 ▶ # uncoverable condition left count:6 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition left count:6 class:ignore_covered_err 21 2 23
# uncoverable condition left count:6 class:ignore_covered_err 4 17 21
# uncoverable condition left count:6 class:ignore_covered_err 1 3 4
795 1 # uncoverable condition right count:6 class:ignore_covered_err
796 1 # uncoverable condition left count:7 class:ignore_covered_err
797 1 ▶ if (best < 0
Branch: 1 branch
BranchTrueFalseTotal
if (best < 0 0 1 1
798 1 || q > best_q
799 1 ▶ || (q == best_q && spec > best_spec)
Branch: 2 branches
BranchTrueFalseTotal
|| (q == best_q && spec > best_spec) 1 0 1
|| (q == best_q && spec > best_spec) 0 1 1
800 1 ▶ || (q == best_q && spec == best_spec && order < best_order)) {
Branch: 2 branches
BranchTrueFalseTotal
|| (q == best_q && spec == best_spec && order < best_order)) { 1 0 1
|| (q == best_q && spec == best_spec && order < best_order)) { 1 0 1
801 1 best = i; best_q = q; best_spec = spec; best_order = order;
802 1 }
803 1 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 1 0 1
804 2 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
2 0 2
805 2 ▶ /* An indifferent client (wildcard or no Accept) that itself sent one
Branch: 1 branch
BranchTrueFalseTotal
/* An indifferent client (wildcard or no Accept) that itself sent one 2 0 2
806 2 ▶ * of the offered types wants that type back - a JSON POST from curl
Branch: 2 branches
BranchTrueFalseTotal
* of the offered types wants that type back - a JSON POST from curl 1 1 2
* of the offered types wants that type back - a JSON POST from curl 1 0 1
807 1 ▶ * with no Accept should not be answered in HTML. Only a branch as
Branch: 2 branches
BranchTrueFalseTotal
* with no Accept should not be answered in HTML. Only a branch as 1 0 1
* with no Accept should not be answered in HTML. Only a branch as 1 0 1
808 1 * acceptable as the provisional winner may take over. */
809 1 if (best >= 0 && best_spec == 0 && ctype_sv) {
810 STRLEN cl; const char *cs = SvPV_const(ctype_sv, cl);
811 const char *ce = (const char *)memchr(cs, ';', cl);
812 STRLEN ml = ce ? (STRLEN)(ce - cs) : cl; /* # uncoverable condition left count:1 class:ignore_covered_err */
813 const char *slash = (const char *)memchr(cs, '/', ml);
814 # uncoverable branch true class:ignore_covered_err
815 # uncoverable condition left count:1 class:ignore_covered_err
816 23 # uncoverable condition right count:1 class:ignore_covered_err
817 23 # uncoverable condition left count:2 class:ignore_covered_err
818 while (ml && pa_ws(cs[ml - 1])) ml--;
819 23 ▶ # uncoverable branch false
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false 23 0 23
820 # uncoverable condition right count:1 class:ignore_covered_err
821 # uncoverable condition right count:2 class:ignore_covered_err
822 24 ▶ if (slash && slash > cs) {
Branch: 2 branches
BranchTrueFalseTotal
if (slash && slash > cs) { 2 21 23
if (slash && slash > cs) { 1 1 2
823 1 STRLEN tl1 = (STRLEN)(slash - cs);
824 2 ▶ const char *s1 = slash + 1;
Branch: 1 branch
BranchTrueFalseTotal
const char *s1 = slash + 1; 1 0 1
825 STRLEN sl1 = ml > tl1 ? ml - tl1 - 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
826 # uncoverable branch false
827 # uncoverable condition right count:1 class:ignore_covered_err
828 1 for (i = 0; i < nb; i++) {
829 # uncoverable condition right count:1 class:ignore_covered_err
830 # uncoverable condition right count:3 class:ignore_covered_err
831 22 ▶ # uncoverable condition right count:4 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:4 class:ignore_covered_err 21 1 22
832 22 # uncoverable condition right count:5 class:ignore_covered_err
833 22 if (bq[i] == best_q
834 22 ▶ && btl[i] == tl1 && foldEQ(bt[i], cs, (I32)tl1)
Branch: 2 branches
BranchTrueFalseTotal
&& btl[i] == tl1 && foldEQ(bt[i], cs, (I32)tl1) 0 22 22
&& btl[i] == tl1 && foldEQ(bt[i], cs, (I32)tl1) 0 22 22
835 22 && bsl[i] == sl1 && foldEQ(bs[i], s1, (I32)sl1)) {
836 22 best = i;
837 22 ▶ break;
Branch: 1 branch
BranchTrueFalseTotal
break; 22 0 22
838 22 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 22 0 22
839 }
840 }
841 }
842  
843 { /* every outcome of a negotiation varies on Accept */
844 SV *res = pcx_force(aTHX_ av, PCX_RES, "Punk::Response", NULL);
845 AV *rav = pcx_res_av(aTHX_ av);
846 PERL_UNUSED_VAR(res);
847 # uncoverable branch false
848 # uncoverable condition right count:1 class:ignore_covered_err
849 if (rav) pa_vary_accept(aTHX_ punk_res_headers(aTHX_ rav));
850 }
851  
852 if (best < 0 && !any_cb) {
853 AV *rav = pcx_res_av(aTHX_ av);
854 RETVAL = punk_triplet(aTHX_ 406,
855 sv_2mortal(newSVpvs("application/json")),
856 newSVpvs("{\"errors\":[{\"message\":"
857 "\"Not Acceptable\"}]}"),
858 rav ? punk_res_headers(aTHX_ rav) : NULL);
859 }
860 else {
861 SV *cb = best >= 0 ? cbs[best] : any_cb;
862 dSP; int count;
863 ENTER; SAVETMPS;
864 PUSHMARK(SP); EXTEND(SP, 1); PUSHs(self); PUTBACK;
865 count = call_sv(cb, G_SCALAR);
866 SPAGAIN;
867 RETVAL = count > 0 ? newSVsv(POPs) : newSV(0); /* # uncoverable condition right count:1 class:ignore_covered_err */
868 PUTBACK; FREETMPS; LEAVE;
869 }
870 #undef PRT_MAX
871 }
872 OUTPUT:
873 RETVAL
874  
875 # ---- pending response state --------------------------------------------------
876  
877 # no args -> the pending status (or undef); with args -> set it, chainable.
878 SV *
879 status(self, ...)
880 SV *self
881 CODE:
882 {
883 27 AV *av = pcx_av(aTHX_ self);
884 27 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 26 1 27
885 26 SV *sval = ST(1); /* capture before forcing/pushing */
886 26 (void)ps_status_arg(aTHX_ sval);
887 18 SV *res = pcx_force(aTHX_ av, PCX_RES, "Punk::Response", NULL);
888 18 (void)pcx_call_meth(aTHX_ res, "status", &sval, 1, 0);
889 18 RETVAL = newSVsv(self);
890 }
891 else {
892 1 AV *res = pcx_res_av(aTHX_ av);
893 1 IV st = pcx_res_status(aTHX_ res);
894 1 ▶ RETVAL = st ? newSViv(st) : &PL_sv_undef; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = st ? newSViv(st) : &PL_sv_undef; /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 1 1
895 }
896 }
897 OUTPUT:
898 RETVAL
899  
900 # header($name) -> read; header(Name => $v, ...) / no args -> set, chainable.
901 SV *
902 header(self, ...)
903 SV *self
904 CODE:
905 {
906 104 AV *av = pcx_av(aTHX_ self);
907 104 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 0 104 104
# uncoverable condition right count:1 class:ignore_covered_err 0 0 0
908 104 # uncoverable condition left count:2 class:ignore_covered_err
909 # uncoverable condition right count:2 class:ignore_covered_err
910 int set = (items > 2 || items == 1);
911 int nargs = items - 1, i;
912 104 ▶ SV **argv, *res, *r;
Branch: 1 branch
BranchTrueFalseTotal
SV **argv, *res, *r; 104 0 104
913 /* captured before the stack is reused, and in a mortal: the response
914 312 ▶ * croaks on a header it will not send, and this must go with it */
Branch: 1 branch
BranchTrueFalseTotal
* croaks on a header it will not send, and this must go with it */ 208 104 312
915 104 argv = (SV **)SvPVX(sv_2mortal(
916 104 newSV((nargs > 0 ? nargs : 1) * sizeof(SV *))));
917 99 ▶ for (i = 0; i < nargs; i++) argv[i] = ST(i + 1);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nargs; i++) argv[i] = ST(i + 1); 99 0 99
918 0 ▶ res = pcx_force(aTHX_ av, PCX_RES, "Punk::Response", NULL);
Branch: 1 branch
BranchTrueFalseTotal
res = pcx_force(aTHX_ av, PCX_RES, "Punk::Response", NULL); 0 0 0
919 r = pcx_call_meth(aTHX_ res, "header", argv, nargs, set ? 0 : 1);
920 # uncoverable branch false
921 # uncoverable condition right count:1 class:ignore_covered_err
922 if (set) RETVAL = newSVsv(self);
923 # uncoverable statement
924 # uncoverable condition left count:1
925 # uncoverable condition right count:1
926 else RETVAL = r ? r : &PL_sv_undef;
927 }
928 OUTPUT:
929 RETVAL
930  
931 # cookie($name) reads (via the request); cookie($name => $value, %opts) sets a
932 # Set-Cookie on the response (undef value deletes). Set form chains. Options
933 # may also arrive as one trailing hashref, in either direction - which is how
934 # the read form takes any at all: cookie('theme', { signed => 1 }).
935 #
936 # `signed => 1` signs with the SESSION's machinery and the session's secret,
937 # deliberately: pk_session_sign/verify already exist, the verify is already
938 # constant-time, and if `secret` ever grows rotation (a list: newest signs,
939 # all verify) signed cookies inherit it by riding the same key path. Do not
940 # build a second HMAC or a second key schedule here.
941 SV *
942 cookie(self, ...)
943 SV *self
944 CODE:
945 {
946 42 AV *av = pcx_av(aTHX_ self);
947 25 ▶ int read_opts = items == 3 && SvROK(ST(2))
Branch: 1 branch
BranchTrueFalseTotal
int read_opts = items == 3 && SvROK(ST(2)) 12 13 25
948 67 ▶ && SvTYPE(SvRV(ST(2))) == SVt_PVHV;
Branch: 2 branches
BranchTrueFalseTotal
&& SvTYPE(SvRV(ST(2))) == SVt_PVHV; 25 17 42
&& SvTYPE(SvRV(ST(2))) == SVt_PVHV; 12 0 12
949 42 ▶ if (items <= 2 || read_opts) { /* read via the request */
Branch: 2 branches
BranchTrueFalseTotal
if (items <= 2 || read_opts) { /* read via the request */ 40 2 42
if (items <= 2 || read_opts) { /* read via the request */ 12 28 40
950 14 SV *req = pcx_force(aTHX_ av, PCX_REQ, "Punk::Request",
951 pcx_get(aTHX_ av, PCX_ENV));
952 SV *argv[1], *r;
953 14 ▶ argv[0] = items >= 2 ? ST(1) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
argv[0] = items >= 2 ? ST(1) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ 14 0 14
954 14 r = pcx_call_meth(aTHX_ req, "cookie", argv, 1, 1);
955 14 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 12 2 14
# uncoverable condition right count:2 class:ignore_covered_err 12 0 12
# uncoverable condition right count:2 class:ignore_covered_err 12 0 12
956 12 # uncoverable condition right count:3 class:ignore_covered_err
957 12 ▶ if (read_opts && r && SvOK(r)) {
Branch: 3 branches
BranchTrueFalseTotal
if (read_opts && r && SvOK(r)) { 12 0 12
if (read_opts && r && SvOK(r)) { 12 0 12
if (read_opts && r && SvOK(r)) { 12 0 12
958 12 SV **sg = hv_fetchs((HV *)SvRV(ST(2)), "signed", 0);
959 12 # uncoverable branch false
960 12 # uncoverable condition right count:1 class:ignore_covered_err
961 12 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 11 1 12
962 # uncoverable condition right count:3 class:ignore_covered_err
963 if (sg && *sg && SvTRUE(*sg)) {
964 HV *scfg = ps_cfg(aTHX_ self);
965 12 ▶ STRLEN kl = 0;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl = 0; 1 11 12
966 1 const char *key = scfg
967 ? ps_cfg_str(aTHX_ scfg, "secret", "", &kl) : "";
968 11 STRLEN cl;
969 11 const char *cv2;
970 11 SV *payload;
971 11 if (!kl)
972 11 ▶ croak("Punk: signed cookies sign with the session's "
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk: signed cookies sign with the session's " 7 4 11
973 "secret - add `session secret => ...`");
974 cv2 = SvPV_const(r, cl);
975 payload = pk_session_verify(aTHX_ cv2, cl, key, kl);
976 SvREFCNT_dec(r);
977 r = NULL;
978 if (payload) {
979 /* The NAME is under the MAC. A signed value the
980 7 * client moved onto another cookie decodes to the
981 7 ▶ * wrong `name=` prefix and fails closed, like any
Branch: 1 branch
BranchTrueFalseTotal
* wrong `name=` prefix and fails closed, like any 7 0 7
982 7 ▶ * other tamper - without this, any two signed
Branch: 3 branches
BranchTrueFalseTotal
* other tamper - without this, any two signed 7 0 7
* other tamper - without this, any two signed 6 1 7
* other tamper - without this, any two signed 6 0 6
983 6 * cookies could be swapped for each other and both
984 7 * would verify. */
985 STRLEN pl, nl2;
986 const char *p = SvPV_const(payload, pl);
987 const char *n2 = SvPV_const(ST(1), nl2);
988 13 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 8 5 13
989 # uncoverable condition right count:3 class:ignore_covered_err
990 if (pl > nl2 && memEQ(p, n2, nl2) && p[nl2] == '=')
991 28 r = newSVpvn(p + nl2 + 1, pl - nl2 - 1);
992 28 SvREFCNT_dec(payload);
993 }
994 35 ▶ }
Branch: 3 branches
BranchTrueFalseTotal
} 7 21 28
} 7 0 7
} 7 0 7
995 7 }
996 RETVAL = r ? r : &PL_sv_undef;
997 7 }
998 17 ▶ else { /* set */
Branch: 1 branch
BranchTrueFalseTotal
else { /* set */ 10 7 17
999 10 ▶ SV *name = ST(1), *value = ST(2), *ck, *res, *hargv[2], *r;
Branch: 2 branches
BranchTrueFalseTotal
SV *name = ST(1), *value = ST(2), *ck, *res, *hargv[2], *r; 10 0 10
SV *name = ST(1), *value = ST(2), *ck, *res, *hargv[2], *r; 0 10 10
1000 HV *opts = (HV *)sv_2mortal((SV *)newHV());
1001 int i;
1002 # uncoverable condition right count:2 class:ignore_covered_err
1003 32 ▶ # uncoverable condition right count:3 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:3 class:ignore_covered_err 11 21 32
1004 11 if (items == 4 && SvROK(ST(3)) && SvTYPE(SvRV(ST(3))) == SVt_PVHV) {
1005 11 HV *oh = (HV *)SvRV(ST(3));
1006 HE *he;
1007 hv_iterinit(oh);
1008 while ((he = hv_iternext(oh)))
1009 28 (void)hv_store_ent(opts, HeSVKEY_force(he),
1010 28 ▶ newSVsv(HeVAL(he)), 0);
Branch: 5 branches
BranchTrueFalseTotal
newSVsv(HeVAL(he)), 0); 8 20 28
newSVsv(HeVAL(he)), 0); 8 0 8
newSVsv(HeVAL(he)), 0); 8 0 8
newSVsv(HeVAL(he)), 0); 8 0 8
newSVsv(HeVAL(he)), 0); 8 0 8
1011 8 }
1012 8 else {
1013 8 for (i = 3; i + 1 < items; i += 2) {
1014 8 ▶ STRLEN kl; const char *k = SvPV_const(ST(i), kl);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl; const char *k = SvPV_const(ST(i), kl); 7 1 8
1015 (void)hv_store(opts, k, (I32)kl, newSVsv(ST(i + 1)), 0);
1016 8 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 1 7 8
1017 1 }
1018 {
1019 7 SV **sg = hv_fetchs(opts, "signed", 0);
1020 7 # uncoverable condition right count:2 class:ignore_covered_err
1021 7 # uncoverable condition right count:3 class:ignore_covered_err
1022 7 # uncoverable condition right count:4 class:ignore_covered_err
1023 # uncoverable condition right count:5 class:ignore_covered_err
1024 if (sg && *sg && SvTRUE(*sg) && value && SvOK(value)) {
1025 HV *scfg = ps_cfg(aTHX_ self);
1026 27 STRLEN kl = 0;
1027 18 const char *key = scfg
1028 18 ? ps_cfg_str(aTHX_ scfg, "secret", "", &kl) : "";
1029 18 SV *payload;
1030 18 if (!kl)
1031 18 ▶ croak("Punk: signed cookies sign with the session's "
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk: signed cookies sign with the session's " 0 18 18
1032 18 "secret - add `session secret => ...`");
1033 payload = sv_2mortal(newSVsv(name));
1034 sv_catpvs(payload, "=");
1035 sv_catsv(payload, value);
1036 value = sv_2mortal(pk_session_sign(aTHX_ payload,
1037 key, kl));
1038 }
1039 }
1040 ck = sv_2mortal(pk_build_cookie(aTHX_ name, value, opts));
1041 res = pcx_force(aTHX_ av, PCX_RES, "Punk::Response", NULL);
1042 hargv[0] = sv_2mortal(newSVpvs("Set-Cookie"));
1043 hargv[1] = ck;
1044 r = pcx_call_meth(aTHX_ res, "header", hargv, 2, 0);
1045 # uncoverable branch true
1046 # uncoverable condition left count:1 class:ignore_covered_err
1047 if (r) SvREFCNT_dec(r);
1048 RETVAL = newSVsv(self);
1049 }
1050 }
1051 OUTPUT:
1052 RETVAL
1053  
1054 # upload($name): the Punk::Upload for that multipart field, via the request.
1055 SV *
1056 upload(self, name)
1057 SV *self
1058 SV *name
1059 CODE:
1060 {
1061 1 AV *av = pcx_av(aTHX_ self);
1062 1 SV *req = pcx_force(aTHX_ av, PCX_REQ, "Punk::Request",
1063 pcx_get(aTHX_ av, PCX_ENV));
1064 SV *argv[1], *r;
1065 1 argv[0] = name;
1066 1 r = pcx_call_meth(aTHX_ req, "upload", argv, 1, 1);
1067 1 ▶ RETVAL = r ? r : &PL_sv_undef; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = r ? r : &PL_sv_undef; /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 0 1
1068 }
1069 OUTPUT:
1070 RETVAL
1071  
1072 # csrf_token: the live single-use token, minted into the session on first ask
1073 # (which dirties the session, so the cookie follows). csrf_field is the hidden
1074 # input a form needs, escaped and ready to drop into a template with `raw`.
1075 SV *
1076 csrf_token(self)
1077 SV *self
1078 ALIAS:
1079 csrf_field = 1
1080 CODE:
1081 {
1082 SV *tok;
1083 21 ▶ if (!pcf_cfg(aTHX_ self))
Branch: 1 branch
BranchTrueFalseTotal
if (!pcf_cfg(aTHX_ self)) 1 20 21
1084 1 croak("Punk: no csrf configured (add a `csrf` keyword)");
1085 20 tok = pcf_token(aTHX_ self, 1);
1086 20 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 20 20
1087 20 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 16 4 20
1088 if (!tok) { RETVAL = newSV(0); }
1089 4 else if (!ix) { RETVAL = newSVsv(tok); }
1090 else {
1091 4 HV *cfg = pcf_cfg(aTHX_ self);
1092 4 STRLEN fl;
1093 4 const char *field = ps_cfg_str(aTHX_ cfg, "field", "_csrf", &fl);
1094 4 SV *out = newSVpvs("<input type=\"hidden\" name=\"");
1095 pcf_attr_escape(aTHX_ out, field, fl);
1096 sv_catpvs(out, "\" value=\"");
1097 4 {
1098 4 STRLEN tl;
1099 const char *tv = SvPV_const(tok, tl);
1100 4 pcf_attr_escape(aTHX_ out, tv, tl);
1101 4 }
1102 sv_catpvs(out, "\">");
1103 RETVAL = out;
1104 }
1105 }
1106 OUTPUT:
1107 RETVAL
1108  
1109 # session: the signed cookie-backed hashref (loaded once, written back if
1110 # changed). session_expire logs out - empties it and deletes the cookie.
1111 SV *
1112 session(self)
1113 SV *self
1114 CODE:
1115 133 RETVAL = ps_load(aTHX_ self);
1116 OUTPUT:
1117 RETVAL
1118  
1119 SV *
1120 session_expire(self)
1121 SV *self
1122 CODE:
1123 {
1124 13 HV *stash = ps_stash(aTHX_ pcx_av(aTHX_ self));
1125 13 (void)hv_stores(stash, "punk.session", newRV_noinc((SV *)newHV()));
1126 13 (void)hv_stores(stash, "punk.session.expire", newSViv(1));
1127 13 RETVAL = newSVsv(self);
1128 }
1129 OUTPUT:
1130 RETVAL
1131  
1132 # session_rotate: keep the session, give it a new name.
1133 #
1134 # Call it at the privilege boundary - a login, an elevation - and nowhere else.
1135 # Session fixation is the attack: somebody plants a known id in a victim's
1136 # browser and waits for them to log in. If logging in writes the user into the
1137 # session the attacker planted, the attacker's id is now an authenticated
1138 # session. The cookie session is immune by accident, because its value changes
1139 # wholesale when its contents do. A stored session is not: the id survives the
1140 # login unless something changes it.
1141 #
1142 # $c->login calls it, because a login is that boundary by definition, and
1143 # that is the only automatic call. "Privilege changed" is application
1144 # knowledge, and every candidate for guessing it is wrong - rotating whenever
1145 # the session gains a key rotates on a shopping basket, rotating on every POST
1146 # is a store write per form submission, and watching for a key named `user_id`
1147 # is a convention nobody agreed to.
1148 SV *
1149 session_rotate(self)
1150 SV *self
1151 CODE:
1152 {
1153 10 AV *av = pcx_av(aTHX_ self);
1154 10 HV *stash = ps_stash(aTHX_ av);
1155 /* Load first: the contents have to survive the rotation, and a
1156 * handler that rotates before touching $c->session would otherwise
1157 * leave the write-back with no session to write. */
1158 10 SV *sess = ps_load(aTHX_ self);
1159 /* a new name, and a new CSRF token to go with it */
1160 10 ▶ # uncoverable branch false
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable branch false 10 0 10
# uncoverable branch false 10 0 10
# uncoverable branch false 10 0 10
1161 10 # uncoverable condition right count:1 class:ignore_covered_err
1162 10 # uncoverable condition right count:2 class:ignore_covered_err
1163 # uncoverable condition right count:3 class:ignore_covered_err
1164 if (sess && SvROK(sess) && SvTYPE(SvRV(sess)) == SVt_PVHV)
1165 (void)hv_delete((HV *)SvRV(sess), "punk.csrf", 9, G_DISCARD);
1166 10 SvREFCNT_dec(sess);
1167 10 /* The old id to retire, or undef when this session is new, which
1168 * still marks the write-back as rotating so an unchanged session is
1169 * written under the new name. */
1170 ps_mark_rotate(aTHX_ self, ps_cfg(aTHX_ self), stash);
1171 RETVAL = newSVsv(self);
1172 }
1173 OUTPUT:
1174 RETVAL
1175  
1176 # flash: one-request messages over the session (punk_flash.h). Any flash call
1177 # rotates the inbound hash out of the session, so the consuming response's
1178 # cookie no longer carries it; writes fill a fresh outbound hash the NEXT
1179 # request will read. No args: the whole inbound hashref (the template
1180 # hand-off). One arg: one inbound value. Pairs: set outbound, chainable.
1181 SV *
1182 flash(self, ...)
1183 SV *self
1184 CODE:
1185 {
1186 38 ▶ if (items == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 1) { 3 35 38
1187 3 RETVAL = newRV_inc((SV *)pf_inbound(aTHX_ self));
1188 }
1189 35 ▶ else if (items == 2) {
Branch: 1 branch
BranchTrueFalseTotal
else if (items == 2) { 20 15 35
1190 20 HV *in = pf_inbound(aTHX_ self);
1191 20 HE *he = hv_fetch_ent(in, ST(1), 0, 0);
1192 20 ▶ RETVAL = he ? newSVsv(HeVAL(he)) : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = he ? newSVsv(HeVAL(he)) : newSV(0); 13 7 20
1193 }
1194 15 ▶ else if ((items - 1) % 2) {
Branch: 1 branch
BranchTrueFalseTotal
else if ((items - 1) % 2) { 1 14 15
1195 1 croak("Punk: flash(key => value, ...) takes pairs");
1196 }
1197 else {
1198 I32 i;
1199 HV *out;
1200 14 (void)pf_inbound(aTHX_ self); /* rotate before writing */
1201 13 out = pf_outbound(aTHX_ self);
1202 31 ▶ for (i = 1; i + 1 < items; i += 2)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) 18 13 31
1203 18 (void)hv_store_ent(out, ST(i), newSVsv(ST(i + 1)), 0);
1204 13 RETVAL = newSVsv(self);
1205 }
1206 }
1207 OUTPUT:
1208 RETVAL
1209  
1210 # validate: collecting request validation, all in C (punk_validate.h, on
1211 # the JSON::Schema::Fast C ABI). With a schema, runs a validation and
1212 # returns the Punk::Validate::Result (also stashed at punk.validation).
1213 # With no arguments, the reader: the last Result this request produced -
1214 # a route-level validate ran before the handler, so this is how the
1215 # handler collects its outcome - or undef.
1216 SV *
1217 validate(self, schema = &PL_sv_undef, source = &PL_sv_undef)
1218 SV *self
1219 SV *schema
1220 SV *source
1221 CODE:
1222 {
1223 717 ▶ if (items == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 1) { 4 713 717
1224 4 HV *stash = ps_stash(aTHX_ pcx_av(aTHX_ self));
1225 4 SV **v = hv_fetchs(stash, "punk.validation", 0);
1226 4 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 3 1 4
# uncoverable condition right count:2 class:ignore_covered_err 3 0 3
# uncoverable condition right count:2 class:ignore_covered_err 3 0 3
1227 # uncoverable condition right count:3 class:ignore_covered_err
1228 RETVAL = (v && *v && SvOK(*v)) ? newSVsv(*v) : newSV(0);
1229 713 }
1230 else {
1231 RETVAL = pv_validate(aTHX_ self, schema, source);
1232 }
1233 }
1234 OUTPUT:
1235 RETVAL
1236  
1237 # flash_keep: re-arm this request's inbound flash for one more request - the
1238 # redirect-through-a-redirect case. Chainable.
1239 SV *
1240 flash_keep(self)
1241 SV *self
1242 CODE:
1243 {
1244 1 HV *in = pf_inbound(aTHX_ self);
1245 1 HV *out = pf_outbound(aTHX_ self);
1246 HE *he;
1247 1 hv_iterinit(in);
1248 3 ▶ while ((he = hv_iternext(in))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(in))) { 2 1 3
1249 2 (void)hv_store_ent(out, hv_iterkeysv(he),
1250 newSVsv(HeVAL(he)), 0);
1251 }
1252 1 RETVAL = newSVsv(self);
1253 }
1254 OUTPUT:
1255 RETVAL
1256  
1257 # response state the finish path folds in without forcing a response object
1258 SV *
1259 _status(self)
1260 SV *self
1261 CODE:
1262 {
1263 2164 AV *res = pcx_res_av(aTHX_ pcx_av(aTHX_ self));
1264 2164 ▶ SV **e = res ? av_fetch(res, PS_STATUS, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **e = res ? av_fetch(res, PS_STATUS, 0) : NULL; 2 2162 2164
1265 2164 ▶ RETVAL = (e && *e) ? newSVsv(*e) : &PL_sv_undef; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (e && *e) ? newSVsv(*e) : &PL_sv_undef; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 2163 2164
RETVAL = (e && *e) ? newSVsv(*e) : &PL_sv_undef; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
1266 }
1267 OUTPUT:
1268 RETVAL
1269  
1270 SV *
1271 _headers(self)
1272 SV *self
1273 CODE:
1274 {
1275 2166 AV *res = pcx_res_av(aTHX_ pcx_av(aTHX_ self));
1276 2166 RETVAL = res ? newRV_inc((SV *)punk_res_headers(aTHX_ res))
1277 2166 ▶ : newRV_noinc((SV *)newAV());
Branch: 1 branch
BranchTrueFalseTotal
: newRV_noinc((SV *)newAV()); 2 2164 2166
1278 }
1279 OUTPUT:
1280 RETVAL
1281  
1282 # ---- abuse controls: Hyperman's v3 arena via the ABI (punk_hm) --------------
1283 #
1284 # These reach the shared denylist / rate counters Hyperman maps before it forks
1285 # its workers. They FAIL OPEN: with no Hyperman >= ABI v3 under us (plackup, an
1286 # older server) punk_hm() is NULL, block_ip is a no-op returning 0 and rate_hit
1287 # reports "allowed", so rate limiting is never the reason a request is refused.
1288  
1289 # $c->block_ip([$ip [, $ttl]]) / $c->unblock_ip([$ip])
1290 # Denylist (or lift) an IP at the edge; $ip defaults to this request's
1291 # REMOTE_ADDR, $ttl seconds (0 = permanent). Returns 1 if the edge arena is
1292 # present (the change took), else 0.
1293 IV
1294 block_ip(self, ...)
1295 SV *self
1296 ALIAS:
1297 unblock_ip = 1
1298 CODE:
1299 {
1300 7 const hm_abi *A = punk_hm(aTHX);
1301 7 const char *ip = NULL;
1302 7 HV *cenv = NULL;
1303 {
1304 7 AV *av = pcx_av(aTHX_ self);
1305 7 SV **e = av_fetch(av, PCX_ENV, 0);
1306 7 ▶ # uncoverable branch false
Branch: 4 branches
BranchTrueFalseTotal
# uncoverable branch false 7 0 7
# uncoverable branch false 7 0 7
# uncoverable branch false 7 0 7
# uncoverable branch false 7 0 7
1307 7 # uncoverable condition right count:1 class:ignore_covered_err
1308 # uncoverable condition right count:2 class:ignore_covered_err
1309 7 ▶ # uncoverable condition right count:3 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:3 class:ignore_covered_err 5 2 7
# uncoverable condition right count:3 class:ignore_covered_err 5 0 5
1310 10 # uncoverable condition right count:4 class:ignore_covered_err
1311 2 ▶ if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 2 0 2
1312 2 cenv = (HV *)SvRV(*e);
1313 4 ▶ }
Branch: 3 branches
BranchTrueFalseTotal
} 2 0 2
} 2 0 2
} 2 0 2
1314 # uncoverable branch false class:ignore_covered_err
1315 # uncoverable condition right count:1 class:ignore_covered_err
1316 # uncoverable condition right count:2 class:ignore_covered_err
1317 if (items > 1 && SvOK(ST(1))) {
1318 ip = SvPV_nolen(ST(1));
1319 # uncoverable branch false class:ignore_covered_err
1320 # uncoverable statement class:ignore_covered_err
1321 7 ▶ # uncoverable branch true class:ignore_covered_err
Branch: 4 branches
BranchTrueFalseTotal
# uncoverable branch true class:ignore_covered_err 5 2 7
# uncoverable branch true class:ignore_covered_err 5 0 5
# uncoverable branch true class:ignore_covered_err 5 0 5
# uncoverable branch true class:ignore_covered_err 5 0 5
1322 5 # uncoverable condition left count:1 class:ignore_covered_err
1323 5 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 2 3 5
# uncoverable condition right count:1 class:ignore_covered_err 2 0 2
# uncoverable condition right count:1 class:ignore_covered_err 2 0 2
1324 } else if (cenv) {
1325 2 SV **r = hv_fetchs(cenv, "REMOTE_ADDR", 0); /* # uncoverable statement class:ignore_covered_err */
1326 2 ▶ # uncoverable branch false class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable branch false class:ignore_covered_err 2 0 2
# uncoverable branch false class:ignore_covered_err 1 1 2
# uncoverable branch false class:ignore_covered_err 1 0 1
1327 1 # uncoverable statement class:ignore_covered_err
1328 # uncoverable branch true class:ignore_covered_err
1329 # uncoverable condition left count:1 class:ignore_covered_err
1330 # uncoverable condition right count:1 class:ignore_covered_err
1331 # uncoverable condition left count:2 class:ignore_covered_err
1332 # uncoverable condition right count:2 class:ignore_covered_err
1333 6 ▶ # uncoverable condition left count:3 class:ignore_covered_err
Branch: 4 branches
BranchTrueFalseTotal
# uncoverable condition left count:3 class:ignore_covered_err 5 1 6
# uncoverable condition left count:3 class:ignore_covered_err 5 0 5
# uncoverable condition left count:3 class:ignore_covered_err 5 0 5
# uncoverable condition left count:3 class:ignore_covered_err 0 5 5
1334 1 # uncoverable condition right count:3 class:ignore_covered_err
1335 5 ▶ if (r && *r && SvOK(*r)) ip = SvPV_nolen(*r);
Branch: 1 branch
BranchTrueFalseTotal
if (r && *r && SvOK(*r)) ip = SvPV_nolen(*r); 2 3 5
1336 2 }
1337 2 /* Behind a proxy, banning the address the socket came from bans the
1338 * load balancer and takes the site down - and punk.peer_addr only
1339 3 ▶ * exists when a `proxy` policy resolved this request, so this cannot
Branch: 2 branches
BranchTrueFalseTotal
* exists when a `proxy` policy resolved this request, so this cannot 1 2 3
* exists when a `proxy` policy resolved this request, so this cannot 1 0 1
1340 3 * fire on a directly-exposed app. Boot-time config cannot catch it;
1341 3 * a silent no-op would leave an operator believing they had banned
1342 * someone, which is worse than an error. */
1343 # uncoverable branch false class:ignore_covered_err
1344 # uncoverable condition right count:1 class:ignore_covered_err
1345 # uncoverable condition right count:2 class:ignore_covered_err
1346 # uncoverable condition right count:3 class:ignore_covered_err
1347 # uncoverable condition right count:4 class:ignore_covered_err
1348 if (ix == 0 && cenv && ip && *ip) {
1349 SV **pa = hv_fetchs(cenv, "punk.peer_addr", 0);
1350 # uncoverable branch false class:ignore_covered_err
1351 # uncoverable condition right count:1 class:ignore_covered_err
1352 # uncoverable condition right count:2 class:ignore_covered_err
1353 # uncoverable condition right count:3 class:ignore_covered_err
1354 if (pa && *pa && SvOK(*pa)) {
1355 STRLEN pl;
1356 const char *peer = SvPV_const(*pa, pl);
1357 # uncoverable condition right count:1 class:ignore_covered_err
1358 # uncoverable condition right count:3 class:ignore_covered_err
1359 if (pl && strlen(ip) == pl && memEQ(ip, peer, pl))
1360 croak("Punk: block_ip would denylist %s, which is the "
1361 "reverse proxy this request came through, not a "
1362 "client - that would take the site down. Ban the "
1363 "client address ($c->req->address) instead", ip);
1364 }
1365 }
1366 # uncoverable branch true class:ignore_covered_err
1367 # uncoverable condition left count:1 class:ignore_covered_err
1368 # uncoverable condition left count:2 class:ignore_covered_err
1369 # uncoverable condition left count:3 class:ignore_covered_err
1370 # uncoverable condition left count:4 class:ignore_covered_err
1371 if (!A || !A->deny_add || !ip || !*ip) {
1372 RETVAL = 0; /* # uncoverable statement class:ignore_covered_err */
1373 # uncoverable branch true class:ignore_covered_err
1374 # uncoverable condition left count:1 class:ignore_covered_err
1375 } else if (ix == 1) {
1376 A->deny_remove(ip); /* # uncoverable statement class:ignore_covered_err */
1377 RETVAL = 1; /* # uncoverable statement class:ignore_covered_err */
1378 } else {
1379 # uncoverable condition left count:1 class:ignore_covered_err
1380 # uncoverable condition left count:2 class:ignore_covered_err
1381 # uncoverable condition right count:2 class:ignore_covered_err
1382 long ttl = (items > 2 && SvOK(ST(2))) ? (long)SvIV(ST(2)) : 0;
1383 A->deny_add(ip, ttl);
1384 RETVAL = 1;
1385 }
1386 }
1387 OUTPUT:
1388 RETVAL
1389  
1390 # $c->rate_hit($key, $limit, $window) -> ($ok, $remaining, $reset)
1391 # Count one hit against the opaque $key under $limit per $window seconds.
1392 # $limit <= 0 is unlimited. Fail-open with no arena: (1, $limit-1, next-window).
1393 void
1394 # uncoverable statement class:ignore_covered_err
1395 # uncoverable subroutine class:ignore_covered_err
1396 rate_hit(self, key, limit, window)
1397 SV *self
1398 SV *key
1399 IV limit
1400 IV window
1401 PPCODE:
1402 {
1403 4 const hm_abi *A = punk_hm(aTHX); /* # uncoverable statement class:ignore_covered_err */
1404 STRLEN klen;
1405 4 const char *k = SvPV(key, klen); /* # uncoverable statement class:ignore_covered_err */
1406 4 IV rem = 0, reset = 0; /* # uncoverable statement class:ignore_covered_err */
1407 int ok;
1408 PERL_UNUSED_VAR(self);
1409 4 ▶ # uncoverable statement class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable statement class:ignore_covered_err 3 1 4
# uncoverable statement class:ignore_covered_err 3 0 3
1410 3 # uncoverable branch true class:ignore_covered_err
1411 # uncoverable branch false class:ignore_covered_err
1412 1 # uncoverable condition left count:1 class:ignore_covered_err
1413 1 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 1 0 1
1414 1 # uncoverable condition left count:2 class:ignore_covered_err
1415 1 # uncoverable condition right count:2 class:ignore_covered_err
1416 1 if (A && A->ratelimit_hit) {
1417 ok = A->ratelimit_hit(k, klen, limit, window, &rem, &reset); /* # uncoverable statement class:ignore_covered_err */
1418 4 ▶ } else {
Branch: 1 branch
BranchTrueFalseTotal
} else { 0 4 4
1419 4 ▶ long now = (long)time(NULL); /* # uncoverable statement class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
long now = (long)time(NULL); /* # uncoverable statement class:ignore_covered_err */ 0 4 4
1420 4 ▶ # uncoverable statement class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable statement class:ignore_covered_err 0 4 4
1421 # uncoverable condition left count:1 class:ignore_covered_err
1422 # uncoverable condition right count:1 class:ignore_covered_err
1423 long w = window > 0 ? window : 60;
1424 ok = 1; /* # uncoverable statement class:ignore_covered_err */
1425 rem = limit > 0 ? limit - 1 : -1; /* # uncoverable statement class:ignore_covered_err */
1426 reset = now - (now % w) + w; /* # uncoverable statement class:ignore_covered_err */
1427 }
1428 XPUSHs(sv_2mortal(newSViv(ok))); /* # uncoverable statement class:ignore_covered_err */
1429 XPUSHs(sv_2mortal(newSViv(rem))); /* # uncoverable statement class:ignore_covered_err */
1430 XPUSHs(sv_2mortal(newSViv(reset))); /* # uncoverable statement class:ignore_covered_err */
1431 }
1432  
1433 # ---- the cross-worker bus ---------------------------------------------------
1434  
1435 # publish($topic, $payload)
1436 #
1437 # 1 on the ring: every worker in the pool will see it
1438 # 0 local only - there is no pool (not under Hyperman, or a Hyperman
1439 # without the bus), so nobody else will
1440 # -1 refused: too big for a bus slot
1441 #
1442 # Three outcomes rather than true or false, because "the pool got it" and
1443 # "only I got it" are different facts, and an application that cannot tell
1444 # them apart cannot diagnose why the other workers stayed quiet.
1445 IV
1446 publish(self, topic, payload)
1447 SV *self
1448 SV *topic
1449 SV *payload
1450 CODE:
1451 PERL_UNUSED_VAR(self);
1452 1 ▶ RETVAL = punk_bus_app_publish(aTHX_ topic, payload);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = punk_bus_app_publish(aTHX_ topic, payload); 0 1 1
1453 OUTPUT:
1454 RETVAL
1455  
1456 # subscribe($topic, $cb, %opt)
1457 #
1458 # $cb is called as $cb->($topic, $payload) in this worker.
1459 #
1460 # `group => $name` is the only difference between the two delivery modes:
1461 # without it every worker's subscriber sees every message (what a fanout
1462 # wants), with it exactly one member of the named group sees each (work spread
1463 # across the pool, with no scheduler - a busy worker simply is not there to
1464 # claim).
1465 #
1466 # Register at BOOT, not per request. A subscription made inside a request
1467 # lands in one worker and lasts as long as that process, which is the same
1468 # class of mistake the bus exists to fix.
1469 IV
1470 subscribe(self, topic, cb, ...)
1471 SV *self
1472 SV *topic
1473 SV *cb
1474 CODE:
1475 {
1476 1 SV *group = &PL_sv_undef;
1477 int i;
1478 PERL_UNUSED_VAR(self);
1479 2 ▶ for (i = 3; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 3; i + 1 < items; i += 2) { 1 1 2
1480 1 const char *k = SvPV_nolen(ST(i));
1481 1 ▶ # uncoverable branch false
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false 1 0 1
1482 # uncoverable condition right count:1 class:ignore_covered_err
1483 1 ▶ if (strEQ(k, "group")) group = ST(i + 1);
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "group")) group = ST(i + 1); 0 1 1
1484 }
1485 RETVAL = punk_bus_app_subscribe(aTHX_ topic, cb, group);
1486 }
1487 OUTPUT:
1488 RETVAL
1489  
1490 # $c->cache / $c->cache($name) - a cache store.
1491 #
1492 # Built once, at to_app, and shared by every request on this worker: a store
1493 # constructed per request would be a cache that never hits.
1494 #
1495 # Named stores exist because a session cache and a rendered-page cache want
1496 # different budgets and different lifetimes, and sharing one means the big
1497 # cold thing evicts the small hot thing.
1498 SV *
1499 cache(self, name = NULL)
1500 SV *self
1501 SV *name
1502 CODE:
1503 {
1504 10 AV *av = pcx_av(aTHX_ self);
1505 10 SV *app = pcx_get(aTHX_ av, PCX_APP);
1506 STRLEN nl;
1507 8 ▶ const char *n = (name && SvOK(name)) ? SvPV_const(name, nl)
Branch: 1 branch
BranchTrueFalseTotal
const char *n = (name && SvOK(name)) ? SvPV_const(name, nl) 4 0 4
1508 10 ▶ : (nl = sizeof(K_DEFAULT) - 1,
Branch: 1 branch
BranchTrueFalseTotal
: (nl = sizeof(K_DEFAULT) - 1, 4 6 10
1509 K_DEFAULT);
1510 10 SV **slot = NULL, **built = NULL;
1511 10 ▶ # uncoverable branch false
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable branch false 10 0 10
# uncoverable branch false 10 0 10
# uncoverable branch false 10 0 10
1512 10 # uncoverable condition right count:1 class:ignore_covered_err
1513 10 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 9 1 10
# uncoverable condition right count:2 class:ignore_covered_err 9 0 9
# uncoverable condition right count:2 class:ignore_covered_err 9 0 9
1514 9 ▶ # uncoverable condition right count:3 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:3 class:ignore_covered_err 9 0 9
1515 9 if (app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)
1516 10 ▶ built = hv_fetchs((HV *)SvRV(app), "cache", 0);
Branch: 3 branches
BranchTrueFalseTotal
built = hv_fetchs((HV *)SvRV(app), "cache", 0); 8 2 10
built = hv_fetchs((HV *)SvRV(app), "cache", 0); 8 0 8
built = hv_fetchs((HV *)SvRV(app), "cache", 0); 0 8 8
1517 2 # uncoverable condition right count:2 class:ignore_covered_err
1518 8 # uncoverable condition right count:3 class:ignore_covered_err
1519 # uncoverable condition right count:4 class:ignore_covered_err
1520 if (built && *built && SvROK(*built)
1521 && SvTYPE(SvRV(*built)) == SVt_PVHV)
1522 slot = hv_fetch((HV *)SvRV(*built), n, (I32)nl, 0);
1523 # uncoverable condition left count:2 class:ignore_covered_err
1524 # uncoverable condition left count:3 class:ignore_covered_err
1525 if (!(slot && *slot && SvOK(*slot)))
1526 croak("Punk: no cache named '%s' (add a `cache` keyword)", n);
1527 RETVAL = newSVsv(*slot);
1528 }
1529 OUTPUT:
1530 RETVAL
1531  
1532 # after_response($code): run $code once this response has been handed to the
1533 # server, off the request's own path. The queue is per request (PCX_AFTER_RES);
1534 # `hook after_response` is the application-wide half, and both are drained
1535 # together by punk_afterres.h. Chains.
1536 SV *
1537 after_response(self, code)
1538 SV *self
1539 SV *code
1540 CODE:
1541 {
1542 10 AV *av = pcx_av(aTHX_ self);
1543 SV *q;
1544 10 ▶ if (!(SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVCV)) /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVCV)) /* # uncoverable condition left count:2 class:ignore_covered_err */ 8 2 10
if (!(SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVCV)) /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 8 8
1545 2 croak("Punk: after_response takes a code reference");
1546 8 q = pcx_get(aTHX_ av, PCX_AFTER_RES);
1547 8 ▶ # uncoverable condition left count:2 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition left count:2 class:ignore_covered_err 2 6 8
# uncoverable condition left count:2 class:ignore_covered_err 2 0 2
# uncoverable condition left count:2 class:ignore_covered_err 0 2 2
1548 6 # uncoverable condition left count:3 class:ignore_covered_err
1549 6 ▶ if (!(q && SvROK(q) && SvTYPE(SvRV(q)) == SVt_PVAV)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(q && SvROK(q) && SvTYPE(SvRV(q)) == SVt_PVAV)) { 0 6 6
1550 0 q = newRV_noinc((SV *)newAV());
1551 0 # uncoverable branch true
1552 # uncoverable condition left count:1 class:ignore_covered_err
1553 if (!av_store(av, PCX_AFTER_RES, q)) {
1554 8 SvREFCNT_dec(q); /* # uncoverable statement */
1555 8 croak("Punk: after_response could not queue"); /* # uncoverable statement */
1556 }
1557 }
1558 av_push((AV *)SvRV(q), newSVsv(code));
1559 RETVAL = newSVsv(self);
1560 }
1561 OUTPUT:
1562 RETVAL
« include/punk/punk_headers.h ↑ Summary include/punk/punk_dispatch.h »