include/punk/punk_headers.h

filter: Statementstmt 97.4% 228 / 234 Branchbran 66.9% 178 / 266 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 81.2% 406 / 500 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/idempotency.xs ↑ Summary include/punk/punk_lang.h »
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 27 ▶ while ((e = hv_iternext(cfg))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hv_iternext(cfg))) { 12 15 27
42 12 ▶ STRLEN kl; const char *k = HePV(e, kl);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl; const char *k = HePV(e, kl); 0 12 12
43 12 ▶ if (kl == nl && foldEQ(k, name, (I32)nl))
Branch: 2 branches
BranchTrueFalseTotal
if (kl == nl && foldEQ(k, name, (I32)nl)) 5 7 12
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 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
74 2 av_push(extras, newSVpvn(k, kl));
75 }
76 6 n = av_len(extras) + 1;
77 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
78 8 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 2 6 8
79 2 SV **kp = av_fetch(extras, j, 0);
80 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
81 2 ▶ (I32)SvCUR(*kp), 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
(I32)SvCUR(*kp), 0) : NULL; 2 0 2
82 2 ▶ if (!(vp && *vp)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(vp && *vp)) continue; 2 0 2
if (!(vp && *vp)) continue; 0 2 2
83 2 av_push(pairs, newSVsv(*kp));
84 2 av_push(pairs, newSVsv(*vp));
85 }
86 }
87 6 return pairs;
88 }
89  
90 /* The scope form of phd_compile: the keyword's pairs flattened with NO
91 * default merging - a scope adds to (or, with undef, subtracts from) the
92 * policy of the responses under its prefix, it does not restate the
93 * application's. Names sorted so the frozen order is deterministic. */
94 3 static AV *phd_flat(pTHX_ HV *cfg) {
95 3 AV *pairs = newAV();
96 3 AV *names = (AV *)sv_2mortal((SV *)newAV());
97 HE *e; SSize_t j, n;
98 3 hv_iterinit(cfg);
99 8 ▶ while ((e = hv_iternext(cfg))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hv_iternext(cfg))) { 5 3 8
100 5 ▶ STRLEN kl; const char *k = HePV(e, kl);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl; const char *k = HePV(e, kl); 0 5 5
101 5 av_push(names, newSVpvn(k, kl));
102 }
103 3 n = av_len(names) + 1;
104 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
105 8 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 5 3 8
106 5 SV **kp = av_fetch(names, j, 0);
107 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
108 5 ▶ (I32)SvCUR(*kp), 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
(I32)SvCUR(*kp), 0) : NULL; 5 0 5
109 5 ▶ if (!vp || !*vp) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!vp || !*vp) continue; 5 0 5
if (!vp || !*vp) continue; 0 5 5
110 5 av_push(pairs, newSVsv(*kp));
111 5 av_push(pairs, newSVsv(*vp)); /* undef survives: it means drop */
112 }
113 3 return pairs;
114 }
115  
116 /* One name into the seen set, lowercased so the set is case-insensitive.
117 * A name too long for the buffer is simply never deduplicated - harmless,
118 * and no real header name is 120 bytes. */
119 13 static void phd_seen_add(pTHX_ HV *seen, const char *k, STRLEN kl) {
120 char buf[120];
121 STRLEN i;
122 13 ▶ if (kl > sizeof(buf)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (kl > sizeof(buf)) return; 0 13 13
123 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
124 13 (void)hv_store(seen, buf, (I32)kl, PUNK_SET_TRUE, 0);
125 }
126 20 static int phd_seen_has(pTHX_ HV *seen, const char *k, STRLEN kl) {
127 char buf[120];
128 STRLEN i;
129 20 ▶ if (kl > sizeof(buf)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (kl > sizeof(buf)) return 0; 0 20 20
130 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
131 20 return hv_exists(seen, buf, (I32)kl);
132 }
133  
134 /* The pair list this request's path gets: the matching scoped policies,
135 * longest prefix first, then the application-wide one - first mention of a
136 * name wins, and an undef mention drops the name entirely. Returns an RV
137 * (+1) to the effective flat AV, or NULL when nothing applies. With no scoped
138 * policy and no CSP this is a reference to the frozen list itself, borrowed;
139 * with a CSP the frozen pairs are copied and the policy pair appended. */
140 /* Punk::Plugin::CSP's per-request policy, appended to whatever the `headers`
141 * keyword produced.
142 *
143 * HERE rather than in a hook, and that is the point of putting it here: this
144 * decoration runs OUTSIDE the routing branch, so the preflight 204, the house
145 * 404 and the house 405 all carry the policy too. An error page is exactly
146 * where an injection lands - it is the page most likely to render something
147 * from the request - so a policy that covers the 200s and misses the 404s is
148 * missing from the response that needed it most.
149 *
150 * Set-if-absent comes free with the rest: an application that set its own
151 * Content-Security-Policy on a particular response keeps it.
152 */
153 3782 static void phd_csp_pair(pTHX_ pk_state *S, HV *env, AV *into) {
154 3782 HV *ch = S->csp;
155 SV **rop;
156 int report_only;
157 3782 ▶ if (!ch) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!ch) return; 5 3777 3782
158 3777 rop = hv_fetchs(ch, "report_only", 0);
159 3777 ▶ report_only = (rop && *rop && SvOK(*rop) && SvTRUE(*rop));
Branch: 4 branches
BranchTrueFalseTotal
report_only = (rop && *rop && SvOK(*rop) && SvTRUE(*rop)); 10 3767 3777
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
160  
161 /* A HASHREF under report_only is a SECOND policy, reported alongside the
162 * first rather than instead of it.
163 *
164 * That combination is the one that gets a strict policy deployed: the
165 * loose one you already trust stays enforcing, and the strict one you are
166 * moving to reports what it would have broken. Treating the two as
167 * exclusive would mean choosing between protection now and knowing what
168 * breaks later, and nobody chooses the second. */
169 3777 ▶ 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 3767 3777
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
170 5 av_push(into, newSVpvs("Content-Security-Policy"));
171 5 av_push(into, pcsp_policy(aTHX_ ch, env));
172 5 av_push(into, newSVpvs("Content-Security-Policy-Report-Only"));
173 5 av_push(into, pcsp_policy(aTHX_ (HV *)SvRV(*rop), env));
174 5 return;
175 }
176  
177 3772 ▶ av_push(into, newSVpv(report_only
Branch: 1 branch
BranchTrueFalseTotal
av_push(into, newSVpv(report_only 5 3767 3772
178 ? "Content-Security-Policy-Report-Only"
179 : "Content-Security-Policy", 0));
180 3772 av_push(into, pcsp_policy(aTHX_ ch, env));
181 }
182  
183 219532 static SV *phd_effective(pTHX_ pk_state *S, HV *env) {
184 219532 AV *app_pairs = S->headers;
185 AV *scoped, *merged;
186 HV *seen;
187 SSize_t i, n;
188 219532 int hits = 0;
189 SV *pi;
190 const char *p; STRLEN pl;
191  
192 219532 ▶ if (!S->headers_scoped) {
Branch: 1 branch
BranchTrueFalseTotal
if (!S->headers_scoped) { 219526 6 219532
193 219526 AV *only = app_pairs;
194 AV *outv;
195 /* nothing configured: nothing to allocate either */
196 219526 ▶ if (!only && !S->csp) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!only && !S->csp) return NULL; 219516 10 219526
if (!only && !S->csp) return NULL; 215739 3777 219516
197 /* no CSP to append: the frozen list itself, borrowed. Every reader
198 * copies what it takes from it (phd_add_absent, the stream and
199 * Future wrappers hold the reference), so it is never written. */
200 3787 ▶ if (!S->csp) return newRV_inc((SV *)only);
Branch: 1 branch
BranchTrueFalseTotal
if (!S->csp) return newRV_inc((SV *)only); 10 3777 3787
201 3777 outv = newAV();
202 3777 ▶ if (only) {
Branch: 1 branch
BranchTrueFalseTotal
if (only) { 0 3777 3777
203 0 SSize_t k, kn = av_len(only) + 1;
204 0 ▶ for (k = 0; k < kn; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < kn; k++) { 0 0 0
205 0 SV **x = av_fetch(only, k, 0);
206 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
207 }
208 }
209 3777 phd_csp_pair(aTHX_ S, env, outv);
210 3777 ▶ 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 3777 3777
211 3777 return newRV_noinc((SV *)outv);
212 }
213  
214 6 scoped = S->headers_scoped;
215 6 n = av_len(scoped) + 1;
216 6 pi = pco_env(aTHX_ env, "PATH_INFO");
217 12 ▶ p = pi ? SvPV_const(pi, pl) : "/";
Branch: 1 branch
BranchTrueFalseTotal
p = pi ? SvPV_const(pi, pl) : "/"; 6 0 6
218 6 ▶ if (!pi) pl = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!pi) pl = 1; 0 6 6
219  
220 6 merged = NULL;
221 6 seen = NULL;
222 16 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 10 6 16
223 10 SV **rp = av_fetch(scoped, i, 0);
224 HV *rec; SV **x; AV *pairs;
225 const char *pf; STRLEN pfl;
226 SSize_t j, np;
227 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
228 10 rec = (HV *)SvRV(*rp);
229 10 x = hv_fetchs(rec, K_PREFIX, 0);
230 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
231 0 else { pf = ""; pfl = 0; }
232 10 ▶ if (!ps_under(p, pl, pf, pfl)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ps_under(p, pl, pf, pfl)) continue; 5 5 10
233 5 x = hv_fetchs(rec, K_HEADERS, 0);
234 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
235 5 pairs = (AV *)SvRV(*x);
236 5 ▶ if (!merged) {
Branch: 1 branch
BranchTrueFalseTotal
if (!merged) { 4 1 5
237 4 merged = newAV();
238 4 seen = (HV *)sv_2mortal((SV *)newHV());
239 }
240 5 hits++;
241 5 np = av_len(pairs) + 1;
242 16 ▶ for (j = 0; j + 1 < np; j += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j + 1 < np; j += 2) { 11 5 16
243 11 SV **kp = av_fetch(pairs, j, 0);
244 11 SV **vp = av_fetch(pairs, j + 1, 0);
245 STRLEN kl; const char *k;
246 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
247 11 k = SvPV_const(*kp, kl);
248 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
249 10 phd_seen_add(aTHX_ seen, k, kl);
250 10 ▶ if (!SvOK(*vp)) continue; /* dropped under this prefix */
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(*vp)) continue; /* dropped under this prefix */ 3 7 10
251 7 av_push(merged, newSVsv(*kp));
252 7 av_push(merged, newSVsv(*vp));
253 }
254 }
255 6 ▶ if (!hits) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hits) { 2 4 6
256 2 AV *only = app_pairs;
257 AV *outv;
258 2 ▶ if (!only && !S->csp) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!only && !S->csp) return NULL; 1 1 2
if (!only && !S->csp) return NULL; 1 0 1
259 1 outv = newAV();
260 1 ▶ if (only) {
Branch: 1 branch
BranchTrueFalseTotal
if (only) { 1 0 1
261 1 SSize_t k, kn = av_len(only) + 1;
262 7 ▶ for (k = 0; k < kn; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < kn; k++) { 6 1 7
263 6 SV **x = av_fetch(only, k, 0);
264 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
265 }
266 }
267 1 phd_csp_pair(aTHX_ S, env, outv);
268 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
269 1 return newRV_noinc((SV *)outv);
270 }
271 4 ▶ if (app_pairs) {
Branch: 1 branch
BranchTrueFalseTotal
if (app_pairs) { 3 1 4
272 3 AV *pairs = app_pairs;
273 3 SSize_t j, np = av_len(pairs) + 1;
274 12 ▶ for (j = 0; j + 1 < np; j += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j + 1 < np; j += 2) { 9 3 12
275 9 SV **kp = av_fetch(pairs, j, 0);
276 9 SV **vp = av_fetch(pairs, j + 1, 0);
277 STRLEN kl; const char *k;
278 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
279 9 k = SvPV_const(*kp, kl);
280 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
281 3 phd_seen_add(aTHX_ seen, k, kl);
282 3 av_push(merged, newSVsv(*kp));
283 3 av_push(merged, newSVsv(*vp));
284 }
285 }
286 4 phd_csp_pair(aTHX_ S, env, merged);
287 4 return newRV_noinc((SV *)merged);
288 }
289  
290 /* Push every frozen pair the response does not already carry. The scan is the
291 * case-insensitive walk Punk::Response::header does; header lists are a
292 * handful of pairs, so the quadratic shape is cheaper than any index. */
293 3792 static void phd_add_absent(pTHX_ AV *headers, AV *pairs) {
294 3792 SSize_t i, np = av_len(pairs) + 1;
295 7617 ▶ for (i = 0; i + 1 < np; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < np; i += 2) { 3825 3792 7617
296 3825 SV **kp = av_fetch(pairs, i, 0);
297 3825 SV **vp = av_fetch(pairs, i + 1, 0);
298 STRLEN kl; const char *k;
299 3825 SSize_t j, nh = av_len(headers) + 1;
300 3825 int found = 0;
301 3825 ▶ if (!(kp && *kp && vp && *vp)) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!(kp && *kp && vp && *vp)) continue; 3825 0 3825
if (!(kp && *kp && vp && *vp)) continue; 3825 0 3825
if (!(kp && *kp && vp && *vp)) continue; 3825 0 3825
if (!(kp && *kp && vp && *vp)) continue; 0 3825 3825
302 3825 k = SvPV_const(*kp, kl);
303 11514 ▶ for (j = 0; j + 1 < nh; j += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j + 1 < nh; j += 2) { 7695 3819 11514
304 7695 SV **e = av_fetch(headers, j, 0);
305 STRLEN el; const char *es;
306 7695 ▶ if (!(e && *e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(e && *e)) continue; 7695 0 7695
if (!(e && *e)) continue; 0 7695 7695
307 7695 es = SvPV_const(*e, el);
308 7695 ▶ 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 7676 7695
if (el == kl && foldEQ(es, k, (I32)kl)) { found = 1; break; } 6 13 19
309 }
310 3825 ▶ if (!found) {
Branch: 1 branch
BranchTrueFalseTotal
if (!found) { 3819 6 3825
311 3819 av_push(headers, newSVsv(*kp));
312 3819 av_push(headers, newSVsv(*vp));
313 }
314 }
315 3792 }
316  
317 /* The responder wrapper for a streaming response - the phd twin of
318 * pco_responder_cb, capturing [inner, pairs] instead of a CORS grant. */
319 XS_INTERNAL(phd_responder_cb);
320 1 XS_INTERNAL(phd_responder_cb) {
321 1 dXSARGS;
322 1 AV *cap = punk_clos_cap(aTHX_ cv);
323 SV *inner, *pairs_sv;
324 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
325 1 inner = *av_fetch(cap, 0, 0);
326 1 pairs_sv = *av_fetch(cap, 1, 0);
327 {
328 1 AV *headers = pco_headers_of(aTHX_ ST(0));
329 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
330 1 phd_add_absent(aTHX_ headers, (AV *)SvRV(pairs_sv));
331 }
332 {
333 1 dSP; int count; SV *r;
334 1 ENTER; SAVETMPS;
335 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
336 1 count = call_sv(inner, G_SCALAR);
337 1 SPAGAIN;
338 1 ▶ r = count > 0 ? SvREFCNT_inc(POPs) : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
r = count > 0 ? SvREFCNT_inc(POPs) : newSV(0); 1 0 1
339 1 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 1 0 1
340 1 ST(0) = sv_2mortal(r);
341 }
342 1 XSRETURN(1);
343 }
344  
345 XS_INTERNAL(phd_stream_cb);
346 1 XS_INTERNAL(phd_stream_cb) {
347 1 dXSARGS;
348 1 AV *cap = punk_clos_cap(aTHX_ cv);
349 SV *app, *pairs_sv, *wrapped;
350 AV *rcap;
351 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
352 1 app = *av_fetch(cap, 0, 0);
353 1 pairs_sv = *av_fetch(cap, 1, 0);
354  
355 1 rcap = newAV();
356 1 av_push(rcap, newSVsv(ST(0))); /* the server's responder */
357 1 av_push(rcap, newSVsv(pairs_sv));
358 1 wrapped = sv_2mortal(punk_closure(aTHX_ phd_responder_cb, rcap));
359  
360 {
361 1 dSP;
362 1 ENTER; SAVETMPS;
363 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
364 1 (void)call_sv(app, G_VOID);
365 1 SPAGAIN;
366 1 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 1 0 1
367 }
368 1 XSRETURN_EMPTY;
369 }
370  
371 /* Decorate whatever pc_app_cb is about to return - the same three shapes
372 * pco_decorate handles, with the same replacements in place. */
373 219532 static void phd_decorate(pTHX_ pk_state *S, HV *env, SV **resp) {
374 SV *pairs_sv;
375 AV *pairs;
376  
377 219532 ▶ if (!(*resp && SvROK(*resp))) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!(*resp && SvROK(*resp))) return; 219532 0 219532
if (!(*resp && SvROK(*resp))) return; 0 219532 219532
378 219532 pairs_sv = phd_effective(aTHX_ S, env);
379 219532 ▶ if (!pairs_sv) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!pairs_sv) return; 215740 3792 219532
380 3792 sv_2mortal(pairs_sv);
381 3792 pairs = (AV *)SvRV(pairs_sv);
382 3792 ▶ if (av_len(pairs) < 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(pairs) < 0) return; 0 3792 3792
383  
384 3792 ▶ if (SvTYPE(SvRV(*resp)) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(*resp)) == SVt_PVAV) { 3790 2 3792
385 3790 AV *headers = pco_headers_of(aTHX_ *resp);
386 3790 ▶ if (headers) phd_add_absent(aTHX_ headers, pairs);
Branch: 1 branch
BranchTrueFalseTotal
if (headers) phd_add_absent(aTHX_ headers, pairs); 3790 0 3790
387 3790 return;
388 }
389  
390 2 ▶ if (SvTYPE(SvRV(*resp)) == SVt_PVCV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(*resp)) == SVt_PVCV) { 1 1 2
391 1 AV *cap = newAV();
392 1 av_push(cap, newSVsv(*resp));
393 1 av_push(cap, newSVsv(pairs_sv));
394 1 SvREFCNT_dec(*resp);
395 1 *resp = punk_closure(aTHX_ phd_stream_cb, cap);
396 1 return;
397 }
398  
399 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
400 1 dSP; int count; SV *chained = NULL;
401 1 ENTER; SAVETMPS;
402 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
403 1 PUSHs(*resp);
404 1 PUSHs(pairs_sv);
405 1 PUTBACK;
406 1 count = call_pv("Punk::Headers::_chain", G_SCALAR | G_EVAL);
407 1 SPAGAIN;
408 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
409 0 ▶ else if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) (void)POPs; 0 0 0
410 1 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 1 0 1
411 1 ▶ if (chained) { SvREFCNT_dec(*resp); *resp = chained; }
Branch: 1 branch
BranchTrueFalseTotal
if (chained) { SvREFCNT_dec(*resp); *resp = chained; } 1 0 1
412 1 return;
413 }
414 }
415  
416 #endif /* PUNK_HEADERS_H */
« xs/idempotency.xs ↑ Summary include/punk/punk_lang.h »