xs/fetch.xs

filter: Statementstmt 47.6% 20 / 42 Branchbran 32.0% 16 / 50 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 39.1% 36 / 92 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/fetch/backend_poll.c ↑ Summary include/fetch/ft_abi.h »
1 MODULE = Fetch PACKAGE = Fetch
2  
3 PROTOTYPES: DISABLE
4  
5 # ---- user agent ----------------------------------------------------------
6  
7 # Fetch->new(%args): loop, headers, agent, tls_verify, max_redirects, timeout,
8 # keep_alive, cookie_jar, pool_size. The object is a blessed IV over an ft_ua.
9 SV *
10 new(class, ...)
11 SV *class
12 CODE:
13 {
14 0 ▶ const char *cls = (SvROK(class) && SvOBJECT(SvRV(class)))
Branch: 1 branch
BranchTrueFalseTotal
const char *cls = (SvROK(class) && SvOBJECT(SvRV(class))) 0 0 0
15 110 ▶ ? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class);
Branch: 7 branches
BranchTrueFalseTotal
? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); 0 55 55
? 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
16 55 RETVAL = ft_ua_new(aTHX_ cls, &ST(1), items - 1); /* shared with the ABI */
17 }
18 OUTPUT:
19 RETVAL
20  
21 # $ua->clone(%overrides): another agent over this one's connection pool and
22 # loop. For a per-request cookie jar, where a fresh agent would cost the pool.
23 SV *
24 clone(self, ...)
25 SV *self
26 CODE:
27 8 RETVAL = ft_ua_clone(aTHX_ self, &ST(1), items - 1);
28 OUTPUT:
29 RETVAL
30  
31 void
32 DESTROY(self)
33 SV *self
34 CODE:
35 {
36 61 ft_ua *ua = ft_ua_of(aTHX_ self);
37 61 ▶ if (ua) {
Branch: 1 branch
BranchTrueFalseTotal
if (ua) { 61 0 61
38 /* Free the pool before the loop: parked connections stay armed for
39 * READ (see ft_conn_park), so freeing them unwatches on the loop -
40 * which must still be alive. A native loop's ft_loop* is owned by
41 * ua->loop, so releasing it first would leave the pool's connections
42 * unwatching a freed loop (use-after-free). */
43 61 ▶ if (ua->pool) SvREFCNT_dec(ua->pool);
Branch: 1 branch
BranchTrueFalseTotal
if (ua->pool) SvREFCNT_dec(ua->pool); 60 1 61
44 61 ▶ if (ua->loop) SvREFCNT_dec(ua->loop);
Branch: 1 branch
BranchTrueFalseTotal
if (ua->loop) SvREFCNT_dec(ua->loop); 61 0 61
45 61 ▶ if (ua->headers) SvREFCNT_dec(ua->headers);
Branch: 1 branch
BranchTrueFalseTotal
if (ua->headers) SvREFCNT_dec(ua->headers); 61 0 61
46 61 ▶ if (ua->agent) SvREFCNT_dec(ua->agent);
Branch: 1 branch
BranchTrueFalseTotal
if (ua->agent) SvREFCNT_dec(ua->agent); 61 0 61
47 61 ▶ if (ua->cookie_jar) SvREFCNT_dec(ua->cookie_jar);
Branch: 1 branch
BranchTrueFalseTotal
if (ua->cookie_jar) SvREFCNT_dec(ua->cookie_jar); 7 54 61
48 61 Safefree(ua);
49 }
50 }
51  
52 SV *
53 loop(self)
54 SV *self
55 CODE:
56 24 RETVAL = newSVsv(ft_ua_of(aTHX_ self)->loop);
57 OUTPUT:
58 RETVAL
59  
60 SV *
61 cookie_jar(self)
62 SV *self
63 CODE:
64 {
65 9 ft_ua *ua = ft_ua_of(aTHX_ self);
66 16 ▶ RETVAL = (ua->cookie_jar && SvOK(ua->cookie_jar))
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (ua->cookie_jar && SvOK(ua->cookie_jar)) 7 0 7
67 16 ▶ ? newSVsv(ua->cookie_jar) : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
? newSVsv(ua->cookie_jar) : newSV(0); 7 2 9
68 }
69 OUTPUT:
70 RETVAL
71  
72 # $ua->request($method, $url, %opt) -> Fetch::Future
73 SV *
74 request(self, method, url, ...)
75 SV *self
76 const char *method
77 const char *url
78 CODE:
79 0 RETVAL = ft_dispatch(aTHX_ self, method, url, &ST(3), items - 3);
80 OUTPUT:
81 RETVAL
82  
83 # verb helpers: $ua->get($url, %opt) etc.
84 SV *
85 get(self, url, ...)
86 SV *self
87 const char *url
88 ALIAS:
89 head = 1
90 delete = 2
91 post = 3
92 put = 4
93 CODE:
94 {
95 static const char *const M[5] = { "GET", "HEAD", "DELETE", "POST", "PUT" };
96 139 RETVAL = ft_dispatch(aTHX_ self, M[ix], url, &ST(2), items - 2);
97 }
98 OUTPUT:
99 RETVAL
100  
101 # ---- introspection / internals ------------------------------------------
102  
103 # True if built with OpenSSL (https support).
104 int
105 _tls_available()
106 CODE:
107 4 ▶ RETVAL = FT_TLS_AVAILABLE;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = FT_TLS_AVAILABLE; 0 4 4
108 OUTPUT:
109 RETVAL
110  
111 # True if built with nghttp2 (HTTP/2 support; needs TLS/ALPN too).
112 int
113 _h2_available()
114 CODE:
115 2 ▶ RETVAL = FT_H2_AVAILABLE;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = FT_H2_AVAILABLE; 0 2 2
116 OUTPUT:
117 RETVAL
118  
119 # True if built with ngtcp2 + nghttp3 against an OpenSSL that can drive them
120 # (HTTP/3). False means an h3 attempt falls back to TCP.
121 int
122 _h3_available()
123 CODE:
124 1 ▶ RETVAL = FT_H3_AVAILABLE;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = FT_H3_AVAILABLE; 0 1 1
125 OUTPUT:
126 RETVAL
127  
128 # True if a name is resolved off the loop thread (ft_dns.h). False means the
129 # build found no pthreads, or this is native Windows, and getaddrinfo runs
130 # inline the way it always did - slower under concurrency, never wrong. An
131 # address literal never resolves at all and is unaffected either way.
132 int
133 _dns_async()
134 CODE:
135 1 ▶ RETVAL = FT_DNS_ASYNC;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = FT_DNS_ASYNC; 0 1 1
136 OUTPUT:
137 RETVAL
138  
139 # Start an HTTP/1.1 request on $loop to host:port with the pre-built request
140 # bytes; returns a pending Fetch::Future resolving to a Fetch::Response.
141 SV *
142 _request(loop, pool, host, port, req, tls, verify, timeout, method, scheme, authority, path, headers, body, on_body)
143 SV *loop
144 SV *pool
145 const char *host
146 const char *port
147 SV *req
148 int tls
149 int verify
150 double timeout
151 SV *method
152 SV *scheme
153 SV *authority
154 SV *path
155 SV *headers
156 SV *body
157 SV *on_body
158 CODE:
159 {
160 /* A native Standalone loop is a blessed IV holding a ft_loop*; any
161 * other object is a foreign adapter (IO::Async/AnyEvent/Hyperman)
162 * whose _ft_arm method the C core calls to arm interest. */
163 0 ft_loop *l = NULL;
164 0 SV *lsv = NULL;
165 0 ft_pool *pl = ft_pool_from_sv(aTHX_ pool);
166 STRLEN len;
167 const char *bytes;
168 0 ▶ if (sv_isobject(loop) && sv_derived_from(loop, "Fetch::Loop::Standalone"))
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(loop) && sv_derived_from(loop, "Fetch::Loop::Standalone")) 0 0 0
if (sv_isobject(loop) && sv_derived_from(loop, "Fetch::Loop::Standalone")) 0 0 0
169 0 l = ft_loop_from_sv(aTHX_ loop);
170 else
171 0 lsv = loop;
172 0 bytes = SvPV(req, len);
173 0 RETVAL = ft_h1_start(aTHX_ l, lsv, pl, host, port, bytes, len, tls, verify,
174 timeout, method, scheme, authority, path, headers,
175 body, on_body, NULL);
176 0 hmf_pin_loop(aTHX_ RETVAL, loop); /* only this loop can resolve it */
177 }
178 OUTPUT:
179 RETVAL
180  
181 # The same request over HTTP/3. No `req` byte string and no `tls` flag: QUIC
182 # frames the request itself from the structured pieces, and there is no
183 # cleartext QUIC to choose. Returns a pending Fetch::Future, or one already
184 # failed when h3 cannot be used - which is an answer the caller can fall back
185 # to TCP on rather than an exception.
186 SV *
187 _h3_request(loop, pool, host, port, verify, timeout, method, scheme, authority, path, headers, body, on_body)
188 SV *loop
189 SV *pool
190 const char *host
191 const char *port
192 int verify
193 double timeout
194 SV *method
195 SV *scheme
196 SV *authority
197 SV *path
198 SV *headers
199 SV *body
200 SV *on_body
201 CODE:
202 {
203 0 ft_loop *l = NULL;
204 0 SV *lsv = NULL;
205 0 ft_pool *pl = ft_pool_from_sv(aTHX_ pool);
206 0 ▶ if (sv_isobject(loop) && sv_derived_from(loop, "Fetch::Loop::Standalone"))
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(loop) && sv_derived_from(loop, "Fetch::Loop::Standalone")) 0 0 0
if (sv_isobject(loop) && sv_derived_from(loop, "Fetch::Loop::Standalone")) 0 0 0
207 0 l = ft_loop_from_sv(aTHX_ loop);
208 else
209 0 lsv = loop;
210 0 RETVAL = ft_h3_start(aTHX_ l, lsv, pl, host, port, verify, timeout,
211 method, scheme, authority, path, headers, body,
212 on_body);
213 0 hmf_pin_loop(aTHX_ RETVAL, loop); /* only this loop can resolve it */
214 }
215 OUTPUT:
216 RETVAL
217  
218 # Create a keep-alive connection pool (opaque handle); DESTROY frees it and
219 # every connection still parked in it.
220 SV *
221 _pool_new(max)
222 int max
223 CODE:
224 {
225 0 ft_pool *p = ft_pool_new(max);
226 0 ▶ if (!p) croak("Fetch: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!p) croak("Fetch: out of memory"); 0 0 0
227 0 RETVAL = sv_bless(newRV_noinc(newSViv(PTR2IV(p))),
228 gv_stashpv("Fetch::_Pool", GV_ADD));
229 }
230 OUTPUT:
231 RETVAL
232  
« include/fetch/backend_poll.c ↑ Summary include/fetch/ft_abi.h »