include/punk/punk_context.h

filter: Statementstmt 96.9% 217 / 230 (6 uncoverable) Branchbran 68.2% 236 / 372 (34 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 79.2% 453 / 602 (40 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_app.h ↑ Summary include/punk/punk_state.h »
1 /* punk_context.h - the per-request context object, in C.
2 *
3 * Punk::Context is a blessed AV whose slots are defined here and nowhere else:
4 * this enum is the whole storage contract. Both faces of it are C - the plain
5 * accessors (env/app/_req/_res/stash_hv/openapi_params/match) and the hot
6 * methods (req/res/stash/param/json/text/html/...) are all XSUBs in
7 * xs/context.xs reading these slots directly, and _build fills them. There is
8 * no Perl-side class builder to keep in step, so lib/Punk/Context.pm is
9 * documentation only.
10 *
11 * Perl headers and punk_response.h (punk_triplet, the PS_* Response slots)
12 * must be in scope before this file.
13 */
14  
15 #ifndef PUNK_CONTEXT_H
16 #define PUNK_CONTEXT_H
17  
18 /* The slot layout. The accessor XSUB's ALIAS indices run in this same order
19 * (see xs/context.xs), which is what lets it map ix straight to a slot. */
20 enum {
21 PCX_ENV = 0,
22 PCX_APP = 1,
23 PCX_REQ = 2,
24 PCX_RES = 3,
25 PCX_STASH = 4,
26 PCX_OPENAPI = 5,
27 PCX_MATCH = 6,
28 PCX_UA = 7, /* memoised $c->ua; the agent itself lives on the app */
29 PCX_REQID = 8, /* Punk::Plugin::RequestId's id, when it is loaded */
30 PCX_AFTER_RES = 9, /* $c->after_response's queue, vivified on first use */
31 PCX_CANON = 10, /* set when the route's body is hashed for an ETag:
32 JSON is then encoded with its keys in order */
33 PCX_T0 = 11 /* Punk::Plugin::Metrics' request start, seconds;
34 negative for a request it does not count */
35 };
36  
37 902402 static AV *pcx_av(pTHX_ SV *self) {
38 902402 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) /* # uncoverable condition left count:1 class:ignore_covered_err */ 902402 0 902402
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 902401 902402
39 1 croak("Punk::Context: not a context object");
40 902401 return (AV *)SvRV(self);
41 }
42  
43 /* the slot value, or NULL when unset */
44 1469036 static SV *pcx_get(pTHX_ AV *av, I32 slot) {
45 1469036 SV **e = av_fetch(av, slot, 0);
46 /*
47 # uncoverable condition right count:2 class:ignore_covered_err
48 # uncoverable condition right count:3 class:ignore_covered_err */
49 1469036 ▶ return (e && *e && SvOK(*e)) ? *e : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && SvOK(*e)) ? *e : NULL; 804693 664343 1469036
return (e && *e && SvOK(*e)) ? *e : NULL; 804693 0 804693
return (e && *e && SvOK(*e)) ? *e : NULL; 804693 0 804693
50 }
51  
52 /* JSON for a response body (+1). Keys in hash order, which is whatever this
53 * process happens to hold - unless the route's body is about to be hashed
54 * into an ETag, where an order that differs between workers, hosts and
55 * restarts is a validator that never matches. */
56 106014 static SV *pcx_json(pTHX_ AV *av, SV *data) {
57 106014 SV *canon = pcx_get(aTHX_ av, PCX_CANON);
58 106014 ▶ if (canon && SvTRUE(canon)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (canon && SvTRUE(canon)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 8 106006 106014
if (canon && SvTRUE(canon)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 8 0 8
59 frj_opts o;
60 8 Zero(&o, 1, frj_opts);
61 8 o.canonical = 1;
62 8 return punk_frj(aTHX)->encode(aTHX_ data, &o);
63 }
64 106006 return punk_frj(aTHX)->encode(aTHX_ data, NULL);
65 }
66  
67 /* the Punk::Response AV at PCX_RES (its slots are the PS_* from
68 * punk_response.h), or NULL when the response was never forced. */
69 220498 static AV *pcx_res_av(pTHX_ AV *av) {
70 220498 SV *r = pcx_get(aTHX_ av, PCX_RES);
71 /*
72 # uncoverable condition right count:2 class:ignore_covered_err
73 # uncoverable condition right count:3 class:ignore_covered_err */
74 220498 ▶ return (r && SvROK(r) && SvTYPE(SvRV(r)) == SVt_PVAV) ? (AV *)SvRV(r) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (r && SvROK(r) && SvTYPE(SvRV(r)) == SVt_PVAV) ? (AV *)SvRV(r) : NULL; 226 220272 220498
return (r && SvROK(r) && SvTYPE(SvRV(r)) == SVt_PVAV) ? (AV *)SvRV(r) : NULL; 226 0 226
return (r && SvROK(r) && SvTYPE(SvRV(r)) == SVt_PVAV) ? (AV *)SvRV(r) : NULL; 226 0 226
75 }
76  
77 /* the pending response status (PS_STATUS), or 0 */
78 215858 static IV pcx_res_status(pTHX_ AV *res) {
79 215858 ▶ SV **e = res ? av_fetch(res, PS_STATUS, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **e = res ? av_fetch(res, PS_STATUS, 0) : NULL; 110 215748 215858
80 215858 ▶ return (e && *e && SvOK(*e)) ? SvIV(*e) : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && SvOK(*e)) ? SvIV(*e) : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 17 215841 215858
return (e && *e && SvOK(*e)) ? SvIV(*e) : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 17 0 17
return (e && *e && SvOK(*e)) ? SvIV(*e) : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 16 1 17
81 }
82  
83 /* the pending response header pair-AV (PS_HEADERS), or NULL */
84 197 static AV *pcx_res_headers(pTHX_ AV *res) {
85 197 ▶ SV **e = res ? av_fetch(res, PS_HEADERS, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV **e = res ? av_fetch(res, PS_HEADERS, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 197 0 197
86 182 ▶ return (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV)
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) 182 0 182
return (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) 182 0 182
return (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) 182 0 182
87 379 ▶ ? (AV *)SvRV(*e) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (AV *)SvRV(*e) : NULL; 182 15 197
88 }
89  
90 /* Lazily build the sub-object in a slot (Punk::Request / Punk::Response) with
91 * Class->new($arg?), caching it. Returns it borrowed (the slot owns it). */
92 7354 static SV *pcx_force(pTHX_ AV *av, I32 slot, const char *class, SV *arg) {
93 7354 SV *cur = pcx_get(aTHX_ av, slot);
94 7354 ▶ if (cur) return cur;
Branch: 1 branch
BranchTrueFalseTotal
if (cur) return cur; 1664 5690 7354
95 {
96 5690 dSP; SV *obj; int count;
97 5690 ENTER; SAVETMPS;
98 /*
99 # uncoverable condition left count:1 class:ignore_covered_err
100 # uncoverable condition left count:3 class:ignore_covered_err
101 # uncoverable condition left count:4 class:ignore_covered_err
102 # uncoverable condition right count:4 class:ignore_covered_err */
103 5690 ▶ PUSHMARK(SP); EXTEND(SP, arg ? 2 : 1);
Branch: 4 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, arg ? 2 : 1); 0 5690 5690
PUSHMARK(SP); EXTEND(SP, arg ? 2 : 1); 5487 203 5690
PUSHMARK(SP); EXTEND(SP, arg ? 2 : 1); 0 5690 5690
PUSHMARK(SP); EXTEND(SP, arg ? 2 : 1); 0 0 0
104 5690 PUSHs(sv_2mortal(newSVpv(class, 0)));
105 5690 ▶ if (arg) PUSHs(arg);
Branch: 1 branch
BranchTrueFalseTotal
if (arg) PUSHs(arg); 5487 203 5690
106 5690 PUTBACK;
107 5690 count = call_method("new", G_SCALAR);
108 5690 SPAGAIN;
109 5690 ▶ obj = count > 0 ? POPs : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
obj = count > 0 ? POPs : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ 5690 0 5690
110 5690 SvREFCNT_inc(obj);
111 5690 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 5690 0 5690
112 5690 (void)av_store(av, slot, obj);
113 5690 return obj;
114 }
115 }
116  
117 /* Call $obj->$meth(@argv) in its own stack scope, returning the scalar result
118 * (+1) when want is true, else NULL. argv holds stable SV* captured before any
119 * stack manipulation (EXTEND may move the caller's stack, staling ST()). */
120 143698 static SV *pcx_call_meth(pTHX_ SV *obj, const char *meth,
121 SV **argv, int nargs, int want) {
122 143698 dSP; SV *r = NULL; int count, i;
123 143698 ENTER; SAVETMPS;
124 143698 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 143698 143698
125 143698 ▶ EXTEND(SP, nargs + 1);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, nargs + 1); 143698 0 143698
EXTEND(SP, nargs + 1); 0 143698 143698
126 143698 PUSHs(obj);
127 789384 ▶ for (i = 0; i < nargs; i++) PUSHs(argv[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nargs; i++) PUSHs(argv[i]); 645686 143698 789384
128 143698 PUTBACK;
129 143698 ▶ count = call_method(meth, want ? G_SCALAR : G_VOID);
Branch: 1 branch
BranchTrueFalseTotal
count = call_method(meth, want ? G_SCALAR : G_VOID); 142466 1232 143698
130 143300 SPAGAIN;
131 143300 ▶ if (want && count > 0) r = SvREFCNT_inc(POPs); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (want && count > 0) r = SvREFCNT_inc(POPs); /* # uncoverable condition right count:2 class:ignore_covered_err */ 142086 1214 143300
if (want && count > 0) r = SvREFCNT_inc(POPs); /* # uncoverable condition right count:2 class:ignore_covered_err */ 142086 0 142086
132 143300 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 143300 0 143300
133 143300 return r;
134 }
135  
136 /* The context's own parameter layers, in precedence order: the validated
137 * OpenAPI params (path, then query), then the route captures. Borrowed,
138 * NULL when neither carries the name - the request is the caller's
139 * fallback, because reaching it forces Punk::Request into being. */
140 109694 static SV *pcx_param_local(pTHX_ AV *av, SV *name) {
141 STRLEN nl;
142 109694 const char *n = SvPV_const(name, nl);
143 109694 SV *op = pcx_get(aTHX_ av, PCX_OPENAPI);
144 109694 SV *m = pcx_get(aTHX_ av, PCX_MATCH);
145 /*
146 # uncoverable condition right count:2 class:ignore_covered_err
147 # uncoverable condition right count:3 class:ignore_covered_err */
148 109694 ▶ if (op && SvROK(op) && SvTYPE(SvRV(op)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (op && SvROK(op) && SvTYPE(SvRV(op)) == SVt_PVHV) { 20 109674 109694
if (op && SvROK(op) && SvTYPE(SvRV(op)) == SVt_PVHV) { 20 0 20
if (op && SvROK(op) && SvTYPE(SvRV(op)) == SVt_PVHV) { 20 0 20
149 20 HV *oph = (HV *)SvRV(op);
150 static const char *loc[2] = { "path", "query" };
151 int i;
152 40 ▶ for (i = 0; i < 2; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 2; i++) { 33 7 40
153 33 SV **lv = hv_fetch(oph, loc[i], (I32)strlen(loc[i]), 0);
154 /*
155 # uncoverable branch false
156 # uncoverable condition right count:1 class:ignore_covered_err
157 # uncoverable condition right count:2 class:ignore_covered_err
158 # uncoverable condition right count:3 class:ignore_covered_err
159 # uncoverable condition right count:4 class:ignore_covered_err */
160 33 ▶ if (lv && *lv && SvROK(*lv) && SvTYPE(SvRV(*lv)) == SVt_PVHV) {
Branch: 4 branches
BranchTrueFalseTotal
if (lv && *lv && SvROK(*lv) && SvTYPE(SvRV(*lv)) == SVt_PVHV) { 33 0 33
if (lv && *lv && SvROK(*lv) && SvTYPE(SvRV(*lv)) == SVt_PVHV) { 33 0 33
if (lv && *lv && SvROK(*lv) && SvTYPE(SvRV(*lv)) == SVt_PVHV) { 33 0 33
if (lv && *lv && SvROK(*lv) && SvTYPE(SvRV(*lv)) == SVt_PVHV) { 33 0 33
161 33 SV **v = hv_fetch((HV *)SvRV(*lv), n, (I32)nl, 0);
162 33 ▶ if (v) return *v ? *v : &PL_sv_undef; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (v) return *v ? *v : &PL_sv_undef; /* # uncoverable condition right count:2 class:ignore_covered_err */ 13 20 33
if (v) return *v ? *v : &PL_sv_undef; /* # uncoverable condition right count:2 class:ignore_covered_err */ 13 0 13
163 }
164 }
165 }
166 /*
167 # uncoverable branch false
168 # uncoverable condition right count:1 class:ignore_covered_err
169 # uncoverable condition right count:2 class:ignore_covered_err
170 # uncoverable condition right count:3 class:ignore_covered_err */
171 109681 ▶ if (m && SvROK(m) && SvTYPE(SvRV(m)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (m && SvROK(m) && SvTYPE(SvRV(m)) == SVt_PVHV) { 109681 0 109681
if (m && SvROK(m) && SvTYPE(SvRV(m)) == SVt_PVHV) { 109681 0 109681
if (m && SvROK(m) && SvTYPE(SvRV(m)) == SVt_PVHV) { 109681 0 109681
172 109681 SV **cap = hv_fetchs((HV *)SvRV(m), "captures", 0);
173 /*
174 # uncoverable branch false
175 # uncoverable condition right count:1 class:ignore_covered_err
176 # uncoverable condition right count:2 class:ignore_covered_err
177 # uncoverable condition right count:3 class:ignore_covered_err
178 # uncoverable condition right count:4 class:ignore_covered_err */
179 109681 ▶ if (cap && *cap && SvROK(*cap) && SvTYPE(SvRV(*cap)) == SVt_PVHV) {
Branch: 4 branches
BranchTrueFalseTotal
if (cap && *cap && SvROK(*cap) && SvTYPE(SvRV(*cap)) == SVt_PVHV) { 109681 0 109681
if (cap && *cap && SvROK(*cap) && SvTYPE(SvRV(*cap)) == SVt_PVHV) { 109681 0 109681
if (cap && *cap && SvROK(*cap) && SvTYPE(SvRV(*cap)) == SVt_PVHV) { 109681 0 109681
if (cap && *cap && SvROK(*cap) && SvTYPE(SvRV(*cap)) == SVt_PVHV) { 109681 0 109681
180 109681 SV **v = hv_fetch((HV *)SvRV(*cap), n, (I32)nl, 0);
181 109681 ▶ if (v) return *v ? *v : &PL_sv_undef; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (v) return *v ? *v : &PL_sv_undef; /* # uncoverable condition right count:2 class:ignore_covered_err */ 107197 2484 109681
if (v) return *v ? *v : &PL_sv_undef; /* # uncoverable condition right count:2 class:ignore_covered_err */ 107197 0 107197
182 }
183 }
184 2484 return NULL;
185 }
186  
187 /* The lazy Punk::Request for this context, borrowed. */
188 2550 static SV *pcx_req(pTHX_ AV *av) {
189 2550 return pcx_force(aTHX_ av, PCX_REQ, "Punk::Request",
190 pcx_get(aTHX_ av, PCX_ENV));
191 }
192  
193 /* $c->param($name): the layers above, else the request (query, then form
194 * body). Owned (+1), NULL when nothing carries the name. */
195 109694 static SV *pcx_param(pTHX_ AV *av, SV *name) {
196 109694 SV *found = pcx_param_local(aTHX_ av, name);
197 SV *argv[1];
198 SV *r;
199 109694 ▶ if (found) return newSVsv(found);
Branch: 1 branch
BranchTrueFalseTotal
if (found) return newSVsv(found); 107210 2484 109694
200 2484 argv[0] = name;
201 2484 r = pcx_call_meth(aTHX_ pcx_req(aTHX_ av), "param", argv, 1, 1);
202 2484 ▶ if (r && SvOK(r)) return r; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (r && SvOK(r)) return r; /* # uncoverable condition right count:1 class:ignore_covered_err */ 2484 0 2484
if (r && SvOK(r)) return r; /* # uncoverable condition right count:1 class:ignore_covered_err */ 234 2250 2484
203 /*
204 # uncoverable branch false
205 # uncoverable condition right count:1 class:ignore_covered_err */
206 2250 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 2250 0 2250
207 2250 return NULL;
208 }
209  
210 /* Every parameter the context can see, stacked in the same precedence
211 * pcx_param resolves one by: the request's merged table underneath, then
212 * the route captures, then the validated OpenAPI query and path. +1. */
213 14 static HV *pcx_params_merged(pTHX_ AV *av) {
214 14 HV *out = newHV();
215 14 SV *rp = pcx_call_meth(aTHX_ pcx_req(aTHX_ av), "params", NULL, 0, 1);
216 14 SV *m = pcx_get(aTHX_ av, PCX_MATCH);
217 14 SV *op = pcx_get(aTHX_ av, PCX_OPENAPI);
218 14 pq_overlay(aTHX_ out, rp);
219 /*
220 # uncoverable branch false
221 # uncoverable condition right count:1 class:ignore_covered_err */
222 14 ▶ if (rp) SvREFCNT_dec(rp);
Branch: 1 branch
BranchTrueFalseTotal
if (rp) SvREFCNT_dec(rp); 14 0 14
223 /*
224 # uncoverable branch false
225 # uncoverable condition right count:1 class:ignore_covered_err
226 # uncoverable condition right count:2 class:ignore_covered_err
227 # uncoverable condition right count:3 class:ignore_covered_err */
228 14 ▶ if (m && SvROK(m) && SvTYPE(SvRV(m)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (m && SvROK(m) && SvTYPE(SvRV(m)) == SVt_PVHV) { 14 0 14
if (m && SvROK(m) && SvTYPE(SvRV(m)) == SVt_PVHV) { 14 0 14
if (m && SvROK(m) && SvTYPE(SvRV(m)) == SVt_PVHV) { 14 0 14
229 14 SV **cap = hv_fetchs((HV *)SvRV(m), "captures", 0);
230 /*
231 # uncoverable branch false
232 # uncoverable condition right count:1 class:ignore_covered_err */
233 14 ▶ if (cap) pq_overlay(aTHX_ out, *cap);
Branch: 1 branch
BranchTrueFalseTotal
if (cap) pq_overlay(aTHX_ out, *cap); 14 0 14
234 }
235 /*
236 # uncoverable condition right count:2 class:ignore_covered_err
237 # uncoverable condition right count:3 class:ignore_covered_err */
238 14 ▶ if (op && SvROK(op) && SvTYPE(SvRV(op)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (op && SvROK(op) && SvTYPE(SvRV(op)) == SVt_PVHV) { 3 11 14
if (op && SvROK(op) && SvTYPE(SvRV(op)) == SVt_PVHV) { 3 0 3
if (op && SvROK(op) && SvTYPE(SvRV(op)) == SVt_PVHV) { 3 0 3
239 3 HV *oph = (HV *)SvRV(op);
240 3 SV **qry = hv_fetchs(oph, "query", 0);
241 3 SV **pth = hv_fetchs(oph, "path", 0);
242 /*
243 # uncoverable branch false
244 # uncoverable condition right count:1 class:ignore_covered_err */
245 3 ▶ if (qry) pq_overlay(aTHX_ out, *qry);
Branch: 1 branch
BranchTrueFalseTotal
if (qry) pq_overlay(aTHX_ out, *qry); 3 0 3
246 /*
247 # uncoverable branch false
248 # uncoverable condition right count:1 class:ignore_covered_err */
249 3 ▶ if (pth) pq_overlay(aTHX_ out, *pth);
Branch: 1 branch
BranchTrueFalseTotal
if (pth) pq_overlay(aTHX_ out, *pth); 3 0 3
250 }
251 14 return out;
252 }
253  
254 /* does the blessed ref have method $meth? (a ->can without the call) */
255 /* Is this exactly a Punk::Future? A cached stash pointer compare, so the two
256 * dispatch paths can skip the ->can and ->isa probes they would otherwise pay
257 * per request. Deliberately not sv_derived_from: a subclass answers no and
258 * takes the general duck-typed path, which is the safe way round.
259 *
260 * pcx_pf_get1 is its companion - the value of a settled future, awaiting a
261 * pending one and croaking exactly as ->get does. It is defined in
262 * punk_future.h, which the unity build includes after this file. */
263 static HV *PCX_PF_STASH = NULL;
264 33 static int pcx_is_punk_future(pTHX_ SV *sv) {
265 /*
266 # uncoverable branch true
267 # uncoverable condition left count:1 class:ignore_covered_err
268 # uncoverable condition left count:2 class:ignore_covered_err */
269 33 ▶ if (!(SvROK(sv) && SvOBJECT(SvRV(sv)))) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(sv) && SvOBJECT(SvRV(sv)))) return 0; 33 0 33
if (!(SvROK(sv) && SvOBJECT(SvRV(sv)))) return 0; 0 33 33
270 33 ▶ if (!PCX_PF_STASH) PCX_PF_STASH = gv_stashpvs("Punk::Future", 0);
Branch: 1 branch
BranchTrueFalseTotal
if (!PCX_PF_STASH) PCX_PF_STASH = gv_stashpvs("Punk::Future", 0); 11 22 33
271 33 ▶ return PCX_PF_STASH && SvSTASH(SvRV(sv)) == PCX_PF_STASH; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
return PCX_PF_STASH && SvSTASH(SvRV(sv)) == PCX_PF_STASH; /* # uncoverable condition right count:1 class:ignore_covered_err */ 33 0 33
return PCX_PF_STASH && SvSTASH(SvRV(sv)) == PCX_PF_STASH; /* # uncoverable condition right count:1 class:ignore_covered_err */ 6 27 33
272 }
273 static SV *pcx_pf_get1(pTHX_ SV *f);
274  
275 994 static int pcx_can(pTHX_ SV *obj, const char *meth) {
276 /*
277 # uncoverable condition right count:1 class:ignore_covered_err
278 # uncoverable condition right count:2 class:ignore_covered_err */
279 994 ▶ HV *stash = (SvROK(obj) && SvOBJECT(SvRV(obj))) ? SvSTASH(SvRV(obj)) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *stash = (SvROK(obj) && SvOBJECT(SvRV(obj))) ? SvSTASH(SvRV(obj)) : NULL; 994 0 994
HV *stash = (SvROK(obj) && SvOBJECT(SvRV(obj))) ? SvSTASH(SvRV(obj)) : NULL; 994 0 994
280 994 ▶ return (stash && gv_fetchmethod_autoload(stash, meth, 0)) ? 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
return (stash && gv_fetchmethod_autoload(stash, meth, 0)) ? 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 994 0 994
return (stash && gv_fetchmethod_autoload(stash, meth, 0)) ? 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 972 22 994
281 }
282  
283 /* $h->($c, $resp) trapping a die; result (+1) or NULL, *died set. */
284 791 static SV *pcx_call2(pTHX_ SV *h, SV *a, SV *b, int *died) {
285 791 dSP; SV *r; int count;
286 791 ENTER; SAVETMPS;
287 791 ▶ PUSHMARK(SP); EXTEND(SP, 2); PUSHs(a); PUSHs(b); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 2); PUSHs(a); PUSHs(b); PUTBACK; 0 791 791
PUSHMARK(SP); EXTEND(SP, 2); PUSHs(a); PUSHs(b); PUTBACK; 0 791 791
288 791 count = call_sv(h, G_SCALAR | G_EVAL);
289 791 SPAGAIN;
290 791 ▶ r = count > 0 ? SvREFCNT_inc(POPs) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
r = count > 0 ? SvREFCNT_inc(POPs) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 791 0 791
291 791 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 661 130 791
292 791 ▶ *died = SvTRUE(ERRSV) ? 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
*died = SvTRUE(ERRSV) ? 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 791 0 791
293 791 return r;
294 }
295  
296 /* Is $ret already a PSGI triplet [ $status, \@headers, \@body ]? */
297 440388 static int pcx_is_triplet(pTHX_ SV *ret) {
298 AV *a;
299 SV **s, **h;
300 440388 ▶ if (!SvROK(ret) || SvTYPE(SvRV(ret)) != SVt_PVAV) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ret) || SvTYPE(SvRV(ret)) != SVt_PVAV) return 0; 440205 183 440388
if (!SvROK(ret) || SvTYPE(SvRV(ret)) != SVt_PVAV) return 0; 105144 335061 440205
301 335061 a = (AV *)SvRV(ret);
302 335061 ▶ if (av_len(a) != 2) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(a) != 2) return 0; 12 335049 335061
303 335049 s = av_fetch(a, 0, 0);
304 335049 h = av_fetch(a, 1, 0);
305 /*
306 # uncoverable branch true
307 # uncoverable condition left count:1 class:ignore_covered_err
308 # uncoverable condition left count:2 class:ignore_covered_err
309 # uncoverable condition left count:3 class:ignore_covered_err
310 # uncoverable condition left count:4 class:ignore_covered_err */
311 335049 ▶ if (!(s && *s && SvOK(*s) && !SvROK(*s))) return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (!(s && *s && SvOK(*s) && !SvROK(*s))) return 0; 335049 0 335049
if (!(s && *s && SvOK(*s) && !SvROK(*s))) return 0; 335049 0 335049
if (!(s && *s && SvOK(*s) && !SvROK(*s))) return 0; 335049 0 335049
if (!(s && *s && SvOK(*s) && !SvROK(*s))) return 0; 0 335049 335049
312 /*
313 # uncoverable condition left count:1 class:ignore_covered_err
314 # uncoverable condition left count:2 class:ignore_covered_err
315 # uncoverable condition left count:4 class:ignore_covered_err */
316 335049 ▶ if (!(h && *h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVAV)) return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (!(h && *h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVAV)) return 0; 335049 0 335049
if (!(h && *h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVAV)) return 0; 335049 0 335049
if (!(h && *h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVAV)) return 0; 335047 2 335049
if (!(h && *h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVAV)) return 0; 0 335047 335047
317 /* the status Punk itself built is an integer: read it as one rather
318 * than upgrade it to a string on every response */
319 335047 ▶ if (SvIOK(*s) && !SvPOK(*s)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (SvIOK(*s) && !SvPOK(*s)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 335046 1 335047
if (SvIOK(*s) && !SvPOK(*s)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 335046 0 335046
320 335046 IV v = SvIVX(*s);
321 335046 ▶ return v >= 100 && v <= 599;
Branch: 2 branches
BranchTrueFalseTotal
return v >= 100 && v <= 599; 335044 2 335046
return v >= 100 && v <= 599; 335043 1 335044
322 }
323 {
324 STRLEN l;
325 1 const char *p = SvPV_const(*s, l);
326 0 ▶ return (l == 3 && p[0] >= '1' && p[0] <= '5' /* # uncoverable statement class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
return (l == 3 && p[0] >= '1' && p[0] <= '5' /* # uncoverable statement class:ignore_covered_err */ 0 0 0
return (l == 3 && p[0] >= '1' && p[0] <= '5' /* # uncoverable statement class:ignore_covered_err */ 0 0 0
327 1 ▶ && isDIGIT(p[1]) && isDIGIT(p[2]));
Branch: 3 branches
BranchTrueFalseTotal
&& isDIGIT(p[1]) && isDIGIT(p[2])); 0 1 1
&& isDIGIT(p[1]) && isDIGIT(p[2])); 0 0 0
&& isDIGIT(p[1]) && isDIGIT(p[2])); 0 0 0
328 }
329 }
330  
331 /* A triplet in everything but its status: an integer first, then a headers
332 * arrayref of name and value pairs whose names are words, then a body that
333 * is an arrayref, a handle or an object. Narrow on purpose, because a
334 * handler may return three things that are simply data - [ 1, 2, 3 ],
335 * [ 'series', \@xs, \@ys ], [ 5, [ 1, 2 ], [ 3, 4 ] ] - and those must
336 * stay data. */
337 105105 static int pcx_triplet_shaped(pTHX_ SV *ret) {
338 AV *a, *hav;
339 SV **s, **h, **b;
340 SSize_t i, n;
341 105105 ▶ if (!SvROK(ret) || SvTYPE(SvRV(ret)) != SVt_PVAV) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ret) || SvTYPE(SvRV(ret)) != SVt_PVAV) return 0; 105103 2 105105
if (!SvROK(ret) || SvTYPE(SvRV(ret)) != SVt_PVAV) return 0; 105085 18 105103
342 18 a = (AV *)SvRV(ret);
343 18 ▶ if (av_len(a) != 2) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(a) != 2) return 0; 12 6 18
344 6 s = av_fetch(a, 0, 0);
345 6 h = av_fetch(a, 1, 0);
346 6 b = av_fetch(a, 2, 0);
347 /*
348 # uncoverable condition left count:1 class:ignore_covered_err
349 # uncoverable condition left count:2 class:ignore_covered_err
350 # uncoverable condition left count:4 class:ignore_covered_err */
351 6 ▶ if (!(h && *h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVAV)) return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (!(h && *h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVAV)) return 0; 6 0 6
if (!(h && *h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVAV)) return 0; 6 0 6
if (!(h && *h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVAV)) return 0; 4 2 6
if (!(h && *h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVAV)) return 0; 0 4 4
352 /*
353 # uncoverable branch true
354 # uncoverable condition left count:1 class:ignore_covered_err
355 # uncoverable condition left count:2 class:ignore_covered_err
356 # uncoverable condition left count:3 class:ignore_covered_err */
357 4 ▶ if (!(b && *b && SvROK(*b))) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!(b && *b && SvROK(*b))) return 0; 4 0 4
if (!(b && *b && SvROK(*b))) return 0; 4 0 4
if (!(b && *b && SvROK(*b))) return 0; 0 4 4
358 /*
359 # uncoverable branch true
360 # uncoverable condition left count:1 class:ignore_covered_err
361 # uncoverable condition left count:2 class:ignore_covered_err
362 # uncoverable condition right count:2 class:ignore_covered_err
363 # uncoverable condition left count:3 class:ignore_covered_err
364 # uncoverable condition right count:3 class:ignore_covered_err */
365 4 ▶ if (!(SvTYPE(SvRV(*b)) == SVt_PVAV || SvTYPE(SvRV(*b)) == SVt_PVGV
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvTYPE(SvRV(*b)) == SVt_PVAV || SvTYPE(SvRV(*b)) == SVt_PVGV 0 4 4
if (!(SvTYPE(SvRV(*b)) == SVt_PVAV || SvTYPE(SvRV(*b)) == SVt_PVGV 0 0 0
366 0 ▶ || SvOBJECT(SvRV(*b)))) return 0; /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
|| SvOBJECT(SvRV(*b)))) return 0; /* # uncoverable statement */ 0 0 0
367 /* An integer, asked WITHOUT numifying it: this value may yet be encoded
368 * as data, and SvIV or SvNV here would leave a number flag on the
369 * caller's scalar that changes how it is written - a 1 became 1.0. */
370 /*
371 # uncoverable branch true
372 # uncoverable condition left count:1 class:ignore_covered_err
373 # uncoverable condition left count:2 class:ignore_covered_err
374 # uncoverable condition left count:3 class:ignore_covered_err
375 # uncoverable condition left count:4 class:ignore_covered_err */
376 4 ▶ if (!(s && *s && SvOK(*s) && !SvROK(*s))) return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (!(s && *s && SvOK(*s) && !SvROK(*s))) return 0; 4 0 4
if (!(s && *s && SvOK(*s) && !SvROK(*s))) return 0; 4 0 4
if (!(s && *s && SvOK(*s) && !SvROK(*s))) return 0; 4 0 4
if (!(s && *s && SvOK(*s) && !SvROK(*s))) return 0; 0 4 4
377 4 ▶ if (!SvIOK(*s)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvIOK(*s)) { 1 3 4
378 1 STRLEN sl, si = 0;
379 const char *sp;
380 /*
381 # uncoverable branch true
382 # uncoverable condition left count:1 class:ignore_covered_err */
383 1 ▶ if (!SvPOK(*s)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvPOK(*s)) return 0; 0 1 1
384 1 sp = SvPVX_const(*s);
385 1 sl = SvCUR(*s);
386 /*
387 # uncoverable branch true
388 # uncoverable condition left count:1 class:ignore_covered_err
389 # uncoverable condition right count:1 class:ignore_covered_err
390 # uncoverable condition left count:2 class:ignore_covered_err */
391 1 ▶ if (sl && sp[0] == '-') si = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (sl && sp[0] == '-') si = 1; 1 0 1
if (sl && sp[0] == '-') si = 1; 0 1 1
392 /*
393 # uncoverable branch true
394 # uncoverable condition left count:1 class:ignore_covered_err */
395 1 ▶ if (si == sl) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (si == sl) return 0; 0 1 1
396 /*
397 # uncoverable branch false
398 # uncoverable condition right count:1 class:ignore_covered_err
399 # uncoverable condition right count:2 class:ignore_covered_err */
400 1 ▶ for (; si < sl; si++) if (!isDIGIT(sp[si])) return 0;
Branch: 2 branches
BranchTrueFalseTotal
for (; si < sl; si++) if (!isDIGIT(sp[si])) return 0; 1 0 1
for (; si < sl; si++) if (!isDIGIT(sp[si])) return 0; 1 0 1
401 }
402 3 hav = (AV *)SvRV(*h);
403 3 n = av_len(hav) + 1;
404 /*
405 # uncoverable branch true
406 # uncoverable condition left count:1 class:ignore_covered_err
407 # uncoverable condition left count:2 class:ignore_covered_err */
408 3 ▶ if (n == 0 || (n & 1)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (n == 0 || (n & 1)) return 0; 3 0 3
if (n == 0 || (n & 1)) return 0; 0 3 3
409 6 ▶ for (i = 0; i < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i += 2) { 4 2 6
410 4 SV **k = av_fetch(hav, i, 0);
411 /*
412 # uncoverable condition left count:1 class:ignore_covered_err
413 # uncoverable condition left count:2 class:ignore_covered_err
414 # uncoverable condition left count:3 class:ignore_covered_err
415 # uncoverable condition left count:4 class:ignore_covered_err */
416 4 ▶ if (!(k && *k && SvOK(*k) && !SvROK(*k)) || looks_like_number(*k))
Branch: 5 branches
BranchTrueFalseTotal
if (!(k && *k && SvOK(*k) && !SvROK(*k)) || looks_like_number(*k)) 4 0 4
if (!(k && *k && SvOK(*k) && !SvROK(*k)) || looks_like_number(*k)) 4 0 4
if (!(k && *k && SvOK(*k) && !SvROK(*k)) || looks_like_number(*k)) 4 0 4
if (!(k && *k && SvOK(*k) && !SvROK(*k)) || looks_like_number(*k)) 4 0 4
if (!(k && *k && SvOK(*k) && !SvROK(*k)) || looks_like_number(*k)) 1 3 4
417 1 return 0;
418 }
419 2 return 1;
420 }
421  
422 /* Coerce a handler return into a PSGI triplet: a triplet passes through, a
423 * Punk::Response finalizes, a Future is awaited and re-coerced, anything else
424 * is JSON-encoded (folding in the context's pending status/headers). +1. */
425 /* The 500 envelope, or $on_error's own response. Defined in punk_serve.h,
426 * which is included later because it is the dispatcher and needs everything;
427 * it calls punk_coerce in turn, so one of the two has to be declared ahead of
428 * the other. Same arrangement as pk_after_res below. */
429 static SV *punk_handle_error(pTHX_ SV *c, SV *err, SV *on_error);
430  
431 218824 static SV *punk_coerce(pTHX_ SV *c, SV *ret) {
432 /* a coderef is a PSGI streaming / delayed-response body (SSE uses this on
433 * a psgi.streaming server) - hand it straight to the server */
434 218824 ▶ if (SvROK(ret) && SvTYPE(SvRV(ret)) == SVt_PVCV) return SvREFCNT_inc(ret);
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(ret) && SvTYPE(SvRV(ret)) == SVt_PVCV) return SvREFCNT_inc(ret); 218822 2 218824
if (SvROK(ret) && SvTYPE(SvRV(ret)) == SVt_PVCV) return SvREFCNT_inc(ret); 108 218714 218822
435 218716 ▶ if (pcx_is_triplet(aTHX_ ret)) return SvREFCNT_inc(ret);
Branch: 1 branch
BranchTrueFalseTotal
if (pcx_is_triplet(aTHX_ ret)) return SvREFCNT_inc(ret); 113611 105105 218716
436 105105 ▶ if (pcx_triplet_shaped(aTHX_ ret))
Branch: 1 branch
BranchTrueFalseTotal
if (pcx_triplet_shaped(aTHX_ ret)) 2 105103 105105
437 /* [ $something, \@headers, \@body ] where $something is not a
438 * status. That is a response somebody built wrongly, and encoding it
439 * as data would publish its headers - a Set-Cookie among them - in
440 * the body of a 200. An error says so instead. */
441 2 return punk_handle_error(aTHX_ c, sv_2mortal(newSVpvs(
442 "Punk: a handler returned a response whose status is not a "
443 "number from 100 to 599")), NULL);
444 105103 ▶ if (SvROK(ret) && SvOBJECT(SvRV(ret))) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(ret) && SvOBJECT(SvRV(ret))) { 105101 2 105103
if (SvROK(ret) && SvOBJECT(SvRV(ret))) { 21 105080 105101
445 /* One of ours: read the value in C rather than walking @ISA for
446 * Punk::Response and then dispatching ->get through perl. */
447 /*
448 # uncoverable branch true
449 # uncoverable condition left count:1 class:ignore_covered_err */
450 21 ▶ if (pcx_is_punk_future(aTHX_ ret)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pcx_is_punk_future(aTHX_ ret)) { 0 21 21
451 0 SV *got = pcx_pf_get1(aTHX_ ret); /* # uncoverable statement */
452 /*
453 # uncoverable statement
454 # uncoverable condition left count:1
455 # uncoverable condition right count:1 */
456 0 ▶ SV *r = punk_coerce(aTHX_ c, got ? got : &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
SV *r = punk_coerce(aTHX_ c, got ? got : &PL_sv_undef); 0 0 0
457 /*
458 # uncoverable statement
459 # uncoverable branch true
460 # uncoverable branch false
461 # uncoverable condition left count:1
462 # uncoverable condition right count:1 */
463 0 ▶ if (got) SvREFCNT_dec(got);
Branch: 1 branch
BranchTrueFalseTotal
if (got) SvREFCNT_dec(got); 0 0 0
464 0 return r; /* # uncoverable statement */
465 }
466 21 ▶ if (sv_derived_from(ret, "Punk::Response"))
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(ret, "Punk::Response")) 11 10 21
467 {
468 /* canonical JSON on an etag route, as pcx_json does for $c->json */
469 11 SV *canon = pcx_get(aTHX_ pcx_av(aTHX_ c), PCX_CANON);
470 SV *argv[1];
471 /*
472 # uncoverable condition left count:1 class:ignore_covered_err
473 # uncoverable condition left count:2 class:ignore_covered_err
474 # uncoverable condition right count:2 class:ignore_covered_err */
475 11 ▶ argv[0] = (canon && SvTRUE(canon)) ? &PL_sv_yes : &PL_sv_no;
Branch: 2 branches
BranchTrueFalseTotal
argv[0] = (canon && SvTRUE(canon)) ? &PL_sv_yes : &PL_sv_no; 0 11 11
argv[0] = (canon && SvTRUE(canon)) ? &PL_sv_yes : &PL_sv_no; 0 0 0
476 11 return pcx_call_meth(aTHX_ ret, "finalize", argv, 1, 1);
477 }
478 /* An XML document, before the on_ready probe below (which walks the
479 * stash of every object) and before the JSON fallthrough (which would
480 * encode it as the blessed reference it is). This is also what makes
481 * respond_to(xml => sub { $doc }) answer application/xml: respond_to
482 * sets no type of its own, it returns whatever its branch returned. */
483 /*
484 # uncoverable branch false
485 # uncoverable condition right count:1 class:ignore_covered_err */
486 10 ▶ if (punk_xml_is(aTHX_ ret)) {
Branch: 1 branch
BranchTrueFalseTotal
if (punk_xml_is(aTHX_ ret)) { 10 0 10
487 10 AV *res = pcx_res_av(aTHX_ pcx_av(aTHX_ c));
488 10 IV st = pcx_res_status(aTHX_ res);
489 10 SV *err = NULL;
490 10 SV *bytes = punk_xml_bytes(aTHX_ ret, &err);
491 /* write reports a refusal rather than croaking, which is what
492 * lets this be a branch: punk_coerce runs from loop callbacks
493 * with no eval around them, so a croak here would leave Punk
494 * instead of becoming a response. */
495 /*
496 # uncoverable branch true
497 # uncoverable condition left count:1 class:ignore_covered_err */
498 10 ▶ if (!bytes)
Branch: 1 branch
BranchTrueFalseTotal
if (!bytes) 0 10 10
499 0 return punk_handle_error(aTHX_ c, /* # uncoverable statement */
500 /*
501 # uncoverable statement
502 # uncoverable condition left count:1
503 # uncoverable condition right count:1 */
504 0 ▶ err ? err : sv_2mortal(newSVpvs("Punk: xml")), NULL);
Branch: 1 branch
BranchTrueFalseTotal
err ? err : sv_2mortal(newSVpvs("Punk: xml")), NULL); 0 0 0
505 /*
506 # uncoverable branch false
507 # uncoverable condition right count:1 class:ignore_covered_err */
508 10 ▶ if (!st) st = 200;
Branch: 1 branch
BranchTrueFalseTotal
if (!st) st = 200; 10 0 10
509 15 ▶ return punk_triplet(aTHX_ st,
Branch: 1 branch
BranchTrueFalseTotal
return punk_triplet(aTHX_ st, 5 5 10
510 sv_2mortal(newSVpvs(PK_XML_CT)),
511 5 bytes, res ? pcx_res_headers(aTHX_ res) : NULL);
512 }
513 /*
514 # uncoverable statement
515 # uncoverable branch true
516 # uncoverable branch false
517 # uncoverable condition left count:1
518 # uncoverable condition right count:1 */
519 0 ▶ if (pcx_can(aTHX_ ret, "on_ready")) {
Branch: 1 branch
BranchTrueFalseTotal
if (pcx_can(aTHX_ ret, "on_ready")) { 0 0 0
520 0 SV *got = pcx_call_meth(aTHX_ ret, "get", NULL, 0, 1); /* # uncoverable statement */
521 /*
522 # uncoverable statement
523 # uncoverable condition left count:1
524 # uncoverable condition right count:1 */
525 0 ▶ SV *r = punk_coerce(aTHX_ c, got ? got : &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
SV *r = punk_coerce(aTHX_ c, got ? got : &PL_sv_undef); 0 0 0
526 /*
527 # uncoverable statement
528 # uncoverable branch true
529 # uncoverable branch false
530 # uncoverable condition left count:1
531 # uncoverable condition right count:1 */
532 0 ▶ if (got) SvREFCNT_dec(got);
Branch: 1 branch
BranchTrueFalseTotal
if (got) SvREFCNT_dec(got); 0 0 0
533 0 return r; /* # uncoverable statement */
534 }
535 }
536 {
537 105082 AV *res = pcx_res_av(aTHX_ pcx_av(aTHX_ c));
538 105082 IV st = pcx_res_status(aTHX_ res);
539 105082 SV *bytes = pcx_json(aTHX_ pcx_av(aTHX_ c), ret);
540 105082 ▶ if (!st) st = 200;
Branch: 1 branch
BranchTrueFalseTotal
if (!st) st = 200; 105077 5 105082
541 105087 ▶ return punk_triplet_ct(aTHX_ st, "application/json", 16,
Branch: 1 branch
BranchTrueFalseTotal
return punk_triplet_ct(aTHX_ st, "application/json", 16, 5 105077 105082
542 5 bytes, res ? pcx_res_headers(aTHX_ res) : NULL);
543 }
544 }
545  
546 /* The after_response phase: scheduled here because this is the one place a
547 * final response exists. Defined in punk_afterres.h, which is included after
548 * hm_abi.h because the loop timer is one of the three ways it defers - the
549 * pcg_check / pi_check arrangement. */
550 static void pk_after_res(pTHX_ SV *c, SV *resp);
551  
552 /* Finish a response: coerce if needed, run the after-dispatch hooks (each may
553 * replace the triplet), and blank the body on HEAD. +1. */
554 220964 static SV *punk_deliver(pTHX_ SV *c, SV *resp, int is_head, AV *after) {
555 441869 SV *r = pcx_is_triplet(aTHX_ resp) ? SvREFCNT_inc(resp)
556 220964 ▶ : punk_coerce(aTHX_ c, resp);
Branch: 1 branch
BranchTrueFalseTotal
: punk_coerce(aTHX_ c, resp); 220905 59 220964
557 /* a streaming coderef has no triplet to mutate: skip after-hooks and HEAD */
558 220964 ▶ if (SvROK(r) && SvTYPE(SvRV(r)) == SVt_PVCV) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(r) && SvTYPE(SvRV(r)) == SVt_PVCV) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 220964 0 220964
if (SvROK(r) && SvTYPE(SvRV(r)) == SVt_PVCV) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 54 220910 220964
559 54 ▶ if (PK_OBS_WANT_RES) pk_obs_fire_res(aTHX_ c, r);
Branch: 1 branch
BranchTrueFalseTotal
if (PK_OBS_WANT_RES) pk_obs_fire_res(aTHX_ c, r); 1 53 54
560 54 pk_after_res(aTHX_ c, r);
561 54 return r;
562 }
563 /*
564 # uncoverable branch false
565 # uncoverable condition right count:1 class:ignore_covered_err */
566 220910 ▶ if (after) {
Branch: 1 branch
BranchTrueFalseTotal
if (after) { 220910 0 220910
567 220910 SSize_t i, n = av_len(after) + 1;
568 221618 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 708 220910 221618
569 708 SV **hp = av_fetch(after, i, 0);
570 708 int died = 0;
571 SV *hr;
572 /*
573 # uncoverable branch true
574 # uncoverable condition left count:1 class:ignore_covered_err
575 # uncoverable condition left count:2 class:ignore_covered_err */
576 708 ▶ if (!hp || !*hp) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!hp || !*hp) continue; 708 0 708
if (!hp || !*hp) continue; 0 708 708
577 708 hr = pcx_call2(aTHX_ *hp, c, r, &died);
578 /*
579 # uncoverable condition right count:1 class:ignore_covered_err
580 # uncoverable condition right count:2 class:ignore_covered_err */
581 708 ▶ if (!died && hr && pcx_is_triplet(aTHX_ hr)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!died && hr && pcx_is_triplet(aTHX_ hr)) { 708 0 708
if (!died && hr && pcx_is_triplet(aTHX_ hr)) { 708 0 708
if (!died && hr && pcx_is_triplet(aTHX_ hr)) { 527 181 708
582 527 SvREFCNT_dec(r);
583 527 r = SvREFCNT_inc(hr);
584 }
585 /*
586 # uncoverable branch false
587 # uncoverable condition right count:1 class:ignore_covered_err */
588 708 ▶ if (hr) SvREFCNT_dec(hr);
Branch: 1 branch
BranchTrueFalseTotal
if (hr) SvREFCNT_dec(hr); 708 0 708
589 }
590 }
591 /*
592 # uncoverable condition right count:2 class:ignore_covered_err
593 # uncoverable condition right count:3 class:ignore_covered_err */
594 220910 ▶ if (is_head && SvROK(r) && SvTYPE(SvRV(r)) == SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
if (is_head && SvROK(r) && SvTYPE(SvRV(r)) == SVt_PVAV) { 21 220889 220910
if (is_head && SvROK(r) && SvTYPE(SvRV(r)) == SVt_PVAV) { 21 0 21
if (is_head && SvROK(r) && SvTYPE(SvRV(r)) == SVt_PVAV) { 21 0 21
595 21 AV *ra = (AV *)SvRV(r);
596 21 SV **b = av_fetch(ra, 2, 0);
597 /*
598 # uncoverable branch false
599 # uncoverable condition right count:1 class:ignore_covered_err
600 # uncoverable condition right count:2 class:ignore_covered_err
601 # uncoverable condition right count:3 class:ignore_covered_err
602 # uncoverable condition right count:4 class:ignore_covered_err */
603 21 ▶ if (b && *b && SvROK(*b) && SvTYPE(SvRV(*b)) == SVt_PVAV) {
Branch: 4 branches
BranchTrueFalseTotal
if (b && *b && SvROK(*b) && SvTYPE(SvRV(*b)) == SVt_PVAV) { 21 0 21
if (b && *b && SvROK(*b) && SvTYPE(SvRV(*b)) == SVt_PVAV) { 21 0 21
if (b && *b && SvROK(*b) && SvTYPE(SvRV(*b)) == SVt_PVAV) { 21 0 21
if (b && *b && SvROK(*b) && SvTYPE(SvRV(*b)) == SVt_PVAV) { 21 0 21
604 21 AV *empty = newAV();
605 21 av_push(empty, newSVpvs(""));
606 21 (void)av_store(ra, 2, newRV_noinc((SV *)empty));
607 }
608 }
609 /* The C ABI's response observers. Here rather than in the dispatcher
610 * because this is the ONE place a final triplet is produced: the
611 * synchronous path reaches it through punk_finish_c, and every
612 * asynchronous completion reaches it from punk_compile.h when the future
613 * settles - so an observer sees the response that was actually sent, at
614 * the moment it was, and sees it exactly once either way. The four
615 * dispatcher exits that never come through here fire it themselves. */
616 220910 ▶ if (PK_OBS_WANT_RES) pk_obs_fire_res(aTHX_ c, r);
Branch: 1 branch
BranchTrueFalseTotal
if (PK_OBS_WANT_RES) pk_obs_fire_res(aTHX_ c, r); 219 220691 220910
617 220910 pk_after_res(aTHX_ c, r);
618 220910 return r;
619 }
620  
621 /* Is this a same-origin relative path - somewhere on this site, and nowhere
622 * else? Backs $c->safe_path, the guard for a redirect destination that came
623 * out of the request (?to=, ?return=, ?next=).
624 *
625 * Checking for a leading '/' and no leading '//' is NOT enough, because the
626 * browser does not read the string byte for byte the way this does:
627 *
628 * - it removes every TAB, CR and LF from a URL before parsing it, so
629 * "/<TAB>/evil.example" is parsed as "//evil.example";
630 * - under a special scheme it treats '\' as '/', so "/\evil.example" is
631 * protocol-relative too.
632 *
633 * Either one leaves the site, which is why this refuses the whole class -
634 * every C0 control, DEL, and a backslash anywhere - rather than trying to
635 * enumerate the bytes some parser somewhere might drop. The same rule (and
636 * the same reasoning) as Punk::OAuth2's same_origin_path, which is where it
637 * was learned the expensive way: CVE-2026-75628. A path that genuinely wants
638 * one of these spells it percent-encoded. */
639 19 static int pk_same_origin_path(pTHX_ SV *path) {
640 STRLEN n, i;
641 const char *p;
642 /*
643 # uncoverable branch true
644 # uncoverable condition left count:1 class:ignore_covered_err
645 # uncoverable condition left count:2 class:ignore_covered_err */
646 19 ▶ if (!path || !SvOK(path)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!path || !SvOK(path)) return 0; 19 0 19
if (!path || !SvOK(path)) return 0; 0 19 19
647 19 p = SvPV_const(path, n);
648 19 ▶ if (n < 1 || p[0] != '/') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (n < 1 || p[0] != '/') return 0; 18 1 19
if (n < 1 || p[0] != '/') return 0; 3 15 18
649 15 ▶ if (n >= 2 && p[1] == '/') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (n >= 2 && p[1] == '/') return 0; 14 1 15
if (n >= 2 && p[1] == '/') return 0; 2 12 14
650 61 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 55 6 61
651 55 const unsigned char ch = (unsigned char)p[i];
652 55 ▶ if (ch < 0x20 || ch == 0x7f || ch == '\\') return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (ch < 0x20 || ch == 0x7f || ch == '\\') return 0; 51 4 55
if (ch < 0x20 || ch == 0x7f || ch == '\\') return 0; 50 1 51
if (ch < 0x20 || ch == 0x7f || ch == '\\') return 0; 2 48 50
653 }
654 6 return 1;
655 }
656  
657 #endif /* PUNK_CONTEXT_H */
« include/punk/punk_app.h ↑ Summary include/punk/punk_state.h »