include/oa_compile.h

filter: Statementstmt 87.5% 833 / 952 Branchbran 65.3% 795 / 1216 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 75.0% 1628 / 2168 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/oa_plack.h ↑ Summary include/oa_tools.h »
1 #ifndef OA_COMPILE_H
2 #define OA_COMPILE_H
3  
4 /* The spec compiler: runs once inside Open::API->new, after the 3.1 gate.
5 * Walks the decoded document and builds the operation table - every path
6 * template pre-split into segments, every parameter/body/response schema
7 * compiled through the JSF ABI - so the per-request path (oa_route.h /
8 * oa_validate.h, phase 2) never touches the document again.
9 *
10 * SV ownership: every SV stored in the table is first pushed onto the api's
11 * keepalive AV (oa_keep), which owns it; the structs hold borrowed pointers.
12 * oa_ops_free only frees the C arrays - dropping the keep AV in oa_api_free
13 * releases all SVs, including the compiled JSF handles (blessed SVs whose
14 * DESTROY frees the underlying validators). */
15  
16 enum { OA_IN_PATH = 0, OA_IN_QUERY, OA_IN_HEADER, OA_IN_COOKIE, OA_IN_N };
17  
18 /* security scheme kinds (oauth2 / openIdConnect are carried as BEARER: the
19 * credential is the access token; flow automation is out of scope) */
20 /* OA_SEC_OTHER carries a scheme the specification defines but this library
21 * cannot settle by itself: mutualTLS (decided by the transport, and carrying
22 * no credential in the request at all) and an http scheme such as digest,
23 * which needs a challenge/response exchange. Refusing the document was worse
24 * than carrying it - a valid document became unusable - so the declaration is
25 * kept and the application's checker decides, exactly as scopes already work. */
26 enum { OA_SEC_APIKEY = 0, OA_SEC_BEARER, OA_SEC_BASIC, OA_SEC_OTHER };
27  
28 typedef struct oa_seg { SV *lit; SV *pname; } oa_seg; /* lit XOR pname */
29 /* `ctype` is set only for a parameter declared with `content` instead of
30 * `schema`: the value on the wire is then a document of that media type, and
31 * has to be decoded before the schema sees it. NULL is the ordinary case. */
32 /* How a parameter's value is spelled on the wire. The default depends on
33 * where the parameter lives: `simple` in a path or header, `form` in a query
34 * or cookie. `explode` defaults to TRUE for `form` and false for every other
35 * style, which is a per-style default rather than a global one.
36 *
37 * A value is split on the RAW text and each piece percent-decoded after,
38 * never the other way round: %2C is a comma that belongs to the value, not a
39 * delimiter, and decoding first would split `a%2Cb` into two elements. */
40 enum {
41 OA_ST_SIMPLE = 0, /* path, header: a,b,c */
42 OA_ST_LABEL, /* path: .a.b.c */
43 OA_ST_MATRIX, /* path: ;name=a,b / ;name=a;name=b */
44 OA_ST_FORM, /* query, cookie: ?n=a&n=b / ?n=a,b */
45 OA_ST_SPACE, /* query: ?n=a%20b */
46 OA_ST_PIPE, /* query: ?n=a|b */
47 OA_ST_DEEP /* query: ?n[k]=v */
48 };
49  
50 typedef struct oa_param {
51 SV *name; SV *handle; SV *ctype; int required; int is_array;
52 /* An object-typed parameter is assembled from its serialized form the way
53 * an array is. `props` holds the declared property names, and is needed
54 * only by form+explode, where the members arrive as SEPARATE top-level
55 * query keys (`R=100&G=200&B=150`) with nothing but the schema to say
56 * which keys belong to this parameter. NULL when there are none. */
57 int is_object; SV *props;
58 int style; int explode; int deprecated; int allow_empty;
59 /* allowReserved: a CLIENT-side rule. The reserved set may go on the wire
60 * unencoded, so this is read by the URL builder, never by the validator -
61 * a server decodes either spelling to the same value. */
62 int allow_reserved;
63 } oa_param;
64 /* `kind` is OA_MT_*: which decoder the DECLARED type wants, decided once at
65 * compile time. A range defers that to the request's own type. handle NULL
66 * still means pass-through. */
67 typedef struct oa_body { SV *ctype; SV *handle; int kind; SV *encoding; } oa_body;
68 typedef struct oa_resp { SV *status; SV *ctype; SV *handle; } oa_resp;
69 /* A declared response header. Keyed by STATUS, not by media type: headers
70 * belong to the Response Object, so a status declaring two content types must
71 * not carry two copies, and a header-only status (204, a 3xx with Location)
72 * must still be checked even though it compiles no oa_resp row at all - the
73 * response row loop skips anything without JSON `content`.
74 *
75 * The value is an oa_param because a Header Object IS a Parameter Object
76 * without `name`/`in`, so oa_check_param applies unchanged. */
77 typedef struct oa_rhdr { SV *status; oa_param p; } oa_rhdr;
78  
79 /* `oauthish` is 1 for oauth2 and openIdConnect. It cannot be derived from
80 * `type`: both collapse to OA_SEC_BEARER, which http/bearer uses too, and the
81 * specification lets only those two carry scopes in a requirement. */
82 typedef struct oa_scheme { SV *name; int type; int loc; SV *pname;
83 int oauthish; } oa_scheme;
84 typedef struct oa_secitem { int scheme; SV *scopes; } oa_secitem; /* scopes AV rv or NULL */
85 typedef struct oa_secalt { oa_secitem *items; int n; } oa_secalt;
86  
87 /* Response-validation coverage, per operation. Every response either is
88 * checked or is skipped for exactly one named reason, so what was not
89 * looked at is a reported number rather than a silent gap. `seen` is the
90 * sampling counter: deterministic 1-in-N, which is cheaper than a random
91 * draw and gives a stable denominator. */
92 typedef struct oa_rvstat {
93 unsigned long total; /* responses reaching the check */
94 unsigned long seen; /* the sampling counter */
95 unsigned long sampled; /* passed the sampling gate */
96 unsigned long checked; /* decoded and validated */
97 unsigned long violations; /* validated and did not conform */
98 unsigned long skip_ctype; /* not a declared JSON content type */
99 unsigned long skip_size; /* Content-Length absent or over max_body */
100 unsigned long skip_body; /* body not a plain scalar (fh/stream) */
101 unsigned long skip_schema; /* the status declares no schema */
102 unsigned long skip_decode; /* the body is not decodable JSON */
103 unsigned long hdr_checked; /* responses whose headers were checked */
104 unsigned long hdr_missing; /* a declared required header was absent */
105 unsigned long hdr_invalid; /* a declared header failed its schema */
106 } oa_rvstat;
107  
108 typedef struct oa_op {
109 SV *op_id, *method, *path; /* method stored lowercase */
110 oa_seg *segs; int nsegs;
111 oa_param *params[OA_IN_N]; int nparams[OA_IN_N];
112 oa_body *bodies; int nbodies; int body_required;
113 oa_resp *resps; int nresps;
114 oa_rhdr *rhdrs; int nrhdrs; /* declared response headers, by status */
115 /* This operation's own server path prefix, when it or its path item
116 * declares `servers` and the caller opted in. NULL is the ordinary case,
117 * where the document-level prefix (a->prefix) applies instead. In `keep`;
118 * borrowed. */
119 SV *prefix;
120 oa_secalt *sec; int nsec; /* requirement alternatives (OR) */
121 int own_sec; /* 1 = op-level copy, 0 = shares rootsec */
122 oa_rvstat rv; /* response-validation coverage */
123 } oa_op;
124  
125 typedef struct oa_ops {
126 oa_op *ops; int n, cap;
127 /* The order the ROUTER walks `ops` in, most specific first. It is a
128 * separate index rather than a sort of `ops` itself because `by_id` holds
129 * INDICES into that array, and reordering it would silently point every
130 * operationId at the wrong operation. See oa_order_routes. */
131 int *order;
132 HV *by_id; /* operationId -> IV index */
133 oa_scheme *schemes; int nschemes; /* components.securitySchemes */
134 oa_secalt *rootsec; int nrootsec; /* document-level security */
135 int v30; /* the document is OpenAPI 3.0.x */
136 } oa_ops;
137  
138 3603 static void oa_secalts_free(oa_secalt *alts, int n) {
139 int i;
140 3603 ▶ if (!alts) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!alts) return; 3589 14 3603
141 30 ▶ for (i = 0; i < n; i++) free(alts[i].items);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) free(alts[i].items); 16 14 30
142 14 free(alts);
143 }
144  
145 3599 static void oa_ops_free(pTHX_ void *p) {
146 3599 oa_ops *t = (oa_ops *)p;
147 int i, loc;
148 3599 ▶ if (!t) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!t) return; 0 3599 3599
149 11171 ▶ for (i = 0; i < t->n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->n; i++) { 7572 3599 11171
150 7572 oa_op *o = &t->ops[i];
151 7572 free(o->segs);
152 37860 ▶ for (loc = 0; loc < OA_IN_N; loc++) free(o->params[loc]);
Branch: 1 branch
BranchTrueFalseTotal
for (loc = 0; loc < OA_IN_N; loc++) free(o->params[loc]); 30288 7572 37860
153 7572 free(o->bodies);
154 7572 free(o->resps);
155 7572 free(o->rhdrs);
156 7572 ▶ if (o->own_sec) oa_secalts_free(o->sec, o->nsec);
Branch: 1 branch
BranchTrueFalseTotal
if (o->own_sec) oa_secalts_free(o->sec, o->nsec); 4 7568 7572
157 }
158 3599 free(t->ops);
159 3599 free(t->order);
160 3599 oa_secalts_free(t->rootsec, t->nrootsec);
161 3599 free(t->schemes);
162 3599 ▶ if (t->by_id) SvREFCNT_dec((SV *)t->by_id);
Branch: 1 branch
BranchTrueFalseTotal
if (t->by_id) SvREFCNT_dec((SV *)t->by_id); 3599 0 3599
163 3599 free(t);
164 }
165  
166 /* ---- base64 (Basic credentials, both directions) -------------------------- */
167  
168 static const char oa_b64_al[] =
169 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
170  
171 0 static SV *oa_b64_encode(pTHX_ const char *in, STRLEN len) {
172 0 SV *out = newSV((len + 2) / 3 * 4 + 1);
173 char *d;
174 STRLEN i;
175 0 SvPOK_on(out);
176 0 d = SvPVX(out);
177 0 ▶ for (i = 0; i + 2 < len; i += 3) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 2 < len; i += 3) { 0 0 0
178 0 U32 v = ((U32)(U8)in[i] << 16) | ((U32)(U8)in[i+1] << 8) | (U8)in[i+2];
179 0 *d++ = oa_b64_al[v >> 18]; *d++ = oa_b64_al[(v >> 12) & 63];
180 0 *d++ = oa_b64_al[(v >> 6) & 63]; *d++ = oa_b64_al[v & 63];
181 }
182 0 ▶ if (i < len) {
Branch: 1 branch
BranchTrueFalseTotal
if (i < len) { 0 0 0
183 0 U32 v = (U32)(U8)in[i] << 16;
184 0 int two = (i + 1 < len);
185 0 ▶ if (two) v |= (U32)(U8)in[i+1] << 8;
Branch: 1 branch
BranchTrueFalseTotal
if (two) v |= (U32)(U8)in[i+1] << 8; 0 0 0
186 0 *d++ = oa_b64_al[v >> 18]; *d++ = oa_b64_al[(v >> 12) & 63];
187 0 ▶ *d++ = two ? oa_b64_al[(v >> 6) & 63] : '=';
Branch: 1 branch
BranchTrueFalseTotal
*d++ = two ? oa_b64_al[(v >> 6) & 63] : '='; 0 0 0
188 0 *d++ = '=';
189 }
190 0 *d = '\0';
191 0 SvCUR_set(out, (STRLEN)(d - SvPVX(out)));
192 0 return out;
193 }
194  
195 0 static int oa_b64_val(char c) {
196 0 ▶ if (c >= 'A' && c <= 'Z') return c - 'A';
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'A' && c <= 'Z') return c - 'A'; 0 0 0
if (c >= 'A' && c <= 'Z') return c - 'A'; 0 0 0
197 0 ▶ if (c >= 'a' && c <= 'z') return c - 'a' + 26;
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'a' && c <= 'z') return c - 'a' + 26; 0 0 0
if (c >= 'a' && c <= 'z') return c - 'a' + 26; 0 0 0
198 0 ▶ if (c >= '0' && c <= '9') return c - '0' + 52;
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '9') return c - '0' + 52; 0 0 0
if (c >= '0' && c <= '9') return c - '0' + 52; 0 0 0
199 0 ▶ if (c == '+') return 62;
Branch: 1 branch
BranchTrueFalseTotal
if (c == '+') return 62; 0 0 0
200 0 ▶ if (c == '/') return 63;
Branch: 1 branch
BranchTrueFalseTotal
if (c == '/') return 63; 0 0 0
201 0 return -1;
202 }
203  
204 0 static SV *oa_b64_decode(pTHX_ const char *in, STRLEN len) { /* NULL on junk */
205 SV *out;
206 char *d;
207 0 U32 acc = 0;
208 0 int nb = 0;
209 STRLEN i;
210 0 ▶ while (len && (in[len-1] == '=' || isSPACE((U8)in[len-1]))) len--;
Branch: 3 branches
BranchTrueFalseTotal
while (len && (in[len-1] == '=' || isSPACE((U8)in[len-1]))) len--; 0 0 0
while (len && (in[len-1] == '=' || isSPACE((U8)in[len-1]))) len--; 0 0 0
while (len && (in[len-1] == '=' || isSPACE((U8)in[len-1]))) len--; 0 0 0
211 0 out = newSV(len / 4 * 3 + 4);
212 0 SvPOK_on(out);
213 0 d = SvPVX(out);
214 0 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 0 0 0
215 0 int v = oa_b64_val(in[i]);
216 0 ▶ if (v < 0) { SvREFCNT_dec(out); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (v < 0) { SvREFCNT_dec(out); return NULL; } 0 0 0
217 0 acc = (acc << 6) | (U32)v;
218 0 ▶ if (++nb == 4) { *d++ = (char)(acc >> 16); *d++ = (char)(acc >> 8);
Branch: 1 branch
BranchTrueFalseTotal
if (++nb == 4) { *d++ = (char)(acc >> 16); *d++ = (char)(acc >> 8); 0 0 0
219 0 *d++ = (char)acc; acc = 0; nb = 0; }
220 }
221 0 ▶ if (nb == 3) { *d++ = (char)(acc >> 10); *d++ = (char)(acc >> 2); }
Branch: 1 branch
BranchTrueFalseTotal
if (nb == 3) { *d++ = (char)(acc >> 10); *d++ = (char)(acc >> 2); } 0 0 0
222 0 ▶ else if (nb == 2) { *d++ = (char)(acc >> 4); }
Branch: 1 branch
BranchTrueFalseTotal
else if (nb == 2) { *d++ = (char)(acc >> 4); } 0 0 0
223 0 ▶ else if (nb == 1) { SvREFCNT_dec(out); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
else if (nb == 1) { SvREFCNT_dec(out); return NULL; } 0 0 0
224 0 *d = '\0';
225 0 SvCUR_set(out, (STRLEN)(d - SvPVX(out)));
226 0 return out;
227 }
228  
229 /* ---- small helpers -------------------------------------------------------- */
230  
231 73239 static SV *oa_keep(pTHX_ oa_api *a, SV *sv) { /* keep AV takes ownership */
232 73239 av_push(a->keep, sv);
233 73239 return sv;
234 }
235 471423 static HV *oa_hv_of(SV *sv) {
236 471423 ▶ return (sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV) ? (HV *)SvRV(sv) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV) ? (HV *)SvRV(sv) : NULL; 337658 133765 471423
return (sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV) ? (HV *)SvRV(sv) : NULL; 337658 0 337658
return (sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV) ? (HV *)SvRV(sv) : NULL; 337393 265 337658
237 }
238 93615 static AV *oa_av_of(SV *sv) {
239 93615 ▶ return (sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV) ? (AV *)SvRV(sv) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV) ? (AV *)SvRV(sv) : NULL; 29074 64541 93615
return (sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV) ? (AV *)SvRV(sv) : NULL; 29055 19 29074
return (sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV) ? (AV *)SvRV(sv) : NULL; 27813 1242 29055
240 }
241 1322762 static SV *oa_get(pTHX_ HV *h, const char *k) {
242 1322762 ▶ SV **e = h ? hv_fetch(h, k, (I32)strlen(k), 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **e = h ? hv_fetch(h, k, (I32)strlen(k), 0) : NULL; 1322762 0 1322762
243 1322762 ▶ return (e && *e && SvOK(*e)) ? *e : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && SvOK(*e)) ? *e : NULL; 317830 1004932 1322762
return (e && *e && SvOK(*e)) ? *e : NULL; 317830 0 317830
return (e && *e && SvOK(*e)) ? *e : NULL; 317830 0 317830
244 }
245  
246 /* What a reference into components.schemas looks like. Defined here rather
247 * than beside oa_rewrite_refs because oa_schema_is_array below reads it too,
248 * and the two must not drift apart on the spelling. */
249 #define OA_COMP_PFX "#/components/schemas/"
250 #define OA_COMP_PFX_LEN 21
251  
252 /* A component schema by name, from the normalised document. ->spec keeps its
253 * refs in `#/components/schemas/` form: the `#/$defs/` rewrite happens on the
254 * copy oa_compile_schema makes, not here. */
255 2 static SV *oa_comp_schema(pTHX_ oa_api *a, const char *nm, STRLEN nl) {
256 2 ▶ HV *doc = a && a->spec ? oa_hv_of(a->spec) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *doc = a && a->spec ? oa_hv_of(a->spec) : NULL; 2 0 2
HV *doc = a && a->spec ? oa_hv_of(a->spec) : NULL; 2 0 2
257 2 ▶ HV *comp = doc ? oa_hv_of(oa_get(aTHX_ doc, "components")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *comp = doc ? oa_hv_of(oa_get(aTHX_ doc, "components")) : NULL; 2 0 2
258 2 ▶ HV *sch = comp ? oa_hv_of(oa_get(aTHX_ comp, "schemas")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *sch = comp ? oa_hv_of(oa_get(aTHX_ comp, "schemas")) : NULL; 2 0 2
259 2 ▶ SV **e = sch ? hv_fetch(sch, nm, (I32)nl, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **e = sch ? hv_fetch(sch, nm, (I32)nl, 0) : NULL; 2 0 2
260 2 ▶ return (e && *e && oa_hv_of(*e)) ? *e : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && oa_hv_of(*e)) ? *e : NULL; 2 0 2
return (e && *e && oa_hv_of(*e)) ? *e : NULL; 2 0 2
return (e && *e && oa_hv_of(*e)) ? *e : NULL; 2 0 2
261 }
262  
263 #define OA_ARR_MAX_DEPTH 8
264  
265 /* Is this schema an array? `type` decides it directly, but the type may sit
266 * behind a $ref or inside an allOf/oneOf/anyOf - and that matters, because
267 * this is what tells the query parser to accumulate repeat keys. Reading only
268 * a literal `type` meant an array declared through a component was invisible,
269 * so ?v=a&v=b collapsed to the last value alone. Bounded, because a document
270 * can point a $ref at itself. */
271 16032 static int oa_schema_is_type(pTHX_ oa_api *a, SV *schema, int depth,
272 const char *tn, STRLEN tnl) {
273 16032 HV *h = oa_hv_of(schema);
274 SV *t, *r;
275 int k;
276 static const char *const unions[] = { "allOf", "oneOf", "anyOf", NULL };
277  
278 16032 ▶ if (!h || depth > OA_ARR_MAX_DEPTH) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!h || depth > OA_ARR_MAX_DEPTH) return 0; 16032 0 16032
if (!h || depth > OA_ARR_MAX_DEPTH) return 0; 0 16032 16032
279  
280 16032 t = oa_get(aTHX_ h, "type");
281 16032 ▶ if (t) {
Branch: 1 branch
BranchTrueFalseTotal
if (t) { 16030 2 16032
282 16030 ▶ if (!SvROK(t)) { STRLEN l; const char *p = SvPV_const(t, l);
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(t)) { STRLEN l; const char *p = SvPV_const(t, l); 16028 2 16030
283 16028 ▶ if (l == tnl && memEQ(p, tn, tnl)) return 1; }
Branch: 2 branches
BranchTrueFalseTotal
if (l == tnl && memEQ(p, tn, tnl)) return 1; } 4104 11924 16028
if (l == tnl && memEQ(p, tn, tnl)) return 1; } 1388 2716 4104
284 else {
285 2 AV *av = oa_av_of(t);
286 2 ▶ SSize_t i, n = av ? av_len(av) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = av ? av_len(av) + 1 : 0; 2 0 2
287 6 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 4 2 6
288 4 SV **e = av_fetch(av, i, 0);
289 4 ▶ if (e && *e) { STRLEN l; const char *p = SvPV_const(*e, l);
Branch: 2 branches
BranchTrueFalseTotal
if (e && *e) { STRLEN l; const char *p = SvPV_const(*e, l); 4 0 4
if (e && *e) { STRLEN l; const char *p = SvPV_const(*e, l); 4 0 4
290 4 ▶ if (l == tnl && memEQ(p, tn, tnl)) return 1; }
Branch: 2 branches
BranchTrueFalseTotal
if (l == tnl && memEQ(p, tn, tnl)) return 1; } 1 3 4
if (l == tnl && memEQ(p, tn, tnl)) return 1; } 0 1 1
291 }
292 }
293 }
294  
295 /* through a reference into components.schemas */
296 14644 r = oa_get(aTHX_ h, "$ref");
297 14644 ▶ if (r && !SvROK(r)) {
Branch: 2 branches
BranchTrueFalseTotal
if (r && !SvROK(r)) { 2 14642 14644
if (r && !SvROK(r)) { 2 0 2
298 2 STRLEN rl; const char *rp = SvPV_const(r, rl);
299 2 ▶ if (rl > OA_COMP_PFX_LEN && memEQ(rp, OA_COMP_PFX, OA_COMP_PFX_LEN)) {
Branch: 2 branches
BranchTrueFalseTotal
if (rl > OA_COMP_PFX_LEN && memEQ(rp, OA_COMP_PFX, OA_COMP_PFX_LEN)) { 2 0 2
if (rl > OA_COMP_PFX_LEN && memEQ(rp, OA_COMP_PFX, OA_COMP_PFX_LEN)) { 2 0 2
300 2 SV *tgt = oa_comp_schema(aTHX_ a, rp + OA_COMP_PFX_LEN,
301 rl - OA_COMP_PFX_LEN);
302 2 ▶ if (tgt && oa_schema_is_type(aTHX_ a, tgt, depth + 1, tn, tnl))
Branch: 2 branches
BranchTrueFalseTotal
if (tgt && oa_schema_is_type(aTHX_ a, tgt, depth + 1, tn, tnl)) 2 0 2
if (tgt && oa_schema_is_type(aTHX_ a, tgt, depth + 1, tn, tnl)) 1 1 2
303 1 return 1;
304 }
305 }
306  
307 /* or inside a union: one member is enough to make repeat keys right */
308 58572 ▶ for (k = 0; unions[k]; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; unions[k]; k++) { 43929 14643 58572
309 43929 AV *av = oa_av_of(oa_get(aTHX_ h, unions[k]));
310 43929 ▶ SSize_t i, n = av ? av_len(av) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = av ? av_len(av) + 1 : 0; 0 43929 43929
311 43929 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 43929 43929
312 0 SV **e = av_fetch(av, i, 0);
313 0 ▶ if (e && *e && oa_schema_is_type(aTHX_ a, *e, depth + 1, tn, tnl))
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && oa_schema_is_type(aTHX_ a, *e, depth + 1, tn, tnl)) 0 0 0
if (e && *e && oa_schema_is_type(aTHX_ a, *e, depth + 1, tn, tnl)) 0 0 0
if (e && *e && oa_schema_is_type(aTHX_ a, *e, depth + 1, tn, tnl)) 0 0 0
314 0 return 1;
315 }
316 }
317 14643 return 0;
318 }
319  
320 8015 static int oa_schema_is_array(pTHX_ oa_api *a, SV *schema, int depth) {
321 8015 return oa_schema_is_type(aTHX_ a, schema, depth, "array", 5);
322 }
323  
324 8015 static int oa_schema_is_object(pTHX_ oa_api *a, SV *schema, int depth) {
325 8015 return oa_schema_is_type(aTHX_ a, schema, depth, "object", 6);
326 }
327  
328 /* The declared property names, following the same $ref and union paths the
329 * type test does. Only form+explode needs them: there the members arrive as
330 * separate top-level query keys and the schema is the only thing that says
331 * which of them belong to this parameter. Returns a new AV ref, or NULL. */
332 30 static void oa_schema_props_into(pTHX_ oa_api *a, SV *schema, int depth,
333 AV *out) {
334 30 HV *h = oa_hv_of(schema);
335 HV *props;
336 SV *r;
337 int k;
338 static const char *const unions[] = { "allOf", "oneOf", "anyOf", NULL };
339  
340 30 ▶ if (!h || depth > OA_ARR_MAX_DEPTH) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!h || depth > OA_ARR_MAX_DEPTH) return; 30 0 30
if (!h || depth > OA_ARR_MAX_DEPTH) return; 0 30 30
341  
342 30 props = oa_hv_of(oa_get(aTHX_ h, "properties"));
343 30 ▶ if (props) {
Branch: 1 branch
BranchTrueFalseTotal
if (props) { 27 3 30
344 HE *he;
345 27 hv_iterinit(props);
346 105 ▶ while ((he = hv_iternext(props))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(props))) { 78 27 105
347 78 I32 kl; const char *kp = hv_iterkey(he, &kl);
348 78 av_push(out, newSVpvn(kp, (STRLEN)kl));
349 }
350 }
351  
352 30 r = oa_get(aTHX_ h, "$ref");
353 30 ▶ if (r && !SvROK(r)) {
Branch: 2 branches
BranchTrueFalseTotal
if (r && !SvROK(r)) { 0 30 30
if (r && !SvROK(r)) { 0 0 0
354 0 STRLEN rl; const char *rp = SvPV_const(r, rl);
355 0 ▶ if (rl > OA_COMP_PFX_LEN && memEQ(rp, OA_COMP_PFX, OA_COMP_PFX_LEN)) {
Branch: 2 branches
BranchTrueFalseTotal
if (rl > OA_COMP_PFX_LEN && memEQ(rp, OA_COMP_PFX, OA_COMP_PFX_LEN)) { 0 0 0
if (rl > OA_COMP_PFX_LEN && memEQ(rp, OA_COMP_PFX, OA_COMP_PFX_LEN)) { 0 0 0
356 0 SV *tgt = oa_comp_schema(aTHX_ a, rp + OA_COMP_PFX_LEN,
357 rl - OA_COMP_PFX_LEN);
358 0 ▶ if (tgt) oa_schema_props_into(aTHX_ a, tgt, depth + 1, out);
Branch: 1 branch
BranchTrueFalseTotal
if (tgt) oa_schema_props_into(aTHX_ a, tgt, depth + 1, out); 0 0 0
359 }
360 }
361 120 ▶ for (k = 0; unions[k]; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; unions[k]; k++) { 90 30 120
362 90 AV *av = oa_av_of(oa_get(aTHX_ h, unions[k]));
363 90 ▶ SSize_t i, n = av ? av_len(av) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = av ? av_len(av) + 1 : 0; 0 90 90
364 90 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 90 90
365 0 SV **e = av_fetch(av, i, 0);
366 0 ▶ if (e && *e) oa_schema_props_into(aTHX_ a, *e, depth + 1, out);
Branch: 2 branches
BranchTrueFalseTotal
if (e && *e) oa_schema_props_into(aTHX_ a, *e, depth + 1, out); 0 0 0
if (e && *e) oa_schema_props_into(aTHX_ a, *e, depth + 1, out); 0 0 0
367 }
368 }
369 }
370  
371 30 static SV *oa_schema_props(pTHX_ oa_api *a, SV *schema) {
372 30 AV *out = newAV();
373 30 oa_schema_props_into(aTHX_ a, schema, 0, out);
374 30 ▶ if (av_len(out) < 0) { SvREFCNT_dec((SV *)out); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(out) < 0) { SvREFCNT_dec((SV *)out); return NULL; } 3 27 30
375 27 return newRV_noinc((SV *)out);
376 }
377  
378 /* ---- JSON booleans ----------------------------------------------------------
379 *
380 * A document's `false` does NOT arrive as a plain 0. File::Raw::JSON yields a
381 * blessed Boolean, and the \0 idiom is a scalar ref - and a reference is TRUE
382 * to C's SvTRUE, whatever it points at. So reading `readOnly: false` or
383 * `explode: false` with SvTRUE gets the answer exactly backwards, silently,
384 * for every real specification.
385 *
386 * These two are the only correct way to read a boolean that came out of a
387 * document. Moved here from oa_normalize.h because oa_compile.h is included
388 * first and most of the reads are in the compiler. */
389 1229 static int oa_sv_is_json_bool(pTHX_ SV *sv) {
390 SV *rv;
391 PERL_UNUSED_CONTEXT;
392 1229 ▶ if (!sv || !SvROK(sv)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvROK(sv)) return 0; 1229 0 1229
if (!sv || !SvROK(sv)) return 0; 11 1218 1229
393 1218 rv = SvRV(sv);
394 2436 ▶ return !(SvTYPE(rv) == SVt_PVHV || SvTYPE(rv) == SVt_PVAV
Branch: 2 branches
BranchTrueFalseTotal
return !(SvTYPE(rv) == SVt_PVHV || SvTYPE(rv) == SVt_PVAV 1218 0 1218
return !(SvTYPE(rv) == SVt_PVHV || SvTYPE(rv) == SVt_PVAV 1218 0 1218
395 1218 ▶ || SvTYPE(rv) == SVt_PVCV);
Branch: 1 branch
BranchTrueFalseTotal
|| SvTYPE(rv) == SVt_PVCV); 1218 0 1218
396 }
397  
398 59159 static int oa_sv_truthy(pTHX_ SV *sv) {
399 59159 ▶ if (!sv || !SvOK(sv)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvOK(sv)) return 0; 7855 51304 59159
if (!sv || !SvOK(sv)) return 0; 0 7855 7855
400 7855 ▶ if (SvROK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sv)) { 6100 1755 7855
401 6100 SV *rv = SvRV(sv);
402 6100 ▶ if (SvTYPE(rv) == SVt_PVHV || SvTYPE(rv) == SVt_PVAV) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (SvTYPE(rv) == SVt_PVHV || SvTYPE(rv) == SVt_PVAV) return 1; 6100 0 6100
if (SvTYPE(rv) == SVt_PVHV || SvTYPE(rv) == SVt_PVAV) return 1; 0 6100 6100
403 6100 return SvTRUE(rv) ? 1 : 0;
404 }
405 1755 return SvTRUE(sv) ? 1 : 0;
406 }
407  
408 /* A Components Object key: the specification fixes the alphabet. */
409 5539 static int oa_key_ok(const char *k, STRLEN l) {
410 STRLEN i;
411 5539 ▶ if (!l) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!l) return 0; 0 5539 5539
412 26606 ▶ for (i = 0; i < l; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l; i++) { 21070 5536 26606
413 21070 char c = k[i];
414 21073 ▶ if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')
Branch: 5 branches
BranchTrueFalseTotal
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 21067 3 21070
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 14222 6845 21067
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 14222 3 14225
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 0 14222 14222
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 3 0 3
415 3 ▶ || (c >= '0' && c <= '9') || c == '.' || c == '-' || c == '_'))
Branch: 4 branches
BranchTrueFalseTotal
|| (c >= '0' && c <= '9') || c == '.' || c == '-' || c == '_')) 0 3 3
|| (c >= '0' && c <= '9') || c == '.' || c == '-' || c == '_')) 0 0 0
|| (c >= '0' && c <= '9') || c == '.' || c == '-' || c == '_')) 3 0 3
|| (c >= '0' && c <= '9') || c == '.' || c == '-' || c == '_')) 3 0 3
416 3 return 0;
417 }
418 5536 return 1;
419 }
420  
421 /* `MUST be in the format of a URL` (Contact) and `MUST be in the form of an
422 * absolute URI` (the XML Object's namespace) are the same test here: a scheme
423 * followed by a colon, and no whitespace anywhere.
424 *
425 * Deliberately loose. A full URI grammar would refuse documents that work, and
426 * a false refusal is worse than a missed typo: nothing in this library
427 * dereferences either field, so the only value of the check is catching a
428 * value that was never a URL at all. Anything with a scheme passes. */
429 10 static int oa_looks_like_url(pTHX_ SV *sv) {
430 STRLEN l, i;
431 const char *p;
432 10 ▶ if (!sv || !SvOK(sv)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvOK(sv)) return 1; 10 0 10
if (!sv || !SvOK(sv)) return 1; 0 10 10
433 10 p = SvPV_const(sv, l);
434 10 ▶ if (!l) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!l) return 0; 0 10 10
435 145 ▶ for (i = 0; i < l; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l; i++) 137 8 145
436 137 ▶ if (p[i] == ' ' || p[i] == '\t' || p[i] == '\n' || p[i] == '\r')
Branch: 4 branches
BranchTrueFalseTotal
if (p[i] == ' ' || p[i] == '\t' || p[i] == '\n' || p[i] == '\r') 135 2 137
if (p[i] == ' ' || p[i] == '\t' || p[i] == '\n' || p[i] == '\r') 135 0 135
if (p[i] == ' ' || p[i] == '\t' || p[i] == '\n' || p[i] == '\r') 135 0 135
if (p[i] == ' ' || p[i] == '\t' || p[i] == '\n' || p[i] == '\r') 0 135 135
437 2 return 0;
438 /* scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) ":" */
439 8 ▶ if (!((p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z')))
Branch: 4 branches
BranchTrueFalseTotal
if (!((p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z'))) 6 2 8
if (!((p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z'))) 6 0 6
if (!((p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z'))) 6 2 8
if (!((p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z'))) 0 6 6
440 2 return 0;
441 41 ▶ for (i = 1; i < l; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < l; i++) { 41 0 41
442 41 char c = p[i];
443 41 ▶ if (c == ':') return i > 0;
Branch: 1 branch
BranchTrueFalseTotal
if (c == ':') return i > 0; 6 35 41
444 36 ▶ if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')
Branch: 5 branches
BranchTrueFalseTotal
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 31 4 35
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 31 0 31
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 31 4 35
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 0 31 31
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 0 1 1
445 4 ▶ || (c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.'))
Branch: 4 branches
BranchTrueFalseTotal
|| (c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.')) 1 3 4
|| (c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.')) 0 1 1
|| (c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.')) 2 1 3
|| (c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.')) 1 1 2
446 0 return 0;
447 }
448 0 return 0;
449 }
450  
451 /* `MUST be in the format of an email address`. Loose for the same reason: one
452 * `@` with something either side, and no whitespace. The local part of a real
453 * address can contain almost anything, so anything stricter would refuse
454 * addresses that deliver. */
455 6 static int oa_looks_like_email(pTHX_ SV *sv) {
456 STRLEN l, i;
457 const char *p;
458 6 STRLEN at = 0;
459 6 int seen = 0;
460 6 ▶ if (!sv || !SvOK(sv)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvOK(sv)) return 1; 6 0 6
if (!sv || !SvOK(sv)) return 1; 0 6 6
461 6 p = SvPV_const(sv, l);
462 6 ▶ if (!l) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!l) return 0; 0 6 6
463 89 ▶ for (i = 0; i < l; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l; i++) { 83 6 89
464 83 char c = p[i];
465 83 ▶ if (c == ' ' || c == '\t' || c == '\n' || c == '\r') return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') return 0; 83 0 83
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') return 0; 83 0 83
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') return 0; 83 0 83
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') return 0; 0 83 83
466 83 ▶ if (c == '@') { if (seen) return 0; seen = 1; at = i; }
Branch: 2 branches
BranchTrueFalseTotal
if (c == '@') { if (seen) return 0; seen = 1; at = i; } 4 79 83
if (c == '@') { if (seen) return 0; seen = 1; at = i; } 0 4 4
467 }
468 6 ▶ return (seen && at > 0 && at < l - 1) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
return (seen && at > 0 && at < l - 1) ? 1 : 0; 4 2 6
return (seen && at > 0 && at < l - 1) ? 1 : 0; 4 0 4
return (seen && at > 0 && at < l - 1) ? 1 : 0; 4 0 4
469 }
470  
471 /* An External Documentation Object requires a `url`. It can hang off the
472 * document root, an operation, a tag or a schema, so the check lives here and
473 * every one of those sites calls it rather than repeating the shape. `where`
474 * names the position for the message, because "externalDocs has no url" on a
475 * document with thirty operations is not an actionable complaint. */
476 11237 static void oa_ext_docs_ok(pTHX_ SV *xd, const char *where) {
477 HV *h;
478 11237 ▶ if (!xd || !SvROK(xd) || SvTYPE(SvRV(xd)) != SVt_PVHV) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!xd || !SvROK(xd) || SvTYPE(SvRV(xd)) != SVt_PVHV) return; 5 11232 11237
if (!xd || !SvROK(xd) || SvTYPE(SvRV(xd)) != SVt_PVHV) return; 5 0 5
if (!xd || !SvROK(xd) || SvTYPE(SvRV(xd)) != SVt_PVHV) return; 0 5 5
479 5 h = (HV *)SvRV(xd);
480 5 ▶ if (!hv_exists(h, "url", 3))
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_exists(h, "url", 3)) 4 1 5
481 4 croak("Open::API: %s: externalDocs has no 'url' (it is required)",
482 where);
483 }
484  
485 /* case-insensitive equality. Defined here rather than beside its users
486 * because media types, header names and scheme names are all compared this
487 * way and this is the first header that needs one. */
488 13025 static int oa_ci_eq(const char *a, STRLEN al, const char *b, STRLEN bl) {
489 STRLEN i;
490 13025 ▶ if (al != bl) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (al != bl) return 0; 41 12984 13025
491 196841 ▶ for (i = 0; i < al; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < al; i++) 185239 11602 196841
492 185239 ▶ if (toLOWER((U8)a[i]) != toLOWER((U8)b[i])) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (toLOWER((U8)a[i]) != toLOWER((U8)b[i])) return 0; 1232 184007 185239
if (toLOWER((U8)a[i]) != toLOWER((U8)b[i])) return 0; 43 185196 185239
if (toLOWER((U8)a[i]) != toLOWER((U8)b[i])) return 0; 1382 183857 185239
493 11602 return 1;
494 }
495  
496 /* A media type we have a decoder for: `application/json` itself, or any
497 * `+json`-suffixed relative (`application/vnd.api+json`, `application/hal+json`).
498 *
499 * Media types are case-insensitive (RFC 7231) and any parameters after `;`
500 * are not part of the type, so both are handled here - this is the one place
501 * that decides, for request bodies, responses and parameter `content` alike.
502 *
503 * The suffix test is a real suffix test. Until 0.13 this was a PREFIX test
504 * whose comment claimed it matched `+json` relatives; it could not, because
505 * `application/vnd.api+json` does not begin with `application/json`. The same
506 * missing delimiter check made `application/json-seq` match as JSON and then
507 * fail to decode. */
508 /* The media type proper: leading space skipped, stopped at `;`, trailing
509 * space trimmed. One place, so every predicate below agrees about where a
510 * media type ends - `application/json; charset=utf-8` is JSON, and
511 * `multipart/form-data; boundary=xx` is multipart. */
512 23486 static void oa_ctype_span(const char *p, STRLEN l, STRLEN *bo, STRLEN *eo) {
513 23486 STRLEN b = 0, e;
514 23486 ▶ while (b < l && isSPACE((U8)p[b])) b++;
Branch: 2 branches
BranchTrueFalseTotal
while (b < l && isSPACE((U8)p[b])) b++; 23486 0 23486
while (b < l && isSPACE((U8)p[b])) b++; 0 23486 23486
515 23486 e = b;
516 369905 ▶ while (e < l && p[e] != ';') e++;
Branch: 2 branches
BranchTrueFalseTotal
while (e < l && p[e] != ';') e++; 346429 23476 369905
while (e < l && p[e] != ';') e++; 346419 10 346429
517 23486 ▶ while (e > b && isSPACE((U8)p[e - 1])) e--;
Branch: 2 branches
BranchTrueFalseTotal
while (e > b && isSPACE((U8)p[e - 1])) e--; 23486 0 23486
while (e > b && isSPACE((U8)p[e - 1])) e--; 0 23486 23486
518 23486 *bo = b; *eo = e;
519 23486 }
520  
521 10317 static int oa_ctype_is_json(const char *p, STRLEN l) {
522 STRLEN b, e;
523 10317 oa_ctype_span(p, l, &b, &e);
524 10317 ▶ if (e - b == 16 && oa_ci_eq(p + b, 16, "application/json", 16)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (e - b == 16 && oa_ci_eq(p + b, 16, "application/json", 16)) return 1; 8984 1333 10317
if (e - b == 16 && oa_ci_eq(p + b, 16, "application/json", 16)) return 1; 8984 0 8984
525 1333 ▶ return (e - b) > 5 && oa_ci_eq(p + e - 5, 5, "+json", 5);
Branch: 2 branches
BranchTrueFalseTotal
return (e - b) > 5 && oa_ci_eq(p + e - 5, 5, "+json", 5); 1333 0 1333
return (e - b) > 5 && oa_ci_eq(p + e - 5, 5, "+json", 5); 1 1332 1333
526 }
527  
528 1342 static int oa_ctype_is_form(const char *p, STRLEN l) {
529 STRLEN b, e;
530 1342 oa_ctype_span(p, l, &b, &e);
531 1342 return e - b == 33
532 1342 ▶ && oa_ci_eq(p + b, 33, "application/x-www-form-urlencoded", 33);
Branch: 2 branches
BranchTrueFalseTotal
&& oa_ci_eq(p + b, 33, "application/x-www-form-urlencoded", 33); 28 1314 1342
&& oa_ci_eq(p + b, 33, "application/x-www-form-urlencoded", 33); 28 0 28
533 }
534  
535 1314 static int oa_ctype_is_multipart(const char *p, STRLEN l) {
536 STRLEN b, e;
537 1314 oa_ctype_span(p, l, &b, &e);
538 1314 ▶ return e - b == 19 && oa_ci_eq(p + b, 19, "multipart/form-data", 19);
Branch: 2 branches
BranchTrueFalseTotal
return e - b == 19 && oa_ci_eq(p + b, 19, "multipart/form-data", 19); 12 1302 1314
return e - b == 19 && oa_ci_eq(p + b, 19, "multipart/form-data", 19); 12 0 12
539 }
540  
541 /* A media range: the catch-all, or a type followed by a wildcard subtype. */
542 4622 static int oa_ctype_is_range(const char *p, STRLEN l) {
543 STRLEN b, e;
544 4622 oa_ctype_span(p, l, &b, &e);
545 4622 ▶ return (e - b) >= 3 && p[e - 1] == '*' && p[e - 2] == '/';
Branch: 3 branches
BranchTrueFalseTotal
return (e - b) >= 3 && p[e - 1] == '*' && p[e - 2] == '/'; 4622 0 4622
return (e - b) >= 3 && p[e - 1] == '*' && p[e - 2] == '/'; 2 4620 4622
return (e - b) >= 3 && p[e - 1] == '*' && p[e - 2] == '/'; 2 0 2
546 }
547  
548 /* Which decoder a media type wants. OA_MT_RANGE is not a decoder: it says the
549 * declared entry is a range, so the decision belongs to the REQUEST's own
550 * content type at validate time. Anything else we cannot decode is OPAQUE and
551 * passes through untouched - a declared text/plain must keep behaving exactly
552 * as it always has. */
553 enum { OA_MT_OPAQUE = 0, OA_MT_JSON, OA_MT_FORM, OA_MT_MULTIPART, OA_MT_RANGE };
554  
555 4622 static int oa_ctype_kind(const char *p, STRLEN l) {
556 4622 ▶ if (oa_ctype_is_range(p, l)) return OA_MT_RANGE;
Branch: 1 branch
BranchTrueFalseTotal
if (oa_ctype_is_range(p, l)) return OA_MT_RANGE; 2 4620 4622
557 4620 ▶ if (oa_ctype_is_json(p, l)) return OA_MT_JSON;
Branch: 1 branch
BranchTrueFalseTotal
if (oa_ctype_is_json(p, l)) return OA_MT_JSON; 3295 1325 4620
558 1325 ▶ if (oa_ctype_is_form(p, l)) return OA_MT_FORM;
Branch: 1 branch
BranchTrueFalseTotal
if (oa_ctype_is_form(p, l)) return OA_MT_FORM; 17 1308 1325
559 1308 ▶ if (oa_ctype_is_multipart(p, l)) return OA_MT_MULTIPART;
Branch: 1 branch
BranchTrueFalseTotal
if (oa_ctype_is_multipart(p, l)) return OA_MT_MULTIPART; 8 1300 1308
560 1300 return OA_MT_OPAQUE;
561 }
562  
563 /* Does a DECLARED media type cover an ACTUAL one? Exact, or a range. */
564 1964 static int oa_ctype_matches(const char *d, STRLEN dl, const char *a, STRLEN al) {
565 STRLEN db, de, ab, ae;
566 1964 oa_ctype_span(d, dl, &db, &de);
567 1964 oa_ctype_span(a, al, &ab, &ae);
568 1964 ▶ if (de - db == 3 && memEQ(d + db, "*/*", 3)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (de - db == 3 && memEQ(d + db, "*/*", 3)) return 1; 0 1964 1964
if (de - db == 3 && memEQ(d + db, "*/*", 3)) return 1; 0 0 0
569 1964 ▶ if ((de - db) > 2 && d[de - 1] == '*' && d[de - 2] == '/') {
Branch: 3 branches
BranchTrueFalseTotal
if ((de - db) > 2 && d[de - 1] == '*' && d[de - 2] == '/') { 1964 0 1964
if ((de - db) > 2 && d[de - 1] == '*' && d[de - 2] == '/') { 0 1964 1964
if ((de - db) > 2 && d[de - 1] == '*' && d[de - 2] == '/') { 0 0 0
570 0 STRLEN tl = de - db - 2, i = 0;
571 0 const char *as = a + ab;
572 0 ▶ while (ab + i < ae && as[i] != '/') i++;
Branch: 2 branches
BranchTrueFalseTotal
while (ab + i < ae && as[i] != '/') i++; 0 0 0
while (ab + i < ae && as[i] != '/') i++; 0 0 0
573 0 ▶ return i == tl && oa_ci_eq(d + db, tl, as, tl);
Branch: 2 branches
BranchTrueFalseTotal
return i == tl && oa_ci_eq(d + db, tl, as, tl); 0 0 0
return i == tl && oa_ci_eq(d + db, tl, as, tl); 0 0 0
574 }
575 1964 return oa_ci_eq(d + db, de - db, a + ab, ae - ab);
576 }
577  
578 /* Does a declared response key cover this status? Exact first, then a range
579 * key (`2XX`), then `default` - the precedence OpenAPI gives them. `want` is
580 * the decimal status as text.
581 *
582 * Both the server (oa_plack.h) and the client (oa_client.h) choose a response
583 * schema, and they used to do it with a copy each of an exact-then-`default`
584 * loop. A range key matched neither, so a document written with `2XX`/`4XX`
585 * compiled schemas that were never selected and never applied. One helper, so
586 * the two cannot disagree about which schema governs a response. */
587 8 static int oa_status_covers(const char *key, STRLEN kl,
588 const char *want, STRLEN wl) {
589 8 ▶ if (kl == wl && memEQ(key, want, kl)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (kl == wl && memEQ(key, want, kl)) return 1; 7 1 8
if (kl == wl && memEQ(key, want, kl)) return 1; 0 7 7
590 8 ▶ if (kl == 7 && memEQ(key, "default", 7)) return 0; /* caller's last pass */
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 7 && memEQ(key, "default", 7)) return 0; /* caller's last pass */ 1 7 8
if (kl == 7 && memEQ(key, "default", 7)) return 0; /* caller's last pass */ 1 0 1
591 7 ▶ if (kl == 3 && wl == 3 && key[0] == want[0]
Branch: 3 branches
BranchTrueFalseTotal
if (kl == 3 && wl == 3 && key[0] == want[0] 7 0 7
if (kl == 3 && wl == 3 && key[0] == want[0] 7 0 7
if (kl == 3 && wl == 3 && key[0] == want[0] 7 0 7
592 7 ▶ && (key[1] == 'X' || key[1] == 'x')
Branch: 2 branches
BranchTrueFalseTotal
&& (key[1] == 'X' || key[1] == 'x') 0 7 7
&& (key[1] == 'X' || key[1] == 'x') 0 0 0
593 7 ▶ && (key[2] == 'X' || key[2] == 'x')) return 1;
Branch: 2 branches
BranchTrueFalseTotal
&& (key[2] == 'X' || key[2] == 'x')) return 1; 0 7 7
&& (key[2] == 'X' || key[2] == 'x')) return 1; 0 0 0
594 0 return 0;
595 }
596  
597 /* ---- the schema keyword tables ---------------------------------------------
598 *
599 * Where a Schema Object can actually appear. Used by the 3.0 converter in
600 * oa_normalize.h and by the readOnly/writeOnly projection below, which must
601 * walk the same positions and no others: `example`, `default` and `enum` hold
602 * arbitrary user JSON that may itself contain a key called `properties`. */
603  
604 185390 static int oa_kw_in(const char *k, STRLEN l, const char *const *set) {
605 int i;
606 1293895 ▶ for (i = 0; set[i]; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; set[i]; i++) 1126412 167483 1293895
607 1126412 ▶ if (strlen(set[i]) == l && memEQ(k, set[i], l)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (strlen(set[i]) == l && memEQ(k, set[i], l)) return 1; 98242 1028170 1126412
if (strlen(set[i]) == l && memEQ(k, set[i], l)) return 1; 17907 80335 98242
608 167483 return 0;
609 }
610  
611 /* keywords whose value is a single Schema Object */
612 63833 static int oa_kw_schema(const char *k, STRLEN l) {
613 static const char *const s[] = {
614 "additionalProperties", "not", "if", "then", "else", "propertyNames",
615 "contains", "unevaluatedItems", "unevaluatedProperties", "contentSchema",
616 NULL
617 };
618 63833 return oa_kw_in(k, l, s);
619 }
620 /* keywords whose value is a map of Schema Objects */
621 63632 static int oa_kw_schema_map(const char *k, STRLEN l) {
622 static const char *const s[] = {
623 "properties", "patternProperties", "$defs", "definitions",
624 "dependentSchemas", NULL
625 };
626 63632 return oa_kw_in(k, l, s);
627 }
628 /* keywords whose value is a list of Schema Objects */
629 54285 static int oa_kw_schema_list(const char *k, STRLEN l) {
630 static const char *const s[] = { "allOf", "anyOf", "oneOf", "prefixItems", NULL };
631 54285 return oa_kw_in(k, l, s);
632 }
633  
634 /* JSF resolves same-document $refs against schema locations it knows -
635 * `#/$defs/Name` works, arbitrary keys like `#/components/schemas/Name` do
636 * not. So at compile time every extracted schema is deep-copied with its
637 * refs REWRITTEN (#/components/schemas/X -> #/$defs/X) and the spec's
638 * components.schemas - itself rewritten once, since schemas reference each
639 * other - is attached as a shared $defs. */
640  
641 131633 static SV *oa_rewrite_refs(pTHX_ SV *sv) {
642 131633 ▶ if (sv && SvROK(sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv && SvROK(sv)) { 131633 0 131633
if (sv && SvROK(sv)) { 75233 56400 131633
643 75233 SV *rv = SvRV(sv);
644 75233 ▶ if (SvTYPE(rv) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(rv) == SVt_PVHV) { 59956 15277 75233
645 59956 HV *src = (HV *)rv, *dst = newHV();
646 HE *he;
647 59956 hv_iterinit(src);
648 160495 ▶ while ((he = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) { 100539 59956 160495
649 100539 I32 kl; const char *k = hv_iterkey(he, &kl);
650 100539 SV *v = hv_iterval(src, he);
651 100539 ▶ if (kl == 4 && memEQ(k, "$ref", 4) && SvOK(v) && !SvROK(v)) {
Branch: 4 branches
BranchTrueFalseTotal
if (kl == 4 && memEQ(k, "$ref", 4) && SvOK(v) && !SvROK(v)) { 44871 55668 100539
if (kl == 4 && memEQ(k, "$ref", 4) && SvOK(v) && !SvROK(v)) { 8081 36790 44871
if (kl == 4 && memEQ(k, "$ref", 4) && SvOK(v) && !SvROK(v)) { 8081 0 8081
if (kl == 4 && memEQ(k, "$ref", 4) && SvOK(v) && !SvROK(v)) { 8079 2 8081
652 8079 STRLEN vl; const char *vp = SvPV_const(v, vl);
653 8079 ▶ if (vl > OA_COMP_PFX_LEN
Branch: 1 branch
BranchTrueFalseTotal
if (vl > OA_COMP_PFX_LEN 8075 4 8079
654 8075 ▶ && memEQ(vp, OA_COMP_PFX, OA_COMP_PFX_LEN)) {
Branch: 1 branch
BranchTrueFalseTotal
&& memEQ(vp, OA_COMP_PFX, OA_COMP_PFX_LEN)) { 8075 0 8075
655 8075 SV *nv = newSVpvs("#/$defs/");
656 8075 sv_catpvn(nv, vp + OA_COMP_PFX_LEN, vl - OA_COMP_PFX_LEN);
657 8075 (void)hv_store(dst, k, kl, nv, 0);
658 8075 continue;
659 }
660 }
661 92464 (void)hv_store(dst, k, kl, oa_rewrite_refs(aTHX_ v), 0);
662 }
663 59956 return newRV_noinc((SV *)dst);
664 }
665 15277 ▶ if (SvTYPE(rv) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(rv) == SVt_PVAV) { 15187 90 15277
666 15187 AV *src = (AV *)rv, *dst = newAV();
667 15187 SSize_t i, n = av_len(src) + 1;
668 37441 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 22254 15187 37441
669 22254 SV **e = av_fetch(src, i, 0);
670 22254 ▶ av_push(dst, (e && *e) ? oa_rewrite_refs(aTHX_ *e) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(dst, (e && *e) ? oa_rewrite_refs(aTHX_ *e) : newSV(0)); 22254 0 22254
av_push(dst, (e && *e) ? oa_rewrite_refs(aTHX_ *e) : newSV(0)); 22254 0 22254
671 }
672 15187 return newRV_noinc((SV *)dst);
673 }
674 }
675 56490 return newSVsv(sv);
676 }
677  
678 /* ---- the readOnly / writeOnly projection ------------------------------------
679 *
680 * `readOnly` says a property may come back in a response but should not be
681 * sent in a request; `writeOnly` is the mirror. And a `required` property that
682 * is readOnly is required of RESPONSES only - so the same component means two
683 * different things depending on which way the payload is travelling.
684 *
685 * None of that can be decided while validating: once a schema is compiled JSF
686 * owns the traversal, exactly as with `discriminator`. So it is expanded into
687 * 2020-12 constructs here, at compile time, per direction. A forbidden
688 * property becomes `{not:{}}` - always false, so its presence fails - spelled
689 * as a hashref because JSF refuses the \0 JSON-false idiom and a bare 0 would
690 * encode as a number and stop being valid JSON Schema on the wire.
691 *
692 * Only the positions where a Schema Object can appear are descended, for the
693 * reason the 3.0 converter has the same rule: `example`, `default` and `enum`
694 * hold user JSON that may itself contain a key called `properties`. */
695  
696 #define OA_DIR_NONE 0
697 #define OA_DIR_REQ 1 /* a request: readOnly is forbidden */
698 #define OA_DIR_RESP 2 /* a response: writeOnly is forbidden */
699 #define OA_PROJ_MAX_DEPTH 128
700  
701 76 static int oa_flagged(pTHX_ SV *sv, const char *flag) {
702 76 HV *h = oa_hv_of(sv);
703 76 ▶ SV *v = h ? oa_get(aTHX_ h, flag) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *v = h ? oa_get(aTHX_ h, flag) : NULL; 76 0 76
704 76 ▶ return (v && oa_sv_truthy(aTHX_ v)) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
return (v && oa_sv_truthy(aTHX_ v)) ? 1 : 0; 26 50 76
return (v && oa_sv_truthy(aTHX_ v)) ? 1 : 0; 26 0 26
705 }
706  
707 /* Does the document mention the flag anywhere? A false positive (the word
708 * inside an `example`) only costs a projection nobody consults; a false
709 * negative would silently disable the feature, so this scan is deliberately
710 * blunt rather than position-aware. */
711 539651 static int oa_uses_flag(pTHX_ SV *node, const char *flag, int depth) {
712 SV *rv;
713 539651 ▶ if (!node || depth > OA_PROJ_MAX_DEPTH || !SvROK(node)) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!node || depth > OA_PROJ_MAX_DEPTH || !SvROK(node)) return 0; 539651 0 539651
if (!node || depth > OA_PROJ_MAX_DEPTH || !SvROK(node)) return 0; 539651 0 539651
if (!node || depth > OA_PROJ_MAX_DEPTH || !SvROK(node)) return 0; 221300 318351 539651
714 318351 rv = SvRV(node);
715 318351 ▶ if (SvTYPE(rv) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(rv) == SVt_PVHV) { 264358 53993 318351
716 264358 HV *h = (HV *)rv;
717 HE *he;
718 264358 STRLEN fl = strlen(flag);
719 264358 hv_iterinit(h);
720 734590 ▶ while ((he = hv_iternext(h))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(h))) { 470394 264196 734590
721 470394 I32 kl; const char *k = hv_iterkey(he, &kl);
722 470394 SV *v = hv_iterval(h, he);
723 470394 ▶ if ((STRLEN)kl == fl && memEQ(k, flag, fl)
Branch: 2 branches
BranchTrueFalseTotal
if ((STRLEN)kl == fl && memEQ(k, flag, fl) 25749 444645 470394
if ((STRLEN)kl == fl && memEQ(k, flag, fl) 19 25730 25749
724 162 ▶ && v && oa_sv_truthy(aTHX_ v)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
&& v && oa_sv_truthy(aTHX_ v)) return 1; 19 0 19
&& v && oa_sv_truthy(aTHX_ v)) return 1; 19 0 19
725 470375 ▶ if (oa_uses_flag(aTHX_ v, flag, depth + 1)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (oa_uses_flag(aTHX_ v, flag, depth + 1)) return 1; 143 470232 470375
726 }
727 264196 return 0;
728 }
729 53993 ▶ if (SvTYPE(rv) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(rv) == SVt_PVAV) { 44395 9598 53993
730 44395 AV *av = (AV *)rv;
731 44395 SSize_t i, n = av_len(av) + 1;
732 106443 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 62048 44395 106443
733 62048 SV **e = av_fetch(av, i, 0);
734 62048 ▶ if (e && *e && oa_uses_flag(aTHX_ *e, flag, depth + 1)) return 1;
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && oa_uses_flag(aTHX_ *e, flag, depth + 1)) return 1; 62048 0 62048
if (e && *e && oa_uses_flag(aTHX_ *e, flag, depth + 1)) return 1; 62048 0 62048
if (e && *e && oa_uses_flag(aTHX_ *e, flag, depth + 1)) return 1; 0 62048 62048
735 }
736 }
737 53993 return 0;
738 }
739  
740 static SV *oa_project(pTHX_ SV *node, const char *flag, int depth);
741  
742 /* project each value of a map-of-schemas / each element of a list-of-schemas */
743 16 static SV *oa_project_map(pTHX_ SV *v, const char *flag, int depth) {
744 16 HV *src = oa_hv_of(v), *dst;
745 HE *he;
746 16 ▶ if (!src) return newSVsv(v);
Branch: 1 branch
BranchTrueFalseTotal
if (!src) return newSVsv(v); 0 16 16
747 16 dst = newHV();
748 16 hv_iterinit(src);
749 32 ▶ while ((he = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) { 16 16 32
750 16 I32 kl; const char *k = hv_iterkey(he, &kl);
751 16 (void)hv_store(dst, k, kl,
752 oa_project(aTHX_ hv_iterval(src, he), flag, depth + 1), 0);
753 }
754 16 return newRV_noinc((SV *)dst);
755 }
756  
757 0 static SV *oa_project_list(pTHX_ SV *v, const char *flag, int depth) {
758 0 AV *src = oa_av_of(v), *dst;
759 SSize_t i, n;
760 0 ▶ if (!src) return newSVsv(v);
Branch: 1 branch
BranchTrueFalseTotal
if (!src) return newSVsv(v); 0 0 0
761 0 dst = newAV();
762 0 n = av_len(src) + 1;
763 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
764 0 SV **e = av_fetch(src, i, 0);
765 0 ▶ av_push(dst, oa_project(aTHX_ (e && *e) ? *e : NULL, flag, depth + 1));
Branch: 2 branches
BranchTrueFalseTotal
av_push(dst, oa_project(aTHX_ (e && *e) ? *e : NULL, flag, depth + 1)); 0 0 0
av_push(dst, oa_project(aTHX_ (e && *e) ? *e : NULL, flag, depth + 1)); 0 0 0
766 }
767 0 return newRV_noinc((SV *)dst);
768 }
769  
770 92 static SV *oa_project(pTHX_ SV *node, const char *flag, int depth) {
771 92 HV *src, *dst, *drop = NULL;
772 SV *props;
773 HE *he;
774 92 ▶ if (!node || depth > OA_PROJ_MAX_DEPTH || !SvROK(node)) return newSVsv(node);
Branch: 3 branches
BranchTrueFalseTotal
if (!node || depth > OA_PROJ_MAX_DEPTH || !SvROK(node)) return newSVsv(node); 92 0 92
if (!node || depth > OA_PROJ_MAX_DEPTH || !SvROK(node)) return newSVsv(node); 92 0 92
if (!node || depth > OA_PROJ_MAX_DEPTH || !SvROK(node)) return newSVsv(node); 0 92 92
775 92 ▶ if (SvTYPE(SvRV(node)) != SVt_PVHV) return newSVsv(node);
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(node)) != SVt_PVHV) return newSVsv(node); 0 92 92
776 92 src = (HV *)SvRV(node);
777 92 dst = newHV();
778  
779 /* `properties` first: it is the only place the flag is read, and what it
780 * finds decides which names have to come out of `required` below. */
781 92 props = oa_get(aTHX_ src, "properties");
782 92 ▶ if (oa_hv_of(props)) {
Branch: 1 branch
BranchTrueFalseTotal
if (oa_hv_of(props)) { 26 66 92
783 26 HV *ph = (HV *)SvRV(props), *np = newHV();
784 HE *pe;
785 26 hv_iterinit(ph);
786 102 ▶ while ((pe = hv_iternext(ph))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((pe = hv_iternext(ph))) { 76 26 102
787 76 I32 pkl; const char *pk = hv_iterkey(pe, &pkl);
788 76 SV *pv = hv_iterval(ph, pe);
789 76 ▶ if (oa_flagged(aTHX_ pv, flag)) {
Branch: 1 branch
BranchTrueFalseTotal
if (oa_flagged(aTHX_ pv, flag)) { 26 50 76
790 26 HV *no = newHV();
791 26 (void)hv_stores(no, "not", newRV_noinc((SV *)newHV()));
792 26 (void)hv_store(np, pk, pkl, newRV_noinc((SV *)no), 0);
793 26 ▶ if (!drop) drop = (HV *)sv_2mortal((SV *)newHV());
Branch: 1 branch
BranchTrueFalseTotal
if (!drop) drop = (HV *)sv_2mortal((SV *)newHV()); 26 0 26
794 26 (void)hv_store(drop, pk, pkl, newSViv(1), 0);
795 }
796 50 else (void)hv_store(np, pk, pkl,
797 oa_project(aTHX_ pv, flag, depth + 1), 0);
798 }
799 26 (void)hv_stores(dst, "properties", newRV_noinc((SV *)np));
800 }
801  
802 92 hv_iterinit(src);
803 259 ▶ while ((he = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) { 167 92 259
804 167 I32 kl; const char *k = hv_iterkey(he, &kl);
805 167 SV *v = hv_iterval(src, he);
806 192 ▶ if (kl == 10 && memEQ(k, "properties", 10)) continue; /* done above */
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 10 && memEQ(k, "properties", 10)) continue; /* done above */ 26 141 167
if (kl == 10 && memEQ(k, "properties", 10)) continue; /* done above */ 26 0 26
807 141 ▶ if (drop && kl == 8 && memEQ(k, "required", 8)) {
Branch: 3 branches
BranchTrueFalseTotal
if (drop && kl == 8 && memEQ(k, "required", 8)) { 51 90 141
if (drop && kl == 8 && memEQ(k, "required", 8)) { 25 26 51
if (drop && kl == 8 && memEQ(k, "required", 8)) { 25 0 25
808 25 AV *ra = oa_av_of(v), *keep = newAV();
809 25 ▶ SSize_t j, rn = ra ? av_len(ra) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t j, rn = ra ? av_len(ra) + 1 : 0; 25 0 25
810 75 ▶ for (j = 0; j < rn; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < rn; j++) { 50 25 75
811 50 SV **e = av_fetch(ra, j, 0);
812 STRLEN nl; const char *np2;
813 63 ▶ if (!e || !*e) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !*e) continue; 50 0 50
if (!e || !*e) continue; 0 50 50
814 50 np2 = SvPV_const(*e, nl);
815 50 ▶ if (hv_exists(drop, np2, (I32)nl)) continue; /* other direction */
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(drop, np2, (I32)nl)) continue; /* other direction */ 13 37 50
816 37 av_push(keep, newSVsv(*e));
817 }
818 /* an emptied `required` is deleted, not left as [] - JSON Schema
819 * allows the empty array but it is noise in ->spec and in errors */
820 25 ▶ if (av_len(keep) >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(keep) >= 0) 25 0 25
821 25 (void)hv_store(dst, k, kl, newRV_noinc((SV *)keep), 0);
822 0 else SvREFCNT_dec((SV *)keep);
823 25 continue;
824 }
825 /* `items` is a Schema Object but appears in none of the three tables:
826 * the converter special-cases it for the 3.0 tuple form, which by now
827 * has already become prefixItems. Both shapes are handled anyway. */
828 116 ▶ if (kl == 5 && memEQ(k, "items", 5)) {
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 5 && memEQ(k, "items", 5)) { 0 116 116
if (kl == 5 && memEQ(k, "items", 5)) { 0 0 0
829 0 ▶ (void)hv_store(dst, k, kl,
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_store(dst, k, kl, 0 0 0
830 oa_av_of(v) ? oa_project_list(aTHX_ v, flag, depth)
831 : oa_project(aTHX_ v, flag, depth + 1), 0);
832 0 continue;
833 }
834 116 ▶ if (oa_kw_schema(k, kl)) {
Branch: 1 branch
BranchTrueFalseTotal
if (oa_kw_schema(k, kl)) { 0 116 116
835 0 (void)hv_store(dst, k, kl, oa_project(aTHX_ v, flag, depth + 1), 0);
836 0 continue;
837 }
838 116 ▶ if (oa_kw_schema_map(k, kl)) {
Branch: 1 branch
BranchTrueFalseTotal
if (oa_kw_schema_map(k, kl)) { 0 116 116
839 0 (void)hv_store(dst, k, kl, oa_project_map(aTHX_ v, flag, depth), 0);
840 0 continue;
841 }
842 116 ▶ if (oa_kw_schema_list(k, kl)) {
Branch: 1 branch
BranchTrueFalseTotal
if (oa_kw_schema_list(k, kl)) { 0 116 116
843 0 (void)hv_store(dst, k, kl, oa_project_list(aTHX_ v, flag, depth), 0);
844 0 continue;
845 }
846 116 (void)hv_store(dst, k, kl, newSVsv(v), 0); /* user JSON: untouched */
847 }
848 92 return newRV_noinc((SV *)dst);
849 }
850  
851 /* Build the shared $defs (rewritten components.schemas) once per api, plus a
852 * projection of it per direction when the document uses readOnly/writeOnly. */
853 3614 static void oa_build_defs(pTHX_ oa_api *a) {
854 3614 HV *doc = (HV *)SvRV(a->spec);
855 3614 HV *comp = oa_hv_of(oa_get(aTHX_ doc, "components"));
856 3614 ▶ SV *schemas = comp ? oa_get(aTHX_ comp, "schemas") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *schemas = comp ? oa_get(aTHX_ comp, "schemas") : NULL; 2335 1279 3614
857  
858 3614 a->proj_req = oa_uses_flag(aTHX_ a->spec, "readOnly", 0);
859 3614 a->proj_resp = oa_uses_flag(aTHX_ a->spec, "writeOnly", 0);
860  
861 3614 ▶ if (schemas && oa_hv_of(schemas)) {
Branch: 2 branches
BranchTrueFalseTotal
if (schemas && oa_hv_of(schemas)) { 2291 1323 3614
if (schemas && oa_hv_of(schemas)) { 2291 0 2291
862 2291 a->defs = oa_keep(aTHX_ a, oa_rewrite_refs(aTHX_ schemas));
863 /* projected from the REWRITTEN defs, so a $ref from a request body
864 * reaches the request-projected component transitively.
865 *
866 * oa_project_MAP, not oa_project: a defs block is `name => schema`,
867 * not a schema. Handed to oa_project it finds no `properties` on the
868 * map, treats every component name as an unknown keyword and copies
869 * it through untouched - so the projection silently did nothing and
870 * only inline schemas were ever direction-aware. */
871 2291 ▶ if (a->proj_req)
Branch: 1 branch
BranchTrueFalseTotal
if (a->proj_req) 8 2283 2291
872 8 a->defs_req = oa_keep(aTHX_ a,
873 oa_project_map(aTHX_ a->defs, "readOnly", 0));
874 2291 ▶ if (a->proj_resp)
Branch: 1 branch
BranchTrueFalseTotal
if (a->proj_resp) 8 2283 2291
875 8 a->defs_resp = oa_keep(aTHX_ a,
876 oa_project_map(aTHX_ a->defs, "writeOnly", 0));
877 }
878 3614 }
879  
880 /* Rewrite a schema's refs, attach the shared $defs, compile via the JSF ABI.
881 * The returned handle is owned by the keep AV. */
882 15920 static SV *oa_compile_schema(pTHX_ oa_api *a, SV *schema, int coerce, int dir) {
883 15920 SV *to = schema;
884 15920 SV *defs = a->defs;
885 /* Which way is this payload travelling? A request forbids readOnly, a
886 * response forbids writeOnly, and each reaches its own projection of the
887 * components so a $ref lands on the right variant. OA_DIR_NONE, and a
888 * document that never uses either flag, compile down the original path. */
889 15920 ▶ if (dir == OA_DIR_REQ) {
Branch: 1 branch
BranchTrueFalseTotal
if (dir == OA_DIR_REQ) { 11941 3979 15920
890 11941 ▶ if (a->defs_req) defs = a->defs_req;
Branch: 1 branch
BranchTrueFalseTotal
if (a->defs_req) defs = a->defs_req; 16 11925 11941
891 11941 ▶ if (a->proj_req) schema = sv_2mortal(oa_project(aTHX_ schema, "readOnly", 0));
Branch: 1 branch
BranchTrueFalseTotal
if (a->proj_req) schema = sv_2mortal(oa_project(aTHX_ schema, "readOnly", 0)); 18 11923 11941
892 }
893 3979 ▶ else if (dir == OA_DIR_RESP) {
Branch: 1 branch
BranchTrueFalseTotal
else if (dir == OA_DIR_RESP) { 3979 0 3979
894 3979 ▶ if (a->defs_resp) defs = a->defs_resp;
Branch: 1 branch
BranchTrueFalseTotal
if (a->defs_resp) defs = a->defs_resp; 8 3971 3979
895 3979 ▶ if (a->proj_resp) schema = sv_2mortal(oa_project(aTHX_ schema, "writeOnly", 0));
Branch: 1 branch
BranchTrueFalseTotal
if (a->proj_resp) schema = sv_2mortal(oa_project(aTHX_ schema, "writeOnly", 0)); 8 3971 3979
896 }
897 15920 to = schema;
898 15920 ▶ if (defs && oa_hv_of(schema)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defs && oa_hv_of(schema)) { 14624 1296 15920
if (defs && oa_hv_of(schema)) { 14624 0 14624
899 14624 SV *rw = sv_2mortal(oa_rewrite_refs(aTHX_ schema));
900 14624 HV *w = (HV *)SvRV(rw);
901 14624 SV **own = hv_fetchs(w, "$defs", 0);
902 14624 ▶ if (own && *own && oa_hv_of(*own)) {
Branch: 3 branches
BranchTrueFalseTotal
if (own && *own && oa_hv_of(*own)) { 0 14624 14624
if (own && *own && oa_hv_of(*own)) { 0 0 0
if (own && *own && oa_hv_of(*own)) { 0 0 0
903 /* schema carries its own $defs: overlay ours underneath */
904 0 HV *merged = newHV(), *shared = (HV *)SvRV(defs), *mine;
905 HE *he;
906 0 hv_iterinit(shared);
907 0 ▶ while ((he = hv_iternext(shared))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(shared))) { 0 0 0
908 0 I32 kl; const char *k = hv_iterkey(he, &kl);
909 0 (void)hv_store(merged, k, kl,
910 SvREFCNT_inc(hv_iterval(shared, he)), 0);
911 }
912 0 mine = (HV *)SvRV(*own);
913 0 hv_iterinit(mine);
914 0 ▶ while ((he = hv_iternext(mine))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(mine))) { 0 0 0
915 0 I32 kl; const char *k = hv_iterkey(he, &kl);
916 0 (void)hv_store(merged, k, kl,
917 SvREFCNT_inc(hv_iterval(mine, he)), 0);
918 }
919 0 (void)hv_stores(w, "$defs", newRV_noinc((SV *)merged));
920 } else {
921 14624 (void)hv_stores(w, "$defs", SvREFCNT_inc(defs));
922 }
923 14624 to = rw;
924 }
925 15920 return oa_keep(aTHX_ a, JSF->compile(aTHX_ to, &PL_sv_undef, coerce, 1));
926 }
927  
928 /* ---- path template -> segments -------------------------------------------- */
929  
930 7577 static void oa_compile_segs(pTHX_ oa_api *a, oa_op *o, const char *tpl, STRLEN tl) {
931 /* count segments */
932 7577 int n = 0, i;
933 7577 STRLEN s = 0;
934 17975 ▶ while (s < tl) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < tl) { 10398 7577 17975
935 20796 ▶ while (s < tl && tpl[s] == '/') s++;
Branch: 2 branches
BranchTrueFalseTotal
while (s < tl && tpl[s] == '/') s++; 20796 0 20796
while (s < tl && tpl[s] == '/') s++; 10398 10398 20796
936 10398 ▶ if (s < tl) n++;
Branch: 1 branch
BranchTrueFalseTotal
if (s < tl) n++; 10398 0 10398
937 53075 ▶ while (s < tl && tpl[s] != '/') s++;
Branch: 2 branches
BranchTrueFalseTotal
while (s < tl && tpl[s] != '/') s++; 45498 7577 53075
while (s < tl && tpl[s] != '/') s++; 42677 2821 45498
938 }
939 7577 o->nsegs = n;
940 7577 ▶ o->segs = n ? (oa_seg *)calloc((size_t)n, sizeof(oa_seg)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
o->segs = n ? (oa_seg *)calloc((size_t)n, sizeof(oa_seg)) : NULL; 7577 0 7577
941 7577 ▶ if (n && !o->segs) croak("Open::API: out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (n && !o->segs) croak("Open::API: out of memory"); 7577 0 7577
if (n && !o->segs) croak("Open::API: out of memory"); 0 7577 7577
942 7577 s = 0; i = 0;
943 17975 ▶ while (s < tl) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < tl) { 10398 7577 17975
944 STRLEN e;
945 20796 ▶ while (s < tl && tpl[s] == '/') s++;
Branch: 2 branches
BranchTrueFalseTotal
while (s < tl && tpl[s] == '/') s++; 20796 0 20796
while (s < tl && tpl[s] == '/') s++; 10398 10398 20796
946 10398 ▶ if (s >= tl) break;
Branch: 1 branch
BranchTrueFalseTotal
if (s >= tl) break; 0 10398 10398
947 10398 e = s;
948 53075 ▶ while (e < tl && tpl[e] != '/') e++;
Branch: 2 branches
BranchTrueFalseTotal
while (e < tl && tpl[e] != '/') e++; 45498 7577 53075
while (e < tl && tpl[e] != '/') e++; 42677 2821 45498
949 10398 ▶ if (tpl[s] == '{' && tpl[e - 1] == '}' && e - s > 2) {
Branch: 3 branches
BranchTrueFalseTotal
if (tpl[s] == '{' && tpl[e - 1] == '}' && e - s > 2) { 2720 7678 10398
if (tpl[s] == '{' && tpl[e - 1] == '}' && e - s > 2) { 2720 0 2720
if (tpl[s] == '{' && tpl[e - 1] == '}' && e - s > 2) { 2720 0 2720
950 2720 o->segs[i].pname = oa_keep(aTHX_ a, newSVpvn(tpl + s + 1, e - s - 2));
951 } else {
952 7678 o->segs[i].lit = oa_keep(aTHX_ a, newSVpvn(tpl + s, e - s));
953 }
954 10398 i++;
955 10398 s = e;
956 }
957 7577 }
958  
959 /* ---- parameters ------------------------------------------------------------ */
960  
961 17283 static int oa_param_loc(pTHX_ SV *in) {
962 STRLEN l; const char *p;
963 17283 ▶ if (!in) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!in) return -1; 0 17283 17283
964 17283 p = SvPV_const(in, l);
965 17283 ▶ if (l == 4 && memEQ(p, "path", 4)) return OA_IN_PATH;
Branch: 2 branches
BranchTrueFalseTotal
if (l == 4 && memEQ(p, "path", 4)) return OA_IN_PATH; 5454 11829 17283
if (l == 4 && memEQ(p, "path", 4)) return OA_IN_PATH; 5454 0 5454
966 11829 ▶ if (l == 5 && memEQ(p, "query", 5)) return OA_IN_QUERY;
Branch: 2 branches
BranchTrueFalseTotal
if (l == 5 && memEQ(p, "query", 5)) return OA_IN_QUERY; 6574 5255 11829
if (l == 5 && memEQ(p, "query", 5)) return OA_IN_QUERY; 6574 0 6574
967 5255 ▶ if (l == 6 && memEQ(p, "header", 6)) return OA_IN_HEADER;
Branch: 2 branches
BranchTrueFalseTotal
if (l == 6 && memEQ(p, "header", 6)) return OA_IN_HEADER; 5255 0 5255
if (l == 6 && memEQ(p, "header", 6)) return OA_IN_HEADER; 2649 2606 5255
968 2606 ▶ if (l == 6 && memEQ(p, "cookie", 6)) return OA_IN_COOKIE;
Branch: 2 branches
BranchTrueFalseTotal
if (l == 6 && memEQ(p, "cookie", 6)) return OA_IN_COOKIE; 2606 0 2606
if (l == 6 && memEQ(p, "cookie", 6)) return OA_IN_COOKIE; 2606 0 2606
969 0 return -1;
970 }
971  
972 /* same (name, in)? - for the path-item / operation parameter merge */
973 3921 static int oa_param_same(pTHX_ HV *x, HV *y) {
974 3921 SV *xn = oa_get(aTHX_ x, "name"), *yn = oa_get(aTHX_ y, "name");
975 3921 SV *xi = oa_get(aTHX_ x, "in"), *yi = oa_get(aTHX_ y, "in");
976 3921 ▶ if (!xn || !yn || !xi || !yi) return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (!xn || !yn || !xi || !yi) return 0; 3921 0 3921
if (!xn || !yn || !xi || !yi) return 0; 3921 0 3921
if (!xn || !yn || !xi || !yi) return 0; 3921 0 3921
if (!xn || !yn || !xi || !yi) return 0; 0 3921 3921
977 3921 ▶ return sv_eq(xn, yn) && sv_eq(xi, yi);
Branch: 2 branches
BranchTrueFalseTotal
return sv_eq(xn, yn) && sv_eq(xi, yi); 1 3920 3921
return sv_eq(xn, yn) && sv_eq(xi, yi); 1 0 1
978 }
979  
980 /* Merge path-item + operation parameter lists (operation wins on name+in),
981 * then compile each into the op's per-location arrays. */
982 7577 static void oa_compile_params(pTHX_ oa_api *a, oa_op *o, AV *item_params,
983 AV *op_params) {
984 HV *merged[64];
985 7577 int nm = 0, i, loc;
986 7577 int counts[OA_IN_N] = { 0, 0, 0, 0 };
987 7577 int fill[OA_IN_N] = { 0, 0, 0, 0 };
988  
989 /* path-item level first */
990 7577 ▶ if (item_params) {
Branch: 1 branch
BranchTrueFalseTotal
if (item_params) { 2603 4974 7577
991 2603 SSize_t n = av_len(item_params) + 1, j;
992 5206 ▶ for (j = 0; j < n && nm < 64; j++) {
Branch: 2 branches
BranchTrueFalseTotal
for (j = 0; j < n && nm < 64; j++) { 2603 2603 5206
for (j = 0; j < n && nm < 64; j++) { 2603 0 2603
993 2603 SV **e = av_fetch(item_params, j, 0);
994 2603 ▶ HV *ph = e ? oa_hv_of(*e) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *ph = e ? oa_hv_of(*e) : NULL; 2603 0 2603
995 2603 ▶ if (ph) merged[nm++] = ph;
Branch: 1 branch
BranchTrueFalseTotal
if (ph) merged[nm++] = ph; 2603 0 2603
996 }
997 }
998 /* operation level: replace same (name,in), else append */
999 7577 ▶ if (op_params) {
Branch: 1 branch
BranchTrueFalseTotal
if (op_params) { 4715 2862 7577
1000 4715 SSize_t n = av_len(op_params) + 1, j;
1001 10738 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 6023 4715 10738
1002 6023 SV **e = av_fetch(op_params, j, 0);
1003 6023 ▶ HV *ph = e ? oa_hv_of(*e) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *ph = e ? oa_hv_of(*e) : NULL; 6023 0 6023
1004 6023 int k, hit = 0;
1005 6023 ▶ if (!ph) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ph) continue; 0 6023 6023
1006 9943 ▶ for (k = 0; k < nm; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nm; k++) 3921 6022 9943
1007 3921 ▶ if (oa_param_same(aTHX_ merged[k], ph)) { merged[k] = ph; hit = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (oa_param_same(aTHX_ merged[k], ph)) { merged[k] = ph; hit = 1; break; } 1 3920 3921
1008 6023 ▶ if (!hit) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hit) { 6022 1 6023
1009 6022 ▶ if (nm >= 64) croak("Open::API: more than 64 parameters on %s",
Branch: 1 branch
BranchTrueFalseTotal
if (nm >= 64) croak("Open::API: more than 64 parameters on %s", 0 6022 6022
1010 SvPV_nolen(o->op_id));
1011 6022 merged[nm++] = ph;
1012 }
1013 }
1014 }
1015  
1016 16202 ▶ for (i = 0; i < nm; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nm; i++) { 8625 7577 16202
1017 8625 int l = oa_param_loc(aTHX_ oa_get(aTHX_ merged[i], "in"));
1018 8625 ▶ if (l < 0) croak("Open::API: parameter with bad 'in' on %s",
Branch: 1 branch
BranchTrueFalseTotal
if (l < 0) croak("Open::API: parameter with bad 'in' on %s", 0 8625 8625
1019 SvPV_nolen(o->op_id));
1020 8625 counts[l]++;
1021 }
1022 37885 ▶ for (loc = 0; loc < OA_IN_N; loc++) {
Branch: 1 branch
BranchTrueFalseTotal
for (loc = 0; loc < OA_IN_N; loc++) { 30308 7577 37885
1023 30308 o->nparams[loc] = counts[loc];
1024 60616 o->params[loc] = counts[loc]
1025 30308 ▶ ? (oa_param *)calloc((size_t)counts[loc], sizeof(oa_param)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (oa_param *)calloc((size_t)counts[loc], sizeof(oa_param)) : NULL; 7319 22989 30308
1026 30308 ▶ if (counts[loc] && !o->params[loc]) croak("Open::API: out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (counts[loc] && !o->params[loc]) croak("Open::API: out of memory"); 7319 22989 30308
if (counts[loc] && !o->params[loc]) croak("Open::API: out of memory"); 0 7319 7319
1027 }
1028 16201 ▶ for (i = 0; i < nm; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nm; i++) { 8625 7576 16201
1029 8625 HV *ph = merged[i];
1030 8625 SV *name = oa_get(aTHX_ ph, "name");
1031 8625 SV *schema = oa_get(aTHX_ ph, "schema");
1032 8625 SV *req = oa_get(aTHX_ ph, "required");
1033 8625 int l = oa_param_loc(aTHX_ oa_get(aTHX_ ph, "in"));
1034 oa_param *pp;
1035 8625 ▶ if (!name) croak("Open::API: parameter without a name on %s",
Branch: 1 branch
BranchTrueFalseTotal
if (!name) croak("Open::API: parameter without a name on %s", 0 8625 8625
1036 SvPV_nolen(o->op_id));
1037 8625 pp = &o->params[l][fill[l]++];
1038 8625 pp->name = oa_keep(aTHX_ a, newSVsv(name));
1039 8625 pp->required = (l == OA_IN_PATH) ? 1
1040 8625 ▶ : (req && oa_sv_truthy(aTHX_ req)) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
: (req && oa_sv_truthy(aTHX_ req)) ? 1 : 0; 5902 2723 8625
: (req && oa_sv_truthy(aTHX_ req)) ? 1 : 0; 1311 4591 5902
: (req && oa_sv_truthy(aTHX_ req)) ? 1 : 0; 1310 1 1311
1041 8625 pp->ctype = NULL;
1042 8625 ▶ pp->is_array = schema ? oa_schema_is_array(aTHX_ a, schema, 0) : 0;
Branch: 1 branch
BranchTrueFalseTotal
pp->is_array = schema ? oa_schema_is_array(aTHX_ a, schema, 0) : 0; 8015 610 8625
1043 8625 ▶ pp->is_object = schema ? oa_schema_is_object(aTHX_ a, schema, 0) : 0;
Branch: 1 branch
BranchTrueFalseTotal
pp->is_object = schema ? oa_schema_is_object(aTHX_ a, schema, 0) : 0; 8015 610 8625
1044 /* only form+explode reads these, but the schema is here and the
1045 * request path is not the place to walk it */
1046 30 ▶ pp->props = (pp->is_object && schema)
Branch: 1 branch
BranchTrueFalseTotal
pp->props = (pp->is_object && schema) 30 0 30
1047 8655 ▶ ? oa_schema_props(aTHX_ a, schema) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? oa_schema_props(aTHX_ a, schema) : NULL; 30 8595 8625
1048 8625 pp->handle = schema
1049 8625 ▶ ? oa_compile_schema(aTHX_ a, schema, 1, OA_DIR_REQ) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? oa_compile_schema(aTHX_ a, schema, 1, OA_DIR_REQ) : NULL; 8015 610 8625
1050  
1051 /* How the value is spelled on the wire. The default follows the
1052 * location: `simple` in a path or header, `form` in a query or
1053 * cookie; `explode` defaults to true for `form` alone. An unknown
1054 * style is left at the default rather than refused - the document is
1055 * still a valid document, and refusing here would reject specs that
1056 * work against other tools. */
1057 {
1058 8625 SV *st = oa_get(aTHX_ ph, "style");
1059 8625 SV *ex = oa_get(aTHX_ ph, "explode");
1060 5902 ▶ pp->style = (l == OA_IN_PATH || l == OA_IN_HEADER)
Branch: 1 branch
BranchTrueFalseTotal
pp->style = (l == OA_IN_PATH || l == OA_IN_HEADER) 1317 4585 5902
1061 14527 ▶ ? OA_ST_SIMPLE : OA_ST_FORM;
Branch: 1 branch
BranchTrueFalseTotal
? OA_ST_SIMPLE : OA_ST_FORM; 5902 2723 8625
1062 8625 ▶ if (st) {
Branch: 1 branch
BranchTrueFalseTotal
if (st) { 119 8506 8625
1063 119 STRLEN sl; const char *sp = SvPV_const(st, sl);
1064 119 ▶ if (sl == 6 && memEQ(sp, "simple", 6)) pp->style = OA_ST_SIMPLE;
Branch: 2 branches
BranchTrueFalseTotal
if (sl == 6 && memEQ(sp, "simple", 6)) pp->style = OA_ST_SIMPLE; 50 69 119
if (sl == 6 && memEQ(sp, "simple", 6)) pp->style = OA_ST_SIMPLE; 28 22 50
1065 91 ▶ else if (sl == 5 && memEQ(sp, "label", 5)) pp->style = OA_ST_LABEL;
Branch: 2 branches
BranchTrueFalseTotal
else if (sl == 5 && memEQ(sp, "label", 5)) pp->style = OA_ST_LABEL; 22 69 91
else if (sl == 5 && memEQ(sp, "label", 5)) pp->style = OA_ST_LABEL; 22 0 22
1066 69 ▶ else if (sl == 6 && memEQ(sp, "matrix", 6)) pp->style = OA_ST_MATRIX;
Branch: 2 branches
BranchTrueFalseTotal
else if (sl == 6 && memEQ(sp, "matrix", 6)) pp->style = OA_ST_MATRIX; 22 47 69
else if (sl == 6 && memEQ(sp, "matrix", 6)) pp->style = OA_ST_MATRIX; 22 0 22
1067 47 ▶ else if (sl == 4 && memEQ(sp, "form", 4)) pp->style = OA_ST_FORM;
Branch: 2 branches
BranchTrueFalseTotal
else if (sl == 4 && memEQ(sp, "form", 4)) pp->style = OA_ST_FORM; 26 21 47
else if (sl == 4 && memEQ(sp, "form", 4)) pp->style = OA_ST_FORM; 26 0 26
1068 21 ▶ else if (sl == 14 && memEQ(sp, "spaceDelimited", 14)) pp->style = OA_ST_SPACE;
Branch: 2 branches
BranchTrueFalseTotal
else if (sl == 14 && memEQ(sp, "spaceDelimited", 14)) pp->style = OA_ST_SPACE; 8 13 21
else if (sl == 14 && memEQ(sp, "spaceDelimited", 14)) pp->style = OA_ST_SPACE; 8 0 8
1069 13 ▶ else if (sl == 13 && memEQ(sp, "pipeDelimited", 13)) pp->style = OA_ST_PIPE;
Branch: 2 branches
BranchTrueFalseTotal
else if (sl == 13 && memEQ(sp, "pipeDelimited", 13)) pp->style = OA_ST_PIPE; 8 5 13
else if (sl == 13 && memEQ(sp, "pipeDelimited", 13)) pp->style = OA_ST_PIPE; 8 0 8
1070 5 ▶ else if (sl == 10 && memEQ(sp, "deepObject", 10)) pp->style = OA_ST_DEEP;
Branch: 2 branches
BranchTrueFalseTotal
else if (sl == 10 && memEQ(sp, "deepObject", 10)) pp->style = OA_ST_DEEP; 5 0 5
else if (sl == 10 && memEQ(sp, "deepObject", 10)) pp->style = OA_ST_DEEP; 5 0 5
1071 }
1072 115 pp->explode = ex ? (oa_sv_truthy(aTHX_ ex) ? 1 : 0)
1073 8740 ▶ : (pp->style == OA_ST_FORM ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
: (pp->style == OA_ST_FORM ? 1 : 0); 115 8510 8625
1074 }
1075 { /* reported to consumers; the specification gives it no effect on
1076 * validation, so it is carried rather than enforced */
1077 8625 SV *dep = oa_get(aTHX_ ph, "deprecated");
1078 8625 ▶ pp->deprecated = (dep && oa_sv_truthy(aTHX_ dep)) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
pp->deprecated = (dep && oa_sv_truthy(aTHX_ dep)) ? 1 : 0; 2 8623 8625
pp->deprecated = (dep && oa_sv_truthy(aTHX_ dep)) ? 1 : 0; 2 0 2
1079 }
1080 { /* allowEmptyValue: `?flag=` is present with no value. 3.1
1081 * deprecates the keyword and its semantics are under-specified,
1082 * so the reading here is the narrow one that can be defended:
1083 * a QUERY parameter whose value is the empty string is permitted
1084 * rather than pushed at its schema. Everywhere else it is
1085 * ignored, which is what the specification asks for. */
1086 8625 SV *ae = oa_get(aTHX_ ph, "allowEmptyValue");
1087 8625 pp->allow_empty = (l == OA_IN_QUERY
1088 8625 ▶ && ae && oa_sv_truthy(aTHX_ ae)) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
&& ae && oa_sv_truthy(aTHX_ ae)) ? 1 : 0; 3282 5343 8625
&& ae && oa_sv_truthy(aTHX_ ae)) ? 1 : 0; 2 3280 3282
&& ae && oa_sv_truthy(aTHX_ ae)) ? 1 : 0; 2 0 2
1089 }
1090 { /* allowReserved: reserved characters MAY be sent without
1091 * percent-encoding. Query only. The specification also allows it
1092 * on a path parameter, but a reserved character in a path segment
1093 * changes the SHAPE of the path, so honouring it there would be a
1094 * bug wearing a feature's clothes. */
1095 8625 SV *ar = oa_get(aTHX_ ph, "allowReserved");
1096 8625 pp->allow_reserved = (l == OA_IN_QUERY
1097 8625 ▶ && ar && oa_sv_truthy(aTHX_ ar)) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
&& ar && oa_sv_truthy(aTHX_ ar)) ? 1 : 0; 3282 5343 8625
&& ar && oa_sv_truthy(aTHX_ ar)) ? 1 : 0; 4 3278 3282
&& ar && oa_sv_truthy(aTHX_ ar)) ? 1 : 0; 4 0 4
1098 }
1099  
1100 /* `content` instead of `schema`: the value is a document, not a
1101 * string standing in for one. The spec allows exactly one entry, so
1102 * the first is the one - and it is compiled WITHOUT coercion, unlike
1103 * an ordinary parameter, because a decoded JSON document already has
1104 * the types the schema is talking about. */
1105 8625 ▶ if (!schema) {
Branch: 1 branch
BranchTrueFalseTotal
if (!schema) { 610 8015 8625
1106 610 HV *content = oa_hv_of(oa_get(aTHX_ ph, "content"));
1107 HE *he;
1108 /* the map MUST contain exactly one entry: it says what the single
1109 * value on the wire is, and two answers is no answer */
1110 610 ▶ if (content && HvUSEDKEYS(content) > 1)
Branch: 3 branches
BranchTrueFalseTotal
if (content && HvUSEDKEYS(content) > 1) 610 0 610
if (content && HvUSEDKEYS(content) > 1) 0 610 610
if (content && HvUSEDKEYS(content) > 1) 1 609 610
1111 3 ▶ croak("Open::API: parameter '%s' on %s declares %d content "
Branch: 2 branches
BranchTrueFalseTotal
croak("Open::API: parameter '%s' on %s declares %d content " 0 1 1
croak("Open::API: parameter '%s' on %s declares %d content " 1 0 1
1112 "entries; the map must contain only one",
1113 SvPV_nolen(name), SvPV_nolen(o->op_id),
1114 (int)HvUSEDKEYS(content));
1115 609 ▶ if (content && HvUSEDKEYS(content)) {
Branch: 3 branches
BranchTrueFalseTotal
if (content && HvUSEDKEYS(content)) { 609 0 609
if (content && HvUSEDKEYS(content)) { 0 609 609
if (content && HvUSEDKEYS(content)) { 609 0 609
1116 609 hv_iterinit(content);
1117 609 ▶ if ((he = hv_iternext(content))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((he = hv_iternext(content))) { 609 0 609
1118 609 I32 ckl; const char *ck = hv_iterkey(he, &ckl);
1119 609 HV *media = oa_hv_of(hv_iterval(content, he));
1120 609 ▶ SV *cs = media ? oa_get(aTHX_ media, "schema") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *cs = media ? oa_get(aTHX_ media, "schema") : NULL; 609 0 609
1121 609 pp->ctype = oa_keep(aTHX_ a, newSVpvn(ck, (STRLEN)ckl));
1122 609 ▶ if (cs && oa_ctype_is_json(ck, (STRLEN)ckl))
Branch: 2 branches
BranchTrueFalseTotal
if (cs && oa_ctype_is_json(ck, (STRLEN)ckl)) 609 0 609
if (cs && oa_ctype_is_json(ck, (STRLEN)ckl)) 607 2 609
1123 607 pp->handle = oa_compile_schema(aTHX_ a, cs, 0, OA_DIR_REQ);
1124 }
1125 }
1126 }
1127 }
1128 7576 }
1129  
1130 /* ---- requestBody / responses ----------------------------------------------- */
1131  
1132 7576 static void oa_compile_body(pTHX_ oa_api *a, oa_op *o, SV *rb) {
1133 7576 HV *rbh = oa_hv_of(rb);
1134 HV *content;
1135 HE *he;
1136 7576 int n = 0, i = 0;
1137 7576 ▶ if (!rbh) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!rbh) return; 4257 3319 7576
1138 {
1139 3319 SV *req = oa_get(aTHX_ rbh, "required");
1140 3319 ▶ o->body_required = (req && oa_sv_truthy(aTHX_ req)) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
o->body_required = (req && oa_sv_truthy(aTHX_ req)) ? 1 : 0; 3310 9 3319
o->body_required = (req && oa_sv_truthy(aTHX_ req)) ? 1 : 0; 3310 0 3310
1141 }
1142 3319 content = oa_hv_of(oa_get(aTHX_ rbh, "content"));
1143 3319 ▶ if (!content) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!content) return; 0 3319 3319
1144 3319 ▶ n = (int)HvUSEDKEYS(content);
Branch: 1 branch
BranchTrueFalseTotal
n = (int)HvUSEDKEYS(content); 0 3319 3319
1145 3319 ▶ if (!n) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return; 0 3319 3319
1146 3319 o->bodies = (oa_body *)calloc((size_t)n, sizeof(oa_body));
1147 3319 ▶ if (!o->bodies) croak("Open::API: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!o->bodies) croak("Open::API: out of memory"); 0 3319 3319
1148 3319 o->nbodies = n;
1149 3319 hv_iterinit(content);
1150 7938 ▶ while ((he = hv_iternext(content))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(content))) { 4619 3319 7938
1151 4619 I32 kl; const char *k = hv_iterkey(he, &kl);
1152 4619 HV *media = oa_hv_of(hv_iterval(content, he));
1153 4619 ▶ SV *schema = media ? oa_get(aTHX_ media, "schema") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *schema = media ? oa_get(aTHX_ media, "schema") : NULL; 4619 0 4619
1154 4619 int kind = oa_ctype_kind(k, (STRLEN)kl);
1155 4619 o->bodies[i].ctype = oa_keep(aTHX_ a, newSVpvn(k, (STRLEN)kl));
1156 4619 o->bodies[i].kind = kind;
1157 /* A handle is compiled for every type we can decode, and for a range,
1158 * whose decoder is chosen from the request. OPAQUE gets none, so a
1159 * declared text/plain keeps passing through with its body preserved -
1160 * the behaviour t/06-body.t pins and the petstore fixtures rely on.
1161 *
1162 * Form bodies are coerced, like parameters: every value arrives as
1163 * text, so `count=3` has to satisfy an integer the way `?count=3`
1164 * does. JSON already carries its own types. */
1165 3320 ▶ o->bodies[i].handle = (schema && kind != OA_MT_OPAQUE)
Branch: 1 branch
BranchTrueFalseTotal
o->bodies[i].handle = (schema && kind != OA_MT_OPAQUE) 3319 1 3320
1166 3319 ▶ ? oa_compile_schema(aTHX_ a, schema,
Branch: 2 branches
BranchTrueFalseTotal
? oa_compile_schema(aTHX_ a, schema, 3303 16 3319
? oa_compile_schema(aTHX_ a, schema, 8 3295 3303
1167 (kind == OA_MT_FORM || kind == OA_MT_MULTIPART),
1168 OA_DIR_REQ)
1169 4620 ▶ : NULL;
Branch: 1 branch
BranchTrueFalseTotal
: NULL; 3320 1299 4619
1170 /* An Encoding Object says how each PROPERTY of a form body is
1171 * serialized. It has meaning only where a form body is parsed into
1172 * properties in the first place, so it is kept for form and multipart
1173 * and ignored elsewhere - a JSON body already carries its own types. */
1174 {
1175 4619 ▶ SV *enc = media ? oa_get(aTHX_ media, "encoding") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *enc = media ? oa_get(aTHX_ media, "encoding") : NULL; 4619 0 4619
1176 4619 o->bodies[i].encoding =
1177 13 ▶ (enc && oa_hv_of(enc)
Branch: 1 branch
BranchTrueFalseTotal
(enc && oa_hv_of(enc) 13 0 13
1178 13 ▶ && (kind == OA_MT_FORM || kind == OA_MT_MULTIPART))
Branch: 2 branches
BranchTrueFalseTotal
&& (kind == OA_MT_FORM || kind == OA_MT_MULTIPART)) 4 9 13
&& (kind == OA_MT_FORM || kind == OA_MT_MULTIPART)) 4 0 4
1179 4632 ▶ ? oa_keep(aTHX_ a, newSVsv(enc)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? oa_keep(aTHX_ a, newSVsv(enc)) : NULL; 13 4606 4619
1180 }
1181 4619 i++;
1182 }
1183 }
1184  
1185 7576 static void oa_compile_responses(pTHX_ oa_api *a, oa_op *o, SV *resps) {
1186 7576 HV *rh = oa_hv_of(resps);
1187 HE *she;
1188 7576 int cap = 0, n = 0;
1189 7576 oa_resp *rows = NULL;
1190 7576 ▶ if (!rh) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!rh) return; 0 7576 7576
1191 7576 hv_iterinit(rh);
1192 16455 ▶ while ((she = hv_iternext(rh))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((she = hv_iternext(rh))) { 8880 7575 16455
1193 8880 I32 skl; const char *sk = hv_iterkey(she, &skl);
1194 8880 HV *robj = oa_hv_of(hv_iterval(rh, she));
1195 8880 ▶ HV *content = robj ? oa_hv_of(oa_get(aTHX_ robj, "content")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *content = robj ? oa_hv_of(oa_get(aTHX_ robj, "content")) : NULL; 8880 0 8880
1196 HE *che;
1197 /* description is REQUIRED on a Response Object. Checked BEFORE the
1198 * `content` early-return below, or a response with neither would be
1199 * skipped rather than refused. */
1200 8880 ▶ if (robj && !oa_get(aTHX_ robj, "description"))
Branch: 2 branches
BranchTrueFalseTotal
if (robj && !oa_get(aTHX_ robj, "description")) 8880 0 8880
if (robj && !oa_get(aTHX_ robj, "description")) 1 8879 8880
1201 2 croak("Open::API: %s: response '%.*s' has no description",
1202 SvPV_nolen(o->op_id), (int)skl, sk);
1203 8879 ▶ if (!content) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!content) continue; 4904 3975 8879
1204 3975 hv_iterinit(content);
1205 7950 ▶ while ((che = hv_iternext(content))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((che = hv_iternext(content))) { 3975 3975 7950
1206 3975 I32 ckl; const char *ck = hv_iterkey(che, &ckl);
1207 3975 HV *media = oa_hv_of(hv_iterval(content, che));
1208 3975 ▶ SV *schema = media ? oa_get(aTHX_ media, "schema") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *schema = media ? oa_get(aTHX_ media, "schema") : NULL; 3975 0 3975
1209 3975 ▶ if (!schema || !oa_ctype_is_json(ck, (STRLEN)ckl)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!schema || !oa_ctype_is_json(ck, (STRLEN)ckl)) continue; 3969 6 3975
if (!schema || !oa_ctype_is_json(ck, (STRLEN)ckl)) continue; 4 3965 3969
1210 3965 ▶ if (n == cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == cap) { 3964 1 3965
1211 3964 ▶ cap = cap ? cap * 2 : 4;
Branch: 1 branch
BranchTrueFalseTotal
cap = cap ? cap * 2 : 4; 0 3964 3964
1212 3964 rows = (oa_resp *)realloc(rows, (size_t)cap * sizeof(oa_resp));
1213 3964 ▶ if (!rows) croak("Open::API: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!rows) croak("Open::API: out of memory"); 0 3964 3964
1214 }
1215 3965 rows[n].status = oa_keep(aTHX_ a, newSVpvn(sk, (STRLEN)skl));
1216 3965 rows[n].ctype = oa_keep(aTHX_ a, newSVpvn(ck, (STRLEN)ckl));
1217 3965 rows[n].handle = oa_compile_schema(aTHX_ a, schema, 0, OA_DIR_RESP);
1218 3965 n++;
1219 }
1220 }
1221 7575 o->resps = rows;
1222 7575 o->nresps = n;
1223  
1224 /* Headers, in a second pass, because they hang off the STATUS and not the
1225 * media type. Two consequences the loop above cannot express: a status
1226 * declaring two content types must not carry two copies of its headers,
1227 * and a status with headers but no JSON body - 204, or a 3xx carrying
1228 * Location - compiles no row above at all yet still has headers to check.
1229 *
1230 * A Header Object is a Parameter Object without `name`/`in`, so each one
1231 * fills an oa_param and oa_check_param applies unchanged. Compiled with
1232 * coercion on, as parameters are: a header value arrives as text. */
1233 {
1234 7575 int hcap = 0, hn = 0;
1235 7575 oa_rhdr *hrows = NULL;
1236 7575 hv_iterinit(rh);
1237 16454 ▶ while ((she = hv_iternext(rh))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((she = hv_iternext(rh))) { 8879 7575 16454
1238 8879 I32 skl; const char *sk = hv_iterkey(she, &skl);
1239 8879 HV *robj = oa_hv_of(hv_iterval(rh, she));
1240 8879 ▶ HV *hdrs = robj ? oa_hv_of(oa_get(aTHX_ robj, "headers")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *hdrs = robj ? oa_hv_of(oa_get(aTHX_ robj, "headers")) : NULL; 8879 0 8879
1241 HE *hhe;
1242 8879 ▶ if (!hdrs) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!hdrs) continue; 8865 14 8879
1243 14 hv_iterinit(hdrs);
1244 28 ▶ while ((hhe = hv_iternext(hdrs))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((hhe = hv_iternext(hdrs))) { 14 14 28
1245 14 I32 hkl; const char *hk = hv_iterkey(hhe, &hkl);
1246 14 HV *hobj = oa_hv_of(hv_iterval(hdrs, hhe));
1247 SV *hs, *hr;
1248 14 ▶ if (!hobj) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!hobj) continue; 0 14 14
1249 14 hs = oa_get(aTHX_ hobj, "schema");
1250 14 hr = oa_get(aTHX_ hobj, "required");
1251 14 ▶ if (hn == hcap) {
Branch: 1 branch
BranchTrueFalseTotal
if (hn == hcap) { 14 0 14
1252 14 ▶ hcap = hcap ? hcap * 2 : 4;
Branch: 1 branch
BranchTrueFalseTotal
hcap = hcap ? hcap * 2 : 4; 0 14 14
1253 14 hrows = (oa_rhdr *)realloc(hrows,
1254 14 (size_t)hcap * sizeof(oa_rhdr));
1255 14 ▶ if (!hrows) croak("Open::API: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!hrows) croak("Open::API: out of memory"); 0 14 14
1256 }
1257 14 hrows[hn].status = oa_keep(aTHX_ a, newSVpvn(sk, (STRLEN)skl));
1258 14 hrows[hn].p.name = oa_keep(aTHX_ a, newSVpvn(hk, (STRLEN)hkl));
1259 14 ▶ hrows[hn].p.required = (hr && oa_sv_truthy(aTHX_ hr)) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
hrows[hn].p.required = (hr && oa_sv_truthy(aTHX_ hr)) ? 1 : 0; 10 4 14
hrows[hn].p.required = (hr && oa_sv_truthy(aTHX_ hr)) ? 1 : 0; 10 0 10
1260 14 hrows[hn].p.ctype = NULL;
1261 14 hrows[hn].p.is_array = 0;
1262 /* these rows come from realloc, NOT calloc: every field has
1263 * to be assigned or it reads uninitialised memory */
1264 14 hrows[hn].p.is_object = 0;
1265 14 hrows[hn].p.props = NULL;
1266 14 hrows[hn].p.style = OA_ST_SIMPLE;
1267 14 hrows[hn].p.explode = 0;
1268 14 hrows[hn].p.deprecated = 0;
1269 14 hrows[hn].p.allow_empty = 0;
1270 14 hrows[hn].p.allow_reserved = 0;
1271 14 hrows[hn].p.handle = hs
1272 14 ▶ ? oa_compile_schema(aTHX_ a, hs, 1, OA_DIR_RESP) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? oa_compile_schema(aTHX_ a, hs, 1, OA_DIR_RESP) : NULL; 14 0 14
1273 14 hn++;
1274 }
1275 }
1276 7575 o->rhdrs = hrows;
1277 7575 o->nrhdrs = hn;
1278 }
1279 }
1280  
1281 /* ---- security: schemes + requirements --------------------------------------- */
1282  
1283 /* components.securitySchemes -> the schemes table. Unsupported types croak
1284 * only when a requirement actually references them (checked at lookup). */
1285 3614 static void oa_compile_schemes(pTHX_ oa_api *a, oa_ops *t) {
1286 3614 HV *doc = (HV *)SvRV(a->spec);
1287 3614 HV *comp = oa_hv_of(oa_get(aTHX_ doc, "components"));
1288 3614 ▶ HV *ss = comp ? oa_hv_of(oa_get(aTHX_ comp, "securitySchemes")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *ss = comp ? oa_hv_of(oa_get(aTHX_ comp, "securitySchemes")) : NULL; 2335 1279 3614
1289 HE *he;
1290 3614 int i = 0, n;
1291 3614 ▶ if (!ss) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!ss) return; 3593 21 3614
1292 21 ▶ n = (int)HvUSEDKEYS(ss);
Branch: 1 branch
BranchTrueFalseTotal
n = (int)HvUSEDKEYS(ss); 0 21 21
1293 21 ▶ if (!n) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return; 0 21 21
1294 21 t->schemes = (oa_scheme *)calloc((size_t)n, sizeof(oa_scheme));
1295 21 ▶ if (!t->schemes) croak("Open::API: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!t->schemes) croak("Open::API: out of memory"); 0 21 21
1296 21 hv_iterinit(ss);
1297 41 ▶ while ((he = hv_iternext(ss))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(ss))) { 24 17 41
1298 24 I32 kl; const char *k = hv_iterkey(he, &kl);
1299 24 HV *sh = oa_hv_of(hv_iterval(ss, he));
1300 24 ▶ SV *tv = sh ? oa_get(aTHX_ sh, "type") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *tv = sh ? oa_get(aTHX_ sh, "type") : NULL; 24 0 24
1301 24 oa_scheme *s = &t->schemes[i];
1302 STRLEN tl; const char *tp;
1303 24 ▶ if (!tv) croak("Open::API: securityScheme '%.*s' has no type", (int)kl, k);
Branch: 1 branch
BranchTrueFalseTotal
if (!tv) croak("Open::API: securityScheme '%.*s' has no type", (int)kl, k); 0 24 24
1304 24 tp = SvPV_const(tv, tl);
1305 24 s->name = oa_keep(aTHX_ a, newSVpvn(k, (STRLEN)kl));
1306 24 s->loc = -1;
1307 24 ▶ if (tl == 6 && memEQ(tp, "apiKey", 6)) {
Branch: 2 branches
BranchTrueFalseTotal
if (tl == 6 && memEQ(tp, "apiKey", 6)) { 18 6 24
if (tl == 6 && memEQ(tp, "apiKey", 6)) { 13 5 18
1308 13 SV *nm = oa_get(aTHX_ sh, "name");
1309 13 int loc = oa_param_loc(aTHX_ oa_get(aTHX_ sh, "in"));
1310 13 ▶ if (!nm || loc < 0 || loc == OA_IN_PATH)
Branch: 3 branches
BranchTrueFalseTotal
if (!nm || loc < 0 || loc == OA_IN_PATH) 13 0 13
if (!nm || loc < 0 || loc == OA_IN_PATH) 13 0 13
if (!nm || loc < 0 || loc == OA_IN_PATH) 0 13 13
1311 0 croak("Open::API: apiKey scheme '%.*s' needs name and "
1312 "in (query/header/cookie)", (int)kl, k);
1313 13 s->type = OA_SEC_APIKEY;
1314 13 s->loc = loc;
1315 13 s->pname = oa_keep(aTHX_ a, newSVsv(nm));
1316 14 ▶ } else if (tl == 4 && memEQ(tp, "http", 4)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (tl == 4 && memEQ(tp, "http", 4)) { 3 8 11
} else if (tl == 4 && memEQ(tp, "http", 4)) { 3 0 3
1317 3 SV *sch = oa_get(aTHX_ sh, "scheme");
1318 6 ▶ STRLEN sl; const char *sp = sch ? SvPV_const(sch, sl) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN sl; const char *sp = sch ? SvPV_const(sch, sl) : NULL; 3 0 3
1319 3 ▶ if (sp && sl == 6 && (memEQ(sp, "bearer", 6) || memEQ(sp, "Bearer", 6)))
Branch: 4 branches
BranchTrueFalseTotal
if (sp && sl == 6 && (memEQ(sp, "bearer", 6) || memEQ(sp, "Bearer", 6))) 3 0 3
if (sp && sl == 6 && (memEQ(sp, "bearer", 6) || memEQ(sp, "Bearer", 6))) 2 1 3
if (sp && sl == 6 && (memEQ(sp, "bearer", 6) || memEQ(sp, "Bearer", 6))) 1 1 2
if (sp && sl == 6 && (memEQ(sp, "bearer", 6) || memEQ(sp, "Bearer", 6))) 0 1 1
1320 1 s->type = OA_SEC_BEARER;
1321 2 ▶ else if (sp && sl == 5 && (memEQ(sp, "basic", 5) || memEQ(sp, "Basic", 5)))
Branch: 4 branches
BranchTrueFalseTotal
else if (sp && sl == 5 && (memEQ(sp, "basic", 5) || memEQ(sp, "Basic", 5))) 2 0 2
else if (sp && sl == 5 && (memEQ(sp, "basic", 5) || memEQ(sp, "Basic", 5))) 1 1 2
else if (sp && sl == 5 && (memEQ(sp, "basic", 5) || memEQ(sp, "Basic", 5))) 0 1 1
else if (sp && sl == 5 && (memEQ(sp, "basic", 5) || memEQ(sp, "Basic", 5))) 0 0 0
1322 1 s->type = OA_SEC_BASIC;
1323 else
1324 1 s->type = OA_SEC_OTHER; /* digest and friends: the checker's */
1325 8 ▶ } else if ((tl == 6 && memEQ(tp, "oauth2", 6))
Branch: 2 branches
BranchTrueFalseTotal
} else if ((tl == 6 && memEQ(tp, "oauth2", 6)) 5 3 8
} else if ((tl == 6 && memEQ(tp, "oauth2", 6)) 0 5 5
1326 3 ▶ || (tl == 13 && memEQ(tp, "openIdConnect", 13))) {
Branch: 2 branches
BranchTrueFalseTotal
|| (tl == 13 && memEQ(tp, "openIdConnect", 13))) { 2 1 3
|| (tl == 13 && memEQ(tp, "openIdConnect", 13))) { 2 0 2
1327 7 s->type = OA_SEC_BEARER; /* the credential is the access token */
1328 7 s->oauthish = 1; /* only these two may carry scopes */
1329 /* Flow automation is out of scope, but the DECLARATION still has
1330 * required fields, and a document that omits them is not a valid
1331 * document. Accepting it means trusting a description nobody
1332 * checked. */
1333 7 ▶ if (tl == 13) {
Branch: 1 branch
BranchTrueFalseTotal
if (tl == 13) { 2 5 7
1334 2 ▶ if (!oa_get(aTHX_ sh, "openIdConnectUrl"))
Branch: 1 branch
BranchTrueFalseTotal
if (!oa_get(aTHX_ sh, "openIdConnectUrl")) 1 1 2
1335 1 croak("Open::API: openIdConnect scheme '%.*s' has no "
1336 "openIdConnectUrl", (int)kl, k);
1337 } else {
1338 5 HV *flows = oa_hv_of(oa_get(aTHX_ sh, "flows"));
1339 HE *fhe;
1340 5 ▶ if (!flows)
Branch: 1 branch
BranchTrueFalseTotal
if (!flows) 1 4 5
1341 1 croak("Open::API: oauth2 scheme '%.*s' has no flows",
1342 (int)kl, k);
1343 4 hv_iterinit(flows);
1344 5 ▶ while ((fhe = hv_iternext(flows))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((fhe = hv_iternext(flows))) { 3 2 5
1345 3 I32 fkl; const char *fk = hv_iterkey(fhe, &fkl);
1346 3 HV *fl = oa_hv_of(hv_iterval(flows, fhe));
1347 0 ▶ int wants_auth = (fkl == 8 && memEQ(fk, "implicit", 8))
Branch: 1 branch
BranchTrueFalseTotal
int wants_auth = (fkl == 8 && memEQ(fk, "implicit", 8)) 0 0 0
1348 3 ▶ || (fkl == 17 && memEQ(fk, "authorizationCode", 17));
Branch: 3 branches
BranchTrueFalseTotal
|| (fkl == 17 && memEQ(fk, "authorizationCode", 17)); 0 3 3
|| (fkl == 17 && memEQ(fk, "authorizationCode", 17)); 3 0 3
|| (fkl == 17 && memEQ(fk, "authorizationCode", 17)); 1 2 3
1349 0 ▶ int wants_token = (fkl == 8 && memEQ(fk, "password", 8))
Branch: 1 branch
BranchTrueFalseTotal
int wants_token = (fkl == 8 && memEQ(fk, "password", 8)) 0 0 0
1350 3 ▶ || (fkl == 17 && memEQ(fk, "clientCredentials", 17))
Branch: 2 branches
BranchTrueFalseTotal
|| (fkl == 17 && memEQ(fk, "clientCredentials", 17)) 3 0 3
|| (fkl == 17 && memEQ(fk, "clientCredentials", 17)) 1 2 3
1351 6 ▶ || (fkl == 17 && memEQ(fk, "authorizationCode", 17));
Branch: 3 branches
BranchTrueFalseTotal
|| (fkl == 17 && memEQ(fk, "authorizationCode", 17)); 0 3 3
|| (fkl == 17 && memEQ(fk, "authorizationCode", 17)); 1 0 1
|| (fkl == 17 && memEQ(fk, "authorizationCode", 17)); 1 0 1
1352 3 ▶ if (!fl)
Branch: 1 branch
BranchTrueFalseTotal
if (!fl) 0 3 3
1353 0 croak("Open::API: oauth2 scheme '%.*s': flow '%.*s' is "
1354 "not an object", (int)kl, k, (int)fkl, fk);
1355 3 ▶ if (!oa_get(aTHX_ fl, "scopes"))
Branch: 1 branch
BranchTrueFalseTotal
if (!oa_get(aTHX_ fl, "scopes")) 1 2 3
1356 1 croak("Open::API: oauth2 scheme '%.*s': flow '%.*s' has "
1357 "no scopes", (int)kl, k, (int)fkl, fk);
1358 2 ▶ if (wants_auth && !oa_get(aTHX_ fl, "authorizationUrl"))
Branch: 2 branches
BranchTrueFalseTotal
if (wants_auth && !oa_get(aTHX_ fl, "authorizationUrl")) 1 1 2
if (wants_auth && !oa_get(aTHX_ fl, "authorizationUrl")) 0 1 1
1359 0 croak("Open::API: oauth2 scheme '%.*s': flow '%.*s' has "
1360 "no authorizationUrl", (int)kl, k, (int)fkl, fk);
1361 2 ▶ if (wants_token && !oa_get(aTHX_ fl, "tokenUrl"))
Branch: 2 branches
BranchTrueFalseTotal
if (wants_token && !oa_get(aTHX_ fl, "tokenUrl")) 2 0 2
if (wants_token && !oa_get(aTHX_ fl, "tokenUrl")) 1 1 2
1362 1 croak("Open::API: oauth2 scheme '%.*s': flow '%.*s' has "
1363 "no tokenUrl", (int)kl, k, (int)fkl, fk);
1364 }
1365 }
1366 1 ▶ } else if (tl == 9 && memEQ(tp, "mutualTLS", 9)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (tl == 9 && memEQ(tp, "mutualTLS", 9)) { 1 0 1
} else if (tl == 9 && memEQ(tp, "mutualTLS", 9)) { 1 0 1
1367 1 s->type = OA_SEC_OTHER; /* settled by the transport */
1368 } else {
1369 0 croak("Open::API: securityScheme '%.*s': unsupported type '%.*s'",
1370 (int)kl, k, (int)tl, tp);
1371 }
1372 20 i++;
1373 }
1374 17 t->nschemes = i;
1375 }
1376  
1377 18 static int oa_scheme_index(pTHX_ oa_ops *t, SV *name) {
1378 int i;
1379 21 ▶ for (i = 0; i < t->nschemes; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->nschemes; i++) 20 1 21
1380 20 ▶ if (sv_eq(t->schemes[i].name, name)) return i;
Branch: 1 branch
BranchTrueFalseTotal
if (sv_eq(t->schemes[i].name, name)) return i; 17 3 20
1381 1 return -1;
1382 }
1383  
1384 /* a `security` AV -> malloc'd alternative list (each element an object whose
1385 * keys are ANDed schemes; an empty object means "no auth is acceptable") */
1386 16 static oa_secalt *oa_compile_security(pTHX_ oa_api *a, oa_ops *t, AV *sec,
1387 int *nout, const char *where) {
1388 16 SSize_t n = av_len(sec) + 1, i;
1389 oa_secalt *alts;
1390 16 *nout = 0;
1391 16 ▶ if (n <= 0) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) return NULL; 0 16 16
1392 16 alts = (oa_secalt *)calloc((size_t)n, sizeof(oa_secalt));
1393 16 ▶ if (!alts) croak("Open::API: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!alts) croak("Open::API: out of memory"); 0 16 16
1394 32 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 18 14 32
1395 18 SV **e = av_fetch(sec, i, 0);
1396 18 ▶ HV *req = e ? oa_hv_of(*e) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *req = e ? oa_hv_of(*e) : NULL; 18 0 18
1397 HE *he;
1398 18 int m, j = 0;
1399 18 ▶ if (!req) croak("Open::API: %s: security entries must be objects", where);
Branch: 1 branch
BranchTrueFalseTotal
if (!req) croak("Open::API: %s: security entries must be objects", where); 0 18 18
1400 18 ▶ m = (int)HvUSEDKEYS(req);
Branch: 1 branch
BranchTrueFalseTotal
m = (int)HvUSEDKEYS(req); 0 18 18
1401 18 alts[i].n = m;
1402 17 alts[i].items = m ? (oa_secitem *)calloc((size_t)m, sizeof(oa_secitem))
1403 18 ▶ : NULL;
Branch: 1 branch
BranchTrueFalseTotal
: NULL; 17 1 18
1404 18 ▶ if (m && !alts[i].items) croak("Open::API: out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (m && !alts[i].items) croak("Open::API: out of memory"); 17 1 18
if (m && !alts[i].items) croak("Open::API: out of memory"); 0 17 17
1405 18 hv_iterinit(req);
1406 34 ▶ while ((he = hv_iternext(req))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(req))) { 18 16 34
1407 18 I32 kl; const char *k = hv_iterkey(he, &kl);
1408 18 SV *nm = sv_2mortal(newSVpvn(k, (STRLEN)kl));
1409 18 SV *scopes = hv_iterval(req, he);
1410 18 int idx = oa_scheme_index(aTHX_ t, nm);
1411 18 ▶ if (idx < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (idx < 0) 1 17 18
1412 1 croak("Open::API: %s references unknown securityScheme '%.*s'",
1413 where, (int)kl, k);
1414 17 alts[i].items[j].scheme = idx;
1415 /* Only oauth2 and openIdConnect may name scopes. Under 3.0 the
1416 * specification says the list for any other scheme MUST be empty,
1417 * and a document asking for scopes on an apiKey is describing
1418 * something that cannot happen. 3.1 relaxed that: the array MAY
1419 * carry role names, which are not defined or exchanged in-band, so
1420 * under 3.1 the list is passed through to the caller untouched. */
1421 {
1422 17 AV *sc = oa_av_of(scopes);
1423 17 ▶ if (t->v30 && sc && av_len(sc) >= 0 && !t->schemes[idx].oauthish)
Branch: 4 branches
BranchTrueFalseTotal
if (t->v30 && sc && av_len(sc) >= 0 && !t->schemes[idx].oauthish) 1 16 17
if (t->v30 && sc && av_len(sc) >= 0 && !t->schemes[idx].oauthish) 1 0 1
if (t->v30 && sc && av_len(sc) >= 0 && !t->schemes[idx].oauthish) 1 0 1
if (t->v30 && sc && av_len(sc) >= 0 && !t->schemes[idx].oauthish) 1 0 1
1424 /* "named" matters: a scheme's NAME is free, so one called
1425 * "apiKey" can be declared `type: http`, and a message
1426 * saying only "scheme 'apiKey'" reads like the type */
1427 1 croak("Open::API: %s: the security scheme NAMED '%.*s' is "
1428 "declared as neither oauth2 nor openIdConnect, so "
1429 "the scopes listed for it here must be an empty list",
1430 where, (int)kl, k);
1431 }
1432 16 alts[i].items[j].scopes = oa_av_of(scopes)
1433 16 ▶ ? oa_keep(aTHX_ a, newSVsv(scopes)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? oa_keep(aTHX_ a, newSVsv(scopes)) : NULL; 16 0 16
1434 16 j++;
1435 }
1436 }
1437 14 *nout = (int)n;
1438 14 return alts;
1439 }
1440  
1441 /* ---- the walk --------------------------------------------------------------- */
1442  
1443 static const char *oa_methods[] = {
1444 "get", "put", "post", "delete", "options", "head", "patch", "trace", NULL
1445 };
1446  
1447 /* The path prefix the first Server Object's URL carries, with {variables}
1448 * expanded from their declared defaults: `https://h.test/api/v1` yields
1449 * `/api/v1`, and a URL with no path (or a bare `/`) yields nothing to strip.
1450 * A relative URL is all path. Called only when the caller opted in. */
1451 /* The prefix a `servers` array's first entry carries, as a kept SV, or NULL.
1452 * Shared by the document level and by a path item or operation declaring its
1453 * own - the specification lets any of the three, most specific winning. */
1454 2 static SV *oa_server_prefix_of(pTHX_ oa_api *a, AV *srv) {
1455 2 ▶ SV **e = srv ? av_fetch(srv, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **e = srv ? av_fetch(srv, 0, 0) : NULL; 2 0 2
1456 2 ▶ HV *s0 = (e && *e) ? oa_hv_of(*e) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *s0 = (e && *e) ? oa_hv_of(*e) : NULL; 2 0 2
HV *s0 = (e && *e) ? oa_hv_of(*e) : NULL; 2 0 2
1457 2 ▶ SV *url = s0 ? oa_get(aTHX_ s0, "url") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *url = s0 ? oa_get(aTHX_ s0, "url") : NULL; 2 0 2
1458 2 ▶ HV *vars = s0 ? oa_hv_of(oa_get(aTHX_ s0, "variables")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *vars = s0 ? oa_hv_of(oa_get(aTHX_ s0, "variables")) : NULL; 2 0 2
1459 2 STRLEN ul, i, pathstart = 0;
1460 const char *up;
1461 SV *out;
1462 2 ▶ if (!url) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!url) return NULL; 0 2 2
1463 2 up = SvPV_const(url, ul);
1464 12 ▶ for (i = 0; i + 2 < ul; i++) { /* past scheme://host */
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 2 < ul; i++) { /* past scheme://host */ 12 0 12
1465 12 ▶ if (up[i] == ':' && up[i + 1] == '/' && up[i + 2] == '/') {
Branch: 3 branches
BranchTrueFalseTotal
if (up[i] == ':' && up[i + 1] == '/' && up[i + 2] == '/') { 2 10 12
if (up[i] == ':' && up[i + 1] == '/' && up[i + 2] == '/') { 2 0 2
if (up[i] == ':' && up[i + 1] == '/' && up[i + 2] == '/') { 2 0 2
1466 2 STRLEN j = i + 3;
1467 14 ▶ while (j < ul && up[j] != '/') j++;
Branch: 2 branches
BranchTrueFalseTotal
while (j < ul && up[j] != '/') j++; 14 0 14
while (j < ul && up[j] != '/') j++; 12 2 14
1468 2 pathstart = j;
1469 2 break;
1470 }
1471 }
1472 2 out = sv_2mortal(newSVpvs(""));
1473 10 ▶ for (i = pathstart; i < ul; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = pathstart; i < ul; i++) { 8 2 10
1474 8 ▶ if (up[i] == '{') {
Branch: 1 branch
BranchTrueFalseTotal
if (up[i] == '{') { 0 8 8
1475 0 STRLEN j = i + 1;
1476 0 ▶ while (j < ul && up[j] != '}') j++;
Branch: 2 branches
BranchTrueFalseTotal
while (j < ul && up[j] != '}') j++; 0 0 0
while (j < ul && up[j] != '}') j++; 0 0 0
1477 0 ▶ if (j < ul) {
Branch: 1 branch
BranchTrueFalseTotal
if (j < ul) { 0 0 0
1478 0 SV **dv = vars ? hv_fetch(vars, up + i + 1,
1479 0 ▶ (I32)(j - i - 1), 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
(I32)(j - i - 1), 0) : NULL; 0 0 0
1480 0 ▶ HV *vh = (dv && *dv) ? oa_hv_of(*dv) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *vh = (dv && *dv) ? oa_hv_of(*dv) : NULL; 0 0 0
HV *vh = (dv && *dv) ? oa_hv_of(*dv) : NULL; 0 0 0
1481 0 ▶ SV *def = vh ? oa_get(aTHX_ vh, "default") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *def = vh ? oa_get(aTHX_ vh, "default") : NULL; 0 0 0
1482 0 ▶ if (def) sv_catsv(out, def);
Branch: 1 branch
BranchTrueFalseTotal
if (def) sv_catsv(out, def); 0 0 0
1483 0 i = j;
1484 0 continue;
1485 }
1486 }
1487 8 sv_catpvn(out, up + i, 1);
1488 }
1489 2 ▶ while (SvCUR(out) && SvPVX(out)[SvCUR(out) - 1] == '/')
Branch: 2 branches
BranchTrueFalseTotal
while (SvCUR(out) && SvPVX(out)[SvCUR(out) - 1] == '/') 2 0 2
while (SvCUR(out) && SvPVX(out)[SvCUR(out) - 1] == '/') 0 2 2
1490 0 SvCUR_set(out, SvCUR(out) - 1); /* a trailing / is not prefix */
1491 2 ▶ return SvCUR(out) ? oa_keep(aTHX_ a, newSVsv(out)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return SvCUR(out) ? oa_keep(aTHX_ a, newSVsv(out)) : NULL; 2 0 2
1492 }
1493  
1494 6 static void oa_set_server_prefix(pTHX_ oa_api *a) {
1495 6 HV *doc = oa_hv_of(a->spec);
1496 6 ▶ AV *srv = doc ? oa_av_of(oa_get(aTHX_ doc, "servers")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
AV *srv = doc ? oa_av_of(oa_get(aTHX_ doc, "servers")) : NULL; 6 0 6
1497 6 ▶ SV **e = srv ? av_fetch(srv, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **e = srv ? av_fetch(srv, 0, 0) : NULL; 4 2 6
1498 6 ▶ HV *s0 = (e && *e) ? oa_hv_of(*e) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *s0 = (e && *e) ? oa_hv_of(*e) : NULL; 4 2 6
HV *s0 = (e && *e) ? oa_hv_of(*e) : NULL; 4 0 4
1499 6 ▶ SV *url = s0 ? oa_get(aTHX_ s0, "url") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *url = s0 ? oa_get(aTHX_ s0, "url") : NULL; 4 2 6
1500 6 ▶ HV *vars = s0 ? oa_hv_of(oa_get(aTHX_ s0, "variables")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *vars = s0 ? oa_hv_of(oa_get(aTHX_ s0, "variables")) : NULL; 4 2 6
1501 6 STRLEN ul, i, pathstart = 0;
1502 const char *up;
1503 SV *out;
1504 6 ▶ if (!url) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!url) return; 2 4 6
1505 4 up = SvPV_const(url, ul);
1506  
1507 /* past scheme://host, when there is one */
1508 24 ▶ for (i = 0; i + 2 < ul; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 2 < ul; i++) { 24 0 24
1509 24 ▶ if (up[i] == ':' && up[i + 1] == '/' && up[i + 2] == '/') {
Branch: 3 branches
BranchTrueFalseTotal
if (up[i] == ':' && up[i + 1] == '/' && up[i + 2] == '/') { 4 20 24
if (up[i] == ':' && up[i + 1] == '/' && up[i + 2] == '/') { 4 0 4
if (up[i] == ':' && up[i + 1] == '/' && up[i + 2] == '/') { 4 0 4
1510 4 STRLEN j = i + 3;
1511 28 ▶ while (j < ul && up[j] != '/') j++;
Branch: 2 branches
BranchTrueFalseTotal
while (j < ul && up[j] != '/') j++; 28 0 28
while (j < ul && up[j] != '/') j++; 24 4 28
1512 4 pathstart = j;
1513 4 break;
1514 }
1515 }
1516  
1517 4 out = sv_2mortal(newSVpvs(""));
1518 24 ▶ for (i = pathstart; i < ul; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = pathstart; i < ul; i++) { 20 4 24
1519 20 ▶ if (up[i] == '{') {
Branch: 1 branch
BranchTrueFalseTotal
if (up[i] == '{') { 0 20 20
1520 0 STRLEN j = i + 1;
1521 0 ▶ while (j < ul && up[j] != '}') j++;
Branch: 2 branches
BranchTrueFalseTotal
while (j < ul && up[j] != '}') j++; 0 0 0
while (j < ul && up[j] != '}') j++; 0 0 0
1522 0 ▶ if (j < ul) {
Branch: 1 branch
BranchTrueFalseTotal
if (j < ul) { 0 0 0
1523 0 SV **dv = vars ? hv_fetch(vars, up + i + 1,
1524 0 ▶ (I32)(j - i - 1), 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
(I32)(j - i - 1), 0) : NULL; 0 0 0
1525 0 ▶ HV *vh = (dv && *dv) ? oa_hv_of(*dv) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *vh = (dv && *dv) ? oa_hv_of(*dv) : NULL; 0 0 0
HV *vh = (dv && *dv) ? oa_hv_of(*dv) : NULL; 0 0 0
1526 0 ▶ SV *def = vh ? oa_get(aTHX_ vh, "default") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *def = vh ? oa_get(aTHX_ vh, "default") : NULL; 0 0 0
1527 0 ▶ if (def) sv_catsv(out, def);
Branch: 1 branch
BranchTrueFalseTotal
if (def) sv_catsv(out, def); 0 0 0
1528 0 i = j;
1529 0 continue;
1530 }
1531 }
1532 20 sv_catpvn(out, up + i, 1);
1533 }
1534 4 ▶ while (SvCUR(out) && SvPVX(out)[SvCUR(out) - 1] == '/')
Branch: 2 branches
BranchTrueFalseTotal
while (SvCUR(out) && SvPVX(out)[SvCUR(out) - 1] == '/') 4 0 4
while (SvCUR(out) && SvPVX(out)[SvCUR(out) - 1] == '/') 0 4 4
1535 0 SvCUR_set(out, SvCUR(out) - 1); /* a trailing / is not prefix */
1536 4 ▶ if (SvCUR(out)) a->prefix = oa_keep(aTHX_ a, newSVsv(out));
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(out)) a->prefix = oa_keep(aTHX_ a, newSVsv(out)); 4 0 4
1537 }
1538  
1539 /* The order the router walks the operation table in.
1540 *
1541 * Without one, the table is in `paths` hash order, which perl randomises per
1542 * process - so a request that two templates both fit routed to a DIFFERENT
1543 * operation between runs of the same document. `/t/fixed` against `/t/fixed`
1544 * and `/t/{id}` picked the template roughly one run in five.
1545 *
1546 * Ordering rule, applied left to right: at the first segment where two
1547 * templates differ in kind, the one with a LITERAL there sorts first, so a
1548 * concrete path is always preferred over a template that also fits. Ties are
1549 * broken on the path string, which makes the order total and reproducible
1550 * even between two templates of identical shape.
1551 *
1552 * A separate index, because by_id holds indices into `ops`. Insertion sort:
1553 * a document has tens of routes and this runs once, at compile. */
1554 6675 static int oa_route_before(pTHX_ oa_op *x, oa_op *y) {
1555 6675 int i, n = x->nsegs < y->nsegs ? x->nsegs : y->nsegs;
1556 STRLEN xl, yl;
1557 const char *xp, *yp;
1558 int c;
1559 14700 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 8104 6596 14700
1560 8104 int xlit = x->segs[i].lit ? 1 : 0;
1561 8104 int ylit = y->segs[i].lit ? 1 : 0;
1562 8104 ▶ if (xlit != ylit) return xlit; /* literal first */
Branch: 1 branch
BranchTrueFalseTotal
if (xlit != ylit) return xlit; /* literal first */ 79 8025 8104
1563 }
1564 6596 ▶ if (x->nsegs != y->nsegs) return x->nsegs < y->nsegs;
Branch: 1 branch
BranchTrueFalseTotal
if (x->nsegs != y->nsegs) return x->nsegs < y->nsegs; 3948 2648 6596
1565 /* identical shape: order on the path, then the method, so the result does
1566 * not depend on which the hash happened to hand over first */
1567 5296 ▶ xp = x->path ? SvPV_const(x->path, xl) : (xl = 0, "");
Branch: 1 branch
BranchTrueFalseTotal
xp = x->path ? SvPV_const(x->path, xl) : (xl = 0, ""); 2648 0 2648
1568 5296 ▶ yp = y->path ? SvPV_const(y->path, yl) : (yl = 0, "");
Branch: 1 branch
BranchTrueFalseTotal
yp = y->path ? SvPV_const(y->path, yl) : (yl = 0, ""); 2648 0 2648
1569 2648 c = memcmp(xp, yp, xl < yl ? xl : yl);
1570 2648 ▶ if (c) return c < 0;
Branch: 1 branch
BranchTrueFalseTotal
if (c) return c < 0; 44 2604 2648
1571 2604 ▶ if (xl != yl) return xl < yl;
Branch: 1 branch
BranchTrueFalseTotal
if (xl != yl) return xl < yl; 0 2604 2604
1572 5208 ▶ xp = x->method ? SvPV_const(x->method, xl) : (xl = 0, "");
Branch: 1 branch
BranchTrueFalseTotal
xp = x->method ? SvPV_const(x->method, xl) : (xl = 0, ""); 2604 0 2604
1573 5208 ▶ yp = y->method ? SvPV_const(y->method, yl) : (yl = 0, "");
Branch: 1 branch
BranchTrueFalseTotal
yp = y->method ? SvPV_const(y->method, yl) : (yl = 0, ""); 2604 0 2604
1574 2604 c = memcmp(xp, yp, xl < yl ? xl : yl);
1575 2604 ▶ if (c) return c < 0;
Branch: 1 branch
BranchTrueFalseTotal
if (c) return c < 0; 2604 0 2604
1576 0 return xl <= yl;
1577 }
1578  
1579 3601 static void oa_order_routes(pTHX_ oa_ops *t) {
1580 int i;
1581 3601 free(t->order);
1582 3601 t->order = NULL;
1583 3601 ▶ if (t->n <= 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (t->n <= 0) return; 31 3570 3601
1584 3570 t->order = (int *)malloc((size_t)t->n * sizeof(int));
1585 3570 ▶ if (!t->order) croak("Open::API: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!t->order) croak("Open::API: out of memory"); 0 3570 3570
1586 11144 ▶ for (i = 0; i < t->n; i++) t->order[i] = i;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->n; i++) t->order[i] = i; 7574 3570 11144
1587 7574 ▶ for (i = 1; i < t->n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < t->n; i++) { 4004 3570 7574
1588 4004 int cur = t->order[i];
1589 4004 int j = i - 1;
1590 8000 ▶ while (j >= 0 && oa_route_before(aTHX_ &t->ops[cur], &t->ops[t->order[j]])) {
Branch: 2 branches
BranchTrueFalseTotal
while (j >= 0 && oa_route_before(aTHX_ &t->ops[cur], &t->ops[t->order[j]])) { 6675 1325 8000
while (j >= 0 && oa_route_before(aTHX_ &t->ops[cur], &t->ops[t->order[j]])) { 3996 2679 6675
1591 3996 t->order[j + 1] = t->order[j];
1592 3996 j--;
1593 }
1594 4004 t->order[j + 1] = cur;
1595 }
1596 }
1597  
1598 3614 static void oa_compile(pTHX_ oa_api *a) {
1599 3614 HV *doc = (HV *)SvRV(a->spec);
1600 3614 HV *paths = oa_hv_of(oa_get(aTHX_ doc, "paths"));
1601 oa_ops *t;
1602 HE *phe;
1603  
1604 3614 oa_build_defs(aTHX_ a); /* shared $defs from components.schemas */
1605  
1606 3614 t = (oa_ops *)calloc(1, sizeof(oa_ops));
1607 3614 ▶ if (!t) croak("Open::API: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!t) croak("Open::API: out of memory"); 0 3614 3614
1608 3614 t->by_id = newHV();
1609 3614 a->ops = t;
1610  
1611 /* The normaliser up-converts a 3.0 document's schemas but leaves its
1612 * `openapi` string alone, so the version is still readable here - and the
1613 * security requirement rules differ between 3.0 and 3.1. The string has
1614 * already been checked to be 3.0[.x] or 3.1[.x]. */
1615 {
1616 3614 SV *ov = oa_get(aTHX_ doc, "openapi");
1617 STRLEN ol;
1618 7228 ▶ const char *op = ov ? SvPV_const(ov, ol) : "";
Branch: 1 branch
BranchTrueFalseTotal
const char *op = ov ? SvPV_const(ov, ol) : ""; 3614 0 3614
1619 3614 ▶ t->v30 = (ov && ol >= 3 && op[2] == '0');
Branch: 3 branches
BranchTrueFalseTotal
t->v30 = (ov && ol >= 3 && op[2] == '0'); 3614 0 3614
t->v30 = (ov && ol >= 3 && op[2] == '0'); 3614 0 3614
t->v30 = (ov && ol >= 3 && op[2] == '0'); 921 2693 3614
1620 }
1621  
1622 3614 oa_compile_schemes(aTHX_ a, t);
1623 {
1624 3610 AV *rs = oa_av_of(oa_get(aTHX_ doc, "security"));
1625 3610 ▶ if (rs) t->rootsec = oa_compile_security(aTHX_ a, t, rs,
Branch: 1 branch
BranchTrueFalseTotal
if (rs) t->rootsec = oa_compile_security(aTHX_ a, t, rs, 12 3598 3610
1626 &t->nrootsec, "document security");
1627 }
1628  
1629 3608 ▶ if (!paths) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!paths) return; 0 3608 3608
1630  
1631 /* Two templated paths with the same hierarchy but different variable
1632 * names are IDENTICAL and must not both exist - /t/{a} and /t/{b} are one
1633 * path, and a document declaring both is asking for two routes that can
1634 * never be told apart. Each template is canonicalised by replacing every
1635 * {...} with a single placeholder, and a repeat is refused. */
1636 {
1637 3608 HV *seen = (HV *)sv_2mortal((SV *)newHV());
1638 3608 hv_iterinit(paths);
1639 8585 ▶ while ((phe = hv_iternext(paths))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((phe = hv_iternext(paths))) { 4979 3606 8585
1640 4979 I32 tkl; const char *tpl = hv_iterkey(phe, &tkl);
1641 4979 SV *canon = sv_2mortal(newSVpvs(""));
1642 STRLEN q;
1643 /* a path field name MUST begin with a forward slash: it is
1644 * appended to a server URL, and one that does not would silently
1645 * join onto the host instead */
1646 4979 ▶ if (tkl < 1 || tpl[0] != '/')
Branch: 2 branches
BranchTrueFalseTotal
if (tkl < 1 || tpl[0] != '/') 4979 0 4979
if (tkl < 1 || tpl[0] != '/') 1 4978 4979
1647 1 croak("Open::API: path '%.*s' does not begin with '/'",
1648 (int)tkl, tpl);
1649 26336 ▶ for (q = 0; q < (STRLEN)tkl; q++) {
Branch: 1 branch
BranchTrueFalseTotal
for (q = 0; q < (STRLEN)tkl; q++) { 21358 4978 26336
1650 21358 ▶ if (tpl[q] == '{') {
Branch: 1 branch
BranchTrueFalseTotal
if (tpl[q] == '{') { 1422 19936 21358
1651 9727 ▶ while (q < (STRLEN)tkl && tpl[q] != '}') q++;
Branch: 2 branches
BranchTrueFalseTotal
while (q < (STRLEN)tkl && tpl[q] != '}') q++; 9727 0 9727
while (q < (STRLEN)tkl && tpl[q] != '}') q++; 8305 1422 9727
1652 1422 sv_catpvs(canon, "\1"); /* one placeholder */
1653 1422 continue;
1654 }
1655 19936 sv_catpvn(canon, tpl + q, 1);
1656 }
1657 {
1658 4978 STRLEN cl; const char *cp = SvPV_const(canon, cl);
1659 4978 ▶ if (hv_exists(seen, cp, (I32)cl))
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(seen, cp, (I32)cl)) 1 4977 4978
1660 1 croak("Open::API: paths '%.*s' duplicates another path "
1661 "that differs only in its template variable names",
1662 (int)tkl, tpl);
1663 4977 (void)hv_store(seen, cp, (I32)cl, newSViv(1), 0);
1664 }
1665 }
1666 }
1667  
1668 3606 hv_iterinit(paths);
1669 8577 ▶ while ((phe = hv_iternext(paths))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((phe = hv_iternext(paths))) { 4976 3601 8577
1670 4976 I32 tkl; const char *tpl = hv_iterkey(phe, &tkl);
1671 4976 HV *item = oa_hv_of(hv_iterval(paths, phe));
1672 AV *item_params;
1673 int m;
1674 4976 ▶ if (!item) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!item) continue; 0 4976 4976
1675 4976 item_params = oa_av_of(oa_get(aTHX_ item, "parameters"));
1676  
1677 44744 ▶ for (m = 0; oa_methods[m]; m++) {
Branch: 1 branch
BranchTrueFalseTotal
for (m = 0; oa_methods[m]; m++) { 39773 4971 44744
1678 39773 HV *oph = oa_hv_of(oa_get(aTHX_ item, oa_methods[m]));
1679 SV *op_id;
1680 oa_op *o;
1681 39773 ▶ if (!oph) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!oph) continue; 32193 7580 39773
1682 7580 oa_ext_docs_ok(aTHX_ oa_get(aTHX_ oph, "externalDocs"), "operation");
1683 7578 op_id = oa_get(aTHX_ oph, "operationId");
1684 7578 ▶ if (!op_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (!op_id) { 2 7576 7578
1685 /* operationId is OPTIONAL per the specification, so a document
1686 * without one is still valid and must still compile. It is the
1687 * dispatch key here, so one is derived: <method>_<path>, with
1688 * the template braces dropped and separators flattened. Same
1689 * rule as plan_maat_mcp/01, so the MCP tool names and the core
1690 * agree on what an unnamed operation is called. A collision
1691 * with a real operationId is still caught below, as it must
1692 * be - two operations cannot share a dispatch key. */
1693 2 SV *d = sv_2mortal(newSVpv(oa_methods[m], 0));
1694 STRLEN q;
1695 2 int owed = 1; /* a separator is owed before the next char */
1696 6 ▶ for (q = 0; q < (STRLEN)tkl; q++) {
Branch: 1 branch
BranchTrueFalseTotal
for (q = 0; q < (STRLEN)tkl; q++) { 4 2 6
1697 4 char ch = tpl[q];
1698 6 ▶ if (ch == '{' || ch == '}') continue;
Branch: 2 branches
BranchTrueFalseTotal
if (ch == '{' || ch == '}') continue; 4 0 4
if (ch == '{' || ch == '}') continue; 0 4 4
1699 /* a slash OWES a separator rather than emitting one, so a
1700 * leading slash does not double the underscore already
1701 * between method and path, and a trailing one leaves none
1702 * dangling: /a -> get_a, /pets/{petId} -> get_pets_petId */
1703 4 ▶ if (ch == '/') { owed = 1; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '/') { owed = 1; continue; } 2 2 4
1704 2 ▶ if (owed) { sv_catpvs(d, "_"); owed = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (owed) { sv_catpvs(d, "_"); owed = 0; } 2 0 2
1705 2 sv_catpvn(d, &ch, 1);
1706 }
1707 2 op_id = d;
1708 }
1709 {
1710 7578 STRLEN il; const char *ip = SvPV_const(op_id, il);
1711 7578 ▶ if (hv_exists(t->by_id, ip, (I32)il))
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(t->by_id, ip, (I32)il)) 1 7577 7578
1712 1 croak("Open::API: duplicate operationId '%.*s'", (int)il, ip);
1713 7577 ▶ if (t->n == t->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->n == t->cap) { 3573 4004 7577
1714 3573 ▶ t->cap = t->cap ? t->cap * 2 : 8;
Branch: 1 branch
BranchTrueFalseTotal
t->cap = t->cap ? t->cap * 2 : 8; 0 3573 3573
1715 3573 t->ops = (oa_op *)realloc(t->ops,
1716 3573 (size_t)t->cap * sizeof(oa_op));
1717 3573 ▶ if (!t->ops) croak("Open::API: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!t->ops) croak("Open::API: out of memory"); 0 3573 3573
1718 }
1719 7577 o = &t->ops[t->n];
1720 7577 memset(o, 0, sizeof(*o));
1721 7577 o->op_id = oa_keep(aTHX_ a, newSVsv(op_id));
1722 7577 o->method = oa_keep(aTHX_ a, newSVpv(oa_methods[m], 0));
1723 7577 o->path = oa_keep(aTHX_ a, newSVpvn(tpl, (STRLEN)tkl));
1724 7577 (void)hv_store(t->by_id, ip, (I32)il, newSViv(t->n), 0);
1725 7577 t->n++;
1726 }
1727 /* A path item or an operation may declare its own `servers`, and
1728 * the more specific wins. Only consulted when the caller opted
1729 * in, exactly as the document-level prefix is: honouring a prefix
1730 * re-routes an application that already mounts without it. */
1731 7577 ▶ if (a->prefix || a->want_servers) {
Branch: 2 branches
BranchTrueFalseTotal
if (a->prefix || a->want_servers) { 7573 4 7577
if (a->prefix || a->want_servers) { 2 7571 7573
1732 6 AV *osrv = oa_av_of(oa_get(aTHX_ oph, "servers"));
1733 6 AV *psrv = oa_av_of(oa_get(aTHX_ item, "servers"));
1734 6 ▶ if (osrv) o->prefix = oa_server_prefix_of(aTHX_ a, osrv);
Branch: 1 branch
BranchTrueFalseTotal
if (osrv) o->prefix = oa_server_prefix_of(aTHX_ a, osrv); 1 5 6
1735 5 ▶ else if (psrv) o->prefix = oa_server_prefix_of(aTHX_ a, psrv);
Branch: 1 branch
BranchTrueFalseTotal
else if (psrv) o->prefix = oa_server_prefix_of(aTHX_ a, psrv); 1 4 5
1736 }
1737 7577 oa_compile_segs(aTHX_ a, o, tpl, (STRLEN)tkl);
1738 7577 oa_compile_params(aTHX_ a, o, item_params,
1739 oa_av_of(oa_get(aTHX_ oph, "parameters")));
1740 7576 oa_compile_body(aTHX_ a, o, oa_get(aTHX_ oph, "requestBody"));
1741 7576 oa_compile_responses(aTHX_ a, o, oa_get(aTHX_ oph, "responses"));
1742 { /* security: operation-level overrides (an explicit empty
1743 * array disables auth); otherwise inherit the document's */
1744 7575 SV *osec = oa_get(aTHX_ oph, "security");
1745 7575 ▶ if (osec) {
Branch: 1 branch
BranchTrueFalseTotal
if (osec) { 4 7571 7575
1746 4 AV *sa = oa_av_of(osec);
1747 4 ▶ if (!sa) croak("Open::API: %s: security must be an array",
Branch: 1 branch
BranchTrueFalseTotal
if (!sa) croak("Open::API: %s: security must be an array", 0 4 4
1748 SvPV_nolen(o->op_id));
1749 8 o->sec = oa_compile_security(aTHX_ a, t, sa, &o->nsec,
1750 8 SvPV_nolen(o->op_id));
1751 4 o->own_sec = 1;
1752 } else {
1753 7571 o->sec = t->rootsec;
1754 7571 o->nsec = t->nrootsec;
1755 7571 o->own_sec = 0;
1756 }
1757 }
1758 }
1759 }
1760  
1761 /* A Link's operationId names "an existing, resolvable OAS operation".
1762 * Links are carried for consumers rather than compiled, so nothing else
1763 * would ever catch a dangling one.
1764 *
1765 * This runs AFTER the walk above, not inside it: a Link may point at an
1766 * operation declared later in the document, and refusing that would be
1767 * wrong. By here every operationId - declared or derived - is in by_id. */
1768 3601 oa_order_routes(aTHX_ t);
1769 {
1770 HE *lphe;
1771 3601 hv_iterinit(paths);
1772 8569 ▶ while ((lphe = hv_iternext(paths))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((lphe = hv_iternext(paths))) { 4970 3599 8569
1773 4970 HV *item = oa_hv_of(hv_iterval(paths, lphe));
1774 int m;
1775 4970 ▶ if (!item) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!item) continue; 0 4970 4970
1776 44714 ▶ for (m = 0; oa_methods[m]; m++) {
Branch: 1 branch
BranchTrueFalseTotal
for (m = 0; oa_methods[m]; m++) { 39746 4968 44714
1777 39746 HV *oph = oa_hv_of(oa_get(aTHX_ item, oa_methods[m]));
1778 39746 ▶ HV *resps = oph ? oa_hv_of(oa_get(aTHX_ oph, "responses")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *resps = oph ? oa_hv_of(oa_get(aTHX_ oph, "responses")) : NULL; 7574 32172 39746
1779 HE *rhe;
1780 39746 ▶ if (!resps) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!resps) continue; 32172 7574 39746
1781 7574 hv_iterinit(resps);
1782 16451 ▶ while ((rhe = hv_iternext(resps))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((rhe = hv_iternext(resps))) { 8879 7572 16451
1783 8879 HV *robj = oa_hv_of(hv_iterval(resps, rhe));
1784 8879 ▶ HV *links = robj ? oa_hv_of(oa_get(aTHX_ robj, "links")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *links = robj ? oa_hv_of(oa_get(aTHX_ robj, "links")) : NULL; 8879 0 8879
1785 HE *lhe;
1786 8879 ▶ if (!links) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!links) continue; 8874 5 8879
1787 5 hv_iterinit(links);
1788 8 ▶ while ((lhe = hv_iternext(links))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((lhe = hv_iternext(links))) { 5 3 8
1789 5 I32 lkl; const char *lk = hv_iterkey(lhe, &lkl);
1790 5 HV *lo = oa_hv_of(hv_iterval(links, lhe));
1791 5 ▶ SV *oid = lo ? oa_get(aTHX_ lo, "operationId") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *oid = lo ? oa_get(aTHX_ lo, "operationId") : NULL; 5 0 5
1792 5 ▶ SV *oref = lo ? oa_get(aTHX_ lo, "operationRef") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *oref = lo ? oa_get(aTHX_ lo, "operationRef") : NULL; 5 0 5
1793 STRLEN il; const char *ip;
1794 5 ▶ if (oid && oref)
Branch: 2 branches
BranchTrueFalseTotal
if (oid && oref) 5 0 5
if (oid && oref) 1 4 5
1795 1 croak("Open::API: link '%.*s' declares both "
1796 "operationId and operationRef, which are "
1797 "mutually exclusive", (int)lkl, lk);
1798 /* operationRef is the other spelling, and names a
1799 * location rather than an id: not ours to resolve */
1800 4 ▶ if (!oid) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!oid) continue; 0 4 4
1801 4 ip = SvPV_const(oid, il);
1802 4 ▶ if (!hv_exists(t->by_id, ip, (I32)il))
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_exists(t->by_id, ip, (I32)il)) 1 3 4
1803 1 croak("Open::API: link '%.*s' names operationId "
1804 "'%.*s', which no operation declares",
1805 (int)lkl, lk, (int)il, ip);
1806 }
1807 }
1808 }
1809 }
1810 }
1811 }
1812  
1813 /* op by id, or NULL */
1814 5453 static oa_op *oa_op_by_id(pTHX_ oa_api *a, SV *id) {
1815 5453 oa_ops *t = (oa_ops *)a->ops;
1816 STRLEN l; const char *p;
1817 SV **e;
1818 5453 ▶ if (!t) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!t) return NULL; 0 5453 5453
1819 5453 p = SvPV_const(id, l);
1820 5453 e = hv_fetch(t->by_id, p, (I32)l, 0);
1821 5453 ▶ return (e && *e) ? &t->ops[SvIV(*e)] : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return (e && *e) ? &t->ops[SvIV(*e)] : NULL; 5447 6 5453
return (e && *e) ? &t->ops[SvIV(*e)] : NULL; 5447 0 5447
1822 }
1823  
1824 #endif /* OA_COMPILE_H */
« include/oa_plack.h ↑ Summary include/oa_tools.h »