include/jsf_fetch.h

filter: Statementstmt 74.5% 38 / 51 Branchbran 32.9% 29 / 88 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 48.2% 67 / 139 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/arch/JSON/Schema/Fast/Install/Files.pm ↑ Summary include/jsf_frj.h »
1 #ifndef JSF_FETCH_H
2 #define JSF_FETCH_H
3  
4 /* Default remote-document resolution through Fetch's C ABI (fetch_abi.h). When a
5 * $ref/$dynamicRef names an http(s) document and the caller passed no resolver,
6 * we GET it via the ABI and decode the body as JSON. Fetch is loaded and its ABI
7 * resolved LAZILY on first use - a schema with only local references never loads
8 * Fetch, and a missing Fetch simply means "no auto-fetch" (the ref then fails to
9 * resolve like any other). Needs the Perl API (perl.h, included by the .xs). */
10  
11 #include "fetch_abi.h" /* via ExtUtils::Depends; runtime version-checked below */
12  
13 static const fetch_abi *JSF_FETCH = NULL; /* resolved on first fetch */
14 static int JSF_FETCH_TRIED = 0;
15 static SV *JSF_FETCH_UA = NULL; /* process-lifetime UA */
16  
17 /* Resolve Fetch's ABI table once; 1 if available. */
18 1 static int jsf_fetch_init(pTHX) {
19 1 dSP; int count; IV p = 0;
20 1 ▶ if (JSF_FETCH) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (JSF_FETCH) return 1; 0 1 1
21 1 ▶ if (JSF_FETCH_TRIED) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (JSF_FETCH_TRIED) return 0; 0 1 1
22 1 JSF_FETCH_TRIED = 1;
23  
24 1 eval_pv("require Fetch;", FALSE); /* trap a missing Fetch */
25 1 ▶ if (SvTRUE(ERRSV)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) return 0; 1 0 1
if (SvTRUE(ERRSV)) return 0; 0 1 1
26 /* The require may have grown (and so reallocated) the value stack; SP was
27 * captured before it and would now point into the freed block. */
28 1 SPAGAIN;
29  
30 1 ▶ ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; 0 1 1
31 1 count = call_pv("Fetch::_abi_ptr", G_SCALAR | G_EVAL);
32 1 SPAGAIN;
33 1 ▶ if (!SvTRUE(ERRSV) && count > 0) p = POPi;
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 1 0 1
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 1 0 1
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 1 0 1
34 0 ▶ else if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) (void)POPs; 0 0 0
35 1 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 1 0 1
36  
37 1 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 1 0 1
38 1 const fetch_abi *a = INT2PTR(const fetch_abi *, p);
39 1 ▶ if (a && a->abi_version >= FETCH_ABI_VERSION) JSF_FETCH = a;
Branch: 2 branches
BranchTrueFalseTotal
if (a && a->abi_version >= FETCH_ABI_VERSION) JSF_FETCH = a; 1 0 1
if (a && a->abi_version >= FETCH_ABI_VERSION) JSF_FETCH = a; 1 0 1
40 }
41 1 return JSF_FETCH != NULL;
42 }
43  
44 /* the request settles here: hand back the body on a 2xx, else undef */
45 1 static SV *jsf_fetch_map(pTHX_ int ok, int status, AV *headers, SV *body, SV *err, void *ud) {
46 PERL_UNUSED_ARG(headers); PERL_UNUSED_ARG(err); PERL_UNUSED_ARG(ud);
47 1 ▶ if (ok && status >= 200 && status < 300 && body && SvOK(body)) return newSVsv(body);
Branch: 5 branches
BranchTrueFalseTotal
if (ok && status >= 200 && status < 300 && body && SvOK(body)) return newSVsv(body); 0 1 1
if (ok && status >= 200 && status < 300 && body && SvOK(body)) return newSVsv(body); 0 0 0
if (ok && status >= 200 && status < 300 && body && SvOK(body)) return newSVsv(body); 0 0 0
if (ok && status >= 200 && status < 300 && body && SvOK(body)) return newSVsv(body); 0 0 0
if (ok && status >= 200 && status < 300 && body && SvOK(body)) return newSVsv(body); 0 0 0
48 1 return newSV(0);
49 }
50  
51 /* A fetched remote document that is not valid JSON must fail the $ref
52 * gracefully (return NULL), not croak. The ABI decode croaks on bad input and
53 * cannot be trapped portably from C here, so this cold path keeps decoding
54 * through File::Raw::JSON under G_EVAL; the hot schema-text path uses the ABI
55 * directly (jsf_json_decode). */
56 0 static SV *jsf_fetch_decode(pTHX_ SV *text) {
57 0 dSP; int count; SV *ret = NULL;
58 0 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 0 0
59 0 ▶ EXTEND(SP, 1); PUSHs(text); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); PUSHs(text); PUTBACK; 0 0 0
60 0 count = call_pv("File::Raw::JSON::file_json_decode", G_SCALAR | G_EVAL);
61 0 SPAGAIN;
62 0 ▶ if (!SvTRUE(ERRSV) && count > 0) { SV *r = POPs; if (SvROK(r)) ret = newSVsv(r); }
Branch: 4 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && count > 0) { SV *r = POPs; if (SvROK(r)) ret = newSVsv(r); } 0 0 0
if (!SvTRUE(ERRSV) && count > 0) { SV *r = POPs; if (SvROK(r)) ret = newSVsv(r); } 0 0 0
if (!SvTRUE(ERRSV) && count > 0) { SV *r = POPs; if (SvROK(r)) ret = newSVsv(r); } 0 0 0
if (!SvTRUE(ERRSV) && count > 0) { SV *r = POPs; if (SvROK(r)) ret = newSVsv(r); } 0 0 0
63 0 ▶ else if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) (void)POPs; 0 0 0
64 0 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 0 0 0
65 0 ▶ return ret ? sv_2mortal(ret) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return ret ? sv_2mortal(ret) : NULL; 0 0 0
66 }
67  
68 /* GET an http(s) URI via the ABI and decode the body; mortal ref, or NULL. */
69 1 static SV *jsf_fetch_uri(pTHX_ const char *uri, STRLEN ulen) {
70 1 SV *f, *body = NULL, *url;
71 1 ▶ if (!((ulen >= 7 && strnEQ(uri, "http://", 7)) ||
Branch: 3 branches
BranchTrueFalseTotal
if (!((ulen >= 7 && strnEQ(uri, "http://", 7)) || 1 0 1
if (!((ulen >= 7 && strnEQ(uri, "http://", 7)) || 1 0 1
if (!((ulen >= 7 && strnEQ(uri, "http://", 7)) || 1 0 1
72 1 ▶ (ulen >= 8 && strnEQ(uri, "https://", 8)))) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
(ulen >= 8 && strnEQ(uri, "https://", 8)))) return NULL; 0 1 1
73 1 ▶ if (!jsf_fetch_init(aTHX)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!jsf_fetch_init(aTHX)) return NULL; 0 1 1
74 1 ▶ if (!JSF_FETCH_UA) JSF_FETCH_UA = JSF_FETCH->ua_new(aTHX_ NULL, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (!JSF_FETCH_UA) JSF_FETCH_UA = JSF_FETCH->ua_new(aTHX_ NULL, 0); 1 0 1
75 1 ▶ if (!JSF_FETCH_UA) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!JSF_FETCH_UA) return NULL; 0 1 1
76  
77 1 url = sv_2mortal(newSVpvn(uri, ulen)); /* NUL-terminated by newSVpvn */
78 1 f = JSF_FETCH->request(aTHX_ JSF_FETCH_UA, "GET", SvPVX(url),
79 NULL, 0, NULL, 0, 30.0, -1, jsf_fetch_map, NULL);
80 1 ▶ if (!f) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!f) return NULL; 0 1 1
81 {
82 1 dSP; int count;
83 1 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 1 1
84 1 ▶ EXTEND(SP, 1); PUSHs(sv_2mortal(f)); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); PUSHs(sv_2mortal(f)); PUTBACK; 0 1 1
85 1 count = call_method("get", G_SCALAR | G_EVAL);
86 1 SPAGAIN;
87 1 ▶ if (!SvTRUE(ERRSV) && count > 0) { SV *r = POPs; if (SvOK(r)) body = newSVsv(r); }
Branch: 4 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && count > 0) { SV *r = POPs; if (SvOK(r)) body = newSVsv(r); } 1 0 1
if (!SvTRUE(ERRSV) && count > 0) { SV *r = POPs; if (SvOK(r)) body = newSVsv(r); } 1 0 1
if (!SvTRUE(ERRSV) && count > 0) { SV *r = POPs; if (SvOK(r)) body = newSVsv(r); } 1 0 1
if (!SvTRUE(ERRSV) && count > 0) { SV *r = POPs; if (SvOK(r)) body = newSVsv(r); } 0 1 1
88 0 ▶ else if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) (void)POPs; 0 0 0
89 1 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 1 0 1
90 }
91 1 ▶ if (!body) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!body) return NULL; 1 0 1
92 0 return jsf_fetch_decode(aTHX_ sv_2mortal(body));
93 }
94  
95 #endif /* JSF_FETCH_H */
« blib/arch/JSON/Schema/Fast/Install/Files.pm ↑ Summary include/jsf_frj.h »