include/punk/punk_headers.h

filter: Statementstmt 98.7% 228 / 234 (3 uncoverable) Branchbran 71.0% 178 / 266 (30 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.0% 406 / 500 (33 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_mrows.h ↑ Summary xs/context.xs »
1 /* punk_headers.h - security response headers, in C.
2 *
3 * The `headers` keyword freezes a pair list at boot; every response is
4 * decorated on the way out of pc_app_cb - after CORS, outside the preflight
5 * early-return, so the 404s and 405s that never build a context and the
6 * preflight 204 itself all carry the policy. A hook cannot do that, for the
7 * same reason CORS is not a hook.
8 *
9 * Set-if-absent: a header the response already carries - from a handler,
10 * a hook, or CORS - wins over the frozen policy, case-insensitively. That
11 * is what lets one route relax X-Frame-Options without a second keyword.
12 *
13 * Include after punk_serve.h (ps_state), punk_static.h (punk_closure) and
14 * punk_cors.h (pco_headers_of).
15 */
16  
17 #ifndef PUNK_HEADERS_H
18 #define PUNK_HEADERS_H
19  
20 /* The bare keyword's policy. CSP and HSTS are deliberately not here: a
21 * default CSP breaks every inline script it has never seen, and HSTS is a
22 * commitment to HTTPS measured in months - both are opt-in by spelling. */
23 static const char *const PHD_DEFAULT_K[] = {
24 "X-Content-Type-Options",
25 "X-Frame-Options",
26 "Referrer-Policy",
27 };
28 static const char *const PHD_DEFAULT_V[] = {
29 "nosniff",
30 "SAMEORIGIN",
31 "strict-origin-when-cross-origin",
32 };
33 #define PHD_DEFAULTS 3
34  
35 /* The config entry for a default header, found the way the response scan
36 * matches: case-insensitively. NULL when the config never names it. */
37 18 static SV *phd_cfg_for(pTHX_ HV *cfg, const char *name) {
38 HE *e;
39 18 STRLEN nl = strlen(name);
40 18 hv_iterinit(cfg);
41 29 ▶ while ((e = hv_iternext(cfg))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hv_iternext(cfg))) { 14 15 29
42 14 ▶ STRLEN kl; const char *k = HePV(e, kl);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl; const char *k = HePV(e, kl); 0 14 14
43 14 ▶ if (kl == nl && foldEQ(k, name, (I32)nl))
Branch: 2 branches
BranchTrueFalseTotal
if (kl == nl && foldEQ(k, name, (I32)nl)) 5 9 14
if (kl == nl && foldEQ(k, name, (I32)nl)) 3 2 5
44 3 return hv_iterval(cfg, e);
45 }
46 15 return NULL;
47 }
48  
49 /* Merge the keyword's config over the defaults, once, at to_app. Returns a
50 * flat name,value AV (+1): defaults first in fixed order - overridden or
51 * dropped (undef) as configured - then the extra names, sorted so the frozen
52 * order is deterministic. Per request this list is only read. */
53 6 static AV *phd_compile(pTHX_ HV *cfg) {
54 6 AV *pairs = newAV();
55 int i;
56 24 ▶ for (i = 0; i < PHD_DEFAULTS; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < PHD_DEFAULTS; i++) { 18 6 24
57 18 SV *o = phd_cfg_for(aTHX_ cfg, PHD_DEFAULT_K[i]);
58 18 ▶ if (o && !SvOK(o)) continue; /* undef removes a default */
Branch: 2 branches
BranchTrueFalseTotal
if (o && !SvOK(o)) continue; /* undef removes a default */ 3 15 18
if (o && !SvOK(o)) continue; /* undef removes a default */ 2 1 3
59 16 av_push(pairs, newSVpv(PHD_DEFAULT_K[i], 0));
60 16 ▶ av_push(pairs, o ? newSVsv(o) : newSVpv(PHD_DEFAULT_V[i], 0));
Branch: 1 branch
BranchTrueFalseTotal
av_push(pairs, o ? newSVsv(o) : newSVpv(PHD_DEFAULT_V[i], 0)); 1 15 16
61 }
62 {
63 6 AV *extras = (AV *)sv_2mortal((SV *)newAV());
64 HE *e; SSize_t j, n;
65 6 hv_iterinit(cfg);
66 11 ▶ while ((e = hv_iternext(cfg))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hv_iternext(cfg))) { 5 6 11
67 5 ▶ STRLEN kl; const char *k = HePV(e, kl);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl; const char *k = HePV(e, kl); 0 5 5
68 5 SV *v = hv_iterval(cfg, e);
69 5 int d, isdflt = 0;
70 15 ▶ for (d = 0; d < PHD_DEFAULTS; d++)
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < PHD_DEFAULTS; d++) 13 2 15
71 13 ▶ if (kl == strlen(PHD_DEFAULT_K[d])
Branch: 1 branch
BranchTrueFalseTotal
if (kl == strlen(PHD_DEFAULT_K[d]) 4 9 13
72 4 ▶ && foldEQ(k, PHD_DEFAULT_K[d], (I32)kl)) { isdflt = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
&& foldEQ(k, PHD_DEFAULT_K[d], (I32)kl)) { isdflt = 1; break; } 3 1 4
73 /*
74 # uncoverable condition left count:2 class:ignore_covered_err
75 # uncoverable condition left count:3 class:ignore_covered_err */
76 5 ▶ if (isdflt || !v || !SvOK(v)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (isdflt || !v || !SvOK(v)) continue; 2 3 5
if (isdflt || !v || !SvOK(v)) continue; 2 0 2
if (isdflt || !v || !SvOK(v)) continue; 0 2 2
77 2 av_push(extras, newSVpvn(k, kl));
78 }
79 6 n = av_len(extras) + 1;
80 /*
81 # uncoverable branch true
82 # uncoverable condition left count:1 class:ignore_covered_err */
83 6 ▶ if (n > 1) sortsv(AvARRAY(extras), (STRLEN)n, Perl_sv_cmp);
Branch: 1 branch
BranchTrueFalseTotal
if (n > 1) sortsv(AvARRAY(extras), (STRLEN)n, Perl_sv_cmp); 0 6 6
84 8 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 2 6 8
85 2 SV **kp = av_fetch(extras, j, 0);
86 2 ▶ SV **vp = kp && *kp ? hv_fetch(cfg, SvPVX(*kp),
Branch: 1 branch
BranchTrueFalseTotal
SV **vp = kp && *kp ? hv_fetch(cfg, SvPVX(*kp), 2 0 2
87 2 ▶ (I32)SvCUR(*kp), 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
(I32)SvCUR(*kp), 0) : NULL; 2 0 2
88 /*
89 # uncoverable branch true
90 # uncoverable condition left count:1 class:ignore_covered_err
91 # uncoverable condition left count:2 class:ignore_covered_err */
92 2 ▶ if (!(vp && *vp)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(vp && *vp)) continue; 2 0 2
if (!(vp && *vp)) continue; 0 2 2
93 2 av_push(pairs, newSVsv(*kp));
94 2 av_push(pairs, newSVsv(*vp));
95 }
96 }
97 6 return pairs;
98 }
99  
100 /* The scope form of phd_compile: the keyword's pairs flattened with NO
101 * default merging - a scope adds to (or, with undef, subtracts from) the
102 * policy of the responses under its prefix, it does not restate the
103 * application's. Names sorted so the frozen order is deterministic. */
104 3 static AV *phd_flat(pTHX_ HV *cfg) {
105 3 AV *pairs = newAV();
106 3 AV *names = (AV *)sv_2mortal((SV *)newAV());
107 HE *e; SSize_t j, n;
108 3 hv_iterinit(cfg);
109 8 ▶ while ((e = hv_iternext(cfg))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hv_iternext(cfg))) { 5 3 8
110 5 ▶ STRLEN kl; const char *k = HePV(e, kl);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl; const char *k = HePV(e, kl); 0 5 5
111 5 av_push(names, newSVpvn(k, kl));
112 }
113 3 n = av_len(names) + 1;
114 3 ▶ if (n > 1) sortsv(AvARRAY(names), (STRLEN)n, Perl_sv_cmp);
Branch: 1 branch
BranchTrueFalseTotal
if (n > 1) sortsv(AvARRAY(names), (STRLEN)n, Perl_sv_cmp); 1 2 3
115 8 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 5 3 8
116 5 SV **kp = av_fetch(names, j, 0);
117 5 ▶ SV **vp = kp && *kp ? hv_fetch(cfg, SvPVX(*kp),
Branch: 1 branch
BranchTrueFalseTotal
SV **vp = kp && *kp ? hv_fetch(cfg, SvPVX(*kp), 5 0 5
118 5 ▶ (I32)SvCUR(*kp), 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
(I32)SvCUR(*kp), 0) : NULL; 5 0 5
119 /*
120 # uncoverable branch true
121 # uncoverable condition left count:1 class:ignore_covered_err
122 # uncoverable condition left count:2 class:ignore_covered_err */
123 5 ▶ if (!vp || !*vp) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!vp || !*vp) continue; 5 0 5
if (!vp || !*vp) continue; 0 5 5
124 5 av_push(pairs, newSVsv(*kp));
125 5 av_push(pairs, newSVsv(*vp)); /* undef survives: it means drop */
126 }
127 3 return pairs;
128 }
129  
130 /* One name into the seen set, lowercased so the set is case-insensitive.
131 * A name too long for the buffer is simply never deduplicated - harmless,
132 * and no real header name is 120 bytes. */
133 13 static void phd_seen_add(pTHX_ HV *seen, const char *k, STRLEN kl) {
134 char buf[120];
135 STRLEN i;
136 /*
137 # uncoverable branch true
138 # uncoverable condition left count:1 class:ignore_covered_err */
139 13 ▶ if (kl > sizeof(buf)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (kl > sizeof(buf)) return; 0 13 13
140 218 ▶ for (i = 0; i < kl; i++) buf[i] = toLOWER(k[i]);
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < kl; i++) buf[i] = toLOWER(k[i]); 38 167 205
for (i = 0; i < kl; i++) buf[i] = toLOWER(k[i]); 205 13 218
141 13 (void)hv_store(seen, buf, (I32)kl, PUNK_SET_TRUE, 0);
142 }
143 20 static int phd_seen_has(pTHX_ HV *seen, const char *k, STRLEN kl) {
144 char buf[120];
145 STRLEN i;
146 /*
147 # uncoverable branch true
148 # uncoverable condition left count:1 class:ignore_covered_err */
149 20 ▶ if (kl > sizeof(buf)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (kl > sizeof(buf)) return 0; 0 20 20
150 330 ▶ for (i = 0; i < kl; i++) buf[i] = toLOWER(k[i]);
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < kl; i++) buf[i] = toLOWER(k[i]); 56 254 310
for (i = 0; i < kl; i++) buf[i] = toLOWER(k[i]); 310 20 330
151 20 return hv_exists(seen, buf, (I32)kl);
152 }
153  
154 /* The pair list this request's path gets: the matching scoped policies,
155 * longest prefix first, then the application-wide one - first mention of a
156 * name wins, and an undef mention drops the name entirely. Returns an RV
157 * (+1) to the effective flat AV, or NULL when nothing applies. With no scoped
158 * policy and no CSP this is a reference to the frozen list itself, borrowed;
159 * with a CSP the frozen pairs are copied and the policy pair appended. */
160 /* Punk::Plugin::CSP's per-request policy, appended to whatever the `headers`
161 * keyword produced.
162 *
163 * HERE rather than in a hook, and that is the point of putting it here: this
164 * decoration runs OUTSIDE the routing branch, so the preflight 204, the house
165 * 404 and the house 405 all carry the policy too. An error page is exactly
166 * where an injection lands - it is the page most likely to render something
167 * from the request - so a policy that covers the 200s and misses the 404s is
168 * missing from the response that needed it most.
169 *
170 * Set-if-absent comes free with the rest: an application that set its own
171 * Content-Security-Policy on a particular response keeps it.
172 */
173 3794 static void phd_csp_pair(pTHX_ pk_state *S, HV *env, AV *into) {
174 3794 HV *ch = S->csp;
175 SV **rop;
176 int report_only;
177 3794 ▶ if (!ch) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!ch) return; 5 3789 3794
178 3789 rop = hv_fetchs(ch, "report_only", 0);
179 /*
180 # uncoverable condition right count:2 class:ignore_covered_err
181 # uncoverable condition right count:3 class:ignore_covered_err
182 # uncoverable condition right count:4 class:ignore_covered_err */
183 3789 ▶ report_only = (rop && *rop && SvOK(*rop) && SvTRUE(*rop));
Branch: 4 branches
BranchTrueFalseTotal
report_only = (rop && *rop && SvOK(*rop) && SvTRUE(*rop)); 10 3779 3789
report_only = (rop && *rop && SvOK(*rop) && SvTRUE(*rop)); 10 0 10
report_only = (rop && *rop && SvOK(*rop) && SvTRUE(*rop)); 10 0 10
report_only = (rop && *rop && SvOK(*rop) && SvTRUE(*rop)); 10 0 10
184  
185 /* A HASHREF under report_only is a SECOND policy, reported alongside the
186 * first rather than instead of it.
187 *
188 * That combination is the one that gets a strict policy deployed: the
189 * loose one you already trust stays enforcing, and the strict one you are
190 * moving to reports what it would have broken. Treating the two as
191 * exclusive would mean choosing between protection now and knowing what
192 * breaks later, and nobody chooses the second. */
193 /*
194 # uncoverable condition right count:2 class:ignore_covered_err
195 # uncoverable condition right count:4 class:ignore_covered_err */
196 3789 ▶ if (rop && *rop && SvROK(*rop) && SvTYPE(SvRV(*rop)) == SVt_PVHV) {
Branch: 4 branches
BranchTrueFalseTotal
if (rop && *rop && SvROK(*rop) && SvTYPE(SvRV(*rop)) == SVt_PVHV) { 10 3779 3789
if (rop && *rop && SvROK(*rop) && SvTYPE(SvRV(*rop)) == SVt_PVHV) { 10 0 10
if (rop && *rop && SvROK(*rop) && SvTYPE(SvRV(*rop)) == SVt_PVHV) { 5 5 10
if (rop && *rop && SvROK(*rop) && SvTYPE(SvRV(*rop)) == SVt_PVHV) { 5 0 5
197 5 av_push(into, newSVpvs("Content-Security-Policy"));
198 5 av_push(into, pcsp_policy(aTHX_ ch, env));
199 5 av_push(into, newSVpvs("Content-Security-Policy-Report-Only"));
200 5 av_push(into, pcsp_policy(aTHX_ (HV *)SvRV(*rop), env));
201 5 return;
202 }
203  
204 3784 ▶ av_push(into, newSVpv(report_only
Branch: 1 branch
BranchTrueFalseTotal
av_push(into, newSVpv(report_only 5 3779 3784
205 ? "Content-Security-Policy-Report-Only"
206 : "Content-Security-Policy", 0));
207 3784 av_push(into, pcsp_policy(aTHX_ ch, env));
208 }
209  
210 221548 static SV *phd_effective(pTHX_ pk_state *S, HV *env) {
211 221548 AV *app_pairs = S->headers;
212 AV *scoped, *merged;
213 HV *seen;
214 SSize_t i, n;
215 221548 int hits = 0;
216 SV *pi;
217 const char *p; STRLEN pl;
218  
219 221548 ▶ if (!S->headers_scoped) {
Branch: 1 branch
BranchTrueFalseTotal
if (!S->headers_scoped) { 221542 6 221548
220 221542 AV *only = app_pairs;
221 AV *outv;
222 /* nothing configured: nothing to allocate either */
223 221542 ▶ if (!only && !S->csp) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!only && !S->csp) return NULL; 221532 10 221542
if (!only && !S->csp) return NULL; 217743 3789 221532
224 /* no CSP to append: the frozen list itself, borrowed. Every reader
225 * copies what it takes from it (phd_add_absent, the stream and
226 * Future wrappers hold the reference), so it is never written. */
227 3799 ▶ if (!S->csp) return newRV_inc((SV *)only);
Branch: 1 branch
BranchTrueFalseTotal
if (!S->csp) return newRV_inc((SV *)only); 10 3789 3799
228 3789 outv = newAV();
229 /*
230 # uncoverable branch true
231 # uncoverable condition left count:1 class:ignore_covered_err */
232 3789 ▶ if (only) {
Branch: 1 branch
BranchTrueFalseTotal
if (only) { 0 3789 3789
233 0 SSize_t k, kn = av_len(only) + 1; /* # uncoverable statement */
234 /*
235 # uncoverable statement
236 # uncoverable branch true
237 # uncoverable branch false
238 # uncoverable condition left count:1
239 # uncoverable condition right count:1 */
240 0 ▶ for (k = 0; k < kn; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < kn; k++) { 0 0 0
241 0 SV **x = av_fetch(only, k, 0); /* # uncoverable statement */
242 /*
243 # uncoverable statement
244 # uncoverable condition left count:1
245 # uncoverable condition right count:1
246 # uncoverable condition left count:2
247 # uncoverable condition right count:2 */
248 0 ▶ av_push(outv, (x && *x) ? newSVsv(*x) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(outv, (x && *x) ? newSVsv(*x) : newSV(0)); 0 0 0
av_push(outv, (x && *x) ? newSVsv(*x) : newSV(0)); 0 0 0
249 }
250 }
251 3789 phd_csp_pair(aTHX_ S, env, outv);
252 /*
253 # uncoverable branch true
254 # uncoverable condition left count:1 class:ignore_covered_err */
255 3789 ▶ if (av_len(outv) < 0) { SvREFCNT_dec((SV *)outv); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(outv) < 0) { SvREFCNT_dec((SV *)outv); return NULL; } 0 3789 3789
256 3789 return newRV_noinc((SV *)outv);
257 }
258  
259 6 scoped = S->headers_scoped;
260 6 n = av_len(scoped) + 1;
261 6 pi = pco_env(aTHX_ env, "PATH_INFO");
262 12 ▶ p = pi ? SvPV_const(pi, pl) : "/"; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
p = pi ? SvPV_const(pi, pl) : "/"; /* # uncoverable condition right count:1 class:ignore_covered_err */ 6 0 6
263 /*
264 # uncoverable branch true
265 # uncoverable condition left count:1 class:ignore_covered_err */
266 6 ▶ if (!pi) pl = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!pi) pl = 1; 0 6 6
267  
268 6 merged = NULL;
269 6 seen = NULL;
270 16 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 10 6 16
271 10 SV **rp = av_fetch(scoped, i, 0);
272 HV *rec; SV **x; AV *pairs;
273 const char *pf; STRLEN pfl;
274 SSize_t j, np;
275 /*
276 # uncoverable branch true class:ignore_covered_err
277 # uncoverable condition left count:1 class:ignore_covered_err
278 # uncoverable condition left count:2 class:ignore_covered_err
279 # uncoverable condition left count:3 class:ignore_covered_err */
280 15 ▶ if (!(rp && *rp && SvROK(*rp))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(rp && *rp && SvROK(*rp))) continue; 10 0 10
if (!(rp && *rp && SvROK(*rp))) continue; 10 0 10
if (!(rp && *rp && SvROK(*rp))) continue; 0 10 10
281 10 rec = (HV *)SvRV(*rp);
282 10 x = hv_fetchs(rec, K_PREFIX, 0);
283 /*
284 # uncoverable branch false
285 # uncoverable condition right count:1 class:ignore_covered_err
286 # uncoverable condition right count:2 class:ignore_covered_err
287 # uncoverable condition right count:3 class:ignore_covered_err */
288 20 ▶ if (x && *x && SvOK(*x)) pf = SvPV_const(*x, pfl);
Branch: 3 branches
BranchTrueFalseTotal
if (x && *x && SvOK(*x)) pf = SvPV_const(*x, pfl); 10 0 10
if (x && *x && SvOK(*x)) pf = SvPV_const(*x, pfl); 10 0 10
if (x && *x && SvOK(*x)) pf = SvPV_const(*x, pfl); 10 0 10
289 0 else { pf = ""; pfl = 0; } /* # uncoverable statement */
290 10 ▶ if (!ps_under(p, pl, pf, pfl)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ps_under(p, pl, pf, pfl)) continue; 5 5 10
291 5 x = hv_fetchs(rec, K_HEADERS, 0);
292 /*
293 # uncoverable branch true
294 # uncoverable condition left count:1 class:ignore_covered_err
295 # uncoverable condition left count:2 class:ignore_covered_err
296 # uncoverable condition left count:3 class:ignore_covered_err
297 # uncoverable condition left count:4 class:ignore_covered_err */
298 5 ▶ if (!(x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV)) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!(x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV)) continue; 5 0 5
if (!(x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV)) continue; 5 0 5
if (!(x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV)) continue; 5 0 5
if (!(x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV)) continue; 0 5 5
299 5 pairs = (AV *)SvRV(*x);
300 5 ▶ if (!merged) {
Branch: 1 branch
BranchTrueFalseTotal
if (!merged) { 4 1 5
301 4 merged = newAV();
302 4 seen = (HV *)sv_2mortal((SV *)newHV());
303 }
304 5 hits++;
305 5 np = av_len(pairs) + 1;
306 16 ▶ for (j = 0; j + 1 < np; j += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j + 1 < np; j += 2) { 11 5 16
307 11 SV **kp = av_fetch(pairs, j, 0);
308 11 SV **vp = av_fetch(pairs, j + 1, 0);
309 STRLEN kl; const char *k;
310 /*
311 # uncoverable branch true class:ignore_covered_err
312 # uncoverable condition left count:1 class:ignore_covered_err
313 # uncoverable condition left count:2 class:ignore_covered_err
314 # uncoverable condition left count:3 class:ignore_covered_err
315 # uncoverable condition left count:4 class:ignore_covered_err */
316 15 ▶ if (!(kp && *kp && vp && *vp)) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!(kp && *kp && vp && *vp)) continue; 11 0 11
if (!(kp && *kp && vp && *vp)) continue; 11 0 11
if (!(kp && *kp && vp && *vp)) continue; 11 0 11
if (!(kp && *kp && vp && *vp)) continue; 0 11 11
317 11 k = SvPV_const(*kp, kl);
318 11 ▶ if (phd_seen_has(aTHX_ seen, k, kl)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (phd_seen_has(aTHX_ seen, k, kl)) continue; 1 10 11
319 10 phd_seen_add(aTHX_ seen, k, kl);
320 10 ▶ if (!SvOK(*vp)) continue; /* dropped under this prefix */
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(*vp)) continue; /* dropped under this prefix */ 3 7 10
321 7 av_push(merged, newSVsv(*kp));
322 7 av_push(merged, newSVsv(*vp));
323 }
324 }
325 6 ▶ if (!hits) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hits) { 2 4 6
326 2 AV *only = app_pairs;
327 AV *outv;
328 2 ▶ if (!only && !S->csp) return NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!only && !S->csp) return NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 1 2
if (!only && !S->csp) return NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
329 1 outv = newAV();
330 /*
331 # uncoverable branch false
332 # uncoverable condition right count:1 class:ignore_covered_err */
333 1 ▶ if (only) {
Branch: 1 branch
BranchTrueFalseTotal
if (only) { 1 0 1
334 1 SSize_t k, kn = av_len(only) + 1;
335 7 ▶ for (k = 0; k < kn; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < kn; k++) { 6 1 7
336 6 SV **x = av_fetch(only, k, 0);
337 /*
338 # uncoverable condition right count:1 class:ignore_covered_err
339 # uncoverable condition right count:2 class:ignore_covered_err */
340 6 ▶ av_push(outv, (x && *x) ? newSVsv(*x) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(outv, (x && *x) ? newSVsv(*x) : newSV(0)); 6 0 6
av_push(outv, (x && *x) ? newSVsv(*x) : newSV(0)); 6 0 6
341 }
342 }
343 1 phd_csp_pair(aTHX_ S, env, outv);
344 /*
345 # uncoverable branch true
346 # uncoverable condition left count:1 class:ignore_covered_err */
347 1 ▶ if (av_len(outv) < 0) { SvREFCNT_dec((SV *)outv); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(outv) < 0) { SvREFCNT_dec((SV *)outv); return NULL; } 0 1 1
348 1 return newRV_noinc((SV *)outv);
349 }
350 4 ▶ if (app_pairs) {
Branch: 1 branch
BranchTrueFalseTotal
if (app_pairs) { 3 1 4
351 3 AV *pairs = app_pairs;
352 3 SSize_t j, np = av_len(pairs) + 1;
353 12 ▶ for (j = 0; j + 1 < np; j += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j + 1 < np; j += 2) { 9 3 12
354 9 SV **kp = av_fetch(pairs, j, 0);
355 9 SV **vp = av_fetch(pairs, j + 1, 0);
356 STRLEN kl; const char *k;
357 /*
358 # uncoverable branch true class:ignore_covered_err
359 # uncoverable condition left count:1 class:ignore_covered_err
360 # uncoverable condition left count:2 class:ignore_covered_err
361 # uncoverable condition left count:3 class:ignore_covered_err
362 # uncoverable condition left count:4 class:ignore_covered_err */
363 15 ▶ if (!(kp && *kp && vp && *vp)) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!(kp && *kp && vp && *vp)) continue; 9 0 9
if (!(kp && *kp && vp && *vp)) continue; 9 0 9
if (!(kp && *kp && vp && *vp)) continue; 9 0 9
if (!(kp && *kp && vp && *vp)) continue; 0 9 9
364 9 k = SvPV_const(*kp, kl);
365 9 ▶ if (phd_seen_has(aTHX_ seen, k, kl)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (phd_seen_has(aTHX_ seen, k, kl)) continue; 6 3 9
366 3 phd_seen_add(aTHX_ seen, k, kl);
367 3 av_push(merged, newSVsv(*kp));
368 3 av_push(merged, newSVsv(*vp));
369 }
370 }
371 4 phd_csp_pair(aTHX_ S, env, merged);
372 4 return newRV_noinc((SV *)merged);
373 }
374  
375 /* Push every frozen pair the response does not already carry. The scan is the
376 * case-insensitive walk Punk::Response::header does; header lists are a
377 * handful of pairs, so the quadratic shape is cheaper than any index. */
378 3804 static void phd_add_absent(pTHX_ AV *headers, AV *pairs) {
379 3804 SSize_t i, np = av_len(pairs) + 1;
380 7641 ▶ for (i = 0; i + 1 < np; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < np; i += 2) { 3837 3804 7641
381 3837 SV **kp = av_fetch(pairs, i, 0);
382 3837 SV **vp = av_fetch(pairs, i + 1, 0);
383 STRLEN kl; const char *k;
384 3837 SSize_t j, nh = av_len(headers) + 1;
385 3837 int found = 0;
386 /*
387 # uncoverable branch true
388 # uncoverable condition left count:1 class:ignore_covered_err
389 # uncoverable condition left count:2 class:ignore_covered_err
390 # uncoverable condition left count:3 class:ignore_covered_err
391 # uncoverable condition left count:4 class:ignore_covered_err */
392 3837 ▶ if (!(kp && *kp && vp && *vp)) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!(kp && *kp && vp && *vp)) continue; 3837 0 3837
if (!(kp && *kp && vp && *vp)) continue; 3837 0 3837
if (!(kp && *kp && vp && *vp)) continue; 3837 0 3837
if (!(kp && *kp && vp && *vp)) continue; 0 3837 3837
393 3837 k = SvPV_const(*kp, kl);
394 11544 ▶ for (j = 0; j + 1 < nh; j += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j + 1 < nh; j += 2) { 7713 3831 11544
395 7713 SV **e = av_fetch(headers, j, 0);
396 STRLEN el; const char *es;
397 /*
398 # uncoverable branch true
399 # uncoverable condition left count:1 class:ignore_covered_err
400 # uncoverable condition left count:2 class:ignore_covered_err */
401 7713 ▶ if (!(e && *e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(e && *e)) continue; 7713 0 7713
if (!(e && *e)) continue; 0 7713 7713
402 7713 es = SvPV_const(*e, el);
403 7713 ▶ if (el == kl && foldEQ(es, k, (I32)kl)) { found = 1; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (el == kl && foldEQ(es, k, (I32)kl)) { found = 1; break; } 19 7694 7713
if (el == kl && foldEQ(es, k, (I32)kl)) { found = 1; break; } 6 13 19
404 }
405 3837 ▶ if (!found) {
Branch: 1 branch
BranchTrueFalseTotal
if (!found) { 3831 6 3837
406 3831 av_push(headers, newSVsv(*kp));
407 3831 av_push(headers, newSVsv(*vp));
408 }
409 }
410 3804 }
411  
412 /* The responder wrapper for a streaming response - the phd twin of
413 * pco_responder_cb, capturing [inner, pairs] instead of a CORS grant. */
414 XS_INTERNAL(phd_responder_cb);
415 1 XS_INTERNAL(phd_responder_cb) {
416 1 dXSARGS;
417 1 AV *cap = punk_clos_cap(aTHX_ cv);
418 SV *inner, *pairs_sv;
419 /*
420 # uncoverable branch true
421 # uncoverable condition left count:1 class:ignore_covered_err
422 # uncoverable condition left count:2 class:ignore_covered_err */
423 1 ▶ if (!cap || items < 1) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!cap || items < 1) XSRETURN_EMPTY; 1 0 1
if (!cap || items < 1) XSRETURN_EMPTY; 0 1 1
424 1 inner = *av_fetch(cap, 0, 0);
425 1 pairs_sv = *av_fetch(cap, 1, 0);
426 {
427 1 AV *headers = pco_headers_of(aTHX_ ST(0));
428 /*
429 # uncoverable branch false
430 # uncoverable condition right count:1 class:ignore_covered_err
431 # uncoverable condition right count:2 class:ignore_covered_err */
432 1 ▶ if (headers && SvROK(pairs_sv))
Branch: 2 branches
BranchTrueFalseTotal
if (headers && SvROK(pairs_sv)) 1 0 1
if (headers && SvROK(pairs_sv)) 1 0 1
433 1 phd_add_absent(aTHX_ headers, (AV *)SvRV(pairs_sv));
434 }
435 {
436 1 dSP; int count; SV *r;
437 1 ENTER; SAVETMPS;
438 1 ▶ PUSHMARK(SP); EXTEND(SP, 1); PUSHs(ST(0)); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(ST(0)); PUTBACK; 0 1 1
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(ST(0)); PUTBACK; 0 1 1
439 1 count = call_sv(inner, G_SCALAR);
440 1 SPAGAIN;
441 1 ▶ r = count > 0 ? SvREFCNT_inc(POPs) : newSV(0); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
r = count > 0 ? SvREFCNT_inc(POPs) : newSV(0); /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 0 1
442 1 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 1 0 1
443 1 ST(0) = sv_2mortal(r);
444 }
445 1 XSRETURN(1);
446 }
447  
448 XS_INTERNAL(phd_stream_cb);
449 1 XS_INTERNAL(phd_stream_cb) {
450 1 dXSARGS;
451 1 AV *cap = punk_clos_cap(aTHX_ cv);
452 SV *app, *pairs_sv, *wrapped;
453 AV *rcap;
454 /*
455 # uncoverable branch true
456 # uncoverable condition left count:1 class:ignore_covered_err
457 # uncoverable condition left count:2 class:ignore_covered_err */
458 1 ▶ if (!cap || items < 1) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!cap || items < 1) XSRETURN_EMPTY; 1 0 1
if (!cap || items < 1) XSRETURN_EMPTY; 0 1 1
459 1 app = *av_fetch(cap, 0, 0);
460 1 pairs_sv = *av_fetch(cap, 1, 0);
461  
462 1 rcap = newAV();
463 1 av_push(rcap, newSVsv(ST(0))); /* the server's responder */
464 1 av_push(rcap, newSVsv(pairs_sv));
465 1 wrapped = sv_2mortal(punk_closure(aTHX_ phd_responder_cb, rcap));
466  
467 {
468 1 dSP;
469 1 ENTER; SAVETMPS;
470 1 ▶ PUSHMARK(SP); EXTEND(SP, 1); PUSHs(wrapped); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(wrapped); PUTBACK; 0 1 1
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(wrapped); PUTBACK; 0 1 1
471 1 (void)call_sv(app, G_VOID);
472 1 SPAGAIN;
473 1 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 1 0 1
474 }
475 1 XSRETURN_EMPTY;
476 }
477  
478 /* Decorate whatever pc_app_cb is about to return - the same three shapes
479 * pco_decorate handles, with the same replacements in place. */
480 221548 static void phd_decorate(pTHX_ pk_state *S, HV *env, SV **resp) {
481 SV *pairs_sv;
482 AV *pairs;
483  
484 /*
485 # uncoverable branch true
486 # uncoverable condition left count:1 class:ignore_covered_err
487 # uncoverable condition left count:2 class:ignore_covered_err */
488 221548 ▶ if (!(*resp && SvROK(*resp))) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!(*resp && SvROK(*resp))) return; 221548 0 221548
if (!(*resp && SvROK(*resp))) return; 0 221548 221548
489 221548 pairs_sv = phd_effective(aTHX_ S, env);
490 221548 ▶ if (!pairs_sv) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!pairs_sv) return; 217744 3804 221548
491 3804 sv_2mortal(pairs_sv);
492 3804 pairs = (AV *)SvRV(pairs_sv);
493 /*
494 # uncoverable branch true
495 # uncoverable condition left count:1 class:ignore_covered_err */
496 3804 ▶ if (av_len(pairs) < 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(pairs) < 0) return; 0 3804 3804
497  
498 3804 ▶ if (SvTYPE(SvRV(*resp)) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(*resp)) == SVt_PVAV) { 3802 2 3804
499 3802 AV *headers = pco_headers_of(aTHX_ *resp);
500 /*
501 # uncoverable branch false
502 # uncoverable condition right count:1 class:ignore_covered_err */
503 3802 ▶ if (headers) phd_add_absent(aTHX_ headers, pairs);
Branch: 1 branch
BranchTrueFalseTotal
if (headers) phd_add_absent(aTHX_ headers, pairs); 3802 0 3802
504 3802 return;
505 }
506  
507 2 ▶ if (SvTYPE(SvRV(*resp)) == SVt_PVCV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(*resp)) == SVt_PVCV) { 1 1 2
508 1 AV *cap = newAV();
509 1 av_push(cap, newSVsv(*resp));
510 1 av_push(cap, newSVsv(pairs_sv));
511 1 SvREFCNT_dec(*resp);
512 1 *resp = punk_closure(aTHX_ phd_stream_cb, cap);
513 1 return;
514 }
515  
516 /*
517 # uncoverable branch false
518 # uncoverable condition right count:1 class:ignore_covered_err
519 # uncoverable condition right count:2 class:ignore_covered_err */
520 1 ▶ if (SvOBJECT(SvRV(*resp)) && pcx_can(aTHX_ *resp, "then")) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvOBJECT(SvRV(*resp)) && pcx_can(aTHX_ *resp, "then")) { 1 0 1
if (SvOBJECT(SvRV(*resp)) && pcx_can(aTHX_ *resp, "then")) { 1 0 1
521 1 dSP; int count; SV *chained = NULL;
522 1 ENTER; SAVETMPS;
523 1 ▶ PUSHMARK(SP); EXTEND(SP, 2);
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 2); 0 1 1
PUSHMARK(SP); EXTEND(SP, 2); 0 1 1
524 1 PUSHs(*resp);
525 1 PUSHs(pairs_sv);
526 1 PUTBACK;
527 1 count = call_pv("Punk::Headers::_chain", G_SCALAR | G_EVAL);
528 1 SPAGAIN;
529 /*
530 # uncoverable branch false
531 # uncoverable condition right count:1 class:ignore_covered_err
532 # uncoverable condition right count:2 class:ignore_covered_err */
533 1 ▶ if (count > 0 && !SvTRUE(ERRSV)) chained = SvREFCNT_inc(POPs);
Branch: 3 branches
BranchTrueFalseTotal
if (count > 0 && !SvTRUE(ERRSV)) chained = SvREFCNT_inc(POPs); 1 0 1
if (count > 0 && !SvTRUE(ERRSV)) chained = SvREFCNT_inc(POPs); 1 0 1
if (count > 0 && !SvTRUE(ERRSV)) chained = SvREFCNT_inc(POPs); 1 0 1
534 /*
535 # uncoverable statement
536 # uncoverable branch true
537 # uncoverable branch false
538 # uncoverable condition left count:1
539 # uncoverable condition right count:1 */
540 0 ▶ else if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) (void)POPs; 0 0 0
541 1 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 1 0 1
542 /*
543 # uncoverable branch false
544 # uncoverable condition right count:1 class:ignore_covered_err */
545 1 ▶ if (chained) { SvREFCNT_dec(*resp); *resp = chained; }
Branch: 1 branch
BranchTrueFalseTotal
if (chained) { SvREFCNT_dec(*resp); *resp = chained; } 1 0 1
546 1 return;
547 }
548 }
549  
550 #endif /* PUNK_HEADERS_H */
« include/punk/punk_mrows.h ↑ Summary xs/context.xs »