include/oa_validate.h

filter: Statementstmt 97.1% 618 / 636 Branchbran 74.1% 608 / 820 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.2% 1226 / 1456 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/oa_compile.h ↑ Summary include/oa_types.h »
1 #ifndef OA_VALIDATE_H
2 #define OA_VALIDATE_H
3  
4 /* Per-request validation for a matched operation: percent-decode path
5 * captures, parse the query string and Cookie header in C, JSON-decode the
6 * body, run every declared parameter and the body through the JSF ABI, and
7 * assemble the validated params structure. Two modes: errs == NULL uses the
8 * cheap boolean path (is_valid); with an AV, all failures are collected as
9 * hashrefs - JSF's error hashes augmented with `in` and `name`. */
10  
11 /* ---- decoding helpers ------------------------------------------------------ */
12  
13 52 static int oa_hexval(char c) {
14 52 ▶ if (c >= '0' && c <= '9') return c - '0';
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '9') return c - '0'; 52 0 52
if (c >= '0' && c <= '9') return c - '0'; 39 13 52
15 13 ▶ if (c >= 'a' && c <= 'f') return c - 'a' + 10;
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'a' && c <= 'f') return c - 'a' + 10; 0 13 13
if (c >= 'a' && c <= 'f') return c - 'a' + 10; 0 0 0
16 13 ▶ if (c >= 'A' && c <= 'F') return c - 'A' + 10;
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'A' && c <= 'F') return c - 'A' + 10; 13 0 13
if (c >= 'A' && c <= 'F') return c - 'A' + 10; 13 0 13
17 0 return -1;
18 }
19  
20 /* %XX (and optionally '+' -> space) decode into a fresh SV */
21 14793 static SV *oa_pct_decode(pTHX_ const char *p, STRLEN l, int plus) {
22 14793 SV *out = newSV(l + 1);
23 char *d;
24 14793 STRLEN i, o = 0;
25 14793 SvPOK_on(out);
26 14793 d = SvPVX(out);
27 46976 ▶ for (i = 0; i < l; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l; i++) { 32183 14793 46976
28 32183 ▶ if (p[i] == '%' && i + 2 < l) {
Branch: 2 branches
BranchTrueFalseTotal
if (p[i] == '%' && i + 2 < l) { 26 32157 32183
if (p[i] == '%' && i + 2 < l) { 26 0 26
29 26 int h = oa_hexval(p[i + 1]), lo = oa_hexval(p[i + 2]);
30 26 ▶ if (h >= 0 && lo >= 0) { d[o++] = (char)((h << 4) | lo); i += 2; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (h >= 0 && lo >= 0) { d[o++] = (char)((h << 4) | lo); i += 2; continue; } 26 0 26
if (h >= 0 && lo >= 0) { d[o++] = (char)((h << 4) | lo); i += 2; continue; } 26 0 26
31 }
32 32157 ▶ if (plus && p[i] == '+') { d[o++] = ' '; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (plus && p[i] == '+') { d[o++] = ' '; continue; } 28938 3219 32157
if (plus && p[i] == '+') { d[o++] = ' '; continue; } 2 28936 28938
33 32155 d[o++] = p[i];
34 }
35 14793 d[o] = '\0';
36 14793 SvCUR_set(out, o);
37 14793 return out;
38 }
39  
40 /* ---- parameter serialization (style / explode) ------------------------------
41 *
42 * Split `l` bytes on `delim` into a fresh AV. `decode` percent-decodes each
43 * piece AFTER the split, which is the only correct order: %2C is a comma that
44 * belongs to the value, not a delimiter, so decoding first would split
45 * `a%2Cb` into two elements.
46 *
47 * Header and cookie values are NOT decoded here. Header values never were,
48 * and oa_parse_cookies deliberately keeps its values raw; decoding them now
49 * would quietly change what every existing document sees. */
50 2600 static SV *oa_split(pTHX_ const char *p, STRLEN l, char delim,
51 int decode, int plus) {
52 2600 AV *av = newAV();
53 2600 STRLEN s = 0;
54 2663 for (;;) {
55 5263 STRLEN e = s;
56 13024 ▶ while (e < l && p[e] != delim) e++;
Branch: 2 branches
BranchTrueFalseTotal
while (e < l && p[e] != delim) e++; 10424 2600 13024
while (e < l && p[e] != delim) e++; 7761 2663 10424
57 5263 ▶ av_push(av, decode ? oa_pct_decode(aTHX_ p + s, e - s, plus)
Branch: 1 branch
BranchTrueFalseTotal
av_push(av, decode ? oa_pct_decode(aTHX_ p + s, e - s, plus) 2187 3076 5263
58 : newSVpvn(p + s, e - s));
59 5263 ▶ if (e >= l) break;
Branch: 1 branch
BranchTrueFalseTotal
if (e >= l) break; 2600 2663 5263
60 2663 s = e + 1;
61 }
62 2600 return newRV_noinc((SV *)av);
63 }
64  
65 /* Split on any of `toks`, matched against the RAW text, decoding each piece
66 * after. This exists beside oa_split because a comma and a space are not
67 * symmetrical: `%2C` is an ESCAPED comma that belongs inside its element,
68 * while a space can only reach us as `%20` or `+`, so for spaceDelimited
69 * those encoded spellings ARE the delimiter. The spec offers no way to put a
70 * literal space inside a spaceDelimited array, so none is invented here. */
71 1534 static SV *oa_split_tokens(pTHX_ const char *p, STRLEN l,
72 const char *const *toks, int plus) {
73 1534 AV *av = newAV();
74 1534 STRLEN s = 0;
75 1572 for (;;) {
76 3106 STRLEN e = s;
77 3106 STRLEN tl = 0;
78 6478 ▶ while (e < l) {
Branch: 1 branch
BranchTrueFalseTotal
while (e < l) { 4944 1534 6478
79 int t;
80 15003 ▶ for (t = 0; toks[t]; t++) {
Branch: 1 branch
BranchTrueFalseTotal
for (t = 0; toks[t]; t++) { 11631 3372 15003
81 11631 STRLEN len = (STRLEN)strlen(toks[t]);
82 11631 ▶ if (e + len <= l && memEQ(p + e, toks[t], len)) { tl = len; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (e + len <= l && memEQ(p + e, toks[t], len)) { tl = len; break; } 8542 3089 11631
if (e + len <= l && memEQ(p + e, toks[t], len)) { tl = len; break; } 1572 6970 8542
83 }
84 4944 ▶ if (tl) break;
Branch: 1 branch
BranchTrueFalseTotal
if (tl) break; 1572 3372 4944
85 3372 e++;
86 }
87 3106 av_push(av, oa_pct_decode(aTHX_ p + s, e - s, plus));
88 3106 ▶ if (e >= l) break;
Branch: 1 branch
BranchTrueFalseTotal
if (e >= l) break; 1534 1572 3106
89 1572 s = e + tl;
90 }
91 1534 return newRV_noinc((SV *)av);
92 }
93  
94 /* ---- object-typed parameters ------------------------------------------------
95 *
96 * Every style in the specification's Style Examples table can carry an object,
97 * not just deepObject. Two shapes reach here, and which one depends only on
98 * `explode`:
99 *
100 * not exploded R,100,G,200,B,150 a flat list, read as key,value pairs
101 * exploded R=100.G=200.B=150 each piece is its own `key=value`
102 *
103 * The delimiter differs per style but the assembly does not, so both take an
104 * already-split list. An odd trailing element in the pair form, or a piece with
105 * no `=` in the exploded form, is skipped rather than guessed at: the schema
106 * refuses the result afterwards, which is a better error than an invented one.
107 */
108 /* The spellings of a list delimiter on the wire: a space reaches a URL only
109 * encoded (or as `+` in a query), a tab likewise, a pipe either way. */
110 510 static const char *const *oa_delim_tokens(char d, int plus) {
111 static const char *const sp_q[] = { "%20", "+", " ", NULL };
112 static const char *const sp_p[] = { "%20", " ", NULL };
113 static const char *const pi[] = { "%7C", "%7c", "|", NULL };
114 static const char *const tb[] = { "%09", "\t", NULL };
115 static const char *const co[] = { ",", NULL };
116 510 ▶ if (d == ' ') return plus ? sp_q : sp_p;
Branch: 2 branches
BranchTrueFalseTotal
if (d == ' ') return plus ? sp_q : sp_p; 2 508 510
if (d == ' ') return plus ? sp_q : sp_p; 0 2 2
117 508 ▶ if (d == '|') return pi;
Branch: 1 branch
BranchTrueFalseTotal
if (d == '|') return pi; 3 505 508
118 505 ▶ if (d == '\t') return tb;
Branch: 1 branch
BranchTrueFalseTotal
if (d == '\t') return tb; 505 0 505
119 0 return co;
120 }
121  
122 /* A Swagger 2.0 Items Object may itself be an array with its own delimiter,
123 * so `1|2,3|4` is [[1,2],[3,4]]. Each piece of a list split at level `lvl` is
124 * split again, in place, into an arrayref; the pieces are already decoded, so
125 * the inner delimiter is matched literally. */
126 4101 static void oa_split_nested(pTHX_ oa_param *pp, SV *listrv, int lvl) {
127 AV *av;
128 SSize_t i, n;
129 4101 ▶ if (!pp || lvl >= pp->ndepth || !listrv || !SvROK(listrv)) return;
Branch: 4 branches
BranchTrueFalseTotal
if (!pp || lvl >= pp->ndepth || !listrv || !SvROK(listrv)) return; 4101 0 4101
if (!pp || lvl >= pp->ndepth || !listrv || !SvROK(listrv)) return; 504 3597 4101
if (!pp || lvl >= pp->ndepth || !listrv || !SvROK(listrv)) return; 504 0 504
if (!pp || lvl >= pp->ndepth || !listrv || !SvROK(listrv)) return; 0 504 504
130 504 av = (AV *)SvRV(listrv);
131 504 n = av_len(av) + 1;
132 1512 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 1008 504 1512
133 1008 SV **e = av_fetch(av, i, 0);
134 SV *sub;
135 STRLEN l; const char *p;
136 1008 ▶ if (!e || !*e || SvROK(*e)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!e || !*e || SvROK(*e)) continue; 1008 0 1008
if (!e || !*e || SvROK(*e)) continue; 1008 0 1008
if (!e || !*e || SvROK(*e)) continue; 0 1008 1008
137 1008 p = SvPV_const(*e, l);
138 1008 sub = oa_split(aTHX_ p, l, pp->idelim[lvl], 0, 0);
139 1008 oa_split_nested(aTHX_ pp, sub, lvl + 1);
140 1008 av_store(av, i, sub);
141 }
142 }
143  
144 13 static SV *oa_obj_from_pairs(pTHX_ SV *listrv) {
145 13 AV *av = (AV *)SvRV(listrv);
146 13 HV *h = newHV();
147 13 SSize_t i, n = av_len(av) + 1;
148 52 ▶ for (i = 0; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < n; i += 2) { 39 13 52
149 39 SV **k = av_fetch(av, i, 0);
150 39 SV **v = av_fetch(av, i + 1, 0);
151 39 ▶ if (k && *k && v && *v) {
Branch: 4 branches
BranchTrueFalseTotal
if (k && *k && v && *v) { 39 0 39
if (k && *k && v && *v) { 39 0 39
if (k && *k && v && *v) { 39 0 39
if (k && *k && v && *v) { 39 0 39
152 39 STRLEN kl; const char *kp = SvPV_const(*k, kl);
153 39 ▶ if (kl) (void)hv_store(h, kp, (I32)kl, newSVsv(*v), 0);
Branch: 1 branch
BranchTrueFalseTotal
if (kl) (void)hv_store(h, kp, (I32)kl, newSVsv(*v), 0); 39 0 39
154 }
155 }
156 13 return sv_2mortal(newRV_noinc((SV *)h));
157 }
158  
159 7 static SV *oa_obj_from_kv(pTHX_ SV *listrv) {
160 7 AV *av = (AV *)SvRV(listrv);
161 7 HV *h = newHV();
162 7 SSize_t i, n = av_len(av) + 1;
163 28 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 21 7 28
164 21 SV **e = av_fetch(av, i, 0);
165 STRLEN el; const char *ep, *eq;
166 21 ▶ if (!e || !*e) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !*e) continue; 21 0 21
if (!e || !*e) continue; 0 21 21
167 21 ep = SvPV_const(*e, el);
168 21 eq = (const char *)memchr(ep, '=', el);
169 21 ▶ if (!eq || eq == ep) continue; /* no key, or no `=`: skip */
Branch: 2 branches
BranchTrueFalseTotal
if (!eq || eq == ep) continue; /* no key, or no `=`: skip */ 21 0 21
if (!eq || eq == ep) continue; /* no key, or no `=`: skip */ 0 21 21
170 21 (void)hv_store(h, ep, (I32)(eq - ep),
171 newSVpvn(eq + 1, el - (STRLEN)(eq - ep) - 1), 0);
172 }
173 7 return sv_2mortal(newRV_noinc((SV *)h));
174 }
175  
176 /* A path parameter, per its style. The style's marker is stripped whether or
177 * not the schema wants a list - `label` on a scalar is `.5` and `matrix` is
178 * `;id=5`, so handling the marker only for arrays would leave scalars holding
179 * it. Returns a mortal: a decoded scalar, or an arrayref of decoded pieces. */
180 2721 static SV *oa_style_path(pTHX_ oa_param *pp, const char *vp, STRLEN vl) {
181 2721 char delim = ',';
182 2721 STRLEN nl = 0;
183 2721 const char *np = NULL;
184  
185 2721 ▶ if (pp->style == OA_ST_LABEL) {
Branch: 1 branch
BranchTrueFalseTotal
if (pp->style == OA_ST_LABEL) { 18 2703 2721
186 18 ▶ if (vl && vp[0] == '.') { vp++; vl--; }
Branch: 2 branches
BranchTrueFalseTotal
if (vl && vp[0] == '.') { vp++; vl--; } 18 0 18
if (vl && vp[0] == '.') { vp++; vl--; } 18 0 18
187 18 ▶ if (pp->explode) delim = '.';
Branch: 1 branch
BranchTrueFalseTotal
if (pp->explode) delim = '.'; 9 9 18
188 }
189 2703 ▶ else if (pp->style == OA_ST_MATRIX) {
Branch: 1 branch
BranchTrueFalseTotal
else if (pp->style == OA_ST_MATRIX) { 18 2685 2703
190 18 np = SvPV_const(pp->name, nl);
191 18 ▶ if (vl && vp[0] == ';') { vp++; vl--; }
Branch: 2 branches
BranchTrueFalseTotal
if (vl && vp[0] == ';') { vp++; vl--; } 18 0 18
if (vl && vp[0] == ';') { vp++; vl--; } 18 0 18
192 18 ▶ if (pp->explode) delim = ';';
Branch: 1 branch
BranchTrueFalseTotal
if (pp->explode) delim = ';'; 9 9 18
193 /* a leading `name=` is the style speaking, not the value */
194 18 ▶ if (vl > nl && memEQ(vp, np, nl) && vp[nl] == '=') {
Branch: 3 branches
BranchTrueFalseTotal
if (vl > nl && memEQ(vp, np, nl) && vp[nl] == '=') { 16 2 18
if (vl > nl && memEQ(vp, np, nl) && vp[nl] == '=') { 14 2 16
if (vl > nl && memEQ(vp, np, nl) && vp[nl] == '=') { 14 0 14
195 14 vp += nl + 1; vl -= nl + 1;
196 }
197 /* `;color` with nothing after it is the EMPTY value, not the literal
198 * string "color" - the name is the style speaking here too, and with
199 * no `=` the strip above does not fire */
200 4 ▶ else if (vl == nl && memEQ(vp, np, nl)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (vl == nl && memEQ(vp, np, nl)) { 2 2 4
else if (vl == nl && memEQ(vp, np, nl)) { 2 0 2
201 2 vp += nl; vl = 0;
202 }
203 }
204  
205 /* An object is assembled from the same pieces an array would be, so the
206 * split is shared - only what is built from it differs. Exploded matrix is
207 * the one shape whose pieces still carry the parameter name, and for an
208 * object they do not: `;R=100;G=200` names the MEMBERS, so the name strip
209 * below is an array-only concern. */
210 2721 ▶ if (pp->is_object && !pp->is_array) {
Branch: 2 branches
BranchTrueFalseTotal
if (pp->is_object && !pp->is_array) { 12 2709 2721
if (pp->is_object && !pp->is_array) { 12 0 12
211 12 SV *rv = sv_2mortal(oa_split(aTHX_ vp, vl, delim, 1, 0));
212 6 return pp->explode ? oa_obj_from_kv(aTHX_ rv)
213 18 ▶ : oa_obj_from_pairs(aTHX_ rv);
Branch: 1 branch
BranchTrueFalseTotal
: oa_obj_from_pairs(aTHX_ rv); 6 6 12
214 }
215  
216 2709 ▶ if (!pp->is_array)
Branch: 1 branch
BranchTrueFalseTotal
if (!pp->is_array) 2680 29 2709
217 2680 return sv_2mortal(oa_pct_decode(aTHX_ vp, vl, 0));
218  
219 {
220 /* a delimiter the style cannot spell is matched in its wire
221 * spellings, and each piece may be a list of its own */
222 29 ▶ SV *rv = sv_2mortal(pp->delim
Branch: 1 branch
BranchTrueFalseTotal
SV *rv = sv_2mortal(pp->delim 7 22 29
223 ? oa_split_tokens(aTHX_ vp, vl, oa_delim_tokens(pp->delim, 0), 0)
224 : oa_split(aTHX_ vp, vl, delim, 1, 0));
225 29 oa_split_nested(aTHX_ pp, rv, 0);
226 /* exploded matrix repeats the name: `;id=a;id=b` */
227 29 ▶ if (pp->style == OA_ST_MATRIX && pp->explode && np) {
Branch: 3 branches
BranchTrueFalseTotal
if (pp->style == OA_ST_MATRIX && pp->explode && np) { 7 22 29
if (pp->style == OA_ST_MATRIX && pp->explode && np) { 4 3 7
if (pp->style == OA_ST_MATRIX && pp->explode && np) { 4 0 4
228 4 AV *av = (AV *)SvRV(rv);
229 4 SSize_t i, n = av_len(av) + 1;
230 14 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 10 4 14
231 10 SV **e = av_fetch(av, i, 0);
232 10 ▶ if (e && *e) {
Branch: 2 branches
BranchTrueFalseTotal
if (e && *e) { 10 0 10
if (e && *e) { 10 0 10
233 10 STRLEN el; const char *ep = SvPV_const(*e, el);
234 10 ▶ if (el > nl && memEQ(ep, np, nl) && ep[nl] == '=')
Branch: 3 branches
BranchTrueFalseTotal
if (el > nl && memEQ(ep, np, nl) && ep[nl] == '=') 6 4 10
if (el > nl && memEQ(ep, np, nl) && ep[nl] == '=') 6 0 6
if (el > nl && memEQ(ep, np, nl) && ep[nl] == '=') 6 0 6
235 6 sv_setpvn(*e, ep + nl + 1, el - nl - 1);
236 }
237 }
238 }
239 29 return rv;
240 }
241 }
242  
243 /* ---- error helpers --------------------------------------------------------- */
244  
245 12 static const char *oa_loc_name(int loc) {
246 static const char *n[OA_IN_N] = { "path", "query", "header", "cookie" };
247 12 return n[loc];
248 }
249  
250 33 static void oa_err_push(pTHX_ AV *errs, const char *in, SV *name,
251 const char *keyword, const char *msg) {
252 33 HV *e = newHV();
253 33 (void)hv_stores(e, "in", newSVpv(in, 0));
254 33 ▶ if (name) (void)hv_stores(e, "name", newSVsv(name));
Branch: 1 branch
BranchTrueFalseTotal
if (name) (void)hv_stores(e, "name", newSVsv(name)); 20 13 33
255 33 (void)hv_stores(e, "keyword", newSVpv(keyword, 0));
256 33 (void)hv_stores(e, "message", newSVpv(msg, 0));
257 33 av_push(errs, newRV_noinc((SV *)e));
258 33 }
259  
260 /* run one value through a handle; on failure augment the new JSF errors */
261 10629 static int oa_check(pTHX_ SV *handle, SV *value, AV *errs,
262 const char *in, SV *name) {
263 10629 ▶ if (!handle) return 1; /* no schema: anything goes */
Branch: 1 branch
BranchTrueFalseTotal
if (!handle) return 1; /* no schema: anything goes */ 0 10629 10629
264 10629 ▶ if (!errs) return JSF->is_valid(aTHX_ handle, value);
Branch: 1 branch
BranchTrueFalseTotal
if (!errs) return JSF->is_valid(aTHX_ handle, value); 0 10629 10629
265 {
266 10629 SSize_t before = av_len(errs);
267 10629 int ok = JSF->validate(aTHX_ handle, value, errs);
268 10629 ▶ if (!ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { 1742 8887 10629
269 1742 SSize_t i, after = av_len(errs);
270 3700 ▶ for (i = before + 1; i <= after; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = before + 1; i <= after; i++) { 1958 1742 3700
271 1958 SV **e = av_fetch(errs, i, 0);
272 1958 ▶ HV *h = (e && *e && SvROK(*e)) ? (HV *)SvRV(*e) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
HV *h = (e && *e && SvROK(*e)) ? (HV *)SvRV(*e) : NULL; 1958 0 1958
HV *h = (e && *e && SvROK(*e)) ? (HV *)SvRV(*e) : NULL; 1958 0 1958
HV *h = (e && *e && SvROK(*e)) ? (HV *)SvRV(*e) : NULL; 1958 0 1958
273 1958 ▶ if (h) {
Branch: 1 branch
BranchTrueFalseTotal
if (h) { 1958 0 1958
274 1958 (void)hv_stores(h, "in", newSVpv(in, 0));
275 1958 ▶ if (name) (void)hv_stores(h, "name", newSVsv(name));
Branch: 1 branch
BranchTrueFalseTotal
if (name) (void)hv_stores(h, "name", newSVsv(name)); 548 1410 1958
276 }
277 }
278 }
279 10629 return ok;
280 }
281 }
282  
283 static SV *oa_body_decode(pTHX_ SV *text); /* defined below */
284  
285 /* One declared parameter against its value.
286 *
287 * The ordinary case is a straight schema check. A parameter declared with
288 * `content` rather than `schema` carries a document of that media type in a
289 * single value - `?filter={"a":1}` - so the value is decoded first and the
290 * schema then sees the structure it was written about. An undecodable value is
291 * a 400 rather than a silent pass, exactly as a request body is; a media type
292 * with no decoder is left alone, as an undeclared body type is. */
293 7912 static int oa_check_param(pTHX_ oa_param *pp, SV *value, AV *errs,
294 const char *in) {
295 7912 SV *data = value;
296 7912 ▶ if (!pp->handle) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!pp->handle) return 1; 1 7911 7912
297 7911 ▶ if (pp->ctype) {
Branch: 1 branch
BranchTrueFalseTotal
if (pp->ctype) { 524 7387 7911
298 524 STRLEN cl; const char *cp = SvPV_const(pp->ctype, cl);
299 525 ▶ if (!oa_ctype_is_json(cp, cl)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!oa_ctype_is_json(cp, cl)) return 1; 0 524 524
300 524 ▶ if (!SvROK(value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(value)) { 524 0 524
301 524 data = oa_body_decode(aTHX_ value);
302 524 ▶ if (!data) {
Branch: 1 branch
BranchTrueFalseTotal
if (!data) { 1 523 524
303 1 ▶ if (errs) oa_err_push(aTHX_ errs, in, pp->name, "content",
Branch: 1 branch
BranchTrueFalseTotal
if (errs) oa_err_push(aTHX_ errs, in, pp->name, "content", 1 0 1
304 "parameter is not valid JSON");
305 1 return 0;
306 }
307 }
308 }
309 7910 return oa_check(aTHX_ pp->handle, data, errs, in, pp->name);
310 }
311  
312 /* ---- query / cookie parsing ------------------------------------------------ */
313  
314 /* Parse a query string into a fresh HV: every key decoded; a key is an
315 * arrayref when the op declares that query parameter as array-typed (repeat
316 * keys accumulate), else last value wins. Undeclared keys are kept (string). */
317 1725 static HV *oa_parse_query(pTHX_ oa_op *o, const char *qs, STRLEN ql) {
318 /* a space reaches us only encoded, so both spellings are the delimiter */
319 static const char *const tok_space[] = { "%20", "+", " ", NULL };
320 static const char *const tok_pipe[] = { "%7C", "%7c", "|", NULL };
321 1725 HV *out = newHV();
322 1725 STRLEN s = 0;
323 6370 ▶ while (s < ql) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < ql) { 4645 1725 6370
324 4645 STRLEN e = s, eq;
325 SV *k, *v;
326 int i;
327 4645 oa_param *pp = NULL;
328 const char *rawv;
329 STRLEN rawvl;
330  
331 42591 ▶ while (e < ql && qs[e] != '&') e++;
Branch: 2 branches
BranchTrueFalseTotal
while (e < ql && qs[e] != '&') e++; 41475 1116 42591
while (e < ql && qs[e] != '&') e++; 37946 3529 41475
332 4645 eq = s;
333 20612 ▶ while (eq < e && qs[eq] != '=') eq++;
Branch: 2 branches
BranchTrueFalseTotal
while (eq < e && qs[eq] != '=') eq++; 20612 0 20612
while (eq < e && qs[eq] != '=') eq++; 15967 4645 20612
334 4645 ▶ if (eq > s) {
Branch: 1 branch
BranchTrueFalseTotal
if (eq > s) { 4645 0 4645
335 4645 k = sv_2mortal(oa_pct_decode(aTHX_ qs + s, eq - s, 1));
336 /* the RAW value: the declared parameter is found first, because
337 * a style has to split before anything is decoded */
338 4645 ▶ rawv = qs + (eq < e ? eq + 1 : e);
Branch: 1 branch
BranchTrueFalseTotal
rawv = qs + (eq < e ? eq + 1 : e); 4645 0 4645
339 4645 ▶ rawvl = eq < e ? e - eq - 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
rawvl = eq < e ? e - eq - 1 : 0; 4645 0 4645
340  
341 16185 ▶ for (i = 0; i < o->nparams[OA_IN_QUERY]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < o->nparams[OA_IN_QUERY]; i++) { 16164 21 16185
342 16164 ▶ if (sv_eq(o->params[OA_IN_QUERY][i].name, k)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_eq(o->params[OA_IN_QUERY][i].name, k)) { 4624 11540 16164
343 4624 pp = &o->params[OA_IN_QUERY][i];
344 4624 break;
345 }
346 }
347  
348 /* deepObject: `v[k]=x` is a MEMBER of the parameter named `v`, so
349 * the key on the wire is not the parameter's name and the lookup
350 * above can never find it. It also accumulates into a hash rather
351 * than replacing a value, so it is settled here, before the
352 * ordinary store. Object-typed, so the is_array gate that guards
353 * every other style does not apply. */
354 4645 ▶ if (!pp) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pp) { 21 4624 4645
355 21 STRLEN dkl; const char *dkp = SvPV_const(k, dkl);
356 14 ▶ const char *br = (dkl > 2 && dkp[dkl - 1] == ']')
Branch: 1 branch
BranchTrueFalseTotal
const char *br = (dkl > 2 && dkp[dkl - 1] == ']') 12 2 14
357 35 ▶ ? (const char *)memchr(dkp, '[', dkl) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (const char *)memchr(dkp, '[', dkl) : NULL; 14 7 21
358 21 int deep = 0;
359 21 ▶ if (br && br > dkp) {
Branch: 2 branches
BranchTrueFalseTotal
if (br && br > dkp) { 12 9 21
if (br && br > dkp) { 12 0 12
360 12 STRLEN bl = (STRLEN)(br - dkp); /* the name */
361 12 STRLEN sbl = dkl - bl - 2; /* inside [ ] */
362 13 ▶ for (i = 0; i < o->nparams[OA_IN_QUERY]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < o->nparams[OA_IN_QUERY]; i++) { 12 1 13
363 12 oa_param *c = &o->params[OA_IN_QUERY][i];
364 12 STRLEN cnl; const char *cnp = SvPV_const(c->name, cnl);
365 SV **cur;
366 HV *bag;
367 12 ▶ if (c->style != OA_ST_DEEP) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (c->style != OA_ST_DEEP) continue; 1 11 12
368 11 ▶ if (cnl != bl || memNE(cnp, dkp, bl)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (cnl != bl || memNE(cnp, dkp, bl)) continue; 11 0 11
if (cnl != bl || memNE(cnp, dkp, bl)) continue; 0 11 11
369 11 cur = hv_fetch(out, cnp, (I32)cnl, 0);
370 11 ▶ if (cur && *cur && SvROK(*cur)
Branch: 3 branches
BranchTrueFalseTotal
if (cur && *cur && SvROK(*cur) 6 5 11
if (cur && *cur && SvROK(*cur) 6 0 6
if (cur && *cur && SvROK(*cur) 6 0 6
371 6 ▶ && SvTYPE(SvRV(*cur)) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(*cur)) == SVt_PVHV) { 6 0 6
372 6 bag = (HV *)SvRV(*cur);
373 } else {
374 5 bag = newHV();
375 5 (void)hv_store(out, cnp, (I32)cnl,
376 newRV_noinc((SV *)bag), 0);
377 }
378 11 (void)hv_store(bag, br + 1, (I32)sbl,
379 oa_pct_decode(aTHX_ rawv, rawvl, 1), 0);
380 11 deep = 1;
381 11 break;
382 }
383 }
384 /* form + explode on an object spreads the MEMBERS as top-level
385 * keys: `R=100&G=200&B=150`. Nothing on the wire ties them to
386 * the parameter, so the declared property names are the only
387 * link - which is why they are compiled in. A key matching no
388 * declared property falls through and is stored as itself. */
389 21 ▶ if (!deep) {
Branch: 1 branch
BranchTrueFalseTotal
if (!deep) { 10 11 21
390 15 ▶ for (i = 0; i < o->nparams[OA_IN_QUERY]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < o->nparams[OA_IN_QUERY]; i++) { 12 3 15
391 12 oa_param *c = &o->params[OA_IN_QUERY][i];
392 AV *names;
393 SSize_t pi, pn;
394 STRLEN cnl; const char *cnp;
395 12 ▶ if (!c->is_object || c->is_array) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!c->is_object || c->is_array) continue; 7 5 12
if (!c->is_object || c->is_array) continue; 0 7 7
396 7 ▶ if (c->style != OA_ST_FORM || !c->explode) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (c->style != OA_ST_FORM || !c->explode) continue; 7 0 7
if (c->style != OA_ST_FORM || !c->explode) continue; 0 7 7
397 7 ▶ if (!c->props) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!c->props) continue; 0 7 7
398 7 names = (AV *)SvRV(c->props);
399 7 pn = av_len(names) + 1;
400 13 ▶ for (pi = 0; pi < pn; pi++) {
Branch: 1 branch
BranchTrueFalseTotal
for (pi = 0; pi < pn; pi++) { 13 0 13
401 13 SV **nm = av_fetch(names, pi, 0);
402 13 ▶ if (nm && *nm && sv_eq(*nm, k)) break;
Branch: 3 branches
BranchTrueFalseTotal
if (nm && *nm && sv_eq(*nm, k)) break; 13 0 13
if (nm && *nm && sv_eq(*nm, k)) break; 13 0 13
if (nm && *nm && sv_eq(*nm, k)) break; 7 6 13
403 }
404 7 ▶ if (pi == pn) continue; /* not a member */
Branch: 1 branch
BranchTrueFalseTotal
if (pi == pn) continue; /* not a member */ 0 7 7
405 7 cnp = SvPV_const(c->name, cnl);
406 {
407 7 SV **cur = hv_fetch(out, cnp, (I32)cnl, 0);
408 HV *bag;
409 7 ▶ if (cur && *cur && SvROK(*cur)
Branch: 3 branches
BranchTrueFalseTotal
if (cur && *cur && SvROK(*cur) 4 3 7
if (cur && *cur && SvROK(*cur) 4 0 4
if (cur && *cur && SvROK(*cur) 4 0 4
410 4 ▶ && SvTYPE(SvRV(*cur)) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(*cur)) == SVt_PVHV) { 4 0 4
411 4 bag = (HV *)SvRV(*cur);
412 } else {
413 3 bag = newHV();
414 3 (void)hv_store(out, cnp, (I32)cnl,
415 newRV_noinc((SV *)bag), 0);
416 }
417 {
418 7 STRLEN mkl; const char *mkp = SvPV_const(k, mkl);
419 7 (void)hv_store(bag, mkp, (I32)mkl,
420 oa_pct_decode(aTHX_ rawv, rawvl, 1), 0);
421 }
422 7 deep = 1;
423 }
424 7 break;
425 }
426 }
427 21 ▶ if (deep) { s = e + 1; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (deep) { s = e + 1; continue; } 18 3 21
428 }
429 {
430 4627 STRLEN kl; const char *kp = SvPV_const(k, kl);
431 4627 int delimited = 0;
432  
433 /* an OBJECT arriving in one value: the same split as the list
434 * form, read as key,value pairs rather than as elements */
435 4627 ▶ if (pp && pp->is_object && !pp->is_array) {
Branch: 3 branches
BranchTrueFalseTotal
if (pp && pp->is_object && !pp->is_array) { 4624 3 4627
if (pp && pp->is_object && !pp->is_array) { 6 4618 4624
if (pp && pp->is_object && !pp->is_array) { 6 0 6
436 6 SV *pieces = NULL;
437 6 ▶ if (pp->style == OA_ST_SPACE)
Branch: 1 branch
BranchTrueFalseTotal
if (pp->style == OA_ST_SPACE) 2 4 6
438 2 pieces = oa_split_tokens(aTHX_ rawv, rawvl, tok_space, 1);
439 4 ▶ else if (pp->style == OA_ST_PIPE)
Branch: 1 branch
BranchTrueFalseTotal
else if (pp->style == OA_ST_PIPE) 2 2 4
440 2 pieces = oa_split_tokens(aTHX_ rawv, rawvl, tok_pipe, 1);
441 2 ▶ else if (pp->style == OA_ST_FORM && !pp->explode)
Branch: 2 branches
BranchTrueFalseTotal
else if (pp->style == OA_ST_FORM && !pp->explode) 2 0 2
else if (pp->style == OA_ST_FORM && !pp->explode) 2 0 2
442 2 pieces = oa_split(aTHX_ rawv, rawvl, ',', 1, 1);
443 6 ▶ if (pieces) {
Branch: 1 branch
BranchTrueFalseTotal
if (pieces) { 6 0 6
444 6 SV *obj = oa_obj_from_pairs(aTHX_ sv_2mortal(pieces));
445 6 (void)hv_store(out, kp, (I32)kl, SvREFCNT_inc(obj), 0);
446 6 s = e + 1;
447 2547 continue;
448 }
449 }
450  
451 /* a style whose list arrives in ONE value */
452 4621 ▶ if (pp && pp->is_array) {
Branch: 2 branches
BranchTrueFalseTotal
if (pp && pp->is_array) { 4618 3 4621
if (pp && pp->is_array) { 3561 1057 4618
453 3561 ▶ if (pp->delim) {
Branch: 1 branch
BranchTrueFalseTotal
if (pp->delim) { 503 3058 3561
454 503 v = oa_split_tokens(aTHX_ rawv, rawvl,
455 503 oa_delim_tokens(pp->delim, 1), 1);
456 503 delimited = 1;
457 }
458 3058 ▶ else if (pp->style == OA_ST_SPACE) {
Branch: 1 branch
BranchTrueFalseTotal
else if (pp->style == OA_ST_SPACE) { 507 2551 3058
459 507 v = oa_split_tokens(aTHX_ rawv, rawvl, tok_space, 1);
460 507 delimited = 1;
461 }
462 2551 ▶ else if (pp->style == OA_ST_PIPE) {
Branch: 1 branch
BranchTrueFalseTotal
else if (pp->style == OA_ST_PIPE) { 507 2044 2551
463 507 v = oa_split_tokens(aTHX_ rawv, rawvl, tok_pipe, 1);
464 507 delimited = 1;
465 }
466 2044 ▶ else if (pp->style == OA_ST_FORM && !pp->explode) {
Branch: 2 branches
BranchTrueFalseTotal
else if (pp->style == OA_ST_FORM && !pp->explode) { 2044 0 2044
else if (pp->style == OA_ST_FORM && !pp->explode) { 1024 1020 2044
467 1024 v = oa_split(aTHX_ rawv, rawvl, ',', 1, 1);
468 1024 delimited = 1;
469 }
470 }
471 4621 ▶ if (delimited) {
Branch: 1 branch
BranchTrueFalseTotal
if (delimited) { 2541 2080 4621
472 2541 oa_split_nested(aTHX_ pp, v, 0);
473 2541 (void)hv_store(out, kp, (I32)kl, v, 0);
474 2541 s = e + 1;
475 2541 continue;
476 }
477  
478 2080 v = oa_pct_decode(aTHX_ rawv, rawvl, 1);
479  
480 /* form + explode (the default): repeat keys accumulate.
481 * Unchanged, and it is what every document that declares no
482 * style at all goes through. */
483 3100 ▶ if (pp && pp->is_array) {
Branch: 2 branches
BranchTrueFalseTotal
if (pp && pp->is_array) { 2077 3 2080
if (pp && pp->is_array) { 1020 1057 2077
484 1020 SV **cur = hv_fetch(out, kp, (I32)kl, 0);
485 AV *av;
486 1020 ▶ if (cur && *cur && SvROK(*cur)
Branch: 3 branches
BranchTrueFalseTotal
if (cur && *cur && SvROK(*cur) 512 508 1020
if (cur && *cur && SvROK(*cur) 512 0 512
if (cur && *cur && SvROK(*cur) 512 0 512
487 512 ▶ && SvTYPE(SvRV(*cur)) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(*cur)) == SVt_PVAV) { 512 0 512
488 512 av = (AV *)SvRV(*cur);
489 } else {
490 508 av = newAV();
491 508 (void)hv_store(out, kp, (I32)kl,
492 newRV_noinc((SV *)av), 0);
493 }
494 1020 ▶ if (pp->ndepth) { /* each repeated value is itself a list */
Branch: 1 branch
BranchTrueFalseTotal
if (pp->ndepth) { /* each repeated value is itself a list */ 0 1020 1020
495 0 STRLEN vl2; const char *vp2 = SvPV_const(v, vl2);
496 0 SV *sub = oa_split(aTHX_ vp2, vl2, pp->idelim[0], 0, 0);
497 0 oa_split_nested(aTHX_ pp, sub, 1);
498 0 SvREFCNT_dec(v);
499 0 v = sub;
500 }
501 1020 av_push(av, v);
502 } else {
503 1060 (void)hv_store(out, kp, (I32)kl, v, 0);
504 }
505 }
506 }
507 2080 s = e + 1;
508 }
509 1725 return out;
510 }
511  
512 /* Parse a Cookie header ("a=b; c=d") into a fresh HV (raw values). */
513 17 static HV *oa_parse_cookies(pTHX_ const char *p, STRLEN l) {
514 17 HV *out = newHV();
515 17 STRLEN s = 0;
516 36 ▶ while (s < l) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < l) { 19 17 36
517 19 STRLEN e = s, eq;
518 181 ▶ while (e < l && p[e] != ';') e++;
Branch: 2 branches
BranchTrueFalseTotal
while (e < l && p[e] != ';') e++; 164 17 181
while (e < l && p[e] != ';') e++; 162 2 164
519 21 ▶ while (s < e && isSPACE((U8)p[s])) s++;
Branch: 2 branches
BranchTrueFalseTotal
while (s < e && isSPACE((U8)p[s])) s++; 21 0 21
while (s < e && isSPACE((U8)p[s])) s++; 2 19 21
520 19 eq = s;
521 116 ▶ while (eq < e && p[eq] != '=') eq++;
Branch: 2 branches
BranchTrueFalseTotal
while (eq < e && p[eq] != '=') eq++; 116 0 116
while (eq < e && p[eq] != '=') eq++; 97 19 116
522 19 ▶ if (eq > s && eq < e) {
Branch: 2 branches
BranchTrueFalseTotal
if (eq > s && eq < e) { 19 0 19
if (eq > s && eq < e) { 19 0 19
523 19 STRLEN ve = e;
524 19 ▶ while (ve > eq + 1 && isSPACE((U8)p[ve - 1])) ve--;
Branch: 2 branches
BranchTrueFalseTotal
while (ve > eq + 1 && isSPACE((U8)p[ve - 1])) ve--; 19 0 19
while (ve > eq + 1 && isSPACE((U8)p[ve - 1])) ve--; 0 19 19
525 19 (void)hv_store(out, p + s, (I32)(eq - s),
526 newSVpvn(p + eq + 1, ve - eq - 1), 0);
527 }
528 19 s = e + 1;
529 }
530 17 return out;
531 }
532  
533 /* ---- body ------------------------------------------------------------------- */
534  
535 /* The declared entry covering this Content-Type, most specific first: an
536 * exact type beats a type-level range, which beats the catch-all range.
537 * Matching is case-insensitive per RFC 7231 and ignores parameters; NULL when
538 * nothing declared covers the type. */
539 2729 static oa_body *oa_body_for(pTHX_ oa_op *o, SV *ctype_hdr) {
540 STRLEN l;
541 const char *p;
542 int i;
543 2729 oa_body *star = NULL, *type_star = NULL;
544 2729 ▶ if (!o->nbodies) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!o->nbodies) return NULL; 0 2729 2729
545 2729 ▶ if (!ctype_hdr || !SvOK(ctype_hdr)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!ctype_hdr || !SvOK(ctype_hdr)) return NULL; 2728 1 2729
if (!ctype_hdr || !SvOK(ctype_hdr)) return NULL; 0 2728 2728
546 2728 p = SvPV_const(ctype_hdr, l);
547 2743 ▶ for (i = 0; i < o->nbodies; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < o->nbodies; i++) { 2734 9 2743
548 2734 STRLEN cl; const char *cp = SvPV_const(o->bodies[i].ctype, cl);
549 STRLEN cb, ce;
550 2734 oa_ctype_span(cp, cl, &cb, &ce);
551 2734 ▶ if (ce - cb == 3 && memEQ(cp + cb, "*/*", 3)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ce - cb == 3 && memEQ(cp + cb, "*/*", 3)) { 3 2731 2734
if (ce - cb == 3 && memEQ(cp + cb, "*/*", 3)) { 3 0 3
552 3 ▶ if (!star) star = &o->bodies[i];
Branch: 1 branch
BranchTrueFalseTotal
if (!star) star = &o->bodies[i]; 3 0 3
553 3 continue;
554 }
555 2731 ▶ if ((ce - cb) > 2 && cp[ce - 1] == '*' && cp[ce - 2] == '/') {
Branch: 3 branches
BranchTrueFalseTotal
if ((ce - cb) > 2 && cp[ce - 1] == '*' && cp[ce - 2] == '/') { 2731 0 2731
if ((ce - cb) > 2 && cp[ce - 1] == '*' && cp[ce - 2] == '/') { 0 2731 2731
if ((ce - cb) > 2 && cp[ce - 1] == '*' && cp[ce - 2] == '/') { 0 0 0
556 0 ▶ if (!type_star && oa_ctype_matches(cp, cl, p, l))
Branch: 2 branches
BranchTrueFalseTotal
if (!type_star && oa_ctype_matches(cp, cl, p, l)) 0 0 0
if (!type_star && oa_ctype_matches(cp, cl, p, l)) 0 0 0
557 0 type_star = &o->bodies[i];
558 0 continue;
559 }
560 2731 ▶ if (oa_ctype_matches(cp, cl, p, l)) return &o->bodies[i];
Branch: 1 branch
BranchTrueFalseTotal
if (oa_ctype_matches(cp, cl, p, l)) return &o->bodies[i]; 2719 12 2731
561 }
562 9 ▶ return type_star ? type_star : star;
Branch: 1 branch
BranchTrueFalseTotal
return type_star ? type_star : star; 0 9 9
563 }
564  
565 /* ---- form bodies ------------------------------------------------------------
566 *
567 * Neither of these goes through the frj ABI: a form body is not JSON. They
568 * build the same shape a decoded JSON object would have, so one schema
569 * validates either spelling.
570 *
571 * A repeat key becomes an arrayref, because a form carries repeats naturally
572 * (checkboxes, multi-selects) and a schema saying `type: array` must see one. */
573 60 static void oa_form_add(pTHX_ HV *out, SV *key, SV *val) {
574 60 STRLEN kl; const char *kp = SvPV_const(key, kl);
575 60 SV **cur = hv_fetch(out, kp, (I32)kl, 0);
576 60 ▶ if (cur && *cur) {
Branch: 2 branches
BranchTrueFalseTotal
if (cur && *cur) { 6 54 60
if (cur && *cur) { 6 0 6
577 AV *list;
578 6 ▶ if (SvROK(*cur) && SvTYPE(SvRV(*cur)) == SVt_PVAV) list = (AV *)SvRV(*cur);
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(*cur) && SvTYPE(SvRV(*cur)) == SVt_PVAV) list = (AV *)SvRV(*cur); 2 4 6
if (SvROK(*cur) && SvTYPE(SvRV(*cur)) == SVt_PVAV) list = (AV *)SvRV(*cur); 2 0 2
579 else {
580 4 list = newAV();
581 4 av_push(list, newSVsv(*cur));
582 4 (void)hv_store(out, kp, (I32)kl, newRV_noinc((SV *)list), 0);
583 }
584 6 av_push(list, val);
585 6 return;
586 }
587 54 (void)hv_store(out, kp, (I32)kl, val, 0);
588 }
589  
590 /* application/x-www-form-urlencoded. Mortal hashref; never NULL - an empty
591 * body is an empty object, which a schema is entitled to reject. */
592 36 static SV *oa_form_decode(pTHX_ SV *text, SV *encoding) {
593 36 STRLEN l; const char *p = SvPV_const(text, l);
594 36 HV *out = newHV();
595 19 ▶ HV *enc = (encoding && SvROK(encoding)
Branch: 1 branch
BranchTrueFalseTotal
HV *enc = (encoding && SvROK(encoding) 19 0 19
596 19 ▶ && SvTYPE(SvRV(encoding)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(encoding)) == SVt_PVHV) 19 0 19
597 55 ▶ ? (HV *)SvRV(encoding) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(encoding) : NULL; 19 17 36
598 36 STRLEN s = 0;
599 81 ▶ while (s < l) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < l) { 45 36 81
600 45 STRLEN e = s, eq;
601 473 ▶ while (e < l && p[e] != '&') e++;
Branch: 2 branches
BranchTrueFalseTotal
while (e < l && p[e] != '&') e++; 437 36 473
while (e < l && p[e] != '&') e++; 428 9 437
602 45 eq = s;
603 158 ▶ while (eq < e && p[eq] != '=') eq++;
Branch: 2 branches
BranchTrueFalseTotal
while (eq < e && p[eq] != '=') eq++; 158 0 158
while (eq < e && p[eq] != '=') eq++; 113 45 158
604 45 ▶ if (eq > s) {
Branch: 1 branch
BranchTrueFalseTotal
if (eq > s) { 45 0 45
605 45 SV *k = sv_2mortal(oa_pct_decode(aTHX_ p + s, eq - s, 1));
606 45 ▶ const char *rawv = p + (eq < e ? eq + 1 : e);
Branch: 1 branch
BranchTrueFalseTotal
const char *rawv = p + (eq < e ? eq + 1 : e); 45 0 45
607 45 ▶ STRLEN rawvl = eq < e ? e - eq - 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN rawvl = eq < e ? e - eq - 1 : 0; 45 0 45
608 45 SV *v = NULL;
609 /* An Encoding Object may say this property is a delimited list.
610 * The split is on the RAW span, BEFORE decoding, so an escaped
611 * delimiter stays inside its element - `a=x%2Cy` is one value
612 * containing a comma, not two values. Same asymmetry the query
613 * styles turn on, and the reason the encoding has to reach the
614 * decoder rather than being applied to its output. */
615 45 ▶ if (enc) {
Branch: 1 branch
BranchTrueFalseTotal
if (enc) { 23 22 45
616 23 STRLEN kl2; const char *kp2 = SvPV_const(k, kl2);
617 23 SV **es = hv_fetch(enc, kp2, (I32)kl2, 0);
618 23 ▶ HV *eh = (es && *es) ? oa_hv_of(*es) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *eh = (es && *es) ? oa_hv_of(*es) : NULL; 23 0 23
HV *eh = (es && *es) ? oa_hv_of(*es) : NULL; 23 0 23
619 23 ▶ if (eh) {
Branch: 1 branch
BranchTrueFalseTotal
if (eh) { 23 0 23
620 23 SV *st = oa_get(aTHX_ eh, "style");
621 23 SV *ex = oa_get(aTHX_ eh, "explode");
622 23 STRLEN sl2 = 0;
623 42 ▶ const char *sp2 = st ? SvPV_const(st, sl2) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
const char *sp2 = st ? SvPV_const(st, sl2) : NULL; 19 4 23
624 /* `form` is the default style here, and it explodes by
625 * default - so only an explicit explode:false makes a
626 * form property a delimited list */
627 23 ▶ int exploded = ex ? (oa_sv_truthy(aTHX_ ex) ? 1 : 0) : 1;
Branch: 2 branches
BranchTrueFalseTotal
int exploded = ex ? (oa_sv_truthy(aTHX_ ex) ? 1 : 0) : 1; 15 8 23
int exploded = ex ? (oa_sv_truthy(aTHX_ ex) ? 1 : 0) : 1; 6 9 15
628 23 ▶ int isform = !sp2 || (sl2 == 4 && memEQ(sp2, "form", 4));
Branch: 3 branches
BranchTrueFalseTotal
int isform = !sp2 || (sl2 == 4 && memEQ(sp2, "form", 4)); 19 4 23
int isform = !sp2 || (sl2 == 4 && memEQ(sp2, "form", 4)); 15 4 19
int isform = !sp2 || (sl2 == 4 && memEQ(sp2, "form", 4)); 15 0 15
629 23 ▶ int isspace = sp2 && sl2 == 14 && memEQ(sp2, "spaceDelimited", 14);
Branch: 3 branches
BranchTrueFalseTotal
int isspace = sp2 && sl2 == 14 && memEQ(sp2, "spaceDelimited", 14); 19 4 23
int isspace = sp2 && sl2 == 14 && memEQ(sp2, "spaceDelimited", 14); 2 17 19
int isspace = sp2 && sl2 == 14 && memEQ(sp2, "spaceDelimited", 14); 2 0 2
630 23 ▶ int ispipe = sp2 && sl2 == 13 && memEQ(sp2, "pipeDelimited", 13);
Branch: 3 branches
BranchTrueFalseTotal
int ispipe = sp2 && sl2 == 13 && memEQ(sp2, "pipeDelimited", 13); 19 4 23
int ispipe = sp2 && sl2 == 13 && memEQ(sp2, "pipeDelimited", 13); 2 17 19
int ispipe = sp2 && sl2 == 13 && memEQ(sp2, "pipeDelimited", 13); 2 0 2
631 /* a tab-separated member (Swagger 2.0 tsv) has no 3.x
632 * style; it travels as the x-oa-collectionFormat extension */
633 23 int istab = oa_cf_delim(aTHX_ oa_get(aTHX_ eh, "x-oa-collectionFormat")) == '\t';
634 23 ▶ if (istab) {
Branch: 1 branch
BranchTrueFalseTotal
if (istab) { 2 21 23
635 static const char *const tok_tb[] = { "%09", "\t", NULL };
636 2 v = oa_split_tokens(aTHX_ rawv, rawvl, tok_tb, 1);
637 }
638 21 ▶ else if (isform && !exploded)
Branch: 2 branches
BranchTrueFalseTotal
else if (isform && !exploded) 17 4 21
else if (isform && !exploded) 7 10 17
639 7 v = oa_split(aTHX_ rawv, rawvl, ',', 1, 1);
640 14 ▶ else if (isspace) {
Branch: 1 branch
BranchTrueFalseTotal
else if (isspace) { 2 12 14
641 static const char *const tok_sp[] = { "%20", "+", " ", NULL };
642 2 v = oa_split_tokens(aTHX_ rawv, rawvl, tok_sp, 1);
643 }
644 12 ▶ else if (ispipe) {
Branch: 1 branch
BranchTrueFalseTotal
else if (ispipe) { 2 10 12
645 static const char *const tok_pp[] = { "%7C", "%7c", "|", NULL };
646 2 v = oa_split_tokens(aTHX_ rawv, rawvl, tok_pp, 1);
647 }
648 }
649 }
650 45 ▶ if (!v) v = oa_pct_decode(aTHX_ rawv, rawvl, 1);
Branch: 1 branch
BranchTrueFalseTotal
if (!v) v = oa_pct_decode(aTHX_ rawv, rawvl, 1); 32 13 45
651 45 oa_form_add(aTHX_ out, k, v);
652 }
653 45 s = e + 1;
654 }
655 36 return sv_2mortal(newRV_noinc((SV *)out));
656 }
657  
658 /* Apply an Encoding Object to a decoded form body. A property whose encoding
659 * names a JSON media type carries a whole document in one field - the
660 * `a={"n":"x"}` idiom - so it is decoded before the schema sees it, exactly
661 * as a parameter declared with `content` is. A field that is already a
662 * reference (a repeat promoted to an arrayref) is left alone, and a value
663 * that will not decode is left as text for the schema to refuse. */
664 23 static void oa_apply_encoding(pTHX_ SV *data, SV *encoding) {
665 HV *d, *e;
666 HE *he;
667 23 ▶ if (!data || !SvROK(data) || SvTYPE(SvRV(data)) != SVt_PVHV) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!data || !SvROK(data) || SvTYPE(SvRV(data)) != SVt_PVHV) return; 23 0 23
if (!data || !SvROK(data) || SvTYPE(SvRV(data)) != SVt_PVHV) return; 23 0 23
if (!data || !SvROK(data) || SvTYPE(SvRV(data)) != SVt_PVHV) return; 0 23 23
668 23 ▶ if (!encoding || !SvROK(encoding) || SvTYPE(SvRV(encoding)) != SVt_PVHV) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!encoding || !SvROK(encoding) || SvTYPE(SvRV(encoding)) != SVt_PVHV) return; 23 0 23
if (!encoding || !SvROK(encoding) || SvTYPE(SvRV(encoding)) != SVt_PVHV) return; 23 0 23
if (!encoding || !SvROK(encoding) || SvTYPE(SvRV(encoding)) != SVt_PVHV) return; 0 23 23
669 23 d = (HV *)SvRV(data);
670 23 e = (HV *)SvRV(encoding);
671 23 hv_iterinit(e);
672 46 ▶ while ((he = hv_iternext(e))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(e))) { 23 23 46
673 23 I32 kl; const char *k = hv_iterkey(he, &kl);
674 23 HV *spec = oa_hv_of(hv_iterval(e, he));
675 23 ▶ SV *ct = spec ? oa_get(aTHX_ spec, "contentType") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *ct = spec ? oa_get(aTHX_ spec, "contentType") : NULL; 23 0 23
676 SV **slot;
677 STRLEN cl; const char *cp;
678 23 ▶ if (!ct) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ct) continue; 20 3 23
679 3 cp = SvPV_const(ct, cl);
680 3 ▶ if (!oa_ctype_is_json(cp, cl)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!oa_ctype_is_json(cp, cl)) continue; 0 3 3
681 3 slot = hv_fetch(d, k, kl, 0);
682 3 ▶ if (slot && *slot && SvOK(*slot) && !SvROK(*slot)) {
Branch: 4 branches
BranchTrueFalseTotal
if (slot && *slot && SvOK(*slot) && !SvROK(*slot)) { 3 0 3
if (slot && *slot && SvOK(*slot) && !SvROK(*slot)) { 3 0 3
if (slot && *slot && SvOK(*slot) && !SvROK(*slot)) { 3 0 3
if (slot && *slot && SvOK(*slot) && !SvROK(*slot)) { 3 0 3
683 3 SV *dec = oa_body_decode(aTHX_ *slot);
684 3 ▶ if (dec) (void)hv_store(d, k, kl, newSVsv(dec), 0);
Branch: 1 branch
BranchTrueFalseTotal
if (dec) (void)hv_store(d, k, kl, newSVsv(dec), 0); 3 0 3
685 }
686 }
687 }
688  
689 /* An Encoding Object may declare `headers` for a multipart part - a map of
690 * Header Objects, exactly as a response declares them. A declared `required`
691 * header that the part does not carry is a bad request, and nothing else
692 * would report it: the part's headers are not part of the value the schema
693 * sees. Header names are compared case-insensitively, per RFC 7231. */
694 23 static int oa_check_encoding_headers(pTHX_ SV *encoding, HV *part_hdrs,
695 AV *errs) {
696 HV *e;
697 HE *he;
698 23 int ok = 1;
699 23 ▶ if (!encoding || !SvROK(encoding) || SvTYPE(SvRV(encoding)) != SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if (!encoding || !SvROK(encoding) || SvTYPE(SvRV(encoding)) != SVt_PVHV) 23 0 23
if (!encoding || !SvROK(encoding) || SvTYPE(SvRV(encoding)) != SVt_PVHV) 23 0 23
if (!encoding || !SvROK(encoding) || SvTYPE(SvRV(encoding)) != SVt_PVHV) 0 23 23
700 0 return 1;
701 23 ▶ if (!part_hdrs) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!part_hdrs) return 1; 19 4 23
702 4 e = (HV *)SvRV(encoding);
703 4 hv_iterinit(e);
704 8 ▶ while ((he = hv_iternext(e))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(e))) { 4 4 8
705 4 I32 pkl; const char *pk = hv_iterkey(he, &pkl);
706 4 HV *spec = oa_hv_of(hv_iterval(e, he));
707 4 ▶ HV *want = spec ? oa_hv_of(oa_get(aTHX_ spec, "headers")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *want = spec ? oa_hv_of(oa_get(aTHX_ spec, "headers")) : NULL; 4 0 4
708 4 SV **got = hv_fetch(part_hdrs, pk, pkl, 0);
709 4 ▶ HV *have = (got && *got) ? oa_hv_of(*got) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *have = (got && *got) ? oa_hv_of(*got) : NULL; 4 0 4
HV *have = (got && *got) ? oa_hv_of(*got) : NULL; 4 0 4
710 HE *whe;
711 4 ▶ if (!want) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!want) continue; 0 4 4
712 4 hv_iterinit(want);
713 8 ▶ while ((whe = hv_iternext(want))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((whe = hv_iternext(want))) { 4 4 8
714 4 I32 hkl; const char *hk = hv_iterkey(whe, &hkl);
715 4 HV *hspec = oa_hv_of(hv_iterval(want, whe));
716 4 ▶ SV *req = hspec ? oa_get(aTHX_ hspec, "required") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *req = hspec ? oa_get(aTHX_ hspec, "required") : NULL; 4 0 4
717 4 int found = 0;
718 4 ▶ if (!req || !oa_sv_truthy(aTHX_ req)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!req || !oa_sv_truthy(aTHX_ req)) continue; 4 0 4
if (!req || !oa_sv_truthy(aTHX_ req)) continue; 0 4 4
719 4 ▶ if (have) {
Branch: 1 branch
BranchTrueFalseTotal
if (have) { 4 0 4
720 HE *ghe;
721 4 hv_iterinit(have);
722 7 ▶ while ((ghe = hv_iternext(have))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((ghe = hv_iternext(have))) { 5 2 7
723 5 I32 gkl; const char *gk = hv_iterkey(ghe, &gkl);
724 5 ▶ if (oa_ci_eq(gk, (STRLEN)gkl, hk, (STRLEN)hkl))
Branch: 1 branch
BranchTrueFalseTotal
if (oa_ci_eq(gk, (STRLEN)gkl, hk, (STRLEN)hkl)) 2 3 5
725 2 { found = 1; break; }
726 }
727 }
728 4 ▶ if (!found) {
Branch: 1 branch
BranchTrueFalseTotal
if (!found) { 2 2 4
729 2 ok = 0;
730 2 ▶ if (errs) {
Branch: 1 branch
BranchTrueFalseTotal
if (errs) { 2 0 2
731 2 SV *nm = sv_2mortal(newSVpvn(hk, (STRLEN)hkl));
732 2 oa_err_push(aTHX_ errs, "body", nm, "required",
733 "multipart part is missing a required "
734 "encoding header");
735 }
736 }
737 }
738 }
739 4 return ok;
740 }
741  
742 /* the value of a header parameter: name=value or name="value" */
743 26 static const char *oa_hdr_param(const char *s, STRLEN sl, const char *name,
744 STRLEN *vl) {
745 26 STRLEN nl = strlen(name), i;
746 737 ▶ for (i = 0; i + nl + 1 <= sl; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + nl + 1 <= sl; i++) { 737 0 737
747 737 ▶ if ((i == 0 || s[i-1] == ' ' || s[i-1] == ';' || s[i-1] == '\t')
Branch: 4 branches
BranchTrueFalseTotal
if ((i == 0 || s[i-1] == ' ' || s[i-1] == ';' || s[i-1] == '\t') 711 26 737
if ((i == 0 || s[i-1] == ' ' || s[i-1] == ';' || s[i-1] == '\t') 670 41 711
if ((i == 0 || s[i-1] == ' ' || s[i-1] == ';' || s[i-1] == '\t') 644 26 670
if ((i == 0 || s[i-1] == ' ' || s[i-1] == ';' || s[i-1] == '\t') 0 644 644
748 93 ▶ && oa_ci_eq(s + i, nl, name, nl) && s[i + nl] == '=') {
Branch: 2 branches
BranchTrueFalseTotal
&& oa_ci_eq(s + i, nl, name, nl) && s[i + nl] == '=') { 26 67 93
&& oa_ci_eq(s + i, nl, name, nl) && s[i + nl] == '=') { 26 0 26
749 26 const char *v = s + i + nl + 1;
750 const char *e;
751 26 ▶ if (v < s + sl && *v == '"') { v++; e = v;
Branch: 2 branches
BranchTrueFalseTotal
if (v < s + sl && *v == '"') { v++; e = v; 26 0 26
if (v < s + sl && *v == '"') { v++; e = v; 15 11 26
752 36 ▶ while (e < s + sl && *e != '"') e++; }
Branch: 2 branches
BranchTrueFalseTotal
while (e < s + sl && *e != '"') e++; } 36 0 36
while (e < s + sl && *e != '"') e++; } 21 15 36
753 73 ▶ else { e = v; while (e < s + sl && *e != ';') e++; }
Branch: 2 branches
BranchTrueFalseTotal
else { e = v; while (e < s + sl && *e != ';') e++; } 62 11 73
else { e = v; while (e < s + sl && *e != ';') e++; } 62 0 62
754 26 *vl = (STRLEN)(e - v);
755 26 return v;
756 }
757 }
758 0 return NULL;
759 }
760  
761 /* multipart/form-data. The boundary lives in the Content-Type's parameters,
762 * which is why the FULL header is needed here and not the trimmed type.
763 * Field parts become values; a part with a filename keeps its content, since
764 * a schema can only speak about what is in the body. NULL when there is no
765 * boundary to walk - that is a malformed request, not an empty one. */
766 /* `part_hdrs`, when given, is filled with part name => { header => value }.
767 * The decoded body itself stays a plain name => value map, because that is
768 * what the schema validates - an Encoding Object's declared headers are a
769 * separate question, asked separately. Pass NULL to not collect them. */
770 11 static SV *oa_multipart_decode(pTHX_ SV *text, const char *ct, STRLEN ctl,
771 HV *part_hdrs) {
772 11 STRLEN bl = 0, l;
773 11 const char *bp = oa_hdr_param(ct, ctl, "boundary", &bl);
774 const char *body, *end, *p;
775 SV *dash;
776 HV *out;
777 11 ▶ if (!bp || !bl) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!bp || !bl) return NULL; 11 0 11
if (!bp || !bl) return NULL; 0 11 11
778 11 body = SvPV_const(text, l);
779 11 end = body + l;
780 11 dash = sv_2mortal(newSVpvs("--"));
781 11 sv_catpvn(dash, bp, bl);
782 {
783 11 char *dp = SvPVX(dash);
784 11 STRLEN dl = SvCUR(dash);
785 11 p = ninstr((char *)body, (char *)end, dp, dp + dl);
786 11 ▶ if (!p) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!p) return NULL; 0 11 11
787 11 out = newHV();
788 11 p += dl;
789 26 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 26 0 26
790 const char *hend, *nd, *content, *hp;
791 26 STRLEN nlen = 0;
792 26 const char *nm = NULL;
793 HV *hdrs_here;
794 26 ▶ if (p + 2 <= end && p[0] == '-' && p[1] == '-') break; /* closing */
Branch: 3 branches
BranchTrueFalseTotal
if (p + 2 <= end && p[0] == '-' && p[1] == '-') break; /* closing */ 26 0 26
if (p + 2 <= end && p[0] == '-' && p[1] == '-') break; /* closing */ 11 15 26
if (p + 2 <= end && p[0] == '-' && p[1] == '-') break; /* closing */ 11 0 11
795 45 ▶ while (p < end && (*p == '\r' || *p == '\n')) p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && (*p == '\r' || *p == '\n')) p++; 45 0 45
while (p < end && (*p == '\r' || *p == '\n')) p++; 15 30 45
while (p < end && (*p == '\r' || *p == '\n')) p++; 15 15 30
796 { /* both ends of the needle must come from ONE object: two
797 * spellings of the same literal are two objects, so `lit + n`
798 * would point past a different array than `lit` */
799 15 const char *crlf2 = "\r\n\r\n";
800 15 hend = ninstr((char *)p, (char *)end,
801 (char *)crlf2, (char *)crlf2 + 4);
802 }
803 15 ▶ if (!hend) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!hend) break; 0 15 15
804 15 ▶ hdrs_here = part_hdrs ? (HV *)sv_2mortal((SV *)newHV()) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
hdrs_here = part_hdrs ? (HV *)sv_2mortal((SV *)newHV()) : NULL; 4 11 15
805 17 ▶ for (hp = p; hp < hend; ) {
Branch: 1 branch
BranchTrueFalseTotal
for (hp = p; hp < hend; ) { 17 0 17
806 17 const char *crlf = "\r\n";
807 17 const char *le = ninstr((char *)hp, (char *)hend,
808 (char *)crlf, (char *)crlf + 2);
809 17 ▶ STRLEN ll = (STRLEN)((le ? le : hend) - hp);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN ll = (STRLEN)((le ? le : hend) - hp); 2 15 17
810 17 ▶ if (ll >= 20 && oa_ci_eq(hp, 20, "Content-Disposition:", 20))
Branch: 2 branches
BranchTrueFalseTotal
if (ll >= 20 && oa_ci_eq(hp, 20, "Content-Disposition:", 20)) 15 2 17
if (ll >= 20 && oa_ci_eq(hp, 20, "Content-Disposition:", 20)) 15 0 15
811 15 nm = oa_hdr_param(hp, ll, "name", &nlen);
812 /* every header line, kept for the encoding check. The name is
813 * everything before the colon; the value is what follows,
814 * with leading space trimmed. */
815 17 ▶ if (hdrs_here) {
Branch: 1 branch
BranchTrueFalseTotal
if (hdrs_here) { 6 11 17
816 6 STRLEN c = 0;
817 88 ▶ while (c < ll && hp[c] != ':') c++;
Branch: 2 branches
BranchTrueFalseTotal
while (c < ll && hp[c] != ':') c++; 88 0 88
while (c < ll && hp[c] != ':') c++; 82 6 88
818 6 ▶ if (c > 0 && c < ll) {
Branch: 2 branches
BranchTrueFalseTotal
if (c > 0 && c < ll) { 6 0 6
if (c > 0 && c < ll) { 6 0 6
819 6 STRLEN vs = c + 1;
820 12 ▶ while (vs < ll && (hp[vs] == ' ' || hp[vs] == '\t')) vs++;
Branch: 3 branches
BranchTrueFalseTotal
while (vs < ll && (hp[vs] == ' ' || hp[vs] == '\t')) vs++; 12 0 12
while (vs < ll && (hp[vs] == ' ' || hp[vs] == '\t')) vs++; 6 6 12
while (vs < ll && (hp[vs] == ' ' || hp[vs] == '\t')) vs++; 0 6 6
821 6 (void)hv_store(hdrs_here, hp, (I32)c,
822 newSVpvn(hp + vs, ll - vs), 0);
823 }
824 }
825 17 ▶ if (!le) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!le) break; 15 2 17
826 2 hp = le + 2;
827 }
828 15 content = hend + 4;
829 15 nd = ninstr((char *)content, (char *)end, dp, dp + dl);
830 15 ▶ if (!nd) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!nd) break; 0 15 15
831 15 ▶ if (nm) {
Branch: 1 branch
BranchTrueFalseTotal
if (nm) { 15 0 15
832 15 STRLEN clen = (STRLEN)(nd - content);
833 30 ▶ while (clen >= 2 && content[clen-1] == '\n' && content[clen-2] == '\r')
Branch: 3 branches
BranchTrueFalseTotal
while (clen >= 2 && content[clen-1] == '\n' && content[clen-2] == '\r') 20 10 30
while (clen >= 2 && content[clen-1] == '\n' && content[clen-2] == '\r') 15 5 20
while (clen >= 2 && content[clen-1] == '\n' && content[clen-2] == '\r') 15 0 15
834 15 clen -= 2; /* the CRLF before --bound */
835 15 oa_form_add(aTHX_ out, sv_2mortal(newSVpvn(nm, nlen)),
836 newSVpvn(content, clen));
837 15 ▶ if (part_hdrs && hdrs_here)
Branch: 2 branches
BranchTrueFalseTotal
if (part_hdrs && hdrs_here) 4 11 15
if (part_hdrs && hdrs_here) 4 0 4
838 4 (void)hv_store(part_hdrs, nm, (I32)nlen,
839 newRV_inc((SV *)hdrs_here), 0);
840 }
841 15 p = nd + dl;
842 }
843 }
844 11 return sv_2mortal(newRV_noinc((SV *)out));
845 }
846  
847 /* Decode JSON text through the frj ABI. The ABI croaks on malformed input,
848 * so the decode runs inside the private _frj_decode trampoline (a direct ABI
849 * call), invoked here on its cached CV with G_EVAL - a bad request body
850 * becomes a 400, never an escaped die. Mortal SV on success, NULL on parse
851 * failure. OA_DEC_CV is resolved in BOOT (API.xs). */
852 static SV *OA_DEC_CV = NULL;
853 static SV *OA_ENC_CV = NULL;
854  
855 /* `*ok` separates the two things a NULL return used to mean: the text was not
856 * JSON at all, and the text was the JSON document `null`. They are not the
857 * same - `null` is a valid JSON text (RFC 8259) and `type: null` is a valid
858 * 3.1 schema, which is also what a 3.0 `nullable` converts INTO - and folding
859 * them together reported a conforming null body as malformed JSON.
860 *
861 * On success with a null document this returns NULL with *ok true, so a caller
862 * that does not care keeps its old behaviour exactly. */
863 4315 static SV *oa_body_decode_ok(pTHX_ SV *text, int *ok) {
864 4315 dSP; int count; SV *ret = NULL;
865 4315 *ok = 0;
866 4315 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 4315 4315
867 4315 ▶ XPUSHs(text); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(text); PUTBACK; 0 4315 4315
868 4315 count = call_sv(OA_DEC_CV, G_SCALAR | G_EVAL);
869 4315 SPAGAIN;
870 8628 ▶ if (!SvTRUE(ERRSV) && count > 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && count > 0) { 4315 0 4315
if (!SvTRUE(ERRSV) && count > 0) { 4313 2 4315
if (!SvTRUE(ERRSV) && count > 0) { 4313 0 4313
871 4313 SV *r = POPs;
872 4313 *ok = 1;
873 4313 ▶ if (SvOK(r)) ret = newSVsv(r);
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(r)) ret = newSVsv(r); 4197 116 4313
874 }
875 2 ▶ else if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) (void)POPs; 2 0 2
876 4315 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 4315 0 4315
877 4315 ▶ return ret ? sv_2mortal(ret) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return ret ? sv_2mortal(ret) : NULL; 4197 118 4315
878 }
879  
880 1666 static SV *oa_body_decode(pTHX_ SV *text) {
881 int ok;
882 1666 return oa_body_decode_ok(aTHX_ text, &ok);
883 }
884  
885 /* ---- the validation pipeline ------------------------------------------------ */
886  
887 /* Validate a matched operation's inputs. All arguments may be NULL/absent
888 * except the op. Returns 1/0; params_out (if non-NULL) receives a fresh HV
889 * { path, query, header, cookie, body } on success. */
890 6160 static int oa_validate_op(pTHX_ oa_api *a, oa_op *o,
891 HV *rawpath, SV *query, HV *headers, SV *body_raw,
892 HV **params_out, AV *errs) {
893 6160 HV *out = newHV();
894 6160 HV *op_ = newHV(), *oq = NULL, *oh = newHV(), *oc = newHV();
895 6160 int ok = 1, i;
896 PERL_UNUSED_ARG(a);
897  
898 /* path captures: decode, validate, store */
899 8882 ▶ for (i = 0; i < o->nparams[OA_IN_PATH]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < o->nparams[OA_IN_PATH]; i++) { 2722 6160 8882
900 2722 oa_param *pp = &o->params[OA_IN_PATH][i];
901 2722 STRLEN nl; const char *np = SvPV_const(pp->name, nl);
902 2722 ▶ SV **raw = rawpath ? hv_fetch(rawpath, np, (I32)nl, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **raw = rawpath ? hv_fetch(rawpath, np, (I32)nl, 0) : NULL; 2721 1 2722
903 5443 ▶ if (raw && *raw && SvOK(*raw)) {
Branch: 3 branches
BranchTrueFalseTotal
if (raw && *raw && SvOK(*raw)) { 2721 1 2722
if (raw && *raw && SvOK(*raw)) { 2721 0 2721
if (raw && *raw && SvOK(*raw)) { 2721 0 2721
904 2721 STRLEN vl; const char *vp = SvPV_const(*raw, vl);
905 2721 SV *dec = oa_style_path(aTHX_ pp, vp, vl);
906 2721 ▶ if (!oa_check_param(aTHX_ pp, dec, errs, "path")) ok = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!oa_check_param(aTHX_ pp, dec, errs, "path")) ok = 0; 510 2211 2721
907 2211 else (void)hv_store(op_, np, (I32)nl, SvREFCNT_inc(dec), 0);
908 } else {
909 1 ok = 0;
910 1 ▶ if (errs) oa_err_push(aTHX_ errs, "path", pp->name,
Branch: 1 branch
BranchTrueFalseTotal
if (errs) oa_err_push(aTHX_ errs, "path", pp->name, 1 0 1
911 "required", "missing required path parameter");
912 }
913 2722 ▶ if (!ok && !errs) goto done;
Branch: 2 branches
BranchTrueFalseTotal
if (!ok && !errs) goto done; 511 2211 2722
if (!ok && !errs) goto done; 0 511 511
914 }
915  
916 /* query: string -> parse; hashref -> use as given */
917 6170 ▶ if (query && SvROK(query) && SvTYPE(SvRV(query)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (query && SvROK(query) && SvTYPE(SvRV(query)) == SVt_PVHV) { 1735 4425 6160
if (query && SvROK(query) && SvTYPE(SvRV(query)) == SVt_PVHV) { 10 1725 1735
if (query && SvROK(query) && SvTYPE(SvRV(query)) == SVt_PVHV) { 10 0 10
918 10 HV *src = (HV *)SvRV(query);
919 HE *he;
920 10 oq = newHV();
921 10 hv_iterinit(src);
922 19 ▶ while ((he = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) { 9 10 19
923 9 I32 kl; const char *k = hv_iterkey(he, &kl);
924 9 (void)hv_store(oq, k, kl, newSVsv(hv_iterval(src, he)), 0);
925 }
926 7875 ▶ } else if (query && SvOK(query)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (query && SvOK(query)) { 1725 4425 6150
} else if (query && SvOK(query)) { 1725 0 1725
927 1725 STRLEN ql; const char *qp = SvPV_const(query, ql);
928 1725 oq = oa_parse_query(aTHX_ o, qp, ql);
929 } else {
930 4425 oq = newHV();
931 }
932 10914 ▶ for (i = 0; i < o->nparams[OA_IN_QUERY]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < o->nparams[OA_IN_QUERY]; i++) { 4754 6160 10914
933 4754 oa_param *pp = &o->params[OA_IN_QUERY][i];
934 4754 STRLEN nl; const char *np = SvPV_const(pp->name, nl);
935 4754 SV **v = hv_fetch(oq, np, (I32)nl, 0);
936 4754 ▶ if (v && *v && SvOK(*v)) {
Branch: 3 branches
BranchTrueFalseTotal
if (v && *v && SvOK(*v)) { 4128 626 4754
if (v && *v && SvOK(*v)) { 4128 0 4128
if (v && *v && SvOK(*v)) { 4128 0 4128
937 /* present and empty, with allowEmptyValue: permitted as it
938 * stands rather than handed to a schema that will refuse it */
939 4128 ▶ if (pp->allow_empty && !SvROK(*v) && SvCUR(*v) == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (pp->allow_empty && !SvROK(*v) && SvCUR(*v) == 0) { 4 4124 4128
if (pp->allow_empty && !SvROK(*v) && SvCUR(*v) == 0) { 4 0 4
if (pp->allow_empty && !SvROK(*v) && SvCUR(*v) == 0) { 3 1 4
940 /* accepted */
941 }
942 4125 ▶ else if (!oa_check_param(aTHX_ pp, *v, errs, "query")) ok = 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (!oa_check_param(aTHX_ pp, *v, errs, "query")) ok = 0; 27 4098 4125
943 626 ▶ } else if (pp->required) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (pp->required) { 7 619 626
944 7 ok = 0;
945 7 ▶ if (errs) oa_err_push(aTHX_ errs, "query", pp->name,
Branch: 1 branch
BranchTrueFalseTotal
if (errs) oa_err_push(aTHX_ errs, "query", pp->name, 7 0 7
946 "required", "missing required query parameter");
947 }
948 4754 ▶ if (!ok && !errs) goto done;
Branch: 2 branches
BranchTrueFalseTotal
if (!ok && !errs) goto done; 48 4706 4754
if (!ok && !errs) goto done; 0 48 48
949 }
950  
951 /* headers: declared names looked up lowercased */
952 9335 ▶ for (i = 0; i < o->nparams[OA_IN_HEADER]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < o->nparams[OA_IN_HEADER]; i++) { 3175 6160 9335
953 3175 oa_param *pp = &o->params[OA_IN_HEADER][i];
954 3175 STRLEN nl; const char *np = SvPV_const(pp->name, nl);
955 char lc[128];
956 3175 SV **v = NULL;
957 3175 ▶ if (nl < sizeof lc) {
Branch: 1 branch
BranchTrueFalseTotal
if (nl < sizeof lc) { 3175 0 3175
958 STRLEN k;
959 35493 ▶ for (k = 0; k < nl; k++) lc[k] = (char)toLOWER((U8)np[k]);
Branch: 2 branches
BranchTrueFalseTotal
for (k = 0; k < nl; k++) lc[k] = (char)toLOWER((U8)np[k]); 8461 23857 32318
for (k = 0; k < nl; k++) lc[k] = (char)toLOWER((U8)np[k]); 32318 3175 35493
960 3175 ▶ v = headers ? hv_fetch(headers, lc, (I32)nl, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
v = headers ? hv_fetch(headers, lc, (I32)nl, 0) : NULL; 1572 1603 3175
961 }
962 4210 ▶ if (v && *v && SvOK(*v)) {
Branch: 3 branches
BranchTrueFalseTotal
if (v && *v && SvOK(*v)) { 1035 2140 3175
if (v && *v && SvOK(*v)) { 1035 0 1035
if (v && *v && SvOK(*v)) { 1035 0 1035
963 1035 SV *val = *v;
964 /* header is always `simple`: a list is comma separated, and the
965 * value is not percent-encoded, so it is split but not decoded */
966 1035 ▶ if (pp->is_array) {
Branch: 1 branch
BranchTrueFalseTotal
if (pp->is_array) { 511 524 1035
967 511 STRLEN hl; const char *hp = SvPV_const(val, hl);
968 511 ▶ val = sv_2mortal(oa_split(aTHX_ hp, hl, pp->delim ? pp->delim : ',', 0, 0));
Branch: 1 branch
BranchTrueFalseTotal
val = sv_2mortal(oa_split(aTHX_ hp, hl, pp->delim ? pp->delim : ',', 0, 0)); 504 7 511
969 511 oa_split_nested(aTHX_ pp, val, 0);
970 }
971 /* an object is comma separated too, and explode decides whether
972 * the pieces are `k,v` pairs or their own `k=v` */
973 524 ▶ else if (pp->is_object) {
Branch: 1 branch
BranchTrueFalseTotal
else if (pp->is_object) { 2 522 524
974 2 STRLEN hl; const char *hp = SvPV_const(val, hl);
975 2 SV *pieces = sv_2mortal(oa_split(aTHX_ hp, hl, ',', 0, 0));
976 2 val = pp->explode ? oa_obj_from_kv(aTHX_ pieces)
977 2 ▶ : oa_obj_from_pairs(aTHX_ pieces);
Branch: 1 branch
BranchTrueFalseTotal
: oa_obj_from_pairs(aTHX_ pieces); 1 1 2
978 }
979 1035 ▶ if (!oa_check_param(aTHX_ pp, val, errs, "header")) ok = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!oa_check_param(aTHX_ pp, val, errs, "header")) ok = 0; 3 1032 1035
980 1032 else (void)hv_store(oh, np, (I32)nl, newSVsv(val), 0);
981 2140 ▶ } else if (pp->required) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (pp->required) { 3 2137 2140
982 3 ok = 0;
983 3 ▶ if (errs) oa_err_push(aTHX_ errs, "header", pp->name,
Branch: 1 branch
BranchTrueFalseTotal
if (errs) oa_err_push(aTHX_ errs, "header", pp->name, 3 0 3
984 "required", "missing required header");
985 }
986 3175 ▶ if (!ok && !errs) goto done;
Branch: 2 branches
BranchTrueFalseTotal
if (!ok && !errs) goto done; 517 2658 3175
if (!ok && !errs) goto done; 0 517 517
987 }
988  
989 /* cookies: parsed from the Cookie header only when declared */
990 6160 ▶ if (o->nparams[OA_IN_COOKIE]) {
Branch: 1 branch
BranchTrueFalseTotal
if (o->nparams[OA_IN_COOKIE]) { 18 6142 6160
991 18 ▶ SV **ch = headers ? hv_fetchs(headers, "cookie", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **ch = headers ? hv_fetchs(headers, "cookie", 0) : NULL; 17 1 18
992 18 HV *jar = NULL;
993 18 ▶ if (ch && *ch && SvOK(*ch)) {
Branch: 3 branches
BranchTrueFalseTotal
if (ch && *ch && SvOK(*ch)) { 17 1 18
if (ch && *ch && SvOK(*ch)) { 17 0 17
if (ch && *ch && SvOK(*ch)) { 17 0 17
994 17 STRLEN cl; const char *cp = SvPV_const(*ch, cl);
995 17 jar = oa_parse_cookies(aTHX_ cp, cl);
996 }
997 36 ▶ for (i = 0; i < o->nparams[OA_IN_COOKIE]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < o->nparams[OA_IN_COOKIE]; i++) { 18 18 36
998 18 oa_param *pp = &o->params[OA_IN_COOKIE][i];
999 18 STRLEN nl; const char *np = SvPV_const(pp->name, nl);
1000 18 ▶ SV **v = jar ? hv_fetch(jar, np, (I32)nl, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **v = jar ? hv_fetch(jar, np, (I32)nl, 0) : NULL; 17 1 18
1001 35 ▶ if (v && *v && SvOK(*v)) {
Branch: 3 branches
BranchTrueFalseTotal
if (v && *v && SvOK(*v)) { 17 1 18
if (v && *v && SvOK(*v)) { 17 0 17
if (v && *v && SvOK(*v)) { 17 0 17
1002 17 SV *val = *v;
1003 /* cookie is `form`; a list is comma separated. Values are
1004 * kept raw by oa_parse_cookies, so split without decoding */
1005 17 ▶ if (pp->is_array) {
Branch: 1 branch
BranchTrueFalseTotal
if (pp->is_array) { 3 14 17
1006 3 STRLEN cl2; const char *cp2 = SvPV_const(val, cl2);
1007 3 ▶ val = sv_2mortal(oa_split(aTHX_ cp2, cl2, pp->delim ? pp->delim : ',', 0, 0));
Branch: 1 branch
BranchTrueFalseTotal
val = sv_2mortal(oa_split(aTHX_ cp2, cl2, pp->delim ? pp->delim : ',', 0, 0)); 1 2 3
1008 3 oa_split_nested(aTHX_ pp, val, 0);
1009 }
1010 /* an object cookie is assembled the way a header one is:
1011 * comma separated, `k,v` pairs or `k=v` when exploded */
1012 14 ▶ else if (pp->is_object) {
Branch: 1 branch
BranchTrueFalseTotal
else if (pp->is_object) { 0 14 14
1013 0 STRLEN cl2; const char *cp2 = SvPV_const(val, cl2);
1014 0 SV *pieces = sv_2mortal(oa_split(aTHX_ cp2, cl2, ',', 0, 0));
1015 0 val = pp->explode ? oa_obj_from_kv(aTHX_ pieces)
1016 0 ▶ : oa_obj_from_pairs(aTHX_ pieces);
Branch: 1 branch
BranchTrueFalseTotal
: oa_obj_from_pairs(aTHX_ pieces); 0 0 0
1017 }
1018 17 ▶ if (!oa_check_param(aTHX_ pp, val, errs, "cookie")) ok = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!oa_check_param(aTHX_ pp, val, errs, "cookie")) ok = 0; 1 16 17
1019 16 else (void)hv_store(oc, np, (I32)nl, newSVsv(val), 0);
1020 1 ▶ } else if (pp->required) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (pp->required) { 1 0 1
1021 1 ok = 0;
1022 1 ▶ if (errs) oa_err_push(aTHX_ errs, "cookie", pp->name,
Branch: 1 branch
BranchTrueFalseTotal
if (errs) oa_err_push(aTHX_ errs, "cookie", pp->name, 1 0 1
1023 "required", "missing required cookie");
1024 }
1025 18 ▶ if (!ok && !errs) { if (jar) SvREFCNT_dec((SV *)jar); goto done; }
Branch: 3 branches
BranchTrueFalseTotal
if (!ok && !errs) { if (jar) SvREFCNT_dec((SV *)jar); goto done; } 2 16 18
if (!ok && !errs) { if (jar) SvREFCNT_dec((SV *)jar); goto done; } 0 2 2
if (!ok && !errs) { if (jar) SvREFCNT_dec((SV *)jar); goto done; } 0 0 0
1026 }
1027 18 ▶ if (jar) SvREFCNT_dec((SV *)jar);
Branch: 1 branch
BranchTrueFalseTotal
if (jar) SvREFCNT_dec((SV *)jar); 17 1 18
1028 }
1029  
1030 /* body */
1031 6160 ▶ if (o->nbodies) {
Branch: 1 branch
BranchTrueFalseTotal
if (o->nbodies) { 3425 2735 6160
1032 2735 ▶ SV **cth = headers ? hv_fetchs(headers, "content-type", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **cth = headers ? hv_fetchs(headers, "content-type", 0) : NULL; 2731 4 2735
1033 2729 ▶ int have = body_raw && SvOK(body_raw)
Branch: 1 branch
BranchTrueFalseTotal
int have = body_raw && SvOK(body_raw) 2729 0 2729
1034 5464 ▶ && (SvROK(body_raw) || SvCUR(body_raw) > 0);
Branch: 3 branches
BranchTrueFalseTotal
&& (SvROK(body_raw) || SvCUR(body_raw) > 0); 2729 6 2735
&& (SvROK(body_raw) || SvCUR(body_raw) > 0); 2704 25 2729
&& (SvROK(body_raw) || SvCUR(body_raw) > 0); 2704 0 2704
1035 2735 ▶ if (!have) {
Branch: 1 branch
BranchTrueFalseTotal
if (!have) { 6 2729 2735
1036 6 ▶ if (o->body_required) {
Branch: 1 branch
BranchTrueFalseTotal
if (o->body_required) { 5 1 6
1037 5 ok = 0;
1038 5 ▶ if (errs) oa_err_push(aTHX_ errs, "body", NULL,
Branch: 1 branch
BranchTrueFalseTotal
if (errs) oa_err_push(aTHX_ errs, "body", NULL, 5 0 5
1039 "required", "missing required request body");
1040 }
1041 } else {
1042 2729 ▶ oa_body *b = oa_body_for(aTHX_ o, cth ? *cth : NULL);
Branch: 1 branch
BranchTrueFalseTotal
oa_body *b = oa_body_for(aTHX_ o, cth ? *cth : NULL); 2728 1 2729
1043 2729 ▶ if (!b) {
Branch: 1 branch
BranchTrueFalseTotal
if (!b) { 7 2722 2729
1044 7 ok = 0;
1045 7 ▶ if (errs) oa_err_push(aTHX_ errs, "body", NULL, "content-type",
Branch: 1 branch
BranchTrueFalseTotal
if (errs) oa_err_push(aTHX_ errs, "body", NULL, "content-type", 7 0 7
1046 "undeclared request content type");
1047 2722 ▶ } else if (b->handle) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (b->handle) { 2720 2 2722
1048 /* A range declares no decoder of its own, so the REQUEST's
1049 * content type decides. Without this a catch-all range would
1050 * match and then pass through unvalidated - a silent
1051 * acceptance, worse than the wrong rejection it replaced. */
1052 2720 STRLEN ctl = 0;
1053 2720 ▶ const char *ctp = (cth && *cth && SvOK(*cth))
Branch: 2 branches
BranchTrueFalseTotal
const char *ctp = (cth && *cth && SvOK(*cth)) 2720 0 2720
const char *ctp = (cth && *cth && SvOK(*cth)) 2720 0 2720
1054 8160 ▶ ? SvPV_const(*cth, ctl) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? SvPV_const(*cth, ctl) : NULL; 2720 0 2720
1055 2720 int kind = b->kind;
1056 SV *data;
1057 /* a JSON body that decoded cleanly to the document `null`:
1058 * data is NULL but nothing is wrong with it */
1059 2720 int json_null = 0;
1060 2720 HV *part_hdrs = NULL; /* multipart, when an encoding asks */
1061 2720 ▶ if (kind == OA_MT_RANGE)
Branch: 1 branch
BranchTrueFalseTotal
if (kind == OA_MT_RANGE) 3 2717 2720
1062 3 ▶ kind = ctp ? oa_ctype_kind(ctp, ctl) : OA_MT_OPAQUE;
Branch: 1 branch
BranchTrueFalseTotal
kind = ctp ? oa_ctype_kind(ctp, ctl) : OA_MT_OPAQUE; 3 0 3
1063  
1064 2720 ▶ if (SvROK(body_raw)) data = body_raw;
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(body_raw)) data = body_raw; 24 2696 2720
1065 2696 ▶ else if (kind == OA_MT_FORM)
Branch: 1 branch
BranchTrueFalseTotal
else if (kind == OA_MT_FORM) 36 2660 2696
1066 36 data = oa_form_decode(aTHX_ body_raw, b->encoding);
1067 2660 ▶ else if (kind == OA_MT_MULTIPART) {
Branch: 1 branch
BranchTrueFalseTotal
else if (kind == OA_MT_MULTIPART) { 11 2649 2660
1068 11 ▶ if (b->encoding)
Branch: 1 branch
BranchTrueFalseTotal
if (b->encoding) 4 7 11
1069 4 part_hdrs = (HV *)sv_2mortal((SV *)newHV());
1070 11 data = ctp ? oa_multipart_decode(aTHX_ body_raw, ctp, ctl,
1071 11 ▶ part_hdrs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
part_hdrs) : NULL; 11 0 11
1072 }
1073 2649 ▶ else if (kind == OA_MT_JSON) {
Branch: 1 branch
BranchTrueFalseTotal
else if (kind == OA_MT_JSON) { 2649 0 2649
1074 int dec_ok;
1075 2649 data = oa_body_decode_ok(aTHX_ body_raw, &dec_ok);
1076 2649 ▶ json_null = dec_ok && !data;
Branch: 2 branches
BranchTrueFalseTotal
json_null = dec_ok && !data; 2648 1 2649
json_null = dec_ok && !data; 116 2532 2648
1077 }
1078 else {
1079 /* a range covering something we cannot decode: preserve
1080 * the pass-through rather than invent a verdict */
1081 0 (void)hv_stores(out, "body", newSVsv(body_raw));
1082 0 goto body_done;
1083 }
1084  
1085 /* the Encoding Object, once the body IS properties */
1086 2720 ▶ if (data && b->encoding) {
Branch: 2 branches
BranchTrueFalseTotal
if (data && b->encoding) { 2603 117 2720
if (data && b->encoding) { 23 2580 2603
1087 23 oa_apply_encoding(aTHX_ data, b->encoding);
1088 23 ▶ if (!oa_check_encoding_headers(aTHX_ b->encoding,
Branch: 1 branch
BranchTrueFalseTotal
if (!oa_check_encoding_headers(aTHX_ b->encoding, 2 21 23
1089 part_hdrs, errs))
1090 2 ok = 0;
1091 }
1092  
1093 2720 ▶ if (!data && json_null) {
Branch: 2 branches
BranchTrueFalseTotal
if (!data && json_null) { 117 2603 2720
if (!data && json_null) { 116 1 117
1094 /* the document IS null. Hand the schema an undef so it can
1095 * say whether `null` was allowed here, rather than calling
1096 * a well-formed body malformed. */
1097 188 ▶ if (!oa_check(aTHX_ b->handle, &PL_sv_undef, errs,
Branch: 1 branch
BranchTrueFalseTotal
if (!oa_check(aTHX_ b->handle, &PL_sv_undef, errs, 72 44 116
1098 "body", NULL))
1099 72 ok = 0;
1100 2604 ▶ } else if (!data) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!data) { 1 2603 2604
1101 1 ok = 0;
1102 1 ▶ if (errs) oa_err_push(aTHX_ errs, "body", NULL,
Branch: 3 branches
BranchTrueFalseTotal
if (errs) oa_err_push(aTHX_ errs, "body", NULL, 1 0 1
if (errs) oa_err_push(aTHX_ errs, "body", NULL, 0 1 1
if (errs) oa_err_push(aTHX_ errs, "body", NULL, 0 1 1
1103 kind == OA_MT_MULTIPART ? "multipart" : "json",
1104 kind == OA_MT_MULTIPART
1105 ? "request body is not valid multipart/form-data"
1106 : "request body is not valid JSON");
1107 } else {
1108 2603 ▶ if (!oa_check(aTHX_ b->handle, data, errs, "body", NULL))
Branch: 1 branch
BranchTrueFalseTotal
if (!oa_check(aTHX_ b->handle, data, errs, "body", NULL)) 1124 1479 2603
1109 1124 ok = 0;
1110 1479 else (void)hv_stores(out, "body", newSVsv(data));
1111 }
1112 } else {
1113 2 (void)hv_stores(out, "body", newSVsv(body_raw)); /* pass-through */
1114 }
1115 }
1116 2735 body_done:
1117 2735 ▶ if (!ok && !errs) goto done;
Branch: 2 branches
BranchTrueFalseTotal
if (!ok && !errs) goto done; 1524 1211 2735
if (!ok && !errs) goto done; 1211 0 1211
1118 }
1119  
1120 6160 done:
1121 6160 ▶ if (ok && params_out) {
Branch: 2 branches
BranchTrueFalseTotal
if (ok && params_out) { 4397 1763 6160
if (ok && params_out) { 4397 0 4397
1122 4397 (void)hv_stores(out, "path", newRV_noinc((SV *)op_));
1123 4397 ▶ (void)hv_stores(out, "query", newRV_noinc((SV *)(oq ? oq : newHV())));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(out, "query", newRV_noinc((SV *)(oq ? oq : newHV()))); 0 4397 4397
1124 4397 (void)hv_stores(out, "header", newRV_noinc((SV *)oh));
1125 4397 (void)hv_stores(out, "cookie", newRV_noinc((SV *)oc));
1126 4397 *params_out = out;
1127 } else {
1128 1763 SvREFCNT_dec((SV *)op_);
1129 1763 ▶ if (oq) SvREFCNT_dec((SV *)oq);
Branch: 1 branch
BranchTrueFalseTotal
if (oq) SvREFCNT_dec((SV *)oq); 1763 0 1763
1130 1763 SvREFCNT_dec((SV *)oh);
1131 1763 SvREFCNT_dec((SV *)oc);
1132 1763 SvREFCNT_dec((SV *)out);
1133 1763 ▶ if (params_out) *params_out = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (params_out) *params_out = NULL; 1763 0 1763
1134 }
1135 6160 return ok;
1136 }
1137  
1138 #endif /* OA_VALIDATE_H */
« include/oa_compile.h ↑ Summary include/oa_types.h »