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