xs/oamount.xs

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