include/fetch/ft_http.h

filter: Statementstmt 73.5% 535 / 727 Branchbran 54.9% 365 / 664 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 64.7% 900 / 1391 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/fetch/ft_cookiejar.h ↑ Summary Fetch.xs »
1 #ifndef FT_HTTP_H
2 #define FT_HTTP_H
3  
4 /* HTTP/1.1 client: a non-blocking connection state machine driven by the loop
5 * through a C-closure readiness callback (no Perl runs per event). Socket IO
6 * and response parsing are all in C; the request resolves a Fetch::Future with
7 * a Fetch::Response. This first cut handles GET/POST with Content-Length or
8 * Connection: close bodies; chunked decoding and keep-alive pooling follow. */
9  
10 #include <sys/types.h>
11 #include <string.h>
12 #include <stdlib.h>
13 /* Sockets, netdb, fcntl, unistd and errno come from ft_win.h (which maps to
14 * Winsock on native Windows and to the POSIX headers everywhere else); the
15 * socket/IO calls below go through its ft_os_* wrappers. */
16  
17 #include "ft_dns.h" /* name resolution, off the loop thread */
18 #include "ft_altsvc.h" /* how an https:// URL ever learns h3 exists */
19  
20 /* FT_RESOLVING comes first because a request in it has no socket yet: c->fd is
21 * the resolver's pipe, so the arming, the deadline and the teardown all work
22 * on it unchanged and a resolve that never answers times out like anything
23 * else. See ft_dns.h. */
24 /* FT_QUIC_HANDSHAKING is HTTP/3's equivalent of FT_CONNECTING plus
25 * FT_HANDSHAKING at once: QUIC carries its TLS handshake inside the transport
26 * handshake, so there is no point between them where a connection is "up but
27 * not secure". */
28 typedef enum { FT_RESOLVING, FT_CONNECTING, FT_HANDSHAKING, FT_WRITING,
29 FT_READING, FT_PARKED, FT_QUIC_HANDSHAKING } ft_http_state;
30  
31 typedef struct ft_conn {
32 ft_loop *loop; /* native Standalone loop, or NULL if foreign */
33 SV *loop_sv; /* foreign loop object (IO::Async/AnyEvent/
34 * Hyperman); interest is armed by calling its
35 * _ft_arm method. NULL for the native loop. */
36 /* Hyperman-direct mode: when the foreign loop is a Fetch::Loop::Hyperman
37 * and Hyperman's C ABI resolved (ft_hm.h), interest and deadlines go
38 * straight through the table - no _ft_arm dispatch, no Perl frame per
39 * readiness event. loop_sv is still held (it keeps the loop alive) but
40 * its methods are never called while hm is set. */
41 const hm_abi *hm; /* the resolved table, or NULL */
42 void *hm_loop; /* opaque Hyperman loop handle */
43 hm_abi_timer *hm_timer; /* pending deadline handle, or NULL */
44 int fd;
45 int armed; /* HM_EV_* currently watched */
46 ft_http_state state;
47 SV *future; /* Fetch::Future to resolve */
48 SV *watcher; /* the C-closure coderef (freed on close) */
49 /* optional per-request deadline: fires ft_conn_timeout, which fails the
50 * request and tears the socket down. timer (native) xor timer_h (foreign
51 * loop handle) is set; timer_cb is our ref to the timeout closure. */
52 ft_timer *timer;
53 SV *timer_h;
54 SV *timer_cb;
55 /* A SECOND, independent timer, for loss detection.
56 *
57 * The deadline above fires once and kills the request. QUIC's probe
58 * timeout is the opposite shape: it is rearmed after almost every packet
59 * sent or acknowledged, it fires many times over one request, and firing
60 * is routine rather than fatal. Sharing one slot would mean a rearmed PTO
61 * silently cancelling the caller's timeout, so it gets its own - same
62 * three-way native/foreign/hm-direct split, no other difference. */
63 ft_timer *pto;
64 SV *pto_h;
65 SV *pto_cb;
66 hm_abi_timer *hm_pto;
67 /* what a fired PTO runs; `struct ft_conn` because the typedef is not
68 * complete until this struct is */
69 void (*pto_fn)(pTHX_ struct ft_conn *c);
70 /* TLS */
71 void *ssl; /* SSL* when https, else NULL */
72 int tls; /* request wants TLS */
73 int verify; /* verify peer + hostname */
74 char *host; /* SNI / verify host, and the redial target */
75 char *port; /* service, kept for the keep-alive redial */
76 ft_dns *dns; /* in-flight resolve (FT_RESOLVING), or NULL */
77 /* HTTP/2 (nghttp2) - populated after ALPN negotiates h2 */
78 void *h2; /* nghttp2_session* */
79 int is_h2;
80 int h2_done; /* stream closed */
81 int h2_ended; /* END_STREAM arrived on a HEADERS or DATA
82 * frame, so the server finished its response.
83 * A closed stream without this was reset */
84 uint32_t h2_close_code; /* the error code the stream closed with */
85 /* HTTP/3 (ngtcp2 + nghttp3). One ft_qconn (ft_h3.h) holds the whole QUIC
86 * connection AND its list of in-flight request streams - unlike h2, where
87 * the connection and the one request it carries are the same object. */
88 void *h3; /* ft_qconn*, or NULL */
89 int is_h3;
90 /* structured request pieces, kept for the h2 nva (built after ALPN) */
91 SV *rq_method; /* "GET" ... */
92 SV *rq_scheme; /* "http"/"https" */
93 SV *rq_authority;/* host[:port] */
94 SV *rq_path; /* "/..." */
95 AV *rq_headers; /* [k,v,...] extra headers */
96 SV *rq_body; /* request body or NULL */
97 size_t rq_body_off; /* h2 data-provider cursor */
98 /* optional streaming sink: when set, each body chunk is handed to this
99 * coderef as it arrives instead of being buffered, so large or endless
100 * (SSE) bodies do not grow memory and the final Response body is empty. */
101 SV *on_body;
102 SV *on_headers; /* fired once, ($status, [k,v,...]), before body */
103 int headers_fired;
104 size_t body_recv; /* cumulative body bytes seen (streaming) */
105 /* request (HTTP/1.1 serialized form) */
106 char *req;
107 size_t req_len, req_off;
108 /* response accumulation + parse */
109 char *rbuf;
110 size_t rlen, rcap;
111 int have_headers;
112 size_t hdr_end; /* offset just past the \r\n\r\n */
113 int status;
114 AV *headers; /* [k,v,k,v,...] */
115 AV *trailers; /* [k,v,...] from the second HEADERS frame of
116 * an h2 response, or from the trailer section
117 * of a chunked HTTP/1.1 body */
118 long content_len; /* -1 = until close */
119 int want_close; /* Connection: close */
120 /* chunked transfer decoding */
121 int chunked;
122 size_t cpos; /* decode cursor into rbuf */
123 long chunk_left; /* bytes left in chunk; -1 = need size line,
124 * FT_CHUNK_TRAILERS = reading the section
125 * after the last chunk */
126 int chunk_done; /* the body is over, trailer section and its
127 * closing CRLF included */
128 size_t trailer_bytes; /* trailer section consumed so far */
129 int trailer_overrun; /* the section passed FT_TRAILER_CAP, so
130 * the connection has to be failed */
131 char *dbody; /* decoded body */
132 size_t dblen, dbcap;
133 /* keep-alive pooling: when this h1 connection may be reused, it is parked
134 * (fd + TLS kept open) in the owning pool under poolkey instead of closed,
135 * and revived for the next request to the same host. */
136 struct ft_pool *pool; /* owning pool (NULL = close-per-request) */
137 char *poolkey; /* "proto:tls:host:port" identity for reuse */
138 struct ft_conn *next_idle; /* pool free-list link while parked */
139 int reused; /* revived from the pool this request */
140 int simple; /* resolve with a raw hash, not a blessed
141 * Fetch::Response (Fetch->new(simple_response)) */
142 /* WebSocket (RFC 6455): after a verified 101 the connection switches to
143 * frame mode and stays open, owned by a Fetch::WebSocket object. */
144 int want_ws; /* this request is a WebSocket upgrade */
145 int is_ws; /* handshake done; in frame mode */
146 int ws_closed; /* socket closed / closing */
147 char *ws_key; /* Sec-WebSocket-Key we sent (verify Accept) */
148 SV *ws_on_message;
149 SV *ws_on_close;
150 SV *ws_waiter; /* pending next_message future */
151 AV *ws_inbox; /* messages arrived before a waiter */
152 char *ws_wbuf; /* queued outbound frame bytes */
153 size_t ws_wlen, ws_wcap, ws_woff;
154 char *ws_msg; /* inbound message reassembly (fragments) */
155 size_t ws_mlen, ws_mcap;
156 int ws_msg_opcode;
157 size_t ws_pos; /* frame parse cursor into rbuf */
158 } ft_conn;
159  
160 /* A per-UA keep-alive pool: a flat list of idle, revivable h1 connections.
161 * Few distinct hosts in practice, so a linear scan by poolkey is fine. */
162 typedef struct ft_pool {
163 ft_conn *idle; /* head of the idle list (via next_idle) */
164 int count;
165 int max; /* cap on parked connections */
166 } ft_pool;
167  
168 #include "ft_tls.h" /* operates on ft_conn (c->ssl, c->fd) */
169  
170 static void ft_h2_free(ft_conn *c); /* defined in ft_h2.h */
171 static void ft_h3_free(pTHX_ ft_conn *c); /* defined in ft_h3.h */
172 static void ft_h3_step(pTHX_ ft_conn *c); /* defined in ft_h3.h */
173 static void ft_loop_arm(pTHX_ ft_conn *c, int mask); /* foreign, below */
174 static void ft_loop_untimer(pTHX_ ft_conn *c); /* foreign, below */
175 static void ft_arm(pTHX_ ft_conn *c, int mask); /* below */
176 static void ft_hm_ready(pTHX_ int fd, int mask, void *ud); /* hm-direct, below */
177 static void ft_conn_cancel_pto(pTHX_ ft_conn *c); /* below */
178 /* The loss-detection timer, declared here because ft_h3.h is included before
179 * it is defined and QUIC is the only thing that arms one. */
180 static void ft_conn_arm_pto(pTHX_ ft_conn *c, double secs,
181 void (*fn)(pTHX_ struct ft_conn *c));
182  
183 /* Cancel a pending deadline timer (nothing to do if it already fired: the
184 * fire path clears c->timer/c->timer_h/c->hm_timer before running).
185 *
186 * Nothing here may touch the loop once PL_dirty is set - see ft_conn_free. */
187 178 static void ft_conn_cancel_timer(pTHX_ ft_conn *c) {
188 178 ▶ if (c->hm_timer) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->hm_timer) { 0 178 178
189 0 ▶ if (!PL_dirty) c->hm->timer_cancel(aTHX_ c->hm_loop, c->hm_timer);
Branch: 1 branch
BranchTrueFalseTotal
if (!PL_dirty) c->hm->timer_cancel(aTHX_ c->hm_loop, c->hm_timer); 0 0 0
190 0 c->hm_timer = NULL;
191 }
192 178 ▶ if (c->timer) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->timer) { 24 154 178
193 24 ▶ if (!PL_dirty) ft_del_timer(aTHX_ c->loop, c->timer);
Branch: 1 branch
BranchTrueFalseTotal
if (!PL_dirty) ft_del_timer(aTHX_ c->loop, c->timer); 24 0 24
194 24 c->timer = NULL;
195 }
196 178 ▶ if (c->timer_h) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->timer_h) { 0 178 178
197 0 ▶ if (!PL_dirty) ft_loop_untimer(aTHX_ c);
Branch: 1 branch
BranchTrueFalseTotal
if (!PL_dirty) ft_loop_untimer(aTHX_ c); 0 0 0
198 0 else { SvREFCNT_dec(c->timer_h); c->timer_h = NULL; }
199 }
200 178 }
201  
202 /* Set by the UA immediately before ft_h1_start so a freshly created connection
203 * inherits its simple_response mode (single-threaded loop; safe as a static). */
204 static int ft_conn_simple_next = 0;
205  
206 /* Likewise the UA's Hyperman-direct mode (ft_hm.h): both set, or both NULL. */
207 static const hm_abi *ft_conn_hm_next = NULL;
208 static void *ft_conn_hm_loop_next = NULL;
209  
210 /* Likewise for the optional on_headers callback (borrowed; consumed at conn
211 * create/revive). Fired once, before the body, with ($status, [k,v,...]). */
212 static SV *ft_conn_on_headers_next = NULL;
213  
214 /* The transport a request is being started on. It is part of the pool key
215 * because an idle connection is only reusable by a request that speaks what
216 * it speaks: handing an HTTP/3 connection to a request expecting to write
217 * HTTP/1.1 bytes at a socket is not a subtle failure, it is a hang.
218 *
219 * The key names the protocol ASKED FOR, not the one negotiated. Those differ
220 * on TLS, where ALPN can turn an intended h1 connection into h2 - but an h2
221 * connection never parks (ft_conn_reusable), so nothing under the "h1" key is
222 * ever anything else. Alt-Svc is what makes a request ask for h3 in the first
223 * place, and it asks before the connection exists. */
224 #define FT_PROTO_H1 "h1"
225 #define FT_PROTO_H3 "h3"
226 static const char *ft_conn_proto_next = FT_PROTO_H1;
227  
228 /* Hand the parsed status line + header list to the on_headers sink, once. */
229 0 static void ft_fire_headers(pTHX_ ft_conn *c) {
230 0 dSP;
231 0 SV *hrv = c->headers ? newRV_inc((SV *)c->headers)
232 0 ▶ : newRV_noinc((SV *)newAV());
Branch: 1 branch
BranchTrueFalseTotal
: newRV_noinc((SV *)newAV()); 0 0 0
233 0 ENTER; SAVETMPS;
234 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
235 0 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 0 0
236 0 PUSHs(sv_2mortal(newSViv(c->status)));
237 0 PUSHs(sv_2mortal(hrv));
238 0 PUTBACK;
239 0 call_sv(c->on_headers, G_DISCARD | G_EVAL);
240 0 ▶ if (SvTRUE(ERRSV)) warn("Fetch: on_headers callback died: %s", SvPV_nolen(ERRSV));
Branch: 3 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) warn("Fetch: on_headers callback died: %s", SvPV_nolen(ERRSV)); 0 0 0
if (SvTRUE(ERRSV)) warn("Fetch: on_headers callback died: %s", SvPV_nolen(ERRSV)); 0 0 0
if (SvTRUE(ERRSV)) warn("Fetch: on_headers callback died: %s", SvPV_nolen(ERRSV)); 0 0 0
241 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
242 0 }
243  
244 122 static void ft_conn_free(pTHX_ ft_conn *c) {
245 122 ▶ if (!c) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!c) return; 0 122 122
246 122 ft_conn_cancel_timer(aTHX_ c);
247 122 ft_conn_cancel_pto(aTHX_ c);
248 /* A resolve still in flight: drop this side's reference and go. The
249 * thread cannot be stopped and is not waited for - it finishes into a
250 * struct nobody is reading, finds itself holding the last reference and
251 * frees it there. The pipe's read end is c->fd and is closed below. */
252 122 ▶ if (c->dns) { ft_dns_unref(c->dns); c->dns = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->dns) { ft_dns_unref(c->dns); c->dns = NULL; } 0 122 122
253 122 ft_h2_free(c);
254 122 ft_h3_free(aTHX_ c);
255 122 ft_tls_free(c);
256 122 ▶ if (c->fd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->fd >= 0) { 120 2 122
257 /* At global destruction the loop may be gone ALREADY: c->loop is a
258 * raw pointer to the loop struct and c->loop_sv holds an adapter
259 * object, and neither keeps the loop alive once PL_dirty is set -
260 * perl frees objects in arena order, so Fetch::Loop::Standalone's
261 * DESTROY (ft_loop_free) can run before the agent's pool is freed.
262 * Disarming through a freed loop reads l->pid, walks l->timers and
263 * calls l->be->remove_io: a use-after-free, and a SIGSEGV whenever
264 * the order comes out that way. It caught a child of t/28-fork.t on
265 * a 5.20.1 smoker and nowhere else, because the order is luck.
266 *
267 * There is nothing to disarm at that point anyway: the process is
268 * ending, ft_loop_free releases the watcher table itself, and the
269 * descriptor is closed just below. */
270 120 ▶ if (c->armed && !PL_dirty) {
Branch: 2 branches
BranchTrueFalseTotal
if (c->armed && !PL_dirty) { 120 0 120
if (c->armed && !PL_dirty) { 113 7 120
271 113 ▶ if (c->hm) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->hm) { 0 113 113
272 0 ▶ if (c->armed & HM_EV_READ)
Branch: 1 branch
BranchTrueFalseTotal
if (c->armed & HM_EV_READ) 0 0 0
273 0 c->hm->io_unwatch(aTHX_ c->hm_loop, c->fd, HM_ABI_READ);
274 0 ▶ if (c->armed & HM_EV_WRITE)
Branch: 1 branch
BranchTrueFalseTotal
if (c->armed & HM_EV_WRITE) 0 0 0
275 0 c->hm->io_unwatch(aTHX_ c->hm_loop, c->fd, HM_ABI_WRITE);
276 }
277 113 ▶ else if (c->loop_sv) ft_loop_arm(aTHX_ c, 0);
Branch: 1 branch
BranchTrueFalseTotal
else if (c->loop_sv) ft_loop_arm(aTHX_ c, 0); 0 113 113
278 113 else ft_unwatch_io(aTHX_ c->loop, c->fd, c->armed);
279 }
280 120 ft_os_close(c->fd);
281 }
282 122 ▶ if (c->on_body) SvREFCNT_dec(c->on_body);
Branch: 1 branch
BranchTrueFalseTotal
if (c->on_body) SvREFCNT_dec(c->on_body); 2 120 122
283 122 ▶ if (c->on_headers) SvREFCNT_dec(c->on_headers);
Branch: 1 branch
BranchTrueFalseTotal
if (c->on_headers) SvREFCNT_dec(c->on_headers); 0 122 122
284 122 ▶ if (c->ws_on_message) SvREFCNT_dec(c->ws_on_message);
Branch: 1 branch
BranchTrueFalseTotal
if (c->ws_on_message) SvREFCNT_dec(c->ws_on_message); 1 121 122
285 122 ▶ if (c->ws_on_close) SvREFCNT_dec(c->ws_on_close);
Branch: 1 branch
BranchTrueFalseTotal
if (c->ws_on_close) SvREFCNT_dec(c->ws_on_close); 0 122 122
286 122 ▶ if (c->ws_waiter) SvREFCNT_dec(c->ws_waiter);
Branch: 1 branch
BranchTrueFalseTotal
if (c->ws_waiter) SvREFCNT_dec(c->ws_waiter); 0 122 122
287 122 ▶ if (c->ws_inbox) SvREFCNT_dec(c->ws_inbox);
Branch: 1 branch
BranchTrueFalseTotal
if (c->ws_inbox) SvREFCNT_dec(c->ws_inbox); 0 122 122
288 122 ▶ if (c->timer_cb) SvREFCNT_dec(c->timer_cb);
Branch: 1 branch
BranchTrueFalseTotal
if (c->timer_cb) SvREFCNT_dec(c->timer_cb); 21 101 122
289 122 ▶ if (c->pto_cb) SvREFCNT_dec(c->pto_cb);
Branch: 1 branch
BranchTrueFalseTotal
if (c->pto_cb) SvREFCNT_dec(c->pto_cb); 0 122 122
290 122 ▶ if (c->loop_sv) SvREFCNT_dec(c->loop_sv);
Branch: 1 branch
BranchTrueFalseTotal
if (c->loop_sv) SvREFCNT_dec(c->loop_sv); 0 122 122
291 122 ▶ if (c->future) SvREFCNT_dec(c->future);
Branch: 1 branch
BranchTrueFalseTotal
if (c->future) SvREFCNT_dec(c->future); 103 19 122
292 122 ▶ if (c->watcher) SvREFCNT_dec(c->watcher);
Branch: 1 branch
BranchTrueFalseTotal
if (c->watcher) SvREFCNT_dec(c->watcher); 122 0 122
293 122 ▶ if (c->headers) SvREFCNT_dec(c->headers);
Branch: 1 branch
BranchTrueFalseTotal
if (c->headers) SvREFCNT_dec(c->headers); 90 32 122
294 122 ▶ if (c->trailers) SvREFCNT_dec(c->trailers);
Branch: 1 branch
BranchTrueFalseTotal
if (c->trailers) SvREFCNT_dec(c->trailers); 2 120 122
295 122 ▶ if (c->rq_method) SvREFCNT_dec(c->rq_method);
Branch: 1 branch
BranchTrueFalseTotal
if (c->rq_method) SvREFCNT_dec(c->rq_method); 7 115 122
296 122 ▶ if (c->rq_scheme) SvREFCNT_dec(c->rq_scheme);
Branch: 1 branch
BranchTrueFalseTotal
if (c->rq_scheme) SvREFCNT_dec(c->rq_scheme); 7 115 122
297 122 ▶ if (c->rq_authority) SvREFCNT_dec(c->rq_authority);
Branch: 1 branch
BranchTrueFalseTotal
if (c->rq_authority) SvREFCNT_dec(c->rq_authority); 7 115 122
298 122 ▶ if (c->rq_path) SvREFCNT_dec(c->rq_path);
Branch: 1 branch
BranchTrueFalseTotal
if (c->rq_path) SvREFCNT_dec(c->rq_path); 7 115 122
299 122 ▶ if (c->rq_headers) SvREFCNT_dec(c->rq_headers);
Branch: 1 branch
BranchTrueFalseTotal
if (c->rq_headers) SvREFCNT_dec(c->rq_headers); 7 115 122
300 122 ▶ if (c->rq_body) SvREFCNT_dec(c->rq_body);
Branch: 1 branch
BranchTrueFalseTotal
if (c->rq_body) SvREFCNT_dec(c->rq_body); 1 121 122
301 122 Safefree(c->host);
302 122 Safefree(c->port);
303 122 Safefree(c->poolkey);
304 122 Safefree(c->ws_key);
305 122 Safefree(c->ws_wbuf);
306 122 Safefree(c->ws_msg);
307 122 Safefree(c->req);
308 122 Safefree(c->rbuf);
309 122 Safefree(c->dbody);
310 122 Safefree(c);
311 }
312  
313 /* --- keep-alive pool ------------------------------------------------------ */
314  
315 55 static ft_pool *ft_pool_new(int max) {
316 55 ft_pool *p = (ft_pool *)calloc(1, sizeof(ft_pool));
317 55 ▶ if (!p) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!p) return NULL; 0 55 55
318 55 ▶ p->max = max > 0 ? max : 32;
Branch: 1 branch
BranchTrueFalseTotal
p->max = max > 0 ? max : 32; 55 0 55
319 55 return p;
320 }
321  
322 /* Take the first idle connection matching key, unlinking it; NULL if none. */
323 154 static ft_conn *ft_pool_take(ft_pool *p, const char *key) {
324 154 ft_conn **pp = &p->idle;
325 154 ▶ while (*pp) {
Branch: 1 branch
BranchTrueFalseTotal
while (*pp) { 38 116 154
326 38 ▶ if ((*pp)->poolkey && strcmp((*pp)->poolkey, key) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ((*pp)->poolkey && strcmp((*pp)->poolkey, key) == 0) { 38 0 38
if ((*pp)->poolkey && strcmp((*pp)->poolkey, key) == 0) { 38 0 38
327 38 ft_conn *c = *pp;
328 38 *pp = c->next_idle;
329 38 c->next_idle = NULL;
330 38 p->count--;
331 38 return c;
332 }
333 0 pp = &(*pp)->next_idle;
334 }
335 116 return NULL;
336 }
337  
338 /* Find a connection under this key WITHOUT taking it out.
339 *
340 * The h1 pool holds only idle connections and hands one over exclusively,
341 * because an HTTP/1.1 connection carries one request at a time. An HTTP/3
342 * connection carries many, so it stays in the pool while it is in use and a
343 * second request attaches to it rather than replacing it - which is the
344 * difference between multiplexing and taking turns, and the reason this
345 * exists beside ft_pool_take rather than instead of it. */
346 0 static ft_conn *ft_pool_peek(ft_pool *p, const char *key) {
347 ft_conn *c;
348 0 ▶ if (!p) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!p) return NULL; 0 0 0
349 0 ▶ for (c = p->idle; c; c = c->next_idle)
Branch: 1 branch
BranchTrueFalseTotal
for (c = p->idle; c; c = c->next_idle) 0 0 0
350 0 ▶ if (c->poolkey && strcmp(c->poolkey, key) == 0) return c;
Branch: 2 branches
BranchTrueFalseTotal
if (c->poolkey && strcmp(c->poolkey, key) == 0) return c; 0 0 0
if (c->poolkey && strcmp(c->poolkey, key) == 0) return c; 0 0 0
351 0 return NULL;
352 }
353  
354 /* Park an idle connection for reuse, or free it if the pool is full. */
355 56 static void ft_pool_put(pTHX_ ft_pool *p, ft_conn *c) {
356 56 ▶ if (p->count >= p->max) { ft_conn_free(aTHX_ c); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (p->count >= p->max) { ft_conn_free(aTHX_ c); return; } 0 56 56
357 56 c->next_idle = p->idle;
358 56 p->idle = c;
359 56 p->count++;
360 }
361  
362 55 static void ft_pool_free(pTHX_ ft_pool *p) {
363 ft_conn *c;
364 55 ▶ if (!p) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!p) return; 0 55 55
365 73 ▶ for (c = p->idle; c; ) { ft_conn *n = c->next_idle; ft_conn_free(aTHX_ c); c = n; }
Branch: 1 branch
BranchTrueFalseTotal
for (c = p->idle; c; ) { ft_conn *n = c->next_idle; ft_conn_free(aTHX_ c); c = n; } 18 55 73
366 55 free(p);
367 }
368  
369 /* Unlink a parked connection from the idle list (before evicting/freeing it). */
370 0 static void ft_pool_remove(ft_pool *p, ft_conn *c) {
371 ft_conn **pp;
372 0 ▶ if (!p) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!p) return; 0 0 0
373 0 ▶ for (pp = &p->idle; *pp; pp = &(*pp)->next_idle) {
Branch: 1 branch
BranchTrueFalseTotal
for (pp = &p->idle; *pp; pp = &(*pp)->next_idle) { 0 0 0
374 0 ▶ if (*pp == c) { *pp = c->next_idle; c->next_idle = NULL; p->count--; return; }
Branch: 1 branch
BranchTrueFalseTotal
if (*pp == c) { *pp = c->next_idle; c->next_idle = NULL; p->count--; return; } 0 0 0
375 }
376 }
377  
378 /* Can this finished connection be parked and reused? Only a delimited h1
379 * response on a pooled connection the server did not mark Connection: close
380 * leaves the socket at a clean message boundary. */
381 144 static int ft_conn_reusable(ft_conn *c) {
382 144 ▶ if (!c->pool || c->want_close || c->is_h2) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!c->pool || c->want_close || c->is_h2) return 0; 139 5 144
if (!c->pool || c->want_close || c->is_h2) return 0; 56 83 139
if (!c->pool || c->want_close || c->is_h2) return 0; 0 56 56
383 56 ▶ if (c->chunked) return c->chunk_done;
Branch: 1 branch
BranchTrueFalseTotal
if (c->chunked) return c->chunk_done; 6 50 56
384 50 return c->content_len >= 0;
385 }
386  
387 /* Park a finished connection: stop watching it, drop the request's per-call
388 * state, keep the fd/TLS/buffers, and hand it to the pool for the next
389 * request to the same host. */
390 56 static void ft_conn_park(pTHX_ ft_conn *c) {
391 /* Stay armed for READ while idle instead of disarming now and re-arming on
392 * the next request. A healthy keep-alive socket is quiet, so this costs
393 * nothing; it saves the disarm+re-arm pair per reuse - two _ft_arm
394 * crossings on a foreign loop, two epoll/kqueue syscalls on the native
395 * one. If the server closes the idle connection it becomes readable and
396 * ft_conn_ready_cb (seeing FT_PARKED) evicts it. */
397 56 ft_arm(aTHX_ c, HM_EV_READ); /* no-op: already READ-armed from recv */
398 56 ft_conn_cancel_timer(aTHX_ c);
399 56 ▶ if (c->future) { SvREFCNT_dec(c->future); c->future = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->future) { SvREFCNT_dec(c->future); c->future = NULL; } 56 0 56
400 56 ▶ if (c->on_body) { SvREFCNT_dec(c->on_body); c->on_body = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->on_body) { SvREFCNT_dec(c->on_body); c->on_body = NULL; } 1 55 56
401 56 ▶ if (c->on_headers) { SvREFCNT_dec(c->on_headers); c->on_headers = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->on_headers) { SvREFCNT_dec(c->on_headers); c->on_headers = NULL; } 0 56 56
402 56 c->headers_fired = 0;
403 56 ▶ if (c->headers) { SvREFCNT_dec(c->headers); c->headers = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->headers) { SvREFCNT_dec(c->headers); c->headers = NULL; } 56 0 56
404 56 ▶ if (c->trailers) { SvREFCNT_dec(c->trailers); c->trailers = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->trailers) { SvREFCNT_dec(c->trailers); c->trailers = NULL; } 3 53 56
405 56 c->rlen = 0; c->have_headers = 0; c->hdr_end = 0; c->status = 0;
406 56 c->content_len = -1; c->want_close = 0; c->chunked = 0;
407 56 c->cpos = 0; c->chunk_left = 0; c->chunk_done = 0;
408 56 c->trailer_bytes = 0; c->trailer_overrun = 0;
409 56 c->dblen = 0; c->body_recv = 0; c->req_off = 0; c->reused = 0;
410 56 c->state = FT_PARKED;
411 56 ft_pool_put(aTHX_ c->pool, c);
412 56 }
413  
414 /* resolve the request's future with a Fetch::Response, then park or free */
415 /* Build the response object and settle a future with it.
416 *
417 * Split out of ft_conn_finish because HTTP/3 finishes a REQUEST without
418 * finishing the connection: several streams settle their own futures on one
419 * connection, so the pieces come from a per-stream struct rather than from
420 * the ft_conn. Everything a caller sees - the hash keys, the blessing, the
421 * absent trailers key - is decided here and once, so the transports cannot
422 * drift in what a Fetch::Response is. `body` is borrowed. */
423 144 static void ft_settle_response(pTHX_ SV *future, int status, AV *headers,
424 AV *trailers, const char *body, size_t blen,
425 int simple) {
426 /* the Response stash never changes; look it up once per process */
427 static HV *resp_stash = NULL;
428 144 HV *resp = newHV();
429 SV *rv, *fut;
430 AV *vals;
431 144 (void)hv_stores(resp, "status", newSViv(status));
432 144 ▶ (void)hv_stores(resp, "headers",
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(resp, "headers", 144 0 144
433 headers ? newRV_inc((SV *)headers) : newRV_noinc((SV *)newAV()));
434 /* Only when there were any: an HTTP/1 response has no trailers and should
435 * not grow an empty key, and a consumer can tell "none were sent" from
436 * "none were captured" by the absence. */
437 144 ▶ if (trailers)
Branch: 1 branch
BranchTrueFalseTotal
if (trailers) 4 140 144
438 4 (void)hv_stores(resp, "trailers", newRV_inc((SV *)trailers));
439 144 ▶ (void)hv_stores(resp, "content", newSVpvn(body ? body : "", blen));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(resp, "content", newSVpvn(body ? body : "", blen)); 144 0 144
440 144 ▶ if (simple) {
Branch: 1 branch
BranchTrueFalseTotal
if (simple) { 0 144 144
441 0 rv = newRV_noinc((SV *)resp); /* raw hash: no bless, no methods */
442 } else {
443 144 ▶ if (!resp_stash) resp_stash = gv_stashpv("Fetch::Response", GV_ADD);
Branch: 1 branch
BranchTrueFalseTotal
if (!resp_stash) resp_stash = gv_stashpv("Fetch::Response", GV_ADD); 27 117 144
444 144 rv = sv_bless(newRV_noinc((SV *)resp), resp_stash);
445 }
446 /* hand the response straight to the future's value AV - no extra copy */
447 144 fut = SvREFCNT_inc(future);
448 144 vals = newAV();
449 144 av_push(vals, rv); /* transfers our ref */
450 144 hmf_settle_av(aTHX_ fut, HMF_DONE, vals);
451 144 SvREFCNT_dec(fut);
452 144 }
453  
454 144 static void ft_conn_finish(pTHX_ ft_conn *c) {
455 144 int reuse = ft_conn_reusable(c);
456 /* Alt-Svc, on the way past. A response that arrived over TCP is the only
457 * place an origin can say it also answers over QUIC, so this is where a
458 * later request learns to try h3 - see ft_altsvc.h. */
459 144 ▶ if (c->tls && c->host && c->headers)
Branch: 3 branches
BranchTrueFalseTotal
if (c->tls && c->host && c->headers) 6 138 144
if (c->tls && c->host && c->headers) 6 0 6
if (c->tls && c->host && c->headers) 6 0 6
460 6 ▶ ft_altsvc_note(aTHX_ c->host, c->port ? atoi(c->port) : 443, c->headers);
Branch: 1 branch
BranchTrueFalseTotal
ft_altsvc_note(aTHX_ c->host, c->port ? atoi(c->port) : 443, c->headers); 6 0 6
461 /* h2 and chunked decode into dbody; a plain h1 body is still in rbuf */
462 144 ▶ const char *body = (c->is_h2 || c->chunked)
Branch: 1 branch
BranchTrueFalseTotal
const char *body = (c->is_h2 || c->chunked) 10 134 144
463 288 ▶ ? (c->dbody ? c->dbody : "") : c->rbuf + c->hdr_end;
Branch: 2 branches
BranchTrueFalseTotal
? (c->dbody ? c->dbody : "") : c->rbuf + c->hdr_end; 144 0 144
? (c->dbody ? c->dbody : "") : c->rbuf + c->hdr_end; 8 2 10
464 144 ▶ size_t blen = (c->is_h2 || c->chunked)
Branch: 1 branch
BranchTrueFalseTotal
size_t blen = (c->is_h2 || c->chunked) 10 134 144
465 288 ▶ ? c->dblen : c->rlen - c->hdr_end;
Branch: 1 branch
BranchTrueFalseTotal
? c->dblen : c->rlen - c->hdr_end; 144 0 144
466 144 ft_settle_response(aTHX_ c->future, c->status, c->headers, c->trailers,
467 body, blen, c->simple);
468 144 ▶ if (reuse) { ft_conn_park(aTHX_ c); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (reuse) { ft_conn_park(aTHX_ c); return; } 56 88 144
469 88 ft_conn_free(aTHX_ c);
470 }
471  
472 15 static void ft_conn_fail(pTHX_ ft_conn *c, const char *msg) {
473 /* An HTTP/3 connection has no future of its own: the futures belong to
474 * its streams, and ft_h3_fail_all has already settled every one of them
475 * before calling this to tear the connection down. Everywhere else there
476 * is exactly one, and it is this. */
477 15 ▶ if (c->future) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->future) { 15 0 15
478 15 SV *e = sv_2mortal(newSVpvf("Fetch: %s", msg));
479 15 SV *fut = SvREFCNT_inc(c->future);
480 15 ▶ if (hmf_state(aTHX_ fut) == HMF_PENDING)
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_state(aTHX_ fut) == HMF_PENDING) 15 0 15
481 15 hmf_settle(aTHX_ fut, HMF_FAILED, &e, 1);
482 15 SvREFCNT_dec(fut);
483 }
484 15 ft_conn_free(aTHX_ c);
485 15 }
486  
487 /* case-insensitive search for "chunked" within [s, end) */
488 11 static int strncasestr_chunked(const char *s, const char *end) {
489 11 ▶ while (s + 7 <= end) {
Branch: 1 branch
BranchTrueFalseTotal
while (s + 7 <= end) { 11 0 11
490 11 ▶ if (strncasecmp(s, "chunked", 7) == 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (strncasecmp(s, "chunked", 7) == 0) return 1; 11 0 11
491 0 s++;
492 }
493 0 return 0;
494 }
495  
496 /* parse status line + headers once the \r\n\r\n terminator is in rbuf */
497 146 static int ft_parse_headers(pTHX_ ft_conn *c) {
498 146 char *p = c->rbuf;
499 146 char *end = ft_memmem(c->rbuf, c->rlen, "\r\n\r\n", 4);
500 char *line_end;
501 146 ▶ if (!end) return 0; /* need more bytes */
Branch: 1 branch
BranchTrueFalseTotal
if (!end) return 0; /* need more bytes */ 0 146 146
502 146 c->hdr_end = (size_t)(end - c->rbuf) + 4;
503  
504 /* status line: HTTP/1.x SP code SP reason */
505 146 line_end = memchr(p, '\r', c->rlen);
506 146 ▶ if (!line_end) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!line_end) return -1; 0 146 146
507 {
508 146 char *sp = memchr(p, ' ', (size_t)(line_end - p));
509 146 ▶ if (!sp) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!sp) return -1; 0 146 146
510 146 c->status = (int)strtol(sp + 1, NULL, 10);
511 }
512 146 p = line_end + 2; /* past CRLF */
513  
514 146 c->headers = newAV();
515 146 c->content_len = -1;
516 /* Header lines run up to the blank line; the last header's terminating CR
517 * is the first CR of the closing \r\n\r\n (at `end`), so search that far
518 * inclusive - stopping at `end` would silently drop the final header. */
519 540 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 394 146 540
520 394 char *eol = memchr(p, '\r', (size_t)((c->rbuf + c->hdr_end) - p));
521 char *colon;
522 394 ▶ if (!eol || eol == p) break;
Branch: 2 branches
BranchTrueFalseTotal
if (!eol || eol == p) break; 394 0 394
if (!eol || eol == p) break; 394 0 394
523 394 colon = memchr(p, ':', (size_t)(eol - p));
524 394 ▶ if (colon) {
Branch: 1 branch
BranchTrueFalseTotal
if (colon) { 394 0 394
525 394 char *vs = colon + 1;
526 394 char *ve = eol;
527 size_t klen;
528 788 ▶ while (vs < ve && (*vs == ' ' || *vs == '\t')) vs++;
Branch: 3 branches
BranchTrueFalseTotal
while (vs < ve && (*vs == ' ' || *vs == '\t')) vs++; 788 0 788
while (vs < ve && (*vs == ' ' || *vs == '\t')) vs++; 394 394 788
while (vs < ve && (*vs == ' ' || *vs == '\t')) vs++; 0 394 394
529 394 klen = (size_t)(colon - p);
530 394 av_push(c->headers, newSVpvn(p, klen));
531 394 av_push(c->headers, newSVpvn(vs, (size_t)(ve - vs)));
532 394 ▶ if (klen == 14 && strncasecmp(p, "Content-Length", 14) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (klen == 14 && strncasecmp(p, "Content-Length", 14) == 0) 134 260 394
if (klen == 14 && strncasecmp(p, "Content-Length", 14) == 0) 134 0 134
533 134 c->content_len = strtol(vs, NULL, 10);
534 260 ▶ else if (klen == 10 && strncasecmp(p, "Connection", 10) == 0 &&
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 10 && strncasecmp(p, "Connection", 10) == 0 && 103 157 260
else if (klen == 10 && strncasecmp(p, "Connection", 10) == 0 && 88 15 103
535 88 ▶ strncasecmp(vs, "close", 5) == 0)
Branch: 1 branch
BranchTrueFalseTotal
strncasecmp(vs, "close", 5) == 0) 87 1 88
536 87 c->want_close = 1;
537 184 ▶ else if (klen == 17 && strncasecmp(p, "Transfer-Encoding", 17) == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
else if (klen == 17 && strncasecmp(p, "Transfer-Encoding", 17) == 0 && 11 162 173
538 11 strncasestr_chunked(vs, ve))
539 11 c->chunked = 1;
540 }
541 394 p = eol + 2;
542 }
543 /* RFC 7230 3.3.3: Transfer-Encoding overrides Content-Length. A response
544 * carrying both is a response-smuggling signal; frame strictly by the
545 * chunked encoding and drop the ambiguous Content-Length so no code path
546 * (ft_body_complete, keep-alive completion) can ever frame by it. */
547 146 ▶ if (c->chunked) c->content_len = -1;
Branch: 1 branch
BranchTrueFalseTotal
if (c->chunked) c->content_len = -1; 11 135 146
548 /* RFC 7230 3.3.3: 1xx, 204 and 304 responses never carry a body, and a
549 * server MUST NOT send Content-Length on 1xx/204 - without this a 204 on
550 * a keep-alive connection would wait for close/timeout that never comes. */
551 146 ▶ if (c->status == 204 || c->status == 304 ||
Branch: 2 branches
BranchTrueFalseTotal
if (c->status == 204 || c->status == 304 || 146 0 146
if (c->status == 204 || c->status == 304 || 146 0 146
552 146 ▶ (c->status >= 100 && c->status < 200)) {
Branch: 2 branches
BranchTrueFalseTotal
(c->status >= 100 && c->status < 200)) { 146 0 146
(c->status >= 100 && c->status < 200)) { 1 145 146
553 1 c->content_len = 0;
554 1 c->chunked = 0;
555 }
556 146 c->have_headers = 1;
557 146 c->cpos = c->hdr_end; /* body decoding starts here */
558 146 ▶ if (c->chunked) c->chunk_left = -1;
Branch: 1 branch
BranchTrueFalseTotal
if (c->chunked) c->chunk_left = -1; 11 135 146
559 146 return 1;
560 }
561  
562 static void ft_emit_body(pTHX_ ft_conn *c, const char *buf, size_t len);
563  
564 /* chunk_left takes this value once the zero-size chunk has been read and
565 * the trailer section is being consumed */
566 #define FT_CHUNK_TRAILERS (-2)
567  
568 /* How much trailer section a response may send: the 64KB a server allows a
569 * request's header block. A section that grows past it fails the response
570 * instead of being buffered for ever, since the body behind it is complete
571 * but can never be handed over while the section has no end. */
572 #define FT_TRAILER_CAP 65536
573  
574 /* One line of the trailer section, without its CRLF. A field line is split
575 * at the first colon, its value trimmed, and appended to c->trailers with
576 * the name as the server wrote it. That is the shape h2 trailers already
577 * have, and they stay out of c->headers because they came after the body.
578 * A line with no colon, or with nothing before it, is skipped, as a header
579 * line of that shape is. */
580 588 static void ft_trailer_line(pTHX_ ft_conn *c, const char *line, size_t len) {
581 588 const char *colon = memchr(line, ':', len);
582 const char *vs, *ve;
583 588 ▶ if (!colon || colon == line) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!colon || colon == line) return; 587 1 588
if (!colon || colon == line) return; 0 587 587
584 587 vs = colon + 1;
585 587 ve = line + len;
586 1175 ▶ while (vs < ve && (*vs == ' ' || *vs == '\t')) vs++;
Branch: 3 branches
BranchTrueFalseTotal
while (vs < ve && (*vs == ' ' || *vs == '\t')) vs++; 1175 0 1175
while (vs < ve && (*vs == ' ' || *vs == '\t')) vs++; 587 588 1175
while (vs < ve && (*vs == ' ' || *vs == '\t')) vs++; 1 587 588
587 588 ▶ while (ve > vs && (ve[-1] == ' ' || ve[-1] == '\t')) ve--;
Branch: 3 branches
BranchTrueFalseTotal
while (ve > vs && (ve[-1] == ' ' || ve[-1] == '\t')) ve--; 588 0 588
while (ve > vs && (ve[-1] == ' ' || ve[-1] == '\t')) ve--; 1 587 588
while (ve > vs && (ve[-1] == ' ' || ve[-1] == '\t')) ve--; 0 587 587
588 587 ▶ if (!c->trailers) c->trailers = newAV();
Branch: 1 branch
BranchTrueFalseTotal
if (!c->trailers) c->trailers = newAV(); 5 582 587
589 587 av_push(c->trailers, newSVpvn(line, (size_t)(colon - line)));
590 587 av_push(c->trailers, newSVpvn(vs, (size_t)(ve - vs)));
591 }
592  
593 /* Incremental chunked decoding of rbuf[cpos..rlen] into dbody. chunk_done
594 * is set when the body is over, which is not at the zero-size chunk but at
595 * the blank line after the trailer section (RFC 9112 7.1).
596 *
597 * This used to stop at the zero-size chunk and leave the section and the
598 * final CRLF in the buffer. Normally they had arrived in the same read and
599 * were discarded with the buffer when the connection was parked, but when
600 * the final CRLF came in a later segment it was taken as the start of the
601 * next response on that connection. The section also holds data a caller
602 * may need: gRPC puts its call status there and nowhere else. */
603 27 static void ft_chunk_feed(pTHX_ ft_conn *c) {
604 683 ▶ while (c->cpos < c->rlen && !c->chunk_done) {
Branch: 2 branches
BranchTrueFalseTotal
while (c->cpos < c->rlen && !c->chunk_done) { 666 17 683
while (c->cpos < c->rlen && !c->chunk_done) { 666 0 666
605 666 ▶ if (c->chunk_left == FT_CHUNK_TRAILERS) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->chunk_left == FT_CHUNK_TRAILERS) { 598 68 666
606 598 char *base = c->rbuf + c->cpos;
607 598 size_t rem = c->rlen - c->cpos;
608 598 char *crlf = (char *)ft_memmem(base, rem, "\r\n", 2);
609 size_t len;
610 598 ▶ if (!crlf) { /* line incomplete */
Branch: 1 branch
BranchTrueFalseTotal
if (!crlf) { /* line incomplete */ 0 598 598
611 0 ▶ if (c->trailer_bytes + rem > FT_TRAILER_CAP) c->trailer_overrun = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (c->trailer_bytes + rem > FT_TRAILER_CAP) c->trailer_overrun = 1; 0 0 0
612 0 return;
613 }
614 598 len = (size_t)(crlf - base);
615 598 c->cpos += len + 2;
616 598 ▶ if (len == 0) { c->chunk_done = 1; return; } /* the blank line */
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) { c->chunk_done = 1; return; } /* the blank line */ 9 589 598
617 589 c->trailer_bytes += len + 2;
618 589 ▶ if (c->trailer_bytes > FT_TRAILER_CAP) { c->trailer_overrun = 1; return; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->trailer_bytes > FT_TRAILER_CAP) { c->trailer_overrun = 1; return; } 1 588 589
619 588 ft_trailer_line(aTHX_ c, base, len);
620 68 ▶ } else if (c->chunk_left < 0) { /* need a chunk-size line */
Branch: 1 branch
BranchTrueFalseTotal
} else if (c->chunk_left < 0) { /* need a chunk-size line */ 30 38 68
621 30 char *base = c->rbuf + c->cpos;
622 30 size_t rem = c->rlen - c->cpos;
623 30 char *crlf = (char *)ft_memmem(base, rem, "\r\n", 2);
624 long sz;
625 30 ▶ if (!crlf) return; /* size line incomplete */
Branch: 1 branch
BranchTrueFalseTotal
if (!crlf) return; /* size line incomplete */ 0 30 30
626 30 sz = strtol(base, NULL, 16); /* hex; ignores ;extensions */
627 30 c->cpos = (size_t)(crlf - c->rbuf) + 2;
628 30 ▶ if (sz <= 0) { c->chunk_left = FT_CHUNK_TRAILERS; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (sz <= 0) { c->chunk_left = FT_CHUNK_TRAILERS; continue; } 11 19 30
629 19 c->chunk_left = sz;
630 38 ▶ } else if (c->chunk_left > 0) { /* copy chunk data */
Branch: 1 branch
BranchTrueFalseTotal
} else if (c->chunk_left > 0) { /* copy chunk data */ 19 19 38
631 19 size_t avail = c->rlen - c->cpos;
632 19 size_t take = (size_t)c->chunk_left < avail ? (size_t)c->chunk_left : avail;
633 19 ▶ if (c->on_body) { /* stream: emit, do not buffer */
Branch: 1 branch
BranchTrueFalseTotal
if (c->on_body) { /* stream: emit, do not buffer */ 5 14 19
634 5 ft_emit_body(aTHX_ c, c->rbuf + c->cpos, take);
635 5 c->body_recv += take;
636 } else {
637 14 ▶ if (c->dblen + take > c->dbcap) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->dblen + take > c->dbcap) { 5 9 14
638 5 c->dbcap = (c->dblen + take) * 2 + 64;
639 5 Renew(c->dbody, c->dbcap, char);
640 }
641 14 memcpy(c->dbody + c->dblen, c->rbuf + c->cpos, take);
642 14 c->dblen += take;
643 }
644 19 c->cpos += take;
645 19 c->chunk_left -= (long)take;
646 } else { /* chunk_left == 0: trailing CRLF */
647 19 ▶ if (c->rlen - c->cpos < 2) return;
Branch: 1 branch
BranchTrueFalseTotal
if (c->rlen - c->cpos < 2) return; 0 19 19
648 19 c->cpos += 2;
649 19 c->chunk_left = -1;
650 }
651 }
652 }
653  
654 /* true when the full body has arrived */
655 134 static int ft_body_complete(ft_conn *c) {
656 134 size_t body = c->rlen - c->hdr_end;
657 134 ▶ if (c->content_len >= 0) return body >= (size_t)c->content_len;
Branch: 1 branch
BranchTrueFalseTotal
if (c->content_len >= 0) return body >= (size_t)c->content_len; 132 2 134
658 2 return 0; /* until-close: completion is signalled by EOF */
659 }
660  
661 /* hand one body chunk to the streaming sink; deaths become warnings */
662 /* One chunk to a streaming sink. Takes the coderef rather than the
663 * connection, because on HTTP/3 the sink belongs to one of several streams on
664 * a shared connection and there is no single c->on_body to reach for. */
665 6 static void ft_emit_body_sv(pTHX_ SV *on_body, const char *buf, size_t len) {
666 6 dSP;
667 6 ▶ if (!len || !on_body) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!len || !on_body) return; 6 0 6
if (!len || !on_body) return; 0 6 6
668 6 ENTER; SAVETMPS;
669 6 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 6 6
670 6 ▶ XPUSHs(sv_2mortal(newSVpvn(buf, len)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(buf, len))); 0 6 6
671 6 PUTBACK;
672 6 call_sv(on_body, G_DISCARD | G_EVAL);
673 6 ▶ if (SvTRUE(ERRSV)) warn("Fetch: on_body callback died: %s", SvPV_nolen(ERRSV));
Branch: 3 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) warn("Fetch: on_body callback died: %s", SvPV_nolen(ERRSV)); 6 0 6
if (SvTRUE(ERRSV)) warn("Fetch: on_body callback died: %s", SvPV_nolen(ERRSV)); 0 6 6
if (SvTRUE(ERRSV)) warn("Fetch: on_body callback died: %s", SvPV_nolen(ERRSV)); 0 0 0
674 6 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 6 0 6
675 }
676  
677 6 static void ft_emit_body(pTHX_ ft_conn *c, const char *buf, size_t len) {
678 6 ft_emit_body_sv(aTHX_ c->on_body, buf, len);
679 6 }
680  
681 /* Foreign loop: reconcile interest for c->fd to exactly `mask` by calling the
682 * loop object's _ft_arm($fd, $mask, $cv). mask 0 removes; bits are HM_EV_READ
683 * (0x1) / HM_EV_WRITE (0x2). The adapter fires c->watcher on readiness. */
684 0 static void ft_loop_arm(pTHX_ ft_conn *c, int mask) {
685 /* Resolve _ft_arm once per adapter class and call the CV directly, rather
686 * than re-resolving the method name on every arm. Real programs use one
687 * adapter class, so this cache hits on essentially every call; a different
688 * class just re-resolves. Falls back to call_method if resolution fails. */
689 static HV *cached_stash = NULL;
690 static CV *cached_cv = NULL;
691 0 ▶ HV *stash = (SvROK(c->loop_sv) && SvOBJECT(SvRV(c->loop_sv)))
Branch: 1 branch
BranchTrueFalseTotal
HV *stash = (SvROK(c->loop_sv) && SvOBJECT(SvRV(c->loop_sv))) 0 0 0
692 0 ▶ ? SvSTASH(SvRV(c->loop_sv)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? SvSTASH(SvRV(c->loop_sv)) : NULL; 0 0 0
693 CV *cv;
694 0 dSP;
695 0 ▶ if (stash && stash == cached_stash) {
Branch: 2 branches
BranchTrueFalseTotal
if (stash && stash == cached_stash) { 0 0 0
if (stash && stash == cached_stash) { 0 0 0
696 0 cv = cached_cv;
697 } else {
698 0 ▶ GV *gv = stash ? gv_fetchmethod_autoload(stash, "_ft_arm", FALSE) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
GV *gv = stash ? gv_fetchmethod_autoload(stash, "_ft_arm", FALSE) : NULL; 0 0 0
699 0 ▶ cv = gv ? GvCV(gv) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
cv = gv ? GvCV(gv) : NULL; 0 0 0
700 0 cached_stash = stash;
701 0 cached_cv = cv;
702 }
703 0 ENTER; SAVETMPS;
704 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
705 0 ▶ EXTEND(SP, 4);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 4); 0 0 0
706 0 PUSHs(c->loop_sv);
707 0 PUSHs(sv_2mortal(newSViv(c->fd)));
708 0 PUSHs(sv_2mortal(newSViv(mask)));
709 0 PUSHs(c->watcher);
710 0 PUTBACK;
711 0 ▶ if (cv) call_sv((SV *)cv, G_VOID | G_DISCARD);
Branch: 1 branch
BranchTrueFalseTotal
if (cv) call_sv((SV *)cv, G_VOID | G_DISCARD); 0 0 0
712 0 else call_method("_ft_arm", G_VOID | G_DISCARD);
713 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
714 0 }
715  
716 /* Foreign loop: schedule the deadline via _ft_timer($secs, $cv), keeping the
717 * returned cancel handle in c->timer_h. */
718 0 static void ft_loop_timer(pTHX_ ft_conn *c, double secs) {
719 0 dSP;
720 int count;
721 0 ENTER; SAVETMPS;
722 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
723 0 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 0 0
724 0 PUSHs(c->loop_sv);
725 0 PUSHs(sv_2mortal(newSVnv(secs)));
726 0 PUSHs(c->timer_cb);
727 0 PUTBACK;
728 0 count = call_method("_ft_timer", G_SCALAR);
729 0 SPAGAIN;
730 0 ▶ if (count > 0) { SV *h = POPs; if (SvOK(h)) c->timer_h = SvREFCNT_inc(h); }
Branch: 2 branches
BranchTrueFalseTotal
if (count > 0) { SV *h = POPs; if (SvOK(h)) c->timer_h = SvREFCNT_inc(h); } 0 0 0
if (count > 0) { SV *h = POPs; if (SvOK(h)) c->timer_h = SvREFCNT_inc(h); } 0 0 0
731 0 PUTBACK;
732 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
733 0 }
734  
735 /* Foreign loop: cancel the deadline via _ft_untimer($handle). */
736 0 static void ft_loop_untimer(pTHX_ ft_conn *c) {
737 0 dSP;
738 0 SV *h = c->timer_h;
739 0 c->timer_h = NULL;
740 0 ENTER; SAVETMPS;
741 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
742 0 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 0 0
743 0 PUSHs(c->loop_sv);
744 0 PUSHs(sv_2mortal(h)); /* hand our ref over to be mortalised */
745 0 PUTBACK;
746 0 call_method("_ft_untimer", G_VOID | G_DISCARD);
747 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
748 0 }
749  
750 /* Re-arm the loop for the readiness direction(s) we currently need. */
751 528 static void ft_arm(pTHX_ ft_conn *c, int mask) {
752 528 ▶ if (c->armed == mask) return;
Branch: 1 branch
BranchTrueFalseTotal
if (c->armed == mask) return; 280 248 528
753 248 ▶ if (c->hm) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->hm) { 0 248 248
754 /* Hyperman-direct: reconcile per direction through the C ABI, so an
755 * unchanged direction is left alone (no drop+re-add churn). */
756 0 int drop = c->armed & ~mask, add = mask & ~c->armed;
757 0 ▶ if (drop & HM_EV_READ)
Branch: 1 branch
BranchTrueFalseTotal
if (drop & HM_EV_READ) 0 0 0
758 0 c->hm->io_unwatch(aTHX_ c->hm_loop, c->fd, HM_ABI_READ);
759 0 ▶ if (drop & HM_EV_WRITE)
Branch: 1 branch
BranchTrueFalseTotal
if (drop & HM_EV_WRITE) 0 0 0
760 0 c->hm->io_unwatch(aTHX_ c->hm_loop, c->fd, HM_ABI_WRITE);
761 0 ▶ if (add & HM_EV_READ)
Branch: 1 branch
BranchTrueFalseTotal
if (add & HM_EV_READ) 0 0 0
762 0 c->hm->io_watch(aTHX_ c->hm_loop, c->fd, HM_ABI_READ, ft_hm_ready, c);
763 0 ▶ if (add & HM_EV_WRITE)
Branch: 1 branch
BranchTrueFalseTotal
if (add & HM_EV_WRITE) 0 0 0
764 0 c->hm->io_watch(aTHX_ c->hm_loop, c->fd, HM_ABI_WRITE, ft_hm_ready, c);
765 248 ▶ } else if (c->loop_sv) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c->loop_sv) { 0 248 248
766 0 ft_loop_arm(aTHX_ c, mask);
767 } else {
768 248 ▶ if (c->armed) ft_unwatch_io(aTHX_ c->loop, c->fd, c->armed);
Branch: 1 branch
BranchTrueFalseTotal
if (c->armed) ft_unwatch_io(aTHX_ c->loop, c->fd, c->armed); 120 128 248
769 248 ▶ if (mask) ft_watch_io(aTHX_ c->loop, c->fd, mask, c->watcher);
Branch: 1 branch
BranchTrueFalseTotal
if (mask) ft_watch_io(aTHX_ c->loop, c->fd, mask, c->watcher); 241 7 248
770 }
771 248 c->armed = mask;
772 }
773  
774 /* IO wrappers dispatching TLS vs raw; on would-block *want gets the needed
775 * readiness (HM_EV_READ/HM_EV_WRITE), else 0. */
776 346 static ssize_t ft_recv(ft_conn *c, void *buf, size_t n, int *want) {
777 346 *want = 0;
778 346 ▶ if (c->ssl) return ft_tls_read(c, buf, n, want);
Branch: 1 branch
BranchTrueFalseTotal
if (c->ssl) return ft_tls_read(c, buf, n, want); 18 328 346
779 {
780 328 ssize_t r = ft_os_recv(c->fd, buf, n, 0);
781 328 ▶ if (r < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) *want = HM_EV_READ;
Branch: 3 branches
BranchTrueFalseTotal
if (r < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) *want = HM_EV_READ; 162 166 328
if (r < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) *want = HM_EV_READ; 0 162 162
if (r < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) *want = HM_EV_READ; 0 0 0
782 328 return r;
783 }
784 }
785 156 static ssize_t ft_send(ft_conn *c, const void *buf, size_t n, int *want) {
786 156 *want = 0;
787 156 ▶ if (c->ssl) return ft_tls_write(c, buf, n, want);
Branch: 1 branch
BranchTrueFalseTotal
if (c->ssl) return ft_tls_write(c, buf, n, want); 6 150 156
788 {
789 150 ssize_t r = ft_os_send(c->fd, buf, n, 0);
790 150 ▶ if (r < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) *want = HM_EV_WRITE;
Branch: 3 branches
BranchTrueFalseTotal
if (r < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) *want = HM_EV_WRITE; 0 150 150
if (r < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) *want = HM_EV_WRITE; 0 0 0
if (r < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) *want = HM_EV_WRITE; 0 0 0
791 150 return r;
792 }
793 }
794  
795 #include "ft_h2.h" /* nghttp2 client; uses ft_recv/ft_send/ft_arm/finish/fail */
796 #include "ft_h3.h" /* nghttp3 + ngtcp2 client, on the same seams */
797 #include "ft_ws.h" /* WebSocket framing; uses ft_recv/ft_send/ft_arm */
798  
799 /* Verify a 101 + Sec-WebSocket-Accept and switch into frame mode, resolving
800 * the upgrade Future with a Fetch::WebSocket. Returns 1 on success, 0 if the
801 * handshake was not a valid WebSocket upgrade. */
802 1 static int ft_ws_upgrade(pTHX_ ft_conn *c) {
803 char expect[30];
804 1 const char *got = NULL;
805 1 STRLEN gotl = 0;
806 SSize_t i, n;
807 SV *ws, *fut;
808 1 ▶ if (c->status != 101 || !c->ws_key) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (c->status != 101 || !c->ws_key) return 0; 1 0 1
if (c->status != 101 || !c->ws_key) return 0; 0 1 1
809 1 ft_ws_accept(c->ws_key, expect);
810 1 ▶ n = c->headers ? av_len(c->headers) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
n = c->headers ? av_len(c->headers) + 1 : 0; 1 0 1
811 3 ▶ for (i = 0; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < n; i += 2) { 3 0 3
812 3 SV **k = av_fetch(c->headers, i, 0);
813 STRLEN kl;
814 const char *kp;
815 3 ▶ if (!k || !*k) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!k || !*k) continue; 3 0 3
if (!k || !*k) continue; 0 3 3
816 3 kp = SvPV(*k, kl);
817 3 ▶ if (kl == 20 && strncasecmp(kp, "Sec-WebSocket-Accept", 20) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 20 && strncasecmp(kp, "Sec-WebSocket-Accept", 20) == 0) { 1 2 3
if (kl == 20 && strncasecmp(kp, "Sec-WebSocket-Accept", 20) == 0) { 1 0 1
818 1 SV **v = av_fetch(c->headers, i + 1, 0);
819 2 ▶ if (v && *v) got = SvPV(*v, gotl);
Branch: 2 branches
BranchTrueFalseTotal
if (v && *v) got = SvPV(*v, gotl); 1 0 1
if (v && *v) got = SvPV(*v, gotl); 1 0 1
820 1 break;
821 }
822 }
823 1 ▶ if (!got || gotl != strlen(expect) || memcmp(got, expect, gotl) != 0)
Branch: 3 branches
BranchTrueFalseTotal
if (!got || gotl != strlen(expect) || memcmp(got, expect, gotl) != 0) 1 0 1
if (!got || gotl != strlen(expect) || memcmp(got, expect, gotl) != 0) 1 0 1
if (!got || gotl != strlen(expect) || memcmp(got, expect, gotl) != 0) 0 1 1
824 0 return 0;
825  
826 /* bytes past the header block are the first WebSocket frames */
827 1 ▶ if (c->rlen > c->hdr_end)
Branch: 1 branch
BranchTrueFalseTotal
if (c->rlen > c->hdr_end) 0 1 1
828 0 memmove(c->rbuf, c->rbuf + c->hdr_end, c->rlen - c->hdr_end);
829 1 ▶ c->rlen = c->rlen > c->hdr_end ? c->rlen - c->hdr_end : 0;
Branch: 1 branch
BranchTrueFalseTotal
c->rlen = c->rlen > c->hdr_end ? c->rlen - c->hdr_end : 0; 0 1 1
830 1 c->ws_pos = 0;
831 1 c->is_ws = 1;
832  
833 1 ws = sv_bless(newRV_noinc(newSViv(PTR2IV(c))),
834 gv_stashpv("Fetch::WebSocket", GV_ADD));
835 1 fut = SvREFCNT_inc(c->future);
836 1 SvREFCNT_dec(c->future); c->future = NULL;
837 1 hmf_settle(aTHX_ fut, HMF_DONE, &ws, 1);
838 1 SvREFCNT_dec(ws);
839 1 SvREFCNT_dec(fut);
840  
841 1 ft_ws_feed(aTHX_ c); /* dispatch anything already buffered */
842 1 ▶ if (!c->ws_closed) ft_ws_rearm(aTHX_ c);
Branch: 1 branch
BranchTrueFalseTotal
if (!c->ws_closed) ft_ws_rearm(aTHX_ c); 1 0 1
843 1 return 1;
844 }
845  
846 /* RFC 7231 4.2.2: methods a client may replay without changing what the
847 * request means. Read off the request line rather than c->rq_method, which
848 * belongs to the request that first opened the connection, not this one. */
849 2 static int ft_req_idempotent(const char *req, size_t len) {
850 static const char *const ok[] = { "GET", "HEAD", "PUT", "DELETE",
851 "OPTIONS", "TRACE", NULL };
852 2 size_t i = 0, n;
853 const char *const *m;
854 9 ▶ while (i < len && req[i] != ' ') i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < len && req[i] != ' ') i++; 9 0 9
while (i < len && req[i] != ' ') i++; 7 2 9
855 8 ▶ for (m = ok; *m; m++) {
Branch: 1 branch
BranchTrueFalseTotal
for (m = ok; *m; m++) { 7 1 8
856 7 n = strlen(*m);
857 7 ▶ if (n == i && strncmp(req, *m, n) == 0) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (n == i && strncmp(req, *m, n) == 0) return 1; 2 5 7
if (n == i && strncmp(req, *m, n) == 0) return 1; 1 1 2
858 }
859 1 return 0;
860 }
861  
862 /* A keep-alive connection revived from the pool can die under the very
863 * request that revived it: the server had already decided to close it and the
864 * FIN was still in flight when ft_conn_alive() peeked. That is not a request
865 * failure, it is a lost race, and every request over a pool has to survive it
866 * - so redial and send the same bytes again on a fresh socket.
867 *
868 * Only ever on a revived connection (c->reused, cleared here so one request
869 * cannot loop), only while nothing of a response has arrived, and only for an
870 * idempotent method - if any byte came back the server did answer, and a
871 * replay would be a second POST rather than a retry.
872 *
873 * Returns 1 when the request is back in flight on a new socket. */
874 2 static int ft_conn_retry(pTHX_ ft_conn *c) {
875 2 struct addrinfo hints, *ai = NULL, *rp;
876 2 int fd = -1;
877  
878 2 ▶ if (!c->reused || c->have_headers || c->rlen || c->body_recv) return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (!c->reused || c->have_headers || c->rlen || c->body_recv) return 0; 2 0 2
if (!c->reused || c->have_headers || c->rlen || c->body_recv) return 0; 2 0 2
if (!c->reused || c->have_headers || c->rlen || c->body_recv) return 0; 2 0 2
if (!c->reused || c->have_headers || c->rlen || c->body_recv) return 0; 0 2 2
879 2 ▶ if (c->is_h2 || c->is_ws || c->want_ws) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (c->is_h2 || c->is_ws || c->want_ws) return 0; 2 0 2
if (c->is_h2 || c->is_ws || c->want_ws) return 0; 2 0 2
if (c->is_h2 || c->is_ws || c->want_ws) return 0; 0 2 2
880 2 ▶ if (!c->host || !c->port || !c->req) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!c->host || !c->port || !c->req) return 0; 2 0 2
if (!c->host || !c->port || !c->req) return 0; 2 0 2
if (!c->host || !c->port || !c->req) return 0; 0 2 2
881 2 ▶ if (!ft_req_idempotent(c->req, c->req_len)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ft_req_idempotent(c->req, c->req_len)) return 0; 1 1 2
882  
883 1 memset(&hints, 0, sizeof(hints));
884 1 hints.ai_family = AF_UNSPEC;
885 1 hints.ai_socktype = SOCK_STREAM;
886 1 ▶ if (getaddrinfo(c->host, c->port, &hints, &ai) != 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (getaddrinfo(c->host, c->port, &hints, &ai) != 0) return 0; 0 1 1
887 1 ▶ for (rp = ai; rp; rp = rp->ai_next) {
Branch: 1 branch
BranchTrueFalseTotal
for (rp = ai; rp; rp = rp->ai_next) { 1 0 1
888 1 fd = ft_os_socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
889 1 ▶ if (fd < 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) continue; 0 1 1
890 1 ft_os_set_nonblock(fd);
891 1 ▶ if (ft_os_connect(fd, rp->ai_addr, (int)rp->ai_addrlen) == 0
Branch: 1 branch
BranchTrueFalseTotal
if (ft_os_connect(fd, rp->ai_addr, (int)rp->ai_addrlen) == 0 1 0 1
892 1 ▶ || errno == EINPROGRESS)
Branch: 1 branch
BranchTrueFalseTotal
|| errno == EINPROGRESS) 0 1 1
893 break;
894 0 ft_os_close(fd); fd = -1;
895 }
896 1 freeaddrinfo(ai);
897 1 ▶ if (fd < 0) return 0; /* cannot redial: let the caller fail */
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) return 0; /* cannot redial: let the caller fail */ 0 1 1
898  
899 1 ft_arm(aTHX_ c, 0); /* stop watching the dead fd first */
900 1 ft_tls_free(c);
901 1 ▶ if (c->fd >= 0) ft_os_close(c->fd);
Branch: 1 branch
BranchTrueFalseTotal
if (c->fd >= 0) ft_os_close(c->fd); 1 0 1
902 1 c->fd = fd;
903 1 c->state = FT_CONNECTING;
904 1 c->req_off = 0;
905 1 c->reused = 0; /* one redial per request */
906 /* the deadline armed for this request keeps running: a redial buys
907 * another socket, not more time. */
908 1 ft_arm(aTHX_ c, HM_EV_WRITE);
909 1 return 1;
910 }
911  
912 /* One pass of the connection state machine. TLS handshake, request send, and
913 * response recv+parse all re-arm the loop for whichever direction OpenSSL (or
914 * the socket) next needs. */
915 /* Walk a resolved address list and start a non-blocking connect on the first
916 * address that takes one. Returns the fd, or -1 with errno set by the last
917 * attempt. Shared by the inline and the threaded resolve so the two cannot
918 * drift in what "the first usable address" means. */
919 121 static int ft_conn_dial(struct addrinfo *ai) {
920 struct addrinfo *rp;
921 121 ▶ for (rp = ai; rp; rp = rp->ai_next) {
Branch: 1 branch
BranchTrueFalseTotal
for (rp = ai; rp; rp = rp->ai_next) { 121 0 121
922 121 int fd = ft_os_socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
923 121 ▶ if (fd < 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) continue; 0 121 121
924 121 ft_os_set_nonblock(fd);
925 121 ▶ if (ft_os_connect(fd, rp->ai_addr, (int)rp->ai_addrlen) == 0
Branch: 1 branch
BranchTrueFalseTotal
if (ft_os_connect(fd, rp->ai_addr, (int)rp->ai_addrlen) == 0 121 0 121
926 121 ▶ || errno == EINPROGRESS)
Branch: 1 branch
BranchTrueFalseTotal
|| errno == EINPROGRESS) 121 0 121
927 121 return fd;
928 0 ft_os_close(fd);
929 }
930 0 return -1;
931 }
932  
933 345 static void ft_conn_step(pTHX_ ft_conn *c) {
934 345 ▶ if (c->is_ws) { ft_ws_step(aTHX_ c); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->is_ws) { ft_ws_step(aTHX_ c); return; } 5 340 345
935 340 ▶ if (c->is_h2) { ft_h2_step(aTHX_ c); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->is_h2) { ft_h2_step(aTHX_ c); return; } 0 340 340
936 340 ▶ if (c->is_h3) { ft_h3_step(aTHX_ c); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->is_h3) { ft_h3_step(aTHX_ c); return; } 0 340 340
937  
938 340 ▶ if (c->state == FT_RESOLVING) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->state == FT_RESOLVING) { 6 334 340
939 /* The resolver woke us. The byte carries nothing - the answer is in
940 * the struct, and it is the write that made it visible here. */
941 6 ft_dns *d = c->dns;
942 char b;
943 int fd;
944 6 (void)!read(c->fd, &b, 1);
945 /* Unarm BEFORE the fd changes: ft_arm reconciles against c->fd, so
946 * dropping the watch afterwards would name the socket that is about
947 * to take its place - or, once the number is recycled, somebody
948 * else's. */
949 6 ft_arm(aTHX_ c, 0);
950 6 ft_os_close(c->fd);
951 6 c->fd = -1;
952 6 c->dns = NULL;
953 6 ▶ if (!d || d->gai != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!d || d->gai != 0) { 6 0 6
if (!d || d->gai != 0) { 1 5 6
954 char msg[320];
955 3 ▶ my_snprintf(msg, sizeof msg, "resolve %s:%s: %s",
Branch: 1 branch
BranchTrueFalseTotal
my_snprintf(msg, sizeof msg, "resolve %s:%s: %s", 1 0 1
956 2 ▶ c->host ? c->host : "?", c->port ? c->port : "?",
Branch: 2 branches
BranchTrueFalseTotal
c->host ? c->host : "?", c->port ? c->port : "?", 1 0 1
c->host ? c->host : "?", c->port ? c->port : "?", 1 0 1
957 1 d ? gai_strerror(d->gai) : "no resolver");
958 1 ft_dns_unref(d);
959 1 ft_conn_fail(aTHX_ c, msg);
960 1 return;
961 }
962 5 fd = ft_conn_dial(d->ai);
963 5 ft_dns_unref(d);
964 5 ▶ if (fd < 0) { ft_conn_fail(aTHX_ c, "connect failed"); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { ft_conn_fail(aTHX_ c, "connect failed"); return; } 0 5 5
965 5 c->fd = fd;
966 5 c->state = FT_CONNECTING;
967 5 ft_arm(aTHX_ c, HM_EV_WRITE); /* wait for connect() to complete */
968 5 return;
969 }
970  
971 334 ▶ if (c->state == FT_CONNECTING) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->state == FT_CONNECTING) { 122 212 334
972 122 int err = 0; socklen_t el = sizeof(err);
973 122 ▶ if (ft_os_getsockopt(c->fd, SOL_SOCKET, SO_ERROR, &err, &el) < 0 || err) {
Branch: 2 branches
BranchTrueFalseTotal
if (ft_os_getsockopt(c->fd, SOL_SOCKET, SO_ERROR, &err, &el) < 0 || err) { 122 0 122
if (ft_os_getsockopt(c->fd, SOL_SOCKET, SO_ERROR, &err, &el) < 0 || err) { 9 113 122
974 9 ▶ ft_conn_fail(aTHX_ c, err ? strerror(err) : "connect failed");
Branch: 1 branch
BranchTrueFalseTotal
ft_conn_fail(aTHX_ c, err ? strerror(err) : "connect failed"); 9 0 9
975 9 return;
976 }
977 113 ▶ if (c->tls) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls) { 7 106 113
978 if (!FT_TLS_AVAILABLE) {
979 ft_conn_fail(aTHX_ c, "https requires OpenSSL (rebuild with libssl)");
980 return;
981 }
982 7 ▶ if (ft_tls_start(c, c->host, c->verify) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ft_tls_start(c, c->host, c->verify) != 0) { 0 7 7
983 0 ft_conn_fail(aTHX_ c, "TLS setup failed");
984 0 return;
985 }
986 7 c->state = FT_HANDSHAKING;
987 } else {
988 106 c->state = FT_WRITING;
989 }
990 }
991  
992 325 ▶ if (c->state == FT_HANDSHAKING) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->state == FT_HANDSHAKING) { 14 311 325
993 14 int want = HM_EV_READ;
994 14 int r = ft_tls_handshake(c, &want);
995 21 ▶ if (r < 0) { ft_conn_fail(aTHX_ c, "TLS handshake failed"); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (r < 0) { ft_conn_fail(aTHX_ c, "TLS handshake failed"); return; } 1 13 14
996 13 ▶ if (r == 0) { ft_arm(aTHX_ c, want); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (r == 0) { ft_arm(aTHX_ c, want); return; } 7 6 13
997 6 c->state = FT_WRITING; /* handshake complete */
998  
999 /* ALPN may have negotiated h2: switch to the nghttp2 client. A
1000 * WebSocket upgrade stays on HTTP/1.1. */
1001 if (FT_H2_AVAILABLE && !c->want_ws && ft_tls_is_h2(c)) {
1002 if (ft_h2_init(aTHX_ c) != 0) { ft_conn_fail(aTHX_ c, "http/2 init failed"); return; }
1003 c->is_h2 = 1;
1004 ft_h2_step(aTHX_ c);
1005 return;
1006 }
1007 }
1008  
1009 317 ▶ if (c->state == FT_WRITING) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->state == FT_WRITING) { 150 167 317
1010 300 ▶ while (c->req_off < c->req_len) {
Branch: 1 branch
BranchTrueFalseTotal
while (c->req_off < c->req_len) { 150 150 300
1011 150 int want = 0;
1012 150 ssize_t n = ft_send(c, c->req + c->req_off, c->req_len - c->req_off, &want);
1013 150 ▶ if (n > 0) { c->req_off += (size_t)n; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { c->req_off += (size_t)n; continue; } 150 0 150
1014 0 ▶ if (n < 0 && want) { ft_arm(aTHX_ c, want); return; }
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0 && want) { ft_arm(aTHX_ c, want); return; } 0 0 0
if (n < 0 && want) { ft_arm(aTHX_ c, want); return; } 0 0 0
1015 0 ▶ if (n < 0 && errno == EINTR) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0 && errno == EINTR) continue; 0 0 0
if (n < 0 && errno == EINTR) continue; 0 0 0
1016 0 ▶ if (ft_conn_retry(aTHX_ c)) return; /* pooled socket died mid-send */
Branch: 1 branch
BranchTrueFalseTotal
if (ft_conn_retry(aTHX_ c)) return; /* pooled socket died mid-send */ 0 0 0
1017 0 ft_conn_fail(aTHX_ c, strerror(errno)); return;
1018 }
1019 150 c->state = FT_READING;
1020 150 ft_arm(aTHX_ c, HM_EV_READ);
1021 }
1022  
1023 317 ▶ if (c->state == FT_READING) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->state == FT_READING) { 317 0 317
1024 19 for (;;) {
1025 336 int want = 0; ssize_t n;
1026 336 ▶ if (c->rlen + 65536 > c->rcap) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->rlen + 65536 > c->rcap) { 117 219 336
1027 117 ▶ c->rcap = c->rcap ? c->rcap * 2 : 65536;
Branch: 1 branch
BranchTrueFalseTotal
c->rcap = c->rcap ? c->rcap * 2 : 65536; 6 111 117
1028 117 Renew(c->rbuf, c->rcap, char);
1029 }
1030 336 n = ft_recv(c, c->rbuf + c->rlen, c->rcap - c->rlen, &want);
1031 336 ▶ if (n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { 163 173 336
1032 163 c->rlen += (size_t)n;
1033 163 ▶ if (!c->have_headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (!c->have_headers) { 146 17 163
1034 146 int r = ft_parse_headers(aTHX_ c);
1035 463 ▶ if (r < 0) { ft_conn_fail(aTHX_ c, "malformed response"); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (r < 0) { ft_conn_fail(aTHX_ c, "malformed response"); return; } 0 146 146
1036 }
1037 163 ▶ if (c->have_headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->have_headers) { 163 0 163
1038 163 ▶ if (c->want_ws) { /* expecting 101 Switching Protocols */
Branch: 1 branch
BranchTrueFalseTotal
if (c->want_ws) { /* expecting 101 Switching Protocols */ 1 162 163
1039 1 ▶ if (ft_ws_upgrade(aTHX_ c)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ft_ws_upgrade(aTHX_ c)) return; 1 0 1
1040 0 ft_conn_fail(aTHX_ c, "websocket upgrade rejected");
1041 0 return;
1042 }
1043 162 ▶ if (c->on_headers && !c->headers_fired) {
Branch: 2 branches
BranchTrueFalseTotal
if (c->on_headers && !c->headers_fired) { 0 162 162
if (c->on_headers && !c->headers_fired) { 0 0 0
1044 0 c->headers_fired = 1;
1045 0 ft_fire_headers(aTHX_ c);
1046 }
1047 162 ▶ if (c->chunked) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->chunked) { 27 135 162
1048 27 ft_chunk_feed(aTHX_ c);
1049 27 ▶ if (c->trailer_overrun) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->trailer_overrun) { 1 26 27
1050 1 ft_conn_fail(aTHX_ c, "trailer section too large");
1051 1 return;
1052 }
1053 26 ▶ if (c->on_body && c->cpos > c->hdr_end) { /* compact */
Branch: 2 branches
BranchTrueFalseTotal
if (c->on_body && c->cpos > c->hdr_end) { /* compact */ 6 20 26
if (c->on_body && c->cpos > c->hdr_end) { /* compact */ 5 1 6
1054 5 size_t keep = c->rlen - c->cpos;
1055 5 memmove(c->rbuf + c->hdr_end, c->rbuf + c->cpos, keep);
1056 5 c->rlen = c->hdr_end + keep;
1057 5 c->cpos = c->hdr_end;
1058 }
1059 26 ▶ if (c->chunk_done) { ft_conn_finish(aTHX_ c); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->chunk_done) { ft_conn_finish(aTHX_ c); return; } 9 17 26
1060 135 ▶ } else if (c->on_body) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c->on_body) { 1 134 135
1061 1 size_t blen = c->rlen - c->hdr_end;
1062 1 ▶ if (blen) {
Branch: 1 branch
BranchTrueFalseTotal
if (blen) { 1 0 1
1063 1 ft_emit_body(aTHX_ c, c->rbuf + c->hdr_end, blen);
1064 1 c->body_recv += blen;
1065 1 c->rlen = c->hdr_end; /* drop, do not accumulate */
1066 }
1067 1 ▶ if (c->content_len >= 0 &&
Branch: 1 branch
BranchTrueFalseTotal
if (c->content_len >= 0 && 1 0 1
1068 1 ▶ c->body_recv >= (size_t)c->content_len) {
Branch: 1 branch
BranchTrueFalseTotal
c->body_recv >= (size_t)c->content_len) { 1 0 1
1069 1 ft_conn_finish(aTHX_ c); return;
1070 }
1071 134 ▶ } else if (ft_body_complete(c)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (ft_body_complete(c)) { 132 2 134
1072 132 ft_conn_finish(aTHX_ c); return;
1073 }
1074 }
1075 19 continue;
1076 }
1077 173 ▶ if (n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { 4 169 173
1078 4 ▶ if (c->have_headers && !c->chunked && c->content_len < 0)
Branch: 3 branches
BranchTrueFalseTotal
if (c->have_headers && !c->chunked && c->content_len < 0) 2 2 4
if (c->have_headers && !c->chunked && c->content_len < 0) 1 1 2
if (c->have_headers && !c->chunked && c->content_len < 0) 1 0 1
1079 1 ft_conn_finish(aTHX_ c);
1080 /* The server closed inside the trailer section, after the
1081 * zero-size chunk but before the blank line. The body is
1082 * complete and the fields read so far are kept, so this is a
1083 * response, not a failure. The connection is closed, so it
1084 * is not parked for reuse. */
1085 3 ▶ else if (c->have_headers && c->chunked
Branch: 2 branches
BranchTrueFalseTotal
else if (c->have_headers && c->chunked 1 2 3
else if (c->have_headers && c->chunked 1 0 1
1086 1 ▶ && c->chunk_left == FT_CHUNK_TRAILERS) {
Branch: 1 branch
BranchTrueFalseTotal
&& c->chunk_left == FT_CHUNK_TRAILERS) { 1 0 1
1087 1 c->want_close = 1;
1088 1 ft_conn_finish(aTHX_ c);
1089 }
1090 2 ▶ else if (ft_conn_retry(aTHX_ c)) /* server hung up on reuse */
Branch: 1 branch
BranchTrueFalseTotal
else if (ft_conn_retry(aTHX_ c)) /* server hung up on reuse */ 1 1 2
1091 ;
1092 else
1093 1 ft_conn_fail(aTHX_ c, "connection closed before response was complete");
1094 4 return;
1095 }
1096 169 ▶ if (want) { ft_arm(aTHX_ c, want); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (want) { ft_arm(aTHX_ c, want); return; } 169 0 169
1097 0 ▶ if (errno == EINTR) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) continue; 0 0 0
1098 0 ▶ if (ft_conn_retry(aTHX_ c)) return; /* reset before any response */
Branch: 1 branch
BranchTrueFalseTotal
if (ft_conn_retry(aTHX_ c)) return; /* reset before any response */ 0 0 0
1099 0 ft_conn_fail(aTHX_ c, strerror(errno)); return;
1100 }
1101 }
1102 }
1103  
1104 /* the loop's readiness callback: a C closure carrying the ft_conn* in cl->i */
1105 XS_INTERNAL(ft_conn_ready_cb);
1106 307 XS_INTERNAL(ft_conn_ready_cb) {
1107 307 dVAR; dXSARGS;
1108 307 hm_clos *cl = hm_clos_of(aTHX_ cv);
1109 307 ft_conn *c = INT2PTR(ft_conn *, cl->i);
1110 PERL_UNUSED_VAR(items);
1111 307 ▶ if (c->state == FT_PARKED) { /* readable while idle: the server hung up */
Branch: 1 branch
BranchTrueFalseTotal
if (c->state == FT_PARKED) { /* readable while idle: the server hung up */ 0 307 307
1112 0 ▶ if (c->pool) ft_pool_remove(c->pool, c);
Branch: 1 branch
BranchTrueFalseTotal
if (c->pool) ft_pool_remove(c->pool, c); 0 0 0
1113 0 ft_conn_free(aTHX_ c);
1114 0 XSRETURN_EMPTY;
1115 }
1116 307 ft_conn_step(aTHX_ c);
1117 307 XSRETURN_EMPTY;
1118 }
1119  
1120 /* the deadline callback: the timer has fired (and been consumed by the loop),
1121 * so clear our handles before failing so teardown does not re-cancel it. */
1122 XS_INTERNAL(ft_conn_timeout_cb);
1123 2 XS_INTERNAL(ft_conn_timeout_cb) {
1124 2 dVAR; dXSARGS;
1125 2 hm_clos *cl = hm_clos_of(aTHX_ cv);
1126 2 ft_conn *c = INT2PTR(ft_conn *, cl->i);
1127 PERL_UNUSED_VAR(items);
1128 2 c->timer = NULL;
1129 2 ▶ if (c->timer_h) { SvREFCNT_dec(c->timer_h); c->timer_h = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->timer_h) { SvREFCNT_dec(c->timer_h); c->timer_h = NULL; } 0 2 2
1130 2 ft_conn_fail(aTHX_ c, "request timed out");
1131 2 XSRETURN_EMPTY;
1132 }
1133  
1134 /* Hyperman-direct (hm_abi) callbacks: same behavior as the closures above,
1135 * with the ft_conn as ud - no CV, no Perl call frame. */
1136 0 static void ft_hm_ready(pTHX_ int fd, int mask, void *ud) {
1137 0 ft_conn *c = (ft_conn *)ud;
1138 PERL_UNUSED_VAR(fd);
1139 PERL_UNUSED_VAR(mask);
1140 0 ▶ if (c->state == FT_PARKED) { /* readable while idle: the server hung up */
Branch: 1 branch
BranchTrueFalseTotal
if (c->state == FT_PARKED) { /* readable while idle: the server hung up */ 0 0 0
1141 0 ▶ if (c->pool) ft_pool_remove(c->pool, c);
Branch: 1 branch
BranchTrueFalseTotal
if (c->pool) ft_pool_remove(c->pool, c); 0 0 0
1142 0 ft_conn_free(aTHX_ c);
1143 0 return;
1144 }
1145 0 ft_conn_step(aTHX_ c);
1146 }
1147  
1148 0 static void ft_hm_timeout(pTHX_ void *ud) {
1149 0 ft_conn *c = (ft_conn *)ud;
1150 0 c->hm_timer = NULL; /* the handle died with the fire */
1151 0 ft_conn_fail(aTHX_ c, "request timed out");
1152 0 }
1153  
1154 /* ---- the loss-detection timer -------------------------------------------- */
1155  
1156 XS_INTERNAL(ft_conn_pto_cb);
1157 0 XS_INTERNAL(ft_conn_pto_cb) {
1158 0 dVAR; dXSARGS;
1159 0 hm_clos *cl = hm_clos_of(aTHX_ cv);
1160 0 ft_conn *c = INT2PTR(ft_conn *, cl->i);
1161 PERL_UNUSED_VAR(items);
1162 0 c->pto = NULL;
1163 0 ▶ if (c->pto_h) { SvREFCNT_dec(c->pto_h); c->pto_h = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->pto_h) { SvREFCNT_dec(c->pto_h); c->pto_h = NULL; } 0 0 0
1164 0 ▶ if (c->pto_fn) c->pto_fn(aTHX_ c);
Branch: 1 branch
BranchTrueFalseTotal
if (c->pto_fn) c->pto_fn(aTHX_ c); 0 0 0
1165 0 XSRETURN_EMPTY;
1166 }
1167  
1168 0 static void ft_hm_pto(pTHX_ void *ud) {
1169 0 ft_conn *c = (ft_conn *)ud;
1170 0 c->hm_pto = NULL; /* the handle died with the fire */
1171 0 ▶ if (c->pto_fn) c->pto_fn(aTHX_ c);
Branch: 1 branch
BranchTrueFalseTotal
if (c->pto_fn) c->pto_fn(aTHX_ c); 0 0 0
1172 0 }
1173  
1174 122 static void ft_conn_cancel_pto(pTHX_ ft_conn *c) {
1175 122 ▶ if (c->hm_pto) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->hm_pto) { 0 122 122
1176 0 ▶ if (!PL_dirty) c->hm->timer_cancel(aTHX_ c->hm_loop, c->hm_pto);
Branch: 1 branch
BranchTrueFalseTotal
if (!PL_dirty) c->hm->timer_cancel(aTHX_ c->hm_loop, c->hm_pto); 0 0 0
1177 0 c->hm_pto = NULL;
1178 }
1179 122 ▶ if (c->pto) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->pto) { 0 122 122
1180 0 ▶ if (!PL_dirty) ft_del_timer(aTHX_ c->loop, c->pto);
Branch: 1 branch
BranchTrueFalseTotal
if (!PL_dirty) ft_del_timer(aTHX_ c->loop, c->pto); 0 0 0
1181 0 c->pto = NULL;
1182 }
1183 122 ▶ if (c->pto_h) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->pto_h) { 0 122 122
1184 /* the foreign-loop handle: cancelled the same way the deadline's is */
1185 0 SvREFCNT_dec(c->pto_h);
1186 0 c->pto_h = NULL;
1187 }
1188 122 }
1189  
1190 /* Can this connection carry a probe timeout at all?
1191 *
1192 * It needs a real one-shot timer. The native loop has them and the
1193 * Hyperman-direct path has them (hm->timer is a kernel timer with a C
1194 * callback). A FOREIGN Perl loop does not, necessarily: Fetch::Loop::Hyperman
1195 * without the C ABI deliberately keeps a list and sweeps it every 0.1s, which
1196 * is a sound trade for a request deadline measured in seconds and useless for
1197 * loss detection measured in milliseconds - a PTO quantised to 100ms is not a
1198 * PTO, it is a stall.
1199 *
1200 * So this is asked before a transport that needs one is chosen, and the honest
1201 * answer where it is false is to refuse that transport rather than to run it
1202 * badly. */
1203 0 static int ft_conn_can_pto(ft_conn *c) {
1204 0 ▶ return c->hm != NULL || c->loop_sv == NULL;
Branch: 2 branches
BranchTrueFalseTotal
return c->hm != NULL || c->loop_sv == NULL; 0 0 0
return c->hm != NULL || c->loop_sv == NULL; 0 0 0
1205 }
1206  
1207 /* (Re)arm the probe timeout. Unlike the deadline this is called constantly,
1208 * so it cancels whatever was pending first: the last call wins. */
1209 0 static void ft_conn_arm_pto(pTHX_ ft_conn *c, double secs,
1210 void (*fn)(pTHX_ struct ft_conn *c)) {
1211 0 ft_conn_cancel_pto(aTHX_ c);
1212 0 c->pto_fn = fn;
1213 0 ▶ if (secs <= 0 || !fn) return;
Branch: 2 branches
BranchTrueFalseTotal
if (secs <= 0 || !fn) return; 0 0 0
if (secs <= 0 || !fn) return; 0 0 0
1214 0 ▶ if (c->hm) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->hm) { 0 0 0
1215 0 c->hm_pto = c->hm->timer(aTHX_ c->hm_loop, secs, ft_hm_pto, c);
1216 0 return;
1217 }
1218 0 ▶ if (c->loop_sv) return; /* refused above; nothing to arm */
Branch: 1 branch
BranchTrueFalseTotal
if (c->loop_sv) return; /* refused above; nothing to arm */ 0 0 0
1219 0 ▶ if (!c->pto_cb)
Branch: 1 branch
BranchTrueFalseTotal
if (!c->pto_cb) 0 0 0
1220 0 c->pto_cb = hm_closure(aTHX_ ft_conn_pto_cb, NULL, NULL, NULL, NULL,
1221 PTR2IV(c), 0);
1222 0 c->pto = ft_add_timer(aTHX_ c->loop, secs, c->pto_cb, 1);
1223 }
1224  
1225 /* Arm the per-request deadline (seconds). Native loop uses the C timer;
1226 * a foreign loop is asked via _ft_timer. */
1227 160 static void ft_conn_arm_timeout(pTHX_ ft_conn *c, double secs) {
1228 160 ▶ if (secs <= 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (secs <= 0) return; 134 26 160
1229 26 ▶ if (c->hm) { /* precise one-shot kernel timer, C callback, cancellable */
Branch: 1 branch
BranchTrueFalseTotal
if (c->hm) { /* precise one-shot kernel timer, C callback, cancellable */ 0 26 26
1230 0 c->hm_timer = c->hm->timer(aTHX_ c->hm_loop, secs, ft_hm_timeout, c);
1231 0 return;
1232 }
1233 26 c->timer_cb = hm_closure(aTHX_ ft_conn_timeout_cb, NULL, NULL, NULL, NULL,
1234 PTR2IV(c), 0);
1235 26 ▶ if (c->loop_sv) ft_loop_timer(aTHX_ c, secs);
Branch: 1 branch
BranchTrueFalseTotal
if (c->loop_sv) ft_loop_timer(aTHX_ c, secs); 0 26 26
1236 26 else c->timer = ft_add_timer(aTHX_ c->loop, secs, c->timer_cb, 1);
1237 }
1238  
1239 /* A parked keep-alive connection may have been closed by the server while
1240 * idle. A one-byte MSG_PEEK distinguishes a healthy idle socket (EAGAIN, no
1241 * pending data) from a closed or confused one (0, unexpected data, or error). */
1242 38 static int ft_conn_alive(ft_conn *c) {
1243 char b;
1244 38 ssize_t n = ft_os_recv(c->fd, &b, 1, MSG_PEEK);
1245 38 ▶ if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) return 1;
Branch: 3 branches
BranchTrueFalseTotal
if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) return 1; 38 0 38
if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) return 1; 0 38 38
if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) return 1; 0 0 0
1246 0 return 0;
1247 }
1248  
1249 /* Revive a parked connection for a new request: swap in the new request bytes,
1250 * future and streaming sink, then send straight away (fd + TLS already up). */
1251 38 static void ft_conn_revive(pTHX_ ft_conn *c, const char *req_bytes, STRLEN req_len,
1252 double timeout, SV *body, SV *on_body, SV *future) {
1253 38 c->state = FT_WRITING;
1254 38 Safefree(c->req);
1255 38 Newx(c->req, req_len, char);
1256 38 memcpy(c->req, req_bytes, req_len);
1257 38 c->req_len = req_len; c->req_off = 0;
1258 38 c->future = SvREFCNT_inc(future);
1259 38 ▶ if (c->on_body) { SvREFCNT_dec(c->on_body); c->on_body = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->on_body) { SvREFCNT_dec(c->on_body); c->on_body = NULL; } 0 38 38
1260 38 ▶ if (on_body && SvOK(on_body) && SvROK(on_body)) c->on_body = SvREFCNT_inc(on_body);
Branch: 3 branches
BranchTrueFalseTotal
if (on_body && SvOK(on_body) && SvROK(on_body)) c->on_body = SvREFCNT_inc(on_body); 38 0 38
if (on_body && SvOK(on_body) && SvROK(on_body)) c->on_body = SvREFCNT_inc(on_body); 0 38 38
if (on_body && SvOK(on_body) && SvROK(on_body)) c->on_body = SvREFCNT_inc(on_body); 0 0 0
1261 38 ▶ if (c->on_headers) { SvREFCNT_dec(c->on_headers); c->on_headers = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->on_headers) { SvREFCNT_dec(c->on_headers); c->on_headers = NULL; } 0 38 38
1262 38 ▶ if (ft_conn_on_headers_next) c->on_headers = SvREFCNT_inc(ft_conn_on_headers_next);
Branch: 1 branch
BranchTrueFalseTotal
if (ft_conn_on_headers_next) c->on_headers = SvREFCNT_inc(ft_conn_on_headers_next); 0 38 38
1263 38 c->headers_fired = 0;
1264 38 ▶ if (c->rq_body) { SvREFCNT_dec(c->rq_body); c->rq_body = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->rq_body) { SvREFCNT_dec(c->rq_body); c->rq_body = NULL; } 0 38 38
1265 38 ▶ if (SvOK(body)) c->rq_body = newSVsv(body);
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(body)) c->rq_body = newSVsv(body); 1 37 38
1266 38 c->reused = 1;
1267 38 ft_conn_arm_timeout(aTHX_ c, timeout);
1268 /* The fd and TLS are already up and the socket is writable, and we are
1269 * still READ-armed from parking. Send now rather than waiting for a
1270 * writability callback: a complete send transitions to READING and leaves
1271 * us correctly armed with no re-arm crossing at all. (A rare short write
1272 * falls back to arming WRITE inside ft_conn_step.) */
1273 38 ft_conn_step(aTHX_ c);
1274 38 }
1275  
1276 /* Start a request. host/port already split; req_bytes is the full HTTP/1.1
1277 * request. Returns a Fetch::Future (pending) resolving to a Fetch::Response. */
1278 160 static SV *ft_h1_start(pTHX_ ft_loop *loop, SV *loop_sv, ft_pool *pool,
1279 const char *host, const char *port,
1280 const char *req_bytes, STRLEN req_len,
1281 int tls, int verify, double timeout,
1282 SV *method, SV *scheme, SV *authority, SV *path,
1283 SV *headers_av, SV *body, SV *on_body,
1284 const char *ws_key) {
1285 160 struct addrinfo *ai = NULL;
1286 160 int fd = -1, dnsfd = -1;
1287 160 ft_dns *dns = NULL;
1288 ft_conn *c;
1289 char poolkey[300];
1290 160 SV *future = hmf_new(aTHX_ "Fetch::Future");
1291  
1292 /* reuse a live parked connection to the same host, if we have one */
1293 160 ▶ if (pool) {
Branch: 1 branch
BranchTrueFalseTotal
if (pool) { 154 6 160
1294 154 snprintf(poolkey, sizeof(poolkey), "%s:%d:%s:%s",
1295 ft_conn_proto_next, tls ? 1 : 0, host, port);
1296 0 for (;;) {
1297 154 ft_conn *k = ft_pool_take(pool, poolkey);
1298 154 ▶ if (!k) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!k) break; 116 38 154
1299 38 ▶ if (!ft_conn_alive(k)) { k->pool = NULL; ft_conn_free(aTHX_ k); continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ft_conn_alive(k)) { k->pool = NULL; ft_conn_free(aTHX_ k); continue; } 0 38 38
1300 38 ft_conn_revive(aTHX_ k, req_bytes, req_len, timeout, body, on_body, future);
1301 38 return future;
1302 }
1303 }
1304  
1305 /* Three ways to get an address, in order of what costs least.
1306 *
1307 * A literal - which is every request in this dist's own tests, and every
1308 * request to a machine named by number - is a parse and nothing else, so
1309 * it connects here with no thread and no extra loop turn.
1310 *
1311 * A name goes to the resolver thread (ft_dns.h) and the connection starts
1312 * in FT_RESOLVING with the pipe as its fd. Only if that cannot be started
1313 * at all - no pthreads in the build, native Windows, or the process out
1314 * of threads - does this fall back to resolving inline, which is what
1315 * every request used to do and is the one thing here that can stall the
1316 * loop. */
1317 122 ▶ if (ft_dns_numeric(host, port, &ai)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ft_dns_numeric(host, port, &ai)) { 116 6 122
1318 116 fd = ft_conn_dial(ai);
1319 116 freeaddrinfo(ai);
1320 116 ai = NULL;
1321 116 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 0 116 116
1322 0 SV *e = sv_2mortal(newSVpvf("Fetch: connect %s:%s: %s",
1323 host, port, strerror(errno)));
1324 0 hmf_settle(aTHX_ future, HMF_FAILED, &e, 1);
1325 0 return future;
1326 }
1327 }
1328 6 ▶ else if ((dnsfd = ft_dns_start(host, port, &dns)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if ((dnsfd = ft_dns_start(host, port, &dns)) < 0) { 0 6 6
1329 struct addrinfo hints;
1330 int gai;
1331 0 memset(&hints, 0, sizeof(hints));
1332 0 hints.ai_family = AF_UNSPEC;
1333 0 hints.ai_socktype = SOCK_STREAM;
1334 0 gai = getaddrinfo(host, port, &hints, &ai);
1335 0 ▶ if (gai != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gai != 0) { 0 0 0
1336 0 SV *e = sv_2mortal(newSVpvf("Fetch: resolve %s:%s: %s",
1337 host, port, gai_strerror(gai)));
1338 0 hmf_settle(aTHX_ future, HMF_FAILED, &e, 1);
1339 0 return future;
1340 }
1341 0 fd = ft_conn_dial(ai);
1342 0 freeaddrinfo(ai);
1343 0 ai = NULL;
1344 0 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 0 0 0
1345 0 SV *e = sv_2mortal(newSVpvf("Fetch: connect %s:%s: %s",
1346 host, port, strerror(errno)));
1347 0 hmf_settle(aTHX_ future, HMF_FAILED, &e, 1);
1348 0 return future;
1349 }
1350 }
1351  
1352 122 Newxz(c, 1, ft_conn);
1353 122 c->loop = loop;
1354 122 ▶ c->loop_sv = loop_sv ? SvREFCNT_inc(loop_sv) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
c->loop_sv = loop_sv ? SvREFCNT_inc(loop_sv) : NULL; 0 122 122
1355 122 ▶ if (pool) {
Branch: 1 branch
BranchTrueFalseTotal
if (pool) { 116 6 122
1356 116 c->pool = pool;
1357 116 Newx(c->poolkey, strlen(poolkey) + 1, char);
1358 116 memcpy(c->poolkey, poolkey, strlen(poolkey) + 1);
1359 }
1360 /* Either a socket mid-connect, or the resolver's pipe. The rest of the
1361 * machinery does not care which: it arms c->fd and waits. */
1362 122 ▶ c->fd = dns ? dnsfd : fd;
Branch: 1 branch
BranchTrueFalseTotal
c->fd = dns ? dnsfd : fd; 6 116 122
1363 122 c->dns = dns;
1364 122 c->state = dns ? FT_RESOLVING : FT_CONNECTING;
1365 122 c->future = SvREFCNT_inc(future);
1366 122 c->simple = ft_conn_simple_next; /* raw-hash response mode for this UA */
1367 122 c->content_len = -1;
1368 122 c->tls = tls;
1369 122 c->verify = verify;
1370 122 ▶ if (ws_key && *ws_key) {
Branch: 2 branches
BranchTrueFalseTotal
if (ws_key && *ws_key) { 1 121 122
if (ws_key && *ws_key) { 1 0 1
1371 1 size_t kl = strlen(ws_key);
1372 1 c->want_ws = 1;
1373 1 Newx(c->ws_key, kl + 1, char);
1374 1 memcpy(c->ws_key, ws_key, kl + 1);
1375 }
1376 /* host/port outlive the connect: SNI and verification need the host, and
1377 * ft_conn_retry needs both to redial a pooled socket the server closed. */
1378 122 { size_t hl = strlen(host); Newx(c->host, hl + 1, char); memcpy(c->host, host, hl + 1); }
1379 122 { size_t pl = strlen(port); Newx(c->port, pl + 1, char); memcpy(c->port, port, pl + 1); }
1380 /* structured pieces for a possible h2 upgrade - consumed only by the
1381 * nghttp2 path after ALPN, which happens on TLS. On a cleartext
1382 * connection h2 can never be negotiated, so skip storing them (the body
1383 * is already in req_bytes; the caller passes undef here). */
1384 122 ▶ if (tls) {
Branch: 1 branch
BranchTrueFalseTotal
if (tls) { 7 115 122
1385 7 c->rq_method = newSVsv(method);
1386 7 c->rq_scheme = newSVsv(scheme);
1387 7 c->rq_authority = newSVsv(authority);
1388 7 c->rq_path = newSVsv(path);
1389 7 ▶ if (SvROK(headers_av) && SvTYPE(SvRV(headers_av)) == SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(headers_av) && SvTYPE(SvRV(headers_av)) == SVt_PVAV) 7 0 7
if (SvROK(headers_av) && SvTYPE(SvRV(headers_av)) == SVt_PVAV) 7 0 7
1390 7 c->rq_headers = (AV *)SvREFCNT_inc(SvRV(headers_av));
1391 7 ▶ if (SvOK(body)) c->rq_body = newSVsv(body);
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(body)) c->rq_body = newSVsv(body); 0 7 7
1392 }
1393 122 ▶ if (on_body && SvOK(on_body) && SvROK(on_body)) c->on_body = SvREFCNT_inc(on_body);
Branch: 3 branches
BranchTrueFalseTotal
if (on_body && SvOK(on_body) && SvROK(on_body)) c->on_body = SvREFCNT_inc(on_body); 122 0 122
if (on_body && SvOK(on_body) && SvROK(on_body)) c->on_body = SvREFCNT_inc(on_body); 3 119 122
if (on_body && SvOK(on_body) && SvROK(on_body)) c->on_body = SvREFCNT_inc(on_body); 3 0 3
1394 122 ▶ if (ft_conn_on_headers_next) c->on_headers = SvREFCNT_inc(ft_conn_on_headers_next);
Branch: 1 branch
BranchTrueFalseTotal
if (ft_conn_on_headers_next) c->on_headers = SvREFCNT_inc(ft_conn_on_headers_next); 0 122 122
1395 122 Newx(c->req, req_len, char);
1396 122 memcpy(c->req, req_bytes, req_len);
1397 122 c->req_len = req_len;
1398  
1399 122 c->hm = ft_conn_hm_next; /* Hyperman-direct mode, per UA */
1400 122 c->hm_loop = ft_conn_hm_loop_next;
1401 122 ▶ if (!c->hm) /* the hm path never fires a coderef; skip building one */
Branch: 1 branch
BranchTrueFalseTotal
if (!c->hm) /* the hm path never fires a coderef; skip building one */ 122 0 122
1402 122 c->watcher = hm_closure(aTHX_ ft_conn_ready_cb, NULL, NULL, NULL, NULL,
1403 PTR2IV(c), 0);
1404 /* Resolving waits to READ the answer; connecting waits for WRITE, which
1405 * is how a non-blocking connect reports itself finished. */
1406 122 ▶ ft_arm(aTHX_ c, dns ? HM_EV_READ : HM_EV_WRITE);
Branch: 1 branch
BranchTrueFalseTotal
ft_arm(aTHX_ c, dns ? HM_EV_READ : HM_EV_WRITE); 6 116 122
1407 (void)ft_pool_peek; /* used by ft_h3_start below */
1408 /* The deadline covers the resolve as well as the request, which is the
1409 * point of putting the pipe in c->fd: a nameserver that never answers
1410 * fails with the timeout the caller asked for rather than hanging. */
1411 122 ft_conn_arm_timeout(aTHX_ c, timeout);
1412 122 return future;
1413 }
1414  
1415 /* Start a request over HTTP/3.
1416 *
1417 * The shape differs from ft_h1_start in one way that matters: a connection is
1418 * REUSED WHILE LIVE. ft_pool_peek finds an h3 connection to this origin
1419 * whether or not it is idle, and the request becomes another stream on it -
1420 * so N concurrent requests cost one handshake and one socket. Only when there
1421 * is none does this build one, and it is put in the pool immediately rather
1422 * than when it goes idle, so the requests started right after it find it.
1423 *
1424 * Returns a pending Fetch::Future, or one already failed when h3 cannot be
1425 * used at all - the caller (ft_ua.h) then has an ordinary answer to fall back
1426 * to TCP on rather than an exception. */
1427 0 static SV *ft_h3_start(pTHX_ ft_loop *loop, SV *loop_sv, ft_pool *pool,
1428 const char *host, const char *port,
1429 int verify, double timeout,
1430 SV *method, SV *scheme, SV *authority, SV *path,
1431 SV *headers_av, SV *body, SV *on_body) {
1432 0 SV *future = hmf_new(aTHX_ "Fetch::Future");
1433 char poolkey[300];
1434 0 ft_conn *c = NULL;
1435 0 struct addrinfo *ai = NULL;
1436 0 int simple = ft_conn_simple_next;
1437  
1438 if (!FT_H3_AVAILABLE) {
1439 0 SV *e = sv_2mortal(newSVpvs("Fetch: HTTP/3 not built "
1440 "(need ngtcp2 + nghttp3)"));
1441 0 hmf_settle(aTHX_ future, HMF_FAILED, &e, 1);
1442 0 return future;
1443 }
1444  
1445 snprintf(poolkey, sizeof(poolkey), "%s:1:%s:%s", FT_PROTO_H3, host, port);
1446 if (pool) c = ft_pool_peek(pool, poolkey);
1447 if (c && ft_conn_alive_h3(c)) {
1448 ft_h3_request(aTHX_ c, future, method, scheme, authority, path,
1449 headers_av, body, on_body, ft_conn_on_headers_next,
1450 simple);
1451 return future;
1452 }
1453 if (c) { /* it died since it was parked; do not hand it out again */
1454 ft_pool_remove(pool, c);
1455 c->pool = NULL;
1456 ft_conn_free(aTHX_ c);
1457 c = NULL;
1458 }
1459  
1460 Newxz(c, 1, ft_conn);
1461 c->loop = loop;
1462 c->loop_sv = loop_sv ? SvREFCNT_inc(loop_sv) : NULL;
1463 c->fd = -1;
1464 c->verify = verify;
1465 c->tls = 1; /* there is no cleartext QUIC */
1466 c->simple = simple;
1467 c->content_len = -1;
1468 c->hm = ft_conn_hm_next;
1469 c->hm_loop = ft_conn_hm_loop_next;
1470 { size_t l = strlen(host); Newx(c->host, l + 1, char); memcpy(c->host, host, l + 1); }
1471 { size_t l = strlen(port); Newx(c->port, l + 1, char); memcpy(c->port, port, l + 1); }
1472  
1473 /* Loss detection needs a real one-shot timer, and a foreign Perl loop
1474 * with only a coarse sweep has none. Refusing here is the honest answer
1475 * the caller can act on; running QUIC on a 100ms timer would look like a
1476 * network problem rather than a configuration one. */
1477 if (!ft_conn_can_pto(c)) {
1478 SV *e = sv_2mortal(newSVpvs("Fetch: HTTP/3 needs the standalone loop "
1479 "or Hyperman's C ABI - this loop has no "
1480 "timer fine enough for loss detection"));
1481 ft_conn_free(aTHX_ c);
1482 hmf_settle(aTHX_ future, HMF_FAILED, &e, 1);
1483 return future;
1484 }
1485  
1486 /* QUIC dials UDP, so the address is wanted here rather than handed to a
1487 * connect() the loop finishes. Resolution reuses ft_dns.h's inline path:
1488 * an h3 attempt follows an Alt-Svc header from a request that has already
1489 * resolved this origin, so the answer is in the system's cache. */
1490 {
1491 struct addrinfo hints;
1492 int gai;
1493 if (!ft_dns_numeric(host, port, &ai)) {
1494 memset(&hints, 0, sizeof hints);
1495 hints.ai_family = AF_UNSPEC;
1496 hints.ai_socktype = SOCK_DGRAM;
1497 gai = getaddrinfo(host, port, &hints, &ai);
1498 if (gai != 0) {
1499 SV *e = sv_2mortal(newSVpvf("Fetch: resolve %s:%s: %s",
1500 host, port, gai_strerror(gai)));
1501 ft_conn_free(aTHX_ c);
1502 hmf_settle(aTHX_ future, HMF_FAILED, &e, 1);
1503 return future;
1504 }
1505 }
1506 }
1507  
1508 if (ft_h3_connect(aTHX_ c, ai) != 0) {
1509 freeaddrinfo(ai);
1510 {
1511 SV *e = sv_2mortal(newSVpvs("Fetch: HTTP/3 connect failed"));
1512 ft_conn_free(aTHX_ c);
1513 hmf_settle(aTHX_ future, HMF_FAILED, &e, 1);
1514 }
1515 return future;
1516 }
1517 freeaddrinfo(ai);
1518  
1519 if (pool) {
1520 c->pool = pool;
1521 Newx(c->poolkey, strlen(poolkey) + 1, char);
1522 memcpy(c->poolkey, poolkey, strlen(poolkey) + 1);
1523 /* In the pool from the start, not when it goes idle: the requests
1524 * issued in the same tick have to find it, or each opens its own
1525 * connection and the multiplexing never happens. */
1526 ft_pool_put(aTHX_ pool, c);
1527 }
1528 if (!c->hm)
1529 c->watcher = hm_closure(aTHX_ ft_conn_ready_cb, NULL, NULL, NULL, NULL,
1530 PTR2IV(c), 0);
1531 ft_h3_request(aTHX_ c, future, method, scheme, authority, path,
1532 headers_av, body, on_body, ft_conn_on_headers_next, simple);
1533 /* The handshake's first flight, and then wait for the peer. */
1534 ft_h3_step(aTHX_ c);
1535 /* The deadline is the CONNECTION's here, not the request's: several
1536 * requests share it and the last one to finish is what it has to cover.
1537 * A per-request deadline over a shared connection would kill the
1538 * connection out from under everybody else's request. */
1539 (void)timeout;
1540 return future;
1541 }
1542  
1543 #endif /* FT_HTTP_H */
« include/fetch/ft_cookiejar.h ↑ Summary Fetch.xs »