include/punk/punk_cors.h

filter: Statementstmt 94.7% 287 / 303 Branchbran 58.2% 205 / 352 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 75.1% 492 / 655 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/sitemap.xs ↑ Summary xs/compile.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;
Branch: 1 branch
BranchTrueFalseTotal
SV **e = cfg ? hv_fetch(cfg, k, (I32)strlen(k), 0) : NULL; 120 0 120
28 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
29 }
30  
31 102 static SV *pco_env(pTHX_ HV *env, const char *k) {
32 102 SV **e = hv_fetch(env, k, (I32)strlen(k), 0);
33 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
34 }
35  
36 /* Join an arrayref config value with ", ", or return the plain string. */
37 12 static SV *pco_list(pTHX_ SV *v) {
38 AV *av;
39 SSize_t i, n;
40 SV *out;
41 12 ▶ if (!v) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!v) return NULL; 0 12 12
42 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
43 12 av = (AV *)SvRV(v);
44 12 n = av_len(av) + 1;
45 12 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 0 12 12
46 12 out = newSVpvs("");
47 29 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 17 12 29
48 17 SV **e = av_fetch(av, i, 0);
49 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
50 17 ▶ if (SvCUR(out)) sv_catpvs(out, ", ");
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(out)) sv_catpvs(out, ", "); 5 12 17
51 17 sv_catsv(out, *e);
52 }
53 12 return out;
54 }
55  
56 /* Is this request's path one CORS applies to? `paths` limits it to prefixes -
57 * which is how "the API side only" is expressed. */
58 35 static int pco_path_ok(pTHX_ HV *cfg, HV *env) {
59 35 SV *paths = pco_get(aTHX_ cfg, "paths");
60 SV *pi;
61 AV *av;
62 SSize_t i, n;
63 STRLEN pl;
64 const char *p;
65 35 ▶ if (!paths) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!paths) return 1; 31 4 35
66 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
67 4 pi = pco_env(aTHX_ env, "PATH_INFO");
68 8 ▶ p = pi ? SvPV_const(pi, pl) : "/";
Branch: 1 branch
BranchTrueFalseTotal
p = pi ? SvPV_const(pi, pl) : "/"; 4 0 4
69 4 ▶ if (!pi) pl = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!pi) pl = 1; 0 4 4
70 4 av = (AV *)SvRV(paths);
71 4 n = av_len(av) + 1;
72 6 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 4 2 6
73 4 SV **e = av_fetch(av, i, 0);
74 STRLEN xl;
75 const char *x;
76 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
77 4 x = SvPV_const(*e, xl);
78 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
79 }
80 2 return 0;
81 }
82  
83 /* The value for Access-Control-Allow-Origin, or NULL when this origin is not
84 * allowed. '*' passes through as '*'; a list matches exactly and echoes; a
85 * coderef decides and may return a replacement origin. Credentials force the
86 * echo, because browsers refuse '*' with them. */
87 24 static SV *pco_allow_origin(pTHX_ HV *cfg, SV *origin) {
88 24 SV *spec = pco_get(aTHX_ cfg, "origins");
89 24 ▶ if (!origin) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!origin) return NULL; 0 24 24
90 24 ▶ if (!spec) return newSVpvs("*");
Branch: 1 branch
BranchTrueFalseTotal
if (!spec) return newSVpvs("*"); 3 21 24
91  
92 21 ▶ if (SvROK(spec) && SvTYPE(SvRV(spec)) == SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(spec) && SvTYPE(SvRV(spec)) == SVt_PVCV) { 21 0 21
if (SvROK(spec) && SvTYPE(SvRV(spec)) == SVt_PVCV) { 2 19 21
93 2 dSP; int count; SV *r = NULL;
94 2 ENTER; SAVETMPS;
95 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
96 2 count = call_sv(spec, G_SCALAR | G_EVAL);
97 2 SPAGAIN;
98 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
99 2 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 2 0 2
100 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
101 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
102 1 return NULL;
103 }
104 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
105 19 AV *av = (AV *)SvRV(spec);
106 19 SSize_t i, n = av_len(av) + 1;
107 STRLEN ol;
108 19 const char *o = SvPV_const(origin, ol);
109 24 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 22 2 24
110 22 SV **e = av_fetch(av, i, 0);
111 STRLEN xl;
112 const char *x;
113 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
114 22 x = SvPV_const(*e, xl);
115 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
116 22 ▶ if (xl == ol && memEQ(x, o, ol)) return newSVsv(origin);
Branch: 2 branches
BranchTrueFalseTotal
if (xl == ol && memEQ(x, o, ol)) return newSVsv(origin); 17 5 22
if (xl == ol && memEQ(x, o, ol)) return newSVsv(origin); 17 0 17
117 }
118 2 return NULL;
119 }
120 { /* a plain string: '*' or one origin */
121 STRLEN sl, ol;
122 0 ▶ const char *s = SvPV_const(spec, sl);
Branch: 1 branch
BranchTrueFalseTotal
const char *s = SvPV_const(spec, sl); 0 0 0
123 0 const char *o = SvPV_const(origin, ol);
124 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
125 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
126 }
127 }
128  
129 /* Does the answer depend on the request's origin? Everything but a flat '*'
130 * does, and then every response on a CORS path says `Vary: Origin` - the
131 * refused and the originless ones too, or a shared cache keeps one of those
132 * and serves it to an origin that was allowed. */
133 11 static int pco_varies(pTHX_ HV *cfg) {
134 11 SV *spec = pco_get(aTHX_ cfg, "origins");
135 STRLEN sl;
136 const char *s;
137 11 ▶ if (!spec) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!spec) return 0; 1 10 11
138 10 ▶ if (SvROK(spec) && SvTYPE(SvRV(spec)) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(spec) && SvTYPE(SvRV(spec)) == SVt_PVAV) { 10 0 10
if (SvROK(spec) && SvTYPE(SvRV(spec)) == SVt_PVAV) { 9 1 10
139 9 AV *av = (AV *)SvRV(spec);
140 9 SSize_t i, n = av_len(av) + 1;
141 9 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 9 0 9
142 9 SV **e = av_fetch(av, i, 0);
143 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
144 9 s = SvPV_const(*e, sl);
145 9 ▶ return !(sl == 1 && *s == '*');
Branch: 2 branches
BranchTrueFalseTotal
return !(sl == 1 && *s == '*'); 1 8 9
return !(sl == 1 && *s == '*'); 0 1 1
146 }
147 0 return 1;
148 }
149 1 ▶ if (SvROK(spec)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(spec)) return 1; 1 0 1
150 0 s = SvPV_const(spec, sl);
151 0 ▶ return !(sl == 1 && *s == '*');
Branch: 2 branches
BranchTrueFalseTotal
return !(sl == 1 && *s == '*'); 0 0 0
return !(sl == 1 && *s == '*'); 0 0 0
152 }
153  
154 19 static int pco_credentials(pTHX_ HV *cfg) {
155 19 SV *v = pco_get(aTHX_ cfg, "credentials");
156 19 ▶ return (v && SvTRUE(v)) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
return (v && SvTRUE(v)) ? 1 : 0; 9 10 19
return (v && SvTRUE(v)) ? 1 : 0; 9 0 9
157 }
158  
159 /* ---- the header block ------------------------------------------------------ */
160  
161 /* The pairs every CORS response carries. `Vary: Origin` goes on whenever the
162 * value depends on the request's origin - anything but a flat '*' - because a
163 * shared cache that misses it will hand one origin's response to another. */
164 19 static void pco_common(pTHX_ AV *headers, HV *cfg, SV *allow) {
165 STRLEN al;
166 19 const char *a = SvPV_const(allow, al);
167 19 av_push(headers, newSVpvs("Access-Control-Allow-Origin"));
168 19 av_push(headers, newSVsv(allow));
169 19 ▶ if (!(al == 1 && *a == '*')) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(al == 1 && *a == '*')) { 3 16 19
if (!(al == 1 && *a == '*')) { 0 3 3
170 16 av_push(headers, newSVpvs("Vary"));
171 16 av_push(headers, newSVpvs("Origin"));
172 }
173 19 ▶ if (pco_credentials(aTHX_ cfg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pco_credentials(aTHX_ cfg)) { 9 10 19
174 9 av_push(headers, newSVpvs("Access-Control-Allow-Credentials"));
175 9 av_push(headers, newSVpvs("true"));
176 }
177 19 }
178  
179 /* The methods a path answers, from every tier that routes: the web router and
180 * each api mount whose prefix covers it. Asking rather than repeating a
181 * configured list is the whole reason preflight lives here - but the router
182 * alone would miss every spec operation, which for an api-first application is
183 * all of them. Pushes uppercase method SVs into `out`. */
184 7 static void pco_methods_for(pTHX_ pk_state *S, const char *path, STRLEN plen,
185 AV *out) {
186 7 SV *router = S->router_sv;
187 7 AV *apims = S->apims;
188 7 HV *seen = (HV *)sv_2mortal((SV *)newHV());
189 SSize_t i, n;
190  
191 7 ▶ if (router && SvROK(router)) {
Branch: 2 branches
BranchTrueFalseTotal
if (router && SvROK(router)) { 7 0 7
if (router && SvROK(router)) { 7 0 7
192 7 AV *web = (AV *)sv_2mortal((SV *)newAV());
193 7 pr_router *rt = (pr_router *)INT2PTR(void *, SvIV(SvRV(router)));
194 /* an empty request method keeps every method in the set - we want the
195 * whole Allow list, not "everything except this request's" */
196 7 (void)pr_allow(aTHX_ rt, "", 0, path, plen, web);
197 7 n = av_len(web) + 1;
198 15 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 8 7 15
199 8 SV **e = av_fetch(web, i, 0);
200 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
201 8 (void)hv_store_ent(seen, *e, PUNK_SET_TRUE, 0);
202 8 av_push(out, newSVsv(*e));
203 }
204 }
205 }
206  
207 7 ▶ if (apims) {
Branch: 1 branch
BranchTrueFalseTotal
if (apims) { 7 0 7
208 7 const oa_abi *A = punk_oa_try(aTHX);
209 7 n = av_len(apims) + 1;
210 9 ▶ for (i = 0; A && i < n; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; A && i < n; i++) { 9 0 9
for (i = 0; A && i < n; i++) { 2 7 9
211 2 SV **mp = av_fetch(apims, i, 0);
212 HV *am;
213 SV **x, *api;
214 const char *pf, *rest;
215 STRLEN pfl, restl;
216 void *api_c;
217 AV *allowv;
218 SSize_t j, an;
219 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
220 2 am = (HV *)SvRV(*mp);
221 2 x = hv_fetchs(am, "prefix", 0);
222 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
223 0 else { pf = ""; pfl = 0; }
224 2 ▶ if (!ps_under(path, plen, pf, pfl)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ps_under(path, plen, pf, pfl)) continue; 0 2 2
225 2 ▶ rest = plen > pfl ? path + pfl : "/";
Branch: 1 branch
BranchTrueFalseTotal
rest = plen > pfl ? path + pfl : "/"; 2 0 2
226 2 ▶ restl = plen > pfl ? plen - pfl : 1;
Branch: 1 branch
BranchTrueFalseTotal
restl = plen > pfl ? plen - pfl : 1; 2 0 2
227 2 x = hv_fetchs(am, "api", 0);
228 2 ▶ if (!(x && *x)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(x && *x)) continue; 2 0 2
if (!(x && *x)) continue; 0 2 2
229 2 api = *x;
230 2 api_c = A->api_of(aTHX_ api);
231 2 ▶ if (!api_c) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!api_c) continue; 0 2 2
232 2 allowv = (AV *)sv_2mortal((SV *)newAV());
233 /* a method no spec declares, so route() reports the 405 Allow set
234 * for the path - which is the complete list we are after */
235 2 (void)A->route(aTHX_ api_c, "PUNKCORSPROBE", 13, rest, restl,
236 NULL, allowv);
237 2 an = av_len(allowv) + 1;
238 6 ▶ for (j = 0; j < an; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < an; j++) { 4 2 6
239 4 SV **e = av_fetch(allowv, j, 0);
240 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
241 4 (void)hv_store_ent(seen, *e, PUNK_SET_TRUE, 0);
242 4 av_push(out, newSVsv(*e));
243 }
244 }
245 }
246 }
247 7 ▶ if (av_len(out) > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(out) > 0) 5 2 7
248 5 sortsv(AvARRAY(out), (STRLEN)(av_len(out) + 1), Perl_sv_cmp);
249 7 }
250  
251 /* ---- preflight ------------------------------------------------------------- */
252  
253 /* Answered before routing, so a preflight never needs an OPTIONS route.
254 *
255 * Returns the response (+1), or NULL when this is not a preflight and the
256 * request should carry on to punk_serve. */
257 219533 static SV *pco_preflight(pTHX_ pk_state *S, HV *env) {
258 219533 HV *cfg = S->cors;
259 SV *method, *origin, *want, *allow;
260 219533 AV *methods = NULL, *headers, *resp, *body;
261 219533 SV *mlist = NULL;
262 STRLEN ml, wl;
263 const char *m, *w;
264 219533 int i, n, found = 0;
265  
266 219533 ▶ if (!cfg) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!cfg) return NULL; 219498 35 219533
267  
268 35 method = pco_env(aTHX_ env, "REQUEST_METHOD");
269 35 ▶ if (!method) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!method) return NULL; 0 35 35
270 35 m = SvPV_const(method, ml);
271 35 ▶ if (!(ml == 7 && memEQ(m, "OPTIONS", 7))) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!(ml == 7 && memEQ(m, "OPTIONS", 7))) return NULL; 9 26 35
if (!(ml == 7 && memEQ(m, "OPTIONS", 7))) return NULL; 0 9 9
272  
273 9 origin = pco_env(aTHX_ env, "HTTP_ORIGIN");
274 9 want = pco_env(aTHX_ env, "HTTP_ACCESS_CONTROL_REQUEST_METHOD");
275 9 ▶ if (!origin || !want) return NULL; /* a plain OPTIONS: let it route */
Branch: 2 branches
BranchTrueFalseTotal
if (!origin || !want) return NULL; /* a plain OPTIONS: let it route */ 9 0 9
if (!origin || !want) return NULL; /* a plain OPTIONS: let it route */ 1 8 9
276 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
277  
278 8 allow = pco_allow_origin(aTHX_ cfg, origin);
279 8 ▶ if (!allow)
Branch: 1 branch
BranchTrueFalseTotal
if (!allow) 1 7 8
280 1 return ps_err_triplet(aTHX_ 403, PS_ERR_403, sizeof(PS_ERR_403) - 1,
281 NULL);
282 7 sv_2mortal(allow);
283  
284 /* Which methods this path really answers. Asking the router rather than
285 * repeating a configured list is the whole reason this lives here: the
286 * reply cannot promise a method the application does not serve. */
287 {
288 7 SV *over = pco_get(aTHX_ cfg, "methods");
289 7 ▶ if (over) {
Branch: 1 branch
BranchTrueFalseTotal
if (over) { 0 7 7
290 0 mlist = pco_list(aTHX_ over);
291 0 w = SvPV_const(want, wl);
292 0 ▶ if (mlist) {
Branch: 1 branch
BranchTrueFalseTotal
if (mlist) { 0 0 0
293 STRLEN ll;
294 0 const char *l = SvPV_const(mlist, ll);
295 /* a substring hit is enough - the list is ours, not input */
296 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
297 }
298 }
299 else {
300 7 SV *pi = pco_env(aTHX_ env, "PATH_INFO");
301 STRLEN pl;
302 14 ▶ const char *p = pi ? SvPV_const(pi, pl) : "/";
Branch: 1 branch
BranchTrueFalseTotal
const char *p = pi ? SvPV_const(pi, pl) : "/"; 7 0 7
303 7 ▶ if (!pi) pl = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!pi) pl = 1; 0 7 7
304 7 methods = (AV *)sv_2mortal((SV *)newAV());
305 7 pco_methods_for(aTHX_ S, p, pl, methods);
306 7 w = SvPV_const(want, wl);
307 7 n = (int)(av_len(methods) + 1);
308 12 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 10 2 12
309 10 SV **e = av_fetch(methods, i, 0);
310 STRLEN xl;
311 const char *x;
312 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
313 10 x = SvPV_const(*e, xl);
314 10 ▶ if (xl == wl && memEQ(x, w, wl)) { found = 1; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (xl == wl && memEQ(x, w, wl)) { found = 1; break; } 5 5 10
if (xl == wl && memEQ(x, w, wl)) { found = 1; break; } 5 0 5
315 }
316 7 mlist = pco_list(aTHX_ sv_2mortal(newRV_inc((SV *)methods)));
317 }
318 }
319 7 ▶ if (mlist) sv_2mortal(mlist);
Branch: 1 branch
BranchTrueFalseTotal
if (mlist) sv_2mortal(mlist); 7 0 7
320  
321 7 ▶ if (!found)
Branch: 1 branch
BranchTrueFalseTotal
if (!found) 2 5 7
322 2 return ps_err_triplet(aTHX_ 403, PS_ERR_403, sizeof(PS_ERR_403) - 1,
323 NULL);
324  
325 5 headers = newAV();
326 5 pco_common(aTHX_ headers, cfg, allow);
327 5 ▶ if (mlist && SvCUR(mlist)) {
Branch: 2 branches
BranchTrueFalseTotal
if (mlist && SvCUR(mlist)) { 5 0 5
if (mlist && SvCUR(mlist)) { 5 0 5
328 5 av_push(headers, newSVpvs("Access-Control-Allow-Methods"));
329 5 av_push(headers, newSVsv(mlist));
330 }
331 { /* the configured header list, or the requested one echoed back */
332 5 SV *hs = pco_get(aTHX_ cfg, "headers");
333 0 SV *val = hs ? pco_list(aTHX_ hs)
334 5 ▶ : NULL;
Branch: 1 branch
BranchTrueFalseTotal
: NULL; 0 5 5
335 5 ▶ if (!val) {
Branch: 1 branch
BranchTrueFalseTotal
if (!val) { 5 0 5
336 5 SV *req = pco_env(aTHX_ env, "HTTP_ACCESS_CONTROL_REQUEST_HEADERS");
337 5 ▶ if (req) val = newSVsv(req);
Branch: 1 branch
BranchTrueFalseTotal
if (req) val = newSVsv(req); 1 4 5
338 }
339 5 ▶ if (val && SvCUR(val)) {
Branch: 2 branches
BranchTrueFalseTotal
if (val && SvCUR(val)) { 1 4 5
if (val && SvCUR(val)) { 1 0 1
340 1 av_push(headers, newSVpvs("Access-Control-Allow-Headers"));
341 1 av_push(headers, val);
342 }
343 4 ▶ else if (val) SvREFCNT_dec(val);
Branch: 1 branch
BranchTrueFalseTotal
else if (val) SvREFCNT_dec(val); 0 4 4
344 }
345 {
346 5 SV *ma = pco_get(aTHX_ cfg, "max_age");
347 5 av_push(headers, newSVpvs("Access-Control-Max-Age"));
348 5 ▶ av_push(headers, ma ? newSVsv(ma) : newSViv(600));
Branch: 1 branch
BranchTrueFalseTotal
av_push(headers, ma ? newSVsv(ma) : newSViv(600)); 0 5 5
349 }
350 /* the preflight reply varies by what was asked, not just by origin */
351 5 av_push(headers, newSVpvs("Vary"));
352 5 av_push(headers, newSVpvs("Access-Control-Request-Method, "
353 "Access-Control-Request-Headers"));
354 5 av_push(headers, newSVpvs("Content-Length"));
355 5 av_push(headers, newSViv(0));
356  
357 5 body = newAV();
358 5 av_push(body, newSVpvs(""));
359 5 resp = newAV();
360 5 av_push(resp, newSViv(204));
361 5 av_push(resp, newRV_noinc((SV *)headers));
362 5 av_push(resp, newRV_noinc((SV *)body));
363 5 return newRV_noinc((SV *)resp);
364 }
365  
366 /* ---- decorating a real response -------------------------------------------- */
367  
368 /* Push the response headers onto an existing header AV. */
369 23 static void pco_add(pTHX_ AV *headers, HV *cfg, SV *allow) {
370 SV *expose;
371 23 ▶ if (!(allow && SvOK(allow))) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(allow && SvOK(allow))) { 23 0 23
if (!(allow && SvOK(allow))) { 9 14 23
372 /* no origin, or one that was refused: nothing is granted, and the
373 * response still has to be kept apart from one that was */
374 9 av_push(headers, newSVpvs("Vary"));
375 9 av_push(headers, newSVpvs("Origin"));
376 9 return;
377 }
378 14 pco_common(aTHX_ headers, cfg, allow);
379 14 expose = pco_get(aTHX_ cfg, "expose");
380 14 ▶ if (expose) {
Branch: 1 branch
BranchTrueFalseTotal
if (expose) { 5 9 14
381 5 SV *v = pco_list(aTHX_ expose);
382 5 ▶ if (v && SvCUR(v)) {
Branch: 2 branches
BranchTrueFalseTotal
if (v && SvCUR(v)) { 5 0 5
if (v && SvCUR(v)) { 5 0 5
383 5 av_push(headers, newSVpvs("Access-Control-Expose-Headers"));
384 5 av_push(headers, v);
385 }
386 0 ▶ else if (v) SvREFCNT_dec(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (v) SvREFCNT_dec(v); 0 0 0
387 }
388 }
389  
390 /* The header AV of a triplet, or NULL. */
391 3815 static AV *pco_headers_of(pTHX_ SV *resp) {
392 AV *r;
393 SV **hp;
394 3815 ▶ 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; 3815 0 3815
if (!(resp && SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV)) return NULL; 3815 0 3815
if (!(resp && SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV)) return NULL; 0 3815 3815
395 3815 r = (AV *)SvRV(resp);
396 3815 ▶ if (av_len(r) < 1) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(r) < 1) return NULL; 0 3815 3815
397 3815 hp = av_fetch(r, 1, 0);
398 3815 ▶ return (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV)
Branch: 3 branches
BranchTrueFalseTotal
return (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) 3815 0 3815
return (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) 3815 0 3815
return (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) 3815 0 3815
399 7630 ▶ ? (AV *)SvRV(*hp) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (AV *)SvRV(*hp) : NULL; 3815 0 3815
400 }
401  
402 /* The responder wrapper for a streaming response: the server hands our
403 * closure a responder, and we hand the application one that decorates the
404 * triplet on its way past. */
405 XS_INTERNAL(pco_responder_cb);
406 2 XS_INTERNAL(pco_responder_cb) {
407 2 dXSARGS;
408 2 AV *cap = punk_clos_cap(aTHX_ cv);
409 SV *inner, *allow, *cfg_sv;
410 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
411 2 inner = *av_fetch(cap, 0, 0);
412 2 cfg_sv = *av_fetch(cap, 1, 0);
413 2 allow = *av_fetch(cap, 2, 0);
414 {
415 2 AV *headers = pco_headers_of(aTHX_ ST(0));
416 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
417 2 pco_add(aTHX_ headers, (HV *)SvRV(cfg_sv), allow);
418 }
419 { /* hand it on and return whatever the real responder returned */
420 2 dSP; int count; SV *r;
421 2 ENTER; SAVETMPS;
422 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
423 2 count = call_sv(inner, G_SCALAR);
424 2 SPAGAIN;
425 2 ▶ r = count > 0 ? SvREFCNT_inc(POPs) : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
r = count > 0 ? SvREFCNT_inc(POPs) : newSV(0); 2 0 2
426 2 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 2 0 2
427 2 ST(0) = sv_2mortal(r);
428 }
429 2 XSRETURN(1);
430 }
431  
432 /* The streaming app wrapper: $app->($responder) becomes
433 * $app->(wrapped($responder)). */
434 XS_INTERNAL(pco_stream_cb);
435 2 XS_INTERNAL(pco_stream_cb) {
436 2 dXSARGS;
437 2 AV *cap = punk_clos_cap(aTHX_ cv);
438 SV *app, *cfg_sv, *allow, *wrapped;
439 AV *rcap;
440 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
441 2 app = *av_fetch(cap, 0, 0);
442 2 cfg_sv = *av_fetch(cap, 1, 0);
443 2 allow = *av_fetch(cap, 2, 0);
444  
445 2 rcap = newAV();
446 2 av_push(rcap, newSVsv(ST(0))); /* the server's responder */
447 2 av_push(rcap, newSVsv(cfg_sv));
448 2 av_push(rcap, newSVsv(allow));
449 2 wrapped = sv_2mortal(punk_closure(aTHX_ pco_responder_cb, rcap));
450  
451 {
452 2 dSP;
453 2 ENTER; SAVETMPS;
454 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
455 2 (void)call_sv(app, G_VOID);
456 2 SPAGAIN;
457 2 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 2 0 2
458 }
459 2 XSRETURN_EMPTY;
460 }
461  
462 /* Decorate whatever punk_serve returned. Three shapes reach here and all
463 * three matter: a triplet (including the 404s and 405s that never build a
464 * context), a streaming coderef, and a Future from a nonblocking handler.
465 * *resp is replaced in place for the shapes that need wrapping. */
466 219524 static void pco_decorate(pTHX_ pk_state *S, HV *env, SV **resp) {
467 219524 HV *cfg = S->cors;
468 SV *cfg_sv;
469 SV *origin, *allow;
470  
471 219524 ▶ if (!cfg) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!cfg) return; 219497 27 219524
472 /* the stream and Future shapes below capture the policy as a reference */
473 27 cfg_sv = sv_2mortal(newRV_inc((SV *)cfg));
474 27 origin = pco_env(aTHX_ env, "HTTP_ORIGIN");
475 27 ▶ if (!pco_path_ok(aTHX_ cfg, env)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!pco_path_ok(aTHX_ cfg, env)) return; 2 25 27
476 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
477 25 ▶ if (!allow) {
Branch: 1 branch
BranchTrueFalseTotal
if (!allow) { 11 14 25
478 /* not cross-origin, or refused: grant nothing, not a lie */
479 11 ▶ if (!pco_varies(aTHX_ cfg)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!pco_varies(aTHX_ cfg)) return; 2 9 11
480 9 allow = newSV(0);
481 }
482 23 sv_2mortal(allow);
483  
484 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
485  
486 23 ▶ if (SvTYPE(SvRV(*resp)) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(*resp)) == SVt_PVAV) { 19 4 23
487 19 AV *headers = pco_headers_of(aTHX_ *resp);
488 19 ▶ if (headers) pco_add(aTHX_ headers, cfg, allow);
Branch: 1 branch
BranchTrueFalseTotal
if (headers) pco_add(aTHX_ headers, cfg, allow); 19 0 19
489 19 return;
490 }
491  
492 4 ▶ if (SvTYPE(SvRV(*resp)) == SVt_PVCV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(*resp)) == SVt_PVCV) { 2 2 4
493 2 AV *cap = newAV();
494 2 av_push(cap, newSVsv(*resp));
495 2 av_push(cap, newSVsv(cfg_sv));
496 2 av_push(cap, newSVsv(allow));
497 2 SvREFCNT_dec(*resp);
498 2 *resp = punk_closure(aTHX_ pco_stream_cb, cap);
499 2 return;
500 }
501  
502 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
503 /* a Future: chain the decoration onto its result, the shape
504 * Punk::App::_finish_future already is */
505 2 dSP; int count; SV *chained = NULL;
506 2 ENTER; SAVETMPS;
507 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
508 2 PUSHs(*resp);
509 2 PUSHs(cfg_sv);
510 2 PUSHs(allow);
511 2 PUTBACK;
512 2 count = call_pv("Punk::CORS::_chain", G_SCALAR | G_EVAL);
513 2 SPAGAIN;
514 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
515 0 ▶ else if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) (void)POPs; 0 0 0
516 2 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 2 0 2
517 2 ▶ if (chained) { SvREFCNT_dec(*resp); *resp = chained; }
Branch: 1 branch
BranchTrueFalseTotal
if (chained) { SvREFCNT_dec(*resp); *resp = chained; } 2 0 2
518 2 return;
519 }
520 }
521  
522 #endif /* PUNK_CORS_H */
« xs/sitemap.xs ↑ Summary xs/compile.xs »