xs/oamount.xs

filter: Statementstmt 86.2% 208 / 240 (3 uncoverable) Branchbran 60.6% 157 / 254 (5 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 73.0% 365 / 494 (8 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/session.xs ↑ Summary xs/password.xs »
1 MODULE = Punk PACKAGE = Punk::Mount::OpenAPI
2  
3 PROTOTYPES: DISABLE
4  
5 # The `api` mount, in C (punk_oamount.h). Everything resolves at boot into one
6 # record per operation - controller, guard chain - and the request path is
7 # punk_dispatch.h. Punk::Mount::OpenAPI.pm is the documentation.
8  
9 SV *
10 new(class, ...)
11 SV *class
12 CODE:
13 {
14 static const char *const KNOWN[] = {
15 "controller_ns", "handlers", "under", "security",
16 "max_body_size", "on_error", "stub"
17 };
18 34 HV *h = newHV();
19 34 SV *spec = NULL, *opts = NULL, *prefix = NULL, *guards = NULL;
20 HV *oh;
21 int i;
22 170 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 136 34 170
23 136 STRLEN kl; const char *k = SvPV_const(ST(i), kl);
24 136 ▶ if (kl == 4 && memEQ(k, "spec", 4)) spec = ST(i + 1);
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 4 && memEQ(k, "spec", 4)) spec = ST(i + 1); 68 68 136
if (kl == 4 && memEQ(k, "spec", 4)) spec = ST(i + 1); 34 34 68
25 102 ▶ else if (kl == 4 && memEQ(k, "opts", 4)) opts = ST(i + 1); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
else if (kl == 4 && memEQ(k, "opts", 4)) opts = ST(i + 1); /* # uncoverable condition right count:2 class:ignore_covered_err */ 34 68 102
else if (kl == 4 && memEQ(k, "opts", 4)) opts = ST(i + 1); /* # uncoverable condition right count:2 class:ignore_covered_err */ 34 0 34
26 68 ▶ else if (kl == 6 && memEQ(k, "prefix", 6)) prefix = ST(i + 1); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
else if (kl == 6 && memEQ(k, "prefix", 6)) prefix = ST(i + 1); /* # uncoverable condition right count:1 class:ignore_covered_err */ 68 0 68
else if (kl == 6 && memEQ(k, "prefix", 6)) prefix = ST(i + 1); /* # uncoverable condition right count:1 class:ignore_covered_err */ 34 34 68
27 34 ▶ # uncoverable branch false
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch false 34 0 34
# uncoverable branch false 34 0 34
28 # uncoverable condition right count:1 class:ignore_covered_err
29 34 # uncoverable condition right count:2 class:ignore_covered_err
30 else if (kl == 6 && memEQ(k, "guards", 6)) guards = ST(i + 1);
31 }
32 oh = poa_hash_of(aTHX_ opts);
33 34 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
34 0 34
34 34 /* unknown options are a typo in the app, so they are named and fatal
35 * at boot rather than quietly ignored */
36 # uncoverable branch false
37 34 # uncoverable condition right count:1 class:ignore_covered_err
38 73 ▶ if (oh) {
Branch: 1 branch
BranchTrueFalseTotal
if (oh) { 39 34 73
39 39 ▶ AV *bad = (AV *)sv_2mortal((SV *)newAV());
Branch: 1 branch
BranchTrueFalseTotal
AV *bad = (AV *)sv_2mortal((SV *)newAV()); 0 39 39
40 39 HE *he;
41 137 ▶ SSize_t bn;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t bn; 136 1 137
42 136 ▶ hv_iterinit(oh);
Branch: 1 branch
BranchTrueFalseTotal
hv_iterinit(oh); 38 98 136
43 39 ▶ while ((he = hv_iternext(oh))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(oh))) { 1 38 39
44 STRLEN kl; const char *k = HePV(he, kl);
45 34 int ok = 0, j;
46 34 ▶ for (j = 0; j < 7; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < 7; j++) 1 33 34
47 1 if (strEQ(k, KNOWN[j])) { ok = 1; break; }
48 if (!ok) av_push(bad, newSVpvn(k, kl));
49 1 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 0 1 1
50 2 ▶ bn = av_len(bad) + 1;
Branch: 1 branch
BranchTrueFalseTotal
bn = av_len(bad) + 1; 1 1 2
51 1 ▶ if (bn) {
Branch: 1 branch
BranchTrueFalseTotal
if (bn) { 0 1 1
52 1 SV *msg = sv_2mortal(newSVpvs(""));
53 SSize_t j;
54 1 # uncoverable branch true
55 1 # uncoverable condition left count:1 class:ignore_covered_err
56 if (bn > 1) sortsv(AvARRAY(bad), (STRLEN)bn, Perl_sv_cmp);
57 for (j = 0; j < bn; j++) {
58 # uncoverable branch true
59 33 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 33 0 33
60 33 ▶ if (j) sv_catpvs(msg, ", ");
Branch: 1 branch
BranchTrueFalseTotal
if (j) sv_catpvs(msg, ", "); 33 0 33
61 sv_catsv(msg, *av_fetch(bad, j, 0));
62 33 ▶ }
Branch: 2 branches
BranchTrueFalseTotal
} 33 0 33
} 33 0 33
63 SvREFCNT_dec((SV *)h);
64 33 ▶ croak("Punk: unknown api mount option(s) %s", SvPV_nolen(msg));
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk: unknown api mount option(s) %s", SvPV_nolen(msg)); 33 0 33
65 }
66 }
67 33  
68 (void)hv_stores(h, "spec", spec ? newSVsv(spec) : newSV(0)); /* # uncoverable condition right count:1 class:ignore_covered_err */
69 (void)hv_stores(h, "opts",
70 oh ? newSVsv(opts) : newRV_noinc((SV *)newHV()));
71 (void)hv_stores(h, "prefix",
72 (prefix && SvOK(prefix)) ? newSVsv(prefix) : newSVpvs(""));
73 (void)hv_stores(h, "guards",
74 poa_av_of(aTHX_ guards) ? newSVsv(guards)
75 : newRV_noinc((SV *)newAV()));
76 RETVAL = sv_bless(newRV_noinc((SV *)h), gv_stashsv(class, GV_ADD));
77 }
78 OUTPUT:
79 RETVAL
80  
81 SV *
82 prefix(self)
83 SV *self
84 ALIAS:
85 api = 1
86 ops = 2
87 max_body_size = 3
88 on_error = 4
89 CODE:
90 {
91 static const char *const SLOT[] =
92 { "prefix", "api", "ops", "max_body_size", "on_error" };
93 184 SV *v = poa_get(aTHX_ poa_hv(aTHX_ self), SLOT[ix]);
94 184 ▶ RETVAL = v ? newSVsv(v) : newSV(0); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = v ? newSVsv(v) : newSV(0); /* # uncoverable condition right count:1 class:ignore_covered_err */ 184 0 184
95 }
96 OUTPUT:
97 RETVAL
98  
99 # ---- boot -------------------------------------------------------------------
100  
101 # compile($app, $resolve): resolve every operationId to a controller method and
102 # freeze its guard chain (scope guards, then the spec's security requirement,
103 # then any per-spec-prefix guards). Chains, returning the mount.
104 SV *
105 compile(self, app, resolve)
106 SV *self
107 SV *app
108 SV *resolve
109 CODE:
110 {
111 33 HV *h = poa_hv(aTHX_ self);
112 33 HV *opts = poa_hash_of(aTHX_ poa_get(aTHX_ h, "opts"));
113 SV *api, *ops_list, *spec_sv;
114 AV *ops_av;
115 HV *spec, *schemes, *checkers, *handlers, *under;
116 HV *resolved, *security, *prefix_guards, *ops;
117 AV *scope_guards, *classes;
118 SV *ns;
119 SSize_t i, n;
120  
121 /* Open::API->new(spec => $self->{spec}) */
122 {
123 SV *argv[2];
124 33 argv[0] = sv_2mortal(newSVpvs("spec"));
125 33 argv[1] = poa_get(aTHX_ h, "spec");
126 33 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 33 33
127 33 # uncoverable condition left count:1 class:ignore_covered_err
128 33 if (!argv[1]) argv[1] = &PL_sv_undef;
129 (void)pk_require_once(aTHX_ "Open::API", TRUE);
130 33 ▶ api = pcx_call_meth(aTHX_ sv_2mortal(newSVpvs("Open::API")), "new",
Branch: 1 branch
BranchTrueFalseTotal
api = pcx_call_meth(aTHX_ sv_2mortal(newSVpvs("Open::API")), "new", 0 33 33
131 33 argv, 2, 1);
132 # uncoverable branch true
133 # uncoverable condition left count:1 class:ignore_covered_err
134 if (!api) croak("Punk: Open::API->new returned nothing");
135 33 (void)hv_stores(h, "api", api); /* the hash owns it */
136 33 }
137 33  
138 33 /* the two things the ABI has no C form for: one call each, at boot */
139 33 ▶ ops_list = pcx_call_meth(aTHX_ api, "operations", NULL, 0, 1);
Branch: 1 branch
BranchTrueFalseTotal
ops_list = pcx_call_meth(aTHX_ api, "operations", NULL, 0, 1); 33 0 33
140 33 ▶ spec_sv = pcx_call_meth(aTHX_ api, "spec", NULL, 0, 1);
Branch: 1 branch
BranchTrueFalseTotal
spec_sv = pcx_call_meth(aTHX_ api, "spec", NULL, 0, 1); 33 0 33
141 33 ▶ ops_av = poa_av_of(aTHX_ ops_list);
Branch: 1 branch
BranchTrueFalseTotal
ops_av = poa_av_of(aTHX_ ops_list); 0 33 33
142 spec = poa_hash_of(aTHX_ spec_sv);
143 # uncoverable branch false
144 33 # uncoverable condition right count:1 class:ignore_covered_err
145 if (ops_list) sv_2mortal(ops_list);
146 33 # uncoverable branch false
147 33 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 33 0 33
148 34 ▶ if (spec_sv) sv_2mortal(spec_sv);
Branch: 1 branch
BranchTrueFalseTotal
if (spec_sv) sv_2mortal(spec_sv); 1 33 34
149 1 # uncoverable branch true
150 # uncoverable condition left count:1 class:ignore_covered_err
151 if (!ops_av) ops_av = (AV *)sv_2mortal((SV *)newAV());
152  
153 /* the mount scope's own guards */
154 scope_guards = (AV *)sv_2mortal((SV *)newAV());
155 {
156 AV *g = poa_av_of(aTHX_ poa_get(aTHX_ h, "guards"));
157 SSize_t gn = g ? av_len(g) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
158 33 for (i = 0; i < gn; i++)
159 33 ▶ av_push(scope_guards,
Branch: 1 branch
BranchTrueFalseTotal
av_push(scope_guards, 33 0 33
160 33 ▶ poa_resolve(aTHX_ resolve, *av_fetch(g, i, 0),
Branch: 1 branch
BranchTrueFalseTotal
poa_resolve(aTHX_ resolve, *av_fetch(g, i, 0), 14 19 33
161 "api mount guard"));
162 33 }
163 33 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
33 0 33
164 33 /* ---- security requirements -> one generated guard per operation ----
165 33 ▶ * Alternatives are OR, schemes within one are AND. Coverage is checked
Branch: 1 branch
BranchTrueFalseTotal
* Alternatives are OR, schemes within one are AND. Coverage is checked 4 29 33
166 * here: a required scheme with no checker croaks at boot rather than
167 4 * leaving an open door. */
168 15 ▶ schemes = spec ? poa_hash_of(aTHX_ poa_get(aTHX_ spec, "components"))
Branch: 1 branch
BranchTrueFalseTotal
schemes = spec ? poa_hash_of(aTHX_ poa_get(aTHX_ spec, "components")) 11 4 15
169 11 ▶ : NULL;
Branch: 2 branches
BranchTrueFalseTotal
: NULL; 11 0 11
: NULL; 0 11 11
170 11 if (schemes) schemes = poa_hash_of(aTHX_
171 11 poa_get(aTHX_ schemes, "securitySchemes"));
172 11 checkers = opts ? poa_hash_of(aTHX_ poa_get(aTHX_ opts, "security"))
173 : NULL;
174 resolved = (HV *)sv_2mortal((SV *)newHV());
175 if (checkers) {
176 HE *he;
177 33 hv_iterinit(checkers);
178 33 while ((he = hv_iternext(checkers))) {
179 109 ▶ SV *k = HeSVKEY_force(he);
Branch: 1 branch
BranchTrueFalseTotal
SV *k = HeSVKEY_force(he); 77 32 109
180 77 SV *what = sv_2mortal(newSVpvs("security checker '"));
181 77 sv_catsv(what, k); sv_catpvs(what, "'");
182 (void)hv_store_ent(resolved, k,
183 poa_resolve(aTHX_ resolve, HeVAL(he), SvPV_nolen(what)), 0);
184 77 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 0 77 77
185 77 }
186 77  
187 77 security = (HV *)sv_2mortal((SV *)newHV());
188 n = av_len(ops_av) + 1;
189 for (i = 0; i < n; i++) {
190 77 ▶ HV *op = poa_hash_of(aTHX_ *av_fetch(ops_av, i, 0));
Branch: 3 branches
BranchTrueFalseTotal
HV *op = poa_hash_of(aTHX_ *av_fetch(ops_av, i, 0)); 77 0 77
HV *op = poa_hash_of(aTHX_ *av_fetch(ops_av, i, 0)); 77 0 77
HV *op = poa_hash_of(aTHX_ *av_fetch(ops_av, i, 0)); 77 0 77
191 77 SV *id, *path, *method, *req = NULL;
192 77 ▶ AV *req_av, *alts;
Branch: 1 branch
BranchTrueFalseTotal
AV *req_av, *alts; 77 0 77
193 77 ▶ SSize_t ai, an;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t ai, an; 77 0 77
194 77 # uncoverable branch true
195 # uncoverable condition left count:1 class:ignore_covered_err
196 77 if (!op) continue;
197 id = poa_get(aTHX_ op, "operationId");
198 77 path = poa_get(aTHX_ op, "path");
199 332 ▶ method = poa_get(aTHX_ op, "method");
Branch: 2 branches
BranchTrueFalseTotal
method = poa_get(aTHX_ op, "method"); 0 255 255
method = poa_get(aTHX_ op, "method"); 255 77 332
200  
201 77 ▶ /* the operation's own `security`, else the document's */
Branch: 1 branch
BranchTrueFalseTotal
/* the operation's own `security`, else the document's */ 77 0 77
202 77 ▶ # uncoverable branch false
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false 77 0 77
203 77 # uncoverable condition right count:1 class:ignore_covered_err
204 77 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 77 0 77
# uncoverable condition right count:2 class:ignore_covered_err 15 62 77
205 15 # uncoverable condition right count:3 class:ignore_covered_err
206 62 if (spec && path && method) {
207 HV *paths = poa_hash_of(aTHX_ poa_get(aTHX_ spec, "paths"));
208 0 HE *pe = paths ? hv_fetch_ent(paths, path, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
209 HV *item = pe ? poa_hash_of(aTHX_ HeVAL(pe)) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
210 77 SV *lc = sv_2mortal(newSVsv(method));
211 77 ▶ HE *me;
Branch: 2 branches
BranchTrueFalseTotal
HE *me; 15 62 77
HE *me; 2 13 15
212 sv_catpvs(lc, "");
213 13 { /* lc $op->{method} */
214 13 STRLEN ml; char *mp = SvPV_force(lc, ml); STRLEN j;
215 27 ▶ for (j = 0; j < ml; j++) mp[j] = (char)toLOWER((U8)mp[j]); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < ml; j++) mp[j] = (char)toLOWER((U8)mp[j]); /* # uncoverable condition left count:1 class:ignore_covered_err */ 15 12 27
216 15 }
217 me = item ? hv_fetch_ent(item, lc, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
218 # uncoverable branch false
219 15 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 0 15 15
220 15 if (me) {
221 HV *raw = poa_hash_of(aTHX_ HeVAL(me));
222 if (raw && hv_exists(raw, "security", 8)) /* # uncoverable condition right count:1 class:ignore_covered_err */
223 15 req = poa_get(aTHX_ raw, "security");
224 31 ▶ else req = poa_get(aTHX_ spec, "security");
Branch: 1 branch
BranchTrueFalseTotal
else req = poa_get(aTHX_ spec, "security"); 16 15 31
225 16 ▶ }
Branch: 2 branches
BranchTrueFalseTotal
} 16 0 16
} 0 16 16
226 else req = poa_get(aTHX_ spec, "security"); /* # uncoverable statement */
227 15 }
228 15 ▶ req_av = poa_av_of(aTHX_ req);
Branch: 1 branch
BranchTrueFalseTotal
req_av = poa_av_of(aTHX_ req); 1 14 15
229 15 if (!req_av || av_len(req_av) < 0) continue;
230 30 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
16 14 30
231 16 alts = newAV();
232 16 an = av_len(req_av) + 1;
233 16 ▶ for (ai = 0; ai < an; ai++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ai = 0; ai < an; ai++) { 16 0 16
234 HV *alt = poa_hash_of(aTHX_ *av_fetch(req_av, ai, 0));
235 AV *names, *and_av;
236 16 ▶ SSize_t ni, nn;
Branch: 2 branches
BranchTrueFalseTotal
SSize_t ni, nn; 15 1 16
SSize_t ni, nn; 0 15 15
237 1 # uncoverable branch true
238 1 # uncoverable condition left count:1 class:ignore_covered_err
239 2 ▶ if (!alt) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!alt) continue; 1 0 1
240 names = (AV *)sv_2mortal((SV *)newAV());
241 {
242 HE *he;
243 hv_iterinit(alt);
244 15 ▶ while ((he = hv_iternext(alt)))
Branch: 2 branches
BranchTrueFalseTotal
while ((he = hv_iternext(alt))) 15 0 15
while ((he = hv_iternext(alt))) 0 15 15
245 0 av_push(names, newSVsv(HeSVKEY_force(he)));
246 0 }
247 0 nn = av_len(names) + 1;
248 if (nn > 1) sortsv(AvARRAY(names), (STRLEN)nn, Perl_sv_cmp);
249 and_av = newAV();
250 15 for (ni = 0; ni < nn; ni++) {
251 15 SV *name = *av_fetch(names, ni, 0);
252 15 HE *ce = hv_fetch_ent(resolved, name, 0, 0);
253 15 HE *de = schemes ? hv_fetch_ent(schemes, name, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
254 15 AV *part;
255 15 ▶ HE *se;
Branch: 1 branch
BranchTrueFalseTotal
HE *se; 15 0 15
256 15 if (!(ce && SvOK(HeVAL(ce)))) { /* # uncoverable condition left count:2 class:ignore_covered_err */
257 SvREFCNT_dec((SV *)and_av);
258 14 ▶ SvREFCNT_dec((SV *)alts);
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_dec((SV *)alts); 14 0 14
259 14 croak("Punk: operation '%s' requires securityScheme "
260 0 "'%s' but no checker is registered in the "
261 "mount's security option",
262 id ? SvPV_nolen(id) : "?", SvPV_nolen(name));
263 24 ▶ }
Branch: 2 branches
BranchTrueFalseTotal
} 12 0 12
} 12 0 12
264 12 # uncoverable branch true
265 12 # uncoverable condition left count:1 class:ignore_covered_err
266 12 # uncoverable condition left count:2 class:ignore_covered_err
267 12 if (!(de && poa_hash_of(aTHX_ HeVAL(de)))) {
268 SvREFCNT_dec((SV *)and_av); /* # uncoverable statement */
269 SvREFCNT_dec((SV *)alts); /* # uncoverable statement */
270 0 croak("Punk: securityScheme '%s' is not declared in " /* # uncoverable statement */
271 "components.securitySchemes", SvPV_nolen(name)); /* # uncoverable statement */
272 }
273 se = hv_fetch_ent(alt, name, 0, 0);
274 part = newAV();
275 av_push(part, newSVsv(name));
276 32 av_push(part, newSVsv(HeVAL(de)));
277 32 ▶ av_push(part, newSVsv(HeVAL(ce)));
Branch: 1 branch
BranchTrueFalseTotal
av_push(part, newSVsv(HeVAL(ce))); 32 0 32
278 32 ▶ av_push(part, se ? newSVsv(HeVAL(se)) : newSV(0)); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
av_push(part, se ? newSVsv(HeVAL(se)) : newSV(0)); /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 30 32
279 2 av_push(and_av, newRV_noinc((SV *)part));
280 2 }
281 # uncoverable branch false
282 # uncoverable condition right count:1 class:ignore_covered_err
283 2 if (av_len(and_av) >= 0)
284 4 ▶ av_push(alts, newRV_noinc((SV *)and_av));
Branch: 1 branch
BranchTrueFalseTotal
av_push(alts, newRV_noinc((SV *)and_av)); 2 2 4
285 2 ▶ else SvREFCNT_dec((SV *)and_av); /* # uncoverable statement */
Branch: 2 branches
BranchTrueFalseTotal
else SvREFCNT_dec((SV *)and_av); /* # uncoverable statement */ 2 0 2
else SvREFCNT_dec((SV *)and_av); /* # uncoverable statement */ 0 2 2
286 2 }
287  
288 # uncoverable branch false
289 2 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 0 2 2
290 0 # uncoverable condition right count:2 class:ignore_covered_err
291 0 if (av_len(alts) >= 0 && id) {
292 0 AV *cap = newAV();
293 0 av_push(cap, newSVsv(id));
294 0 ▶ av_push(cap, newRV_noinc((SV *)alts));
Branch: 2 branches
BranchTrueFalseTotal
av_push(cap, newRV_noinc((SV *)alts)); 0 0 0
av_push(cap, newRV_noinc((SV *)alts)); 0 0 0
295 0 (void)hv_store_ent(security, id,
296 punk_closure(aTHX_ punk_oa_security_cb, cap), 0);
297 0 }
298 else SvREFCNT_dec((SV *)alts); /* # uncoverable statement */
299 0 }
300  
301 4 ▶ /* ---- per-spec-prefix guards (the mount's `under` option) ----------
Branch: 1 branch
BranchTrueFalseTotal
/* ---- per-spec-prefix guards (the mount's `under` option) ---------- 2 2 4
302 2 * Longest prefix wins, and a prefix only matches on a path-segment
303 2 * boundary so /books never picks up /booksellers. */
304 2 ▶ prefix_guards = (HV *)sv_2mortal((SV *)newHV());
Branch: 1 branch
BranchTrueFalseTotal
prefix_guards = (HV *)sv_2mortal((SV *)newHV()); 2 0 2
305 2 under = opts ? poa_hash_of(aTHX_ poa_get(aTHX_ opts, "under")) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
306 2 ▶ if (under) {
Branch: 1 branch
BranchTrueFalseTotal
if (under) { 0 2 2
307 0 AV *keys = (AV *)sv_2mortal((SV *)newAV());
308 0 ▶ AV *rules = (AV *)sv_2mortal((SV *)newAV());
Branch: 1 branch
BranchTrueFalseTotal
AV *rules = (AV *)sv_2mortal((SV *)newAV()); 0 0 0
309 0 SSize_t ki, kn;
310 HE *he;
311 hv_iterinit(under);
312 2 ▶ while ((he = hv_iternext(under)))
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(under))) 2 0 2
313 2 av_push(keys, newSVsv(HeSVKEY_force(he)));
314 kn = av_len(keys) + 1;
315 2 /* by length, descending - an insertion sort; a mount has a
316 2 * handful of prefixes, not thousands */
317 2 # uncoverable branch true
318 # uncoverable condition left count:1 class:ignore_covered_err
319 2 for (ki = 1; ki < kn; ki++) {
320 8 ▶ SV *cur = *av_fetch(keys, ki, 0); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
SV *cur = *av_fetch(keys, ki, 0); /* # uncoverable statement */ 6 2 8
321 6 SSize_t kj = ki - 1; /* # uncoverable statement */
322 6 ▶ SvREFCNT_inc(cur); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_inc(cur); /* # uncoverable statement */ 6 0 6
323 6 ▶ # uncoverable statement
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable statement 6 0 6
324 # uncoverable branch true
325 # uncoverable branch false
326 6 while (kj >= 0
327 6 ▶ # uncoverable statement
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable statement 6 0 6
# uncoverable statement 0 6 6
328 6 # uncoverable condition left count:1
329 8 ▶ # uncoverable condition right count:1
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:1 6 2 8
330 6 # uncoverable condition left count:2
331 6 # uncoverable condition right count:2
332 && SvCUR(*av_fetch(keys, kj, 0)) < SvCUR(cur)) {
333 6 (void)av_store(keys, kj + 1, /* # uncoverable statement */
334 6 ▶ SvREFCNT_inc(*av_fetch(keys, kj, 0)));
Branch: 2 branches
BranchTrueFalseTotal
SvREFCNT_inc(*av_fetch(keys, kj, 0))); 4 2 6
SvREFCNT_inc(*av_fetch(keys, kj, 0))); 0 4 4
335 4 ▶ kj--; /* # uncoverable statement */
Branch: 2 branches
BranchTrueFalseTotal
kj--; /* # uncoverable statement */ 1 3 4
kj--; /* # uncoverable statement */ 0 1 1
336 4 }
337 (void)av_store(keys, kj + 1, cur); /* # uncoverable statement */
338 4 }
339 for (ki = 0; ki < kn; ki++) {
340 SV *k = *av_fetch(keys, ki, 0);
341 HE *ue = hv_fetch_ent(under, k, 0, 0);
342 AV *list = ue ? poa_av_of(aTHX_ HeVAL(ue)) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
343 AV *gs = newAV(), *rule = newAV();
344 32 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 32 0 32
345 64 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 0 32 32
# uncoverable condition left count:1 class:ignore_covered_err 0 0 0
346 32 if (list) {
347 32 ▶ SSize_t gi, gn = av_len(list) + 1; /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
SSize_t gi, gn = av_len(list) + 1; /* # uncoverable statement */ 0 32 32
348 32 # uncoverable statement
349 # uncoverable branch true
350 0 # uncoverable branch false
351 32 # uncoverable condition left count:1
352 # uncoverable condition right count:1
353 32 for (gi = 0; gi < gn; gi++)
354 32 av_push(gs, poa_resolve(aTHX_ resolve, /* # uncoverable statement */
355 32 ▶ *av_fetch(list, gi, 0), "api under guard"));
Branch: 1 branch
BranchTrueFalseTotal
*av_fetch(list, gi, 0), "api under guard")); 32 0 32
356 32 }
357 32 # uncoverable branch false
358 106 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 75 31 106
359 75 else if (ue)
360 75 ▶ av_push(gs, poa_resolve(aTHX_ resolve, HeVAL(ue),
Branch: 1 branch
BranchTrueFalseTotal
av_push(gs, poa_resolve(aTHX_ resolve, HeVAL(ue), 75 0 75
361 "api under guard"));
362 av_push(rule, newSVsv(k));
363 av_push(rule, newRV_noinc((SV *)gs));
364 av_push(rules, newRV_noinc((SV *)rule));
365 }
366 75 ▶ n = av_len(ops_av) + 1;
Branch: 1 branch
BranchTrueFalseTotal
n = av_len(ops_av) + 1; 0 75 75
367 for (i = 0; i < n; i++) {
368 75 HV *op = poa_hash_of(aTHX_ *av_fetch(ops_av, i, 0));
369 75 ▶ SV *path = op ? poa_get(aTHX_ op, "path") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *path = op ? poa_get(aTHX_ op, "path") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 17 58 75
370 17 ▶ SV *id = op ? poa_get(aTHX_ op, "operationId") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *id = op ? poa_get(aTHX_ op, "operationId") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 17 0 17
371 17 ▶ STRLEN pl;
Branch: 2 branches
BranchTrueFalseTotal
STRLEN pl; 16 1 17
STRLEN pl; 0 16 16
372 1 const char *ps;
373 2 ▶ SSize_t ri, rn = av_len(rules) + 1;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t ri, rn = av_len(rules) + 1; 1 0 1
374 # uncoverable branch true
375 # uncoverable condition left count:1 class:ignore_covered_err
376 # uncoverable condition left count:2 class:ignore_covered_err
377 if (!(path && id)) continue;
378 ps = SvPV_const(path, pl);
379 for (ri = 0; ri < rn; ri++) {
380 74 AV *rule = (AV *)SvRV(*av_fetch(rules, ri, 0));
381 74 SV *pre = *av_fetch(rule, 0, 0);
382 77 ▶ STRLEN rl;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN rl; 3 74 77
383 3 const char *rs = SvPV_const(pre, rl);
384 74 if (pl < rl || memNE(ps, rs, rl)) continue; /* # uncoverable condition left count:2 class:ignore_covered_err */
385 74 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 12 62 74
386 74 # uncoverable condition left count:1 class:ignore_covered_err
387 74 ▶ # uncoverable condition left count:2 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:2 class:ignore_covered_err 4 70 74
388 4 if (pl != rl && ps[rl] != '/') continue;
389 4 ▶ (void)hv_store_ent(prefix_guards, id,
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_store_ent(prefix_guards, id, 4 0 4
390 8 ▶ newSVsv(*av_fetch(rule, 1, 0)), 0);
Branch: 1 branch
BranchTrueFalseTotal
newSVsv(*av_fetch(rule, 1, 0)), 0); 4 4 8
391 4 break; /* longest prefix wins */
392 }
393 }
394 74 }
395 74 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
16 58 74
396 74 /* ---- the operation table ------------------------------------------ */
397 74 ns = opts ? poa_get(aTHX_ opts, "controller_ns") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
398 # uncoverable branch false
399 31 # uncoverable condition right count:1 class:ignore_covered_err
400 # uncoverable condition left count:2 class:ignore_covered_err
401 # uncoverable condition right count:2 class:ignore_covered_err
402 31 ▶ if (!(ns && SvOK(ns))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(ns && SvOK(ns))) { 31 0 31
403 31 ▶ SV *cc = pcx_call_meth(aTHX_ app, "caller_class", NULL, 0, 1);
Branch: 2 branches
BranchTrueFalseTotal
SV *cc = pcx_call_meth(aTHX_ app, "caller_class", NULL, 0, 1); 3 28 31
SV *cc = pcx_call_meth(aTHX_ app, "caller_class", NULL, 0, 1); 3 0 3
404 ns = sv_2mortal(cc ? cc : newSVpvs("")); /* # uncoverable condition left count:1 class:ignore_covered_err */
405 sv_catpvs(ns, "::Controller::API");
406 }
407 31 ▶ else ns = sv_2mortal(newSVsv(ns)); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
else ns = sv_2mortal(newSVsv(ns)); /* # uncoverable statement */ 31 0 31
408 31 ▶ (void)hv_stores(h, "ns", newSVsv(ns));
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_stores(h, "ns", newSVsv(ns)); 1 30 31
(void)hv_stores(h, "ns", newSVsv(ns)); 1 0 1
409  
410 classes = poa_load_namespace(aTHX_ ns);
411 handlers = opts ? poa_hash_of(aTHX_ poa_get(aTHX_ opts, "handlers"))
412 : NULL;
413 ops = newHV();
414 31 n = av_len(ops_av) + 1;
415 for (i = 0; i < n; i++) {
416 HV *op = poa_hash_of(aTHX_ *av_fetch(ops_av, i, 0));
417 SV *id = op ? poa_get(aTHX_ op, "operationId") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
418 SV *code;
419 HV *rec;
420 AV *guards;
421 HE *he;
422 SSize_t gi, gn;
423 # uncoverable branch true
424 # uncoverable condition left count:1 class:ignore_covered_err
425 if (!id) continue;
426  
427 code = poa_find_handler(aTHX_ id, handlers, classes, resolve);
428 if (!code) {
429 SV *stub = opts ? poa_get(aTHX_ opts, "stub") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
430 if (!(stub && SvTRUE(stub))) { /* # uncoverable condition left count:2 class:ignore_covered_err */
431 SvREFCNT_dec((SV *)ops);
432 croak("Punk: operation '%s' has no controller method under "
433 "%s (define it, name a handler in the mount's "
434 "handlers option, or set stub => 1)",
435 SvPV_nolen(id), SvPV_nolen(ns));
436 }
437 }
438  
439 guards = newAV();
440 gn = av_len(scope_guards) + 1;
441 for (gi = 0; gi < gn; gi++)
442 av_push(guards, newSVsv(*av_fetch(scope_guards, gi, 0)));
443 he = hv_fetch_ent(security, id, 0, 0);
444 if (he) av_push(guards, newSVsv(HeVAL(he)));
445 he = hv_fetch_ent(prefix_guards, id, 0, 0);
446 if (he) {
447 AV *pg = poa_av_of(aTHX_ HeVAL(he));
448 SSize_t pi, pn = pg ? av_len(pg) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
449 for (pi = 0; pi < pn; pi++)
450 av_push(guards, newSVsv(*av_fetch(pg, pi, 0)));
451 }
452  
453 rec = newHV();
454 (void)hv_stores(rec, "code", code ? code : newSV(0));
455 (void)hv_stores(rec, "guards", newRV_noinc((SV *)guards));
456 (void)hv_store_ent(ops, id, newRV_noinc((SV *)rec), 0);
457 }
458 (void)hv_stores(h, "ops", newRV_noinc((SV *)ops));
459  
460 {
461 SV *m = opts ? poa_get(aTHX_ opts, "max_body_size") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
462 (void)hv_stores(h, "max_body_size",
463 (m && SvOK(m)) ? newSVsv(m) : newSViv(1048576));
464 }
465 {
466 SV *e = opts ? poa_get(aTHX_ opts, "on_error") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
467 (void)hv_stores(h, "on_error",
468 (e && SvOK(e))
469 ? poa_resolve(aTHX_ resolve, e, "api mount on_error")
470 : newSV(0));
471 }
472  
473 RETVAL = newSVsv(self);
474 }
475 OUTPUT:
476 RETVAL
477  
478 # ---- request time -----------------------------------------------------------
479  
480 # The whole per-request path runs in C (punk_dispatch.h): before-dispatch
481 # hooks, the operation's guards, the body-size and no-handler checks, raw-input
482 # assembly and validation through the ABI, stashing the typed params, and the
483 # controller call. Guards run before validation, so a 401 costs no body read.
484 # Returns ($ret, $err) for Punk::App's finish path.
485 void
486 # uncoverable statement
487 # uncoverable subroutine
488 dispatch(self, c, before, op_id, caps)
489 SV *self
490 SV *c
491 SV *before
492 SV *op_id
493 SV *caps
494 PPCODE:
495 {
496 0 HV *h = poa_hv(aTHX_ self); /* # uncoverable statement */
497 0 HV *ops = poa_hash_of(aTHX_ poa_get(aTHX_ h, "ops")); /* # uncoverable statement */
498 0 ▶ # uncoverable statement
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable statement 0 0 0
499 0 ▶ # uncoverable condition left count:1
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:1 0 0 0
500 0 # uncoverable condition right count:1
501 0 HE *he = ops ? hv_fetch_ent(ops, op_id, 0, 0) : NULL;
502 # uncoverable statement
503 0 ▶ # uncoverable condition left count:1
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition left count:1 0 0 0
# uncoverable condition left count:1 0 0 0
504 0 # uncoverable condition right count:1
505 SV *rec = he ? HeVAL(he) : &PL_sv_undef;
506 0 ▶ SV *mbs = poa_get(aTHX_ h, "max_body_size"); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
SV *mbs = poa_get(aTHX_ h, "max_body_size"); /* # uncoverable statement */ 0 0 0
507 AV *before_av = poa_av_of(aTHX_ before); /* # uncoverable statement */
508 0 SV *ret, *err;
509 0 # uncoverable statement
510 0 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 0 0
511 0 ▶ # uncoverable branch false
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false 0 0 0
512 0 ▶ # uncoverable condition left count:1
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:1 0 0 0
513 # uncoverable condition right count:1
514 # uncoverable condition left count:2
515 # uncoverable condition right count:2
516 if (!(SvROK(rec) && SvTYPE(SvRV(rec)) == SVt_PVHV))
517 croak("Punk: no compiled record for operation '%s'", /* # uncoverable statement */
518 SvPV_nolen(op_id)); /* # uncoverable statement */
519 punk_oa_dispatch(aTHX_ c, before_av, (HV *)SvRV(rec), /* # uncoverable statement */
520 poa_get(aTHX_ h, "api"), op_id, caps, /* # uncoverable statement */
521 mbs ? SvIV(mbs) : 1048576, &ret, &err); /* # uncoverable statement */
522 SP = PL_stack_base + ax - 1; /* hooks and the controller ran */ /* # uncoverable statement */
523 EXTEND(SP, 2); /* # uncoverable statement */
524 # uncoverable statement
525 # uncoverable condition left count:1
526 # uncoverable condition right count:1
527 PUSHs(ret == &PL_sv_undef ? ret : sv_2mortal(ret));
528 # uncoverable statement
529 # uncoverable condition left count:1
530 # uncoverable condition right count:1
531 PUSHs(err == &PL_sv_undef ? err : sv_2mortal(err));
532 }
« xs/session.xs ↑ Summary xs/password.xs »