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