include/punk/punk_validate.h

filter: Statementstmt 95.2% 301 / 316 Branchbran 59.0% 249 / 422 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 74.5% 550 / 738 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_wshandshake.h ↑ Summary include/punk/punk_context.h »
1 /* punk_validate.h - collecting request validation, in C.
2 *
3 * The whole tier runs on the JSON::Schema::Fast C ABI (jsf_abi.h): plain
4 * hashref schemas compile once through the table (coercion on) and cache by
5 * their canonical JSON via the File::Raw::JSON ABI - a hashref literal in a
6 * handler is a fresh ref every call, so refaddr keying would recompile
7 * forever; a prebuilt JSON::Schema::Fast::Compiled passes straight through.
8 * Per request nothing is Perl but the (optional) req->json / params calls
9 * that fetch the data.
10 *
11 * Invalid data never croaks - it collects into a Punk::Validate::Result the
12 * handler (or the generated route guard, a punk_closure appended to the
13 * record's guards AV at boot) turns into a 400 or a form re-render. Errors
14 * carry the Open::API shape: the JSON::Schema::Fast fields plus `name`,
15 * with a root-level `required` failure expanded to one error per missing
16 * property so a form can put the message beside its field.
17 *
18 * Needs (include after): punk_context.h (pcx_*), punk_session.h (ps_stash),
19 * punk_dispatch.h (pd_call), punk_static.h (punk_closure), punk_names.h,
20 * and the root's punk_frj. */
21  
22 #ifndef PUNK_VALIDATE_H
23 #define PUNK_VALIDATE_H
24  
25 #include "jsf_abi.h"
26  
27 #define PV_RESULT_CLASS "Punk::Validate::Result"
28  
29 /* ---- the JSON::Schema::Fast ABI, resolved once at runtime ----------------- */
30  
31 static const jsf_abi *PUNK_JSF_TAB = NULL;
32 static int PUNK_JSF_TAB_TRIED = 0;
33  
34 1431 static const jsf_abi *punk_jsf_try(pTHX) {
35 1431 ▶ if (!PUNK_JSF_TAB_TRIED) {
Branch: 1 branch
BranchTrueFalseTotal
if (!PUNK_JSF_TAB_TRIED) { 4 1427 1431
36 4 dSP; int count, ok; IV p = 0;
37 4 PUNK_JSF_TAB_TRIED = 1;
38 4 ok = pk_require_once(aTHX_ "JSON::Schema::Fast", FALSE);
39 4 SPAGAIN; /* the require may have reallocated the value stack */
40 4 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 4 0 4
41 4 ▶ ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; 0 4 4
42 4 count = call_pv("JSON::Schema::Fast::_abi_ptr", G_SCALAR | G_EVAL);
43 4 SPAGAIN;
44 4 ▶ if (!SvTRUE(ERRSV) && count > 0) p = POPi;
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 4 0 4
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 4 0 4
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 4 0 4
45 0 ▶ else if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) (void)POPs; 0 0 0
46 4 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 4 0 4
47 4 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 4 0 4
48 4 const jsf_abi *a = INT2PTR(const jsf_abi *, p);
49 4 ▶ if (a && a->abi_version >= JSF_ABI_VERSION)
Branch: 2 branches
BranchTrueFalseTotal
if (a && a->abi_version >= JSF_ABI_VERSION) 4 0 4
if (a && a->abi_version >= JSF_ABI_VERSION) 4 0 4
50 4 PUNK_JSF_TAB = a;
51 }
52 }
53 }
54 1431 return PUNK_JSF_TAB;
55 }
56  
57 1431 static const jsf_abi *punk_jsf(pTHX) {
58 1431 const jsf_abi *a = punk_jsf_try(aTHX);
59 1431 ▶ if (!a)
Branch: 1 branch
BranchTrueFalseTotal
if (!a) 0 1431 1431
60 0 croak("Punk: validation needs JSON::Schema::Fast with a compatible "
61 "C ABI (JSF_ABI_VERSION %d)", JSF_ABI_VERSION);
62 1431 return a;
63 }
64  
65 /* ---- small helpers -------------------------------------------------------- */
66  
67 /* $obj->$meth(@argv) under G_EVAL: NULL (and a cleared error) on a die -
68 * the data-fetching calls mirror the defensive evals the Perl tier had. */
69 2870 static SV *pv_meth_ev(pTHX_ SV *obj, const char *meth, SV **argv, int nargs) {
70 2870 dSP; SV *r = NULL; int count, i;
71 2870 ENTER; SAVETMPS;
72 2870 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 2870 2870
73 2870 ▶ EXTEND(SP, nargs + 1);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, nargs + 1); 2870 0 2870
EXTEND(SP, nargs + 1); 0 2870 2870
74 2870 PUSHs(obj);
75 3588 ▶ for (i = 0; i < nargs; i++) PUSHs(argv[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nargs; i++) PUSHs(argv[i]); 718 2870 3588
76 2870 PUTBACK;
77 2870 count = call_method(meth, G_SCALAR | G_EVAL);
78 2870 SPAGAIN;
79 2870 ▶ if (count > 0) { SV *t = POPs; if (!SvTRUE(ERRSV)) r = newSVsv(t); }
Branch: 3 branches
BranchTrueFalseTotal
if (count > 0) { SV *t = POPs; if (!SvTRUE(ERRSV)) r = newSVsv(t); } 2870 0 2870
if (count > 0) { SV *t = POPs; if (!SvTRUE(ERRSV)) r = newSVsv(t); } 2870 0 2870
if (count > 0) { SV *t = POPs; if (!SvTRUE(ERRSV)) r = newSVsv(t); } 2870 0 2870
80 2870 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 2870 0 2870
81 2870 ▶ if (SvTRUE(ERRSV)) sv_setsv(ERRSV, &PL_sv_no);
Branch: 3 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) sv_setsv(ERRSV, &PL_sv_no); 2870 0 2870
if (SvTRUE(ERRSV)) sv_setsv(ERRSV, &PL_sv_no); 0 2870 2870
if (SvTRUE(ERRSV)) sv_setsv(ERRSV, &PL_sv_no); 0 0 0
82 2870 return r;
83 }
84  
85 /* case-insensitive "does hay contain needle" */
86 713 static int pv_ci_has(const char *hay, STRLEN hl, const char *needle) {
87 713 STRLEN nl = strlen(needle), i, j;
88 713 ▶ if (nl == 0 || hl < nl) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (nl == 0 || hl < nl) return 0; 713 0 713
if (nl == 0 || hl < nl) return 0; 0 713 713
89 731 ▶ for (i = 0; i + nl <= hl; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + nl <= hl; i++) { 730 1 731
90 12135 ▶ for (j = 0; j < nl; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < nl; j++) { 11423 712 12135
91 11423 char a = hay[i + j], b = needle[j];
92 11423 ▶ if (a >= 'A' && a <= 'Z') a = (char)(a - 'A' + 'a');
Branch: 2 branches
BranchTrueFalseTotal
if (a >= 'A' && a <= 'Z') a = (char)(a - 'A' + 'a'); 10707 716 11423
if (a >= 'A' && a <= 'Z') a = (char)(a - 'A' + 'a'); 0 10707 10707
93 11423 ▶ if (b >= 'A' && b <= 'Z') b = (char)(b - 'A' + 'a');
Branch: 2 branches
BranchTrueFalseTotal
if (b >= 'A' && b <= 'Z') b = (char)(b - 'A' + 'a'); 10710 713 11423
if (b >= 'A' && b <= 'Z') b = (char)(b - 'A' + 'a'); 0 10710 10710
94 11423 ▶ if (a != b) break;
Branch: 1 branch
BranchTrueFalseTotal
if (a != b) break; 18 11405 11423
95 }
96 730 ▶ if (j == nl) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (j == nl) return 1; 712 18 730
97 }
98 1 return 0;
99 }
100  
101 /* canonical JSON of an SV (+1) - the schema cache key */
102 718 static SV *pv_canon(pTHX_ SV *data) {
103 frj_opts o;
104 718 Zero(&o, 1, frj_opts);
105 718 o.canonical = 1;
106 718 return punk_frj(aTHX)->encode(aTHX_ data, &o);
107 }
108  
109 /* ---- the schema cache ----------------------------------------------------- */
110  
111 static HV *PV_CACHE = NULL;
112  
113 /* How many compiled schemas the cache may hold. A schema written once in a
114 * route is one entry for the life of the worker, and an application has tens
115 * of those. One BUILT per request - an enum from a query, say - is a new
116 * entry per distinct shape, without end; past this many the cache starts
117 * over rather than grow with the data. */
118 #define PV_CACHE_MAX 256
119  
120 /* A compiled validator (+1): a blessed Compiled passes through, a hashref
121 * compiles once (coerce on) and caches by canonical JSON. Croaks on
122 * anything else, and on a malformed schema (from the ABI). */
123 720 static SV *pv_compile(pTHX_ SV *schema) {
124 720 ▶ if (SvROK(schema) && SvOBJECT(SvRV(schema)))
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(schema) && SvOBJECT(SvRV(schema))) 719 1 720
if (SvROK(schema) && SvOBJECT(SvRV(schema))) 1 718 719
125 1 return newSVsv(schema);
126 719 ▶ if (!(SvROK(schema) && SvTYPE(SvRV(schema)) == SVt_PVHV))
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(schema) && SvTYPE(SvRV(schema)) == SVt_PVHV)) 718 1 719
if (!(SvROK(schema) && SvTYPE(SvRV(schema)) == SVt_PVHV)) 0 718 718
127 1 croak("Punk: the validate schema must be a hashref or a "
128 "JSON::Schema::Fast::Compiled");
129 {
130 718 SV *key = sv_2mortal(pv_canon(aTHX_ schema));
131 HE *he;
132 718 ▶ if (!PV_CACHE) PV_CACHE = newHV();
Branch: 1 branch
BranchTrueFalseTotal
if (!PV_CACHE) PV_CACHE = newHV(); 4 714 718
133 718 he = hv_fetch_ent(PV_CACHE, key, 0, 0);
134 718 ▶ if (he) return newSVsv(HeVAL(he));
Branch: 1 branch
BranchTrueFalseTotal
if (he) return newSVsv(HeVAL(he)); 9 709 718
135 {
136 709 SV *compiled = punk_jsf(aTHX)->compile(aTHX_ schema,
137 &PL_sv_undef, 1, 0);
138 709 ▶ if (HvUSEDKEYS(PV_CACHE) >= PV_CACHE_MAX) hv_clear(PV_CACHE);
Branch: 2 branches
BranchTrueFalseTotal
if (HvUSEDKEYS(PV_CACHE) >= PV_CACHE_MAX) hv_clear(PV_CACHE); 0 709 709
if (HvUSEDKEYS(PV_CACHE) >= PV_CACHE_MAX) hv_clear(PV_CACHE); 2 707 709
139 709 (void)hv_store_ent(PV_CACHE, key, compiled, 0); /* cache owns */
140 709 return newSVsv(compiled);
141 }
142 }
143 }
144  
145 /* ---- errors: the Open::API shape plus name -------------------------------- */
146  
147 /* instanceLocation -> the field name a form wants: '/year' -> 'year',
148 * '/a/b' -> 'a.b', '~1'/'~0' unescaped, the root -> '' (+1) */
149 8 static SV *pv_name_of(pTHX_ const char *s, STRLEN l) {
150 8 SV *out = newSVpvs("");
151 8 ▶ STRLEN i = (l && s[0] == '/') ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
STRLEN i = (l && s[0] == '/') ? 1 : 0; 8 0 8
STRLEN i = (l && s[0] == '/') ? 1 : 0; 8 0 8
152 42 ▶ for (; i < l; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i < l; i++) { 34 8 42
153 34 ▶ if (s[i] == '/') sv_catpvs(out, ".");
Branch: 1 branch
BranchTrueFalseTotal
if (s[i] == '/') sv_catpvs(out, "."); 0 34 34
154 34 ▶ else if (s[i] == '~' && i + 1 < l && s[i + 1] == '1') {
Branch: 3 branches
BranchTrueFalseTotal
else if (s[i] == '~' && i + 1 < l && s[i + 1] == '1') { 0 34 34
else if (s[i] == '~' && i + 1 < l && s[i + 1] == '1') { 0 0 0
else if (s[i] == '~' && i + 1 < l && s[i + 1] == '1') { 0 0 0
155 0 sv_catpvs(out, "/"); i++;
156 }
157 34 ▶ else if (s[i] == '~' && i + 1 < l && s[i + 1] == '0') {
Branch: 3 branches
BranchTrueFalseTotal
else if (s[i] == '~' && i + 1 < l && s[i + 1] == '0') { 0 34 34
else if (s[i] == '~' && i + 1 < l && s[i + 1] == '0') { 0 0 0
else if (s[i] == '~' && i + 1 < l && s[i + 1] == '0') { 0 0 0
158 0 sv_catpvs(out, "~"); i++;
159 }
160 34 else sv_catpvn(out, s + i, 1);
161 }
162 8 return out;
163 }
164  
165 /* Augment raw JSON::Schema::Fast errors (+1 AV): every error gains `name`;
166 * a root-level `required` failure is expanded to one error per missing
167 * property (when the plain schema and a hash of data are known), so
168 * error($name) lands beside the right field. */
169 10 static AV *pv_augment(pTHX_ AV *errors, HV *schema, SV *data) {
170 10 AV *out = newAV();
171 10 ▶ HV *datah = (data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
HV *datah = (data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV) 10 0 10
HV *datah = (data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV) 10 0 10
172 20 ▶ ? (HV *)SvRV(data) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(data) : NULL; 10 0 10
173 10 SSize_t i, n = av_len(errors) + 1;
174 25 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 15 10 25
175 15 SV **ep = av_fetch(errors, i, 0);
176 HV *e; SV **kw, **loc;
177 15 const char *locs = ""; STRLEN locl = 0;
178 15 ▶ if (!(ep && *ep && SvROK(*ep) && SvTYPE(SvRV(*ep)) == SVt_PVHV))
Branch: 4 branches
BranchTrueFalseTotal
if (!(ep && *ep && SvROK(*ep) && SvTYPE(SvRV(*ep)) == SVt_PVHV)) 15 0 15
if (!(ep && *ep && SvROK(*ep) && SvTYPE(SvRV(*ep)) == SVt_PVHV)) 15 0 15
if (!(ep && *ep && SvROK(*ep) && SvTYPE(SvRV(*ep)) == SVt_PVHV)) 15 0 15
if (!(ep && *ep && SvROK(*ep) && SvTYPE(SvRV(*ep)) == SVt_PVHV)) 0 15 15
179 7 continue;
180 15 e = (HV *)SvRV(*ep);
181 15 kw = hv_fetchs(e, "keyword", 0);
182 15 loc = hv_fetchs(e, "instanceLocation", 0);
183 30 ▶ if (loc && *loc && SvOK(*loc)) locs = SvPV_const(*loc, locl);
Branch: 3 branches
BranchTrueFalseTotal
if (loc && *loc && SvOK(*loc)) locs = SvPV_const(*loc, locl); 15 0 15
if (loc && *loc && SvOK(*loc)) locs = SvPV_const(*loc, locl); 15 0 15
if (loc && *loc && SvOK(*loc)) locs = SvPV_const(*loc, locl); 15 0 15
184 30 ▶ if (kw && *kw && SvOK(*kw) && strEQ(SvPV_nolen(*kw), "required")
Branch: 4 branches
BranchTrueFalseTotal
if (kw && *kw && SvOK(*kw) && strEQ(SvPV_nolen(*kw), "required") 15 0 15
if (kw && *kw && SvOK(*kw) && strEQ(SvPV_nolen(*kw), "required") 15 0 15
if (kw && *kw && SvOK(*kw) && strEQ(SvPV_nolen(*kw), "required") 15 0 15
if (kw && *kw && SvOK(*kw) && strEQ(SvPV_nolen(*kw), "required") 7 8 15
185 7 ▶ && locl == 0 && schema && datah) {
Branch: 3 branches
BranchTrueFalseTotal
&& locl == 0 && schema && datah) { 7 0 7
&& locl == 0 && schema && datah) { 7 0 7
&& locl == 0 && schema && datah) { 7 0 7
186 7 SV **rq = hv_fetchs(schema, "required", 0);
187 7 ▶ if (rq && *rq && SvROK(*rq) && SvTYPE(SvRV(*rq)) == SVt_PVAV) {
Branch: 4 branches
BranchTrueFalseTotal
if (rq && *rq && SvROK(*rq) && SvTYPE(SvRV(*rq)) == SVt_PVAV) { 7 0 7
if (rq && *rq && SvROK(*rq) && SvTYPE(SvRV(*rq)) == SVt_PVAV) { 7 0 7
if (rq && *rq && SvROK(*rq) && SvTYPE(SvRV(*rq)) == SVt_PVAV) { 7 0 7
if (rq && *rq && SvROK(*rq) && SvTYPE(SvRV(*rq)) == SVt_PVAV) { 7 0 7
188 7 AV *req = (AV *)SvRV(*rq);
189 7 SSize_t j, rn = av_len(req) + 1;
190 7 int missing = 0;
191 14 ▶ for (j = 0; j < rn; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < rn; j++) { 7 7 14
192 7 SV **pp = av_fetch(req, j, 0);
193 STRLEN pl; const char *p;
194 HV *ne; SV *iloc;
195 7 ▶ if (!(pp && *pp && SvOK(*pp))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(pp && *pp && SvOK(*pp))) continue; 7 0 7
if (!(pp && *pp && SvOK(*pp))) continue; 7 0 7
if (!(pp && *pp && SvOK(*pp))) continue; 0 7 7
196 7 p = SvPV_const(*pp, pl);
197 7 ▶ if (hv_exists(datah, p, (I32)pl)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(datah, p, (I32)pl)) continue; 0 7 7
198 7 ne = newHVhv(e);
199 7 iloc = newSVpvs("/");
200 7 sv_catpvn(iloc, p, pl);
201 7 (void)hv_stores(ne, "instanceLocation", iloc);
202 7 (void)hv_stores(ne, "name", newSVpvn(p, pl));
203 7 (void)hv_stores(ne, "message",
204 newSVpvf("missing required property '%.*s'",
205 (int)pl, p));
206 7 av_push(out, newRV_noinc((SV *)ne));
207 7 missing = 1;
208 }
209 7 ▶ if (missing) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (missing) continue; 7 0 7
210 }
211 }
212 {
213 8 HV *ne = newHVhv(e);
214 8 (void)hv_stores(ne, "name", pv_name_of(aTHX_ locs, locl));
215 8 av_push(out, newRV_noinc((SV *)ne));
216 }
217 }
218 10 return out;
219 }
220  
221 /* ---- the data sources ----------------------------------------------------- */
222  
223 713 static SV *pv_source_json(pTHX_ SV *c) {
224 713 SV *req = pv_meth_ev(aTHX_ c, "req", NULL, 0);
225 713 SV *body = NULL;
226 713 ▶ if (req) {
Branch: 1 branch
BranchTrueFalseTotal
if (req) { 713 0 713
227 713 body = pv_meth_ev(aTHX_ req, "json", NULL, 0);
228 713 SvREFCNT_dec(req);
229 }
230 713 ▶ if (body && SvROK(body)) return body;
Branch: 2 branches
BranchTrueFalseTotal
if (body && SvROK(body)) return body; 713 0 713
if (body && SvROK(body)) return body; 713 0 713
231 0 ▶ if (body) SvREFCNT_dec(body);
Branch: 1 branch
BranchTrueFalseTotal
if (body) SvREFCNT_dec(body); 0 0 0
232 0 return newRV_noinc((SV *)newHV());
233 }
234  
235 8 static SV *pv_source_params(pTHX_ SV *c) {
236 8 SV *p = pv_meth_ev(aTHX_ c, "params", NULL, 0);
237 8 ▶ if (p && SvROK(p) && SvTYPE(SvRV(p)) == SVt_PVHV) return p;
Branch: 3 branches
BranchTrueFalseTotal
if (p && SvROK(p) && SvTYPE(SvRV(p)) == SVt_PVHV) return p; 8 0 8
if (p && SvROK(p) && SvTYPE(SvRV(p)) == SVt_PVHV) return p; 8 0 8
if (p && SvROK(p) && SvTYPE(SvRV(p)) == SVt_PVHV) return p; 8 0 8
238 0 ▶ if (p) SvREFCNT_dec(p);
Branch: 1 branch
BranchTrueFalseTotal
if (p) SvREFCNT_dec(p); 0 0 0
239 0 return newRV_noinc((SV *)newHV());
240 }
241  
242 /* auto: the decoded JSON body for a JSON request, the merged params
243 * (captures, query, form) otherwise (+1) */
244 718 static SV *pv_source_auto(pTHX_ SV *c) {
245 718 SV *req = pv_meth_ev(aTHX_ c, "req", NULL, 0);
246 718 int is_json = 0;
247 718 ▶ if (req) {
Branch: 1 branch
BranchTrueFalseTotal
if (req) { 718 0 718
248 SV *argv[1], *ct;
249 718 argv[0] = sv_2mortal(newSVpvs("content-type"));
250 718 ct = pv_meth_ev(aTHX_ req, "header", argv, 1);
251 718 ▶ if (ct && SvOK(ct)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ct && SvOK(ct)) { 718 0 718
if (ct && SvOK(ct)) { 713 5 718
252 713 STRLEN l; const char *s = SvPV_const(ct, l);
253 713 is_json = pv_ci_has(s, l, "application/json");
254 }
255 718 ▶ if (ct) SvREFCNT_dec(ct);
Branch: 1 branch
BranchTrueFalseTotal
if (ct) SvREFCNT_dec(ct); 718 0 718
256 718 SvREFCNT_dec(req);
257 }
258 718 ▶ return is_json ? pv_source_json(aTHX_ c) : pv_source_params(aTHX_ c);
Branch: 1 branch
BranchTrueFalseTotal
return is_json ? pv_source_json(aTHX_ c) : pv_source_params(aTHX_ c); 712 6 718
259 }
260  
261 /* ---- the Result ----------------------------------------------------------- */
262  
263 /* bless { errors, data, schema? } (+1); owns errs */
264 722 static SV *pv_result_new(pTHX_ AV *errs, SV *data, SV *plain_schema) {
265 722 HV *h = newHV();
266 722 (void)hv_stores(h, "errors", newRV_noinc((SV *)errs));
267 722 (void)hv_stores(h, "data", newSVsv(data));
268 722 ▶ if (plain_schema)
Branch: 1 branch
BranchTrueFalseTotal
if (plain_schema) 721 1 722
269 721 (void)hv_stores(h, "schema", newSVsv(plain_schema));
270 722 return sv_bless(newRV_noinc((SV *)h),
271 gv_stashpvs(PV_RESULT_CLASS, GV_ADD));
272 }
273  
274 731 static HV *pv_result_hv(pTHX_ SV *self) {
275 731 ▶ if (!(self && SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV))
Branch: 3 branches
BranchTrueFalseTotal
if (!(self && SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)) 731 0 731
if (!(self && SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)) 731 0 731
if (!(self && SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)) 0 731 731
276 0 croak("Punk: not a " PV_RESULT_CLASS);
277 731 return (HV *)SvRV(self);
278 }
279  
280 722 static AV *pv_result_errors(pTHX_ HV *h) {
281 722 SV **e = hv_fetchs(h, "errors", 0);
282 722 ▶ return (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV)
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) 722 0 722
return (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) 722 0 722
return (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) 722 0 722
283 1444 ▶ ? (AV *)SvRV(*e) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (AV *)SvRV(*e) : NULL; 722 0 722
284 }
285  
286 /* valid / valid($name): only declared properties, numeric strings numified
287 * for integer/number, booleans canonical 1/0 - the shapes $c->openapi
288 * delivers. Without a plain schema (a precompiled validator) the data
289 * comes back as-is (+1). */
290 5 static SV *pv_result_valid(pTHX_ HV *h, SV *name) {
291 5 SV **d = hv_fetchs(h, "data", 0);
292 5 SV **s = hv_fetchs(h, "schema", 0);
293 5 ▶ SV *data = (d && *d) ? *d : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
SV *data = (d && *d) ? *d : &PL_sv_undef; 5 0 5
SV *data = (d && *d) ? *d : &PL_sv_undef; 5 0 5
294 5 ▶ HV *datah = (SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
HV *datah = (SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV) 5 0 5
295 10 ▶ ? (HV *)SvRV(data) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(data) : NULL; 5 0 5
296 5 HV *props = NULL;
297 5 ▶ if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) {
Branch: 4 branches
BranchTrueFalseTotal
if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) { 4 1 5
if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) { 4 0 4
if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) { 4 0 4
if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) { 4 0 4
298 4 SV **pp = hv_fetchs((HV *)SvRV(*s), "properties", 0);
299 4 ▶ if (pp && *pp && SvROK(*pp) && SvTYPE(SvRV(*pp)) == SVt_PVHV)
Branch: 4 branches
BranchTrueFalseTotal
if (pp && *pp && SvROK(*pp) && SvTYPE(SvRV(*pp)) == SVt_PVHV) 4 0 4
if (pp && *pp && SvROK(*pp) && SvTYPE(SvRV(*pp)) == SVt_PVHV) 4 0 4
if (pp && *pp && SvROK(*pp) && SvTYPE(SvRV(*pp)) == SVt_PVHV) 4 0 4
if (pp && *pp && SvROK(*pp) && SvTYPE(SvRV(*pp)) == SVt_PVHV) 4 0 4
300 4 props = (HV *)SvRV(*pp);
301 }
302 5 ▶ if (!props || !datah) {
Branch: 2 branches
BranchTrueFalseTotal
if (!props || !datah) { 4 1 5
if (!props || !datah) { 0 4 4
303 1 ▶ if (name && SvOK(name)) {
Branch: 2 branches
BranchTrueFalseTotal
if (name && SvOK(name)) { 1 0 1
if (name && SvOK(name)) { 0 1 1
304 0 ▶ HE *he = datah ? hv_fetch_ent(datah, name, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HE *he = datah ? hv_fetch_ent(datah, name, 0, 0) : NULL; 0 0 0
305 0 ▶ return he ? newSVsv(HeVAL(he)) : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
return he ? newSVsv(HeVAL(he)) : newSV(0); 0 0 0
306 }
307 1 ▶ return datah ? newRV_noinc((SV *)newHVhv(datah)) : newSVsv(data);
Branch: 1 branch
BranchTrueFalseTotal
return datah ? newRV_noinc((SV *)newHVhv(datah)) : newSVsv(data); 1 0 1
308 }
309 {
310 4 HV *out = newHV();
311 HE *he;
312 4 hv_iterinit(props);
313 12 ▶ while ((he = hv_iternext(props))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(props))) { 8 4 12
314 8 SV *k = hv_iterkeysv(he);
315 8 HE *dhe = hv_fetch_ent(datah, k, 0, 0);
316 8 SV *v, *prop, *nv = NULL;
317 8 const char *type = NULL;
318 8 ▶ if (!dhe) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!dhe) continue; 0 8 8
319 8 v = HeVAL(dhe);
320 8 prop = HeVAL(he);
321 8 ▶ if (prop && SvROK(prop) && SvTYPE(SvRV(prop)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (prop && SvROK(prop) && SvTYPE(SvRV(prop)) == SVt_PVHV) { 8 0 8
if (prop && SvROK(prop) && SvTYPE(SvRV(prop)) == SVt_PVHV) { 8 0 8
if (prop && SvROK(prop) && SvTYPE(SvRV(prop)) == SVt_PVHV) { 8 0 8
322 8 SV **tp = hv_fetchs((HV *)SvRV(prop), "type", 0);
323 16 ▶ if (tp && *tp && SvOK(*tp)) type = SvPV_nolen(*tp);
Branch: 3 branches
BranchTrueFalseTotal
if (tp && *tp && SvOK(*tp)) type = SvPV_nolen(*tp); 8 0 8
if (tp && *tp && SvOK(*tp)) type = SvPV_nolen(*tp); 8 0 8
if (tp && *tp && SvOK(*tp)) type = SvPV_nolen(*tp); 8 0 8
324 }
325 8 ▶ if (type && v && SvOK(v) && !SvROK(v)) {
Branch: 4 branches
BranchTrueFalseTotal
if (type && v && SvOK(v) && !SvROK(v)) { 8 0 8
if (type && v && SvOK(v) && !SvROK(v)) { 8 0 8
if (type && v && SvOK(v) && !SvROK(v)) { 8 0 8
if (type && v && SvOK(v) && !SvROK(v)) { 7 1 8
326 7 ▶ if ((strEQ(type, "integer") || strEQ(type, "number"))
Branch: 2 branches
BranchTrueFalseTotal
if ((strEQ(type, "integer") || strEQ(type, "number")) 3 4 7
if ((strEQ(type, "integer") || strEQ(type, "number")) 0 3 3
327 4 ▶ && looks_like_number(v)) {
Branch: 1 branch
BranchTrueFalseTotal
&& looks_like_number(v)) { 4 0 4
328 4 nv = strEQ(type, "integer") ? newSViv((IV)SvNV(v))
329 4 ▶ : newSVnv(SvNV(v));
Branch: 1 branch
BranchTrueFalseTotal
: newSVnv(SvNV(v)); 4 0 4
330 }
331 3 ▶ else if (strEQ(type, "boolean")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(type, "boolean")) { 1 2 3
332 1 STRLEN vl; const char *vs = SvPV_const(v, vl);
333 1 int truthy = SvTRUE(v)
334 1 ▶ && !(vl == 5 && memEQ(vs, "false", 5))
Branch: 2 branches
BranchTrueFalseTotal
&& !(vl == 5 && memEQ(vs, "false", 5)) 1 0 1
&& !(vl == 5 && memEQ(vs, "false", 5)) 0 1 1
335 2 ▶ && !(vl == 1 && vs[0] == '0');
Branch: 3 branches
BranchTrueFalseTotal
&& !(vl == 1 && vs[0] == '0'); 1 0 1
&& !(vl == 1 && vs[0] == '0'); 0 0 0
&& !(vl == 1 && vs[0] == '0'); 0 0 0
336 1 nv = newSViv(truthy ? 1 : 0);
337 }
338 }
339 8 ▶ (void)hv_store_ent(out, k, nv ? nv : newSVsv(v), 0);
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_store_ent(out, k, nv ? nv : newSVsv(v), 0); 3 5 8
340 }
341 4 ▶ if (name && SvOK(name)) {
Branch: 2 branches
BranchTrueFalseTotal
if (name && SvOK(name)) { 4 0 4
if (name && SvOK(name)) { 2 2 4
342 2 HE *ohe = hv_fetch_ent(out, name, 0, 0);
343 2 ▶ SV *r = ohe ? newSVsv(HeVAL(ohe)) : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
SV *r = ohe ? newSVsv(HeVAL(ohe)) : newSV(0); 2 0 2
344 2 SvREFCNT_dec((SV *)out);
345 2 return r;
346 }
347 2 return newRV_noinc((SV *)out);
348 }
349 }
350  
351 /* ---- run one validation --------------------------------------------------- */
352  
353 /* validate `data` against a compiled schema, stash and return the Result
354 * (+1). plain_schema is the hashref form when known (drives `required`
355 * expansion and valid()'s property filter), else NULL. */
356 722 static SV *pv_run(pTHX_ SV *c, SV *compiled, SV *plain_schema, SV *data) {
357 722 AV *raw = (AV *)sv_2mortal((SV *)newAV());
358 722 int ok = punk_jsf(aTHX)->validate(aTHX_ compiled, data, raw);
359 721 ▶ HV *sh = (plain_schema && SvROK(plain_schema)
Branch: 1 branch
BranchTrueFalseTotal
HV *sh = (plain_schema && SvROK(plain_schema) 721 0 721
360 721 ▶ && SvTYPE(SvRV(plain_schema)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(plain_schema)) == SVt_PVHV) 721 0 721
361 1443 ▶ ? (HV *)SvRV(plain_schema) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(plain_schema) : NULL; 721 1 722
362 722 ▶ AV *errs = ok ? newAV() : pv_augment(aTHX_ raw, sh, data);
Branch: 1 branch
BranchTrueFalseTotal
AV *errs = ok ? newAV() : pv_augment(aTHX_ raw, sh, data); 712 10 722
363 722 ▶ SV *r = pv_result_new(aTHX_ errs, data, sh ? plain_schema : NULL);
Branch: 1 branch
BranchTrueFalseTotal
SV *r = pv_result_new(aTHX_ errs, data, sh ? plain_schema : NULL); 721 1 722
364 722 HV *stash = ps_stash(aTHX_ pcx_av(aTHX_ c));
365 722 (void)hv_stores(stash, "punk.validation", newSVsv(r));
366 722 return r;
367 }
368  
369 /* $c->validate($schema, $data?) (+1 Result) */
370 713 static SV *pv_validate(pTHX_ SV *c, SV *schema, SV *source) {
371 713 SV *compiled = sv_2mortal(pv_compile(aTHX_ schema));
372 712 ▶ SV *plain = (SvROK(schema) && SvTYPE(SvRV(schema)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
SV *plain = (SvROK(schema) && SvTYPE(SvRV(schema)) == SVt_PVHV) 711 1 712
373 1424 ▶ ? schema : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? schema : NULL; 712 0 712
374 712 ▶ SV *data = (source && SvROK(source)) ? newSVsv(source)
Branch: 1 branch
BranchTrueFalseTotal
SV *data = (source && SvROK(source)) ? newSVsv(source) 1 711 712
375 1423 ▶ : pv_source_auto(aTHX_ c);
Branch: 1 branch
BranchTrueFalseTotal
: pv_source_auto(aTHX_ c); 712 0 712
376 SV *r;
377 712 sv_2mortal(data);
378 712 r = pv_run(aTHX_ c, compiled, plain, data);
379 712 return r;
380 }
381  
382 /* ---- the route guard ------------------------------------------------------ */
383  
384 /* capture: [ compiled, plain-schema-or-undef, source-or-undef,
385 * on_invalid-or-undef ] */
386 XS_INTERNAL(pv_guard_cb);
387 10 XS_INTERNAL(pv_guard_cb) {
388 10 dXSARGS;
389 10 AV *cap = punk_clos_cap(aTHX_ cv);
390 SV *c, *compiled, *plain, *srcv, *oi, *data, *r;
391 AV *errs;
392 10 ▶ if (items < 1 || !cap) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (items < 1 || !cap) XSRETURN_EMPTY; 10 0 10
if (items < 1 || !cap) XSRETURN_EMPTY; 0 10 10
393 10 c = ST(0);
394 10 compiled = *av_fetch(cap, 0, 0);
395 10 plain = *av_fetch(cap, 1, 0);
396 10 srcv = *av_fetch(cap, 2, 0);
397 10 oi = *av_fetch(cap, 3, 0);
398  
399 10 ▶ if (SvOK(srcv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(srcv)) { 3 7 10
400 3 data = strEQ(SvPV_nolen(srcv), "json") ? pv_source_json(aTHX_ c)
401 3 ▶ : pv_source_params(aTHX_ c);
Branch: 1 branch
BranchTrueFalseTotal
: pv_source_params(aTHX_ c); 1 2 3
402 }
403 7 else data = pv_source_auto(aTHX_ c);
404 10 sv_2mortal(data);
405  
406 10 ▶ r = sv_2mortal(pv_run(aTHX_ c, compiled,
Branch: 1 branch
BranchTrueFalseTotal
r = sv_2mortal(pv_run(aTHX_ c, compiled, 10 0 10
407 SvOK(plain) ? plain : NULL, data));
408 10 errs = pv_result_errors(aTHX_ pv_result_hv(aTHX_ r));
409 10 ▶ if (!errs || av_len(errs) < 0) XSRETURN_EMPTY; /* valid: continue */
Branch: 2 branches
BranchTrueFalseTotal
if (!errs || av_len(errs) < 0) XSRETURN_EMPTY; /* valid: continue */ 10 0 10
if (!errs || av_len(errs) < 0) XSRETURN_EMPTY; /* valid: continue */ 6 4 10
410  
411 4 ▶ if (SvOK(oi)) { /* the app's own failure response */
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(oi)) { /* the app's own failure response */ 1 3 4
412 1 int died = 0;
413 1 SV *out = pd_call(aTHX_ oi, c, &died);
414 1 ▶ if (died) { if (out) SvREFCNT_dec(out); croak_sv(ERRSV); }
Branch: 3 branches
BranchTrueFalseTotal
if (died) { if (out) SvREFCNT_dec(out); croak_sv(ERRSV); } 0 1 1
if (died) { if (out) SvREFCNT_dec(out); croak_sv(ERRSV); } 0 0 0
if (died) { if (out) SvREFCNT_dec(out); croak_sv(ERRSV); } 0 0 0
415 1 ▶ ST(0) = out ? sv_2mortal(out) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = out ? sv_2mortal(out) : &PL_sv_undef; 1 0 1
416 1 XSRETURN(1);
417 }
418 { /* the OpenAPI-mount-shaped 400 */
419 3 HV *body = newHV();
420 SV *argv[2], *resp;
421 3 (void)hv_stores(body, "errors", newRV_inc((SV *)errs));
422 3 argv[0] = sv_2mortal(newRV_noinc((SV *)body));
423 3 argv[1] = sv_2mortal(newSViv(400));
424 3 resp = pcx_call_meth(aTHX_ c, "json", argv, 2, 1);
425 3 ▶ ST(0) = resp ? sv_2mortal(resp) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = resp ? sv_2mortal(resp) : &PL_sv_undef; 3 0 3
426 3 XSRETURN(1);
427 }
428 }
429  
430 /* ---- boot: compile the route-level validate options ----------------------- */
431  
432 /* Called from Punk::App::compile_extras (after the router compiled): parse
433 * each validate spec, compile its schema once, resolve on_invalid, and
434 * APPEND a guard closure to the record - after any auth guards, so a 401
435 * still costs no body parse. punk_serve walks the same guards AV with no
436 * idea anything changed. */
437 5 static void pv_compile_routes(pTHX_ SV *self) {
438 5 HV *h = (HV *)SvRV(self);
439 5 SV **vrp = hv_fetchs(h, K_VALIDATE_ROUTES, 0);
440 AV *vrs;
441 HV *by;
442 SSize_t i, n;
443 5 ▶ if (!(vrp && *vrp && SvROK(*vrp) && SvTYPE(SvRV(*vrp)) == SVt_PVAV))
Branch: 4 branches
BranchTrueFalseTotal
if (!(vrp && *vrp && SvROK(*vrp) && SvTYPE(SvRV(*vrp)) == SVt_PVAV)) 5 0 5
if (!(vrp && *vrp && SvROK(*vrp) && SvTYPE(SvRV(*vrp)) == SVt_PVAV)) 5 0 5
if (!(vrp && *vrp && SvROK(*vrp) && SvTYPE(SvRV(*vrp)) == SVt_PVAV)) 5 0 5
if (!(vrp && *vrp && SvROK(*vrp) && SvTYPE(SvRV(*vrp)) == SVt_PVAV)) 0 5 5
444 0 return;
445 5 vrs = (AV *)SvRV(*vrp);
446 5 n = av_len(vrs) + 1;
447 5 ▶ if (!n) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return; 0 5 5
448 5 by = pk_route_index(aTHX_ self, "validate");
449  
450 11 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 8 3 11
451 8 SV **vp = av_fetch(vrs, i, 0);
452 HV *vr, *rec;
453 SV **m, **p, **sp;
454 8 SV *where, *spec, *schema, *src = NULL, *oi = NULL;
455 8 SV *compiled, *resolved = NULL;
456 HE *he;
457 8 ▶ if (!(vp && *vp && SvROK(*vp) && SvTYPE(SvRV(*vp)) == SVt_PVHV))
Branch: 4 branches
BranchTrueFalseTotal
if (!(vp && *vp && SvROK(*vp) && SvTYPE(SvRV(*vp)) == SVt_PVHV)) 8 0 8
if (!(vp && *vp && SvROK(*vp) && SvTYPE(SvRV(*vp)) == SVt_PVHV)) 8 0 8
if (!(vp && *vp && SvROK(*vp) && SvTYPE(SvRV(*vp)) == SVt_PVHV)) 8 0 8
if (!(vp && *vp && SvROK(*vp) && SvTYPE(SvRV(*vp)) == SVt_PVHV)) 0 8 8
458 0 continue;
459 8 vr = (HV *)SvRV(*vp);
460 8 m = hv_fetchs(vr, K_METHOD, 0);
461 8 p = hv_fetchs(vr, K_PATH, 0);
462 8 sp = hv_fetchs(vr, K_VALIDATE, 0);
463 8 ▶ if (!(m && *m && p && *p && sp && *sp)) continue;
Branch: 6 branches
BranchTrueFalseTotal
if (!(m && *m && p && *p && sp && *sp)) continue; 8 0 8
if (!(m && *m && p && *p && sp && *sp)) continue; 8 0 8
if (!(m && *m && p && *p && sp && *sp)) continue; 8 0 8
if (!(m && *m && p && *p && sp && *sp)) continue; 8 0 8
if (!(m && *m && p && *p && sp && *sp)) continue; 8 0 8
if (!(m && *m && p && *p && sp && *sp)) continue; 0 8 8
464 8 where = sv_2mortal(newSVsv(*m));
465 8 sv_catpvs(where, " ");
466 8 sv_catsv(where, *p);
467  
468 8 he = hv_fetch_ent(by, where, 0, 0);
469 8 ▶ if (!he)
Branch: 1 branch
BranchTrueFalseTotal
if (!he) 0 8 8
470 0 croak("Punk: validate on unknown route %s", SvPV_nolen(where));
471 8 rec = (HV *)SvRV(HeVAL(he));
472  
473 8 spec = *sp;
474 8 schema = spec;
475 8 ▶ if (SvROK(spec) && SvTYPE(SvRV(spec)) == SVt_PVHV
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(spec) && SvTYPE(SvRV(spec)) == SVt_PVHV 8 0 8
if (SvROK(spec) && SvTYPE(SvRV(spec)) == SVt_PVHV 8 0 8
476 8 ▶ && hv_exists((HV *)SvRV(spec), "schema", 6)) {
Branch: 1 branch
BranchTrueFalseTotal
&& hv_exists((HV *)SvRV(spec), "schema", 6)) { 4 4 8
477 4 HV *sh = (HV *)SvRV(spec);
478 HE *ke;
479 SV **x;
480 4 hv_iterinit(sh);
481 12 ▶ while ((ke = hv_iternext(sh))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((ke = hv_iternext(sh))) { 8 4 12
482 8 ▶ STRLEN kl; const char *k = HePV(ke, kl);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl; const char *k = HePV(ke, kl); 0 8 8
483 8 ▶ if (!(strEQ(k, "schema") || strEQ(k, "source")
Branch: 2 branches
BranchTrueFalseTotal
if (!(strEQ(k, "schema") || strEQ(k, "source") 4 4 8
if (!(strEQ(k, "schema") || strEQ(k, "source") 1 3 4
484 1 ▶ || strEQ(k, "on_invalid")))
Branch: 1 branch
BranchTrueFalseTotal
|| strEQ(k, "on_invalid"))) 0 1 1
485 0 croak("Punk: unknown validate option '%s' on %s",
486 k, SvPV_nolen(where));
487 }
488 4 x = hv_fetchs(sh, "schema", 0);
489 4 ▶ schema = (x && *x) ? *x : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
schema = (x && *x) ? *x : &PL_sv_undef; 4 0 4
schema = (x && *x) ? *x : &PL_sv_undef; 4 0 4
490 4 x = hv_fetchs(sh, "source", 0);
491 4 ▶ if (x && *x && SvOK(*x)) src = *x;
Branch: 3 branches
BranchTrueFalseTotal
if (x && *x && SvOK(*x)) src = *x; 3 1 4
if (x && *x && SvOK(*x)) src = *x; 3 0 3
if (x && *x && SvOK(*x)) src = *x; 3 0 3
492 4 x = hv_fetchs(sh, "on_invalid", 0);
493 4 ▶ if (x && *x && SvOK(*x)) oi = *x;
Branch: 3 branches
BranchTrueFalseTotal
if (x && *x && SvOK(*x)) oi = *x; 1 3 4
if (x && *x && SvOK(*x)) oi = *x; 1 0 1
if (x && *x && SvOK(*x)) oi = *x; 1 0 1
494 }
495  
496 8 ▶ if (!(SvROK(schema)
Branch: 1 branch
BranchTrueFalseTotal
if (!(SvROK(schema) 7 1 8
497 7 ▶ && (SvOBJECT(SvRV(schema))
Branch: 1 branch
BranchTrueFalseTotal
&& (SvOBJECT(SvRV(schema)) 7 0 7
498 7 ▶ || SvTYPE(SvRV(schema)) == SVt_PVHV)))
Branch: 1 branch
BranchTrueFalseTotal
|| SvTYPE(SvRV(schema)) == SVt_PVHV))) 0 7 7
499 1 croak("Punk: invalid validate schema on %s: the schema must be "
500 "a hashref or a JSON::Schema::Fast::Compiled",
501 SvPV_nolen(where));
502 7 compiled = pv_compile(aTHX_ schema); /* croaks on a bad schema */
503  
504 10 ▶ if (src && !(strEQ(SvPV_nolen(src), "params")
Branch: 2 branches
BranchTrueFalseTotal
if (src && !(strEQ(SvPV_nolen(src), "params") 3 4 7
if (src && !(strEQ(SvPV_nolen(src), "params") 2 1 3
505 2 ▶ || strEQ(SvPV_nolen(src), "json"))) {
Branch: 1 branch
BranchTrueFalseTotal
|| strEQ(SvPV_nolen(src), "json"))) { 1 1 2
506 1 SvREFCNT_dec(compiled);
507 1 croak("Punk: validate source on %s must be 'params' or 'json'",
508 SvPV_nolen(where));
509 }
510 6 ▶ if (oi) {
Branch: 1 branch
BranchTrueFalseTotal
if (oi) { 1 5 6
511 SV *argv[2];
512 1 argv[0] = oi;
513 1 argv[1] = sv_2mortal(newSVpvs("on_invalid"));
514 1 resolved = pcx_call_meth(aTHX_ self, "_resolve_target",
515 argv, 2, 1); /* croaks on a typo */
516 }
517  
518 {
519 6 AV *cap = newAV();
520 6 av_push(cap, compiled); /* owns the +1 */
521 6 ▶ av_push(cap, (SvROK(schema)
Branch: 2 branches
BranchTrueFalseTotal
av_push(cap, (SvROK(schema) 6 0 6
av_push(cap, (SvROK(schema) 6 0 6
522 && SvTYPE(SvRV(schema)) == SVt_PVHV)
523 ? newSVsv(schema) : newSV(0));
524 6 ▶ av_push(cap, src ? newSVsv(src) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_push(cap, src ? newSVsv(src) : newSV(0)); 2 4 6
525 6 ▶ av_push(cap, resolved ? resolved : newSV(0)); /* owns the +1 */
Branch: 1 branch
BranchTrueFalseTotal
av_push(cap, resolved ? resolved : newSV(0)); /* owns the +1 */ 5 1 6
526 6 pk_route_guard_push(aTHX_ rec,
527 punk_closure(aTHX_ pv_guard_cb, cap));
528 }
529 }
530 }
531  
532 #endif /* PUNK_VALIDATE_H */
« include/punk/punk_wshandshake.h ↑ Summary include/punk/punk_context.h »