API.xs

filter: Statementstmt 86.8% 779 / 897 Branchbran 57.1% 657 / 1150 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 70.1% 1436 / 2047 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/oa_abi_impl.h ↑ Summary include/oa_plack.h »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #include "oa.h"
7  
8 /* ---- the JSON::Schema::Fast C ABI (required) ----------------------------- *
9 * Resolved once at boot from JSON::Schema::Fast::_abi_ptr and version-checked.
10 * NULL means the installed JSON::Schema::Fast is absent or too old; Open/API.pm
11 * treats that as a hard load-time error. */
12 static const jsf_abi *JSF = NULL;
13  
14 /* ---- the Fetch C ABI (client only; resolved lazily in phase 4) ----------- */
15 static const fetch_abi *OA_FETCH = NULL;
16 static int OA_FETCH_TRIED = 0;
17  
18 /* ---- the File::Raw::JSON C ABI (spec + body JSON decode; required) -------- *
19 * Resolved lazily on first decode from File::Raw::JSON::_abi_ptr. */
20 static const frj_abi *OA_FRJ = NULL;
21 static int OA_FRJ_TRIED = 0;
22 static const frj_abi *oa_frj(pTHX); /* defined below; used by the headers */
23  
24 #include "oa_compile.h" /* needs the JSF table above */
25 #include "oa_swagger2.h" /* Swagger 2.0 -> 3.0 shape, before the 3.0 pass */
26 #include "oa_normalize.h" /* 3.0 -> 3.1 + discriminator (needs oa_get/oa_methods) */
27 #include "oa_tools.h" /* the document's own schema and prose per operation */
28 #include "oa_route.h"
29 #include "oa_validate.h"
30 #include "oa_plack.h"
31 #include "oa_mock.h" /* response synthesis (needs oa_json_encode above) */
32 #include "oa_client.h"
33 #include "oa_abi_impl.h" /* the OA_ABI table (needs route/validate/compile) */
34  
35 /* the PSGI app coderef body: captures
36 * ($api, \%handlers, $vresp, $before, $after, \%security, \%csrf, \%opts) */
37 XS_INTERNAL(oa_app_cb);
38 629 XS_INTERNAL(oa_app_cb) {
39 629 dXSARGS;
40 629 AV *cap = oa_clos_cap(aTHX_ cv);
41 SV *self_sv, *hs, *flags, *before, *after, *sec, *csrf, *opts, *env, *resp;
42 629 ▶ if (!cap || items < 1) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!cap || items < 1) XSRETURN_EMPTY; 629 0 629
if (!cap || items < 1) XSRETURN_EMPTY; 0 629 629
43 629 self_sv = *av_fetch(cap, 0, 0);
44 629 hs = *av_fetch(cap, 1, 0);
45 629 flags = *av_fetch(cap, 2, 0);
46 629 before = *av_fetch(cap, 3, 0);
47 629 after = *av_fetch(cap, 4, 0);
48 629 sec = *av_fetch(cap, 5, 0);
49 629 csrf = *av_fetch(cap, 6, 0);
50 629 opts = *av_fetch(cap, 7, 0);
51 629 env = ST(0);
52 629 ▶ if (!SvROK(env) || SvTYPE(SvRV(env)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(env) || SvTYPE(SvRV(env)) != SVt_PVHV) 629 0 629
if (!SvROK(env) || SvTYPE(SvRV(env)) != SVt_PVHV) 0 629 629
53 0 croak("Open::API app: env must be a hashref");
54 2516 ▶ resp = oa_serve_psgi(aTHX_ self_sv, (HV *)SvRV(hs), env, (int)SvIV(flags),
Branch: 3 branches
BranchTrueFalseTotal
resp = oa_serve_psgi(aTHX_ self_sv, (HV *)SvRV(hs), env, (int)SvIV(flags), 629 0 629
resp = oa_serve_psgi(aTHX_ self_sv, (HV *)SvRV(hs), env, (int)SvIV(flags), 629 0 629
resp = oa_serve_psgi(aTHX_ self_sv, (HV *)SvRV(hs), env, (int)SvIV(flags), 629 0 629
55 before, after,
56 629 ▶ (sec && SvROK(sec)) ? (HV *)SvRV(sec) : NULL,
Branch: 1 branch
BranchTrueFalseTotal
(sec && SvROK(sec)) ? (HV *)SvRV(sec) : NULL, 629 0 629
57 629 ▶ (csrf && SvROK(csrf)) ? (HV *)SvRV(csrf) : NULL,
Branch: 1 branch
BranchTrueFalseTotal
(csrf && SvROK(csrf)) ? (HV *)SvRV(csrf) : NULL, 19 610 629
58 629 ▶ (opts && SvROK(opts)) ? (HV *)SvRV(opts) : NULL);
Branch: 1 branch
BranchTrueFalseTotal
(opts && SvROK(opts)) ? (HV *)SvRV(opts) : NULL); 629 0 629
59 629 ST(0) = sv_2mortal(resp);
60 629 XSRETURN(1);
61 }
62  
63 /* The coderef can() hands back for an operationId: sub { $s->call($op, @_) },
64 * with $op captured. Dispatches straight to oa_cli_call (the ->call C path). */
65 XS_INTERNAL(oa_op_call_cb);
66 0 XS_INTERNAL(oa_op_call_cb) {
67 0 dXSARGS;
68 0 AV *cap = oa_clos_cap(aTHX_ cv);
69 SV *self, *op, *ret;
70 HV *params;
71 int i;
72 0 ▶ if (!cap || items < 1) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!cap || items < 1) XSRETURN_EMPTY; 0 0 0
if (!cap || items < 1) XSRETURN_EMPTY; 0 0 0
73 0 op = *av_fetch(cap, 0, 0); /* captured operationId */
74 0 self = ST(0);
75 0 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) 0 0 0
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) 0 0 0
76 0 croak("Open::API::Client: not a client");
77 0 params = (HV *)sv_2mortal((SV *)newHV());
78 0 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 0 0 0
79 0 STRLEN kl; const char *k = SvPV_const(ST(i), kl);
80 0 (void)hv_store(params, k, (I32)kl, newSVsv(ST(i + 1)), 0);
81 }
82 0 ret = oa_cli_call(aTHX_ (HV *)SvRV(self), op, params);
83 0 ST(0) = sv_2mortal(ret);
84 0 XSRETURN(1);
85 }
86  
87 /* ---- object plumbing ------------------------------------------------------ */
88  
89 14770 static oa_api *oa_api_of(pTHX_ SV *self) {
90 14770 ▶ if (!SvROK(self)) croak("Open::API: not an Open::API object");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(self)) croak("Open::API: not an Open::API object"); 0 14770 14770
91 14770 return (oa_api *)INT2PTR(void *, SvIV(SvRV(self)));
92 }
93  
94 /* ---- spec decoding -------------------------------------------------------- */
95  
96 /* Resolve File::Raw::JSON's ABI table once (a PREREQ). Croak if unavailable. */
97 7941 static const frj_abi *oa_frj(pTHX) {
98 7941 ▶ if (!OA_FRJ && !OA_FRJ_TRIED) {
Branch: 2 branches
BranchTrueFalseTotal
if (!OA_FRJ && !OA_FRJ_TRIED) { 30 7911 7941
if (!OA_FRJ && !OA_FRJ_TRIED) { 30 0 30
99 30 dSP; int count; IV p = 0;
100 30 OA_FRJ_TRIED = 1;
101 30 eval_pv("require File::Raw::JSON;", FALSE);
102 /* The require runs arbitrary Perl; if that grew the value stack it was
103 * reallocated, and the SP captured above now points into the freed
104 * block - which PUTBACK would hand straight back to the interpreter. */
105 30 SPAGAIN;
106 30 ▶ if (!SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV)) { 30 0 30
if (!SvTRUE(ERRSV)) { 30 0 30
107 30 ▶ ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; 0 30 30
108 30 count = call_pv("File::Raw::JSON::_abi_ptr", G_SCALAR | G_EVAL);
109 30 SPAGAIN;
110 30 ▶ if (!SvTRUE(ERRSV) && count > 0) p = POPi;
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 30 0 30
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 30 0 30
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 30 0 30
111 0 ▶ else if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) (void)POPs; 0 0 0
112 30 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 30 0 30
113 30 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 30 0 30
114 30 const frj_abi *a = INT2PTR(const frj_abi *, p);
115 30 ▶ if (a && a->abi_version >= FRJ_ABI_VERSION) OA_FRJ = a;
Branch: 2 branches
BranchTrueFalseTotal
if (a && a->abi_version >= FRJ_ABI_VERSION) OA_FRJ = a; 30 0 30
if (a && a->abi_version >= FRJ_ABI_VERSION) OA_FRJ = a; 30 0 30
116 }
117 }
118 }
119 7941 ▶ if (!OA_FRJ)
Branch: 1 branch
BranchTrueFalseTotal
if (!OA_FRJ) 0 7941 7941
120 0 croak("Open::API: JSON decode needs File::Raw::JSON with a compatible "
121 "C ABI (FRJ_ABI_VERSION %d)", FRJ_ABI_VERSION);
122 7941 return OA_FRJ;
123 }
124  
125 /* Decode JSON text via File::Raw::JSON's C ABI. Mortal ref (SV +1 -> mortal),
126 * or croak (byte-offset message) on malformed JSON. */
127 2553 static SV *oa_json_decode(pTHX_ SV *text) {
128 2553 const frj_abi *J = oa_frj(aTHX);
129 STRLEN len;
130 2553 const char *pv = SvPV(text, len);
131 2553 return sv_2mortal(J->decode(aTHX_ pv, len, NULL));
132 }
133  
134 /* Decode YAML text via YAML::XS, loaded lazily - only a YAML spec needs it,
135 * so it is not a PREREQ. A small Perl shim is installed once: it localises
136 * $YAML::XS::Boolean to 'JSON::PP' (when JSON::PP is available) so YAML
137 * true/false decode to boolean objects like JSON booleans do, without
138 * changing YAML::XS behaviour for the rest of the process. Mortal ref, or
139 * croak. */
140 0 static SV *oa_yaml_decode(pTHX_ SV *text) {
141 0 dSP; int count; SV *ret;
142 static int shim = 0;
143 0 eval_pv("require YAML::XS;", FALSE);
144 0 ▶ if (SvTRUE(ERRSV))
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) 0 0 0
if (SvTRUE(ERRSV)) 0 0 0
145 0 croak("Open::API: a YAML spec requires YAML::XS (not installed)");
146 0 ▶ if (!shim) {
Branch: 1 branch
BranchTrueFalseTotal
if (!shim) { 0 0 0
147 0 eval_pv(
148 "sub Open::API::_yaml_load {"
149 " local $YAML::XS::Boolean ="
150 " eval { require JSON::PP; 1 } ? 'JSON::PP' : $YAML::XS::Boolean;"
151 " YAML::XS::Load($_[0]);"
152 "}", TRUE);
153 0 shim = 1;
154 }
155 /* Both eval_pv calls above can grow (and so reallocate) the value stack;
156 * SP was captured before them. */
157 0 SPAGAIN;
158 0 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 0 0
159 0 ▶ XPUSHs(text); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(text); PUTBACK; 0 0 0
160 0 count = call_pv("Open::API::_yaml_load", G_SCALAR | G_EVAL);
161 0 SPAGAIN;
162 0 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 0 0 0
if (SvTRUE(ERRSV)) { 0 0 0
163 0 ▶ SV *e = newSVsv(ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
SV *e = newSVsv(ERRSV); 0 0 0
164 0 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 0 0 0
165 0 croak("Open::API: spec YAML parse failed: %s", SvPV_nolen(e));
166 }
167 0 ▶ ret = count > 0 ? newSVsv(POPs) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
ret = count > 0 ? newSVsv(POPs) : &PL_sv_undef; 0 0 0
168 0 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 0 0 0
169 0 return sv_2mortal(ret);
170 }
171  
172 /* True when a client's spec declares the operationId `name`. Shared by the
173 * _has_op and AUTOLOAD XSUBs. `self` must be an Open::API::Client hashref. */
174 9 static int oa_op_exists(pTHX_ HV *self, SV *name) {
175 9 SV **e = hv_fetchs(self, "api", 0);
176 9 ▶ SV *api = (e && *e) ? *e : NULL;
Branch: 2 branches
BranchTrueFalseTotal
SV *api = (e && *e) ? *e : NULL; 9 0 9
SV *api = (e && *e) ? *e : NULL; 9 0 9
177 9 ▶ if (api && SvROK(api)) {
Branch: 2 branches
BranchTrueFalseTotal
if (api && SvROK(api)) { 9 0 9
if (api && SvROK(api)) { 9 0 9
178 9 oa_api *a = (oa_api *)INT2PTR(void *, SvIV(SvRV(api)));
179 9 return oa_op_by_id(aTHX_ a, name) ? 1 : 0;
180 }
181 0 return 0;
182 }
183  
184 /* Slurp a file into a mortal SV, or NULL if it cannot be opened. */
185 2553 static SV *oa_slurp(pTHX_ const char *path) {
186 2553 PerlIO *fh = PerlIO_open(path, "rb");
187 SV *sv;
188 char buf[8192];
189 SSize_t n;
190 2553 ▶ if (!fh) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!fh) return NULL; 3 2550 2553
191 2550 sv = sv_2mortal(newSVpvs(""));
192 5707 ▶ while ((n = PerlIO_read(fh, buf, sizeof buf)) > 0)
Branch: 1 branch
BranchTrueFalseTotal
while ((n = PerlIO_read(fh, buf, sizeof buf)) > 0) 3157 2550 5707
193 3157 sv_catpvn(sv, buf, (STRLEN)n);
194 2550 PerlIO_close(fh);
195 2550 return sv;
196 }
197  
198 /* True when the text's first non-whitespace byte is `c`. */
199 3 static int oa_first_nonws_is(const char *p, STRLEN l, char c) {
200 3 STRLEN i = 0;
201 3 ▶ while (i < l && isSPACE((U8)p[i])) i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < l && isSPACE((U8)p[i])) i++; 3 0 3
while (i < l && isSPACE((U8)p[i])) i++; 0 3 3
202 3 ▶ return i < l && p[i] == c;
Branch: 2 branches
BranchTrueFalseTotal
return i < l && p[i] == c; 3 0 3
return i < l && p[i] == c; 3 0 3
203 }
204  
205 /* Resolve the spec argument - a hashref, JSON text, YAML text, or a filename -
206 * into a decoded document hashref (mortal or borrowed). Croaks on failure. */
207 7971 static SV *oa_load_spec(pTHX_ SV *spec) {
208 STRLEN l;
209 const char *p;
210 7971 SV *doc = NULL;
211  
212 7971 ▶ if (SvROK(spec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(spec)) { 5418 2553 7971
213 5418 ▶ if (SvTYPE(SvRV(spec)) != SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(spec)) != SVt_PVHV) 1 5417 5418
214 1 croak("Open::API: spec reference must be a hashref");
215 5417 return spec;
216 }
217 2553 ▶ if (!SvOK(spec) || !SvPOK(spec))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(spec) || !SvPOK(spec)) 2553 0 2553
if (!SvOK(spec) || !SvPOK(spec)) 0 2553 2553
218 0 croak("Open::API: spec must be a hashref, JSON/YAML text, or a filename");
219  
220 2553 p = SvPV_const(spec, l);
221  
222 /* a filename: no newline, and the file opens */
223 2553 ▶ if (!memchr(p, '\n', l)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!memchr(p, '\n', l)) { 2553 0 2553
224 2553 SV *body = oa_slurp(aTHX_ p);
225 2553 ▶ if (body) {
Branch: 1 branch
BranchTrueFalseTotal
if (body) { 2550 3 2553
226 2550 STRLEN bl; const char *bp = SvPV_const(body, bl);
227 int is_json;
228 2550 ▶ if (l >= 5 && memEQ(p + l - 5, ".json", 5)) is_json = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (l >= 5 && memEQ(p + l - 5, ".json", 5)) is_json = 1; 2550 0 2550
if (l >= 5 && memEQ(p + l - 5, ".json", 5)) is_json = 1; 2550 0 2550
229 0 ▶ else if (l >= 5 && memEQ(p + l - 5, ".yaml", 5)) is_json = 0;
Branch: 2 branches
BranchTrueFalseTotal
else if (l >= 5 && memEQ(p + l - 5, ".yaml", 5)) is_json = 0; 0 0 0
else if (l >= 5 && memEQ(p + l - 5, ".yaml", 5)) is_json = 0; 0 0 0
230 0 ▶ else if (l >= 4 && memEQ(p + l - 4, ".yml", 4)) is_json = 0;
Branch: 2 branches
BranchTrueFalseTotal
else if (l >= 4 && memEQ(p + l - 4, ".yml", 4)) is_json = 0; 0 0 0
else if (l >= 4 && memEQ(p + l - 4, ".yml", 4)) is_json = 0; 0 0 0
231 0 else is_json = oa_first_nonws_is(bp, bl, '{');
232 2550 return is_json ? oa_json_decode(aTHX_ body)
233 5100 ▶ : oa_yaml_decode(aTHX_ body);
Branch: 1 branch
BranchTrueFalseTotal
: oa_yaml_decode(aTHX_ body); 2550 0 2550
234 }
235 /* fall through: not an openable file, treat as inline text */
236 }
237  
238 6 doc = oa_first_nonws_is(p, l, '{') ? oa_json_decode(aTHX_ spec)
239 3 ▶ : oa_yaml_decode(aTHX_ spec);
Branch: 1 branch
BranchTrueFalseTotal
: oa_yaml_decode(aTHX_ spec); 3 0 3
240 2 return doc;
241 }
242  
243 /* Classify the document's version (oa_doc_version in oa_compile.h: `openapi`
244 * 3.0[.x] or 3.1[.x], or `swagger` "2.0") and check the root structure every
245 * version shares. Croaks on anything else. */
246 7969 static int oa_check_version(pTHX_ SV *doc) {
247 HV *h;
248 int ver;
249 7969 ▶ if (!doc || !SvROK(doc) || SvTYPE(SvRV(doc)) != SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if (!doc || !SvROK(doc) || SvTYPE(SvRV(doc)) != SVt_PVHV) 7969 0 7969
if (!doc || !SvROK(doc) || SvTYPE(SvRV(doc)) != SVt_PVHV) 7969 0 7969
if (!doc || !SvROK(doc) || SvTYPE(SvRV(doc)) != SVt_PVHV) 0 7969 7969
250 0 croak("Open::API: spec did not decode to an object/hashref");
251 7969 h = (HV *)SvRV(doc);
252 /* The version is settled FIRST: the License identifier/url exclusion is a
253 * 3.1 rule and the components key alphabet a 3.x one, so the checks below
254 * need to know which dialect this is. */
255 7969 ver = oa_doc_version(aTHX_ h);
256  
257 /* `info` is REQUIRED, and so are its own `title` and `version`. Nothing
258 * here reads them, but a document without them is not an OpenAPI
259 * document, and accepting it means a typo in the top-level shape passes
260 * silently - which is the whole class of bug this validator exists to
261 * stop for requests. */
262 {
263 7960 SV **iv = hv_fetchs(h, "info", 0);
264 HV *info;
265 SV *lic;
266 7960 ▶ if (!iv || !*iv || !SvROK(*iv) || SvTYPE(SvRV(*iv)) != SVt_PVHV)
Branch: 4 branches
BranchTrueFalseTotal
if (!iv || !*iv || !SvROK(*iv) || SvTYPE(SvRV(*iv)) != SVt_PVHV) 7959 1 7960
if (!iv || !*iv || !SvROK(*iv) || SvTYPE(SvRV(*iv)) != SVt_PVHV) 7959 0 7959
if (!iv || !*iv || !SvROK(*iv) || SvTYPE(SvRV(*iv)) != SVt_PVHV) 7959 0 7959
if (!iv || !*iv || !SvROK(*iv) || SvTYPE(SvRV(*iv)) != SVt_PVHV) 0 7959 7959
267 1 croak("Open::API: spec has no 'info' object (it is required)");
268 7959 info = (HV *)SvRV(*iv);
269 7959 ▶ if (!oa_get(aTHX_ info, "title"))
Branch: 1 branch
BranchTrueFalseTotal
if (!oa_get(aTHX_ info, "title")) 3 7956 7959
270 3 croak("Open::API: info has no 'title' (it is required)");
271 7956 ▶ if (!oa_get(aTHX_ info, "version"))
Branch: 1 branch
BranchTrueFalseTotal
if (!oa_get(aTHX_ info, "version")) 2 7954 7956
272 2 croak("Open::API: info has no 'version' (it is required)");
273 7954 lic = oa_get(aTHX_ info, "license");
274 7954 ▶ if (lic && oa_hv_of(lic)) {
Branch: 2 branches
BranchTrueFalseTotal
if (lic && oa_hv_of(lic)) { 1245 6709 7954
if (lic && oa_hv_of(lic)) { 1245 0 1245
275 1245 HV *lh = (HV *)SvRV(lic);
276 1245 ▶ if (!oa_get(aTHX_ lh, "name"))
Branch: 1 branch
BranchTrueFalseTotal
if (!oa_get(aTHX_ lh, "name")) 2 1243 1245
277 2 croak("Open::API: info.license has no 'name' (it is required)");
278 /* 3.1 added `identifier`, and it excludes `url` */
279 1243 ▶ if (ver == OA_V31 && oa_get(aTHX_ lh, "identifier")
Branch: 2 branches
BranchTrueFalseTotal
if (ver == OA_V31 && oa_get(aTHX_ lh, "identifier") 1 1242 1243
if (ver == OA_V31 && oa_get(aTHX_ lh, "identifier") 1 0 1
280 1 ▶ && oa_get(aTHX_ lh, "url"))
Branch: 1 branch
BranchTrueFalseTotal
&& oa_get(aTHX_ lh, "url")) 1 0 1
281 1 croak("Open::API: info.license declares both 'identifier' and "
282 "'url', which are mutually exclusive");
283 }
284 /* Contact `email` and `url` are both constrained by the
285 * specification. Neither is read here, but a contact address nobody
286 * can write to is the same class of typo as a missing title. */
287 {
288 7951 SV *con = oa_get(aTHX_ info, "contact");
289 7951 ▶ if (con && oa_hv_of(con)) {
Branch: 2 branches
BranchTrueFalseTotal
if (con && oa_hv_of(con)) { 1249 6702 7951
if (con && oa_hv_of(con)) { 1249 0 1249
290 1249 HV *ch = (HV *)SvRV(con);
291 1249 SV *em = oa_get(aTHX_ ch, "email");
292 1249 SV *cu = oa_get(aTHX_ ch, "url");
293 1249 ▶ if (em && !oa_looks_like_email(aTHX_ em))
Branch: 2 branches
BranchTrueFalseTotal
if (em && !oa_looks_like_email(aTHX_ em)) 1231 18 1249
if (em && !oa_looks_like_email(aTHX_ em)) 3 1228 1231
294 3 croak("Open::API: info.contact.email is not in the format "
295 "of an email address");
296 1246 ▶ if (cu && !oa_looks_like_url(aTHX_ cu))
Branch: 2 branches
BranchTrueFalseTotal
if (cu && !oa_looks_like_url(aTHX_ cu)) 1229 17 1246
if (cu && !oa_looks_like_url(aTHX_ cu)) 2 1227 1229
297 2 croak("Open::API: info.contact.url is not in the format of "
298 "a URL");
299 }
300 }
301 }
302  
303 /* External Documentation at the document root. The operation-level and
304 * tag-level positions are checked where those are walked. */
305 7946 oa_ext_docs_ok(aTHX_ oa_get(aTHX_ h, "externalDocs"), "the document");
306  
307 /* Every Tag Object requires a name, and the names in the list MUST be
308 * unique - two tags of the same name describe one tag twice. */
309 {
310 7943 AV *tags = oa_av_of(oa_get(aTHX_ h, "tags"));
311 7943 ▶ SSize_t i, n = tags ? av_len(tags) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = tags ? av_len(tags) + 1 : 0; 609 7334 7943
312 7943 ▶ HV *seen = n ? (HV *)sv_2mortal((SV *)newHV()) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *seen = n ? (HV *)sv_2mortal((SV *)newHV()) : NULL; 609 7334 7943
313 8550 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 611 7939 8550
314 611 SV **e = av_fetch(tags, i, 0);
315 611 ▶ HV *t = (e && *e) ? oa_hv_of(*e) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *t = (e && *e) ? oa_hv_of(*e) : NULL; 611 0 611
HV *t = (e && *e) ? oa_hv_of(*e) : NULL; 611 0 611
316 SV *nm, *xd;
317 STRLEN tl; const char *tp;
318 611 ▶ if (!t) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!t) continue; 0 611 611
319 611 nm = oa_get(aTHX_ t, "name");
320 611 ▶ if (!nm) croak("Open::API: a tag has no 'name' (it is required)");
Branch: 1 branch
BranchTrueFalseTotal
if (!nm) croak("Open::API: a tag has no 'name' (it is required)"); 1 610 611
321 610 tp = SvPV_const(nm, tl);
322 610 ▶ if (hv_exists(seen, tp, (I32)tl))
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(seen, tp, (I32)tl)) 2 608 610
323 2 croak("Open::API: tag '%.*s' is declared more than once; tag "
324 "names must be unique", (int)tl, tp);
325 608 (void)hv_store(seen, tp, (I32)tl, newSViv(1), 0);
326 608 xd = oa_get(aTHX_ t, "externalDocs");
327 608 ▶ if (xd && oa_hv_of(xd) && !oa_get(aTHX_ (HV *)SvRV(xd), "url"))
Branch: 3 branches
BranchTrueFalseTotal
if (xd && oa_hv_of(xd) && !oa_get(aTHX_ (HV *)SvRV(xd), "url")) 604 4 608
if (xd && oa_hv_of(xd) && !oa_get(aTHX_ (HV *)SvRV(xd), "url")) 604 0 604
if (xd && oa_hv_of(xd) && !oa_get(aTHX_ (HV *)SvRV(xd), "url")) 1 603 604
328 1 croak("Open::API: tag '%.*s': externalDocs has no 'url' "
329 "(it is required)", (int)tl, tp);
330 }
331 }
332  
333 /* A Server Variable requires a `default`, and when it declares an `enum`
334 * the default has to be one of the values - a default outside its own
335 * enumeration describes a server nobody can reach. */
336 {
337 7939 AV *srv = oa_av_of(oa_get(aTHX_ h, "servers"));
338 7939 ▶ SSize_t i, n = srv ? av_len(srv) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = srv ? av_len(srv) + 1 : 0; 23 7916 7939
339 7959 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 24 7935 7959
340 24 SV **e = av_fetch(srv, i, 0);
341 24 ▶ HV *s = (e && *e) ? oa_hv_of(*e) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *s = (e && *e) ? oa_hv_of(*e) : NULL; 24 0 24
HV *s = (e && *e) ? oa_hv_of(*e) : NULL; 24 0 24
342 HV *vars;
343 HE *vhe;
344 /* `url` is the one REQUIRED field of a Server Object, and with
345 * servers => 1 it is what the prefix is taken from. */
346 24 ▶ if (s && !oa_get(aTHX_ s, "url"))
Branch: 2 branches
BranchTrueFalseTotal
if (s && !oa_get(aTHX_ s, "url")) 24 0 24
if (s && !oa_get(aTHX_ s, "url")) 2 22 24
347 2 croak("Open::API: a server has no 'url' (it is required)");
348 22 ▶ vars = s ? oa_hv_of(oa_get(aTHX_ s, "variables")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
vars = s ? oa_hv_of(oa_get(aTHX_ s, "variables")) : NULL; 22 0 22
349 22 ▶ if (!vars) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!vars) continue; 18 4 22
350 4 hv_iterinit(vars);
351 6 ▶ while ((vhe = hv_iternext(vars))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((vhe = hv_iternext(vars))) { 4 2 6
352 4 I32 vkl; const char *vk = hv_iterkey(vhe, &vkl);
353 4 HV *vh = oa_hv_of(hv_iterval(vars, vhe));
354 4 ▶ SV *def = vh ? oa_get(aTHX_ vh, "default") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *def = vh ? oa_get(aTHX_ vh, "default") : NULL; 4 0 4
355 4 ▶ AV *en = vh ? oa_av_of(oa_get(aTHX_ vh, "enum")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
AV *en = vh ? oa_av_of(oa_get(aTHX_ vh, "enum")) : NULL; 4 0 4
356 4 ▶ if (!vh) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!vh) continue; 0 4 4
357 4 ▶ if (!def)
Branch: 1 branch
BranchTrueFalseTotal
if (!def) 1 3 4
358 1 croak("Open::API: server variable '%.*s' has no 'default' "
359 "(it is required)", (int)vkl, vk);
360 3 ▶ if (en) {
Branch: 1 branch
BranchTrueFalseTotal
if (en) { 1 2 3
361 1 SSize_t j, m = av_len(en) + 1;
362 1 int found = 0;
363 3 ▶ for (j = 0; j < m && !found; j++) {
Branch: 2 branches
BranchTrueFalseTotal
for (j = 0; j < m && !found; j++) { 2 1 3
for (j = 0; j < m && !found; j++) { 2 0 2
364 2 SV **ev = av_fetch(en, j, 0);
365 2 ▶ if (ev && *ev && sv_eq(*ev, def)) found = 1;
Branch: 3 branches
BranchTrueFalseTotal
if (ev && *ev && sv_eq(*ev, def)) found = 1; 2 0 2
if (ev && *ev && sv_eq(*ev, def)) found = 1; 2 0 2
if (ev && *ev && sv_eq(*ev, def)) found = 1; 0 2 2
366 }
367 1 ▶ if (!found)
Branch: 1 branch
BranchTrueFalseTotal
if (!found) 1 0 1
368 1 croak("Open::API: server variable '%.*s': its default "
369 "is not one of its enum values", (int)vkl, vk);
370 }
371 }
372 }
373 }
374  
375 /* Components keys are drawn from a fixed alphabet. A key outside it
376 * cannot be named by a $ref, so the component is unreachable. 3.x only:
377 * 2.0 fixes no alphabet for `definitions`, and a converted 2.0 document
378 * fed back through new() carries them as components, so checking them
379 * there would refuse this build's own output on the second pass. */
380 7935 ▶ if (ver >= OA_V30) {
Branch: 1 branch
BranchTrueFalseTotal
if (ver >= OA_V30) { 4011 3924 7935
381 static const char *const sections[] = {
382 "schemas", "responses", "parameters", "examples", "requestBodies",
383 "headers", "securitySchemes", "links", "callbacks", "pathItems",
384 NULL
385 };
386 4011 HV *comp = oa_hv_of(oa_get(aTHX_ h, "components"));
387 int si;
388 27545 ▶ for (si = 0; comp && sections[si]; si++) {
Branch: 2 branches
BranchTrueFalseTotal
for (si = 0; comp && sections[si]; si++) { 25889 1656 27545
for (si = 0; comp && sections[si]; si++) { 23537 2352 25889
389 23537 HV *sec = oa_hv_of(oa_get(aTHX_ comp, sections[si]));
390 HE *she;
391 23537 ▶ if (!sec) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!sec) continue; 21147 2390 23537
392 2390 hv_iterinit(sec);
393 7936 ▶ while ((she = hv_iternext(sec))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((she = hv_iternext(sec))) { 5549 2387 7936
394 5549 I32 kl; const char *k = hv_iterkey(she, &kl);
395 5549 ▶ if (!oa_key_ok(k, (STRLEN)kl))
Branch: 1 branch
BranchTrueFalseTotal
if (!oa_key_ok(k, (STRLEN)kl)) 3 5546 5549
396 3 croak("Open::API: components.%s key '%.*s' is not a legal "
397 "component name", sections[si], (int)kl, k);
398 }
399 }
400 }
401 /* jsonSchemaDialect names the dialect every Schema Object in the document
402 * is written against. A 3.1 Schema Object IS JSON Schema 2020-12, which
403 * is what this build validates, so a document declaring any other dialect
404 * cannot be validated here - and validating it as 2020-12 anyway, which
405 * is what happened before, is worse than saying so. */
406 {
407 static const char std[] = "https://json-schema.org/draft/2020-12/schema";
408 7932 SV **dv = hv_fetchs(h, "jsonSchemaDialect", 0);
409 7932 ▶ if (ver >= OA_V30 && dv && *dv && SvOK(*dv)) {
Branch: 4 branches
BranchTrueFalseTotal
if (ver >= OA_V30 && dv && *dv && SvOK(*dv)) { 4008 3924 7932
if (ver >= OA_V30 && dv && *dv && SvOK(*dv)) { 1 4007 4008
if (ver >= OA_V30 && dv && *dv && SvOK(*dv)) { 1 0 1
if (ver >= OA_V30 && dv && *dv && SvOK(*dv)) { 1 0 1
410 STRLEN dl;
411 1 const char *dp = SvPV_const(*dv, dl);
412 1 ▶ if (!(dl == sizeof(std) - 1 && memEQ(dp, std, sizeof(std) - 1)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(dl == sizeof(std) - 1 && memEQ(dp, std, sizeof(std) - 1))) 0 1 1
if (!(dl == sizeof(std) - 1 && memEQ(dp, std, sizeof(std) - 1))) 0 0 0
413 1 croak("Open::API: jsonSchemaDialect '%.*s' is not supported; "
414 "this build validates JSON Schema 2020-12", (int)dl, dp);
415 }
416 }
417 7931 return ver;
418 }
419  
420 /* Construct a compiled Open::API from a spec argument (shared by
421 * Open::API->new and Open::API::Client->new(spec => ...)).
422 *
423 * The document is normalised here, before anything else sees it, so `a->spec` -
424 * and therefore the validator, the mock generator and the docs UI - only ever
425 * deals in one schema dialect: a 3.0 document is up-converted to 3.1 shape, and
426 * in either version a `discriminator` is expanded into the 2020-12 constructs
427 * that express it. See oa_normalize.h. */
428 7971 static SV *oa_new_from_spec(pTHX_ const char *cls, SV *spec, int servers) {
429 7971 SV *doc = oa_load_spec(aTHX_ spec);
430 oa_api *a;
431 7969 int ver = oa_check_version(aTHX_ doc);
432 /* A 2.0 document is first brought to 3.0 SHAPE (oa_swagger2.h), and the
433 * 3.0 pass then does the schema work it already does for 3.0. */
434 7931 ▶ if (ver == OA_V20) doc = sv_2mortal(oa_swagger2_to_30(aTHX_ doc));
Branch: 1 branch
BranchTrueFalseTotal
if (ver == OA_V20) doc = sv_2mortal(oa_swagger2_to_30(aTHX_ doc)); 3924 4007 7931
435 5786 doc = sv_2mortal(oa_normalize(aTHX_ doc, ver <= OA_V30));
436 5754 a = oa_api_new(aTHX);
437 5754 ▶ if (!a) croak("Open::API: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!a) croak("Open::API: out of memory"); 0 5754 5754
438 5754 a->spec = newSVsv(doc);
439 5754 a->want_servers = servers ? 1 : 0;
440 5754 ▶ if (servers) oa_set_server_prefix(aTHX_ a);
Branch: 1 branch
BranchTrueFalseTotal
if (servers) oa_set_server_prefix(aTHX_ a); 8 5746 5754
441 5754 oa_compile(aTHX_ a);
442 5730 return sv_bless(newRV_noinc(newSViv(PTR2IV(a))), gv_stashpv(cls, GV_ADD));
443 }
444  
445 /* ---- Open::API::Plack configuration --------------------------------------- *
446 * An Open::API::Plack instance is a blessed config HV: options accumulate
447 * through new and the accessors, and to_app compiles them into the app. */
448  
449 2133 static HV *oa_plack_hv(pTHX_ SV *self) {
450 2133 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV 2133 0 2133
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV 2133 0 2133
451 2133 ▶ || !sv_derived_from(self, "Open::API::Plack"))
Branch: 1 branch
BranchTrueFalseTotal
|| !sv_derived_from(self, "Open::API::Plack")) 0 2133 2133
452 0 croak("Open::API::Plack: not an Open::API::Plack object");
453 2133 return (HV *)SvRV(self);
454 }
455  
456 /* Fetch-or-create a keyed map entry (handlers / security) on the config HV. */
457 1084 static HV *oa_plack_map(pTHX_ HV *cfg, const char *key, I32 kl) {
458 1084 SV **e = hv_fetch(cfg, key, kl, 0);
459 HV *m;
460 1084 ▶ if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)
Branch: 4 branches
BranchTrueFalseTotal
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 4 1080 1084
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 4 0 4
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 4 0 4
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 4 0 4
461 4 return (HV *)SvRV(*e);
462 1080 m = newHV();
463 1080 (void)hv_store(cfg, key, kl, newRV_noinc((SV *)m), 0);
464 1080 return m;
465 }
466  
467 /* Store one configuration option. `api` is type-checked and `spec` compiled
468 * right away (both land under `api`); `handlers` and `security` merge a
469 * hashref into the stored map; anything else is a plain replace. Shared by
470 * new and the accessors so the semantics are identical everywhere. */
471 1730 static void oa_plack_set(pTHX_ HV *cfg, const char *k, STRLEN kl, SV *v) {
472 1730 ▶ if (kl == 3 && memEQ(k, "api", 3)) {
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 3 && memEQ(k, "api", 3)) { 565 1165 1730
if (kl == 3 && memEQ(k, "api", 3)) { 565 0 565
473 565 ▶ if (!SvROK(v) || !sv_derived_from(v, "Open::API"))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(v) || !sv_derived_from(v, "Open::API")) 564 1 565
if (!SvROK(v) || !sv_derived_from(v, "Open::API")) 0 564 564
474 1 croak("Open::API::Plack: 'api' must be an Open::API");
475 564 (void)hv_stores(cfg, "api", newSVsv(v));
476 1165 ▶ } else if (kl == 4 && memEQ(k, "spec", 4)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (kl == 4 && memEQ(k, "spec", 4)) { 29 1136 1165
} else if (kl == 4 && memEQ(k, "spec", 4)) { 15 14 29
477 15 (void)hv_stores(cfg, "api", oa_new_from_spec(aTHX_ "Open::API", v, 0));
478 1150 ▶ } else if ((kl == 8 && memEQ(k, "handlers", 8))
Branch: 2 branches
BranchTrueFalseTotal
} else if ((kl == 8 && memEQ(k, "handlers", 8)) 576 574 1150
} else if ((kl == 8 && memEQ(k, "handlers", 8)) 501 75 576
479 1651 ▶ || (kl == 8 && memEQ(k, "security", 8))) {
Branch: 2 branches
BranchTrueFalseTotal
|| (kl == 8 && memEQ(k, "security", 8))) { 501 574 1075
|| (kl == 8 && memEQ(k, "security", 8))) { 501 0 501
480 576 HV *src = oa_hv_of(v), *dst;
481 HE *he;
482 576 ▶ if (!src)
Branch: 1 branch
BranchTrueFalseTotal
if (!src) 0 576 576
483 0 croak("Open::API::Plack: '%.*s' must be a hashref", (int)kl, k);
484 576 dst = oa_plack_map(aTHX_ cfg, k, (I32)kl);
485 576 hv_iterinit(src);
486 720 ▶ while ((he = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) { 144 576 720
487 144 I32 ekl; const char *ek = hv_iterkey(he, &ekl);
488 144 (void)hv_store(dst, ek, ekl, newSVsv(hv_iterval(src, he)), 0);
489 }
490 } else {
491 574 (void)hv_store(cfg, k, (I32)kl, newSVsv(v), 0);
492 }
493 1729 }
494  
495 MODULE = Open::API PACKAGE = Open::API
496  
497 PROTOTYPES: DISABLE
498  
499 BOOT:
500 {
501 38 IV p = 0;
502 38 dSP;
503 38 eval_pv("require JSON::Schema::Fast;", FALSE);
504 38 SPAGAIN; /* the require may have reallocated the value stack */
505 38 ▶ if (!SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV)) { 38 0 38
if (!SvTRUE(ERRSV)) { 38 0 38
506 38 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 38 38
507 38 PUTBACK;
508 38 ▶ if (call_pv("JSON::Schema::Fast::_abi_ptr", G_SCALAR | G_EVAL) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (call_pv("JSON::Schema::Fast::_abi_ptr", G_SCALAR | G_EVAL) > 0) { 38 0 38
509 38 SPAGAIN;
510 38 ▶ if (!SvTRUE(ERRSV)) p = POPi; else (void)POPs;
Branch: 2 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV)) p = POPi; else (void)POPs; 38 0 38
if (!SvTRUE(ERRSV)) p = POPi; else (void)POPs; 37 1 38
511 38 PUTBACK;
512 }
513 }
514 38 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 36 2 38
515 36 const jsf_abi *a = INT2PTR(const jsf_abi *, p);
516 36 ▶ if (a && a->abi_version >= JSF_ABI_VERSION) JSF = a;
Branch: 2 branches
BranchTrueFalseTotal
if (a && a->abi_version >= JSF_ABI_VERSION) JSF = a; 36 0 36
if (a && a->abi_version >= JSF_ABI_VERSION) JSF = a; 36 0 36
517 }
518 /* cache the frj trampoline CVs: the request path invokes them via
519 * call_sv + G_EVAL (no name lookup, croaks contained) */
520 38 OA_DEC_CV = SvREFCNT_inc((SV *)get_cv("Open::API::_frj_decode", GV_ADD));
521 38 OA_ENC_CV = SvREFCNT_inc((SV *)get_cv("Open::API::_frj_encode", GV_ADD));
522 }
523  
524 # True when JSON::Schema::Fast's C ABI resolved and matched our vendored
525 # version. Open/API.pm croaks at load when it did not.
526 int
527 _abi_ok()
528 CODE:
529 39 ▶ RETVAL = JSF ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = JSF ? 1 : 0; 0 39 39
530 OUTPUT:
531 RETVAL
532  
533 # Address of Open::API's own C ABI table (oa_abi.h). A consumer XS module
534 # (Punk's C dispatcher) fetches this once at boot, INT2PTRs it to a
535 # `const oa_abi *`, and checks ->abi_version before using it. Not part of the
536 # public Perl API.
537 IV
538 _abi_ptr()
539 CODE:
540 2 ▶ RETVAL = PTR2IV(&OA_ABI);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2IV(&OA_ABI); 0 2 2
541 OUTPUT:
542 RETVAL
543  
544 # Exercise the whole oa_abi table the way a C consumer (Punk) would: resolve
545 # it from the IV _abi_ptr hands back, gate on abi_version, then drive
546 # api_of -> route -> op_id -> validate through the function pointers. Returns a
547 # fixed 3-tuple the test compares against native match/validate_request:
548 # (undef, undef, undef) 404 - no such path
549 # (undef, \@allow, undef) 405 - path exists, wrong method
550 # ($op_id, undef, undef) matched; $raw undef, so no validation
551 # ($op_id, $ok, $ref) matched + validated: (1, \%params) or (0, \@errors)
552 # Private; the version guard returning empty stands in for a consumer's
553 # fall-back to the Perl-visible API.
554 void
555 _abi_selftest(self, method, path, raw)
556 SV *self
557 SV *method
558 SV *path
559 SV *raw
560 PPCODE:
561 {
562 7 const oa_abi *A = INT2PTR(const oa_abi *, PTR2IV(&OA_ABI));
563 STRLEN ml, pl;
564 7 ▶ const char *mp = SvPV_const(method, ml);
Branch: 1 branch
BranchTrueFalseTotal
const char *mp = SvPV_const(method, ml); 7 0 7
565 7 const char *pp = SvPV_const(path, pl);
566 void *api, *op;
567 7 HV *caps = (HV *)sv_2mortal((SV *)newHV());
568 7 AV *allow = (AV *)sv_2mortal((SV *)newAV());
569 SV *oid;
570 7 ▶ if (!A || A->abi_version != OA_ABI_VERSION) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!A || A->abi_version != OA_ABI_VERSION) XSRETURN_EMPTY; 7 0 7
if (!A || A->abi_version != OA_ABI_VERSION) XSRETURN_EMPTY; 0 7 7
571 7 api = A->api_of(aTHX_ self);
572 7 ▶ if (!api) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!api) XSRETURN_EMPTY; 0 7 7
573 7 op = A->route(aTHX_ api, mp, ml, pp, pl, caps, allow);
574 7 ▶ if (!op) {
Branch: 1 branch
BranchTrueFalseTotal
if (!op) { 3 4 7
575 3 ▶ if (av_len(allow) >= 0) { /* 405 */
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(allow) >= 0) { /* 405 */ 2 1 3
576 2 ▶ XPUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(&PL_sv_undef); 0 2 2
577 2 ▶ mXPUSHs(newRV_inc((SV *)allow));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_inc((SV *)allow)); 0 2 2
578 2 ▶ XPUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(&PL_sv_undef); 0 2 2
579 } else { /* 404 */
580 1 ▶ XPUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(&PL_sv_undef); 0 1 1
581 1 ▶ XPUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(&PL_sv_undef); 0 1 1
582 1 ▶ XPUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(&PL_sv_undef); 0 1 1
583 }
584 } else {
585 4 oid = A->op_id(aTHX_ op);
586 4 ▶ mXPUSHs(oid ? newSVsv(oid) : &PL_sv_undef);
Branch: 2 branches
BranchTrueFalseTotal
mXPUSHs(oid ? newSVsv(oid) : &PL_sv_undef); 0 4 4
mXPUSHs(oid ? newSVsv(oid) : &PL_sv_undef); 4 0 4
587 4 ▶ if (raw && SvROK(raw) && SvTYPE(SvRV(raw)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (raw && SvROK(raw) && SvTYPE(SvRV(raw)) == SVt_PVHV) { 4 0 4
if (raw && SvROK(raw) && SvTYPE(SvRV(raw)) == SVt_PVHV) { 2 2 4
if (raw && SvROK(raw) && SvTYPE(SvRV(raw)) == SVt_PVHV) { 2 0 2
588 2 HV *typed = (HV *)sv_2mortal((SV *)newHV());
589 2 AV *errs = (AV *)sv_2mortal((SV *)newAV());
590 2 int ok = A->validate(aTHX_ api, op, (HV *)SvRV(raw),
591 typed, errs);
592 2 ▶ mXPUSHs(newSViv(ok));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSViv(ok)); 0 2 2
593 2 ▶ mXPUSHs(ok ? newRV_inc((SV *)typed) : newRV_inc((SV *)errs));
Branch: 2 branches
BranchTrueFalseTotal
mXPUSHs(ok ? newRV_inc((SV *)typed) : newRV_inc((SV *)errs)); 0 2 2
mXPUSHs(ok ? newRV_inc((SV *)typed) : newRV_inc((SV *)errs)); 1 1 2
594 } else {
595 2 ▶ XPUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(&PL_sv_undef); 0 2 2
596 2 ▶ XPUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(&PL_sv_undef); 0 2 2
597 }
598 }
599 }
600  
601 # Direct frj-ABI trampolines. They croak on bad input (the ABI's semantics);
602 # the request path calls them on cached CVs with G_EVAL so a malformed body
603 # is a 400 and an unencodable handler return degrades cleanly. Private.
604 SV *
605 _frj_decode(text)
606 SV *text
607 CODE:
608 {
609 4315 const frj_abi *J = oa_frj(aTHX);
610 STRLEN len;
611 4315 const char *pv = SvPV_const(text, len);
612 4315 RETVAL = J->decode(aTHX_ pv, len, NULL);
613 }
614 OUTPUT:
615 RETVAL
616  
617 SV *
618 _frj_encode(val)
619 SV *val
620 CODE:
621 578 RETVAL = oa_frj(aTHX)->encode(aTHX_ val, NULL);
622 OUTPUT:
623 RETVAL
624  
625 # Open::API->new(spec => ...): load and gate the document. The operation
626 # table is compiled here too (phase 1).
627 SV *
628 new(class, ...)
629 SV *class
630 CODE:
631 {
632 0 ▶ const char *cls = (SvROK(class) && SvOBJECT(SvRV(class)))
Branch: 1 branch
BranchTrueFalseTotal
const char *cls = (SvROK(class) && SvOBJECT(SvRV(class))) 0 0 0
633 15914 ▶ ? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class);
Branch: 7 branches
BranchTrueFalseTotal
? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); 0 7957 7957
? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); 0 0 0
? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); 0 0 0
? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); 0 0 0
? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); 0 0 0
? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); 0 0 0
? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); 0 0 0
634 7957 SV *spec = NULL;
635 7957 int servers = 0;
636 int i;
637 7957 ▶ if (!JSF) croak("Open::API: JSON::Schema::Fast C ABI unavailable");
Branch: 1 branch
BranchTrueFalseTotal
if (!JSF) croak("Open::API: JSON::Schema::Fast C ABI unavailable"); 0 7957 7957
638 15921 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 7964 7957 15921
639 7964 STRLEN kl; const char *k = SvPV_const(ST(i), kl);
640 7964 ▶ if (kl == 4 && memEQ(k, "spec", 4)) spec = ST(i + 1);
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 4 && memEQ(k, "spec", 4)) spec = ST(i + 1); 7956 8 7964
if (kl == 4 && memEQ(k, "spec", 4)) spec = ST(i + 1); 7956 0 7956
641 /* opt-in: strip the first server URL's path prefix when routing */
642 8 ▶ else if (kl == 7 && memEQ(k, "servers", 7))
Branch: 2 branches
BranchTrueFalseTotal
else if (kl == 7 && memEQ(k, "servers", 7)) 8 0 8
else if (kl == 7 && memEQ(k, "servers", 7)) 8 0 8
643 8 servers = SvTRUE(ST(i + 1)) ? 1 : 0;
644 }
645 7957 ▶ if (!spec) croak("Open::API->new: a 'spec' is required");
Branch: 1 branch
BranchTrueFalseTotal
if (!spec) croak("Open::API->new: a 'spec' is required"); 1 7956 7957
646 7956 RETVAL = oa_new_from_spec(aTHX_ cls, spec, servers);
647 }
648 OUTPUT:
649 RETVAL
650  
651 # The decoded spec document (a hashref copy reference).
652 SV *
653 spec(self)
654 SV *self
655 CODE:
656 {
657 210 oa_api *a = oa_api_of(aTHX_ self);
658 210 ▶ RETVAL = a->spec ? newSVsv(a->spec) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = a->spec ? newSVsv(a->spec) : &PL_sv_undef; 210 0 210
659 }
660 OUTPUT:
661 RETVAL
662  
663 # The document's declared version: its `openapi` string, or "2.0" for a
664 # Swagger document (which declares `swagger`, not `openapi`). A 2.0 or 3.0
665 # document keeps its own version string after conversion, so this tells a
666 # caller which dialect the spec was written in even though ->spec is
667 # 3.1-shaped throughout.
668 SV *
669 openapi_version(self)
670 SV *self
671 CODE:
672 {
673 13 oa_api *a = oa_api_of(aTHX_ self);
674 13 ▶ HV *h = a->spec ? oa_hv_of(a->spec) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *h = a->spec ? oa_hv_of(a->spec) : NULL; 13 0 13
675 13 ▶ SV *v = h ? oa_get(aTHX_ h, "openapi") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *v = h ? oa_get(aTHX_ h, "openapi") : NULL; 13 0 13
676 13 ▶ if (!v && h) v = oa_get(aTHX_ h, "swagger");
Branch: 2 branches
BranchTrueFalseTotal
if (!v && h) v = oa_get(aTHX_ h, "swagger"); 7 6 13
if (!v && h) v = oa_get(aTHX_ h, "swagger"); 7 0 7
677 13 ▶ RETVAL = v ? newSVsv(v) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = v ? newSVsv(v) : &PL_sv_undef; 13 0 13
678 }
679 OUTPUT:
680 RETVAL
681  
682 # All operations: [ { operationId, method, path }, ... ] in spec order.
683 SV *
684 operations(self)
685 SV *self
686 CODE:
687 {
688 58 oa_api *a = oa_api_of(aTHX_ self);
689 58 oa_ops *t = (oa_ops *)a->ops;
690 58 AV *out = newAV();
691 int i;
692 252 ▶ for (i = 0; t && i < t->n; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; t && i < t->n; i++) { 252 0 252
for (i = 0; t && i < t->n; i++) { 194 58 252
693 194 HV *h = newHV();
694 194 (void)hv_stores(h, "operationId", newSVsv(t->ops[i].op_id));
695 194 (void)hv_stores(h, "method", newSVsv(t->ops[i].method));
696 194 (void)hv_stores(h, "path", newSVsv(t->ops[i].path));
697 194 av_push(out, newRV_noinc((SV *)h));
698 }
699 58 RETVAL = newRV_noinc((SV *)out);
700 }
701 OUTPUT:
702 RETVAL
703  
704 # One operation described: params by location, body content types, response
705 # statuses. Returns undef for an unknown operationId.
706 SV *
707 operation(self, id)
708 SV *self
709 SV *id
710 CODE:
711 {
712 21 oa_api *a = oa_api_of(aTHX_ self);
713 21 oa_op *o = oa_op_by_id(aTHX_ a, id);
714 21 ▶ if (!o) { RETVAL = &PL_sv_undef; }
Branch: 1 branch
BranchTrueFalseTotal
if (!o) { RETVAL = &PL_sv_undef; } 1 20 21
715 else {
716 static const char *locs[OA_IN_N] =
717 { "path", "query", "header", "cookie" };
718 20 HV *h = newHV(), *ph = newHV(), *bh = newHV();
719 20 AV *ra = newAV(), *ca = newAV();
720 int loc, i;
721 20 (void)hv_stores(h, "operationId", newSVsv(o->op_id));
722 20 (void)hv_stores(h, "method", newSVsv(o->method));
723 20 (void)hv_stores(h, "path", newSVsv(o->path));
724 100 ▶ for (loc = 0; loc < OA_IN_N; loc++) {
Branch: 1 branch
BranchTrueFalseTotal
for (loc = 0; loc < OA_IN_N; loc++) { 80 20 100
725 80 AV *pa = newAV();
726 106 ▶ for (i = 0; i < o->nparams[loc]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < o->nparams[loc]; i++) { 26 80 106
727 26 HV *pe = newHV();
728 26 (void)hv_stores(pe, "name", newSVsv(o->params[loc][i].name));
729 26 (void)hv_stores(pe, "required",
730 newSViv(o->params[loc][i].required));
731 26 av_push(pa, newRV_noinc((SV *)pe));
732 }
733 80 (void)hv_store(ph, locs[loc], (I32)strlen(locs[loc]),
734 newRV_noinc((SV *)pa), 0);
735 }
736 20 (void)hv_stores(h, "params", newRV_noinc((SV *)ph));
737 28 ▶ for (i = 0; i < o->nbodies; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < o->nbodies; i++) 8 20 28
738 8 av_push(ca, newSVsv(o->bodies[i].ctype));
739 20 (void)hv_stores(bh, "required", newSViv(o->body_required));
740 20 (void)hv_stores(bh, "content", newRV_noinc((SV *)ca));
741 20 (void)hv_stores(h, "body", newRV_noinc((SV *)bh));
742 43 ▶ for (i = 0; i < o->nresps; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < o->nresps; i++) 23 20 43
743 23 av_push(ra, newSVsv(o->resps[i].status));
744 20 (void)hv_stores(h, "responses", newRV_noinc((SV *)ra));
745 20 RETVAL = newRV_noinc((SV *)h);
746 }
747 }
748 OUTPUT:
749 RETVAL
750  
751 # Route a (method, path) pair. List returns:
752 # matched: ($operationId, \%raw_path_captures)
753 # path, wrong method: (undef, \@allow) - a 405 with its Allow list
754 # no such path: () - a 404
755 void
756 match(self, method, path)
757 SV *self
758 SV *method
759 SV *path
760 PPCODE:
761 {
762 189 oa_api *a = oa_api_of(aTHX_ self);
763 STRLEN ml, pl;
764 189 ▶ const char *mp = SvPV_const(method, ml);
Branch: 1 branch
BranchTrueFalseTotal
const char *mp = SvPV_const(method, ml); 189 0 189
765 189 const char *pp = SvPV_const(path, pl);
766 189 HV *caps = (HV *)sv_2mortal((SV *)newHV());
767 189 AV *allow = (AV *)sv_2mortal((SV *)newAV());
768 189 oa_op *o = oa_route(aTHX_ a, mp, ml, pp, pl, caps, allow);
769 189 ▶ if (o) {
Branch: 1 branch
BranchTrueFalseTotal
if (o) { 137 52 189
770 137 ▶ mXPUSHs(newSVsv(o->op_id));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSVsv(o->op_id)); 0 137 137
771 137 ▶ mXPUSHs(newRV_inc((SV *)caps));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_inc((SV *)caps)); 0 137 137
772 52 ▶ } else if (av_len(allow) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (av_len(allow) >= 0) { 4 48 52
773 4 ▶ XPUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(&PL_sv_undef); 0 4 4
774 4 ▶ mXPUSHs(newRV_inc((SV *)allow));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_inc((SV *)allow)); 0 4 4
775 }
776 /* else: empty list = 404 */
777 }
778  
779 # Validate raw inputs for an operation. $raw is
780 # { path => \%captures, query => $string_or_hashref,
781 # header => \%lowercased, body => $raw_text_or_decoded_ref }
782 # List returns (1, \%params) or (0, \@errors).
783 void
784 validate_request(self, op_id, raw)
785 SV *self
786 SV *op_id
787 SV *raw
788 PPCODE:
789 {
790 5559 oa_api *a = oa_api_of(aTHX_ self);
791 5559 oa_op *o = oa_op_by_id(aTHX_ a, op_id);
792 5559 HV *rh, *rawpath = NULL, *headers = NULL;
793 5559 SV *query = NULL, *body = NULL, *e;
794 AV *errs;
795 5559 HV *params = NULL;
796 int ok;
797 5560 ▶ if (!o) croak("Open::API: unknown operationId '%s'", SvPV_nolen(op_id));
Branch: 1 branch
BranchTrueFalseTotal
if (!o) croak("Open::API: unknown operationId '%s'", SvPV_nolen(op_id)); 1 5558 5559
798 5558 rh = oa_hv_of(raw);
799 5558 ▶ if (!rh) croak("Open::API: validate_request needs a hashref of raw inputs");
Branch: 1 branch
BranchTrueFalseTotal
if (!rh) croak("Open::API: validate_request needs a hashref of raw inputs"); 0 5558 5558
800 5558 rawpath = oa_hv_of(oa_get(aTHX_ rh, "path"));
801 5558 headers = oa_hv_of(oa_get(aTHX_ rh, "header"));
802 5558 query = oa_get(aTHX_ rh, "query");
803 5558 body = oa_get(aTHX_ rh, "body");
804 5558 errs = (AV *)sv_2mortal((SV *)newAV());
805 5558 ok = oa_validate_op(aTHX_ a, o, rawpath, query, headers, body,
806 &params, errs);
807 PERL_UNUSED_VAR(e);
808 5558 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 3806 1752 5558
809 3806 ▶ mXPUSHs(newSViv(1));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSViv(1)); 0 3806 3806
810 3806 ▶ mXPUSHs(newRV_noinc((SV *)params));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)params)); 0 3806 3806
811 } else {
812 1752 ▶ mXPUSHs(newSViv(0));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSViv(0)); 0 1752 1752
813 1752 ▶ mXPUSHs(newRV_inc((SV *)errs));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_inc((SV *)errs)); 0 1752 1752
814 }
815 }
816  
817 # Validate straight from a PSGI environment.
818 #
819 # validate_request wants a lowercased header hash, and a caller in Perl
820 # can only build one by walking the whole environment - which measured at
821 # 3.3us per request in Maat, four times the cost of the routing and
822 # validation it was feeding. This does it in C, and only for the headers
823 # THIS operation declares (plus content-type and cookie), so a document
824 # that declares no header parameters costs two hash fetches rather than
825 # a loop over twenty environment keys.
826 #
827 # \%caps is the capture hash from match(); $body is the raw request body
828 # the caller has already read (it usually needs those bytes itself, so
829 # reading psgi.input here would only take them away).
830 void
831 validate_env(self, op_id, env, caps = NULL, body = NULL)
832 SV *self
833 SV *op_id
834 SV *env
835 SV *caps
836 SV *body
837 PPCODE:
838 {
839 0 oa_api *a = oa_api_of(aTHX_ self);
840 0 oa_op *o = oa_op_by_id(aTHX_ a, op_id);
841 HV *envhv, *headers, *rawpath;
842 SV *query;
843 AV *errs;
844 0 HV *params = NULL;
845 int ok;
846 0 ▶ if (!o) croak("Open::API: unknown operationId '%s'", SvPV_nolen(op_id));
Branch: 1 branch
BranchTrueFalseTotal
if (!o) croak("Open::API: unknown operationId '%s'", SvPV_nolen(op_id)); 0 0 0
847 0 envhv = oa_hv_of(env);
848 0 ▶ if (!envhv)
Branch: 1 branch
BranchTrueFalseTotal
if (!envhv) 0 0 0
849 0 croak("Open::API: validate_env needs the PSGI environment hashref");
850  
851 /* fresh, and ours: oa_validate_op copies whatever it keeps */
852 0 headers = oa_headers_for(aTHX_ o, envhv);
853 0 sv_2mortal((SV *)headers);
854  
855 0 ▶ rawpath = caps ? oa_hv_of(caps) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
rawpath = caps ? oa_hv_of(caps) : NULL; 0 0 0
856 0 query = oa_env_get(aTHX_ envhv, "QUERY_STRING");
857 0 errs = (AV *)sv_2mortal((SV *)newAV());
858 0 ▶ ok = oa_validate_op(aTHX_ a, o, rawpath, query, headers,
Branch: 1 branch
BranchTrueFalseTotal
ok = oa_validate_op(aTHX_ a, o, rawpath, query, headers, 0 0 0
859 0 ▶ (body && SvOK(body)) ? body : NULL, &params, errs);
Branch: 1 branch
BranchTrueFalseTotal
(body && SvOK(body)) ? body : NULL, &params, errs); 0 0 0
860 0 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 0 0 0
861 0 ▶ mXPUSHs(newSViv(1));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSViv(1)); 0 0 0
862 0 ▶ mXPUSHs(newRV_noinc((SV *)params));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)params)); 0 0 0
863 } else {
864 0 ▶ mXPUSHs(newSViv(0));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSViv(0)); 0 0 0
865 0 ▶ mXPUSHs(newRV_inc((SV *)errs));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_inc((SV *)errs)); 0 0 0
866 }
867 }
868  
869 # Weigh a response against the schema the document declares for its status.
870 # $trip is a PSGI triplet; it is never modified, whatever the verdict.
871 # \%opts: sample => 1-in-N (per operation, deterministic), max_body => bytes.
872 # Returns \@errors for a mismatch, undef for anything else - conforming,
873 # unsampled, or skipped. Which of those it was is in response_coverage.
874 SV *
875 check_response(self, op_id, trip, opts = NULL)
876 SV *self
877 SV *op_id
878 SV *trip
879 SV *opts
880 CODE:
881 {
882 1135 oa_api *a = oa_api_of(aTHX_ self);
883 1135 oa_op *o = oa_op_by_id(aTHX_ a, op_id);
884 oa_rvcfg rv;
885 1135 AV *errs = NULL;
886 SV *bad;
887 1135 ▶ if (!o) croak("Open::API: unknown operationId '%s'", SvPV_nolen(op_id));
Branch: 1 branch
BranchTrueFalseTotal
if (!o) croak("Open::API: unknown operationId '%s'", SvPV_nolen(op_id)); 0 1135 1135
888 1135 rv.mode = OA_RV_REPORT; rv.report = NULL; rv.sample = 1; rv.max_body = 0;
889 1135 ▶ if (opts && SvOK(opts)) {
Branch: 2 branches
BranchTrueFalseTotal
if (opts && SvOK(opts)) { 70 1065 1135
if (opts && SvOK(opts)) { 70 0 70
890 70 HV *oh = oa_hv_of(opts);
891 SV **v;
892 70 ▶ if (!oh) croak("Open::API: check_response options must be a hashref");
Branch: 1 branch
BranchTrueFalseTotal
if (!oh) croak("Open::API: check_response options must be a hashref"); 0 70 70
893 70 ▶ if ((v = hv_fetchs(oh, "sample", 0)) && *v && SvOK(*v))
Branch: 3 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(oh, "sample", 0)) && *v && SvOK(*v)) 0 70 70
if ((v = hv_fetchs(oh, "sample", 0)) && *v && SvOK(*v)) 0 0 0
if ((v = hv_fetchs(oh, "sample", 0)) && *v && SvOK(*v)) 0 0 0
894 0 rv.sample = SvIV(*v);
895 70 ▶ if ((v = hv_fetchs(oh, "max_body", 0)) && *v && SvOK(*v))
Branch: 3 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(oh, "max_body", 0)) && *v && SvOK(*v)) 0 70 70
if ((v = hv_fetchs(oh, "max_body", 0)) && *v && SvOK(*v)) 0 0 0
if ((v = hv_fetchs(oh, "max_body", 0)) && *v && SvOK(*v)) 0 0 0
896 0 rv.max_body = SvIV(*v);
897 }
898 1135 bad = oa_check_response_cfg(aTHX_ o, trip, &rv, NULL, &errs);
899 1135 ▶ if (bad) SvREFCNT_dec(bad); /* report mode never builds one */
Branch: 1 branch
BranchTrueFalseTotal
if (bad) SvREFCNT_dec(bad); /* report mode never builds one */ 0 1135 1135
900 1135 ▶ RETVAL = errs ? newRV_inc((SV *)errs) : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = errs ? newRV_inc((SV *)errs) : newSV(0); 37 1098 1135
901 }
902 OUTPUT:
903 RETVAL
904  
905 # Response-validation coverage. With an operationId, that operation's
906 # counters; without one, a hashref of every operation that has seen a
907 # response. Every response is either checked or skipped for exactly one
908 # named reason, so what was not looked at is a number, not a silent gap.
909 SV *
910 response_coverage(self, op_id = NULL)
911 SV *self
912 SV *op_id
913 CODE:
914 {
915 51 oa_api *a = oa_api_of(aTHX_ self);
916 51 oa_ops *t = (oa_ops *)a->ops;
917 101 ▶ if (op_id && SvOK(op_id)) {
Branch: 2 branches
BranchTrueFalseTotal
if (op_id && SvOK(op_id)) { 50 1 51
if (op_id && SvOK(op_id)) { 50 0 50
918 50 oa_op *o = oa_op_by_id(aTHX_ a, op_id);
919 50 ▶ if (!o) croak("Open::API: unknown operationId '%s'",
Branch: 1 branch
BranchTrueFalseTotal
if (!o) croak("Open::API: unknown operationId '%s'", 0 50 50
920 SvPV_nolen(op_id));
921 50 RETVAL = newRV_noinc((SV *)oa_rv_hv(aTHX_ &o->rv));
922 } else {
923 1 HV *out = newHV();
924 int i;
925 5 ▶ for (i = 0; t && i < t->n; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; t && i < t->n; i++) { 5 0 5
for (i = 0; t && i < t->n; i++) { 4 1 5
926 4 oa_op *o = &t->ops[i];
927 STRLEN kl; const char *kp;
928 4 ▶ if (!o->rv.total) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!o->rv.total) continue; 3 1 4
929 1 kp = SvPV_const(o->op_id, kl);
930 1 (void)hv_store(out, kp, (I32)kl,
931 newRV_noinc((SV *)oa_rv_hv(aTHX_ &o->rv)), 0);
932 }
933 1 RETVAL = newRV_noinc((SV *)out);
934 }
935 }
936 OUTPUT:
937 RETVAL
938  
939 # Run the operation's spec-declared security requirement against a PSGI
940 # environment, driving a caller-supplied { scheme-name => checker } map.
941 #
942 # This is the SAME compiled guards and the SAME checker-calling convention
943 # the Plack dispatch uses (oa_security_check), exposed so a request path
944 # that does its own routing - Maat's - enforces auth because the SPEC says
945 # so, rather than inventing a parallel check. The checker is called
946 # exactly as under to_app: ($credential, $env, $op_id, $scopes), the
947 # credential already extracted per the scheme's type/location.
948 #
949 # Returns undef when the request may proceed - INCLUDING an operation with
950 # no security requirement, which is the off-by-default case - or a PSGI
951 # response arrayref (401 unauthorized, or 500 for a checker die) when it
952 # is denied. On success the matched checker results are left in
953 # $env->{'openapi.auth'}, exactly as the Plack path leaves them.
954 SV *
955 check_op_security(self, op_id, env, checkers)
956 SV *self
957 SV *op_id
958 SV *env
959 SV *checkers
960 CODE:
961 {
962 13 oa_api *a = oa_api_of(aTHX_ self);
963 13 oa_op *o = oa_op_by_id(aTHX_ a, op_id);
964 13 ▶ if (!o) croak("Open::API: unknown operationId '%s'", SvPV_nolen(op_id));
Branch: 1 branch
BranchTrueFalseTotal
if (!o) croak("Open::API: unknown operationId '%s'", SvPV_nolen(op_id)); 0 13 13
965 13 ▶ if (!SvROK(env) || SvTYPE(SvRV(env)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(env) || SvTYPE(SvRV(env)) != SVt_PVHV) 13 0 13
if (!SvROK(env) || SvTYPE(SvRV(env)) != SVt_PVHV) 0 13 13
966 0 croak("Open::API: check_op_security needs the PSGI environment hashref");
967 13 ▶ if (!SvROK(checkers) || SvTYPE(SvRV(checkers)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(checkers) || SvTYPE(SvRV(checkers)) != SVt_PVHV) 13 0 13
if (!SvROK(checkers) || SvTYPE(SvRV(checkers)) != SVt_PVHV) 0 13 13
968 0 croak("Open::API: check_op_security needs a { scheme => checker } hashref");
969 13 ▶ if (o->nsec <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (o->nsec <= 0) { 1 12 13
970 1 RETVAL = &PL_sv_undef; /* no requirement: proceed */
971 } else {
972 12 SV *denied = oa_security_check(aTHX_ a, o, env,
973 12 (HV *)SvRV(checkers));
974 12 ▶ RETVAL = denied ? denied : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = denied ? denied : &PL_sv_undef; 5 7 12
975 }
976 }
977 OUTPUT:
978 RETVAL
979  
980 # The resolved contract for one operation: what will actually be checked,
981 # after $ref resolution and compilation. `maat explain` prints this.
982 SV *
983 operation_info(self, op_id)
984 SV *self
985 SV *op_id
986 CODE:
987 {
988 46 oa_api *a = oa_api_of(aTHX_ self);
989 46 oa_op *o = oa_op_by_id(aTHX_ a, op_id);
990 HV *out;
991 AV *av;
992 int i, loc;
993 static const char *const locname[OA_IN_N] = {
994 "path", "query", "header", "cookie"
995 };
996 46 ▶ if (!o) croak("Open::API: unknown operationId '%s'", SvPV_nolen(op_id));
Branch: 1 branch
BranchTrueFalseTotal
if (!o) croak("Open::API: unknown operationId '%s'", SvPV_nolen(op_id)); 0 46 46
997 46 out = newHV();
998 46 (void)hv_stores(out, "operationId", newSVsv(o->op_id));
999 46 (void)hv_stores(out, "method", newSVsv(o->method));
1000 46 (void)hv_stores(out, "path", newSVsv(o->path));
1001  
1002 46 av = newAV();
1003 230 ▶ for (loc = 0; loc < OA_IN_N; loc++) {
Branch: 1 branch
BranchTrueFalseTotal
for (loc = 0; loc < OA_IN_N; loc++) { 184 46 230
1004 229 ▶ for (i = 0; i < o->nparams[loc]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < o->nparams[loc]; i++) { 45 184 229
1005 45 HV *p = newHV();
1006 45 (void)hv_stores(p, "name", newSVsv(o->params[loc][i].name));
1007 45 (void)hv_stores(p, "in", newSVpv(locname[loc], 0));
1008 45 (void)hv_stores(p, "required",
1009 newSViv(o->params[loc][i].required));
1010 45 (void)hv_stores(p, "schema",
1011 newSViv(o->params[loc][i].handle ? 1 : 0));
1012 45 (void)hv_stores(p, "deprecated",
1013 newSViv(o->params[loc][i].deprecated));
1014 45 av_push(av, newRV_noinc((SV *)p));
1015 }
1016 }
1017 46 (void)hv_stores(out, "parameters", newRV_noinc((SV *)av));
1018  
1019 46 av = newAV();
1020 54 ▶ for (i = 0; i < o->nbodies; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < o->nbodies; i++) { 8 46 54
1021 8 HV *b = newHV();
1022 8 (void)hv_stores(b, "content_type", newSVsv(o->bodies[i].ctype));
1023 8 (void)hv_stores(b, "validated",
1024 newSViv(o->bodies[i].handle ? 1 : 0));
1025 8 av_push(av, newRV_noinc((SV *)b));
1026 }
1027 46 (void)hv_stores(out, "body", newRV_noinc((SV *)av));
1028 46 (void)hv_stores(out, "body_required", newSViv(o->body_required));
1029  
1030 46 av = newAV();
1031 105 ▶ for (i = 0; i < o->nresps; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < o->nresps; i++) { 59 46 105
1032 59 HV *r = newHV();
1033 59 (void)hv_stores(r, "status", newSVsv(o->resps[i].status));
1034 59 (void)hv_stores(r, "content_type", newSVsv(o->resps[i].ctype));
1035 59 (void)hv_stores(r, "validated",
1036 newSViv(o->resps[i].handle ? 1 : 0));
1037 59 av_push(av, newRV_noinc((SV *)r));
1038 }
1039 46 (void)hv_stores(out, "responses", newRV_noinc((SV *)av));
1040 46 (void)hv_stores(out, "secured", newSViv(o->nsec ? 1 : 0));
1041 46 RETVAL = newRV_noinc((SV *)out);
1042 }
1043 OUTPUT:
1044 RETVAL
1045  
1046 # What the document SAYS about an operation, as against what the validator
1047 # made of it: summary, description, tags, deprecated and any x- extensions.
1048 # Returns undef for an unknown operationId. Phrased that way on purpose: in
1049 # XS a comment starting "# undef" IS an #undef directive, and cpp was running
1050 # it. Never start one of these with a directive keyword.
1051 SV *
1052 operation_doc(self, op_id)
1053 SV *self
1054 SV *op_id
1055 CODE:
1056 {
1057 4 oa_api *a = oa_api_of(aTHX_ self);
1058 4 SV *r = oa_tools_doc(aTHX_ a, op_id);
1059 4 ▶ RETVAL = r ? r : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = r ? r : newSV(0); 1 3 4
1060 }
1061 OUTPUT:
1062 RETVAL
1063  
1064 # Every input an operation declares, as one JSON Schema object: a property per
1065 # parameter, plus `body` for a JSON request body. undef for an unknown
1066 # operationId. 3.1 schemas are 2020-12 already, so this compiles as it stands.
1067 SV *
1068 operation_schema(self, op_id)
1069 SV *self
1070 SV *op_id
1071 CODE:
1072 {
1073 51 oa_api *a = oa_api_of(aTHX_ self);
1074 51 SV *r = oa_tools_schema(aTHX_ a, op_id);
1075 51 ▶ RETVAL = r ? r : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = r ? r : newSV(0); 1 50 51
1076 }
1077 OUTPUT:
1078 RETVAL
1079  
1080 # Synthesize a response for an operation from the document alone: a PSGI
1081 # triplet whose body follows the precedence in oa_mock.h (media example,
1082 # named/first examples entry, schema default, generated from the schema).
1083 # Deterministic - the same arguments give byte-identical bodies.
1084 #
1085 # \%opts:
1086 # code => 404 select a status ('default' body when undeclared)
1087 # example => 'name' select a named example
1088 # prefer => $header a raw Prefer header; code=/example= tokens are
1089 # honoured where the explicit keys are absent
1090 SV *
1091 synthesize(self, op_id, opts = NULL)
1092 SV *self
1093 SV *op_id
1094 SV *opts
1095 CODE:
1096 {
1097 1005 oa_api *a = oa_api_of(aTHX_ self);
1098 1005 oa_op *o = oa_op_by_id(aTHX_ a, op_id);
1099 1005 int want_code = 0;
1100 1005 SV *want_name = NULL;
1101 1005 HV *caps = NULL;
1102 1005 ▶ if (!o) croak("Open::API: unknown operationId '%s'", SvPV_nolen(op_id));
Branch: 1 branch
BranchTrueFalseTotal
if (!o) croak("Open::API: unknown operationId '%s'", SvPV_nolen(op_id)); 0 1005 1005
1103 1005 ▶ if (opts && SvOK(opts)) {
Branch: 2 branches
BranchTrueFalseTotal
if (opts && SvOK(opts)) { 49 956 1005
if (opts && SvOK(opts)) { 49 0 49
1104 49 HV *oh = oa_hv_of(opts);
1105 SV **v;
1106 49 ▶ if (!oh) croak("Open::API: synthesize options must be a hashref");
Branch: 1 branch
BranchTrueFalseTotal
if (!oh) croak("Open::API: synthesize options must be a hashref"); 0 49 49
1107 49 ▶ if ((v = hv_fetchs(oh, "code", 0)) && *v && SvOK(*v))
Branch: 3 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(oh, "code", 0)) && *v && SvOK(*v)) 33 16 49
if ((v = hv_fetchs(oh, "code", 0)) && *v && SvOK(*v)) 33 0 33
if ((v = hv_fetchs(oh, "code", 0)) && *v && SvOK(*v)) 33 0 33
1108 33 want_code = (int)SvIV(*v);
1109 49 ▶ if ((v = hv_fetchs(oh, "example", 0)) && *v && SvOK(*v))
Branch: 3 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(oh, "example", 0)) && *v && SvOK(*v)) 1 48 49
if ((v = hv_fetchs(oh, "example", 0)) && *v && SvOK(*v)) 1 0 1
if ((v = hv_fetchs(oh, "example", 0)) && *v && SvOK(*v)) 1 0 1
1110 1 want_name = *v;
1111 49 ▶ if ((v = hv_fetchs(oh, "prefer", 0)) && *v && SvOK(*v)) {
Branch: 3 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(oh, "prefer", 0)) && *v && SvOK(*v)) { 5 44 49
if ((v = hv_fetchs(oh, "prefer", 0)) && *v && SvOK(*v)) { 5 0 5
if ((v = hv_fetchs(oh, "prefer", 0)) && *v && SvOK(*v)) { 5 0 5
1112 5 ▶ if (!want_code) {
Branch: 1 branch
BranchTrueFalseTotal
if (!want_code) { 5 0 5
1113 5 SV *t = oa_prefer_token(aTHX_ *v, "code", 4);
1114 5 ▶ if (t && looks_like_number(t)) want_code = (int)SvIV(t);
Branch: 2 branches
BranchTrueFalseTotal
if (t && looks_like_number(t)) want_code = (int)SvIV(t); 3 2 5
if (t && looks_like_number(t)) want_code = (int)SvIV(t); 3 0 3
1115 }
1116 5 ▶ if (!want_name)
Branch: 1 branch
BranchTrueFalseTotal
if (!want_name) 5 0 5
1117 5 want_name = oa_prefer_token(aTHX_ *v, "example", 7);
1118 }
1119 /* The request's path captures. A property the response schema
1120 * names, and the request has already answered, is answered
1121 * with what the request said - see oa_gen_echo. */
1122 49 ▶ if ((v = hv_fetchs(oh, "path", 0)) && *v && SvROK(*v)
Branch: 3 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(oh, "path", 0)) && *v && SvROK(*v) 6 43 49
if ((v = hv_fetchs(oh, "path", 0)) && *v && SvROK(*v) 6 0 6
if ((v = hv_fetchs(oh, "path", 0)) && *v && SvROK(*v) 6 0 6
1123 6 ▶ && SvTYPE(SvRV(*v)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(*v)) == SVt_PVHV) 6 0 6
1124 6 caps = (HV *)SvRV(*v);
1125 49 ▶ if (want_code && (want_code < 100 || want_code > 599))
Branch: 3 branches
BranchTrueFalseTotal
if (want_code && (want_code < 100 || want_code > 599)) 36 13 49
if (want_code && (want_code < 100 || want_code > 599)) 36 0 36
if (want_code && (want_code < 100 || want_code > 599)) 1 35 36
1126 1 want_code = 0; /* junk in Prefer: ignore it */
1127 }
1128 1005 RETVAL = oa_synthesize(aTHX_ a, o, want_code, want_name, caps);
1129 }
1130 OUTPUT:
1131 RETVAL
1132  
1133 MODULE = Open::API PACKAGE = Open::API::Plack
1134  
1135 # Open::API::Plack->new(api => $api | spec => ..., handlers => {...},
1136 # before => ..., after => ..., security => {...}, csrf => {...},
1137 # headers => {...}, cors => {...}, max_body_size => N, negotiate => 0|1,
1138 # error_format => 'json'|'problem', validate_responses => 0|1).
1139 # Configuration only: options accumulate here and via the accessors, and
1140 # everything is resolved, validated and compiled by to_app.
1141 SV *
1142 new(class, ...)
1143 SV *class
1144 CODE:
1145 {
1146 0 ▶ const char *cls = (SvROK(class) && SvOBJECT(SvRV(class)))
Branch: 1 branch
BranchTrueFalseTotal
const char *cls = (SvROK(class) && SvOBJECT(SvRV(class))) 0 0 0
1147 1160 ▶ ? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class);
Branch: 7 branches
BranchTrueFalseTotal
? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); 0 580 580
? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); 0 0 0
? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); 0 0 0
? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); 0 0 0
? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); 0 0 0
? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); 0 0 0
? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); 0 0 0
1148 580 HV *cfg = (HV *)sv_2mortal((SV *)newHV()); /* mortal until blessed */
1149 int i;
1150 1301 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 722 579 1301
1151 722 STRLEN kl; const char *k = SvPV_const(ST(i), kl);
1152 722 oa_plack_set(aTHX_ cfg, k, kl, ST(i + 1));
1153 }
1154 579 RETVAL = sv_bless(newRV_inc((SV *)cfg), gv_stashpv(cls, GV_ADD));
1155 }
1156 OUTPUT:
1157 RETVAL
1158  
1159 # $plack->handlers the live map hashref (created on first use)
1160 # $plack->handlers(%pairs) merge, return $plack (chainable)
1161 # $plack->handlers(\%map) merge, return $plack
1162 # `security` is the same accessor for the scheme => checker map.
1163 SV *
1164 handlers(self, ...)
1165 SV *self
1166 ALIAS:
1167 security = 1
1168 CODE:
1169 {
1170 1009 HV *cfg = oa_plack_hv(aTHX_ self);
1171 1009 ▶ const char *key = ix ? "security" : "handlers";
Branch: 1 branch
BranchTrueFalseTotal
const char *key = ix ? "security" : "handlers"; 501 508 1009
1172 1009 ▶ if (items == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 1) { 2 1007 1009
1173 2 RETVAL = newRV_inc((SV *)oa_plack_map(aTHX_ cfg, key, 8));
1174 1007 ▶ } else if (items == 2) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (items == 2) { 501 506 1007
1175 501 oa_plack_set(aTHX_ cfg, key, 8, ST(1));
1176 501 RETVAL = newSVsv(self);
1177 } else {
1178 506 HV *dst = oa_plack_map(aTHX_ cfg, key, 8);
1179 int i;
1180 1013 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 507 506 1013
1181 507 STRLEN kl; const char *k = SvPV_const(ST(i), kl);
1182 507 (void)hv_store(dst, k, (I32)kl, newSVsv(ST(i + 1)), 0);
1183 }
1184 506 RETVAL = newSVsv(self);
1185 }
1186 }
1187 OUTPUT:
1188 RETVAL
1189  
1190 # Get/set accessors for the remaining options; setters return $plack so
1191 # calls chain. `spec` compiles the document into `api` right away, so both
1192 # its getter and `api`'s return the Open::API.
1193 SV *
1194 before(self, ...)
1195 SV *self
1196 ALIAS:
1197 after = 1
1198 csrf = 2
1199 cors = 3
1200 headers = 4
1201 max_body_size = 5
1202 negotiate = 6
1203 error_format = 7
1204 validate_responses = 8
1205 api = 9
1206 spec = 10
1207 ui = 11
1208 CODE:
1209 {
1210 static const char *const keys[] = {
1211 "before", "after", "csrf", "cors", "headers", "max_body_size",
1212 "negotiate", "error_format", "validate_responses", "api", "spec",
1213 "ui"
1214 };
1215 541 HV *cfg = oa_plack_hv(aTHX_ self);
1216 541 const char *k = keys[ix];
1217 541 ▶ if (items == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 1) { 34 507 541
1218 23 ▶ SV **e = (ix == 9 || ix == 10)
Branch: 1 branch
BranchTrueFalseTotal
SV **e = (ix == 9 || ix == 10) 0 23 23
1219 11 ? hv_fetchs(cfg, "api", 0)
1220 57 ▶ : hv_fetch(cfg, k, (I32)strlen(k), 0);
Branch: 1 branch
BranchTrueFalseTotal
: hv_fetch(cfg, k, (I32)strlen(k), 0); 23 11 34
1221 34 ▶ RETVAL = (e && *e) ? newSVsv(*e) : newSV(0);
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (e && *e) ? newSVsv(*e) : newSV(0); 25 9 34
RETVAL = (e && *e) ? newSVsv(*e) : newSV(0); 25 0 25
1222 } else {
1223 507 oa_plack_set(aTHX_ cfg, k, strlen(k), ST(1));
1224 507 RETVAL = newSVsv(self);
1225 }
1226 }
1227 OUTPUT:
1228 RETVAL
1229  
1230 # Finalise: compile the stored configuration into the PSGI app coderef.
1231 # Handler names and hooks resolve here (a typo croaks at startup, not per
1232 # request), the spec's security coverage is checked, csrf / cors / header
1233 # options are normalised, and the closure is built. The request path is
1234 # entirely C: route -> validate -> dispatch -> respond (see oa_plack.h).
1235 SV *
1236 to_app(self)
1237 SV *self
1238 CODE:
1239 {
1240 583 HV *cfg = oa_plack_hv(aTHX_ self);
1241 583 SV *api = NULL, *handlers = NULL, *before = NULL, *after = NULL;
1242 583 SV *security = NULL, *csrf = NULL;
1243 583 SV *headers_opt = NULL, *cors_opt = NULL, *errfmt_opt = NULL;
1244 583 IV maxbody = 0;
1245 583 int negotiate = 0, vresp = 0;
1246 583 SV *rv_report = NULL;
1247 583 IV rv_sample = 0, rv_maxbody = 0;
1248 AV *cap;
1249 oa_api *a;
1250 SV **e;
1251 583 ▶ if ((e = hv_fetchs(cfg, "api", 0)) && *e && SvOK(*e)) api = *e;
Branch: 3 branches
BranchTrueFalseTotal
if ((e = hv_fetchs(cfg, "api", 0)) && *e && SvOK(*e)) api = *e; 582 1 583
if ((e = hv_fetchs(cfg, "api", 0)) && *e && SvOK(*e)) api = *e; 582 0 582
if ((e = hv_fetchs(cfg, "api", 0)) && *e && SvOK(*e)) api = *e; 582 0 582
1252 583 ▶ if ((e = hv_fetchs(cfg, "handlers", 0)) && *e && SvOK(*e)) handlers = *e;
Branch: 3 branches
BranchTrueFalseTotal
if ((e = hv_fetchs(cfg, "handlers", 0)) && *e && SvOK(*e)) handlers = *e; 581 2 583
if ((e = hv_fetchs(cfg, "handlers", 0)) && *e && SvOK(*e)) handlers = *e; 581 0 581
if ((e = hv_fetchs(cfg, "handlers", 0)) && *e && SvOK(*e)) handlers = *e; 581 0 581
1253 583 ▶ if ((e = hv_fetchs(cfg, "before", 0)) && *e && SvOK(*e)) before = *e;
Branch: 3 branches
BranchTrueFalseTotal
if ((e = hv_fetchs(cfg, "before", 0)) && *e && SvOK(*e)) before = *e; 6 577 583
if ((e = hv_fetchs(cfg, "before", 0)) && *e && SvOK(*e)) before = *e; 6 0 6
if ((e = hv_fetchs(cfg, "before", 0)) && *e && SvOK(*e)) before = *e; 6 0 6
1254 583 ▶ if ((e = hv_fetchs(cfg, "after", 0)) && *e && SvOK(*e)) after = *e;
Branch: 3 branches
BranchTrueFalseTotal
if ((e = hv_fetchs(cfg, "after", 0)) && *e && SvOK(*e)) after = *e; 7 576 583
if ((e = hv_fetchs(cfg, "after", 0)) && *e && SvOK(*e)) after = *e; 7 0 7
if ((e = hv_fetchs(cfg, "after", 0)) && *e && SvOK(*e)) after = *e; 7 0 7
1255 583 ▶ if ((e = hv_fetchs(cfg, "security", 0)) && *e && SvOK(*e)) security = *e;
Branch: 3 branches
BranchTrueFalseTotal
if ((e = hv_fetchs(cfg, "security", 0)) && *e && SvOK(*e)) security = *e; 502 81 583
if ((e = hv_fetchs(cfg, "security", 0)) && *e && SvOK(*e)) security = *e; 502 0 502
if ((e = hv_fetchs(cfg, "security", 0)) && *e && SvOK(*e)) security = *e; 502 0 502
1256 583 ▶ if ((e = hv_fetchs(cfg, "csrf", 0)) && *e) csrf = *e;
Branch: 2 branches
BranchTrueFalseTotal
if ((e = hv_fetchs(cfg, "csrf", 0)) && *e) csrf = *e; 10 573 583
if ((e = hv_fetchs(cfg, "csrf", 0)) && *e) csrf = *e; 10 0 10
1257 583 ▶ if ((e = hv_fetchs(cfg, "headers", 0)) && *e && SvOK(*e)) headers_opt = *e;
Branch: 3 branches
BranchTrueFalseTotal
if ((e = hv_fetchs(cfg, "headers", 0)) && *e && SvOK(*e)) headers_opt = *e; 1 582 583
if ((e = hv_fetchs(cfg, "headers", 0)) && *e && SvOK(*e)) headers_opt = *e; 1 0 1
if ((e = hv_fetchs(cfg, "headers", 0)) && *e && SvOK(*e)) headers_opt = *e; 1 0 1
1258 583 ▶ if ((e = hv_fetchs(cfg, "cors", 0)) && *e && SvOK(*e)) cors_opt = *e;
Branch: 3 branches
BranchTrueFalseTotal
if ((e = hv_fetchs(cfg, "cors", 0)) && *e && SvOK(*e)) cors_opt = *e; 4 579 583
if ((e = hv_fetchs(cfg, "cors", 0)) && *e && SvOK(*e)) cors_opt = *e; 4 0 4
if ((e = hv_fetchs(cfg, "cors", 0)) && *e && SvOK(*e)) cors_opt = *e; 4 0 4
1259 583 ▶ if ((e = hv_fetchs(cfg, "error_format", 0)) && *e && SvOK(*e))
Branch: 3 branches
BranchTrueFalseTotal
if ((e = hv_fetchs(cfg, "error_format", 0)) && *e && SvOK(*e)) 1 582 583
if ((e = hv_fetchs(cfg, "error_format", 0)) && *e && SvOK(*e)) 1 0 1
if ((e = hv_fetchs(cfg, "error_format", 0)) && *e && SvOK(*e)) 1 0 1
1260 1 errfmt_opt = *e;
1261 583 ▶ if ((e = hv_fetchs(cfg, "max_body_size", 0)) && *e && SvOK(*e))
Branch: 3 branches
BranchTrueFalseTotal
if ((e = hv_fetchs(cfg, "max_body_size", 0)) && *e && SvOK(*e)) 501 82 583
if ((e = hv_fetchs(cfg, "max_body_size", 0)) && *e && SvOK(*e)) 501 0 501
if ((e = hv_fetchs(cfg, "max_body_size", 0)) && *e && SvOK(*e)) 501 0 501
1262 501 maxbody = SvIV(*e);
1263 583 ▶ if ((e = hv_fetchs(cfg, "negotiate", 0)) && *e)
Branch: 2 branches
BranchTrueFalseTotal
if ((e = hv_fetchs(cfg, "negotiate", 0)) && *e) 1 582 583
if ((e = hv_fetchs(cfg, "negotiate", 0)) && *e) 1 0 1
1264 1 negotiate = SvTRUE(*e) ? 1 : 0;
1265 /* validate_responses: 0 | 1 (replace with a 500 - the dev tool) |
1266 * 'report' | a hashref { mode, report, sample, max_body }. The
1267 * scalar forms are unchanged from 0.06. */
1268 583 ▶ if ((e = hv_fetchs(cfg, "validate_responses", 0)) && *e && SvOK(*e)) {
Branch: 3 branches
BranchTrueFalseTotal
if ((e = hv_fetchs(cfg, "validate_responses", 0)) && *e && SvOK(*e)) { 30 553 583
if ((e = hv_fetchs(cfg, "validate_responses", 0)) && *e && SvOK(*e)) { 30 0 30
if ((e = hv_fetchs(cfg, "validate_responses", 0)) && *e && SvOK(*e)) { 30 0 30
1269 30 SV *v = *e;
1270 25 ▶ HV *rh = (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
HV *rh = (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) 25 0 25
1271 55 ▶ ? (HV *)SvRV(v) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(v) : NULL; 25 5 30
1272 30 SV *modesv = v;
1273 SV **x;
1274 30 ▶ if (rh) {
Branch: 1 branch
BranchTrueFalseTotal
if (rh) { 25 5 30
1275 50 ▶ modesv = (x = hv_fetchs(rh, "mode", 0)) && *x && SvOK(*x)
Branch: 2 branches
BranchTrueFalseTotal
modesv = (x = hv_fetchs(rh, "mode", 0)) && *x && SvOK(*x) 25 0 25
modesv = (x = hv_fetchs(rh, "mode", 0)) && *x && SvOK(*x) 25 0 25
1276 50 ▶ ? *x : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? *x : NULL; 25 0 25
1277 25 ▶ if ((x = hv_fetchs(rh, "report", 0)) && *x && SvOK(*x)) {
Branch: 3 branches
BranchTrueFalseTotal
if ((x = hv_fetchs(rh, "report", 0)) && *x && SvOK(*x)) { 25 0 25
if ((x = hv_fetchs(rh, "report", 0)) && *x && SvOK(*x)) { 25 0 25
if ((x = hv_fetchs(rh, "report", 0)) && *x && SvOK(*x)) { 25 0 25
1278 25 rv_report = oa_cv_or_name(aTHX_ *x,
1279 "validate_responses 'report' callback");
1280 /* a report callback means report mode unless told
1281 * otherwise - nobody passes one to get a 500 */
1282 25 ▶ if (!modesv) vresp = 2;
Branch: 1 branch
BranchTrueFalseTotal
if (!modesv) vresp = 2; 0 25 25
1283 }
1284 25 ▶ if ((x = hv_fetchs(rh, "sample", 0)) && *x && SvOK(*x))
Branch: 3 branches
BranchTrueFalseTotal
if ((x = hv_fetchs(rh, "sample", 0)) && *x && SvOK(*x)) 1 24 25
if ((x = hv_fetchs(rh, "sample", 0)) && *x && SvOK(*x)) 1 0 1
if ((x = hv_fetchs(rh, "sample", 0)) && *x && SvOK(*x)) 1 0 1
1285 1 rv_sample = SvIV(*x);
1286 25 ▶ if ((x = hv_fetchs(rh, "max_body", 0)) && *x && SvOK(*x))
Branch: 3 branches
BranchTrueFalseTotal
if ((x = hv_fetchs(rh, "max_body", 0)) && *x && SvOK(*x)) 2 23 25
if ((x = hv_fetchs(rh, "max_body", 0)) && *x && SvOK(*x)) 2 0 2
if ((x = hv_fetchs(rh, "max_body", 0)) && *x && SvOK(*x)) 2 0 2
1287 2 rv_maxbody = SvIV(*x);
1288 }
1289 30 ▶ if (modesv) {
Branch: 1 branch
BranchTrueFalseTotal
if (modesv) { 30 0 30
1290 56 ▶ if (SvPOK(modesv) && !looks_like_number(modesv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvPOK(modesv) && !looks_like_number(modesv)) { 27 3 30
if (SvPOK(modesv) && !looks_like_number(modesv)) { 27 0 27
1291 27 STRLEN sl; const char *sp = SvPV_const(modesv, sl);
1292 27 ▶ if (sl == 6 && memEQ(sp, "report", 6)) vresp = 2;
Branch: 2 branches
BranchTrueFalseTotal
if (sl == 6 && memEQ(sp, "report", 6)) vresp = 2; 26 1 27
if (sl == 6 && memEQ(sp, "report", 6)) vresp = 2; 26 0 26
1293 1 ▶ else if (sl == 7 && memEQ(sp, "replace", 7)) vresp = 1;
Branch: 2 branches
BranchTrueFalseTotal
else if (sl == 7 && memEQ(sp, "replace", 7)) vresp = 1; 0 1 1
else if (sl == 7 && memEQ(sp, "replace", 7)) vresp = 1; 0 0 0
1294 1 ▶ else if (sl == 3 && memEQ(sp, "off", 3)) vresp = 0;
Branch: 2 branches
BranchTrueFalseTotal
else if (sl == 3 && memEQ(sp, "off", 3)) vresp = 0; 0 1 1
else if (sl == 3 && memEQ(sp, "off", 3)) vresp = 0; 0 0 0
1295 1 else croak("Open::API::Plack->to_app: validate_responses "
1296 "must be 0, 1, 'replace', 'report' or a hashref "
1297 "- not '%.*s'", (int)sl, sp);
1298 } else {
1299 3 vresp = SvTRUE(modesv) ? 1 : 0;
1300 }
1301 }
1302 29 ▶ if (vresp == 2 && !rv_report)
Branch: 2 branches
BranchTrueFalseTotal
if (vresp == 2 && !rv_report) 26 3 29
if (vresp == 2 && !rv_report) 1 25 26
1303 1 croak("Open::API::Plack->to_app: validate_responses => 'report' "
1304 "needs somewhere to report to - pass "
1305 "validate_responses => { mode => 'report', "
1306 "report => sub {...} }");
1307 }
1308 581 ▶ if (!api)
Branch: 1 branch
BranchTrueFalseTotal
if (!api) 1 580 581
1309 1 croak("Open::API::Plack->to_app: give 'api' or 'spec'");
1310 580 a = oa_api_of(aTHX_ api);
1311 { /* normalise: values are coderefs, or fully qualified sub names
1312 * ('MyApp::listPets') resolved right here - a typo croaks at
1313 * to_app time, not per request. No registered handlers is legal
1314 * (every matched operation answers 501). */
1315 580 ▶ HV *src = handlers ? oa_hv_of(handlers) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HV *src = handlers ? oa_hv_of(handlers) : NULL; 579 1 580
1316 580 HV *dst = newHV();
1317 HE *he;
1318 580 ▶ if (handlers && !src)
Branch: 2 branches
BranchTrueFalseTotal
if (handlers && !src) 579 1 580
if (handlers && !src) 0 579 579
1319 0 croak("Open::API::Plack->to_app: 'handlers' must be a hashref");
1320 580 ▶ if (src) {
Branch: 1 branch
BranchTrueFalseTotal
if (src) { 579 1 580
1321 579 hv_iterinit(src);
1322 1227 ▶ while ((he = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) { 650 577 1227
1323 650 I32 kl; const char *k = hv_iterkey(he, &kl);
1324 650 SV *v = hv_iterval(src, he);
1325 650 ▶ if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVCV) {
Branch: 3 branches
BranchTrueFalseTotal
if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVCV) { 650 0 650
if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVCV) { 648 2 650
if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVCV) { 647 1 648
1326 647 (void)hv_store(dst, k, kl, newSVsv(v), 0);
1327 4 ▶ } else if (v && SvOK(v) && !SvROK(v)) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (v && SvOK(v) && !SvROK(v)) { 3 0 3
} else if (v && SvOK(v) && !SvROK(v)) { 3 0 3
} else if (v && SvOK(v) && !SvROK(v)) { 2 1 3
1328 2 STRLEN nl; const char *np = SvPV_const(v, nl);
1329 2 CV *cv = get_cv(np, 0);
1330 2 ▶ if (!cv)
Branch: 1 branch
BranchTrueFalseTotal
if (!cv) 1 1 2
1331 1 croak("Open::API::Plack->to_app: handler for '%.*s' "
1332 "names no such sub '%.*s'",
1333 (int)kl, k, (int)nl, np);
1334 1 (void)hv_store(dst, k, kl, newRV_inc((SV *)cv), 0);
1335 } else {
1336 1 croak("Open::API::Plack->to_app: handler for '%.*s' must "
1337 "be a coderef or a fully qualified sub name",
1338 (int)kl, k);
1339 }
1340 }
1341 }
1342 578 handlers = sv_2mortal(newRV_noinc((SV *)dst));
1343 }
1344 { /* security checkers: resolve each (coderef or sub name) and
1345 * verify at startup that every scheme any operation's
1346 * requirements reference has a checker - a missing one is a
1347 * config error now, not a 500 later. */
1348 578 HV *seccb = newHV();
1349 578 oa_ops *t = (oa_ops *)a->ops;
1350 578 ▶ if (security) {
Branch: 1 branch
BranchTrueFalseTotal
if (security) { 502 76 578
1351 502 HV *src = oa_hv_of(security);
1352 HE *he;
1353 502 ▶ if (!src) croak("Open::API::Plack->to_app: 'security' must be a "
Branch: 1 branch
BranchTrueFalseTotal
if (!src) croak("Open::API::Plack->to_app: 'security' must be a " 0 502 502
1354 "hashref of scheme => checker");
1355 502 hv_iterinit(src);
1356 504 ▶ while ((he = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) { 2 502 504
1357 2 I32 kl; const char *k = hv_iterkey(he, &kl);
1358 char what[160];
1359 2 my_snprintf(what, sizeof what,
1360 "security checker for '%.*s'", (int)kl, k);
1361 2 (void)hv_store(seccb, k, kl,
1362 oa_cv_or_name(aTHX_ hv_iterval(src, he), what), 0);
1363 }
1364 }
1365 578 ▶ if (t) {
Branch: 1 branch
BranchTrueFalseTotal
if (t) { 578 0 578
1366 int oi, ai, ii;
1367 2838 ▶ for (oi = 0; oi < t->n; oi++) {
Branch: 1 branch
BranchTrueFalseTotal
for (oi = 0; oi < t->n; oi++) { 2261 577 2838
1368 2261 oa_op *o = &t->ops[oi];
1369 2263 ▶ for (ai = 0; ai < o->nsec; ai++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ai = 0; ai < o->nsec; ai++) { 3 2260 2263
1370 5 ▶ for (ii = 0; ii < o->sec[ai].n; ii++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ii = 0; ii < o->sec[ai].n; ii++) { 3 2 5
1371 3 oa_scheme *s =
1372 3 &t->schemes[o->sec[ai].items[ii].scheme];
1373 3 STRLEN nl; const char *np = SvPV_const(s->name, nl);
1374 3 ▶ if (!hv_fetch(seccb, np, (I32)nl, 0))
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_fetch(seccb, np, (I32)nl, 0)) 1 2 3
1375 2 croak("Open::API::Plack->to_app: operation '%s' "
1376 "requires securityScheme '%.*s' but no "
1377 "checker was given (security => { '%.*s' "
1378 "=> sub { ... } })",
1379 SvPV_nolen(o->op_id), (int)nl, np,
1380 (int)nl, np);
1381 }
1382 }
1383 }
1384 }
1385 577 cap = newAV();
1386 577 av_push(cap, SvREFCNT_inc(api));
1387 577 av_push(cap, SvREFCNT_inc(handlers));
1388 577 av_push(cap, newSViv(vresp));
1389 577 ▶ av_push(cap, before ? oa_cv_or_name(aTHX_ before, "'before' hook")
Branch: 1 branch
BranchTrueFalseTotal
av_push(cap, before ? oa_cv_or_name(aTHX_ before, "'before' hook") 6 571 577
1390 : newSV(0));
1391 576 ▶ av_push(cap, after ? oa_cv_or_name(aTHX_ after, "'after' hook")
Branch: 1 branch
BranchTrueFalseTotal
av_push(cap, after ? oa_cv_or_name(aTHX_ after, "'after' hook") 7 569 576
1392 : newSV(0));
1393 576 av_push(cap, newRV_noinc((SV *)seccb));
1394 }
1395 { /* csrf: normalise into a config HV (origins AV, check callback,
1396 * cookie + header names) once, so the request path only reads it.
1397 * undef when no csrf option was given. */
1398 576 SV *ccfg = NULL;
1399 576 ▶ if (csrf && SvOK(csrf)) {
Branch: 2 branches
BranchTrueFalseTotal
if (csrf && SvOK(csrf)) { 10 566 576
if (csrf && SvOK(csrf)) { 10 0 10
1400 10 HV *src = oa_hv_of(csrf);
1401 HV *dst;
1402 SV **v;
1403 10 ▶ if (!src) croak("Open::API::Plack->to_app: 'csrf' must be a hashref");
Branch: 1 branch
BranchTrueFalseTotal
if (!src) croak("Open::API::Plack->to_app: 'csrf' must be a hashref"); 0 10 10
1404 10 dst = newHV();
1405 10 ▶ if ((v = hv_fetchs(src, "origins", 0)) && *v && SvROK(*v)
Branch: 3 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(src, "origins", 0)) && *v && SvROK(*v) 7 3 10
if ((v = hv_fetchs(src, "origins", 0)) && *v && SvROK(*v) 7 0 7
if ((v = hv_fetchs(src, "origins", 0)) && *v && SvROK(*v) 7 0 7
1406 7 ▶ && SvTYPE(SvRV(*v)) == SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(*v)) == SVt_PVAV) 7 0 7
1407 7 (void)hv_stores(dst, "origins", newSVsv(*v));
1408 10 ▶ if ((v = hv_fetchs(src, "check", 0)) && *v && SvOK(*v))
Branch: 3 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(src, "check", 0)) && *v && SvOK(*v)) 7 3 10
if ((v = hv_fetchs(src, "check", 0)) && *v && SvOK(*v)) 7 0 7
if ((v = hv_fetchs(src, "check", 0)) && *v && SvOK(*v)) 7 0 7
1409 7 (void)hv_stores(dst, "check",
1410 oa_cv_or_name(aTHX_ *v, "csrf 'check' callback"));
1411 10 v = hv_fetchs(src, "cookie", 0);
1412 10 ▶ (void)hv_stores(dst, "cookie",
Branch: 3 branches
BranchTrueFalseTotal
(void)hv_stores(dst, "cookie", 3 7 10
(void)hv_stores(dst, "cookie", 3 0 3
(void)hv_stores(dst, "cookie", 3 0 3
1413 (v && *v && SvOK(*v)) ? newSVsv(*v) : newSVpvs("csrf"));
1414 10 v = hv_fetchs(src, "header", 0);
1415 10 ▶ (void)hv_stores(dst, "header",
Branch: 3 branches
BranchTrueFalseTotal
(void)hv_stores(dst, "header", 2 8 10
(void)hv_stores(dst, "header", 2 0 2
(void)hv_stores(dst, "header", 2 0 2
1416 (v && *v && SvOK(*v)) ? newSVsv(*v) : newSVpvs("X-CSRF-Token"));
1417 10 ccfg = sv_2mortal(newRV_noinc((SV *)dst));
1418 }
1419 /* newSV(0), never &PL_sv_undef, and for the same reason the
1420 * 'before'/'after' slots above use it: perl before 5.20 treats an
1421 * array element that IS &PL_sv_undef as a deleted one, so
1422 * av_fetch hands back NULL and the request path dereferences it.
1423 * A fresh undef SV reads identically to every caller and survives
1424 * the round trip on every perl. */
1425 576 ▶ av_push(cap, ccfg ? SvREFCNT_inc(ccfg) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_push(cap, ccfg ? SvREFCNT_inc(ccfg) : newSV(0)); 10 566 576
1426 }
1427 { /* server options (slot 7): security headers (secure defaults on,
1428 * overridable / removable), body-size limit, CORS, error format,
1429 * content negotiation. Always built - the default header set is
1430 * stamped onto every response in finalize. */
1431 576 HV *o_opts = newHV();
1432 576 HV *hdrs = newHV();
1433  
1434 /* secure defaults appropriate for a JSON API (HSTS is opt-in) */
1435 576 (void)hv_stores(hdrs, "X-Content-Type-Options", newSVpvs("nosniff"));
1436 576 (void)hv_stores(hdrs, "Content-Security-Policy",
1437 newSVpvs("default-src 'none'; frame-ancestors 'none'"));
1438 576 (void)hv_stores(hdrs, "X-Frame-Options", newSVpvs("DENY"));
1439 576 (void)hv_stores(hdrs, "Referrer-Policy", newSVpvs("no-referrer"));
1440 576 ▶ if (headers_opt && SvOK(headers_opt)) {
Branch: 2 branches
BranchTrueFalseTotal
if (headers_opt && SvOK(headers_opt)) { 1 575 576
if (headers_opt && SvOK(headers_opt)) { 1 0 1
1441 1 HV *uh = oa_hv_of(headers_opt);
1442 HE *he;
1443 1 ▶ if (!uh) croak("Open::API::Plack->to_app: 'headers' must be a hashref");
Branch: 1 branch
BranchTrueFalseTotal
if (!uh) croak("Open::API::Plack->to_app: 'headers' must be a hashref"); 0 1 1
1444 1 hv_iterinit(uh);
1445 4 ▶ while ((he = hv_iternext(uh))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(uh))) { 3 1 4
1446 3 I32 kl; const char *k = hv_iterkey(he, &kl);
1447 3 SV *v = hv_iterval(uh, he);
1448 3 HE *h2; AV *del = (AV *)sv_2mortal((SV *)newAV());
1449 SSize_t di;
1450 3 hv_iterinit(hdrs); /* drop any case-variant default */
1451 16 ▶ while ((h2 = hv_iternext(hdrs))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((h2 = hv_iternext(hdrs))) { 13 3 16
1452 13 I32 kl2; const char *k2 = hv_iterkey(h2, &kl2);
1453 13 ▶ if (oa_ci_eq(k2, (STRLEN)kl2, k, (STRLEN)kl))
Branch: 1 branch
BranchTrueFalseTotal
if (oa_ci_eq(k2, (STRLEN)kl2, k, (STRLEN)kl)) 2 11 13
1454 2 av_push(del, newSVpvn(k2, kl2));
1455 }
1456 5 ▶ for (di = 0; di <= av_len(del); di++) {
Branch: 1 branch
BranchTrueFalseTotal
for (di = 0; di <= av_len(del); di++) { 2 3 5
1457 2 SV **d = av_fetch(del, di, 0);
1458 2 STRLEN dl; const char *dp = SvPV_const(*d, dl);
1459 2 (void)hv_delete(hdrs, dp, (I32)dl, G_DISCARD);
1460 }
1461 3 ▶ if (SvOK(v)) /* an undef value removes it */
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(v)) /* an undef value removes it */ 2 1 3
1462 2 (void)hv_store(hdrs, k, kl, newSVsv(v), 0);
1463 }
1464 }
1465 576 (void)hv_stores(o_opts, "headers", newRV_noinc((SV *)hdrs));
1466  
1467 576 ▶ if (maxbody > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (maxbody > 0) 501 75 576
1468 501 (void)hv_stores(o_opts, "max_body_size", newSViv(maxbody));
1469 576 ▶ if (negotiate)
Branch: 1 branch
BranchTrueFalseTotal
if (negotiate) 1 575 576
1470 1 (void)hv_stores(o_opts, "negotiate", newSViv(1));
1471  
1472 /* response validation: the mode rides in its own capture slot,
1473 * the rest here, where the request path already reads options */
1474 576 ▶ if (rv_report)
Branch: 1 branch
BranchTrueFalseTotal
if (rv_report) 25 551 576
1475 25 (void)hv_stores(o_opts, "response_report", rv_report);
1476 576 ▶ if (rv_sample > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (rv_sample > 1) 1 575 576
1477 1 (void)hv_stores(o_opts, "response_sample", newSViv(rv_sample));
1478 576 ▶ if (rv_maxbody > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (rv_maxbody > 0) 2 574 576
1479 2 (void)hv_stores(o_opts, "response_max_body", newSViv(rv_maxbody));
1480 576 ▶ if (errfmt_opt && SvOK(errfmt_opt)) {
Branch: 2 branches
BranchTrueFalseTotal
if (errfmt_opt && SvOK(errfmt_opt)) { 1 575 576
if (errfmt_opt && SvOK(errfmt_opt)) { 1 0 1
1481 1 STRLEN l; const char *p = SvPV_const(errfmt_opt, l);
1482 1 ▶ if (l == 7 && memEQ(p, "problem", 7))
Branch: 2 branches
BranchTrueFalseTotal
if (l == 7 && memEQ(p, "problem", 7)) 1 0 1
if (l == 7 && memEQ(p, "problem", 7)) 1 0 1
1483 1 (void)hv_stores(o_opts, "error_format", newSVpvs("problem"));
1484 0 ▶ else if (!(l == 4 && memEQ(p, "json", 4)))
Branch: 2 branches
BranchTrueFalseTotal
else if (!(l == 4 && memEQ(p, "json", 4))) 0 0 0
else if (!(l == 4 && memEQ(p, "json", 4))) 0 0 0
1485 0 croak("Open::API::Plack->to_app: 'error_format' must be "
1486 "'json' or 'problem'");
1487 }
1488 576 ▶ if (cors_opt && SvOK(cors_opt)) {
Branch: 2 branches
BranchTrueFalseTotal
if (cors_opt && SvOK(cors_opt)) { 4 572 576
if (cors_opt && SvOK(cors_opt)) { 4 0 4
1489 4 HV *cs = oa_hv_of(cors_opt), *cd;
1490 SV **v, *origins;
1491 4 int creds = 0, wild;
1492 4 ▶ if (!cs) croak("Open::API::Plack->to_app: 'cors' must be a hashref");
Branch: 1 branch
BranchTrueFalseTotal
if (!cs) croak("Open::API::Plack->to_app: 'cors' must be a hashref"); 0 4 4
1493 4 cd = newHV();
1494 4 ▶ if ((v = hv_fetchs(cs, "origins", 0)) && *v && SvOK(*v))
Branch: 3 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(cs, "origins", 0)) && *v && SvOK(*v)) 2 2 4
if ((v = hv_fetchs(cs, "origins", 0)) && *v && SvOK(*v)) 2 0 2
if ((v = hv_fetchs(cs, "origins", 0)) && *v && SvOK(*v)) 2 0 2
1495 2 origins = newSVsv(*v);
1496 else
1497 2 origins = newSVpvs("*"); /* public by default */
1498 4 ▶ if ((v = hv_fetchs(cs, "credentials", 0)) && *v)
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(cs, "credentials", 0)) && *v) 2 2 4
if ((v = hv_fetchs(cs, "credentials", 0)) && *v) 2 0 2
1499 2 creds = SvTRUE(*v);
1500 /* a wildcard origin with credentials would let any site make
1501 * credentialed requests - refuse the footgun */
1502 4 wild = 0;
1503 4 ▶ if (!SvROK(origins)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(origins)) { 2 2 4
1504 2 STRLEN wl; const char *wp = SvPV_const(origins, wl);
1505 2 ▶ wild = (wl == 1 && *wp == '*');
Branch: 2 branches
BranchTrueFalseTotal
wild = (wl == 1 && *wp == '*'); 2 0 2
wild = (wl == 1 && *wp == '*'); 2 0 2
1506 }
1507 4 ▶ if (SvROK(origins) && SvTYPE(SvRV(origins)) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(origins) && SvTYPE(SvRV(origins)) == SVt_PVAV) { 2 2 4
if (SvROK(origins) && SvTYPE(SvRV(origins)) == SVt_PVAV) { 2 0 2
1508 2 AV *l = (AV *)SvRV(origins); SSize_t j;
1509 4 ▶ for (j = 0; j <= av_len(l); j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j <= av_len(l); j++) { 2 2 4
1510 2 SV **e = av_fetch(l, j, 0);
1511 2 ▶ if (e && *e && SvPOK(*e) && SvCUR(*e) == 1
Branch: 4 branches
BranchTrueFalseTotal
if (e && *e && SvPOK(*e) && SvCUR(*e) == 1 2 0 2
if (e && *e && SvPOK(*e) && SvCUR(*e) == 1 2 0 2
if (e && *e && SvPOK(*e) && SvCUR(*e) == 1 2 0 2
if (e && *e && SvPOK(*e) && SvCUR(*e) == 1 0 2 2
1512 0 ▶ && *SvPVX(*e) == '*') { wild = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
&& *SvPVX(*e) == '*') { wild = 1; break; } 0 0 0
1513 }
1514 }
1515 4 ▶ if (creds && wild)
Branch: 2 branches
BranchTrueFalseTotal
if (creds && wild) 2 2 4
if (creds && wild) 1 1 2
1516 1 croak("Open::API::Plack->to_app: cors credentials => 1 needs an "
1517 "explicit origins list (a wildcard '*' origin cannot "
1518 "be used with credentials)");
1519 3 (void)hv_stores(cd, "origins", origins);
1520 3 (void)hv_stores(cd, "credentials", newSViv(creds ? 1 : 0));
1521 3 ▶ if ((v = hv_fetchs(cs, "headers", 0)) && *v && SvOK(*v))
Branch: 3 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(cs, "headers", 0)) && *v && SvOK(*v)) 0 3 3
if ((v = hv_fetchs(cs, "headers", 0)) && *v && SvOK(*v)) 0 0 0
if ((v = hv_fetchs(cs, "headers", 0)) && *v && SvOK(*v)) 0 0 0
1522 0 (void)hv_stores(cd, "headers", newSVsv(*v));
1523 3 ▶ if ((v = hv_fetchs(cs, "expose", 0)) && *v && SvROK(*v))
Branch: 3 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(cs, "expose", 0)) && *v && SvROK(*v)) 0 3 3
if ((v = hv_fetchs(cs, "expose", 0)) && *v && SvROK(*v)) 0 0 0
if ((v = hv_fetchs(cs, "expose", 0)) && *v && SvROK(*v)) 0 0 0
1524 0 (void)hv_stores(cd, "expose", newSVsv(*v));
1525 3 ▶ if ((v = hv_fetchs(cs, "max_age", 0)) && *v && SvOK(*v))
Branch: 3 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(cs, "max_age", 0)) && *v && SvOK(*v)) 1 2 3
if ((v = hv_fetchs(cs, "max_age", 0)) && *v && SvOK(*v)) 1 0 1
if ((v = hv_fetchs(cs, "max_age", 0)) && *v && SvOK(*v)) 1 0 1
1526 1 (void)hv_stores(cd, "max_age", newSVsv(*v));
1527 3 (void)hv_stores(o_opts, "cors", newRV_noinc((SV *)cd));
1528 }
1529 575 av_push(cap, newRV_noinc((SV *)o_opts));
1530 }
1531 575 RETVAL = oa_closure(aTHX_ oa_app_cb, cap);
1532 { /* ui option: wrap the finished app so the docs routes answer
1533 * before the router sees them. The wrap itself lives in Perl
1534 * (Open::API::UI); a missing Template::Stencil croaks through
1535 * here - a startup configuration error like everything else. */
1536 575 SV **u = hv_fetchs(cfg, "ui", 0);
1537 575 ▶ if (u && *u && SvTRUE(*u)) {
Branch: 3 branches
BranchTrueFalseTotal
if (u && *u && SvTRUE(*u)) { 10 565 575
if (u && *u && SvTRUE(*u)) { 10 0 10
if (u && *u && SvTRUE(*u)) { 9 1 10
1538 9 dSP;
1539 int n;
1540 9 eval_pv("require Open::API::UI;", TRUE);
1541 9 SPAGAIN; /* the require may have reallocated the value stack */
1542 9 ENTER; SAVETMPS;
1543 9 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 9 9
1544 9 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 9 9
1545 9 PUSHs(self);
1546 9 mPUSHs(RETVAL); /* ownership of the inner app moves to
1547 * the wrapping closure (or the tmps) */
1548 9 PUTBACK;
1549 9 n = call_pv("Open::API::UI::_wrap_psgi", G_SCALAR);
1550 8 SPAGAIN;
1551 8 ▶ if (n != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (n != 1) 0 8 8
1552 0 croak("Open::API::UI::_wrap_psgi returned no app");
1553 8 RETVAL = newSVsv(POPs);
1554 8 PUTBACK;
1555 8 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 8 0 8
1556 }
1557 }
1558 }
1559 OUTPUT:
1560 RETVAL
1561  
1562 MODULE = Open::API PACKAGE = Open::API
1563  
1564 # ---- test shims (validate one value through a compiled handle) -------------
1565  
1566 # -1 unknown op/param, -2 no schema, else is_valid 0/1
1567 int
1568 _param_check(self, op_id, in, name, value)
1569 SV *self
1570 SV *op_id
1571 SV *in
1572 SV *name
1573 SV *value
1574 CODE:
1575 {
1576 30 oa_api *a = oa_api_of(aTHX_ self);
1577 30 oa_op *o = oa_op_by_id(aTHX_ a, op_id);
1578 30 int loc = oa_param_loc(aTHX_ in), i;
1579 30 RETVAL = -1;
1580 30 ▶ if (o && loc >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (o && loc >= 0) { 29 1 30
if (o && loc >= 0) { 29 0 29
1581 34 ▶ for (i = 0; i < o->nparams[loc]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < o->nparams[loc]; i++) { 33 1 34
1582 33 ▶ if (sv_eq(o->params[loc][i].name, name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_eq(o->params[loc][i].name, name)) { 28 5 33
1583 56 RETVAL = o->params[loc][i].handle
1584 28 ? JSF->is_valid(aTHX_ o->params[loc][i].handle, value)
1585 28 ▶ : -2;
Branch: 1 branch
BranchTrueFalseTotal
: -2; 28 0 28
1586 28 break;
1587 }
1588 }
1589 }
1590 }
1591 OUTPUT:
1592 RETVAL
1593  
1594 # -1 unknown op/ctype, -2 pass-through (no schema), else is_valid 0/1
1595 int
1596 _body_check(self, op_id, ctype, value)
1597 SV *self
1598 SV *op_id
1599 SV *ctype
1600 SV *value
1601 CODE:
1602 {
1603 75 oa_api *a = oa_api_of(aTHX_ self);
1604 75 oa_op *o = oa_op_by_id(aTHX_ a, op_id);
1605 int i;
1606 75 RETVAL = -1;
1607 75 ▶ if (o) {
Branch: 1 branch
BranchTrueFalseTotal
if (o) { 75 0 75
1608 107 ▶ for (i = 0; i < o->nbodies; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < o->nbodies; i++) { 106 1 107
1609 106 ▶ if (sv_eq(o->bodies[i].ctype, ctype)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_eq(o->bodies[i].ctype, ctype)) { 74 32 106
1610 148 RETVAL = o->bodies[i].handle
1611 73 ? JSF->is_valid(aTHX_ o->bodies[i].handle, value)
1612 74 ▶ : -2;
Branch: 1 branch
BranchTrueFalseTotal
: -2; 73 1 74
1613 74 break;
1614 }
1615 }
1616 }
1617 }
1618 OUTPUT:
1619 RETVAL
1620  
1621 void
1622 DESTROY(self)
1623 SV *self
1624 CODE:
1625 5730 oa_api_free(aTHX_ oa_api_of(aTHX_ self));
1626  
1627 MODULE = Open::API PACKAGE = Open::API::Client
1628  
1629 # The URL this client would build for an operation, without firing it. A
1630 # client's serialization rules - allowReserved above all - are otherwise
1631 # observable only by standing up a real server, which a compliance case must
1632 # not do. No security credentials are attached: this inspects serialization,
1633 # not authentication.
1634 #
1635 # In the Client package deliberately: `self` is a client HV, not an Open::API.
1636 # It first landed beside _param_check, which is Open::API's, where it compiled
1637 # perfectly and simply did not exist as a client method - a silent miss.
1638 SV *
1639 _request_url(self, op_id, params)
1640 SV *self
1641 SV *op_id
1642 SV *params
1643 CODE:
1644 {
1645 SV *api_sv;
1646 oa_api *a;
1647 oa_op *o;
1648 74 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) 74 0 74
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) 0 74 74
1649 0 croak("Open::API::Client: not a client");
1650 74 api_sv = oa_cli_get(aTHX_ (HV *)SvRV(self), "api");
1651 74 ▶ if (!api_sv || !SvROK(api_sv))
Branch: 2 branches
BranchTrueFalseTotal
if (!api_sv || !SvROK(api_sv)) 74 0 74
if (!api_sv || !SvROK(api_sv)) 0 74 74
1652 0 croak("Open::API::Client: no api on this client");
1653 74 a = (oa_api *)INT2PTR(void *, SvIV(SvRV(api_sv)));
1654 74 o = oa_op_by_id(aTHX_ a, op_id);
1655 74 ▶ if (!o) croak("Open::API::Client: unknown operationId '%" SVf "'",
Branch: 1 branch
BranchTrueFalseTotal
if (!o) croak("Open::API::Client: unknown operationId '%" SVf "'", 0 74 74
1656 SVfARG(op_id));
1657 74 ▶ if (!params || !SvROK(params) || SvTYPE(SvRV(params)) != SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if (!params || !SvROK(params) || SvTYPE(SvRV(params)) != SVt_PVHV) 74 0 74
if (!params || !SvROK(params) || SvTYPE(SvRV(params)) != SVt_PVHV) 74 0 74
if (!params || !SvROK(params) || SvTYPE(SvRV(params)) != SVt_PVHV) 0 74 74
1658 0 croak("Open::API::Client: _request_url wants a params hashref");
1659 74 RETVAL = newSVsv(oa_cli_url(aTHX_ o, (HV *)SvRV(params),
1660 oa_cli_get(aTHX_ (HV *)SvRV(self), "base_url"),
1661 NULL, 0));
1662 }
1663 OUTPUT:
1664 RETVAL
1665  
1666 # ($body, $content_type) for what this client WOULD send. The body half of
1667 # _request_url: a body that exists only inside a live request cannot be
1668 # asserted against, and the builder used to label everything application/json.
1669 void
1670 _request_body(self, op_id, params)
1671 SV *self
1672 SV *op_id
1673 SV *params
1674 PPCODE:
1675 {
1676 SV *api_sv;
1677 oa_api *a;
1678 oa_op *o;
1679 18 SV *body, *ctype = NULL;
1680 18 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) 18 0 18
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) 0 18 18
1681 0 croak("Open::API::Client: not a client");
1682 18 api_sv = oa_cli_get(aTHX_ (HV *)SvRV(self), "api");
1683 18 ▶ if (!api_sv || !SvROK(api_sv))
Branch: 2 branches
BranchTrueFalseTotal
if (!api_sv || !SvROK(api_sv)) 18 0 18
if (!api_sv || !SvROK(api_sv)) 0 18 18
1684 0 croak("Open::API::Client: no api on this client");
1685 18 a = (oa_api *)INT2PTR(void *, SvIV(SvRV(api_sv)));
1686 18 o = oa_op_by_id(aTHX_ a, op_id);
1687 18 ▶ if (!o) croak("Open::API::Client: unknown operationId '%" SVf "'",
Branch: 1 branch
BranchTrueFalseTotal
if (!o) croak("Open::API::Client: unknown operationId '%" SVf "'", 0 18 18
1688 SVfARG(op_id));
1689 18 ▶ if (!params || !SvROK(params) || SvTYPE(SvRV(params)) != SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if (!params || !SvROK(params) || SvTYPE(SvRV(params)) != SVt_PVHV) 18 0 18
if (!params || !SvROK(params) || SvTYPE(SvRV(params)) != SVt_PVHV) 18 0 18
if (!params || !SvROK(params) || SvTYPE(SvRV(params)) != SVt_PVHV) 0 18 18
1690 0 croak("Open::API::Client: _request_body wants a params hashref");
1691 18 body = oa_cli_build_body(aTHX_ o, (HV *)SvRV(params), &ctype);
1692 17 ▶ if (!body) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!body) XSRETURN_EMPTY; 0 17 17
1693 17 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 17 17
1694 17 PUSHs(sv_2mortal(newSVsv(body)));
1695 17 ▶ PUSHs(ctype ? sv_2mortal(newSVsv(ctype)) : &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(ctype ? sv_2mortal(newSVsv(ctype)) : &PL_sv_undef); 17 0 17
1696 }
1697  
1698 # Open::API::Client->new(api => $api | spec => ..., base_url => ...,
1699 # validate => 0|1, %ua_opts). Remaining pairs (timeout, tls_verify,
1700 # pool_size, loop, ...) are passed to Fetch's ua_new on first use.
1701 SV *
1702 new(class, ...)
1703 SV *class
1704 CODE:
1705 {
1706 0 ▶ const char *cls = (SvROK(class) && SvOBJECT(SvRV(class)))
Branch: 1 branch
BranchTrueFalseTotal
const char *cls = (SvROK(class) && SvOBJECT(SvRV(class))) 0 0 0
1707 200 ▶ ? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class);
Branch: 7 branches
BranchTrueFalseTotal
? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); 0 100 100
? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); 0 0 0
? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); 0 0 0
? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); 0 0 0
? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); 0 0 0
? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); 0 0 0
? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); 0 0 0
1708 /* mortal until blessed: a croak below must not leak them */
1709 100 HV *self = (HV *)sv_2mortal((SV *)newHV());
1710 100 AV *ua_opts = (AV *)sv_2mortal((SV *)newAV());
1711 100 SV *api = NULL, *spec = NULL;
1712 SV **bv;
1713 100 STRLEN bl = 0;
1714 int i;
1715 298 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 198 100 298
1716 198 STRLEN kl; const char *k = SvPV_const(ST(i), kl);
1717 198 SV *v = ST(i + 1);
1718 198 ▶ if (kl == 3 && memEQ(k, "api", 3)) api = v;
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 3 && memEQ(k, "api", 3)) api = v; 99 99 198
if (kl == 3 && memEQ(k, "api", 3)) api = v; 99 0 99
1719 99 ▶ else if (kl == 4 && memEQ(k, "spec", 4)) spec = v;
Branch: 2 branches
BranchTrueFalseTotal
else if (kl == 4 && memEQ(k, "spec", 4)) spec = v; 0 99 99
else if (kl == 4 && memEQ(k, "spec", 4)) spec = v; 0 0 0
1720 99 ▶ else if (kl == 8 && memEQ(k, "base_url", 8))
Branch: 2 branches
BranchTrueFalseTotal
else if (kl == 8 && memEQ(k, "base_url", 8)) 99 0 99
else if (kl == 8 && memEQ(k, "base_url", 8)) 99 0 99
1721 99 (void)hv_stores(self, "base_url", newSVsv(v));
1722 0 ▶ else if (kl == 8 && memEQ(k, "security", 8))
Branch: 2 branches
BranchTrueFalseTotal
else if (kl == 8 && memEQ(k, "security", 8)) 0 0 0
else if (kl == 8 && memEQ(k, "security", 8)) 0 0 0
1723 0 (void)hv_stores(self, "security", newSVsv(v));
1724 0 ▶ else if (kl == 4 && memEQ(k, "csrf", 4))
Branch: 2 branches
BranchTrueFalseTotal
else if (kl == 4 && memEQ(k, "csrf", 4)) 0 0 0
else if (kl == 4 && memEQ(k, "csrf", 4)) 0 0 0
1725 0 (void)hv_stores(self, "_csrf_opt", newSVsv(v));
1726 0 ▶ else if (kl == 8 && memEQ(k, "validate", 8))
Branch: 2 branches
BranchTrueFalseTotal
else if (kl == 8 && memEQ(k, "validate", 8)) 0 0 0
else if (kl == 8 && memEQ(k, "validate", 8)) 0 0 0
1727 0 (void)hv_stores(self, "validate", newSViv(SvTRUE(v) ? 1 : 0));
1728 else {
1729 0 av_push(ua_opts, newSVpvn(k, kl));
1730 0 av_push(ua_opts, newSVsv(v));
1731 }
1732 }
1733 100 ▶ if (api) {
Branch: 1 branch
BranchTrueFalseTotal
if (api) { 99 1 100
1734 99 ▶ if (!sv_derived_from(api, "Open::API"))
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_derived_from(api, "Open::API")) 1 98 99
1735 1 croak("Open::API::Client->new: 'api' must be an Open::API");
1736 98 (void)hv_stores(self, "api", newSVsv(api));
1737 1 ▶ } else if (spec) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (spec) { 0 1 1
1738 0 (void)hv_stores(self, "api",
1739 oa_new_from_spec(aTHX_ "Open::API", spec, 0));
1740 } else {
1741 1 croak("Open::API::Client->new: give 'api' or 'spec'");
1742 }
1743 98 bv = hv_fetchs(self, "base_url", 0);
1744 98 ▶ if (bv && *bv && SvOK(*bv)) (void)SvPV_const(*bv, bl);
Branch: 3 branches
BranchTrueFalseTotal
if (bv && *bv && SvOK(*bv)) (void)SvPV_const(*bv, bl); 97 1 98
if (bv && *bv && SvOK(*bv)) (void)SvPV_const(*bv, bl); 97 0 97
if (bv && *bv && SvOK(*bv)) (void)SvPV_const(*bv, bl); 97 0 97
1745 98 ▶ if (!bl)
Branch: 1 branch
BranchTrueFalseTotal
if (!bl) 1 97 98
1746 1 croak("Open::API::Client->new: a 'base_url' is required");
1747 { /* normalise transparent-CSRF config: cookie + header names and
1748 * the Origin to present (derived from base_url unless given) */
1749 97 SV **c = hv_fetchs(self, "_csrf_opt", 0);
1750 97 ▶ if (c && *c && SvTRUE(*c)) {
Branch: 3 branches
BranchTrueFalseTotal
if (c && *c && SvTRUE(*c)) { 0 97 97
if (c && *c && SvTRUE(*c)) { 0 0 0
if (c && *c && SvTRUE(*c)) { 0 0 0
1751 0 HV *cd = newHV();
1752 0 ▶ HV *src = (SvROK(*c) && SvTYPE(SvRV(*c)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
HV *src = (SvROK(*c) && SvTYPE(SvRV(*c)) == SVt_PVHV) 0 0 0
1753 0 ▶ ? (HV *)SvRV(*c) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(*c) : NULL; 0 0 0
1754 SV **v;
1755 0 ▶ if (src && (v = hv_fetchs(src, "cookie", 0)) && *v && SvOK(*v))
Branch: 4 branches
BranchTrueFalseTotal
if (src && (v = hv_fetchs(src, "cookie", 0)) && *v && SvOK(*v)) 0 0 0
if (src && (v = hv_fetchs(src, "cookie", 0)) && *v && SvOK(*v)) 0 0 0
if (src && (v = hv_fetchs(src, "cookie", 0)) && *v && SvOK(*v)) 0 0 0
if (src && (v = hv_fetchs(src, "cookie", 0)) && *v && SvOK(*v)) 0 0 0
1756 0 (void)hv_stores(cd, "cookie", newSVsv(*v));
1757 0 else (void)hv_stores(cd, "cookie", newSVpvs("csrf"));
1758 0 ▶ if (src && (v = hv_fetchs(src, "header", 0)) && *v && SvOK(*v))
Branch: 4 branches
BranchTrueFalseTotal
if (src && (v = hv_fetchs(src, "header", 0)) && *v && SvOK(*v)) 0 0 0
if (src && (v = hv_fetchs(src, "header", 0)) && *v && SvOK(*v)) 0 0 0
if (src && (v = hv_fetchs(src, "header", 0)) && *v && SvOK(*v)) 0 0 0
if (src && (v = hv_fetchs(src, "header", 0)) && *v && SvOK(*v)) 0 0 0
1759 0 (void)hv_stores(cd, "header", newSVsv(*v));
1760 0 else (void)hv_stores(cd, "header", newSVpvs("X-CSRF-Token"));
1761 0 ▶ if (src && (v = hv_fetchs(src, "origin", 0)) && *v && SvOK(*v))
Branch: 4 branches
BranchTrueFalseTotal
if (src && (v = hv_fetchs(src, "origin", 0)) && *v && SvOK(*v)) 0 0 0
if (src && (v = hv_fetchs(src, "origin", 0)) && *v && SvOK(*v)) 0 0 0
if (src && (v = hv_fetchs(src, "origin", 0)) && *v && SvOK(*v)) 0 0 0
if (src && (v = hv_fetchs(src, "origin", 0)) && *v && SvOK(*v)) 0 0 0
1762 0 (void)hv_stores(cd, "origin", newSVsv(*v));
1763 else { /* scheme://authority of base_url (strip any path) */
1764 0 SV *bu = *hv_fetchs(self, "base_url", 0);
1765 0 STRLEN bl; const char *bp = SvPV_const(bu, bl);
1766 STRLEN i2, sep;
1767 0 ▶ for (i2 = 0; i2 + 2 < bl; i2++)
Branch: 1 branch
BranchTrueFalseTotal
for (i2 = 0; i2 + 2 < bl; i2++) 0 0 0
1768 0 ▶ if (bp[i2] == ':' && bp[i2+1] == '/' && bp[i2+2] == '/')
Branch: 3 branches
BranchTrueFalseTotal
if (bp[i2] == ':' && bp[i2+1] == '/' && bp[i2+2] == '/') 0 0 0
if (bp[i2] == ':' && bp[i2+1] == '/' && bp[i2+2] == '/') 0 0 0
if (bp[i2] == ':' && bp[i2+1] == '/' && bp[i2+2] == '/') 0 0 0
1769 0 { i2 += 3; break; }
1770 0 ▶ if (i2 + 2 >= bl) i2 = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (i2 + 2 >= bl) i2 = 0; 0 0 0
1771 0 ▶ for (sep = i2; sep < bl && bp[sep] != '/'; sep++) ;
Branch: 2 branches
BranchTrueFalseTotal
for (sep = i2; sep < bl && bp[sep] != '/'; sep++) ; 0 0 0
for (sep = i2; sep < bl && bp[sep] != '/'; sep++) ; 0 0 0
1772 0 (void)hv_stores(cd, "origin", newSVpvn(bp, sep));
1773 }
1774 0 (void)hv_stores(self, "_csrf", newRV_noinc((SV *)cd));
1775 }
1776 97 (void)hv_deletes(self, "_csrf_opt", G_DISCARD);
1777 }
1778 97 (void)hv_stores(self, "ua_opts", newRV_inc((SV *)ua_opts));
1779 97 RETVAL = sv_bless(newRV_inc((SV *)self), gv_stashpv(cls, GV_ADD));
1780 }
1781 OUTPUT:
1782 RETVAL
1783  
1784 # Fire one operation: $client->call($operationId, %params). Flat %params are
1785 # matched to the operation's declared parameters by name (body => the request
1786 # body). Croaks on invalid input BEFORE any I/O; returns a Fetch::Future
1787 # resolving to { status, headers, data, error? }.
1788 SV *
1789 call(self, op_id, ...)
1790 SV *self
1791 SV *op_id
1792 CODE:
1793 {
1794 7 HV *params = (HV *)sv_2mortal((SV *)newHV());
1795 int i;
1796 7 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) 7 0 7
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) 0 7 7
1797 0 croak("Open::API::Client: not a client");
1798 11 ▶ for (i = 2; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i + 1 < items; i += 2) { 4 7 11
1799 4 STRLEN kl; const char *k = SvPV_const(ST(i), kl);
1800 4 (void)hv_store(params, k, (I32)kl, newSVsv(ST(i + 1)), 0);
1801 }
1802 7 RETVAL = oa_cli_call(aTHX_ (HV *)SvRV(self), op_id, params);
1803 }
1804 OUTPUT:
1805 RETVAL
1806  
1807 # True when the operationId exists (can() support; can stays pure Perl).
1808 int
1809 _has_op(self, name)
1810 SV *self
1811 SV *name
1812 CODE:
1813 0 ▶ RETVAL = (SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV) 0 0 0
RETVAL = (SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV) 0 0 0
1814 0 ▶ ? oa_op_exists(aTHX_ (HV *)SvRV(self), name) : 0;
Branch: 1 branch
BranchTrueFalseTotal
? oa_op_exists(aTHX_ (HV *)SvRV(self), name) : 0; 0 0 0
1815 OUTPUT:
1816 RETVAL
1817  
1818 # $client->$operationId(%params) sugar. Perl sets $Open::API::Client::AUTOLOAD
1819 # to the fully-qualified name; we dispatch straight to oa_cli_call (the same C
1820 # path as ->call), skipping a Perl method hop. An unknown name croaks - a typo
1821 # dies rather than silently 404ing.
1822 void
1823 AUTOLOAD(self, ...)
1824 SV *self
1825 PPCODE:
1826 {
1827 104 SV *avar = get_sv("Open::API::Client::AUTOLOAD", 0);
1828 104 const char *full = NULL, *mp;
1829 SV *op, *ret;
1830 HV *params;
1831 int i;
1832 /* Perl hands an XSUB AUTOLOAD its method name one of two ways. Newer
1833 * perls set $AUTOLOAD; perls through 5.14 skip that deliberately -
1834 * rather than build the variable only for the XSUB to parse it back
1835 * apart, gv_autoload stores the name in the CV's own PV slot - so
1836 * $AUTOLOAD is simply undef there and a typo'd method silently
1837 * returned empty instead of croaking. Read whichever one is present.
1838 *
1839 * Test SvPVX/SvCUR rather than SvPOK: gv_autoload does the assignment
1840 * with SvPV_set/SvCUR_set and never turns the POK flag on, so the CV
1841 * carries the name with POK clear. The CV holds no prototype that
1842 * could be mistaken for it - PROTOTYPES is DISABLE above. */
1843 208 ▶ if (avar && SvOK(avar)) full = SvPV_nolen(avar);
Branch: 2 branches
BranchTrueFalseTotal
if (avar && SvOK(avar)) full = SvPV_nolen(avar); 104 0 104
if (avar && SvOK(avar)) full = SvPV_nolen(avar); 104 0 104
1844 0 ▶ else if (SvPVX((SV *)cv) && SvCUR((SV *)cv)) full = SvPVX((SV *)cv);
Branch: 2 branches
BranchTrueFalseTotal
else if (SvPVX((SV *)cv) && SvCUR((SV *)cv)) full = SvPVX((SV *)cv); 0 0 0
else if (SvPVX((SV *)cv) && SvCUR((SV *)cv)) full = SvPVX((SV *)cv); 0 0 0
1845 104 ▶ if (!full) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!full) XSRETURN_EMPTY; 0 104 104
1846 104 mp = strrchr(full, ':');
1847 104 ▶ mp = mp ? mp + 1 : full;
Branch: 1 branch
BranchTrueFalseTotal
mp = mp ? mp + 1 : full; 104 0 104
1848 104 ▶ if (strEQ(mp, "DESTROY")) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(mp, "DESTROY")) XSRETURN_EMPTY; 97 7 104
1849 7 op = sv_2mortal(newSVpv(mp, 0));
1850 14 ▶ if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV
Branch: 1 branch
BranchTrueFalseTotal
if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV 7 0 7
1851 7 && oa_op_exists(aTHX_ (HV *)SvRV(self), op)))
1852 1 croak("Open::API::Client: no such method or operation '%s'", mp);
1853 6 params = (HV *)sv_2mortal((SV *)newHV());
1854 12 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 6 6 12
1855 6 STRLEN kl; const char *k = SvPV_const(ST(i), kl);
1856 6 (void)hv_store(params, k, (I32)kl, newSVsv(ST(i + 1)), 0);
1857 }
1858 6 ret = oa_cli_call(aTHX_ (HV *)SvRV(self), op, params);
1859 6 ST(0) = sv_2mortal(ret);
1860 6 XSRETURN(1);
1861 }
1862  
1863 # can(): a real method if one exists (like UNIVERSAL::can - Open::API::Client
1864 # has no @ISA, so the original SUPER::can collapsed to that), else a coderef
1865 # for an operationId (so `$c->can($op)->($c, %p)` works), else undef.
1866 SV *
1867 can(self, method)
1868 SV *self
1869 SV *method
1870 CODE:
1871 {
1872 3 const char *m = SvPV_nolen(method);
1873 3 ▶ HV *stash = (SvROK(self) && SvOBJECT(SvRV(self))) ? SvSTASH(SvRV(self))
Branch: 1 branch
BranchTrueFalseTotal
HV *stash = (SvROK(self) && SvOBJECT(SvRV(self))) ? SvSTASH(SvRV(self)) 3 0 3
1874 6 ▶ : SvPOK(self) ? gv_stashsv(self, 0) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
: SvPOK(self) ? gv_stashsv(self, 0) : NULL; 3 0 3
: SvPOK(self) ? gv_stashsv(self, 0) : NULL; 0 0 0
1875 GV *gv;
1876 3 RETVAL = &PL_sv_undef;
1877 3 ▶ if (stash && (gv = gv_fetchmethod_autoload(stash, m, FALSE)) && GvCV(gv))
Branch: 3 branches
BranchTrueFalseTotal
if (stash && (gv = gv_fetchmethod_autoload(stash, m, FALSE)) && GvCV(gv)) 3 0 3
if (stash && (gv = gv_fetchmethod_autoload(stash, m, FALSE)) && GvCV(gv)) 1 2 3
if (stash && (gv = gv_fetchmethod_autoload(stash, m, FALSE)) && GvCV(gv)) 1 0 1
1878 1 RETVAL = newRV_inc((SV *)GvCV(gv)); /* a real method */
1879 2 ▶ else if (SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV
Branch: 2 branches
BranchTrueFalseTotal
else if (SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV 2 0 2
else if (SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV 2 0 2
1880 2 ▶ && oa_op_exists(aTHX_ (HV *)SvRV(self), method)) {
Branch: 1 branch
BranchTrueFalseTotal
&& oa_op_exists(aTHX_ (HV *)SvRV(self), method)) { 1 1 2
1881 1 AV *cap = newAV();
1882 1 av_push(cap, newSVsv(method)); /* capture the op id */
1883 1 RETVAL = oa_closure(aTHX_ oa_op_call_cb, cap); /* +1 owned coderef */
1884 }
1885 }
1886 OUTPUT:
1887 RETVAL
1888  
1889 # The underlying Open::API object.
1890 SV *
1891 api(self)
1892 SV *self
1893 CODE:
1894 {
1895 1 ▶ SV **e = (SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
SV **e = (SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV) 1 0 1
1896 2 ▶ ? hv_fetchs((HV *)SvRV(self), "api", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? hv_fetchs((HV *)SvRV(self), "api", 0) : NULL; 1 0 1
1897 1 ▶ RETVAL = (e && *e) ? newSVsv(*e) : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (e && *e) ? newSVsv(*e) : &PL_sv_undef; 1 0 1
RETVAL = (e && *e) ? newSVsv(*e) : &PL_sv_undef; 1 0 1
1898 }
1899 OUTPUT:
1900 RETVAL
« include/oa_abi_impl.h ↑ Summary include/oa_plack.h »