include/fetch/ft_abi.h

filter: Statementstmt 57.0% 203 / 356 Branchbran 35.0% 145 / 414 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 45.1% 348 / 770 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/fetch.xs ↑ Summary xs/websocket.xs »
1 #ifndef FT_ABI_H
2 #define FT_ABI_H
3  
4 /* Fetch-side implementation of the shared C ABI (fetch_abi.h). Included by
5 * Fetch.xs AFTER ft_ua.h / ft_future.h, so ft_ua_of, ft_follow and the hmf_*
6 * future helpers are in scope. Everything here is private to Fetch's
7 * translation unit; consumers reach it only through the FETCH_ABI table
8 * returned by Fetch::_abi_ptr. */
9  
10 #include "fetch_abi.h"
11  
12 /* Construct a Fetch UA from flat key/value SV pairs (kv[0]=key, kv[1]=val,...);
13 * the shared body of Fetch->new and the ABI ua_new. Blesses into `cls`. */
14 55 static SV *ft_ua_new(pTHX_ const char *cls, SV **kv, int nkv) {
15 ft_ua *ua;
16 55 SV *loop_arg = NULL, *headers_arg = NULL, *agent_arg = NULL, *jar_arg = NULL;
17 55 int have_keep = 0, keep = 1, have_verify = 0, verify = 1;
18 55 int have_maxr = 0, maxr = 5, pool_size = 32, simple = 0;
19 55 double timeout = 0.0;
20 int i;
21 82 ▶ for (i = 0; i + 1 < nkv; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < nkv; i += 2) { 27 55 82
22 27 const char *k = SvPV_nolen(kv[i]);
23 27 SV *v = kv[i + 1];
24 27 ▶ if (strEQ(k, "loop")) loop_arg = v;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "loop")) loop_arg = v; 9 18 27
25 18 ▶ else if (strEQ(k, "headers")) headers_arg = v;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "headers")) headers_arg = v; 0 18 18
26 18 ▶ else if (strEQ(k, "agent")) agent_arg = v;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "agent")) agent_arg = v; 1 17 18
27 17 ▶ else if (strEQ(k, "cookie_jar")) jar_arg = v;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "cookie_jar")) jar_arg = v; 2 15 17
28 15 ▶ else if (strEQ(k, "keep_alive")) { have_keep = 1; keep = SvTRUE(v) ? 1 : 0; }
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "keep_alive")) { have_keep = 1; keep = SvTRUE(v) ? 1 : 0; } 2 13 15
29 13 ▶ else if (strEQ(k, "tls_verify")) { have_verify = 1; verify = SvTRUE(v) ? 1 : 0; }
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "tls_verify")) { have_verify = 1; verify = SvTRUE(v) ? 1 : 0; } 0 13 13
30 13 ▶ else if (strEQ(k, "max_redirects")) { have_maxr = 1; maxr = (int)SvIV(v); }
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "max_redirects")) { have_maxr = 1; maxr = (int)SvIV(v); } 1 12 13
31 12 ▶ else if (strEQ(k, "timeout")) timeout = SvNV(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "timeout")) timeout = SvNV(v); 12 0 12
32 0 ▶ else if (strEQ(k, "pool_size")) pool_size = (int)SvIV(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "pool_size")) pool_size = (int)SvIV(v); 0 0 0
33 0 ▶ else if (strEQ(k, "simple_response")) simple = SvTRUE(v) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "simple_response")) simple = SvTRUE(v) ? 1 : 0; 0 0 0
34 }
35 55 Newxz(ua, 1, ft_ua);
36 55 ua->loop = ft_resolve_loop(aTHX_ loop_arg);
37 55 ▶ if (ft_obj_can(aTHX_ ua->loop, "install_await")) {
Branch: 1 branch
BranchTrueFalseTotal
if (ft_obj_can(aTHX_ ua->loop, "install_await")) { 55 0 55
38 55 dSP;
39 55 ▶ ENTER; SAVETMPS; PUSHMARK(SP); XPUSHs(ua->loop); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); XPUSHs(ua->loop); PUTBACK; 0 55 55
ENTER; SAVETMPS; PUSHMARK(SP); XPUSHs(ua->loop); PUTBACK; 0 55 55
40 55 call_method("install_await", G_DISCARD);
41 55 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 55 0 55
42 }
43 /* Hyperman-direct mode: when the adapter is Fetch::Loop::Hyperman and
44 * Hyperman's C ABI resolves (ft_hm.h), connections drive fd interest and
45 * deadlines straight through the table. Absent/old Hyperman leaves both
46 * NULL and the Perl _ft_arm/_ft_timer seam is used as before. */
47 55 ▶ if (sv_isobject(ua->loop)
Branch: 1 branch
BranchTrueFalseTotal
if (sv_isobject(ua->loop) 55 0 55
48 55 ▶ && sv_derived_from(ua->loop, "Fetch::Loop::Hyperman")
Branch: 1 branch
BranchTrueFalseTotal
&& sv_derived_from(ua->loop, "Fetch::Loop::Hyperman") 0 55 55
49 0 ▶ && SvTYPE(SvRV(ua->loop)) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(ua->loop)) == SVt_PVHV) { 0 0 0
50 0 const hm_abi *A = ft_hm(aTHX);
51 0 SV **e = hv_fetchs((HV *)SvRV(ua->loop), "loop", 0);
52 0 ▶ if (A && e && *e && sv_isobject(*e)
Branch: 4 branches
BranchTrueFalseTotal
if (A && e && *e && sv_isobject(*e) 0 0 0
if (A && e && *e && sv_isobject(*e) 0 0 0
if (A && e && *e && sv_isobject(*e) 0 0 0
if (A && e && *e && sv_isobject(*e) 0 0 0
53 0 ▶ && sv_derived_from(*e, "Hyperman::Loop")) {
Branch: 1 branch
BranchTrueFalseTotal
&& sv_derived_from(*e, "Hyperman::Loop")) { 0 0 0
54 0 ua->hm_loop = A->loop_of_sv(aTHX_ *e);
55 0 ua->hm = A;
56 /* C await: bridge + run_until instead of the Perl AWAIT sub */
57 0 ft_hm_install_await(aTHX_ ua->loop, ua->hm_loop);
58 }
59 }
60 55 ▶ ua->keep_alive = have_keep ? keep : 1;
Branch: 1 branch
BranchTrueFalseTotal
ua->keep_alive = have_keep ? keep : 1; 2 53 55
61 55 ua->simple_response = simple;
62 55 ▶ if (jar_arg && SvOK(jar_arg)) {
Branch: 2 branches
BranchTrueFalseTotal
if (jar_arg && SvOK(jar_arg)) { 2 53 55
if (jar_arg && SvOK(jar_arg)) { 2 0 2
63 2 ▶ if (SvROK(jar_arg)) ua->cookie_jar = SvREFCNT_inc(jar_arg);
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(jar_arg)) ua->cookie_jar = SvREFCNT_inc(jar_arg); 0 2 2
64 2 ▶ else if (SvTRUE(jar_arg)) ua->cookie_jar = ft_load_new(aTHX_ "Fetch::CookieJar", NULL);
Branch: 1 branch
BranchTrueFalseTotal
else if (SvTRUE(jar_arg)) ua->cookie_jar = ft_load_new(aTHX_ "Fetch::CookieJar", NULL); 2 0 2
65 }
66 {
67 55 AV *hav = newAV();
68 55 ▶ if (headers_arg && SvOK(headers_arg))
Branch: 2 branches
BranchTrueFalseTotal
if (headers_arg && SvOK(headers_arg)) 0 55 55
if (headers_arg && SvOK(headers_arg)) 0 0 0
69 0 ft_hdr_pairs_into(aTHX_ hav, headers_arg);
70 55 ua->headers = sv_bless(newRV_noinc((SV *)hav),
71 gv_stashpv("Fetch::Headers", GV_ADD));
72 }
73 55 ▶ if (agent_arg && SvOK(agent_arg)) {
Branch: 2 branches
BranchTrueFalseTotal
if (agent_arg && SvOK(agent_arg)) { 1 54 55
if (agent_arg && SvOK(agent_arg)) { 1 0 1
74 1 ua->agent = newSVsv(agent_arg);
75 } else {
76 54 SV *ver = get_sv("Fetch::VERSION", 0);
77 108 ▶ ua->agent = newSVpvf("Fetch/%s", (ver && SvOK(ver)) ? SvPV_nolen(ver) : "0");
Branch: 2 branches
BranchTrueFalseTotal
ua->agent = newSVpvf("Fetch/%s", (ver && SvOK(ver)) ? SvPV_nolen(ver) : "0"); 54 0 54
ua->agent = newSVpvf("Fetch/%s", (ver && SvOK(ver)) ? SvPV_nolen(ver) : "0"); 54 0 54
78 }
79 55 ▶ ua->tls_verify = have_verify ? verify : 1;
Branch: 1 branch
BranchTrueFalseTotal
ua->tls_verify = have_verify ? verify : 1; 0 55 55
80 55 ▶ ua->max_redirects = have_maxr ? maxr : 5;
Branch: 1 branch
BranchTrueFalseTotal
ua->max_redirects = have_maxr ? maxr : 5; 1 54 55
81 55 ua->timeout = timeout;
82 55 ▶ if (ua->keep_alive) {
Branch: 1 branch
BranchTrueFalseTotal
if (ua->keep_alive) { 54 1 55
83 54 ▶ ft_pool *p = ft_pool_new(pool_size > 0 ? pool_size : 32);
Branch: 1 branch
BranchTrueFalseTotal
ft_pool *p = ft_pool_new(pool_size > 0 ? pool_size : 32); 54 0 54
84 54 ▶ if (!p) { SvREFCNT_dec(ua->loop); Safefree(ua); croak("Fetch: out of memory"); }
Branch: 1 branch
BranchTrueFalseTotal
if (!p) { SvREFCNT_dec(ua->loop); Safefree(ua); croak("Fetch: out of memory"); } 0 54 54
85 54 ua->pool = sv_bless(newRV_noinc(newSViv(PTR2IV(p))),
86 gv_stashpv("Fetch::_Pool", GV_ADD));
87 }
88 55 return sv_bless(newRV_noinc(newSViv(PTR2IV(ua))), gv_stashpv(cls, GV_ADD));
89 }
90  
91 /* A second agent over this one's connection pool and loop, with some options
92 * replaced.
93 *
94 * The case this exists for is a per-request cookie jar. A jar on a long-lived
95 * agent is shared by every request that agent serves, which leaks between them
96 * the moment the cookies identify an end user rather than the application; and
97 * building a whole fresh agent per request to avoid that throws away the
98 * keep-alive pool and re-resolves the loop adapter, which is most of what
99 * new() costs. A clone gives the caller its own jar over the same pool.
100 *
101 * Sharing is safe because DESTROY only releases references: the pool and the
102 * loop live until the last agent holding them goes. `loop` and `pool_size`
103 * cannot be overridden - they are the things being shared, and a clone on a
104 * different loop would be driving the parent's parked connections from the
105 * wrong place. */
106 8 static SV *ft_ua_clone(pTHX_ SV *self, SV **kv, int nkv) {
107 8 ft_ua *src = ft_ua_of(aTHX_ self);
108 ft_ua *ua;
109 8 SV *headers_arg = NULL, *agent_arg = NULL, *jar_arg = NULL;
110 8 int have_jar = 0, have_keep = 0, keep = 0, have_verify = 0, verify = 0;
111 8 int have_maxr = 0, maxr = 0, have_simple = 0, simple = 0, have_to = 0;
112 8 double timeout = 0.0;
113 const char *cls;
114 int i;
115  
116 8 ▶ if (!(SvROK(self) && SvOBJECT(SvRV(self))))
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(self) && SvOBJECT(SvRV(self)))) 8 0 8
if (!(SvROK(self) && SvOBJECT(SvRV(self)))) 0 8 8
117 0 croak("Fetch->clone: not a Fetch user agent");
118 8 ▶ cls = HvNAME(SvSTASH(SvRV(self)));
Branch: 6 branches
BranchTrueFalseTotal
cls = HvNAME(SvSTASH(SvRV(self))); 8 0 8
cls = HvNAME(SvSTASH(SvRV(self))); 8 0 8
cls = HvNAME(SvSTASH(SvRV(self))); 0 8 8
cls = HvNAME(SvSTASH(SvRV(self))); 0 0 0
cls = HvNAME(SvSTASH(SvRV(self))); 8 0 8
cls = HvNAME(SvSTASH(SvRV(self))); 0 8 8
119  
120 13 ▶ for (i = 0; i + 1 < nkv; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < nkv; i += 2) { 7 6 13
121 7 const char *k = SvPV_nolen(kv[i]);
122 7 SV *v = kv[i + 1];
123 7 ▶ if (strEQ(k, "cookie_jar")) { have_jar = 1; jar_arg = v; }
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "cookie_jar")) { have_jar = 1; jar_arg = v; } 5 2 7
124 2 ▶ else if (strEQ(k, "headers")) headers_arg = v;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "headers")) headers_arg = v; 0 2 2
125 2 ▶ else if (strEQ(k, "agent")) agent_arg = v;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "agent")) agent_arg = v; 0 2 2
126 2 ▶ else if (strEQ(k, "keep_alive")) { have_keep = 1; keep = SvTRUE(v) ? 1 : 0; }
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "keep_alive")) { have_keep = 1; keep = SvTRUE(v) ? 1 : 0; } 0 2 2
127 2 ▶ else if (strEQ(k, "tls_verify")) { have_verify = 1; verify = SvTRUE(v) ? 1 : 0; }
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "tls_verify")) { have_verify = 1; verify = SvTRUE(v) ? 1 : 0; } 0 2 2
128 2 ▶ else if (strEQ(k, "max_redirects")) { have_maxr = 1; maxr = (int)SvIV(v); }
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "max_redirects")) { have_maxr = 1; maxr = (int)SvIV(v); } 0 2 2
129 2 ▶ else if (strEQ(k, "timeout")) { have_to = 1; timeout = SvNV(v); }
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "timeout")) { have_to = 1; timeout = SvNV(v); } 0 2 2
130 2 ▶ else if (strEQ(k, "simple_response")){ have_simple = 1; simple = SvTRUE(v) ? 1 : 0; }
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "simple_response")){ have_simple = 1; simple = SvTRUE(v) ? 1 : 0; } 0 2 2
131 2 ▶ else if (strEQ(k, "loop") || strEQ(k, "pool_size"))
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "loop") || strEQ(k, "pool_size")) 1 1 2
else if (strEQ(k, "loop") || strEQ(k, "pool_size")) 1 0 1
132 2 croak("Fetch->clone: '%s' cannot be overridden on a clone; the "
133 "loop and the connection pool are what it shares", k);
134 }
135  
136 6 Newxz(ua, 1, ft_ua);
137 /* shared with the parent, by reference */
138 6 ▶ ua->loop = src->loop ? SvREFCNT_inc(src->loop) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ua->loop = src->loop ? SvREFCNT_inc(src->loop) : NULL; 6 0 6
139 6 ▶ ua->pool = src->pool ? SvREFCNT_inc(src->pool) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ua->pool = src->pool ? SvREFCNT_inc(src->pool) : NULL; 6 0 6
140 6 ua->hm = src->hm;
141 6 ua->hm_loop = src->hm_loop;
142 /* inherited unless overridden */
143 6 ▶ ua->keep_alive = have_keep ? keep : src->keep_alive;
Branch: 1 branch
BranchTrueFalseTotal
ua->keep_alive = have_keep ? keep : src->keep_alive; 6 0 6
144 6 ▶ ua->tls_verify = have_verify ? verify : src->tls_verify;
Branch: 1 branch
BranchTrueFalseTotal
ua->tls_verify = have_verify ? verify : src->tls_verify; 6 0 6
145 6 ▶ ua->max_redirects = have_maxr ? maxr : src->max_redirects;
Branch: 1 branch
BranchTrueFalseTotal
ua->max_redirects = have_maxr ? maxr : src->max_redirects; 6 0 6
146 6 ▶ ua->simple_response = have_simple ? simple : src->simple_response;
Branch: 1 branch
BranchTrueFalseTotal
ua->simple_response = have_simple ? simple : src->simple_response; 6 0 6
147 6 ▶ ua->timeout = have_to ? timeout : src->timeout;
Branch: 1 branch
BranchTrueFalseTotal
ua->timeout = have_to ? timeout : src->timeout; 6 0 6
148  
149 6 ▶ if (have_jar) {
Branch: 1 branch
BranchTrueFalseTotal
if (have_jar) { 5 1 6
150 /* an explicit undef/0 means "no jar", which is how a clone drops one */
151 5 ▶ if (jar_arg && SvOK(jar_arg)) {
Branch: 2 branches
BranchTrueFalseTotal
if (jar_arg && SvOK(jar_arg)) { 5 0 5
if (jar_arg && SvOK(jar_arg)) { 4 1 5
152 4 ▶ if (SvROK(jar_arg)) ua->cookie_jar = SvREFCNT_inc(jar_arg);
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(jar_arg)) ua->cookie_jar = SvREFCNT_inc(jar_arg); 0 4 4
153 8 ▶ else if (SvTRUE(jar_arg)) ua->cookie_jar =
Branch: 1 branch
BranchTrueFalseTotal
else if (SvTRUE(jar_arg)) ua->cookie_jar = 4 0 4
154 4 ft_load_new(aTHX_ "Fetch::CookieJar", NULL);
155 }
156 }
157 1 ▶ else ua->cookie_jar = src->cookie_jar ? SvREFCNT_inc(src->cookie_jar) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
else ua->cookie_jar = src->cookie_jar ? SvREFCNT_inc(src->cookie_jar) : NULL; 1 0 1
158  
159 /* Headers are copied, not shared: a clone that mutated a shared list would
160 * be writing into every other agent's defaults. */
161 {
162 6 AV *hav = newAV();
163 6 ▶ if (src->headers) ft_hdr_pairs_into(aTHX_ hav, src->headers);
Branch: 1 branch
BranchTrueFalseTotal
if (src->headers) ft_hdr_pairs_into(aTHX_ hav, src->headers); 6 0 6
164 6 ▶ if (headers_arg && SvOK(headers_arg))
Branch: 2 branches
BranchTrueFalseTotal
if (headers_arg && SvOK(headers_arg)) 0 6 6
if (headers_arg && SvOK(headers_arg)) 0 0 0
165 0 ft_hdr_pairs_into(aTHX_ hav, headers_arg);
166 6 ua->headers = sv_bless(newRV_noinc((SV *)hav),
167 gv_stashpv("Fetch::Headers", GV_ADD));
168 }
169  
170 0 ▶ ua->agent = (agent_arg && SvOK(agent_arg))
Branch: 1 branch
BranchTrueFalseTotal
ua->agent = (agent_arg && SvOK(agent_arg)) 0 0 0
171 0 ? newSVsv(agent_arg)
172 6 ▶ : (src->agent ? SvREFCNT_inc(src->agent) : NULL);
Branch: 2 branches
BranchTrueFalseTotal
: (src->agent ? SvREFCNT_inc(src->agent) : NULL); 0 6 6
: (src->agent ? SvREFCNT_inc(src->agent) : NULL); 6 0 6
173  
174 6 return sv_bless(newRV_noinc(newSViv(PTR2IV(ua))), gv_stashpv(cls, GV_ADD));
175 }
176  
177 0 static SV *ft_abi_ua_new(pTHX_ SV **kv, int nkv) {
178 0 return ft_ua_new(aTHX_ "Fetch", kv, nkv);
179 }
180  
181 /* carried through the upstream future to the completion callback */
182 typedef struct ft_abi_ctx { fetch_map_cb map; void *ud; } ft_abi_ctx;
183  
184 /* Fires when the upstream request settles: read the response parts in C, let
185 * the consumer's `map` shape the resolved value, settle the derived future
186 * with it. Mirrors ft_redirect_cb's ownership (next lives in the closure). */
187 XS_INTERNAL(ft_abi_complete_cb);
188 0 XS_INTERNAL(ft_abi_complete_cb) {
189 0 dXSARGS;
190 0 hm_clos *cl = hm_clos_of(aTHX_ cv);
191 0 SV *f, *next, *val, *err = NULL;
192 ft_abi_ctx *ctx;
193 0 int ok = 0, status = 0;
194 0 AV *headers = NULL;
195 0 SV *body = NULL;
196 IV st;
197 0 ▶ if (!cl || items < 1) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!cl || items < 1) XSRETURN_EMPTY; 0 0 0
if (!cl || items < 1) XSRETURN_EMPTY; 0 0 0
198 0 f = ST(0);
199 0 next = cl->a;
200 0 ctx = INT2PTR(ft_abi_ctx *, cl->i);
201 0 st = hmf_state(aTHX_ f);
202 0 ▶ if (st == HMF_DONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (st == HMF_DONE) { 0 0 0
203 0 AV *vals = hmf_values_av(aTHX_ f);
204 0 ▶ SV **rp = (vals && av_len(vals) >= 0) ? av_fetch(vals, 0, 0) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
SV **rp = (vals && av_len(vals) >= 0) ? av_fetch(vals, 0, 0) : NULL; 0 0 0
SV **rp = (vals && av_len(vals) >= 0) ? av_fetch(vals, 0, 0) : NULL; 0 0 0
205 0 ▶ SV *res = rp ? *rp : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *res = rp ? *rp : NULL; 0 0 0
206 0 ▶ if (res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV) { 0 0 0
if (res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV) { 0 0 0
if (res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV) { 0 0 0
207 0 HV *h = (HV *)SvRV(res);
208 SV **e;
209 0 ok = 1;
210 0 ▶ if ((e = hv_fetchs(h, "status", 0)) && *e) status = (int)SvIV(*e);
Branch: 2 branches
BranchTrueFalseTotal
if ((e = hv_fetchs(h, "status", 0)) && *e) status = (int)SvIV(*e); 0 0 0
if ((e = hv_fetchs(h, "status", 0)) && *e) status = (int)SvIV(*e); 0 0 0
211 0 ▶ if ((e = hv_fetchs(h, "headers", 0)) && *e && SvROK(*e)
Branch: 3 branches
BranchTrueFalseTotal
if ((e = hv_fetchs(h, "headers", 0)) && *e && SvROK(*e) 0 0 0
if ((e = hv_fetchs(h, "headers", 0)) && *e && SvROK(*e) 0 0 0
if ((e = hv_fetchs(h, "headers", 0)) && *e && SvROK(*e) 0 0 0
212 0 ▶ && SvTYPE(SvRV(*e)) == SVt_PVAV) headers = (AV *)SvRV(*e);
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(*e)) == SVt_PVAV) headers = (AV *)SvRV(*e); 0 0 0
213 0 ▶ if ((e = hv_fetchs(h, "content", 0)) && *e) body = *e;
Branch: 2 branches
BranchTrueFalseTotal
if ((e = hv_fetchs(h, "content", 0)) && *e) body = *e; 0 0 0
if ((e = hv_fetchs(h, "content", 0)) && *e) body = *e; 0 0 0
214 }
215 0 ▶ } else if (st == HMF_FAILED) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (st == HMF_FAILED) { 0 0 0
216 0 AV *vals = hmf_values_av(aTHX_ f);
217 0 ▶ SV **e = (vals && av_len(vals) >= 0) ? av_fetch(vals, 0, 0) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
SV **e = (vals && av_len(vals) >= 0) ? av_fetch(vals, 0, 0) : NULL; 0 0 0
SV **e = (vals && av_len(vals) >= 0) ? av_fetch(vals, 0, 0) : NULL; 0 0 0
218 0 ▶ err = e ? *e : NULL;
Branch: 1 branch
BranchTrueFalseTotal
err = e ? *e : NULL; 0 0 0
219 }
220 0 val = ctx->map(aTHX_ ok, status, headers, body, err, ctx->ud);
221 0 ▶ if (!val) val = newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (!val) val = newSV(0); 0 0 0
222 0 hmf_settle(aTHX_ next, HMF_DONE, &val, 1);
223 0 SvREFCNT_dec(val);
224 0 Safefree(ctx);
225 0 XSRETURN_EMPTY;
226 }
227  
228 0 static SV *ft_abi_request(pTHX_ SV *ua_sv, const char *method, const char *url,
229 const fetch_hdr *hdrs, int nhdrs,
230 const char *body, STRLEN blen,
231 double timeout, int max_redirects,
232 fetch_map_cb map, void *ud) {
233 0 ft_ua *ua = ft_ua_of(aTHX_ ua_sv);
234 0 HV *opt = newHV();
235 SV *f, *next, *cb;
236 ft_abi_ctx *ctx;
237 0 ▶ if (nhdrs > 0 && hdrs) {
Branch: 2 branches
BranchTrueFalseTotal
if (nhdrs > 0 && hdrs) { 0 0 0
if (nhdrs > 0 && hdrs) { 0 0 0
238 0 AV *hav = newAV();
239 int i;
240 0 ▶ for (i = 0; i < nhdrs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nhdrs; i++) { 0 0 0
241 0 av_push(hav, newSVpvn(hdrs[i].name, hdrs[i].nlen));
242 0 av_push(hav, newSVpvn(hdrs[i].val, hdrs[i].vlen));
243 }
244 0 (void)hv_stores(opt, "headers", newRV_noinc((SV *)hav));
245 }
246 0 ▶ if (body) (void)hv_stores(opt, "body", newSVpvn(body, blen));
Branch: 1 branch
BranchTrueFalseTotal
if (body) (void)hv_stores(opt, "body", newSVpvn(body, blen)); 0 0 0
247 0 ▶ if (timeout > 0) (void)hv_stores(opt, "timeout", newSVnv(timeout));
Branch: 1 branch
BranchTrueFalseTotal
if (timeout > 0) (void)hv_stores(opt, "timeout", newSVnv(timeout)); 0 0 0
248  
249 0 ▶ f = ft_follow(aTHX_ ua_sv, ua, method, url, opt,
Branch: 1 branch
BranchTrueFalseTotal
f = ft_follow(aTHX_ ua_sv, ua, method, url, opt, 0 0 0
250 0 max_redirects < 0 ? ua->max_redirects : (IV)max_redirects);
251 0 SvREFCNT_dec((SV *)opt);
252  
253 0 next = hmf_new(aTHX_ hmf_class_of(aTHX_ f));
254 0 Newxz(ctx, 1, ft_abi_ctx);
255 0 ctx->map = map;
256 0 ctx->ud = ud;
257 0 hmf_set_upstream(aTHX_ next, f);
258 0 cb = hm_closure(aTHX_ ft_abi_complete_cb, next, NULL, NULL, NULL,
259 PTR2IV(ctx), 0);
260 0 hmf_on_ready(aTHX_ f, cb);
261 0 SvREFCNT_dec(cb);
262 0 SvREFCNT_dec(f); /* the request keeps f alive */
263 0 return next;
264 }
265  
266 0 static void ft_abi_res_parts(pTHX_ SV *res, int *status, AV **headers,
267 SV **body) {
268 HV *h;
269 SV **e;
270 0 ▶ if (status) *status = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (status) *status = 0; 0 0 0
271 0 ▶ if (headers) *headers = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (headers) *headers = NULL; 0 0 0
272 0 ▶ if (body) *body = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (body) *body = NULL; 0 0 0
273 0 ▶ if (!res || !SvROK(res) || SvTYPE(SvRV(res)) != SVt_PVHV) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!res || !SvROK(res) || SvTYPE(SvRV(res)) != SVt_PVHV) return; 0 0 0
if (!res || !SvROK(res) || SvTYPE(SvRV(res)) != SVt_PVHV) return; 0 0 0
if (!res || !SvROK(res) || SvTYPE(SvRV(res)) != SVt_PVHV) return; 0 0 0
274 0 h = (HV *)SvRV(res);
275 0 ▶ if (status && (e = hv_fetchs(h, "status", 0)) && *e) *status = (int)SvIV(*e);
Branch: 3 branches
BranchTrueFalseTotal
if (status && (e = hv_fetchs(h, "status", 0)) && *e) *status = (int)SvIV(*e); 0 0 0
if (status && (e = hv_fetchs(h, "status", 0)) && *e) *status = (int)SvIV(*e); 0 0 0
if (status && (e = hv_fetchs(h, "status", 0)) && *e) *status = (int)SvIV(*e); 0 0 0
276 0 ▶ if (headers && (e = hv_fetchs(h, "headers", 0)) && *e && SvROK(*e)
Branch: 4 branches
BranchTrueFalseTotal
if (headers && (e = hv_fetchs(h, "headers", 0)) && *e && SvROK(*e) 0 0 0
if (headers && (e = hv_fetchs(h, "headers", 0)) && *e && SvROK(*e) 0 0 0
if (headers && (e = hv_fetchs(h, "headers", 0)) && *e && SvROK(*e) 0 0 0
if (headers && (e = hv_fetchs(h, "headers", 0)) && *e && SvROK(*e) 0 0 0
277 0 ▶ && SvTYPE(SvRV(*e)) == SVt_PVAV) *headers = (AV *)SvRV(*e);
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(*e)) == SVt_PVAV) *headers = (AV *)SvRV(*e); 0 0 0
278 0 ▶ if (body && (e = hv_fetchs(h, "content", 0)) && *e) *body = *e;
Branch: 3 branches
BranchTrueFalseTotal
if (body && (e = hv_fetchs(h, "content", 0)) && *e) *body = *e; 0 0 0
if (body && (e = hv_fetchs(h, "content", 0)) && *e) *body = *e; 0 0 0
if (body && (e = hv_fetchs(h, "content", 0)) && *e) *body = *e; 0 0 0
279 }
280  
281 /* ---- streaming: wrap the consumer's C callbacks as Fetch's coderefs ------ */
282  
283 typedef struct ft_abi_sctx {
284 fetch_on_headers on_headers;
285 fetch_on_body on_body;
286 fetch_on_done on_done;
287 void *ud;
288 } ft_abi_sctx;
289  
290 XS_INTERNAL(ft_abi_hdr_tramp);
291 0 XS_INTERNAL(ft_abi_hdr_tramp) {
292 0 dXSARGS;
293 0 hm_clos *cl = hm_clos_of(aTHX_ cv);
294 0 ▶ ft_abi_sctx *c = cl ? INT2PTR(ft_abi_sctx *, cl->i) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ft_abi_sctx *c = cl ? INT2PTR(ft_abi_sctx *, cl->i) : NULL; 0 0 0
295 0 int status = 0;
296 0 AV *headers = NULL;
297 0 ▶ if (c && c->on_headers) {
Branch: 2 branches
BranchTrueFalseTotal
if (c && c->on_headers) { 0 0 0
if (c && c->on_headers) { 0 0 0
298 0 ▶ if (items >= 1 && SvOK(ST(0))) status = (int)SvIV(ST(0));
Branch: 2 branches
BranchTrueFalseTotal
if (items >= 1 && SvOK(ST(0))) status = (int)SvIV(ST(0)); 0 0 0
if (items >= 1 && SvOK(ST(0))) status = (int)SvIV(ST(0)); 0 0 0
299 0 ▶ if (items >= 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVAV)
Branch: 3 branches
BranchTrueFalseTotal
if (items >= 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVAV) 0 0 0
if (items >= 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVAV) 0 0 0
if (items >= 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVAV) 0 0 0
300 0 headers = (AV *)SvRV(ST(1));
301 0 c->on_headers(aTHX_ status, headers, c->ud);
302 }
303 0 XSRETURN_EMPTY;
304 }
305  
306 XS_INTERNAL(ft_abi_body_tramp);
307 0 XS_INTERNAL(ft_abi_body_tramp) {
308 0 dXSARGS;
309 0 hm_clos *cl = hm_clos_of(aTHX_ cv);
310 0 ▶ ft_abi_sctx *c = cl ? INT2PTR(ft_abi_sctx *, cl->i) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ft_abi_sctx *c = cl ? INT2PTR(ft_abi_sctx *, cl->i) : NULL; 0 0 0
311 0 ▶ if (c && c->on_body && items >= 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (c && c->on_body && items >= 1) { 0 0 0
if (c && c->on_body && items >= 1) { 0 0 0
if (c && c->on_body && items >= 1) { 0 0 0
312 STRLEN l;
313 0 const char *p = SvPV_const(ST(0), l);
314 0 c->on_body(aTHX_ p, l, c->ud);
315 }
316 0 XSRETURN_EMPTY;
317 }
318  
319 XS_INTERNAL(ft_abi_done_tramp);
320 0 XS_INTERNAL(ft_abi_done_tramp) {
321 0 dXSARGS;
322 0 hm_clos *cl = hm_clos_of(aTHX_ cv);
323 0 ▶ ft_abi_sctx *c = cl ? INT2PTR(ft_abi_sctx *, cl->i) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
ft_abi_sctx *c = cl ? INT2PTR(ft_abi_sctx *, cl->i) : NULL; 0 0 0
324 0 ▶ SV *f = items >= 1 ? ST(0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *f = items >= 1 ? ST(0) : NULL; 0 0 0
325 0 int ok = 0;
326 0 SV *err = NULL;
327 0 ▶ if (c) {
Branch: 1 branch
BranchTrueFalseTotal
if (c) { 0 0 0
328 0 ▶ IV st = f ? hmf_state(aTHX_ f) : HMF_FAILED;
Branch: 1 branch
BranchTrueFalseTotal
IV st = f ? hmf_state(aTHX_ f) : HMF_FAILED; 0 0 0
329 0 ▶ if (st == HMF_DONE) ok = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (st == HMF_DONE) ok = 1; 0 0 0
330 0 ▶ else if (st == HMF_FAILED) {
Branch: 1 branch
BranchTrueFalseTotal
else if (st == HMF_FAILED) { 0 0 0
331 0 ▶ AV *v = f ? hmf_values_av(aTHX_ f) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
AV *v = f ? hmf_values_av(aTHX_ f) : NULL; 0 0 0
332 0 ▶ SV **e = (v && av_len(v) >= 0) ? av_fetch(v, 0, 0) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
SV **e = (v && av_len(v) >= 0) ? av_fetch(v, 0, 0) : NULL; 0 0 0
SV **e = (v && av_len(v) >= 0) ? av_fetch(v, 0, 0) : NULL; 0 0 0
333 0 ▶ err = e ? *e : NULL;
Branch: 1 branch
BranchTrueFalseTotal
err = e ? *e : NULL; 0 0 0
334 }
335 0 ▶ if (c->on_done) c->on_done(aTHX_ ok, err, c->ud);
Branch: 1 branch
BranchTrueFalseTotal
if (c->on_done) c->on_done(aTHX_ ok, err, c->ud); 0 0 0
336 0 Safefree(c); /* last callback: release the context */
337 }
338 0 XSRETURN_EMPTY;
339 }
340  
341 0 static SV *ft_abi_request_stream(pTHX_ SV *ua_sv, const char *method,
342 const char *url, const fetch_hdr *hdrs,
343 int nhdrs, const char *body, STRLEN blen,
344 double timeout, int max_redirects,
345 fetch_on_headers on_h, fetch_on_body on_b,
346 fetch_on_done on_d, void *ud) {
347 0 ft_ua *ua = ft_ua_of(aTHX_ ua_sv);
348 0 HV *opt = newHV();
349 ft_abi_sctx *ctx;
350 SV *f, *hcb, *bcb, *dcb;
351 0 ▶ if (nhdrs > 0 && hdrs) {
Branch: 2 branches
BranchTrueFalseTotal
if (nhdrs > 0 && hdrs) { 0 0 0
if (nhdrs > 0 && hdrs) { 0 0 0
352 0 AV *hav = newAV();
353 int i;
354 0 ▶ for (i = 0; i < nhdrs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nhdrs; i++) { 0 0 0
355 0 av_push(hav, newSVpvn(hdrs[i].name, hdrs[i].nlen));
356 0 av_push(hav, newSVpvn(hdrs[i].val, hdrs[i].vlen));
357 }
358 0 (void)hv_stores(opt, "headers", newRV_noinc((SV *)hav));
359 }
360 0 ▶ if (body) (void)hv_stores(opt, "body", newSVpvn(body, blen));
Branch: 1 branch
BranchTrueFalseTotal
if (body) (void)hv_stores(opt, "body", newSVpvn(body, blen)); 0 0 0
361 0 ▶ if (timeout > 0) (void)hv_stores(opt, "timeout", newSVnv(timeout));
Branch: 1 branch
BranchTrueFalseTotal
if (timeout > 0) (void)hv_stores(opt, "timeout", newSVnv(timeout)); 0 0 0
362  
363 0 Newxz(ctx, 1, ft_abi_sctx);
364 0 ctx->on_headers = on_h; ctx->on_body = on_b; ctx->on_done = on_d; ctx->ud = ud;
365 0 hcb = hm_closure(aTHX_ ft_abi_hdr_tramp, NULL, NULL, NULL, NULL, PTR2IV(ctx), 0);
366 0 bcb = hm_closure(aTHX_ ft_abi_body_tramp, NULL, NULL, NULL, NULL, PTR2IV(ctx), 0);
367 0 (void)hv_stores(opt, "on_headers", hcb); /* hash takes the ref */
368 0 (void)hv_stores(opt, "on_body", bcb);
369  
370 0 ▶ f = ft_follow(aTHX_ ua_sv, ua, method, url, opt,
Branch: 1 branch
BranchTrueFalseTotal
f = ft_follow(aTHX_ ua_sv, ua, method, url, opt, 0 0 0
371 0 max_redirects < 0 ? ua->max_redirects : (IV)max_redirects);
372 0 SvREFCNT_dec((SV *)opt);
373  
374 0 dcb = hm_closure(aTHX_ ft_abi_done_tramp, NULL, NULL, NULL, NULL, PTR2IV(ctx), 0);
375 0 hmf_on_ready(aTHX_ f, dcb);
376 0 SvREFCNT_dec(dcb);
377 0 return f; /* +1 owned by caller */
378 }
379  
380 /* ---- raw blocking upstream connection (for a proxy's Upgrade tunnel) ----- *
381 * A plain or TLS TCP connection Fetch owns, exposed for blocking read/write/
382 * splice from C. TLS reuses Fetch's client SSL_CTX (SNI, optional verify), so
383 * a consumer tunnels to a wss/https upstream without linking OpenSSL itself.
384 * Pure C (no pTHX): usable directly in a consumer's splice loop. */
385 typedef struct ft_rawconn { int fd; void *ssl; } ft_rawconn;
386  
387 /* The TLS handshake on an already-connected plaintext rawconn: Fetch's client
388 * SSL_CTX, SNI, hostname verification when asked. One function shared by
389 * tunnel_connect (tls=1) and tunnel_starttls (v3), so the two handshakes
390 * cannot drift apart. 0 on success. -1 when the handle is NULL, already TLS,
391 * or the handshake fails - and then the rawconn is left as it was found
392 * (no ssl, fd still open) for the caller to close. -1 always in a build
393 * without TLS. */
394 6 static int ft_abi_tls_wrap(ft_rawconn *rc, const char *host, int verify) {
395 #if FT_TLS_AVAILABLE
396 SSL_CTX *ctx;
397 SSL *ssl;
398 6 ▶ if (!rc || rc->ssl || rc->fd < 0) return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (!rc || rc->ssl || rc->fd < 0) return -1; 3 3 6
if (!rc || rc->ssl || rc->fd < 0) return -1; 3 0 3
if (!rc || rc->ssl || rc->fd < 0) return -1; 0 3 3
399 3 ctx = ft_client_ctx(verify);
400 3 ▶ if (!ctx || !(ssl = SSL_new(ctx))) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (!ctx || !(ssl = SSL_new(ctx))) return -1; 3 0 3
if (!ctx || !(ssl = SSL_new(ctx))) return -1; 0 3 3
401 3 SSL_set_fd(ssl, FT_SSL_FD(rc->fd));
402 3 SSL_set_connect_state(ssl);
403 #ifdef SSL_set_tlsext_host_name
404 3 SSL_set_tlsext_host_name(ssl, host);
405 #endif
406 3 ▶ if (verify) {
Branch: 1 branch
BranchTrueFalseTotal
if (verify) { 1 2 3
407 1 SSL_set1_host(ssl, host);
408 1 SSL_set_hostflags(ssl, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
409 }
410 3 ▶ if (SSL_connect(ssl) != 1) { SSL_free(ssl); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (SSL_connect(ssl) != 1) { SSL_free(ssl); return -1; } 1 2 3
411 2 rc->ssl = ssl;
412 2 return 0;
413 #else
414 (void)rc; (void)host; (void)verify;
415 return -1;
416 #endif
417 }
418  
419 5 static void *ft_abi_tunnel_connect(const char *host, int port, int tls, int verify) {
420 5 struct addrinfo hints, *ai = NULL, *it;
421 char portbuf[16];
422 5 int fd = -1;
423 ft_rawconn *rc;
424 5 memset(&hints, 0, sizeof hints);
425 5 hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM;
426 5 snprintf(portbuf, sizeof portbuf, "%d", port);
427 5 ▶ if (getaddrinfo(host, portbuf, &hints, &ai) != 0) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (getaddrinfo(host, portbuf, &hints, &ai) != 0) return NULL; 0 5 5
428 5 ▶ for (it = ai; it; it = it->ai_next) {
Branch: 1 branch
BranchTrueFalseTotal
for (it = ai; it; it = it->ai_next) { 5 0 5
429 5 fd = ft_os_socket(it->ai_family, it->ai_socktype, it->ai_protocol);
430 5 ▶ if (fd < 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) continue; 0 5 5
431 5 ▶ if (ft_os_connect(fd, it->ai_addr, (int)it->ai_addrlen) == 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (ft_os_connect(fd, it->ai_addr, (int)it->ai_addrlen) == 0) break; 5 0 5
432 0 ft_os_close(fd); fd = -1;
433 }
434 5 freeaddrinfo(ai);
435 5 ▶ if (fd < 0) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) return NULL; 0 5 5
436 5 rc = (ft_rawconn *)calloc(1, sizeof(ft_rawconn));
437 5 ▶ if (!rc) { ft_os_close(fd); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!rc) { ft_os_close(fd); return NULL; } 0 5 5
438 5 rc->fd = fd; rc->ssl = NULL;
439 5 ▶ if (tls && ft_abi_tls_wrap(rc, host, verify) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (tls && ft_abi_tls_wrap(rc, host, verify) != 0) { 0 5 5
if (tls && ft_abi_tls_wrap(rc, host, verify) != 0) { 0 0 0
440 /* handshake failed, or TLS asked for in a build without it */
441 0 ft_os_close(fd); free(rc); return NULL;
442 }
443 5 return rc;
444 }
445  
446 /* v3: STARTTLS. The application protocol has said "upgrade now" on a tunnel
447 * opened with tls=0; this is the handshake. See ft_abi_tls_wrap for the
448 * failure contract. */
449 6 static int ft_abi_tunnel_starttls(void *h, const char *host, int verify) {
450 6 return ft_abi_tls_wrap((ft_rawconn *)h, host, verify);
451 }
452  
453 5 ▶static int ft_abi_tunnel_fd(void *h) { return h ? ((ft_rawconn *)h)->fd : -1; }
Branch: 1 branch
BranchTrueFalseTotal
static int ft_abi_tunnel_fd(void *h) { return h ? ((ft_rawconn *)h)->fd : -1; } 5 0 5
454  
455 110 static IV ft_abi_tunnel_read(void *h, char *buf, IV len) {
456 110 ft_rawconn *rc = (ft_rawconn *)h;
457 110 ▶ if (!rc) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!rc) return -1; 0 110 110
458 #if FT_TLS_AVAILABLE
459 110 ▶ if (rc->ssl) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc->ssl) { 20 90 110
460 20 ▶ int r = SSL_read((SSL *)rc->ssl, buf, (int)(len > INT_MAX ? INT_MAX : len));
Branch: 1 branch
BranchTrueFalseTotal
int r = SSL_read((SSL *)rc->ssl, buf, (int)(len > INT_MAX ? INT_MAX : len)); 20 0 20
461 20 ▶ if (r > 0) return r;
Branch: 1 branch
BranchTrueFalseTotal
if (r > 0) return r; 20 0 20
462 0 ▶ return (SSL_get_error((SSL *)rc->ssl, r) == SSL_ERROR_ZERO_RETURN) ? 0 : -1;
Branch: 1 branch
BranchTrueFalseTotal
return (SSL_get_error((SSL *)rc->ssl, r) == SSL_ERROR_ZERO_RETURN) ? 0 : -1; 0 0 0
463 }
464 #endif
465 90 { ssize_t r; do { r = ft_os_recv(rc->fd, buf, (size_t)len, 0); }
466 90 ▶ while (r < 0 && errno == EINTR); return (IV)r; }
Branch: 2 branches
BranchTrueFalseTotal
while (r < 0 && errno == EINTR); return (IV)r; } 0 90 90
while (r < 0 && errno == EINTR); return (IV)r; } 0 0 0
467 }
468  
469 7 static IV ft_abi_tunnel_write_all(void *h, const char *buf, IV len) {
470 7 ft_rawconn *rc = (ft_rawconn *)h;
471 7 IV off = 0;
472 7 ▶ if (!rc) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!rc) return -1; 0 7 7
473 14 ▶ while (off < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (off < len) { 7 7 14
474 #if FT_TLS_AVAILABLE
475 7 ▶ if (rc->ssl) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc->ssl) { 2 5 7
476 2 int w = SSL_write((SSL *)rc->ssl, buf + off,
477 2 ▶ (int)((len - off) > INT_MAX ? INT_MAX : (len - off)));
Branch: 1 branch
BranchTrueFalseTotal
(int)((len - off) > INT_MAX ? INT_MAX : (len - off))); 2 0 2
478 2 ▶ if (w <= 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (w <= 0) return -1; 0 2 2
479 2 off += w; continue;
480 }
481 #endif
482 5 { ssize_t w = ft_os_send(rc->fd, buf + off, (size_t)(len - off), 0);
483 5 ▶ if (w < 0) { if (errno == EINTR) continue; return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (w < 0) { if (errno == EINTR) continue; return -1; } 0 5 5
if (w < 0) { if (errno == EINTR) continue; return -1; } 0 0 0
484 5 ▶ if (w == 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (w == 0) return -1; 0 5 5
485 5 off += w; }
486 }
487 7 return 0;
488 }
489  
490 0 static int ft_abi_tunnel_pending(void *h) {
491 0 ft_rawconn *rc = (ft_rawconn *)h;
492 #if FT_TLS_AVAILABLE
493 0 ▶ if (rc && rc->ssl) return SSL_pending((SSL *)rc->ssl);
Branch: 2 branches
BranchTrueFalseTotal
if (rc && rc->ssl) return SSL_pending((SSL *)rc->ssl); 0 0 0
if (rc && rc->ssl) return SSL_pending((SSL *)rc->ssl); 0 0 0
494 #endif
495 0 (void)rc; return 0;
496 }
497  
498 5 static void ft_abi_tunnel_close(void *h) {
499 5 ft_rawconn *rc = (ft_rawconn *)h;
500 5 ▶ if (!rc) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!rc) return; 0 5 5
501 #if FT_TLS_AVAILABLE
502 5 ▶ if (rc->ssl) SSL_free((SSL *)rc->ssl);
Branch: 1 branch
BranchTrueFalseTotal
if (rc->ssl) SSL_free((SSL *)rc->ssl); 2 3 5
503 #endif
504 5 ▶ if (rc->fd >= 0) ft_os_close(rc->fd);
Branch: 1 branch
BranchTrueFalseTotal
if (rc->fd >= 0) ft_os_close(rc->fd); 5 0 5
505 5 free(rc);
506 }
507  
508 static const fetch_abi FETCH_ABI = {
509 FETCH_ABI_VERSION,
510 ft_abi_ua_new,
511 ft_abi_request,
512 ft_abi_res_parts,
513 ft_abi_request_stream,
514 ft_abi_tunnel_connect,
515 ft_abi_tunnel_fd,
516 ft_abi_tunnel_read,
517 ft_abi_tunnel_write_all,
518 ft_abi_tunnel_pending,
519 ft_abi_tunnel_close,
520 ft_obs_add, /* v2 */
521 ft_abi_tunnel_starttls, /* v3 */
522 };
523  
524 /* ---- the v3 STARTTLS upgrade, driven from C (t/25-tunnel-starttls.t) --- *
525 * A Perl test cannot call into a function-pointer table, so the protocol
526 * dance that exercises it lives here, and goes through FETCH_ABI rather than
527 * the static functions: the probe proves the table ENTRY, not just the code
528 * behind it. Connect plain, take the greeting, send STARTTLS, and upgrade
529 * only on a 220 - a client that upgrades on any reply is the one that hangs
530 * against a server that declined. Then one line each way over TLS.
531 *
532 * Returns what the server said, joined with "\n", as far as the probe got;
533 * *where is "ok" or the name of the step that stopped it. */
534 12 static IV ft_abi_st_readline(const fetch_abi *a, void *h, char *buf, size_t size) {
535 12 size_t n = 0;
536 110 ▶ while (n + 1 < size) {
Branch: 1 branch
BranchTrueFalseTotal
while (n + 1 < size) { 110 0 110
537 110 IV r = a->tunnel_read(h, buf + n, 1);
538 110 ▶ if (r <= 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (r <= 0) return -1; 0 110 110
539 110 n++;
540 110 ▶ if (buf[n - 1] == '\n') break;
Branch: 1 branch
BranchTrueFalseTotal
if (buf[n - 1] == '\n') break; 12 98 110
541 }
542 36 ▶ while (n && (buf[n - 1] == '\n' || buf[n - 1] == '\r')) n--;
Branch: 3 branches
BranchTrueFalseTotal
while (n && (buf[n - 1] == '\n' || buf[n - 1] == '\r')) n--; 36 0 36
while (n && (buf[n - 1] == '\n' || buf[n - 1] == '\r')) n--; 12 24 36
while (n && (buf[n - 1] == '\n' || buf[n - 1] == '\r')) n--; 12 12 24
543 12 buf[n] = 0;
544 12 return (IV)n;
545 }
546  
547 5 static SV *ft_abi_starttls_selftest(pTHX_ const char *host, int port, int verify,
548 const char **where) {
549 5 const fetch_abi *a = &FETCH_ABI;
550 5 SV *out = newSVpvs("");
551 char buf[512];
552 void *h;
553  
554 5 *where = "connect";
555 5 h = a->tunnel_connect(host, port, 0, verify);
556 5 ▶ if (!h) return out;
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return out; 0 5 5
557 #ifndef _WIN32
558 { /* the consumer's side of the timeout contract: the table has no
559 * timeout, the fd does */
560 5 struct timeval tv; tv.tv_sec = 10; tv.tv_usec = 0;
561 5 (void)setsockopt(a->tunnel_fd(h), SOL_SOCKET, SO_RCVTIMEO,
562 (const void *)&tv, sizeof tv);
563 }
564 #endif
565 5 *where = "greeting";
566 5 ▶ if (ft_abi_st_readline(a, h, buf, sizeof buf) < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (ft_abi_st_readline(a, h, buf, sizeof buf) < 0) goto done; 0 5 5
567 5 sv_catpv(out, buf);
568  
569 5 *where = "starttls-request";
570 5 ▶ if (a->tunnel_write_all(h, "STARTTLS\r\n", 10) != 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (a->tunnel_write_all(h, "STARTTLS\r\n", 10) != 0) goto done; 0 5 5
571 5 *where = "starttls-reply";
572 5 ▶ if (ft_abi_st_readline(a, h, buf, sizeof buf) < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (ft_abi_st_readline(a, h, buf, sizeof buf) < 0) goto done; 0 5 5
573 5 sv_catpvs(out, "\n"); sv_catpv(out, buf);
574 5 ▶ if (strncmp(buf, "220", 3) != 0) { *where = "refused"; goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (strncmp(buf, "220", 3) != 0) { *where = "refused"; goto done; } 2 3 5
575  
576 3 *where = "starttls";
577 3 ▶ if (a->tunnel_starttls(h, host, verify) != 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (a->tunnel_starttls(h, host, verify) != 0) goto done; 1 2 3
578  
579 2 *where = "ping";
580 2 ▶ if (a->tunnel_write_all(h, "PING\r\n", 6) != 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (a->tunnel_write_all(h, "PING\r\n", 6) != 0) goto done; 0 2 2
581 2 ▶ if (ft_abi_st_readline(a, h, buf, sizeof buf) < 0) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (ft_abi_st_readline(a, h, buf, sizeof buf) < 0) goto done; 0 2 2
582 2 sv_catpvs(out, "\n"); sv_catpv(out, buf);
583 2 *where = "ok";
584 5 done:
585 5 a->tunnel_close(h);
586 5 return out;
587 }
588  
589 /* ---- the v2 observer, driven from C (t/23-observer.t) -------------- *
590 * A Perl test cannot register a C callback, so the observable half lives
591 * here: the start callback adds a header the test server can echo back, and
592 * the done callback records how the hop ended. */
593 static IV FT_OBS_ST_STARTS = 0;
594 static IV FT_OBS_ST_DONES = 0;
595 static IV FT_OBS_ST_OK = 0;
596 static IV FT_OBS_ST_ERR = 0;
597  
598 13 static void *ft_obs_st_start(pTHX_ const char *method, STRLEN mlen,
599 const char *url, STRLEN ulen, AV *headers,
600 void *ud) {
601 PERL_UNUSED_ARG(method); PERL_UNUSED_ARG(mlen);
602 PERL_UNUSED_ARG(url); PERL_UNUSED_ARG(ulen);
603 PERL_UNUSED_ARG(ud);
604 13 FT_OBS_ST_STARTS++;
605 /* prove the list is still mutable and that what is pushed here reaches
606 * the wire - the entire point of firing before the request is built */
607 13 av_push(headers, newSVpvs("X-Fetch-Observer"));
608 13 av_push(headers, newSVpvf("hop%" IVdf, FT_OBS_ST_STARTS));
609 13 return INT2PTR(void *, FT_OBS_ST_STARTS); /* the token: this hop's number */
610 }
611  
612 13 static void ft_obs_st_done(pTHX_ void *token, SV *res, SV *err, void *ud) {
613 PERL_UNUSED_ARG(ud);
614 PERL_UNUSED_CONTEXT;
615 13 FT_OBS_ST_DONES++;
616 13 ▶ if (res) FT_OBS_ST_OK++;
Branch: 1 branch
BranchTrueFalseTotal
if (res) FT_OBS_ST_OK++; 10 3 13
617 13 ▶ if (err) FT_OBS_ST_ERR++;
Branch: 1 branch
BranchTrueFalseTotal
if (err) FT_OBS_ST_ERR++; 3 10 13
618 /* the token is this hop's number, so a mismatch means correlation broke */
619 13 ▶ if (PTR2IV(token) < 1 || PTR2IV(token) > FT_OBS_ST_STARTS)
Branch: 2 branches
BranchTrueFalseTotal
if (PTR2IV(token) < 1 || PTR2IV(token) > FT_OBS_ST_STARTS) 13 0 13
if (PTR2IV(token) < 1 || PTR2IV(token) > FT_OBS_ST_STARTS) 0 13 13
620 0 FT_OBS_ST_ERR = -1;
621 13 }
622  
623 1 static int ft_obs_selftest_install(pTHX) {
624 1 const fetch_abi *A = INT2PTR(const fetch_abi *, PTR2IV(&FETCH_ABI));
625 1 ▶ if (!A || A->abi_version != FETCH_ABI_VERSION) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!A || A->abi_version != FETCH_ABI_VERSION) return 0; 1 0 1
if (!A || A->abi_version != FETCH_ABI_VERSION) return 0; 0 1 1
626 1 return A->on_request(aTHX_ ft_obs_st_start, ft_obs_st_done, NULL);
627 }
628  
629 #endif /* FT_ABI_H */
« xs/fetch.xs ↑ Summary xs/websocket.xs »