| 1 |
|
| /* ft_obs_perl.h - the Perl door onto the fetch_abi v2 observer registry. |
| 2 |
|
| * |
| 3 |
|
| * ft_obs.h holds C function pointers, which is what an instrumented consumer |
| 4 |
|
| * written in XS wants and what keeps an uninstrumented request at one branch. |
| 5 |
|
| * This registers a pair of SHIMS into that same table whose user data is a |
| 6 |
|
| * pair of Perl coderefs, so an application that is not an XS module can |
| 7 |
|
| * observe its own outbound requests: |
| 8 |
|
| * |
| 9 |
|
| * Fetch->on_request(\&start, \&done); |
| 10 |
|
| * |
| 11 |
|
| * The contract is deliberately identical to the C one rather than friendlier |
| 12 |
|
| * - process-global, fired per hop, no deregistration - so the two doors |
| 13 |
|
| * cannot drift into different semantics. What differs is the cost (one |
| 14 |
|
| * call_sv per hop, paid by the process that asked for it) and one rule C |
| 15 |
|
| * could state and Perl cannot enforce: NEITHER CALLBACK MAY CROAK. A C |
| 16 |
|
| * consumer honours that by construction; a Perl one dies on a typo, and the |
| 17 |
|
| * die would come up through whatever the event loop was doing and take an |
| 18 |
|
| * unrelated request down with it. So both shims run under G_EVAL and turn a |
| 19 |
|
| * death into a warning naming which half died - an observer is a bystander, |
| 20 |
|
| * and a broken bystander must not fail the request it was watching. |
| 21 |
|
| * |
| 22 |
|
| * Included after ft_obs.h, which holds the table it registers into. |
| 23 |
|
| */ |
| 24 |
|
| |
| 25 |
|
| #ifndef FT_OBS_PERL_H |
| 26 |
|
| #define FT_OBS_PERL_H |
| 27 |
|
| |
| 28 |
|
| #include "ft_obs.h" |
| 29 |
|
| |
| 30 |
|
| typedef struct { SV *start; SV *done; } ft_obs_perl; |
| 31 |
|
| |
| 32 |
|
| /* The start shim. The header list arrives as an arrayref aliasing the SAME |
| 33 |
|
| * AV the request is being built from, so a push in the callback reaches the |
| 34 |
|
| * wire - which is the entire point of the hook. Whatever the callback returns |
| 35 |
|
| * becomes the token: any Perl scalar, kept until done, which frees it. */ |
| 36 |
16 |
| static void *ft_obs_perl_start(pTHX_ const char *method, STRLEN mlen, |
| 37 |
|
| const char *url, STRLEN ulen, AV *headers, |
| 38 |
|
| void *ud) { |
| 39 |
16 |
| ft_obs_perl *p = (ft_obs_perl *)ud; |
| 40 |
16 |
| SV *tok = NULL; |
| 41 |
16 |
| dSP; |
| 42 |
|
| int count; |
| 43 |
16 |
▶ | if (!p || !p->start) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!p || !p->start) return NULL; |
16 |
0 |
16 |
| if (!p || !p->start) return NULL; |
0 |
16 |
16 |
|
| 44 |
16 |
| ENTER; SAVETMPS; |
| 45 |
16 |
▶ | PUSHMARK(SP); EXTEND(SP, 3); |
Branch: 2 branches
| Branch | True | False | Total |
| PUSHMARK(SP); EXTEND(SP, 3); |
0 |
16 |
16 |
| PUSHMARK(SP); EXTEND(SP, 3); |
0 |
16 |
16 |
|
| 46 |
16 |
| mPUSHp(method, mlen); |
| 47 |
16 |
| mPUSHp(url, ulen); |
| 48 |
16 |
| mPUSHs(newRV_inc((SV *)headers)); |
| 49 |
16 |
| PUTBACK; |
| 50 |
16 |
| count = call_sv(p->start, G_SCALAR | G_EVAL); |
| 51 |
16 |
| SPAGAIN; |
| 52 |
16 |
▶ | if (SvTRUE(ERRSV)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) { |
16 |
0 |
16 |
| if (SvTRUE(ERRSV)) { |
2 |
14 |
16 |
|
| 53 |
2 |
▶ | if (count > 0) (void)POPs; |
Branch: 1 branch
| Branch | True | False | Total |
| if (count > 0) (void)POPs; |
2 |
0 |
2 |
|
| 54 |
4 |
▶ | warn("Fetch: on_request start callback died: %s", SvPV_nolen(ERRSV)); |
Branch: 1 branch
| Branch | True | False | Total |
| warn("Fetch: on_request start callback died: %s", SvPV_nolen(ERRSV)); |
2 |
0 |
2 |
|
| 55 |
|
| } |
| 56 |
14 |
▶ | else if (count > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (count > 0) { |
14 |
0 |
14 |
|
| 57 |
14 |
| SV *r = POPs; |
| 58 |
14 |
▶ | if (SvOK(r)) tok = newSVsv(r); /* +1; the done shim frees it */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(r)) tok = newSVsv(r); /* +1; the done shim frees it */ |
11 |
3 |
14 |
|
| 59 |
|
| } |
| 60 |
16 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
16 |
0 |
16 |
|
| 61 |
16 |
| return (void *)tok; |
| 62 |
|
| } |
| 63 |
|
| |
| 64 |
|
| /* The done shim. Fires for every start, so it is also where the token dies - |
| 65 |
|
| * including when the caller registered no done callback at all. */ |
| 66 |
16 |
| static void ft_obs_perl_done(pTHX_ void *token, SV *res, SV *err, void *ud) { |
| 67 |
16 |
| ft_obs_perl *p = (ft_obs_perl *)ud; |
| 68 |
16 |
| SV *tok = (SV *)token; |
| 69 |
16 |
▶ | if (p && p->done) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (p && p->done) { |
16 |
0 |
16 |
| if (p && p->done) { |
12 |
4 |
16 |
|
| 70 |
12 |
| dSP; |
| 71 |
12 |
| ENTER; SAVETMPS; |
| 72 |
12 |
▶ | PUSHMARK(SP); EXTEND(SP, 3); |
Branch: 2 branches
| Branch | True | False | Total |
| PUSHMARK(SP); EXTEND(SP, 3); |
0 |
12 |
12 |
| PUSHMARK(SP); EXTEND(SP, 3); |
0 |
12 |
12 |
|
| 73 |
12 |
▶ | PUSHs(tok ? sv_2mortal(newSVsv(tok)) : &PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHs(tok ? sv_2mortal(newSVsv(tok)) : &PL_sv_undef); |
11 |
1 |
12 |
|
| 74 |
12 |
▶ | PUSHs(res ? res : &PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHs(res ? res : &PL_sv_undef); |
9 |
3 |
12 |
|
| 75 |
12 |
▶ | PUSHs(err ? err : &PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHs(err ? err : &PL_sv_undef); |
3 |
9 |
12 |
|
| 76 |
12 |
| PUTBACK; |
| 77 |
12 |
| call_sv(p->done, G_DISCARD | G_EVAL); |
| 78 |
12 |
▶ | SPAGAIN; PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| SPAGAIN; PUTBACK; FREETMPS; LEAVE; |
11 |
1 |
12 |
|
| 79 |
12 |
▶ | if (SvTRUE(ERRSV)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) |
12 |
0 |
12 |
| if (SvTRUE(ERRSV)) |
1 |
11 |
12 |
|
| 80 |
2 |
▶ | warn("Fetch: on_request done callback died: %s", |
Branch: 1 branch
| Branch | True | False | Total |
| warn("Fetch: on_request done callback died: %s", |
1 |
0 |
1 |
|
| 81 |
|
| SvPV_nolen(ERRSV)); |
| 82 |
|
| } |
| 83 |
16 |
▶ | if (tok) SvREFCNT_dec(tok); |
Branch: 1 branch
| Branch | True | False | Total |
| if (tok) SvREFCNT_dec(tok); |
11 |
5 |
16 |
|
| 84 |
16 |
| } |
| 85 |
|
| |
| 86 |
|
| /* Fetch->on_request(\&start, \&done). A non-coderef croaks here, in the |
| 87 |
|
| * caller's own frame: that is a mistake at registration time, and finding out |
| 88 |
|
| * about it during an unrelated request would be far worse. `done` is |
| 89 |
|
| * optional. */ |
| 90 |
8 |
| static int ft_obs_add_perl(pTHX_ SV *start, SV *done) { |
| 91 |
|
| ft_obs_perl *p; |
| 92 |
8 |
▶ | int have_done = (done && SvOK(done)); |
Branch: 2 branches
| Branch | True | False | Total |
| int have_done = (done && SvOK(done)); |
8 |
0 |
8 |
| int have_done = (done && SvOK(done)); |
4 |
4 |
8 |
|
| 93 |
8 |
▶ | if (!(start && SvROK(start) && SvTYPE(SvRV(start)) == SVt_PVCV)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(start && SvROK(start) && SvTYPE(SvRV(start)) == SVt_PVCV)) |
8 |
0 |
8 |
| if (!(start && SvROK(start) && SvTYPE(SvRV(start)) == SVt_PVCV)) |
6 |
2 |
8 |
| if (!(start && SvROK(start) && SvTYPE(SvRV(start)) == SVt_PVCV)) |
0 |
6 |
6 |
|
| 94 |
2 |
| croak("Fetch->on_request: the start callback must be a code reference"); |
| 95 |
6 |
▶ | if (have_done && !(SvROK(done) && SvTYPE(SvRV(done)) == SVt_PVCV)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (have_done && !(SvROK(done) && SvTYPE(SvRV(done)) == SVt_PVCV)) |
4 |
2 |
6 |
| if (have_done && !(SvROK(done) && SvTYPE(SvRV(done)) == SVt_PVCV)) |
2 |
2 |
4 |
| if (have_done && !(SvROK(done) && SvTYPE(SvRV(done)) == SVt_PVCV)) |
0 |
2 |
2 |
|
| 96 |
2 |
| croak("Fetch->on_request: the done callback must be a code reference"); |
| 97 |
4 |
| Newxz(p, 1, ft_obs_perl); |
| 98 |
4 |
| p->start = newSVsv(start); |
| 99 |
4 |
▶ | p->done = have_done ? newSVsv(done) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| p->done = have_done ? newSVsv(done) : NULL; |
2 |
2 |
4 |
|
| 100 |
|
| /* the done shim is registered either way: it owns freeing the token */ |
| 101 |
4 |
▶ | if (!ft_obs_add(aTHX_ ft_obs_perl_start, ft_obs_perl_done, p)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ft_obs_add(aTHX_ ft_obs_perl_start, ft_obs_perl_done, p)) { |
0 |
4 |
4 |
|
| 102 |
0 |
| SvREFCNT_dec(p->start); |
| 103 |
0 |
▶ | if (p->done) SvREFCNT_dec(p->done); |
Branch: 1 branch
| Branch | True | False | Total |
| if (p->done) SvREFCNT_dec(p->done); |
0 |
0 |
0 |
|
| 104 |
0 |
| Safefree(p); |
| 105 |
0 |
| return 0; |
| 106 |
|
| } |
| 107 |
4 |
| return 1; |
| 108 |
|
| } |
| 109 |
|
| |
| 110 |
|
| #endif /* FT_OBS_PERL_H */ |