include/punk/punk_cors.h

filter: Statementstmt 97.0% 287 / 303 (7 uncoverable) Branchbran 65.3% 205 / 352 (53 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.0% 492 / 655 (60 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/blob.xs ↑ Summary xs/idempotency.xs »
1 /* punk_cors.h - cross-origin resource sharing, in C.
2 *
3 * Two things happen here, both in pc_app_cb, which is the one function every
4 * request passes through before punk_serve routes it:
5 *
6 * - a preflight is answered outright, before routing. It has to be: an
7 * OPTIONS to a path with no OPTIONS route would otherwise be the router's
8 * 405, and the reply needs the method set the router computes anyway.
9 * - every other response is decorated on the way out - including the 404s
10 * and 405s that never build a context, which is why this is not an
11 * after-dispatch hook.
12 *
13 * What this is not: access control. CORS tells a *browser* what script is
14 * allowed to read. Nothing else honours it, and a credentialed cross-origin
15 * write still meets the csrf check.
16 *
17 * Include after punk_route.h (pr_allow), punk_response.h (punk_triplet) and
18 * punk_static.h (punk_closure).
19 */
20  
21 #ifndef PUNK_CORS_H
22 #define PUNK_CORS_H
23  
24 /* ---- configuration -------------------------------------------------------- */
25  
26 120 static SV *pco_get(pTHX_ HV *cfg, const char *k) {
27 120 ▶ SV **e = cfg ? hv_fetch(cfg, k, (I32)strlen(k), 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV **e = cfg ? hv_fetch(cfg, k, (I32)strlen(k), 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 120 0 120
28 /*
29 # uncoverable condition right count:2 class:ignore_covered_err
30 # uncoverable condition right count:3 class:ignore_covered_err */
31 120 ▶ return (e && *e && SvOK(*e)) ? *e : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && SvOK(*e)) ? *e : NULL; 49 71 120
return (e && *e && SvOK(*e)) ? *e : NULL; 49 0 49
return (e && *e && SvOK(*e)) ? *e : NULL; 49 0 49
32 }
33  
34 102 static SV *pco_env(pTHX_ HV *env, const char *k) {
35 102 SV **e = hv_fetch(env, k, (I32)strlen(k), 0);
36 /*
37 # uncoverable condition right count:2 class:ignore_covered_err
38 # uncoverable condition right count:3 class:ignore_covered_err
39 # uncoverable condition right count:4 class:ignore_covered_err */
40 102 ▶ return (e && *e && SvOK(*e) && SvCUR(*e)) ? *e : NULL;
Branch: 4 branches
BranchTrueFalseTotal
return (e && *e && SvOK(*e) && SvCUR(*e)) ? *e : NULL; 87 15 102
return (e && *e && SvOK(*e) && SvCUR(*e)) ? *e : NULL; 87 0 87
return (e && *e && SvOK(*e) && SvCUR(*e)) ? *e : NULL; 87 0 87
return (e && *e && SvOK(*e) && SvCUR(*e)) ? *e : NULL; 87 0 87
41 }
42  
43 /* Join an arrayref config value with ", ", or return the plain string. */
44 12 static SV *pco_list(pTHX_ SV *v) {
45 AV *av;
46 SSize_t i, n;
47 SV *out;
48 /*
49 # uncoverable branch true
50 # uncoverable condition left count:1 class:ignore_covered_err */
51 12 ▶ if (!v) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!v) return NULL; 0 12 12
52 /*
53 # uncoverable branch true
54 # uncoverable condition left count:1 class:ignore_covered_err
55 # uncoverable condition left count:2 class:ignore_covered_err */
56 12 ▶ if (!(SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV)) return newSVsv(v);
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV)) return newSVsv(v); 12 0 12
if (!(SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV)) return newSVsv(v); 0 12 12
57 12 av = (AV *)SvRV(v);
58 12 n = av_len(av) + 1;
59 /*
60 # uncoverable branch true
61 # uncoverable condition left count:1 class:ignore_covered_err */
62 12 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 0 12 12
63 12 out = newSVpvs("");
64 29 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 17 12 29
65 17 SV **e = av_fetch(av, i, 0);
66 /*
67 # uncoverable branch true
68 # uncoverable condition left count:1 class:ignore_covered_err
69 # uncoverable condition left count:2 class:ignore_covered_err
70 # uncoverable condition left count:3 class:ignore_covered_err */
71 17 ▶ if (!(e && *e && SvOK(*e))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(e && *e && SvOK(*e))) continue; 17 0 17
if (!(e && *e && SvOK(*e))) continue; 17 0 17
if (!(e && *e && SvOK(*e))) continue; 0 17 17
72 17 ▶ if (SvCUR(out)) sv_catpvs(out, ", ");
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(out)) sv_catpvs(out, ", "); 5 12 17
73 17 sv_catsv(out, *e);
74 }
75 12 return out;
76 }
77  
78 /* Is this request's path one CORS applies to? `paths` limits it to prefixes -
79 * which is how "the API side only" is expressed. */
80 35 static int pco_path_ok(pTHX_ HV *cfg, HV *env) {
81 35 SV *paths = pco_get(aTHX_ cfg, "paths");
82 SV *pi;
83 AV *av;
84 SSize_t i, n;
85 STRLEN pl;
86 const char *p;
87 35 ▶ if (!paths) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!paths) return 1; 31 4 35
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 4 ▶ if (!(SvROK(paths) && SvTYPE(SvRV(paths)) == SVt_PVAV)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(paths) && SvTYPE(SvRV(paths)) == SVt_PVAV)) return 1; 4 0 4
if (!(SvROK(paths) && SvTYPE(SvRV(paths)) == SVt_PVAV)) return 1; 0 4 4
93 4 pi = pco_env(aTHX_ env, "PATH_INFO");
94 8 ▶ 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 */ 4 0 4
95 /*
96 # uncoverable branch true
97 # uncoverable condition left count:1 class:ignore_covered_err */
98 4 ▶ if (!pi) pl = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!pi) pl = 1; 0 4 4
99 4 av = (AV *)SvRV(paths);
100 4 n = av_len(av) + 1;
101 6 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 4 2 6
102 4 SV **e = av_fetch(av, i, 0);
103 STRLEN xl;
104 const char *x;
105 /*
106 # uncoverable branch true
107 # uncoverable condition left count:1 class:ignore_covered_err
108 # uncoverable condition left count:2 class:ignore_covered_err
109 # uncoverable condition left count:3 class:ignore_covered_err */
110 4 ▶ if (!(e && *e && SvOK(*e))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(e && *e && SvOK(*e))) continue; 4 0 4
if (!(e && *e && SvOK(*e))) continue; 4 0 4
if (!(e && *e && SvOK(*e))) continue; 0 4 4
111 4 x = SvPV_const(*e, xl);
112 4 ▶ if (ps_under(p, pl, x, xl)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ps_under(p, pl, x, xl)) return 1; 2 2 4
113 }
114 2 return 0;
115 }
116  
117 /* The value for Access-Control-Allow-Origin, or NULL when this origin is not
118 * allowed. '*' passes through as '*'; a list matches exactly and echoes; a
119 * coderef decides and may return a replacement origin. Credentials force the
120 * echo, because browsers refuse '*' with them. */
121 24 static SV *pco_allow_origin(pTHX_ HV *cfg, SV *origin) {
122 24 SV *spec = pco_get(aTHX_ cfg, "origins");
123 /*
124 # uncoverable branch true
125 # uncoverable condition left count:1 class:ignore_covered_err */
126 24 ▶ if (!origin) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!origin) return NULL; 0 24 24
127 24 ▶ if (!spec) return newSVpvs("*");
Branch: 1 branch
BranchTrueFalseTotal
if (!spec) return newSVpvs("*"); 3 21 24
128  
129 21 ▶ if (SvROK(spec) && SvTYPE(SvRV(spec)) == SVt_PVCV) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(spec) && SvTYPE(SvRV(spec)) == SVt_PVCV) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 21 0 21
if (SvROK(spec) && SvTYPE(SvRV(spec)) == SVt_PVCV) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 19 21
130 2 dSP; int count; SV *r = NULL;
131 2 ENTER; SAVETMPS;
132 2 ▶ PUSHMARK(SP); EXTEND(SP, 1); PUSHs(origin); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(origin); PUTBACK; 0 2 2
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(origin); PUTBACK; 0 2 2
133 2 count = call_sv(spec, G_SCALAR | G_EVAL);
134 2 SPAGAIN;
135 /*
136 # uncoverable branch false class:ignore_covered_err
137 # uncoverable condition right count:1 class:ignore_covered_err */
138 2 ▶ if (count > 0) { SV *v = POPs; if (SvTRUE(v)) r = newSVsv(v); }
Branch: 2 branches
BranchTrueFalseTotal
if (count > 0) { SV *v = POPs; if (SvTRUE(v)) r = newSVsv(v); } 2 0 2
if (count > 0) { SV *v = POPs; if (SvTRUE(v)) r = newSVsv(v); } 1 1 2
139 2 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 2 0 2
140 /*
141 # uncoverable branch true
142 # uncoverable condition left count:1 class:ignore_covered_err
143 # uncoverable condition left count:2 class:ignore_covered_err
144 # uncoverable condition right count:2 class:ignore_covered_err
145 # uncoverable condition left count:3 class:ignore_covered_err */
146 2 ▶ if (r && !SvROK(r) && SvCUR(r) > 1) return r; /* an origin back */
Branch: 3 branches
BranchTrueFalseTotal
if (r && !SvROK(r) && SvCUR(r) > 1) return r; /* an origin back */ 1 1 2
if (r && !SvROK(r) && SvCUR(r) > 1) return r; /* an origin back */ 1 0 1
if (r && !SvROK(r) && SvCUR(r) > 1) return r; /* an origin back */ 0 1 1
147 2 ▶ if (r) { SvREFCNT_dec(r); return newSVsv(origin); } /* just true */
Branch: 1 branch
BranchTrueFalseTotal
if (r) { SvREFCNT_dec(r); return newSVsv(origin); } /* just true */ 1 1 2
148 1 return NULL;
149 }
150 /*
151 # uncoverable branch false
152 # uncoverable condition right count:1 class:ignore_covered_err
153 # uncoverable condition right count:2 class:ignore_covered_err */
154 19 ▶ if (SvROK(spec) && SvTYPE(SvRV(spec)) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(spec) && SvTYPE(SvRV(spec)) == SVt_PVAV) { 19 0 19
if (SvROK(spec) && SvTYPE(SvRV(spec)) == SVt_PVAV) { 19 0 19
155 19 AV *av = (AV *)SvRV(spec);
156 19 SSize_t i, n = av_len(av) + 1;
157 STRLEN ol;
158 19 const char *o = SvPV_const(origin, ol);
159 24 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 22 2 24
160 22 SV **e = av_fetch(av, i, 0);
161 STRLEN xl;
162 const char *x;
163 /*
164 # uncoverable branch true
165 # uncoverable condition left count:1 class:ignore_covered_err
166 # uncoverable condition left count:2 class:ignore_covered_err
167 # uncoverable condition left count:3 class:ignore_covered_err */
168 22 ▶ if (!(e && *e && SvOK(*e))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(e && *e && SvOK(*e))) continue; 22 0 22
if (!(e && *e && SvOK(*e))) continue; 22 0 22
if (!(e && *e && SvOK(*e))) continue; 0 22 22
169 22 x = SvPV_const(*e, xl);
170 /*
171 # uncoverable branch true class:ignore_covered_err
172 # uncoverable condition left count:1 class:ignore_covered_err
173 # uncoverable condition left count:2 class:ignore_covered_err
174 # uncoverable condition right count:2 class:ignore_covered_err */
175 39 ▶ if (xl == 1 && *x == '*') return newSVpvs("*");
Branch: 2 branches
BranchTrueFalseTotal
if (xl == 1 && *x == '*') return newSVpvs("*"); 0 22 22
if (xl == 1 && *x == '*') return newSVpvs("*"); 0 0 0
176 22 ▶ if (xl == ol && memEQ(x, o, ol)) return newSVsv(origin); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (xl == ol && memEQ(x, o, ol)) return newSVsv(origin); /* # uncoverable condition right count:2 class:ignore_covered_err */ 17 5 22
if (xl == ol && memEQ(x, o, ol)) return newSVsv(origin); /* # uncoverable condition right count:2 class:ignore_covered_err */ 17 0 17
177 }
178 2 return NULL;
179 }
180 { /* a plain string: '*' or one origin */
181 STRLEN sl, ol;
182 0 ▶ const char *s = SvPV_const(spec, sl); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
const char *s = SvPV_const(spec, sl); /* # uncoverable statement */ 0 0 0
183 0 const char *o = SvPV_const(origin, ol); /* # uncoverable statement */
184 /*
185 # uncoverable statement
186 # uncoverable branch true
187 # uncoverable branch false
188 # uncoverable condition left count:1
189 # uncoverable condition right count:1
190 # uncoverable condition left count:2
191 # uncoverable condition right count:2 */
192 0 ▶ if (sl == 1 && *s == '*') return newSVpvs("*");
Branch: 2 branches
BranchTrueFalseTotal
if (sl == 1 && *s == '*') return newSVpvs("*"); 0 0 0
if (sl == 1 && *s == '*') return newSVpvs("*"); 0 0 0
193 /*
194 # uncoverable statement
195 # uncoverable condition left count:1
196 # uncoverable condition right count:1
197 # uncoverable condition left count:2
198 # uncoverable condition right count:2 */
199 0 ▶ return (sl == ol && memEQ(s, o, ol)) ? newSVsv(origin) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return (sl == ol && memEQ(s, o, ol)) ? newSVsv(origin) : NULL; 0 0 0
return (sl == ol && memEQ(s, o, ol)) ? newSVsv(origin) : NULL; 0 0 0
200 }
201 }
202  
203 /* Does the answer depend on the request's origin? Everything but a flat '*'
204 * does, and then every response on a CORS path says `Vary: Origin` - the
205 * refused and the originless ones too, or a shared cache keeps one of those
206 * and serves it to an origin that was allowed. */
207 11 static int pco_varies(pTHX_ HV *cfg) {
208 11 SV *spec = pco_get(aTHX_ cfg, "origins");
209 STRLEN sl;
210 const char *s;
211 11 ▶ if (!spec) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!spec) return 0; 1 10 11
212 10 ▶ if (SvROK(spec) && SvTYPE(SvRV(spec)) == SVt_PVAV) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(spec) && SvTYPE(SvRV(spec)) == SVt_PVAV) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 10 0 10
if (SvROK(spec) && SvTYPE(SvRV(spec)) == SVt_PVAV) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 9 1 10
213 9 AV *av = (AV *)SvRV(spec);
214 9 SSize_t i, n = av_len(av) + 1;
215 /*
216 # uncoverable branch false
217 # uncoverable condition right count:1 class:ignore_covered_err */
218 9 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 9 0 9
219 9 SV **e = av_fetch(av, i, 0);
220 /*
221 # uncoverable branch true
222 # uncoverable condition left count:1 class:ignore_covered_err
223 # uncoverable condition left count:2 class:ignore_covered_err
224 # uncoverable condition left count:3 class:ignore_covered_err */
225 9 ▶ if (!(e && *e && SvOK(*e))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(e && *e && SvOK(*e))) continue; 9 0 9
if (!(e && *e && SvOK(*e))) continue; 9 0 9
if (!(e && *e && SvOK(*e))) continue; 0 9 9
226 9 s = SvPV_const(*e, sl);
227 9 ▶ return !(sl == 1 && *s == '*'); /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
return !(sl == 1 && *s == '*'); /* # uncoverable condition left count:2 class:ignore_covered_err */ 1 8 9
return !(sl == 1 && *s == '*'); /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 1 1
228 }
229 0 return 1; /* # uncoverable statement */
230 }
231 /*
232 # uncoverable branch false
233 # uncoverable condition right count:1 class:ignore_covered_err */
234 1 ▶ if (SvROK(spec)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(spec)) return 1; 1 0 1
235 0 s = SvPV_const(spec, sl); /* # uncoverable statement */
236 /*
237 # uncoverable statement
238 # uncoverable condition left count:1
239 # uncoverable condition right count:1
240 # uncoverable condition left count:2
241 # uncoverable condition right count:2 */
242 0 ▶ return !(sl == 1 && *s == '*');
Branch: 2 branches
BranchTrueFalseTotal
return !(sl == 1 && *s == '*'); 0 0 0
return !(sl == 1 && *s == '*'); 0 0 0
243 }
244  
245 19 static int pco_credentials(pTHX_ HV *cfg) {
246 19 SV *v = pco_get(aTHX_ cfg, "credentials");
247 19 ▶ return (v && SvTRUE(v)) ? 1 : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
return (v && SvTRUE(v)) ? 1 : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 9 10 19
return (v && SvTRUE(v)) ? 1 : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 9 0 9
248 }
249  
250 /* ---- the header block ------------------------------------------------------ */
251  
252 /* The pairs every CORS response carries. `Vary: Origin` goes on whenever the
253 * value depends on the request's origin - anything but a flat '*' - because a
254 * shared cache that misses it will hand one origin's response to another. */
255 19 static void pco_common(pTHX_ AV *headers, HV *cfg, SV *allow) {
256 STRLEN al;
257 19 const char *a = SvPV_const(allow, al);
258 19 av_push(headers, newSVpvs("Access-Control-Allow-Origin"));
259 19 av_push(headers, newSVsv(allow));
260 19 ▶ if (!(al == 1 && *a == '*')) { /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(al == 1 && *a == '*')) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 3 16 19
if (!(al == 1 && *a == '*')) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 3 3
261 16 av_push(headers, newSVpvs("Vary"));
262 16 av_push(headers, newSVpvs("Origin"));
263 }
264 19 ▶ if (pco_credentials(aTHX_ cfg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pco_credentials(aTHX_ cfg)) { 9 10 19
265 9 av_push(headers, newSVpvs("Access-Control-Allow-Credentials"));
266 9 av_push(headers, newSVpvs("true"));
267 }
268 19 }
269  
270 /* The methods a path answers, from every tier that routes: the web router and
271 * each api mount whose prefix covers it. Asking rather than repeating a
272 * configured list is the whole reason preflight lives here - but the router
273 * alone would miss every spec operation, which for an api-first application is
274 * all of them. Pushes uppercase method SVs into `out`. */
275 7 static void pco_methods_for(pTHX_ pk_state *S, const char *path, STRLEN plen,
276 AV *out) {
277 7 SV *router = S->router_sv;
278 7 AV *apims = S->apims;
279 7 HV *seen = (HV *)sv_2mortal((SV *)newHV());
280 SSize_t i, n;
281  
282 /*
283 # uncoverable branch false
284 # uncoverable condition right count:1 class:ignore_covered_err
285 # uncoverable condition right count:2 class:ignore_covered_err */
286 7 ▶ if (router && SvROK(router)) {
Branch: 2 branches
BranchTrueFalseTotal
if (router && SvROK(router)) { 7 0 7
if (router && SvROK(router)) { 7 0 7
287 7 AV *web = (AV *)sv_2mortal((SV *)newAV());
288 7 pr_router *rt = (pr_router *)INT2PTR(void *, SvIV(SvRV(router)));
289 /* an empty request method keeps every method in the set - we want the
290 * whole Allow list, not "everything except this request's" */
291 7 (void)pr_allow(aTHX_ rt, "", 0, path, plen, web);
292 7 n = av_len(web) + 1;
293 15 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 8 7 15
294 8 SV **e = av_fetch(web, i, 0);
295 /*
296 # uncoverable branch false
297 # uncoverable condition right count:1 class:ignore_covered_err
298 # uncoverable condition right count:2 class:ignore_covered_err
299 # uncoverable condition right count:3 class:ignore_covered_err */
300 8 ▶ if (e && *e && !hv_exists_ent(seen, *e, 0)) {
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && !hv_exists_ent(seen, *e, 0)) { 8 0 8
if (e && *e && !hv_exists_ent(seen, *e, 0)) { 8 0 8
if (e && *e && !hv_exists_ent(seen, *e, 0)) { 8 0 8
301 8 (void)hv_store_ent(seen, *e, PUNK_SET_TRUE, 0);
302 8 av_push(out, newSVsv(*e));
303 }
304 }
305 }
306  
307 /*
308 # uncoverable branch false
309 # uncoverable condition right count:1 class:ignore_covered_err */
310 7 ▶ if (apims) {
Branch: 1 branch
BranchTrueFalseTotal
if (apims) { 7 0 7
311 7 const oa_abi *A = punk_oa_try(aTHX);
312 7 n = av_len(apims) + 1;
313 9 ▶ for (i = 0; A && i < n; i++) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; A && i < n; i++) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 9 0 9
for (i = 0; A && i < n; i++) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 7 9
314 2 SV **mp = av_fetch(apims, i, 0);
315 HV *am;
316 SV **x, *api;
317 const char *pf, *rest;
318 STRLEN pfl, restl;
319 void *api_c;
320 AV *allowv;
321 SSize_t j, an;
322 /*
323 # uncoverable branch true
324 # uncoverable condition left count:1 class:ignore_covered_err
325 # uncoverable condition left count:2 class:ignore_covered_err
326 # uncoverable condition left count:3 class:ignore_covered_err */
327 2 ▶ if (!mp || !*mp || !SvROK(*mp)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!mp || !*mp || !SvROK(*mp)) continue; 2 0 2
if (!mp || !*mp || !SvROK(*mp)) continue; 2 0 2
if (!mp || !*mp || !SvROK(*mp)) continue; 0 2 2
328 2 am = (HV *)SvRV(*mp);
329 2 x = hv_fetchs(am, "prefix", 0);
330 /*
331 # uncoverable branch false
332 # uncoverable condition right count:1 class:ignore_covered_err
333 # uncoverable condition right count:2 class:ignore_covered_err
334 # uncoverable condition right count:3 class:ignore_covered_err */
335 4 ▶ if (x && *x && SvOK(*x)) pf = SvPV_const(*x, pfl);
Branch: 3 branches
BranchTrueFalseTotal
if (x && *x && SvOK(*x)) pf = SvPV_const(*x, pfl); 2 0 2
if (x && *x && SvOK(*x)) pf = SvPV_const(*x, pfl); 2 0 2
if (x && *x && SvOK(*x)) pf = SvPV_const(*x, pfl); 2 0 2
336 0 else { pf = ""; pfl = 0; } /* # uncoverable statement */
337 /*
338 # uncoverable branch true
339 # uncoverable condition left count:1 class:ignore_covered_err */
340 2 ▶ if (!ps_under(path, plen, pf, pfl)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ps_under(path, plen, pf, pfl)) continue; 0 2 2
341 2 ▶ rest = plen > pfl ? path + pfl : "/"; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
rest = plen > pfl ? path + pfl : "/"; /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 0 2
342 2 ▶ restl = plen > pfl ? plen - pfl : 1; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
restl = plen > pfl ? plen - pfl : 1; /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 0 2
343 2 x = hv_fetchs(am, "api", 0);
344 /*
345 # uncoverable branch true
346 # uncoverable condition left count:1 class:ignore_covered_err
347 # uncoverable condition left count:2 class:ignore_covered_err */
348 2 ▶ if (!(x && *x)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(x && *x)) continue; 2 0 2
if (!(x && *x)) continue; 0 2 2
349 2 api = *x;
350 2 api_c = A->api_of(aTHX_ api);
351 /*
352 # uncoverable branch true
353 # uncoverable condition left count:1 class:ignore_covered_err */
354 2 ▶ if (!api_c) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!api_c) continue; 0 2 2
355 2 allowv = (AV *)sv_2mortal((SV *)newAV());
356 /* a method no spec declares, so route() reports the 405 Allow set
357 * for the path - which is the complete list we are after */
358 2 (void)A->route(aTHX_ api_c, "PUNKCORSPROBE", 13, rest, restl,
359 NULL, allowv);
360 2 an = av_len(allowv) + 1;
361 6 ▶ for (j = 0; j < an; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < an; j++) { 4 2 6
362 4 SV **e = av_fetch(allowv, j, 0);
363 /*
364 # uncoverable branch false
365 # uncoverable condition right count:1 class:ignore_covered_err
366 # uncoverable condition right count:2 class:ignore_covered_err
367 # uncoverable condition right count:3 class:ignore_covered_err */
368 4 ▶ if (e && *e && !hv_exists_ent(seen, *e, 0)) {
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && !hv_exists_ent(seen, *e, 0)) { 4 0 4
if (e && *e && !hv_exists_ent(seen, *e, 0)) { 4 0 4
if (e && *e && !hv_exists_ent(seen, *e, 0)) { 4 0 4
369 4 (void)hv_store_ent(seen, *e, PUNK_SET_TRUE, 0);
370 4 av_push(out, newSVsv(*e));
371 }
372 }
373 }
374 }
375 7 ▶ if (av_len(out) > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(out) > 0) 5 2 7
376 5 sortsv(AvARRAY(out), (STRLEN)(av_len(out) + 1), Perl_sv_cmp);
377 7 }
378  
379 /* ---- preflight ------------------------------------------------------------- */
380  
381 /* Answered before routing, so a preflight never needs an OPTIONS route.
382 *
383 * Returns the response (+1), or NULL when this is not a preflight and the
384 * request should carry on to punk_serve. */
385 221550 static SV *pco_preflight(pTHX_ pk_state *S, HV *env) {
386 221550 HV *cfg = S->cors;
387 SV *method, *origin, *want, *allow;
388 221550 AV *methods = NULL, *headers, *resp, *body;
389 221550 SV *mlist = NULL;
390 STRLEN ml, wl;
391 const char *m, *w;
392 221550 int i, n, found = 0;
393  
394 221550 ▶ if (!cfg) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!cfg) return NULL; 221515 35 221550
395  
396 35 method = pco_env(aTHX_ env, "REQUEST_METHOD");
397 /*
398 # uncoverable branch true
399 # uncoverable condition left count:1 class:ignore_covered_err */
400 35 ▶ if (!method) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!method) return NULL; 0 35 35
401 35 m = SvPV_const(method, ml);
402 35 ▶ if (!(ml == 7 && memEQ(m, "OPTIONS", 7))) return NULL; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(ml == 7 && memEQ(m, "OPTIONS", 7))) return NULL; /* # uncoverable condition left count:2 class:ignore_covered_err */ 9 26 35
if (!(ml == 7 && memEQ(m, "OPTIONS", 7))) return NULL; /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 9 9
403  
404 9 origin = pco_env(aTHX_ env, "HTTP_ORIGIN");
405 9 want = pco_env(aTHX_ env, "HTTP_ACCESS_CONTROL_REQUEST_METHOD");
406 9 ▶ if (!origin || !want) return NULL; /* a plain OPTIONS: let it route */ /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!origin || !want) return NULL; /* a plain OPTIONS: let it route */ /* # uncoverable condition left count:1 class:ignore_covered_err */ 9 0 9
if (!origin || !want) return NULL; /* a plain OPTIONS: let it route */ /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 8 9
407 /*
408 # uncoverable branch true
409 # uncoverable condition left count:1 class:ignore_covered_err */
410 8 ▶ if (!pco_path_ok(aTHX_ cfg, env)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!pco_path_ok(aTHX_ cfg, env)) return NULL; 0 8 8
411  
412 8 allow = pco_allow_origin(aTHX_ cfg, origin);
413 8 ▶ if (!allow)
Branch: 1 branch
BranchTrueFalseTotal
if (!allow) 1 7 8
414 1 return ps_err_triplet(aTHX_ 403, PS_ERR_403, sizeof(PS_ERR_403) - 1,
415 NULL);
416 7 sv_2mortal(allow);
417  
418 /* Which methods this path really answers. Asking the router rather than
419 * repeating a configured list is the whole reason this lives here: the
420 * reply cannot promise a method the application does not serve. */
421 {
422 7 SV *over = pco_get(aTHX_ cfg, "methods");
423 /*
424 # uncoverable branch true
425 # uncoverable condition left count:1 class:ignore_covered_err */
426 7 ▶ if (over) {
Branch: 1 branch
BranchTrueFalseTotal
if (over) { 0 7 7
427 0 mlist = pco_list(aTHX_ over); /* # uncoverable statement */
428 0 w = SvPV_const(want, wl); /* # uncoverable statement */
429 /*
430 # uncoverable statement
431 # uncoverable branch true
432 # uncoverable branch false
433 # uncoverable condition left count:1
434 # uncoverable condition right count:1 */
435 0 ▶ if (mlist) {
Branch: 1 branch
BranchTrueFalseTotal
if (mlist) { 0 0 0
436 STRLEN ll;
437 0 const char *l = SvPV_const(mlist, ll); /* # uncoverable statement */
438 /* a substring hit is enough - the list is ours, not input */
439 /*
440 # uncoverable statement
441 # uncoverable condition left count:1
442 # uncoverable condition right count:1
443 # uncoverable condition left count:2
444 # uncoverable condition right count:2 */
445 0 ▶ found = (ll >= wl) && strstr(l, w) != NULL;
Branch: 2 branches
BranchTrueFalseTotal
found = (ll >= wl) && strstr(l, w) != NULL; 0 0 0
found = (ll >= wl) && strstr(l, w) != NULL; 0 0 0
446 }
447 }
448 else {
449 7 SV *pi = pco_env(aTHX_ env, "PATH_INFO");
450 STRLEN pl;
451 14 ▶ const char *p = pi ? SvPV_const(pi, pl) : "/"; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
const char *p = pi ? SvPV_const(pi, pl) : "/"; /* # uncoverable condition right count:1 class:ignore_covered_err */ 7 0 7
452 /*
453 # uncoverable branch true
454 # uncoverable condition left count:1 class:ignore_covered_err */
455 7 ▶ if (!pi) pl = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!pi) pl = 1; 0 7 7
456 7 methods = (AV *)sv_2mortal((SV *)newAV());
457 7 pco_methods_for(aTHX_ S, p, pl, methods);
458 7 w = SvPV_const(want, wl);
459 7 n = (int)(av_len(methods) + 1);
460 12 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 10 2 12
461 10 SV **e = av_fetch(methods, i, 0);
462 STRLEN xl;
463 const char *x;
464 /*
465 # uncoverable branch true
466 # uncoverable condition left count:1 class:ignore_covered_err
467 # uncoverable condition left count:2 class:ignore_covered_err
468 # uncoverable condition left count:3 class:ignore_covered_err */
469 10 ▶ if (!(e && *e && SvOK(*e))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(e && *e && SvOK(*e))) continue; 10 0 10
if (!(e && *e && SvOK(*e))) continue; 10 0 10
if (!(e && *e && SvOK(*e))) continue; 0 10 10
470 10 x = SvPV_const(*e, xl);
471 10 ▶ if (xl == wl && memEQ(x, w, wl)) { found = 1; break; } /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (xl == wl && memEQ(x, w, wl)) { found = 1; break; } /* # uncoverable condition right count:2 class:ignore_covered_err */ 5 5 10
if (xl == wl && memEQ(x, w, wl)) { found = 1; break; } /* # uncoverable condition right count:2 class:ignore_covered_err */ 5 0 5
472 }
473 7 mlist = pco_list(aTHX_ sv_2mortal(newRV_inc((SV *)methods)));
474 }
475 }
476 /*
477 # uncoverable branch false
478 # uncoverable condition right count:1 class:ignore_covered_err */
479 7 ▶ if (mlist) sv_2mortal(mlist);
Branch: 1 branch
BranchTrueFalseTotal
if (mlist) sv_2mortal(mlist); 7 0 7
480  
481 7 ▶ if (!found)
Branch: 1 branch
BranchTrueFalseTotal
if (!found) 2 5 7
482 2 return ps_err_triplet(aTHX_ 403, PS_ERR_403, sizeof(PS_ERR_403) - 1,
483 NULL);
484  
485 5 headers = newAV();
486 5 pco_common(aTHX_ headers, cfg, allow);
487 /*
488 # uncoverable branch false
489 # uncoverable condition right count:1 class:ignore_covered_err
490 # uncoverable condition right count:2 class:ignore_covered_err */
491 5 ▶ if (mlist && SvCUR(mlist)) {
Branch: 2 branches
BranchTrueFalseTotal
if (mlist && SvCUR(mlist)) { 5 0 5
if (mlist && SvCUR(mlist)) { 5 0 5
492 5 av_push(headers, newSVpvs("Access-Control-Allow-Methods"));
493 5 av_push(headers, newSVsv(mlist));
494 }
495 { /* the configured header list, or the requested one echoed back */
496 5 SV *hs = pco_get(aTHX_ cfg, "headers");
497 0 SV *val = hs ? pco_list(aTHX_ hs) /* # uncoverable statement class:ignore_covered_err */
498 5 ▶ : NULL;
Branch: 1 branch
BranchTrueFalseTotal
: NULL; 0 5 5
499 /*
500 # uncoverable branch false
501 # uncoverable condition right count:1 class:ignore_covered_err */
502 5 ▶ if (!val) {
Branch: 1 branch
BranchTrueFalseTotal
if (!val) { 5 0 5
503 5 SV *req = pco_env(aTHX_ env, "HTTP_ACCESS_CONTROL_REQUEST_HEADERS");
504 5 ▶ if (req) val = newSVsv(req);
Branch: 1 branch
BranchTrueFalseTotal
if (req) val = newSVsv(req); 1 4 5
505 }
506 5 ▶ if (val && SvCUR(val)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (val && SvCUR(val)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 4 5
if (val && SvCUR(val)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
507 1 av_push(headers, newSVpvs("Access-Control-Allow-Headers"));
508 1 av_push(headers, val);
509 }
510 /*
511 # uncoverable branch true
512 # uncoverable condition left count:1 class:ignore_covered_err */
513 4 ▶ else if (val) SvREFCNT_dec(val);
Branch: 1 branch
BranchTrueFalseTotal
else if (val) SvREFCNT_dec(val); 0 4 4
514 }
515 {
516 5 SV *ma = pco_get(aTHX_ cfg, "max_age");
517 5 av_push(headers, newSVpvs("Access-Control-Max-Age"));
518 5 ▶ av_push(headers, ma ? newSVsv(ma) : newSViv(600)); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
av_push(headers, ma ? newSVsv(ma) : newSViv(600)); /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 5 5
519 }
520 /* the preflight reply varies by what was asked, not just by origin */
521 5 av_push(headers, newSVpvs("Vary"));
522 5 av_push(headers, newSVpvs("Access-Control-Request-Method, "
523 "Access-Control-Request-Headers"));
524 5 av_push(headers, newSVpvs("Content-Length"));
525 5 av_push(headers, newSViv(0));
526  
527 5 body = newAV();
528 5 av_push(body, newSVpvs(""));
529 5 resp = newAV();
530 5 av_push(resp, newSViv(204));
531 5 av_push(resp, newRV_noinc((SV *)headers));
532 5 av_push(resp, newRV_noinc((SV *)body));
533 5 return newRV_noinc((SV *)resp);
534 }
535  
536 /* ---- decorating a real response -------------------------------------------- */
537  
538 /* Push the response headers onto an existing header AV. */
539 23 static void pco_add(pTHX_ AV *headers, HV *cfg, SV *allow) {
540 SV *expose;
541 23 ▶ if (!(allow && SvOK(allow))) { /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(allow && SvOK(allow))) { /* # uncoverable condition left count:1 class:ignore_covered_err */ 23 0 23
if (!(allow && SvOK(allow))) { /* # uncoverable condition left count:1 class:ignore_covered_err */ 9 14 23
542 /* no origin, or one that was refused: nothing is granted, and the
543 * response still has to be kept apart from one that was */
544 9 av_push(headers, newSVpvs("Vary"));
545 9 av_push(headers, newSVpvs("Origin"));
546 9 return;
547 }
548 14 pco_common(aTHX_ headers, cfg, allow);
549 14 expose = pco_get(aTHX_ cfg, "expose");
550 14 ▶ if (expose) {
Branch: 1 branch
BranchTrueFalseTotal
if (expose) { 5 9 14
551 5 SV *v = pco_list(aTHX_ expose);
552 /*
553 # uncoverable branch false
554 # uncoverable condition right count:1 class:ignore_covered_err
555 # uncoverable condition right count:2 class:ignore_covered_err */
556 5 ▶ if (v && SvCUR(v)) {
Branch: 2 branches
BranchTrueFalseTotal
if (v && SvCUR(v)) { 5 0 5
if (v && SvCUR(v)) { 5 0 5
557 5 av_push(headers, newSVpvs("Access-Control-Expose-Headers"));
558 5 av_push(headers, v);
559 }
560 /*
561 # uncoverable statement
562 # uncoverable branch true
563 # uncoverable branch false
564 # uncoverable condition left count:1
565 # uncoverable condition right count:1 */
566 0 ▶ else if (v) SvREFCNT_dec(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (v) SvREFCNT_dec(v); 0 0 0
567 }
568 }
569  
570 /* The header AV of a triplet, or NULL. */
571 3827 static AV *pco_headers_of(pTHX_ SV *resp) {
572 AV *r;
573 SV **hp;
574 /*
575 # uncoverable branch true
576 # uncoverable condition left count:1 class:ignore_covered_err
577 # uncoverable condition left count:2 class:ignore_covered_err
578 # uncoverable condition left count:3 class:ignore_covered_err */
579 3827 ▶ if (!(resp && SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV)) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!(resp && SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV)) return NULL; 3827 0 3827
if (!(resp && SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV)) return NULL; 3827 0 3827
if (!(resp && SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV)) return NULL; 0 3827 3827
580 3827 r = (AV *)SvRV(resp);
581 /*
582 # uncoverable branch true
583 # uncoverable condition left count:1 class:ignore_covered_err */
584 3827 ▶ if (av_len(r) < 1) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(r) < 1) return NULL; 0 3827 3827
585 3827 hp = av_fetch(r, 1, 0);
586 3827 ▶ return (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV)
Branch: 3 branches
BranchTrueFalseTotal
return (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) 3827 0 3827
return (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) 3827 0 3827
return (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) 3827 0 3827
587 7654 ▶ ? (AV *)SvRV(*hp) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (AV *)SvRV(*hp) : NULL; 3827 0 3827
588 }
589  
590 /* The responder wrapper for a streaming response: the server hands our
591 * closure a responder, and we hand the application one that decorates the
592 * triplet on its way past. */
593 XS_INTERNAL(pco_responder_cb);
594 2 XS_INTERNAL(pco_responder_cb) {
595 2 dXSARGS;
596 2 AV *cap = punk_clos_cap(aTHX_ cv);
597 SV *inner, *allow, *cfg_sv;
598 /*
599 # uncoverable branch true
600 # uncoverable condition left count:1 class:ignore_covered_err
601 # uncoverable condition left count:2 class:ignore_covered_err */
602 2 ▶ if (!cap || items < 1) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!cap || items < 1) XSRETURN_EMPTY; 2 0 2
if (!cap || items < 1) XSRETURN_EMPTY; 0 2 2
603 2 inner = *av_fetch(cap, 0, 0);
604 2 cfg_sv = *av_fetch(cap, 1, 0);
605 2 allow = *av_fetch(cap, 2, 0);
606 {
607 2 AV *headers = pco_headers_of(aTHX_ ST(0));
608 /*
609 # uncoverable branch false
610 # uncoverable condition right count:1 class:ignore_covered_err
611 # uncoverable condition right count:2 class:ignore_covered_err */
612 2 ▶ if (headers && SvROK(cfg_sv))
Branch: 2 branches
BranchTrueFalseTotal
if (headers && SvROK(cfg_sv)) 2 0 2
if (headers && SvROK(cfg_sv)) 2 0 2
613 2 pco_add(aTHX_ headers, (HV *)SvRV(cfg_sv), allow);
614 }
615 { /* hand it on and return whatever the real responder returned */
616 2 dSP; int count; SV *r;
617 2 ENTER; SAVETMPS;
618 2 ▶ PUSHMARK(SP); EXTEND(SP, 1); PUSHs(ST(0)); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(ST(0)); PUTBACK; 0 2 2
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(ST(0)); PUTBACK; 0 2 2
619 2 count = call_sv(inner, G_SCALAR);
620 2 SPAGAIN;
621 2 ▶ 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 */ 2 0 2
622 2 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 2 0 2
623 2 ST(0) = sv_2mortal(r);
624 }
625 2 XSRETURN(1);
626 }
627  
628 /* The streaming app wrapper: $app->($responder) becomes
629 * $app->(wrapped($responder)). */
630 XS_INTERNAL(pco_stream_cb);
631 2 XS_INTERNAL(pco_stream_cb) {
632 2 dXSARGS;
633 2 AV *cap = punk_clos_cap(aTHX_ cv);
634 SV *app, *cfg_sv, *allow, *wrapped;
635 AV *rcap;
636 /*
637 # uncoverable branch true
638 # uncoverable condition left count:1 class:ignore_covered_err
639 # uncoverable condition left count:2 class:ignore_covered_err */
640 2 ▶ if (!cap || items < 1) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!cap || items < 1) XSRETURN_EMPTY; 2 0 2
if (!cap || items < 1) XSRETURN_EMPTY; 0 2 2
641 2 app = *av_fetch(cap, 0, 0);
642 2 cfg_sv = *av_fetch(cap, 1, 0);
643 2 allow = *av_fetch(cap, 2, 0);
644  
645 2 rcap = newAV();
646 2 av_push(rcap, newSVsv(ST(0))); /* the server's responder */
647 2 av_push(rcap, newSVsv(cfg_sv));
648 2 av_push(rcap, newSVsv(allow));
649 2 wrapped = sv_2mortal(punk_closure(aTHX_ pco_responder_cb, rcap));
650  
651 {
652 2 dSP;
653 2 ENTER; SAVETMPS;
654 2 ▶ PUSHMARK(SP); EXTEND(SP, 1); PUSHs(wrapped); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(wrapped); PUTBACK; 0 2 2
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(wrapped); PUTBACK; 0 2 2
655 2 (void)call_sv(app, G_VOID);
656 2 SPAGAIN;
657 2 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 2 0 2
658 }
659 2 XSRETURN_EMPTY;
660 }
661  
662 /* Decorate whatever punk_serve returned. Three shapes reach here and all
663 * three matter: a triplet (including the 404s and 405s that never build a
664 * context), a streaming coderef, and a Future from a nonblocking handler.
665 * *resp is replaced in place for the shapes that need wrapping. */
666 221540 static void pco_decorate(pTHX_ pk_state *S, HV *env, SV **resp) {
667 221540 HV *cfg = S->cors;
668 SV *cfg_sv;
669 SV *origin, *allow;
670  
671 221540 ▶ if (!cfg) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!cfg) return; 221513 27 221540
672 /* the stream and Future shapes below capture the policy as a reference */
673 27 cfg_sv = sv_2mortal(newRV_inc((SV *)cfg));
674 27 origin = pco_env(aTHX_ env, "HTTP_ORIGIN");
675 27 ▶ if (!pco_path_ok(aTHX_ cfg, env)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!pco_path_ok(aTHX_ cfg, env)) return; 2 25 27
676 25 ▶ allow = origin ? pco_allow_origin(aTHX_ cfg, origin) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
allow = origin ? pco_allow_origin(aTHX_ cfg, origin) : NULL; 16 9 25
677 25 ▶ if (!allow) {
Branch: 1 branch
BranchTrueFalseTotal
if (!allow) { 11 14 25
678 /* not cross-origin, or refused: grant nothing, not a lie */
679 11 ▶ if (!pco_varies(aTHX_ cfg)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!pco_varies(aTHX_ cfg)) return; 2 9 11
680 9 allow = newSV(0);
681 }
682 23 sv_2mortal(allow);
683  
684 /*
685 # uncoverable branch true
686 # uncoverable condition left count:1 class:ignore_covered_err
687 # uncoverable condition left count:2 class:ignore_covered_err */
688 23 ▶ if (!(*resp && SvROK(*resp))) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!(*resp && SvROK(*resp))) return; 23 0 23
if (!(*resp && SvROK(*resp))) return; 0 23 23
689  
690 23 ▶ if (SvTYPE(SvRV(*resp)) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(*resp)) == SVt_PVAV) { 19 4 23
691 19 AV *headers = pco_headers_of(aTHX_ *resp);
692 /*
693 # uncoverable branch false
694 # uncoverable condition right count:1 class:ignore_covered_err */
695 19 ▶ if (headers) pco_add(aTHX_ headers, cfg, allow);
Branch: 1 branch
BranchTrueFalseTotal
if (headers) pco_add(aTHX_ headers, cfg, allow); 19 0 19
696 19 return;
697 }
698  
699 4 ▶ if (SvTYPE(SvRV(*resp)) == SVt_PVCV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(*resp)) == SVt_PVCV) { 2 2 4
700 2 AV *cap = newAV();
701 2 av_push(cap, newSVsv(*resp));
702 2 av_push(cap, newSVsv(cfg_sv));
703 2 av_push(cap, newSVsv(allow));
704 2 SvREFCNT_dec(*resp);
705 2 *resp = punk_closure(aTHX_ pco_stream_cb, cap);
706 2 return;
707 }
708  
709 /*
710 # uncoverable branch false
711 # uncoverable condition right count:1 class:ignore_covered_err
712 # uncoverable condition right count:2 class:ignore_covered_err */
713 2 ▶ if (SvOBJECT(SvRV(*resp)) && pcx_can(aTHX_ *resp, "then")) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvOBJECT(SvRV(*resp)) && pcx_can(aTHX_ *resp, "then")) { 2 0 2
if (SvOBJECT(SvRV(*resp)) && pcx_can(aTHX_ *resp, "then")) { 2 0 2
714 /* a Future: chain the decoration onto its result, the shape
715 * Punk::App::_finish_future already is */
716 2 dSP; int count; SV *chained = NULL;
717 2 ENTER; SAVETMPS;
718 2 ▶ PUSHMARK(SP); EXTEND(SP, 3);
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 3); 0 2 2
PUSHMARK(SP); EXTEND(SP, 3); 0 2 2
719 2 PUSHs(*resp);
720 2 PUSHs(cfg_sv);
721 2 PUSHs(allow);
722 2 PUTBACK;
723 2 count = call_pv("Punk::CORS::_chain", G_SCALAR | G_EVAL);
724 2 SPAGAIN;
725 /*
726 # uncoverable branch false
727 # uncoverable condition right count:1 class:ignore_covered_err
728 # uncoverable condition right count:2 class:ignore_covered_err */
729 2 ▶ if (count > 0 && !SvTRUE(ERRSV)) chained = SvREFCNT_inc(POPs);
Branch: 3 branches
BranchTrueFalseTotal
if (count > 0 && !SvTRUE(ERRSV)) chained = SvREFCNT_inc(POPs); 2 0 2
if (count > 0 && !SvTRUE(ERRSV)) chained = SvREFCNT_inc(POPs); 2 0 2
if (count > 0 && !SvTRUE(ERRSV)) chained = SvREFCNT_inc(POPs); 2 0 2
730 /*
731 # uncoverable statement
732 # uncoverable branch true
733 # uncoverable branch false
734 # uncoverable condition left count:1
735 # uncoverable condition right count:1 */
736 0 ▶ else if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) (void)POPs; 0 0 0
737 2 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 2 0 2
738 /*
739 # uncoverable branch false
740 # uncoverable condition right count:1 class:ignore_covered_err */
741 2 ▶ if (chained) { SvREFCNT_dec(*resp); *resp = chained; }
Branch: 1 branch
BranchTrueFalseTotal
if (chained) { SvREFCNT_dec(*resp); *resp = chained; } 2 0 2
742 2 return;
743 }
744 }
745  
746 #endif /* PUNK_CORS_H */
« xs/blob.xs ↑ Summary xs/idempotency.xs »