include/oa_compile.h

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