include/oa_client.h

filter: Statementstmt 72.8% 490 / 673 Branchbran 51.3% 448 / 872 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 60.7% 938 / 1545 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/arch/Open/API/Install/Files.pm ↑ Summary include/oa_mock.h »
1 #ifndef OA_CLIENT_H
2 #define OA_CLIENT_H
3  
4 /* Open::API::Client - the spec-driven HTTP client on Fetch's C ABI.
5 *
6 * A client is a blessed HV: { api => Open::API, base_url, validate, ua,
7 * ua_opts => [k, v, ...] }. call($operationId, %params) validates the flat
8 * %params against the operation's compiled JSF handles (croaking BEFORE any
9 * I/O on bad input), builds the URL/headers/body in C, and fires the request
10 * through the Fetch ABI. The returned Fetch::Future resolves to
11 * { status, headers => {lc-name => value}, data, error? }
12 * - `data` is the JSON-decoded body when the response is application/json,
13 * the raw body otherwise; transport failures resolve with status 0 and
14 * `error`; with validate => 1 a response-schema mismatch sets `error` and
15 * `errors`. */
16  
17 /* ---- lazy Fetch ABI resolution (the jsf_fetch.h pattern) ------------------- */
18  
19 12 static int oa_fetch_init(pTHX) {
20 12 dSP; int count; IV p = 0;
21 12 ▶ if (OA_FETCH) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (OA_FETCH) return 1; 10 2 12
22 2 ▶ if (OA_FETCH_TRIED) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (OA_FETCH_TRIED) return 0; 0 2 2
23 2 OA_FETCH_TRIED = 1;
24 2 eval_pv("require Fetch;", FALSE);
25 2 ▶ if (SvTRUE(ERRSV)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) return 0; 2 0 2
if (SvTRUE(ERRSV)) return 0; 0 2 2
26 2 SPAGAIN; /* the require may have reallocated the value stack */
27 2 ▶ ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; 0 2 2
28 2 count = call_pv("Fetch::_abi_ptr", G_SCALAR | G_EVAL);
29 2 SPAGAIN;
30 2 ▶ if (!SvTRUE(ERRSV) && count > 0) p = POPi;
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 2 0 2
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 2 0 2
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 2 0 2
31 0 ▶ else if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) (void)POPs; 0 0 0
32 2 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 2 0 2
33 2 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 2 0 2
34 2 const fetch_abi *f = INT2PTR(const fetch_abi *, p);
35 2 ▶ if (f && f->abi_version >= FETCH_ABI_VERSION) OA_FETCH = f;
Branch: 2 branches
BranchTrueFalseTotal
if (f && f->abi_version >= FETCH_ABI_VERSION) OA_FETCH = f; 2 0 2
if (f && f->abi_version >= FETCH_ABI_VERSION) OA_FETCH = f; 2 0 2
36 }
37 2 return OA_FETCH != NULL;
38 }
39  
40 /* ---- percent-encoding ------------------------------------------------------- */
41  
42 /* RFC 3986 gen-delims + sub-delims. Passed through unencoded only for a query
43 * parameter declared allowReserved, and only for its VALUE - a name is always
44 * encoded, or the pair separators stop meaning anything. */
45 3 static int oa_is_reserved(U8 c) {
46 3 ▶ return c == ':' || c == '/' || c == '?' || c == '#' || c == '['
Branch: 4 branches
BranchTrueFalseTotal
return c == ':' || c == '/' || c == '?' || c == '#' || c == '[' 1 2 3
return c == ':' || c == '/' || c == '?' || c == '#' || c == '[' 1 0 1
return c == ':' || c == '/' || c == '?' || c == '#' || c == '[' 1 0 1
return c == ':' || c == '/' || c == '?' || c == '#' || c == '[' 1 0 1
47 1 ▶ || c == ']' || c == '@' || c == '!' || c == '$' || c == '&'
Branch: 5 branches
BranchTrueFalseTotal
|| c == ']' || c == '@' || c == '!' || c == '$' || c == '&' 1 0 1
|| c == ']' || c == '@' || c == '!' || c == '$' || c == '&' 1 0 1
|| c == ']' || c == '@' || c == '!' || c == '$' || c == '&' 1 0 1
|| c == ']' || c == '@' || c == '!' || c == '$' || c == '&' 1 0 1
|| c == ']' || c == '@' || c == '!' || c == '$' || c == '&' 1 0 1
48 1 ▶ || c == '\'' || c == '(' || c == ')' || c == '*' || c == '+'
Branch: 5 branches
BranchTrueFalseTotal
|| c == '\'' || c == '(' || c == ')' || c == '*' || c == '+' 1 0 1
|| c == '\'' || c == '(' || c == ')' || c == '*' || c == '+' 1 0 1
|| c == '\'' || c == '(' || c == ')' || c == '*' || c == '+' 1 0 1
|| c == '\'' || c == '(' || c == ')' || c == '*' || c == '+' 1 0 1
|| c == '\'' || c == '(' || c == ')' || c == '*' || c == '+' 1 0 1
49 6 ▶ || c == ',' || c == ';' || c == '=';
Branch: 4 branches
BranchTrueFalseTotal
|| c == ',' || c == ';' || c == '='; 3 0 3
|| c == ',' || c == ';' || c == '='; 1 0 1
|| c == ',' || c == ';' || c == '='; 1 0 1
|| c == ',' || c == ';' || c == '='; 0 1 1
50 }
51  
52 309 static void oa_pct_encode_res(pTHX_ SV *out, const char *p, STRLEN l,
53 int reserved) {
54 static const char hex[] = "0123456789ABCDEF";
55 STRLEN i;
56 1441 ▶ for (i = 0; i < l; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l; i++) { 1132 309 1441
57 1132 U8 c = (U8)p[i];
58 1132 ▶ if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')
Branch: 4 branches
BranchTrueFalseTotal
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 1017 115 1132
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 984 33 1017
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 984 115 1099
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 0 984 984
59 115 ▶ || (c >= '0' && c <= '9') || c == '-' || c == '.'
Branch: 4 branches
BranchTrueFalseTotal
|| (c >= '0' && c <= '9') || c == '-' || c == '.' 109 6 115
|| (c >= '0' && c <= '9') || c == '-' || c == '.' 0 109 109
|| (c >= '0' && c <= '9') || c == '-' || c == '.' 6 0 6
|| (c >= '0' && c <= '9') || c == '-' || c == '.' 6 0 6
60 6 ▶ || c == '_' || c == '~'
Branch: 2 branches
BranchTrueFalseTotal
|| c == '_' || c == '~' 6 0 6
|| c == '_' || c == '~' 6 0 6
61 6 ▶ || (reserved && oa_is_reserved(c))) {
Branch: 2 branches
BranchTrueFalseTotal
|| (reserved && oa_is_reserved(c))) { 3 3 6
|| (reserved && oa_is_reserved(c))) { 2 1 3
62 1128 sv_catpvn(out, (const char *)&c, 1);
63 } else {
64 char b[3];
65 4 b[0] = '%'; b[1] = hex[c >> 4]; b[2] = hex[c & 15];
66 4 sv_catpvn(out, b, 3);
67 }
68 }
69 309 }
70  
71 /* the ordinary encoding: nothing reserved passes through */
72 104 static void oa_pct_encode_into(pTHX_ SV *out, const char *p, STRLEN l) {
73 104 oa_pct_encode_res(aTHX_ out, p, l, 0);
74 104 }
75  
76 /* ---- style-aware serialization ----------------------------------------------
77 *
78 * The mirror of oa_style_path and oa_parse_query on the way OUT. Without it the
79 * builder handed whatever it was given to SvPV, so an arrayref or hashref in a
80 * path segment stringified to `ARRAY(0x...)` and went on the wire, and `style`
81 * was ignored everywhere: a matrix parameter emitted a bare value with no
82 * `;name=`, a spaceDelimited list emitted repeat keys. The client built URLs
83 * this library's own server would refuse.
84 *
85 * Structural characters (the `;` `.` `,` `=` a style is MADE of) are written
86 * literally; only the values around them are encoded. That is why each piece is
87 * encoded on its own rather than the finished string being encoded at the end.
88 */
89  
90 /* An object's members in a stable order. Perl randomises hash order, so
91 * serializing one straight out of the hash produces a DIFFERENT URL each run -
92 * the same defect the router had. Sorted, so a URL is reproducible; object
93 * members carry no meaningful order, so the server rebuilds the same value
94 * whatever the order. */
95 25 static AV *oa_cli_sorted_keys(pTHX_ HV *h) {
96 25 AV *keys = (AV *)sv_2mortal((SV *)newAV());
97 HE *he;
98 SSize_t i;
99 25 hv_iterinit(h);
100 80 ▶ while ((he = hv_iternext(h))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(h))) { 55 25 80
101 55 I32 kl; const char *kp = hv_iterkey(he, &kl);
102 55 av_push(keys, newSVpvn(kp, (STRLEN)kl));
103 }
104 55 ▶ for (i = 1; i <= av_len(keys); i++) { /* insertion sort: few members */
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i <= av_len(keys); i++) { /* insertion sort: few members */ 30 25 55
105 30 SV **cur = av_fetch(keys, i, 0);
106 30 ▶ SV *c = cur && *cur ? SvREFCNT_inc(*cur) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
SV *c = cur && *cur ? SvREFCNT_inc(*cur) : NULL; 30 0 30
SV *c = cur && *cur ? SvREFCNT_inc(*cur) : NULL; 30 0 30
107 30 SSize_t j = i - 1;
108 30 ▶ if (!c) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!c) continue; 0 30 30
109 45 ▶ while (j >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (j >= 0) { 41 4 45
110 41 SV **prev = av_fetch(keys, j, 0);
111 41 ▶ if (!prev || !*prev || sv_cmp(*prev, c) <= 0) break;
Branch: 3 branches
BranchTrueFalseTotal
if (!prev || !*prev || sv_cmp(*prev, c) <= 0) break; 41 0 41
if (!prev || !*prev || sv_cmp(*prev, c) <= 0) break; 41 0 41
if (!prev || !*prev || sv_cmp(*prev, c) <= 0) break; 15 26 41
112 15 (void)av_store(keys, j + 1, SvREFCNT_inc(*prev));
113 15 j--;
114 }
115 30 (void)av_store(keys, j + 1, c);
116 }
117 25 return keys;
118 }
119  
120 /* one value, encoded */
121 196 static void oa_cli_val(pTHX_ SV *out, SV *v, int reserved) {
122 STRLEN l;
123 const char *p;
124 196 ▶ if (!v || !SvOK(v)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!v || !SvOK(v)) return; 196 0 196
if (!v || !SvOK(v)) return; 0 196 196
125 196 p = SvPV_const(v, l);
126 196 oa_pct_encode_res(aTHX_ out, p, l, reserved);
127 }
128  
129 /* a list delimiter as it goes into a URL: a space and a tab percent-encoded,
130 * a comma and a pipe literal (the 3.x pipeDelimited row has it literal) */
131 20 static const char *oa_cli_delim_url(char d) {
132 20 ▶ return d == ' ' ? "%20" : d == '\t' ? "%09" : d == '|' ? "|" : ",";
Branch: 3 branches
BranchTrueFalseTotal
return d == ' ' ? "%20" : d == '\t' ? "%09" : d == '|' ? "|" : ","; 16 4 20
return d == ' ' ? "%20" : d == '\t' ? "%09" : d == '|' ? "|" : ","; 9 7 16
return d == ' ' ? "%20" : d == '\t' ? "%09" : d == '|' ? "|" : ","; 9 0 9
133 }
134  
135 /* One element of a list. When the parameter declares nested levels (a Swagger
136 * 2.0 Items Object with its own collectionFormat) and the element is itself an
137 * arrayref, its members are joined with that level's delimiter, recursively;
138 * `url` says whether values and delimiters are percent-encoded (path, query)
139 * or literal (header, cookie). */
140 135 static void oa_cli_elem(pTHX_ oa_param *pp, SV *elem, SV *out, int lvl, int url, int res) {
141 135 ▶ AV *av = (lvl < pp->ndepth) ? oa_av_of(elem) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
AV *av = (lvl < pp->ndepth) ? oa_av_of(elem) : NULL; 4 131 135
142 135 ▶ if (av) {
Branch: 1 branch
BranchTrueFalseTotal
if (av) { 4 131 135
143 4 SSize_t i, n = av_len(av) + 1;
144 4 char d = pp->idelim[lvl];
145 12 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 8 4 12
146 8 SV **e = av_fetch(av, i, 0);
147 8 ▶ if (i) {
Branch: 1 branch
BranchTrueFalseTotal
if (i) { 4 4 8
148 4 ▶ if (url) sv_catpv(out, oa_cli_delim_url(d));
Branch: 1 branch
BranchTrueFalseTotal
if (url) sv_catpv(out, oa_cli_delim_url(d)); 4 0 4
149 0 else sv_catpvn(out, &d, 1);
150 }
151 8 ▶ if (e && *e) oa_cli_elem(aTHX_ pp, *e, out, lvl + 1, url, res);
Branch: 2 branches
BranchTrueFalseTotal
if (e && *e) oa_cli_elem(aTHX_ pp, *e, out, lvl + 1, url, res); 8 0 8
if (e && *e) oa_cli_elem(aTHX_ pp, *e, out, lvl + 1, url, res); 8 0 8
152 }
153 4 return;
154 }
155 131 ▶ if (url) oa_cli_val(aTHX_ out, elem, res);
Branch: 1 branch
BranchTrueFalseTotal
if (url) oa_cli_val(aTHX_ out, elem, res); 126 5 131
156 5 ▶ else if (SvOK(elem)) sv_catsv(out, elem);
Branch: 1 branch
BranchTrueFalseTotal
else if (SvOK(elem)) sv_catsv(out, elem); 5 0 5
157 }
158  
159 /* Serialize `val` for a PATH segment per pp->style. `out` already ends at the
160 * '/' this segment follows. */
161 42 static void oa_cli_ser_path(pTHX_ oa_param *pp, SV *val, SV *out) {
162 42 STRLEN nl; const char *np = SvPV_const(pp->name, nl);
163 42 AV *av = oa_av_of(val);
164 21 ▶ HV *hv = (!av && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
HV *hv = (!av && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) 6 15 21
HV *hv = (!av && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) 6 0 6
165 63 ▶ ? (HV *)SvRV(val) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(val) : NULL; 21 21 42
166 84 const char *pfx = pp->style == OA_ST_LABEL ? "."
167 42 ▶ : pp->style == OA_ST_MATRIX ? ";" : "";
Branch: 2 branches
BranchTrueFalseTotal
: pp->style == OA_ST_MATRIX ? ";" : ""; 32 10 42
: pp->style == OA_ST_MATRIX ? ";" : ""; 10 22 32
168 /* the separator BETWEEN elements once exploded */
169 74 ▶ const char *esep = pp->style == OA_ST_LABEL ? "."
Branch: 1 branch
BranchTrueFalseTotal
const char *esep = pp->style == OA_ST_LABEL ? "." 32 10 42
170 32 ▶ : pp->style == OA_ST_MATRIX ? ";" : ",";
Branch: 1 branch
BranchTrueFalseTotal
: pp->style == OA_ST_MATRIX ? ";" : ","; 10 22 32
171  
172 42 ▶ if (*pfx) sv_catpv(out, pfx);
Branch: 1 branch
BranchTrueFalseTotal
if (*pfx) sv_catpv(out, pfx); 20 22 42
173  
174 42 ▶ if (!av && !hv) { /* a scalar */
Branch: 2 branches
BranchTrueFalseTotal
if (!av && !hv) { /* a scalar */ 21 21 42
if (!av && !hv) { /* a scalar */ 15 6 21
175 15 ▶ if (pp->style == OA_ST_MATRIX) {
Branch: 1 branch
BranchTrueFalseTotal
if (pp->style == OA_ST_MATRIX) { 4 11 15
176 /* `;name=value`, and `;name` alone when the value is empty */
177 4 STRLEN vl; const char *vp = SvPV_const(val, vl);
178 4 oa_pct_encode_into(aTHX_ out, np, nl);
179 4 ▶ if (vl) { sv_catpvs(out, "="); oa_cli_val(aTHX_ out, val, 0); }
Branch: 1 branch
BranchTrueFalseTotal
if (vl) { sv_catpvs(out, "="); oa_cli_val(aTHX_ out, val, 0); } 4 0 4
180 } else {
181 11 oa_cli_val(aTHX_ out, val, 0);
182 }
183 36 return;
184 }
185  
186 27 ▶ if (av) {
Branch: 1 branch
BranchTrueFalseTotal
if (av) { 21 6 27
187 21 SSize_t i, n = av_len(av) + 1;
188 /* matrix, not exploded, prefixes the name ONCE: `;name=a,b` */
189 21 ▶ if (pp->style == OA_ST_MATRIX && !pp->explode) {
Branch: 2 branches
BranchTrueFalseTotal
if (pp->style == OA_ST_MATRIX && !pp->explode) { 4 17 21
if (pp->style == OA_ST_MATRIX && !pp->explode) { 2 2 4
190 2 oa_pct_encode_into(aTHX_ out, np, nl);
191 2 sv_catpvs(out, "=");
192 }
193 83 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 62 21 83
194 62 SV **e = av_fetch(av, i, 0);
195 62 ▶ if (i) {
Branch: 1 branch
BranchTrueFalseTotal
if (i) { 41 21 62
196 /* label and simple keep a comma unless exploded; matrix
197 * exploded repeats `;name=` per element; a delimiter the
198 * style cannot spell (pp->delim) replaces the comma */
199 41 ▶ if (pp->explode) sv_catpv(out, esep);
Branch: 1 branch
BranchTrueFalseTotal
if (pp->explode) sv_catpv(out, esep); 12 29 41
200 29 ▶ else if (pp->delim) sv_catpv(out, oa_cli_delim_url(pp->delim));
Branch: 1 branch
BranchTrueFalseTotal
else if (pp->delim) sv_catpv(out, oa_cli_delim_url(pp->delim)); 13 16 29
201 16 else sv_catpvs(out, ",");
202 }
203 62 ▶ if (pp->style == OA_ST_MATRIX && pp->explode) {
Branch: 2 branches
BranchTrueFalseTotal
if (pp->style == OA_ST_MATRIX && pp->explode) { 12 50 62
if (pp->style == OA_ST_MATRIX && pp->explode) { 6 6 12
204 if (i) { /* the `;` was written above */ }
205 6 oa_pct_encode_into(aTHX_ out, np, nl);
206 6 sv_catpvs(out, "=");
207 }
208 62 ▶ if (e && *e) oa_cli_elem(aTHX_ pp, *e, out, 0, 1, 0);
Branch: 2 branches
BranchTrueFalseTotal
if (e && *e) oa_cli_elem(aTHX_ pp, *e, out, 0, 1, 0); 62 0 62
if (e && *e) oa_cli_elem(aTHX_ pp, *e, out, 0, 1, 0); 62 0 62
209 }
210 21 return;
211 }
212  
213 { /* an object */
214 6 AV *keys = oa_cli_sorted_keys(aTHX_ hv);
215 6 SSize_t i, n = av_len(keys) + 1;
216 6 ▶ if (pp->style == OA_ST_MATRIX && !pp->explode) {
Branch: 2 branches
BranchTrueFalseTotal
if (pp->style == OA_ST_MATRIX && !pp->explode) { 2 4 6
if (pp->style == OA_ST_MATRIX && !pp->explode) { 1 1 2
217 1 oa_pct_encode_into(aTHX_ out, np, nl);
218 1 sv_catpvs(out, "=");
219 }
220 24 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 18 6 24
221 18 SV **k = av_fetch(keys, i, 0);
222 SV **v;
223 STRLEN kl; const char *kp;
224 18 ▶ if (!k || !*k) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!k || !*k) continue; 18 0 18
if (!k || !*k) continue; 0 18 18
225 18 kp = SvPV_const(*k, kl);
226 18 v = hv_fetch(hv, kp, (I32)kl, 0);
227 18 ▶ if (i) {
Branch: 1 branch
BranchTrueFalseTotal
if (i) { 12 6 18
228 12 ▶ if (pp->explode) sv_catpv(out, esep);
Branch: 1 branch
BranchTrueFalseTotal
if (pp->explode) sv_catpv(out, esep); 6 6 12
229 6 else sv_catpvs(out, ",");
230 }
231 /* exploded names each MEMBER with `=`; not exploded, the members
232 * are just more comma-separated pieces. sv_catpv, not sv_catpvs:
233 * the _s form is a macro over a string LITERAL. */
234 18 oa_pct_encode_into(aTHX_ out, kp, kl);
235 18 ▶ sv_catpv(out, pp->explode ? "=" : ",");
Branch: 1 branch
BranchTrueFalseTotal
sv_catpv(out, pp->explode ? "=" : ","); 9 9 18
236 18 ▶ if (v && *v) oa_cli_val(aTHX_ out, *v, 0);
Branch: 2 branches
BranchTrueFalseTotal
if (v && *v) oa_cli_val(aTHX_ out, *v, 0); 18 0 18
if (v && *v) oa_cli_val(aTHX_ out, *v, 0); 18 0 18
237 }
238 }
239 }
240  
241 /* Serialize `val` as QUERY pieces per pp->style, appending to `url`. */
242 26 static void oa_cli_ser_query(pTHX_ oa_param *pp, SV *val, SV *url, int *first) {
243 26 STRLEN nl; const char *np = SvPV_const(pp->name, nl);
244 26 AV *av = oa_av_of(val);
245 5 ▶ HV *hv = (!av && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
HV *hv = (!av && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) 5 0 5
HV *hv = (!av && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) 5 0 5
246 31 ▶ ? (HV *)SvRV(val) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(val) : NULL; 5 21 26
247 26 int res = pp->allow_reserved;
248 3 const char *dsep = pp->delim ? oa_cli_delim_url(pp->delim)
249 49 ▶ : pp->style == OA_ST_SPACE ? "%20"
Branch: 1 branch
BranchTrueFalseTotal
: pp->style == OA_ST_SPACE ? "%20" 3 23 26
250 23 ▶ : pp->style == OA_ST_PIPE ? "|" : ",";
Branch: 2 branches
BranchTrueFalseTotal
: pp->style == OA_ST_PIPE ? "|" : ","; 18 5 23
: pp->style == OA_ST_PIPE ? "|" : ","; 5 13 18
251  
252 26 ▶ if (!av && !hv) { /* a scalar */
Branch: 2 branches
BranchTrueFalseTotal
if (!av && !hv) { /* a scalar */ 5 21 26
if (!av && !hv) { /* a scalar */ 0 5 5
253 0 ▶ sv_catpvn(url, *first ? "?" : "&", 1); *first = 0;
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvn(url, *first ? "?" : "&", 1); *first = 0; 0 0 0
254 0 oa_pct_encode_into(aTHX_ url, np, nl);
255 0 sv_catpvs(url, "=");
256 0 oa_cli_val(aTHX_ url, val, res);
257 21 return;
258 }
259  
260 26 ▶ if (av) {
Branch: 1 branch
BranchTrueFalseTotal
if (av) { 21 5 26
261 21 SSize_t i, n = av_len(av) + 1;
262 /* form + explode repeats the key; every other query style joins the
263 * elements into ONE value with its own delimiter */
264 21 ▶ if (pp->style == OA_ST_FORM && pp->explode) {
Branch: 2 branches
BranchTrueFalseTotal
if (pp->style == OA_ST_FORM && pp->explode) { 13 8 21
if (pp->style == OA_ST_FORM && pp->explode) { 4 9 13
265 16 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 12 4 16
266 12 SV **e = av_fetch(av, i, 0);
267 12 ▶ if (!e || !*e || !SvOK(*e)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!e || !*e || !SvOK(*e)) continue; 12 0 12
if (!e || !*e || !SvOK(*e)) continue; 12 0 12
if (!e || !*e || !SvOK(*e)) continue; 0 12 12
268 12 ▶ sv_catpvn(url, *first ? "?" : "&", 1); *first = 0;
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvn(url, *first ? "?" : "&", 1); *first = 0; 4 8 12
269 12 oa_pct_encode_into(aTHX_ url, np, nl);
270 12 sv_catpvs(url, "=");
271 12 oa_cli_elem(aTHX_ pp, *e, url, 0, 1, res);
272 }
273 4 return;
274 }
275 17 ▶ sv_catpvn(url, *first ? "?" : "&", 1); *first = 0;
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvn(url, *first ? "?" : "&", 1); *first = 0; 16 1 17
276 17 oa_pct_encode_into(aTHX_ url, np, nl);
277 17 sv_catpvs(url, "=");
278 65 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 48 17 65
279 48 SV **e = av_fetch(av, i, 0);
280 48 ▶ if (i) sv_catpv(url, dsep);
Branch: 1 branch
BranchTrueFalseTotal
if (i) sv_catpv(url, dsep); 31 17 48
281 48 ▶ if (e && *e) oa_cli_elem(aTHX_ pp, *e, url, 0, 1, res);
Branch: 2 branches
BranchTrueFalseTotal
if (e && *e) oa_cli_elem(aTHX_ pp, *e, url, 0, 1, res); 48 0 48
if (e && *e) oa_cli_elem(aTHX_ pp, *e, url, 0, 1, res); 48 0 48
282 }
283 17 return;
284 }
285  
286 { /* an object */
287 5 AV *keys = oa_cli_sorted_keys(aTHX_ hv);
288 5 SSize_t i, n = av_len(keys) + 1;
289 9 ▶ int one_value = !(pp->style == OA_ST_DEEP
Branch: 1 branch
BranchTrueFalseTotal
int one_value = !(pp->style == OA_ST_DEEP 4 1 5
290 4 ▶ || (pp->style == OA_ST_FORM && pp->explode));
Branch: 2 branches
BranchTrueFalseTotal
|| (pp->style == OA_ST_FORM && pp->explode)); 2 2 4
|| (pp->style == OA_ST_FORM && pp->explode)); 1 1 2
291 5 ▶ if (one_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (one_value) { 3 2 5
292 3 ▶ sv_catpvn(url, *first ? "?" : "&", 1); *first = 0;
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvn(url, *first ? "?" : "&", 1); *first = 0; 3 0 3
293 3 oa_pct_encode_into(aTHX_ url, np, nl);
294 3 sv_catpvs(url, "=");
295 }
296 20 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 15 5 20
297 15 SV **k = av_fetch(keys, i, 0);
298 SV **v;
299 STRLEN kl; const char *kp;
300 24 ▶ if (!k || !*k) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!k || !*k) continue; 15 0 15
if (!k || !*k) continue; 0 15 15
301 15 kp = SvPV_const(*k, kl);
302 15 v = hv_fetch(hv, kp, (I32)kl, 0);
303 15 ▶ if (one_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (one_value) { 9 6 15
304 9 ▶ if (i) sv_catpv(url, dsep);
Branch: 1 branch
BranchTrueFalseTotal
if (i) sv_catpv(url, dsep); 6 3 9
305 9 oa_pct_encode_into(aTHX_ url, kp, kl);
306 9 sv_catpv(url, dsep);
307 9 ▶ if (v && *v) oa_cli_val(aTHX_ url, *v, res);
Branch: 2 branches
BranchTrueFalseTotal
if (v && *v) oa_cli_val(aTHX_ url, *v, res); 9 0 9
if (v && *v) oa_cli_val(aTHX_ url, *v, res); 9 0 9
308 9 continue;
309 }
310 6 ▶ sv_catpvn(url, *first ? "?" : "&", 1); *first = 0;
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvn(url, *first ? "?" : "&", 1); *first = 0; 2 4 6
311 6 ▶ if (pp->style == OA_ST_DEEP) {
Branch: 1 branch
BranchTrueFalseTotal
if (pp->style == OA_ST_DEEP) { 3 3 6
312 /* `name[member]=value` - the brackets are structural */
313 3 oa_pct_encode_into(aTHX_ url, np, nl);
314 3 sv_catpvs(url, "[");
315 3 oa_pct_encode_into(aTHX_ url, kp, kl);
316 3 sv_catpvs(url, "]");
317 } else {
318 /* form + explode: the MEMBER is the key */
319 3 oa_pct_encode_into(aTHX_ url, kp, kl);
320 }
321 6 sv_catpvs(url, "=");
322 6 ▶ if (v && *v) oa_cli_val(aTHX_ url, *v, res);
Branch: 2 branches
BranchTrueFalseTotal
if (v && *v) oa_cli_val(aTHX_ url, *v, res); 6 0 6
if (v && *v) oa_cli_val(aTHX_ url, *v, res); 6 0 6
323 }
324 }
325 }
326  
327 /* A header or cookie value. Neither is percent-encoded - the server splits
328 * them but does not decode - so this joins the pieces literally rather than
329 * going through oa_cli_val. Both locations are comma separated: a header is
330 * `simple` by definition, and a cookie is `form`, whose non-exploded list is
331 * also a comma. Without this an arrayref reached SvPV and the request carried
332 * `X-H: ARRAY(0x...)`, which was verified going out over a socket. */
333 4 static void oa_cli_ser_flat(pTHX_ oa_param *pp, SV *val, SV *out) {
334 4 AV *av = oa_av_of(val);
335 2 ▶ HV *hv = (!av && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
HV *hv = (!av && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) 2 0 2
HV *hv = (!av && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) 2 0 2
336 6 ▶ ? (HV *)SvRV(val) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(val) : NULL; 2 2 4
337 4 ▶ if (!av && !hv) { sv_catsv(out, val); return; }
Branch: 2 branches
BranchTrueFalseTotal
if (!av && !hv) { sv_catsv(out, val); return; } 2 2 4
if (!av && !hv) { sv_catsv(out, val); return; } 0 2 2
338 4 ▶ if (av) {
Branch: 1 branch
BranchTrueFalseTotal
if (av) { 2 2 4
339 2 SSize_t i, n = av_len(av) + 1;
340 2 ▶ char d = pp->delim ? pp->delim : ',';
Branch: 1 branch
BranchTrueFalseTotal
char d = pp->delim ? pp->delim : ','; 1 1 2
341 7 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 5 2 7
342 5 SV **e = av_fetch(av, i, 0);
343 5 ▶ if (i) sv_catpvn(out, &d, 1);
Branch: 1 branch
BranchTrueFalseTotal
if (i) sv_catpvn(out, &d, 1); 3 2 5
344 5 ▶ if (e && *e) oa_cli_elem(aTHX_ pp, *e, out, 0, 0, 0);
Branch: 2 branches
BranchTrueFalseTotal
if (e && *e) oa_cli_elem(aTHX_ pp, *e, out, 0, 0, 0); 5 0 5
if (e && *e) oa_cli_elem(aTHX_ pp, *e, out, 0, 0, 0); 5 0 5
345 }
346 2 return;
347 }
348 {
349 2 AV *keys = oa_cli_sorted_keys(aTHX_ hv);
350 2 SSize_t i, n = av_len(keys) + 1;
351 6 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 4 2 6
352 4 SV **k = av_fetch(keys, i, 0);
353 SV **v;
354 STRLEN kl; const char *kp;
355 4 ▶ if (!k || !*k) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!k || !*k) continue; 4 0 4
if (!k || !*k) continue; 0 4 4
356 4 kp = SvPV_const(*k, kl);
357 4 v = hv_fetch(hv, kp, (I32)kl, 0);
358 4 ▶ if (i) sv_catpvs(out, ",");
Branch: 1 branch
BranchTrueFalseTotal
if (i) sv_catpvs(out, ","); 2 2 4
359 4 sv_catpvn(out, kp, kl);
360 4 ▶ sv_catpv(out, pp->explode ? "=" : ",");
Branch: 1 branch
BranchTrueFalseTotal
sv_catpv(out, pp->explode ? "=" : ","); 2 2 4
361 4 ▶ if (v && *v && SvOK(*v)) sv_catsv(out, *v);
Branch: 3 branches
BranchTrueFalseTotal
if (v && *v && SvOK(*v)) sv_catsv(out, *v); 4 0 4
if (v && *v && SvOK(*v)) sv_catsv(out, *v); 4 0 4
if (v && *v && SvOK(*v)) sv_catsv(out, *v); 4 0 4
362 }
363 }
364 }
365  
366 /* ---- client-side validation -------------------------------------------------- */
367  
368 /* croak with the first JSF error message for a bad client-side value */
369 2 static void oa_cli_bad(pTHX_ oa_op *o, const char *in, SV *name, SV *value,
370 SV *handle) {
371 2 AV *errs = (AV *)sv_2mortal((SV *)newAV());
372 2 const char *msg = "value does not match the schema";
373 2 (void)JSF->validate(aTHX_ handle, value, errs);
374 2 ▶ if (av_len(errs) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(errs) >= 0) { 2 0 2
375 2 SV **e = av_fetch(errs, 0, 0);
376 2 ▶ HV *h = (e && *e && SvROK(*e)) ? (HV *)SvRV(*e) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
HV *h = (e && *e && SvROK(*e)) ? (HV *)SvRV(*e) : NULL; 2 0 2
HV *h = (e && *e && SvROK(*e)) ? (HV *)SvRV(*e) : NULL; 2 0 2
HV *h = (e && *e && SvROK(*e)) ? (HV *)SvRV(*e) : NULL; 2 0 2
377 2 ▶ SV *m = h ? oa_get(aTHX_ h, "message") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *m = h ? oa_get(aTHX_ h, "message") : NULL; 2 0 2
378 4 ▶ if (m) msg = SvPV_nolen(m);
Branch: 1 branch
BranchTrueFalseTotal
if (m) msg = SvPV_nolen(m); 2 0 2
379 }
380 3 ▶ croak("Open::API::Client: %s: invalid %s parameter '%s': %s",
Branch: 1 branch
BranchTrueFalseTotal
croak("Open::API::Client: %s: invalid %s parameter '%s': %s", 1 1 2
381 SvPV_nolen(o->op_id), in, name ? SvPV_nolen(name) : "body", msg);
382 }
383  
384 /* The wire form of a parameter value.
385 *
386 * A `content` parameter carries a whole document in one value, so a structure
387 * is encoded to its media type before it goes into the URL or a header -
388 * without this it would stringify as HASH(0x...). A caller who has already
389 * encoded it passes a string, which travels as it is. Mortal, or the original
390 * SV when nothing needs doing. */
391 56 static SV *oa_cli_wire(pTHX_ oa_param *pp, SV *v) {
392 56 ▶ if (pp->ctype && SvROK(v)
Branch: 2 branches
BranchTrueFalseTotal
if (pp->ctype && SvROK(v) 0 56 56
if (pp->ctype && SvROK(v) 0 0 0
393 0 ▶ && (SvTYPE(SvRV(v)) == SVt_PVHV || SvTYPE(SvRV(v)) == SVt_PVAV)) {
Branch: 2 branches
BranchTrueFalseTotal
&& (SvTYPE(SvRV(v)) == SVt_PVHV || SvTYPE(SvRV(v)) == SVt_PVAV)) { 0 0 0
&& (SvTYPE(SvRV(v)) == SVt_PVHV || SvTYPE(SvRV(v)) == SVt_PVAV)) { 0 0 0
394 0 STRLEN cl; const char *cp = SvPV_const(pp->ctype, cl);
395 0 ▶ if (oa_ctype_is_json(cp, cl)) {
Branch: 1 branch
BranchTrueFalseTotal
if (oa_ctype_is_json(cp, cl)) { 0 0 0
396 0 SV *j = oa_json_encode(aTHX_ v);
397 0 ▶ if (j) return j;
Branch: 1 branch
BranchTrueFalseTotal
if (j) return j; 0 0 0
398 }
399 }
400 56 return v;
401 }
402  
403 /* The value a `content` parameter's schema should see: the decoded document.
404 * Croaks on undecodable input, so the client fails before any I/O. */
405 9 static SV *oa_cli_decoded(pTHX_ oa_op *o, oa_param *pp, const char *in, SV *v) {
406 9 ▶ if (pp->ctype && !SvROK(v)) {
Branch: 2 branches
BranchTrueFalseTotal
if (pp->ctype && !SvROK(v)) { 0 9 9
if (pp->ctype && !SvROK(v)) { 0 0 0
407 0 STRLEN cl; const char *cp = SvPV_const(pp->ctype, cl);
408 0 ▶ if (oa_ctype_is_json(cp, cl)) {
Branch: 1 branch
BranchTrueFalseTotal
if (oa_ctype_is_json(cp, cl)) { 0 0 0
409 0 SV *d = oa_body_decode(aTHX_ v);
410 0 ▶ if (!d) croak("Open::API::Client: %s: %s parameter '%s' is not "
Branch: 2 branches
BranchTrueFalseTotal
if (!d) croak("Open::API::Client: %s: %s parameter '%s' is not " 0 0 0
if (!d) croak("Open::API::Client: %s: %s parameter '%s' is not " 0 0 0
411 "valid JSON", SvPV_nolen(o->op_id), in,
412 SvPV_nolen(pp->name));
413 0 return d;
414 }
415 }
416 9 return v;
417 }
418  
419 /* ---- the response map callback ------------------------------------------------ */
420  
421 typedef struct oa_cli_ctx {
422 SV *api_sv; /* keeps the op table alive (+1) */
423 oa_op *op;
424 int validate;
425 SV *cli; /* RV to the client HV, for CSRF token capture (+1) */
426 SV *csrf_cookie; /* CSRF cookie name to watch for in Set-Cookie (+1) */
427 } oa_cli_ctx;
428  
429 7 static SV *oa_cli_map(pTHX_ int ok, int status, AV *headers, SV *body,
430 SV *err, void *ud) {
431 7 oa_cli_ctx *c = (oa_cli_ctx *)ud;
432 7 HV *out = newHV();
433 7 HV *hh = newHV();
434 7 int is_json = 0;
435 SSize_t i, n;
436 7 SV *data = NULL;
437  
438 7 ▶ if (!ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { 2 5 7
439 2 (void)hv_stores(out, "status", newSViv(0));
440 2 ▶ (void)hv_stores(out, "error",
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_stores(out, "error", 2 0 2
(void)hv_stores(out, "error", 2 0 2
441 (err && SvOK(err)) ? newSVsv(err)
442 : newSVpvs("request failed"));
443 2 SvREFCNT_dec((SV *)hh);
444 2 goto done;
445 }
446  
447 5 (void)hv_stores(out, "status", newSViv(status));
448 5 ▶ n = headers ? av_len(headers) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
n = headers ? av_len(headers) + 1 : 0; 5 0 5
449 20 ▶ for (i = 0; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < n; i += 2) { 15 5 20
450 15 SV **k = av_fetch(headers, i, 0);
451 15 SV **v = av_fetch(headers, i + 1, 0);
452 15 ▶ if (k && *k && v && *v) {
Branch: 4 branches
BranchTrueFalseTotal
if (k && *k && v && *v) { 15 0 15
if (k && *k && v && *v) { 15 0 15
if (k && *k && v && *v) { 15 0 15
if (k && *k && v && *v) { 15 0 15
453 15 STRLEN kl; const char *kp = SvPV_const(*k, kl);
454 char lc[128];
455 STRLEN j;
456 15 ▶ if (kl >= sizeof lc) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (kl >= sizeof lc) continue; 0 15 15
457 195 ▶ for (j = 0; j < kl; j++) lc[j] = (char)toLOWER((U8)kp[j]);
Branch: 2 branches
BranchTrueFalseTotal
for (j = 0; j < kl; j++) lc[j] = (char)toLOWER((U8)kp[j]); 25 155 180
for (j = 0; j < kl; j++) lc[j] = (char)toLOWER((U8)kp[j]); 180 15 195
458 15 (void)hv_store(hh, lc, (I32)kl, newSVsv(*v), 0);
459 15 ▶ if (kl == 12 && memEQ(lc, "content-type", 12)) {
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 12 && memEQ(lc, "content-type", 12)) { 5 10 15
if (kl == 12 && memEQ(lc, "content-type", 12)) { 5 0 5
460 5 STRLEN vl; const char *vp = SvPV_const(*v, vl);
461 5 ▶ if (vl >= 16 && memEQ(vp, "application/json", 16))
Branch: 2 branches
BranchTrueFalseTotal
if (vl >= 16 && memEQ(vp, "application/json", 16)) 0 5 5
if (vl >= 16 && memEQ(vp, "application/json", 16)) 0 0 0
462 0 is_json = 1;
463 }
464 }
465 }
466 5 (void)hv_stores(out, "headers", newRV_noinc((SV *)hh));
467  
468 /* transparent CSRF: capture the (rotated) token the server set, so the
469 * next unsafe call can echo it in the token header */
470 5 ▶ if (c->csrf_cookie && c->cli && headers) {
Branch: 3 branches
BranchTrueFalseTotal
if (c->csrf_cookie && c->cli && headers) { 0 5 5
if (c->csrf_cookie && c->cli && headers) { 0 0 0
if (c->csrf_cookie && c->cli && headers) { 0 0 0
471 0 STRLEN wnl; const char *wnp = SvPV_const(c->csrf_cookie, wnl);
472 0 ▶ for (i = 0; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < n; i += 2) { 0 0 0
473 0 SV **k = av_fetch(headers, i, 0);
474 0 SV **v = av_fetch(headers, i + 1, 0);
475 STRLEN kl, vl; const char *kp, *vp;
476 0 ▶ if (!k || !*k || !v || !*v) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!k || !*k || !v || !*v) continue; 0 0 0
if (!k || !*k || !v || !*v) continue; 0 0 0
if (!k || !*k || !v || !*v) continue; 0 0 0
if (!k || !*k || !v || !*v) continue; 0 0 0
477 0 kp = SvPV_const(*k, kl);
478 0 ▶ if (!(kl == 10 && oa_ci_eq(kp, kl, "set-cookie", 10))) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(kl == 10 && oa_ci_eq(kp, kl, "set-cookie", 10))) continue; 0 0 0
if (!(kl == 10 && oa_ci_eq(kp, kl, "set-cookie", 10))) continue; 0 0 0
479 0 vp = SvPV_const(*v, vl);
480 0 ▶ if (vl > wnl + 1 && memEQ(vp, wnp, wnl) && vp[wnl] == '=') {
Branch: 3 branches
BranchTrueFalseTotal
if (vl > wnl + 1 && memEQ(vp, wnp, wnl) && vp[wnl] == '=') { 0 0 0
if (vl > wnl + 1 && memEQ(vp, wnp, wnl) && vp[wnl] == '=') { 0 0 0
if (vl > wnl + 1 && memEQ(vp, wnp, wnl) && vp[wnl] == '=') { 0 0 0
481 0 STRLEN s = wnl + 1, e = s;
482 0 ▶ while (e < vl && vp[e] != ';') e++;
Branch: 2 branches
BranchTrueFalseTotal
while (e < vl && vp[e] != ';') e++; 0 0 0
while (e < vl && vp[e] != ';') e++; 0 0 0
483 0 (void)hv_stores((HV *)SvRV(c->cli), "_csrf_token",
484 newSVpvn(vp + s, e - s));
485 }
486 }
487 }
488  
489 5 ▶ if (is_json && body && SvOK(body) && SvCUR(body))
Branch: 4 branches
BranchTrueFalseTotal
if (is_json && body && SvOK(body) && SvCUR(body)) 0 5 5
if (is_json && body && SvOK(body) && SvCUR(body)) 0 0 0
if (is_json && body && SvOK(body) && SvCUR(body)) 0 0 0
if (is_json && body && SvOK(body) && SvCUR(body)) 0 0 0
490 0 data = oa_body_decode(aTHX_ body); /* mortal or NULL */
491 5 ▶ if (data) (void)hv_stores(out, "data", newSVsv(data));
Branch: 1 branch
BranchTrueFalseTotal
if (data) (void)hv_stores(out, "data", newSVsv(data)); 0 5 5
492 5 ▶ else (void)hv_stores(out, "data",
Branch: 2 branches
BranchTrueFalseTotal
else (void)hv_stores(out, "data", 5 0 5
else (void)hv_stores(out, "data", 5 0 5
493 body && SvOK(body) ? newSVsv(body) : newSV(0));
494  
495 /* optional response-schema validation */
496 5 ▶ if (c->validate && c->op && data) {
Branch: 3 branches
BranchTrueFalseTotal
if (c->validate && c->op && data) { 5 0 5
if (c->validate && c->op && data) { 0 0 0
if (c->validate && c->op && data) { 0 0 0
497 0 SV *handle = NULL;
498 char sbuf[8];
499 0 STRLEN sl = (STRLEN)my_snprintf(sbuf, sizeof sbuf, "%d", status);
500 int ri;
501 0 ▶ for (ri = 0; ri < c->op->nresps; ri++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ri = 0; ri < c->op->nresps; ri++) { 0 0 0
502 0 STRLEN rl; const char *rp = SvPV_const(c->op->resps[ri].status, rl);
503 0 ▶ if (rl == sl && memEQ(rp, sbuf, sl))
Branch: 2 branches
BranchTrueFalseTotal
if (rl == sl && memEQ(rp, sbuf, sl)) 0 0 0
if (rl == sl && memEQ(rp, sbuf, sl)) 0 0 0
504 0 { handle = c->op->resps[ri].handle; break; }
505 }
506 0 ▶ if (!handle) { /* a range key: 2XX, 4XX, ... (same order as the server) */
Branch: 1 branch
BranchTrueFalseTotal
if (!handle) { /* a range key: 2XX, 4XX, ... (same order as the server) */ 0 0 0
507 0 ▶ for (ri = 0; ri < c->op->nresps; ri++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ri = 0; ri < c->op->nresps; ri++) { 0 0 0
508 0 STRLEN rl; const char *rp = SvPV_const(c->op->resps[ri].status, rl);
509 0 ▶ if (oa_status_covers(rp, rl, sbuf, sl))
Branch: 1 branch
BranchTrueFalseTotal
if (oa_status_covers(rp, rl, sbuf, sl)) 0 0 0
510 0 { handle = c->op->resps[ri].handle; break; }
511 }
512 }
513 0 ▶ if (!handle) {
Branch: 1 branch
BranchTrueFalseTotal
if (!handle) { 0 0 0
514 0 ▶ for (ri = 0; ri < c->op->nresps; ri++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ri = 0; ri < c->op->nresps; ri++) { 0 0 0
515 0 STRLEN rl; const char *rp = SvPV_const(c->op->resps[ri].status, rl);
516 0 ▶ if (rl == 7 && memEQ(rp, "default", 7))
Branch: 2 branches
BranchTrueFalseTotal
if (rl == 7 && memEQ(rp, "default", 7)) 0 0 0
if (rl == 7 && memEQ(rp, "default", 7)) 0 0 0
517 0 { handle = c->op->resps[ri].handle; break; }
518 }
519 }
520 0 ▶ if (handle) {
Branch: 1 branch
BranchTrueFalseTotal
if (handle) { 0 0 0
521 0 AV *errs = newAV();
522 0 ▶ if (!JSF->validate(aTHX_ handle, data, errs)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!JSF->validate(aTHX_ handle, data, errs)) { 0 0 0
523 0 (void)hv_stores(out, "error",
524 newSVpvs("response validation failed"));
525 0 (void)hv_stores(out, "errors", newRV_noinc((SV *)errs));
526 } else {
527 0 SvREFCNT_dec((SV *)errs);
528 }
529 }
530 }
531  
532 5 done:
533 7 ▶ if (c) {
Branch: 1 branch
BranchTrueFalseTotal
if (c) { 7 0 7
534 7 ▶ if (c->api_sv) SvREFCNT_dec(c->api_sv);
Branch: 1 branch
BranchTrueFalseTotal
if (c->api_sv) SvREFCNT_dec(c->api_sv); 7 0 7
535 7 ▶ if (c->cli) SvREFCNT_dec(c->cli);
Branch: 1 branch
BranchTrueFalseTotal
if (c->cli) SvREFCNT_dec(c->cli); 0 7 7
536 7 ▶ if (c->csrf_cookie) SvREFCNT_dec(c->csrf_cookie);
Branch: 1 branch
BranchTrueFalseTotal
if (c->csrf_cookie) SvREFCNT_dec(c->csrf_cookie); 0 7 7
537 7 Safefree(c);
538 }
539 7 return newRV_noinc((SV *)out);
540 }
541  
542 /* ---- call ----------------------------------------------------------------------- */
543  
544 /* the client HV field, or NULL */
545 219 static SV *oa_cli_get(pTHX_ HV *self, const char *k) {
546 219 return oa_get(aTHX_ self, k);
547 }
548  
549 /* lazily build (and cache) the client's Fetch UA from ua_opts */
550 7 static SV *oa_cli_ua(pTHX_ HV *self) {
551 7 SV *ua = oa_cli_get(aTHX_ self, "ua");
552 AV *opts;
553 SV *kv[16];
554 7 int nkv = 0;
555 7 ▶ if (ua && SvROK(ua)) return ua;
Branch: 2 branches
BranchTrueFalseTotal
if (ua && SvROK(ua)) return ua; 1 6 7
if (ua && SvROK(ua)) return ua; 1 0 1
556 6 opts = oa_av_of(oa_cli_get(aTHX_ self, "ua_opts"));
557 6 ▶ if (opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (opts) { 6 0 6
558 6 SSize_t i, n = av_len(opts) + 1;
559 6 ▶ for (i = 0; i < n && nkv < 16; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n && nkv < 16; i++) { 0 6 6
for (i = 0; i < n && nkv < 16; i++) { 0 0 0
560 0 SV **e = av_fetch(opts, i, 0);
561 0 ▶ kv[nkv++] = (e && *e) ? *e : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
kv[nkv++] = (e && *e) ? *e : &PL_sv_undef; 0 0 0
kv[nkv++] = (e && *e) ? *e : &PL_sv_undef; 0 0 0
562 }
563 }
564 6 ua = OA_FETCH->ua_new(aTHX_ kv, nkv); /* +1 */
565 6 (void)hv_stores(self, "ua", ua); /* HV takes the ref */
566 6 return ua;
567 }
568  
569 /* "user:pass" (or an [user, pass] arrayref) -> mortal "Basic base64" value */
570 0 static SV *oa_cli_basic(pTHX_ SV *cred) {
571 0 AV *av = oa_av_of(cred);
572 0 SV *joined = cred;
573 SV *out;
574 STRLEN l; const char *pv;
575 0 ▶ if (av) {
Branch: 1 branch
BranchTrueFalseTotal
if (av) { 0 0 0
576 0 SV **u = av_fetch(av, 0, 0), **p = av_fetch(av, 1, 0);
577 0 joined = sv_2mortal(newSVpvs(""));
578 0 ▶ if (u && *u && SvOK(*u)) sv_catsv(joined, *u);
Branch: 3 branches
BranchTrueFalseTotal
if (u && *u && SvOK(*u)) sv_catsv(joined, *u); 0 0 0
if (u && *u && SvOK(*u)) sv_catsv(joined, *u); 0 0 0
if (u && *u && SvOK(*u)) sv_catsv(joined, *u); 0 0 0
579 0 sv_catpvs(joined, ":");
580 0 ▶ if (p && *p && SvOK(*p)) sv_catsv(joined, *p);
Branch: 3 branches
BranchTrueFalseTotal
if (p && *p && SvOK(*p)) sv_catsv(joined, *p); 0 0 0
if (p && *p && SvOK(*p)) sv_catsv(joined, *p); 0 0 0
if (p && *p && SvOK(*p)) sv_catsv(joined, *p); 0 0 0
581 }
582 0 pv = SvPV_const(joined, l);
583 0 out = sv_2mortal(newSVpvs("Basic "));
584 0 sv_catsv(out, sv_2mortal(oa_b64_encode(aTHX_ pv, l)));
585 0 return out;
586 }
587  
588 /* An apiKey credential selected from the op's security requirements, to be
589 * attached as a header, a query pair or a cookie. Named rather than anonymous
590 * because oa_cli_url below takes the query ones as an argument. */
591 typedef struct oa_cli_sec { SV *name; SV *val; } oa_cli_sec;
592  
593 /* The request URL: base + interpolated path + query. Split out of
594 * oa_cli_call so the same construction can be inspected without firing a
595 * request - a client's serialization rules (allowReserved) are otherwise
596 * observable only by standing up a real server. `sec_q`/`nsec_q` carry
597 * apiKey-in-query credentials; pass 0 for none. Mortal. */
598 81 static SV *oa_cli_url(pTHX_ oa_op *o, HV *params, SV *base,
599 const oa_cli_sec *sec_q, int nsec_q) {
600 81 SV *url = sv_2mortal(newSVpvs(""));
601 int i;
602  
603 81 ▶ if (base && SvOK(base)) {
Branch: 2 branches
BranchTrueFalseTotal
if (base && SvOK(base)) { 81 0 81
if (base && SvOK(base)) { 81 0 81
604 81 STRLEN bl; const char *bpv = SvPV_const(base, bl);
605 81 ▶ while (bl && bpv[bl - 1] == '/') bl--;
Branch: 2 branches
BranchTrueFalseTotal
while (bl && bpv[bl - 1] == '/') bl--; 81 0 81
while (bl && bpv[bl - 1] == '/') bl--; 0 81 81
606 81 sv_catpvn(url, bpv, bl);
607 }
608 204 ▶ for (i = 0; i < o->nsegs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < o->nsegs; i++) { 123 81 204
609 123 sv_catpvs(url, "/");
610 123 ▶ if (o->segs[i].lit) {
Branch: 1 branch
BranchTrueFalseTotal
if (o->segs[i].lit) { 81 42 123
611 81 sv_catsv(url, o->segs[i].lit);
612 } else {
613 42 STRLEN nl; const char *np = SvPV_const(o->segs[i].pname, nl);
614 42 SV **v = hv_fetch(params, np, (I32)nl, 0);
615 SV *w;
616 42 oa_param *decl = NULL;
617 int k;
618 /* declared path params were checked by the caller, but a template
619 * var the spec never declared as a parameter reaches here
620 * unchecked */
621 42 ▶ if (!v || !*v || !SvOK(*v))
Branch: 3 branches
BranchTrueFalseTotal
if (!v || !*v || !SvOK(*v)) 42 0 42
if (!v || !*v || !SvOK(*v)) 42 0 42
if (!v || !*v || !SvOK(*v)) 0 42 42
622 0 croak("Open::API::Client: %s: missing required path parameter "
623 "'%.*s'", SvPV_nolen(o->op_id), (int)nl, np);
624 42 w = *v;
625 42 ▶ for (k = 0; k < o->nparams[OA_IN_PATH]; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < o->nparams[OA_IN_PATH]; k++) 42 0 42
626 42 ▶ if (sv_eq(o->params[OA_IN_PATH][k].name, o->segs[i].pname)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_eq(o->params[OA_IN_PATH][k].name, o->segs[i].pname)) { 42 0 42
627 42 decl = &o->params[OA_IN_PATH][k];
628 42 w = oa_cli_wire(aTHX_ decl, *v);
629 42 break;
630 }
631 /* Always fully encoded: a reserved character in a path segment
632 * changes the SHAPE of the path, so allowReserved does not reach
633 * here even when a path parameter declares it. The style's own
634 * structural characters are written by the serializer and are not
635 * encoded - they ARE the shape. */
636 42 ▶ if (decl) {
Branch: 1 branch
BranchTrueFalseTotal
if (decl) { 42 0 42
637 42 oa_cli_ser_path(aTHX_ decl, w, url);
638 } else {
639 0 STRLEN vl; const char *vp = SvPV_const(w, vl);
640 0 oa_pct_encode_into(aTHX_ url, vp, vl);
641 }
642 }
643 }
644 81 ▶ if (!o->nsegs) sv_catpvs(url, "/");
Branch: 1 branch
BranchTrueFalseTotal
if (!o->nsegs) sv_catpvs(url, "/"); 0 81 81
645 {
646 81 int first = 1;
647 116 ▶ for (i = 0; i < o->nparams[OA_IN_QUERY]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < o->nparams[OA_IN_QUERY]; i++) { 35 81 116
648 35 oa_param *pp = &o->params[OA_IN_QUERY][i];
649 35 STRLEN nl; const char *np = SvPV_const(pp->name, nl);
650 35 SV **v = hv_fetch(params, np, (I32)nl, 0);
651 int structured;
652 35 ▶ if (!v || !*v || !SvOK(*v)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!v || !*v || !SvOK(*v)) continue; 35 0 35
if (!v || !*v || !SvOK(*v)) continue; 35 0 35
if (!v || !*v || !SvOK(*v)) continue; 0 35 35
653 /* a content parameter's arrayref is its DOCUMENT, not a list to
654 * serialize per style: it goes through oa_cli_wire as one value */
655 35 ▶ structured = !pp->ctype && SvROK(*v)
Branch: 1 branch
BranchTrueFalseTotal
structured = !pp->ctype && SvROK(*v) 26 9 35
656 75 ▶ && (SvTYPE(SvRV(*v)) == SVt_PVAV
Branch: 2 branches
BranchTrueFalseTotal
&& (SvTYPE(SvRV(*v)) == SVt_PVAV 35 0 35
&& (SvTYPE(SvRV(*v)) == SVt_PVAV 5 21 26
657 5 ▶ || SvTYPE(SvRV(*v)) == SVt_PVHV);
Branch: 1 branch
BranchTrueFalseTotal
|| SvTYPE(SvRV(*v)) == SVt_PVHV); 5 0 5
658 35 ▶ if (structured) {
Branch: 1 branch
BranchTrueFalseTotal
if (structured) { 26 9 35
659 26 oa_cli_ser_query(aTHX_ pp, *v, url, &first);
660 } else {
661 9 SV *w = oa_cli_wire(aTHX_ pp, *v);
662 9 STRLEN vl; const char *vp = SvPV_const(w, vl);
663 9 ▶ sv_catpvn(url, first ? "?" : "&", 1); first = 0;
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvn(url, first ? "?" : "&", 1); first = 0; 9 0 9
664 9 oa_pct_encode_into(aTHX_ url, np, nl);
665 9 sv_catpvs(url, "=");
666 9 oa_pct_encode_res(aTHX_ url, vp, vl, pp->allow_reserved);
667 }
668 }
669 81 ▶ for (i = 0; i < nsec_q; i++) { /* apiKey-in-query schemes */
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nsec_q; i++) { /* apiKey-in-query schemes */ 0 81 81
670 STRLEN nl2, vl2;
671 0 const char *np2 = SvPV_const(sec_q[i].name, nl2);
672 0 ▶ const char *vp2 = SvPV_const(sec_q[i].val, vl2);
Branch: 1 branch
BranchTrueFalseTotal
const char *vp2 = SvPV_const(sec_q[i].val, vl2); 0 0 0
673 0 ▶ sv_catpvn(url, first ? "?" : "&", 1); first = 0;
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvn(url, first ? "?" : "&", 1); first = 0; 0 0 0
674 0 oa_pct_encode_into(aTHX_ url, np2, nl2);
675 0 sv_catpvs(url, "=");
676 0 oa_pct_encode_into(aTHX_ url, vp2, vl2);
677 }
678 }
679 81 return url;
680 }
681  
682 /* ---- the request body, per the media type the operation DECLARES ------------
683 *
684 * The builder used to JSON-encode whatever it was handed and label it
685 * `application/json`, whatever the document said. An operation declaring only
686 * `application/x-www-form-urlencoded` therefore sent JSON under the wrong
687 * Content-Type, and this library's OWN server answered 415: a client and a
688 * server generated from one document could not talk to each other.
689 *
690 * Split out of oa_cli_call so `_request_body` can expose it, the way
691 * oa_cli_url is exposed as `_request_url`. Untestable otherwise: a body that
692 * only exists inside a live request cannot be asserted against.
693 */
694  
695 /* Which declared body to send. JSON when the operation declares it - the
696 * safest default and what callers already relied on - then form, then
697 * multipart, then whatever is first. */
698 18 static oa_body *oa_cli_pick_body(oa_op *o) {
699 int i;
700 18 oa_body *form = NULL, *multi = NULL, *any = NULL;
701 30 ▶ for (i = 0; i < o->nbodies; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < o->nbodies; i++) { 18 12 30
702 18 oa_body *b = &o->bodies[i];
703 18 ▶ if (b->kind == OA_MT_JSON) return b;
Branch: 1 branch
BranchTrueFalseTotal
if (b->kind == OA_MT_JSON) return b; 6 12 18
704 12 ▶ if (!form && b->kind == OA_MT_FORM) form = b;
Branch: 2 branches
BranchTrueFalseTotal
if (!form && b->kind == OA_MT_FORM) form = b; 12 0 12
if (!form && b->kind == OA_MT_FORM) form = b; 9 3 12
705 12 ▶ if (!multi && b->kind == OA_MT_MULTIPART) multi = b;
Branch: 2 branches
BranchTrueFalseTotal
if (!multi && b->kind == OA_MT_MULTIPART) multi = b; 12 0 12
if (!multi && b->kind == OA_MT_MULTIPART) multi = b; 3 9 12
706 12 ▶ if (!any) any = b;
Branch: 1 branch
BranchTrueFalseTotal
if (!any) any = b; 12 0 12
707 }
708 12 ▶ return form ? form : multi ? multi : any;
Branch: 2 branches
BranchTrueFalseTotal
return form ? form : multi ? multi : any; 3 9 12
return form ? form : multi ? multi : any; 3 0 3
709 }
710  
711 /* `a=1&b=2`, with an array member repeated unless its Encoding says otherwise.
712 * Members are sorted for the same reason the URL builder sorts them: hash
713 * order is randomised and a body that changes between runs is not one a test
714 * or a signature can rely on. */
715 /* The delimiter an Encoding Object entry gives an array member, as a URL
716 * spelling, or NULL when the member is repeated (`form` exploded, the
717 * default). The entry's style/explode is the server's reading too
718 * (oa_form_decode), so the two halves cannot drift apart on it. */
719 5 static const char *oa_cli_form_delim(pTHX_ SV *encoding, const char *kp, STRLEN kl) {
720 5 HV *enc = oa_hv_of(encoding), *eh;
721 SV **es, *st, *ex;
722 5 STRLEN sl = 0; const char *sp = NULL;
723 int exploded;
724 5 ▶ if (!enc) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!enc) return NULL; 0 5 5
725 5 es = hv_fetch(enc, kp, (I32)kl, 0);
726 5 ▶ eh = (es && *es) ? oa_hv_of(*es) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
eh = (es && *es) ? oa_hv_of(*es) : NULL; 5 0 5
eh = (es && *es) ? oa_hv_of(*es) : NULL; 5 0 5
727 5 ▶ if (!eh) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!eh) return NULL; 0 5 5
728 5 ▶ if (oa_cf_delim(aTHX_ oa_get(aTHX_ eh, "x-oa-collectionFormat")) == '\t') return "%09";
Branch: 1 branch
BranchTrueFalseTotal
if (oa_cf_delim(aTHX_ oa_get(aTHX_ eh, "x-oa-collectionFormat")) == '\t') return "%09"; 1 4 5
729 4 st = oa_get(aTHX_ eh, "style");
730 4 ex = oa_get(aTHX_ eh, "explode");
731 8 ▶ if (st) sp = SvPV_const(st, sl);
Branch: 1 branch
BranchTrueFalseTotal
if (st) sp = SvPV_const(st, sl); 4 0 4
732 4 ▶ if (sp && sl == 14 && memEQ(sp, "spaceDelimited", 14)) return "%20";
Branch: 3 branches
BranchTrueFalseTotal
if (sp && sl == 14 && memEQ(sp, "spaceDelimited", 14)) return "%20"; 4 0 4
if (sp && sl == 14 && memEQ(sp, "spaceDelimited", 14)) return "%20"; 1 3 4
if (sp && sl == 14 && memEQ(sp, "spaceDelimited", 14)) return "%20"; 1 0 1
733 3 ▶ if (sp && sl == 13 && memEQ(sp, "pipeDelimited", 13)) return "|";
Branch: 3 branches
BranchTrueFalseTotal
if (sp && sl == 13 && memEQ(sp, "pipeDelimited", 13)) return "|"; 3 0 3
if (sp && sl == 13 && memEQ(sp, "pipeDelimited", 13)) return "|"; 1 2 3
if (sp && sl == 13 && memEQ(sp, "pipeDelimited", 13)) return "|"; 1 0 1
734 2 ▶ exploded = ex ? (oa_sv_truthy(aTHX_ ex) ? 1 : 0) : 1;
Branch: 2 branches
BranchTrueFalseTotal
exploded = ex ? (oa_sv_truthy(aTHX_ ex) ? 1 : 0) : 1; 2 0 2
exploded = ex ? (oa_sv_truthy(aTHX_ ex) ? 1 : 0) : 1; 1 1 2
735 2 ▶ return exploded ? NULL : ",";
Branch: 1 branch
BranchTrueFalseTotal
return exploded ? NULL : ","; 1 1 2
736 }
737  
738 9 static SV *oa_cli_form_encode(pTHX_ oa_op *o, SV *val, SV *encoding) {
739 9 SV *out = sv_2mortal(newSVpvs(""));
740 HV *hv;
741 AV *keys;
742 SSize_t i, n;
743 9 int first = 1;
744 9 ▶ if (!val || !SvROK(val) || SvTYPE(SvRV(val)) != SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if (!val || !SvROK(val) || SvTYPE(SvRV(val)) != SVt_PVHV) 9 0 9
if (!val || !SvROK(val) || SvTYPE(SvRV(val)) != SVt_PVHV) 9 0 9
if (!val || !SvROK(val) || SvTYPE(SvRV(val)) != SVt_PVHV) 0 9 9
745 0 croak("Open::API::Client: %s: a form body wants a hashref",
746 SvPV_nolen(o->op_id));
747 9 hv = (HV *)SvRV(val);
748 9 keys = oa_cli_sorted_keys(aTHX_ hv);
749 9 n = av_len(keys) + 1;
750 21 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 12 9 21
751 12 SV **k = av_fetch(keys, i, 0);
752 SV **v;
753 STRLEN kl; const char *kp;
754 AV *av;
755 17 ▶ if (!k || !*k) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!k || !*k) continue; 12 0 12
if (!k || !*k) continue; 0 12 12
756 12 kp = SvPV_const(*k, kl);
757 12 v = hv_fetch(hv, kp, (I32)kl, 0);
758 12 ▶ if (!v || !*v || !SvOK(*v)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!v || !*v || !SvOK(*v)) continue; 12 0 12
if (!v || !*v || !SvOK(*v)) continue; 12 0 12
if (!v || !*v || !SvOK(*v)) continue; 0 12 12
759 12 av = oa_av_of(*v);
760 12 ▶ if (av) {
Branch: 1 branch
BranchTrueFalseTotal
if (av) { 5 7 12
761 5 SSize_t j, m = av_len(av) + 1;
762 5 const char *dsep = oa_cli_form_delim(aTHX_ encoding, kp, kl);
763 5 ▶ if (dsep) { /* one value, delimited */
Branch: 1 branch
BranchTrueFalseTotal
if (dsep) { /* one value, delimited */ 4 1 5
764 4 ▶ if (!first) sv_catpvs(out, "&");
Branch: 1 branch
BranchTrueFalseTotal
if (!first) sv_catpvs(out, "&"); 0 4 4
765 4 first = 0;
766 4 oa_pct_encode_into(aTHX_ out, kp, kl);
767 4 sv_catpvs(out, "=");
768 16 ▶ for (j = 0; j < m; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < m; j++) { 12 4 16
769 12 SV **e = av_fetch(av, j, 0);
770 12 ▶ if (j) sv_catpv(out, dsep);
Branch: 1 branch
BranchTrueFalseTotal
if (j) sv_catpv(out, dsep); 8 4 12
771 12 ▶ if (e && *e && SvOK(*e)) oa_cli_val(aTHX_ out, *e, 0);
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e)) oa_cli_val(aTHX_ out, *e, 0); 12 0 12
if (e && *e && SvOK(*e)) oa_cli_val(aTHX_ out, *e, 0); 12 0 12
if (e && *e && SvOK(*e)) oa_cli_val(aTHX_ out, *e, 0); 12 0 12
772 }
773 4 continue;
774 }
775 4 ▶ for (j = 0; j < m; j++) { /* repeated keys */
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < m; j++) { /* repeated keys */ 3 1 4
776 3 SV **e = av_fetch(av, j, 0);
777 3 ▶ if (!e || !*e || !SvOK(*e)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!e || !*e || !SvOK(*e)) continue; 3 0 3
if (!e || !*e || !SvOK(*e)) continue; 3 0 3
if (!e || !*e || !SvOK(*e)) continue; 0 3 3
778 3 ▶ if (!first) sv_catpvs(out, "&");
Branch: 1 branch
BranchTrueFalseTotal
if (!first) sv_catpvs(out, "&"); 2 1 3
779 3 first = 0;
780 3 oa_pct_encode_into(aTHX_ out, kp, kl);
781 3 sv_catpvs(out, "=");
782 3 oa_cli_val(aTHX_ out, *e, 0);
783 }
784 1 continue;
785 }
786 7 ▶ if (!first) sv_catpvs(out, "&");
Branch: 1 branch
BranchTrueFalseTotal
if (!first) sv_catpvs(out, "&"); 3 4 7
787 7 first = 0;
788 7 oa_pct_encode_into(aTHX_ out, kp, kl);
789 7 sv_catpvs(out, "=");
790 7 oa_cli_val(aTHX_ out, *v, 0);
791 }
792 9 return out;
793 }
794  
795 /* multipart/form-data. The boundary is fixed rather than random: this library
796 * sends no binary parts of its own, a body that differs run to run cannot be
797 * asserted, and the value is checked against the payload below. */
798 3 static SV *oa_cli_multipart_encode(pTHX_ oa_op *o, SV *val, const char *bnd) {
799 3 SV *out = sv_2mortal(newSVpvs(""));
800 HV *hv;
801 AV *keys;
802 SSize_t i, n;
803 3 ▶ if (!val || !SvROK(val) || SvTYPE(SvRV(val)) != SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if (!val || !SvROK(val) || SvTYPE(SvRV(val)) != SVt_PVHV) 3 0 3
if (!val || !SvROK(val) || SvTYPE(SvRV(val)) != SVt_PVHV) 3 0 3
if (!val || !SvROK(val) || SvTYPE(SvRV(val)) != SVt_PVHV) 0 3 3
804 0 croak("Open::API::Client: %s: a multipart body wants a hashref",
805 SvPV_nolen(o->op_id));
806 3 hv = (HV *)SvRV(val);
807 3 keys = oa_cli_sorted_keys(aTHX_ hv);
808 3 n = av_len(keys) + 1;
809 9 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 6 3 9
810 6 SV **k = av_fetch(keys, i, 0);
811 SV **v;
812 STRLEN kl, vl; const char *kp, *vp;
813 6 ▶ if (!k || !*k) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!k || !*k) continue; 6 0 6
if (!k || !*k) continue; 0 6 6
814 6 kp = SvPV_const(*k, kl);
815 6 v = hv_fetch(hv, kp, (I32)kl, 0);
816 6 ▶ if (!v || !*v || !SvOK(*v)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!v || !*v || !SvOK(*v)) continue; 6 0 6
if (!v || !*v || !SvOK(*v)) continue; 6 0 6
if (!v || !*v || !SvOK(*v)) continue; 0 6 6
817 /* a boundary that appears inside a part would end it early */
818 6 vp = SvPV_const(*v, vl);
819 6 ▶ if (vl && ninstr(vp, vp + vl, bnd, bnd + strlen(bnd)))
Branch: 2 branches
BranchTrueFalseTotal
if (vl && ninstr(vp, vp + vl, bnd, bnd + strlen(bnd))) 6 0 6
if (vl && ninstr(vp, vp + vl, bnd, bnd + strlen(bnd))) 0 6 6
820 0 croak("Open::API::Client: %s: a multipart value contains the "
821 "boundary", SvPV_nolen(o->op_id));
822 6 sv_catpvs(out, "--"); sv_catpv(out, bnd); sv_catpvs(out, "\r\n");
823 6 sv_catpvs(out, "Content-Disposition: form-data; name=\"");
824 6 sv_catpvn(out, kp, kl);
825 6 sv_catpvs(out, "\"\r\n\r\n");
826 6 sv_catpvn(out, vp, vl);
827 6 sv_catpvs(out, "\r\n");
828 }
829 3 sv_catpvs(out, "--"); sv_catpv(out, bnd); sv_catpvs(out, "--\r\n");
830 3 return out;
831 }
832  
833 #define OA_CLI_BOUNDARY "OpenAPIClientBoundary1"
834  
835 /* Returns a mortal body SV, or NULL when the operation sends none. *ctype is
836 * set to a mortal Content-Type SV whenever a body is returned. */
837 27 static SV *oa_cli_build_body(pTHX_ oa_op *o, HV *params, SV **ctype) {
838 27 SV **bv = hv_fetchs(params, "body", 0);
839 27 ▶ SV *bsv = (bv && *bv && SvOK(*bv)) ? *bv : NULL;
Branch: 3 branches
BranchTrueFalseTotal
SV *bsv = (bv && *bv && SvOK(*bv)) ? *bv : NULL; 18 9 27
SV *bsv = (bv && *bv && SvOK(*bv)) ? *bv : NULL; 18 0 18
SV *bsv = (bv && *bv && SvOK(*bv)) ? *bv : NULL; 18 0 18
840 oa_body *b;
841 SV *out;
842  
843 27 *ctype = NULL;
844 27 ▶ if (!o->nbodies) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!o->nbodies) return NULL; 7 20 27
845 20 ▶ if (!bsv) {
Branch: 1 branch
BranchTrueFalseTotal
if (!bsv) { 2 18 20
846 2 ▶ if (o->body_required)
Branch: 1 branch
BranchTrueFalseTotal
if (o->body_required) 2 0 2
847 4 croak("Open::API::Client: %s: missing required body",
848 SvPV_nolen(o->op_id));
849 0 return NULL;
850 }
851 18 b = oa_cli_pick_body(o);
852 18 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 18 18
853  
854 /* the declared schema still decides, whatever the media type */
855 18 ▶ if (b->handle && !JSF->is_valid(aTHX_ b->handle, bsv))
Branch: 2 branches
BranchTrueFalseTotal
if (b->handle && !JSF->is_valid(aTHX_ b->handle, bsv)) 18 0 18
if (b->handle && !JSF->is_valid(aTHX_ b->handle, bsv)) 1 17 18
856 1 oa_cli_bad(aTHX_ o, "body", NULL, bsv, b->handle);
857  
858 17 ▶ if (b->kind == OA_MT_FORM) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->kind == OA_MT_FORM) { 9 8 17
859 9 out = oa_cli_form_encode(aTHX_ o, bsv, b->encoding);
860 9 *ctype = sv_2mortal(newSVpvs("application/x-www-form-urlencoded"));
861 9 return out;
862 }
863 8 ▶ if (b->kind == OA_MT_MULTIPART) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->kind == OA_MT_MULTIPART) { 3 5 8
864 3 out = oa_cli_multipart_encode(aTHX_ o, bsv, OA_CLI_BOUNDARY);
865 3 *ctype = sv_2mortal(newSVpvs("multipart/form-data; boundary="
866 OA_CLI_BOUNDARY));
867 3 return out;
868 }
869 5 ▶ if (b->kind == OA_MT_JSON) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->kind == OA_MT_JSON) { 5 0 5
870 5 out = oa_json_encode(aTHX_ bsv);
871 5 ▶ if (!out) croak("Open::API::Client: %s: could not encode body",
Branch: 1 branch
BranchTrueFalseTotal
if (!out) croak("Open::API::Client: %s: could not encode body", 0 5 5
872 SvPV_nolen(o->op_id));
873 5 *ctype = sv_2mortal(newSVsv(b->ctype));
874 5 return out;
875 }
876 /* opaque: send what the caller gave, under the declared type */
877 0 ▶ if (SvROK(bsv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(bsv)) 0 0 0
878 0 croak("Open::API::Client: %s: %" SVf " wants a plain string body",
879 SvPV_nolen(o->op_id), SVfARG(b->ctype));
880 0 out = sv_2mortal(newSVsv(bsv));
881 0 *ctype = sv_2mortal(newSVsv(b->ctype));
882 0 return out;
883 }
884  
885 /* Build and fire one call. params is the flat name => value HV. Returns the
886 * request future (+1). */
887 13 static SV *oa_cli_call(pTHX_ HV *self, SV *op_id, HV *params) {
888 13 SV *api_sv = oa_cli_get(aTHX_ self, "api");
889 oa_api *a;
890 oa_op *o;
891 13 SV *base = oa_cli_get(aTHX_ self, "base_url");
892 SV *url;
893 fetch_hdr hdrs[32];
894 13 int nh = 0;
895 13 SV *cookie = NULL, *bodyjson = NULL;
896 13 const char *bp = NULL;
897 13 STRLEN blen = 0;
898 char method[16];
899 STRLEN ml, mk;
900 const char *mmp;
901 oa_cli_ctx *ctx;
902 int loc, i;
903 SV *ua, *csrf_cfg;
904 13 SV *csrf_cookie_name = NULL; /* set when transparent CSRF is enabled */
905 /* security attachments, selected from the op's requirements below */
906 13 SV *sec_auth = NULL; /* Authorization */
907 oa_cli_sec sec_h[8], sec_q[8], sec_c[8];
908 13 int nsec_h = 0, nsec_q = 0, nsec_c = 0;
909  
910 13 ▶ if (!api_sv || !SvROK(api_sv))
Branch: 2 branches
BranchTrueFalseTotal
if (!api_sv || !SvROK(api_sv)) 13 0 13
if (!api_sv || !SvROK(api_sv)) 0 13 13
911 0 croak("Open::API::Client: no api on this client");
912 13 a = (oa_api *)INT2PTR(void *, SvIV(SvRV(api_sv)));
913 13 o = oa_op_by_id(aTHX_ a, op_id);
914 14 ▶ if (!o) croak("Open::API::Client: unknown operationId '%s'",
Branch: 1 branch
BranchTrueFalseTotal
if (!o) croak("Open::API::Client: unknown operationId '%s'", 1 12 13
915 SvPV_nolen(op_id));
916 12 ▶ if (!oa_fetch_init(aTHX))
Branch: 1 branch
BranchTrueFalseTotal
if (!oa_fetch_init(aTHX)) 0 12 12
917 0 croak("Open::API::Client requires Fetch with its C ABI "
918 "(Fetch 0.05 or later)");
919  
920 /* ---- security: first requirement alternative the credentials cover ---- */
921 12 ▶ if (o->nsec) {
Branch: 1 branch
BranchTrueFalseTotal
if (o->nsec) { 0 12 12
922 0 HV *creds = oa_hv_of(oa_cli_get(aTHX_ self, "security"));
923 0 oa_ops *t = (oa_ops *)a->ops;
924 0 int ai, found = -1;
925 0 ▶ for (ai = 0; ai < o->nsec && found < 0; ai++) {
Branch: 2 branches
BranchTrueFalseTotal
for (ai = 0; ai < o->nsec && found < 0; ai++) { 0 0 0
for (ai = 0; ai < o->nsec && found < 0; ai++) { 0 0 0
926 0 int ii, all = 1;
927 0 ▶ for (ii = 0; ii < o->sec[ai].n; ii++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ii = 0; ii < o->sec[ai].n; ii++) { 0 0 0
928 0 oa_scheme *s = &t->schemes[o->sec[ai].items[ii].scheme];
929 0 STRLEN nl; const char *np = SvPV_const(s->name, nl);
930 0 ▶ SV **c = creds ? hv_fetch(creds, np, (I32)nl, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **c = creds ? hv_fetch(creds, np, (I32)nl, 0) : NULL; 0 0 0
931 0 ▶ if (!c || !*c || !SvOK(*c)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!c || !*c || !SvOK(*c)) { 0 0 0
if (!c || !*c || !SvOK(*c)) { 0 0 0
if (!c || !*c || !SvOK(*c)) { 0 0 0
932 /* an apiKey-in-cookie (e.g. a session cookie set at login)
933 * is carried ambiently by the cookie jar, so it needs no
934 * explicit credential; every other scheme does */
935 0 ▶ if (!(s->type == OA_SEC_APIKEY && s->loc == OA_IN_COOKIE))
Branch: 2 branches
BranchTrueFalseTotal
if (!(s->type == OA_SEC_APIKEY && s->loc == OA_IN_COOKIE)) 0 0 0
if (!(s->type == OA_SEC_APIKEY && s->loc == OA_IN_COOKIE)) 0 0 0
936 0 { all = 0; break; }
937 }
938 }
939 0 ▶ if (all) found = ai;
Branch: 1 branch
BranchTrueFalseTotal
if (all) found = ai; 0 0 0
940 }
941 0 ▶ if (found < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (found < 0) 0 0 0
942 0 croak("Open::API::Client: %s: no satisfiable security requirement "
943 "- pass security => { scheme_name => credential }",
944 SvPV_nolen(o->op_id));
945 0 ▶ for (i = 0; i < o->sec[found].n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < o->sec[found].n; i++) { 0 0 0
946 0 oa_scheme *s = &t->schemes[o->sec[found].items[i].scheme];
947 0 STRLEN nl; const char *np = SvPV_const(s->name, nl);
948 0 ▶ SV **cp = creds ? hv_fetch(creds, np, (I32)nl, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **cp = creds ? hv_fetch(creds, np, (I32)nl, 0) : NULL; 0 0 0
949 0 ▶ SV *cred = (cp && *cp && SvOK(*cp)) ? *cp : NULL;
Branch: 3 branches
BranchTrueFalseTotal
SV *cred = (cp && *cp && SvOK(*cp)) ? *cp : NULL; 0 0 0
SV *cred = (cp && *cp && SvOK(*cp)) ? *cp : NULL; 0 0 0
SV *cred = (cp && *cp && SvOK(*cp)) ? *cp : NULL; 0 0 0
950 0 switch (s->type) {
951 0 case OA_SEC_APIKEY:
952 0 ▶ if (!cred) break; /* cookie carried by the jar: nothing to add */
Branch: 1 branch
BranchTrueFalseTotal
if (!cred) break; /* cookie carried by the jar: nothing to add */ 0 0 0
953 0 ▶ if (s->loc == OA_IN_HEADER && nsec_h < 8)
Branch: 2 branches
BranchTrueFalseTotal
if (s->loc == OA_IN_HEADER && nsec_h < 8) 0 0 0
if (s->loc == OA_IN_HEADER && nsec_h < 8) 0 0 0
954 0 { sec_h[nsec_h].name = s->pname; sec_h[nsec_h].val = cred; nsec_h++; }
955 0 ▶ else if (s->loc == OA_IN_QUERY && nsec_q < 8)
Branch: 2 branches
BranchTrueFalseTotal
else if (s->loc == OA_IN_QUERY && nsec_q < 8) 0 0 0
else if (s->loc == OA_IN_QUERY && nsec_q < 8) 0 0 0
956 0 { sec_q[nsec_q].name = s->pname; sec_q[nsec_q].val = cred; nsec_q++; }
957 0 ▶ else if (s->loc == OA_IN_COOKIE && nsec_c < 8)
Branch: 2 branches
BranchTrueFalseTotal
else if (s->loc == OA_IN_COOKIE && nsec_c < 8) 0 0 0
else if (s->loc == OA_IN_COOKIE && nsec_c < 8) 0 0 0
958 0 { sec_c[nsec_c].name = s->pname; sec_c[nsec_c].val = cred; nsec_c++; }
959 0 break;
960 0 case OA_SEC_BEARER:
961 0 ▶ if (!cred) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!cred) break; 0 0 0
962 0 sec_auth = sv_2mortal(newSVpvs("Bearer "));
963 0 sv_catsv(sec_auth, cred);
964 0 break;
965 0 case OA_SEC_BASIC:
966 0 ▶ if (!cred) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!cred) break; 0 0 0
967 0 sec_auth = oa_cli_basic(aTHX_ cred);
968 0 break;
969 }
970 }
971 }
972  
973 /* ---- validate every declared parameter (fail before I/O) ---- */
974 51 ▶ for (loc = 0; loc < OA_IN_N; loc++) {
Branch: 1 branch
BranchTrueFalseTotal
for (loc = 0; loc < OA_IN_N; loc++) { 42 9 51
975 52 ▶ for (i = 0; i < o->nparams[loc]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < o->nparams[loc]; i++) { 13 39 52
976 13 oa_param *pp = &o->params[loc][i];
977 13 STRLEN nl; const char *np = SvPV_const(pp->name, nl);
978 13 SV **v = hv_fetch(params, np, (I32)nl, 0);
979 21 ▶ if (v && *v && SvOK(*v)) {
Branch: 3 branches
BranchTrueFalseTotal
if (v && *v && SvOK(*v)) { 9 4 13
if (v && *v && SvOK(*v)) { 9 0 9
if (v && *v && SvOK(*v)) { 9 0 9
980 9 SV *chk = oa_cli_decoded(aTHX_ o, pp, oa_loc_name(loc), *v);
981 9 ▶ if (pp->handle && !JSF->is_valid(aTHX_ pp->handle, chk))
Branch: 2 branches
BranchTrueFalseTotal
if (pp->handle && !JSF->is_valid(aTHX_ pp->handle, chk)) 9 0 9
if (pp->handle && !JSF->is_valid(aTHX_ pp->handle, chk)) 1 8 9
982 1 oa_cli_bad(aTHX_ o, oa_loc_name(loc), pp->name, chk,
983 pp->handle);
984 4 ▶ } else if (pp->required) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (pp->required) { 2 2 4
985 4 croak("Open::API::Client: %s: missing required %s parameter '%.*s'",
986 SvPV_nolen(o->op_id), oa_loc_name(loc), (int)nl, np);
987 }
988 }
989 }
990  
991 /* ---- body ---- */
992 {
993 9 SV *bctype = NULL;
994 9 bodyjson = oa_cli_build_body(aTHX_ o, params, &bctype);
995 7 ▶ if (bodyjson && bctype) {
Branch: 2 branches
BranchTrueFalseTotal
if (bodyjson && bctype) { 0 7 7
if (bodyjson && bctype) { 0 0 0
996 STRLEN ctl;
997 0 ▶ const char *ctp = SvPV_const(bctype, ctl);
Branch: 1 branch
BranchTrueFalseTotal
const char *ctp = SvPV_const(bctype, ctl); 0 0 0
998 0 bp = SvPV_const(bodyjson, blen);
999 0 hdrs[nh].name = "Content-Type"; hdrs[nh].nlen = 12;
1000 0 hdrs[nh].val = ctp; hdrs[nh].vlen = ctl;
1001 0 nh++;
1002 }
1003 }
1004  
1005 /* ---- URL: base + interpolated path + query ---- */
1006 7 url = oa_cli_url(aTHX_ o, params, base, sec_q, nsec_q);
1007  
1008 /* ---- headers + cookies ---- */
1009 14 ▶ for (i = 0; i < o->nparams[OA_IN_HEADER] && nh < 31; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < o->nparams[OA_IN_HEADER] && nh < 31; i++) { 7 7 14
for (i = 0; i < o->nparams[OA_IN_HEADER] && nh < 31; i++) { 7 0 7
1010 7 oa_param *pp = &o->params[OA_IN_HEADER][i];
1011 7 STRLEN nl; const char *np = SvPV_const(pp->name, nl);
1012 7 SV **v = hv_fetch(params, np, (I32)nl, 0);
1013 7 ▶ if (v && *v && SvOK(*v)) {
Branch: 3 branches
BranchTrueFalseTotal
if (v && *v && SvOK(*v)) { 5 2 7
if (v && *v && SvOK(*v)) { 5 0 5
if (v && *v && SvOK(*v)) { 5 0 5
1014 5 SV *w = oa_cli_wire(aTHX_ pp, *v);
1015 STRLEN vl; const char *vp;
1016 /* a list or object header is joined, not stringified: SvPV on a
1017 * reference puts `ARRAY(0x...)` on the wire */
1018 5 ▶ if (SvROK(w) && (SvTYPE(SvRV(w)) == SVt_PVAV
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(w) && (SvTYPE(SvRV(w)) == SVt_PVAV 4 1 5
if (SvROK(w) && (SvTYPE(SvRV(w)) == SVt_PVAV 2 2 4
1019 2 ▶ || SvTYPE(SvRV(w)) == SVt_PVHV)) {
Branch: 1 branch
BranchTrueFalseTotal
|| SvTYPE(SvRV(w)) == SVt_PVHV)) { 2 0 2
1020 4 SV *flat = sv_2mortal(newSVpvs(""));
1021 4 oa_cli_ser_flat(aTHX_ pp, w, flat);
1022 4 w = flat;
1023 }
1024 5 vp = SvPV_const(w, vl);
1025 5 hdrs[nh].name = np; hdrs[nh].nlen = nl;
1026 5 hdrs[nh].val = vp; hdrs[nh].vlen = vl;
1027 5 nh++;
1028 }
1029 }
1030 7 ▶ for (i = 0; i < o->nparams[OA_IN_COOKIE]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < o->nparams[OA_IN_COOKIE]; i++) { 0 7 7
1031 0 oa_param *pp = &o->params[OA_IN_COOKIE][i];
1032 0 STRLEN nl; const char *np = SvPV_const(pp->name, nl);
1033 0 SV **v = hv_fetch(params, np, (I32)nl, 0);
1034 0 ▶ if (v && *v && SvOK(*v)) {
Branch: 3 branches
BranchTrueFalseTotal
if (v && *v && SvOK(*v)) { 0 0 0
if (v && *v && SvOK(*v)) { 0 0 0
if (v && *v && SvOK(*v)) { 0 0 0
1035 0 ▶ if (!cookie) cookie = sv_2mortal(newSVpvs(""));
Branch: 1 branch
BranchTrueFalseTotal
if (!cookie) cookie = sv_2mortal(newSVpvs("")); 0 0 0
1036 0 else sv_catpvs(cookie, "; ");
1037 0 sv_catpvn(cookie, np, nl);
1038 0 sv_catpvs(cookie, "=");
1039 { /* same reference trap as the header loop above */
1040 0 SV *w = oa_cli_wire(aTHX_ pp, *v);
1041 0 ▶ if (SvROK(w) && (SvTYPE(SvRV(w)) == SVt_PVAV
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(w) && (SvTYPE(SvRV(w)) == SVt_PVAV 0 0 0
if (SvROK(w) && (SvTYPE(SvRV(w)) == SVt_PVAV 0 0 0
1042 0 ▶ || SvTYPE(SvRV(w)) == SVt_PVHV))
Branch: 1 branch
BranchTrueFalseTotal
|| SvTYPE(SvRV(w)) == SVt_PVHV)) 0 0 0
1043 0 oa_cli_ser_flat(aTHX_ pp, w, cookie);
1044 else
1045 0 sv_catsv(cookie, w);
1046 }
1047 }
1048 }
1049 7 ▶ for (i = 0; i < nsec_c; i++) { /* apiKey-in-cookie schemes */
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nsec_c; i++) { /* apiKey-in-cookie schemes */ 0 7 7
1050 0 ▶ if (!cookie) cookie = sv_2mortal(newSVpvs(""));
Branch: 1 branch
BranchTrueFalseTotal
if (!cookie) cookie = sv_2mortal(newSVpvs("")); 0 0 0
1051 0 else sv_catpvs(cookie, "; ");
1052 0 sv_catsv(cookie, sec_c[i].name);
1053 0 sv_catpvs(cookie, "=");
1054 0 sv_catsv(cookie, sec_c[i].val);
1055 }
1056 7 ▶ for (i = 0; i < nsec_h && nh < 31; i++) { /* apiKey-in-header schemes */
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < nsec_h && nh < 31; i++) { /* apiKey-in-header schemes */ 0 7 7
for (i = 0; i < nsec_h && nh < 31; i++) { /* apiKey-in-header schemes */ 0 0 0
1057 STRLEN nl2, vl2;
1058 0 hdrs[nh].name = SvPV_const(sec_h[i].name, nl2); hdrs[nh].nlen = nl2;
1059 0 ▶ hdrs[nh].val = SvPV_const(sec_h[i].val, vl2); hdrs[nh].vlen = vl2;
Branch: 1 branch
BranchTrueFalseTotal
hdrs[nh].val = SvPV_const(sec_h[i].val, vl2); hdrs[nh].vlen = vl2; 0 0 0
1060 0 nh++;
1061 }
1062 7 ▶ if (sec_auth && nh < 31) { /* bearer / basic */
Branch: 2 branches
BranchTrueFalseTotal
if (sec_auth && nh < 31) { /* bearer / basic */ 0 7 7
if (sec_auth && nh < 31) { /* bearer / basic */ 0 0 0
1063 STRLEN al;
1064 0 hdrs[nh].name = "Authorization"; hdrs[nh].nlen = 13;
1065 0 hdrs[nh].val = SvPV_const(sec_auth, al); hdrs[nh].vlen = al;
1066 0 nh++;
1067 }
1068 7 ▶ if (cookie && nh < 31) {
Branch: 2 branches
BranchTrueFalseTotal
if (cookie && nh < 31) { 0 7 7
if (cookie && nh < 31) { 0 0 0
1069 0 STRLEN cl; const char *cp = SvPV_const(cookie, cl);
1070 0 hdrs[nh].name = "Cookie"; hdrs[nh].nlen = 6;
1071 0 hdrs[nh].val = cp; hdrs[nh].vlen = cl;
1072 0 nh++;
1073 }
1074  
1075 /* ---- transparent CSRF ---- *
1076 * On a state-changing method, present the Origin the server expects and
1077 * echo the CSRF token captured from the previous response into the token
1078 * header (a custom header a cross-site page could not set). The response
1079 * map (oa_cli_map) captures the rotated token for the next call. */
1080 7 csrf_cfg = oa_cli_get(aTHX_ self, "_csrf");
1081 7 ▶ if (csrf_cfg && SvROK(csrf_cfg)) {
Branch: 2 branches
BranchTrueFalseTotal
if (csrf_cfg && SvROK(csrf_cfg)) { 0 7 7
if (csrf_cfg && SvROK(csrf_cfg)) { 0 0 0
1082 0 HV *cd = (HV *)SvRV(csrf_cfg);
1083 0 STRLEN mml; const char *mp2 = SvPV_const(o->method, mml);
1084 0 ▶ int unsafe = !((mml == 3 && memEQ(mp2, "get", 3))
Branch: 2 branches
BranchTrueFalseTotal
int unsafe = !((mml == 3 && memEQ(mp2, "get", 3)) 0 0 0
int unsafe = !((mml == 3 && memEQ(mp2, "get", 3)) 0 0 0
1085 0 ▶ || (mml == 4 && memEQ(mp2, "head", 4))
Branch: 2 branches
BranchTrueFalseTotal
|| (mml == 4 && memEQ(mp2, "head", 4)) 0 0 0
|| (mml == 4 && memEQ(mp2, "head", 4)) 0 0 0
1086 0 ▶ || (mml == 7 && memEQ(mp2, "options", 7))
Branch: 2 branches
BranchTrueFalseTotal
|| (mml == 7 && memEQ(mp2, "options", 7)) 0 0 0
|| (mml == 7 && memEQ(mp2, "options", 7)) 0 0 0
1087 0 ▶ || (mml == 5 && memEQ(mp2, "trace", 5)));
Branch: 2 branches
BranchTrueFalseTotal
|| (mml == 5 && memEQ(mp2, "trace", 5))); 0 0 0
|| (mml == 5 && memEQ(mp2, "trace", 5))); 0 0 0
1088 0 csrf_cookie_name = *hv_fetchs(cd, "cookie", 0); /* for capture */
1089 0 ▶ if (unsafe) {
Branch: 1 branch
BranchTrueFalseTotal
if (unsafe) { 0 0 0
1090 0 SV **org = hv_fetchs(cd, "origin", 0);
1091 0 SV *tok = oa_cli_get(aTHX_ self, "_csrf_token");
1092 0 ▶ if (org && *org && SvOK(*org) && nh < 31) {
Branch: 4 branches
BranchTrueFalseTotal
if (org && *org && SvOK(*org) && nh < 31) { 0 0 0
if (org && *org && SvOK(*org) && nh < 31) { 0 0 0
if (org && *org && SvOK(*org) && nh < 31) { 0 0 0
if (org && *org && SvOK(*org) && nh < 31) { 0 0 0
1093 0 STRLEN ol; const char *op = SvPV_const(*org, ol);
1094 0 hdrs[nh].name = "Origin"; hdrs[nh].nlen = 6;
1095 0 hdrs[nh].val = op; hdrs[nh].vlen = ol;
1096 0 nh++;
1097 }
1098 0 ▶ if (tok && SvOK(tok) && nh < 31) {
Branch: 3 branches
BranchTrueFalseTotal
if (tok && SvOK(tok) && nh < 31) { 0 0 0
if (tok && SvOK(tok) && nh < 31) { 0 0 0
if (tok && SvOK(tok) && nh < 31) { 0 0 0
1099 0 SV *hn = *hv_fetchs(cd, "header", 0);
1100 0 ▶ STRLEN hnl; const char *hnp = SvPV_const(hn, hnl);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN hnl; const char *hnp = SvPV_const(hn, hnl); 0 0 0
1101 0 STRLEN tl; const char *tp = SvPV_const(tok, tl);
1102 0 hdrs[nh].name = hnp; hdrs[nh].nlen = hnl;
1103 0 hdrs[nh].val = tp; hdrs[nh].vlen = tl;
1104 0 nh++;
1105 }
1106 }
1107 }
1108  
1109 /* ---- fire ---- */
1110 7 mmp = SvPV_const(o->method, ml);
1111 7 ▶ if (ml >= sizeof method) ml = sizeof method - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ml >= sizeof method) ml = sizeof method - 1; 0 7 7
1112 28 ▶ for (mk = 0; mk < ml; mk++) method[mk] = (char)toUPPER((U8)mmp[mk]);
Branch: 2 branches
BranchTrueFalseTotal
for (mk = 0; mk < ml; mk++) method[mk] = (char)toUPPER((U8)mmp[mk]); 21 0 21
for (mk = 0; mk < ml; mk++) method[mk] = (char)toUPPER((U8)mmp[mk]); 21 7 28
1113 7 method[ml] = '\0';
1114 7 Newxz(ctx, 1, oa_cli_ctx);
1115 7 ctx->api_sv = SvREFCNT_inc(api_sv);
1116 7 ctx->op = o;
1117 7 ctx->validate = oa_cli_get(aTHX_ self, "validate")
1118 7 ▶ && SvTRUE(oa_cli_get(aTHX_ self, "validate")) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
&& SvTRUE(oa_cli_get(aTHX_ self, "validate")) ? 1 : 0; 0 7 7
&& SvTRUE(oa_cli_get(aTHX_ self, "validate")) ? 1 : 0; 0 0 0
1119 7 ▶ if (csrf_cookie_name) { /* capture the rotated token from the response */
Branch: 1 branch
BranchTrueFalseTotal
if (csrf_cookie_name) { /* capture the rotated token from the response */ 0 7 7
1120 0 ctx->cli = newRV_inc((SV *)self);
1121 0 ctx->csrf_cookie = newSVsv(csrf_cookie_name);
1122 }
1123 7 ua = oa_cli_ua(aTHX_ self);
1124 14 ▶ return OA_FETCH->request(aTHX_ ua, method, SvPV_nolen(url),
Branch: 1 branch
BranchTrueFalseTotal
return OA_FETCH->request(aTHX_ ua, method, SvPV_nolen(url), 5 2 7
1125 nh ? hdrs : NULL, nh, bp, blen,
1126 0.0, -1, oa_cli_map, ctx);
1127 }
1128  
1129 #endif /* OA_CLIENT_H */
« blib/arch/Open/API/Install/Files.pm ↑ Summary include/oa_mock.h »