include/hyperman/hm_core.h

filter: Statementstmt 91.6% 3025 / 3391 (103 uncoverable) Branchbran 78.1% 2236 / 3430 (515 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.8% 5261 / 6821 (618 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/hyperman/hm_future.h ↑ Summary include/hyperman/hm_compress.h »
1 #ifndef HM_CORE_H
2 #define HM_CORE_H
3  
4 /* Hyperman loop core: connections, HTTP parse and
5 * serialize, PSGI dispatch (sync, streaming, Future), watchers, deferred
6 * queue, timeouts/fairness, listener, workers, and the prefork supervisor.
7 * Requires hm_future.h (futures + C closures) and hyperman.h (backends). */
8  
9 #define HM_POOL_MAX 1024 /* pooled hm_conn structs kept per loop */
10 #define HM_IOV_MAX 64 /* writev: header + up to 63 body chunks */
11 #define HM_SWEEP_SECS 5.0 /* timeout sweep interval */
12 #define HM_HIDX_STACK 64 /* header-line index on the stack before
13 falling back to the loop's growable one */
14 #define HM_WB_HIWAT (1 << 20) /* queued response bytes a connection may
15 hold before hm_process stops serving
16 its pipeline until the client reads */
17 #define HM_WB_KEEP (1 << 20) /* a pooled connection keeps a write
18 buffer up to this; larger ones are
19 freed, so one big response does not
20 pin its buffer for the pool's life */
21  
22 #include "hm_win.h" /* the socket/IO shim: hm_os_*, hm_iovec, hm_memmem.
23 First, so every later header sees the platform
24 headers it pulls; a passthrough on POSIX. */
25 #include "hm_parse.h" /* hm_hline, hm_parse_index, hm_chunked_* */
26 #include "hm_proxy.h" /* the PROXY protocol header, v1 and v2 */
27 #ifndef _WIN32
28 #include <pwd.h> /* user => 'name' (hm_resolve_drop) */
29 #include <grp.h> /* group => 'name' */
30 #include <netdb.h> /* getaddrinfo (hm_resolve) */
31 #include <sys/un.h> /* path => listeners */
32 #include <sys/stat.h>
33 #endif
34 #include "hm_abi.h" /* public C ABI: hm_abi table + callback typedefs */
35  
36 #if defined(HM_HAVE_SENDFILE_LINUX)
37 #include <sys/sendfile.h>
38 #elif defined(HM_HAVE_SENDFILE_FREEBSD) || defined(HM_HAVE_SENDFILE_MACOS)
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <sys/uio.h>
42 #endif
43  
44 typedef struct hm_loop hm_loop;
45  
46 /* A streaming response body (filehandle / IO-object PSGI bodies): instead
47 * of slurping the whole file into one SV, the connection drains its write
48 * buffer and refills it a chunk at a time - or hands the file straight to
49 * sendfile(2) on a plaintext socket. `sv` keeps the body (and so its fd)
50 * alive; kind 1 reads the fd at an explicit offset (pread), kind 2 pulls
51 * a getline object. At most HM_BSRC_CHUNK response-body bytes are ever
52 * buffered per connection. */
53 #define HM_BSRC_CHUNK 65536
54 #define HM_SENDFILE_MAX (1 << 20) /* bytes per sendfile(2) call */
55 typedef struct {
56 int kind; /* 0 none, 1 regular-file fd, 2 getline */
57 int fd; /* kind 1: borrowed from the body's handle */
58 off_t off; /* kind 1: next byte to read */
59 UV remaining; /* bytes still owed to the client */
60 SV *sv; /* the body, owned: keeps the fd alive */
61 unsigned char nosf; /* sendfile(2) refused; use the read path */
62 } hm_bsrc;
63 typedef struct hm_conn hm_conn;
64  
65 /* A bound listening socket. The loop holds an array of these; a connection
66 * remembers the one it was accepted on so TLS-wrap, SERVER_PORT, and the
67 * built-in https redirect are all per-listener. */
68 typedef struct {
69 int fd; /* bound listener fd, -1 until bound */
70 char *host; /* bind host (owned) */
71 int port;
72 void *tls_ctx; /* SSL_CTX*; NULL = plain */
73 int http2; /* per-listener h2 (h2c / ALPN) */
74 int http3; /* per-listener h3: a QUIC listener on the
75 * same host and port, over UDP. Intrinsically
76 * the same origin as the TLS listener it sits
77 * on - same certificate, same SNI registry -
78 * which is why it is a flag here rather than
79 * a listener of its own (hm_quic.h) */
80 int redirect_https; /* 0 = off; else target https port for 301 */
81  
82 /* What the context above was built from, kept so a worker can build
83 * another one. The spec these are copied from lives in the parent's
84 * cfg, which a worker does not hold - and without them tls_reload
85 * would have nothing to rebuild with. Owned copies: the spec's
86 * strings belong to the parent's config and outlive nothing here. */
87 char *tls_cert; /* NULL on a plain listener */
88 char *tls_key;
89 char *tls_ca; /* NULL unless client certs are verified */
90 int tls_verify;
91 SV *tls_sni; /* the SNI map run() was given, or NULL: a
92 * tls_reload with no map rebuilds from it */
93  
94 /* The QUIC half, when http3 is on. It rides on this listener rather
95 * than being one of its own because h3 is intrinsically the same origin
96 * as the TLS listener it advertises from: same host, same port number,
97 * same certificate, same SNI registry - only the transport differs. */
98 int udp_fd; /* bound SOCK_DGRAM fd, or -1 */
99 void *quic; /* hm_quic_srv *, or NULL */
100 void *quic_ctx; /* the QUIC-side SSL_CTX */
101 char *altsvc; /* precomputed Alt-Svc value, or NULL */
102 int starved; /* accept hit EMFILE: read interest is off
103 * until a short timer puts it back */
104 int is_unix; /* a path listener: no peer address, no
105 * TCP options, SERVER_PORT 0 */
106 int proxy_protocol; /* a connection starts with a PROXY header
107 * (hm_proxy_readable), before any TLS */
108 } hm_listener;
109  
110 struct hm_conn {
111 int fd;
112 char *rbuf; /* owned; grows for large bodies, capacity kept */
113 size_t rlen, rcap;
114 char *wbuf; /* owned; capacity kept across requests */
115 size_t wlen, woff, wcap;
116 int writing; /* write watcher armed? */
117 int keepalive;
118 int awaiting; /* parked on a pending Future? */
119 UV id; /* generation id, guards fd reuse */
120 UV nreqs; /* requests served on this connection */
121 SV *io_sv; /* psgix.io: dup'd filehandle, per conn */
122 SV *cid_sv; /* psgix.hyperman.conn: [fd, id], per conn */
123 SV *peer_sv; /* cached env values, constant for the */
124 SV *peer_host_sv; /* life of the connection: REMOTE_ADDR, */
125 SV *peer_port_sv; /* REMOTE_HOST, REMOTE_PORT, */
126 SV *sname_sv; /* SERVER_NAME, */
127 SV *sport_sv; /* SERVER_PORT */
128 SV *resp_f; /* pending response Future (awaiting) */
129 SV *env_sv; /* held while parked, for access_log */
130 void *h2; /* hm_h2_sess* when in HTTP/2 mode */
131 void *ssl; /* SSL* when this is a TLS connection */
132 const char *tls_proto; /* SSL_PROTOCOL / SSL_CIPHER for $env. */
133 const char *tls_cipher; /* OpenSSL-owned, valid for the conn */
134 void *tls_peer; /* hm_tls_peer*, only if a client cert */
135 unsigned char tls_hs; /* TLS handshake in progress */
136 unsigned char tls_r_wants_w; /* SSL_read blocked wanting write */
137 unsigned char tls_failed; /* SSL_new failed at accept: closed */
138 unsigned char stream_open; /* a streamed response has begun and
139 * not closed: one per request */
140 UV body_rx; /* body bytes since body_phase began */
141 time_t body_t0; /* when body_phase began */
142 unsigned char tls_w_wants_r; /* SSL_write blocked wanting read */
143 unsigned char ktls_tx; /* TX offloaded to the kernel (reporting
144 * only - the write paths ask OpenSSL
145 * itself, see hm_ktls_tx) */
146 unsigned char recv_fallback; /* this connection reads off readiness for
147 * good: completion was unavailable or
148 * refused once, and a persistent watcher
149 * is armed */
150 unsigned char recv_inflight; /* a completion-mode recv is outstanding
151 * into c->rbuf; the kernel may write to
152 * it at any moment, so it must not be
153 * grown, freed or reused until the
154 * completion lands */
155 unsigned char recv_lent; /* reading off a lent readiness watcher
156 * while requests wait unconsumed in
157 * rbuf (hm_recv_unsafe) */
158 unsigned char again_queued; /* hm_again_push has it for the top of
159 * the next turn (hm_run_again) */
160 unsigned char detached; /* app took the socket (see hm_detach) */
161 unsigned char tunnel; /* a 101 stream handle owns the bytes both
162 * ways (hm_stream_open): what the peer
163 * sends goes to the handle's read half,
164 * never to the request parser */
165 unsigned char accepts_gzip; /* this request's Accept-Encoding */
166 unsigned char te_trailers; /* the request's TE listed "trailers", so
167 * a chunked body may end with trailer
168 * fields (hm_stream_finish). A client
169 * that did not ask is allowed to discard
170 * them and may not even parse them, so
171 * without this bit they are dropped */
172 unsigned char cont_sent; /* a 100 Continue has gone out for the
173 * request being read. Per request, and
174 * on the connection rather than on the
175 * stack, because the headers stay in rbuf
176 * and hm_process re-reads them on every
177 * readable event until the body is whole */
178 unsigned char is_head; /* the request being answered is a HEAD:
179 * the headers go out, the body does not */
180 unsigned char in_app; /* a synchronous handler for this
181 * connection is on the C stack. A ->get
182 * inside it pumps this same loop, so a
183 * close asked for meanwhile is deferred
184 * to the frame that called the app */
185 unsigned char close_deferred; /* hm_close was asked for while in_app */
186 unsigned char read_paused; /* read interest dropped while in_app with
187 * a full buffer (hm_read_pause) */
188 unsigned char pp_wait; /* awaiting the PROXY header: nothing
189 * else reads this socket until it is
190 * parsed (hm_proxy_readable) */
191 char proxy_addr[INET6_ADDRSTRLEN]; /* the proxy that connected, once
192 * the header named the real client */
193 int proxy_port;
194 unsigned char backlogged; /* hm_process stopped on a full write
195 * buffer; the drain re-queues it */
196 unsigned char req_11; /* the request on the plate is HTTP/1.1 */
197 unsigned char stream_mode; /* a streamed body's framing (HM_SM_*) */
198 unsigned char stream_done; /* a streamed body ended with keep-alive
199 * on: the drain serves the pipeline */
200 UV stream_serial; /* streamed responses begun on this conn; a
201 * Writer carries the one it belongs to, so a
202 * writer kept past its response cannot end
203 * the next one */
204 UV stream_left; /* HM_SM_LENGTH: body octets still owed */
205 size_t chunk_pos; /* chunked body: octets validated so far, so
206 * a re-scan starts at the last chunk boundary
207 * rather than at the first chunk */
208 size_t chunk_dec; /* decoded octets up to chunk_pos */
209 hm_bsrc bsrc; /* streaming body being dripped, if any */
210 int last_status; /* last response status/bytes (logging) */
211 size_t last_blen;
212 char peer[INET6_ADDRSTRLEN]; /* REMOTE_ADDR, filled at accept */
213 int peer_port; /* REMOTE_PORT, filled at accept */
214 time_t last_active;
215 time_t req_start; /* first byte of an incomplete request */
216 time_t last_rx; /* last read that delivered bytes: the body
217 * phase is timed from here, not from the
218 * request's first byte */
219 unsigned char body_phase; /* headers complete, body still arriving:
220 * hm_sweep applies body_timeout */
221 /* A body being drained to a temp file rather than accumulated in rbuf.
222 * -1 when not spilling. The fd becomes psgi.input and the handle owns it
223 * from that moment; the file is unlinked at creation, so nothing is left
224 * behind however the request ends. */
225 int spill_fd;
226 long spill_got; /* body octets already written to it */
227 hm_conn *lru_prev, *lru_next;
228 hm_conn *pool_next;
229 hm_loop *loop;
230 hm_listener *lst; /* listener this conn was accepted on */
231 };
232  
233 /* QUIC's expiry min-heap. hm_quic.h owns every operation on it; the type is
234 * here because hm_loop carries one BY VALUE, so the "has QUIC anything to do"
235 * test on the loop's hot path is a load and a compare rather than a
236 * dereference. ngtcp2 rearms a connection's expiry after essentially every
237 * packet, and none of the backend timer vtables can carry that - see the
238 * header comment in hm_quic.h for the costing. */
239 typedef struct hm_qconn hm_qconn;
240 typedef struct { hm_qconn **a; int n, cap; } hm_qheap;
241  
242 /* app-owned fd watcher: a one-shot Future, a persistent Perl callback, or a
243 * persistent C callback (the ABI path - dispatched with no Perl frame) */
244 typedef struct {
245 SV *sv; /* future or Perl cb; NULL for a C watcher */
246 unsigned char is_cb;
247 hm_abi_io_cb c_cb; /* C watcher; checked before sv */
248 void *c_ud;
249 } hm_iow;
250  
251 /* timer watcher */
252 #define HM_TW_FUTURE 0
253 #define HM_TW_CB 1
254 #define HM_TW_SWEEP 2
255 #define HM_TW_HARDSTOP 3
256 #define HM_TW_C 4 /* ABI: C callback, cancellable via handle */
257 typedef struct hm_tw_s {
258 int kind;
259 SV *sv;
260 hm_loop *loop;
261 hm_abi_timer_cb c_cb; /* HM_TW_C only */
262 void *c_ud;
263 double due; /* oneshot deadline, monotonic seconds; 0 = none */
264 /* Cancelled, and waiting on the loop's graveyard to be freed. The
265 * backends fill a whole event array before any of it is dispatched, so
266 * a timer cancelled from an earlier event in the batch may still be
267 * named by a later one; freeing it at once was a use after free. */
268 unsigned char dead;
269 struct hm_tw_s *gy_next;
270 } hm_tw;
271  
272 /* The deadline a oneshot is armed for, on the clock hm_dispatch checks it
273 * against. The backend's own timer is a wake-up hint, not the deadline:
274 * kqueue's EVFILT_TIMER on NetBSD is a callout in scheduler ticks, and in a
275 * VM those ticks run ahead of CLOCK_MONOTONIC. */
276 #define hm_tw_due(secs) ((double)hm_now_ns() / 1e9 + (secs))
277  
278 typedef struct { int fd; UV id; } hm_again;
279  
280 /* The events one hm_loop_run frame took from the backend and has not yet
281 * dispatched. A nested run (a handler's ->get) sits inside one of them, and
282 * the rest are already out of the kernel: a oneshot timer is never reported
283 * again, so a nested wait for the event next to it waited forever. Each
284 * frame links its batch here, and a nested one dispatches what its callers
285 * still hold before it waits. */
286 typedef struct hm_batch_s {
287 hm_event *evs;
288 int n, i; /* i: the next one to dispatch */
289 struct hm_batch_s *up; /* the batch of the frame that called us */
290 } hm_batch;
291  
292 /* One piece of an access-log line. The format string is compiled to these
293 * once, in the parent, so writing a line is a walk over an array and never a
294 * parse: a literal run, an $env value (the key SV carries its hash), or one
295 * of the computed fields. */
296 #define HM_LT_LIT 0 /* literal bytes */
297 #define HM_LT_ENV 1 /* $env->{key}, escaped; "-" when absent */
298 #define HM_LT_DASH 2 /* %l: always "-" */
299 #define HM_LT_STATUS 3 /* %s, %>s */
300 #define HM_LT_BYTES 4 /* %b: body bytes, "-" when there were none */
301 #define HM_LT_BYTES0 5 /* %B: body bytes, "0" when there were none */
302 #define HM_LT_TIME 6 /* %t: [dd/Mon/YYYY:HH:MM:SS +ZZZZ] */
303 #define HM_LT_TIME_ISO 7 /* json: YYYY-MM-DDTHH:MM:SS+ZZZZ */
304 #define HM_LT_REQLINE 8 /* %r: METHOD URI PROTO */
305 #define HM_LT_QUERY 9 /* %q: ?query, or nothing */
306 #define HM_LT_PID 10 /* %P */
307 #define HM_LT_DUR_US 11 /* %D: microseconds since the request was built */
308 #define HM_LT_DUR_S 12 /* %T: whole seconds of the same */
309 typedef struct {
310 int kind;
311 char *lit; /* HM_LT_LIT: owned */
312 size_t litlen;
313 SV *key; /* HM_LT_ENV: a shared-string SV, hash precomputed */
314 } hm_logtok;
315  
316 struct hm_loop {
317 int pid; /* the process that created this loop
318 (hm_os_getpid: MSVC has no pid_t) */
319 hm_backend *be;
320 hm_conn **conns; /* [HM_MAXFD] */
321 hm_iow *io_r, *io_w; /* [HM_MAXFD] app io watchers */
322 AV *deferred; /* run-soon callbacks */
323 hm_again *again; /* conns with buffered requests */
324 int again_n, again_cap;
325 hm_hline *hidx; /* header-line index overflow buffer */
326 int hidx_cap;
327 hm_conn *lru_head, *lru_tail;
328 hm_conn *pool; int pool_n;
329 /* Completion-mode receive (io_uring only, opt-in). `completion` is set
330 * once at loop start when the backend can do it and the operator asked.
331 * `quar` holds read buffers belonging to connections that closed with a
332 * recv still outstanding - see hm_quar_push. */
333 int completion;
334 struct hm_quar *quar;
335 hm_tw *tw_gy; /* cancelled timers, freed between batches */
336 int starved_retry; /* a listener waits for hm_accept_unstarve */
337 hm_tw *sweep_tw;
338 SV *app; /* server: PSGI app coderef */
339 hm_listener *listeners; /* server: bound listeners */
340 int nlisteners;
341 int attached; /* server attached (>=1 listener) */
342 int nconns;
343 int stop;
344 int stopping; /* graceful shutdown in progress */
345 int running;
346 hm_batch *batch; /* the innermost run's (see hm_batch) */
347 int perl_owned; /* created via Hyperman::Loop->new */
348 time_t now;
349 double idle_timeout;
350 double header_timeout;
351 double body_timeout; /* a body stalled this long is closed */
352 double request_timeout; /* whole request ceiling; 0 = none */
353 UV body_min_rate; /* bytes/s floor over a body; 0 = none */
354 int max_pipeline; /* fairness: requests per conn per wakeup */
355 size_t max_read; /* request size ceiling (headers + body) */
356 int compress; /* gzip responses (0 = off, the default) */
357 size_t compress_min; /* below this, gzip buys nothing */
358 int compress_level; /* gzip level; HZ_LEVEL by default */
359 SV *compress_types; /* AV of extra compressible media types */
360 SV *self_sv; /* cached psgix.loop wrapper */
361 SV *log_cb; /* access_log coderef, or NULL */
362 int log_fd; /* fast access-log fd (C writer), or -1 */
363 char *log_buf; /* pending Combined-format log lines */
364 size_t log_len, log_cap;
365 time_t log_ts_sec; /* second the cached stamp below is for */
366 char log_ts[32]; /* "dd/Mon/YYYY:HH:MM:SS +ZZZZ", 1Hz */
367 time_t date_sec; /* second the Date header below is for */
368 char date_hdr[40]; /* "Date: <IMF-fixdate>\r\n", 1Hz */
369 hm_logtok *logtok; /* the compiled access_log_format */
370 int nlogtok;
371 int log_json; /* values escaped for JSON, not CLF */
372 int log_wants_t0; /* the format reads %D/%T: stamp requests */
373 time_t log_iso_sec; /* second the ISO stamp below is for */
374 char log_iso[40]; /* "YYYY-MM-DDTHH:MM:SS+ZZZZ", 1Hz */
375 UV requests; /* requests dispatched (stats) */
376 UV accepts; /* connections accepted (stats) */
377 UV denied; /* connections dropped at accept, denylist */
378 UV bytes_out; /* response bytes queued (stats) */
379 UV datagrams; /* QUIC datagrams taken off the wire */
380 UV h3_conns; /* QUIC handshakes completed */
381 UV h3_live; /* QUIC connections open now */
382 UV h3_requests; /* requests served over HTTP/3 */
383 UV datagrams_out; /* QUIC datagrams sent (one sendto
384 * each: no GSO anywhere yet) */
385 UV http3_max_conns; /* ceiling on h3_live, 0 = unlimited */
386 hm_qheap qheap; /* QUIC expiry deadlines (hm_quic.h) */
387 UV max_requests; /* recycle worker after N (0=off) */
388 int recycle_pending; /* max_requests hit; drain at loop top */
389 double shutdown_grace; /* graceful-drain bound (secs) */
390 hm_tw *hardstop_tw;
391 };
392  
393 /* One served request, counted, and max_requests tested - for every protocol
394 * that finishes one. The bound belongs to the worker and not to HTTP/1: a
395 * site whose browsers all negotiate h2 would otherwise never reach it, and
396 * the worker it was meant to retire grows for as long as the box lets it.
397 *
398 * Not on the 101 paths (h2c upgrade, detach, tunnel), which count their
399 * request and deliberately do not test: the socket there has just left this
400 * loop for an application that means to hold it open, and a drain decided by
401 * the request that handed it over is the one moment it must not happen.
402 *
403 * A function and not a macro because the argument is read five times. */
404 874 static void hm_count_request(hm_loop *loop) {
405 874 loop->requests++;
406 /*
407 # uncoverable condition right count:3 class:ignore_covered_err
408 # uncoverable condition right count:4 class:ignore_covered_err */
409 874 ▶ if (loop->max_requests && loop->requests >= loop->max_requests
Branch: 2 branches
BranchTrueFalseTotal
if (loop->max_requests && loop->requests >= loop->max_requests 82 792 874
if (loop->max_requests && loop->requests >= loop->max_requests 8 74 82
410 8 ▶ && !loop->stopping && loop->attached)
Branch: 2 branches
BranchTrueFalseTotal
&& !loop->stopping && loop->attached) 8 0 8
&& !loop->stopping && loop->attached) 8 0 8
411 8 loop->recycle_pending = 1; /* drain at the loop iteration boundary */
412 874 }
413  
414 /* Does a Hyperman::Writer's tag slot hold this string? The four-element
415 * writer shapes (h2 and h3) have the same arity, so they are told apart by
416 * the tag and never by length - a length test routes an HTTP/3 body into an
417 * nghttp2 session. Used from xs/writer.xs. */
418 121 static int hm_writer_tag_is(pTHX_ AV *w, const char *want, STRLEN wantlen) {
419 121 SV **t = av_fetch(w, 3, 0);
420 STRLEN l;
421 const char *p;
422 /*
423 # uncoverable branch true class:ignore_covered_err
424 # uncoverable condition left count:1 class:ignore_covered_err
425 # uncoverable condition left count:2 class:ignore_covered_err
426 # uncoverable condition left count:3 class:ignore_covered_err */
427 121 ▶ if (!t || !*t || !SvOK(*t)) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!t || !*t || !SvOK(*t)) return 0; 121 0 121
if (!t || !*t || !SvOK(*t)) return 0; 121 0 121
if (!t || !*t || !SvOK(*t)) return 0; 0 121 121
428 121 p = SvPV(*t, l);
429 121 ▶ return l == wantlen && memcmp(p, want, l) == 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
return l == wantlen && memcmp(p, want, l) == 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 121 0 121
return l == wantlen && memcmp(p, want, l) == 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 89 32 121
430 }
431  
432 /* Find the listener owning a ready fd (listener count is tiny). */
433 32596 static hm_listener *hm_listener_for_fd(hm_loop *loop, int fd) {
434 int i;
435 33985 ▶ for (i = 0; i < loop->nlisteners; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < loop->nlisteners; i++) 32767 1218 33985
436 32767 ▶ if (loop->listeners[i].fd == fd) return &loop->listeners[i];
Branch: 1 branch
BranchTrueFalseTotal
if (loop->listeners[i].fd == fd) return &loop->listeners[i]; 31378 1389 32767
437 1218 return NULL;
438 }
439  
440 /* The same, for the QUIC side. Separate rather than a flag on the above,
441 * because a UDP listener fd is never in loop->conns[] and the caller wants
442 * to know which of the two it has. */
443 1218 static hm_listener *hm_listener_for_udp(hm_loop *loop, int fd) {
444 int i;
445 2592 ▶ for (i = 0; i < loop->nlisteners; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < loop->nlisteners; i++) 1374 1218 2592
446 /*
447 # uncoverable branch true class:ignore_covered_err
448 # uncoverable condition left count:1 class:ignore_covered_err
449 # uncoverable condition left count:2 class:ignore_covered_err
450 # uncoverable condition right count:2 class:ignore_covered_err */
451 1374 ▶ if (loop->listeners[i].udp_fd >= 0 && loop->listeners[i].udp_fd == fd)
Branch: 2 branches
BranchTrueFalseTotal
if (loop->listeners[i].udp_fd >= 0 && loop->listeners[i].udp_fd == fd) 0 1374 1374
if (loop->listeners[i].udp_fd >= 0 && loop->listeners[i].udp_fd == fd) 0 0 0
452 0 return &loop->listeners[i]; /* # uncoverable statement class:ignore_covered_err */
453 1218 return NULL;
454 }
455  
456 static SV *hm_empty_input; /* shared empty psgi.input */
457 static SV *hm_empty_input_sv(pTHX); /* ... checked still open */
458 #ifndef _WIN32
459 static pid_t hm_children[1024]; /* the supervisor's pool (POSIX only) */
460 static time_t hm_child_start[1024];
461 /* The WORKER INDEX each of those was forked with, which is not its position
462 * here: this array is kept packed by swapping the last entry down over a
463 * departed one, so a position means nothing beyond "somebody lives here". The
464 * index is an identity - it picks the worker's waker slot and its CPU - so it
465 * has to travel with the child rather than be recomputed from where the child
466 * happens to sit. */
467 static int hm_child_widx[1024];
468 static int hm_nchildren = 0;
469 #endif
470 static UV hm_id_counter = 0;
471  
472 static SV *hm_loop_to_sv(pTHX_ hm_loop *loop);
473 static void hm_on_signal(pTHX_ hm_loop *loop);
474 static void hm_deliver(pTHX_ int fd, UV id, SV *resp);
475 static int hm_flush(pTHX_ hm_conn *c);
476 static void hm_process(pTHX_ hm_conn *c);
477 static int hm_queue_response(pTHX_ hm_conn *c, SV *resp);
478 static void hm_close(pTHX_ hm_loop *loop, hm_conn *c);
479 static void hm_wb_reserve(hm_conn *c, size_t need);
480 static void hm_wb_put(hm_conn *c, const char *p, size_t n);
481 static void hm_again_push(hm_loop *loop, hm_conn *c);
482 static void hm_tw_gy_sweep(hm_loop *loop);
483 static void hm_h2_free(pTHX_ void *h2); /* defined in hm_http2.h */
484 static void hm_readable(pTHX_ hm_conn *c);
485 static void hm_read_resume(hm_loop *loop, hm_conn *c);
486  
487 /* ABI v6 stream handles, defined in hm_stream.h (included below, after the
488 * HTTP/1 and HTTP/2 halves it sits on top of). The two counters are read on
489 * the close and flush paths rather than called into, because they are zero
490 * for every connection that never opened a handle - which is nearly all of
491 * them - and a list walk per flush is not free. */
492 static int hm_stream_live_n = 0;
493 static int hm_stream_paused_n = 0;
494 static void hm_quic_readable(pTHX_ hm_loop *loop, hm_listener *lst); /* hm_quic.h */
495 static void hm_quic_expire(pTHX_ hm_loop *loop); /* hm_quic.h */
496 static void hm_quic_srv_attach(pTHX_ hm_loop *loop, hm_listener *lst, int widx);
497 static void hm_quic_srv_free(pTHX_ hm_listener *lst);
498 static void hm_quic_shutdown(pTHX_ hm_loop *loop);
499 static void hm_quic_ctx_swap(pTHX_ hm_listener *lst, void *fresh);
500 static uint64_t hm_quic_next_deadline(hm_loop *loop);
501 static void hm_quic_secret_init(void);
502 static void hm_stream_conn_gone(pTHX_ int fd, UV id);
503 static int hm_stream_deliver(pTHX_ int fd, UV cid, int64_t sid,
504 const char *buf, STRLEN len, int fin);
505 static void hm_stream_h2_gone(pTHX_ int fd, UV id, int64_t sid);
506 static void hm_stream_drained(pTHX_ hm_conn *c);
507 static int hm_h2_reentrant(hm_conn *c); /* hm_http2.h */
508  
509 #include "hm_tls.h" /* SSL_CTX, wrap, hm_cread/hm_cwrite */
510  
511 /* ---- small utilities ----------------------------------------------------- */
512  
513 18 static const char *hm_reason(int s) {
514 /*
515 # uncoverable branch true count:1
516 # uncoverable branch true count:5
517 # uncoverable branch true count:6
518 # uncoverable branch true count:7
519 # uncoverable branch true count:8
520 # uncoverable branch true count:9
521 # uncoverable branch true count:10
522 # uncoverable branch true count:11
523 # uncoverable branch true count:12
524 # uncoverable branch true count:13
525 # uncoverable branch true count:14
526 # uncoverable branch true count:15
527 # uncoverable branch true count:17 */
528 18 switch (s) {
529 10 case 100: return "Continue"; case 101: return "Switching Protocols";
530 2 case 102: return "Processing"; case 103: return "Early Hints";
531 0 case 200: return "OK"; case 201: return "Created"; /* # uncoverable statement */
532 0 case 204: return "No Content"; case 301: return "Moved Permanently"; /* # uncoverable statement */
533 0 case 302: return "Found"; case 304: return "Not Modified"; /* # uncoverable statement */
534 0 case 400: return "Bad Request"; case 401: return "Unauthorized"; /* # uncoverable statement */
535 0 case 403: return "Forbidden"; case 404: return "Not Found"; /* # uncoverable statement */
536 0 case 405: return "Method Not Allowed"; /* # uncoverable statement */
537 1 case 426: return "Upgrade Required";
538 0 case 500: return "Internal Server Error"; /* # uncoverable statement */
539 5 default: return "OK";
540 }
541 }
542  
543 /* Tiny decimal formatters: snprintf's format parsing and locale machinery
544 * are measurable at hot-path request rates. Return bytes written. */
545 775 static size_t hm_utoa(char *out, unsigned long v) {
546 char tmp[20];
547 775 size_t n = 0, i;
548 1362 ▶ do { tmp[n++] = (char)('0' + v % 10); v /= 10; } while (v);
Branch: 1 branch
BranchTrueFalseTotal
do { tmp[n++] = (char)('0' + v % 10); v /= 10; } while (v); 587 775 1362
549 2137 ▶ for (i = 0; i < n; i++) out[i] = tmp[n - 1 - i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) out[i] = tmp[n - 1 - i]; 1362 775 2137
550 775 return n;
551 }
552 102 static size_t hm_itoa(char *out, long v) {
553 /*
554 # uncoverable branch true
555 # uncoverable condition left count:1 class:ignore_covered_err */
556 102 ▶ if (v < 0) { out[0] = '-'; return 1 + hm_utoa(out + 1, 0UL - (unsigned long)v); }
Branch: 1 branch
BranchTrueFalseTotal
if (v < 0) { out[0] = '-'; return 1 + hm_utoa(out + 1, 0UL - (unsigned long)v); } 0 102 102
557 102 return hm_utoa(out, (unsigned long)v);
558 }
559  
560 /* Status line for the response: precomputed for the common statuses, built
561 * into buf (>= 64 bytes) otherwise. Sets *len, returns the line. */
562 873 static const char *hm_status_line(int s, size_t *len, char *buf) {
563 #define HM_SL(str) do { *len = sizeof(str) - 1; return str; } while (0)
564 873 switch (s) {
565 833 case 200: HM_SL("HTTP/1.1 200 OK\r\n");
566 1 case 201: HM_SL("HTTP/1.1 201 Created\r\n");
567 4 case 204: HM_SL("HTTP/1.1 204 No Content\r\n");
568 3 case 301: HM_SL("HTTP/1.1 301 Moved Permanently\r\n");
569 1 case 302: HM_SL("HTTP/1.1 302 Found\r\n");
570 3 case 304: HM_SL("HTTP/1.1 304 Not Modified\r\n");
571 1 case 400: HM_SL("HTTP/1.1 400 Bad Request\r\n");
572 1 case 401: HM_SL("HTTP/1.1 401 Unauthorized\r\n");
573 1 case 403: HM_SL("HTTP/1.1 403 Forbidden\r\n");
574 2 case 404: HM_SL("HTTP/1.1 404 Not Found\r\n");
575 1 case 405: HM_SL("HTTP/1.1 405 Method Not Allowed\r\n");
576 4 case 500: HM_SL("HTTP/1.1 500 Internal Server Error\r\n");
577 }
578 #undef HM_SL
579 {
580 18 const char *r = hm_reason(s);
581 18 size_t rl = strlen(r), n = 9;
582 18 memcpy(buf, "HTTP/1.1 ", 9);
583 18 n += hm_itoa(buf + n, (long)s);
584 18 buf[n++] = ' ';
585 18 memcpy(buf + n, r, rl); n += rl;
586 18 buf[n++] = '\r'; buf[n++] = '\n';
587 18 *len = n;
588 18 return buf;
589 }
590 }
591  
592 /* Format a peer sockaddr into a printable REMOTE_ADDR string and its port ("" /
593 * 0 on failure or for address families we don't serve). */
594 939 static void hm_fmt_peer(char *out, size_t outlen, int *port, const struct sockaddr *sa) {
595 939 out[0] = '\0';
596 /*
597 # uncoverable branch false class:ignore_covered_err
598 # uncoverable condition right count:1 class:ignore_covered_err */
599 939 ▶ if (port) *port = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (port) *port = 0; 939 0 939
600 /*
601 # uncoverable branch true
602 # uncoverable condition left count:1 class:ignore_covered_err */
603 939 ▶ if (!sa) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!sa) return; 0 939 939
604 939 ▶ if (sa->sa_family == AF_INET) {
Branch: 1 branch
BranchTrueFalseTotal
if (sa->sa_family == AF_INET) { 937 2 939
605 937 const struct sockaddr_in *s4 = (const struct sockaddr_in *)sa;
606 937 inet_ntop(AF_INET, &s4->sin_addr, out, (socklen_t)outlen);
607 /*
608 # uncoverable branch false class:ignore_covered_err
609 # uncoverable condition right count:1 class:ignore_covered_err */
610 937 ▶ if (port) *port = ntohs(s4->sin_port);
Branch: 1 branch
BranchTrueFalseTotal
if (port) *port = ntohs(s4->sin_port); 937 0 937
611 /*
612 # uncoverable branch true class:ignore_covered_err
613 # uncoverable condition left count:1 class:ignore_covered_err */
614 2 ▶ } else if (sa->sa_family == AF_INET6) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (sa->sa_family == AF_INET6) { 0 2 2
615 0 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *)sa; /* # uncoverable statement class:ignore_covered_err */
616 /* An IPv4 client of a dual-stack listener arrives as ::ffff:a.b.c.d.
617 * Formatted as the IPv4 it is: the denylist and the rate limiter
618 * key on the string, and "1.2.3.4" must be one client whichever
619 * listener it came through. */
620 /*
621 # uncoverable statement class:ignore_covered_err
622 # uncoverable branch true class:ignore_covered_err
623 # uncoverable branch false class:ignore_covered_err
624 # uncoverable condition left count:1 class:ignore_covered_err
625 # uncoverable condition right count:1 class:ignore_covered_err */
626 0 ▶ if (IN6_IS_ADDR_V4MAPPED(&s6->sin6_addr))
Branch: 4 branches
BranchTrueFalseTotal
if (IN6_IS_ADDR_V4MAPPED(&s6->sin6_addr)) 0 0 0
if (IN6_IS_ADDR_V4MAPPED(&s6->sin6_addr)) 0 0 0
if (IN6_IS_ADDR_V4MAPPED(&s6->sin6_addr)) 0 0 0
if (IN6_IS_ADDR_V4MAPPED(&s6->sin6_addr)) 0 0 0
627 0 inet_ntop(AF_INET, ((const unsigned char *)&s6->sin6_addr) + 12, /* # uncoverable statement */
628 out, (socklen_t)outlen); /* # uncoverable statement */
629 else
630 0 inet_ntop(AF_INET6, &s6->sin6_addr, out, (socklen_t)outlen); /* # uncoverable statement class:ignore_covered_err */
631 /*
632 # uncoverable statement class:ignore_covered_err
633 # uncoverable branch true class:ignore_covered_err
634 # uncoverable branch false class:ignore_covered_err
635 # uncoverable condition left count:1 class:ignore_covered_err
636 # uncoverable condition right count:1 class:ignore_covered_err */
637 0 ▶ if (port) *port = ntohs(s6->sin6_port);
Branch: 1 branch
BranchTrueFalseTotal
if (port) *port = ntohs(s6->sin6_port); 0 0 0
638 }
639 }
640  
641 92 static void hm_set_nonblock(int fd) { hm_os_set_nonblock(fd); }
642  
643 /* fd of a filehandle / IO ref, or a plain fd number */
644 20 static int hm_fd_of(pTHX_ SV *sv) {
645 /*
646 # uncoverable branch false
647 # uncoverable condition right count:1 class:ignore_covered_err */
648 20 ▶ if (SvROK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sv)) { 20 0 20
649 20 IO *io = sv_2io(sv);
650 20 ▶ if (io && IoIFP(io)) return PerlIO_fileno(IoIFP(io)); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (io && IoIFP(io)) return PerlIO_fileno(IoIFP(io)); /* # uncoverable condition right count:1 class:ignore_covered_err */ 20 0 20
if (io && IoIFP(io)) return PerlIO_fileno(IoIFP(io)); /* # uncoverable condition right count:1 class:ignore_covered_err */ 19 1 20
651 1 croak("Hyperman: not an open filehandle");
652 }
653 0 return (int)SvIV(sv); /* # uncoverable statement */
654 }
655  
656 /* a fresh anonymous glob with an IO slot (standard idiom) */
657 933 static GV *hm_anon_glob(pTHX) {
658 933 GV *gv = newGVgen("Hyperman");
659 933 ▶ (void)GvIOn(gv);
Branch: 5 branches
BranchTrueFalseTotal
(void)GvIOn(gv); 933 0 933
(void)GvIOn(gv); 0 933 933
(void)GvIOn(gv); 0 0 0
(void)GvIOn(gv); 933 0 933
(void)GvIOn(gv); 0 933 933
660 933 SvREFCNT_inc((SV *)gv);
661 933 (void)hv_delete(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), G_DISCARD);
662 933 return gv;
663 }
664  
665 /* A read handle over an already-open descriptor, rewound. The handle owns the
666 * descriptor from this point. */
667 5 static SV *hm_input_from_fd(pTHX_ int fd) {
668 PerlIO *fp;
669 GV *gv;
670 IO *io;
671 /*
672 # uncoverable branch true
673 # uncoverable condition left count:1 class:ignore_covered_err */
674 5 ▶ if (PerlLIO_lseek(fd, 0, SEEK_SET) < 0) { PerlLIO_close(fd); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (PerlLIO_lseek(fd, 0, SEEK_SET) < 0) { PerlLIO_close(fd); return NULL; } 0 5 5
675 5 fp = PerlIO_fdopen(fd, "rb");
676 /*
677 # uncoverable branch true
678 # uncoverable condition left count:1 class:ignore_covered_err */
679 5 ▶ if (!fp) { PerlLIO_close(fd); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!fp) { PerlLIO_close(fd); return NULL; } 0 5 5
680 5 gv = hm_anon_glob(aTHX);
681 /*
682 # uncoverable branch true
683 # uncoverable condition left count:1 class:ignore_covered_err */
684 5 ▶ if (!gv) { PerlIO_close(fp); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!gv) { PerlIO_close(fp); return NULL; } 0 5 5
685 5 ▶ io = GvIOn(gv);
Branch: 5 branches
BranchTrueFalseTotal
io = GvIOn(gv); 5 0 5
io = GvIOn(gv); 0 5 5
io = GvIOn(gv); 0 0 0
io = GvIOn(gv); 5 0 5
io = GvIOn(gv); 5 0 5
686 5 IoIFP(io) = fp;
687 5 IoTYPE(io) = IoTYPE_RDONLY;
688 5 return newRV_noinc((SV *)gv);
689 }
690  
691 /* A temp file for a body being drained out of the read buffer.
692 *
693 * Unlinked the instant it exists, so the descriptor is its only reference: it
694 * goes away when the request ends, when the handler dies, or when the worker
695 * is killed, and there is no cleanup path anywhere to forget. Nothing is
696 * inherited across a fork either, which is the failure this workspace keeps
697 * meeting from the other direction. */
698 /* Where a body temp file goes. TMPDIR is the POSIX spelling of the question
699 * and TEMP the Windows one - asking only the first left Windows falling back
700 * to a "/tmp" that is not there, which is a failed mkstemp on every request
701 * carrying a body. */
702 6 static const char *hm_tmpdir(void) {
703 6 const char *d = getenv("TMPDIR");
704 /*
705 # uncoverable branch true class:ignore_covered_err
706 # uncoverable condition left count:1 class:ignore_covered_err
707 # uncoverable condition left count:2 class:ignore_covered_err
708 # uncoverable condition right count:2 class:ignore_covered_err */
709 6 ▶ if (d && *d) return d;
Branch: 2 branches
BranchTrueFalseTotal
if (d && *d) return d; 0 6 6
if (d && *d) return d; 0 0 0
710 #ifdef _WIN32
711 if ((d = getenv("TEMP")) && *d) return d;
712 if ((d = getenv("TMP")) && *d) return d;
713 return ".";
714 #else
715 6 return "/tmp";
716 #endif
717 }
718  
719 /* dir + "/hm-body-XXXXXX" into buf, or 0 if the directory will not fit. */
720 6 static int hm_body_tmppath(char *buf, size_t cap) {
721 6 const char *dir = hm_tmpdir();
722 6 size_t dl = strlen(dir);
723 /*
724 # uncoverable branch true
725 # uncoverable condition left count:1 class:ignore_covered_err */
726 6 ▶ if (dl + sizeof("/hm-body-XXXXXX") > cap) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (dl + sizeof("/hm-body-XXXXXX") > cap) return 0; 0 6 6
727 6 my_strlcpy(buf, dir, cap);
728 /*
729 # uncoverable branch false class:ignore_covered_err
730 # uncoverable condition right count:1 class:ignore_covered_err
731 # uncoverable condition right count:2 class:ignore_covered_err
732 # uncoverable condition right count:3 class:ignore_covered_err */
733 6 ▶ if (dl && dir[dl - 1] != '/' && dir[dl - 1] != '\\')
Branch: 3 branches
BranchTrueFalseTotal
if (dl && dir[dl - 1] != '/' && dir[dl - 1] != '\\') 6 0 6
if (dl && dir[dl - 1] != '/' && dir[dl - 1] != '\\') 6 0 6
if (dl && dir[dl - 1] != '/' && dir[dl - 1] != '\\') 6 0 6
734 6 my_strlcat(buf, "/", cap);
735 6 my_strlcat(buf, "hm-body-XXXXXX", cap);
736 6 return 1;
737 }
738  
739 5 static int hm_body_tmpfile(void) {
740 char path[512];
741 int fd;
742 /*
743 # uncoverable branch true
744 # uncoverable condition left count:1 class:ignore_covered_err */
745 5 ▶ if (!hm_body_tmppath(path, sizeof path)) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_body_tmppath(path, sizeof path)) return -1; 0 5 5
746 5 fd = mkstemp(path);
747 /*
748 # uncoverable branch true
749 # uncoverable condition left count:1 class:ignore_covered_err */
750 5 ▶ if (fd < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) return -1; 0 5 5
751 5 (void)unlink(path);
752 5 return fd;
753 }
754  
755 /* Above this, the body goes to a temp file instead of a second copy in
756 * memory. Below it, the copy is cheaper than an inode and three syscalls. */
757 #ifndef HM_SPILL_BODY
758 # define HM_SPILL_BODY (1024 * 1024)
759 #endif
760  
761 /* psgi.input over a temp file, for a body large enough that a second copy in
762 * memory is the wrong trade.
763 *
764 * The file is UNLINKED as soon as it is open, and the handle keeps it alive:
765 * it vanishes when the handle is closed, whether that is a normal response, a
766 * handler that died, or the worker being killed mid-request. There is no
767 * cleanup path to forget and nothing on disk for another process to find.
768 *
769 * Returns NULL to fall back to the in-memory handle, because a server that
770 * cannot make a temp file should still serve the request.
771 */
772 1 static SV *hm_new_input_file(pTHX_ const char *body, STRLEN len) {
773 char path[512];
774 int fd;
775 1 STRLEN off = 0;
776  
777 /*
778 # uncoverable branch true
779 # uncoverable condition left count:1 class:ignore_covered_err */
780 1 ▶ if (!hm_body_tmppath(path, sizeof path)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_body_tmppath(path, sizeof path)) return NULL; 0 1 1
781  
782 1 fd = mkstemp(path);
783 /*
784 # uncoverable branch true
785 # uncoverable condition left count:1 class:ignore_covered_err */
786 1 ▶ if (fd < 0) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) return NULL; 0 1 1
787 1 (void)PerlLIO_unlink(path); /* the handle is the only reference */
788  
789 2 ▶ while (off < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (off < len) { 1 1 2
790 1 SSize_t n = PerlLIO_write(fd, body + off, len - off);
791 /*
792 # uncoverable branch true
793 # uncoverable condition left count:1 class:ignore_covered_err */
794 1 ▶ if (n <= 0) { PerlLIO_close(fd); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) { PerlLIO_close(fd); return NULL; } 0 1 1
795 1 off += (STRLEN)n;
796 }
797 /*
798 # uncoverable branch true
799 # uncoverable condition left count:1 class:ignore_covered_err */
800 1 ▶ if (PerlLIO_lseek(fd, 0, SEEK_SET) < 0) { PerlLIO_close(fd); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (PerlLIO_lseek(fd, 0, SEEK_SET) < 0) { PerlLIO_close(fd); return NULL; } 0 1 1
801  
802 1 return hm_input_from_fd(aTHX_ fd);
803 }
804  
805 /* psgi.input: an in-memory read handle over the request body, via the
806 * PerlIO :scalar layer - the C equivalent of open($fh,'<',\$body).
807 *
808 * WINDOWS TAKES THE FILE AT EVERY SIZE. PerlIO_openn is declared in perlio.h
809 * but is not among the symbols perl5xx.dll exports, so the :scalar layer
810 * cannot be reached from XS there at all - it is a link error, which is what
811 * the first Windows build of this core hit. The temp file needs nothing but
812 * PerlIO_fdopen, which IS exported, and it is the path every platform
813 * already runs for a body over HM_SPILL_BODY rather than a second one
814 * written for this. The trade is an inode and three syscalls on a Windows
815 * request that carries a body. */
816 111 static SV *hm_new_input(pTHX_ const char *body, STRLEN len) {
817 #ifdef _WIN32
818 SV *f = hm_new_input_file(aTHX_ body, len);
819 return f ? f : &PL_sv_undef;
820 #else
821 static int layer_loaded = 0;
822 SV *bufsv;
823 111 ▶ if (len > HM_SPILL_BODY) {
Branch: 1 branch
BranchTrueFalseTotal
if (len > HM_SPILL_BODY) { 1 110 111
824 1 SV *f = hm_new_input_file(aTHX_ body, len);
825 /*
826 # uncoverable branch false
827 # uncoverable condition right count:1 class:ignore_covered_err */
828 1 ▶ if (f) return f;
Branch: 1 branch
BranchTrueFalseTotal
if (f) return f; 1 0 1
829 }
830 110 bufsv = newSVpvn(body, len);
831 110 SV *rv = sv_2mortal(newRV_noinc(bufsv));
832 PerlIO *fp;
833 GV *gv;
834 IO *io;
835 110 ▶ if (!layer_loaded) {
Branch: 1 branch
BranchTrueFalseTotal
if (!layer_loaded) { 83 27 110
836 83 load_module(PERL_LOADMOD_NOIMPORT, newSVpvs("PerlIO::scalar"), NULL);
837 83 layer_loaded = 1;
838 }
839 110 fp = PerlIO_openn(aTHX_ ":scalar", "r", -1, 0, 0, NULL, 1, &rv);
840 /*
841 # uncoverable branch true
842 # uncoverable condition left count:1 class:ignore_covered_err */
843 110 ▶ if (!fp) return &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
if (!fp) return &PL_sv_undef; 0 110 110
844 110 gv = hm_anon_glob(aTHX);
845 110 ▶ io = GvIO(gv);
Branch: 4 branches
BranchTrueFalseTotal
io = GvIO(gv); 110 0 110
io = GvIO(gv); 0 110 110
io = GvIO(gv); 0 0 0
io = GvIO(gv); 110 0 110
846 110 IoIFP(io) = fp;
847 110 IoTYPE(io) = IoTYPE_RDONLY;
848 110 return newRV_noinc((SV *)gv);
849 #endif
850 }
851  
852 /* The shared empty psgi.input, as every bodyless request gets it: one glob
853 * for the whole worker, because a fresh :scalar handle per request is a
854 * glob, an IO and a layer for a body that is not there. One check keeps the
855 * sharing honest: an application that closed it (or re-opened it over
856 * something else) left a handle with no input stream, and the next request
857 * would read a closed filehandle. A new one replaces it then. */
858 854 static SV *hm_empty_input_sv(pTHX) {
859 /*
860 # uncoverable branch false class:ignore_covered_err
861 # uncoverable condition right count:1 class:ignore_covered_err
862 # uncoverable condition right count:2 class:ignore_covered_err */
863 854 ▶ if (hm_empty_input && SvROK(hm_empty_input)) {
Branch: 2 branches
BranchTrueFalseTotal
if (hm_empty_input && SvROK(hm_empty_input)) { 854 0 854
if (hm_empty_input && SvROK(hm_empty_input)) { 854 0 854
864 854 SV *gv = SvRV(hm_empty_input);
865 854 ▶ IO *io = SvTYPE(gv) == SVt_PVGV ? GvIO((GV *)gv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 5 branches
BranchTrueFalseTotal
IO *io = SvTYPE(gv) == SVt_PVGV ? GvIO((GV *)gv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 854 0 854
IO *io = SvTYPE(gv) == SVt_PVGV ? GvIO((GV *)gv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 854 0 854
IO *io = SvTYPE(gv) == SVt_PVGV ? GvIO((GV *)gv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 0 854 854
IO *io = SvTYPE(gv) == SVt_PVGV ? GvIO((GV *)gv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 0 0 0
IO *io = SvTYPE(gv) == SVt_PVGV ? GvIO((GV *)gv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 854 0 854
866 854 ▶ if (io && IoIFP(io)) return SvREFCNT_inc(hm_empty_input); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (io && IoIFP(io)) return SvREFCNT_inc(hm_empty_input); /* # uncoverable condition right count:1 class:ignore_covered_err */ 854 0 854
if (io && IoIFP(io)) return SvREFCNT_inc(hm_empty_input); /* # uncoverable condition right count:1 class:ignore_covered_err */ 853 1 854
867 1 SvREFCNT_dec(hm_empty_input);
868 1 hm_empty_input = NULL;
869 }
870 1 hm_empty_input = hm_new_input(aTHX_ "", 0);
871 1 return SvREFCNT_inc(hm_empty_input);
872 }
873  
874 /* psgix.io: a dup of the client socket, so an app that hijacks it keeps
875 * the socket alive after the server side closes. Absent on Windows:
876 * duplicating a socket there is WSADuplicateSocket, not dup(2), and the
877 * result would have to be re-wrapped for perl's IO layer - unproven, and
878 * psgix.io is optional in PSGI, so it is simply not offered. */
879 818 static SV *hm_fh_for_fd(pTHX_ int fd) {
880 #ifdef _WIN32
881 (void)fd;
882 return &PL_sv_undef;
883 #else
884 818 int nfd = dup(fd);
885 PerlIO *fp;
886 GV *gv;
887 IO *io;
888 /*
889 # uncoverable branch true class:ignore_covered_err
890 # uncoverable condition left count:1 class:ignore_covered_err */
891 818 ▶ if (nfd < 0) return &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
if (nfd < 0) return &PL_sv_undef; 0 818 818
892 818 fp = PerlIO_fdopen(nfd, "r+");
893 /*
894 # uncoverable branch true
895 # uncoverable condition left count:1 class:ignore_covered_err */
896 818 ▶ if (!fp) { hm_os_close(nfd); return &PL_sv_undef; }
Branch: 1 branch
BranchTrueFalseTotal
if (!fp) { hm_os_close(nfd); return &PL_sv_undef; } 0 818 818
897 818 gv = hm_anon_glob(aTHX);
898 818 ▶ io = GvIO(gv);
Branch: 4 branches
BranchTrueFalseTotal
io = GvIO(gv); 818 0 818
io = GvIO(gv); 0 818 818
io = GvIO(gv); 0 0 0
io = GvIO(gv); 818 0 818
899 818 IoIFP(io) = fp;
900 818 IoOFP(io) = fp;
901 818 IoTYPE(io) = IoTYPE_RDWR;
902 818 return newRV_noinc((SV *)gv);
903 #endif
904 }
905  
906 /* Read out a filehandle / IO::Handle-like response body, honouring
907 * ->getline/->close on objects (PSGI body protocol). Returns a PV SV. */
908 4 static SV *hm_slurp_body(pTHX_ SV *body) {
909 4 SV *out = newSVpvs("");
910 4 ▶ if (sv_isobject(body) /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
if (sv_isobject(body) /* # uncoverable condition right count:2 class:ignore_covered_err */ 3 1 4
911 3 ▶ && gv_fetchmethod_autoload(SvSTASH(SvRV(body)), "getline", 0)) {
Branch: 1 branch
BranchTrueFalseTotal
&& gv_fetchmethod_autoload(SvSTASH(SvRV(body)), "getline", 0)) { 3 0 3
912 6 for (;;) {
913 9 dSP;
914 SV *chunk;
915 int n;
916 9 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 9 9
917 9 ▶ XPUSHs(body);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(body); 0 9 9
918 9 PUTBACK;
919 9 n = call_method("getline", G_SCALAR | G_EVAL);
920 9 SPAGAIN;
921 9 ▶ chunk = n ? POPs : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
chunk = n ? POPs : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 9 0 9
922 9 ▶ if (SvTRUE(ERRSV) || !chunk || !SvOK(chunk)) { /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 4 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV) || !chunk || !SvOK(chunk)) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 9 0 9
if (SvTRUE(ERRSV) || !chunk || !SvOK(chunk)) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 8 1 9
if (SvTRUE(ERRSV) || !chunk || !SvOK(chunk)) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 8 0 8
if (SvTRUE(ERRSV) || !chunk || !SvOK(chunk)) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 2 6 8
923 3 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 3 0 3
924 3 break;
925 }
926 6 sv_catsv(out, chunk);
927 6 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 6 0 6
928 }
929 5 ▶ if (gv_fetchmethod_autoload(SvSTASH(SvRV(body)), "close", 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (gv_fetchmethod_autoload(SvSTASH(SvRV(body)), "close", 0)) { 2 1 3
930 2 dSP;
931 2 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 2 2
932 2 ▶ XPUSHs(body);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(body); 0 2 2
933 2 PUTBACK;
934 2 call_method("close", G_DISCARD | G_EVAL);
935 2 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 2 0 2
936 }
937 /*
938 # uncoverable branch false class:ignore_covered_err
939 # uncoverable condition right count:1 class:ignore_covered_err
940 # uncoverable condition right count:2 class:ignore_covered_err
941 # uncoverable condition left count:3 class:ignore_covered_err
942 # uncoverable condition right count:3 class:ignore_covered_err */
943 1 ▶ } else if (SvROK(body) && (SvTYPE(SvRV(body)) == SVt_PVGV
Branch: 2 branches
BranchTrueFalseTotal
} else if (SvROK(body) && (SvTYPE(SvRV(body)) == SVt_PVGV 1 0 1
} else if (SvROK(body) && (SvTYPE(SvRV(body)) == SVt_PVGV 0 1 1
944 0 ▶ || SvTYPE(SvRV(body)) == SVt_PVIO)) { /* # uncoverable statement class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
|| SvTYPE(SvRV(body)) == SVt_PVIO)) { /* # uncoverable statement class:ignore_covered_err */ 0 0 0
945 /* sv_2io croaks on anything that is not a handle, and this is not
946 * under an eval: a body of [200, [], {}] would unwind the loop */
947 1 IO *io = sv_2io(body);
948 /*
949 # uncoverable branch false class:ignore_covered_err
950 # uncoverable condition right count:1 class:ignore_covered_err
951 # uncoverable condition right count:2 class:ignore_covered_err */
952 1 ▶ if (io && IoIFP(io)) {
Branch: 2 branches
BranchTrueFalseTotal
if (io && IoIFP(io)) { 1 0 1
if (io && IoIFP(io)) { 1 0 1
953 char buf[8192];
954 SSize_t got;
955 2 ▶ while ((got = PerlIO_read(IoIFP(io), buf, sizeof(buf))) > 0)
Branch: 1 branch
BranchTrueFalseTotal
while ((got = PerlIO_read(IoIFP(io), buf, sizeof(buf))) > 0) 1 1 2
956 1 sv_catpvn(out, buf, got);
957 1 PerlIO_close(IoIFP(io));
958 1 IoIFP(io) = NULL;
959 1 IoOFP(io) = NULL;
960 }
961 }
962 4 return out;
963 }
964  
965 /* ---- streaming body sources ---------------------------------------------- *
966 * The lift: can this filehandle-ish body be streamed rather than slurped?
967 *
968 * - a glob / IO ref over a regular file lifts to (fd, tell, remaining):
969 * remaining is the Content-Length header when the response carries one,
970 * else file-size minus position (which is then also what the auto
971 * Content-Length announces).
972 * - a blessed object lifts when it has BOTH getline and fileno over a
973 * regular file AND the response promises a Content-Length - the range
974 * contract Punk::SendFile::Reader satisfies; the raw fd position is
975 * taken as the read point, so hand over a handle that has not been
976 * buffer-read from. Without a usable fd, a getline object with a
977 * Content-Length still streams, one chunk per getline (kind 2).
978 *
979 * Anything else returns 0 and the caller slurps, exactly as before. */
980  
981 2 static UV hm_bsrc_method_uv(pTHX_ SV *obj, const char *meth, int *ok) {
982 2 dSP;
983 2 UV out = 0;
984 int n;
985 2 *ok = 0;
986 2 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 2 2
987 2 ▶ XPUSHs(obj);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(obj); 0 2 2
988 2 PUTBACK;
989 2 n = call_method(meth, G_SCALAR | G_EVAL);
990 2 SPAGAIN;
991 /*
992 # uncoverable branch false class:ignore_covered_err
993 # uncoverable condition right count:1 class:ignore_covered_err */
994 2 ▶ if (n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { 2 0 2
995 2 SV *r = POPs;
996 /*
997 # uncoverable branch false class:ignore_covered_err
998 # uncoverable condition right count:1 class:ignore_covered_err
999 # uncoverable condition right count:2 class:ignore_covered_err
1000 # uncoverable condition right count:3 class:ignore_covered_err */
1001 2 ▶ if (!SvTRUE(ERRSV) && SvOK(r) && SvIV(r) >= 0) {
Branch: 4 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && SvOK(r) && SvIV(r) >= 0) { 2 0 2
if (!SvTRUE(ERRSV) && SvOK(r) && SvIV(r) >= 0) { 2 0 2
if (!SvTRUE(ERRSV) && SvOK(r) && SvIV(r) >= 0) { 2 0 2
if (!SvTRUE(ERRSV) && SvOK(r) && SvIV(r) >= 0) { 2 0 2
1002 2 out = (UV)SvUV(r);
1003 2 *ok = 1;
1004 }
1005 }
1006 2 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 2 0 2
1007 2 return out;
1008 }
1009  
1010 18 static int hm_bsrc_lift(pTHX_ SV *body, hm_bsrc *bs, int has_len, UV clen) {
1011 /*
1012 # uncoverable branch true
1013 # uncoverable condition left count:1 class:ignore_covered_err */
1014 18 ▶ if (!SvROK(body)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(body)) return 0; 0 18 18
1015  
1016 18 ▶ if (sv_isobject(body)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_isobject(body)) { 6 12 18
1017 6 HV *stash = SvSTASH(SvRV(body));
1018 6 int fd = -1, ok = 0;
1019 /*
1020 # uncoverable branch true
1021 # uncoverable condition left count:1 class:ignore_covered_err */
1022 6 ▶ if (!gv_fetchmethod_autoload(stash, "getline", 0)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!gv_fetchmethod_autoload(stash, "getline", 0)) return 0; 0 6 6
1023 6 ▶ if (!has_len) return 0; /* no promised length, keep the slurp */
Branch: 1 branch
BranchTrueFalseTotal
if (!has_len) return 0; /* no promised length, keep the slurp */ 3 3 6
1024 3 ▶ if (gv_fetchmethod_autoload(stash, "fileno", 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (gv_fetchmethod_autoload(stash, "fileno", 0)) { 2 1 3
1025 2 UV v = hm_bsrc_method_uv(aTHX_ body, "fileno", &ok);
1026 /*
1027 # uncoverable branch false class:ignore_covered_err
1028 # uncoverable condition right count:1 class:ignore_covered_err */
1029 2 ▶ if (ok) fd = (int)v;
Branch: 1 branch
BranchTrueFalseTotal
if (ok) fd = (int)v; 2 0 2
1030 }
1031 3 ▶ if (fd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd >= 0) { 2 1 3
1032 struct stat st;
1033 off_t pos;
1034 /*
1035 # uncoverable branch false class:ignore_covered_err
1036 # uncoverable condition right count:1 class:ignore_covered_err
1037 # uncoverable condition right count:2 class:ignore_covered_err
1038 # uncoverable condition right count:3 class:ignore_covered_err */
1039 2 ▶ if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode)
Branch: 2 branches
BranchTrueFalseTotal
if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode) 2 0 2
if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode) 2 0 2
1040 2 ▶ && (pos = lseek(fd, 0, SEEK_CUR)) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
&& (pos = lseek(fd, 0, SEEK_CUR)) >= 0) { 2 0 2
1041 2 bs->kind = 1;
1042 2 bs->fd = fd;
1043 2 bs->off = pos;
1044 2 bs->remaining = clen;
1045 2 bs->sv = SvREFCNT_inc(body);
1046 2 return 1;
1047 }
1048 }
1049 1 bs->kind = 2;
1050 1 bs->remaining = clen;
1051 1 bs->sv = SvREFCNT_inc(body);
1052 1 return 1;
1053 }
1054  
1055 /*
1056 # uncoverable branch false class:ignore_covered_err
1057 # uncoverable condition right count:1 class:ignore_covered_err
1058 # uncoverable condition left count:2 class:ignore_covered_err
1059 # uncoverable condition right count:2 class:ignore_covered_err */
1060 12 ▶ if (SvTYPE(SvRV(body)) == SVt_PVGV || SvTYPE(SvRV(body)) == SVt_PVIO) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTYPE(SvRV(body)) == SVt_PVGV || SvTYPE(SvRV(body)) == SVt_PVIO) { 0 12 12
if (SvTYPE(SvRV(body)) == SVt_PVGV || SvTYPE(SvRV(body)) == SVt_PVIO) { 0 0 0
1061 12 IO *io = sv_2io(body);
1062 12 ▶ PerlIO *fp = io ? IoIFP(io) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
PerlIO *fp = io ? IoIFP(io) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 12 0 12
1063 struct stat st;
1064 int fd;
1065 off_t pos;
1066 /*
1067 # uncoverable branch true
1068 # uncoverable condition left count:1 class:ignore_covered_err */
1069 12 ▶ if (!fp) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!fp) return 0; 0 12 12
1070 12 fd = PerlIO_fileno(fp);
1071 /*
1072 # uncoverable condition left count:2 class:ignore_covered_err
1073 # uncoverable condition left count:3 class:ignore_covered_err */
1074 12 ▶ if (fd < 0 || fstat(fd, &st) < 0 || !S_ISREG(st.st_mode)) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (fd < 0 || fstat(fd, &st) < 0 || !S_ISREG(st.st_mode)) return 0; 11 1 12
if (fd < 0 || fstat(fd, &st) < 0 || !S_ISREG(st.st_mode)) return 0; 11 0 11
if (fd < 0 || fstat(fd, &st) < 0 || !S_ISREG(st.st_mode)) return 0; 0 11 11
1075 11 pos = (off_t)PerlIO_tell(fp);
1076 /*
1077 # uncoverable branch true class:ignore_covered_err
1078 # uncoverable condition left count:1 class:ignore_covered_err
1079 # uncoverable condition left count:2 class:ignore_covered_err */
1080 11 ▶ if (pos < 0 || pos > st.st_size) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (pos < 0 || pos > st.st_size) return 0; 11 0 11
if (pos < 0 || pos > st.st_size) return 0; 0 11 11
1081 11 bs->kind = 1;
1082 11 bs->fd = fd;
1083 11 bs->off = pos;
1084 11 ▶ bs->remaining = has_len ? clen : (UV)(st.st_size - pos);
Branch: 1 branch
BranchTrueFalseTotal
bs->remaining = has_len ? clen : (UV)(st.st_size - pos); 7 4 11
1085 11 bs->sv = SvREFCNT_inc(body);
1086 11 return 1;
1087 }
1088 0 return 0; /* # uncoverable statement */
1089 }
1090  
1091 /* Close out a source: the PSGI body contract says the server closes the
1092 * handle it was given (hm_slurp_body has always done so). Objects get
1093 * their close method; a glob's PerlIO is closed directly. */
1094 14 static void hm_bsrc_release(pTHX_ hm_bsrc *bs) {
1095 /*
1096 # uncoverable branch true
1097 # uncoverable condition left count:1 class:ignore_covered_err */
1098 14 ▶ if (!bs->kind) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!bs->kind) return; 0 14 14
1099 /*
1100 # uncoverable branch false class:ignore_covered_err
1101 # uncoverable condition right count:1 class:ignore_covered_err */
1102 14 ▶ if (bs->sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (bs->sv) { 14 0 14
1103 14 ▶ if (sv_isobject(bs->sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_isobject(bs->sv)) { 3 11 14
1104 /*
1105 # uncoverable branch false class:ignore_covered_err
1106 # uncoverable condition right count:1 class:ignore_covered_err */
1107 3 ▶ if (gv_fetchmethod_autoload(SvSTASH(SvRV(bs->sv)), "close", 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (gv_fetchmethod_autoload(SvSTASH(SvRV(bs->sv)), "close", 0)) { 3 0 3
1108 3 dSP;
1109 3 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 3 3
1110 3 ▶ XPUSHs(bs->sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(bs->sv); 0 3 3
1111 3 PUTBACK;
1112 3 call_method("close", G_DISCARD | G_EVAL);
1113 3 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 3 0 3
1114 }
1115 }
1116 else {
1117 11 IO *io = sv_2io(bs->sv);
1118 /*
1119 # uncoverable branch false class:ignore_covered_err
1120 # uncoverable condition right count:1 class:ignore_covered_err
1121 # uncoverable condition right count:2 class:ignore_covered_err */
1122 11 ▶ if (io && IoIFP(io)) {
Branch: 2 branches
BranchTrueFalseTotal
if (io && IoIFP(io)) { 11 0 11
if (io && IoIFP(io)) { 11 0 11
1123 11 PerlIO_close(IoIFP(io));
1124 11 IoIFP(io) = NULL;
1125 11 IoOFP(io) = NULL;
1126 }
1127 }
1128 14 SvREFCNT_dec(bs->sv);
1129 }
1130 14 memset(bs, 0, sizeof(*bs));
1131 }
1132  
1133 /* Refill the write buffer with the next chunk of the source. Returns the
1134 * bytes added; 0 when the source is spent; -1 on a source that ended
1135 * early (file truncated under us, getline died or dried up) - the client
1136 * was promised more bytes than it will get, so the connection must not
1137 * be reused. */
1138 45 static ssize_t hm_bsrc_fill(pTHX_ hm_conn *c) {
1139 45 hm_bsrc *bs = &c->bsrc;
1140 45 ▶ if (!bs->remaining) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!bs->remaining) return 0; 1 44 45
1141 44 ▶ if (bs->kind == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (bs->kind == 1) { 1 43 44
1142 1 size_t want = bs->remaining < HM_BSRC_CHUNK
1143 ? (size_t)bs->remaining : HM_BSRC_CHUNK;
1144 ssize_t n;
1145 1 hm_wb_reserve(c, want);
1146 1 n = hm_os_pread(bs->fd, c->wbuf + c->wlen, want, bs->off);
1147 /*
1148 # uncoverable branch true
1149 # uncoverable condition left count:1 class:ignore_covered_err */
1150 1 ▶ if (n <= 0) { c->keepalive = 0; return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) { c->keepalive = 0; return -1; } 0 1 1
1151 1 c->wlen += (size_t)n;
1152 1 bs->off += n;
1153 1 bs->remaining -= (UV)n;
1154 1 return n;
1155 }
1156 0 for (;;) { /* kind 2: one getline */ /* # uncoverable statement class:ignore_covered_err */
1157 43 dSP;
1158 SV *chunk;
1159 43 STRLEN l = 0;
1160 43 const char *p = NULL;
1161 int n;
1162 43 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 43 43
1163 43 ▶ XPUSHs(bs->sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(bs->sv); 0 43 43
1164 43 PUTBACK;
1165 43 n = call_method("getline", G_SCALAR | G_EVAL);
1166 43 SPAGAIN;
1167 43 ▶ chunk = n > 0 ? POPs : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
chunk = n > 0 ? POPs : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 43 0 43
1168 /*
1169 # uncoverable branch true class:ignore_covered_err
1170 # uncoverable condition left count:1 class:ignore_covered_err
1171 # uncoverable condition left count:2 class:ignore_covered_err
1172 # uncoverable condition left count:3 class:ignore_covered_err */
1173 43 ▶ if (SvTRUE(ERRSV) || !chunk || !SvOK(chunk)) {
Branch: 4 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV) || !chunk || !SvOK(chunk)) { 43 0 43
if (SvTRUE(ERRSV) || !chunk || !SvOK(chunk)) { 43 0 43
if (SvTRUE(ERRSV) || !chunk || !SvOK(chunk)) { 43 0 43
if (SvTRUE(ERRSV) || !chunk || !SvOK(chunk)) { 0 43 43
1174 0 ▶ PUTBACK; FREETMPS; LEAVE; /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; /* # uncoverable statement */ 0 0 0
1175 0 c->keepalive = 0; /* remaining > 0 here */ /* # uncoverable statement */
1176 43 return -1; /* # uncoverable statement class:ignore_covered_err */
1177 }
1178 43 p = SvPV(chunk, l);
1179 /*
1180 # uncoverable branch true class:ignore_covered_err
1181 # uncoverable condition left count:1 class:ignore_covered_err */
1182 43 ▶ if (l > bs->remaining) l = (STRLEN)bs->remaining; /* hold the framing */
Branch: 1 branch
BranchTrueFalseTotal
if (l > bs->remaining) l = (STRLEN)bs->remaining; /* hold the framing */ 0 43 43
1183 /*
1184 # uncoverable branch false class:ignore_covered_err
1185 # uncoverable condition right count:1 class:ignore_covered_err */
1186 43 ▶ if (l) hm_wb_put(c, p, l);
Branch: 1 branch
BranchTrueFalseTotal
if (l) hm_wb_put(c, p, l); 43 0 43
1187 43 bs->remaining -= (UV)l;
1188 43 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 43 0 43
1189 /*
1190 # uncoverable branch false
1191 # uncoverable condition right count:1 class:ignore_covered_err */
1192 43 ▶ if (l) return (ssize_t)l;
Branch: 1 branch
BranchTrueFalseTotal
if (l) return (ssize_t)l; 43 0 43
1193 /*
1194 # uncoverable statement
1195 # uncoverable branch true
1196 # uncoverable branch false
1197 # uncoverable condition left count:1
1198 # uncoverable condition right count:1 */
1199 0 ▶ if (!bs->remaining) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!bs->remaining) return 0; 0 0 0
1200 /* an empty chunk with bytes still owed: ask again */
1201 }
1202 }
1203  
1204 /* Scan a header AV for Content-Length; fills *out and returns 1 if found. */
1205 21 static int hm_hav_clen(pTHX_ AV *hav, UV *out) {
1206 SSize_t i, n;
1207 /*
1208 # uncoverable branch true
1209 # uncoverable condition left count:1 class:ignore_covered_err */
1210 21 ▶ if (!hav) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!hav) return 0; 0 21 21
1211 21 n = av_len(hav) + 1;
1212 36 ▶ for (i = 0; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < n; i += 2) { 25 11 36
1213 25 SV **k = av_fetch(hav, i, 0);
1214 STRLEN kl;
1215 const char *ks;
1216 /*
1217 # uncoverable branch true class:ignore_covered_err
1218 # uncoverable condition left count:1 class:ignore_covered_err
1219 # uncoverable condition left count:2 class:ignore_covered_err */
1220 25 ▶ if (!k || !*k) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!k || !*k) continue; 25 0 25
if (!k || !*k) continue; 0 25 25
1221 25 ks = SvPV(*k, kl);
1222 25 ▶ if (kl == 14 && hm_strncasecmp(ks, "Content-Length", 14) == 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 14 && hm_strncasecmp(ks, "Content-Length", 14) == 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 10 15 25
if (kl == 14 && hm_strncasecmp(ks, "Content-Length", 14) == 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 10 0 10
1223 10 SV **v = av_fetch(hav, i + 1, 0);
1224 /*
1225 # uncoverable branch false class:ignore_covered_err
1226 # uncoverable condition right count:1 class:ignore_covered_err
1227 # uncoverable condition right count:2 class:ignore_covered_err
1228 # uncoverable condition right count:3 class:ignore_covered_err */
1229 10 ▶ if (v && *v && SvOK(*v)) {
Branch: 3 branches
BranchTrueFalseTotal
if (v && *v && SvOK(*v)) { 10 0 10
if (v && *v && SvOK(*v)) { 10 0 10
if (v && *v && SvOK(*v)) { 10 0 10
1230 /* Digits only. SvUV of "abc" is 0, which selected the
1231 * counted mode with a length of nothing and cut the body
1232 * at its first write; a value that is not a length is
1233 * treated as absent, and the framing chosen without it. */
1234 STRLEN vl, j;
1235 10 const char *vs = SvPV(*v, vl);
1236 /*
1237 # uncoverable branch true class:ignore_covered_err
1238 # uncoverable condition left count:1 class:ignore_covered_err
1239 # uncoverable condition left count:2 class:ignore_covered_err */
1240 10 ▶ if (vl == 0 || vl > 19) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (vl == 0 || vl > 19) return 0; 10 0 10
if (vl == 0 || vl > 19) return 0; 0 10 10
1241 50 ▶ for (j = 0; j < vl; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < vl; j++) 41 9 50
1242 41 ▶ if (vs[j] < '0' || vs[j] > '9') return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (vs[j] < '0' || vs[j] > '9') return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */ 41 0 41
if (vs[j] < '0' || vs[j] > '9') return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 40 41
1243 9 *out = (UV)SvUV(*v);
1244 9 return 1;
1245 }
1246 }
1247 }
1248 11 return 0;
1249 }
1250  
1251 /* a [500, [...], [$msg]] PSGI triple (mortal) */
1252 6 static SV *hm_500_resp(pTHX_ const char *msg) {
1253 6 AV *resp = newAV();
1254 6 AV *hdrs = newAV();
1255 6 AV *body = newAV();
1256 6 av_push(hdrs, newSVpvs("Content-Type"));
1257 6 av_push(hdrs, newSVpvs("text/plain"));
1258 6 av_push(body, newSVpv(msg, 0));
1259 6 av_push(resp, newSViv(500));
1260 6 av_push(resp, newRV_noinc((SV *)hdrs));
1261 6 av_push(resp, newRV_noinc((SV *)body));
1262 6 return sv_2mortal(newRV_noinc((SV *)resp));
1263 }
1264  
1265 /* ---- connections --------------------------------------------------------- */
1266  
1267 /* Return a connection struct to the pool, or free it when the pool is full.
1268 * The buffers ride along so the steady state allocates nothing - but only
1269 * at their ordinary sizes. A read buffer grown for a 15MB body, or a write
1270 * buffer grown for one slow client, would otherwise stay that size for as
1271 * long as the pool held the struct, and a thousand of them is the whole
1272 * ceiling resident for nothing. */
1273 937 static void hm_pool_conn(hm_loop *loop, hm_conn *c) {
1274 937 ▶ if (c->rbuf && c->rcap > HM_RBUF) { free(c->rbuf); c->rbuf = NULL; c->rcap = 0; } /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (c->rbuf && c->rcap > HM_RBUF) { free(c->rbuf); c->rbuf = NULL; c->rcap = 0; } /* # uncoverable condition right count:1 class:ignore_covered_err */ 937 0 937
if (c->rbuf && c->rcap > HM_RBUF) { free(c->rbuf); c->rbuf = NULL; c->rcap = 0; } /* # uncoverable condition right count:1 class:ignore_covered_err */ 7 930 937
1275 937 ▶ if (c->wbuf && c->wcap > HM_WB_KEEP) { free(c->wbuf); c->wbuf = NULL; c->wcap = 0; }
Branch: 2 branches
BranchTrueFalseTotal
if (c->wbuf && c->wcap > HM_WB_KEEP) { free(c->wbuf); c->wbuf = NULL; c->wcap = 0; } 881 56 937
if (c->wbuf && c->wcap > HM_WB_KEEP) { free(c->wbuf); c->wbuf = NULL; c->wcap = 0; } 5 876 881
1276 /*
1277 # uncoverable branch false
1278 # uncoverable condition right count:1 class:ignore_covered_err */
1279 937 ▶ if (loop->pool_n < HM_POOL_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if (loop->pool_n < HM_POOL_MAX) { 937 0 937
1280 937 c->pool_next = loop->pool;
1281 937 loop->pool = c;
1282 937 loop->pool_n++;
1283 } else {
1284 /*
1285 # uncoverable statement
1286 # uncoverable branch true
1287 # uncoverable branch false
1288 # uncoverable condition left count:1
1289 # uncoverable condition right count:1 */
1290 0 ▶ if (c->wbuf) free(c->wbuf);
Branch: 1 branch
BranchTrueFalseTotal
if (c->wbuf) free(c->wbuf); 0 0 0
1291 /*
1292 # uncoverable statement
1293 # uncoverable branch true
1294 # uncoverable branch false
1295 # uncoverable condition left count:1
1296 # uncoverable condition right count:1 */
1297 0 ▶ if (c->rbuf) free(c->rbuf);
Branch: 1 branch
BranchTrueFalseTotal
if (c->rbuf) free(c->rbuf); 0 0 0
1298 0 free(c); /* # uncoverable statement */
1299 }
1300 937 }
1301  
1302 947 static void hm_lru_unlink(hm_loop *loop, hm_conn *c) {
1303 947 ▶ if (c->lru_prev) c->lru_prev->lru_next = c->lru_next;
Branch: 1 branch
BranchTrueFalseTotal
if (c->lru_prev) c->lru_prev->lru_next = c->lru_next; 64 883 947
1304 /*
1305 # uncoverable branch false class:ignore_covered_err
1306 # uncoverable condition right count:1 class:ignore_covered_err */
1307 883 ▶ else if (loop->lru_head == c) loop->lru_head = c->lru_next;
Branch: 1 branch
BranchTrueFalseTotal
else if (loop->lru_head == c) loop->lru_head = c->lru_next; 883 0 883
1308 947 ▶ if (c->lru_next) c->lru_next->lru_prev = c->lru_prev;
Branch: 1 branch
BranchTrueFalseTotal
if (c->lru_next) c->lru_next->lru_prev = c->lru_prev; 131 816 947
1309 /*
1310 # uncoverable branch false class:ignore_covered_err
1311 # uncoverable condition right count:1 class:ignore_covered_err */
1312 816 ▶ else if (loop->lru_tail == c) loop->lru_tail = c->lru_prev;
Branch: 1 branch
BranchTrueFalseTotal
else if (loop->lru_tail == c) loop->lru_tail = c->lru_prev; 816 0 816
1313 947 c->lru_prev = c->lru_next = NULL;
1314 947 }
1315  
1316 972 static void hm_lru_touch(hm_loop *loop, hm_conn *c) {
1317 972 c->last_active = loop->now;
1318 972 ▶ if (loop->lru_tail == c) return;
Branch: 1 branch
BranchTrueFalseTotal
if (loop->lru_tail == c) return; 962 10 972
1319 10 hm_lru_unlink(loop, c);
1320 10 c->lru_prev = loop->lru_tail;
1321 /*
1322 # uncoverable branch false class:ignore_covered_err
1323 # uncoverable condition right count:1 class:ignore_covered_err */
1324 10 ▶ if (loop->lru_tail) loop->lru_tail->lru_next = c;
Branch: 1 branch
BranchTrueFalseTotal
if (loop->lru_tail) loop->lru_tail->lru_next = c; 10 0 10
1325 10 loop->lru_tail = c;
1326 /*
1327 # uncoverable branch true class:ignore_covered_err
1328 # uncoverable condition left count:1 class:ignore_covered_err */
1329 10 ▶ if (!loop->lru_head) loop->lru_head = c;
Branch: 1 branch
BranchTrueFalseTotal
if (!loop->lru_head) loop->lru_head = c; 0 10 10
1330 }
1331  
1332 /* ---- completion-mode receive ---------------------------------------------
1333 *
1334 * A readiness backend says "this fd has bytes" and the core reads them. A
1335 * completion backend is asked for the read UP FRONT and hands back bytes that
1336 * are already in the buffer - which is how the read syscall stops being a
1337 * syscall per request and becomes part of one batched submission per turn of
1338 * the loop.
1339 *
1340 * What that costs is that the kernel holds a pointer into c->rbuf for a while,
1341 * and the two things the core does casually with that buffer - GROW it, and
1342 * POOL it for the next connection - both become memory corruption if they
1343 * happen while a read is outstanding. Growth is handled by only ever growing
1344 * before arming (hm_arm_recv). Pooling is handled here.
1345 *
1346 * The identity problem is the other half. A completion can outlive the
1347 * connection that asked for it: hm_close closes the fd, the number is reused
1348 * by a new connection moments later, and the old read lands looking exactly
1349 * like the new one's. So every read carries a token of fd plus the
1350 * connection's generation id, and a completion whose token does not match the
1351 * connection now holding that fd is somebody else's. */
1352  
1353 #define HM_RECV_TOKEN(c) \
1354 (((uint64_t)(c)->id << 17) | (uint64_t)(unsigned)(c)->fd)
1355  
1356 /* A read buffer whose connection is gone but whose kernel read is not. Parked
1357 * under the token the kernel will hand back, and freed when it does. */
1358 typedef struct hm_quar {
1359 uint64_t token;
1360 char *buf;
1361 struct hm_quar *next;
1362 } hm_quar;
1363  
1364 /*
1365 # uncoverable statement
1366 # uncoverable subroutine */
1367 0 static void hm_quar_push(hm_loop *loop, uint64_t token, char *buf) {
1368 hm_quar *q;
1369 /*
1370 # uncoverable statement
1371 # uncoverable branch true
1372 # uncoverable branch false
1373 # uncoverable condition left count:1
1374 # uncoverable condition right count:1 */
1375 0 ▶ if (!buf) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!buf) return; 0 0 0
1376 0 q = (hm_quar *)malloc(sizeof(hm_quar)); /* # uncoverable statement */
1377 /* Out of memory with a read outstanding: the buffer CANNOT be freed, so
1378 * it is leaked deliberately. One leaked read buffer beats handing the
1379 * kernel a pointer into the heap it no longer owns. */
1380 /*
1381 # uncoverable statement
1382 # uncoverable branch true
1383 # uncoverable branch false
1384 # uncoverable condition left count:1
1385 # uncoverable condition right count:1 */
1386 0 ▶ if (!q) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!q) return; 0 0 0
1387 0 q->token = token; /* # uncoverable statement */
1388 0 q->buf = buf; /* # uncoverable statement */
1389 0 q->next = loop->quar; /* # uncoverable statement */
1390 0 loop->quar = q; /* # uncoverable statement */
1391 }
1392  
1393 /*
1394 # uncoverable statement
1395 # uncoverable subroutine */
1396 0 static void hm_quar_release(hm_loop *loop, uint64_t token) {
1397 0 hm_quar **pp = &loop->quar, *q; /* # uncoverable statement */
1398 /*
1399 # uncoverable statement
1400 # uncoverable branch true
1401 # uncoverable branch false
1402 # uncoverable condition left count:1
1403 # uncoverable condition right count:1 */
1404 0 ▶ while ((q = *pp)) {
Branch: 1 branch
BranchTrueFalseTotal
while ((q = *pp)) { 0 0 0
1405 /*
1406 # uncoverable statement
1407 # uncoverable branch true
1408 # uncoverable branch false
1409 # uncoverable condition left count:1
1410 # uncoverable condition right count:1 */
1411 0 ▶ if (q->token == token) { *pp = q->next; free(q->buf); free(q); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (q->token == token) { *pp = q->next; free(q->buf); free(q); return; } 0 0 0
1412 0 pp = &q->next; /* # uncoverable statement */
1413 }
1414 }
1415  
1416 171 static void hm_quar_free_all(hm_loop *loop) {
1417 171 hm_quar *q = loop->quar, *n;
1418 /*
1419 # uncoverable branch true class:ignore_covered_err
1420 # uncoverable condition left count:1 class:ignore_covered_err */
1421 171 ▶ while (q) { n = q->next; free(q->buf); free(q); q = n; }
Branch: 1 branch
BranchTrueFalseTotal
while (q) { n = q->next; free(q->buf); free(q); q = n; } 0 171 171
1422 171 loop->quar = NULL;
1423 171 }
1424  
1425 /* Ask for this connection's next read. 1 if one is now outstanding, 0 if the
1426 * caller must fall back to a readiness watcher - which is always correct, so
1427 * every refusal here is a performance decision and never a correctness one. */
1428 865 static int hm_arm_recv(hm_loop *loop, hm_conn *c) {
1429 size_t want;
1430 /*
1431 # uncoverable branch false class:ignore_covered_err
1432 # uncoverable condition right count:1 class:ignore_covered_err
1433 # uncoverable condition left count:2 class:ignore_covered_err
1434 # uncoverable condition right count:2 class:ignore_covered_err */
1435 865 ▶ if (!loop->completion || !loop->be->submit_recv) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!loop->completion || !loop->be->submit_recv) return 0; 0 865 865
if (!loop->completion || !loop->be->submit_recv) return 0; 0 0 0
1436 /* TLS reads belong to OpenSSL: the bytes on the socket are ciphertext and
1437 * the engine may already hold plaintext this would never see. */
1438 /*
1439 # uncoverable statement
1440 # uncoverable branch true
1441 # uncoverable branch false
1442 # uncoverable condition left count:1
1443 # uncoverable condition right count:1
1444 # uncoverable condition left count:2
1445 # uncoverable condition right count:2
1446 # uncoverable condition left count:3
1447 # uncoverable condition right count:3
1448 # uncoverable condition left count:4
1449 # uncoverable condition right count:4 */
1450 0 ▶ if (c->ssl || c->recv_inflight || c->detached || c->pp_wait) return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (c->ssl || c->recv_inflight || c->detached || c->pp_wait) return 0; 0 0 0
if (c->ssl || c->recv_inflight || c->detached || c->pp_wait) return 0; 0 0 0
if (c->ssl || c->recv_inflight || c->detached || c->pp_wait) return 0; 0 0 0
if (c->ssl || c->recv_inflight || c->detached || c->pp_wait) return 0; 0 0 0
1451 /*
1452 # uncoverable statement
1453 # uncoverable branch true
1454 # uncoverable branch false
1455 # uncoverable condition left count:1
1456 # uncoverable condition right count:1 */
1457 0 ▶ if (c->rlen == c->rcap) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->rlen == c->rcap) { 0 0 0
1458 /* Grow BEFORE the kernel is given the pointer. Never while a read is
1459 * outstanding, which is why this cannot live in the completion. */
1460 size_t ncap;
1461 /*
1462 # uncoverable statement
1463 # uncoverable branch true
1464 # uncoverable branch false
1465 # uncoverable condition left count:1
1466 # uncoverable condition right count:1 */
1467 0 ▶ if (c->rcap >= loop->max_read) return 0; /* the readiness path owns
Branch: 1 branch
BranchTrueFalseTotal
if (c->rcap >= loop->max_read) return 0; /* the readiness path owns 0 0 0
1468 * the oversize/413 answer */
1469 0 ncap = c->rcap * 2; /* # uncoverable statement */
1470 /*
1471 # uncoverable statement
1472 # uncoverable branch true
1473 # uncoverable branch false
1474 # uncoverable condition left count:1
1475 # uncoverable condition right count:1 */
1476 0 ▶ if (ncap > loop->max_read) ncap = loop->max_read;
Branch: 1 branch
BranchTrueFalseTotal
if (ncap > loop->max_read) ncap = loop->max_read; 0 0 0
1477 0 c->rbuf = (char *)hm_xrealloc(c->rbuf, ncap); /* # uncoverable statement */
1478 0 c->rcap = ncap; /* # uncoverable statement */
1479 }
1480 0 want = c->rcap - c->rlen; /* # uncoverable statement */
1481 /*
1482 # uncoverable statement
1483 # uncoverable branch true
1484 # uncoverable branch false
1485 # uncoverable condition left count:1
1486 # uncoverable condition right count:1 */
1487 0 ▶ if (loop->be->submit_recv(loop->be, c->fd, c->rbuf + c->rlen, want,
Branch: 1 branch
BranchTrueFalseTotal
if (loop->be->submit_recv(loop->be, c->fd, c->rbuf + c->rlen, want, 0 0 0
1488 0 HM_RECV_TOKEN(c)) != 0) /* # uncoverable statement */
1489 0 return 0; /* # uncoverable statement */
1490 0 c->recv_inflight = 1; /* # uncoverable statement */
1491 0 return 1; /* # uncoverable statement */
1492 }
1493  
1494 static int hm_read_lend(hm_loop *loop, hm_conn *c);
1495 static void hm_read_unlend(hm_loop *loop, hm_conn *c);
1496  
1497 /* Requests are waiting in rbuf behind a park, a streamed body or a full write
1498 * buffer, and whoever serves them will memmove the buffer down. A completion
1499 * armed now holds rbuf + rlen, and after the move it lands past the new rlen
1500 * while rlen counts it from the front: the parser re-reads the tail of the
1501 * request it already served and waits for the rest of it until the idle
1502 * sweep. (t/27 on every Linux smoker with liburing: the request after a
1503 * pipelined one behind a sendfile body was never read.) */
1504 /*
1505 # uncoverable statement
1506 # uncoverable subroutine */
1507 0 static int hm_recv_unsafe(hm_conn *c) {
1508 0 return c->rlen > 0 /* # uncoverable statement */
1509 0 ▶ && (c->again_queued || c->bsrc.kind || c->awaiting /* # uncoverable statement */
Branch: 4 branches
BranchTrueFalseTotal
&& (c->again_queued || c->bsrc.kind || c->awaiting /* # uncoverable statement */ 0 0 0
&& (c->again_queued || c->bsrc.kind || c->awaiting /* # uncoverable statement */ 0 0 0
&& (c->again_queued || c->bsrc.kind || c->awaiting /* # uncoverable statement */ 0 0 0
&& (c->again_queued || c->bsrc.kind || c->awaiting /* # uncoverable statement */ 0 0 0
1510 0 ▶ || c->backlogged || c->stream_open); /* # uncoverable statement */
Branch: 2 branches
BranchTrueFalseTotal
|| c->backlogged || c->stream_open); /* # uncoverable statement */ 0 0 0
|| c->backlogged || c->stream_open); /* # uncoverable statement */ 0 0 0
1511 }
1512  
1513 /* Arm the next read however this connection reads: a completion if it can,
1514 * a readiness watcher otherwise. */
1515 875 static void hm_rearm_read(hm_loop *loop, hm_conn *c) {
1516 /* A readiness read happens at rlen as it stands at the time, so it is
1517 * safe across the move; given back once the buffer has been served. */
1518 /*
1519 # uncoverable branch true class:ignore_covered_err
1520 # uncoverable condition left count:1 class:ignore_covered_err */
1521 875 ▶ if (c->recv_lent) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->recv_lent) { 0 875 875
1522 /*
1523 # uncoverable statement
1524 # uncoverable branch true
1525 # uncoverable branch false
1526 # uncoverable condition left count:1
1527 # uncoverable condition right count:1 */
1528 0 ▶ if (hm_recv_unsafe(c)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (hm_recv_unsafe(c)) return; 0 0 0
1529 0 hm_read_unlend(loop, c); /* # uncoverable statement */
1530 0 c->recv_lent = 0; /* # uncoverable statement */
1531 }
1532 /* Already reading off readiness: the watcher is persistent and
1533 * level-triggered, so re-adding it would be a syscall per response on
1534 * epoll for no effect at all. */
1535 875 ▶ if (c->recv_fallback) return;
Branch: 1 branch
BranchTrueFalseTotal
if (c->recv_fallback) return; 10 865 875
1536 /* A read is already outstanding: the kernel holds rbuf + rlen and will
1537 * complete into it. A readiness watcher added now would read the same
1538 * bytes into the same offset first. hm_arm_recv refuses for the same
1539 * reason, but its refusal is not a request to fall back. */
1540 /*
1541 # uncoverable branch true
1542 # uncoverable condition left count:1 class:ignore_covered_err */
1543 865 ▶ if (c->recv_inflight) return;
Branch: 1 branch
BranchTrueFalseTotal
if (c->recv_inflight) return; 0 865 865
1544 /*
1545 # uncoverable branch true class:ignore_covered_err
1546 # uncoverable condition left count:1 class:ignore_covered_err
1547 # uncoverable condition left count:2 class:ignore_covered_err
1548 # uncoverable condition right count:2 class:ignore_covered_err */
1549 865 ▶ if (loop->completion && hm_recv_unsafe(c)) {
Branch: 2 branches
BranchTrueFalseTotal
if (loop->completion && hm_recv_unsafe(c)) { 0 865 865
if (loop->completion && hm_recv_unsafe(c)) { 0 0 0
1550 /*
1551 # uncoverable statement
1552 # uncoverable branch true
1553 # uncoverable branch false
1554 # uncoverable condition left count:1
1555 # uncoverable condition right count:1 */
1556 0 ▶ if (hm_read_lend(loop, c)) c->recv_lent = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (hm_read_lend(loop, c)) c->recv_lent = 1; 0 0 0
1557 0 return; /* # uncoverable statement */
1558 }
1559 /*
1560 # uncoverable branch true
1561 # uncoverable condition left count:1 class:ignore_covered_err */
1562 865 ▶ if (hm_arm_recv(loop, c)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (hm_arm_recv(loop, c)) return; 0 865 865
1563 865 c->recv_fallback = 1;
1564 865 loop->be->add_io(loop->be, c->fd, HM_EV_READ, 0);
1565 }
1566  
1567 /* Lend a completion-mode connection a readiness watcher for as long as an
1568 * application call that may wait on it is on the stack: an HTTP/2 handler
1569 * waiting for its body to drain is waiting for window updates on this very
1570 * socket. No completion is outstanding then - HM_EV_RECV re-arms only after
1571 * hm_process returns, and arming one under it would let the kernel write into
1572 * a buffer nghttp2 is consuming - so without the watcher the handler pumps a
1573 * loop that never reads its connection. 1 if lent. */
1574 0 static int hm_read_lend(hm_loop *loop, hm_conn *c) {
1575 /*
1576 # uncoverable branch false class:ignore_covered_err
1577 # uncoverable condition right count:1 class:ignore_covered_err
1578 # uncoverable condition left count:2 class:ignore_covered_err
1579 # uncoverable condition right count:2 class:ignore_covered_err
1580 # uncoverable condition left count:3 class:ignore_covered_err
1581 # uncoverable condition right count:3 class:ignore_covered_err */
1582 0 ▶ if (!loop->completion || c->recv_fallback || c->recv_inflight) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!loop->completion || c->recv_fallback || c->recv_inflight) return 0; 0 0 0
if (!loop->completion || c->recv_fallback || c->recv_inflight) return 0; 0 0 0
if (!loop->completion || c->recv_fallback || c->recv_inflight) return 0; 0 0 0
1583 0 c->recv_fallback = 1; /* # uncoverable statement */
1584 0 loop->be->add_io(loop->be, c->fd, HM_EV_READ, 0); /* # uncoverable statement */
1585 0 return 1; /* # uncoverable statement */
1586 }
1587  
1588 /* And take it back: the caller's hm_rearm_read returns it to completion. */
1589 /*
1590 # uncoverable statement
1591 # uncoverable subroutine */
1592 0 static void hm_read_unlend(hm_loop *loop, hm_conn *c) {
1593 /*
1594 # uncoverable statement
1595 # uncoverable branch true
1596 # uncoverable branch false
1597 # uncoverable condition left count:1
1598 # uncoverable condition right count:1 */
1599 0 ▶ if (!c->read_paused) loop->be->remove_io(loop->be, c->fd, HM_EV_READ);
Branch: 1 branch
BranchTrueFalseTotal
if (!c->read_paused) loop->be->remove_io(loop->be, c->fd, HM_EV_READ); 0 0 0
1600 0 c->read_paused = 0; /* # uncoverable statement */
1601 0 c->recv_fallback = 0; /* # uncoverable statement */
1602 0 }
1603  
1604 934 static void hm_close(pTHX_ hm_loop *loop, hm_conn *c) {
1605 /* A synchronous handler for this connection is on the stack: its ->get
1606 * pumped this loop and the loop found the peer gone, or the sweep found
1607 * it idle. The frame that called the app still holds c and will finish
1608 * with it (hm_process's consumed/memmove, hm_queue_response), so the
1609 * struct cannot be pooled - the next accept would hand it to a new
1610 * client and the first client's response would land on the second's
1611 * socket. Stop reading, mark it, and let that frame close it. */
1612 934 ▶ if (c->in_app) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->in_app) { 1 933 934
1613 /*
1614 # uncoverable branch false class:ignore_covered_err
1615 # uncoverable condition right count:1 class:ignore_covered_err */
1616 1 ▶ if (!c->close_deferred) {
Branch: 1 branch
BranchTrueFalseTotal
if (!c->close_deferred) { 1 0 1
1617 1 c->close_deferred = 1;
1618 1 loop->be->remove_io(loop->be, c->fd,
1619 1 ▶ HM_EV_READ | (c->writing ? HM_EV_WRITE : 0)); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
HM_EV_READ | (c->writing ? HM_EV_WRITE : 0)); /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 1 1
1620 1 c->writing = 0;
1621 }
1622 1 return;
1623 }
1624 /* Stream handles open on this connection die with it. They are told
1625 * first, while the connection is still consistent, and they are only
1626 * marked dead - the handle itself belongs to whoever opened it until
1627 * it calls stream_close. */
1628 933 ▶ if (hm_stream_live_n) hm_stream_conn_gone(aTHX_ c->fd, c->id);
Branch: 1 branch
BranchTrueFalseTotal
if (hm_stream_live_n) hm_stream_conn_gone(aTHX_ c->fd, c->id); 12 921 933
1629 /* Before anything else touches the buffers: if the kernel is still
1630 * holding a pointer into this connection's read buffer, take the buffer
1631 * away from the connection so the pooling below cannot hand it to the
1632 * next one. The connection is pooled with a NULL rbuf and hm_new_conn
1633 * allocates a fresh one. */
1634 /* c is never NULL here - every caller passes a live connection, and this
1635 * function dereferences it unconditionally a few lines down. The `c &&`
1636 * these two guards used to carry was dead, and it told a static analyser
1637 * the opposite of the truth: cppcheck reported four null dereferences
1638 * from it, all of them the guard's fault rather than the code's. */
1639 /*
1640 # uncoverable branch true class:ignore_covered_err
1641 # uncoverable condition left count:1 class:ignore_covered_err */
1642 933 ▶ if (c->recv_inflight) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->recv_inflight) { 0 933 933
1643 /*
1644 # uncoverable statement
1645 # uncoverable branch true
1646 # uncoverable branch false
1647 # uncoverable condition left count:1
1648 # uncoverable condition right count:1 */
1649 0 ▶ if (loop->be->cancel_recv) loop->be->cancel_recv(loop->be, c->fd);
Branch: 1 branch
BranchTrueFalseTotal
if (loop->be->cancel_recv) loop->be->cancel_recv(loop->be, c->fd); 0 0 0
1650 0 hm_quar_push(loop, HM_RECV_TOKEN(c), c->rbuf); /* # uncoverable statement */
1651 0 c->rbuf = NULL; /* # uncoverable statement */
1652 0 c->rcap = 0; /* # uncoverable statement */
1653 0 c->rlen = 0; /* # uncoverable statement */
1654 0 c->recv_inflight = 0; /* # uncoverable statement */
1655 }
1656 933 ▶ if (c->spill_fd >= 0) { /* a body was still draining */
Branch: 1 branch
BranchTrueFalseTotal
if (c->spill_fd >= 0) { /* a body was still draining */ 1 932 933
1657 1 PerlLIO_close(c->spill_fd);
1658 1 c->spill_fd = -1;
1659 1 c->spill_got = 0;
1660 }
1661 933 int fd = c->fd;
1662 933 SV *pending = c->resp_f; /* cancelled below, once state is consistent */
1663 933 c->resp_f = NULL;
1664 933 ▶ loop->be->remove_io(loop->be, fd, HM_EV_READ | (c->writing ? HM_EV_WRITE : 0)); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
loop->be->remove_io(loop->be, fd, HM_EV_READ | (c->writing ? HM_EV_WRITE : 0)); /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 932 933
1665 933 ▶ if (c->bsrc.kind) hm_bsrc_release(aTHX_ &c->bsrc); /* mid-stream abort */
Branch: 1 branch
BranchTrueFalseTotal
if (c->bsrc.kind) hm_bsrc_release(aTHX_ &c->bsrc); /* mid-stream abort */ 1 932 933
1666 933 ▶ if (c->ssl) hm_tls_conn_free(c);
Branch: 1 branch
BranchTrueFalseTotal
if (c->ssl) hm_tls_conn_free(c); 61 872 933
1667 933 hm_os_close(fd);
1668 933 hm_lru_unlink(loop, c);
1669 933 loop->conns[fd] = NULL;
1670 933 loop->nconns--;
1671 933 ▶ if (c->io_sv) { SvREFCNT_dec(c->io_sv); c->io_sv = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->io_sv) { SvREFCNT_dec(c->io_sv); c->io_sv = NULL; } 813 120 933
1672 933 ▶ if (c->cid_sv) { SvREFCNT_dec(c->cid_sv); c->cid_sv = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->cid_sv) { SvREFCNT_dec(c->cid_sv); c->cid_sv = NULL; } 813 120 933
1673 /*
1674 # uncoverable branch true class:ignore_covered_err
1675 # uncoverable condition left count:1 class:ignore_covered_err */
1676 933 ▶ if (c->env_sv) { SvREFCNT_dec(c->env_sv); c->env_sv = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->env_sv) { SvREFCNT_dec(c->env_sv); c->env_sv = NULL; } 0 933 933
1677 933 ▶ if (c->peer_sv) { SvREFCNT_dec(c->peer_sv); c->peer_sv = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->peer_sv) { SvREFCNT_dec(c->peer_sv); c->peer_sv = NULL; } 811 122 933
1678 933 ▶ if (c->peer_host_sv) { SvREFCNT_dec(c->peer_host_sv); c->peer_host_sv = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->peer_host_sv) { SvREFCNT_dec(c->peer_host_sv); c->peer_host_sv = NULL; } 811 122 933
1679 933 ▶ if (c->peer_port_sv) { SvREFCNT_dec(c->peer_port_sv); c->peer_port_sv = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->peer_port_sv) { SvREFCNT_dec(c->peer_port_sv); c->peer_port_sv = NULL; } 811 122 933
1680 933 ▶ if (c->sname_sv) { SvREFCNT_dec(c->sname_sv); c->sname_sv = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->sname_sv) { SvREFCNT_dec(c->sname_sv); c->sname_sv = NULL; } 813 120 933
1681 933 ▶ if (c->sport_sv) { SvREFCNT_dec(c->sport_sv); c->sport_sv = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->sport_sv) { SvREFCNT_dec(c->sport_sv); c->sport_sv = NULL; } 813 120 933
1682 /* Detached from the connection BEFORE it is torn down: freeing the
1683 * session cancels every stream's parked Future, and a continuation
1684 * that fires on that cancel looks the stream up through c->h2. */
1685 933 ▶ if (c->h2) { void *h2 = c->h2; c->h2 = NULL; hm_h2_free(aTHX_ h2); }
Branch: 1 branch
BranchTrueFalseTotal
if (c->h2) { void *h2 = c->h2; c->h2 = NULL; hm_h2_free(aTHX_ h2); } 0 933 933
1686 933 hm_pool_conn(loop, c);
1687 933 ▶ if (loop->stopping && loop->nconns == 0) loop->stop = 1; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (loop->stopping && loop->nconns == 0) loop->stop = 1; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 932 933
if (loop->stopping && loop->nconns == 0) loop->stop = 1; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
1688 /* client went away while awaiting: cancel the response Future so
1689 * in-flight async work can abort (propagates up the then-chain) */
1690 933 ▶ if (pending) {
Branch: 1 branch
BranchTrueFalseTotal
if (pending) { 1 932 933
1691 1 dSP;
1692 1 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 1 1
1693 1 ▶ XPUSHs(pending);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(pending); 0 1 1
1694 1 PUTBACK;
1695 1 call_method("cancel", G_DISCARD | G_EVAL);
1696 1 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 1 0 1
1697 1 SvREFCNT_dec(pending);
1698 }
1699 }
1700  
1701 /* ---- detach: hand a live HTTP/1 connection to the application ------------ *
1702 * The seam a protocol upgrade (WebSocket) needs: the socket stays open and
1703 * becomes the app's, while the server forgets it entirely so the app's own
1704 * io watchers on that fd fire (hm_dispatch routes to loop->io_r/io_w only
1705 * when conns[fd] is NULL).
1706 *
1707 * Two phases, because the callers of the app frame keep using the conn after
1708 * it returns (hm_process's consumed/memmove, hm_deliver's flush):
1709 *
1710 * hm_detach - immediate: validate, disarm the server's watchers so
1711 * no further bytes are pulled in, flag the conn.
1712 * hm_conn_release - deferred: hm_close minus the close(2) and minus the
1713 * watcher removal, run once the caller is done with the
1714 * conn. After it, conns[fd] is NULL and the fd is the
1715 * application's to own and to close.
1716 *
1717 * The app must not have produced any response bytes for this request, and
1718 * returns a sentinel (conventionally [101, [], []]) which is discarded. */
1719 10 static int hm_detach(pTHX_ hm_loop *loop, int fd, UV id) {
1720 hm_conn *c;
1721 #ifdef _WIN32
1722 /* The fd is a CRT wrapper around a SOCKET (hm_win.h). Handing that to
1723 * perl's IO layer to own, after this server stops watching it, has not
1724 * been proven on a real Windows box - so it is refused rather than
1725 * half-supported. A WebSocket tier over this server falls back to
1726 * psgi.streaming, which is its documented second transport. */
1727 (void)loop; (void)fd; (void)id; (void)c;
1728 return -6;
1729 #else
1730 10 ▶ if (fd < 0 || fd >= HM_MAXFD) return -1; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (fd < 0 || fd >= HM_MAXFD) return -1; /* # uncoverable condition left count:1 class:ignore_covered_err */ 10 0 10
if (fd < 0 || fd >= HM_MAXFD) return -1; /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 9 10
1731 9 c = loop->conns[fd];
1732 9 ▶ if (!c || c->id != id) return -1; /* gone, or a stale ticket */ /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!c || c->id != id) return -1; /* gone, or a stale ticket */ /* # uncoverable condition left count:2 class:ignore_covered_err */ 8 1 9
if (!c || c->id != id) return -1; /* gone, or a stale ticket */ /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 8 8
1733 /*
1734 # uncoverable branch true
1735 # uncoverable condition left count:1 class:ignore_covered_err */
1736 8 ▶ if (c->h2) return -2; /* HTTP/2 streams share a conn */
Branch: 1 branch
BranchTrueFalseTotal
if (c->h2) return -2; /* HTTP/2 streams share a conn */ 0 8 8
1737 8 ▶ if (c->ssl) return -3; /* TLS state cannot be handed on */
Branch: 1 branch
BranchTrueFalseTotal
if (c->ssl) return -3; /* TLS state cannot be handed on */ 1 7 8
1738 7 ▶ if (c->wlen != c->woff /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
if (c->wlen != c->woff /* # uncoverable condition left count:2 class:ignore_covered_err */ 6 1 7
1739 7 ▶ || c->bsrc.kind) return -4; /* output still draining */
Branch: 1 branch
BranchTrueFalseTotal
|| c->bsrc.kind) return -4; /* output still draining */ 0 6 6
1740 6 ▶ if (c->detached) return -5; /* already handed over */
Branch: 1 branch
BranchTrueFalseTotal
if (c->detached) return -5; /* already handed over */ 2 4 6
1741 4 loop->be->remove_io(loop->be, fd,
1742 4 ▶ HM_EV_READ | (c->writing ? HM_EV_WRITE : 0)); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
HM_EV_READ | (c->writing ? HM_EV_WRITE : 0)); /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 4 4
1743 4 c->writing = 0;
1744 4 c->detached = 1;
1745 4 return 0;
1746 #endif
1747 }
1748  
1749 /* Deferred half of hm_detach: forget the connection without closing its fd. */
1750 4 static void hm_conn_release(pTHX_ hm_loop *loop, hm_conn *c) {
1751 4 int fd = c->fd;
1752 4 SV *pending = c->resp_f;
1753 /* The fd survives, the connection object does not: a handle naming it
1754 * is as dead as it would be after a close. */
1755 /*
1756 # uncoverable branch true class:ignore_covered_err
1757 # uncoverable condition left count:1 class:ignore_covered_err */
1758 4 ▶ if (hm_stream_live_n) hm_stream_conn_gone(aTHX_ c->fd, c->id);
Branch: 1 branch
BranchTrueFalseTotal
if (hm_stream_live_n) hm_stream_conn_gone(aTHX_ c->fd, c->id); 0 4 4
1759 4 c->resp_f = NULL; /* # uncoverable statement class:ignore_covered_err */
1760 /*
1761 # uncoverable statement class:ignore_covered_err
1762 # uncoverable branch true
1763 # uncoverable condition left count:1 */
1764 4 ▶ if (c->bsrc.kind) hm_bsrc_release(aTHX_ &c->bsrc);
Branch: 1 branch
BranchTrueFalseTotal
if (c->bsrc.kind) hm_bsrc_release(aTHX_ &c->bsrc); 0 4 4
1765 4 hm_lru_unlink(loop, c);
1766 4 loop->conns[fd] = NULL;
1767 4 loop->nconns--;
1768 /*
1769 # uncoverable branch false class:ignore_covered_err
1770 # uncoverable condition right count:1 class:ignore_covered_err */
1771 4 ▶ if (c->io_sv) { SvREFCNT_dec(c->io_sv); c->io_sv = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->io_sv) { SvREFCNT_dec(c->io_sv); c->io_sv = NULL; } 4 0 4
1772 /*
1773 # uncoverable branch false class:ignore_covered_err
1774 # uncoverable condition right count:1 class:ignore_covered_err */
1775 4 ▶ if (c->cid_sv) { SvREFCNT_dec(c->cid_sv); c->cid_sv = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->cid_sv) { SvREFCNT_dec(c->cid_sv); c->cid_sv = NULL; } 4 0 4
1776 /*
1777 # uncoverable branch true class:ignore_covered_err
1778 # uncoverable condition left count:1 class:ignore_covered_err */
1779 4 ▶ if (c->env_sv) { SvREFCNT_dec(c->env_sv); c->env_sv = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->env_sv) { SvREFCNT_dec(c->env_sv); c->env_sv = NULL; } 0 4 4
1780 /*
1781 # uncoverable branch false class:ignore_covered_err
1782 # uncoverable condition right count:1 class:ignore_covered_err */
1783 4 ▶ if (c->peer_sv) { SvREFCNT_dec(c->peer_sv); c->peer_sv = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->peer_sv) { SvREFCNT_dec(c->peer_sv); c->peer_sv = NULL; } 4 0 4
1784 /*
1785 # uncoverable branch false class:ignore_covered_err
1786 # uncoverable condition right count:1 class:ignore_covered_err */
1787 4 ▶ if (c->peer_host_sv) { SvREFCNT_dec(c->peer_host_sv); c->peer_host_sv = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->peer_host_sv) { SvREFCNT_dec(c->peer_host_sv); c->peer_host_sv = NULL; } 4 0 4
1788 /*
1789 # uncoverable branch false class:ignore_covered_err
1790 # uncoverable condition right count:1 class:ignore_covered_err */
1791 4 ▶ if (c->peer_port_sv) { SvREFCNT_dec(c->peer_port_sv); c->peer_port_sv = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->peer_port_sv) { SvREFCNT_dec(c->peer_port_sv); c->peer_port_sv = NULL; } 4 0 4
1792 /*
1793 # uncoverable branch false class:ignore_covered_err
1794 # uncoverable condition right count:1 class:ignore_covered_err */
1795 4 ▶ if (c->sname_sv) { SvREFCNT_dec(c->sname_sv); c->sname_sv = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->sname_sv) { SvREFCNT_dec(c->sname_sv); c->sname_sv = NULL; } 4 0 4
1796 /*
1797 # uncoverable branch false class:ignore_covered_err
1798 # uncoverable condition right count:1 class:ignore_covered_err */
1799 4 ▶ if (c->sport_sv) { SvREFCNT_dec(c->sport_sv); c->sport_sv = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->sport_sv) { SvREFCNT_dec(c->sport_sv); c->sport_sv = NULL; } 4 0 4
1800 4 hm_pool_conn(loop, c);
1801 /*
1802 # uncoverable branch true class:ignore_covered_err
1803 # uncoverable condition left count:1 class:ignore_covered_err
1804 # uncoverable condition left count:2 class:ignore_covered_err
1805 # uncoverable condition right count:2 class:ignore_covered_err */
1806 4 ▶ if (loop->stopping && loop->nconns == 0) loop->stop = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (loop->stopping && loop->nconns == 0) loop->stop = 1; 0 4 4
if (loop->stopping && loop->nconns == 0) loop->stop = 1; 0 0 0
1807 /* the request's own Future, if any, is simply dropped - the app owns
1808 * the socket now, so there is nothing left to cancel it for */
1809 /*
1810 # uncoverable statement class:ignore_covered_err
1811 # uncoverable branch true
1812 # uncoverable condition left count:1 */
1813 4 ▶ if (pending) SvREFCNT_dec(pending);
Branch: 1 branch
BranchTrueFalseTotal
if (pending) SvREFCNT_dec(pending); 0 4 4
1814 4 }
1815  
1816 938 static hm_conn *hm_new_conn(hm_loop *loop, int fd, hm_listener *lst) {
1817 hm_conn *c;
1818 938 ▶ if (loop->pool) {
Branch: 1 branch
BranchTrueFalseTotal
if (loop->pool) { 796 142 938
1819 char *wbuf, *rbuf; size_t wcap, rcap;
1820 796 c = loop->pool;
1821 796 loop->pool = c->pool_next;
1822 796 loop->pool_n--;
1823 796 wbuf = c->wbuf; wcap = c->wcap; /* keep the buffers */
1824 796 rbuf = c->rbuf; rcap = c->rcap;
1825 796 memset(c, 0, sizeof(*c));
1826 796 c->wbuf = wbuf; c->wcap = wcap;
1827 796 c->rbuf = rbuf; c->rcap = rcap;
1828 } else {
1829 142 c = (hm_conn *)hm_xcalloc(1, sizeof(hm_conn));
1830 }
1831 938 ▶ if (!c->rbuf) {
Branch: 1 branch
BranchTrueFalseTotal
if (!c->rbuf) { 148 790 938
1832 148 c->rbuf = (char *)hm_xmalloc(HM_RBUF);
1833 148 c->rcap = HM_RBUF;
1834 }
1835 938 c->fd = fd;
1836 938 c->keepalive = 1;
1837 938 c->spill_fd = -1; /* 0 is a real fd; the zeroed struct is not
1838 * "not spilling" without this */
1839 938 c->spill_got = 0;
1840 938 c->id = ++hm_id_counter;
1841 938 c->loop = loop;
1842 938 c->lst = lst;
1843 938 c->last_active = loop->now;
1844 938 loop->conns[fd] = c;
1845 938 loop->nconns++;
1846 #ifndef HM_HAVE_ACCEPT4
1847 hm_set_nonblock(fd); /* accept4 did it where there is one */
1848 #endif
1849 938 ▶ if (!(lst && lst->is_unix)) { /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(lst && lst->is_unix)) { /* # uncoverable condition left count:1 class:ignore_covered_err */ 938 0 938
if (!(lst && lst->is_unix)) { /* # uncoverable condition left count:1 class:ignore_covered_err */ 936 2 938
1850 936 int one = 1;
1851 936 hm_os_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
1852 }
1853 938 ▶ if (lst && lst->proxy_protocol) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (lst && lst->proxy_protocol) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 938 0 938
if (lst && lst->proxy_protocol) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 14 924 938
1854 /* The PROXY header comes first, on the raw socket, before TLS can
1855 * wrap it and before any request byte is read. Readiness reads for
1856 * life (recv_fallback), the header-phase timeout from now, and no
1857 * TLS until hm_proxy_readable has the client's address. */
1858 14 c->pp_wait = 1;
1859 14 c->recv_fallback = 1;
1860 14 c->req_start = loop->now;
1861 14 loop->be->add_io(loop->be, fd, HM_EV_READ, 0);
1862 }
1863 924 ▶ else if (lst && lst->tls_ctx) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
else if (lst && lst->tls_ctx) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 924 0 924
else if (lst && lst->tls_ctx) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 60 864 924
1864 /* TLS wraps first: the handshake is driven off readiness, and
1865 * hm_arm_recv would decline a TLS connection anyway. The handshake
1866 * is timed as a request header is (req_start): a peer that opens a
1867 * socket and never finishes its ClientHello was holding a slot for
1868 * idle_timeout. A wrap that fails (SSL_new under memory pressure)
1869 * closes rather than serving plaintext on a TLS listener. */
1870 60 c->req_start = loop->now;
1871 /*
1872 # uncoverable branch true
1873 # uncoverable condition left count:1 class:ignore_covered_err */
1874 120 ▶ if (hm_tls_wrap(c, lst->tls_ctx) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (hm_tls_wrap(c, lst->tls_ctx) < 0) { 0 60 60
1875 0 hm_errlog("Hyperman: TLS setup failed for a new connection; closed\n"); /* # uncoverable statement */
1876 0 c->tls_failed = 1; /* hm_accept closes it, with aTHX */ /* # uncoverable statement */
1877 } else {
1878 60 loop->be->add_io(loop->be, fd, HM_EV_READ, 0);
1879 }
1880 }
1881 864 else hm_rearm_read(loop, c);
1882 /* append to LRU tail */
1883 938 c->lru_prev = loop->lru_tail;
1884 938 ▶ if (loop->lru_tail) loop->lru_tail->lru_next = c;
Branch: 1 branch
BranchTrueFalseTotal
if (loop->lru_tail) loop->lru_tail->lru_next = c; 149 789 938
1885 938 loop->lru_tail = c;
1886 938 ▶ if (!loop->lru_head) loop->lru_head = c;
Branch: 1 branch
BranchTrueFalseTotal
if (!loop->lru_head) loop->lru_head = c; 789 149 938
1887 938 return c;
1888 }
1889  
1890 /* ---- HTTP parse ---------------------------------------------------------- */
1891  
1892 /* Find a request header's value in the header block (case-insensitive name).
1893 * Returns a pointer to the (trimmed) value and sets *vlen, or NULL. */
1894 416 static const char *hm_find_header(const char *head, size_t headlen,
1895 const char *name, size_t nlen, size_t *vlen) {
1896 416 const char *p = (const char *)memchr(head, '\n', headlen); /* skip req line */
1897 416 const char *hend = head + headlen;
1898 /*
1899 # uncoverable branch true
1900 # uncoverable condition left count:1 class:ignore_covered_err */
1901 416 ▶ if (!p) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!p) return NULL; 0 416 416
1902 416 p++;
1903 1367 ▶ while (p < hend) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < hend) { 971 396 1367
1904 971 const char *eol = (const char *)memchr(p, '\r', hend - p);
1905 971 ▶ size_t len = eol ? (size_t)(eol - p) : (size_t)(hend - p);
Branch: 1 branch
BranchTrueFalseTotal
size_t len = eol ? (size_t)(eol - p) : (size_t)(hend - p); 568 403 971
1906 971 const char *colon = (const char *)memchr(p, ':', len);
1907 971 ▶ if (colon && (size_t)(colon - p) == nlen
Branch: 2 branches
BranchTrueFalseTotal
if (colon && (size_t)(colon - p) == nlen 971 0 971
if (colon && (size_t)(colon - p) == nlen 40 931 971
1908 40 ▶ && hm_strncasecmp(p, name, nlen) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
&& hm_strncasecmp(p, name, nlen) == 0) { 20 20 40
1909 20 const char *v = colon + 1;
1910 20 size_t vl = (size_t)((p + len) - v);
1911 /*
1912 # uncoverable condition right count:1 class:ignore_covered_err
1913 # uncoverable condition left count:3 class:ignore_covered_err */
1914 40 ▶ while (vl && (*v == ' ' || *v == '\t')) { v++; vl--; }
Branch: 3 branches
BranchTrueFalseTotal
while (vl && (*v == ' ' || *v == '\t')) { v++; vl--; } 40 0 40
while (vl && (*v == ' ' || *v == '\t')) { v++; vl--; } 20 20 40
while (vl && (*v == ' ' || *v == '\t')) { v++; vl--; } 0 20 20
1915 /*
1916 # uncoverable branch true
1917 # uncoverable condition left count:1 class:ignore_covered_err
1918 # uncoverable condition right count:1 class:ignore_covered_err
1919 # uncoverable condition left count:2 class:ignore_covered_err
1920 # uncoverable condition left count:3 class:ignore_covered_err */
1921 20 ▶ while (vl && (v[vl-1] == ' ' || v[vl-1] == '\t')) vl--;
Branch: 3 branches
BranchTrueFalseTotal
while (vl && (v[vl-1] == ' ' || v[vl-1] == '\t')) vl--; 20 0 20
while (vl && (v[vl-1] == ' ' || v[vl-1] == '\t')) vl--; 0 20 20
while (vl && (v[vl-1] == ' ' || v[vl-1] == '\t')) vl--; 0 20 20
1922 20 *vlen = vl;
1923 20 return v;
1924 }
1925 951 ▶ p = eol ? eol + 2 : hend;
Branch: 1 branch
BranchTrueFalseTotal
p = eol ? eol + 2 : hend; 555 396 951
1926 }
1927 396 return NULL;
1928 }
1929  
1930 /* Is "trailers" one of the t-codings in a TE value? The value is a
1931 * comma-separated list, each member a token with optional ;parameters and
1932 * optional whitespace, so each token is compared whole and without regard
1933 * to case. "x-trailers" or "trailersx" is a different token. */
1934 0 static int hm_te_names_trailers(const char *v, size_t vl) {
1935 0 size_t i = 0;
1936 0 ▶ while (i < vl) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < vl) { 0 0 0
1937 size_t start;
1938 0 ▶ while (i < vl && (v[i] == ' ' || v[i] == '\t' || v[i] == ',')) i++;
Branch: 4 branches
BranchTrueFalseTotal
while (i < vl && (v[i] == ' ' || v[i] == '\t' || v[i] == ',')) i++; 0 0 0
while (i < vl && (v[i] == ' ' || v[i] == '\t' || v[i] == ',')) i++; 0 0 0
while (i < vl && (v[i] == ' ' || v[i] == '\t' || v[i] == ',')) i++; 0 0 0
while (i < vl && (v[i] == ' ' || v[i] == '\t' || v[i] == ',')) i++; 0 0 0
1939 0 start = i;
1940 0 ▶ while (i < vl && v[i] != ',' && v[i] != ';' && v[i] != ' ' && v[i] != '\t') i++;
Branch: 5 branches
BranchTrueFalseTotal
while (i < vl && v[i] != ',' && v[i] != ';' && v[i] != ' ' && v[i] != '\t') i++; 0 0 0
while (i < vl && v[i] != ',' && v[i] != ';' && v[i] != ' ' && v[i] != '\t') i++; 0 0 0
while (i < vl && v[i] != ',' && v[i] != ';' && v[i] != ' ' && v[i] != '\t') i++; 0 0 0
while (i < vl && v[i] != ',' && v[i] != ';' && v[i] != ' ' && v[i] != '\t') i++; 0 0 0
while (i < vl && v[i] != ',' && v[i] != ';' && v[i] != ' ' && v[i] != '\t') i++; 0 0 0
1941 0 ▶ if (i - start == 8 && hm_strncasecmp(v + start, "trailers", 8) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (i - start == 8 && hm_strncasecmp(v + start, "trailers", 8) == 0) 0 0 0
if (i - start == 8 && hm_strncasecmp(v + start, "trailers", 8) == 0) 0 0 0
1942 0 return 1;
1943 0 ▶ while (i < vl && v[i] != ',') i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < vl && v[i] != ',') i++; 0 0 0
while (i < vl && v[i] != ',') i++; 0 0 0
1944 }
1945 0 return 0;
1946 }
1947  
1948 /* case-insensitive substring search */
1949 9 static int hm_ci_contains(const char *hay, size_t hl, const char *needle) {
1950 9 size_t nl = strlen(needle), i;
1951 /*
1952 # uncoverable branch true
1953 # uncoverable condition left count:1 class:ignore_covered_err */
1954 9 ▶ if (nl > hl) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (nl > hl) return 0; 0 9 9
1955 36 ▶ for (i = 0; i + nl <= hl; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + nl <= hl; i++) 27 9 36
1956 27 ▶ if (hm_strncasecmp(hay + i, needle, nl) == 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (hm_strncasecmp(hay + i, needle, nl) == 0) return 1; 0 27 27
1957 9 return 0;
1958 }
1959  
1960 /* ---- Expect: 100-continue (RFC 9110 10.1.1) -------------------------------
1961 * A client that expects to send a large body may ask permission first and
1962 * wait for an interim 100 before spending the upload. A server that never
1963 * answers does not break such a client - it stalls it, for however long the
1964 * client is prepared to wait (a second, in curl and in most of the Java
1965 * clients) on every request. So the answer is worth having for its own sake,
1966 * and the refusals are worth having more: a body over max_body is refused
1967 * here, on the headers, rather than after the client has sent it all.
1968 */
1969 #define HM_EXPECT_NONE 0
1970 #define HM_EXPECT_CONTINUE 1
1971 #define HM_EXPECT_OTHER 2
1972  
1973 /* The request line's HTTP version. Only "is it 1.1" is asked: 100-continue
1974 * is an HTTP/1.1 expectation and a 1.0 client's Expect MUST be ignored. */
1975 955 static int hm_req_is_11(const char *head, size_t headlen) {
1976 955 const char *eol = (const char *)memchr(head, '\r', headlen);
1977 955 ▶ size_t ll = eol ? (size_t)(eol - head) : headlen;
Branch: 1 branch
BranchTrueFalseTotal
size_t ll = eol ? (size_t)(eol - head) : headlen; 719 236 955
1978 955 ▶ return ll >= 8 && memcmp(head + ll - 8, "HTTP/1.1", 8) == 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
return ll >= 8 && memcmp(head + ll - 8, "HTTP/1.1", 8) == 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 955 0 955
return ll >= 8 && memcmp(head + ll - 8, "HTTP/1.1", 8) == 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 416 539 955
1979 }
1980  
1981 /* Classify the Expect field. The value is a list, and 100-continue is the
1982 * only member this server understands, so anything else - including
1983 * 100-continue with something else beside it - is an expectation it cannot
1984 * meet and earns a 417 rather than a silent promise it will not keep. */
1985 955 static int hm_expectation(const char *head, size_t headlen) {
1986 955 size_t vl = 0;
1987 const char *v;
1988 955 ▶ if (!hm_req_is_11(head, headlen)) return HM_EXPECT_NONE;
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_req_is_11(head, headlen)) return HM_EXPECT_NONE; 539 416 955
1989 416 v = hm_find_header(head, headlen, "expect", 6, &vl);
1990 416 ▶ if (!v || !vl) return HM_EXPECT_NONE; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!v || !vl) return HM_EXPECT_NONE; /* # uncoverable condition left count:2 class:ignore_covered_err */ 20 396 416
if (!v || !vl) return HM_EXPECT_NONE; /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 20 20
1991 20 ▶ if (vl == 12 && hm_strncasecmp(v, "100-continue", 12) == 0) /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (vl == 12 && hm_strncasecmp(v, "100-continue", 12) == 0) /* # uncoverable condition right count:2 class:ignore_covered_err */ 18 2 20
if (vl == 12 && hm_strncasecmp(v, "100-continue", 12) == 0) /* # uncoverable condition right count:2 class:ignore_covered_err */ 18 0 18
1992 18 return HM_EXPECT_CONTINUE;
1993 2 return HM_EXPECT_OTHER;
1994 }
1995  
1996 /* Send the interim response, once per request. Called only where the headers
1997 * have been accepted AND the body is not all here yet: a request already
1998 * carrying its whole body is answered with its final status instead, which
1999 * RFC 9110 allows and every client handles, and a request that is going to
2000 * be refused is refused before this is ever reached. */
2001 54 static void hm_send_continue(hm_conn *c, int expect) {
2002 static const char cont[] = "HTTP/1.1 100 Continue\r\n\r\n";
2003 54 ▶ if (expect != HM_EXPECT_CONTINUE || c->cont_sent) return;
Branch: 2 branches
BranchTrueFalseTotal
if (expect != HM_EXPECT_CONTINUE || c->cont_sent) return; 10 44 54
if (expect != HM_EXPECT_CONTINUE || c->cont_sent) return; 5 5 10
2004 5 c->cont_sent = 1;
2005 5 hm_wb_put(c, cont, sizeof(cont) - 1);
2006 }
2007  
2008 /* Percent-decode a path once (RFC 3875 PATH_INFO): %XX only, no '+'. */
2009 885 static SV *hm_pct_decode(pTHX_ const char *p, STRLEN n) {
2010 885 SV *sv = newSV(n + 1);
2011 885 char *d = SvPVX(sv);
2012 885 STRLEN i = 0, o = 0;
2013 885 SvPOK_on(sv);
2014 3642 ▶ while (i < n) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < n) { 2757 885 3642
2015 2757 ▶ if (p[i] == '%' && i + 2 < n /* # uncoverable condition right count:4 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (p[i] == '%' && i + 2 < n /* # uncoverable condition right count:4 class:ignore_covered_err */ 11 2746 2757
if (p[i] == '%' && i + 2 < n /* # uncoverable condition right count:4 class:ignore_covered_err */ 10 1 11
2016 10 ▶ && isxdigit((unsigned char)p[i+1]) && isxdigit((unsigned char)p[i+2])) {
Branch: 2 branches
BranchTrueFalseTotal
&& isxdigit((unsigned char)p[i+1]) && isxdigit((unsigned char)p[i+2])) { 9 1 10
&& isxdigit((unsigned char)p[i+1]) && isxdigit((unsigned char)p[i+2])) { 9 0 9
2017 9 int hi = p[i+1], lo = p[i+2];
2018 9 ▶ hi = (hi <= '9') ? hi - '0' : (hi | 32) - 'a' + 10;
Branch: 1 branch
BranchTrueFalseTotal
hi = (hi <= '9') ? hi - '0' : (hi | 32) - 'a' + 10; 8 1 9
2019 9 ▶ lo = (lo <= '9') ? lo - '0' : (lo | 32) - 'a' + 10;
Branch: 1 branch
BranchTrueFalseTotal
lo = (lo <= '9') ? lo - '0' : (lo | 32) - 'a' + 10; 7 2 9
2020 9 d[o++] = (char)((hi << 4) | lo);
2021 9 i += 3;
2022 } else {
2023 2748 d[o++] = p[i++];
2024 }
2025 }
2026 885 d[o] = 0;
2027 885 SvCUR_set(sv, o);
2028 885 return sv;
2029 }
2030  
2031 /* ---- $env fast path ------------------------------------------------------
2032 * The fixed $env keys as shared-string SVs: the hash is computed once and
2033 * cached in the shared string table, so hv_store_ent skips both the hashing
2034 * and the key copy on every request. Values that never vary per process
2035 * ([1,1], the 0/1 flags, psgi.errors) are read-only singletons shared by
2036 * refcount; per-connection constants (REMOTE_*, SERVER_*) are cached on the
2037 * connection. Anything a middleware conventionally assigns in place
2038 * (psgi.url_scheme, SCRIPT_NAME, PATH_INFO) stays a fresh SV per request. */
2039  
2040 enum {
2041 HMK_REQUEST_METHOD, HMK_REQUEST_URI, HMK_PATH_INFO, HMK_QUERY_STRING,
2042 HMK_SERVER_PROTOCOL, HMK_SCRIPT_NAME, HMK_SERVER_NAME, HMK_SERVER_PORT,
2043 HMK_REMOTE_ADDR, HMK_REMOTE_HOST, HMK_REMOTE_PORT,
2044 HMK_PSGI_VERSION, HMK_PSGI_URL_SCHEME, HMK_PSGI_MULTITHREAD,
2045 HMK_PSGI_MULTIPROCESS, HMK_PSGI_RUN_ONCE, HMK_PSGI_STREAMING,
2046 HMK_PSGI_NONBLOCKING, HMK_PSGI_ERRORS, HMK_PSGIX_LOOP, HMK_PSGIX_IO,
2047 HMK_PSGI_INPUT, HMK_PSGIX_INPUT_BUFFERED, HMK_CONTENT_LENGTH,
2048 HMK_CONTENT_TYPE, HMK_PSGIX_HM_CONN, HMK_PSGIX_HM_STREAM, HMK_PSGIX_PROXY_ADDR,
2049 HMK_COUNT
2050 };
2051 static const char *const hm_env_key_name[HMK_COUNT] = {
2052 "REQUEST_METHOD", "REQUEST_URI", "PATH_INFO", "QUERY_STRING",
2053 "SERVER_PROTOCOL", "SCRIPT_NAME", "SERVER_NAME", "SERVER_PORT",
2054 "REMOTE_ADDR", "REMOTE_HOST", "REMOTE_PORT",
2055 "psgi.version", "psgi.url_scheme", "psgi.multithread",
2056 "psgi.multiprocess", "psgi.run_once", "psgi.streaming",
2057 "psgi.nonblocking", "psgi.errors", "psgix.loop", "psgix.io",
2058 "psgi.input", "psgix.input.buffered", "CONTENT_LENGTH",
2059 "CONTENT_TYPE", "psgix.hyperman.conn", "psgix.hyperman.stream",
2060 "psgix.proxy_addr"
2061 };
2062 static SV *hm_env_key[HMK_COUNT];
2063 static SV *hm_env_zero, *hm_env_one; /* read-only 0/1 flag values */
2064 static SV *hm_env_version; /* read-only [1,1] */
2065 static SV *hm_env_errors; /* read-only \*STDERR */
2066  
2067 /* The HTTP_* keys for the headers real traffic actually sends, as shared
2068 * key SVs made once. Storing with a plain char* key makes hv_common hash
2069 * the string and cut a fresh HEK against the shared string table on every
2070 * request, then release it again at env teardown; storing through a shared
2071 * SV reuses the precomputed hash and the one HEK for the process's life.
2072 * A browser request carries a dozen of these, so it pays per header, per
2073 * request. Anything not in this table just takes the char* path it always
2074 * took. Grouped by key length so the lookup is a couple of memcmps. */
2075 static const char *const hm_hdrk_name[] = {
2076 "HTTP_TE", /* 7 */
2077 "HTTP_DNT", /* 8 */
2078 "HTTP_HOST", /* 9 */
2079 "HTTP_RANGE", /* 10 */
2080 "HTTP_ACCEPT", "HTTP_COOKIE", "HTTP_ORIGIN", "HTTP_PRAGMA", /* 11 */
2081 "HTTP_REFERER", "HTTP_UPGRADE", /* 12 */
2082 "HTTP_USER_AGENT", "HTTP_CONNECTION", /* 15 */
2083 "HTTP_AUTHORIZATION", "HTTP_CACHE_CONTROL",
2084 "HTTP_IF_NONE_MATCH", /* 18 */
2085 "HTTP_ACCEPT_ENCODING", "HTTP_ACCEPT_LANGUAGE",
2086 "HTTP_X_FORWARDED_FOR", /* 20 */
2087 "HTTP_X_REQUESTED_WITH", /* 21 */
2088 "HTTP_IF_MODIFIED_SINCE", "HTTP_X_FORWARDED_PROTO",
2089 "HTTP_SEC_WEBSOCKET_KEY", /* 22 */
2090 "HTTP_SEC_WEBSOCKET_VERSION", /* 26 */
2091 NULL
2092 };
2093 #define HM_HDRK_MAXLEN 26
2094 #define HM_HDRK_PERLEN 5
2095 static SV *hm_hdrk_sv[sizeof(hm_hdrk_name) / sizeof(char *)];
2096 /* index+1 of each candidate, per key length; 0 ends the row */
2097 static unsigned char hm_hdrk_by_len[HM_HDRK_MAXLEN + 1][HM_HDRK_PERLEN];
2098  
2099 /* the cached key SV for keybuf/klen, or NULL for the char* fallback */
2100 1040 static SV *hm_hdrk_lookup(const char *keybuf, size_t klen) {
2101 const unsigned char *row;
2102 int i;
2103 /*
2104 # uncoverable branch true
2105 # uncoverable condition left count:1 class:ignore_covered_err */
2106 1040 ▶ if (klen > HM_HDRK_MAXLEN) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (klen > HM_HDRK_MAXLEN) return NULL; 0 1040 1040
2107 1040 row = hm_hdrk_by_len[klen];
2108 1339 ▶ for (i = 0; i < HM_HDRK_PERLEN && row[i]; i++) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < HM_HDRK_PERLEN && row[i]; i++) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 1339 0 1339
for (i = 0; i < HM_HDRK_PERLEN && row[i]; i++) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 1218 121 1339
2109 1218 int idx = row[i] - 1;
2110 1218 ▶ if (memcmp(keybuf, hm_hdrk_name[idx], klen) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(keybuf, hm_hdrk_name[idx], klen) == 0) 919 299 1218
2111 919 return hm_hdrk_sv[idx];
2112 }
2113 121 return NULL;
2114 }
2115  
2116 /* The two bytes a repeated request header folds with. PSGI joins with the
2117 * comma of a list header (RFC 9110 5.3). Cookie is the one request header
2118 * whose list is "; " (RFC 6265 4.2.1), and an HTTP/2 client may send each
2119 * cookie as its own field (RFC 9113 8.2.3), which browsers do: folded with
2120 * a comma the session cookie's value grows a tail and its signature fails
2121 * on every request. The same fold serves HTTP/1.1, where a client that
2122 * splits Cookie is out of spec but the app is still owed one value. */
2123 1 static const char *hm_hdr_join_sep(const char *keybuf, size_t klen) {
2124 1 ▶ return (klen == 11 && memcmp(keybuf, "HTTP_COOKIE", 11) == 0) ? "; " : ", "; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
return (klen == 11 && memcmp(keybuf, "HTTP_COOKIE", 11) == 0) ? "; " : ", "; /* # uncoverable condition right count:2 class:ignore_covered_err */ 0 1 1
return (klen == 11 && memcmp(keybuf, "HTTP_COOKIE", 11) == 0) ? "; " : ", "; /* # uncoverable condition right count:2 class:ignore_covered_err */ 0 0 0
2125 }
2126  
2127 885 static void hm_env_init(pTHX) {
2128 int i;
2129 AV *ver;
2130 885 ▶ if (hm_env_key[0]) return;
Branch: 1 branch
BranchTrueFalseTotal
if (hm_env_key[0]) return; 796 89 885
2131 2581 ▶ for (i = 0; i < HMK_COUNT; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < HMK_COUNT; i++) 2492 89 2581
2132 2492 hm_env_key[i] = newSVpvn_share(hm_env_key_name[i],
2133 strlen(hm_env_key_name[i]), 0);
2134 89 hm_env_zero = newSViv(0); SvREADONLY_on(hm_env_zero);
2135 89 hm_env_one = newSViv(1); SvREADONLY_on(hm_env_one);
2136 89 ver = newAV();
2137 89 av_push(ver, newSViv(1)); av_push(ver, newSViv(1));
2138 89 SvREADONLY_on((SV *)ver);
2139 89 hm_env_version = newRV_noinc((SV *)ver); SvREADONLY_on(hm_env_version);
2140 89 hm_env_errors = newRV_inc((SV *)PL_stderrgv); SvREADONLY_on(hm_env_errors);
2141  
2142 2136 ▶ for (i = 0; hm_hdrk_name[i]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; hm_hdrk_name[i]; i++) { 2047 89 2136
2143 2047 size_t l = strlen(hm_hdrk_name[i]);
2144 int j;
2145 2047 hm_hdrk_sv[i] = newSVpvn_share(hm_hdrk_name[i], (I32)l, 0);
2146 /*
2147 # uncoverable branch false class:ignore_covered_err
2148 # uncoverable condition right count:1 class:ignore_covered_err */
2149 3560 ▶ for (j = 0; j < HM_HDRK_PERLEN; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < HM_HDRK_PERLEN; j++) { 3560 0 3560
2150 3560 ▶ if (!hm_hdrk_by_len[l][j]) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_hdrk_by_len[l][j]) { 2047 1513 3560
2151 2047 hm_hdrk_by_len[l][j] = (unsigned char)(i + 1);
2152 2047 break;
2153 }
2154 }
2155 }
2156 }
2157  
2158 #define hm_env_store(env, k, val) \
2159 (void)hv_store_ent((env), hm_env_key[(k)], (val), 0)
2160  
2161 /* Build a full PSGI env from the request line, the header lines indexed by
2162 * idx (from hm_parse_index) and, if present, the request body. Sets
2163 * c->keepalive from the request. */
2164 885 static HV *hm_build_env(pTHX_ char *head, size_t headlen,
2165 const char *body, long clen, hm_conn *c,
2166 const hm_hline *idx, int nlines) {
2167 885 HV *env = newHV();
2168 885 hm_env_init(aTHX);
2169  
2170 /* A fresh HV starts with 8 buckets and this function stores 25-odd keys
2171 * before the app adds its own, so left alone every request pays two
2172 * bucket-split reallocs (8 -> 16 -> 32) plus the rehash walk each one
2173 * does. Ask for the final size up front. 64 rather than 32: a typical
2174 * browser request carries a dozen HTTP_* headers on top of ours, and
2175 * middleware adds more. */
2176 885 hv_ksplit(env, 64);
2177  
2178 /* request line */
2179 885 char *line_end = (char *)memchr(head, '\r', headlen);
2180 885 ▶ size_t ll = line_end ? (size_t)(line_end - head) : headlen;
Branch: 1 branch
BranchTrueFalseTotal
size_t ll = line_end ? (size_t)(line_end - head) : headlen; 649 236 885
2181 885 char *sp1 = (char *)memchr(head, ' ', ll);
2182 885 const char *method = head; STRLEN mlen = ll;
2183 885 const char *uri = "/"; STRLEN ulen = 1;
2184 885 const char *proto = "HTTP/1.0"; STRLEN plen = 8;
2185 /*
2186 # uncoverable branch false class:ignore_covered_err
2187 # uncoverable condition right count:1 class:ignore_covered_err */
2188 885 ▶ if (sp1) {
Branch: 1 branch
BranchTrueFalseTotal
if (sp1) { 885 0 885
2189 885 mlen = (STRLEN)(sp1 - head);
2190 885 char *u = sp1 + 1;
2191 885 char *sp2 = (char *)memchr(u, ' ', ll - (u - head));
2192 /*
2193 # uncoverable branch false
2194 # uncoverable condition right count:1 class:ignore_covered_err */
2195 885 ▶ if (sp2) {
Branch: 1 branch
BranchTrueFalseTotal
if (sp2) { 885 0 885
2196 885 uri = u; ulen = (STRLEN)(sp2 - u);
2197 885 proto = sp2 + 1; plen = (STRLEN)((head + ll) - (sp2 + 1));
2198 0 } else { uri = u; ulen = (STRLEN)((head + ll) - u); } /* # uncoverable statement class:ignore_covered_err */
2199 }
2200 885 const char *qmark = (const char *)memchr(uri, '?', ulen);
2201 885 const char *path = uri; STRLEN path_l = ulen;
2202 885 const char *qs = ""; STRLEN qs_l = 0;
2203 885 ▶ if (qmark) { path_l = (STRLEN)(qmark - uri); qs = qmark + 1; qs_l = ulen - path_l - 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (qmark) { path_l = (STRLEN)(qmark - uri); qs = qmark + 1; qs_l = ulen - path_l - 1; } 14 871 885
2204  
2205 885 hm_env_store(env, HMK_REQUEST_METHOD, newSVpvn(method, mlen));
2206 885 hm_env_store(env, HMK_REQUEST_URI, newSVpvn(uri, ulen));
2207 885 hm_env_store(env, HMK_PATH_INFO, hm_pct_decode(aTHX_ path, path_l));
2208 885 hm_env_store(env, HMK_QUERY_STRING, newSVpvn(qs, qs_l));
2209 885 hm_env_store(env, HMK_SERVER_PROTOCOL, newSVpvn(proto, plen));
2210 885 hm_env_store(env, HMK_SCRIPT_NAME, newSVpvs(""));
2211 885 ▶ c->is_head = (mlen == 4 && memcmp(method, "HEAD", 4) == 0);
Branch: 2 branches
BranchTrueFalseTotal
c->is_head = (mlen == 4 && memcmp(method, "HEAD", 4) == 0); 36 849 885
c->is_head = (mlen == 4 && memcmp(method, "HEAD", 4) == 0); 4 32 36
2212 885 ▶ if (!c->sname_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (!c->sname_sv) { 818 67 885
2213 /*
2214 # uncoverable condition right count:1 class:ignore_covered_err
2215 # uncoverable condition right count:2 class:ignore_covered_err */
2216 818 ▶ c->sname_sv = newSVpv(c->lst && c->lst->host ? c->lst->host : "0.0.0.0", 0);
Branch: 2 branches
BranchTrueFalseTotal
c->sname_sv = newSVpv(c->lst && c->lst->host ? c->lst->host : "0.0.0.0", 0); 818 0 818
c->sname_sv = newSVpv(c->lst && c->lst->host ? c->lst->host : "0.0.0.0", 0); 818 0 818
2217 818 ▶ c->sport_sv = newSViv(c->lst ? c->lst->port : 0); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
c->sport_sv = newSViv(c->lst ? c->lst->port : 0); /* # uncoverable condition right count:1 class:ignore_covered_err */ 818 0 818
2218 }
2219 /* Copies, not the cached SVs themselves. `$env->{REMOTE_ADDR} = $ip`
2220 * - which is what a reverse-proxy middleware does - assigns INTO the
2221 * value SV, and a shared one would then carry the forwarded address
2222 * into every later request on this keep-alive connection, before any
2223 * middleware had run on them. newSVsv shares the buffer copy-on-write
2224 * on perls that can, so the copy costs an SV head and no string. */
2225 885 hm_env_store(env, HMK_SERVER_NAME, newSVsv(c->sname_sv));
2226 885 hm_env_store(env, HMK_SERVER_PORT, newSVsv(c->sport_sv));
2227 885 ▶ if (c->peer[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->peer[0]) { 883 2 885
2228 883 ▶ if (!c->peer_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (!c->peer_sv) { 816 67 883
2229 816 c->peer_sv = newSVpv(c->peer, 0);
2230 816 c->peer_host_sv = newSVpv(c->peer, 0);
2231 }
2232 883 hm_env_store(env, HMK_REMOTE_ADDR, newSVsv(c->peer_sv));
2233 883 hm_env_store(env, HMK_REMOTE_HOST, newSVsv(c->peer_host_sv));
2234 }
2235 885 ▶ if (c->peer_port) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->peer_port) { 883 2 885
2236 883 ▶ if (!c->peer_port_sv) c->peer_port_sv = newSViv(c->peer_port);
Branch: 1 branch
BranchTrueFalseTotal
if (!c->peer_port_sv) c->peer_port_sv = newSViv(c->peer_port); 816 67 883
2237 883 hm_env_store(env, HMK_REMOTE_PORT, newSVsv(c->peer_port_sv));
2238 }
2239 /* Per-request copies, not the shared read-only SVs: an assignment into
2240 * the slot writes INTO the SV there, and Plack::Middleware::BufferedStreaming
2241 * does `$env->{'psgi.streaming'} = 1`, which died "Modification of a
2242 * read-only value" and made every request a 500 under it. The version
2243 * copy is a fresh RV onto the one read-only array. */
2244 885 hm_env_store(env, HMK_PSGI_VERSION, newSVsv(hm_env_version));
2245 885 ▶ hm_env_store(env, HMK_PSGI_URL_SCHEME,
Branch: 1 branch
BranchTrueFalseTotal
hm_env_store(env, HMK_PSGI_URL_SCHEME, 47 838 885
2246 c->ssl ? newSVpvs("https") : newSVpvs("http"));
2247 885 hm_env_store(env, HMK_PSGI_MULTITHREAD, newSViv(0));
2248 885 hm_env_store(env, HMK_PSGI_MULTIPROCESS, newSViv(1));
2249 885 hm_env_store(env, HMK_PSGI_RUN_ONCE, newSViv(0));
2250 885 hm_env_store(env, HMK_PSGI_STREAMING, newSViv(1));
2251 885 hm_env_store(env, HMK_PSGI_NONBLOCKING, newSViv(1));
2252 /* A copy: `$env->{'psgi.errors'} = $fh` is an assignment INTO the slot's
2253 * SV, and the shared one is read-only, so an application redirecting
2254 * its errors died with "Modification of a read-only value". */
2255 885 hm_env_store(env, HMK_PSGI_ERRORS, newSVsv(hm_env_errors));
2256 885 ▶ if (c->loop->log_wants_t0) /* %D / %T in the log format */
Branch: 1 branch
BranchTrueFalseTotal
if (c->loop->log_wants_t0) /* %D / %T in the log format */ 55 830 885
2257 55 (void)hv_stores(env, "psgix.request_start", newSVnv((NV)hm_now_ns() / 1e9));
2258  
2259 /* psgix.loop: this worker's loop, so apps/clients can create loop
2260 * Futures; psgix.io: the client socket, dup'd so an app that
2261 * hijacks it keeps the socket alive after we close our fd. */
2262 885 hm_env_store(env, HMK_PSGIX_LOOP, hm_loop_to_sv(aTHX_ c->loop));
2263 885 ▶ if (!c->io_sv) c->io_sv = hm_fh_for_fd(aTHX_ c->fd);
Branch: 1 branch
BranchTrueFalseTotal
if (!c->io_sv) c->io_sv = hm_fh_for_fd(aTHX_ c->fd); 818 67 885
2264 885 hm_env_store(env, HMK_PSGIX_IO, SvREFCNT_inc(c->io_sv));
2265 /* psgix.hyperman.conn: [fd, generation id] - the ticket an app hands
2266 * back to Hyperman::detach (or the ABI's conn_detach) to take this
2267 * socket over for a protocol upgrade. HTTP/1 only; the generation id
2268 * makes a stale ticket a no-op rather than a wrong-connection hit. */
2269 885 ▶ if (!c->cid_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (!c->cid_sv) { 818 67 885
2270 818 AV *cid = newAV();
2271 818 av_extend(cid, 2);
2272 818 av_store(cid, 0, newSViv(c->fd));
2273 818 av_store(cid, 1, newSVuv(c->id));
2274 /* The third element is the stream id, and -1 is "the whole
2275 * connection is the stream" - which is what HTTP/1.1 is. It rides on
2276 * the same cached arrayref rather than a second one, so
2277 * psgix.hyperman.stream costs a store and not an allocation. */
2278 818 av_store(cid, 2, newSViv(-1));
2279 818 c->cid_sv = newRV_noinc((SV *)cid);
2280 }
2281 885 hm_env_store(env, HMK_PSGIX_HM_CONN, SvREFCNT_inc(c->cid_sv));
2282 885 hm_env_store(env, HMK_PSGIX_HM_STREAM, SvREFCNT_inc(c->cid_sv));
2283 885 ▶ if (c->proxy_addr[0]) /* the address that connected */
Branch: 1 branch
BranchTrueFalseTotal
if (c->proxy_addr[0]) /* the address that connected */ 10 875 885
2284 10 hm_env_store(env, HMK_PSGIX_PROXY_ADDR, newSVpv(c->proxy_addr, 0));
2285 885 ▶ if (c->ssl) hm_tls_env(aTHX_ c, env);
Branch: 1 branch
BranchTrueFalseTotal
if (c->ssl) hm_tls_env(aTHX_ c, env); 47 838 885
2286  
2287 885 ▶ c->keepalive = (plen == 8 && memcmp(proto, "HTTP/1.1", 8) == 0); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
c->keepalive = (plen == 8 && memcmp(proto, "HTTP/1.1", 8) == 0); /* # uncoverable condition right count:1 class:ignore_covered_err */ 885 0 885
c->keepalive = (plen == 8 && memcmp(proto, "HTTP/1.1", 8) == 0); /* # uncoverable condition right count:1 class:ignore_covered_err */ 359 526 885
2288 885 c->req_11 = c->keepalive; /* before Connection: close can clear it */
2289 /* Per request, not per connection: a keep-alive connection may send a
2290 * different Accept-Encoding on its next request, and the response path
2291 * has no env to read by the time it needs this. One bit, set below. */
2292 885 c->accepts_gzip = 0;
2293 885 c->te_trailers = 0;
2294  
2295 /* header lines, from the framing pass's index (no rescan) */
2296 char keybuf[300];
2297 885 int have_cl = 0;
2298 int li;
2299 1970 ▶ for (li = 0; li < nlines; li++) {
Branch: 1 branch
BranchTrueFalseTotal
for (li = 0; li < nlines; li++) { 1085 885 1970
2300 1085 const char *p = head + idx[li].off;
2301 1085 size_t nk = idx[li].klen;
2302 1085 const char *vp = head + idx[li].voff;
2303 1085 size_t nv = idx[li].vlen;
2304  
2305 /*
2306 # uncoverable branch true
2307 # uncoverable condition left count:1 class:ignore_covered_err */
2308 1085 ▶ if (nk + 5 >= sizeof(keybuf)) continue; /* absurd name: not a header
Branch: 1 branch
BranchTrueFalseTotal
if (nk + 5 >= sizeof(keybuf)) continue; /* absurd name: not a header 0 1085 1085
2309 we would have stored before
2310 either */
2311 /* Fold to the canonical HTTP_ form first, then every name test below
2312 * is a memcmp on known-case bytes rather than a strncasecmp on the
2313 * wire bytes - three case-blind scans per header gone. The fold maps
2314 * '-' to '_', which would let a literal "Content_Length" alias the
2315 * real thing, so the special names also check the wire byte at the
2316 * hyphen position, exactly as the old raw-name compare required. */
2317 {
2318 size_t i;
2319 1085 int underscore = 0;
2320 1085 memcpy(keybuf, "HTTP_", 5);
2321 7927 ▶ for (i = 0; i < nk; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nk; i++) { 6842 1085 7927
2322 6842 unsigned char ch = (unsigned char)p[i];
2323 6842 ▶ if (ch >= 'a' && ch <= 'z') ch -= 32; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (ch >= 'a' && ch <= 'z') ch -= 32; /* # uncoverable condition right count:2 class:ignore_covered_err */ 5088 1754 6842
if (ch >= 'a' && ch <= 'z') ch -= 32; /* # uncoverable condition right count:2 class:ignore_covered_err */ 5088 0 5088
2324 1754 ▶ else if (ch == '-') ch = '_';
Branch: 1 branch
BranchTrueFalseTotal
else if (ch == '-') ch = '_'; 266 1488 1754
2325 1488 ▶ else if (ch == '_') underscore = 1;
Branch: 1 branch
BranchTrueFalseTotal
else if (ch == '_') underscore = 1; 3 1485 1488
2326 6842 keybuf[5 + i] = (char)ch;
2327 }
2328 /* A wire name with an underscore folds to the same env key as
2329 * the hyphenated name, so "X_Forwarded_For" would land in
2330 * HTTP_X_FORWARDED_FOR beside - or instead of - the header a
2331 * proxy in front vetted. Proxies strip the hyphenated form and
2332 * pass the underscored one untouched, which is the whole
2333 * attack. Dropped, as nginx and Apache do. */
2334 1085 ▶ if (underscore) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (underscore) continue; 2 1083 1085
2335 }
2336 /* Accept-Encoding is answered on this one pass and remembered as a
2337 * bit on the connection: hm_queue_response has no env to consult by
2338 * the time it must decide. Not an arm of the chain below - the
2339 * header is still stored as HTTP_ACCEPT_ENCODING like any other. */
2340 /*
2341 # uncoverable condition right count:2 class:ignore_covered_err
2342 # uncoverable condition right count:3 class:ignore_covered_err
2343 # uncoverable condition right count:4 class:ignore_covered_err */
2344 1083 ▶ if (nk == 15 && p[6] == '-' && c->loop->compress
Branch: 3 branches
BranchTrueFalseTotal
if (nk == 15 && p[6] == '-' && c->loop->compress 17 1066 1083
if (nk == 15 && p[6] == '-' && c->loop->compress 17 0 17
if (nk == 15 && p[6] == '-' && c->loop->compress 17 0 17
2345 17 ▶ && memEQ(keybuf + 5, "ACCEPT_ENCODING", 15))
Branch: 1 branch
BranchTrueFalseTotal
&& memEQ(keybuf + 5, "ACCEPT_ENCODING", 15)) 17 0 17
2346 17 c->accepts_gzip = (unsigned char)hz_accepts_gzip(vp, nv);
2347 /* TE is read the same way, into one bit, and still stored as HTTP_TE.
2348 * Several TE lines form one list, so the bit only ever goes on. */
2349 1083 ▶ if (nk == 2 && memEQ(keybuf + 5, "TE", 2) && hm_te_names_trailers(vp, nv))
Branch: 3 branches
BranchTrueFalseTotal
if (nk == 2 && memEQ(keybuf + 5, "TE", 2) && hm_te_names_trailers(vp, nv)) 0 1083 1083
if (nk == 2 && memEQ(keybuf + 5, "TE", 2) && hm_te_names_trailers(vp, nv)) 0 0 0
if (nk == 2 && memEQ(keybuf + 5, "TE", 2) && hm_te_names_trailers(vp, nv)) 0 0 0
2350 0 c->te_trailers = 1;
2351  
2352 1083 ▶ if (nk == 14 && p[7] == '-' && memEQ(keybuf + 5, "CONTENT_LENGTH", 14)) { /* # uncoverable condition right count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (nk == 14 && p[7] == '-' && memEQ(keybuf + 5, "CONTENT_LENGTH", 14)) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 22 1061 1083
if (nk == 14 && p[7] == '-' && memEQ(keybuf + 5, "CONTENT_LENGTH", 14)) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 22 0 22
if (nk == 14 && p[7] == '-' && memEQ(keybuf + 5, "CONTENT_LENGTH", 14)) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 22 0 22
2353 22 hm_env_store(env, HMK_CONTENT_LENGTH, newSVpvn(vp, nv));
2354 22 have_cl = 1;
2355 1061 ▶ } else if (nk == 12 && p[7] == '-' /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
} else if (nk == 12 && p[7] == '-' /* # uncoverable condition right count:2 class:ignore_covered_err */ 48 1013 1061
} else if (nk == 12 && p[7] == '-' /* # uncoverable condition right count:2 class:ignore_covered_err */ 48 0 48
2356 48 ▶ && memEQ(keybuf + 5, "CONTENT_TYPE", 12)) {
Branch: 1 branch
BranchTrueFalseTotal
&& memEQ(keybuf + 5, "CONTENT_TYPE", 12)) { 11 37 48
2357 11 hm_env_store(env, HMK_CONTENT_TYPE, newSVpvn(vp, nv));
2358 /*
2359 # uncoverable condition right count:2 class:ignore_covered_err
2360 # uncoverable condition right count:3 class:ignore_covered_err */
2361 1050 ▶ } else if (nk == 17 && p[8] == '-'
Branch: 2 branches
BranchTrueFalseTotal
} else if (nk == 17 && p[8] == '-' 10 1040 1050
} else if (nk == 17 && p[8] == '-' 10 0 10
2362 10 ▶ && memEQ(keybuf + 5, "TRANSFER_ENCODING", 17)) {
Branch: 1 branch
BranchTrueFalseTotal
&& memEQ(keybuf + 5, "TRANSFER_ENCODING", 17)) { 10 0 10
2363 /* consumed by framing (already dechunked); not exposed to the
2364 * app, whose psgi.input is the decoded body with CONTENT_LENGTH */
2365 } else {
2366 {
2367 /* repeated header: join values (hm_hdr_join_sep) */
2368 1040 SV *ksv = hm_hdrk_lookup(keybuf, nk + 5);
2369 1040 ▶ if (ksv) {
Branch: 1 branch
BranchTrueFalseTotal
if (ksv) { 919 121 1040
2370 /* a common header: the shared key SV carries its hash
2371 * and its HEK, so no hashing and no HEK churn */
2372 919 HE *he = hv_fetch_ent(env, ksv, 0, 0);
2373 919 ▶ if (he) {
Branch: 1 branch
BranchTrueFalseTotal
if (he) { 0 919 919
2374 0 sv_catpvn(HeVAL(he), hm_hdr_join_sep(keybuf, nk + 5), 2);
2375 0 sv_catpvn(HeVAL(he), vp, nv);
2376 } else {
2377 919 (void)hv_store_ent(env, ksv, newSVpvn(vp, nv), 0);
2378 }
2379 } else {
2380 121 SV **old = hv_fetch(env, keybuf, (I32)(nk + 5), 0);
2381 121 ▶ if (old) {
Branch: 1 branch
BranchTrueFalseTotal
if (old) { 1 120 121
2382 1 sv_catpvn(*old, hm_hdr_join_sep(keybuf, nk + 5), 2);
2383 1 sv_catpvn(*old, vp, nv);
2384 } else {
2385 120 hv_store(env, keybuf, (I32)(nk + 5), newSVpvn(vp, nv), 0);
2386 }
2387 }
2388 }
2389 /* the name is already canonical; only the value keeps wire case */
2390 1040 ▶ if (nk == 10 && memEQ(keybuf + 5, "CONNECTION", 10)) {
Branch: 2 branches
BranchTrueFalseTotal
if (nk == 10 && memEQ(keybuf + 5, "CONNECTION", 10)) { 235 805 1040
if (nk == 10 && memEQ(keybuf + 5, "CONNECTION", 10)) { 177 58 235
2391 177 ▶ if (nv >= 5 && hm_strncasecmp(vp, "close", 5) == 0) c->keepalive = 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (nv >= 5 && hm_strncasecmp(vp, "close", 5) == 0) c->keepalive = 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 177 0 177
if (nv >= 5 && hm_strncasecmp(vp, "close", 5) == 0) c->keepalive = 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 168 9 177
2392 /*
2393 # uncoverable branch true class:ignore_covered_err
2394 # uncoverable condition left count:1 class:ignore_covered_err
2395 # uncoverable condition left count:2 class:ignore_covered_err */
2396 9 ▶ else if (nv >= 10 && hm_strncasecmp(vp, "keep-alive", 10) == 0) c->keepalive = 1;
Branch: 2 branches
BranchTrueFalseTotal
else if (nv >= 10 && hm_strncasecmp(vp, "keep-alive", 10) == 0) c->keepalive = 1; 0 9 9
else if (nv >= 10 && hm_strncasecmp(vp, "keep-alive", 10) == 0) c->keepalive = 1; 0 0 0
2397 }
2398 }
2399 }
2400 /* dechunked request: no Content-Length header, but we know the decoded
2401 * length - present it so apps/middleware see the real body size */
2402 885 ▶ if (!have_cl && clen > 0)
Branch: 2 branches
BranchTrueFalseTotal
if (!have_cl && clen > 0) 863 22 885
if (!have_cl && clen > 0) 10 853 863
2403 10 hm_env_store(env, HMK_CONTENT_LENGTH, newSViv((IV)clen));
2404  
2405 889 ▶ if (c && c->spill_fd >= 0) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (c && c->spill_fd >= 0) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 885 0 885
if (c && c->spill_fd >= 0) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 4 881 885
2406 /* The body was drained to a file as it arrived, so there is no body
2407 * pointer to copy from: the descriptor IS psgi.input. The handle owns
2408 * it from here, and the connection stops tracking it. */
2409 4 SV *in = hm_input_from_fd(aTHX_ c->spill_fd);
2410 4 c->spill_fd = -1;
2411 4 c->spill_got = 0;
2412 4 ▶ hm_env_store(env, HMK_PSGI_INPUT,
Branch: 1 branch
BranchTrueFalseTotal
hm_env_store(env, HMK_PSGI_INPUT, 0 4 4
2413 in ? in : hm_empty_input_sv(aTHX));
2414 }
2415 881 ▶ else if (clen > 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (clen > 0) 27 854 881
2416 27 hm_env_store(env, HMK_PSGI_INPUT, hm_new_input(aTHX_ body, (STRLEN)clen));
2417 else
2418 854 hm_env_store(env, HMK_PSGI_INPUT, hm_empty_input_sv(aTHX));
2419 885 hm_env_store(env, HMK_PSGIX_INPUT_BUFFERED, SvREFCNT_inc(hm_env_one));
2420 /* The flag promises only what the framing can deliver: a streamed body
2421 * is chunked on an HTTP/1.1 keep-alive connection and nowhere else, and
2422 * only a chunked body has room for trailers. An application that then
2423 * supplies its own Content-Length gives that room away again, see
2424 * hm_stream_finish. */
2425 885 ▶ if (c->te_trailers && c->req_11 && c->keepalive)
Branch: 3 branches
BranchTrueFalseTotal
if (c->te_trailers && c->req_11 && c->keepalive) 0 885 885
if (c->te_trailers && c->req_11 && c->keepalive) 0 0 0
if (c->te_trailers && c->req_11 && c->keepalive) 0 0 0
2426 0 hv_stores(env, "psgix.hyperman.trailers", SvREFCNT_inc(hm_env_one));
2427  
2428 885 return env;
2429 }
2430  
2431 /* ---- app invocation ------------------------------------------------------ */
2432  
2433 /* Call the app with an env we keep alive (access_log needs it afterwards). */
2434 170 static SV *hm_call_app(pTHX_ hm_loop *loop, SV *env_rv) {
2435 170 dSP;
2436 170 SV *resp = NULL;
2437 int count;
2438 170 ENTER; SAVETMPS;
2439 170 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 170 170
2440 170 ▶ XPUSHs(sv_2mortal(SvREFCNT_inc(env_rv)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(SvREFCNT_inc(env_rv))); 0 170 170
2441 170 PUTBACK;
2442 170 count = call_sv(loop->app, G_SCALAR | G_EVAL);
2443 170 SPAGAIN;
2444 170 ▶ if (!SvTRUE(ERRSV) && count > 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && count > 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 170 0 170
if (!SvTRUE(ERRSV) && count > 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 169 1 170
if (!SvTRUE(ERRSV) && count > 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 169 0 169
2445 169 resp = POPs;
2446 169 SvREFCNT_inc(resp);
2447 /*
2448 # uncoverable branch false class:ignore_covered_err
2449 # uncoverable condition right count:1 class:ignore_covered_err */
2450 1 ▶ } else if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (count > 0) { 1 0 1
2451 1 (void)POPs;
2452 }
2453 170 hm_warn_died(aTHX_ "application");
2454 170 PUTBACK;
2455 170 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 170 0 170
2456 170 return resp;
2457 }
2458  
2459 /* No-logger fast path: the env is handed to the call and freed with it. */
2460 713 static SV *hm_call_app0(pTHX_ hm_loop *loop, HV *env) {
2461 713 dSP;
2462 713 SV *resp = NULL;
2463 int count;
2464 713 ENTER; SAVETMPS;
2465 713 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 713 713
2466 713 ▶ XPUSHs(sv_2mortal(newRV_noinc((SV *)env)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_noinc((SV *)env))); 0 713 713
2467 713 PUTBACK;
2468 713 count = call_sv(loop->app, G_SCALAR | G_EVAL);
2469 713 SPAGAIN;
2470 713 ▶ if (!SvTRUE(ERRSV) && count > 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && count > 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 713 0 713
if (!SvTRUE(ERRSV) && count > 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 711 2 713
if (!SvTRUE(ERRSV) && count > 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 711 0 711
2471 711 resp = POPs;
2472 711 SvREFCNT_inc(resp);
2473 /*
2474 # uncoverable branch false class:ignore_covered_err
2475 # uncoverable condition right count:1 class:ignore_covered_err */
2476 2 ▶ } else if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (count > 0) { 2 0 2
2477 2 (void)POPs;
2478 }
2479 713 hm_warn_died(aTHX_ "application");
2480 713 PUTBACK;
2481 713 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 713 0 713
2482 713 return resp;
2483 }
2484  
2485 /* Is any access logging configured for this loop? Guards the (small) cost of
2486 * keeping the env alive past the response purely for the logger. */
2487 #define hm_logging(loop) ((loop)->log_cb || (loop)->log_fd >= 0)
2488  
2489 /* ---- fast default access-log writer (Combined Log Format) ----------------
2490 * When access_log is a path/handle rather than a coderef, log lines are
2491 * formatted and buffered in C and flushed once per loop wakeup - no Perl
2492 * call on the hot path. The fd is shared (inherited) across workers and
2493 * opened O_APPEND, so whole-line writes stay atomic between them. */
2494  
2495 #define HM_LOG_FLUSH_AT (32 * 1024) /* flush the buffer once it grows past */
2496  
2497 75 static void hm_log_flush(hm_loop *loop) {
2498 75 size_t off = 0;
2499 75 ▶ if (loop->log_fd < 0 || loop->log_len == 0) return; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (loop->log_fd < 0 || loop->log_len == 0) return; /* # uncoverable condition left count:1 class:ignore_covered_err */ 75 0 75
if (loop->log_fd < 0 || loop->log_len == 0) return; /* # uncoverable condition left count:1 class:ignore_covered_err */ 8 67 75
2500 134 ▶ while (off < loop->log_len) {
Branch: 1 branch
BranchTrueFalseTotal
while (off < loop->log_len) { 67 67 134
2501 67 ssize_t n = hm_os_write_file(loop->log_fd, loop->log_buf + off,
2502 loop->log_len - off);
2503 /*
2504 # uncoverable branch false
2505 # uncoverable condition right count:1 class:ignore_covered_err */
2506 67 ▶ if (n > 0) { off += (size_t)n; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { off += (size_t)n; continue; } 67 0 67
2507 /*
2508 # uncoverable statement
2509 # uncoverable branch true
2510 # uncoverable branch false
2511 # uncoverable condition left count:1
2512 # uncoverable condition right count:1
2513 # uncoverable condition left count:2
2514 # uncoverable condition right count:2 */
2515 0 ▶ if (n < 0 && errno == EINTR) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0 && errno == EINTR) continue; 0 0 0
if (n < 0 && errno == EINTR) continue; 0 0 0
2516 0 break; /* EAGAIN/error: keep the unwritten tail for the next flush */ /* # uncoverable statement */
2517 }
2518 /*
2519 # uncoverable branch true class:ignore_covered_err
2520 # uncoverable condition left count:1 class:ignore_covered_err
2521 # uncoverable condition right count:1 class:ignore_covered_err
2522 # uncoverable condition left count:2 class:ignore_covered_err */
2523 67 ▶ if (off && off < loop->log_len)
Branch: 2 branches
BranchTrueFalseTotal
if (off && off < loop->log_len) 67 0 67
if (off && off < loop->log_len) 0 67 67
2524 0 memmove(loop->log_buf, loop->log_buf + off, loop->log_len - off); /* # uncoverable statement */
2525 67 loop->log_len -= off;
2526 }
2527  
2528 1552 static void hm_log_raw(hm_loop *loop, const char *p, size_t n) {
2529 1552 ▶ if (loop->log_len + n > loop->log_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (loop->log_len + n > loop->log_cap) { 8 1544 1552
2530 8 ▶ size_t ncap = loop->log_cap ? loop->log_cap : 8192; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
size_t ncap = loop->log_cap ? loop->log_cap : 8192; /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 8 8
2531 /*
2532 # uncoverable branch true class:ignore_covered_err
2533 # uncoverable condition left count:1 class:ignore_covered_err */
2534 8 ▶ while (ncap < loop->log_len + n) ncap *= 2;
Branch: 1 branch
BranchTrueFalseTotal
while (ncap < loop->log_len + n) ncap *= 2; 0 8 8
2535 8 loop->log_buf = (char *)hm_xrealloc(loop->log_buf, ncap);
2536 8 loop->log_cap = ncap;
2537 }
2538 1552 memcpy(loop->log_buf + loop->log_len, p, n);
2539 1552 loop->log_len += n;
2540 1552 }
2541  
2542 /* A plain token: '-' when empty (CLF uses '-' for missing fields). */
2543 /*
2544 # uncoverable statement
2545 # uncoverable subroutine */
2546 0 static void hm_log_tok(hm_loop *loop, const char *p, size_t n) {
2547 /*
2548 # uncoverable statement
2549 # uncoverable branch true
2550 # uncoverable branch false
2551 # uncoverable condition left count:1
2552 # uncoverable condition right count:1
2553 # uncoverable condition left count:2
2554 # uncoverable condition right count:2 */
2555 0 ▶ if (!p || n == 0) { hm_log_raw(loop, "-", 1); return; }
Branch: 2 branches
BranchTrueFalseTotal
if (!p || n == 0) { hm_log_raw(loop, "-", 1); return; } 0 0 0
if (!p || n == 0) { hm_log_raw(loop, "-", 1); return; } 0 0 0
2556 0 hm_log_raw(loop, p, n); /* # uncoverable statement */
2557 }
2558  
2559 /* A value going inside "quotes": escape ", \ and control bytes \xHH so a
2560 * hostile URI or User-Agent cannot forge or split a log line. */
2561 83 static void hm_log_quoted(hm_loop *loop, const char *p, size_t n) {
2562 static const char hex[] = "0123456789abcdef";
2563 83 size_t i, run = 0;
2564 /*
2565 # uncoverable branch true
2566 # uncoverable condition left count:1 class:ignore_covered_err */
2567 83 ▶ if (!p) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!p) return; 0 83 83
2568 720 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 637 83 720
2569 637 unsigned char ch = (unsigned char)p[i];
2570 /*
2571 # uncoverable condition left count:3 class:ignore_covered_err */
2572 642 ▶ if (ch == '"' || ch == '\\' || ch < 0x20 || ch == 0x7f) {
Branch: 4 branches
BranchTrueFalseTotal
if (ch == '"' || ch == '\\' || ch < 0x20 || ch == 0x7f) { 634 3 637
if (ch == '"' || ch == '\\' || ch < 0x20 || ch == 0x7f) { 633 1 634
if (ch == '"' || ch == '\\' || ch < 0x20 || ch == 0x7f) { 633 0 633
if (ch == '"' || ch == '\\' || ch < 0x20 || ch == 0x7f) { 1 632 633
2573 char e[4];
2574 /*
2575 # uncoverable branch false class:ignore_covered_err
2576 # uncoverable condition right count:1 class:ignore_covered_err */
2577 5 ▶ if (run) { hm_log_raw(loop, p + i - run, run); run = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (run) { hm_log_raw(loop, p + i - run, run); run = 0; } 5 0 5
2578 5 ▶ if (ch == '"' || ch == '\\') {
Branch: 2 branches
BranchTrueFalseTotal
if (ch == '"' || ch == '\\') { 2 3 5
if (ch == '"' || ch == '\\') { 1 1 2
2579 4 e[0] = '\\'; e[1] = (char)ch; hm_log_raw(loop, e, 2);
2580 } else {
2581 1 e[0] = '\\'; e[1] = 'x'; e[2] = hex[ch >> 4]; e[3] = hex[ch & 0xf];
2582 1 hm_log_raw(loop, e, 4);
2583 }
2584 } else {
2585 632 run++;
2586 }
2587 }
2588 83 ▶ if (run) hm_log_raw(loop, p + n - run, run);
Branch: 1 branch
BranchTrueFalseTotal
if (run) hm_log_raw(loop, p + n - run, run); 82 1 83
2589 }
2590  
2591 /* The Date header (RFC 9110 6.6.1, IMF-fixdate), recomputed at most once a
2592 * second. Formatted by hand: strftime's %a and %b follow the locale, and
2593 * the wire wants English. 37 bytes, always: "Date: " + 29 + CRLF. */
2594 #define HM_DATE_LEN 37
2595 871 static const char *hm_date_header(hm_loop *loop) {
2596 871 ▶ if (loop->now != loop->date_sec) {
Branch: 1 branch
BranchTrueFalseTotal
if (loop->now != loop->date_sec) { 115 756 871
2597 static const char days[] = "SunMonTueWedThuFriSat";
2598 static const char mons[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
2599 struct tm tm;
2600 115 time_t t = loop->now;
2601 115 char *d = loop->date_hdr;
2602 /*
2603 # uncoverable branch true
2604 # uncoverable condition left count:1 class:ignore_covered_err */
2605 115 ▶ if (!hm_gmtime_r(&t, &tm)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_gmtime_r(&t, &tm)) return NULL; 0 115 115
2606 115 memcpy(d, "Date: ", 6); d += 6;
2607 115 memcpy(d, days + tm.tm_wday * 3, 3); d += 3;
2608 115 *d++ = ','; *d++ = ' ';
2609 115 *d++ = (char)('0' + tm.tm_mday / 10); *d++ = (char)('0' + tm.tm_mday % 10);
2610 115 *d++ = ' ';
2611 115 memcpy(d, mons + tm.tm_mon * 3, 3); d += 3;
2612 115 *d++ = ' ';
2613 {
2614 115 int y = tm.tm_year + 1900;
2615 115 *d++ = (char)('0' + y / 1000 % 10); *d++ = (char)('0' + y / 100 % 10);
2616 115 *d++ = (char)('0' + y / 10 % 10); *d++ = (char)('0' + y % 10);
2617 }
2618 115 *d++ = ' ';
2619 115 *d++ = (char)('0' + tm.tm_hour / 10); *d++ = (char)('0' + tm.tm_hour % 10);
2620 115 *d++ = ':';
2621 115 *d++ = (char)('0' + tm.tm_min / 10); *d++ = (char)('0' + tm.tm_min % 10);
2622 115 *d++ = ':';
2623 115 *d++ = (char)('0' + tm.tm_sec / 10); *d++ = (char)('0' + tm.tm_sec % 10);
2624 115 memcpy(d, " GMT\r\n", 6);
2625 115 loop->date_sec = loop->now;
2626 }
2627 871 return loop->date_hdr;
2628 }
2629  
2630 /* Does an app-supplied header name or value carry a byte that would end
2631 * the field early? A CR, LF or NUL in either splits the response, so a
2632 * response carrying one is refused as a 500 rather than serialised: the
2633 * server is the last place the bug can be caught. */
2634 1942 static int hm_hdr_bytes_ok(const char *p, STRLEN n) {
2635 /*
2636 # uncoverable condition right count:2 class:ignore_covered_err
2637 # uncoverable condition right count:3 class:ignore_covered_err */
2638 1942 ▶ return !memchr(p, '\r', n) && !memchr(p, '\n', n) && !memchr(p, '\0', n);
Branch: 3 branches
BranchTrueFalseTotal
return !memchr(p, '\r', n) && !memchr(p, '\n', n) && !memchr(p, '\0', n); 1940 2 1942
return !memchr(p, '\r', n) && !memchr(p, '\n', n) && !memchr(p, '\0', n); 1940 0 1940
return !memchr(p, '\r', n) && !memchr(p, '\n', n) && !memchr(p, '\0', n); 1940 0 1940
2639 }
2640 875 static int hm_headers_ok(pTHX_ AV *hav) {
2641 SSize_t i, hn;
2642 /*
2643 # uncoverable branch true
2644 # uncoverable condition left count:1 class:ignore_covered_err */
2645 875 ▶ if (!hav) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!hav) return 1; 0 875 875
2646 875 hn = av_len(hav) + 1;
2647 2817 ▶ for (i = 0; i < hn; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < hn; i++) { 1944 873 2817
2648 1944 SV **e = av_fetch(hav, i, 0);
2649 STRLEN l;
2650 const char *p;
2651 1944 ▶ if (!e || !*e) continue; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !*e) continue; /* # uncoverable condition left count:2 class:ignore_covered_err */ 1942 2 1944
if (!e || !*e) continue; /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 1942 1942
2652 1942 p = SvPV(*e, l);
2653 1942 ▶ if (!hm_hdr_bytes_ok(p, l)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_hdr_bytes_ok(p, l)) return 0; 2 1940 1942
2654 }
2655 873 return 1;
2656 }
2657  
2658 /* The same escape, for a value inside a JSON string. */
2659 /* The length of a well-formed UTF-8 sequence starting at p (2..4), or 0 for
2660 * a byte that does not start one. Overlongs, surrogates and > U+10FFFF
2661 * are 0 too: the point is that what passes through is valid JSON text. */
2662 19 static size_t hm_utf8_seq(const unsigned char *p, size_t n) {
2663 19 unsigned char c = p[0];
2664 size_t len, i;
2665 19 ▶ if (c >= 0xc2 && c <= 0xdf) len = 2;
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 0xc2 && c <= 0xdf) len = 2; 8 11 19
if (c >= 0xc2 && c <= 0xdf) len = 2; 2 6 8
2666 17 ▶ else if (c >= 0xe0 && c <= 0xef) len = 3;
Branch: 2 branches
BranchTrueFalseTotal
else if (c >= 0xe0 && c <= 0xef) len = 3; 6 11 17
else if (c >= 0xe0 && c <= 0xef) len = 3; 3 3 6
2667 14 ▶ else if (c >= 0xf0 && c <= 0xf4) len = 4; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
else if (c >= 0xf0 && c <= 0xf4) len = 4; /* # uncoverable condition right count:2 class:ignore_covered_err */ 3 11 14
else if (c >= 0xf0 && c <= 0xf4) len = 4; /* # uncoverable condition right count:2 class:ignore_covered_err */ 3 0 3
2668 11 else return 0;
2669 8 ▶ if (n < len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (n < len) return 0; 1 7 8
2670 /*
2671 # uncoverable branch true class:ignore_covered_err
2672 # uncoverable condition left count:1 class:ignore_covered_err */
2673 23 ▶ for (i = 1; i < len; i++) if ((p[i] & 0xc0) != 0x80) return 0;
Branch: 2 branches
BranchTrueFalseTotal
for (i = 1; i < len; i++) if ((p[i] & 0xc0) != 0x80) return 0; 0 16 16
for (i = 1; i < len; i++) if ((p[i] & 0xc0) != 0x80) return 0; 16 7 23
2674 7 ▶ if (len == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (len == 3) { 3 4 7
2675 3 ▶ if (c == 0xe0 && p[1] < 0xa0) return 0; /* overlong */ /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (c == 0xe0 && p[1] < 0xa0) return 0; /* overlong */ /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 2 3
if (c == 0xe0 && p[1] < 0xa0) return 0; /* overlong */ /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
2676 2 ▶ if (c == 0xed && p[1] >= 0xa0) return 0; /* surrogate */ /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (c == 0xed && p[1] >= 0xa0) return 0; /* surrogate */ /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 1 2
if (c == 0xed && p[1] >= 0xa0) return 0; /* surrogate */ /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
2677 4 ▶ } else if (len == 4) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (len == 4) { 3 1 4
2678 3 ▶ if (c == 0xf0 && p[1] < 0x90) return 0; /* overlong */
Branch: 2 branches
BranchTrueFalseTotal
if (c == 0xf0 && p[1] < 0x90) return 0; /* overlong */ 2 1 3
if (c == 0xf0 && p[1] < 0x90) return 0; /* overlong */ 1 1 2
2679 2 ▶ if (c == 0xf4 && p[1] >= 0x90) return 0; /* > 10FFFF */ /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (c == 0xf4 && p[1] >= 0x90) return 0; /* > 10FFFF */ /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 1 2
if (c == 0xf4 && p[1] >= 0x90) return 0; /* > 10FFFF */ /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
2680 }
2681 3 return len;
2682 }
2683  
2684 199 static void hm_log_json_esc(hm_loop *loop, const char *p, size_t n) {
2685 static const char hex[] = "0123456789abcdef";
2686 199 size_t i, run = 0;
2687 /*
2688 # uncoverable branch true
2689 # uncoverable condition left count:1 class:ignore_covered_err */
2690 199 ▶ if (!p) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!p) return; 0 199 199
2691 1586 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 1387 199 1586
2692 1387 unsigned char ch = (unsigned char)p[i];
2693 1387 ▶ if (ch >= 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (ch >= 0x80) { 19 1368 1387
2694 /* Valid UTF-8 passes; a stray high byte (a URI or User-Agent
2695 * that is not UTF-8) is escaped, so the line stays valid JSON
2696 * and a strict pipeline does not drop it - which was a way to
2697 * keep one's requests out of the log. */
2698 19 size_t sl = hm_utf8_seq((const unsigned char *)p + i, n - i);
2699 19 ▶ if (sl) { run += sl; i += sl - 1; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (sl) { run += sl; i += sl - 1; continue; } 3 16 19
2700 }
2701 1405 ▶ if (ch == '"' || ch == '\\' || ch < 0x20 || ch >= 0x80) {
Branch: 4 branches
BranchTrueFalseTotal
if (ch == '"' || ch == '\\' || ch < 0x20 || ch >= 0x80) { 1381 3 1384
if (ch == '"' || ch == '\\' || ch < 0x20 || ch >= 0x80) { 1380 1 1381
if (ch == '"' || ch == '\\' || ch < 0x20 || ch >= 0x80) { 1379 1 1380
if (ch == '"' || ch == '\\' || ch < 0x20 || ch >= 0x80) { 16 1363 1379
2702 char e[6];
2703 21 ▶ if (run) { hm_log_raw(loop, p + i - run, run); run = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (run) { hm_log_raw(loop, p + i - run, run); run = 0; } 11 10 21
2704 21 ▶ if (ch == '"' || ch == '\\') {
Branch: 2 branches
BranchTrueFalseTotal
if (ch == '"' || ch == '\\') { 18 3 21
if (ch == '"' || ch == '\\') { 1 17 18
2705 4 e[0] = '\\'; e[1] = (char)ch; hm_log_raw(loop, e, 2);
2706 } else {
2707 17 e[0] = '\\'; e[1] = 'u'; e[2] = '0'; e[3] = '0';
2708 17 e[4] = hex[ch >> 4]; e[5] = hex[ch & 0xf];
2709 17 hm_log_raw(loop, e, 6);
2710 }
2711 } else {
2712 1363 run++;
2713 }
2714 }
2715 199 ▶ if (run) hm_log_raw(loop, p + n - run, run);
Branch: 1 branch
BranchTrueFalseTotal
if (run) hm_log_raw(loop, p + n - run, run); 198 1 199
2716 }
2717  
2718 /* A value from $env, escaped the way this log's format wants. */
2719 282 static void hm_log_esc(hm_loop *loop, const char *p, size_t n) {
2720 282 ▶ if (loop->log_json) hm_log_json_esc(loop, p, n);
Branch: 1 branch
BranchTrueFalseTotal
if (loop->log_json) hm_log_json_esc(loop, p, n); 199 83 282
2721 83 else hm_log_quoted(loop, p, n);
2722 282 }
2723  
2724 /* Cached apache-style timestamp, recomputed at most once per second. */
2725 14 static const char *hm_log_time(hm_loop *loop) {
2726 14 ▶ if (loop->now != loop->log_ts_sec) {
Branch: 1 branch
BranchTrueFalseTotal
if (loop->now != loop->log_ts_sec) { 4 10 14
2727 struct tm tm;
2728 4 time_t t = loop->now;
2729 4 hm_localtime_r(&t, &tm);
2730 4 strftime(loop->log_ts, sizeof(loop->log_ts), "%d/%b/%Y:%H:%M:%S %z", &tm);
2731 4 loop->log_ts_sec = loop->now;
2732 }
2733 14 return loop->log_ts;
2734 }
2735  
2736 /* The ISO 8601 stamp the JSON format carries, cached the same way. */
2737 49 static const char *hm_log_time_iso(hm_loop *loop) {
2738 49 ▶ if (loop->now != loop->log_iso_sec) {
Branch: 1 branch
BranchTrueFalseTotal
if (loop->now != loop->log_iso_sec) { 4 45 49
2739 struct tm tm;
2740 4 time_t t = loop->now;
2741 4 hm_localtime_r(&t, &tm);
2742 4 strftime(loop->log_iso, sizeof(loop->log_iso), "%Y-%m-%dT%H:%M:%S%z", &tm);
2743 4 loop->log_iso_sec = loop->now;
2744 }
2745 49 return loop->log_iso;
2746 }
2747  
2748 /* ---- the access-log format compiler ----------------------------------------
2749 * Apache's directives, the ones a PSGI server can honour:
2750 * %h %a REMOTE_ADDR %l "-" %u REMOTE_USER %t [time]
2751 * %r request line %s %>s status %b bytes or "-" %B bytes or 0
2752 * %D microseconds %T seconds %p SERVER_PORT %m METHOD
2753 * %U PATH_INFO %q ?query %H SERVER_PROTOCOL %v SERVER_NAME
2754 * %P pid %{Name}i request header %{Name}e any $env key
2755 * %% a literal percent sign
2756 * Returns 0, or -1 with the offending directive in err. Every value is
2757 * escaped (CLF or JSON), so no directive is a way to forge a line. */
2758 #define HM_LOG_COMBINED "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\""
2759  
2760 1624 static void hm_logtok_push(hm_logtok **toks, int *n, int *cap, hm_logtok t) {
2761 1624 ▶ if (*n == *cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (*n == *cap) { 179 1445 1624
2762 179 ▶ *cap = *cap ? *cap * 2 : 16;
Branch: 1 branch
BranchTrueFalseTotal
*cap = *cap ? *cap * 2 : 16; 89 90 179
2763 179 *toks = (hm_logtok *)hm_xrealloc(*toks, (size_t)*cap * sizeof(hm_logtok));
2764 }
2765 1624 (*toks)[(*n)++] = t;
2766 1624 }
2767 906 static void hm_logtok_lit(hm_logtok **toks, int *n, int *cap, const char *p, size_t l) {
2768 hm_logtok t;
2769 907 ▶ if (!l) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!l) return; 92 814 906
2770 814 ▶ if (*n && (*toks)[*n - 1].kind == HM_LT_LIT) { /* extend the last run */
Branch: 2 branches
BranchTrueFalseTotal
if (*n && (*toks)[*n - 1].kind == HM_LT_LIT) { /* extend the last run */ 811 3 814
if (*n && (*toks)[*n - 1].kind == HM_LT_LIT) { /* extend the last run */ 1 810 811
2771 1 hm_logtok *last = &(*toks)[*n - 1];
2772 1 last->lit = (char *)hm_xrealloc(last->lit, last->litlen + l);
2773 1 memcpy(last->lit + last->litlen, p, l);
2774 1 last->litlen += l;
2775 1 return;
2776 }
2777 813 t.kind = HM_LT_LIT; t.lit = (char *)hm_xmalloc(l); memcpy(t.lit, p, l);
2778 813 t.litlen = l; t.key = NULL;
2779 813 hm_logtok_push(toks, n, cap, t);
2780 }
2781 368 static void hm_logtok_env(pTHX_ hm_logtok **toks, int *n, int *cap,
2782 const char *key, size_t klen) {
2783 hm_logtok t;
2784 368 t.kind = HM_LT_ENV; t.lit = NULL; t.litlen = 0;
2785 368 t.key = newSVpvn_share(key, (I32)klen, 0); /* hash computed once */
2786 368 hm_logtok_push(toks, n, cap, t);
2787 368 }
2788 443 static void hm_logtok_kind(hm_logtok **toks, int *n, int *cap, int kind) {
2789 hm_logtok t;
2790 443 t.kind = kind; t.lit = NULL; t.litlen = 0; t.key = NULL;
2791 443 hm_logtok_push(toks, n, cap, t);
2792 443 }
2793  
2794 90 static int hm_logfmt_compile(pTHX_ const char *fmt, hm_logtok **out, int *nout,
2795 int *wants_t0, char *err, size_t errlen) {
2796 90 hm_logtok *toks = NULL;
2797 90 int n = 0, cap = 0;
2798 90 const char *p = fmt, *lit = fmt;
2799 90 *wants_t0 = 0;
2800 2070 ▶ while (*p) {
Branch: 1 branch
BranchTrueFalseTotal
while (*p) { 1984 86 2070
2801 char d;
2802 1984 ▶ if (*p != '%') { p++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (*p != '%') { p++; continue; } 1201 783 1984
2803 783 hm_logtok_lit(&toks, &n, &cap, lit, (size_t)(p - lit));
2804 783 p++;
2805 783 ▶ if (*p == '>') p++; /* %>s is %s */
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '>') p++; /* %>s is %s */ 86 697 783
2806 783 ▶ if (*p == '{') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '{') { 175 608 783
2807 175 const char *name = p + 1, *end = strchr(name, '}');
2808 char key[300];
2809 size_t nl, i;
2810 175 ▶ if (!end || (end[1] != 'i' && end[1] != 'e')) {
Branch: 3 branches
BranchTrueFalseTotal
if (!end || (end[1] != 'i' && end[1] != 'e')) { 174 1 175
if (!end || (end[1] != 'i' && end[1] != 'e')) { 2 172 174
if (!end || (end[1] != 'i' && end[1] != 'e')) { 1 1 2
2811 2 ▶ snprintf(err, errlen, "%%{...}%c", end ? end[1] : '?');
Branch: 1 branch
BranchTrueFalseTotal
snprintf(err, errlen, "%%{...}%c", end ? end[1] : '?'); 1 1 2
2812 3 goto bad;
2813 }
2814 173 nl = (size_t)(end - name);
2815 173 ▶ if (nl == 0 || nl + 6 > sizeof(key)) { snprintf(err, errlen, "%%{%.*s}", (int)nl, name); goto bad; } /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (nl == 0 || nl + 6 > sizeof(key)) { snprintf(err, errlen, "%%{%.*s}", (int)nl, name); goto bad; } /* # uncoverable condition left count:2 class:ignore_covered_err */ 172 1 173
if (nl == 0 || nl + 6 > sizeof(key)) { snprintf(err, errlen, "%%{%.*s}", (int)nl, name); goto bad; } /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 172 172
2816 172 ▶ if (end[1] == 'i') {
Branch: 1 branch
BranchTrueFalseTotal
if (end[1] == 'i') { 171 1 172
2817 171 memcpy(key, "HTTP_", 5);
2818 1624 ▶ for (i = 0; i < nl; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nl; i++) { 1453 171 1624
2819 1453 unsigned char ch = (unsigned char)name[i];
2820 1453 ▶ if (ch >= 'a' && ch <= 'z') ch -= 32; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (ch >= 'a' && ch <= 'z') ch -= 32; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1108 345 1453
if (ch >= 'a' && ch <= 'z') ch -= 32; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1108 0 1108
2821 345 ▶ else if (ch == '-') ch = '_';
Branch: 1 branch
BranchTrueFalseTotal
else if (ch == '-') ch = '_'; 87 258 345
2822 1453 key[5 + i] = (char)ch;
2823 }
2824 171 hm_logtok_env(aTHX_ &toks, &n, &cap, key, nl + 5);
2825 } else {
2826 1 hm_logtok_env(aTHX_ &toks, &n, &cap, name, nl);
2827 }
2828 172 p = end + 2;
2829 172 lit = p;
2830 172 continue;
2831 }
2832 608 d = *p;
2833 /*
2834 # uncoverable branch true count:11 class:ignore_covered_err
2835 # uncoverable branch true count:12 class:ignore_covered_err */
2836 608 switch (d) {
2837 1 case '%': hm_logtok_lit(&toks, &n, &cap, "%", 1); break;
2838 87 case 'h': case 'a': hm_logtok_env(aTHX_ &toks, &n, &cap, "REMOTE_ADDR", 11); break;
2839 84 case 'l': hm_logtok_kind(&toks, &n, &cap, HM_LT_DASH); break;
2840 84 case 'u': hm_logtok_env(aTHX_ &toks, &n, &cap, "REMOTE_USER", 11); break;
2841 84 case 't': hm_logtok_kind(&toks, &n, &cap, HM_LT_TIME); break;
2842 84 case 'r': hm_logtok_kind(&toks, &n, &cap, HM_LT_REQLINE); break;
2843 86 case 's': hm_logtok_kind(&toks, &n, &cap, HM_LT_STATUS); break;
2844 86 case 'b': hm_logtok_kind(&toks, &n, &cap, HM_LT_BYTES); break;
2845 1 case 'B': hm_logtok_kind(&toks, &n, &cap, HM_LT_BYTES0); break;
2846 2 case 'D': hm_logtok_kind(&toks, &n, &cap, HM_LT_DUR_US); *wants_t0 = 1; break;
2847 1 case 'T': hm_logtok_kind(&toks, &n, &cap, HM_LT_DUR_S); *wants_t0 = 1; break;
2848 0 case 'p': hm_logtok_env(aTHX_ &toks, &n, &cap, "SERVER_PORT", 11); break; /* # uncoverable statement */
2849 1 case 'm': hm_logtok_env(aTHX_ &toks, &n, &cap, "REQUEST_METHOD", 14); break;
2850 1 case 'U': hm_logtok_env(aTHX_ &toks, &n, &cap, "PATH_INFO", 9); break;
2851 1 case 'q': hm_logtok_kind(&toks, &n, &cap, HM_LT_QUERY); break;
2852 1 case 'H': hm_logtok_env(aTHX_ &toks, &n, &cap, "SERVER_PROTOCOL", 15); break;
2853 1 case 'v': hm_logtok_env(aTHX_ &toks, &n, &cap, "SERVER_NAME", 11); break;
2854 2 case 'P': hm_logtok_kind(&toks, &n, &cap, HM_LT_PID); break;
2855 1 default:
2856 1 ▶ snprintf(err, errlen, "%%%c", d ? d : '?'); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
snprintf(err, errlen, "%%%c", d ? d : '?'); /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 0 1
2857 1 goto bad;
2858 }
2859 607 p++;
2860 607 lit = p;
2861 }
2862 86 hm_logtok_lit(&toks, &n, &cap, lit, (size_t)(p - lit));
2863 86 *out = toks; *nout = n;
2864 86 return 0;
2865 4 bad:
2866 {
2867 int i;
2868 6 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2 4 6
2869 2 ▶ if (toks[i].lit) free(toks[i].lit);
Branch: 1 branch
BranchTrueFalseTotal
if (toks[i].lit) free(toks[i].lit); 1 1 2
2870 2 ▶ if (toks[i].key) SvREFCNT_dec(toks[i].key);
Branch: 1 branch
BranchTrueFalseTotal
if (toks[i].key) SvREFCNT_dec(toks[i].key); 1 1 2
2871 }
2872 4 ▶ if (toks) free(toks);
Branch: 1 branch
BranchTrueFalseTotal
if (toks) free(toks); 1 3 4
2873 }
2874 4 return -1;
2875 }
2876  
2877 /* The JSON shape: one object per line, fixed keys, numbers unquoted. */
2878 3 static void hm_logfmt_json(pTHX_ hm_logtok **out, int *nout) {
2879 3 hm_logtok *toks = NULL;
2880 3 int n = 0, cap = 0;
2881 #define HM_JL(s) hm_logtok_lit(&toks, &n, &cap, s, sizeof(s) - 1)
2882 #define HM_JE(k) hm_logtok_env(aTHX_ &toks, &n, &cap, k, sizeof(k) - 1)
2883 3 HM_JL("{\"time\":\""); hm_logtok_kind(&toks, &n, &cap, HM_LT_TIME_ISO);
2884 3 HM_JL("\",\"remote_addr\":\""); HM_JE("REMOTE_ADDR");
2885 3 HM_JL("\",\"user\":\""); HM_JE("REMOTE_USER");
2886 3 HM_JL("\",\"method\":\""); HM_JE("REQUEST_METHOD");
2887 3 HM_JL("\",\"uri\":\""); HM_JE("REQUEST_URI");
2888 3 HM_JL("\",\"protocol\":\""); HM_JE("SERVER_PROTOCOL");
2889 3 HM_JL("\",\"status\":"); hm_logtok_kind(&toks, &n, &cap, HM_LT_STATUS);
2890 3 HM_JL(",\"bytes\":"); hm_logtok_kind(&toks, &n, &cap, HM_LT_BYTES);
2891 3 HM_JL(",\"referer\":\""); HM_JE("HTTP_REFERER");
2892 3 HM_JL("\",\"user_agent\":\""); HM_JE("HTTP_USER_AGENT");
2893 3 HM_JL("\",\"duration_us\":"); hm_logtok_kind(&toks, &n, &cap, HM_LT_DUR_US);
2894 3 HM_JL("}");
2895 #undef HM_JL
2896 #undef HM_JE
2897 3 *out = toks; *nout = n;
2898 3 }
2899  
2900 48 static const char *hm_env_str(pTHX_ HV *env, const char *key, I32 klen, STRLEN *lenp) {
2901 48 SV **s = hv_fetch(env, key, klen, 0);
2902 /*
2903 # uncoverable branch false class:ignore_covered_err
2904 # uncoverable condition right count:1 class:ignore_covered_err
2905 # uncoverable condition right count:2 class:ignore_covered_err
2906 # uncoverable condition right count:3 class:ignore_covered_err */
2907 96 ▶ if (s && *s && SvOK(*s)) return SvPV(*s, *lenp);
Branch: 3 branches
BranchTrueFalseTotal
if (s && *s && SvOK(*s)) return SvPV(*s, *lenp); 48 0 48
if (s && *s && SvOK(*s)) return SvPV(*s, *lenp); 48 0 48
if (s && *s && SvOK(*s)) return SvPV(*s, *lenp); 48 0 48
2908 0 *lenp = 0; /* # uncoverable statement */
2909 0 return NULL; /* # uncoverable statement */
2910 }
2911  
2912 /* One $env value into the line, escaped; the "absent" spelling differs
2913 * between the two shapes ("-" in CLF, an empty string in JSON). */
2914 429 static void hm_log_env_val(pTHX_ hm_loop *loop, HV *env, const hm_logtok *t) {
2915 429 HE *he = hv_fetch_ent(env, t->key, 0, 0);
2916 429 ▶ SV *v = he ? HeVAL(he) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *v = he ? HeVAL(he) : NULL; 239 190 429
2917 429 ▶ if (v && SvOK(v)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (v && SvOK(v)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 239 190 429
if (v && SvOK(v)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 239 0 239
2918 STRLEN n;
2919 239 const char *s = SvPV(v, n);
2920 /*
2921 # uncoverable branch false
2922 # uncoverable condition right count:1 class:ignore_covered_err */
2923 239 ▶ if (n) { hm_log_esc(loop, s, n); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (n) { hm_log_esc(loop, s, n); return; } 239 0 239
2924 }
2925 190 ▶ if (!loop->log_json) hm_log_raw(loop, "-", 1);
Branch: 1 branch
BranchTrueFalseTotal
if (!loop->log_json) hm_log_raw(loop, "-", 1); 46 144 190
2926 }
2927  
2928 /* Build one access-log line into the loop's log buffer by walking the
2929 * compiled format. The default compiles to Combined:
2930 * host - user [time] "METHOD URI PROTO" status bytes "referer" "ua" */
2931 69 static void hm_log_line(pTHX_ hm_loop *loop, SV *env_rv, int status, ssize_t blen) {
2932 HV *env;
2933 char num[32];
2934 int ln, i;
2935  
2936 /*
2937 # uncoverable branch true class:ignore_covered_err
2938 # uncoverable condition left count:1 class:ignore_covered_err
2939 # uncoverable condition left count:2 class:ignore_covered_err
2940 # uncoverable condition left count:3 class:ignore_covered_err */
2941 69 ▶ if (!(env_rv && SvROK(env_rv) && SvTYPE(SvRV(env_rv)) == SVt_PVHV)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!(env_rv && SvROK(env_rv) && SvTYPE(SvRV(env_rv)) == SVt_PVHV)) return; 69 0 69
if (!(env_rv && SvROK(env_rv) && SvTYPE(SvRV(env_rv)) == SVt_PVHV)) return; 69 0 69
if (!(env_rv && SvROK(env_rv) && SvTYPE(SvRV(env_rv)) == SVt_PVHV)) return; 0 69 69
2942 69 env = (HV *)SvRV(env_rv);
2943  
2944 1572 ▶ for (i = 0; i < loop->nlogtok; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < loop->nlogtok; i++) { 1503 69 1572
2945 1503 const hm_logtok *t = &loop->logtok[i];
2946 1503 switch (t->kind) { /* # uncoverable branch true count:13 class:ignore_covered_err */
2947 774 case HM_LT_LIT: hm_log_raw(loop, t->lit, t->litlen); break;
2948 429 case HM_LT_ENV: hm_log_env_val(aTHX_ loop, env, t); break;
2949 14 case HM_LT_DASH: hm_log_raw(loop, "-", 1); break;
2950 69 case HM_LT_STATUS:
2951 69 ln = (int)hm_itoa(num, (long)status); hm_log_raw(loop, num, (size_t)ln);
2952 69 break;
2953 69 case HM_LT_BYTES:
2954 69 ▶ if (blen >= 0) { ln = (int)hm_utoa(num, (unsigned long)blen);
Branch: 1 branch
BranchTrueFalseTotal
if (blen >= 0) { ln = (int)hm_utoa(num, (unsigned long)blen); 57 12 69
2955 57 hm_log_raw(loop, num, (size_t)ln); }
2956 12 ▶ else if (loop->log_json) hm_log_raw(loop, "null", 4);
Branch: 1 branch
BranchTrueFalseTotal
else if (loop->log_json) hm_log_raw(loop, "null", 4); 7 5 12
2957 5 else hm_log_raw(loop, "-", 1);
2958 69 break;
2959 4 case HM_LT_BYTES0:
2960 4 ln = (int)hm_utoa(num, blen >= 0 ? (unsigned long)blen : 0UL);
2961 4 hm_log_raw(loop, num, (size_t)ln);
2962 4 break;
2963 14 case HM_LT_TIME: {
2964 14 const char *ts = hm_log_time(loop);
2965 14 hm_log_raw(loop, "[", 1); hm_log_raw(loop, ts, strlen(ts)); hm_log_raw(loop, "]", 1);
2966 14 break;
2967 }
2968 49 case HM_LT_TIME_ISO: {
2969 49 const char *ts = hm_log_time_iso(loop);
2970 49 hm_log_raw(loop, ts, strlen(ts));
2971 49 break;
2972 }
2973 14 case HM_LT_REQLINE: {
2974 const char *s; STRLEN n;
2975 14 s = hm_env_str(aTHX_ env, "REQUEST_METHOD", 14, &n);
2976 /*
2977 # uncoverable condition right count:1 class:ignore_covered_err
2978 # uncoverable condition right count:2 class:ignore_covered_err */
2979 14 ▶ if (s && n) hm_log_esc(loop, s, n); else hm_log_raw(loop, "-", 1);
Branch: 2 branches
BranchTrueFalseTotal
if (s && n) hm_log_esc(loop, s, n); else hm_log_raw(loop, "-", 1); 14 0 14
if (s && n) hm_log_esc(loop, s, n); else hm_log_raw(loop, "-", 1); 14 0 14
2980 14 hm_log_raw(loop, " ", 1);
2981 14 s = hm_env_str(aTHX_ env, "REQUEST_URI", 11, &n);
2982 /*
2983 # uncoverable condition right count:1 class:ignore_covered_err
2984 # uncoverable condition right count:2 class:ignore_covered_err */
2985 14 ▶ if (s && n) hm_log_esc(loop, s, n); else hm_log_raw(loop, "-", 1);
Branch: 2 branches
BranchTrueFalseTotal
if (s && n) hm_log_esc(loop, s, n); else hm_log_raw(loop, "-", 1); 14 0 14
if (s && n) hm_log_esc(loop, s, n); else hm_log_raw(loop, "-", 1); 14 0 14
2986 14 hm_log_raw(loop, " ", 1);
2987 14 s = hm_env_str(aTHX_ env, "SERVER_PROTOCOL", 15, &n);
2988 /*
2989 # uncoverable condition right count:1 class:ignore_covered_err
2990 # uncoverable condition right count:2 class:ignore_covered_err */
2991 14 ▶ if (s && n) hm_log_esc(loop, s, n); else hm_log_raw(loop, "-", 1);
Branch: 2 branches
BranchTrueFalseTotal
if (s && n) hm_log_esc(loop, s, n); else hm_log_raw(loop, "-", 1); 14 0 14
if (s && n) hm_log_esc(loop, s, n); else hm_log_raw(loop, "-", 1); 14 0 14
2992 14 break;
2993 }
2994 2 case HM_LT_QUERY: {
2995 const char *s; STRLEN n;
2996 2 s = hm_env_str(aTHX_ env, "QUERY_STRING", 12, &n);
2997 2 ▶ if (s && n) { hm_log_raw(loop, "?", 1); hm_log_esc(loop, s, n); } /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (s && n) { hm_log_raw(loop, "?", 1); hm_log_esc(loop, s, n); } /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 0 2
if (s && n) { hm_log_raw(loop, "?", 1); hm_log_esc(loop, s, n); } /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 1 2
2998 2 break;
2999 }
3000 6 case HM_LT_PID:
3001 6 ln = (int)hm_itoa(num, (long)hm_os_getpid()); hm_log_raw(loop, num, (size_t)ln);
3002 6 break;
3003 59 case HM_LT_DUR_US:
3004 case HM_LT_DUR_S: {
3005 59 SV **t0 = hv_fetch(env, "psgix.request_start", 19, 0);
3006 /*
3007 # uncoverable branch false class:ignore_covered_err
3008 # uncoverable condition right count:1 class:ignore_covered_err
3009 # uncoverable condition right count:2 class:ignore_covered_err
3010 # uncoverable condition right count:3 class:ignore_covered_err */
3011 118 ▶ if (t0 && *t0 && SvOK(*t0)) {
Branch: 3 branches
BranchTrueFalseTotal
if (t0 && *t0 && SvOK(*t0)) { 59 0 59
if (t0 && *t0 && SvOK(*t0)) { 59 0 59
if (t0 && *t0 && SvOK(*t0)) { 59 0 59
3012 59 double el = (double)hm_now_ns() / 1e9 - SvNV(*t0);
3013 59 unsigned long v = el < 0 ? 0UL
3014 59 ▶ : (unsigned long)(t->kind == HM_LT_DUR_US ? el * 1e6 : el); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
: (unsigned long)(t->kind == HM_LT_DUR_US ? el * 1e6 : el); /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 59 59
: (unsigned long)(t->kind == HM_LT_DUR_US ? el * 1e6 : el); /* # uncoverable condition left count:1 class:ignore_covered_err */ 55 4 59
3015 59 ln = (int)hm_utoa(num, v); hm_log_raw(loop, num, (size_t)ln);
3016 }
3017 /*
3018 # uncoverable statement
3019 # uncoverable branch true
3020 # uncoverable branch false
3021 # uncoverable condition left count:1
3022 # uncoverable condition right count:1 */
3023 0 ▶ else if (loop->log_json) hm_log_raw(loop, "null", 4);
Branch: 1 branch
BranchTrueFalseTotal
else if (loop->log_json) hm_log_raw(loop, "null", 4); 0 0 0
3024 0 else hm_log_raw(loop, "-", 1); /* # uncoverable statement */
3025 59 break;
3026 }
3027 }
3028 }
3029 69 hm_log_raw(loop, "\n", 1);
3030  
3031 /*
3032 # uncoverable branch true class:ignore_covered_err
3033 # uncoverable condition left count:1 class:ignore_covered_err */
3034 69 ▶ if (loop->log_len >= HM_LOG_FLUSH_AT) hm_log_flush(loop);
Branch: 1 branch
BranchTrueFalseTotal
if (loop->log_len >= HM_LOG_FLUSH_AT) hm_log_flush(loop); 0 69 69
3035 }
3036  
3037 /* Record one response. Fast path: format in C to the access-log fd. Else, if
3038 * a coderef was given, call $cb->($env, $status, $bytes); errors in the logger
3039 * are swallowed so they cannot take the loop down. */
3040 170 static void hm_access_log(pTHX_ hm_loop *loop, SV *env_rv, int status, ssize_t blen) {
3041 170 dSP;
3042 170 ▶ if (loop->log_fd >= 0) { hm_log_line(aTHX_ loop, env_rv, status, blen); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (loop->log_fd >= 0) { hm_log_line(aTHX_ loop, env_rv, status, blen); return; } 69 101 170
3043 /*
3044 # uncoverable branch true
3045 # uncoverable condition left count:1 class:ignore_covered_err */
3046 101 ▶ if (!loop->log_cb) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!loop->log_cb) return; 0 101 101
3047 101 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 101 101
3048 101 ▶ XPUSHs(env_rv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(env_rv); 0 101 101
3049 101 ▶ XPUSHs(sv_2mortal(newSViv(status)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(status))); 0 101 101
3050 /*
3051 # uncoverable condition left count:1 class:ignore_covered_err
3052 # uncoverable condition right count:2 class:ignore_covered_err */
3053 101 ▶ XPUSHs(blen >= 0 ? sv_2mortal(newSViv((IV)blen)) : &PL_sv_undef);
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(blen >= 0 ? sv_2mortal(newSViv((IV)blen)) : &PL_sv_undef); 0 101 101
XPUSHs(blen >= 0 ? sv_2mortal(newSViv((IV)blen)) : &PL_sv_undef); 101 0 101
3054 101 PUTBACK;
3055 101 call_sv(loop->log_cb, G_DISCARD | G_EVAL);
3056 101 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 101 0 101
3057 }
3058  
3059 /* ---- response serialization ---------------------------------------------- */
3060  
3061 /* Reusable per-connection write buffer: keep capacity across requests so the
3062 * steady-state response path does no malloc/free. */
3063 10137 static void hm_wb_reserve(hm_conn *c, size_t need) {
3064 10137 ▶ if (c->wlen + need > c->wcap) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->wlen + need > c->wcap) { 144 9993 10137
3065 144 ▶ size_t ncap = c->wcap ? c->wcap : 4096;
Branch: 1 branch
BranchTrueFalseTotal
size_t ncap = c->wcap ? c->wcap : 4096; 19 125 144
3066 210 ▶ while (ncap < c->wlen + need) ncap *= 2;
Branch: 1 branch
BranchTrueFalseTotal
while (ncap < c->wlen + need) ncap *= 2; 66 144 210
3067 144 c->wbuf = (char *)hm_xrealloc(c->wbuf, ncap);
3068 144 c->wcap = ncap;
3069 }
3070 10137 }
3071 10136 static void hm_wb_put(hm_conn *c, const char *p, size_t n) {
3072 10136 hm_wb_reserve(c, n);
3073 10136 memcpy(c->wbuf + c->wlen, p, n);
3074 10136 c->wlen += n;
3075 10136 }
3076  
3077 static const char HM_ERR500[] =
3078 "HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/plain\r\n"
3079 "Content-Length: 21\r\nConnection: close\r\n\r\nInternal Server Error";
3080  
3081 /* Serialize $resp: headers into c->wbuf, then try an immediate writev of
3082 * header + body chunks (zero-copy body). Whatever the kernel doesn't take
3083 * is buffered; a write watcher resumes it (backpressure). Falls back to
3084 * copy-into-buffer when bytes are already queued (pipelining) or the body
3085 * is a filehandle. Returns 0, or -1 if the connection was closed. */
3086 835 static int hm_queue_response(pTHX_ hm_conn *c, SV *resp) {
3087 835 hm_loop *loop = c->loop;
3088 835 AV *rav, *hav = NULL;
3089 SV **s0, **s1, **s2;
3090 835 SV *fh_body = NULL;
3091 835 AV *bav = NULL;
3092 835 int status = 500, has_len = 0, has_conn = 0, has_date = 0;
3093 835 int gz_out = 0;
3094 835 size_t blen = 0;
3095 835 size_t hdr_start = c->wlen;
3096 835 SSize_t bn = 0;
3097 char line[64];
3098 int ln;
3099  
3100 /*
3101 # uncoverable branch true class:ignore_covered_err
3102 # uncoverable condition left count:1 class:ignore_covered_err */
3103 835 ▶ if (loop->stopping) c->keepalive = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (loop->stopping) c->keepalive = 0; 0 835 835
3104  
3105 /*
3106 # uncoverable condition left count:2 class:ignore_covered_err
3107 # uncoverable condition left count:3 class:ignore_covered_err */
3108 835 ▶ if (!(resp && SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!(resp && SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV)) { 832 3 835
if (!(resp && SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV)) { 832 0 832
if (!(resp && SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV)) { 0 832 832
3109 3 hm_wb_put(c, HM_ERR500, sizeof(HM_ERR500) - 1);
3110 3 c->keepalive = 0;
3111 3 c->last_status = 500;
3112 3 c->last_blen = 21;
3113 3 return 0;
3114 }
3115 832 rav = (AV *)SvRV(resp);
3116 832 s0 = av_fetch(rav, 0, 0);
3117 832 s1 = av_fetch(rav, 1, 0);
3118 832 s2 = av_fetch(rav, 2, 0);
3119 /*
3120 # uncoverable branch false class:ignore_covered_err
3121 # uncoverable condition right count:1 class:ignore_covered_err
3122 # uncoverable condition right count:2 class:ignore_covered_err */
3123 832 ▶ if (s0 && *s0) status = (int)SvIV(*s0);
Branch: 2 branches
BranchTrueFalseTotal
if (s0 && *s0) status = (int)SvIV(*s0); 832 0 832
if (s0 && *s0) status = (int)SvIV(*s0); 832 0 832
3124 /*
3125 # uncoverable branch false class:ignore_covered_err
3126 # uncoverable condition right count:1 class:ignore_covered_err
3127 # uncoverable condition right count:2 class:ignore_covered_err
3128 # uncoverable condition right count:3 class:ignore_covered_err
3129 # uncoverable condition right count:4 class:ignore_covered_err */
3130 832 ▶ if (s1 && *s1 && SvROK(*s1) && SvTYPE(SvRV(*s1)) == SVt_PVAV) hav = (AV *)SvRV(*s1);
Branch: 4 branches
BranchTrueFalseTotal
if (s1 && *s1 && SvROK(*s1) && SvTYPE(SvRV(*s1)) == SVt_PVAV) hav = (AV *)SvRV(*s1); 832 0 832
if (s1 && *s1 && SvROK(*s1) && SvTYPE(SvRV(*s1)) == SVt_PVAV) hav = (AV *)SvRV(*s1); 832 0 832
if (s1 && *s1 && SvROK(*s1) && SvTYPE(SvRV(*s1)) == SVt_PVAV) hav = (AV *)SvRV(*s1); 832 0 832
if (s1 && *s1 && SvROK(*s1) && SvTYPE(SvRV(*s1)) == SVt_PVAV) hav = (AV *)SvRV(*s1); 832 0 832
3131 832 ▶ if (!hm_headers_ok(aTHX_ hav)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_headers_ok(aTHX_ hav)) { 1 831 832
3132 1 hm_errlog("Hyperman: response header carries CR, LF or NUL; refused\n");
3133 1 hm_wb_put(c, HM_ERR500, sizeof(HM_ERR500) - 1);
3134 1 c->keepalive = 0;
3135 1 c->last_status = 500;
3136 1 c->last_blen = 21;
3137 1 return 0;
3138 }
3139 /*
3140 # uncoverable branch true class:ignore_covered_err
3141 # uncoverable condition left count:1 class:ignore_covered_err
3142 # uncoverable condition right count:1 class:ignore_covered_err
3143 # uncoverable condition left count:2 class:ignore_covered_err */
3144 831 ▶ if (s2 && !*s2) s2 = NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (s2 && !*s2) s2 = NULL; 831 0 831
if (s2 && !*s2) s2 = NULL; 0 831 831
3145  
3146 /* body length (first pass, no copy) */
3147 /*
3148 # uncoverable condition right count:1 class:ignore_covered_err
3149 # uncoverable condition right count:2 class:ignore_covered_err */
3150 1644 ▶ if (s2 && SvROK(*s2) && SvTYPE(SvRV(*s2)) == SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
if (s2 && SvROK(*s2) && SvTYPE(SvRV(*s2)) == SVt_PVAV) { 831 0 831
if (s2 && SvROK(*s2) && SvTYPE(SvRV(*s2)) == SVt_PVAV) { 831 0 831
if (s2 && SvROK(*s2) && SvTYPE(SvRV(*s2)) == SVt_PVAV) { 813 18 831
3151 SSize_t i;
3152 813 bav = (AV *)SvRV(*s2);
3153 813 bn = av_len(bav) + 1;
3154 1620 ▶ for (i = 0; i < bn; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < bn; i++) { 807 813 1620
3155 807 SV **e = av_fetch(bav, i, 0);
3156 /*
3157 # uncoverable branch false class:ignore_covered_err
3158 # uncoverable condition right count:1 class:ignore_covered_err */
3159 1614 ▶ if (e) { STRLEN l; (void)SvPV(*e, l); blen += l; }
Branch: 1 branch
BranchTrueFalseTotal
if (e) { STRLEN l; (void)SvPV(*e, l); blen += l; } 807 0 807
3160 }
3161 /*
3162 # uncoverable branch false class:ignore_covered_err
3163 # uncoverable condition right count:1 class:ignore_covered_err
3164 # uncoverable condition right count:2 class:ignore_covered_err */
3165 18 ▶ } else if (s2 && SvROK(*s2)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (s2 && SvROK(*s2)) { 18 0 18
} else if (s2 && SvROK(*s2)) { 18 0 18
3166 /* filehandle or IO::Handle-like body: stream it when it lifts to
3167 * a byte-countable source, else slurp exactly as before */
3168 18 UV clv = 0;
3169 18 int cl_seen = hm_hav_clen(aTHX_ hav, &clv);
3170 18 ▶ if (!c->bsrc.kind && hm_bsrc_lift(aTHX_ *s2, &c->bsrc, cl_seen, clv)) /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!c->bsrc.kind && hm_bsrc_lift(aTHX_ *s2, &c->bsrc, cl_seen, clv)) /* # uncoverable condition right count:1 class:ignore_covered_err */ 18 0 18
if (!c->bsrc.kind && hm_bsrc_lift(aTHX_ *s2, &c->bsrc, cl_seen, clv)) /* # uncoverable condition right count:1 class:ignore_covered_err */ 14 4 18
3171 14 blen = (size_t)c->bsrc.remaining;
3172 else {
3173 4 fh_body = hm_slurp_body(aTHX_ *s2);
3174 4 { STRLEN l; (void)SvPV(fh_body, l); blen = l; }
3175 }
3176 }
3177  
3178 /* ---- compression decision, before a byte of the response is written --
3179 * Cheapest test first, and the whole block is skipped on a server that
3180 * did not ask for it. The rules, in order:
3181 * 1. enabled, and this request accepted gzip
3182 * 2. the response carries NO Content-Encoding. This one rule covers
3183 * every hands-off case at once - a framework's precompressed file
3184 * already says `gzip`, an opted-out route says `identity` (which
3185 * we strip below), and anything else was encoded deliberately.
3186 * 3. a plain in-memory body, not a glob/reader/streamed source:
3187 * compressing those would undo the 0.20 sendfile and drip paths.
3188 * 4. big enough to be worth a gzip member's header and trailer
3189 * 5. a status that carries an entity, and never 206 (a range
3190 * describes offsets into the UNCOMPRESSED representation)
3191 * 6. a compressible media type, from an allowlist */
3192 /*
3193 # uncoverable condition right count:3 class:ignore_covered_err
3194 # uncoverable condition right count:4 class:ignore_covered_err
3195 # uncoverable condition right count:5 class:ignore_covered_err
3196 # uncoverable condition left count:8 class:ignore_covered_err */
3197 831 ▶ if (loop->compress && c->accepts_gzip && bav && !c->bsrc.kind && !fh_body
Branch: 5 branches
BranchTrueFalseTotal
if (loop->compress && c->accepts_gzip && bav && !c->bsrc.kind && !fh_body 79 752 831
if (loop->compress && c->accepts_gzip && bav && !c->bsrc.kind && !fh_body 15 64 79
if (loop->compress && c->accepts_gzip && bav && !c->bsrc.kind && !fh_body 15 0 15
if (loop->compress && c->accepts_gzip && bav && !c->bsrc.kind && !fh_body 15 0 15
if (loop->compress && c->accepts_gzip && bav && !c->bsrc.kind && !fh_body 15 0 15
3198 15 ▶ && blen >= loop->compress_min
Branch: 1 branch
BranchTrueFalseTotal
&& blen >= loop->compress_min 13 2 15
3199 13 ▶ && (status == 200 || status == 203)) {
Branch: 2 branches
BranchTrueFalseTotal
&& (status == 200 || status == 203)) { 1 12 13
&& (status == 200 || status == 203)) { 0 1 1
3200 12 const char *ct = NULL;
3201 12 STRLEN ctl = 0;
3202 12 int enc_seen = 0;
3203 12 ▶ SSize_t i, hn = hav ? av_len(hav) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, hn = hav ? av_len(hav) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 12 0 12
3204 30 ▶ for (i = 0; i + 1 < hn; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < hn; i += 2) { 22 8 30
3205 22 SV **k = av_fetch(hav, i, 0);
3206 STRLEN kl;
3207 const char *ks;
3208 /*
3209 # uncoverable condition left count:1 class:ignore_covered_err
3210 # uncoverable condition left count:2 class:ignore_covered_err */
3211 26 ▶ if (!k || !*k) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!k || !*k) continue; 22 0 22
if (!k || !*k) continue; 0 22 22
3212 22 ks = SvPV(*k, kl);
3213 /* A response that sets a cookie is one a BREACH-style attacker
3214 * would want compressed beside a reflected input; left alone,
3215 * as the no-transform contract for a Set-Cookie response. */
3216 /*
3217 # uncoverable condition right count:2 class:ignore_covered_err
3218 # uncoverable condition right count:3 class:ignore_covered_err */
3219 22 ▶ if (kl == 10 && (ks[0] | 32) == 's'
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 10 && (ks[0] | 32) == 's' 1 21 22
if (kl == 10 && (ks[0] | 32) == 's' 1 0 1
3220 1 ▶ && hm_strncasecmp(ks, "Set-Cookie", 10) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
&& hm_strncasecmp(ks, "Set-Cookie", 10) == 0) { 1 0 1
3221 1 enc_seen = 1;
3222 4 break;
3223 }
3224 21 ▶ if ((ks[0] | 32) != 'c') continue;
Branch: 1 branch
BranchTrueFalseTotal
if ((ks[0] | 32) != 'c') continue; 4 17 21
3225 17 ▶ if (kl == 16 && hm_strncasecmp(ks, "Content-Encoding", 16) == 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 16 && hm_strncasecmp(ks, "Content-Encoding", 16) == 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 3 14 17
if (kl == 16 && hm_strncasecmp(ks, "Content-Encoding", 16) == 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 3 0 3
3226 3 enc_seen = 1;
3227 3 break;
3228 }
3229 14 ▶ if (kl == 12 && hm_strncasecmp(ks, "Content-Type", 12) == 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 12 && hm_strncasecmp(ks, "Content-Type", 12) == 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 12 2 14
if (kl == 12 && hm_strncasecmp(ks, "Content-Type", 12) == 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 12 0 12
3230 12 SV **v = av_fetch(hav, i + 1, 0);
3231 /*
3232 # uncoverable branch false class:ignore_covered_err
3233 # uncoverable condition right count:1 class:ignore_covered_err
3234 # uncoverable condition right count:2 class:ignore_covered_err */
3235 24 ▶ if (v && *v) ct = SvPV(*v, ctl);
Branch: 2 branches
BranchTrueFalseTotal
if (v && *v) ct = SvPV(*v, ctl); 12 0 12
if (v && *v) ct = SvPV(*v, ctl); 12 0 12
3236 }
3237 }
3238 12 ▶ if (!enc_seen && hz_type_ok(ct, ctl)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!enc_seen && hz_type_ok(ct, ctl)) { 8 4 12
if (!enc_seen && hz_type_ok(ct, ctl)) { 7 1 8
3239 /* one contiguous copy of the body, then one deflate */
3240 7 char *flat, *gz = NULL;
3241 7 size_t gzl = 0, off = 0;
3242 SSize_t j;
3243 7 ▶ Newx(flat, blen ? blen : 1, char); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
Newx(flat, blen ? blen : 1, char); /* # uncoverable condition right count:1 class:ignore_covered_err */ 7 0 7
3244 14 ▶ for (j = 0; j < bn; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < bn; j++) { 7 7 14
3245 7 SV **e = av_fetch(bav, j, 0);
3246 /*
3247 # uncoverable branch false class:ignore_covered_err
3248 # uncoverable condition right count:1 class:ignore_covered_err */
3249 7 ▶ if (e) {
Branch: 1 branch
BranchTrueFalseTotal
if (e) { 7 0 7
3250 STRLEN l;
3251 7 const char *p = SvPV(*e, l);
3252 /* Bounded by the length the first pass measured: an
3253 * overloaded or tied element whose stringification grew
3254 * between the two calls would otherwise run past flat. */
3255 /*
3256 # uncoverable branch true class:ignore_covered_err
3257 # uncoverable condition left count:1 class:ignore_covered_err */
3258 7 ▶ if (l > blen - off) l = blen - off;
Branch: 1 branch
BranchTrueFalseTotal
if (l > blen - off) l = blen - off; 0 7 7
3259 /*
3260 # uncoverable branch false class:ignore_covered_err
3261 # uncoverable condition right count:1 class:ignore_covered_err */
3262 7 ▶ if (l) { memcpy(flat + off, p, l); off += l; }
Branch: 1 branch
BranchTrueFalseTotal
if (l) { memcpy(flat + off, p, l); off += l; } 7 0 7
3263 }
3264 }
3265 /*
3266 # uncoverable branch false class:ignore_covered_err
3267 # uncoverable condition right count:1 class:ignore_covered_err */
3268 7 ▶ if (hz_gzip(flat, off, loop->compress_level, &gz, &gzl)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hz_gzip(flat, off, loop->compress_level, &gz, &gzl)) { 7 0 7
3269 /* route it down the fh_body path: one SV, one copy into the
3270 * write buffer. A response that just paid a deflate is not
3271 * the one to spend the writev fast path on. */
3272 7 fh_body = newSVpvn(gz, gzl);
3273 7 Safefree(gz);
3274 7 bav = NULL; bn = 0;
3275 7 blen = gzl;
3276 7 c->last_blen = blen;
3277 7 gz_out = 1;
3278 }
3279 7 Safefree(flat);
3280 }
3281 }
3282  
3283 /* status line + headers */
3284 831 c->last_status = status;
3285 831 c->last_blen = blen;
3286 {
3287 size_t sll;
3288 831 const char *sl = hm_status_line(status, &sll, line);
3289 831 hm_wb_put(c, sl, sll);
3290 }
3291 /*
3292 # uncoverable branch false class:ignore_covered_err
3293 # uncoverable condition right count:1 class:ignore_covered_err */
3294 831 ▶ if (hav) {
Branch: 1 branch
BranchTrueFalseTotal
if (hav) { 831 0 831
3295 831 SSize_t i, hn = av_len(hav) + 1;
3296 1748 ▶ for (i = 0; i + 1 < hn; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < hn; i += 2) { 917 831 1748
3297 917 SV **k = av_fetch(hav, i, 0);
3298 917 SV **v = av_fetch(hav, i + 1, 0);
3299 STRLEN kl, vl;
3300 const char *ks, *vs;
3301 /*
3302 # uncoverable condition left count:2 class:ignore_covered_err
3303 # uncoverable condition left count:3 class:ignore_covered_err
3304 # uncoverable condition left count:4 class:ignore_covered_err */
3305 924 ▶ if (!k || !*k || !v || !*v) continue; /* a hole in the array */
Branch: 4 branches
BranchTrueFalseTotal
if (!k || !*k || !v || !*v) continue; /* a hole in the array */ 916 1 917
if (!k || !*k || !v || !*v) continue; /* a hole in the array */ 916 0 916
if (!k || !*k || !v || !*v) continue; /* a hole in the array */ 916 0 916
if (!k || !*k || !v || !*v) continue; /* a hole in the array */ 0 916 916
3306 916 ks = SvPV(*k, kl);
3307 916 ▶ vs = SvPV(*v, vl);
Branch: 1 branch
BranchTrueFalseTotal
vs = SvPV(*v, vl); 644 272 916
3308 916 ▶ if (kl == 4 && (ks[0] | 32) == 'd' && hm_strncasecmp(ks, "Date", 4) == 0) /* # uncoverable condition right count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (kl == 4 && (ks[0] | 32) == 'd' && hm_strncasecmp(ks, "Date", 4) == 0) /* # uncoverable condition right count:3 class:ignore_covered_err */ 6 910 916
if (kl == 4 && (ks[0] | 32) == 'd' && hm_strncasecmp(ks, "Date", 4) == 0) /* # uncoverable condition right count:3 class:ignore_covered_err */ 1 5 6
if (kl == 4 && (ks[0] | 32) == 'd' && hm_strncasecmp(ks, "Date", 4) == 0) /* # uncoverable condition right count:3 class:ignore_covered_err */ 1 0 1
3309 1 has_date = 1;
3310 /* both interesting names start with 'c', so one folded-byte test
3311 * skips the case-blind scans for every other header */
3312 916 ▶ if ((ks[0] | 32) == 'c') {
Branch: 1 branch
BranchTrueFalseTotal
if ((ks[0] | 32) == 'c') { 901 15 916
3313 901 ▶ if (kl == 14 && hm_strncasecmp(ks, "Content-Length", 14) == 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 14 && hm_strncasecmp(ks, "Content-Length", 14) == 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 271 630 901
if (kl == 14 && hm_strncasecmp(ks, "Content-Length", 14) == 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 271 0 271
3314 271 has_len = 1;
3315 /* the app's length describes the bytes we just replaced */
3316 271 ▶ if (gz_out) { has_len = 0; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (gz_out) { has_len = 0; continue; } 2 269 271
3317 }
3318 899 ▶ if (kl == 10 && hm_strncasecmp(ks, "Connection", 10) == 0) has_conn = 1; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 10 && hm_strncasecmp(ks, "Connection", 10) == 0) has_conn = 1; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 898 899
if (kl == 10 && hm_strncasecmp(ks, "Connection", 10) == 0) has_conn = 1; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
3319 /* `identity` is the documented way for anything above to opt
3320 * a response out (see hm_compress.h). It has done its job by
3321 * now; sending it on would be noise. */
3322 /*
3323 # uncoverable condition right count:3 class:ignore_covered_err
3324 # uncoverable condition right count:4 class:ignore_covered_err
3325 # uncoverable condition right count:5 class:ignore_covered_err */
3326 899 ▶ if (kl == 16 && vl == 8 && loop->compress
Branch: 3 branches
BranchTrueFalseTotal
if (kl == 16 && vl == 8 && loop->compress 3 896 899
if (kl == 16 && vl == 8 && loop->compress 2 1 3
if (kl == 16 && vl == 8 && loop->compress 2 0 2
3327 2 ▶ && hm_strncasecmp(ks, "Content-Encoding", 16) == 0
Branch: 1 branch
BranchTrueFalseTotal
&& hm_strncasecmp(ks, "Content-Encoding", 16) == 0 2 0 2
3328 2 ▶ && hm_strncasecmp(vs, "identity", 8) == 0)
Branch: 1 branch
BranchTrueFalseTotal
&& hm_strncasecmp(vs, "identity", 8) == 0) 2 0 2
3329 2 continue;
3330 }
3331 /* The layer that compressed must be the layer that fixes the
3332 * validator, or a cache serves these bytes to a client that
3333 * asked for none. nginx does the same; it is in the POD because
3334 * an app author will otherwise find it by debugging. */
3335 912 ▶ if (gz_out && kl == 4 && hm_strncasecmp(ks, "ETag", 4) == 0) { /* # uncoverable condition right count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (gz_out && kl == 4 && hm_strncasecmp(ks, "ETag", 4) == 0) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 10 902 912
if (gz_out && kl == 4 && hm_strncasecmp(ks, "ETag", 4) == 0) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 3 7 10
if (gz_out && kl == 4 && hm_strncasecmp(ks, "ETag", 4) == 0) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 3 0 3
3336 3 hm_wb_put(c, ks, kl);
3337 3 hm_wb_put(c, ": ", 2);
3338 /*
3339 # uncoverable condition right count:1 class:ignore_covered_err */
3340 3 ▶ if (vl > 1 && vs[vl - 1] == '"') {
Branch: 2 branches
BranchTrueFalseTotal
if (vl > 1 && vs[vl - 1] == '"') { 3 0 3
if (vl > 1 && vs[vl - 1] == '"') { 2 1 3
3341 2 hm_wb_put(c, vs, vl - 1);
3342 2 hm_wb_put(c, "-gzip\"", 6);
3343 } else {
3344 1 hm_wb_put(c, vs, vl);
3345 1 hm_wb_put(c, "-gzip", 5);
3346 }
3347 3 hm_wb_put(c, "\r\n", 2);
3348 3 continue;
3349 }
3350 909 hm_wb_put(c, ks, kl);
3351 909 hm_wb_put(c, ": ", 2);
3352 909 hm_wb_put(c, vs, vl);
3353 909 hm_wb_put(c, "\r\n", 2);
3354 }
3355 }
3356 831 ▶ if (gz_out) {
Branch: 1 branch
BranchTrueFalseTotal
if (gz_out) { 7 824 831
3357 7 hm_wb_put(c, "Content-Encoding: gzip\r\n", 24);
3358 7 hm_wb_put(c, "Vary: Accept-Encoding\r\n", 23);
3359 }
3360 /* Alt-Svc: how a client finds out this origin also speaks HTTP/3. It is
3361 * advertised on the HTTP/1.1 and HTTP/2 responses and never on an h3 one,
3362 * where it would be telling a client what it is already using. The value
3363 * is precomputed per listener in hm_worker_listeners - never built from a
3364 * client-supplied Host, which would let a request choose the header. */
3365 /*
3366 # uncoverable branch true class:ignore_covered_err
3367 # uncoverable condition left count:1 class:ignore_covered_err
3368 # uncoverable condition right count:1 class:ignore_covered_err
3369 # uncoverable condition left count:2 class:ignore_covered_err */
3370 831 ▶ if (c->lst && c->lst->altsvc) {
Branch: 2 branches
BranchTrueFalseTotal
if (c->lst && c->lst->altsvc) { 831 0 831
if (c->lst && c->lst->altsvc) { 0 831 831
3371 0 hm_wb_put(c, "Alt-Svc: ", 9); /* # uncoverable statement class:ignore_covered_err */
3372 0 hm_wb_put(c, c->lst->altsvc, strlen(c->lst->altsvc)); /* # uncoverable statement class:ignore_covered_err */
3373 0 hm_wb_put(c, "\r\n", 2); /* # uncoverable statement class:ignore_covered_err */
3374 }
3375 /* 1xx/204/304 carry no entity: no Content-Length, no body (RFC 7230) */
3376 {
3377 831 ▶ int no_entity = (status < 200 || status == 204 || status == 304);
Branch: 3 branches
BranchTrueFalseTotal
int no_entity = (status < 200 || status == 204 || status == 304); 828 3 831
int no_entity = (status < 200 || status == 204 || status == 304); 825 3 828
int no_entity = (status < 200 || status == 204 || status == 304); 3 822 825
3378 831 ▶ if (no_entity) {
Branch: 1 branch
BranchTrueFalseTotal
if (no_entity) { 9 822 831
3379 /*
3380 # uncoverable branch true class:ignore_covered_err
3381 # uncoverable condition left count:1 class:ignore_covered_err */
3382 9 ▶ if (fh_body) { SvREFCNT_dec(fh_body); fh_body = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (fh_body) { SvREFCNT_dec(fh_body); fh_body = NULL; } 0 9 9
3383 9 ▶ if (c->bsrc.kind) hm_bsrc_release(aTHX_ &c->bsrc);
Branch: 1 branch
BranchTrueFalseTotal
if (c->bsrc.kind) hm_bsrc_release(aTHX_ &c->bsrc); 1 8 9
3384 9 bav = NULL; bn = 0; blen = 0;
3385 822 ▶ } else if (!has_len) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!has_len) { 553 269 822
3386 553 memcpy(line, "Content-Length: ", 16);
3387 553 ln = 16 + (int)hm_utoa(line + 16, (unsigned long)blen);
3388 553 line[ln++] = '\r'; line[ln++] = '\n';
3389 553 hm_wb_put(c, line, (size_t)ln);
3390 }
3391 /* HEAD: the headers of the GET, including its Content-Length, and
3392 * not one body byte - which the app, like most apps, produced
3393 * anyway. Sending it would be read as the start of the next
3394 * response on this connection. */
3395 831 ▶ if (!no_entity && c->is_head) {
Branch: 2 branches
BranchTrueFalseTotal
if (!no_entity && c->is_head) { 822 9 831
if (!no_entity && c->is_head) { 3 819 822
3396 /*
3397 # uncoverable branch true class:ignore_covered_err
3398 # uncoverable condition left count:1 class:ignore_covered_err */
3399 3 ▶ if (fh_body) { SvREFCNT_dec(fh_body); fh_body = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (fh_body) { SvREFCNT_dec(fh_body); fh_body = NULL; } 0 3 3
3400 3 ▶ if (c->bsrc.kind) hm_bsrc_release(aTHX_ &c->bsrc);
Branch: 1 branch
BranchTrueFalseTotal
if (c->bsrc.kind) hm_bsrc_release(aTHX_ &c->bsrc); 1 2 3
3401 3 bav = NULL; bn = 0; blen = 0;
3402 3 c->last_blen = 0;
3403 }
3404 }
3405 831 ▶ if (!has_date) {
Branch: 1 branch
BranchTrueFalseTotal
if (!has_date) { 830 1 831
3406 830 const char *dh = hm_date_header(loop);
3407 /*
3408 # uncoverable branch false class:ignore_covered_err
3409 # uncoverable condition right count:1 class:ignore_covered_err */
3410 830 ▶ if (dh) hm_wb_put(c, dh, HM_DATE_LEN);
Branch: 1 branch
BranchTrueFalseTotal
if (dh) hm_wb_put(c, dh, HM_DATE_LEN); 830 0 830
3411 }
3412 831 ▶ if (!has_conn)
Branch: 1 branch
BranchTrueFalseTotal
if (!has_conn) 830 1 831
3413 830 ▶ hm_wb_put(c, c->keepalive ? "Connection: keep-alive\r\n" : "Connection: close\r\n",
Branch: 1 branch
BranchTrueFalseTotal
hm_wb_put(c, c->keepalive ? "Connection: keep-alive\r\n" : "Connection: close\r\n", 160 670 830
3414 830 ▶ c->keepalive ? 24 : 19);
Branch: 1 branch
BranchTrueFalseTotal
c->keepalive ? 24 : 19); 160 670 830
3415 831 hm_wb_put(c, "\r\n", 2);
3416  
3417 831 loop->bytes_out += (c->wlen - hdr_start) + blen;
3418  
3419 /* fast path: nothing queued before this response -> writev header+body
3420 * straight from the response SVs, no body copy */
3421 /*
3422 # uncoverable condition right count:2 class:ignore_covered_err
3423 # uncoverable condition right count:3 class:ignore_covered_err
3424 # uncoverable condition left count:6 class:ignore_covered_err
3425 # uncoverable condition right count:9 class:ignore_covered_err
3426 # uncoverable condition right count:10 class:ignore_covered_err */
3427 831 ▶ if (hdr_start == 0 && c->woff == 0 && !c->writing && !fh_body
Branch: 4 branches
BranchTrueFalseTotal
if (hdr_start == 0 && c->woff == 0 && !c->writing && !fh_body 830 1 831
if (hdr_start == 0 && c->woff == 0 && !c->writing && !fh_body 830 0 830
if (hdr_start == 0 && c->woff == 0 && !c->writing && !fh_body 830 0 830
if (hdr_start == 0 && c->woff == 0 && !c->writing && !fh_body 819 11 830
3428 819 ▶ && (!c->ssl || hm_ktls_tx(c))
Branch: 2 branches
BranchTrueFalseTotal
&& (!c->ssl || hm_ktls_tx(c)) 43 776 819
&& (!c->ssl || hm_ktls_tx(c)) 43 0 43
3429 819 ▶ && !c->bsrc.kind && bav && bn >= 0 && bn < HM_IOV_MAX) {
Branch: 4 branches
BranchTrueFalseTotal
&& !c->bsrc.kind && bav && bn >= 0 && bn < HM_IOV_MAX) { 807 12 819
&& !c->bsrc.kind && bav && bn >= 0 && bn < HM_IOV_MAX) { 795 12 807
&& !c->bsrc.kind && bav && bn >= 0 && bn < HM_IOV_MAX) { 795 0 795
&& !c->bsrc.kind && bav && bn >= 0 && bn < HM_IOV_MAX) { 795 0 795
3430 hm_iovec iov[HM_IOV_MAX];
3431 795 size_t total = c->wlen;
3432 795 int niov = 1;
3433 SSize_t i;
3434 ssize_t n;
3435 795 HM_IOV_SET(iov, 0, c->wbuf, c->wlen);
3436 1588 ▶ for (i = 0; i < bn; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < bn; i++) { 793 795 1588
3437 793 SV **e = av_fetch(bav, i, 0);
3438 /*
3439 # uncoverable branch false class:ignore_covered_err
3440 # uncoverable condition right count:1 class:ignore_covered_err */
3441 793 ▶ if (e) {
Branch: 1 branch
BranchTrueFalseTotal
if (e) { 793 0 793
3442 793 STRLEN l; const char *p = SvPV(*e, l);
3443 /*
3444 # uncoverable branch false class:ignore_covered_err
3445 # uncoverable condition right count:1 class:ignore_covered_err */
3446 793 ▶ if (l) {
Branch: 1 branch
BranchTrueFalseTotal
if (l) { 793 0 793
3447 793 HM_IOV_SET(iov, niov, p, l);
3448 793 niov++;
3449 793 total += l;
3450 }
3451 }
3452 }
3453 795 n = hm_os_writev(c->fd, iov, niov);
3454 /*
3455 # uncoverable branch true class:ignore_covered_err
3456 # uncoverable condition left count:1 class:ignore_covered_err */
3457 795 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 0 795 795
3458 /*
3459 # uncoverable statement
3460 # uncoverable branch true
3461 # uncoverable branch false
3462 # uncoverable condition left count:1
3463 # uncoverable condition right count:1
3464 # uncoverable condition left count:2
3465 # uncoverable condition right count:2 */
3466 0 ▶ if (errno != EAGAIN && errno != EWOULDBLOCK) { hm_close(aTHX_ loop, c); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (errno != EAGAIN && errno != EWOULDBLOCK) { hm_close(aTHX_ loop, c); return -1; } 0 0 0
if (errno != EAGAIN && errno != EWOULDBLOCK) { hm_close(aTHX_ loop, c); return -1; } 0 0 0
3467 0 n = 0; /* # uncoverable statement */
3468 }
3469 /*
3470 # uncoverable branch false class:ignore_covered_err
3471 # uncoverable condition right count:1 class:ignore_covered_err */
3472 795 ▶ if ((size_t)n >= total) { c->wlen = 0; return 0; } /* all written */
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)n >= total) { c->wlen = 0; return 0; } /* all written */ 792 3 795
3473 /* partial: keep the unwritten remainder in wbuf */
3474 {
3475 3 size_t skip = (size_t)n; /* # uncoverable statement class:ignore_covered_err */
3476 3 size_t hdrlen = c->wlen; /* # uncoverable statement class:ignore_covered_err */
3477 /*
3478 # uncoverable branch true class:ignore_covered_err
3479 # uncoverable statement class:ignore_covered_err
3480 # uncoverable branch false class:ignore_covered_err
3481 # uncoverable condition left count:1 class:ignore_covered_err
3482 # uncoverable condition right count:1 class:ignore_covered_err */
3483 3 ▶ if (skip < hdrlen) {
Branch: 1 branch
BranchTrueFalseTotal
if (skip < hdrlen) { 0 3 3
3484 0 memmove(c->wbuf, c->wbuf + skip, hdrlen - skip); /* # uncoverable statement */
3485 0 c->wlen = hdrlen - skip; /* # uncoverable statement */
3486 0 skip = 0; /* # uncoverable statement */
3487 } else {
3488 3 skip -= hdrlen; /* # uncoverable statement class:ignore_covered_err */
3489 3 c->wlen = 0; /* # uncoverable statement class:ignore_covered_err */
3490 }
3491 /*
3492 # uncoverable statement class:ignore_covered_err
3493 # uncoverable branch true class:ignore_covered_err
3494 # uncoverable branch false class:ignore_covered_err
3495 # uncoverable condition left count:1 class:ignore_covered_err
3496 # uncoverable condition right count:1 class:ignore_covered_err */
3497 6 ▶ for (i = 0; i < bn; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < bn; i++) { 3 3 6
3498 3 SV **e = av_fetch(bav, i, 0); /* # uncoverable statement class:ignore_covered_err */
3499 /*
3500 # uncoverable statement class:ignore_covered_err
3501 # uncoverable branch true class:ignore_covered_err
3502 # uncoverable branch false class:ignore_covered_err
3503 # uncoverable condition left count:1 class:ignore_covered_err
3504 # uncoverable condition right count:1 class:ignore_covered_err */
3505 3 ▶ if (e) {
Branch: 1 branch
BranchTrueFalseTotal
if (e) { 3 0 3
3506 3 STRLEN l; const char *p = SvPV(*e, l); /* # uncoverable statement class:ignore_covered_err */
3507 /*
3508 # uncoverable branch true class:ignore_covered_err
3509 # uncoverable statement class:ignore_covered_err
3510 # uncoverable branch false class:ignore_covered_err
3511 # uncoverable condition left count:1 class:ignore_covered_err
3512 # uncoverable condition right count:1 class:ignore_covered_err */
3513 3 ▶ if (skip >= l) { skip -= l; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (skip >= l) { skip -= l; continue; } 0 3 3
3514 3 hm_wb_put(c, p + skip, l - skip); /* # uncoverable statement class:ignore_covered_err */
3515 3 skip = 0; /* # uncoverable statement class:ignore_covered_err */
3516 }
3517 }
3518 }
3519 3 loop->be->add_io(loop->be, c->fd, HM_EV_WRITE, 0); /* # uncoverable statement class:ignore_covered_err */
3520 3 c->writing = 1; /* # uncoverable statement class:ignore_covered_err */
3521 3 return 0; /* # uncoverable statement class:ignore_covered_err */
3522 }
3523  
3524 /* slow path: copy the body into the write buffer */
3525 36 ▶ if (fh_body) {
Branch: 1 branch
BranchTrueFalseTotal
if (fh_body) { 11 25 36
3526 11 STRLEN l; const char *p = SvPV(fh_body, l);
3527 11 hm_wb_put(c, p, l);
3528 11 SvREFCNT_dec(fh_body);
3529 25 ▶ } else if (bav) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (bav) { 1 24 25
3530 SSize_t i;
3531 2 ▶ for (i = 0; i < bn; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < bn; i++) { 1 1 2
3532 1 SV **e = av_fetch(bav, i, 0);
3533 /*
3534 # uncoverable branch false class:ignore_covered_err
3535 # uncoverable condition right count:1 class:ignore_covered_err */
3536 2 ▶ if (e) { STRLEN l; const char *p = SvPV(*e, l); hm_wb_put(c, p, l); }
Branch: 1 branch
BranchTrueFalseTotal
if (e) { STRLEN l; const char *p = SvPV(*e, l); hm_wb_put(c, p, l); } 1 0 1
3537 }
3538 }
3539 36 return 0;
3540 }
3541  
3542 /* Flush c->wbuf; on EAGAIN arm a write watcher. Returns 0 to keep the
3543 * connection, -1 if it was closed. A streaming body source (c->bsrc) is
3544 * drained here: buffer empties, source refills it a chunk at a time - or
3545 * skips the buffer entirely via sendfile(2) on a plaintext socket - so a
3546 * large file response never holds more than one chunk in memory. */
3547 3171 static int hm_flush(pTHX_ hm_conn *c) {
3548 3171 hm_loop *loop = c->loop;
3549 3171 int src_done = 0;
3550 3171 ▶ if (c->wlen == 0 && !c->bsrc.kind) {
Branch: 2 branches
BranchTrueFalseTotal
if (c->wlen == 0 && !c->bsrc.kind) { 2240 931 3171
if (c->wlen == 0 && !c->bsrc.kind) { 6 925 931
3551 925 ▶ if (!c->keepalive && !c->awaiting) { hm_close(aTHX_ loop, c); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (!c->keepalive && !c->awaiting) { hm_close(aTHX_ loop, c); return -1; } 695 230 925
if (!c->keepalive && !c->awaiting) { hm_close(aTHX_ loop, c); return -1; } 676 19 695
3552 249 ▶ if (c->stream_done) { /* a streamed body ended
Branch: 1 branch
BranchTrueFalseTotal
if (c->stream_done) { /* a streamed body ended 1 248 249
3553 * with nothing to flush */
3554 1 c->stream_done = 0;
3555 /*
3556 # uncoverable branch false class:ignore_covered_err
3557 # uncoverable condition right count:1 class:ignore_covered_err
3558 # uncoverable condition left count:2 class:ignore_covered_err
3559 # uncoverable condition right count:2 class:ignore_covered_err */
3560 1 ▶ if (c->rlen > 0 || loop->completion) hm_again_push(loop, c);
Branch: 2 branches
BranchTrueFalseTotal
if (c->rlen > 0 || loop->completion) hm_again_push(loop, c); 0 1 1
if (c->rlen > 0 || loop->completion) hm_again_push(loop, c); 0 0 0
3561 }
3562 249 return 0;
3563 }
3564 for (;;) {
3565 4353 ▶ while (c->woff < c->wlen) {
Branch: 1 branch
BranchTrueFalseTotal
while (c->woff < c->wlen) { 3342 1011 4353
3566 3342 ssize_t n = hm_cwrite(c, c->wbuf + c->woff, c->wlen - c->woff);
3567 3342 ▶ if (n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { 2043 1299 3342
3568 2043 c->woff += (size_t)n;
3569 /*
3570 # uncoverable condition right count:1 class:ignore_covered_err
3571 # uncoverable condition left count:3 class:ignore_covered_err */
3572 1299 ▶ } else if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { 1299 0 1299
} else if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { 2 1297 1299
} else if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { 0 2 2
3573 /* arm a write watcher, unless the TLS engine is blocked
3574 * wanting a read (the always-armed read watcher re-drives
3575 * us then) */
3576 1297 ▶ if (!c->writing && !c->tls_w_wants_r) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!c->writing && !c->tls_w_wants_r) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 5 1292 1297
if (!c->writing && !c->tls_w_wants_r) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 5 0 5
3577 5 loop->be->add_io(loop->be, c->fd, HM_EV_WRITE, 0);
3578 5 c->writing = 1;
3579 }
3580 1297 return 0;
3581 } else {
3582 2 hm_close(aTHX_ loop, c);
3583 2 return -1;
3584 }
3585 }
3586 /* buffer drained; keep the capacity for the next round */
3587 1011 c->wlen = c->woff = 0;
3588 1011 ▶ if (!c->bsrc.kind) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!c->bsrc.kind) break; 930 81 1011
3589  
3590 #if defined(HM_HAVE_SENDFILE_LINUX) || defined(HM_HAVE_SENDFILE_FREEBSD) \
3591 || defined(HM_HAVE_SENDFILE_MACOS)
3592 /* file straight to a plaintext socket, no copy through userspace */
3593 /*
3594 # uncoverable condition left count:3 class:ignore_covered_err
3595 # uncoverable condition right count:4 class:ignore_covered_err */
3596 81 ▶ if (c->bsrc.kind == 1 && (!c->ssl || hm_ktls_tx(c)) && !c->bsrc.nosf) {
Branch: 4 branches
BranchTrueFalseTotal
if (c->bsrc.kind == 1 && (!c->ssl || hm_ktls_tx(c)) && !c->bsrc.nosf) { 37 44 81
if (c->bsrc.kind == 1 && (!c->ssl || hm_ktls_tx(c)) && !c->bsrc.nosf) { 1 36 37
if (c->bsrc.kind == 1 && (!c->ssl || hm_ktls_tx(c)) && !c->bsrc.nosf) { 1 0 1
if (c->bsrc.kind == 1 && (!c->ssl || hm_ktls_tx(c)) && !c->bsrc.nosf) { 37 0 37
3597 37 size_t want = c->bsrc.remaining < HM_SENDFILE_MAX
3598 ? (size_t)c->bsrc.remaining : HM_SENDFILE_MAX;
3599 37 ssize_t sent = -1;
3600 37 int again = 0;
3601 #if defined(HM_HAVE_SENDFILE_LINUX)
3602 37 off_t off = c->bsrc.off;
3603 37 sent = sendfile(c->fd, c->bsrc.fd, &off, want);
3604 /*
3605 # uncoverable condition right count:2 class:ignore_covered_err
3606 # uncoverable condition left count:3 class:ignore_covered_err
3607 # uncoverable condition right count:3 class:ignore_covered_err */
3608 37 ▶ again = (sent < 0 && (errno == EAGAIN || errno == EWOULDBLOCK));
Branch: 3 branches
BranchTrueFalseTotal
again = (sent < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)); 7 30 37
again = (sent < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)); 1 6 7
again = (sent < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)); 0 1 1
3609 #elif defined(HM_HAVE_SENDFILE_FREEBSD)
3610 off_t sbytes = 0;
3611 int r = sendfile(c->bsrc.fd, c->fd, c->bsrc.off, want,
3612 NULL, &sbytes, 0);
3613 if (r == 0 || errno == EAGAIN || errno == EWOULDBLOCK) {
3614 sent = (ssize_t)sbytes;
3615 again = (r != 0);
3616 }
3617 #elif defined(HM_HAVE_SENDFILE_MACOS)
3618 off_t len = (off_t)want;
3619 int r = sendfile(c->bsrc.fd, c->fd, c->bsrc.off, &len, NULL, 0);
3620 if (r == 0 || errno == EAGAIN || errno == EWOULDBLOCK) {
3621 sent = (ssize_t)len;
3622 again = (r != 0);
3623 }
3624 #endif
3625 37 ▶ if (sent > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (sent > 0) { 30 7 37
3626 30 c->bsrc.off += sent;
3627 30 c->bsrc.remaining -= (UV)sent;
3628 }
3629 37 ▶ if (c->bsrc.remaining == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->bsrc.remaining == 0) { 10 27 37
3630 10 hm_bsrc_release(aTHX_ &c->bsrc);
3631 10 src_done = 1;
3632 10 break;
3633 }
3634 /*
3635 # uncoverable branch false class:ignore_covered_err
3636 # uncoverable condition right count:1 class:ignore_covered_err
3637 # uncoverable condition right count:2 class:ignore_covered_err */
3638 27 ▶ if (again || sent > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (again || sent > 0) { 21 6 27
if (again || sent > 0) { 20 1 21
3639 26 ▶ if (again) {
Branch: 1 branch
BranchTrueFalseTotal
if (again) { 6 20 26
3640 6 ▶ if (!c->writing) {
Branch: 1 branch
BranchTrueFalseTotal
if (!c->writing) { 1 5 6
3641 1 loop->be->add_io(loop->be, c->fd, HM_EV_WRITE, 0);
3642 1 c->writing = 1;
3643 }
3644 6 return 0;
3645 }
3646 20 continue; /* kernel took it all; try more */
3647 }
3648 /* sendfile refused this pairing (filesystem, socket state):
3649 * fall back to the read path for the rest of this body */
3650 1 c->bsrc.nosf = 1; /* # uncoverable statement class:ignore_covered_err */
3651 }
3652 #endif
3653 {
3654 45 ssize_t n = hm_bsrc_fill(aTHX_ c);
3655 45 ▶ if (n <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) { 1 44 45
3656 /* spent (0), or ended early (-1, keepalive already off) */
3657 1 hm_bsrc_release(aTHX_ &c->bsrc);
3658 1 src_done = 1;
3659 /*
3660 # uncoverable branch true
3661 # uncoverable condition left count:1 class:ignore_covered_err */
3662 1 ▶ if (c->wlen) continue; /* write what the fill added */
Branch: 1 branch
BranchTrueFalseTotal
if (c->wlen) continue; /* write what the fill added */ 0 1 1
3663 1 break;
3664 }
3665 }
3666 }
3667 /* fully written; keep the buffer + capacity for the next response */
3668 941 c->wlen = c->woff = 0;
3669 941 ▶ if (c->writing) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->writing) { 8 933 941
3670 8 loop->be->remove_io(loop->be, c->fd, HM_EV_WRITE);
3671 8 c->writing = 0;
3672 }
3673 /* A stream handle that was told to stop producing can start again. The
3674 * callback may write, and writing calls back in here, so re-read the
3675 * connection afterwards rather than deciding on what was true before. */
3676 941 ▶ if (hm_stream_paused_n) {
Branch: 1 branch
BranchTrueFalseTotal
if (hm_stream_paused_n) { 1 940 941
3677 1 int fd = c->fd;
3678 1 hm_stream_drained(aTHX_ c);
3679 /*
3680 # uncoverable branch true
3681 # uncoverable condition left count:1 class:ignore_covered_err */
3682 1 ▶ if (loop->conns[fd] != c) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (loop->conns[fd] != c) return -1; 0 1 1
3683 1 ▶ if (c->wlen || c->bsrc.kind) return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (c->wlen || c->bsrc.kind) return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 1 1
if (c->wlen || c->bsrc.kind) return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 0 0
3684 }
3685 /* hm_process stopped on a full write buffer with requests still in the
3686 * read buffer: the client has now read, so serve the next of them. */
3687 940 ▶ if (c->backlogged) { c->backlogged = 0; hm_again_push(loop, c); }
Branch: 1 branch
BranchTrueFalseTotal
if (c->backlogged) { c->backlogged = 0; hm_again_push(loop, c); } 1 939 940
3688 940 ▶ if (!c->keepalive && !c->awaiting) { hm_close(aTHX_ loop, c); return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (!c->keepalive && !c->awaiting) { hm_close(aTHX_ loop, c); return -1; } 880 60 940
if (!c->keepalive && !c->awaiting) { hm_close(aTHX_ loop, c); return -1; } 54 826 880
3689 /* requests that pipelined in behind the stream were parked; pick them
3690 * up on the next loop turn */
3691 /* Pipelined requests behind the stream are picked up next turn - and in
3692 * completion mode the SAME push is what re-arms the read, which is why
3693 * an empty buffer is scheduled too. Without that a connection that
3694 * streamed a body would never be read from again. */
3695 886 ▶ if (c->stream_done) { c->stream_done = 0; src_done = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->stream_done) { c->stream_done = 0; src_done = 1; } 8 878 886
3696 886 ▶ if (src_done && (c->rlen > 0 || loop->completion)) hm_again_push(loop, c); /* # uncoverable condition left count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (src_done && (c->rlen > 0 || loop->completion)) hm_again_push(loop, c); /* # uncoverable condition left count:3 class:ignore_covered_err */ 18 868 886
if (src_done && (c->rlen > 0 || loop->completion)) hm_again_push(loop, c); /* # uncoverable condition left count:3 class:ignore_covered_err */ 15 3 18
if (src_done && (c->rlen > 0 || loop->completion)) hm_again_push(loop, c); /* # uncoverable condition left count:3 class:ignore_covered_err */ 0 15 15
3697 886 return 0;
3698 }
3699  
3700 /* ---- async delivery (Future + streaming) --------------------------------- */
3701  
3702 /* Deliver an async response to a parked connection. No-op if the
3703 * connection is gone or the fd was reused. */
3704 12 static void hm_deliver(pTHX_ int fd, UV id, SV *resp) {
3705 12 hm_loop *loop = hm_cur_loop;
3706 /*
3707 # uncoverable condition right count:1 class:ignore_covered_err
3708 # uncoverable condition right count:2 class:ignore_covered_err
3709 # uncoverable condition right count:3 class:ignore_covered_err */
3710 12 ▶ hm_conn *c = (loop && fd >= 0 && fd < HM_MAXFD) ? loop->conns[fd] : NULL;
Branch: 3 branches
BranchTrueFalseTotal
hm_conn *c = (loop && fd >= 0 && fd < HM_MAXFD) ? loop->conns[fd] : NULL; 12 0 12
hm_conn *c = (loop && fd >= 0 && fd < HM_MAXFD) ? loop->conns[fd] : NULL; 12 0 12
hm_conn *c = (loop && fd >= 0 && fd < HM_MAXFD) ? loop->conns[fd] : NULL; 12 0 12
3711 12 ▶ if (!(c && c->id == id)) return; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(c && c->id == id)) return; /* # uncoverable condition left count:2 class:ignore_covered_err */ 10 2 12
if (!(c && c->id == id)) return; /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 10 10
3712 /* Only a parked connection is owed a response. A responder called a
3713 * second time, or the 500 for a streaming handler that died AFTER it
3714 * had already answered, would otherwise be serialised onto a live
3715 * keep-alive connection as the reply to its next request. */
3716 10 ▶ if (!c->awaiting) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!c->awaiting) return; 1 9 10
3717 /* A streamed response is open on this connection: a stream handle
3718 * opened after the handler parked (the documented pattern), whose
3719 * Future has now resolved with a value that is not a response, or a
3720 * second response for one request. Serialising it here put a status
3721 * line inside a chunked body. The handle owns the response; this one
3722 * is dropped. (A handler that DIED with a writer alive is the other
3723 * case, and hm_delayed closes the connection for that.) */
3724 /*
3725 # uncoverable branch true
3726 # uncoverable condition left count:1 class:ignore_covered_err */
3727 9 ▶ if (c->stream_open) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->stream_open) { 0 9 9
3728 /*
3729 # uncoverable statement
3730 # uncoverable branch true
3731 # uncoverable branch false
3732 # uncoverable condition left count:1
3733 # uncoverable condition right count:1 */
3734 0 ▶ if (c->resp_f) { SvREFCNT_dec(c->resp_f); c->resp_f = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->resp_f) { SvREFCNT_dec(c->resp_f); c->resp_f = NULL; } 0 0 0
3735 0 return; /* # uncoverable statement */
3736 }
3737 /* detached from inside a Future continuation (async work before an
3738 * upgrade): the app owns the socket, so drop the response it resolved
3739 * with and forget the connection */
3740 /*
3741 # uncoverable branch true
3742 # uncoverable condition left count:1 class:ignore_covered_err */
3743 9 ▶ if (c->detached) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->detached) { 0 9 9
3744 0 c->awaiting = 0; /* # uncoverable statement */
3745 /*
3746 # uncoverable statement
3747 # uncoverable branch true
3748 # uncoverable branch false
3749 # uncoverable condition left count:1
3750 # uncoverable condition right count:1 */
3751 0 ▶ if (c->env_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->env_sv) { 0 0 0
3752 0 hm_access_log(aTHX_ loop, c->env_sv, 101, -1); /* # uncoverable statement */
3753 0 SvREFCNT_dec(c->env_sv); /* # uncoverable statement */
3754 0 c->env_sv = NULL; /* # uncoverable statement */
3755 }
3756 0 hm_conn_release(aTHX_ loop, c); /* # uncoverable statement */
3757 0 return; /* # uncoverable statement */
3758 }
3759 9 c->awaiting = 0;
3760 9 ▶ if (c->resp_f) { SvREFCNT_dec(c->resp_f); c->resp_f = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->resp_f) { SvREFCNT_dec(c->resp_f); c->resp_f = NULL; } 6 3 9
3761 9 ▶ if (c->env_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->env_sv) { 2 7 9
3762 2 SV *env_rv = c->env_sv;
3763 2 c->env_sv = NULL;
3764 /*
3765 # uncoverable branch false class:ignore_covered_err
3766 # uncoverable condition right count:1 class:ignore_covered_err
3767 # uncoverable condition right count:2 class:ignore_covered_err */
3768 2 ▶ if (hm_queue_response(aTHX_ c, resp) == 0 && loop->conns[fd] == c) {
Branch: 2 branches
BranchTrueFalseTotal
if (hm_queue_response(aTHX_ c, resp) == 0 && loop->conns[fd] == c) { 2 0 2
if (hm_queue_response(aTHX_ c, resp) == 0 && loop->conns[fd] == c) { 2 0 2
3769 2 hm_access_log(aTHX_ loop, env_rv, c->last_status,
3770 2 (ssize_t)c->last_blen);
3771 2 SvREFCNT_dec(env_rv);
3772 /*
3773 # uncoverable condition left count:1 class:ignore_covered_err
3774 # uncoverable condition left count:2 class:ignore_covered_err
3775 # uncoverable condition right count:2 class:ignore_covered_err */
3776 2 ▶ if (hm_flush(aTHX_ c) == 0 && loop->conns[fd] == c) {
Branch: 2 branches
BranchTrueFalseTotal
if (hm_flush(aTHX_ c) == 0 && loop->conns[fd] == c) { 0 2 2
if (hm_flush(aTHX_ c) == 0 && loop->conns[fd] == c) { 0 0 0
3777 0 hm_process(aTHX_ c); /* # uncoverable statement */
3778 /*
3779 # uncoverable statement
3780 # uncoverable branch true
3781 # uncoverable branch false
3782 # uncoverable condition left count:1
3783 # uncoverable condition right count:1 */
3784 0 ▶ if (loop->conns[fd] == c) hm_flush(aTHX_ c);
Branch: 1 branch
BranchTrueFalseTotal
if (loop->conns[fd] == c) hm_flush(aTHX_ c); 0 0 0
3785 }
3786 } else {
3787 0 SvREFCNT_dec(env_rv); /* # uncoverable statement */
3788 }
3789 /* Parked connections have no read outstanding - the completion
3790 * handler skipped arming one while the response was being made - so
3791 * the re-arm is scheduled here, for the top of the next turn. */
3792 /*
3793 # uncoverable branch true class:ignore_covered_err
3794 # uncoverable condition left count:1 class:ignore_covered_err
3795 # uncoverable condition left count:2 class:ignore_covered_err
3796 # uncoverable condition right count:2 class:ignore_covered_err
3797 # uncoverable condition left count:3 class:ignore_covered_err
3798 # uncoverable condition right count:3 class:ignore_covered_err */
3799 2 ▶ if (loop->conns[fd] == c && !c->awaiting && !c->bsrc.kind)
Branch: 3 branches
BranchTrueFalseTotal
if (loop->conns[fd] == c && !c->awaiting && !c->bsrc.kind) 0 2 2
if (loop->conns[fd] == c && !c->awaiting && !c->bsrc.kind) 0 0 0
if (loop->conns[fd] == c && !c->awaiting && !c->bsrc.kind) 0 0 0
3800 0 hm_again_push(loop, c); /* # uncoverable statement */
3801 2 return;
3802 }
3803 /*
3804 # uncoverable branch false class:ignore_covered_err
3805 # uncoverable condition right count:1 class:ignore_covered_err
3806 # uncoverable condition right count:2 class:ignore_covered_err
3807 # uncoverable condition right count:3 class:ignore_covered_err
3808 # uncoverable condition right count:4 class:ignore_covered_err */
3809 7 ▶ if (hm_queue_response(aTHX_ c, resp) == 0
Branch: 1 branch
BranchTrueFalseTotal
if (hm_queue_response(aTHX_ c, resp) == 0 7 0 7
3810 7 ▶ && loop->conns[fd] == c
Branch: 1 branch
BranchTrueFalseTotal
&& loop->conns[fd] == c 7 0 7
3811 7 ▶ && hm_flush(aTHX_ c) == 0
Branch: 1 branch
BranchTrueFalseTotal
&& hm_flush(aTHX_ c) == 0 7 0 7
3812 7 ▶ && loop->conns[fd] == c) {
Branch: 1 branch
BranchTrueFalseTotal
&& loop->conns[fd] == c) { 7 0 7
3813 7 hm_process(aTHX_ c);
3814 /*
3815 # uncoverable branch false class:ignore_covered_err
3816 # uncoverable condition right count:1 class:ignore_covered_err */
3817 7 ▶ if (loop->conns[fd] == c) hm_flush(aTHX_ c);
Branch: 1 branch
BranchTrueFalseTotal
if (loop->conns[fd] == c) hm_flush(aTHX_ c); 7 0 7
3818 }
3819 /*
3820 # uncoverable condition right count:2 class:ignore_covered_err
3821 # uncoverable condition right count:3 class:ignore_covered_err */
3822 7 ▶ if (loop->conns[fd] == c && !c->awaiting && !c->bsrc.kind)
Branch: 3 branches
BranchTrueFalseTotal
if (loop->conns[fd] == c && !c->awaiting && !c->bsrc.kind) 5 2 7
if (loop->conns[fd] == c && !c->awaiting && !c->bsrc.kind) 5 0 5
if (loop->conns[fd] == c && !c->awaiting && !c->bsrc.kind) 5 0 5
3823 5 hm_again_push(loop, c);
3824 }
3825  
3826 /* How a streamed HTTP/1.1 body is framed, decided once in hm_start_stream:
3827 * EOF - Connection: close, the body ends when the connection does. The
3828 * only framing there was before 0.57, and still the one for
3829 * HTTP/1.0, a request that asked to close, a 101 tunnel, an app
3830 * that wrote its own Transfer-Encoding, and a draining server.
3831 * CHUNKED - Transfer-Encoding: chunked, keep-alive kept. Every write is one
3832 * chunk, close sends the last one, and the pipeline resumes.
3833 * LENGTH - the app gave Content-Length: bytes are counted; an overrun or
3834 * a short body breaks the framing, so the connection closes.
3835 * NOBODY - 1xx, 204, 304: headers only, keep-alive kept. */
3836 #define HM_SM_EOF 0
3837 #define HM_SM_CHUNKED 1
3838 #define HM_SM_LENGTH 2
3839 #define HM_SM_NOBODY 3
3840  
3841 /* Send status+headers for a streaming response; body follows via
3842 * hm_stream_write/hm_stream_close. */
3843 /* 1 when the response was begun; 0 when it was refused because this
3844 * connection is not parked on a response, or one has already begun; -1
3845 * when the headers were refused (a 500 went out instead). Only a PARKED
3846 * connection with no response begun: a second responder call, or a stale
3847 * responder kept from an earlier request on this keep-alive connection,
3848 * otherwise wrote a status line and headers into the body being streamed,
3849 * or into the next request's response. */
3850 45 static int hm_start_stream(pTHX_ int fd, UV id, int status, SV *headers) {
3851 45 hm_loop *loop = hm_cur_loop;
3852 /*
3853 # uncoverable condition right count:1 class:ignore_covered_err
3854 # uncoverable condition right count:2 class:ignore_covered_err
3855 # uncoverable condition right count:3 class:ignore_covered_err */
3856 45 ▶ hm_conn *c = (loop && fd >= 0 && fd < HM_MAXFD) ? loop->conns[fd] : NULL;
Branch: 3 branches
BranchTrueFalseTotal
hm_conn *c = (loop && fd >= 0 && fd < HM_MAXFD) ? loop->conns[fd] : NULL; 45 0 45
hm_conn *c = (loop && fd >= 0 && fd < HM_MAXFD) ? loop->conns[fd] : NULL; 45 0 45
hm_conn *c = (loop && fd >= 0 && fd < HM_MAXFD) ? loop->conns[fd] : NULL; 45 0 45
3857 char line[64];
3858 45 AV *hav = NULL;
3859 45 int has_date = 0, has_conn = 0, conn_close = 0, has_te = 0, has_len = 0;
3860 45 UV clen = 0;
3861 /*
3862 # uncoverable branch true class:ignore_covered_err
3863 # uncoverable condition left count:1 class:ignore_covered_err
3864 # uncoverable condition left count:2 class:ignore_covered_err */
3865 45 ▶ if (!(c && c->id == id)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!(c && c->id == id)) return 0; 45 0 45
if (!(c && c->id == id)) return 0; 0 45 45
3866 45 ▶ if (!c->awaiting || c->stream_open) return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!c->awaiting || c->stream_open) return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */ 45 0 45
if (!c->awaiting || c->stream_open) return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */ 2 43 45
3867 43 c->stream_open = 1;
3868 43 c->stream_serial++;
3869 43 c->stream_mode = HM_SM_EOF;
3870 43 c->stream_left = 0;
3871 43 c->stream_done = 0;
3872 43 ▶ if (c->env_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->env_sv) { 11 32 43
3873 11 hm_access_log(aTHX_ loop, c->env_sv, status, -1);
3874 11 SvREFCNT_dec(c->env_sv);
3875 11 c->env_sv = NULL;
3876 }
3877 /*
3878 # uncoverable branch false class:ignore_covered_err
3879 # uncoverable condition right count:1 class:ignore_covered_err
3880 # uncoverable condition right count:2 class:ignore_covered_err
3881 # uncoverable condition right count:3 class:ignore_covered_err */
3882 43 ▶ if (headers && SvROK(headers) && SvTYPE(SvRV(headers)) == SVt_PVAV)
Branch: 3 branches
BranchTrueFalseTotal
if (headers && SvROK(headers) && SvTYPE(SvRV(headers)) == SVt_PVAV) 43 0 43
if (headers && SvROK(headers) && SvTYPE(SvRV(headers)) == SVt_PVAV) 43 0 43
if (headers && SvROK(headers) && SvTYPE(SvRV(headers)) == SVt_PVAV) 43 0 43
3883 43 hav = (AV *)SvRV(headers);
3884 43 ▶ if (!hm_headers_ok(aTHX_ hav)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_headers_ok(aTHX_ hav)) { 1 42 43
3885 1 hm_errlog("Hyperman: response header carries CR, LF or NUL; refused\n");
3886 1 hm_wb_put(c, HM_ERR500, sizeof(HM_ERR500) - 1);
3887 1 c->last_status = 500;
3888 1 c->last_blen = 21;
3889 1 c->awaiting = 0; /* nothing more is owed on this connection */
3890 1 c->stream_open = 0;
3891 1 hm_flush(aTHX_ c);
3892 1 return -1;
3893 }
3894 {
3895 size_t sll;
3896 42 const char *sl = hm_status_line(status, &sll, line);
3897 42 hm_wb_put(c, sl, sll);
3898 }
3899 /*
3900 # uncoverable branch false class:ignore_covered_err
3901 # uncoverable condition right count:1 class:ignore_covered_err */
3902 42 ▶ if (hav) {
Branch: 1 branch
BranchTrueFalseTotal
if (hav) { 42 0 42
3903 42 SSize_t i, hn = av_len(hav) + 1;
3904 94 ▶ for (i = 0; i + 1 < hn; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < hn; i += 2) { 52 42 94
3905 52 SV **k = av_fetch(hav, i, 0);
3906 52 SV **v = av_fetch(hav, i + 1, 0);
3907 STRLEN kl, vl;
3908 const char *ks, *vs;
3909 /*
3910 # uncoverable branch true class:ignore_covered_err
3911 # uncoverable condition left count:1 class:ignore_covered_err
3912 # uncoverable condition left count:2 class:ignore_covered_err
3913 # uncoverable condition left count:3 class:ignore_covered_err
3914 # uncoverable condition left count:4 class:ignore_covered_err */
3915 52 ▶ if (!k || !*k || !v || !*v) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!k || !*k || !v || !*v) continue; 52 0 52
if (!k || !*k || !v || !*v) continue; 52 0 52
if (!k || !*k || !v || !*v) continue; 52 0 52
if (!k || !*k || !v || !*v) continue; 0 52 52
3916 52 ks = SvPV(*k, kl);
3917 52 ▶ vs = SvPV(*v, vl);
Branch: 1 branch
BranchTrueFalseTotal
vs = SvPV(*v, vl); 50 2 52
3918 /*
3919 # uncoverable condition right count:2 class:ignore_covered_err
3920 # uncoverable condition right count:3 class:ignore_covered_err */
3921 52 ▶ if (kl == 4 && (ks[0] | 32) == 'd' && hm_strncasecmp(ks, "Date", 4) == 0)
Branch: 3 branches
BranchTrueFalseTotal
if (kl == 4 && (ks[0] | 32) == 'd' && hm_strncasecmp(ks, "Date", 4) == 0) 1 51 52
if (kl == 4 && (ks[0] | 32) == 'd' && hm_strncasecmp(ks, "Date", 4) == 0) 1 0 1
if (kl == 4 && (ks[0] | 32) == 'd' && hm_strncasecmp(ks, "Date", 4) == 0) 1 0 1
3922 1 has_date = 1;
3923 /*
3924 # uncoverable condition right count:2 class:ignore_covered_err
3925 # uncoverable condition right count:3 class:ignore_covered_err */
3926 51 ▶ else if (kl == 10 && (ks[0] | 32) == 'c'
Branch: 2 branches
BranchTrueFalseTotal
else if (kl == 10 && (ks[0] | 32) == 'c' 9 42 51
else if (kl == 10 && (ks[0] | 32) == 'c' 9 0 9
3927 9 ▶ && hm_strncasecmp(ks, "Connection", 10) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
&& hm_strncasecmp(ks, "Connection", 10) == 0) { 9 0 9
3928 9 has_conn = 1;
3929 9 ▶ if (hm_ci_contains(vs, vl, "close")) conn_close = 1; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
if (hm_ci_contains(vs, vl, "close")) conn_close = 1; /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 9 9
3930 }
3931 /*
3932 # uncoverable condition right count:2 class:ignore_covered_err
3933 # uncoverable condition right count:3 class:ignore_covered_err */
3934 42 ▶ else if (kl == 14 && (ks[0] | 32) == 'c'
Branch: 2 branches
BranchTrueFalseTotal
else if (kl == 14 && (ks[0] | 32) == 'c' 3 39 42
else if (kl == 14 && (ks[0] | 32) == 'c' 3 0 3
3935 3 ▶ && hm_strncasecmp(ks, "Content-Length", 14) == 0)
Branch: 1 branch
BranchTrueFalseTotal
&& hm_strncasecmp(ks, "Content-Length", 14) == 0) 3 0 3
3936 3 has_len = 1;
3937 /*
3938 # uncoverable condition right count:2 class:ignore_covered_err
3939 # uncoverable condition right count:3 class:ignore_covered_err */
3940 39 ▶ else if (kl == 17 && (ks[0] | 32) == 't'
Branch: 2 branches
BranchTrueFalseTotal
else if (kl == 17 && (ks[0] | 32) == 't' 1 38 39
else if (kl == 17 && (ks[0] | 32) == 't' 1 0 1
3941 1 ▶ && hm_strncasecmp(ks, "Transfer-Encoding", 17) == 0)
Branch: 1 branch
BranchTrueFalseTotal
&& hm_strncasecmp(ks, "Transfer-Encoding", 17) == 0) 1 0 1
3942 1 has_te = 1;
3943 52 hm_wb_put(c, ks, kl);
3944 52 hm_wb_put(c, ": ", 2);
3945 52 hm_wb_put(c, vs, vl);
3946 52 hm_wb_put(c, "\r\n", 2);
3947 }
3948 }
3949 42 ▶ if (!has_date) {
Branch: 1 branch
BranchTrueFalseTotal
if (!has_date) { 41 1 42
3950 41 const char *dh = hm_date_header(loop);
3951 /*
3952 # uncoverable branch false class:ignore_covered_err
3953 # uncoverable condition right count:1 class:ignore_covered_err */
3954 41 ▶ if (dh) hm_wb_put(c, dh, HM_DATE_LEN);
Branch: 1 branch
BranchTrueFalseTotal
if (dh) hm_wb_put(c, dh, HM_DATE_LEN); 41 0 41
3955 }
3956 42 ▶ if (has_len && !hm_hav_clen(aTHX_ hav, &clen)) has_len = 0; /* unparseable */
Branch: 2 branches
BranchTrueFalseTotal
if (has_len && !hm_hav_clen(aTHX_ hav, &clen)) has_len = 0; /* unparseable */ 3 39 42
if (has_len && !hm_hav_clen(aTHX_ hav, &clen)) has_len = 0; /* unparseable */ 1 2 3
3957 {
3958 42 ▶ int no_entity = (status < 200 || status == 204 || status == 304); /* # uncoverable condition left count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
int no_entity = (status < 200 || status == 204 || status == 304); /* # uncoverable condition left count:3 class:ignore_covered_err */ 33 9 42
int no_entity = (status < 200 || status == 204 || status == 304); /* # uncoverable condition left count:3 class:ignore_covered_err */ 32 1 33
int no_entity = (status < 200 || status == 204 || status == 304); /* # uncoverable condition left count:3 class:ignore_covered_err */ 0 32 32
3959 /*
3960 # uncoverable condition left count:2 class:ignore_covered_err
3961 # uncoverable condition left count:5 class:ignore_covered_err
3962 # uncoverable condition left count:6 class:ignore_covered_err */
3963 42 ▶ if (status == 101 || loop->stopping || !c->req_11 || !c->keepalive
Branch: 4 branches
BranchTrueFalseTotal
if (status == 101 || loop->stopping || !c->req_11 || !c->keepalive 33 9 42
if (status == 101 || loop->stopping || !c->req_11 || !c->keepalive 33 0 33
if (status == 101 || loop->stopping || !c->req_11 || !c->keepalive 32 1 33
if (status == 101 || loop->stopping || !c->req_11 || !c->keepalive 13 19 32
3964 13 ▶ || conn_close || has_te) {
Branch: 2 branches
BranchTrueFalseTotal
|| conn_close || has_te) { 13 0 13
|| conn_close || has_te) { 0 13 13
3965 29 c->stream_mode = HM_SM_EOF;
3966 29 c->keepalive = 0;
3967 /*
3968 # uncoverable branch true
3969 # uncoverable condition left count:1 class:ignore_covered_err */
3970 13 ▶ } else if (no_entity) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (no_entity) { 0 13 13
3971 0 c->stream_mode = HM_SM_NOBODY; /* # uncoverable statement */
3972 13 ▶ } else if (has_len) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (has_len) { 2 11 13
3973 2 c->stream_mode = HM_SM_LENGTH;
3974 2 c->stream_left = clen;
3975 } else {
3976 11 c->stream_mode = HM_SM_CHUNKED;
3977 11 hm_wb_put(c, "Transfer-Encoding: chunked\r\n", 28);
3978 }
3979 }
3980 /* A 101 is an upgrade: the caller's own Upgrade and Connection headers
3981 * are the response, and "close" would contradict them. The connection
3982 * still ends when the handle closes - keepalive is off above. */
3983 42 ▶ if (status == 101) hm_wb_put(c, "\r\n", 2);
Branch: 1 branch
BranchTrueFalseTotal
if (status == 101) hm_wb_put(c, "\r\n", 2); 9 33 42
3984 33 ▶ else if (c->stream_mode == HM_SM_EOF) hm_wb_put(c, "Connection: close\r\n\r\n", 21);
Branch: 1 branch
BranchTrueFalseTotal
else if (c->stream_mode == HM_SM_EOF) hm_wb_put(c, "Connection: close\r\n\r\n", 21); 20 13 33
3985 else {
3986 /*
3987 # uncoverable branch false class:ignore_covered_err
3988 # uncoverable condition right count:1 class:ignore_covered_err */
3989 13 ▶ if (!has_conn) hm_wb_put(c, "Connection: keep-alive\r\n", 24);
Branch: 1 branch
BranchTrueFalseTotal
if (!has_conn) hm_wb_put(c, "Connection: keep-alive\r\n", 24); 13 0 13
3990 13 hm_wb_put(c, "\r\n", 2);
3991 }
3992 42 hm_flush(aTHX_ c);
3993 42 return 1;
3994 }
3995  
3996 /* `serial` names the streamed response a Writer was handed out for; 0 skips
3997 * the check (the ABI stream handle has a registry of its own for that). A
3998 * writer kept past its response, or closed twice, must not touch the
3999 * response that followed on the same keep-alive connection. */
4000 /* Queue body bytes on a streamed HTTP/1.1 response, framed as the mode says.
4001 * Shared by the Writer (hm_stream_write) and the ABI stream handle
4002 * (hm_stream.h), so the two cannot disagree about the framing. Returns the
4003 * octets queued: 0 when nothing was (HEAD, no body, past the end), and the
4004 * caller then has nothing to flush. */
4005 2111 static STRLEN hm_stream_body_put(pTHX_ hm_conn *c, const char *p, STRLEN l) {
4006 /*
4007 # uncoverable branch true class:ignore_covered_err
4008 # uncoverable condition left count:1 class:ignore_covered_err
4009 # uncoverable condition left count:2 class:ignore_covered_err
4010 # uncoverable condition right count:2 class:ignore_covered_err */
4011 2111 ▶ if (!c->awaiting && !c->tunnel) return 0; /* the body has ended */
Branch: 2 branches
BranchTrueFalseTotal
if (!c->awaiting && !c->tunnel) return 0; /* the body has ended */ 0 2111 2111
if (!c->awaiting && !c->tunnel) return 0; /* the body has ended */ 0 0 0
4012 2111 ▶ if (c->is_head && !c->tunnel) return 0; /* HEAD: headers only */ /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (c->is_head && !c->tunnel) return 0; /* HEAD: headers only */ /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 2107 2111
if (c->is_head && !c->tunnel) return 0; /* HEAD: headers only */ /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 0 4
4013 /*
4014 # uncoverable branch true
4015 # uncoverable condition left count:1 class:ignore_covered_err */
4016 2107 ▶ if (c->stream_mode == HM_SM_NOBODY) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (c->stream_mode == HM_SM_NOBODY) return 0; 0 2107 2107
4017 2107 ▶ if (c->stream_mode == HM_SM_LENGTH) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->stream_mode == HM_SM_LENGTH) { 3 2104 2107
4018 3 ▶ if (l > c->stream_left) { /* overrun: the framing is
Branch: 1 branch
BranchTrueFalseTotal
if (l > c->stream_left) { /* overrun: the framing is 1 2 3
4019 * broken, end the conn */
4020 1 l = (STRLEN)c->stream_left;
4021 1 c->keepalive = 0;
4022 }
4023 3 c->stream_left -= l;
4024 }
4025 2107 ▶ if (!l) return 0; /* a zero chunk ends a body */
Branch: 1 branch
BranchTrueFalseTotal
if (!l) return 0; /* a zero chunk ends a body */ 3 2104 2107
4026 2104 ▶ if (c->stream_mode == HM_SM_CHUNKED) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->stream_mode == HM_SM_CHUNKED) { 17 2087 2104
4027 char sz[24];
4028 17 int n = snprintf(sz, sizeof(sz), "%" UVxf "\r\n", (UV)l);
4029 17 hm_wb_put(c, sz, (size_t)n);
4030 17 hm_wb_put(c, p, l);
4031 17 hm_wb_put(c, "\r\n", 2);
4032 } else {
4033 2087 hm_wb_put(c, p, l);
4034 }
4035 2104 c->loop->bytes_out += l;
4036 2104 return l;
4037 }
4038  
4039 /* 1: queued, and the connection holds less than HM_WB_HIWAT unwritten.
4040 * 0: queued, but the connection is holding more than that - the producer
4041 * should wait for a later write to answer 1 again before writing more
4042 * (the Perl Writer's ->write hands this back; before 0.62 it returned
4043 * nothing, and a producer feeding a slow reader grew the buffer without
4044 * bound).
4045 * -1: not written: the writer is stale (closed, or from an earlier
4046 * request on this connection). */
4047 43 static int hm_stream_write(pTHX_ int fd, UV id, UV serial, SV *data) {
4048 43 hm_loop *loop = hm_cur_loop;
4049 /*
4050 # uncoverable condition right count:2 class:ignore_covered_err
4051 # uncoverable condition right count:3 class:ignore_covered_err */
4052 43 ▶ hm_conn *c = (loop && fd >= 0 && fd < HM_MAXFD) ? loop->conns[fd] : NULL;
Branch: 3 branches
BranchTrueFalseTotal
hm_conn *c = (loop && fd >= 0 && fd < HM_MAXFD) ? loop->conns[fd] : NULL; 41 2 43
hm_conn *c = (loop && fd >= 0 && fd < HM_MAXFD) ? loop->conns[fd] : NULL; 41 0 41
hm_conn *c = (loop && fd >= 0 && fd < HM_MAXFD) ? loop->conns[fd] : NULL; 41 0 41
4053 STRLEN l;
4054 const char *p;
4055 43 ▶ if (!(c && c->id == id)) return -1; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(c && c->id == id)) return -1; /* # uncoverable condition left count:2 class:ignore_covered_err */ 40 3 43
if (!(c && c->id == id)) return -1; /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 40 40
4056 /*
4057 # uncoverable branch true class:ignore_covered_err
4058 # uncoverable condition left count:1 class:ignore_covered_err
4059 # uncoverable condition left count:2 class:ignore_covered_err */
4060 40 ▶ if (serial && c->stream_serial != serial) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (serial && c->stream_serial != serial) return -1; 38 2 40
if (serial && c->stream_serial != serial) return -1; 0 38 38
4061 40 ▶ if (!c->awaiting && !c->tunnel) return -1; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!c->awaiting && !c->tunnel) return -1; /* # uncoverable condition right count:2 class:ignore_covered_err */ 3 37 40
if (!c->awaiting && !c->tunnel) return -1; /* # uncoverable condition right count:2 class:ignore_covered_err */ 3 0 3
4062 37 p = SvPV(data, l);
4063 37 ▶ if (hm_stream_body_put(aTHX_ c, p, l)) hm_flush(aTHX_ c);
Branch: 1 branch
BranchTrueFalseTotal
if (hm_stream_body_put(aTHX_ c, p, l)) hm_flush(aTHX_ c); 30 7 37
4064 /*
4065 # uncoverable branch true
4066 # uncoverable condition left count:1 class:ignore_covered_err */
4067 37 ▶ if (loop->conns[fd] != c) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (loop->conns[fd] != c) return -1; 0 37 37
4068 37 return (c->wlen - c->woff) > HM_WB_HIWAT ? 0 : 1;
4069 }
4070  
4071 /* End a streamed HTTP/1.1 body. `trailers` is a list from hm_trailers_build
4072 * or NULL, borrowed for the duration of the call.
4073 *
4074 * Trailer fields have exactly one home in HTTP/1.1: between the last chunk
4075 * and the blank line that ends a chunked body (RFC 9112 7.1.2). So they go
4076 * out when the body is chunked, the request was not a HEAD, and the client's
4077 * TE asked for them. Otherwise the body ends as it would have with no list,
4078 * since a counted or EOF-delimited body has no place for them and a client
4079 * that did not ask may not parse them. Every name and value was checked by
4080 * hm_trailers_build, so nothing here can put a bare CR or LF on the wire. */
4081 63 static void hm_stream_finish(pTHX_ int fd, UV id, UV serial, AV *trailers) {
4082 63 hm_loop *loop = hm_cur_loop;
4083 /*
4084 # uncoverable condition right count:1 class:ignore_covered_err
4085 # uncoverable condition right count:2 class:ignore_covered_err
4086 # uncoverable condition right count:3 class:ignore_covered_err */
4087 63 ▶ hm_conn *c = (loop && fd >= 0 && fd < HM_MAXFD) ? loop->conns[fd] : NULL;
Branch: 3 branches
BranchTrueFalseTotal
hm_conn *c = (loop && fd >= 0 && fd < HM_MAXFD) ? loop->conns[fd] : NULL; 62 1 63
hm_conn *c = (loop && fd >= 0 && fd < HM_MAXFD) ? loop->conns[fd] : NULL; 62 0 62
hm_conn *c = (loop && fd >= 0 && fd < HM_MAXFD) ? loop->conns[fd] : NULL; 62 0 62
4088 63 ▶ if (!(c && c->id == id)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!(c && c->id == id)) return; 47 16 63
if (!(c && c->id == id)) return; 2 45 47
4089 /*
4090 # uncoverable branch true class:ignore_covered_err
4091 # uncoverable condition left count:1 class:ignore_covered_err
4092 # uncoverable condition left count:2 class:ignore_covered_err */
4093 45 ▶ if (serial && c->stream_serial != serial) return;
Branch: 2 branches
BranchTrueFalseTotal
if (serial && c->stream_serial != serial) return; 27 18 45
if (serial && c->stream_serial != serial) return; 0 27 27
4094 45 ▶ if (!c->awaiting) return; /* closed already */
Branch: 1 branch
BranchTrueFalseTotal
if (!c->awaiting) return; /* closed already */ 8 37 45
4095 37 c->stream_open = 0;
4096 37 ▶ if (c->stream_mode == HM_SM_CHUNKED && !c->is_head) {
Branch: 2 branches
BranchTrueFalseTotal
if (c->stream_mode == HM_SM_CHUNKED && !c->is_head) { 9 28 37
if (c->stream_mode == HM_SM_CHUNKED && !c->is_head) { 8 1 9
4097 8 ▶ if (trailers && c->te_trailers) {
Branch: 2 branches
BranchTrueFalseTotal
if (trailers && c->te_trailers) { 0 8 8
if (trailers && c->te_trailers) { 0 0 0
4098 0 SV **field = AvARRAY(trailers);
4099 0 SSize_t i, n = av_len(trailers) + 1;
4100 0 hm_wb_put(c, "0\r\n", 3);
4101 0 ▶ for (i = 0; i < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i += 2) { 0 0 0
4102 0 hm_wb_put(c, SvPVX_const(field[i]), SvCUR(field[i]));
4103 0 hm_wb_put(c, ": ", 2);
4104 0 hm_wb_put(c, SvPVX_const(field[i + 1]), SvCUR(field[i + 1]));
4105 0 hm_wb_put(c, "\r\n", 2);
4106 }
4107 0 hm_wb_put(c, "\r\n", 2);
4108 }
4109 8 else hm_wb_put(c, "0\r\n\r\n", 5);
4110 }
4111 29 ▶ else if (c->stream_mode == HM_SM_LENGTH && c->stream_left && !c->is_head) /* # uncoverable condition right count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
else if (c->stream_mode == HM_SM_LENGTH && c->stream_left && !c->is_head) /* # uncoverable condition right count:3 class:ignore_covered_err */ 2 27 29
else if (c->stream_mode == HM_SM_LENGTH && c->stream_left && !c->is_head) /* # uncoverable condition right count:3 class:ignore_covered_err */ 1 1 2
else if (c->stream_mode == HM_SM_LENGTH && c->stream_left && !c->is_head) /* # uncoverable condition right count:3 class:ignore_covered_err */ 1 0 1
4112 1 c->keepalive = 0; /* short body: cannot keep
4113 * the connection in frame */
4114 37 c->stream_mode = HM_SM_EOF;
4115 37 c->stream_left = 0;
4116 37 c->awaiting = 0;
4117 37 ▶ if (c->keepalive) c->stream_done = 1; /* hm_flush: serve the
Branch: 1 branch
BranchTrueFalseTotal
if (c->keepalive) c->stream_done = 1; /* hm_flush: serve the 9 28 37
4118 * pipeline when drained */
4119 37 hm_flush(aTHX_ c); /* closes when drained if keepalive is off */
4120 }
4121  
4122 25 static void hm_stream_close(pTHX_ int fd, UV id, UV serial) {
4123 25 hm_stream_finish(aTHX_ fd, id, serial, NULL);
4124 25 }
4125  
4126 /* The parked-Future continuation: when the response Future resolves,
4127 * deliver its result (or a 500 on failure, logged to psgi.errors). */
4128 XS_INTERNAL(hm_xs_park_cb);
4129 7 XS_INTERNAL(hm_xs_park_cb) {
4130 7 dXSARGS;
4131 7 hm_clos *cl = hm_clos_of(aTHX_ cv);
4132 7 SV *f, *resp = NULL;
4133 IV st;
4134 int fd;
4135 UV id;
4136 /*
4137 # uncoverable branch true class:ignore_covered_err
4138 # uncoverable condition left count:1 class:ignore_covered_err
4139 # uncoverable condition left count:2 class:ignore_covered_err */
4140 7 ▶ if (!cl || items < 1) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!cl || items < 1) XSRETURN_EMPTY; 7 0 7
if (!cl || items < 1) XSRETURN_EMPTY; 0 7 7
4141 7 f = ST(0);
4142 7 fd = (int)cl->i;
4143 7 id = cl->u;
4144 7 st = hm_any_state(aTHX_ f);
4145 7 ▶ if (st == HMF_DONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (st == HMF_DONE) { 5 2 7
4146 5 ▶ if (hmf_is_future(aTHX_ f)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_is_future(aTHX_ f)) { 5 0 5
4147 5 AV *vals = hmf_values_av(aTHX_ f);
4148 /*
4149 # uncoverable condition right count:1 class:ignore_covered_err
4150 # uncoverable condition right count:2 class:ignore_covered_err */
4151 5 ▶ SV **e = (vals && av_len(vals) >= 0) ? av_fetch(vals, 0, 0) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
SV **e = (vals && av_len(vals) >= 0) ? av_fetch(vals, 0, 0) : NULL; 5 0 5
SV **e = (vals && av_len(vals) >= 0) ? av_fetch(vals, 0, 0) : NULL; 5 0 5
4152 5 ▶ resp = e ? sv_mortalcopy(*e) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
resp = e ? sv_mortalcopy(*e) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 5 0 5
4153 } else {
4154 0 dSP;
4155 0 SV *got = NULL;
4156 int n;
4157 0 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 0 0
4158 0 ▶ XPUSHs(f);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(f); 0 0 0
4159 0 PUTBACK;
4160 0 n = call_method("get", G_SCALAR | G_EVAL);
4161 0 SPAGAIN;
4162 /*
4163 # uncoverable branch false class:ignore_covered_err
4164 # uncoverable condition right count:1 class:ignore_covered_err
4165 # uncoverable condition right count:2 class:ignore_covered_err */
4166 0 ▶ if (n && !SvTRUE(ERRSV)) got = newSVsv(POPs);
Branch: 3 branches
BranchTrueFalseTotal
if (n && !SvTRUE(ERRSV)) got = newSVsv(POPs); 0 0 0
if (n && !SvTRUE(ERRSV)) got = newSVsv(POPs); 0 0 0
if (n && !SvTRUE(ERRSV)) got = newSVsv(POPs); 0 0 0
4167 /*
4168 # uncoverable statement
4169 # uncoverable branch true
4170 # uncoverable branch false
4171 # uncoverable condition left count:1
4172 # uncoverable condition right count:1 */
4173 0 ▶ else if (n) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
else if (n) (void)POPs; 0 0 0
4174 0 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 0 0 0
4175 /*
4176 # uncoverable branch false class:ignore_covered_err
4177 # uncoverable condition right count:1 class:ignore_covered_err */
4178 0 ▶ if (got) resp = sv_2mortal(got); /* mortal in the outer frame */
Branch: 1 branch
BranchTrueFalseTotal
if (got) resp = sv_2mortal(got); /* mortal in the outer frame */ 0 0 0
4179 }
4180 2 ▶ } else if (st == HMF_FAILED) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (st == HMF_FAILED) { 1 1 2
4181 1 AV *errs = newAV();
4182 SV **e;
4183 STRLEN l;
4184 1 const char *msg = "unknown error";
4185 1 hm_any_values(aTHX_ f, st, errs);
4186 1 ▶ e = av_len(errs) >= 0 ? av_fetch(errs, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
e = av_len(errs) >= 0 ? av_fetch(errs, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 0 1
4187 /*
4188 # uncoverable branch false class:ignore_covered_err
4189 # uncoverable condition right count:1 class:ignore_covered_err
4190 # uncoverable condition right count:2 class:ignore_covered_err */
4191 2 ▶ if (e && SvOK(*e)) msg = SvPV(*e, l);
Branch: 2 branches
BranchTrueFalseTotal
if (e && SvOK(*e)) msg = SvPV(*e, l); 1 0 1
if (e && SvOK(*e)) msg = SvPV(*e, l); 1 0 1
4192 1 hm_errlog("Hyperman: async response failed: %s%s",
4193 /*
4194 # uncoverable condition right count:1 class:ignore_covered_err
4195 # uncoverable condition right count:2 class:ignore_covered_err */
4196 1 ▶ msg, (*msg && msg[strlen(msg)-1] == '\n') ? "" : "\n");
Branch: 2 branches
BranchTrueFalseTotal
msg, (*msg && msg[strlen(msg)-1] == '\n') ? "" : "\n"); 1 0 1
msg, (*msg && msg[strlen(msg)-1] == '\n') ? "" : "\n"); 1 0 1
4197 1 SvREFCNT_dec((SV *)errs);
4198 }
4199 /*
4200 # uncoverable condition left count:2 class:ignore_covered_err
4201 # uncoverable condition left count:3 class:ignore_covered_err */
4202 7 ▶ if (!(resp && SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV))
Branch: 3 branches
BranchTrueFalseTotal
if (!(resp && SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV)) 5 2 7
if (!(resp && SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV)) 5 0 5
if (!(resp && SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV)) 0 5 5
4203 2 ▶ resp = hm_500_resp(aTHX_ st == HMF_DONE
Branch: 1 branch
BranchTrueFalseTotal
resp = hm_500_resp(aTHX_ st == HMF_DONE 0 2 2
4204 ? "Bad async response" : "Internal Server Error");
4205 7 hm_deliver(aTHX_ fd, id, resp);
4206 7 XSRETURN_EMPTY;
4207 }
4208  
4209 /* Park a connection awaiting a pending Future. */
4210 8 static void hm_park(pTHX_ hm_conn *c, SV *future) {
4211 8 SV *cb = hm_closure(aTHX_ hm_xs_park_cb, NULL, NULL, NULL, NULL,
4212 8 (IV)c->fd, c->id);
4213 8 hm_any_on_ready(aTHX_ future, cb);
4214 8 SvREFCNT_dec(cb);
4215 8 }
4216  
4217 /* psgi.streaming responder: [$s,$h,$b] delivers now; [$s,$h] starts a
4218 * stream and returns a Hyperman::Writer. */
4219 XS_INTERNAL(hm_xs_responder);
4220 30 XS_INTERNAL(hm_xs_responder) {
4221 30 dXSARGS;
4222 30 hm_clos *cl = hm_clos_of(aTHX_ cv);
4223 SV *r;
4224 AV *rav;
4225 SSize_t n;
4226 int fd;
4227 UV id;
4228 /*
4229 # uncoverable branch true class:ignore_covered_err
4230 # uncoverable condition left count:1 class:ignore_covered_err */
4231 30 ▶ if (!cl) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!cl) XSRETURN_EMPTY; 0 30 30
4232 30 fd = (int)cl->i;
4233 30 id = cl->u;
4234 /*
4235 # uncoverable condition left count:1 class:ignore_covered_err
4236 # uncoverable condition left count:3 class:ignore_covered_err */
4237 30 ▶ if (items < 1 || !(SvROK(ST(0)) && SvTYPE(SvRV(ST(0))) == SVt_PVAV))
Branch: 3 branches
BranchTrueFalseTotal
if (items < 1 || !(SvROK(ST(0)) && SvTYPE(SvRV(ST(0))) == SVt_PVAV)) 30 0 30
if (items < 1 || !(SvROK(ST(0)) && SvTYPE(SvRV(ST(0))) == SVt_PVAV)) 29 1 30
if (items < 1 || !(SvROK(ST(0)) && SvTYPE(SvRV(ST(0))) == SVt_PVAV)) 0 29 29
4238 1 croak("Hyperman responder expects an array reference");
4239 29 r = ST(0);
4240 29 rav = (AV *)SvRV(r);
4241 29 n = av_len(rav) + 1;
4242 29 ▶ if (n >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (n >= 3) { 2 27 29
4243 2 ▶ hm_conn *c = (hm_cur_loop && fd >= 0 && fd < HM_MAXFD)
Branch: 2 branches
BranchTrueFalseTotal
hm_conn *c = (hm_cur_loop && fd >= 0 && fd < HM_MAXFD) 2 0 2
hm_conn *c = (hm_cur_loop && fd >= 0 && fd < HM_MAXFD) 2 0 2
4244 4 ▶ ? hm_cur_loop->conns[fd] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? hm_cur_loop->conns[fd] : NULL; 2 0 2
4245 /*
4246 # uncoverable condition right count:1 class:ignore_covered_err
4247 # uncoverable condition right count:2 class:ignore_covered_err */
4248 2 ▶ if (c && c->id == id && c->stream_open)
Branch: 3 branches
BranchTrueFalseTotal
if (c && c->id == id && c->stream_open) 2 0 2
if (c && c->id == id && c->stream_open) 2 0 2
if (c && c->id == id && c->stream_open) 1 1 2
4249 1 croak("Hyperman responder: a response has already been started "
4250 "on this connection");
4251 1 hm_deliver(aTHX_ fd, id, r);
4252 1 XSRETURN_EMPTY;
4253 }
4254 27 ▶ if (n == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 2) { 26 1 27
4255 26 SV **s0 = av_fetch(rav, 0, 0);
4256 26 SV **s1 = av_fetch(rav, 1, 0);
4257 26 AV *w = newAV();
4258 SV *wrv;
4259 /*
4260 # uncoverable condition right count:1 class:ignore_covered_err
4261 # uncoverable condition right count:2 class:ignore_covered_err */
4262 26 ▶ int began = hm_start_stream(aTHX_ fd, id, s0 ? (int)SvIV(*s0) : 200,
Branch: 2 branches
BranchTrueFalseTotal
int began = hm_start_stream(aTHX_ fd, id, s0 ? (int)SvIV(*s0) : 200, 26 0 26
int began = hm_start_stream(aTHX_ fd, id, s0 ? (int)SvIV(*s0) : 200, 26 0 26
4263 s1 ? *s1 : NULL);
4264 26 ▶ if (began == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (began == 0) { 2 24 26
4265 2 SvREFCNT_dec((SV *)w);
4266 2 croak("Hyperman responder: a response has already been started "
4267 "on this connection");
4268 }
4269 24 ▶ if (began < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (began < 0) { 1 23 24
4270 1 SvREFCNT_dec((SV *)w);
4271 1 croak("Hyperman responder: a response header carries CR, LF or NUL");
4272 }
4273 23 av_push(w, newSViv(fd));
4274 23 av_push(w, newSVuv(id));
4275 /* the serial of the response just begun, read back from the conn
4276 * (hm_start_stream is a no-op on a stale ticket, so 0 then) */
4277 {
4278 23 ▶ hm_conn *c = (hm_cur_loop && fd >= 0 && fd < HM_MAXFD)
Branch: 2 branches
BranchTrueFalseTotal
hm_conn *c = (hm_cur_loop && fd >= 0 && fd < HM_MAXFD) 23 0 23
hm_conn *c = (hm_cur_loop && fd >= 0 && fd < HM_MAXFD) 23 0 23
4279 46 ▶ ? hm_cur_loop->conns[fd] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? hm_cur_loop->conns[fd] : NULL; 23 0 23
4280 /*
4281 # uncoverable condition right count:1 class:ignore_covered_err
4282 # uncoverable condition right count:2 class:ignore_covered_err */
4283 23 ▶ av_push(w, newSVuv(c && c->id == id ? c->stream_serial : 0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(w, newSVuv(c && c->id == id ? c->stream_serial : 0)); 23 0 23
av_push(w, newSVuv(c && c->id == id ? c->stream_serial : 0)); 23 0 23
4284 }
4285 23 av_push(w, newSVpvs("h1"));
4286 23 wrv = newRV_noinc((SV *)w);
4287 23 sv_bless(wrv, gv_stashpv("Hyperman::Writer", GV_ADD));
4288 23 ST(0) = sv_2mortal(wrv);
4289 23 XSRETURN(1);
4290 }
4291 1 croak("Hyperman responder expects [status, headers, body] or [status, headers]");
4292 }
4293  
4294 /* psgi.streaming: run the app's coderef with a responder. */
4295 40 static void hm_delayed(pTHX_ hm_conn *c, SV *code) {
4296 40 SV *responder = hm_closure(aTHX_ hm_xs_responder, NULL, NULL, NULL, NULL,
4297 40 (IV)c->fd, c->id);
4298 40 int fd = c->fd;
4299 40 UV id = c->id;
4300 40 dSP;
4301 40 ENTER; SAVETMPS;
4302 40 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 40 40
4303 40 ▶ XPUSHs(responder);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(responder); 0 40 40
4304 40 PUTBACK;
4305 40 call_sv(code, G_DISCARD | G_EVAL);
4306 40 ▶ if (SvTRUE(ERRSV)) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 40 0 40
if (SvTRUE(ERRSV)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 6 34 40
4307 6 hm_loop *loop = hm_cur_loop;
4308 /*
4309 # uncoverable condition right count:1 class:ignore_covered_err
4310 # uncoverable condition right count:2 class:ignore_covered_err
4311 # uncoverable condition right count:3 class:ignore_covered_err */
4312 6 ▶ hm_conn *c = (loop && fd >= 0 && fd < HM_MAXFD) ? loop->conns[fd] : NULL;
Branch: 3 branches
BranchTrueFalseTotal
hm_conn *c = (loop && fd >= 0 && fd < HM_MAXFD) ? loop->conns[fd] : NULL; 6 0 6
hm_conn *c = (loop && fd >= 0 && fd < HM_MAXFD) ? loop->conns[fd] : NULL; 6 0 6
hm_conn *c = (loop && fd >= 0 && fd < HM_MAXFD) ? loop->conns[fd] : NULL; 6 0 6
4313 6 ▶ if (c && c->id == id && c->awaiting && c->stream_open) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 4 branches
BranchTrueFalseTotal
if (c && c->id == id && c->awaiting && c->stream_open) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 5 1 6
if (c && c->id == id && c->awaiting && c->stream_open) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 5 0 5
if (c && c->id == id && c->awaiting && c->stream_open) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 1 5
if (c && c->id == id && c->awaiting && c->stream_open) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 2 4
4314 /* It answered through the responder, kept the writer alive, and
4315 * died with the body part written. A 500 cannot follow a status
4316 * line already sent, and ending the body as if complete would
4317 * hand the client a cut body it may cache: the connection ends
4318 * once what was written has drained, WITHOUT the terminating
4319 * chunk. (A writer that was a lexical has already been
4320 * destroyed by the unwind and ended the body itself, the
4321 * promise a dropped writer keeps.) */
4322 2 hm_warn_died(aTHX_ "streaming handler");
4323 2 c->stream_open = 0;
4324 2 c->stream_mode = HM_SM_EOF;
4325 2 c->stream_done = 0;
4326 2 c->keepalive = 0;
4327 2 c->awaiting = 0;
4328 /*
4329 # uncoverable branch true class:ignore_covered_err
4330 # uncoverable condition left count:1 class:ignore_covered_err */
4331 2 ▶ if (c->resp_f) { SvREFCNT_dec(c->resp_f); c->resp_f = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->resp_f) { SvREFCNT_dec(c->resp_f); c->resp_f = NULL; } 0 2 2
4332 2 hm_flush(aTHX_ c);
4333 } else {
4334 4 hm_deliver(aTHX_ fd, id, hm_500_resp(aTHX_ "Internal Server Error"));
4335 }
4336 }
4337 40 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 4 36 40
4338 40 SvREFCNT_dec(responder);
4339 40 }
4340  
4341 /* ---- trailer fields: checked once, for both transports and both doors --- */
4342  
4343 #include "hm_trailers.h"
4344  
4345 /* ---- HTTP/2 (h2c via nghttp2), reusing the helpers above ----------------- */
4346  
4347 #include "hm_http2.h"
4348  
4349 /* ---- ABI v6 stream handles, over both halves above ---------------------- */
4350  
4351 #include "hm_stream.h"
4352  
4353 /* ---- HTTP/3 over QUIC: the transport, then the semantics ---------------- */
4354  
4355 #include "hm_quic.h"
4356 #include "hm_http3.h"
4357  
4358 /* ---- request processing -------------------------------------------------- */
4359  
4360 12 static void hm_again_push(hm_loop *loop, hm_conn *c) {
4361 12 ▶ if (loop->again_n == loop->again_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (loop->again_n == loop->again_cap) { 8 4 12
4362 8 ▶ loop->again_cap = loop->again_cap ? loop->again_cap * 2 : 64; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
loop->again_cap = loop->again_cap ? loop->again_cap * 2 : 64; /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 8 8
4363 8 loop->again = (hm_again *)hm_xrealloc(loop->again,
4364 8 loop->again_cap * sizeof(hm_again));
4365 }
4366 12 loop->again[loop->again_n].fd = c->fd;
4367 12 loop->again[loop->again_n].id = c->id;
4368 12 loop->again_n++;
4369 12 c->again_queued = 1;
4370 12 }
4371  
4372 /* Built-in https redirect: synthesize a 301 to the same host and target on
4373 * the https port, so a plain :80 listener can bounce browsers to :443 without
4374 * ever touching the app. Host comes from the request (its :port is stripped);
4375 * the target port is omitted when it is the standard 443. */
4376 2 static SV *hm_redirect_response(pTHX_ HV *env, int https_port) {
4377 const char *host, *uri;
4378 STRLEN hlen, ulen;
4379 SV *loc;
4380 AV *rav, *hav, *bav;
4381 2 host = hm_env_str(aTHX_ env, "HTTP_HOST", 9, &hlen);
4382 2 uri = hm_env_str(aTHX_ env, "REQUEST_URI", 11, &ulen);
4383 /*
4384 # uncoverable branch true class:ignore_covered_err
4385 # uncoverable condition left count:1 class:ignore_covered_err */
4386 2 ▶ if (!uri) { uri = "/"; ulen = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (!uri) { uri = "/"; ulen = 1; } 0 2 2
4387 2 loc = newSVpvs("https://");
4388 /* Host and request-target are reflected into a response header, so stop at
4389 * the first control or space byte: a header value is only \r-delimited, so
4390 * a bare LF could otherwise smuggle a CRLF and inject/split the response.
4391 * A valid host[:port] / request-target contains no CTL or SP anyway. */
4392 /*
4393 # uncoverable branch false class:ignore_covered_err
4394 # uncoverable condition right count:1 class:ignore_covered_err */
4395 2 ▶ if (host) {
Branch: 1 branch
BranchTrueFalseTotal
if (host) { 2 0 2
4396 2 STRLEN i = 0; /* host, up to :port */
4397 2 ▶ if (hlen && host[0] == '[') { /* [v6]:port: whole */ /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (hlen && host[0] == '[') { /* [v6]:port: whole */ /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 0 2
if (hlen && host[0] == '[') { /* [v6]:port: whole */ /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 1 2
4398 /*
4399 # uncoverable condition right count:1 class:ignore_covered_err
4400 # uncoverable condition right count:2 class:ignore_covered_err */
4401 5 ▶ while (i < hlen && (unsigned char)host[i] > ' ' && host[i] != ']') i++;
Branch: 3 branches
BranchTrueFalseTotal
while (i < hlen && (unsigned char)host[i] > ' ' && host[i] != ']') i++; 5 0 5
while (i < hlen && (unsigned char)host[i] > ' ' && host[i] != ']') i++; 5 0 5
while (i < hlen && (unsigned char)host[i] > ' ' && host[i] != ']') i++; 4 1 5
4402 /*
4403 # uncoverable branch false class:ignore_covered_err
4404 # uncoverable condition right count:1 class:ignore_covered_err
4405 # uncoverable condition right count:2 class:ignore_covered_err */
4406 1 ▶ if (i < hlen && host[i] == ']') i++;
Branch: 2 branches
BranchTrueFalseTotal
if (i < hlen && host[i] == ']') i++; 1 0 1
if (i < hlen && host[i] == ']') i++; 1 0 1
4407 } else {
4408 /*
4409 # uncoverable condition right count:1 class:ignore_covered_err
4410 # uncoverable condition right count:2 class:ignore_covered_err */
4411 11 ▶ while (i < hlen && (unsigned char)host[i] > ' ' && host[i] != ':') i++;
Branch: 3 branches
BranchTrueFalseTotal
while (i < hlen && (unsigned char)host[i] > ' ' && host[i] != ':') i++; 11 0 11
while (i < hlen && (unsigned char)host[i] > ' ' && host[i] != ':') i++; 11 0 11
while (i < hlen && (unsigned char)host[i] > ' ' && host[i] != ':') i++; 10 1 11
4412 }
4413 2 sv_catpvn(loc, host, i);
4414 }
4415 /*
4416 # uncoverable branch false class:ignore_covered_err
4417 # uncoverable condition right count:1 class:ignore_covered_err */
4418 2 ▶ if (https_port != 443) sv_catpvf(loc, ":%d", https_port);
Branch: 1 branch
BranchTrueFalseTotal
if (https_port != 443) sv_catpvf(loc, ":%d", https_port); 2 0 2
4419 {
4420 2 STRLEN i = 0; /* request-target */
4421 14 ▶ while (i < ulen && (unsigned char)uri[i] > ' ') i++; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
while (i < ulen && (unsigned char)uri[i] > ' ') i++; /* # uncoverable condition right count:2 class:ignore_covered_err */ 12 2 14
while (i < ulen && (unsigned char)uri[i] > ' ') i++; /* # uncoverable condition right count:2 class:ignore_covered_err */ 12 0 12
4422 2 sv_catpvn(loc, uri, i);
4423 }
4424  
4425 2 hav = newAV();
4426 2 av_push(hav, newSVpvs("Location")); av_push(hav, loc);
4427 2 av_push(hav, newSVpvs("Content-Length")); av_push(hav, newSVpvs("0"));
4428 2 bav = newAV();
4429 2 rav = newAV();
4430 2 av_push(rav, newSViv(301));
4431 2 av_push(rav, newRV_noinc((SV *)hav));
4432 2 av_push(rav, newRV_noinc((SV *)bav));
4433 2 return newRV_noinc((SV *)rav);
4434 }
4435  
4436 /* Process complete requests buffered in c->rbuf, capped per wakeup so one
4437 * pipelined connection can't starve the others (fairness). */
4438 980 static void hm_process(pTHX_ hm_conn *c) {
4439 980 hm_loop *loop = c->loop;
4440 980 int served = 0;
4441  
4442 /* HTTP/2: once a connection is in h2 mode, all bytes go to nghttp2.
4443 * On a fresh connection with http2 enabled, sniff the h2 preface. */
4444 980 ▶ if (c->h2) { hm_h2_input(aTHX_ c); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->h2) { hm_h2_input(aTHX_ c); return; } 0 980 980
4445 980 ▶ if (c->lst && c->lst->http2 && c->nreqs == 0 && c->rlen > 0) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 4 branches
BranchTrueFalseTotal
if (c->lst && c->lst->http2 && c->nreqs == 0 && c->rlen > 0) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 0 980 980
if (c->lst && c->lst->http2 && c->nreqs == 0 && c->rlen > 0) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 980 0 980
if (c->lst && c->lst->http2 && c->nreqs == 0 && c->rlen > 0) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 0 0 0
if (c->lst && c->lst->http2 && c->nreqs == 0 && c->rlen > 0) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 0 0 0
4446 0 ▶ if (hm_h2_detect(aTHX_ c) != 0) return; /* started, or need more */
Branch: 1 branch
BranchTrueFalseTotal
if (hm_h2_detect(aTHX_ c) != 0) return; /* started, or need more */ 0 0 0
4447 }
4448  
4449 153 for (;;) {
4450 /*
4451 # uncoverable branch true
4452 # uncoverable condition left count:1 class:ignore_covered_err */
4453 1133 ▶ if (c->awaiting) break; /* parked; resumes via hm_deliver */
Branch: 1 branch
BranchTrueFalseTotal
if (c->awaiting) break; /* parked; resumes via hm_deliver */ 0 1133 1133
4454 1133 ▶ if (c->bsrc.kind) break; /* a body is still streaming out; the
Branch: 1 branch
BranchTrueFalseTotal
if (c->bsrc.kind) break; /* a body is still streaming out; the 11 1122 1133
4455 flush that finishes it re-queues us */
4456 /* Responses the client has not read yet. A pipeline that never
4457 * reads would otherwise have every request in the read buffer
4458 * answered into a write buffer that only grows: hold the rest until
4459 * the drain re-queues this connection (hm_flush, backlogged). */
4460 /*
4461 # uncoverable branch true class:ignore_covered_err
4462 # uncoverable condition left count:1 class:ignore_covered_err */
4463 1122 ▶ if (c->wlen - c->woff > HM_WB_HIWAT) { c->backlogged = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->wlen - c->woff > HM_WB_HIWAT) { c->backlogged = 1; break; } 1 1121 1122
4464  
4465 /* RFC 9112 2.2: at least one empty line before the request line is
4466 * ignored. Stripped here so the request line is the first line and
4467 * not an empty one with the real request line read as a header. */
4468 1121 ▶ if (c->rlen >= 2 && c->rbuf[0] == '\r' && c->rbuf[1] == '\n') { /* # uncoverable condition right count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (c->rlen >= 2 && c->rbuf[0] == '\r' && c->rbuf[1] == '\n') { /* # uncoverable condition right count:3 class:ignore_covered_err */ 975 146 1121
if (c->rlen >= 2 && c->rbuf[0] == '\r' && c->rbuf[1] == '\n') { /* # uncoverable condition right count:3 class:ignore_covered_err */ 1 974 975
if (c->rlen >= 2 && c->rbuf[0] == '\r' && c->rbuf[1] == '\n') { /* # uncoverable condition right count:3 class:ignore_covered_err */ 1 0 1
4469 1 size_t k = 0;
4470 /*
4471 # uncoverable condition right count:1 class:ignore_covered_err
4472 # uncoverable condition right count:3 class:ignore_covered_err */
4473 3 ▶ while (k + 1 < c->rlen && c->rbuf[k] == '\r' && c->rbuf[k + 1] == '\n') k += 2;
Branch: 3 branches
BranchTrueFalseTotal
while (k + 1 < c->rlen && c->rbuf[k] == '\r' && c->rbuf[k + 1] == '\n') k += 2; 3 0 3
while (k + 1 < c->rlen && c->rbuf[k] == '\r' && c->rbuf[k + 1] == '\n') k += 2; 2 1 3
while (k + 1 < c->rlen && c->rbuf[k] == '\r' && c->rbuf[k + 1] == '\n') k += 2; 2 0 2
4474 1 memmove(c->rbuf, c->rbuf + k, c->rlen - k);
4475 1 c->rlen -= k;
4476 /*
4477 # uncoverable branch true
4478 # uncoverable condition left count:1 class:ignore_covered_err */
4479 1 ▶ if (c->rlen == 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (c->rlen == 0) break; 0 1 1
4480 }
4481  
4482 1121 char *end = (char *)hm_memmem(c->rbuf, c->rlen, "\r\n\r\n", 4);
4483 1121 ▶ if (!end) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!end) break; 155 966 1121
4484  
4485 966 ▶ if (served >= loop->max_pipeline) { /* yield to other conns */
Branch: 1 branch
BranchTrueFalseTotal
if (served >= loop->max_pipeline) { /* yield to other conns */ 2 964 966
4486 2 hm_again_push(loop, c);
4487 2 break;
4488 }
4489  
4490 964 size_t headlen = (size_t)(end - c->rbuf);
4491 964 size_t bodystart = headlen + 4;
4492  
4493 /* one pass: framing decision + header-line index for the env build */
4494 hm_hline hidx_stack[HM_HIDX_STACK];
4495 964 const hm_hline *hidx = hidx_stack;
4496 964 int nhl = 0;
4497 964 long clen = 0;
4498 964 int frame = hm_parse_index(c->rbuf, headlen, &clen,
4499 hidx_stack, HM_HIDX_STACK, &nhl);
4500 964 ▶ if (frame == -2) { /* > HM_HIDX_STACK header lines: rescan
Branch: 1 branch
BranchTrueFalseTotal
if (frame == -2) { /* > HM_HIDX_STACK header lines: rescan 1 963 964
4501 into the loop's growable index */
4502 /*
4503 # uncoverable branch false class:ignore_covered_err
4504 # uncoverable condition right count:1 class:ignore_covered_err */
4505 1 ▶ if (nhl > loop->hidx_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (nhl > loop->hidx_cap) { 1 0 1
4506 2 loop->hidx = (hm_hline *)hm_xrealloc(loop->hidx,
4507 1 (size_t)nhl * sizeof(hm_hline));
4508 1 loop->hidx_cap = nhl;
4509 }
4510 1 frame = hm_parse_index(c->rbuf, headlen, &clen,
4511 loop->hidx, loop->hidx_cap, &nhl);
4512 1 hidx = loop->hidx;
4513 }
4514 964 ▶ if (frame) { /* smuggling/framing: reject + close */
Branch: 1 branch
BranchTrueFalseTotal
if (frame) { /* smuggling/framing: reject + close */ 9 955 964
4515 9 ▶ if (frame == 501) {
Branch: 1 branch
BranchTrueFalseTotal
if (frame == 501) { 1 8 9
4516 static const char e501[] =
4517 "HTTP/1.1 501 Not Implemented\r\nContent-Length: 0\r\n"
4518 "Connection: close\r\n\r\n";
4519 1 hm_wb_put(c, e501, sizeof(e501) - 1);
4520 } else {
4521 static const char e400[] =
4522 "HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\n"
4523 "Connection: close\r\n\r\n";
4524 8 hm_wb_put(c, e400, sizeof(e400) - 1);
4525 }
4526 9 c->keepalive = 0;
4527 9 break;
4528 }
4529  
4530 /* An expectation this server cannot meet is refused now, on the
4531 * headers alone - which is the answer the client asked for. */
4532 955 int expect = hm_expectation(c->rbuf, headlen);
4533 955 ▶ if (expect == HM_EXPECT_OTHER) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect == HM_EXPECT_OTHER) { 2 953 955
4534 static const char e417[] =
4535 "HTTP/1.1 417 Expectation Failed\r\nContent-Length: 0\r\n"
4536 "Connection: close\r\n\r\n";
4537 2 hm_wb_put(c, e417, sizeof(e417) - 1);
4538 2 c->keepalive = 0;
4539 2 c->last_status = 417;
4540 2 break;
4541 }
4542  
4543 size_t body_consumed; /* wire octets after the headers this request uses */
4544 953 ▶ if (clen < 0) { /* chunked: validate, then decode in place */
Branch: 1 branch
BranchTrueFalseTotal
if (clen < 0) { /* chunked: validate, then decode in place */ 22 931 953
4545 22 size_t enc = 0, dec = 0;
4546 22 int r = hm_chunked_scan_from(c->rbuf + bodystart, c->rlen - bodystart,
4547 &enc, &dec, loop->max_read,
4548 &c->chunk_pos, &c->chunk_dec);
4549 22 ▶ if (r == HM_CHUNKED_TOOBIG) { /* well framed, just too large */
Branch: 1 branch
BranchTrueFalseTotal
if (r == HM_CHUNKED_TOOBIG) { /* well framed, just too large */ 2 20 22
4550 static const char e413[] =
4551 "HTTP/1.1 413 Payload Too Large\r\nContent-Length: 0\r\n"
4552 "Connection: close\r\n\r\n";
4553 2 hm_wb_put(c, e413, sizeof(e413) - 1);
4554 2 c->keepalive = 0;
4555 2 c->last_status = 413;
4556 12 break;
4557 }
4558 20 ▶ if (r < 0) { /* malformed chunked */
Branch: 1 branch
BranchTrueFalseTotal
if (r < 0) { /* malformed chunked */ 4 16 20
4559 static const char e400[] =
4560 "HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\n"
4561 "Connection: close\r\n\r\n";
4562 4 hm_wb_put(c, e400, sizeof(e400) - 1);
4563 4 c->keepalive = 0;
4564 4 break;
4565 }
4566 16 ▶ if (r == 0) { /* need more bytes */
Branch: 1 branch
BranchTrueFalseTotal
if (r == 0) { /* need more bytes */ 6 10 16
4567 6 c->body_phase = 1;
4568 6 c->body_t0 = loop->now;
4569 6 c->body_rx = 0;
4570 /*
4571 # uncoverable branch true
4572 # uncoverable condition left count:1 class:ignore_covered_err */
4573 6 ▶ if (c->rlen - bodystart >= loop->max_read) { /* too big to fit */
Branch: 1 branch
BranchTrueFalseTotal
if (c->rlen - bodystart >= loop->max_read) { /* too big to fit */ 0 6 6
4574 static const char e413[] =
4575 "HTTP/1.1 413 Payload Too Large\r\nContent-Length: 0\r\n"
4576 "Connection: close\r\n\r\n";
4577 0 hm_wb_put(c, e413, sizeof(e413) - 1); /* # uncoverable statement */
4578 0 c->keepalive = 0; /* # uncoverable statement */
4579 }
4580 6 else hm_send_continue(c, expect);
4581 6 break;
4582 }
4583 10 hm_chunked_compact(c->rbuf + bodystart); /* -> dec octets @ bodystart */
4584 10 clen = (long)dec;
4585 10 body_consumed = enc;
4586 } else {
4587 931 ▶ if (bodystart + (size_t)clen > loop->max_read) { /* can never fit */
Branch: 1 branch
BranchTrueFalseTotal
if (bodystart + (size_t)clen > loop->max_read) { /* can never fit */ 8 923 931
4588 static const char e413[] =
4589 "HTTP/1.1 413 Payload Too Large\r\nContent-Length: 0\r\n"
4590 "Connection: close\r\n\r\n";
4591 8 hm_wb_put(c, e413, sizeof(e413) - 1);
4592 8 c->keepalive = 0;
4593 8 break;
4594 }
4595 /* A large body is drained to a temp file as it arrives rather
4596 * than accumulated here. rbuf keeps the headers and whatever
4597 * arrived after the body; the file gets the body.
4598 *
4599 * Identity framing only. A chunked body has no Content-Length to
4600 * divert on and its decoder wants the octets contiguous, so those
4601 * still accumulate - documented rather than hidden.
4602 *
4603 * Nothing about the loop changes: this still `break`s to wait for
4604 * more, and is re-driven when the socket is readable again. What
4605 * changes is that the waiting no longer costs the body. */
4606 923 ▶ if ((long)clen > HM_SPILL_BODY) {
Branch: 1 branch
BranchTrueFalseTotal
if ((long)clen > HM_SPILL_BODY) { 28 895 923
4607 28 size_t have = c->rlen - bodystart;
4608 /*
4609 # uncoverable branch true class:ignore_covered_err
4610 # uncoverable condition left count:1 class:ignore_covered_err
4611 # uncoverable condition left count:2 class:ignore_covered_err */
4612 28 ▶ if (c->spill_fd < 0 && (c->spill_fd = hm_body_tmpfile()) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (c->spill_fd < 0 && (c->spill_fd = hm_body_tmpfile()) < 0) 5 23 28
if (c->spill_fd < 0 && (c->spill_fd = hm_body_tmpfile()) < 0) 0 5 5
4613 0 goto hm_no_spill; /* cannot: behave as before */ /* # uncoverable statement */
4614 /*
4615 # uncoverable branch false class:ignore_covered_err
4616 # uncoverable condition right count:1 class:ignore_covered_err */
4617 28 ▶ if (have) {
Branch: 1 branch
BranchTrueFalseTotal
if (have) { 27 1 28
4618 27 size_t want = have;
4619 27 size_t room = (size_t)(clen - c->spill_got);
4620 /*
4621 # uncoverable branch true class:ignore_covered_err
4622 # uncoverable condition left count:1 class:ignore_covered_err */
4623 27 ▶ if (want > room) want = room; /* the rest is the next
Branch: 1 branch
BranchTrueFalseTotal
if (want > room) want = room; /* the rest is the next 1 26 27
4624 * request, not this body */
4625 /*
4626 # uncoverable branch false class:ignore_covered_err
4627 # uncoverable condition right count:1 class:ignore_covered_err */
4628 27 ▶ if (want) {
Branch: 1 branch
BranchTrueFalseTotal
if (want) { 27 0 27
4629 27 ssize_t w = write(c->spill_fd, c->rbuf + bodystart, want);
4630 /*
4631 # uncoverable branch true class:ignore_covered_err
4632 # uncoverable condition left count:1 class:ignore_covered_err */
4633 61 ▶ if (w <= 0) { hm_close(aTHX_ loop, c); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (w <= 0) { hm_close(aTHX_ loop, c); return; } 0 27 27
4634 27 c->spill_got += w;
4635 27 memmove(c->rbuf + bodystart, c->rbuf + bodystart + w,
4636 27 c->rlen - bodystart - w);
4637 27 c->rlen -= (size_t)w;
4638 }
4639 }
4640 28 ▶ if (c->spill_got < (long)clen) { /* await more */
Branch: 1 branch
BranchTrueFalseTotal
if (c->spill_got < (long)clen) { /* await more */ 24 4 28
4641 24 c->body_phase = 1;
4642 24 c->body_t0 = loop->now;
4643 24 c->body_rx = 0;
4644 24 hm_send_continue(c, expect);
4645 24 break;
4646 }
4647 /*
4648 # uncoverable branch true
4649 # uncoverable condition left count:1 class:ignore_covered_err */
4650 4 ▶ if (lseek(c->spill_fd, 0, SEEK_SET) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (lseek(c->spill_fd, 0, SEEK_SET) < 0) { 0 4 4
4651 0 hm_close(aTHX_ loop, c); return; /* # uncoverable statement */
4652 }
4653 4 body_consumed = 0; /* none of it is in rbuf */
4654 4 goto hm_have_body;
4655 }
4656 895 hm_no_spill:
4657 895 ▶ if (c->rlen - bodystart < (size_t)clen) { /* await full body */
Branch: 1 branch
BranchTrueFalseTotal
if (c->rlen - bodystart < (size_t)clen) { /* await full body */ 24 871 895
4658 24 c->body_phase = 1;
4659 24 c->body_t0 = loop->now;
4660 24 c->body_rx = 0;
4661 24 hm_send_continue(c, expect);
4662 24 break;
4663 }
4664 871 body_consumed = (size_t)clen;
4665 875 hm_have_body: ;
4666 }
4667  
4668 #ifdef HM_HAVE_NGHTTP2
4669 /* HTTP/1.1 Upgrade: h2c (cleartext, first request only). h2c-over-TLS
4670 * is forbidden by RFC 7540; the TLS path uses ALPN. */
4671 /*
4672 # uncoverable condition right count:1 class:ignore_covered_err
4673 # uncoverable condition right count:4 class:ignore_covered_err */
4674 if (c->lst && c->lst->http2 && !c->ssl && !c->h2 && c->nreqs == 0) {
4675 size_t ul = 0, sl = 0;
4676 const char *up = hm_find_header(c->rbuf, headlen, "upgrade", 7, &ul);
4677 const char *se = hm_find_header(c->rbuf, headlen, "http2-settings", 14, &sl);
4678 if (up && se && hm_ci_contains(up, ul, "h2c")) { /* # uncoverable condition right count:3 class:ignore_covered_err */
4679 static const char resp101[] =
4680 "HTTP/1.1 101 Switching Protocols\r\n"
4681 "Connection: Upgrade\r\nUpgrade: h2c\r\n\r\n";
4682 /*
4683 # uncoverable condition left count:1 class:ignore_covered_err
4684 # uncoverable condition right count:1 class:ignore_covered_err
4685 # uncoverable condition left count:2 class:ignore_covered_err */
4686 int is_head = (headlen >= 5 && hm_strncasecmp(c->rbuf, "HEAD ", 5) == 0);
4687 HV *env = hm_build_env(aTHX_ c->rbuf, headlen,
4688 c->rbuf + bodystart, clen, c, hidx, nhl);
4689 (void)hv_stores(env, "SERVER_PROTOCOL", newSVpvs("HTTP/2"));
4690 hm_wb_put(c, resp101, sizeof(resp101) - 1);
4691 {
4692 size_t consumed = bodystart + body_consumed;
4693 memmove(c->rbuf, c->rbuf + consumed, c->rlen - consumed);
4694 c->rlen -= consumed;
4695 c->nreqs++;
4696 loop->requests++;
4697 /* The request is consumed: the header and body clocks
4698 * stop here, as they do for an ordinary request below.
4699 * An h2 session never passes that point again, so one
4700 * whose upgrade arrived in two reads was closed by the
4701 * sweep as a slow request header_timeout later. */
4702 c->req_start = 0;
4703 c->body_phase = 0;
4704 c->chunk_pos = c->chunk_dec = 0;
4705 }
4706 {
4707 int rc;
4708 c->in_app = 1; /* the upgrade dispatches stream 1 */
4709 rc = hm_h2_start_upgrade(aTHX_ c, se, sl, env, is_head);
4710 c->in_app = 0;
4711 if (rc < 0 || c->close_deferred) { /* # uncoverable condition left count:2 class:ignore_covered_err */
4712 c->close_deferred = 0;
4713 hm_close(aTHX_ loop, c);
4714 return;
4715 }
4716 hm_read_resume(loop, c);
4717 }
4718 hm_h2_input(aTHX_ c); /* client's h2 preface + frames, + flush */
4719 return;
4720 }
4721 }
4722 #endif
4723  
4724 885 HV *env = hm_build_env(aTHX_ c->rbuf, headlen, c->rbuf + bodystart,
4725 clen, c, hidx, nhl);
4726 885 SV *env_rv = NULL;
4727 SV *resp;
4728 885 c->in_app = 1; /* see hm_close: a close meanwhile is deferred */
4729 885 ▶ if (c->lst && c->lst->redirect_https) { /* built-in :80 -> :443 bounce */ /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (c->lst && c->lst->redirect_https) { /* built-in :80 -> :443 bounce */ /* # uncoverable condition right count:1 class:ignore_covered_err */ 885 0 885
if (c->lst && c->lst->redirect_https) { /* built-in :80 -> :443 bounce */ /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 883 885
4730 2 resp = hm_redirect_response(aTHX_ env, c->lst->redirect_https);
4731 2 ▶ if (hm_logging(loop)) env_rv = newRV_noinc((SV *)env); /* # uncoverable branch true class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (hm_logging(loop)) env_rv = newRV_noinc((SV *)env); /* # uncoverable branch true class:ignore_covered_err */ 2 0 2
if (hm_logging(loop)) env_rv = newRV_noinc((SV *)env); /* # uncoverable branch true class:ignore_covered_err */ 0 2 2
4732 2 else SvREFCNT_dec((SV *)env);
4733 883 ▶ } else if (hm_logging(loop)) { /* env survives for access_log */
Branch: 2 branches
BranchTrueFalseTotal
} else if (hm_logging(loop)) { /* env survives for access_log */ 782 101 883
} else if (hm_logging(loop)) { /* env survives for access_log */ 69 713 782
4734 170 env_rv = newRV_noinc((SV *)env);
4735 170 resp = hm_call_app(aTHX_ loop, env_rv);
4736 } else {
4737 713 resp = hm_call_app0(aTHX_ loop, env);
4738 }
4739 885 c->in_app = 0;
4740 885 ▶ if (c->close_deferred) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->close_deferred) { 1 884 885
4741 /* The peer left, or the sweep gave up on it, while the handler
4742 * ran. Nothing it returned has anywhere to go. */
4743 1 c->close_deferred = 0;
4744 /*
4745 # uncoverable branch true class:ignore_covered_err
4746 # uncoverable condition left count:1 class:ignore_covered_err */
4747 1 ▶ if (env_rv) SvREFCNT_dec(env_rv);
Branch: 1 branch
BranchTrueFalseTotal
if (env_rv) SvREFCNT_dec(env_rv); 0 1 1
4748 /*
4749 # uncoverable branch false class:ignore_covered_err
4750 # uncoverable condition right count:1 class:ignore_covered_err */
4751 1 ▶ if (resp) SvREFCNT_dec(resp);
Branch: 1 branch
BranchTrueFalseTotal
if (resp) SvREFCNT_dec(resp); 1 0 1
4752 1 hm_close(aTHX_ loop, c);
4753 1 return;
4754 }
4755 884 hm_read_resume(loop, c);
4756  
4757 /* The app called detach: the socket is its own now. Drop whatever it
4758 * returned (the sentinel), forget the connection, and do not touch c
4759 * again. Any bytes past this request are dropped with it - RFC 6455
4760 * forbids the client sending before the 101, so they are a protocol
4761 * violation, not data we owe anyone. */
4762 884 ▶ if (c->detached) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->detached) { 4 880 884
4763 4 ▶ if (env_rv) {
Branch: 1 branch
BranchTrueFalseTotal
if (env_rv) { 1 3 4
4764 1 hm_access_log(aTHX_ loop, env_rv, 101, -1);
4765 1 SvREFCNT_dec(env_rv);
4766 }
4767 /*
4768 # uncoverable branch false class:ignore_covered_err
4769 # uncoverable condition right count:1 class:ignore_covered_err */
4770 4 ▶ if (resp) SvREFCNT_dec(resp);
Branch: 1 branch
BranchTrueFalseTotal
if (resp) SvREFCNT_dec(resp); 4 0 4
4771 4 c->nreqs++;
4772 4 loop->requests++;
4773 4 hm_conn_release(aTHX_ loop, c);
4774 4 return;
4775 }
4776  
4777 /* The app opened a 101 stream handle from inside the handler
4778 * (hm_stream_open): the connection is a tunnel now, its bytes
4779 * belong to the handle both ways, and the triplet the handler
4780 * returned is the same sentinel a detach leaves behind. Parked for
4781 * the life of the handle - stream_close unparks it, and the close
4782 * that follows (keepalive is off) ends the tunnel. Bytes that came
4783 * in with the request are the handle's too, so they are handed
4784 * over rather than parsed as a request that will never come. */
4785 880 ▶ if (c->tunnel == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tunnel == 2) { 6 874 880
4786 6 size_t consumed = bodystart + body_consumed;
4787 6 c->tunnel = 1;
4788 /*
4789 # uncoverable branch true class:ignore_covered_err
4790 # uncoverable condition left count:1 class:ignore_covered_err */
4791 6 ▶ if (env_rv) {
Branch: 1 branch
BranchTrueFalseTotal
if (env_rv) { 0 6 6
4792 0 hm_access_log(aTHX_ loop, env_rv, 101, -1); /* # uncoverable statement */
4793 0 SvREFCNT_dec(env_rv); /* # uncoverable statement */
4794 }
4795 /*
4796 # uncoverable branch false class:ignore_covered_err
4797 # uncoverable condition right count:1 class:ignore_covered_err */
4798 6 ▶ if (resp) SvREFCNT_dec(resp);
Branch: 1 branch
BranchTrueFalseTotal
if (resp) SvREFCNT_dec(resp); 6 0 6
4799 6 memmove(c->rbuf, c->rbuf + consumed, c->rlen - consumed);
4800 6 c->rlen -= consumed;
4801 6 c->req_start = 0;
4802 6 c->nreqs++;
4803 6 loop->requests++;
4804 6 ▶ if (c->rlen) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->rlen) { 5 1 6
4805 5 size_t n = c->rlen;
4806 5 c->rlen = 0;
4807 /*
4808 # uncoverable branch true class:ignore_covered_err
4809 # uncoverable condition left count:1 class:ignore_covered_err
4810 # uncoverable condition left count:2 class:ignore_covered_err
4811 # uncoverable condition right count:2 class:ignore_covered_err */
4812 5 ▶ if (!hm_stream_deliver(aTHX_ c->fd, c->id, -1, c->rbuf, n, 0)
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_stream_deliver(aTHX_ c->fd, c->id, -1, c->rbuf, n, 0) 0 5 5
4813 0 ▶ && loop->conns[c->fd] == c) /* # uncoverable statement class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
&& loop->conns[c->fd] == c) /* # uncoverable statement class:ignore_covered_err */ 0 0 0
4814 0 c->rlen = n; /* no read half yet: kept for it */ /* # uncoverable statement */
4815 }
4816 6 return;
4817 }
4818  
4819 874 size_t consumed = bodystart + body_consumed;
4820 874 memmove(c->rbuf, c->rbuf + consumed, c->rlen - consumed);
4821 874 c->rlen -= consumed;
4822 874 c->req_start = 0;
4823 874 c->body_phase = 0;
4824 874 c->chunk_pos = c->chunk_dec = 0;
4825 874 c->cont_sent = 0; /* the next request on this connection asks
4826 * for itself */
4827 874 served++;
4828 874 c->nreqs++;
4829 874 hm_count_request(loop);
4830  
4831 /* async: handler returned a Future -> park and wait; hold the
4832 * future so a client disconnect can cancel it */
4833 874 ▶ if (resp && hm_is_awaitable(aTHX_ resp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (resp && hm_is_awaitable(aTHX_ resp)) { 871 3 874
if (resp && hm_is_awaitable(aTHX_ resp)) { 8 863 871
4834 8 c->awaiting = 1;
4835 8 c->resp_f = SvREFCNT_inc(resp);
4836 8 c->env_sv = env_rv; /* logged at delivery */
4837 8 hm_park(aTHX_ c, resp);
4838 8 SvREFCNT_dec(resp);
4839 /*
4840 # uncoverable branch true
4841 # uncoverable condition left count:1 class:ignore_covered_err */
4842 8 ▶ if (loop->conns[c->fd] != c) return; /* resolved + closed inline */
Branch: 1 branch
BranchTrueFalseTotal
if (loop->conns[c->fd] != c) return; /* resolved + closed inline */ 0 8 8
4843 8 break;
4844 }
4845  
4846 /* psgi.streaming: coderef response -> park, hand it a responder */
4847 866 ▶ if (resp && SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVCV) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (resp && SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVCV) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 863 3 866
if (resp && SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVCV) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 863 0 863
if (resp && SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVCV) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 40 823 863
4848 40 c->awaiting = 1;
4849 40 c->env_sv = env_rv; /* logged at respond time */
4850 40 hm_delayed(aTHX_ c, resp);
4851 40 SvREFCNT_dec(resp);
4852 40 ▶ if (loop->conns[c->fd] != c) return; /* responder ran + closed */
Branch: 1 branch
BranchTrueFalseTotal
if (loop->conns[c->fd] != c) return; /* responder ran + closed */ 23 17 40
4853 17 break;
4854 }
4855  
4856 {
4857 826 int rc = hm_queue_response(aTHX_ c, resp);
4858 826 ▶ if (resp) SvREFCNT_dec(resp);
Branch: 1 branch
BranchTrueFalseTotal
if (resp) SvREFCNT_dec(resp); 823 3 826
4859 /*
4860 # uncoverable branch true
4861 # uncoverable condition left count:1 class:ignore_covered_err
4862 # uncoverable condition left count:2 class:ignore_covered_err
4863 # uncoverable condition right count:2 class:ignore_covered_err */
4864 826 ▶ if (rc < 0) { if (env_rv) SvREFCNT_dec(env_rv); return; }
Branch: 2 branches
BranchTrueFalseTotal
if (rc < 0) { if (env_rv) SvREFCNT_dec(env_rv); return; } 0 826 826
if (rc < 0) { if (env_rv) SvREFCNT_dec(env_rv); return; } 0 0 0
4865 }
4866 826 ▶ if (env_rv) {
Branch: 1 branch
BranchTrueFalseTotal
if (env_rv) { 156 670 826
4867 156 hm_access_log(aTHX_ loop, env_rv, c->last_status, (ssize_t)c->last_blen);
4868 156 SvREFCNT_dec(env_rv);
4869 }
4870 826 ▶ if (!c->keepalive) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!c->keepalive) break; 673 153 826
4871 }
4872 946 ▶ if (loop->conns[c->fd] == c && c->rlen > 0 && !c->awaiting && !c->req_start) /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 4 branches
BranchTrueFalseTotal
if (loop->conns[c->fd] == c && c->rlen > 0 && !c->awaiting && !c->req_start) /* # uncoverable condition right count:1 class:ignore_covered_err */ 946 0 946
if (loop->conns[c->fd] == c && c->rlen > 0 && !c->awaiting && !c->req_start) /* # uncoverable condition right count:1 class:ignore_covered_err */ 96 850 946
if (loop->conns[c->fd] == c && c->rlen > 0 && !c->awaiting && !c->req_start) /* # uncoverable condition right count:1 class:ignore_covered_err */ 94 2 96
if (loop->conns[c->fd] == c && c->rlen > 0 && !c->awaiting && !c->req_start) /* # uncoverable condition right count:1 class:ignore_covered_err */ 54 40 94
4873 54 c->req_start = loop->now;
4874 }
4875  
4876 /* A tunnel's bytes belong to its handle, not the request parser: hand over
4877 * whatever the read left in rbuf, or keep it when no read half is registered
4878 * yet. ONE function for both ways a read completes - readiness (hm_readable)
4879 * and completion (HM_EV_RECV) - because the completion path once had no
4880 * tunnel branch at all and fed the bytes to hm_process, where they sat as
4881 * the start of a request that never came. Every Linux box with liburing
4882 * takes that path by default, and macOS never does, so the six smokers that
4883 * failed t/41 were the first time it ran. Returns 0 when the callback closed
4884 * the connection. */
4885 7 static int hm_tunnel_bytes(pTHX_ hm_conn *c) {
4886 7 hm_loop *loop = c->loop;
4887 7 size_t have = c->rlen;
4888 7 c->rlen = 0;
4889 /*
4890 # uncoverable branch true class:ignore_covered_err
4891 # uncoverable condition left count:1 class:ignore_covered_err */
4892 7 ▶ if (!hm_stream_deliver(aTHX_ c->fd, c->id, -1, c->rbuf, have, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_stream_deliver(aTHX_ c->fd, c->id, -1, c->rbuf, have, 0)) { 0 7 7
4893 /*
4894 # uncoverable statement
4895 # uncoverable branch true
4896 # uncoverable branch false
4897 # uncoverable condition left count:1
4898 # uncoverable condition right count:1 */
4899 0 ▶ if (loop->conns[c->fd] != c) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (loop->conns[c->fd] != c) return 0; 0 0 0
4900 0 c->rlen = have; /* no read half yet: kept for it */ /* # uncoverable statement */
4901 }
4902 7 return loop->conns[c->fd] == c;
4903 }
4904  
4905 /* Stop and restart reading a connection whose buffer is full while a frame
4906 * on the stack still holds pointers into it (hm_readable, below). The
4907 * watcher is persistent and level-triggered, so leaving the bytes in the
4908 * socket without this would wake the loop every turn until the frame
4909 * returned. Readiness mode only: a completion-mode read is armed from the
4910 * top of a loop turn (hm_run_again), never from under a frame. */
4911 37 static void hm_read_pause(hm_loop *loop, hm_conn *c) {
4912 /*
4913 # uncoverable branch true
4914 # uncoverable condition left count:1 class:ignore_covered_err */
4915 37 ▶ if (c->read_paused) return;
Branch: 1 branch
BranchTrueFalseTotal
if (c->read_paused) return; 0 37 37
4916 37 c->read_paused = 1;
4917 37 loop->be->remove_io(loop->be, c->fd, HM_EV_READ);
4918 }
4919 921 static void hm_read_resume(hm_loop *loop, hm_conn *c) {
4920 921 ▶ if (!c->read_paused) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!c->read_paused) return; 884 37 921
4921 37 c->read_paused = 0;
4922 37 loop->be->add_io(loop->be, c->fd, HM_EV_READ, 0);
4923 }
4924  
4925 1094 static void hm_readable(pTHX_ hm_conn *c) {
4926 1094 hm_loop *loop = c->loop;
4927 96 for (;;) {
4928 1190 ▶ if (c->rlen == c->rcap) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->rlen == c->rcap) { 40 1150 1190
4929 /* A synchronous handler on the stack (in_app) may be nghttp2
4930 * reading frames out of this very buffer, or hm_process holding
4931 * offsets into it: growing it here would move it from under
4932 * them. Leave the rest in the socket until that frame returns. */
4933 /*
4934 # uncoverable branch true class:ignore_covered_err
4935 # uncoverable condition left count:1 class:ignore_covered_err
4936 # uncoverable condition left count:2 class:ignore_covered_err
4937 # uncoverable condition right count:2 class:ignore_covered_err */
4938 40 ▶ if (c->in_app && !hm_h2_reentrant(c)) {
Branch: 2 branches
BranchTrueFalseTotal
if (c->in_app && !hm_h2_reentrant(c)) { 0 40 40
if (c->in_app && !hm_h2_reentrant(c)) { 0 0 0
4939 0 hm_read_pause(loop, c); /* # uncoverable statement */
4940 0 break; /* # uncoverable statement */
4941 }
4942 /* HTTP/2: the buffer holds frames, not a request, and nghttp2
4943 * parses them incrementally - a frame larger than the buffer is
4944 * consumed across reads. Hand over what is here and let the
4945 * level-triggered watcher bring the rest. The request ceiling
4946 * is per stream there (hm_h2_cb_data_chunk); applied to this
4947 * buffer it closed every upload over 16KB on a server whose
4948 * max_body was below that. */
4949 40 ▶ if (c->h2) break;
Branch: 1 branch
BranchTrueFalseTotal
if (c->h2) break; 0 40 40
4950 /* The same, one read earlier: the preface is in the buffer but
4951 * hm_process has not run to detect it. Let it. */
4952 40 ▶ if (hm_h2_preface_pending(c)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (hm_h2_preface_pending(c)) break; 0 40 40
4953 /* grow for large bodies; unbounded headers are rejected */
4954 /*
4955 # uncoverable condition left count:2 class:ignore_covered_err
4956 # uncoverable condition left count:3 class:ignore_covered_err */
4957 40 ▶ if (c->rcap >= loop->max_read
Branch: 1 branch
BranchTrueFalseTotal
if (c->rcap >= loop->max_read 40 0 40
4958 40 ▶ || (c->rlen >= 65536 && !hm_memmem(c->rbuf, c->rlen, "\r\n\r\n", 4))) {
Branch: 2 branches
BranchTrueFalseTotal
|| (c->rlen >= 65536 && !hm_memmem(c->rbuf, c->rlen, "\r\n\r\n", 4))) { 26 14 40
|| (c->rlen >= 65536 && !hm_memmem(c->rbuf, c->rlen, "\r\n\r\n", 4))) { 0 26 26
4959 /* The buffer is full and may not grow. Until max_body was
4960 * configurable this only happened to a genuinely enormous
4961 * request and a bare close was defensible; now that an
4962 * operator can set the ceiling BELOW the initial buffer
4963 * (HM_RBUF, 16KB), it is the ordinary way an oversize
4964 * request ends - and dropping the connection with no
4965 * response at all would leave every such client staring at
4966 * a timeout rather than a 413. Answer, then close.
4967 *
4968 * Only for a request that at least has a complete header
4969 * block: without one this is a client streaming garbage,
4970 * which has earned nothing but the close it used to get. */
4971 /*
4972 # uncoverable branch false class:ignore_covered_err
4973 # uncoverable condition right count:1 class:ignore_covered_err
4974 # uncoverable condition right count:2 class:ignore_covered_err */
4975 0 ▶ if (c->rcap >= loop->max_read
Branch: 1 branch
BranchTrueFalseTotal
if (c->rcap >= loop->max_read 0 0 0
4976 0 ▶ && hm_memmem(c->rbuf, c->rlen, "\r\n\r\n", 4)) {
Branch: 1 branch
BranchTrueFalseTotal
&& hm_memmem(c->rbuf, c->rlen, "\r\n\r\n", 4)) { 0 0 0
4977 static const char e413[] =
4978 "HTTP/1.1 413 Payload Too Large\r\nContent-Length: 0\r\n"
4979 "Connection: close\r\n\r\n";
4980 0 hm_wb_put(c, e413, sizeof(e413) - 1);
4981 0 c->keepalive = 0;
4982 0 c->last_status = 413;
4983 0 c->last_blen = 0;
4984 0 (void)hm_flush(aTHX_ c);
4985 /*
4986 # uncoverable branch false
4987 # uncoverable condition right count:1 class:ignore_covered_err */
4988 0 ▶ if (loop->conns[c->fd] != c) return; /* flush closed it */
Branch: 1 branch
BranchTrueFalseTotal
if (loop->conns[c->fd] != c) return; /* flush closed it */ 0 0 0
4989 }
4990 0 hm_close(aTHX_ loop, c); /* # uncoverable statement */
4991 0 return; /* # uncoverable statement */
4992 }
4993 {
4994 40 size_t ncap = c->rcap * 2; /* # uncoverable statement class:ignore_covered_err */
4995 /*
4996 # uncoverable statement class:ignore_covered_err
4997 # uncoverable branch true
4998 # uncoverable condition left count:1 */
4999 40 ▶ if (ncap > loop->max_read) ncap = loop->max_read;
Branch: 1 branch
BranchTrueFalseTotal
if (ncap > loop->max_read) ncap = loop->max_read; 0 40 40
5000 40 c->rbuf = (char *)hm_xrealloc(c->rbuf, ncap);
5001 40 c->rcap = ncap;
5002 }
5003 }
5004 1190 size_t want = c->rcap - c->rlen;
5005 1190 ssize_t n = hm_cread(c, c->rbuf + c->rlen, want);
5006 1190 ▶ if (n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { 1017 173 1190
5007 1017 c->rlen += (size_t)n;
5008 1017 c->last_rx = loop->now;
5009 1017 c->body_rx += (UV)n;
5010 /* A tunnel (a 101 stream handle): the bytes are the handle's,
5011 * not a request. Handed up read by read, so the buffer never
5012 * grows toward the ceiling that ends an oversize request. The
5013 * callback may close the stream and with it the connection.
5014 * Only once SETTLED (1): at 2 the request line is still in
5015 * rbuf and the hm_process frame that opened the tunnel owns the
5016 * buffer, so the bytes wait for it (it hands them over itself). */
5017 /*
5018 # uncoverable branch true class:ignore_covered_err
5019 # uncoverable condition left count:1 class:ignore_covered_err
5020 # uncoverable condition left count:2 class:ignore_covered_err */
5021 1017 ▶ if (c->tunnel == 1 && !hm_tunnel_bytes(aTHX_ c)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (c->tunnel == 1 && !hm_tunnel_bytes(aTHX_ c)) return; 7 1010 1017
if (c->tunnel == 1 && !hm_tunnel_bytes(aTHX_ c)) return; 0 7 7
5022 /* A SHORT read means the socket buffer is drained: the kernel
5023 * handed over everything it had, so the confirming read that
5024 * would follow can only return EAGAIN. Stopping here removes ONE
5025 * SYSCALL FROM EVERY REQUEST - measured, an ordinary keep-alive
5026 * request cost three (two reads and a writev) and the second read
5027 * was always the empty one.
5028 *
5029 * Safe because the backends are LEVEL-triggered - no EV_CLEAR, no
5030 * EPOLLET. Draining to EAGAIN is an edge-triggered obligation,
5031 * and under level triggering anything that arrived between the
5032 * read and this line simply reports the fd readable again on the
5033 * next turn of the loop. Nothing is lost, and nothing stalls.
5034 *
5035 * NOT for TLS: a short SSL_read says nothing about the socket,
5036 * because the engine may hold more plaintext than it just
5037 * returned, and stopping on it would leave that sitting in the
5038 * engine until the peer happened to send again. */
5039 1017 ▶ if (!c->ssl && (size_t)n < want) {
Branch: 2 branches
BranchTrueFalseTotal
if (!c->ssl && (size_t)n < want) { 961 56 1017
if (!c->ssl && (size_t)n < want) { 921 40 961
5040 /* The disarm the EAGAIN branch below would have done. A write
5041 * watcher left armed over an empty write buffer re-fires
5042 * every turn and spins the worker at 100% CPU. */
5043 /*
5044 # uncoverable branch true class:ignore_covered_err
5045 # uncoverable condition left count:1 class:ignore_covered_err
5046 # uncoverable condition left count:2 class:ignore_covered_err */
5047 921 ▶ if (c->writing && c->woff == c->wlen) {
Branch: 2 branches
BranchTrueFalseTotal
if (c->writing && c->woff == c->wlen) { 0 921 921
if (c->writing && c->woff == c->wlen) { 0 0 0
5048 0 loop->be->remove_io(loop->be, c->fd, HM_EV_WRITE); /* # uncoverable statement */
5049 0 c->writing = 0; /* # uncoverable statement */
5050 }
5051 921 break;
5052 }
5053 173 ▶ } else if (n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (n == 0) { 122 51 173
5054 /* The peer half-closed a tunnel: the same thing a zero read
5055 * means to a socket owner, told to the handle as fin before
5056 * the connection goes and the abort callback says the rest. */
5057 /*
5058 # uncoverable branch true class:ignore_covered_err
5059 # uncoverable condition left count:1 class:ignore_covered_err */
5060 122 ▶ if (c->tunnel) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tunnel) { 0 122 122
5061 0 (void)hm_stream_deliver(aTHX_ c->fd, c->id, -1, c->rbuf, 0, 1); /* # uncoverable statement */
5062 /*
5063 # uncoverable statement
5064 # uncoverable branch true
5065 # uncoverable branch false
5066 # uncoverable condition left count:1
5067 # uncoverable condition right count:1 */
5068 0 ▶ if (loop->conns[c->fd] != c) return;
Branch: 1 branch
BranchTrueFalseTotal
if (loop->conns[c->fd] != c) return; 0 0 0
5069 }
5070 122 hm_close(aTHX_ loop, c);
5071 122 return;
5072 51 ▶ } else if (errno == EAGAIN || errno == EWOULDBLOCK) { /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
} else if (errno == EAGAIN || errno == EWOULDBLOCK) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 51 51
} else if (errno == EAGAIN || errno == EWOULDBLOCK) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 0 0
5073 /* TLS read blocked wanting writable: arm a write watcher so a
5074 * write-readiness event re-drives us. Once it unblocks, drop that
5075 * watcher again unless there is queued output still waiting on
5076 * it. The backends are level-triggered, so a watcher left armed
5077 * over an empty write buffer is not merely untidy - it re-fires
5078 * every turn of the loop and spins the worker at 100% CPU. */
5079 /*
5080 # uncoverable branch true
5081 # uncoverable condition left count:1 class:ignore_covered_err */
5082 51 ▶ if (c->tls_r_wants_w) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_r_wants_w) { 0 51 51
5083 /*
5084 # uncoverable statement
5085 # uncoverable branch true
5086 # uncoverable branch false
5087 # uncoverable condition left count:1
5088 # uncoverable condition right count:1 */
5089 0 ▶ if (!c->writing) {
Branch: 1 branch
BranchTrueFalseTotal
if (!c->writing) { 0 0 0
5090 0 loop->be->add_io(loop->be, c->fd, HM_EV_WRITE, 0); /* # uncoverable statement */
5091 0 c->writing = 1; /* # uncoverable statement */
5092 }
5093 /*
5094 # uncoverable branch true class:ignore_covered_err
5095 # uncoverable condition left count:1 class:ignore_covered_err
5096 # uncoverable condition left count:2 class:ignore_covered_err
5097 # uncoverable condition right count:2 class:ignore_covered_err */
5098 51 ▶ } else if (c->writing && c->woff == c->wlen) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (c->writing && c->woff == c->wlen) { 0 51 51
} else if (c->writing && c->woff == c->wlen) { 0 0 0
5099 0 loop->be->remove_io(loop->be, c->fd, HM_EV_WRITE); /* # uncoverable statement */
5100 0 c->writing = 0; /* # uncoverable statement */
5101 }
5102 51 break;
5103 } else {
5104 0 hm_close(aTHX_ loop, c);
5105 0 return;
5106 }
5107 }
5108 972 hm_lru_touch(loop, c);
5109 /* A tunnel carries no requests. A connection whose synchronous handler
5110 * is on the stack (its ->get pumped this loop) keeps the new bytes in
5111 * rbuf for the hm_process frame already running on it: a second frame
5112 * would consume the pipeline from under the first one's offsets. */
5113 /* HTTP/2 is the exception once nghttp2 has returned: a handler waiting
5114 * for its body to drain is waiting for the window updates in these very
5115 * bytes, and hm_h2_input takes them without starting another request. */
5116 972 ▶ if (!c->tunnel && (!c->in_app || hm_h2_reentrant(c))) hm_process(aTHX_ c);
Branch: 3 branches
BranchTrueFalseTotal
if (!c->tunnel && (!c->in_app || hm_h2_reentrant(c))) hm_process(aTHX_ c); 963 9 972
if (!c->tunnel && (!c->in_app || hm_h2_reentrant(c))) hm_process(aTHX_ c); 2 961 963
if (!c->tunnel && (!c->in_app || hm_h2_reentrant(c))) hm_process(aTHX_ c); 0 2 2
5117 972 ▶ if (loop->conns[c->fd] == c) hm_flush(aTHX_ c);
Branch: 1 branch
BranchTrueFalseTotal
if (loop->conns[c->fd] == c) hm_flush(aTHX_ c); 944 28 972
5118 }
5119  
5120 static hm_tw *hm_add_timer_watch_c(hm_loop *loop, double secs,
5121 hm_abi_timer_cb cb, void *ud);
5122 static void hm_tls_handshake(pTHX_ hm_conn *c);
5123  
5124 /* The first bytes on a proxy_protocol listener. PEEK, so the header can be
5125 * parsed without taking anything behind it off the socket: a ClientHello or
5126 * an h2 preface in the same segment stays in the kernel for hm_tls_wrap and
5127 * hm_h2_detect, which then run exactly as on a listener without a proxy.
5128 * Only a complete header is consumed, by a read of exactly its length. A
5129 * partial one is re-peeked when the level-triggered watcher fires again;
5130 * one that never completes is closed by hm_sweep after header_timeout. */
5131 /* A partial PROXY header: the bytes stay in the socket (peeked, not read)
5132 * and the watcher is level-triggered, so returning to the loop would wake
5133 * it again at once - a peer that sent "PROXY TCP4 1" and stopped had the
5134 * worker re-peeking at 100% CPU until header_timeout. Reading is paused
5135 * and resumed by this timer; the sweep still closes a header that never
5136 * completes. */
5137 typedef struct { hm_loop *loop; int fd; UV id; } hm_pp_retry;
5138 37 static void hm_proxy_retry(pTHX_ void *ud) {
5139 37 hm_pp_retry *r = (hm_pp_retry *)ud;
5140 /*
5141 # uncoverable condition right count:1 class:ignore_covered_err
5142 # uncoverable condition right count:2 class:ignore_covered_err */
5143 37 ▶ hm_conn *c = (r->fd >= 0 && r->fd < HM_MAXFD) ? r->loop->conns[r->fd] : NULL;
Branch: 2 branches
BranchTrueFalseTotal
hm_conn *c = (r->fd >= 0 && r->fd < HM_MAXFD) ? r->loop->conns[r->fd] : NULL; 37 0 37
hm_conn *c = (r->fd >= 0 && r->fd < HM_MAXFD) ? r->loop->conns[r->fd] : NULL; 37 0 37
5144 PERL_UNUSED_CONTEXT;
5145 /*
5146 # uncoverable branch false class:ignore_covered_err
5147 # uncoverable condition right count:1 class:ignore_covered_err
5148 # uncoverable condition right count:2 class:ignore_covered_err
5149 # uncoverable condition right count:3 class:ignore_covered_err */
5150 37 ▶ if (c && c->id == r->id && c->pp_wait) hm_read_resume(r->loop, c);
Branch: 3 branches
BranchTrueFalseTotal
if (c && c->id == r->id && c->pp_wait) hm_read_resume(r->loop, c); 37 0 37
if (c && c->id == r->id && c->pp_wait) hm_read_resume(r->loop, c); 37 0 37
if (c && c->id == r->id && c->pp_wait) hm_read_resume(r->loop, c); 37 0 37
5151 37 free(r);
5152 37 }
5153  
5154 50 static void hm_proxy_readable(pTHX_ hm_conn *c) {
5155 50 hm_loop *loop = c->loop;
5156 unsigned char buf[HM_PP_MAX];
5157 hm_pp_result r;
5158 ssize_t n;
5159 long len;
5160 50 n = hm_os_recv_peek(c->fd, buf, sizeof(buf));
5161 /*
5162 # uncoverable branch true class:ignore_covered_err
5163 # uncoverable condition left count:1 class:ignore_covered_err */
5164 90 ▶ if (n == 0) { hm_close(aTHX_ loop, c); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { hm_close(aTHX_ loop, c); return; } 0 50 50
5165 /*
5166 # uncoverable branch true
5167 # uncoverable condition left count:1 class:ignore_covered_err */
5168 50 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 0 50 50
5169 /*
5170 # uncoverable statement
5171 # uncoverable branch true
5172 # uncoverable branch false
5173 # uncoverable condition left count:1
5174 # uncoverable condition right count:1
5175 # uncoverable condition left count:2
5176 # uncoverable condition right count:2
5177 # uncoverable condition left count:3
5178 # uncoverable condition right count:3 */
5179 0 ▶ if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) return;
Branch: 3 branches
BranchTrueFalseTotal
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) return; 0 0 0
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) return; 0 0 0
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) return; 0 0 0
5180 0 hm_close(aTHX_ loop, c); return; /* # uncoverable statement */
5181 }
5182 50 len = hm_pp_parse(buf, (size_t)n, &r);
5183 50 ▶ if (len == 0) { /* more to come */
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) { /* more to come */ 37 13 50
5184 hm_pp_retry *rt;
5185 /*
5186 # uncoverable branch true
5187 # uncoverable condition left count:1 class:ignore_covered_err */
5188 37 ▶ if ((size_t)n >= sizeof(buf)) { hm_close(aTHX_ loop, c); return; }
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)n >= sizeof(buf)) { hm_close(aTHX_ loop, c); return; } 0 37 37
5189 37 rt = (hm_pp_retry *)hm_xmalloc(sizeof(*rt));
5190 37 rt->loop = loop; rt->fd = c->fd; rt->id = c->id;
5191 37 hm_read_pause(loop, c);
5192 /*
5193 # uncoverable branch true class:ignore_covered_err
5194 # uncoverable condition left count:1 class:ignore_covered_err */
5195 37 ▶ if (!hm_add_timer_watch_c(loop, 0.05, hm_proxy_retry, rt)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_add_timer_watch_c(loop, 0.05, hm_proxy_retry, rt)) { 0 37 37
5196 0 free(rt); /* no timer: level-triggered it is */ /* # uncoverable statement */
5197 0 hm_read_resume(loop, c); /* # uncoverable statement */
5198 }
5199 37 return;
5200 }
5201 13 ▶ if (len < 0) { hm_close(aTHX_ loop, c); return; } /* not a PROXY header */
Branch: 1 branch
BranchTrueFalseTotal
if (len < 0) { hm_close(aTHX_ loop, c); return; } /* not a PROXY header */ 2 11 13
5202 /* take exactly the header; what follows is the client's first bytes */
5203 {
5204 11 ssize_t got = hm_os_recv(c->fd, buf, (size_t)len);
5205 /*
5206 # uncoverable branch true
5207 # uncoverable condition left count:1 class:ignore_covered_err */
5208 11 ▶ if (got != len) { hm_close(aTHX_ loop, c); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (got != len) { hm_close(aTHX_ loop, c); return; } 0 11 11
5209 }
5210 11 c->pp_wait = 0;
5211 11 c->req_start = 0;
5212 /* The proxy's own address is kept for $env; the client's replaces the
5213 * peer, so REMOTE_ADDR, the access log, the rate limiter and the
5214 * denylist all see the client from here on. */
5215 11 memcpy(c->proxy_addr, c->peer, sizeof(c->proxy_addr));
5216 11 c->proxy_port = c->peer_port;
5217 11 ▶ if (r.family) {
Branch: 1 branch
BranchTrueFalseTotal
if (r.family) { 9 2 11
5218 static const unsigned char v4map[12] =
5219 { 0,0,0,0, 0,0,0,0, 0,0,0xff,0xff };
5220 9 c->peer[0] = '\0';
5221 /* ::ffff:a.b.c.d is the IPv4 client it names (see hm_fmt_peer) */
5222 9 ▶ if (r.family == AF_INET6 && memcmp(r.src, v4map, 12) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (r.family == AF_INET6 && memcmp(r.src, v4map, 12) == 0) 3 6 9
if (r.family == AF_INET6 && memcmp(r.src, v4map, 12) == 0) 1 2 3
5223 1 (void)inet_ntop(AF_INET, (const unsigned char *)r.src + 12,
5224 1 c->peer, (socklen_t)sizeof(c->peer));
5225 else
5226 8 (void)inet_ntop(r.family, r.src, c->peer, (socklen_t)sizeof(c->peer));
5227 9 c->peer_port = r.src_port;
5228 /* the denylist, again, now for the real client; the accept-time
5229 * check saw the proxy */
5230 9 ▶ if (hm_sa_deny_check(c->peer)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hm_sa_deny_check(c->peer)) { 1 8 9
5231 1 loop->denied++;
5232 1 hm_close(aTHX_ loop, c);
5233 1 return;
5234 }
5235 }
5236 /* distinct clients counts clients, not proxies, so the accept path
5237 * skipped it on this listener */
5238 /*
5239 # uncoverable branch true class:ignore_covered_err
5240 # uncoverable condition left count:1 class:ignore_covered_err
5241 # uncoverable condition left count:2 class:ignore_covered_err
5242 # uncoverable condition right count:2 class:ignore_covered_err */
5243 10 ▶ if (hm_sa_hll && c->peer[0]) hm_sa_hll_add(c->peer);
Branch: 2 branches
BranchTrueFalseTotal
if (hm_sa_hll && c->peer[0]) hm_sa_hll_add(c->peer); 0 10 10
if (hm_sa_hll && c->peer[0]) hm_sa_hll_add(c->peer); 0 0 0
5244 10 ▶ if (c->lst && c->lst->tls_ctx) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (c->lst && c->lst->tls_ctx) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 10 0 10
if (c->lst && c->lst->tls_ctx) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 9 10
5245 1 hm_tls_wrap(c, c->lst->tls_ctx);
5246 1 hm_tls_handshake(aTHX_ c);
5247 } else {
5248 9 hm_readable(aTHX_ c);
5249 }
5250 }
5251  
5252 /* accept(2) failed for want of a descriptor. The listener is level-readable
5253 * and stays so, so simply returning would wake the loop again at once and
5254 * spin the worker at 100% CPU until the table had room. Reclaim an idle
5255 * keep-alive connection when there is one; otherwise stop listening for a
5256 * tenth of a second and come back. */
5257 /* Listeners whose starvation timer could not be armed (hm_accept_starved):
5258 * re-added from the top of a loop turn. */
5259 /*
5260 # uncoverable statement class:ignore_covered_err
5261 # uncoverable subroutine class:ignore_covered_err */
5262 30222 static void hm_accept_unstarve(hm_loop *loop) {
5263 int i;
5264 30222 loop->starved_retry = 0; /* # uncoverable statement class:ignore_covered_err */
5265 /*
5266 # uncoverable statement class:ignore_covered_err
5267 # uncoverable branch true class:ignore_covered_err
5268 # uncoverable branch false class:ignore_covered_err */
5269 60444 ▶ for (i = 0; i < loop->nlisteners; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < loop->nlisteners; i++) { 30222 30222 60444
5270 30222 hm_listener *l = &loop->listeners[i]; /* # uncoverable statement class:ignore_covered_err */
5271 /*
5272 # uncoverable statement class:ignore_covered_err
5273 # uncoverable branch true class:ignore_covered_err
5274 # uncoverable branch false class:ignore_covered_err
5275 # uncoverable condition left count:1 */
5276 30222 ▶ if (l->starved != 2) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (l->starved != 2) continue; 0 30222 30222
5277 30222 l->starved = 0; /* # uncoverable statement class:ignore_covered_err */
5278 /*
5279 # uncoverable statement class:ignore_covered_err
5280 # uncoverable branch false
5281 # uncoverable condition right count:1
5282 # uncoverable condition right count:2 */
5283 30222 ▶ if (!loop->stopping && l->fd >= 0)
Branch: 2 branches
BranchTrueFalseTotal
if (!loop->stopping && l->fd >= 0) 30222 0 30222
if (!loop->stopping && l->fd >= 0) 30222 0 30222
5284 30222 loop->be->add_io(loop->be, l->fd, HM_EV_READ, 0); /* # uncoverable statement class:ignore_covered_err */
5285 }
5286 30222 }
5287  
5288 typedef struct { hm_loop *loop; hm_listener *lst; } hm_starve;
5289 /*
5290 # uncoverable statement class:ignore_covered_err
5291 # uncoverable subroutine class:ignore_covered_err */
5292 0 static void hm_accept_rearm(pTHX_ void *ud) {
5293 0 hm_starve *s = (hm_starve *)ud; /* # uncoverable statement class:ignore_covered_err */
5294 0 s->lst->starved = 0; /* # uncoverable statement class:ignore_covered_err */
5295 /*
5296 # uncoverable statement class:ignore_covered_err
5297 # uncoverable branch true class:ignore_covered_err
5298 # uncoverable branch false class:ignore_covered_err
5299 # uncoverable condition left count:1 class:ignore_covered_err
5300 # uncoverable condition right count:1 class:ignore_covered_err
5301 # uncoverable condition left count:2 class:ignore_covered_err
5302 # uncoverable condition right count:2 class:ignore_covered_err */
5303 0 ▶ if (!s->loop->stopping && s->lst->fd >= 0)
Branch: 2 branches
BranchTrueFalseTotal
if (!s->loop->stopping && s->lst->fd >= 0) 0 0 0
if (!s->loop->stopping && s->lst->fd >= 0) 0 0 0
5304 0 s->loop->be->add_io(s->loop->be, s->lst->fd, HM_EV_READ, 0); /* # uncoverable statement class:ignore_covered_err */
5305 0 free(s); /* # uncoverable statement class:ignore_covered_err */
5306 0 }
5307 30282 static void hm_accept_starved(pTHX_ hm_loop *loop, hm_listener *lst) {
5308 30282 hm_conn *c = loop->lru_head; /* oldest activity first */
5309 967164 ▶ while (c) {
Branch: 1 branch
BranchTrueFalseTotal
while (c) { 936942 30222 967164
5310 /*
5311 # uncoverable condition right count:1 class:ignore_covered_err
5312 # uncoverable condition right count:2 class:ignore_covered_err
5313 # uncoverable condition right count:3 class:ignore_covered_err
5314 # uncoverable condition right count:4 class:ignore_covered_err
5315 # uncoverable condition right count:5 class:ignore_covered_err
5316 # uncoverable condition right count:6 class:ignore_covered_err
5317 # uncoverable condition right count:7 class:ignore_covered_err
5318 # uncoverable condition right count:8 class:ignore_covered_err */
5319 936942 ▶ if (!c->awaiting && !c->in_app && !c->h2 && !c->tunnel && !c->tls_hs
Branch: 5 branches
BranchTrueFalseTotal
if (!c->awaiting && !c->in_app && !c->h2 && !c->tunnel && !c->tls_hs 936942 0 936942
if (!c->awaiting && !c->in_app && !c->h2 && !c->tunnel && !c->tls_hs 936942 0 936942
if (!c->awaiting && !c->in_app && !c->h2 && !c->tunnel && !c->tls_hs 936942 0 936942
if (!c->awaiting && !c->in_app && !c->h2 && !c->tunnel && !c->tls_hs 936942 0 936942
if (!c->awaiting && !c->in_app && !c->h2 && !c->tunnel && !c->tls_hs 936942 0 936942
5320 936942 ▶ && c->rlen == 0 && c->wlen == c->woff && !c->bsrc.kind
Branch: 3 branches
BranchTrueFalseTotal
&& c->rlen == 0 && c->wlen == c->woff && !c->bsrc.kind 936942 0 936942
&& c->rlen == 0 && c->wlen == c->woff && !c->bsrc.kind 936942 0 936942
&& c->rlen == 0 && c->wlen == c->woff && !c->bsrc.kind 936942 0 936942
5321 936942 ▶ && c->nreqs > 0) {
Branch: 1 branch
BranchTrueFalseTotal
&& c->nreqs > 0) { 60 936882 936942
5322 60 hm_close(aTHX_ loop, c);
5323 60 return;
5324 }
5325 936882 c = c->lru_next;
5326 }
5327 /*
5328 # uncoverable branch true
5329 # uncoverable condition left count:1 class:ignore_covered_err */
5330 30222 ▶ if (lst->starved) return;
Branch: 1 branch
BranchTrueFalseTotal
if (lst->starved) return; 0 30222 30222
5331 {
5332 30222 hm_starve *s = (hm_starve *)hm_xmalloc(sizeof(*s));
5333 30222 s->loop = loop;
5334 30222 s->lst = lst;
5335 30222 lst->starved = 1;
5336 30222 loop->be->remove_io(loop->be, lst->fd, HM_EV_READ);
5337 /*
5338 # uncoverable branch false class:ignore_covered_err
5339 # uncoverable condition right count:1 class:ignore_covered_err */
5340 30222 ▶ if (!hm_add_timer_watch_c(loop, 0.1, hm_accept_rearm, s)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_add_timer_watch_c(loop, 0.1, hm_accept_rearm, s)) { 30222 0 30222
5341 /* No timer to be had: on epoll a timer IS a descriptor, and
5342 * this is the descriptor famine. Re-added from the top of a
5343 * later loop turn instead (hm_accept_unstarve) - the next close
5344 * wakes the loop, and the interval sweep does within five
5345 * seconds regardless. Before this the listener stayed removed
5346 * and the worker never accepted again. */
5347 30222 free(s); /* # uncoverable statement class:ignore_covered_err */
5348 30222 lst->starved = 2; /* # uncoverable statement class:ignore_covered_err */
5349 30222 loop->starved_retry = 1; /* # uncoverable statement class:ignore_covered_err */
5350 }
5351 }
5352 }
5353  
5354 31378 static void hm_accept(pTHX_ hm_loop *loop, hm_listener *lst) {
5355 31378 int batch = 0;
5356 /*
5357 # uncoverable branch true class:ignore_covered_err
5358 # uncoverable condition left count:1 class:ignore_covered_err */
5359 31378 ▶ if (loop->stopping) return;
Branch: 1 branch
BranchTrueFalseTotal
if (loop->stopping) return; 2 31376 31378
5360 939 for (;;) {
5361 struct sockaddr_storage ss;
5362 32315 socklen_t slen = sizeof(ss);
5363 hm_conn *c;
5364 char peer[INET6_ADDRSTRLEN];
5365 int peer_port;
5366 #ifdef HM_HAVE_ACCEPT4
5367 /* Non-blocking and close-on-exec in the accept itself: two fewer
5368 * syscalls per connection, and no window in which a fork from a
5369 * handler inherits a client socket. */
5370 32315 int fd = accept4(lst->fd, (struct sockaddr *)&ss, &slen,
5371 SOCK_NONBLOCK | SOCK_CLOEXEC);
5372 #else
5373 int fd = hm_os_accept(lst->fd, (struct sockaddr *)&ss, &slen);
5374 #endif
5375 32315 ▶ if (fd < 0) { /* EAGAIN: drained */
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { /* EAGAIN: drained */ 31376 939 32315
5376 31376 ▶ if (errno == EMFILE || errno == ENFILE) /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (errno == EMFILE || errno == ENFILE) /* # uncoverable condition left count:2 class:ignore_covered_err */ 1094 30282 31376
if (errno == EMFILE || errno == ENFILE) /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 1094 1094
5377 30282 hm_accept_starved(aTHX_ loop, lst);
5378 31376 break;
5379 }
5380 /*
5381 # uncoverable branch true class:ignore_covered_err
5382 # uncoverable condition left count:1 class:ignore_covered_err */
5383 940 ▶ if (fd >= HM_MAXFD) { hm_os_close(fd); continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (fd >= HM_MAXFD) { hm_os_close(fd); continue; } 0 939 939
5384 #if !defined(HM_HAVE_ACCEPT4) && !defined(_WIN32)
5385 /* Not into anything the application execs: a child that kept a
5386 * client socket open held the client past the response, and could
5387 * write onto it (onto a TLS socket, raw). hm_new_conn sets
5388 * non-blocking. */
5389 (void)fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
5390 #endif
5391 /* Denylist: format the peer and drop a blocked IP HERE, before a
5392 * connection object is allocated or a byte is read - the cheapest
5393 * possible rejection. Fails open (no arena -> deny_check is 0). */
5394 939 hm_fmt_peer(peer, sizeof(peer), &peer_port, (struct sockaddr *)&ss);
5395 939 ▶ if (hm_sa_deny_check(peer)) { hm_os_close(fd); loop->denied++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (hm_sa_deny_check(peer)) { hm_os_close(fd); loop->denied++; continue; } 1 938 939
5396 /* Distinct clients, opt-in: with the sketch off this is one load of
5397 * a NULL, with it on a hash and a CAS-max on the peer string. */
5398 /*
5399 # uncoverable condition right count:2 class:ignore_covered_err
5400 # uncoverable condition right count:3 class:ignore_covered_err */
5401 938 ▶ if (hm_sa_hll && peer[0] && !lst->proxy_protocol) hm_sa_hll_add(peer);
Branch: 3 branches
BranchTrueFalseTotal
if (hm_sa_hll && peer[0] && !lst->proxy_protocol) hm_sa_hll_add(peer); 212 726 938
if (hm_sa_hll && peer[0] && !lst->proxy_protocol) hm_sa_hll_add(peer); 212 0 212
if (hm_sa_hll && peer[0] && !lst->proxy_protocol) hm_sa_hll_add(peer); 212 0 212
5402 938 c = hm_new_conn(loop, fd, lst);
5403 938 memcpy(c->peer, peer, sizeof(c->peer));
5404 938 c->peer_port = peer_port;
5405 938 loop->accepts++;
5406 /*
5407 # uncoverable branch true
5408 # uncoverable condition left count:1 class:ignore_covered_err */
5409 938 ▶ if (c->tls_failed) { hm_close(aTHX_ loop, c); continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_failed) { hm_close(aTHX_ loop, c); continue; } 0 938 938
5410 /* fairness: don't grab the whole queue in one wakeup; the listener
5411 * stays level-readable, so the next iteration resumes */
5412 /*
5413 # uncoverable branch true
5414 # uncoverable condition left count:1 class:ignore_covered_err */
5415 938 ▶ if (++batch >= 64) break;
Branch: 1 branch
BranchTrueFalseTotal
if (++batch >= 64) break; 0 938 938
5416 }
5417 }
5418  
5419 /* Drive the TLS handshake for a connection still in SSL_accept. On success,
5420 * switch to HTTP/2 if ALPN negotiated h2, then start serving. WANT_READ waits
5421 * on the always-armed read watcher; WANT_WRITE arms a write watcher. */
5422 117 static void hm_tls_handshake(pTHX_ hm_conn *c) {
5423 #ifdef HM_HAVE_OPENSSL
5424 117 hm_loop *loop = c->loop;
5425 117 SSL *ssl = (SSL *)c->ssl;
5426 int r;
5427 117 ERR_clear_error();
5428 117 r = SSL_accept(ssl);
5429 117 ▶ if (r == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (r == 1) { 54 63 117
5430 54 c->tls_hs = 0;
5431 54 hm_tls_capture_peer(c); /* protocol/cipher + any client cert */
5432 /* What the kernel took, for stats and tests. The write paths do NOT
5433 * read this - they ask OpenSSL again each time, because the answer
5434 * can change under them (see hm_ktls_tx). */
5435 54 c->ktls_tx = hm_ktls_tx(c) ? 1 : 0;
5436 /*
5437 # uncoverable branch true class:ignore_covered_err
5438 # uncoverable condition left count:1 class:ignore_covered_err */
5439 54 ▶ if (c->writing) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->writing) { 0 54 54
5440 0 loop->be->remove_io(loop->be, c->fd, HM_EV_WRITE); /* # uncoverable statement */
5441 0 c->writing = 0; /* # uncoverable statement */
5442 }
5443 #ifdef HM_HAVE_NGHTTP2
5444 if (c->lst && c->lst->http2) { /* # uncoverable condition right count:1 class:ignore_covered_err */
5445 const unsigned char *proto = NULL;
5446 unsigned int plen = 0;
5447 SSL_get0_alpn_selected(ssl, &proto, &plen);
5448 /*
5449 # uncoverable branch true class:ignore_covered_err
5450 # uncoverable condition left count:1 class:ignore_covered_err
5451 # uncoverable condition left count:2 class:ignore_covered_err
5452 # uncoverable condition right count:2 class:ignore_covered_err
5453 # uncoverable condition left count:3 class:ignore_covered_err */
5454 if (plen == 2 && memcmp(proto, "h2", 2) == 0
5455 && hm_h2_start(aTHX_ c) < 0) {
5456 hm_close(aTHX_ loop, c); /* # uncoverable statement */
5457 return; /* # uncoverable statement */
5458 }
5459 }
5460 #endif
5461 54 hm_readable(aTHX_ c); /* any early app data is already buffered */
5462 54 return;
5463 }
5464 {
5465 63 int e = SSL_get_error(ssl, r);
5466 63 ▶ if (e == SSL_ERROR_WANT_READ) return; /* read watcher armed */
Branch: 1 branch
BranchTrueFalseTotal
if (e == SSL_ERROR_WANT_READ) return; /* read watcher armed */ 56 7 63
5467 /*
5468 # uncoverable branch true
5469 # uncoverable condition left count:1 class:ignore_covered_err */
5470 7 ▶ if (e == SSL_ERROR_WANT_WRITE) {
Branch: 1 branch
BranchTrueFalseTotal
if (e == SSL_ERROR_WANT_WRITE) { 0 7 7
5471 /*
5472 # uncoverable statement
5473 # uncoverable branch true
5474 # uncoverable branch false
5475 # uncoverable condition left count:1
5476 # uncoverable condition right count:1 */
5477 0 ▶ if (!c->writing) {
Branch: 1 branch
BranchTrueFalseTotal
if (!c->writing) { 0 0 0
5478 0 loop->be->add_io(loop->be, c->fd, HM_EV_WRITE, 0); /* # uncoverable statement */
5479 0 c->writing = 1; /* # uncoverable statement */
5480 }
5481 0 return; /* # uncoverable statement */
5482 }
5483 7 hm_close(aTHX_ loop, c); /* handshake failed */
5484 }
5485 #else
5486 (void)c;
5487 #endif
5488 }
5489  
5490 /* ---- timeouts, shutdown, loop -------------------------------------------- */
5491  
5492 /* Resolve an app io-readiness Future (fd became readable/writable), or run
5493 * a persistent watch_io callback. */
5494 22 static void hm_io_event(pTHX_ hm_loop *loop, int fd, int mask, hm_iow *slot) {
5495 22 SV *sv = slot->sv;
5496 PERL_UNUSED_VAR(loop);
5497 22 ▶ if (slot->c_cb) { slot->c_cb(aTHX_ fd, mask, slot->c_ud); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (slot->c_cb) { slot->c_cb(aTHX_ fd, mask, slot->c_ud); return; } 6 16 22
5498 /*
5499 # uncoverable branch true
5500 # uncoverable condition left count:1 class:ignore_covered_err */
5501 16 ▶ if (!sv) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!sv) return; 0 16 16
5502 16 ▶ if (slot->is_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (slot->is_cb) { 8 8 16
5503 8 dSP;
5504 8 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 8 8
5505 8 PUTBACK;
5506 8 call_sv(sv, G_DISCARD | G_EVAL);
5507 8 hm_warn_died(aTHX_ "watch_io callback");
5508 8 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 8 8
5509 } else {
5510 8 slot->sv = NULL; /* one-shot */
5511 8 hmf_done0(aTHX_ sv);
5512 8 SvREFCNT_dec(sv);
5513 }
5514 }
5515  
5516 /* Timeout sweep: close idle keep-alive connections and slow/partial-request
5517 * (slowloris) connections. Runs on a periodic backend timer. */
5518 16 static void hm_sweep(pTHX_ hm_loop *loop) {
5519 16 hm_conn *c = loop->lru_head;
5520 16 time_t now = time(NULL);
5521 16 loop->now = now;
5522 25 ▶ while (c) {
Branch: 1 branch
BranchTrueFalseTotal
while (c) { 9 16 25
5523 9 hm_conn *next = c->lru_next;
5524 /* in_app: a synchronous handler is running for it (and pumping
5525 * this loop); its request is in progress, not idle */
5526 /*
5527 # uncoverable branch false class:ignore_covered_err
5528 # uncoverable condition right count:1 class:ignore_covered_err
5529 # uncoverable condition right count:2 class:ignore_covered_err */
5530 9 ▶ if (!c->awaiting && !c->in_app) {
Branch: 2 branches
BranchTrueFalseTotal
if (!c->awaiting && !c->in_app) { 9 0 9
if (!c->awaiting && !c->in_app) { 9 0 9
5531 9 double idle = difftime(now, c->last_active);
5532 /* Two guards for two phases. The header block is timed from the
5533 * request's first byte, which is the slowloris guard. Once the
5534 * headers are complete and a body is arriving, the clock is
5535 * progress: a body that has not delivered a byte for
5536 * body_timeout is dead, however long it has been coming - and
5537 * an upload slower than max_body in header_timeout is not. */
5538 17 ▶ int slow = c->req_start &&
Branch: 1 branch
BranchTrueFalseTotal
int slow = c->req_start && 8 1 9
5539 8 ▶ (c->body_phase
Branch: 1 branch
BranchTrueFalseTotal
(c->body_phase 5 3 8
5540 5 ▶ ? difftime(now, c->last_rx) > loop->body_timeout
Branch: 1 branch
BranchTrueFalseTotal
? difftime(now, c->last_rx) > loop->body_timeout 1 4 5
5541 3 ▶ : difftime(now, c->req_start) > loop->header_timeout);
Branch: 1 branch
BranchTrueFalseTotal
: difftime(now, c->req_start) > loop->header_timeout); 2 1 3
5542 /* Two OPT-IN floors under the progress clock, for a deployment
5543 * that cannot afford a body dribbled a byte a minute for ever:
5544 * a minimum average rate over the body phase (judged after two
5545 * seconds, so a first slow segment is not a verdict), and a
5546 * ceiling on the whole request from its first byte. */
5547 9 ▶ if (!slow && c->req_start && c->body_phase && loop->body_min_rate) {
Branch: 4 branches
BranchTrueFalseTotal
if (!slow && c->req_start && c->body_phase && loop->body_min_rate) { 6 3 9
if (!slow && c->req_start && c->body_phase && loop->body_min_rate) { 5 1 6
if (!slow && c->req_start && c->body_phase && loop->body_min_rate) { 4 1 5
if (!slow && c->req_start && c->body_phase && loop->body_min_rate) { 1 3 4
5548 1 double span = difftime(now, c->body_t0);
5549 /*
5550 # uncoverable branch false class:ignore_covered_err
5551 # uncoverable condition right count:1 class:ignore_covered_err
5552 # uncoverable condition right count:2 class:ignore_covered_err */
5553 1 ▶ if (span >= 2.0
Branch: 1 branch
BranchTrueFalseTotal
if (span >= 2.0 1 0 1
5554 1 ▶ && (double)c->body_rx / span < (double)loop->body_min_rate)
Branch: 1 branch
BranchTrueFalseTotal
&& (double)c->body_rx / span < (double)loop->body_min_rate) 1 0 1
5555 1 slow = 1;
5556 }
5557 9 ▶ if (!slow && c->req_start && loop->request_timeout > 0 /* # uncoverable condition right count:4 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (!slow && c->req_start && loop->request_timeout > 0 /* # uncoverable condition right count:4 class:ignore_covered_err */ 5 4 9
if (!slow && c->req_start && loop->request_timeout > 0 /* # uncoverable condition right count:4 class:ignore_covered_err */ 4 1 5
if (!slow && c->req_start && loop->request_timeout > 0 /* # uncoverable condition right count:4 class:ignore_covered_err */ 1 3 4
5558 1 ▶ && difftime(now, c->req_start) > loop->request_timeout)
Branch: 1 branch
BranchTrueFalseTotal
&& difftime(now, c->req_start) > loop->request_timeout) 1 0 1
5559 1 slow = 1;
5560 9 ▶ if (idle > loop->idle_timeout || slow)
Branch: 2 branches
BranchTrueFalseTotal
if (idle > loop->idle_timeout || slow) 8 1 9
if (idle > loop->idle_timeout || slow) 5 3 8
5561 6 hm_close(aTHX_ loop, c);
5562 }
5563 9 c = next;
5564 }
5565 16 }
5566  
5567 /* Graceful shutdown: stop accepting, close idle (served) connections,
5568 * finish in-flight work bounded by shutdown_grace; responses during
5569 * shutdown get Connection: close. */
5570 134 static void hm_on_signal(pTHX_ hm_loop *loop) {
5571 hm_conn *c;
5572 /*
5573 # uncoverable branch true
5574 # uncoverable condition left count:1 class:ignore_covered_err */
5575 134 ▶ if (loop->stopping) { loop->stop = 1; return; } /* second signal: hard */
Branch: 1 branch
BranchTrueFalseTotal
if (loop->stopping) { loop->stop = 1; return; } /* second signal: hard */ 0 134 134
5576 134 loop->stopping = 1;
5577 {
5578 int i;
5579 279 ▶ for (i = 0; i < loop->nlisteners; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < loop->nlisteners; i++) { 145 134 279
5580 /*
5581 # uncoverable branch false class:ignore_covered_err
5582 # uncoverable condition right count:1 class:ignore_covered_err */
5583 145 ▶ if (loop->listeners[i].fd >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (loop->listeners[i].fd >= 0) 145 0 145
5584 145 loop->be->remove_io(loop->be, loop->listeners[i].fd, HM_EV_READ);
5585 /* Stop taking datagrams too. A QUIC connection already up is
5586 * not on this fd in any fd-indexed sense, so nothing else here
5587 * reaches it; draining those needs a graceful shutdown that
5588 * sends CONNECTION_CLOSE, which is not implemented. */
5589 /*
5590 # uncoverable branch true class:ignore_covered_err
5591 # uncoverable condition left count:1 class:ignore_covered_err */
5592 145 ▶ if (loop->listeners[i].udp_fd >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (loop->listeners[i].udp_fd >= 0) 0 145 145
5593 0 loop->be->remove_io(loop->be, loop->listeners[i].udp_fd, HM_EV_READ); /* # uncoverable statement class:ignore_covered_err */
5594 }
5595 }
5596 134 hm_quic_shutdown(aTHX_ loop);
5597 134 c = loop->lru_head;
5598 136 ▶ while (c) {
Branch: 1 branch
BranchTrueFalseTotal
while (c) { 2 134 136
5599 2 hm_conn *next = c->lru_next;
5600 /*
5601 # uncoverable condition right count:2 class:ignore_covered_err
5602 # uncoverable condition right count:3 class:ignore_covered_err
5603 # uncoverable condition right count:4 class:ignore_covered_err */
5604 2 ▶ if (!c->awaiting && !c->in_app && c->rlen == 0 && c->wlen == c->woff
Branch: 4 branches
BranchTrueFalseTotal
if (!c->awaiting && !c->in_app && c->rlen == 0 && c->wlen == c->woff 1 1 2
if (!c->awaiting && !c->in_app && c->rlen == 0 && c->wlen == c->woff 1 0 1
if (!c->awaiting && !c->in_app && c->rlen == 0 && c->wlen == c->woff 1 0 1
if (!c->awaiting && !c->in_app && c->rlen == 0 && c->wlen == c->woff 1 0 1
5605 1 ▶ && c->nreqs > 0)
Branch: 1 branch
BranchTrueFalseTotal
&& c->nreqs > 0) 1 0 1
5606 1 hm_close(aTHX_ loop, c);
5607 2 c = next;
5608 }
5609 134 ▶ if (loop->nconns == 0) { loop->stop = 1; return; }
Branch: 1 branch
BranchTrueFalseTotal
if (loop->nconns == 0) { loop->stop = 1; return; } 133 1 134
5610 /*
5611 # uncoverable branch false class:ignore_covered_err
5612 # uncoverable condition right count:1 class:ignore_covered_err */
5613 1 ▶ if (!loop->hardstop_tw) {
Branch: 1 branch
BranchTrueFalseTotal
if (!loop->hardstop_tw) { 1 0 1
5614 1 loop->hardstop_tw = (hm_tw *)hm_xcalloc(1, sizeof(hm_tw));
5615 1 loop->hardstop_tw->kind = HM_TW_HARDSTOP;
5616 1 loop->hardstop_tw->loop = loop;
5617 1 loop->hardstop_tw->due = hm_tw_due(loop->shutdown_grace);
5618 /*
5619 # uncoverable branch true class:ignore_covered_err
5620 # uncoverable condition left count:1 class:ignore_covered_err */
5621 1 ▶ if (loop->be->add_timer(loop->be, loop->shutdown_grace, 1, loop->hardstop_tw) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (loop->be->add_timer(loop->be, loop->shutdown_grace, 1, loop->hardstop_tw) < 0) 0 1 1
5622 0 hm_errlog("Hyperman: cannot arm the shutdown_grace timer: %s\n", strerror(errno)); /* # uncoverable statement */
5623 }
5624 }
5625  
5626 /* Drain a snapshot of the deferred (run-soon) queue. */
5627 35011 static void hm_run_deferred(pTHX_ hm_loop *loop) {
5628 35011 SSize_t n = av_len(loop->deferred) + 1;
5629 SSize_t i;
5630 35011 ▶ if (!n) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return; 35009 2 35011
5631 2 ENTER; SAVETMPS;
5632 5 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 3 2 5
5633 3 SV *cb = sv_2mortal(av_shift(loop->deferred));
5634 3 dSP;
5635 3 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 3 3
5636 3 PUTBACK;
5637 3 call_sv(cb, G_DISCARD | G_EVAL);
5638 3 hm_warn_died(aTHX_ "deferred callback");
5639 }
5640 2 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 2 0 2
5641 }
5642  
5643 /* Re-process connections that hit the fairness cap with requests still
5644 * buffered, now that everyone else has been serviced. */
5645 35011 static void hm_run_again(pTHX_ hm_loop *loop) {
5646 35011 int n = loop->again_n;
5647 int i;
5648 35011 ▶ if (!n) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return; 34999 12 35011
5649 12 loop->again_n = 0;
5650 24 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 12 12 24
5651 12 hm_conn *c = loop->conns[loop->again[i].fd];
5652 /*
5653 # uncoverable branch false class:ignore_covered_err
5654 # uncoverable condition right count:1 class:ignore_covered_err
5655 # uncoverable condition right count:2 class:ignore_covered_err */
5656 12 ▶ if (c && c->id == loop->again[i].id) c->again_queued = 0;
Branch: 2 branches
BranchTrueFalseTotal
if (c && c->id == loop->again[i].id) c->again_queued = 0; 12 0 12
if (c && c->id == loop->again[i].id) c->again_queued = 0; 12 0 12
5657 /*
5658 # uncoverable branch false class:ignore_covered_err
5659 # uncoverable condition right count:1 class:ignore_covered_err
5660 # uncoverable condition right count:2 class:ignore_covered_err
5661 # uncoverable condition right count:3 class:ignore_covered_err
5662 # uncoverable condition right count:4 class:ignore_covered_err
5663 # uncoverable condition left count:5 class:ignore_covered_err
5664 # uncoverable condition right count:5 class:ignore_covered_err
5665 # uncoverable condition right count:6 class:ignore_covered_err */
5666 12 ▶ if (c && c->id == loop->again[i].id && !c->awaiting
Branch: 3 branches
BranchTrueFalseTotal
if (c && c->id == loop->again[i].id && !c->awaiting 12 0 12
if (c && c->id == loop->again[i].id && !c->awaiting 12 0 12
if (c && c->id == loop->again[i].id && !c->awaiting 12 0 12
5667 12 ▶ && (!c->in_app || hm_h2_reentrant(c)) && !c->bsrc.kind) {
Branch: 3 branches
BranchTrueFalseTotal
&& (!c->in_app || hm_h2_reentrant(c)) && !c->bsrc.kind) { 0 12 12
&& (!c->in_app || hm_h2_reentrant(c)) && !c->bsrc.kind) { 0 0 0
&& (!c->in_app || hm_h2_reentrant(c)) && !c->bsrc.kind) { 12 0 12
5668 12 hm_process(aTHX_ c);
5669 /*
5670 # uncoverable branch false class:ignore_covered_err
5671 # uncoverable condition right count:1 class:ignore_covered_err */
5672 12 ▶ if (loop->conns[c->fd] == c) {
Branch: 1 branch
BranchTrueFalseTotal
if (loop->conns[c->fd] == c) { 12 0 12
5673 12 hm_flush(aTHX_ c);
5674 /* THE re-arm point for completion mode, and the reason
5675 * anything that wants a read armed calls hm_again_push
5676 * rather than arming for itself: this runs at the top of a
5677 * loop turn, so hm_process is not on the stack and growing
5678 * the read buffer cannot pull the ground out from under the
5679 * pointers it holds into it. */
5680 12 ▶ if (loop->conns[c->fd] == c) hm_rearm_read(loop, c);
Branch: 1 branch
BranchTrueFalseTotal
if (loop->conns[c->fd] == c) hm_rearm_read(loop, c); 11 1 12
5681 }
5682 }
5683 }
5684 }
5685  
5686 35088 static void hm_dispatch(pTHX_ hm_loop *loop, hm_event *ev) {
5687 /*
5688 # uncoverable branch true count:3
5689 # uncoverable branch true count:6 class:ignore_covered_err */
5690 35088 switch (ev->kind) {
5691 2349 case HM_EV_TIMER: {
5692 2349 hm_tw *tw = (hm_tw *)ev->udata;
5693 2349 ▶ if (!tw || tw->dead) return; /* cancelled earlier in this batch */ /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!tw || tw->dead) return; /* cancelled earlier in this batch */ /* # uncoverable condition left count:1 class:ignore_covered_err */ 2349 0 2349
if (!tw || tw->dead) return; /* cancelled earlier in this batch */ /* # uncoverable condition left count:1 class:ignore_covered_err */ 10 2339 2349
5694 2339 ▶ if (tw->kind == HM_TW_SWEEP) { hm_sweep(aTHX_ loop); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (tw->kind == HM_TW_SWEEP) { hm_sweep(aTHX_ loop); return; } 16 2323 2339
5695 /* A oneshot delivered before its deadline (NetBSD's kqueue timed a
5696 * 300ms Fetch request out at 246ms on a smoker) is re-armed for the
5697 * remainder and not fired. The backend has already dropped it, so the
5698 * same watcher is free to re-add; each re-arm is shorter, so a
5699 * backend that is always early still converges. Within a
5700 * millisecond is the backends' own rounding. */
5701 /*
5702 # uncoverable branch false class:ignore_covered_err
5703 # uncoverable condition right count:1 class:ignore_covered_err */
5704 2323 ▶ if (tw->due > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (tw->due > 0) { 2323 0 2323
5705 2323 double left = tw->due - (double)hm_now_ns() / 1e9;
5706 /* could not re-arm (no descriptor for a timerfd): early beats
5707 * never, so it fires now */
5708 /*
5709 # uncoverable branch true class:ignore_covered_err
5710 # uncoverable condition left count:1 class:ignore_covered_err
5711 # uncoverable condition left count:2 class:ignore_covered_err
5712 # uncoverable condition right count:2 class:ignore_covered_err */
5713 2323 ▶ if (left > 1e-3 && loop->be->add_timer(loop->be, left, 1, tw) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (left > 1e-3 && loop->be->add_timer(loop->be, left, 1, tw) == 0) 0 2323 2323
if (left > 1e-3 && loop->be->add_timer(loop->be, left, 1, tw) == 0) 0 0 0
5714 0 return; /* # uncoverable statement */
5715 }
5716 2323 ▶ if (tw->kind == HM_TW_HARDSTOP) { loop->stop = 1; return; }
Branch: 1 branch
BranchTrueFalseTotal
if (tw->kind == HM_TW_HARDSTOP) { loop->stop = 1; return; } 1 2322 2323
5717 2322 ▶ if (tw->kind == HM_TW_C) { /* ABI timer: no Perl frame */
Branch: 1 branch
BranchTrueFalseTotal
if (tw->kind == HM_TW_C) { /* ABI timer: no Perl frame */ 273 2049 2322
5718 273 hm_abi_timer_cb cb = tw->c_cb;
5719 273 void *ud = tw->c_ud;
5720 273 free(tw);
5721 273 cb(aTHX_ ud);
5722 273 return;
5723 }
5724 {
5725 2049 SV *sv = tw->sv;
5726 2049 int kind = tw->kind;
5727 2049 free(tw);
5728 2049 ▶ if (kind == HM_TW_FUTURE) {
Branch: 1 branch
BranchTrueFalseTotal
if (kind == HM_TW_FUTURE) { 2020 29 2049
5729 2020 hmf_done0(aTHX_ sv);
5730 } else {
5731 29 dSP;
5732 29 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 29 29
5733 29 PUTBACK;
5734 29 call_sv(sv, G_DISCARD | G_EVAL);
5735 29 hm_warn_died(aTHX_ "timer callback");
5736 29 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 29 29
5737 }
5738 2049 SvREFCNT_dec(sv);
5739 }
5740 2049 return;
5741 }
5742 129 case HM_EV_SIGNAL:
5743 129 ▶ if (ev->fd == SIGUSR1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ev->fd == SIGUSR1) { 3 126 129
5744 /* With a scoreboard the supervisor prints every row itself and
5745 * never forwards USR1; this line is the no-arena fallback. */
5746 /*
5747 # uncoverable branch false class:ignore_covered_err
5748 # uncoverable condition right count:1 class:ignore_covered_err */
5749 3 ▶ if (!hm_sa_board_live())
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_sa_board_live()) 3 0 3
5750 6 hm_errlog("Hyperman worker %d: requests=%lu accepts=%lu conns=%d "
5751 "bytes_out=%lu backend=%s%s\n",
5752 3 hm_os_getpid(),
5753 3 (unsigned long)loop->requests, (unsigned long)loop->accepts,
5754 3 loop->nconns, (unsigned long)loop->bytes_out,
5755 6 ▶ loop->be->name, loop->stopping ? " (draining)" : ""); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
loop->be->name, loop->stopping ? " (draining)" : ""); /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 3 3
5756 3 return;
5757 }
5758 126 hm_on_signal(aTHX_ loop);
5759 126 return;
5760 0 case HM_EV_RECV: { /* # uncoverable statement */
5761 /* A read the core asked for has finished; the bytes are already in
5762 * the buffer it supplied. */
5763 0 int fd = ev->fd; /* # uncoverable statement */
5764 /*
5765 # uncoverable statement
5766 # uncoverable condition left count:1
5767 # uncoverable condition right count:1
5768 # uncoverable condition left count:2
5769 # uncoverable condition right count:2 */
5770 0 ▶ hm_conn *c = (fd >= 0 && fd < HM_MAXFD) ? loop->conns[fd] : NULL;
Branch: 2 branches
BranchTrueFalseTotal
hm_conn *c = (fd >= 0 && fd < HM_MAXFD) ? loop->conns[fd] : NULL; 0 0 0
hm_conn *c = (fd >= 0 && fd < HM_MAXFD) ? loop->conns[fd] : NULL; 0 0 0
5771  
5772 /* Is this OUR read? A completion can outlive its connection - the fd
5773 * may have been closed and reused since - and the token is the only
5774 * thing that can tell the difference. Anything that does not match
5775 * belongs to a connection that is gone, and the buffer it was reading
5776 * into is parked waiting for exactly this moment. */
5777 /*
5778 # uncoverable statement
5779 # uncoverable branch true
5780 # uncoverable branch false
5781 # uncoverable condition left count:1
5782 # uncoverable condition right count:1
5783 # uncoverable condition left count:2
5784 # uncoverable condition right count:2
5785 # uncoverable condition left count:3
5786 # uncoverable condition right count:3 */
5787 0 ▶ if (!c || !c->recv_inflight || HM_RECV_TOKEN(c) != ev->token) {
Branch: 3 branches
BranchTrueFalseTotal
if (!c || !c->recv_inflight || HM_RECV_TOKEN(c) != ev->token) { 0 0 0
if (!c || !c->recv_inflight || HM_RECV_TOKEN(c) != ev->token) { 0 0 0
if (!c || !c->recv_inflight || HM_RECV_TOKEN(c) != ev->token) { 0 0 0
5788 0 hm_quar_release(loop, ev->token); /* # uncoverable statement */
5789 0 return; /* # uncoverable statement */
5790 }
5791 0 c->recv_inflight = 0; /* # uncoverable statement */
5792 /*
5793 # uncoverable statement
5794 # uncoverable branch true
5795 # uncoverable branch false
5796 # uncoverable condition left count:1
5797 # uncoverable condition right count:1 */
5798 0 ▶ if (ev->res <= 0) { /* EOF, error, or the cancel from a close */
Branch: 1 branch
BranchTrueFalseTotal
if (ev->res <= 0) { /* EOF, error, or the cancel from a close */ 0 0 0
5799 /* The peer half-closed a tunnel: fin to the handle first, as
5800 * hm_readable's zero read does, then the close says the rest. */
5801 /*
5802 # uncoverable statement
5803 # uncoverable branch true
5804 # uncoverable branch false
5805 # uncoverable condition left count:1
5806 # uncoverable condition right count:1
5807 # uncoverable condition left count:2
5808 # uncoverable condition right count:2 */
5809 0 ▶ if (c->tunnel && ev->res == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (c->tunnel && ev->res == 0) { 0 0 0
if (c->tunnel && ev->res == 0) { 0 0 0
5810 0 (void)hm_stream_deliver(aTHX_ c->fd, c->id, -1, c->rbuf, 0, 1); /* # uncoverable statement */
5811 /*
5812 # uncoverable statement
5813 # uncoverable branch true
5814 # uncoverable branch false
5815 # uncoverable condition left count:1
5816 # uncoverable condition right count:1 */
5817 0 ▶ if (loop->conns[fd] != c) return;
Branch: 1 branch
BranchTrueFalseTotal
if (loop->conns[fd] != c) return; 0 0 0
5818 }
5819 0 hm_close(aTHX_ loop, c); /* # uncoverable statement */
5820 0 return; /* # uncoverable statement */
5821 }
5822 0 c->rlen += (size_t)ev->res; /* # uncoverable statement */
5823 0 c->last_rx = loop->now; /* # uncoverable statement */
5824 0 c->body_rx += (UV)ev->res; /* # uncoverable statement */
5825 0 c->last_active = loop->now; /* # uncoverable statement */
5826 0 hm_lru_touch(loop, c); /* # uncoverable statement */
5827 /* A tunnel carries no requests: its bytes go to the handle. The
5828 * parser would hold them as an unfinished request line forever. */
5829 /*
5830 # uncoverable statement
5831 # uncoverable branch true
5832 # uncoverable branch false
5833 # uncoverable branch true count:2
5834 # uncoverable branch false count:2
5835 # uncoverable branch true count:3
5836 # uncoverable branch false count:3
5837 # uncoverable condition left count:1
5838 # uncoverable condition right count:1
5839 # uncoverable condition left count:2
5840 # uncoverable condition right count:2 */
5841 0 ▶ if (c->tunnel == 1) { if (!hm_tunnel_bytes(aTHX_ c)) return; }
Branch: 2 branches
BranchTrueFalseTotal
if (c->tunnel == 1) { if (!hm_tunnel_bytes(aTHX_ c)) return; } 0 0 0
if (c->tunnel == 1) { if (!hm_tunnel_bytes(aTHX_ c)) return; } 0 0 0
5842 /* The same rule hm_readable applies: a synchronous handler's frame
5843 * is on the stack for this connection (its ->get pumped the loop,
5844 * and this read was in flight when it was handed the pipeline), or
5845 * a tunnel was opened inside it and the request is still in rbuf.
5846 * That frame owns the buffer. The bytes wait in it, nothing is
5847 * dispatched or re-armed from under it, and whoever called that
5848 * frame re-arms when it returns (hm_deliver's hm_again_push, or the
5849 * re-arm below in the frame that called hm_process). Without this a
5850 * second hm_process re-parsed the request at rbuf[0], called the
5851 * application twice, and left the outer frame to memmove with a
5852 * consumed count larger than what remained. */
5853 /*
5854 # uncoverable statement
5855 # uncoverable branch true
5856 # uncoverable branch false
5857 # uncoverable condition left count:1
5858 # uncoverable condition right count:1
5859 # uncoverable condition left count:2
5860 # uncoverable condition right count:2
5861 # uncoverable condition left count:3
5862 # uncoverable condition right count:3 */
5863 0 ▶ else if (c->tunnel == 2 || (c->in_app && !hm_h2_reentrant(c))) return;
Branch: 3 branches
BranchTrueFalseTotal
else if (c->tunnel == 2 || (c->in_app && !hm_h2_reentrant(c))) return; 0 0 0
else if (c->tunnel == 2 || (c->in_app && !hm_h2_reentrant(c))) return; 0 0 0
else if (c->tunnel == 2 || (c->in_app && !hm_h2_reentrant(c))) return; 0 0 0
5864 0 else hm_process(aTHX_ c); /* # uncoverable statement */
5865 /*
5866 # uncoverable statement
5867 # uncoverable branch true
5868 # uncoverable branch false
5869 # uncoverable condition left count:1
5870 # uncoverable condition right count:1 */
5871 0 ▶ if (loop->conns[fd] != c) return; /* dispatch closed it */
Branch: 1 branch
BranchTrueFalseTotal
if (loop->conns[fd] != c) return; /* dispatch closed it */ 0 0 0
5872 0 hm_flush(aTHX_ c); /* # uncoverable statement */
5873 /*
5874 # uncoverable statement
5875 # uncoverable branch true
5876 # uncoverable branch false
5877 # uncoverable condition left count:1
5878 # uncoverable condition right count:1 */
5879 0 ▶ if (loop->conns[fd] != c) return; /* flush closed it */
Branch: 1 branch
BranchTrueFalseTotal
if (loop->conns[fd] != c) return; /* flush closed it */ 0 0 0
5880 /* Re-armed even while PARKED or streaming, and that is not an
5881 * optimisation - it is how a disconnect is noticed at all.
5882 *
5883 * A readiness backend keeps a persistent watcher on every connection,
5884 * so a client that walks away from a parked async request wakes the
5885 * loop with EOF and the response Future gets cancelled. Completion
5886 * mode has no such watcher: with no read outstanding the close is
5887 * simply never seen, the Future is never cancelled, and the
5888 * connection sits there until the idle sweep. t/13-async-psgi.t
5889 * catches exactly this.
5890 *
5891 * Safe because the request's bytes are gone from the buffer before it
5892 * parks - hm_process consumes them, and hm_new_input COPIES the body
5893 * into an SV - so nothing points into rbuf across a park, and the
5894 * kernel only ever writes past rlen. */
5895 0 hm_rearm_read(loop, c); /* # uncoverable statement */
5896 0 return; /* # uncoverable statement */
5897 }
5898 32596 case HM_EV_READ: {
5899 32596 int fd = ev->fd;
5900 hm_conn *c;
5901 hm_listener *lst;
5902 32596 ▶ if ((lst = hm_listener_for_fd(loop, fd))) { hm_accept(aTHX_ loop, lst); return; }
Branch: 1 branch
BranchTrueFalseTotal
if ((lst = hm_listener_for_fd(loop, fd))) { hm_accept(aTHX_ loop, lst); return; } 31378 1218 32596
5903 /* Between the two, because a UDP listener fd is never in conns[]:
5904 * QUIC multiplexes every connection over the one socket, so there is
5905 * nothing fd-indexed to find. */
5906 /*
5907 # uncoverable branch true class:ignore_covered_err
5908 # uncoverable condition left count:1 class:ignore_covered_err */
5909 1218 ▶ if ((lst = hm_listener_for_udp(loop, fd))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((lst = hm_listener_for_udp(loop, fd))) { 0 1218 1218
5910 0 hm_quic_readable(aTHX_ loop, lst); /* # uncoverable statement class:ignore_covered_err */
5911 0 return; /* # uncoverable statement class:ignore_covered_err */
5912 }
5913 1218 ▶ if ((c = loop->conns[fd])) {
Branch: 1 branch
BranchTrueFalseTotal
if ((c = loop->conns[fd])) { 1197 21 1218
5914 1197 ▶ if (c->pp_wait) hm_proxy_readable(aTHX_ c);
Branch: 1 branch
BranchTrueFalseTotal
if (c->pp_wait) hm_proxy_readable(aTHX_ c); 50 1147 1197
5915 1147 ▶ else if (c->tls_hs) hm_tls_handshake(aTHX_ c);
Branch: 1 branch
BranchTrueFalseTotal
else if (c->tls_hs) hm_tls_handshake(aTHX_ c); 116 1031 1147
5916 1031 else hm_readable(aTHX_ c);
5917 }
5918 /*
5919 # uncoverable branch false class:ignore_covered_err
5920 # uncoverable condition right count:1 class:ignore_covered_err
5921 # uncoverable condition right count:2 class:ignore_covered_err */
5922 21 ▶ else if (loop->io_r[fd].sv || loop->io_r[fd].c_cb)
Branch: 2 branches
BranchTrueFalseTotal
else if (loop->io_r[fd].sv || loop->io_r[fd].c_cb) 6 15 21
else if (loop->io_r[fd].sv || loop->io_r[fd].c_cb) 6 0 6
5923 21 hm_io_event(aTHX_ loop, fd, HM_EV_READ, &loop->io_r[fd]);
5924 1218 return;
5925 }
5926 14 case HM_EV_WRITE: {
5927 14 int fd = ev->fd;
5928 hm_conn *c;
5929 14 ▶ if ((c = loop->conns[fd])) {
Branch: 1 branch
BranchTrueFalseTotal
if ((c = loop->conns[fd])) { 13 1 14
5930 /* TLS post-handshake: when the write watcher is armed because
5931 * SSL_read wanted a writable socket, route through hm_readable so
5932 * the blocked read is re-driven (it flushes pending output at the
5933 * end). Otherwise this is ordinary write backpressure and the
5934 * read side is not blocked at all - going through hm_readable
5935 * then costs an SSL_read that can only return EAGAIN, once per
5936 * writable wakeup for the whole of a large response. */
5937 /*
5938 # uncoverable branch true
5939 # uncoverable condition left count:1 class:ignore_covered_err */
5940 13 ▶ if (c->tls_hs) hm_tls_handshake(aTHX_ c);
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_hs) hm_tls_handshake(aTHX_ c); 0 13 13
5941 /*
5942 # uncoverable branch true class:ignore_covered_err
5943 # uncoverable condition left count:1 class:ignore_covered_err
5944 # uncoverable condition left count:2 class:ignore_covered_err */
5945 13 ▶ else if (c->ssl && c->tls_r_wants_w) hm_readable(aTHX_ c);
Branch: 2 branches
BranchTrueFalseTotal
else if (c->ssl && c->tls_r_wants_w) hm_readable(aTHX_ c); 3 10 13
else if (c->ssl && c->tls_r_wants_w) hm_readable(aTHX_ c); 0 3 3
5946 13 else hm_flush(aTHX_ c);
5947 }
5948 /*
5949 # uncoverable condition left count:2 class:ignore_covered_err
5950 # uncoverable branch false class:ignore_covered_err
5951 # uncoverable condition right count:1 class:ignore_covered_err
5952 # uncoverable condition right count:2 class:ignore_covered_err */
5953 1 ▶ else if (loop->io_w[fd].sv || loop->io_w[fd].c_cb)
Branch: 2 branches
BranchTrueFalseTotal
else if (loop->io_w[fd].sv || loop->io_w[fd].c_cb) 0 1 1
else if (loop->io_w[fd].sv || loop->io_w[fd].c_cb) 0 0 0
5954 1 hm_io_event(aTHX_ loop, fd, HM_EV_WRITE, &loop->io_w[fd]);
5955 14 return;
5956 }
5957 }
5958 }
5959  
5960 /* Run the loop until ->stop, or (when awaiting) until $until is ready.
5961 * Re-entrant: ->get/->await inside a handler pumps this same loop, so the
5962 * worker keeps serving other connections while awaiting. */
5963 /* Dispatch what the calling frames took from the backend and have not yet
5964 * reached, nearest first. Each index moves past an event BEFORE it is
5965 * dispatched, so a run nested deeper inside it skips it too. */
5966 26 static void hm_batch_drain(pTHX_ hm_loop *loop, hm_batch *b) {
5967 52 ▶ for (; b; b = b->up)
Branch: 1 branch
BranchTrueFalseTotal
for (; b; b = b->up) 26 26 52
5968 34 ▶ while (b->i < b->n) hm_dispatch(aTHX_ loop, &b->evs[b->i++]);
Branch: 1 branch
BranchTrueFalseTotal
while (b->i < b->n) hm_dispatch(aTHX_ loop, &b->evs[b->i++]); 8 26 34
5969 26 }
5970  
5971 226 static void hm_loop_run(pTHX_ hm_loop *loop, SV *until) {
5972 hm_event evs[HM_MAXEV];
5973 hm_batch b;
5974 /* The fire queue is pumped by whoever enqueues, unless a pump is
5975 * already on the stack (hm_fq_active), which makes a continuation that
5976 * enqueues from inside a callback leave its work for the pump above
5977 * it. That pump is above THIS frame too when a handler reached through
5978 * the queue (a parked response delivering the next pipelined request)
5979 * calls ->get: the nested run here must pump for itself, or every
5980 * continuation it waits on - a ->then, a combinator - sits in the queue
5981 * until it returns, which is never.
5982 *
5983 * This frame's state goes on the save stack, so a croak out of here (a
5984 * die from a signal handler under a ->get) restores it as a return
5985 * does. loop->batch most of all: left behind, it names this frame's
5986 * evs after the frame is gone. */
5987 226 ENTER;
5988 226 SAVEINT(hm_fq_active);
5989 226 SAVEVPTR(hm_cur_loop);
5990 226 SAVEINT(loop->running);
5991 226 SAVEVPTR(loop->batch);
5992 226 hm_fq_active = 0;
5993 226 hm_cur_loop = loop;
5994 226 loop->running++;
5995 226 b.evs = evs;
5996 226 b.n = b.i = 0;
5997 226 b.up = loop->batch;
5998 226 loop->batch = &b;
5999 /* Cleared for the OUTERMOST run only. A nested run is a handler's ->get
6000 * pumping this loop, and a stop asked for while it runs - the second
6001 * TERM, the shutdown_grace hard-stop, the last connection closing while
6002 * draining - is the outer run's to act on: the nested one breaks and
6003 * leaves the flag set. Clearing it on exit, as this once did, swallowed
6004 * every stop that landed under a handler, and the hard-stop timer is
6005 * one-shot so the worker then never left. */
6006 226 ▶ if (loop->running == 1) loop->stop = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (loop->running == 1) loop->stop = 0; 205 21 226
6007 34785 for (;;) {
6008 35011 double timeout = -1.0;
6009 int n, i;
6010 35011 loop->now = time(NULL);
6011 35011 ▶ if (b.up) hm_batch_drain(aTHX_ loop, b.up);
Branch: 1 branch
BranchTrueFalseTotal
if (b.up) hm_batch_drain(aTHX_ loop, b.up); 26 34985 35011
6012 35011 ▶ if (loop->recycle_pending && !loop->stopping) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (loop->recycle_pending && !loop->stopping) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 8 35003 35011
if (loop->recycle_pending && !loop->stopping) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 8 0 8
6013 8 loop->recycle_pending = 0;
6014 8 hm_on_signal(aTHX_ loop); /* max_requests recycle: drain + exit */
6015 }
6016 35011 hm_run_deferred(aTHX_ loop);
6017 35011 hm_run_again(aTHX_ loop);
6018 35011 ▶ if (loop->starved_retry) hm_accept_unstarve(loop);
Branch: 1 branch
BranchTrueFalseTotal
if (loop->starved_retry) hm_accept_unstarve(loop); 30222 4789 35011
6019 35011 ▶ if (loop->stop) break;
Branch: 1 branch
BranchTrueFalseTotal
if (loop->stop) break; 12 34999 35011
6020 34999 ▶ if (until && hmf_state(aTHX_ until) != HMF_PENDING) break;
Branch: 2 branches
BranchTrueFalseTotal
if (until && hmf_state(aTHX_ until) != HMF_PENDING) break; 2077 32922 34999
if (until && hmf_state(aTHX_ until) != HMF_PENDING) break; 4 2073 2077
6021 34995 ▶ if (av_len(loop->deferred) >= 0 || loop->again_n) timeout = 0.0;
Branch: 2 branches
BranchTrueFalseTotal
if (av_len(loop->deferred) >= 0 || loop->again_n) timeout = 0.0; 34994 1 34995
if (av_len(loop->deferred) >= 0 || loop->again_n) timeout = 0.0; 1 34993 34994
6022 /* QUIC's deadlines do not live in the backend's timer list - see the
6023 * costing in hm_quic.h - so the wait is clamped to the nearest one
6024 * here instead. Rounding UP is correct: ngtcp2 copes with a late
6025 * expiry and not with an early one, and the backends already round
6026 * to milliseconds. One compare when there is no QUIC listener. */
6027 /*
6028 # uncoverable branch true class:ignore_covered_err
6029 # uncoverable condition left count:1 class:ignore_covered_err */
6030 34995 ▶ if (loop->qheap.n) {
Branch: 1 branch
BranchTrueFalseTotal
if (loop->qheap.n) { 0 34995 34995
6031 /* Through a function rather than reaching into the heap here:
6032 * hm_qconn is an opaque forward declaration on a build without
6033 * QUIC, and dereferencing it would compile only where the
6034 * feature is present - which is not where the mistake shows up. */
6035 0 uint64_t now_ns = hm_now_ns(); /* # uncoverable statement class:ignore_covered_err */
6036 0 uint64_t due = hm_quic_next_deadline(loop); /* # uncoverable statement class:ignore_covered_err */
6037 /*
6038 # uncoverable statement class:ignore_covered_err
6039 # uncoverable branch true class:ignore_covered_err
6040 # uncoverable branch false class:ignore_covered_err
6041 # uncoverable condition left count:1 class:ignore_covered_err
6042 # uncoverable condition right count:1 class:ignore_covered_err */
6043 0 ▶ if (due != (uint64_t)-1) {
Branch: 1 branch
BranchTrueFalseTotal
if (due != (uint64_t)-1) { 0 0 0
6044 0 double secs = due <= now_ns ? 0.0 /* # uncoverable statement class:ignore_covered_err */
6045 0 ▶ : (double)(due - now_ns) / 1e9; /* # uncoverable statement class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
: (double)(due - now_ns) / 1e9; /* # uncoverable statement class:ignore_covered_err */ 0 0 0
6046 /*
6047 # uncoverable statement class:ignore_covered_err
6048 # uncoverable branch true class:ignore_covered_err
6049 # uncoverable branch false class:ignore_covered_err
6050 # uncoverable condition left count:1 class:ignore_covered_err
6051 # uncoverable condition right count:1 class:ignore_covered_err
6052 # uncoverable condition left count:2 class:ignore_covered_err
6053 # uncoverable condition right count:2 class:ignore_covered_err */
6054 0 ▶ if (timeout < 0.0 || secs < timeout) timeout = secs;
Branch: 2 branches
BranchTrueFalseTotal
if (timeout < 0.0 || secs < timeout) timeout = secs; 0 0 0
if (timeout < 0.0 || secs < timeout) timeout = secs; 0 0 0
6055 }
6056 }
6057 34995 n = loop->be->wait(loop->be, evs, HM_MAXEV, timeout);
6058 /*
6059 # uncoverable branch true
6060 # uncoverable condition left count:1 class:ignore_covered_err
6061 # uncoverable condition left count:2 class:ignore_covered_err
6062 # uncoverable condition right count:2 class:ignore_covered_err */
6063 34995 ▶ if (n < 0) { if (errno == EINTR) continue; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0) { if (errno == EINTR) continue; break; } 0 34995 34995
if (n < 0) { if (errno == EINTR) continue; break; } 0 0 0
6064 34995 b.n = n;
6065 34995 b.i = 0;
6066 70075 ▶ while (b.i < b.n) hm_dispatch(aTHX_ loop, &evs[b.i++]);
Branch: 1 branch
BranchTrueFalseTotal
while (b.i < b.n) hm_dispatch(aTHX_ loop, &evs[b.i++]); 35080 34995 70075
6067 34995 b.n = 0;
6068 34995 ▶ if (loop->tw_gy && loop->running == 1) hm_tw_gy_sweep(loop); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (loop->tw_gy && loop->running == 1) hm_tw_gy_sweep(loop); /* # uncoverable condition right count:2 class:ignore_covered_err */ 11 34984 34995
if (loop->tw_gy && loop->running == 1) hm_tw_gy_sweep(loop); /* # uncoverable condition right count:2 class:ignore_covered_err */ 11 0 11
6069 /*
6070 # uncoverable branch true class:ignore_covered_err
6071 # uncoverable condition left count:1 class:ignore_covered_err */
6072 34995 ▶ if (loop->qheap.n) hm_quic_expire(aTHX_ loop);
Branch: 1 branch
BranchTrueFalseTotal
if (loop->qheap.n) hm_quic_expire(aTHX_ loop); 0 34995 34995
6073 34995 ▶ if (loop->log_fd >= 0 && loop->log_len) hm_log_flush(loop);
Branch: 2 branches
BranchTrueFalseTotal
if (loop->log_fd >= 0 && loop->log_len) hm_log_flush(loop); 187 34808 34995
if (loop->log_fd >= 0 && loop->log_len) hm_log_flush(loop); 67 120 187
6074 34995 ▶ if (loop->stop) break;
Branch: 1 branch
BranchTrueFalseTotal
if (loop->stop) break; 145 34850 34995
6075 34850 ▶ if (until && hmf_state(aTHX_ until) != HMF_PENDING) break;
Branch: 2 branches
BranchTrueFalseTotal
if (until && hmf_state(aTHX_ until) != HMF_PENDING) break; 2070 32780 34850
if (until && hmf_state(aTHX_ until) != HMF_PENDING) break; 65 2005 2070
6076 }
6077 /*
6078 # uncoverable branch true class:ignore_covered_err
6079 # uncoverable condition left count:1 class:ignore_covered_err
6080 # uncoverable condition left count:2 class:ignore_covered_err
6081 # uncoverable condition right count:2 class:ignore_covered_err */
6082 226 ▶ if (loop->tw_gy && loop->running == 1) hm_tw_gy_sweep(loop);
Branch: 2 branches
BranchTrueFalseTotal
if (loop->tw_gy && loop->running == 1) hm_tw_gy_sweep(loop); 0 226 226
if (loop->tw_gy && loop->running == 1) hm_tw_gy_sweep(loop); 0 0 0
6083 226 LEAVE;
6084 226 }
6085  
6086 /* ---- loop lifecycle ------------------------------------------------------- */
6087  
6088 177 static hm_loop *hm_loop_new(pTHX_ const char *backend_name) {
6089 177 hm_loop *loop = (hm_loop *)hm_xcalloc(1, sizeof(hm_loop));
6090 /*
6091 # uncoverable branch true
6092 # uncoverable condition left count:1 class:ignore_covered_err */
6093 177 ▶ if (!loop) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!loop) return NULL; 0 177 177
6094 177 ▶ if (!backend_name || !*backend_name) /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!backend_name || !*backend_name) /* # uncoverable condition left count:2 class:ignore_covered_err */ 24 153 177
if (!backend_name || !*backend_name) /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 24 24
6095 153 backend_name = getenv("HYPERMAN_BACKEND");
6096 177 loop->be = hm_backend_create(backend_name);
6097 177 ▶ if (!loop->be) { free(loop); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!loop->be) { free(loop); return NULL; } 4 173 177
6098 173 loop->pid = hm_os_getpid();
6099 173 loop->conns = (hm_conn **)hm_xcalloc(HM_MAXFD, sizeof(hm_conn *));
6100 173 loop->io_r = (hm_iow *)hm_xcalloc(HM_MAXFD, sizeof(hm_iow));
6101 173 loop->io_w = (hm_iow *)hm_xcalloc(HM_MAXFD, sizeof(hm_iow));
6102 173 loop->deferred = newAV();
6103 173 loop->log_fd = -1;
6104 173 loop->now = time(NULL);
6105 173 loop->idle_timeout = 60.0;
6106 173 loop->header_timeout = 30.0;
6107 173 loop->body_timeout = 60.0;
6108 173 loop->max_pipeline = 32;
6109 173 loop->max_read = 16 * 1024 * 1024; /* request ceiling */
6110 173 loop->compress = 0; /* off by default */
6111 173 loop->compress_min = HZ_MIN_LENGTH;
6112 173 loop->compress_level = HZ_LEVEL;
6113 173 loop->shutdown_grace = 30.0;
6114 /* Completion-mode reads, on wherever the backend can do them.
6115 *
6116 * That is io_uring alone, and io_uring IS auto-selected wherever
6117 * liburing was found at build time (the chooser goes kqueue, io_uring,
6118 * epoll, poll), so on such a Linux box this is the default read path -
6119 * which is why every change to how bytes ARRIVE lands on both the
6120 * readiness and the completion path (t/49 sweeps both).
6121 *
6122 * HYPERMAN_COMPLETION overrides, exactly as HYPERMAN_BACKEND overrides
6123 * the chooser; run(completion => ...) overrides both, being the most
6124 * specific thing the caller said. */
6125 173 loop->completion = loop->be->submit_recv ? 1 : 0;
6126 {
6127 173 const char *v = getenv("HYPERMAN_COMPLETION");
6128 /*
6129 # uncoverable branch true class:ignore_covered_err
6130 # uncoverable condition left count:1 class:ignore_covered_err
6131 # uncoverable condition left count:2 class:ignore_covered_err
6132 # uncoverable condition right count:2 class:ignore_covered_err */
6133 173 ▶ if (v && *v)
Branch: 2 branches
BranchTrueFalseTotal
if (v && *v) 0 173 173
if (v && *v) 0 0 0
6134 0 loop->completion = /* # uncoverable statement */
6135 /*
6136 # uncoverable statement
6137 # uncoverable condition left count:1
6138 # uncoverable condition right count:1
6139 # uncoverable condition left count:2
6140 # uncoverable condition right count:2 */
6141 0 ▶ (*v != '0' && loop->be->submit_recv) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
(*v != '0' && loop->be->submit_recv) ? 1 : 0; 0 0 0
(*v != '0' && loop->be->submit_recv) ? 1 : 0; 0 0 0
6142 }
6143 173 return loop;
6144 }
6145  
6146 /* Free a loop.
6147 *
6148 * A loop belongs to the process that created it, and every DESCRIPTOR in it -
6149 * the backend's own (kqueue/epoll), the connection sockets, the log file -
6150 * belongs to that process too. A loop inherited across a fork is therefore
6151 * freed WITHOUT closing anything: the memory is ours to release, the fds are
6152 * not.
6153 *
6154 * Closing them is destructive, not merely untidy. kqueue descriptors do not
6155 * survive fork(2): the kernel invalidates the child's copy, so the number is
6156 * free and the child's very next kqueue() is handed it back - closing the
6157 * inherited loop then shuts the CHILD'S OWN queue and nothing it watches
6158 * fires again. The epoll and connection fds are the parent's, still live, and
6159 * closing our duplicates frees numbers the child immediately reuses. */
6160 /* A loop inherited across a fork must never reach its backend. On Linux an
6161 * epoll INSTANCE is a shared kernel object: fork duplicates the fd but both
6162 * processes' epoll_ctl calls edit the SAME interest list, so a child
6163 * "cleaning up" inherited watchers silently deregisters the PARENT's fds -
6164 * the parent then waits on an empty set forever (the DBIx::Loop
6165 * disown-in-a-forked-child hang, Punk t/39). kqueue merely hands the child
6166 * a dead descriptor, which is why macOS never showed it. The copied
6167 * user-space tables are ours to update; the kernel object is not. */
6168 #define HM_LOOP_INHERITED(loop) ((loop)->pid != hm_os_getpid())
6169  
6170 171 static void hm_loop_free(pTHX_ hm_loop *loop) {
6171 int i;
6172 int owned;
6173 /*
6174 # uncoverable branch true
6175 # uncoverable condition left count:1 class:ignore_covered_err */
6176 171 ▶ if (!loop) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!loop) return; 0 171 171
6177 171 owned = (loop->pid == hm_os_getpid());
6178 /* Read buffers still parked for completions that will now never be
6179 * reaped. The ring is torn down with the backend below, so nothing can
6180 * be writing into them by the time this returns. */
6181 171 hm_quar_free_all(loop);
6182 171 hm_tw_gy_sweep(loop);
6183 11206827 ▶ for (i = 0; i < HM_MAXFD; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < HM_MAXFD; i++) { 11206656 171 11206827
6184 11206656 ▶ if (loop->conns[i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (loop->conns[i]) { 1 11206655 11206656
6185 1 hm_conn *c = loop->conns[i];
6186 /*
6187 # uncoverable branch false class:ignore_covered_err
6188 # uncoverable condition right count:1 class:ignore_covered_err */
6189 1 ▶ if (owned) hm_os_close(c->fd);
Branch: 1 branch
BranchTrueFalseTotal
if (owned) hm_os_close(c->fd); 1 0 1
6190 /*
6191 # uncoverable branch true class:ignore_covered_err
6192 # uncoverable condition left count:1 class:ignore_covered_err */
6193 1 ▶ if (c->h2) hm_h2_free(aTHX_ c->h2);
Branch: 1 branch
BranchTrueFalseTotal
if (c->h2) hm_h2_free(aTHX_ c->h2); 0 1 1
6194 /*
6195 # uncoverable branch false class:ignore_covered_err
6196 # uncoverable condition right count:1 class:ignore_covered_err */
6197 1 ▶ if (c->io_sv) SvREFCNT_dec(c->io_sv);
Branch: 1 branch
BranchTrueFalseTotal
if (c->io_sv) SvREFCNT_dec(c->io_sv); 1 0 1
6198 /*
6199 # uncoverable branch false class:ignore_covered_err
6200 # uncoverable condition right count:1 class:ignore_covered_err */
6201 1 ▶ if (c->resp_f) SvREFCNT_dec(c->resp_f);
Branch: 1 branch
BranchTrueFalseTotal
if (c->resp_f) SvREFCNT_dec(c->resp_f); 1 0 1
6202 /*
6203 # uncoverable branch true class:ignore_covered_err
6204 # uncoverable condition left count:1 class:ignore_covered_err */
6205 1 ▶ if (c->env_sv) SvREFCNT_dec(c->env_sv);
Branch: 1 branch
BranchTrueFalseTotal
if (c->env_sv) SvREFCNT_dec(c->env_sv); 0 1 1
6206 /*
6207 # uncoverable branch false class:ignore_covered_err
6208 # uncoverable condition right count:1 class:ignore_covered_err */
6209 1 ▶ if (c->peer_sv) SvREFCNT_dec(c->peer_sv);
Branch: 1 branch
BranchTrueFalseTotal
if (c->peer_sv) SvREFCNT_dec(c->peer_sv); 1 0 1
6210 /*
6211 # uncoverable branch false class:ignore_covered_err
6212 # uncoverable condition right count:1 class:ignore_covered_err */
6213 1 ▶ if (c->peer_host_sv) SvREFCNT_dec(c->peer_host_sv);
Branch: 1 branch
BranchTrueFalseTotal
if (c->peer_host_sv) SvREFCNT_dec(c->peer_host_sv); 1 0 1
6214 /*
6215 # uncoverable branch false class:ignore_covered_err
6216 # uncoverable condition right count:1 class:ignore_covered_err */
6217 1 ▶ if (c->peer_port_sv) SvREFCNT_dec(c->peer_port_sv);
Branch: 1 branch
BranchTrueFalseTotal
if (c->peer_port_sv) SvREFCNT_dec(c->peer_port_sv); 1 0 1
6218 /*
6219 # uncoverable branch false class:ignore_covered_err
6220 # uncoverable condition right count:1 class:ignore_covered_err */
6221 1 ▶ if (c->sname_sv) SvREFCNT_dec(c->sname_sv);
Branch: 1 branch
BranchTrueFalseTotal
if (c->sname_sv) SvREFCNT_dec(c->sname_sv); 1 0 1
6222 /*
6223 # uncoverable branch false class:ignore_covered_err
6224 # uncoverable condition right count:1 class:ignore_covered_err */
6225 1 ▶ if (c->sport_sv) SvREFCNT_dec(c->sport_sv);
Branch: 1 branch
BranchTrueFalseTotal
if (c->sport_sv) SvREFCNT_dec(c->sport_sv); 1 0 1
6226 /*
6227 # uncoverable branch false class:ignore_covered_err
6228 # uncoverable condition right count:1 class:ignore_covered_err
6229 # uncoverable branch true class:ignore_covered_err
6230 # uncoverable condition left count:1 class:ignore_covered_err */
6231 1 ▶ if (c->wbuf) free(c->wbuf);
Branch: 1 branch
BranchTrueFalseTotal
if (c->wbuf) free(c->wbuf); 1 0 1
6232 /*
6233 # uncoverable branch false class:ignore_covered_err
6234 # uncoverable condition right count:1 class:ignore_covered_err */
6235 1 ▶ if (c->rbuf) free(c->rbuf);
Branch: 1 branch
BranchTrueFalseTotal
if (c->rbuf) free(c->rbuf); 1 0 1
6236 1 free(c);
6237 1 loop->conns[i] = NULL;
6238 }
6239 /*
6240 # uncoverable branch true class:ignore_covered_err
6241 # uncoverable condition left count:1 class:ignore_covered_err */
6242 11206656 ▶ if (loop->io_r[i].sv) SvREFCNT_dec(loop->io_r[i].sv);
Branch: 1 branch
BranchTrueFalseTotal
if (loop->io_r[i].sv) SvREFCNT_dec(loop->io_r[i].sv); 0 11206656 11206656
6243 /*
6244 # uncoverable statement class:ignore_covered_err
6245 # uncoverable branch true
6246 # uncoverable condition left count:1 */
6247 11206656 ▶ if (loop->io_w[i].sv) SvREFCNT_dec(loop->io_w[i].sv);
Branch: 1 branch
BranchTrueFalseTotal
if (loop->io_w[i].sv) SvREFCNT_dec(loop->io_w[i].sv); 0 11206656 11206656
6248 }
6249 312 ▶ while (loop->pool) {
Branch: 1 branch
BranchTrueFalseTotal
while (loop->pool) { 141 171 312
6250 141 hm_conn *c = loop->pool;
6251 141 loop->pool = c->pool_next;
6252 141 ▶ if (c->wbuf) free(c->wbuf);
Branch: 1 branch
BranchTrueFalseTotal
if (c->wbuf) free(c->wbuf); 119 22 141
6253 141 ▶ if (c->rbuf) free(c->rbuf);
Branch: 1 branch
BranchTrueFalseTotal
if (c->rbuf) free(c->rbuf); 140 1 141
6254 141 free(c);
6255 }
6256 171 ▶ if (loop->log_fd >= 0 && owned) { hm_log_flush(loop); hm_os_close(loop->log_fd); } /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (loop->log_fd >= 0 && owned) { hm_log_flush(loop); hm_os_close(loop->log_fd); } /* # uncoverable condition right count:2 class:ignore_covered_err */ 8 163 171
if (loop->log_fd >= 0 && owned) { hm_log_flush(loop); hm_os_close(loop->log_fd); } /* # uncoverable condition right count:2 class:ignore_covered_err */ 8 0 8
6257 171 ▶ if (loop->log_buf) free(loop->log_buf);
Branch: 1 branch
BranchTrueFalseTotal
if (loop->log_buf) free(loop->log_buf); 8 163 171
6258 316 ▶ for (i = 0; i < loop->nlisteners; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < loop->nlisteners; i++) { 145 171 316
6259 /* Before the fds: freeing a connection writes a CONNECTION_CLOSE. */
6260 /*
6261 # uncoverable branch false class:ignore_covered_err
6262 # uncoverable condition right count:1 class:ignore_covered_err */
6263 145 ▶ if (owned) hm_quic_srv_free(aTHX_ &loop->listeners[i]);
Branch: 1 branch
BranchTrueFalseTotal
if (owned) hm_quic_srv_free(aTHX_ &loop->listeners[i]); 145 0 145
6264 /*
6265 # uncoverable branch false class:ignore_covered_err
6266 # uncoverable condition right count:1 class:ignore_covered_err */
6267 145 ▶ if (loop->listeners[i].host) free(loop->listeners[i].host);
Branch: 1 branch
BranchTrueFalseTotal
if (loop->listeners[i].host) free(loop->listeners[i].host); 145 0 145
6268 /*
6269 # uncoverable branch true class:ignore_covered_err
6270 # uncoverable condition left count:1 class:ignore_covered_err */
6271 145 ▶ if (loop->listeners[i].altsvc) free(loop->listeners[i].altsvc);
Branch: 1 branch
BranchTrueFalseTotal
if (loop->listeners[i].altsvc) free(loop->listeners[i].altsvc); 0 145 145
6272 145 ▶ if (loop->listeners[i].tls_sni) SvREFCNT_dec(loop->listeners[i].tls_sni);
Branch: 1 branch
BranchTrueFalseTotal
if (loop->listeners[i].tls_sni) SvREFCNT_dec(loop->listeners[i].tls_sni); 4 141 145
6273 /* `owned` for the same reason every other descriptor here is: a loop
6274 * belonging to another process holds fd NUMBERS that are ours, and
6275 * closing them would close whatever this process has since put
6276 * there. An inherited UDP listener is the third case of that, after
6277 * the backend descriptor and the connections. */
6278 /*
6279 # uncoverable branch true class:ignore_covered_err
6280 # uncoverable condition left count:1 class:ignore_covered_err
6281 # uncoverable condition left count:2 class:ignore_covered_err
6282 # uncoverable condition right count:2 class:ignore_covered_err */
6283 145 ▶ if (loop->listeners[i].udp_fd >= 0 && owned)
Branch: 2 branches
BranchTrueFalseTotal
if (loop->listeners[i].udp_fd >= 0 && owned) 0 145 145
if (loop->listeners[i].udp_fd >= 0 && owned) 0 0 0
6284 0 hm_os_close(loop->listeners[i].udp_fd); /* # uncoverable statement class:ignore_covered_err */
6285 }
6286 171 ▶ if (loop->listeners) free(loop->listeners);
Branch: 1 branch
BranchTrueFalseTotal
if (loop->listeners) free(loop->listeners); 134 37 171
6287 /* A weak copy of the wrapper (hm_loop_to_sv): dropping it frees the RV
6288 * alone and runs no DESTROY. */
6289 171 ▶ if (loop->self_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (loop->self_sv) { 125 46 171
6290 125 SV *self = loop->self_sv;
6291 125 loop->self_sv = NULL;
6292 125 loop->perl_owned = 0;
6293 125 SvREFCNT_dec(self);
6294 }
6295 171 ▶ if (loop->log_cb) SvREFCNT_dec(loop->log_cb);
Branch: 1 branch
BranchTrueFalseTotal
if (loop->log_cb) SvREFCNT_dec(loop->log_cb); 4 167 171
6296 171 ▶ if (loop->app) SvREFCNT_dec(loop->app);
Branch: 1 branch
BranchTrueFalseTotal
if (loop->app) SvREFCNT_dec(loop->app); 134 37 171
6297 171 SvREFCNT_dec((SV *)loop->deferred);
6298 171 ▶ if (loop->again) free(loop->again);
Branch: 1 branch
BranchTrueFalseTotal
if (loop->again) free(loop->again); 8 163 171
6299 171 ▶ if (loop->hidx) free(loop->hidx);
Branch: 1 branch
BranchTrueFalseTotal
if (loop->hidx) free(loop->hidx); 1 170 171
6300 171 ▶ if (loop->sweep_tw) free(loop->sweep_tw);
Branch: 1 branch
BranchTrueFalseTotal
if (loop->sweep_tw) free(loop->sweep_tw); 134 37 171
6301 171 ▶ if (loop->hardstop_tw) free(loop->hardstop_tw);
Branch: 1 branch
BranchTrueFalseTotal
if (loop->hardstop_tw) free(loop->hardstop_tw); 1 170 171
6302 /*
6303 # uncoverable branch true class:ignore_covered_err
6304 # uncoverable condition left count:1 class:ignore_covered_err */
6305 171 ▶ if (loop->qheap.a) free(loop->qheap.a);
Branch: 1 branch
BranchTrueFalseTotal
if (loop->qheap.a) free(loop->qheap.a); 0 171 171
6306 /* destroy() releases the backend's memory either way; `foreign` is what
6307 * stops it closing descriptors that are not ours - see the note above */
6308 171 ▶ if (!owned) loop->be->foreign = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!owned) loop->be->foreign = 1; 6 165 171
6309 171 loop->be->destroy(loop->be);
6310 171 free(loop->conns);
6311 171 free(loop->io_r);
6312 171 free(loop->io_w);
6313 171 free(loop);
6314 }
6315  
6316 /* Attach the PSGI server to a loop: listener, shutdown/stats signals via
6317 * the backend's signal watcher, and the timeout sweep timer. */
6318 134 static void hm_attach_server(pTHX_ hm_loop *loop, SV *app) {
6319 int i;
6320 134 loop->app = SvREFCNT_inc(app);
6321 134 loop->attached = 1;
6322 279 ▶ for (i = 0; i < loop->nlisteners; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < loop->nlisteners; i++) { 145 134 279
6323 145 loop->be->add_io(loop->be, loop->listeners[i].fd, HM_EV_READ, 0);
6324 /*
6325 # uncoverable branch true class:ignore_covered_err
6326 # uncoverable condition left count:1 class:ignore_covered_err */
6327 145 ▶ if (loop->listeners[i].udp_fd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (loop->listeners[i].udp_fd >= 0) { 0 145 145
6328 /* After the fork, in the worker that will serve: the SSL_CTX and
6329 * the CID table belong to this process alone. */
6330 0 hm_quic_srv_attach(aTHX_ loop, &loop->listeners[i], i); /* # uncoverable statement class:ignore_covered_err */
6331 0 loop->be->add_io(loop->be, loop->listeners[i].udp_fd, HM_EV_READ, 0); /* # uncoverable statement class:ignore_covered_err */
6332 }
6333 }
6334 #ifndef _WIN32
6335 /* Ignore the default dispositions: these arrive through the backend's
6336 * signal watcher instead. Windows has no signal to ignore - the
6337 * WSAPoll backend's console handler is the only source there. */
6338 134 signal(SIGPIPE, SIG_IGN);
6339 134 signal(SIGTERM, SIG_IGN);
6340 134 signal(SIGINT, SIG_IGN);
6341 134 signal(SIGUSR1, SIG_IGN);
6342 #endif
6343 134 loop->be->add_signal(loop->be, SIGTERM);
6344 134 loop->be->add_signal(loop->be, SIGINT);
6345 134 loop->be->add_signal(loop->be, SIGUSR1);
6346 134 loop->sweep_tw = (hm_tw *)hm_xcalloc(1, sizeof(hm_tw));
6347 134 loop->sweep_tw->kind = HM_TW_SWEEP;
6348 134 loop->sweep_tw->loop = loop;
6349 /*
6350 # uncoverable branch true class:ignore_covered_err
6351 # uncoverable condition left count:1 class:ignore_covered_err */
6352 134 ▶ if (loop->be->add_timer(loop->be, HM_SWEEP_SECS, 0, loop->sweep_tw) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (loop->be->add_timer(loop->be, HM_SWEEP_SECS, 0, loop->sweep_tw) < 0) 0 134 134
6353 0 hm_errlog("Hyperman: cannot arm the timeout sweep: %s\n", strerror(errno)); /* # uncoverable statement class:ignore_covered_err */
6354 134 }
6355  
6356 /* register a timer watcher resolving a Future / running a callback */
6357 2061 static void hm_add_timer_watch(pTHX_ hm_loop *loop, double secs, SV *sv, int kind) {
6358 2061 hm_tw *tw = (hm_tw *)hm_xcalloc(1, sizeof(hm_tw));
6359 2061 tw->kind = kind;
6360 2061 tw->sv = SvREFCNT_inc(sv);
6361 2061 tw->loop = loop;
6362 2061 tw->due = hm_tw_due(secs);
6363 /* A backend can refuse: on epoll every timer is a descriptor, so under
6364 * EMFILE there is none to be had. Silently dropping it left a ->get on
6365 * the timer's future pumping the loop for ever. */
6366 /*
6367 # uncoverable branch true class:ignore_covered_err
6368 # uncoverable condition left count:1 class:ignore_covered_err
6369 # uncoverable condition right count:1 class:ignore_covered_err
6370 # uncoverable condition left count:2 class:ignore_covered_err */
6371 2061 ▶ if (!HM_LOOP_INHERITED(loop) && loop->be->add_timer(loop->be, secs, 1, tw) < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!HM_LOOP_INHERITED(loop) && loop->be->add_timer(loop->be, secs, 1, tw) < 0) { 2061 0 2061
if (!HM_LOOP_INHERITED(loop) && loop->be->add_timer(loop->be, secs, 1, tw) < 0) { 0 2061 2061
6372 0 int e = errno; /* # uncoverable statement */
6373 0 SvREFCNT_dec(sv); /* # uncoverable statement */
6374 0 free(tw); /* # uncoverable statement */
6375 0 croak("Hyperman: cannot arm a timer: %s", strerror(e)); /* # uncoverable statement */
6376 }
6377 2061 }
6378  
6379 /* Let go of whatever a watcher slot held. A one-shot future that was still
6380 * pending is CANCELLED, not dropped: a `->get` waiting on it would otherwise
6381 * pump the loop for a readiness nothing is watching for any more. */
6382 143 static void hm_iow_release_sv(pTHX_ SV *sv, int is_cb) {
6383 143 ▶ if (!sv) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!sv) return; 136 7 143
6384 /*
6385 # uncoverable condition right count:2 class:ignore_covered_err
6386 # uncoverable condition right count:3 class:ignore_covered_err */
6387 7 ▶ if (!is_cb && hmf_is_future(aTHX_ sv) && hmf_state(aTHX_ sv) == HMF_PENDING)
Branch: 3 branches
BranchTrueFalseTotal
if (!is_cb && hmf_is_future(aTHX_ sv) && hmf_state(aTHX_ sv) == HMF_PENDING) 3 4 7
if (!is_cb && hmf_is_future(aTHX_ sv) && hmf_state(aTHX_ sv) == HMF_PENDING) 3 0 3
if (!is_cb && hmf_is_future(aTHX_ sv) && hmf_state(aTHX_ sv) == HMF_PENDING) 3 0 3
6388 3 hmf_cancel(aTHX_ sv);
6389 7 SvREFCNT_dec(sv);
6390 }
6391  
6392 /* The slot is EMPTIED before the old occupant is released, and the new
6393 * occupant (where there is one) is seated before that too: releasing a
6394 * pending future fires its callbacks, and one of those may watch this same
6395 * fd again. With the release first, that inner registration landed in the
6396 * slot and was then overwritten by the outer one - a future that never
6397 * fired - or, on the remove path, was left in a slot whose kind had been
6398 * reset, so the next event settled a callback CV as if it were a future. */
6399 /*
6400 # uncoverable statement
6401 # uncoverable subroutine */
6402 0 static void hm_iow_release(pTHX_ hm_iow *slot) {
6403 0 SV *sv = slot->sv; /* # uncoverable statement */
6404 0 int is_cb = slot->is_cb; /* # uncoverable statement */
6405 /*
6406 # uncoverable statement
6407 # uncoverable branch true
6408 # uncoverable branch false
6409 # uncoverable condition left count:1
6410 # uncoverable condition right count:1 */
6411 0 ▶ if (!sv) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!sv) return; 0 0 0
6412 0 slot->sv = NULL; /* # uncoverable statement */
6413 0 hm_iow_release_sv(aTHX_ sv, is_cb); /* # uncoverable statement */
6414 }
6415  
6416 15 static void hm_add_io_watch(pTHX_ hm_loop *loop, int fd, const char *mode,
6417 SV *sv, int is_cb) {
6418 hm_iow *slot;
6419 SV *old;
6420 int old_cb;
6421 int mask;
6422 /*
6423 # uncoverable branch true class:ignore_covered_err
6424 # uncoverable condition left count:1 class:ignore_covered_err
6425 # uncoverable condition left count:2 class:ignore_covered_err */
6426 15 ▶ if (fd < 0 || fd >= HM_MAXFD) croak("Hyperman: bad fd %d", fd);
Branch: 2 branches
BranchTrueFalseTotal
if (fd < 0 || fd >= HM_MAXFD) croak("Hyperman: bad fd %d", fd); 15 0 15
if (fd < 0 || fd >= HM_MAXFD) croak("Hyperman: bad fd %d", fd); 0 15 15
6427 15 ▶ if (mode[0] == 'w') { slot = &loop->io_w[fd]; mask = HM_EV_WRITE; }
Branch: 1 branch
BranchTrueFalseTotal
if (mode[0] == 'w') { slot = &loop->io_w[fd]; mask = HM_EV_WRITE; } 1 14 15
6428 14 else { slot = &loop->io_r[fd]; mask = HM_EV_READ; }
6429 15 old = slot->sv;
6430 15 old_cb = slot->is_cb;
6431 15 slot->sv = SvREFCNT_inc(sv);
6432 15 slot->is_cb = (unsigned char)is_cb;
6433 15 slot->c_cb = NULL;
6434 15 slot->c_ud = NULL;
6435 /*
6436 # uncoverable branch false class:ignore_covered_err
6437 # uncoverable condition right count:1 class:ignore_covered_err */
6438 15 ▶ if (!HM_LOOP_INHERITED(loop))
Branch: 1 branch
BranchTrueFalseTotal
if (!HM_LOOP_INHERITED(loop)) 15 0 15
6439 15 loop->be->add_io(loop->be, fd, mask, is_cb ? 0 : 1);
6440 15 hm_iow_release_sv(aTHX_ old, old_cb);
6441 15 }
6442  
6443 /* ABI: persistent C watcher, replacing whatever held the slot. */
6444 121 static void hm_add_io_watch_c(pTHX_ hm_loop *loop, int fd, int mask,
6445 hm_abi_io_cb cb, void *ud) {
6446 hm_iow *slot;
6447 SV *old;
6448 int old_cb;
6449 /*
6450 # uncoverable branch true class:ignore_covered_err
6451 # uncoverable condition left count:1 class:ignore_covered_err
6452 # uncoverable condition left count:2 class:ignore_covered_err */
6453 121 ▶ if (fd < 0 || fd >= HM_MAXFD) croak("Hyperman: bad fd %d", fd);
Branch: 2 branches
BranchTrueFalseTotal
if (fd < 0 || fd >= HM_MAXFD) croak("Hyperman: bad fd %d", fd); 121 0 121
if (fd < 0 || fd >= HM_MAXFD) croak("Hyperman: bad fd %d", fd); 0 121 121
6454 121 ▶ slot = (mask & HM_EV_WRITE) ? &loop->io_w[fd] : &loop->io_r[fd]; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
slot = (mask & HM_EV_WRITE) ? &loop->io_w[fd] : &loop->io_r[fd]; /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 121 121
6455 121 old = slot->sv;
6456 121 old_cb = slot->is_cb;
6457 121 slot->sv = NULL;
6458 121 slot->is_cb = 0;
6459 121 slot->c_cb = cb;
6460 121 slot->c_ud = ud;
6461 /*
6462 # uncoverable branch false class:ignore_covered_err
6463 # uncoverable condition right count:1 class:ignore_covered_err */
6464 121 ▶ if (!HM_LOOP_INHERITED(loop))
Branch: 1 branch
BranchTrueFalseTotal
if (!HM_LOOP_INHERITED(loop)) 121 0 121
6465 121 loop->be->add_io(loop->be, fd,
6466 121 ▶ (mask & HM_EV_WRITE) ? HM_EV_WRITE : HM_EV_READ, 0); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
(mask & HM_EV_WRITE) ? HM_EV_WRITE : HM_EV_READ, 0); /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 121 121
6467 121 hm_iow_release_sv(aTHX_ old, old_cb);
6468 121 }
6469  
6470 /* Drop one direction's watcher (any kind); idempotent. */
6471 7 static void hm_del_io_watch(pTHX_ hm_loop *loop, int fd, int mask) {
6472 hm_iow *slot;
6473 SV *old;
6474 int old_cb;
6475 /*
6476 # uncoverable branch true class:ignore_covered_err
6477 # uncoverable condition left count:1 class:ignore_covered_err
6478 # uncoverable condition left count:2 class:ignore_covered_err */
6479 7 ▶ if (fd < 0 || fd >= HM_MAXFD) return;
Branch: 2 branches
BranchTrueFalseTotal
if (fd < 0 || fd >= HM_MAXFD) return; 7 0 7
if (fd < 0 || fd >= HM_MAXFD) return; 0 7 7
6480 7 ▶ slot = (mask & HM_EV_WRITE) ? &loop->io_w[fd] : &loop->io_r[fd]; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
slot = (mask & HM_EV_WRITE) ? &loop->io_w[fd] : &loop->io_r[fd]; /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 7 7
6481 /*
6482 # uncoverable branch true class:ignore_covered_err
6483 # uncoverable condition left count:1 class:ignore_covered_err
6484 # uncoverable condition left count:2 class:ignore_covered_err */
6485 7 ▶ if (!slot->sv && !slot->c_cb) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!slot->sv && !slot->c_cb) return; 1 6 7
if (!slot->sv && !slot->c_cb) return; 0 1 1
6486 7 ▶ if (!HM_LOOP_INHERITED(loop))
Branch: 1 branch
BranchTrueFalseTotal
if (!HM_LOOP_INHERITED(loop)) 5 2 7
6487 5 loop->be->remove_io(loop->be, fd,
6488 5 ▶ (mask & HM_EV_WRITE) ? HM_EV_WRITE : HM_EV_READ); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
(mask & HM_EV_WRITE) ? HM_EV_WRITE : HM_EV_READ); /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 5 5
6489 7 old = slot->sv;
6490 7 old_cb = slot->is_cb;
6491 7 slot->sv = NULL;
6492 7 slot->is_cb = 0;
6493 7 slot->c_cb = NULL;
6494 7 slot->c_ud = NULL;
6495 7 hm_iow_release_sv(aTHX_ old, old_cb);
6496 }
6497  
6498 /* ABI: one-shot C timer; the returned watcher doubles as the cancel handle
6499 * (freed on fire or on cancel, never both - single-threaded). */
6500 30640 static hm_tw *hm_add_timer_watch_c(hm_loop *loop, double secs,
6501 hm_abi_timer_cb cb, void *ud) {
6502 30640 hm_tw *tw = (hm_tw *)hm_xcalloc(1, sizeof(hm_tw));
6503 30640 tw->kind = HM_TW_C;
6504 30640 tw->loop = loop;
6505 30640 tw->c_cb = cb;
6506 30640 tw->c_ud = ud;
6507 30640 tw->due = hm_tw_due(secs);
6508 30640 ▶ if (loop->be->add_timer(loop->be, secs, 1, tw) < 0) { /* # uncoverable branch true class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
if (loop->be->add_timer(loop->be, secs, 1, tw) < 0) { /* # uncoverable branch true class:ignore_covered_err */ 30222 418 30640
6509 /* NULL, and a line in the log: the C callers (the scoreboard tick,
6510 * the bus poll, the accept re-arm) each have a fallback or a reason
6511 * to be loud, and an ABI consumer is told the handle is NULL. */
6512 30222 hm_errlog("Hyperman: cannot arm a %.3fs timer: %s\n", secs, strerror(errno)); /* # uncoverable statement class:ignore_covered_err */
6513 30222 free(tw); /* # uncoverable statement class:ignore_covered_err */
6514 30222 return NULL; /* # uncoverable statement class:ignore_covered_err */
6515 }
6516 418 return tw;
6517 }
6518  
6519 11 static void hm_del_timer_watch(pTHX_ hm_loop *loop, hm_tw *tw) {
6520 /*
6521 # uncoverable branch true class:ignore_covered_err
6522 # uncoverable condition left count:1 class:ignore_covered_err
6523 # uncoverable condition left count:2 class:ignore_covered_err */
6524 11 ▶ if (!tw || tw->dead) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!tw || tw->dead) return; 11 0 11
if (!tw || tw->dead) return; 0 11 11
6525 /*
6526 # uncoverable branch false class:ignore_covered_err
6527 # uncoverable condition right count:1 class:ignore_covered_err */
6528 11 ▶ if (!HM_LOOP_INHERITED(loop))
Branch: 1 branch
BranchTrueFalseTotal
if (!HM_LOOP_INHERITED(loop)) 11 0 11
6529 11 loop->be->del_timer(loop->be, tw);
6530 /*
6531 # uncoverable branch true class:ignore_covered_err
6532 # uncoverable condition left count:1 class:ignore_covered_err */
6533 11 ▶ if (tw->sv) { SvREFCNT_dec(tw->sv); tw->sv = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (tw->sv) { SvREFCNT_dec(tw->sv); tw->sv = NULL; } 0 11 11
6534 /* Not freed here: an event naming it may already be in the batch being
6535 * dispatched (see hm_tw.dead). hm_dispatch skips it; hm_tw_gy_sweep
6536 * frees it once the outermost loop's batch is done. */
6537 11 tw->dead = 1;
6538 11 tw->gy_next = loop->tw_gy;
6539 11 loop->tw_gy = tw;
6540 }
6541  
6542 /* Free the cancelled timers. Only between batches of the OUTERMOST loop
6543 * run: a nested run (a handler's ->get) sits inside one of the outer
6544 * batch's events, and the outer batch may still name any of them. */
6545 182 static void hm_tw_gy_sweep(hm_loop *loop) {
6546 182 hm_tw *tw = loop->tw_gy, *n;
6547 182 loop->tw_gy = NULL;
6548 193 ▶ while (tw) { n = tw->gy_next; free(tw); tw = n; }
Branch: 1 branch
BranchTrueFalseTotal
while (tw) { n = tw->gy_next; free(tw); tw = n; } 11 182 193
6549 182 }
6550  
6551 9 static hm_loop *hm_need_loop(void) {
6552 9 ▶ if (!hm_cur_loop)
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_cur_loop) 1 8 9
6553 1 croak("Hyperman: no event loop is running in this process");
6554 8 return hm_cur_loop;
6555 }
6556  
6557 /* ---- listener + process model -------------------------------------------- */
6558  
6559 /* Create, bind, and listen a non-blocking TCP socket. Returns fd or -1. */
6560 /* Resolve a listen address ONCE, at startup: an IPv4 or IPv6 literal (with
6561 * or without brackets), a hostname, or nothing for every IPv4 interface.
6562 * getaddrinfo's first answer is taken. Failure is an error string for the
6563 * croak and never a silent fallback: a host that did not parse used to bind
6564 * INADDR_ANY, which exposed the service on every interface for a typo. */
6565 108 static int hm_resolve(const char *host, int port, int socktype,
6566 struct sockaddr_storage *out, socklen_t *outlen,
6567 char *err, size_t errlen) {
6568 108 struct addrinfo hints, *res = NULL;
6569 char hbuf[256], pbuf[16];
6570 108 const char *h = host;
6571 int rc;
6572 /*
6573 # uncoverable branch true class:ignore_covered_err
6574 # uncoverable condition left count:1 class:ignore_covered_err
6575 # uncoverable condition left count:2 class:ignore_covered_err */
6576 108 ▶ if (!h || !*h) h = "0.0.0.0";
Branch: 2 branches
BranchTrueFalseTotal
if (!h || !*h) h = "0.0.0.0"; 108 0 108
if (!h || !*h) h = "0.0.0.0"; 0 108 108
6577 108 ▶ if (h[0] == '[') {
Branch: 1 branch
BranchTrueFalseTotal
if (h[0] == '[') { 2 106 108
6578 2 size_t l = strlen(h);
6579 /*
6580 # uncoverable condition left count:1 class:ignore_covered_err
6581 # uncoverable condition left count:3 class:ignore_covered_err */
6582 2 ▶ if (l < 3 || h[l - 1] != ']' || l - 2 >= sizeof(hbuf)) {
Branch: 3 branches
BranchTrueFalseTotal
if (l < 3 || h[l - 1] != ']' || l - 2 >= sizeof(hbuf)) { 2 0 2
if (l < 3 || h[l - 1] != ']' || l - 2 >= sizeof(hbuf)) { 1 1 2
if (l < 3 || h[l - 1] != ']' || l - 2 >= sizeof(hbuf)) { 0 1 1
6583 1 snprintf(err, errlen, "malformed bracketed address");
6584 1 return -1;
6585 }
6586 1 memcpy(hbuf, h + 1, l - 2);
6587 1 hbuf[l - 2] = '\0';
6588 1 h = hbuf;
6589 }
6590 107 memset(&hints, 0, sizeof(hints));
6591 107 hints.ai_family = AF_UNSPEC;
6592 107 hints.ai_socktype = socktype;
6593 107 hints.ai_flags = AI_NUMERICSERV | AI_PASSIVE;
6594 107 snprintf(pbuf, sizeof(pbuf), "%d", port);
6595 107 rc = getaddrinfo(h, pbuf, &hints, &res);
6596 /*
6597 # uncoverable branch true class:ignore_covered_err
6598 # uncoverable condition left count:1 class:ignore_covered_err
6599 # uncoverable condition left count:2 class:ignore_covered_err */
6600 107 ▶ if (rc != 0 || !res) {
Branch: 2 branches
BranchTrueFalseTotal
if (rc != 0 || !res) { 107 0 107
if (rc != 0 || !res) { 0 107 107
6601 /*
6602 # uncoverable statement class:ignore_covered_err
6603 # uncoverable condition left count:1 class:ignore_covered_err
6604 # uncoverable condition right count:1 class:ignore_covered_err */
6605 0 ▶ snprintf(err, errlen, "%s", rc ? gai_strerror(rc) : "no address");
Branch: 1 branch
BranchTrueFalseTotal
snprintf(err, errlen, "%s", rc ? gai_strerror(rc) : "no address"); 0 0 0
6606 /*
6607 # uncoverable statement class:ignore_covered_err
6608 # uncoverable branch true class:ignore_covered_err
6609 # uncoverable branch false class:ignore_covered_err
6610 # uncoverable condition left count:1 class:ignore_covered_err
6611 # uncoverable condition right count:1 class:ignore_covered_err */
6612 0 ▶ if (res) freeaddrinfo(res);
Branch: 1 branch
BranchTrueFalseTotal
if (res) freeaddrinfo(res); 0 0 0
6613 0 return -1; /* # uncoverable statement class:ignore_covered_err */
6614 }
6615 /*
6616 # uncoverable branch true
6617 # uncoverable condition left count:1 class:ignore_covered_err */
6618 107 ▶ if (res->ai_addrlen > sizeof(*out)) { freeaddrinfo(res); snprintf(err, errlen, "address too long"); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (res->ai_addrlen > sizeof(*out)) { freeaddrinfo(res); snprintf(err, errlen, "address too long"); return -1; } 0 107 107
6619 107 memcpy(out, res->ai_addr, res->ai_addrlen);
6620 107 *outlen = (socklen_t)res->ai_addrlen;
6621 107 freeaddrinfo(res);
6622 107 return 0;
6623 }
6624  
6625 /* A unix-domain address. sun_path is 104 bytes on macOS and 108 on Linux,
6626 * and a path that does not fit is refused rather than truncated. */
6627 7 static int hm_resolve_unix(const char *path, struct sockaddr_storage *out,
6628 socklen_t *outlen, char *err, size_t errlen) {
6629 #ifdef _WIN32
6630 (void)path; (void)out; (void)outlen;
6631 snprintf(err, errlen, "unix-domain listeners are not supported on Windows");
6632 return -1;
6633 #else
6634 7 struct sockaddr_un *un = (struct sockaddr_un *)out;
6635 7 size_t l = strlen(path);
6636 7 ▶ if (l == 0 || l >= sizeof(un->sun_path)) { /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (l == 0 || l >= sizeof(un->sun_path)) { /* # uncoverable condition left count:1 class:ignore_covered_err */ 7 0 7
if (l == 0 || l >= sizeof(un->sun_path)) { /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 6 7
6637 1 snprintf(err, errlen, "path is empty or longer than %d bytes",
6638 (int)sizeof(un->sun_path) - 1);
6639 1 return -1;
6640 }
6641 6 memset(out, 0, sizeof(*out));
6642 6 un->sun_family = AF_UNIX;
6643 6 memcpy(un->sun_path, path, l + 1);
6644 6 *outlen = (socklen_t)(offsetof(struct sockaddr_un, sun_path) + l + 1);
6645 6 return 0;
6646 #endif
6647 }
6648  
6649 /* An IPv6 listener is v6 ONLY, so `::` and `0.0.0.0` can both be listed on
6650 * one port; without it the second bind fails on Linux and the v4-mapped
6651 * addresses the dual-stack socket would report differ by platform. */
6652 87 static void hm_v6only(int fd, const struct sockaddr *sa) {
6653 #ifdef IPV6_V6ONLY
6654 /*
6655 # uncoverable branch true class:ignore_covered_err
6656 # uncoverable condition left count:1 class:ignore_covered_err */
6657 87 ▶ if (sa->sa_family == AF_INET6) {
Branch: 1 branch
BranchTrueFalseTotal
if (sa->sa_family == AF_INET6) { 0 87 87
6658 0 int one = 1; /* # uncoverable statement class:ignore_covered_err */
6659 0 hm_os_setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one)); /* # uncoverable statement class:ignore_covered_err */
6660 }
6661 #else
6662 (void)fd; (void)sa;
6663 #endif
6664 87 }
6665  
6666 88 static int hm_make_listener(const struct sockaddr *sa, socklen_t salen, int reuseport) {
6667 88 int one = 1;
6668 int fd;
6669 #ifndef _WIN32
6670 /* A path listener: a socket file left by an earlier run is replaced; a
6671 * regular file at the path is somebody else's and is refused. */
6672 88 ▶ if (sa->sa_family == AF_UNIX) {
Branch: 1 branch
BranchTrueFalseTotal
if (sa->sa_family == AF_UNIX) { 3 85 88
6673 3 const char *p = ((const struct sockaddr_un *)sa)->sun_path;
6674 struct stat st;
6675 3 ▶ if (stat(p, &st) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (stat(p, &st) == 0) { 2 1 3
6676 int pfd;
6677 2 ▶ if (!S_ISSOCK(st.st_mode)) { errno = EEXIST; return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (!S_ISSOCK(st.st_mode)) { errno = EEXIST; return -1; } 1 1 2
6678 /* Somebody answering at the path is a live server, and this
6679 * would have unlinked its socket from under it (a second
6680 * instance started by mistake took the path silently). Refused
6681 * as a TCP bind of a busy port is. */
6682 1 pfd = hm_os_socket(AF_UNIX, SOCK_STREAM, 0);
6683 /*
6684 # uncoverable branch false class:ignore_covered_err
6685 # uncoverable condition right count:1 class:ignore_covered_err */
6686 1 ▶ if (pfd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (pfd >= 0) { 1 0 1
6687 1 int alive = connect(pfd, sa, salen) == 0;
6688 1 hm_os_close(pfd);
6689 /*
6690 # uncoverable branch true
6691 # uncoverable condition left count:1 class:ignore_covered_err */
6692 1 ▶ if (alive) { errno = EADDRINUSE; return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (alive) { errno = EADDRINUSE; return -1; } 0 1 1
6693 }
6694 1 (void)unlink(p);
6695 }
6696 }
6697 #endif
6698 87 fd = hm_os_socket(sa->sa_family, SOCK_STREAM, 0);
6699 /*
6700 # uncoverable branch true
6701 # uncoverable condition left count:1 class:ignore_covered_err */
6702 87 ▶ if (fd < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) return -1; 0 87 87
6703 #ifndef _WIN32
6704 /* Not into anything the application execs. A re-exec successor is the
6705 * one program meant to inherit it, and hm_reexec clears this for that
6706 * child alone. */
6707 87 (void)fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
6708 #endif
6709 87 hm_v6only(fd, sa);
6710 #ifdef _WIN32
6711 /* SO_REUSEADDR on Windows lets a second bind STEAL a live listener,
6712 * which is the opposite of the Unix meaning we want here (rebind a
6713 * TIME_WAIT port). SO_EXCLUSIVEADDRUSE is the flag that buys what
6714 * SO_REUSEADDR buys everywhere else. */
6715 hm_os_setsockopt(fd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, &one, sizeof(one));
6716 #else
6717 87 hm_os_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
6718 #endif
6719 #ifdef SO_REUSEPORT
6720 87 ▶ if (reuseport)
Branch: 1 branch
BranchTrueFalseTotal
if (reuseport) 3 84 87
6721 3 hm_os_setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
6722 #else
6723 (void)reuseport;
6724 #endif
6725 87 ▶ if (hm_os_bind(fd, sa, salen) < 0) { /* # uncoverable branch true class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
if (hm_os_bind(fd, sa, salen) < 0) { /* # uncoverable branch true class:ignore_covered_err */ 1 86 87
6726 1 hm_os_close(fd); return -1; /* # uncoverable statement class:ignore_covered_err */
6727 }
6728 /*
6729 # uncoverable branch true
6730 # uncoverable condition left count:1 class:ignore_covered_err */
6731 86 ▶ if (hm_os_listen(fd, SOMAXCONN) < 0) { hm_os_close(fd); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (hm_os_listen(fd, SOMAXCONN) < 0) { hm_os_close(fd); return -1; } 0 86 86
6732 86 hm_set_nonblock(fd);
6733 86 return fd;
6734 }
6735  
6736 /* The QUIC listener: the same address as its TCP twin, over UDP. No
6737 * listen(2) - there is no accept queue, because there is no accept: one
6738 * socket carries every connection and the Destination Connection ID in each
6739 * packet says which (hm_quic.h). SO_REUSEPORT is honoured the same way, and
6740 * carries the same caveat the TCP side does not have: the kernel's UDP
6741 * reuseport hash is over the 4-tuple, so a client that migrates lands on a
6742 * different worker than the one holding its connection. That is why
6743 * migration is deferred rather than half-supported. */
6744 /*
6745 # uncoverable statement class:ignore_covered_err
6746 # uncoverable subroutine class:ignore_covered_err */
6747 0 static int hm_make_udp_listener(const struct sockaddr *sa, socklen_t salen, int reuseport) {
6748 0 int one = 1; /* # uncoverable statement class:ignore_covered_err */
6749 0 int fd = hm_os_socket(sa->sa_family, SOCK_DGRAM, 0); /* # uncoverable statement class:ignore_covered_err */
6750 /*
6751 # uncoverable branch true class:ignore_covered_err
6752 # uncoverable statement class:ignore_covered_err
6753 # uncoverable branch false class:ignore_covered_err
6754 # uncoverable condition left count:1 class:ignore_covered_err
6755 # uncoverable condition right count:1 class:ignore_covered_err */
6756 0 ▶ if (fd < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) return -1; 0 0 0
6757 #ifndef _WIN32
6758 0 (void)fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC); /* # uncoverable statement class:ignore_covered_err */
6759 #endif
6760 0 hm_v6only(fd, sa); /* # uncoverable statement class:ignore_covered_err */
6761 #ifdef _WIN32
6762 hm_os_setsockopt(fd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, &one, sizeof(one));
6763 #else
6764 0 hm_os_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); /* # uncoverable statement class:ignore_covered_err */
6765 #endif
6766 #ifdef SO_REUSEPORT
6767 /*
6768 # uncoverable statement class:ignore_covered_err
6769 # uncoverable branch true class:ignore_covered_err
6770 # uncoverable branch false class:ignore_covered_err
6771 # uncoverable condition left count:1 class:ignore_covered_err
6772 # uncoverable condition right count:1 class:ignore_covered_err */
6773 0 ▶ if (reuseport)
Branch: 1 branch
BranchTrueFalseTotal
if (reuseport) 0 0 0
6774 0 hm_os_setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)); /* # uncoverable statement */
6775 #else
6776 (void)reuseport;
6777 #endif
6778 /*
6779 # uncoverable branch true class:ignore_covered_err
6780 # uncoverable statement class:ignore_covered_err
6781 # uncoverable branch false class:ignore_covered_err
6782 # uncoverable condition left count:1 class:ignore_covered_err
6783 # uncoverable condition right count:1 class:ignore_covered_err */
6784 0 ▶ if (hm_os_bind(fd, sa, salen) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (hm_os_bind(fd, sa, salen) < 0) { 0 0 0
6785 0 hm_os_close(fd); return -1; /* # uncoverable statement */
6786 }
6787 0 hm_set_nonblock(fd); /* # uncoverable statement class:ignore_covered_err */
6788 0 return fd; /* # uncoverable statement class:ignore_covered_err */
6789 }
6790  
6791 /* ---- the supervisor half: POSIX only --------------------------------------
6792 * Everything from here to the end of hm_run_server's pool branch needs
6793 * fork(2), waitpid(2) and POSIX signal delivery. Windows has none of the
6794 * three, and perl's fork emulation is ithreads - N interpreters inside ONE
6795 * process, sharing the file-scope state this server keeps per process - so
6796 * a pool there would corrupt, not scale. Windows runs the single-worker
6797 * in-process path instead (hm_run_server, above the pool branch), and an
6798 * explicit workers > 1 is refused at boot. */
6799 #ifndef _WIN32
6800  
6801 /* Supervisor signal flags: TERM/INT = shut the pool down, HUP/USR2 =
6802 * recycle workers with zero downtime, USR1 = stats dump. */
6803 static volatile sig_atomic_t hm_sup_term = 0;
6804 static volatile sig_atomic_t hm_sup_hup = 0;
6805 static volatile sig_atomic_t hm_sup_usr1 = 0;
6806 static volatile sig_atomic_t hm_sup_tick = 0; /* ALRM: the liveness look */
6807 static volatile sig_atomic_t hm_sup_usr2 = 0; /* re-exec a successor */
6808 /* The successor supervisor a USR2 started, until it has taken over (it
6809 * TERMs us) or died trying (the reaper clears it). */
6810 static pid_t hm_resup_pid = 0;
6811 /* What this process was started as, for the exec: $^X and perl's argv from
6812 * the first argument on. Copied at BOOT, because `$0 = ...` on Linux writes
6813 * over the memory PL_origargv points into. */
6814 static char **hm_boot_argv = NULL;
6815 /* Set in a successor: the supervisor that handed its sockets over and is
6816 * told to drain once this pool is up. */
6817 static pid_t hm_resup_old = 0;
6818 /* An alarm the caller had pending when run() was called: the deadline it asked
6819 * for, and whether it has come due. The supervisor's tick owns SIGALRM, so the
6820 * caller's is carried rather than cancelled. */
6821 static time_t hm_sup_alarm_at = 0;
6822 static int hm_sup_alarmed = 0;
6823 86 static void hm_sup_sig(int sig) {
6824 86 ▶ if (sig == SIGHUP) hm_sup_hup = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (sig == SIGHUP) hm_sup_hup = 1; 2 84 86
6825 84 ▶ else if (sig == SIGUSR2) hm_sup_usr2 = 1;
Branch: 1 branch
BranchTrueFalseTotal
else if (sig == SIGUSR2) hm_sup_usr2 = 1; 5 79 84
6826 79 ▶ else if (sig == SIGUSR1) hm_sup_usr1 = 1;
Branch: 1 branch
BranchTrueFalseTotal
else if (sig == SIGUSR1) hm_sup_usr1 = 1; 5 74 79
6827 74 ▶ else if (sig == SIGALRM) hm_sup_tick = 1;
Branch: 1 branch
BranchTrueFalseTotal
else if (sig == SIGALRM) hm_sup_tick = 1; 3 71 74
6828 71 ▶ else if (sig == SIGCHLD) ; /* no-op: just wakes sigsuspend() */
Branch: 1 branch
BranchTrueFalseTotal
else if (sig == SIGCHLD) ; /* no-op: just wakes sigsuspend() */ 27 44 71
6829 27 else hm_sup_term = 1;
6830 86 }
6831  
6832 /* Wedged is not dead. A worker that is alive but has stopped turning its
6833 * loop keeps its pid and its row; what stops is the row's `updated` stamp,
6834 * which the one-second tick moves. Three looks three seconds apart with the
6835 * stamp unmoved and the pid answering kill(0) is the signal, logged once per
6836 * episode. The respawn policy is untouched: this says, it does not act. */
6837 #define HM_SUP_TICK_SECS 3
6838 #define HM_SUP_WEDGED_TICKS 3
6839 static uint64_t hm_child_seen[1024];
6840 static int hm_child_stale[1024];
6841  
6842 2 static void hm_sup_watch_board(void) {
6843 int i;
6844 /*
6845 # uncoverable branch true
6846 # uncoverable condition left count:1 class:ignore_covered_err */
6847 2 ▶ if (!hm_sa_board_live()) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_sa_board_live()) return; 0 2 2
6848 6 ▶ for (i = 0; i < hm_nchildren; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < hm_nchildren; i++) { 4 2 6
6849 sa_sb_reading rd;
6850 4 pid_t pid = hm_children[i];
6851 /*
6852 # uncoverable branch true
6853 # uncoverable condition left count:1 class:ignore_covered_err */
6854 4 ▶ if (hm_sa_board_row_of((uint64_t)pid, &rd) < 0) { hm_child_stale[i] = 0; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (hm_sa_board_row_of((uint64_t)pid, &rd) < 0) { hm_child_stale[i] = 0; continue; } 0 4 4
6855 /*
6856 # uncoverable branch true
6857 # uncoverable condition left count:1 class:ignore_covered_err */
6858 4 ▶ if (kill(pid, 0) != 0) continue; /* gone: the reaper's business */
Branch: 1 branch
BranchTrueFalseTotal
if (kill(pid, 0) != 0) continue; /* gone: the reaper's business */ 0 4 4
6859 /*
6860 # uncoverable branch true
6861 # uncoverable condition left count:1 class:ignore_covered_err */
6862 4 ▶ if (rd.updated == hm_child_seen[i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (rd.updated == hm_child_seen[i]) { 0 4 4
6863 /*
6864 # uncoverable statement
6865 # uncoverable branch true
6866 # uncoverable branch false
6867 # uncoverable condition left count:1
6868 # uncoverable condition right count:1 */
6869 0 ▶ if (++hm_child_stale[i] == HM_SUP_WEDGED_TICKS)
Branch: 1 branch
BranchTrueFalseTotal
if (++hm_child_stale[i] == HM_SUP_WEDGED_TICKS) 0 0 0
6870 0 hm_errlog("Hyperman: worker %d is alive but has not " /* # uncoverable statement */
6871 "updated its scoreboard row for %d seconds: wedged?\n",
6872 (int)pid, HM_SUP_TICK_SECS * HM_SUP_WEDGED_TICKS); /* # uncoverable statement */
6873 }
6874 else {
6875 4 hm_child_seen[i] = rd.updated;
6876 4 hm_child_stale[i] = 0;
6877 }
6878 }
6879 }
6880  
6881 /* What to exec on USR2: perl's own argv from the first argument on. $^X
6882 * supplies the interpreter at exec time. Copied at BOOT, because `$0 = ...`
6883 * on Linux writes over the memory PL_origargv points into. */
6884 149 static void hm_boot_argv_save(pTHX) {
6885 149 int i, n = PL_origargc;
6886 /*
6887 # uncoverable branch true class:ignore_covered_err
6888 # uncoverable condition left count:1 class:ignore_covered_err
6889 # uncoverable condition left count:2 class:ignore_covered_err */
6890 149 ▶ if (n <= 0 || !PL_origargv) return;
Branch: 2 branches
BranchTrueFalseTotal
if (n <= 0 || !PL_origargv) return; 149 0 149
if (n <= 0 || !PL_origargv) return; 0 149 149
6891 149 hm_boot_argv = (char **)hm_xmalloc(sizeof(char *) * (size_t)(n + 1));
6892 650 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 501 149 650
6893 501 ▶ const char *a = PL_origargv[i] ? PL_origargv[i] : ""; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
const char *a = PL_origargv[i] ? PL_origargv[i] : ""; /* # uncoverable condition right count:1 class:ignore_covered_err */ 501 0 501
6894 501 hm_boot_argv[i] = (char *)hm_xmalloc(strlen(a) + 1);
6895 501 memcpy(hm_boot_argv[i], a, strlen(a) + 1);
6896 }
6897 149 hm_boot_argv[n] = NULL;
6898 /* argv[0] becomes the interpreter path at exec; keep the name perl was
6899 * invoked under as its argv[0] */
6900 }
6901  
6902 #else
6903 /* No re-exec on Windows: there is no USR2 to ask for one. */
6904 #define hm_boot_argv_save(x) ((void)0)
6905 #endif /* !_WIN32 - the rest of this section is shared; hm_spawn and the
6906 * pool branch of hm_run_server re-open the guard below */
6907  
6908 /* One listener as configured by run(): its bind address, optional TLS, h2,
6909 * and https-redirect target. The bound fd and built SSL_CTX are filled in by
6910 * hm_run_server before the workers fork. */
6911 typedef struct {
6912 const char *host;
6913 int port;
6914 const char *path; /* a unix-domain listener instead of host:port */
6915 int mode_set, mode; /* its file mode, when given */
6916 int inherit; /* adopt this one from LISTEN_FDS */
6917 int proxy_protocol; /* every connection opens with a PROXY header */
6918 const char *tls_cert, *tls_key, *tls_ca;
6919 int tls_verify;
6920 SV *tls_sni;
6921 int http2;
6922 int http3;
6923 int redirect_https;
6924 int fd; /* bound fd (parent), or -1 under reuseport */
6925 int udp_fd; /* the QUIC socket, same rules; -1 if no h3 */
6926 void *tls_ctx; /* built SSL_CTX*, or NULL for plain */
6927 struct sockaddr_storage sa; /* host:port resolved once, in the parent; */
6928 socklen_t salen; /* what every bind, incl. a reuseport
6929 * rebind in a worker, binds */
6930 } hm_listener_spec;
6931  
6932 /* Validate one listener's options at run() time (before any bind/fork). */
6933 129 static void hm_check_listener(pTHX_ const hm_listener_spec *s) {
6934 /*
6935 # uncoverable condition left count:2 class:ignore_covered_err
6936 # uncoverable condition left count:3 class:ignore_covered_err
6937 # uncoverable condition left count:4 class:ignore_covered_err
6938 # uncoverable condition left count:5 class:ignore_covered_err */
6939 129 ▶ if (s->tls_cert || s->tls_key || s->tls_ca || s->tls_sni || s->tls_verify) {
Branch: 5 branches
BranchTrueFalseTotal
if (s->tls_cert || s->tls_key || s->tls_ca || s->tls_sni || s->tls_verify) { 115 14 129
if (s->tls_cert || s->tls_key || s->tls_ca || s->tls_sni || s->tls_verify) { 115 0 115
if (s->tls_cert || s->tls_key || s->tls_ca || s->tls_sni || s->tls_verify) { 115 0 115
if (s->tls_cert || s->tls_key || s->tls_ca || s->tls_sni || s->tls_verify) { 115 0 115
if (s->tls_cert || s->tls_key || s->tls_ca || s->tls_sni || s->tls_verify) { 0 115 115
6940 14 ▶ if (!(s->tls_cert && s->tls_key)) /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(s->tls_cert && s->tls_key)) /* # uncoverable condition left count:1 class:ignore_covered_err */ 14 0 14
if (!(s->tls_cert && s->tls_key)) /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 13 14
6941 1 croak("Hyperman->run: tls_cert and tls_key must be given together");
6942 /*
6943 # uncoverable branch true
6944 # uncoverable condition left count:1 class:ignore_covered_err */
6945 13 ▶ if (!hm_tls_available())
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_tls_available()) 0 13 13
6946 0 croak("Hyperman->run: TLS requested but OpenSSL support was not " /* # uncoverable statement */
6947 "built (install libssl-dev and reinstall Hyperman)");
6948 13 ▶ if (s->tls_verify && !s->tls_ca)
Branch: 2 branches
BranchTrueFalseTotal
if (s->tls_verify && !s->tls_ca) 3 10 13
if (s->tls_verify && !s->tls_ca) 1 2 3
6949 1 croak("Hyperman->run: tls_verify needs tls_ca (the CA to verify "
6950 "client certificates against)");
6951 }
6952 /*
6953 # uncoverable branch true class:ignore_covered_err
6954 # uncoverable condition left count:1 class:ignore_covered_err
6955 # uncoverable condition left count:2 class:ignore_covered_err */
6956 127 ▶ if (s->http2 && !hm_h2_available())
Branch: 2 branches
BranchTrueFalseTotal
if (s->http2 && !hm_h2_available()) 1 126 127
if (s->http2 && !hm_h2_available()) 1 0 1
6957 1 croak("Hyperman->run: http2 requested but nghttp2 support was not " /* # uncoverable statement */
6958 "built (install libnghttp2-dev and reinstall Hyperman)");
6959 126 ▶ if (s->http3) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->http3) { 3 123 126
6960 /*
6961 # uncoverable branch true class:ignore_covered_err
6962 # uncoverable branch false class:ignore_covered_err
6963 # uncoverable condition right count:1 class:ignore_covered_err */
6964 3 ▶ if (!hm_h3_available())
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_h3_available()) 3 0 3
6965 3 croak("Hyperman->run: http3 requested but QUIC support was not " /* # uncoverable statement class:ignore_covered_err */
6966 "built (it needs ngtcp2, ngtcp2_crypto_ossl, nghttp3 and "
6967 "OpenSSL 3.5 or newer - reinstall Hyperman once they are "
6968 "installed)");
6969 /* There is no cleartext QUIC the way there is h2c: the transport's
6970 * handshake IS a TLS handshake, so a certificate is not optional. */
6971 /*
6972 # uncoverable statement class:ignore_covered_err
6973 # uncoverable branch true class:ignore_covered_err
6974 # uncoverable branch false class:ignore_covered_err
6975 # uncoverable condition left count:1 class:ignore_covered_err
6976 # uncoverable condition right count:1 class:ignore_covered_err
6977 # uncoverable condition left count:2 class:ignore_covered_err
6978 # uncoverable condition right count:2 class:ignore_covered_err */
6979 0 ▶ if (!(s->tls_cert && s->tls_key))
Branch: 2 branches
BranchTrueFalseTotal
if (!(s->tls_cert && s->tls_key)) 0 0 0
if (!(s->tls_cert && s->tls_key)) 0 0 0
6980 0 croak("Hyperman->run: http3 needs tls_cert and tls_key (QUIC has " /* # uncoverable statement class:ignore_covered_err */
6981 "no cleartext mode)");
6982 }
6983 123 ▶ if (s->redirect_https && (s->tls_cert || s->tls_key)) /* # uncoverable condition left count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (s->redirect_https && (s->tls_cert || s->tls_key)) /* # uncoverable condition left count:3 class:ignore_covered_err */ 2 121 123
if (s->redirect_https && (s->tls_cert || s->tls_key)) /* # uncoverable condition left count:3 class:ignore_covered_err */ 1 1 2
if (s->redirect_https && (s->tls_cert || s->tls_key)) /* # uncoverable condition left count:3 class:ignore_covered_err */ 0 1 1
6984 1 croak("Hyperman->run: redirect_https on a TLS listener makes no sense");
6985 122 ▶ if (s->path) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->path) { 8 114 122
6986 /*
6987 # uncoverable branch true class:ignore_covered_err
6988 # uncoverable condition left count:1 class:ignore_covered_err */
6989 8 ▶ if (s->http3)
Branch: 1 branch
BranchTrueFalseTotal
if (s->http3) 0 8 8
6990 0 croak("Hyperman->run: http3 needs an IP listener; a path listener " /* # uncoverable statement class:ignore_covered_err */
6991 "has no UDP twin");
6992 }
6993 /*
6994 # uncoverable branch true class:ignore_covered_err
6995 # uncoverable condition left count:1 class:ignore_covered_err
6996 # uncoverable condition left count:2 class:ignore_covered_err */
6997 122 ▶ if (s->proxy_protocol && s->http3)
Branch: 2 branches
BranchTrueFalseTotal
if (s->proxy_protocol && s->http3) 3 119 122
if (s->proxy_protocol && s->http3) 0 3 3
6998 0 warn("Hyperman->run: proxy_protocol applies to the TCP side of this " /* # uncoverable statement class:ignore_covered_err */
6999 "listener only; QUIC has no byte stream before its handshake and "
7000 "reports real datagram sources");
7001 122 }
7002  
7003 /* Overlay per-listener options from a `listen => [ {..} ]` hashref onto a spec
7004 * pre-seeded with the top-level defaults. Unknown keys croak. A bare true
7005 * redirect_https means "the standard 443". */
7006 22 static void hm_listener_from_hv(pTHX_ HV *h, hm_listener_spec *s) {
7007 HE *he;
7008 22 hv_iterinit(h);
7009 59 ▶ while ((he = hv_iternext(h))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(h))) { 40 19 59
7010 40 ▶ STRLEN klen; const char *k = HePV(he, klen); SV *v = HeVAL(he);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN klen; const char *k = HePV(he, klen); SV *v = HeVAL(he); 0 40 40
7011 40 ▶ if (strEQ(k, "port")) s->port = (int)SvIV(v);
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "port")) s->port = (int)SvIV(v); 12 28 40
7012 /*
7013 # uncoverable branch true count:3 class:ignore_covered_err
7014 # uncoverable condition right count:2 class:ignore_covered_err */
7015 35 ▶ else if (strEQ(k, "host")) { if (SvOK(v)) s->host = SvPV_nolen(v); }
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "host")) { if (SvOK(v)) s->host = SvPV_nolen(v); } 7 21 28
else if (strEQ(k, "host")) { if (SvOK(v)) s->host = SvPV_nolen(v); } 7 0 7
7016 /*
7017 # uncoverable branch true count:3 class:ignore_covered_err
7018 # uncoverable condition right count:2 class:ignore_covered_err */
7019 30 ▶ else if (strEQ(k, "path")) { if (SvOK(v)) s->path = SvPV_nolen(v); }
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "path")) { if (SvOK(v)) s->path = SvPV_nolen(v); } 9 12 21
else if (strEQ(k, "path")) { if (SvOK(v)) s->path = SvPV_nolen(v); } 9 0 9
7020 12 ▶ else if (strEQ(k, "inherit")) s->inherit = SvTRUE(v) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "inherit")) s->inherit = SvTRUE(v) ? 1 : 0; 1 11 12
7021 11 ▶ else if (strEQ(k, "proxy_protocol")) s->proxy_protocol = SvTRUE(v) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "proxy_protocol")) s->proxy_protocol = SvTRUE(v) ? 1 : 0; 3 8 11
7022 8 ▶ else if (strEQ(k, "mode")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "mode")) { 2 6 8
7023 /*
7024 # uncoverable branch false class:ignore_covered_err
7025 # uncoverable condition right count:1 class:ignore_covered_err */
7026 2 ▶ if (SvOK(v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(v)) { 2 0 2
7027 2 ▶ if (SvIOK(v) || SvNOK(v)) s->mode = (int)SvIV(v); /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (SvIOK(v) || SvNOK(v)) s->mode = (int)SvIV(v); /* # uncoverable condition left count:2 class:ignore_covered_err */ 1 1 2
if (SvIOK(v) || SvNOK(v)) s->mode = (int)SvIV(v); /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 1 1
7028 else {
7029 char *end;
7030 1 const char *m = SvPV_nolen(v);
7031 1 s->mode = (int)strtol(m, &end, 8);
7032 /*
7033 # uncoverable branch false class:ignore_covered_err
7034 # uncoverable condition left count:1 class:ignore_covered_err
7035 # uncoverable condition right count:1 class:ignore_covered_err
7036 # uncoverable condition right count:2 class:ignore_covered_err */
7037 1 ▶ if (*m == '\0' || *end != '\0')
Branch: 2 branches
BranchTrueFalseTotal
if (*m == '\0' || *end != '\0') 1 0 1
if (*m == '\0' || *end != '\0') 1 0 1
7038 1 croak("Hyperman->run: mode '%s' is not an octal number", m);
7039 }
7040 1 s->mode_set = 1;
7041 }
7042 }
7043 7 ▶ else if (strEQ(k, "tls_cert")) s->tls_cert = SvOK(v) ? SvPV_nolen(v) : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "tls_cert")) s->tls_cert = SvOK(v) ? SvPV_nolen(v) : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 5 6
else if (strEQ(k, "tls_cert")) s->tls_cert = SvOK(v) ? SvPV_nolen(v) : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
7044 6 ▶ else if (strEQ(k, "tls_key")) s->tls_key = SvOK(v) ? SvPV_nolen(v) : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "tls_key")) s->tls_key = SvOK(v) ? SvPV_nolen(v) : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 4 5
else if (strEQ(k, "tls_key")) s->tls_key = SvOK(v) ? SvPV_nolen(v) : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
7045 /*
7046 # uncoverable branch true
7047 # uncoverable condition left count:1 class:ignore_covered_err
7048 # uncoverable condition left count:2 class:ignore_covered_err
7049 # uncoverable condition right count:2 class:ignore_covered_err */
7050 4 ▶ else if (strEQ(k, "tls_ca")) s->tls_ca = SvOK(v) ? SvPV_nolen(v) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "tls_ca")) s->tls_ca = SvOK(v) ? SvPV_nolen(v) : NULL; 0 4 4
else if (strEQ(k, "tls_ca")) s->tls_ca = SvOK(v) ? SvPV_nolen(v) : NULL; 0 0 0
7051 /*
7052 # uncoverable branch true
7053 # uncoverable condition left count:1 class:ignore_covered_err
7054 # uncoverable condition left count:2 class:ignore_covered_err
7055 # uncoverable condition right count:2 class:ignore_covered_err */
7056 4 ▶ else if (strEQ(k, "tls_sni")) s->tls_sni = SvOK(v) ? v : NULL;
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "tls_sni")) s->tls_sni = SvOK(v) ? v : NULL; 0 4 4
else if (strEQ(k, "tls_sni")) s->tls_sni = SvOK(v) ? v : NULL; 0 0 0
7057 4 ▶ else if (strEQ(k, "http2")) s->http2 = SvTRUE(v) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "http2")) s->http2 = SvTRUE(v) ? 1 : 0; 0 4 4
7058 4 ▶ else if (strEQ(k, "http3")) s->http3 = SvTRUE(v) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "http3")) s->http3 = SvTRUE(v) ? 1 : 0; 1 3 4
7059 3 ▶ else if (strEQ(k, "redirect_https")) s->redirect_https = (int)SvIV(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "redirect_https")) s->redirect_https = (int)SvIV(v); 1 2 3
7060 2 ▶ else if (strEQ(k, "tls_verify")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "tls_verify")) { 1 1 2
7061 1 const char *m = SvPV_nolen(v);
7062 /*
7063 # uncoverable branch true
7064 # uncoverable condition left count:1 class:ignore_covered_err */
7065 1 ▶ if (strEQ(m, "require")) s->tls_verify = 2;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(m, "require")) s->tls_verify = 2; 0 1 1
7066 /*
7067 # uncoverable branch true
7068 # uncoverable condition left count:1 class:ignore_covered_err */
7069 1 ▶ else if (strEQ(m, "optional")) s->tls_verify = 1;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(m, "optional")) s->tls_verify = 1; 0 1 1
7070 /*
7071 # uncoverable branch true
7072 # uncoverable condition left count:1 class:ignore_covered_err */
7073 1 ▶ else if (strEQ(m, "none")) s->tls_verify = 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(m, "none")) s->tls_verify = 0; 0 1 1
7074 1 else croak("Hyperman->run: tls_verify must be none/optional/require");
7075 }
7076 1 else croak("Hyperman->run: unknown listen option '%s'", k);
7077 }
7078 /*
7079 # uncoverable branch true class:ignore_covered_err
7080 # uncoverable condition left count:1 class:ignore_covered_err */
7081 19 ▶ if (s->redirect_https == 1) s->redirect_https = 443;
Branch: 1 branch
BranchTrueFalseTotal
if (s->redirect_https == 1) s->redirect_https = 443; 0 19 19
7082 /* A path listener is seeded with the top-level host and port like every
7083 * other spec; the path is the address, so those defaults are dropped
7084 * rather than read as a contradiction. */
7085 19 ▶ if (s->path) { s->host = NULL; s->port = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (s->path) { s->host = NULL; s->port = 0; } 8 11 19
7086 19 }
7087  
7088 /* Everything a worker needs; one struct so spawn plumbing stays sane. */
7089 typedef struct {
7090 SV *app;
7091 hm_listener_spec *lspecs; /* one or more listeners */
7092 int nlspecs;
7093 double idle_t, header_t, body_t, request_t, grace;
7094 UV body_min_rate;
7095 int max_pipe, reuseport, affinity, nworkers;
7096 int completion; /* -1 = say nothing, 0/1 = force */
7097 int compress;
7098 size_t compress_min;
7099 int compress_level;
7100 size_t max_read; /* request ceiling; 16MB by default */
7101 SV *compress_types;
7102 UV max_requests;
7103 SV *log_cb; /* access_log coderef, or NULL */
7104 int log_fd; /* fast access-log fd, or -1 */
7105 const char *log_path; /* access_log file to open (parent), or NULL */
7106 const char *log_format; /* access_log_format, NULL = Combined */
7107 hm_logtok *logtok; /* ... compiled, before the fork */
7108 int nlogtok, log_json, log_wants_t0;
7109 int err_fd; /* error_log fd, or -1 = stderr */
7110 const char *err_path; /* error_log file to open (parent), or NULL */
7111 /* Privileges, dropped in the parent after every bind and before the
7112 * first fork (hm_drop_privileges). Names are resolved FIRST, before the
7113 * bind, so an unknown user is a croak with the port still free. */
7114 const char *drop_user; /* user => name or uid, or NULL */
7115 const char *drop_group; /* group => name or gid, or NULL */
7116 const char *pid_path; /* pid_file, or NULL */
7117 int listen_fds; /* adopt every listener from LISTEN_FDS */
7118 int umask_set; /* umask => given */
7119 unsigned umask_val;
7120 int drop_uid_set, drop_gid_set;
7121 long drop_uid, drop_gid;
7122 char drop_name[64]; /* the user's name, for initgroups */
7123 SV *deny; /* arrayref of IPs to denylist, or NULL */
7124 unsigned deny_cap, rate_cap; /* arena table sizes, 0 = default */
7125 unsigned bus_slots, bus_slot_size, bus_groups; /* 0 = default */
7126 UV http3_max_conns; /* QUIC connection ceiling, 0 = off */
7127 const char *arena_name; /* Shared::Arena name, NULL = anonymous */
7128 STRLEN arena_name_len;
7129 UV arena_size; /* bytes, 0 = computed from the tenants */
7130 int distinct_clients; /* count distinct peers at accept (HLL) */
7131 } hm_worker_cfg;
7132  
7133 /* Populate the loop's listener array from the (already bound) specs. Under
7134 * reuseport each worker rebinds, so fds are passed in; otherwise they are the
7135 * parent's inherited fds. Returns 0, or -1 if a reuseport rebind failed. */
7136 134 static int hm_worker_listeners(hm_loop *loop, const hm_worker_cfg *cfg,
7137 const int *fds) {
7138 int i;
7139 134 loop->listeners = (hm_listener *)hm_xcalloc(cfg->nlspecs, sizeof(hm_listener));
7140 134 loop->nlisteners = cfg->nlspecs;
7141 279 ▶ for (i = 0; i < cfg->nlspecs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cfg->nlspecs; i++) { 145 134 279
7142 145 const hm_listener_spec *s = &cfg->lspecs[i];
7143 145 hm_listener *l = &loop->listeners[i];
7144 145 ▶ l->fd = fds ? fds[i] : s->fd;
Branch: 1 branch
BranchTrueFalseTotal
l->fd = fds ? fds[i] : s->fd; 2 143 145
7145 /*
7146 # uncoverable branch true
7147 # uncoverable condition left count:1 class:ignore_covered_err */
7148 145 ▶ if (l->fd < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (l->fd < 0) return -1; 0 145 145
7149 145 ▶ l->udp_fd = fds ? fds[cfg->nlspecs + i] : s->udp_fd;
Branch: 1 branch
BranchTrueFalseTotal
l->udp_fd = fds ? fds[cfg->nlspecs + i] : s->udp_fd; 2 143 145
7150 145 l->is_unix = s->path ? 1 : 0;
7151 145 l->proxy_protocol = s->proxy_protocol;
7152 282 ▶ l->host = strdup(s->path ? s->path
Branch: 1 branch
BranchTrueFalseTotal
l->host = strdup(s->path ? s->path 8 137 145
7153 /*
7154 # uncoverable condition right count:1 class:ignore_covered_err
7155 # uncoverable condition right count:2 class:ignore_covered_err */
7156 137 ▶ : s->host && *s->host ? s->host : "0.0.0.0");
Branch: 2 branches
BranchTrueFalseTotal
: s->host && *s->host ? s->host : "0.0.0.0"); 137 0 137
: s->host && *s->host ? s->host : "0.0.0.0"); 137 0 137
7157 145 l->port = s->port;
7158 145 l->tls_ctx = s->tls_ctx;
7159 145 l->http2 = s->http2;
7160 145 l->http3 = s->http3;
7161 145 l->redirect_https = s->redirect_https;
7162 /* Copied, not borrowed: hm_tls_reload frees the context it
7163 * replaces, and a rebuild has to name the same cert and key. */
7164 145 ▶ l->tls_cert = s->tls_cert ? strdup(s->tls_cert) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
l->tls_cert = s->tls_cert ? strdup(s->tls_cert) : NULL; 8 137 145
7165 145 ▶ l->tls_key = s->tls_key ? strdup(s->tls_key) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
l->tls_key = s->tls_key ? strdup(s->tls_key) : NULL; 8 137 145
7166 145 ▶ l->tls_ca = s->tls_ca ? strdup(s->tls_ca) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
l->tls_ca = s->tls_ca ? strdup(s->tls_ca) : NULL; 1 144 145
7167 145 l->tls_verify = s->tls_verify;
7168 145 ▶ l->tls_sni = s->tls_sni ? SvREFCNT_inc(s->tls_sni) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
l->tls_sni = s->tls_sni ? SvREFCNT_inc(s->tls_sni) : NULL; 4 141 145
7169 /* Precomputed once here rather than per response: the value never
7170 * changes for the life of the listener, and a header built per
7171 * request would be built on the hot path for every request the
7172 * listener ever serves. */
7173 /*
7174 # uncoverable branch true class:ignore_covered_err
7175 # uncoverable condition left count:1 class:ignore_covered_err */
7176 145 ▶ if (l->udp_fd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->udp_fd >= 0) { 0 145 145
7177 char b[64];
7178 0 int n = snprintf(b, sizeof(b), "h3=\":%d\"; ma=86400", l->port); /* # uncoverable statement class:ignore_covered_err */
7179 /*
7180 # uncoverable statement class:ignore_covered_err
7181 # uncoverable condition left count:1 class:ignore_covered_err
7182 # uncoverable condition right count:1 class:ignore_covered_err
7183 # uncoverable condition left count:2 class:ignore_covered_err
7184 # uncoverable condition right count:2 class:ignore_covered_err */
7185 0 ▶ l->altsvc = (n > 0 && n < (int)sizeof(b)) ? strdup(b) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
l->altsvc = (n > 0 && n < (int)sizeof(b)) ? strdup(b) : NULL; 0 0 0
l->altsvc = (n > 0 && n < (int)sizeof(b)) ? strdup(b) : NULL; 0 0 0
7186 }
7187 }
7188 134 return 0;
7189 }
7190  
7191 /* Rebuild this worker's TLS contexts from a fresh SNI map.
7192 *
7193 * The listener array is the WORKER'S: hm_worker_listeners above ran after
7194 * the fork and copied the parent's tls_ctx pointer into it, so swapping
7195 * that pointer changes this process and nothing else. The listening fd is
7196 * not touched, so nothing is unbound and no connection is refused;
7197 * connections already handshook keep the SSL they hold, and the next one
7198 * this worker accepts uses the new context.
7199 *
7200 * NOTHING IS INSTALLED THAT SERVES LESS THAN WHAT IS RUNNING. Two ways a
7201 * rebuild can come back worse, and both keep the old context:
7202 *
7203 * - the default certificate will not load. It is the one every
7204 * hostname falls back to, so a fleet keeps serving the stale one
7205 * rather than none.
7206 * - a per-host certificate will not load. hm_tls_ctx_build skips it
7207 * with a message and still returns a usable default, which is right
7208 * at boot but silently drops that domain to the fallback here. So
7209 * the built registry is counted against what was asked for, and a
7210 * context carrying fewer hosts is thrown away. The cost is that one
7211 * corrupt PEM defers everybody's update to the next poll, against
7212 * installing a context that quietly stopped serving a domain.
7213 *
7214 * Returns the number of listeners rebuilt. */
7215 3 static int hm_tls_reload(pTHX_ hm_loop *loop, SV *sni) {
7216 3 int i, done = 0;
7217 /*
7218 # uncoverable condition right count:1 class:ignore_covered_err
7219 # uncoverable condition right count:2 class:ignore_covered_err
7220 # uncoverable condition right count:3 class:ignore_covered_err */
7221 3 ▶ int given = sni && SvROK(sni) && SvTYPE(SvRV(sni)) == SVt_PVHV;
Branch: 3 branches
BranchTrueFalseTotal
int given = sni && SvROK(sni) && SvTYPE(SvRV(sni)) == SVt_PVHV; 3 0 3
int given = sni && SvROK(sni) && SvTYPE(SvRV(sni)) == SVt_PVHV; 3 0 3
int given = sni && SvROK(sni) && SvTYPE(SvRV(sni)) == SVt_PVHV; 3 0 3
7222 /*
7223 # uncoverable branch true
7224 # uncoverable condition left count:1 class:ignore_covered_err */
7225 3 ▶ if (!loop) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!loop) return 0; 0 3 3
7226  
7227 6 ▶ for (i = 0; i < loop->nlisteners; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < loop->nlisteners; i++) { 3 3 6
7228 3 hm_listener *l = &loop->listeners[i];
7229 void *fresh;
7230 unsigned char tkey[HM_TLS_TKEY_MAX];
7231 3 const unsigned char *tk = NULL;
7232 3 size_t tklen = 0;
7233 /* No map given: the listener's own, so a reload called for the
7234 * default certificate alone keeps every SNI host. Without this the
7235 * rebuilt context had no registry and the "fewer hosts" refusal
7236 * below did not apply to it, so every name fell to the default. */
7237 3 ▶ SV *use = given ? sni : l->tls_sni; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *use = given ? sni : l->tls_sni; /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 3 3
7238 3 ▶ int want = (use && SvROK(use) && SvTYPE(SvRV(use)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
int want = (use && SvROK(use) && SvTYPE(SvRV(use)) == SVt_PVHV) 3 0 3
int want = (use && SvROK(use) && SvTYPE(SvRV(use)) == SVt_PVHV) 3 0 3
7239 6 ▶ ? (int)HvUSEDKEYS((HV *)SvRV(use)) : -1; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
? (int)HvUSEDKEYS((HV *)SvRV(use)) : -1; /* # uncoverable condition left count:1 class:ignore_covered_err */ 3 0 3
? (int)HvUSEDKEYS((HV *)SvRV(use)) : -1; /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 3 3
7240 /*
7241 # uncoverable condition left count:1 class:ignore_covered_err
7242 # uncoverable condition left count:2 class:ignore_covered_err
7243 # uncoverable condition left count:3 class:ignore_covered_err */
7244 4 ▶ if (!l->tls_ctx || !l->tls_cert || !l->tls_key) continue; /* plain */
Branch: 3 branches
BranchTrueFalseTotal
if (!l->tls_ctx || !l->tls_cert || !l->tls_key) continue; /* plain */ 3 0 3
if (!l->tls_ctx || !l->tls_cert || !l->tls_key) continue; /* plain */ 3 0 3
if (!l->tls_ctx || !l->tls_cert || !l->tls_key) continue; /* plain */ 0 3 3
7245  
7246 /* Carry the running context's session-ticket key into the new one.
7247 * The boot-time contexts are built before the fork, so every worker
7248 * starts out able to decrypt every other worker's tickets - but this
7249 * reload runs independently in each of them, and SSL_CTX_new mints a
7250 * fresh random key. Left alone, the first rotation would leave a
7251 * ticket issued by one worker undecryptable by its siblings, and
7252 * every resumption a client attempted would quietly become a full
7253 * handshake: the signature and key exchange that tickets exist to
7254 * avoid, on a fleet that rotates certificates continuously. */
7255 /*
7256 # uncoverable branch false class:ignore_covered_err
7257 # uncoverable condition right count:1 class:ignore_covered_err */
7258 3 ▶ if (hm_tls_get_ticket_key(l->tls_ctx, tkey, &tklen)) tk = tkey;
Branch: 1 branch
BranchTrueFalseTotal
if (hm_tls_get_ticket_key(l->tls_ctx, tkey, &tklen)) tk = tkey; 3 0 3
7259  
7260 3 fresh = hm_tls_ctx_build(aTHX_ l->tls_cert, l->tls_key, l->tls_ca,
7261 l->tls_verify, use, l->http2, tk, tklen);
7262 /*
7263 # uncoverable branch true
7264 # uncoverable condition left count:1 class:ignore_covered_err */
7265 3 ▶ if (!fresh) continue; /* keep what we are serving */
Branch: 1 branch
BranchTrueFalseTotal
if (!fresh) continue; /* keep what we are serving */ 0 3 3
7266  
7267 3 ▶ if (want > 0 && hm_tls_sni_count(fresh) < want) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (want > 0 && hm_tls_sni_count(fresh) < want) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 3 0 3
if (want > 0 && hm_tls_sni_count(fresh) < want) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 2 3
7268 1 hm_errlog("Hyperman TLS: reload built %d of %d SNI hosts; "
7269 "keeping the running certificates\n",
7270 hm_tls_sni_count(fresh), want);
7271 1 hm_tls_ctx_free(fresh);
7272 1 continue;
7273 }
7274  
7275 /* The QUIC context serves the SAME certificate over the same origin,
7276 * so it is rebuilt with the TCP one and the two are swapped together
7277 * or not at all. Swapping only the TCP side leaves a fleet serving a
7278 * fresh certificate over TCP and a stale one over QUIC - a mismatch
7279 * no TCP-only monitor can see, on the transport a browser prefers.
7280 *
7281 * The new QUIC context is built BEFORE either is installed, so a
7282 * failure to build it keeps both of the running pair rather than
7283 * leaving a half-swapped listener. */
7284 /*
7285 # uncoverable statement class:ignore_covered_err
7286 # uncoverable branch true
7287 # uncoverable condition left count:1
7288 # uncoverable condition left count:2
7289 # uncoverable condition right count:2 */
7290 2 ▶ if (l->udp_fd >= 0 && l->quic) {
Branch: 2 branches
BranchTrueFalseTotal
if (l->udp_fd >= 0 && l->quic) { 0 2 2
if (l->udp_fd >= 0 && l->quic) { 0 0 0
7291 0 void *qfresh = hm_tls_quic_ctx_build(l->tls_cert, l->tls_key, /* # uncoverable statement */
7292 0 l->tls_ca, l->tls_verify, /* # uncoverable statement */
7293 tk, tklen); /* # uncoverable statement */
7294 /*
7295 # uncoverable statement
7296 # uncoverable branch true
7297 # uncoverable branch false
7298 # uncoverable condition left count:1
7299 # uncoverable condition right count:1 */
7300 0 ▶ if (!qfresh) {
Branch: 1 branch
BranchTrueFalseTotal
if (!qfresh) { 0 0 0
7301 0 hm_errlog("Hyperman TLS: reload could not rebuild the " /* # uncoverable statement */
7302 "QUIC context; keeping both running "
7303 "certificates\n");
7304 0 hm_tls_ctx_free(fresh); /* # uncoverable statement */
7305 0 continue; /* # uncoverable statement */
7306 }
7307 0 hm_quic_ctx_swap(aTHX_ l, qfresh); /* # uncoverable statement */
7308 }
7309  
7310 2 hm_tls_ctx_free(l->tls_ctx);
7311 2 l->tls_ctx = fresh;
7312 2 done++;
7313 }
7314 3 return done;
7315 }
7316  
7317 /* The bus wakeup, on this worker's loop.
7318 *
7319 * Lives here rather than in hm_sa_bus.h because it is the one part that needs
7320 * both halves: hm_sa_bus.h deliberately knows nothing about the server, and
7321 * the loop knows nothing about the ring.
7322 *
7323 * The callback MUST NOT CROAK. It is called from the event loop with no Perl
7324 * frame around it, and a die from a subscriber would unwind through the loop
7325 * and take the worker with it - so the dispatch below is the only thing here,
7326 * and anything Perl-facing wraps itself in G_EVAL before it gets this far. */
7327 5 static void hm_bus_wake_cb(pTHX_ int fd, int mask, void *ud) {
7328 PERL_UNUSED_ARG(fd);
7329 PERL_UNUSED_ARG(mask);
7330 PERL_UNUSED_ARG(ud);
7331 PERL_UNUSED_CONTEXT;
7332 /* Empty the pipe and clear the flag BEFORE dispatching, so a publish that
7333 * lands while we are dispatching sets it again and pokes afresh, rather
7334 * than being folded into a wakeup that has already been handled. The
7335 * order inside the ring's wake_drained matters too - see hm_sa_bus.h. */
7336 5 hm_sa_bus_waker_drained();
7337 5 (void)hm_sa_bus_dispatch();
7338 5 }
7339  
7340 /* The one-second tick: the loop's counters into this worker's scoreboard
7341 * row in one seqlock bracket, a heartbeat on the peer row, and the next
7342 * tick armed. A C timer is one-shot and freed on fire, so re-arming here is
7343 * the whole of the repetition. Stops with the loop: a draining worker keeps
7344 * mirroring until its last connection closes, and nothing here holds the
7345 * loop open once it wants to end. */
7346 248 static void hm_board_tick(pTHX_ void *ud) {
7347 248 hm_loop *loop = (hm_loop *)ud;
7348 248 hm_sa_board_mirror((uint64_t)loop->requests, (uint64_t)loop->accepts,
7349 248 (uint64_t)loop->denied, loop->nconns,
7350 248 (uint64_t)loop->bytes_out, (uint64_t)loop->datagrams,
7351 248 (uint64_t)loop->h3_requests);
7352 /*
7353 # uncoverable branch false class:ignore_covered_err
7354 # uncoverable condition right count:1 class:ignore_covered_err
7355 # uncoverable condition right count:2 class:ignore_covered_err */
7356 248 ▶ if (HM_SA && hm_sa_region) HM_SA->beat(hm_sa_region);
Branch: 2 branches
BranchTrueFalseTotal
if (HM_SA && hm_sa_region) HM_SA->beat(hm_sa_region); 248 0 248
if (HM_SA && hm_sa_region) HM_SA->beat(hm_sa_region); 248 0 248
7357 248 ▶ if (!loop->stopping)
Branch: 1 branch
BranchTrueFalseTotal
if (!loop->stopping) 246 2 248
7358 246 (void)hm_add_timer_watch_c(loop, 1.0, hm_board_tick, loop);
7359 248 }
7360  
7361 /* The polling fallback's tick: drain the ring, re-arm. */
7362 67 static void hm_bus_poll_cb(pTHX_ void *ud) {
7363 67 hm_loop *loop = (hm_loop *)ud;
7364 67 (void)hm_sa_bus_dispatch();
7365 /*
7366 # uncoverable branch false class:ignore_covered_err
7367 # uncoverable condition right count:1 class:ignore_covered_err */
7368 67 ▶ if (!loop->stop) (void)hm_add_timer_watch_c(loop, 0.05, hm_bus_poll_cb, loop);
Branch: 1 branch
BranchTrueFalseTotal
if (!loop->stop) (void)hm_add_timer_watch_c(loop, 0.05, hm_bus_poll_cb, loop); 67 0 67
7369 67 }
7370  
7371 134 static void hm_bus_worker_attach(pTHX_ hm_loop *loop) {
7372 int fd;
7373 134 ▶ if (!hm_sa_bus_live()) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_sa_bus_live()) return; 8 126 134
7374 /* A worker inherited the parent's cursors along with everything else, and
7375 * a cursor is a position in a stream this process has not been reading.
7376 * Left alone it either replays what the parent already handled or skips
7377 * what it has not. From now on. */
7378 126 hm_sa_bus_reset_cursors();
7379  
7380 /* THE LAST WORD ON WHETHER THIS PROCESS CAN BE WOKEN.
7381 *
7382 * hm_spawn asks for a slot by worker index, which is what keeps two
7383 * workers off one descriptor. It can come back empty-handed: the index it
7384 * asked for may still be held by a worker that is draining after a HUP,
7385 * and single-worker mode never goes through hm_spawn at all. Either way
7386 * the process ends up with no descriptor.
7387 *
7388 * Before this, that was the end of it - no watch was installed and this
7389 * worker never dispatched again, so a published invalidation or room
7390 * broadcast reached every worker but this one, for as long as it ran, with
7391 * nothing in any statistic to say so. Single-worker mode never delivered
7392 * to its own subscribers at all.
7393 *
7394 * So take whatever is going rather than go deaf. Which slot a worker holds
7395 * has never mattered; holding one is the whole of it. */
7396 126 fd = hm_sa_bus_waker_fd();
7397 126 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 58 68 126
7398 58 (void)hm_sa_bus_waker_take(-1);
7399 58 fd = hm_sa_bus_waker_fd();
7400 }
7401 126 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 6 120 126
7402 /* No descriptor: the slots were all held, or this process attached
7403 * the arena by name (a re-exec successor's pool does) and the pipes
7404 * in it belong to another process. Poll the ring instead, every
7405 * fifty milliseconds. Slower to notice a message than a poke, never
7406 * deaf. */
7407 6 hm_errlog("Hyperman: worker %d has no wakeup descriptor; polling the "
7408 6 "bus every 50ms instead", hm_os_getpid());
7409 6 (void)hm_add_timer_watch_c(loop, 0.05, hm_bus_poll_cb, loop);
7410 6 return;
7411 }
7412 120 hm_add_io_watch_c(aTHX_ loop, fd, HM_EV_READ, hm_bus_wake_cb, NULL);
7413 /* Anything published between the fork and this attach is already on the
7414 * ring with no poke coming, because nothing was watching to be poked. */
7415 120 (void)hm_sa_bus_dispatch();
7416 }
7417  
7418 134 static void hm_worker(pTHX_ const hm_worker_cfg *cfg, const int *fds) {
7419 134 hm_loop *loop = hm_loop_new(aTHX_ NULL);
7420 /*
7421 # uncoverable branch true
7422 # uncoverable condition left count:1 class:ignore_covered_err */
7423 134 ▶ if (!loop) croak("Hyperman: cannot create event loop");
Branch: 1 branch
BranchTrueFalseTotal
if (!loop) croak("Hyperman: cannot create event loop"); 0 134 134
7424 134 ▶ if (cfg->idle_t > 0) loop->idle_timeout = cfg->idle_t;
Branch: 1 branch
BranchTrueFalseTotal
if (cfg->idle_t > 0) loop->idle_timeout = cfg->idle_t; 1 133 134
7425 134 ▶ if (cfg->header_t > 0) loop->header_timeout = cfg->header_t;
Branch: 1 branch
BranchTrueFalseTotal
if (cfg->header_t > 0) loop->header_timeout = cfg->header_t; 4 130 134
7426 134 ▶ if (cfg->body_t > 0) loop->body_timeout = cfg->body_t;
Branch: 1 branch
BranchTrueFalseTotal
if (cfg->body_t > 0) loop->body_timeout = cfg->body_t; 3 131 134
7427 134 ▶ if (cfg->request_t > 0) loop->request_timeout = cfg->request_t;
Branch: 1 branch
BranchTrueFalseTotal
if (cfg->request_t > 0) loop->request_timeout = cfg->request_t; 1 133 134
7428 134 ▶ if (cfg->body_min_rate) loop->body_min_rate = cfg->body_min_rate;
Branch: 1 branch
BranchTrueFalseTotal
if (cfg->body_min_rate) loop->body_min_rate = cfg->body_min_rate; 1 133 134
7429 134 ▶ if (cfg->max_pipe > 0) loop->max_pipeline = cfg->max_pipe;
Branch: 1 branch
BranchTrueFalseTotal
if (cfg->max_pipe > 0) loop->max_pipeline = cfg->max_pipe; 2 132 134
7430 134 ▶ if (cfg->grace > 0) loop->shutdown_grace = cfg->grace;
Branch: 1 branch
BranchTrueFalseTotal
if (cfg->grace > 0) loop->shutdown_grace = cfg->grace; 17 117 134
7431 134 loop->max_requests = cfg->max_requests;
7432 /* Only when the caller actually said so: -1 leaves whatever the loop
7433 * decided for itself from the backend and the environment. */
7434 134 ▶ if (cfg->completion >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (cfg->completion >= 0) 1 133 134
7435 /*
7436 # uncoverable condition left count:1 class:ignore_covered_err
7437 # uncoverable condition left count:2 class:ignore_covered_err
7438 # uncoverable condition right count:2 class:ignore_covered_err */
7439 1 ▶ loop->completion = (cfg->completion && loop->be->submit_recv) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
loop->completion = (cfg->completion && loop->be->submit_recv) ? 1 : 0; 0 1 1
loop->completion = (cfg->completion && loop->be->submit_recv) ? 1 : 0; 0 0 0
7440 134 loop->compress = cfg->compress;
7441 134 ▶ if (cfg->compress_min) loop->compress_min = cfg->compress_min;
Branch: 1 branch
BranchTrueFalseTotal
if (cfg->compress_min) loop->compress_min = cfg->compress_min; 2 132 134
7442 134 ▶ if (cfg->compress_level) loop->compress_level = cfg->compress_level;
Branch: 1 branch
BranchTrueFalseTotal
if (cfg->compress_level) loop->compress_level = cfg->compress_level; 1 133 134
7443 134 ▶ if (cfg->max_read) loop->max_read = cfg->max_read;
Branch: 1 branch
BranchTrueFalseTotal
if (cfg->max_read) loop->max_read = cfg->max_read; 12 122 134
7444 134 loop->http3_max_conns = cfg->http3_max_conns;
7445 268 loop->compress_types = cfg->compress_types
7446 /*
7447 # uncoverable statement class:ignore_covered_err
7448 # uncoverable condition left count:1 */
7449 134 ▶ ? SvREFCNT_inc(cfg->compress_types) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? SvREFCNT_inc(cfg->compress_types) : NULL; 0 134 134
7450 134 loop->logtok = cfg->logtok; /* compiled once, in the parent */
7451 134 loop->nlogtok = cfg->nlogtok;
7452 134 loop->log_json = cfg->log_json;
7453 134 loop->log_wants_t0 = cfg->log_wants_t0;
7454 /*
7455 # uncoverable branch true class:ignore_covered_err
7456 # uncoverable condition left count:1 class:ignore_covered_err */
7457 134 ▶ if (hm_worker_listeners(loop, cfg, fds) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (hm_worker_listeners(loop, cfg, fds) < 0) { 0 134 134
7458 0 hm_loop_free(aTHX_ loop); /* # uncoverable statement */
7459 0 hm_exit(1); /* # uncoverable statement */
7460 }
7461 134 ▶ if (cfg->log_fd >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (cfg->log_fd >= 0) 8 126 134
7462 8 loop->log_fd = cfg->log_fd; /* fast C writer wins */
7463 126 ▶ else if (cfg->log_cb && SvOK(cfg->log_cb)) /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
else if (cfg->log_cb && SvOK(cfg->log_cb)) /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 122 126
else if (cfg->log_cb && SvOK(cfg->log_cb)) /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 0 4
7464 4 loop->log_cb = SvREFCNT_inc(cfg->log_cb);
7465 134 hm_attach_server(aTHX_ loop, cfg->app);
7466 /* v4 on_worker_start: the loop exists and has not started turning, and in
7467 * a prefork server this is already the child. The one moment a consumer
7468 * can attach something loop-bound and have it belong to the process that
7469 * will actually serve. Fires here rather than at either call site so the
7470 * single-worker case, which never forks, gets it too. */
7471 134 hm_worker_hook_fire(aTHX_ (void *)loop);
7472  
7473 /* The bus wakeup. The descriptors were made before the fork; this worker
7474 * takes the one that is its own and puts it on the loop, so a publish from
7475 * a sibling returns this loop from epoll with the message already on the
7476 * ring rather than leaving it to be found whenever somebody next looks.
7477 *
7478 * After hm_worker_hook_fire, so an application's own on_worker_start has
7479 * already run and anything it subscribed is registered before the first
7480 * wakeup can arrive. */
7481 134 hm_bus_worker_attach(aTHX_ loop);
7482 /* A peer slot of this worker's own in the Shared::Arena, keyed on the
7483 * pid it has now rather than the one it inherited. */
7484 134 hm_sa_worker_join();
7485 /* This worker's scoreboard row, mirrored now and then once a second.
7486 * The tick also beats the peer row, so a worker that only ever receives
7487 * on the bus never looks dead to the ring's hole escalation. */
7488 134 ▶ if (hm_sa_board_live() && hm_sa_board_take() >= 0) /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (hm_sa_board_live() && hm_sa_board_take() >= 0) /* # uncoverable condition right count:2 class:ignore_covered_err */ 130 4 134
if (hm_sa_board_live() && hm_sa_board_take() >= 0) /* # uncoverable condition right count:2 class:ignore_covered_err */ 130 0 130
7489 130 hm_board_tick(aTHX_ loop);
7490  
7491 134 hm_loop_run(aTHX_ loop, NULL);
7492 134 hm_loop_free(aTHX_ loop);
7493 134 }
7494  
7495 #ifndef _WIN32
7496 /* Fork one worker. With reuseport each worker binds its own listeners; with
7497 * affinity (Linux) worker widx is pinned to core widx % ncpu. */
7498 82 static pid_t hm_spawn(pTHX_ const hm_worker_cfg *cfg, int widx) {
7499 82 pid_t pid = fork();
7500 166 ▶ if (pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (pid == 0) { 84 82 166
7501 int stackfds[8];
7502 84 int *fds = NULL;
7503  
7504 /* A RESPAWNED worker is forked from inside the supervisor's loop,
7505 * where the pool's signals are handled by hm_sup_sig and BLOCKED.
7506 * The child inherits both. Until the worker's own loop has taken
7507 * over its signals, a TERM landing here would run the supervisor's
7508 * handler and set a flag nobody in this process reads: the signal
7509 * is swallowed, the worker never learns the pool is shutting down,
7510 * and the supervisor waits for it for ever. Found by a test that
7511 * killed a worker and sent TERM the moment its replacement had a
7512 * scoreboard row. Default dispositions and an empty mask, so a
7513 * TERM in that window ends the child, which is what TERM means. */
7514 {
7515 sigset_t none;
7516 84 sigemptyset(&none);
7517 84 signal(SIGTERM, SIG_DFL); signal(SIGINT, SIG_DFL);
7518 84 signal(SIGHUP, SIG_DFL); signal(SIGUSR1, SIG_DFL);
7519 84 signal(SIGUSR2, SIG_DFL); signal(SIGCHLD, SIG_DFL);
7520 84 signal(SIGALRM, SIG_DFL);
7521 84 alarm(0);
7522 84 sigprocmask(SIG_SETMASK, &none, NULL);
7523 }
7524  
7525 /* Tell perl it has been forked. $$ is a plain SV that perl only
7526 * refreshes inside pp_fork; forking from C here happens behind its
7527 * back, so without this the worker keeps reporting the supervisor's
7528 * pid - and POSIX::getpid is `sub getpid { $$ }`, so that reads wrong
7529 * too. Anything in the application that names a log line, a temp file
7530 * or a pid file after $$ then collides across the whole pool. This is
7531 * what pp_fork itself does in the child. */
7532 {
7533 84 GV *pidgv = gv_fetchpvs("$", GV_ADD|GV_NOTQUAL, SVt_PV);
7534 /*
7535 # uncoverable branch false class:ignore_covered_err
7536 # uncoverable condition right count:1 class:ignore_covered_err */
7537 84 ▶ if (pidgv) {
Branch: 1 branch
BranchTrueFalseTotal
if (pidgv) { 84 0 84
7538 84 SvREADONLY_off(GvSV(pidgv));
7539 84 sv_setiv(GvSV(pidgv), (IV)getpid());
7540 84 SvREADONLY_on(GvSV(pidgv));
7541 }
7542 }
7543  
7544 #if defined(__linux__) && defined(CPU_SET)
7545 /*
7546 # uncoverable branch true
7547 # uncoverable condition left count:1 */
7548 84 ▶ if (cfg->affinity) {
Branch: 1 branch
BranchTrueFalseTotal
if (cfg->affinity) { 0 84 84
7549 0 long ncpu = sysconf(_SC_NPROCESSORS_ONLN); /* # uncoverable statement */
7550 /*
7551 # uncoverable statement
7552 # uncoverable branch true
7553 # uncoverable branch false
7554 # uncoverable condition left count:1
7555 # uncoverable condition right count:1 */
7556 0 ▶ if (ncpu > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ncpu > 0) { 0 0 0
7557 cpu_set_t set;
7558 0 CPU_ZERO(&set); /* # uncoverable statement */
7559 0 ▶ CPU_SET((int)(widx % ncpu), &set); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
CPU_SET((int)(widx % ncpu), &set); /* # uncoverable statement */ 0 0 0
7560 0 sched_setaffinity(0, sizeof(set), &set); /* # uncoverable statement */
7561 }
7562 }
7563 #else
7564 (void)widx;
7565 #endif
7566 84 ▶ if (cfg->reuseport) {
Branch: 1 branch
BranchTrueFalseTotal
if (cfg->reuseport) { 2 82 84
7567 2 int i, n = cfg->nlspecs;
7568 /* Two descriptors per listener now, TCP in the first half and
7569 * QUIC in the second, so hm_worker_listeners reads fds[i] and
7570 * fds[n + i]. */
7571 4 fds = 2 * n <= (int)(sizeof(stackfds)/sizeof(stackfds[0]))
7572 ? stackfds
7573 2 ▶ : (int *)hm_xmalloc(sizeof(int) * 2 * n); /* # uncoverable statement class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
: (int *)hm_xmalloc(sizeof(int) * 2 * n); /* # uncoverable statement class:ignore_covered_err */ 2 0 2
7574 4 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2 2 4
7575 4 fds[i] = hm_make_listener((struct sockaddr *)&cfg->lspecs[i].sa,
7576 2 cfg->lspecs[i].salen, 1);
7577 /*
7578 # uncoverable branch true class:ignore_covered_err
7579 # uncoverable condition left count:1 class:ignore_covered_err */
7580 2 ▶ if (fds[i] < 0) hm_exit(1);
Branch: 1 branch
BranchTrueFalseTotal
if (fds[i] < 0) hm_exit(1); 0 2 2
7581 2 fds[n + i] = -1; /* # uncoverable statement class:ignore_covered_err */
7582 /*
7583 # uncoverable statement class:ignore_covered_err
7584 # uncoverable branch true
7585 # uncoverable condition left count:1 */
7586 2 ▶ if (cfg->lspecs[i].http3) {
Branch: 1 branch
BranchTrueFalseTotal
if (cfg->lspecs[i].http3) { 0 2 2
7587 0 fds[n + i] = hm_make_udp_listener((struct sockaddr *)&cfg->lspecs[i].sa, /* # uncoverable statement */
7588 0 cfg->lspecs[i].salen, 1); /* # uncoverable statement */
7589 /*
7590 # uncoverable statement
7591 # uncoverable branch true
7592 # uncoverable branch false
7593 # uncoverable condition left count:1
7594 # uncoverable condition right count:1 */
7595 0 ▶ if (fds[n + i] < 0) hm_exit(1);
Branch: 1 branch
BranchTrueFalseTotal
if (fds[n + i] < 0) hm_exit(1); 0 0 0
7596 }
7597 }
7598 }
7599 /* This child's own waker slot, claimed before hm_worker so the
7600 * attach below finds it. Index by worker number, so two workers never
7601 * share a descriptor. */
7602 84 (void)hm_sa_bus_waker_take(widx);
7603 84 hm_worker(aTHX_ cfg, fds);
7604 84 hm_exit(0);
7605 }
7606 82 return pid;
7607 }
7608 #endif /* !_WIN32 */
7609  
7610 /* The full server: validate + bind, then either run in-process (1 worker)
7611 * or supervise a pool (respawn with backoff, HUP/USR2 recycle, USR1 stats,
7612 * TERM/INT bounded drain). */
7613 /* ---- privileges and the pid file ----------------------------------------- */
7614  
7615 11 static int hm_all_digits(const char *s) {
7616 /*
7617 # uncoverable branch true
7618 # uncoverable condition left count:1 class:ignore_covered_err */
7619 11 ▶ if (!*s) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!*s) return 0; 0 11 11
7620 39 ▶ for (; *s; s++) if (*s < '0' || *s > '9') return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
for (; *s; s++) if (*s < '0' || *s > '9') return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */ 32 0 32
for (; *s; s++) if (*s < '0' || *s > '9') return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */ 4 28 32
for (; *s; s++) if (*s < '0' || *s > '9') return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */ 32 7 39
7621 7 return 1;
7622 }
7623  
7624 /* Turn user/group names into ids. Called before anything is bound: a typo
7625 * in `user` must not leave a server listening as root. */
7626 114 static void hm_resolve_drop(pTHX_ hm_worker_cfg *cfg) {
7627 114 ▶ if (!cfg->drop_user && !cfg->drop_group && !cfg->umask_set && !cfg->pid_path) /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 4 branches
BranchTrueFalseTotal
if (!cfg->drop_user && !cfg->drop_group && !cfg->umask_set && !cfg->pid_path) /* # uncoverable condition right count:2 class:ignore_covered_err */ 105 9 114
if (!cfg->drop_user && !cfg->drop_group && !cfg->umask_set && !cfg->pid_path) /* # uncoverable condition right count:2 class:ignore_covered_err */ 105 0 105
if (!cfg->drop_user && !cfg->drop_group && !cfg->umask_set && !cfg->pid_path) /* # uncoverable condition right count:2 class:ignore_covered_err */ 104 1 105
if (!cfg->drop_user && !cfg->drop_group && !cfg->umask_set && !cfg->pid_path) /* # uncoverable condition right count:2 class:ignore_covered_err */ 92 12 104
7628 92 return;
7629 #ifdef _WIN32
7630 croak("Hyperman->run: user, group, umask and pid_file are not supported "
7631 "on Windows");
7632 #else
7633 22 cfg->drop_name[0] = '\0';
7634 22 ▶ if (cfg->drop_user) {
Branch: 1 branch
BranchTrueFalseTotal
if (cfg->drop_user) { 9 13 22
7635 9 ▶ if (hm_all_digits(cfg->drop_user)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hm_all_digits(cfg->drop_user)) { 6 3 9
7636 struct passwd *pw;
7637 6 cfg->drop_uid = atol(cfg->drop_user);
7638 6 cfg->drop_uid_set = 1;
7639 6 pw = getpwuid((uid_t)cfg->drop_uid);
7640 6 ▶ if (pw) {
Branch: 1 branch
BranchTrueFalseTotal
if (pw) { 3 3 6
7641 3 strncpy(cfg->drop_name, pw->pw_name, sizeof(cfg->drop_name) - 1);
7642 3 ▶ if (!cfg->drop_group) { cfg->drop_gid = (long)pw->pw_gid; cfg->drop_gid_set = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (!cfg->drop_group) { cfg->drop_gid = (long)pw->pw_gid; cfg->drop_gid_set = 1; } 1 2 3
7643 /*
7644 # uncoverable branch false
7645 # uncoverable condition right count:1 class:ignore_covered_err */
7646 3 ▶ } else if (!cfg->drop_group) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!cfg->drop_group) { 3 0 3
7647 /* No entry to take a group from: without one the workers
7648 * would keep gid 0 and root's supplementary groups behind a
7649 * harmless-looking uid. */
7650 3 croak("Hyperman->run: user %s has no passwd entry; name a "
7651 "group => as well", cfg->drop_user);
7652 }
7653 } else {
7654 3 struct passwd *pw = getpwnam(cfg->drop_user);
7655 3 ▶ if (!pw) croak("Hyperman->run: user '%s': no such user", cfg->drop_user);
Branch: 1 branch
BranchTrueFalseTotal
if (!pw) croak("Hyperman->run: user '%s': no such user", cfg->drop_user); 2 1 3
7656 1 cfg->drop_uid = (long)pw->pw_uid;
7657 1 cfg->drop_uid_set = 1;
7658 1 strncpy(cfg->drop_name, pw->pw_name, sizeof(cfg->drop_name) - 1);
7659 /*
7660 # uncoverable branch false class:ignore_covered_err
7661 # uncoverable condition right count:1 class:ignore_covered_err */
7662 1 ▶ if (!cfg->drop_group) { cfg->drop_gid = (long)pw->pw_gid; cfg->drop_gid_set = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (!cfg->drop_group) { cfg->drop_gid = (long)pw->pw_gid; cfg->drop_gid_set = 1; } 1 0 1
7663 }
7664 }
7665 17 ▶ if (cfg->drop_group) {
Branch: 1 branch
BranchTrueFalseTotal
if (cfg->drop_group) { 2 15 17
7666 2 ▶ if (hm_all_digits(cfg->drop_group)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hm_all_digits(cfg->drop_group)) { 1 1 2
7667 1 cfg->drop_gid = atol(cfg->drop_group);
7668 } else {
7669 1 struct group *gr = getgrnam(cfg->drop_group);
7670 /*
7671 # uncoverable branch false
7672 # uncoverable condition right count:1 class:ignore_covered_err */
7673 1 ▶ if (!gr) croak("Hyperman->run: group '%s': no such group", cfg->drop_group);
Branch: 1 branch
BranchTrueFalseTotal
if (!gr) croak("Hyperman->run: group '%s': no such group", cfg->drop_group); 1 0 1
7674 0 cfg->drop_gid = (long)gr->gr_gid; /* # uncoverable statement */
7675 }
7676 1 cfg->drop_gid_set = 1;
7677 }
7678 #endif
7679 }
7680  
7681 /* Group first, then the user: once the uid is gone the gid cannot follow.
7682 * Each step is checked and a failure CROAKS - a server that could not drop
7683 * must not go on serving as root as if it had. */
7684 77 static void hm_drop_privileges(pTHX_ const hm_worker_cfg *cfg) {
7685 #ifndef _WIN32
7686 77 ▶ if (cfg->umask_set) umask((mode_t)cfg->umask_val);
Branch: 1 branch
BranchTrueFalseTotal
if (cfg->umask_set) umask((mode_t)cfg->umask_val); 1 76 77
7687 /* Not root: a re-exec successor forked by a supervisor that had already
7688 * dropped. It cannot change its ids and need not, if it already has the
7689 * ones asked for; anything else is refused, as before. */
7690 /*
7691 # uncoverable branch true class:ignore_covered_err
7692 # uncoverable condition left count:1 class:ignore_covered_err
7693 # uncoverable condition left count:2 class:ignore_covered_err
7694 # uncoverable condition right count:2 class:ignore_covered_err
7695 # uncoverable condition left count:3 class:ignore_covered_err
7696 # uncoverable condition right count:3 class:ignore_covered_err */
7697 77 ▶ if (geteuid() != 0 && (cfg->drop_uid_set || cfg->drop_gid_set)) {
Branch: 3 branches
BranchTrueFalseTotal
if (geteuid() != 0 && (cfg->drop_uid_set || cfg->drop_gid_set)) { 77 0 77
if (geteuid() != 0 && (cfg->drop_uid_set || cfg->drop_gid_set)) { 77 0 77
if (geteuid() != 0 && (cfg->drop_uid_set || cfg->drop_gid_set)) { 0 77 77
7698 /*
7699 # uncoverable statement
7700 # uncoverable branch true
7701 # uncoverable branch false
7702 # uncoverable condition left count:1
7703 # uncoverable condition right count:1
7704 # uncoverable condition left count:2
7705 # uncoverable condition right count:2
7706 # uncoverable condition left count:3
7707 # uncoverable condition right count:3 */
7708 0 ▶ if (cfg->drop_uid_set && (getuid() != (uid_t)cfg->drop_uid
Branch: 2 branches
BranchTrueFalseTotal
if (cfg->drop_uid_set && (getuid() != (uid_t)cfg->drop_uid 0 0 0
if (cfg->drop_uid_set && (getuid() != (uid_t)cfg->drop_uid 0 0 0
7709 0 ▶ || geteuid() != (uid_t)cfg->drop_uid)) /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
|| geteuid() != (uid_t)cfg->drop_uid)) /* # uncoverable statement */ 0 0 0
7710 0 croak("Hyperman->run: not root, and not uid %ld either: cannot " /* # uncoverable statement */
7711 "become user '%s'", cfg->drop_uid, cfg->drop_user); /* # uncoverable statement */
7712 /*
7713 # uncoverable statement
7714 # uncoverable branch true
7715 # uncoverable branch false
7716 # uncoverable condition left count:1
7717 # uncoverable condition right count:1
7718 # uncoverable condition left count:2
7719 # uncoverable condition right count:2 */
7720 0 ▶ if (cfg->drop_gid_set && getegid() != (gid_t)cfg->drop_gid)
Branch: 2 branches
BranchTrueFalseTotal
if (cfg->drop_gid_set && getegid() != (gid_t)cfg->drop_gid) 0 0 0
if (cfg->drop_gid_set && getegid() != (gid_t)cfg->drop_gid) 0 0 0
7721 0 croak("Hyperman->run: not root, and not gid %ld either: cannot " /* # uncoverable statement */
7722 "change group", cfg->drop_gid); /* # uncoverable statement */
7723 0 return; /* # uncoverable statement */
7724 }
7725 /*
7726 # uncoverable statement class:ignore_covered_err
7727 # uncoverable branch true
7728 # uncoverable condition left count:1 */
7729 77 ▶ if (cfg->drop_gid_set) {
Branch: 1 branch
BranchTrueFalseTotal
if (cfg->drop_gid_set) { 0 77 77
7730 /*
7731 # uncoverable statement
7732 # uncoverable branch true
7733 # uncoverable branch false
7734 # uncoverable condition left count:1
7735 # uncoverable condition right count:1 */
7736 0 ▶ if (cfg->drop_name[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (cfg->drop_name[0]) { 0 0 0
7737 /*
7738 # uncoverable statement
7739 # uncoverable branch true
7740 # uncoverable branch false
7741 # uncoverable condition left count:1
7742 # uncoverable condition right count:1 */
7743 0 ▶ if (initgroups(cfg->drop_name, (gid_t)cfg->drop_gid) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (initgroups(cfg->drop_name, (gid_t)cfg->drop_gid) < 0) 0 0 0
7744 0 croak("Hyperman->run: initgroups(%s): %s", cfg->drop_name, strerror(errno)); /* # uncoverable statement */
7745 } else {
7746 0 gid_t g = (gid_t)cfg->drop_gid; /* # uncoverable statement */
7747 /*
7748 # uncoverable statement
7749 # uncoverable branch true
7750 # uncoverable branch false
7751 # uncoverable condition left count:1
7752 # uncoverable condition right count:1 */
7753 0 ▶ if (setgroups(1, &g) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (setgroups(1, &g) < 0) 0 0 0
7754 0 croak("Hyperman->run: setgroups: %s", strerror(errno)); /* # uncoverable statement */
7755 }
7756 /*
7757 # uncoverable statement
7758 # uncoverable branch true
7759 # uncoverable branch false
7760 # uncoverable condition left count:1
7761 # uncoverable condition right count:1 */
7762 0 ▶ if (setgid((gid_t)cfg->drop_gid) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (setgid((gid_t)cfg->drop_gid) < 0) 0 0 0
7763 0 croak("Hyperman->run: setgid(%ld): %s", cfg->drop_gid, strerror(errno)); /* # uncoverable statement */
7764 }
7765 /*
7766 # uncoverable statement class:ignore_covered_err
7767 # uncoverable branch true
7768 # uncoverable condition left count:1 */
7769 77 ▶ if (cfg->drop_uid_set) {
Branch: 1 branch
BranchTrueFalseTotal
if (cfg->drop_uid_set) { 0 77 77
7770 /*
7771 # uncoverable statement
7772 # uncoverable branch true
7773 # uncoverable branch false
7774 # uncoverable condition left count:1
7775 # uncoverable condition right count:1 */
7776 0 ▶ if (setuid((uid_t)cfg->drop_uid) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (setuid((uid_t)cfg->drop_uid) < 0) 0 0 0
7777 0 croak("Hyperman->run: setuid(%ld): %s", cfg->drop_uid, strerror(errno)); /* # uncoverable statement */
7778 /*
7779 # uncoverable statement
7780 # uncoverable branch true
7781 # uncoverable branch false
7782 # uncoverable condition left count:1
7783 # uncoverable condition right count:1
7784 # uncoverable condition left count:2
7785 # uncoverable condition right count:2
7786 # uncoverable condition left count:3
7787 # uncoverable condition right count:3 */
7788 0 ▶ if (cfg->drop_uid != 0 && (getuid() != (uid_t)cfg->drop_uid
Branch: 2 branches
BranchTrueFalseTotal
if (cfg->drop_uid != 0 && (getuid() != (uid_t)cfg->drop_uid 0 0 0
if (cfg->drop_uid != 0 && (getuid() != (uid_t)cfg->drop_uid 0 0 0
7789 0 ▶ || geteuid() != (uid_t)cfg->drop_uid)) /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
|| geteuid() != (uid_t)cfg->drop_uid)) /* # uncoverable statement */ 0 0 0
7790 0 croak("Hyperman->run: still not uid %ld after setuid", cfg->drop_uid); /* # uncoverable statement */
7791 }
7792 #else
7793 (void)cfg;
7794 #endif
7795 }
7796  
7797 /* The pid file is written whole and renamed into place, so a reader never
7798 * sees it empty, and removed at exit ONLY if it still holds this pid: two
7799 * supervisors overlap during a re-exec reload, and the one leaving must not
7800 * take the other's file with it. */
7801 #ifndef O_NOFOLLOW
7802 #define O_NOFOLLOW 0
7803 #endif
7804 /* loud: a failure croaks (boot). Quiet: -1 and a log line, for the
7805 * supervisor re-asserting its own pid after a successor died. */
7806 83 static int hm_write_pidfile_(pTHX_ const hm_worker_cfg *cfg, int loud) {
7807 83 const char *path = cfg->pid_path;
7808 char tmp[1024], num[32];
7809 83 int fd = -1, n, tries, in_place = 0;
7810 83 ▶ if (!path) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!path) return 0; 69 14 83
7811 /* The temporary is created EXCLUSIVELY and never through a symlink: it
7812 * is written as root, before the drop, in a directory others may be
7813 * able to write to, and a predictable name opened with O_TRUNC was an
7814 * invitation to point it at a file of theirs. The suffix is random too,
7815 * so a planted name is not even a denial of service. */
7816 22 ▶ for (tries = 0; tries < 8 && fd < 0; tries++) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
for (tries = 0; tries < 8 && fd < 0; tries++) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 22 0 22
for (tries = 0; tries < 8 && fd < 0; tries++) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 14 8 22
7817 14 unsigned long r = (unsigned long)(hm_now_ns() ^ ((uint64_t)hm_os_getpid() << 20));
7818 14 n = snprintf(tmp, sizeof(tmp), "%s.%lx.tmp", path, r + (unsigned long)tries * 7919UL);
7819 14 ▶ if (n <= 0 || n >= (int)sizeof(tmp)) /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (n <= 0 || n >= (int)sizeof(tmp)) /* # uncoverable condition left count:1 class:ignore_covered_err */ 14 0 14
if (n <= 0 || n >= (int)sizeof(tmp)) /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 13 14
7820 1 croak("Hyperman->run: pid_file path too long");
7821 13 fd = hm_os_open_file(tmp, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0644);
7822 13 ▶ if (fd < 0 && errno != EEXIST) break; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (fd < 0 && errno != EEXIST) break; /* # uncoverable condition right count:2 class:ignore_covered_err */ 5 8 13
if (fd < 0 && errno != EEXIST) break; /* # uncoverable condition right count:2 class:ignore_covered_err */ 5 0 5
7823 }
7824 /* Eight random names cannot all exist: an EEXIST that outlasts the loop
7825 * is a directory that will not create anything for this user, which
7826 * DragonFly reports this way where others say EACCES (CPAN Testers,
7827 * 0.68). It is handled as that refusal below. */
7828 /*
7829 # uncoverable branch true
7830 # uncoverable condition left count:1
7831 # uncoverable condition left count:2 */
7832 13 ▶ if (fd < 0 && errno == EEXIST) errno = EACCES;
Branch: 2 branches
BranchTrueFalseTotal
if (fd < 0 && errno == EEXIST) errno = EACCES; 5 8 13
if (fd < 0 && errno == EEXIST) errno = EACCES; 0 5 5
7833 #ifndef _WIN32
7834 /*
7835 # uncoverable branch true class:ignore_covered_err
7836 # uncoverable condition left count:1 class:ignore_covered_err
7837 # uncoverable condition left count:2 class:ignore_covered_err
7838 # uncoverable condition left count:3 class:ignore_covered_err
7839 # uncoverable condition left count:4 class:ignore_covered_err
7840 # uncoverable condition right count:4 class:ignore_covered_err */
7841 13 ▶ if (fd < 0 && (errno == EACCES || errno == EPERM) && geteuid() != 0) {
Branch: 4 branches
BranchTrueFalseTotal
if (fd < 0 && (errno == EACCES || errno == EPERM) && geteuid() != 0) { 5 8 13
if (fd < 0 && (errno == EACCES || errno == EPERM) && geteuid() != 0) { 1 4 5
if (fd < 0 && (errno == EACCES || errno == EPERM) && geteuid() != 0) { 0 1 1
if (fd < 0 && (errno == EACCES || errno == EPERM) && geteuid() != 0) { 4 0 4
7842 /* A re-exec successor already running as the dropped user, in a
7843 * directory only root may create in (/run): the file itself was
7844 * chowned to that user when root wrote it, so it is rewritten in
7845 * place. Not atomic - a reader can catch it empty - but a pid file
7846 * the successor cannot update at all named a dead process. */
7847 4 fd = hm_os_open_file(path, O_WRONLY | O_TRUNC | O_NOFOLLOW | O_CLOEXEC, 0644); /* # uncoverable statement class:ignore_covered_err */
7848 4 in_place = 1; /* # uncoverable statement class:ignore_covered_err */
7849 }
7850 #endif
7851 13 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 4 9 13
7852 /*
7853 # uncoverable branch false
7854 # uncoverable condition right count:1 class:ignore_covered_err
7855 # uncoverable condition right count:2 class:ignore_covered_err */
7856 4 ▶ if (loud) croak("Hyperman->run: pid_file '%s': %s", in_place ? path : tmp,
Branch: 2 branches
BranchTrueFalseTotal
if (loud) croak("Hyperman->run: pid_file '%s': %s", in_place ? path : tmp, 4 0 4
if (loud) croak("Hyperman->run: pid_file '%s': %s", in_place ? path : tmp, 1 3 4
7857 strerror(errno));
7858 0 hm_errlog("Hyperman: pid_file '%s': %s\n", path, strerror(errno)); /* # uncoverable statement */
7859 0 return -1; /* # uncoverable statement */
7860 }
7861 9 n = (int)hm_itoa(num, (long)hm_os_getpid());
7862 9 num[n++] = '\n';
7863 /*
7864 # uncoverable branch true
7865 # uncoverable condition left count:1 class:ignore_covered_err */
7866 9 ▶ if (hm_os_write_file(fd, num, (size_t)n) != n) {
Branch: 1 branch
BranchTrueFalseTotal
if (hm_os_write_file(fd, num, (size_t)n) != n) { 0 9 9
7867 0 int e = errno; /* # uncoverable statement */
7868 0 hm_os_close(fd); /* # uncoverable statement */
7869 /*
7870 # uncoverable statement
7871 # uncoverable branch true
7872 # uncoverable branch false
7873 # uncoverable condition left count:1
7874 # uncoverable condition right count:1 */
7875 0 ▶ if (!in_place) (void)unlink(tmp);
Branch: 1 branch
BranchTrueFalseTotal
if (!in_place) (void)unlink(tmp); 0 0 0
7876 /*
7877 # uncoverable statement
7878 # uncoverable branch true
7879 # uncoverable branch false
7880 # uncoverable condition left count:1
7881 # uncoverable condition right count:1 */
7882 0 ▶ if (loud) croak("Hyperman->run: pid_file '%s': write: %s", path, strerror(e));
Branch: 1 branch
BranchTrueFalseTotal
if (loud) croak("Hyperman->run: pid_file '%s': write: %s", path, strerror(e)); 0 0 0
7883 0 hm_errlog("Hyperman: pid_file '%s': write: %s\n", path, strerror(e)); /* # uncoverable statement */
7884 0 return -1; /* # uncoverable statement */
7885 }
7886 #ifndef _WIN32
7887 /* Owned by the user the server will run as, so the successor of a
7888 * re-exec (born dropped) and the exit path can both still write it. */
7889 /*
7890 # uncoverable branch true class:ignore_covered_err
7891 # uncoverable condition left count:1 class:ignore_covered_err
7892 # uncoverable condition left count:2 class:ignore_covered_err
7893 # uncoverable condition right count:2 class:ignore_covered_err */
7894 9 ▶ if (cfg->drop_uid_set && geteuid() == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (cfg->drop_uid_set && geteuid() == 0) 0 9 9
if (cfg->drop_uid_set && geteuid() == 0) 0 0 0
7895 0 (void)fchown(fd, (uid_t)cfg->drop_uid, /* # uncoverable statement */
7896 /*
7897 # uncoverable statement
7898 # uncoverable condition left count:1
7899 # uncoverable condition right count:1 */
7900 0 ▶ cfg->drop_gid_set ? (gid_t)cfg->drop_gid : (gid_t)-1);
Branch: 1 branch
BranchTrueFalseTotal
cfg->drop_gid_set ? (gid_t)cfg->drop_gid : (gid_t)-1); 0 0 0
7901 #endif
7902 9 hm_os_close(fd);
7903 /*
7904 # uncoverable branch true class:ignore_covered_err
7905 # uncoverable condition left count:1 class:ignore_covered_err */
7906 9 ▶ if (in_place) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (in_place) return 0; 1 8 9
7907 /*
7908 # uncoverable branch true
7909 # uncoverable condition left count:1 class:ignore_covered_err */
7910 8 ▶ if (rename(tmp, path) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rename(tmp, path) < 0) { 0 8 8
7911 0 int e = errno; /* # uncoverable statement */
7912 0 (void)unlink(tmp); /* # uncoverable statement */
7913 /*
7914 # uncoverable statement
7915 # uncoverable branch true
7916 # uncoverable branch false
7917 # uncoverable condition left count:1
7918 # uncoverable condition right count:1 */
7919 0 ▶ if (loud) croak("Hyperman->run: pid_file '%s': rename: %s", path, strerror(e));
Branch: 1 branch
BranchTrueFalseTotal
if (loud) croak("Hyperman->run: pid_file '%s': rename: %s", path, strerror(e)); 0 0 0
7920 0 hm_errlog("Hyperman: pid_file '%s': rename: %s\n", path, strerror(e)); /* # uncoverable statement */
7921 0 return -1; /* # uncoverable statement */
7922 }
7923 8 return 0;
7924 }
7925 82 static void hm_write_pidfile(pTHX_ const hm_worker_cfg *cfg) {
7926 82 (void)hm_write_pidfile_(aTHX_ cfg, 1);
7927 77 }
7928  
7929 /* Removed at exit if it still holds this pid. A process that dropped
7930 * privileges may not unlink in the directory any more (/run is root's);
7931 * then the file is emptied instead, which is what a reader should take as
7932 * "not running", where a stale pid is a live lie. */
7933 77 static void hm_unlink_pidfile(const char *path) {
7934 char buf[32];
7935 int fd, n;
7936 84 ▶ if (!path) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!path) return; 69 8 77
7937 8 fd = hm_os_open_file(path, O_RDONLY | O_NOFOLLOW, 0);
7938 /*
7939 # uncoverable branch true
7940 # uncoverable condition left count:1 class:ignore_covered_err */
7941 8 ▶ if (fd < 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) return; 0 8 8
7942 8 n = (int)read(fd, buf, sizeof(buf) - 1);
7943 8 hm_os_close(fd);
7944 /*
7945 # uncoverable branch true
7946 # uncoverable condition left count:1 class:ignore_covered_err */
7947 8 ▶ if (n <= 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) return; 0 8 8
7948 8 buf[n] = '\0';
7949 8 ▶ if (atol(buf) != (long)hm_os_getpid()) return;
Branch: 1 branch
BranchTrueFalseTotal
if (atol(buf) != (long)hm_os_getpid()) return; 3 5 8
7950 /*
7951 # uncoverable branch false class:ignore_covered_err
7952 # uncoverable condition right count:1 class:ignore_covered_err */
7953 5 ▶ if (unlink(path) == 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (unlink(path) == 0) return; 4 1 5
7954 /*
7955 # uncoverable statement class:ignore_covered_err
7956 # uncoverable branch true class:ignore_covered_err
7957 # uncoverable branch false class:ignore_covered_err
7958 # uncoverable condition left count:1 class:ignore_covered_err
7959 # uncoverable condition right count:1 class:ignore_covered_err
7960 # uncoverable condition left count:2 class:ignore_covered_err
7961 # uncoverable condition right count:2 class:ignore_covered_err */
7962 1 ▶ if (errno == EACCES || errno == EPERM) {
Branch: 2 branches
BranchTrueFalseTotal
if (errno == EACCES || errno == EPERM) { 0 1 1
if (errno == EACCES || errno == EPERM) { 0 0 0
7963 1 fd = hm_os_open_file(path, O_WRONLY | O_TRUNC | O_NOFOLLOW, 0); /* # uncoverable statement class:ignore_covered_err */
7964 /*
7965 # uncoverable statement class:ignore_covered_err
7966 # uncoverable branch true class:ignore_covered_err
7967 # uncoverable branch false class:ignore_covered_err
7968 # uncoverable condition left count:1 class:ignore_covered_err
7969 # uncoverable condition right count:1 class:ignore_covered_err */
7970 1 ▶ if (fd >= 0) hm_os_close(fd);
Branch: 1 branch
BranchTrueFalseTotal
if (fd >= 0) hm_os_close(fd); 1 0 1
7971 }
7972 }
7973  
7974 /* Remove a variable from the C environment AND perl's %ENV, which is a hash
7975 * of its own, filled at startup: unsetenv alone leaves the application
7976 * reading the old value from the hash. Deleting through the hash updates
7977 * the C side via its magic; the explicit unsetenv covers a perl built
7978 * without that. */
7979 8 static void hm_env_unset(pTHX_ const char *name) {
7980 8 HV *penv = GvHV(PL_envgv);
7981 /*
7982 # uncoverable branch false class:ignore_covered_err
7983 # uncoverable condition right count:1 class:ignore_covered_err */
7984 8 ▶ if (penv) (void)hv_delete(penv, name, (I32)strlen(name), G_DISCARD);
Branch: 1 branch
BranchTrueFalseTotal
if (penv) (void)hv_delete(penv, name, (I32)strlen(name), G_DISCARD); 8 0 8
7985 #ifndef _WIN32
7986 8 unsetenv(name);
7987 #endif
7988 8 }
7989  
7990 /* Socket activation: a service manager that bound the sockets hands them
7991 * over as fds 3.., naming the count in LISTEN_FDS and this pid in
7992 * LISTEN_PID. Specs marked `inherit` (or all of them under listen_fds) take
7993 * them in order instead of binding. Each one is checked to be a listening
7994 * socket of the family the spec resolved to, and both variables are removed
7995 * at once so no worker - and no server an application forks - adopts them
7996 * again. The same helper will seat a re-exec successor (08). */
7997 104 static void hm_adopt_fds(pTHX_ hm_worker_cfg *cfg) {
7998 104 const char *nfds = getenv("LISTEN_FDS"), *lpid = getenv("LISTEN_PID");
7999 104 int i, n, next = 3, want = 0;
8000 /* A spec that already holds a socket was seated by hm_adopt_env: a
8001 * re-exec successor of a socket-activated server. LISTEN_FDS was
8002 * removed from the environment at the first boot, so counting those
8003 * specs made every USR2 under systemd croak "not set". */
8004 216 ▶ for (i = 0; i < cfg->nlspecs; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cfg->nlspecs; i++) 112 104 216
8005 112 ▶ if ((cfg->listen_fds || cfg->lspecs[i].inherit) && cfg->lspecs[i].fd < 0) /* # uncoverable condition right count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if ((cfg->listen_fds || cfg->lspecs[i].inherit) && cfg->lspecs[i].fd < 0) /* # uncoverable condition right count:3 class:ignore_covered_err */ 103 9 112
if ((cfg->listen_fds || cfg->lspecs[i].inherit) && cfg->lspecs[i].fd < 0) /* # uncoverable condition right count:3 class:ignore_covered_err */ 1 102 103
if ((cfg->listen_fds || cfg->lspecs[i].inherit) && cfg->lspecs[i].fd < 0) /* # uncoverable condition right count:3 class:ignore_covered_err */ 10 0 10
8006 10 want++;
8007 104 ▶ if (!want) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!want) return; 95 9 104
8008 #ifdef _WIN32
8009 croak("Hyperman->run: socket activation is not supported on Windows");
8010 #else
8011 9 ▶ if (!nfds || !lpid) /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!nfds || !lpid) /* # uncoverable condition left count:2 class:ignore_covered_err */ 8 1 9
if (!nfds || !lpid) /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 8 8
8012 1 croak("Hyperman->run: a listener asks to inherit a socket but LISTEN_FDS "
8013 "and LISTEN_PID are not set");
8014 8 ▶ if (atol(lpid) != (long)hm_os_getpid())
Branch: 1 branch
BranchTrueFalseTotal
if (atol(lpid) != (long)hm_os_getpid()) 2 6 8
8015 2 croak("Hyperman->run: LISTEN_PID is %s, this is %d: the sockets were "
8016 "not handed to this process", lpid, hm_os_getpid());
8017 6 ▶ if (cfg->reuseport)
Branch: 1 branch
BranchTrueFalseTotal
if (cfg->reuseport) 1 5 6
8018 1 croak("Hyperman->run: an inherited socket cannot be rebound per worker; "
8019 "drop reuseport");
8020 5 n = atoi(nfds);
8021 7 ▶ for (i = 0; i < cfg->nlspecs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cfg->nlspecs; i++) { 6 1 7
8022 6 hm_listener_spec *s = &cfg->lspecs[i];
8023 struct sockaddr_storage got;
8024 6 socklen_t gl = sizeof(got);
8025 6 int fd, acc = 0;
8026 6 socklen_t al = sizeof(acc);
8027 /*
8028 # uncoverable branch true class:ignore_covered_err
8029 # uncoverable condition left count:1 class:ignore_covered_err
8030 # uncoverable condition left count:2 class:ignore_covered_err
8031 # uncoverable condition right count:2 class:ignore_covered_err
8032 # uncoverable condition left count:3 class:ignore_covered_err */
8033 6 ▶ if (!(cfg->listen_fds || s->inherit) || s->fd >= 0) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(cfg->listen_fds || s->inherit) || s->fd >= 0) continue; 0 6 6
if (!(cfg->listen_fds || s->inherit) || s->fd >= 0) continue; 0 0 0
if (!(cfg->listen_fds || s->inherit) || s->fd >= 0) continue; 0 6 6
8034 6 ▶ if (next - 3 >= n)
Branch: 1 branch
BranchTrueFalseTotal
if (next - 3 >= n) 1 5 6
8035 1 croak("Hyperman->run: LISTEN_FDS=%d but listener %d wants one too", n, i);
8036 5 fd = next++;
8037 /*
8038 # uncoverable branch true class:ignore_covered_err
8039 # uncoverable condition left count:1 class:ignore_covered_err */
8040 5 ▶ if (s->http3)
Branch: 1 branch
BranchTrueFalseTotal
if (s->http3) 0 5 5
8041 0 croak("Hyperman->run: an inherited listener cannot carry http3"); /* # uncoverable statement class:ignore_covered_err */
8042 5 ▶ if (getsockname(fd, (struct sockaddr *)&got, &gl) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (getsockname(fd, (struct sockaddr *)&got, &gl) < 0) 1 4 5
8043 1 croak("Hyperman->run: inherited fd %d: getsockname: %s", fd, strerror(errno));
8044 4 ▶ if (got.ss_family != s->sa.ss_family)
Branch: 1 branch
BranchTrueFalseTotal
if (got.ss_family != s->sa.ss_family) 1 3 4
8045 1 croak("Hyperman->run: inherited fd %d is address family %d, listener %d "
8046 "resolved to %d", fd, (int)got.ss_family, i, (int)s->sa.ss_family);
8047 3 ▶ if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &acc, &al) == 0 && !acc) /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &acc, &al) == 0 && !acc) /* # uncoverable condition right count:1 class:ignore_covered_err */ 3 0 3
if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &acc, &al) == 0 && !acc) /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 2 3
8048 1 croak("Hyperman->run: inherited fd %d is not a listening socket", fd); /* # uncoverable statement class:ignore_covered_err */
8049 2 hm_set_nonblock(fd);
8050 /* As a bound listener is (hm_make_listener): not into anything the
8051 * application execs; hm_reexec clears it for the successor alone. */
8052 2 (void)fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
8053 2 s->fd = fd;
8054 }
8055 1 hm_env_unset(aTHX_ "LISTEN_FDS");
8056 1 hm_env_unset(aTHX_ "LISTEN_PID");
8057 1 hm_env_unset(aTHX_ "LISTEN_FDNAMES");
8058 #endif
8059 }
8060  
8061 /* ---- re-exec reload (USR2) -------------------------------------------------
8062 *
8063 * HUP reforks the pool from this process, which still holds the application
8064 * as it was compiled at boot. USR2 starts a NEW supervisor - a fresh perl, a
8065 * fresh compile of the application - that takes the bound listeners over,
8066 * forks its own pool, and only then asks this one to drain. The listen fds
8067 * are held by one process or the other the whole time, so no connection is
8068 * refused. A successor that fails to start changes nothing here; this
8069 * supervisor keeps serving and says so.
8070 *
8071 * What crosses the exec: the listener fds, named in HYPERMAN_REEXEC by the
8072 * host:port (or path) each was bound for, so the successor's configuration
8073 * may reorder or add listeners; the arena NAME, when there is one, so both
8074 * pools share the one arena during the overlap; and the old supervisor's
8075 * pid, to TERM. An anonymous arena is not shared: it is a private mapping
8076 * with no name to attach to, and the successor starts a fresh one (the
8077 * denylist and the rate counters begin again at the handoff). */
8078  
8079 #ifndef _WIN32
8080 5 static void hm_reexec(pTHX_ const hm_worker_cfg *cfg, const sigset_t *orig) {
8081 char env[4096];
8082 5 size_t n = 0;
8083 int i, k;
8084 pid_t pid;
8085 SV *perl;
8086 5 ▶ if (hm_resup_pid > 0 && kill(hm_resup_pid, 0) == 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (hm_resup_pid > 0 && kill(hm_resup_pid, 0) == 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 4 5
if (hm_resup_pid > 0 && kill(hm_resup_pid, 0) == 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
8087 1 hm_errlog("Hyperman: USR2 ignored: successor %d is still starting",
8088 (int)hm_resup_pid);
8089 1 return;
8090 }
8091 /*
8092 # uncoverable branch true class:ignore_covered_err
8093 # uncoverable condition left count:1 class:ignore_covered_err
8094 # uncoverable condition left count:2 class:ignore_covered_err */
8095 4 ▶ if (!hm_boot_argv || !hm_boot_argv[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (!hm_boot_argv || !hm_boot_argv[0]) { 4 0 4
if (!hm_boot_argv || !hm_boot_argv[0]) { 0 4 4
8096 0 hm_errlog("Hyperman: USR2 ignored: no argv to re-exec with"); /* # uncoverable statement */
8097 0 return; /* # uncoverable statement */
8098 }
8099 4 k = snprintf(env + n, sizeof(env) - n, "sup=%d", hm_os_getpid());
8100 /*
8101 # uncoverable branch true class:ignore_covered_err
8102 # uncoverable condition left count:1 class:ignore_covered_err
8103 # uncoverable condition left count:2 class:ignore_covered_err */
8104 4 ▶ if (k < 0 || (size_t)k >= sizeof(env) - n) return;
Branch: 2 branches
BranchTrueFalseTotal
if (k < 0 || (size_t)k >= sizeof(env) - n) return; 4 0 4
if (k < 0 || (size_t)k >= sizeof(env) - n) return; 0 4 4
8105 4 n += (size_t)k;
8106 4 ▶ if (cfg->arena_name && cfg->arena_name_len) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (cfg->arena_name && cfg->arena_name_len) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 3 1 4
if (cfg->arena_name && cfg->arena_name_len) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 3 0 3
8107 3 k = snprintf(env + n, sizeof(env) - n, ";arena=%.*s",
8108 3 (int)cfg->arena_name_len, cfg->arena_name);
8109 /*
8110 # uncoverable branch true class:ignore_covered_err
8111 # uncoverable condition left count:1 class:ignore_covered_err
8112 # uncoverable condition left count:2 class:ignore_covered_err */
8113 3 ▶ if (k < 0 || (size_t)k >= sizeof(env) - n) {
Branch: 2 branches
BranchTrueFalseTotal
if (k < 0 || (size_t)k >= sizeof(env) - n) { 3 0 3
if (k < 0 || (size_t)k >= sizeof(env) - n) { 0 3 3
8114 0 hm_errlog("Hyperman: USR2 ignored: the arena name does not fit the " /* # uncoverable statement */
8115 "handover environment");
8116 0 return; /* # uncoverable statement */
8117 }
8118 3 n += (size_t)k;
8119 }
8120 9 ▶ for (i = 0; i < cfg->nlspecs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cfg->nlspecs; i++) { 5 4 9
8121 5 const hm_listener_spec *s = &cfg->lspecs[i];
8122 /*
8123 # uncoverable branch true
8124 # uncoverable condition left count:1 class:ignore_covered_err */
8125 5 ▶ if (s->fd < 0) continue; /* reuseport: nothing held */
Branch: 1 branch
BranchTrueFalseTotal
if (s->fd < 0) continue; /* reuseport: nothing held */ 0 5 5
8126 5 ▶ if (s->path)
Branch: 1 branch
BranchTrueFalseTotal
if (s->path) 1 4 5
8127 1 k = snprintf(env + n, sizeof(env) - n, ";u=%s|%d", s->path, s->fd);
8128 else
8129 4 k = snprintf(env + n, sizeof(env) - n, ";l=%s|%d|%d|%d",
8130 /*
8131 # uncoverable condition right count:1
8132 # uncoverable condition right count:2 */
8133 4 ▶ s->host && *s->host ? s->host : "0.0.0.0", s->port,
Branch: 1 branch
BranchTrueFalseTotal
s->host && *s->host ? s->host : "0.0.0.0", s->port, 4 0 4
8134 4 ▶ s->fd, s->udp_fd);
Branch: 1 branch
BranchTrueFalseTotal
s->fd, s->udp_fd); 4 0 4
8135 /*
8136 # uncoverable branch true class:ignore_covered_err
8137 # uncoverable condition left count:1 class:ignore_covered_err
8138 # uncoverable condition left count:2 class:ignore_covered_err */
8139 5 ▶ if (k < 0 || (size_t)k >= sizeof(env) - n) {
Branch: 2 branches
BranchTrueFalseTotal
if (k < 0 || (size_t)k >= sizeof(env) - n) { 5 0 5
if (k < 0 || (size_t)k >= sizeof(env) - n) { 0 5 5
8140 0 hm_errlog("Hyperman: USR2 ignored: too many listeners to hand over"); /* # uncoverable statement */
8141 0 return; /* # uncoverable statement */
8142 }
8143 5 n += (size_t)k;
8144 }
8145 4 perl = get_sv("\030", 0); /* $^X */
8146 /*
8147 # uncoverable branch true class:ignore_covered_err
8148 # uncoverable condition left count:1 class:ignore_covered_err
8149 # uncoverable condition left count:2 class:ignore_covered_err */
8150 4 ▶ if (!perl || !SvOK(perl)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!perl || !SvOK(perl)) { 4 0 4
if (!perl || !SvOK(perl)) { 0 4 4
8151 0 hm_errlog("Hyperman: USR2 ignored: $^X is not set"); /* # uncoverable statement */
8152 0 return; /* # uncoverable statement */
8153 }
8154 4 pid = fork();
8155 /*
8156 # uncoverable branch true
8157 # uncoverable condition left count:1 class:ignore_covered_err */
8158 8 ▶ if (pid < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (pid < 0) { 0 8 8
8159 0 hm_errlog("Hyperman: USR2: fork: %s", strerror(errno)); /* # uncoverable statement */
8160 0 return; /* # uncoverable statement */
8161 }
8162 8 ▶ if (pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (pid == 0) { 4 4 8
8163 /* The supervisor runs with its signals BLOCKED and the mask survives
8164 * exec, as does a pending alarm: a successor born with either would
8165 * never wake in its own sigsuspend. */
8166 4 sigprocmask(SIG_SETMASK, orig, NULL);
8167 4 alarm(0);
8168 4 signal(SIGTERM, SIG_DFL); signal(SIGINT, SIG_DFL);
8169 4 signal(SIGHUP, SIG_DFL); signal(SIGUSR1, SIG_DFL);
8170 4 signal(SIGUSR2, SIG_DFL); signal(SIGCHLD, SIG_DFL);
8171 4 signal(SIGALRM, SIG_DFL);
8172 9 ▶ for (i = 0; i < cfg->nlspecs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cfg->nlspecs; i++) { 5 4 9
8173 5 const hm_listener_spec *s = &cfg->lspecs[i];
8174 /*
8175 # uncoverable branch false class:ignore_covered_err
8176 # uncoverable condition right count:1 class:ignore_covered_err */
8177 5 ▶ if (s->fd >= 0) (void)fcntl(s->fd, F_SETFD, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (s->fd >= 0) (void)fcntl(s->fd, F_SETFD, 0); 5 0 5
8178 /*
8179 # uncoverable branch true class:ignore_covered_err
8180 # uncoverable condition left count:1 class:ignore_covered_err */
8181 5 ▶ if (s->udp_fd >= 0) (void)fcntl(s->udp_fd, F_SETFD, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (s->udp_fd >= 0) (void)fcntl(s->udp_fd, F_SETFD, 0); 0 5 5
8182 }
8183 4 setenv("HYPERMAN_REEXEC", env, 1);
8184 4 execv(SvPV_nolen(perl), hm_boot_argv);
8185 4 _exit(127); /* # uncoverable statement class:ignore_covered_err */
8186 }
8187 4 hm_resup_pid = pid;
8188 4 hm_errlog("Hyperman: USR2: successor %d started; serving until it takes over",
8189 (int)pid);
8190 }
8191  
8192 /* The successor's half: HYPERMAN_REEXEC names the sockets and the arena.
8193 * Runs after the addresses are resolved and before anything binds. */
8194 105 static void hm_adopt_env(pTHX_ hm_worker_cfg *cfg) {
8195 105 const char *raw = getenv("HYPERMAN_REEXEC");
8196 105 char *buf, *tok, *save = NULL;
8197 int i;
8198 105 ▶ if (!raw || !*raw) return; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!raw || !*raw) return; /* # uncoverable condition left count:2 class:ignore_covered_err */ 5 100 105
if (!raw || !*raw) return; /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 5 5
8199 5 buf = strdup(raw);
8200 /*
8201 # uncoverable branch true
8202 # uncoverable condition left count:1 class:ignore_covered_err */
8203 5 ▶ if (!buf) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!buf) return; 0 5 5
8204 5 hm_env_unset(aTHX_ "HYPERMAN_REEXEC");
8205 11 ▶ for (i = 0; i < cfg->nlspecs; i++) { cfg->lspecs[i].fd = -1; cfg->lspecs[i].udp_fd = -1; }
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cfg->nlspecs; i++) { cfg->lspecs[i].fd = -1; cfg->lspecs[i].udp_fd = -1; } 6 5 11
8206 21 ▶ for (tok = strtok_r(buf, ";", &save); tok; tok = strtok_r(NULL, ";", &save)) {
Branch: 1 branch
BranchTrueFalseTotal
for (tok = strtok_r(buf, ";", &save); tok; tok = strtok_r(NULL, ";", &save)) { 17 4 21
8207 17 ▶ if (strncmp(tok, "sup=", 4) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strncmp(tok, "sup=", 4) == 0) { 5 12 17
8208 5 hm_resup_old = (pid_t)atol(tok + 4);
8209 12 ▶ } else if (strncmp(tok, "arena=", 6) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strncmp(tok, "arena=", 6) == 0) { 4 8 12
8210 4 ▶ if (!cfg->arena_name || !cfg->arena_name_len) { /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!cfg->arena_name || !cfg->arena_name_len) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 2 2 4
if (!cfg->arena_name || !cfg->arena_name_len) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 2 2
8211 2 cfg->arena_name = strdup(tok + 6); /* process lifetime */
8212 2 cfg->arena_name_len = strlen(tok + 6);
8213 }
8214 8 ▶ } else if (strncmp(tok, "l=", 2) == 0 || strncmp(tok, "u=", 2) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (strncmp(tok, "l=", 2) == 0 || strncmp(tok, "u=", 2) == 0) { 3 5 8
} else if (strncmp(tok, "l=", 2) == 0 || strncmp(tok, "u=", 2) == 0) { 2 1 3
8215 7 int unix_ = tok[0] == 'u';
8216 7 char *f1, *f2, *f3, *f4, *s2 = NULL;
8217 7 int fd = -1, udp = -1, port = 0, matched = 0;
8218 const char *name;
8219 7 f1 = strtok_r(tok + 2, "|", &s2);
8220 7 f2 = strtok_r(NULL, "|", &s2);
8221 7 f3 = strtok_r(NULL, "|", &s2);
8222 7 f4 = strtok_r(NULL, "|", &s2);
8223 /*
8224 # uncoverable branch true class:ignore_covered_err
8225 # uncoverable condition left count:1 class:ignore_covered_err
8226 # uncoverable condition left count:2 class:ignore_covered_err */
8227 7 ▶ if (!f1 || !f2) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!f1 || !f2) continue; 7 0 7
if (!f1 || !f2) continue; 0 7 7
8228 7 name = f1;
8229 7 ▶ if (unix_) { fd = atoi(f2); }
Branch: 1 branch
BranchTrueFalseTotal
if (unix_) { fd = atoi(f2); } 2 5 7
8230 /*
8231 # uncoverable condition left count:1 class:ignore_covered_err
8232 # uncoverable condition right count:2 class:ignore_covered_err */
8233 5 ▶ else { if (!f3) continue; port = atoi(f2); fd = atoi(f3); udp = f4 ? atoi(f4) : -1; }
Branch: 2 branches
BranchTrueFalseTotal
else { if (!f3) continue; port = atoi(f2); fd = atoi(f3); udp = f4 ? atoi(f4) : -1; } 0 5 5
else { if (!f3) continue; port = atoi(f2); fd = atoi(f3); udp = f4 ? atoi(f4) : -1; } 5 0 5
8234 /* reuseport pools rebind per worker and hold nothing to adopt */
8235 /*
8236 # uncoverable branch false class:ignore_covered_err
8237 # uncoverable condition right count:1 class:ignore_covered_err */
8238 7 ▶ if (!cfg->reuseport) {
Branch: 1 branch
BranchTrueFalseTotal
if (!cfg->reuseport) { 7 0 7
8239 10 ▶ for (i = 0; i < cfg->nlspecs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cfg->nlspecs; i++) { 8 2 10
8240 8 hm_listener_spec *s = &cfg->lspecs[i];
8241 8 ▶ if (s->fd >= 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (s->fd >= 0) continue; 1 7 8
8242 12 ▶ if (unix_ ? (s->path && strcmp(s->path, name) == 0)
Branch: 4 branches
BranchTrueFalseTotal
if (unix_ ? (s->path && strcmp(s->path, name) == 0) 2 5 7
if (unix_ ? (s->path && strcmp(s->path, name) == 0) 1 1 2
if (unix_ ? (s->path && strcmp(s->path, name) == 0) 1 0 1
if (unix_ ? (s->path && strcmp(s->path, name) == 0) 5 2 7
8243 5 ▶ : (!s->path && s->port == port
Branch: 1 branch
BranchTrueFalseTotal
: (!s->path && s->port == port 4 1 5
8244 10 ▶ && strcmp(s->host && *s->host ? s->host : "0.0.0.0", name) == 0)) {
Branch: 4 branches
BranchTrueFalseTotal
&& strcmp(s->host && *s->host ? s->host : "0.0.0.0", name) == 0)) { 5 0 5
&& strcmp(s->host && *s->host ? s->host : "0.0.0.0", name) == 0)) { 4 0 4
&& strcmp(s->host && *s->host ? s->host : "0.0.0.0", name) == 0)) { 4 0 4
&& strcmp(s->host && *s->host ? s->host : "0.0.0.0", name) == 0)) { 4 0 4
8245 struct sockaddr_storage got;
8246 5 socklen_t gl = sizeof(got);
8247 5 int acc = 0;
8248 5 socklen_t al = sizeof(acc);
8249 /*
8250 # uncoverable condition left count:2 class:ignore_covered_err
8251 # uncoverable condition left count:3 class:ignore_covered_err
8252 # uncoverable condition right count:3 class:ignore_covered_err
8253 # uncoverable condition left count:4 class:ignore_covered_err */
8254 5 ▶ if (getsockname(fd, (struct sockaddr *)&got, &gl) < 0
Branch: 1 branch
BranchTrueFalseTotal
if (getsockname(fd, (struct sockaddr *)&got, &gl) < 0 4 1 5
8255 4 ▶ || got.ss_family != s->sa.ss_family
Branch: 1 branch
BranchTrueFalseTotal
|| got.ss_family != s->sa.ss_family 4 0 4
8256 4 ▶ || (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &acc, &al) == 0 && !acc))
Branch: 2 branches
BranchTrueFalseTotal
|| (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &acc, &al) == 0 && !acc)) 4 0 4
|| (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &acc, &al) == 0 && !acc)) 0 4 4
8257 1 croak("Hyperman->run: re-exec: handed fd %d for %s is not "
8258 "the listener it was said to be", fd, name);
8259 4 hm_set_nonblock(fd); /* # uncoverable statement class:ignore_covered_err */
8260 4 (void)fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC); /* # uncoverable statement class:ignore_covered_err */
8261 4 s->fd = fd; /* # uncoverable statement class:ignore_covered_err */
8262 /*
8263 # uncoverable statement class:ignore_covered_err
8264 # uncoverable branch true
8265 # uncoverable condition left count:1
8266 # uncoverable condition left count:2
8267 # uncoverable condition right count:2 */
8268 4 ▶ if (udp >= 0 && s->http3) {
Branch: 2 branches
BranchTrueFalseTotal
if (udp >= 0 && s->http3) { 0 4 4
if (udp >= 0 && s->http3) { 0 0 0
8269 0 hm_set_nonblock(udp); /* # uncoverable statement */
8270 0 (void)fcntl(udp, F_SETFD, fcntl(udp, F_GETFD, 0) | FD_CLOEXEC); /* # uncoverable statement */
8271 0 s->udp_fd = udp; /* # uncoverable statement */
8272 /*
8273 # uncoverable statement class:ignore_covered_err
8274 # uncoverable branch true
8275 # uncoverable condition left count:1 */
8276 4 ▶ } else if (udp >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (udp >= 0) { 0 4 4
8277 0 hm_os_close(udp); /* # uncoverable statement */
8278 }
8279 4 matched = 1;
8280 4 break;
8281 }
8282 }
8283 }
8284 6 ▶ if (!matched) { /* a listener the new config dropped */
Branch: 1 branch
BranchTrueFalseTotal
if (!matched) { /* a listener the new config dropped */ 2 4 6
8285 /*
8286 # uncoverable branch false class:ignore_covered_err
8287 # uncoverable condition right count:1 class:ignore_covered_err */
8288 2 ▶ if (fd >= 0) hm_os_close(fd);
Branch: 1 branch
BranchTrueFalseTotal
if (fd >= 0) hm_os_close(fd); 2 0 2
8289 /*
8290 # uncoverable branch true class:ignore_covered_err
8291 # uncoverable condition left count:1 class:ignore_covered_err */
8292 2 ▶ if (udp >= 0) hm_os_close(udp);
Branch: 1 branch
BranchTrueFalseTotal
if (udp >= 0) hm_os_close(udp); 0 2 2
8293 }
8294 }
8295 }
8296 4 free(buf);
8297 4 hm_errlog("Hyperman: re-exec successor %d: sockets adopted from supervisor %d",
8298 4 hm_os_getpid(), (int)hm_resup_old);
8299 }
8300  
8301 /* The successor's pool is up: tell the old supervisor to drain. Waited for a
8302 * scoreboard row per child (ten seconds at most), or one second flat when
8303 * there is no board, so the old pool is not torn down ahead of the new one
8304 * answering. */
8305 27 static void hm_reexec_takeover(const pid_t *kids, int nkids) {
8306 int i, tries;
8307 27 ▶ if (hm_resup_old <= 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (hm_resup_old <= 0) return; 23 4 27
8308 /*
8309 # uncoverable branch false class:ignore_covered_err
8310 # uncoverable condition right count:1 class:ignore_covered_err */
8311 4 ▶ if (hm_sa_board_live()) {
Branch: 1 branch
BranchTrueFalseTotal
if (hm_sa_board_live()) { 4 0 4
8312 /*
8313 # uncoverable branch false class:ignore_covered_err
8314 # uncoverable condition right count:1 class:ignore_covered_err */
8315 8 ▶ for (tries = 0; tries < 100; tries++) {
Branch: 1 branch
BranchTrueFalseTotal
for (tries = 0; tries < 100; tries++) { 8 0 8
8316 8 int ready = 1;
8317 19 ▶ for (i = 0; i < nkids; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nkids; i++) { 15 4 19
8318 sa_sb_reading rd;
8319 15 ▶ if (hm_sa_board_row_of((uint64_t)kids[i], &rd) < 0) { ready = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (hm_sa_board_row_of((uint64_t)kids[i], &rd) < 0) { ready = 0; break; } 4 11 15
8320 }
8321 8 ▶ if (ready) break;
Branch: 1 branch
BranchTrueFalseTotal
if (ready) break; 4 4 8
8322 4 poll(NULL, 0, 100);
8323 }
8324 } else {
8325 0 poll(NULL, 0, 1000); /* # uncoverable statement class:ignore_covered_err */
8326 }
8327 /*
8328 # uncoverable branch false class:ignore_covered_err
8329 # uncoverable condition right count:1 class:ignore_covered_err */
8330 4 ▶ if (kill(hm_resup_old, SIGTERM) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (kill(hm_resup_old, SIGTERM) == 0) 4 0 4
8331 4 hm_errlog("Hyperman: re-exec: pool up, supervisor %d asked to drain",
8332 (int)hm_resup_old);
8333 4 hm_resup_old = 0;
8334 }
8335 #endif /* !_WIN32 */
8336  
8337 114 static void hm_run_server(pTHX_ hm_worker_cfg *cfg) {
8338 114 int workers = cfg->nworkers;
8339  
8340 /* Before the arena, before the bind: an unknown user or group is refused
8341 * while nothing is held, and on Windows the whole family is. */
8342 114 hm_resolve_drop(aTHX_ cfg);
8343 #ifndef _WIN32
8344 /* The umask FIRST: it is asked for so that what the server creates - the
8345 * unix socket, the pid file, the logs - comes out with it, and all of
8346 * those are made below. Applied at the drop it reached only the files
8347 * the application made. */
8348 108 ▶ if (cfg->umask_set) (void)umask((mode_t)cfg->umask_val);
Branch: 1 branch
BranchTrueFalseTotal
if (cfg->umask_set) (void)umask((mode_t)cfg->umask_val); 1 107 108
8349 #endif
8350 /* Likewise every listen address: a host that does not resolve is a
8351 * croak here, never a bind of INADDR_ANY in its place. */
8352 {
8353 int i;
8354 char err[128];
8355 221 ▶ for (i = 0; i < cfg->nlspecs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cfg->nlspecs; i++) { 116 105 221
8356 116 hm_listener_spec *s = &cfg->lspecs[i];
8357 116 s->fd = -1; /* calloc'd, and 0 is a descriptor */
8358 116 s->udp_fd = -1;
8359 116 ▶ if (s->path) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->path) { 8 108 116
8360 8 ▶ if (cfg->reuseport)
Branch: 1 branch
BranchTrueFalseTotal
if (cfg->reuseport) 1 7 8
8361 1 croak("Hyperman->run: reuseport cannot rebind a path listener "
8362 "per worker (each rebind would unlink the other's socket)");
8363 7 ▶ if (hm_resolve_unix(s->path, &s->sa, &s->salen, err, sizeof(err)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (hm_resolve_unix(s->path, &s->sa, &s->salen, err, sizeof(err)) < 0) 1 6 7
8364 1 croak("Hyperman->run: listen path '%s': %s", s->path, err);
8365 }
8366 108 ▶ else if (hm_resolve(s->host, s->port, SOCK_STREAM, &s->sa, &s->salen,
Branch: 1 branch
BranchTrueFalseTotal
else if (hm_resolve(s->host, s->port, SOCK_STREAM, &s->sa, &s->salen, 1 107 108
8367 err, sizeof(err)) < 0)
8368 1 ▶ croak("Hyperman->run: listen host '%s': %s",
Branch: 1 branch
BranchTrueFalseTotal
croak("Hyperman->run: listen host '%s': %s", 1 0 1
8369 s->host ? s->host : "", err); /* # uncoverable condition right count:1 class:ignore_covered_err */
8370 }
8371 }
8372 #ifndef _WIN32
8373 105 hm_adopt_env(aTHX_ cfg); /* a re-exec successor: the old sockets */
8374 #endif
8375 104 hm_adopt_fds(aTHX_ cfg);
8376  
8377 96 ▶ if (!hm_empty_input)
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_empty_input) 83 13 96
8378 83 hm_empty_input = hm_new_input(aTHX_ "", 0); /* inherited by workers */
8379 96 ▶ if (workers < 1) /* default: one per CPU */
Branch: 1 branch
BranchTrueFalseTotal
if (workers < 1) /* default: one per CPU */ 13 83 96
8380 13 workers = hm_os_ncpu(); /* always 1 on Windows */
8381 96 cfg->nworkers = workers;
8382  
8383 /* The Shared::Arena, created HERE - before any worker forks - so every
8384 * worker inherits the one mapping, and from Perl, so Hyperman->arena is
8385 * the real object with every tenant on it. The denylist and the rate
8386 * counters are two maps in it; static `deny` entries are loaded now, in
8387 * the parent, so they are live from the first accepted connection.
8388 * Single-worker (dev) mode maps it too, so the ABI behaves the same
8389 * whether or not a supervisor forks. */
8390 96 hm_sa_open(aTHX_ cfg->arena_name, cfg->arena_name_len,
8391 96 ▶ cfg->arena_size ? cfg->arena_size
Branch: 1 branch
BranchTrueFalseTotal
cfg->arena_size ? cfg->arena_size 2 94 96
8392 94 : hm_sa_bytes(cfg->deny_cap, cfg->rate_cap,
8393 cfg->bus_slots, cfg->bus_slot_size));
8394 {
8395 96 const char *which = NULL;
8396 96 int err = 0;
8397 96 ▶ if (hm_sa_abuse_open(cfg->deny_cap, cfg->rate_cap, &which, &err) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (hm_sa_abuse_open(cfg->deny_cap, cfg->rate_cap, &which, &err) < 0) 2 94 96
8398 2 warn("Hyperman: could not carve the \"%s\" map in the arena "
8399 "(Shared::Arena error %d): the denylist and the rate "
8400 "counters fail open; raise arena_size", which, err);
8401 }
8402  
8403 /* QUIC's stateless-reset and (later) Retry secret, generated HERE - once,
8404 * before any worker forks - for the same reason the TLS ticket key is:
8405 * under SO_REUSEPORT a client's token can land on a different worker than
8406 * the one that issued it, and a per-worker secret would make every such
8407 * token unverifiable. Inherited across the fork, so every worker agrees. */
8408 96 hm_quic_secret_init();
8409  
8410 /* The message bus, a ring in the same arena and for the same reason: a
8411 * ring created after the fork would be one ring per worker, which is a
8412 * bus that delivers to nobody. Opened even in single-worker mode, so the
8413 * ABI behaves the same whether or not a supervisor forks. */
8414 {
8415 96 int err = 0;
8416 96 ▶ if (hm_sa_bus_open(cfg->bus_slots, cfg->bus_slot_size, &err) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (hm_sa_bus_open(cfg->bus_slots, cfg->bus_slot_size, &err) < 0) 2 94 96
8417 2 warn("Hyperman: could not carve the bus ring in the arena "
8418 "(Shared::Arena error %d): the bus is local-only; raise "
8419 "arena_size", err);
8420 }
8421 /* ... and the wakeup descriptors, HERE for the same reason as the ring
8422 * and one more: a pipe created inside a worker is invisible to its
8423 * siblings, so a bus built after the fork delivers to some workers and
8424 * not others.
8425 *
8426 * TWO GENERATIONS' WORTH, plus the supervisor's own. A HUP or USR2 forks
8427 * the replacement pool while the old one is still draining, so both exist
8428 * at once and both want descriptors - and sized for one generation the
8429 * fresh workers found every slot held and came up deaf, which outlives the
8430 * recycle that caused it. Shared::Arena caps this at its own ceiling. */
8431 96 ▶ (void)hm_sa_bus_wake_init((uint32_t)(workers > 0 ? 2 * workers + 1 : 2)); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
(void)hm_sa_bus_wake_init((uint32_t)(workers > 0 ? 2 * workers + 1 : 2)); /* # uncoverable condition right count:1 class:ignore_covered_err */ 96 0 96
8432 /* The worker scoreboard, one row per worker plus one, and the
8433 * distinct-client sketch when asked for. Neither is on a hot path by
8434 * default: the board is mirrored by a timer, the sketch is opt-in. */
8435 {
8436 96 int err = 0;
8437 /* Two generations' worth, like the wakers: a re-exec successor's
8438 * pool shares this board with the pool it replaces until that one
8439 * has drained, and a row is not reclaimable while its worker lives. */
8440 /*
8441 # uncoverable branch true class:ignore_covered_err
8442 # uncoverable condition right count:1 class:ignore_covered_err
8443 # uncoverable condition left count:2 class:ignore_covered_err */
8444 96 ▶ if (hm_sa_board_open((uint32_t)(workers > 0 ? 2 * workers + 2 : 2), &err) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (hm_sa_board_open((uint32_t)(workers > 0 ? 2 * workers + 2 : 2), &err) < 0) 96 0 96
if (hm_sa_board_open((uint32_t)(workers > 0 ? 2 * workers + 2 : 2), &err) < 0) 0 96 96
8445 0 warn("Hyperman: could not carve the worker scoreboard in the arena " /* # uncoverable statement class:ignore_covered_err */
8446 "(Shared::Arena error %d): pool_stats is empty; raise "
8447 "arena_size", err);
8448 /*
8449 # uncoverable branch true class:ignore_covered_err
8450 # uncoverable condition left count:1 class:ignore_covered_err
8451 # uncoverable condition left count:2 class:ignore_covered_err */
8452 96 ▶ if (cfg->distinct_clients && hm_sa_hll_open(&err) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (cfg->distinct_clients && hm_sa_hll_open(&err) < 0) 2 94 96
if (cfg->distinct_clients && hm_sa_hll_open(&err) < 0) 0 2 2
8453 0 warn("Hyperman: could not carve the distinct-client sketch in the " /* # uncoverable statement */
8454 "arena (Shared::Arena error %d): distinct_clients is off", err); /* # uncoverable statement */
8455 }
8456 /*
8457 # uncoverable condition right count:2 class:ignore_covered_err
8458 # uncoverable condition right count:3 class:ignore_covered_err */
8459 96 ▶ if (cfg->deny && SvROK(cfg->deny) && SvTYPE(SvRV(cfg->deny)) == SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
if (cfg->deny && SvROK(cfg->deny) && SvTYPE(SvRV(cfg->deny)) == SVt_PVAV) { 3 93 96
if (cfg->deny && SvROK(cfg->deny) && SvTYPE(SvRV(cfg->deny)) == SVt_PVAV) { 3 0 3
if (cfg->deny && SvROK(cfg->deny) && SvTYPE(SvRV(cfg->deny)) == SVt_PVAV) { 3 0 3
8460 3 AV *av = (AV *)SvRV(cfg->deny);
8461 3 SSize_t i, n = av_len(av) + 1;
8462 6 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 3 3 6
8463 3 SV **e = av_fetch(av, i, 0);
8464 /*
8465 # uncoverable branch false class:ignore_covered_err
8466 # uncoverable condition right count:1 class:ignore_covered_err
8467 # uncoverable condition right count:2 class:ignore_covered_err
8468 # uncoverable condition right count:3 class:ignore_covered_err */
8469 6 ▶ if (e && *e && SvOK(*e)) hm_sa_deny_add(SvPV_nolen(*e), 0);
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e)) hm_sa_deny_add(SvPV_nolen(*e), 0); 3 0 3
if (e && *e && SvOK(*e)) hm_sa_deny_add(SvPV_nolen(*e), 0); 3 0 3
if (e && *e && SvOK(*e)) hm_sa_deny_add(SvPV_nolen(*e), 0); 3 0 3
8470 }
8471 }
8472  
8473 #ifndef SO_REUSEPORT
8474 if (cfg->reuseport) croak("Hyperman: SO_REUSEPORT not supported on this platform");
8475 #endif
8476 #ifndef __linux__
8477 if (cfg->affinity) croak("Hyperman: CPU affinity is Linux-only");
8478 #endif
8479  
8480 /* Per listener: build the TLS context before forking, so a bad cert/key
8481 * fails fast and the (read-only) SSL_CTX is shared copy-on-write across
8482 * workers. Then bind - in the parent, unless reuseport (each worker binds
8483 * its own). Under reuseport the parent still binds one probe fd so an
8484 * unusable address (e.g. a privileged port without root) fails fast. */
8485 {
8486 int i;
8487 197 ▶ for (i = 0; i < cfg->nlspecs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cfg->nlspecs; i++) { 104 93 197
8488 104 hm_listener_spec *s = &cfg->lspecs[i];
8489 104 ▶ if (s->tls_cert && s->tls_key) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (s->tls_cert && s->tls_key) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 11 93 104
if (s->tls_cert && s->tls_key) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 11 0 11
8490 11 s->tls_ctx = hm_tls_ctx_build(aTHX_ s->tls_cert, s->tls_key,
8491 s->tls_ca, s->tls_verify,
8492 s->tls_sni, s->http2, NULL, 0);
8493 11 ▶ if (!s->tls_ctx)
Branch: 1 branch
BranchTrueFalseTotal
if (!s->tls_ctx) 3 8 11
8494 3 croak("Hyperman: TLS: could not initialise from cert '%s' "
8495 "/ key '%s'", s->tls_cert, s->tls_key);
8496 }
8497 }
8498 }
8499  
8500 /* The access-log format, compiled once here so an unknown directive is a
8501 * croak before the fork and a line is a walk over an array after it. The
8502 * tokens are process-lifetime: every worker reads the parent's copy. */
8503 93 ▶ if (cfg->log_format && strcmp(cfg->log_format, "json") == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (cfg->log_format && strcmp(cfg->log_format, "json") == 0) { 9 84 93
if (cfg->log_format && strcmp(cfg->log_format, "json") == 0) { 3 6 9
8504 3 hm_logfmt_json(aTHX_ &cfg->logtok, &cfg->nlogtok);
8505 3 cfg->log_json = 1;
8506 3 cfg->log_wants_t0 = 1;
8507 } else {
8508 char err[64];
8509 90 ▶ if (hm_logfmt_compile(aTHX_ cfg->log_format ? cfg->log_format : HM_LOG_COMBINED,
Branch: 2 branches
BranchTrueFalseTotal
if (hm_logfmt_compile(aTHX_ cfg->log_format ? cfg->log_format : HM_LOG_COMBINED, 6 84 90
if (hm_logfmt_compile(aTHX_ cfg->log_format ? cfg->log_format : HM_LOG_COMBINED, 4 86 90
8510 &cfg->logtok, &cfg->nlogtok, &cfg->log_wants_t0,
8511 err, sizeof(err)) < 0)
8512 4 croak("Hyperman->run: access_log_format: unknown directive %s", err);
8513 }
8514 /* The error log, likewise opened once and inherited. */
8515 89 ▶ if (cfg->err_path && cfg->err_fd < 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (cfg->err_path && cfg->err_fd < 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 12 77 89
if (cfg->err_path && cfg->err_fd < 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 12 0 12
8516 12 cfg->err_fd = hm_os_open_file(cfg->err_path,
8517 O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC, 0644);
8518 12 ▶ if (cfg->err_fd < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (cfg->err_fd < 0) 1 11 12
8519 1 croak("Hyperman: open error_log '%s': %s", cfg->err_path, strerror(errno));
8520 }
8521 88 ▶ if (cfg->err_fd >= 0) hm_err_fd = cfg->err_fd;
Branch: 1 branch
BranchTrueFalseTotal
if (cfg->err_fd >= 0) hm_err_fd = cfg->err_fd; 12 76 88
8522  
8523 /* Open the access-log file once in the parent so all workers share (and
8524 * O_APPEND to) the same fd - whole-line writes stay atomic between them. */
8525 88 ▶ if (cfg->log_path && cfg->log_fd < 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (cfg->log_path && cfg->log_fd < 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 8 80 88
if (cfg->log_path && cfg->log_fd < 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 8 0 8
8526 8 cfg->log_fd = hm_os_open_file(cfg->log_path,
8527 O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC, 0644);
8528 8 ▶ if (cfg->log_fd < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (cfg->log_fd < 0) 1 7 8
8529 1 croak("Hyperman: open access_log '%s': %s",
8530 cfg->log_path, strerror(errno));
8531 }
8532  
8533 /* Under reuseport the workers rebind their ports themselves, which
8534 * needs the privilege for a port below 1024 that a dropped user no
8535 * longer has; that pairing is refused rather than half-honoured, and
8536 * refused here, before any bind, so it is the error a caller sees even
8537 * where this process could not bind the low port either. */
8538 87 ▶ if (cfg->reuseport && cfg->drop_uid_set) {
Branch: 2 branches
BranchTrueFalseTotal
if (cfg->reuseport && cfg->drop_uid_set) { 4 83 87
if (cfg->reuseport && cfg->drop_uid_set) { 3 1 4
8539 int i;
8540 /*
8541 # uncoverable branch false
8542 # uncoverable condition right count:1 class:ignore_covered_err */
8543 3 ▶ for (i = 0; i < cfg->nlspecs; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cfg->nlspecs; i++) 3 0 3
8544 /*
8545 # uncoverable branch false
8546 # uncoverable condition right count:1 class:ignore_covered_err */
8547 3 ▶ if (cfg->lspecs[i].port < 1024)
Branch: 1 branch
BranchTrueFalseTotal
if (cfg->lspecs[i].port < 1024) 3 0 3
8548 3 croak("Hyperman->run: user => with reuseport => 1 cannot rebind "
8549 "port %d as the dropped user; drop reuseport or use a "
8550 "port above 1023", cfg->lspecs[i].port);
8551 }
8552  
8553 {
8554 int i;
8555 174 ▶ for (i = 0; i < cfg->nlspecs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cfg->nlspecs; i++) { 92 82 174
8556 92 hm_listener_spec *s = &cfg->lspecs[i];
8557 92 ▶ if (s->fd >= 0) continue; /* adopted from LISTEN_FDS */
Branch: 1 branch
BranchTrueFalseTotal
if (s->fd >= 0) continue; /* adopted from LISTEN_FDS */ 6 86 92
8558 #ifndef _WIN32
8559 /* `mode` through the umask around the bind, not a chmod of the
8560 * path afterwards: in a directory others can write to, the path
8561 * could be swapped for a symlink between the two and root would
8562 * chmod whatever it pointed at. A bind creates the socket with
8563 * 0777 less the umask, so the mask is the mode's complement. */
8564 86 mode_t saved_umask = 0;
8565 86 int have_saved = 0;
8566 86 ▶ if (s->path && s->mode_set) {
Branch: 2 branches
BranchTrueFalseTotal
if (s->path && s->mode_set) { 3 83 86
if (s->path && s->mode_set) { 1 2 3
8567 1 saved_umask = umask((mode_t)(~(unsigned)s->mode & 0777u));
8568 1 have_saved = 1;
8569 }
8570 #endif
8571 86 s->fd = hm_make_listener((struct sockaddr *)&s->sa, s->salen, cfg->reuseport);
8572 #ifndef _WIN32
8573 86 ▶ if (have_saved) (void)umask(saved_umask);
Branch: 1 branch
BranchTrueFalseTotal
if (have_saved) (void)umask(saved_umask); 1 85 86
8574 #endif
8575 86 ▶ if (s->fd < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (s->fd < 0) 2 84 86
8576 2 ▶ croak("Hyperman: bind %s%s:%d: %s", s->path ? s->path : "",
Branch: 2 branches
BranchTrueFalseTotal
croak("Hyperman: bind %s%s:%d: %s", s->path ? s->path : "", 1 1 2
croak("Hyperman: bind %s%s:%d: %s", s->path ? s->path : "", 1 1 2
8577 s->path ? "" : s->host, s->port, strerror(errno));
8578 84 s->udp_fd = -1;
8579 /*
8580 # uncoverable branch true class:ignore_covered_err
8581 # uncoverable condition left count:1 class:ignore_covered_err */
8582 84 ▶ if (s->http3) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->http3) { 0 84 84
8583 0 s->udp_fd = hm_make_udp_listener((struct sockaddr *)&s->sa, s->salen, cfg->reuseport); /* # uncoverable statement class:ignore_covered_err */
8584 /*
8585 # uncoverable branch true class:ignore_covered_err
8586 # uncoverable statement class:ignore_covered_err
8587 # uncoverable branch false class:ignore_covered_err
8588 # uncoverable condition left count:1 class:ignore_covered_err
8589 # uncoverable condition right count:1 class:ignore_covered_err */
8590 0 ▶ if (s->udp_fd < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (s->udp_fd < 0) 0 0 0
8591 0 croak("Hyperman: bind udp %s:%d: %s", s->host, s->port, /* # uncoverable statement */
8592 strerror(errno)); /* # uncoverable statement */
8593 }
8594 84 ▶ if (cfg->reuseport) { /* probe only */
Branch: 1 branch
BranchTrueFalseTotal
if (cfg->reuseport) { /* probe only */ 1 83 84
8595 1 hm_os_close(s->fd); s->fd = -1;
8596 /*
8597 # uncoverable branch true class:ignore_covered_err
8598 # uncoverable condition left count:1 class:ignore_covered_err */
8599 1 ▶ if (s->udp_fd >= 0) { hm_os_close(s->udp_fd); s->udp_fd = -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (s->udp_fd >= 0) { hm_os_close(s->udp_fd); s->udp_fd = -1; } 0 1 1
8600 }
8601 }
8602 }
8603  
8604 /* Everything that needed root is held: the ports, the logs, the arena.
8605 * Record who we are, then stop being root, before the first fork so
8606 * every worker is born as the configured user. */
8607 82 hm_write_pidfile(aTHX_ cfg);
8608 77 hm_drop_privileges(aTHX_ cfg);
8609  
8610 77 ▶ if (workers == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (workers == 1) { 50 27 77
8611 /* dev mode: run the worker in this process, no supervisor. reuseport
8612 * needs a real bound fd here since there is no per-worker rebind. */
8613 /*
8614 # uncoverable branch true class:ignore_covered_err
8615 # uncoverable condition left count:1 class:ignore_covered_err */
8616 50 ▶ if (cfg->reuseport) {
Branch: 1 branch
BranchTrueFalseTotal
if (cfg->reuseport) { 0 50 50
8617 int i;
8618 /*
8619 # uncoverable statement
8620 # uncoverable branch true
8621 # uncoverable branch false
8622 # uncoverable condition left count:1
8623 # uncoverable condition right count:1 */
8624 0 ▶ for (i = 0; i < cfg->nlspecs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cfg->nlspecs; i++) { 0 0 0
8625 0 hm_listener_spec *s = &cfg->lspecs[i]; /* # uncoverable statement */
8626 0 s->fd = hm_make_listener((struct sockaddr *)&s->sa, s->salen, 1); /* # uncoverable statement */
8627 /*
8628 # uncoverable statement
8629 # uncoverable branch true
8630 # uncoverable branch false
8631 # uncoverable condition left count:1
8632 # uncoverable condition right count:1 */
8633 0 ▶ if (s->fd < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (s->fd < 0) 0 0 0
8634 0 croak("Hyperman: bind %s:%d: %s", s->host, s->port, /* # uncoverable statement */
8635 strerror(errno)); /* # uncoverable statement */
8636 /*
8637 # uncoverable statement
8638 # uncoverable branch true
8639 # uncoverable branch false
8640 # uncoverable condition left count:1
8641 # uncoverable condition right count:1 */
8642 0 ▶ if (s->http3) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->http3) { 0 0 0
8643 0 s->udp_fd = hm_make_udp_listener((struct sockaddr *)&s->sa, s->salen, 1); /* # uncoverable statement */
8644 /*
8645 # uncoverable statement
8646 # uncoverable branch true
8647 # uncoverable branch false
8648 # uncoverable condition left count:1
8649 # uncoverable condition right count:1 */
8650 0 ▶ if (s->udp_fd < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (s->udp_fd < 0) 0 0 0
8651 0 croak("Hyperman: bind udp %s:%d: %s", s->host, s->port, /* # uncoverable statement */
8652 strerror(errno)); /* # uncoverable statement */
8653 }
8654 }
8655 }
8656 50 hm_worker(aTHX_ cfg, NULL);
8657 50 hm_unlink_pidfile(cfg->pid_path);
8658 50 return;
8659 }
8660  
8661 #ifdef _WIN32
8662 /* Not a silent downgrade: an application sized for a pool is told, at
8663 * boot, in the log it already reads. */
8664 croak("Hyperman: a worker pool needs fork(2); Windows runs a single "
8665 "worker (workers => 1).");
8666 #else
8667 {
8668 int i;
8669 27 int backoff = 0;
8670 struct sigaction sa;
8671  
8672 93 ▶ for (i = 0; i < workers; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < workers; i++) { 66 27 93
8673 66 pid_t pid = hm_spawn(aTHX_ cfg, i);
8674 /*
8675 # uncoverable branch true
8676 # uncoverable condition left count:1 class:ignore_covered_err */
8677 66 ▶ if (pid < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (pid < 0) { 0 66 66
8678 /* take the pool down with us: croaking out of run() leaves
8679 * nobody supervising the workers already forked, and they
8680 * would keep the listening socket alive forever. */
8681 int j;
8682 /*
8683 # uncoverable statement
8684 # uncoverable branch true
8685 # uncoverable branch false
8686 # uncoverable condition left count:1
8687 # uncoverable condition right count:1 */
8688 0 ▶ for (j = 0; j < hm_nchildren; j++) kill(hm_children[j], SIGTERM);
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < hm_nchildren; j++) kill(hm_children[j], SIGTERM); 0 0 0
8689 0 croak("Hyperman: fork: %s", strerror(errno)); /* # uncoverable statement */
8690 }
8691 66 hm_child_start[hm_nchildren] = time(NULL);
8692 66 hm_child_widx[hm_nchildren] = i;
8693 66 hm_children[hm_nchildren++] = pid;
8694 }
8695  
8696 27 memset(&sa, 0, sizeof(sa));
8697 27 sa.sa_handler = hm_sup_sig; /* no SA_RESTART: waitpid must EINTR */
8698 27 sigemptyset(&sa.sa_mask);
8699 27 sigaction(SIGTERM, &sa, NULL);
8700 27 sigaction(SIGINT, &sa, NULL);
8701 27 sigaction(SIGHUP, &sa, NULL);
8702 27 sigaction(SIGUSR1, &sa, NULL);
8703 27 sigaction(SIGUSR2, &sa, NULL);
8704 /* SIGCHLD must be delivered so sigsuspend() below wakes when a worker
8705 * exits; hm_sup_sig() treats it as a no-op wakeup. */
8706 27 sigaction(SIGCHLD, &sa, NULL);
8707 /* ALRM is the supervisor's only clock: a look at the scoreboard every
8708 * few seconds for a worker that is alive and not moving.
8709 *
8710 * An alarm the CALLER already had pending is not ours to cancel. A
8711 * watchdog that says `alarm(120); $server->run` has asked to be killed
8712 * at that deadline, and taking SIGALRM over - a handler that only sets
8713 * a flag, re-armed every tick - silently turns that into a promise
8714 * nothing keeps, leaving an unsupervised pool running for ever.
8715 *
8716 * So the pending deadline is read out and carried on the tick, which
8717 * is armed for it even with no board to watch. Letting the signal
8718 * through unhandled instead would kill the supervisor where it stood
8719 * and leave the workers holding the listening socket with nobody to
8720 * shut them down - a watchdog that takes half the pool with it is not
8721 * one. The tick is three seconds, so the deadline is met to within
8722 * that, and what follows is the death the caller asked for. */
8723 27 hm_sup_alarm_at = 0;
8724 {
8725 27 unsigned pending = alarm(0);
8726 27 ▶ if (pending) hm_sup_alarm_at = time(NULL) + (time_t)pending;
Branch: 1 branch
BranchTrueFalseTotal
if (pending) hm_sup_alarm_at = time(NULL) + (time_t)pending; 22 5 27
8727 /*
8728 # uncoverable branch false class:ignore_covered_err
8729 # uncoverable condition right count:1 class:ignore_covered_err
8730 # uncoverable condition right count:2 class:ignore_covered_err */
8731 27 ▶ if (hm_sa_board_live() || hm_sup_alarm_at) {
Branch: 2 branches
BranchTrueFalseTotal
if (hm_sa_board_live() || hm_sup_alarm_at) { 1 26 27
if (hm_sa_board_live() || hm_sup_alarm_at) { 1 0 1
8732 27 sigaction(SIGALRM, &sa, NULL);
8733 27 alarm(HM_SUP_TICK_SECS);
8734 }
8735 }
8736  
8737 /* A successor: the pool is forked and serving on the adopted sockets;
8738 * the supervisor that handed them over can drain now. AFTER the
8739 * handlers above: this waits up to ten seconds, and the pid file
8740 * already names this process, so a deploy script retrying its
8741 * `kill -USR2 $(cat pidfile)` into that window hit SIG_DFL and
8742 * killed the successor, orphaning its workers on the sockets. */
8743 27 hm_reexec_takeover(hm_children, hm_nchildren);
8744  
8745 /* Race-free supervision: keep the acted-on signals blocked, check the
8746 * flags, reap without blocking, then sigsuspend() to wait for the next
8747 * signal atomically. A plain blocking waitpid() has a lost-wakeup
8748 * window - a signal arriving between a flag check and re-entering
8749 * waitpid() goes unserviced until some child happens to exit, which
8750 * for USR1 (stats dump) on an otherwise idle pool could be never. */
8751 {
8752 sigset_t block, orig;
8753 27 sigemptyset(&block);
8754 27 sigaddset(&block, SIGTERM); sigaddset(&block, SIGINT);
8755 27 sigaddset(&block, SIGHUP); sigaddset(&block, SIGUSR1);
8756 27 sigaddset(&block, SIGUSR2); sigaddset(&block, SIGCHLD);
8757 27 sigaddset(&block, SIGALRM);
8758 27 sigprocmask(SIG_BLOCK, &block, &orig);
8759  
8760 58 for (;;) {
8761 int st;
8762 pid_t pid;
8763  
8764 85 ▶ if (hm_sup_term) {
Branch: 1 branch
BranchTrueFalseTotal
if (hm_sup_term) { 27 58 85
8765 93 ▶ for (i = 0; i < hm_nchildren; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < hm_nchildren; i++) 66 27 93
8766 66 kill(hm_children[i], SIGTERM);
8767 /* workers self-bound their drain with shutdown_grace.
8768 * Waited for BY PID: a re-exec successor is a child of
8769 * this process too, and it does not exit - it is the
8770 * one that asked us to; waitpid(-1) would wait for it
8771 * for ever. */
8772 93 ▶ for (i = 0; i < hm_nchildren; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < hm_nchildren; i++) { 66 27 93
8773 /*
8774 # uncoverable branch true class:ignore_covered_err
8775 # uncoverable condition left count:1 class:ignore_covered_err
8776 # uncoverable condition left count:2 class:ignore_covered_err
8777 # uncoverable condition right count:2 class:ignore_covered_err */
8778 66 ▶ while (waitpid(hm_children[i], &st, 0) < 0 && errno == EINTR) { }
Branch: 2 branches
BranchTrueFalseTotal
while (waitpid(hm_children[i], &st, 0) < 0 && errno == EINTR) { } 0 66 66
while (waitpid(hm_children[i], &st, 0) < 0 && errno == EINTR) { } 0 0 0
8779 }
8780 27 break;
8781 }
8782  
8783 58 ▶ if (hm_sup_usr1) {
Branch: 1 branch
BranchTrueFalseTotal
if (hm_sup_usr1) { 5 53 58
8784 5 hm_sup_usr1 = 0;
8785 /* With a scoreboard the supervisor has every worker's
8786 * numbers already: one aggregate line and one per row,
8787 * a dead row marked. Without one, each worker prints its
8788 * own line as it always did. */
8789 5 ▶ if (hm_sa_board_live()) hm_sa_board_print(stderr);
Branch: 1 branch
BranchTrueFalseTotal
if (hm_sa_board_live()) hm_sa_board_print(stderr); 4 1 5
8790 else
8791 3 ▶ for (i = 0; i < hm_nchildren; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < hm_nchildren; i++) 2 1 3
8792 2 kill(hm_children[i], SIGUSR1);
8793 }
8794  
8795 58 ▶ if (hm_sup_tick) {
Branch: 1 branch
BranchTrueFalseTotal
if (hm_sup_tick) { 2 56 58
8796 2 hm_sup_tick = 0;
8797 /* The caller's own deadline, if it had one: take the pool
8798 * down and then let SIGALRM do what it would have done. */
8799 /*
8800 # uncoverable branch true class:ignore_covered_err
8801 # uncoverable condition left count:1 class:ignore_covered_err
8802 # uncoverable condition right count:1 class:ignore_covered_err
8803 # uncoverable condition left count:2 class:ignore_covered_err */
8804 2 ▶ if (hm_sup_alarm_at && time(NULL) >= hm_sup_alarm_at) {
Branch: 2 branches
BranchTrueFalseTotal
if (hm_sup_alarm_at && time(NULL) >= hm_sup_alarm_at) { 2 0 2
if (hm_sup_alarm_at && time(NULL) >= hm_sup_alarm_at) { 0 2 2
8805 0 hm_sup_alarmed = 1; /* # uncoverable statement */
8806 0 hm_sup_term = 1; /* # uncoverable statement */
8807 2 continue; /* # uncoverable statement class:ignore_covered_err */
8808 }
8809 2 hm_sup_watch_board();
8810 2 alarm(HM_SUP_TICK_SECS);
8811 }
8812  
8813 58 ▶ if (hm_sup_usr2) {
Branch: 1 branch
BranchTrueFalseTotal
if (hm_sup_usr2) { 5 53 58
8814 5 hm_sup_usr2 = 0;
8815 5 hm_reexec(aTHX_ cfg, &orig);
8816 }
8817  
8818 58 ▶ if (hm_sup_hup) {
Branch: 1 branch
BranchTrueFalseTotal
if (hm_sup_hup) { 2 56 58
8819 pid_t old[1024];
8820 time_t old_start[1024];
8821 int old_widx[1024];
8822 2 int nold = hm_nchildren, nfresh = 0;
8823 2 memcpy(old, hm_children, nold * sizeof(pid_t));
8824 2 memcpy(old_start, hm_child_start, nold * sizeof(time_t));
8825 2 memcpy(old_widx, hm_child_widx, nold * sizeof(int));
8826 2 hm_sup_hup = 0;
8827 2 hm_nchildren = 0;
8828 6 ▶ for (i = 0; i < workers; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < workers; i++) { 4 2 6
8829 /* a failed fork must never reach hm_children: -1 there
8830 * turns the TERM sweep below into kill(-1, SIGTERM),
8831 * which signals every process this uid owns. */
8832 4 pid_t pid = hm_spawn(aTHX_ cfg, i);
8833 /*
8834 # uncoverable branch true
8835 # uncoverable condition left count:1 class:ignore_covered_err */
8836 4 ▶ if (pid < 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (pid < 0) continue; 0 4 4
8837 4 hm_child_start[hm_nchildren] = time(NULL);
8838 4 hm_child_widx[hm_nchildren] = i;
8839 4 hm_children[hm_nchildren++] = pid;
8840 4 nfresh++;
8841 }
8842 /* retire one old worker per fresh one. Short of processes
8843 * the survivors stay in the live set, so a HUP under fork
8844 * pressure shrinks the pool instead of emptying it. */
8845 6 ▶ for (i = 0; i < nold; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nold; i++) { 4 2 6
8846 /*
8847 # uncoverable branch false
8848 # uncoverable condition right count:1 class:ignore_covered_err */
8849 4 ▶ if (i < nfresh) {
Branch: 1 branch
BranchTrueFalseTotal
if (i < nfresh) { 4 0 4
8850 4 kill(old[i], SIGTERM); /* drain gracefully */
8851 } else {
8852 0 hm_child_start[hm_nchildren] = old_start[i]; /* # uncoverable statement */
8853 0 hm_child_widx[hm_nchildren] = old_widx[i]; /* # uncoverable statement */
8854 0 hm_children[hm_nchildren++] = old[i]; /* # uncoverable statement */
8855 }
8856 }
8857 }
8858  
8859 /* reap every exited child without blocking, respawning the
8860 * live set. Recycled workers exiting are expected; only a
8861 * death in the live set is respawned. Abnormal quick deaths
8862 * (crash loops) back off exponentially; clean exits respawn
8863 * immediately. */
8864 73 ▶ while ((pid = waitpid(-1, &st, WNOHANG)) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while ((pid = waitpid(-1, &st, WNOHANG)) > 0) { 17 56 73
8865 17 int live = 0;
8866 17 int dead_widx = 0;
8867 17 time_t started = 0;
8868 17 ▶ if (pid == hm_resup_pid) {
Branch: 1 branch
BranchTrueFalseTotal
if (pid == hm_resup_pid) { 1 16 17
8869 /* It never took over (a successor that did would have
8870 * TERMed us first). Nothing here changed, so nothing
8871 * is undone: this pool serves on. Except the pid
8872 * file, which it may have written before it died,
8873 * and which would then name a dead process for the
8874 * next `kill -USR2 $(cat pidfile)`: re-asserted. */
8875 1 hm_errlog("Hyperman: re-exec successor %d exited (%s %d) "
8876 "before taking over; still serving", (int)pid,
8877 1 ▶ WIFEXITED(st) ? "status" : "signal", /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
WIFEXITED(st) ? "status" : "signal", /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 0 1
8878 1 ▶ WIFEXITED(st) ? WEXITSTATUS(st) : WTERMSIG(st)); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
WIFEXITED(st) ? WEXITSTATUS(st) : WTERMSIG(st)); /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 0 1
8879 1 hm_resup_pid = 0;
8880 1 (void)hm_write_pidfile_(aTHX_ cfg, 0);
8881 1 continue;
8882 }
8883 26 ▶ for (i = 0; i < hm_nchildren; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < hm_nchildren; i++) { 22 4 26
8884 22 ▶ if (hm_children[i] == pid) {
Branch: 1 branch
BranchTrueFalseTotal
if (hm_children[i] == pid) { 12 10 22
8885 12 started = hm_child_start[i];
8886 12 dead_widx = hm_child_widx[i];
8887 12 hm_nchildren--;
8888 12 hm_children[i] = hm_children[hm_nchildren];
8889 12 hm_child_start[i] = hm_child_start[hm_nchildren];
8890 12 hm_child_widx[i] = hm_child_widx[hm_nchildren];
8891 12 live = 1;
8892 12 break;
8893 }
8894 }
8895 16 ▶ if (!live) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!live) continue; 4 12 16
8896  
8897 {
8898 12 ▶ int crashed = !(WIFEXITED(st) && WEXITSTATUS(st) == 0); /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
int crashed = !(WIFEXITED(st) && WEXITSTATUS(st) == 0); /* # uncoverable condition left count:2 class:ignore_covered_err */ 8 4 12
int crashed = !(WIFEXITED(st) && WEXITSTATUS(st) == 0); /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 8 8
8899 12 ▶ if (crashed && time(NULL) - started < 5) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (crashed && time(NULL) - started < 5) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 8 12
if (crashed && time(NULL) - started < 5) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 0 4
8900 4 ▶ backoff = backoff ? backoff * 2 : 1; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
backoff = backoff ? backoff * 2 : 1; /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 4 4
8901 /*
8902 # uncoverable branch true class:ignore_covered_err
8903 # uncoverable condition left count:1 class:ignore_covered_err */
8904 4 ▶ if (backoff > 30) backoff = 30;
Branch: 1 branch
BranchTrueFalseTotal
if (backoff > 30) backoff = 30; 0 4 4
8905 /* keep TERM responsive during crash-loop backoff */
8906 4 sigprocmask(SIG_SETMASK, &orig, NULL);
8907 4 sleep((unsigned)backoff);
8908 4 sigprocmask(SIG_BLOCK, &block, NULL);
8909 /*
8910 # uncoverable branch false class:ignore_covered_err
8911 # uncoverable condition right count:1 class:ignore_covered_err
8912 # uncoverable condition left count:2 class:ignore_covered_err
8913 # uncoverable condition right count:2 class:ignore_covered_err */
8914 8 ▶ } else if (!crashed || time(NULL) - started >= 5) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (!crashed || time(NULL) - started >= 5) { 0 8 8
} else if (!crashed || time(NULL) - started >= 5) { 0 0 0
8915 8 backoff = 0;
8916 }
8917 }
8918 {
8919 /* The replacement takes the DEAD worker's index, not
8920 * the position it is about to occupy here.
8921 *
8922 * Those are different numbers, because the array above
8923 * is kept packed by swapping the last entry down: kill
8924 * the worker at position 0 of three and the survivor
8925 * from position 2 moves into it, so the next free
8926 * position is 2 - an index a LIVE worker still holds.
8927 * Spawned with that, the replacement asked for a waker
8928 * slot its own sibling owned, was refused, and ran
8929 * deaf to the bus for the rest of its life while the
8930 * slot its predecessor had died holding went spare. It
8931 * was pinned to that sibling's CPU too.
8932 *
8933 * So identity comes from the child that left, and only
8934 * the bookkeeping position is reused. */
8935 12 int slot = hm_nchildren;
8936 12 pid_t fresh = hm_spawn(aTHX_ cfg, dead_widx);
8937 /*
8938 # uncoverable branch false class:ignore_covered_err
8939 # uncoverable condition right count:1 class:ignore_covered_err */
8940 12 ▶ if (fresh > 0) { /* never record a -1 */
Branch: 1 branch
BranchTrueFalseTotal
if (fresh > 0) { /* never record a -1 */ 12 0 12
8941 12 hm_child_start[slot] = time(NULL);
8942 12 hm_child_widx[slot] = dead_widx;
8943 12 hm_children[slot] = fresh;
8944 12 hm_nchildren = slot + 1;
8945 }
8946 }
8947 12 ▶ if (hm_sup_term) break; /* TERM arrived during backoff */
Branch: 1 branch
BranchTrueFalseTotal
if (hm_sup_term) break; /* TERM arrived during backoff */ 2 10 12
8948 }
8949  
8950 58 ▶ if (hm_sup_term) continue; /* serviced at the top of the loop */
Branch: 1 branch
BranchTrueFalseTotal
if (hm_sup_term) continue; /* serviced at the top of the loop */ 2 56 58
8951  
8952 /* atomically unblock and sleep until the next signal */
8953 56 sigsuspend(&orig);
8954 }
8955  
8956 27 sigprocmask(SIG_SETMASK, &orig, NULL);
8957 27 hm_unlink_pidfile(cfg->pid_path);
8958  
8959 /* The pool is down because the caller's own alarm came due, so
8960 * finish the job the alarm was set to do. */
8961 /*
8962 # uncoverable branch true class:ignore_covered_err
8963 # uncoverable condition left count:1 class:ignore_covered_err */
8964 27 ▶ if (hm_sup_alarmed) {
Branch: 1 branch
BranchTrueFalseTotal
if (hm_sup_alarmed) { 0 27 27
8965 0 signal(SIGALRM, SIG_DFL); /* # uncoverable statement */
8966 0 raise(SIGALRM); /* # uncoverable statement */
8967 }
8968 }
8969 }
8970 #endif /* !_WIN32 */
8971 }
8972  
8973 /* loop object <-> pointer */
8974 3042 static hm_loop *hm_loop_from_sv(pTHX_ SV *sv) {
8975 /*
8976 # uncoverable condition left count:1 class:ignore_covered_err
8977 # uncoverable condition left count:3 class:ignore_covered_err */
8978 3042 ▶ if (!(sv && SvROK(sv) && sv_derived_from(sv, "Hyperman::Loop")))
Branch: 3 branches
BranchTrueFalseTotal
if (!(sv && SvROK(sv) && sv_derived_from(sv, "Hyperman::Loop"))) 3042 0 3042
if (!(sv && SvROK(sv) && sv_derived_from(sv, "Hyperman::Loop"))) 3041 1 3042
if (!(sv && SvROK(sv) && sv_derived_from(sv, "Hyperman::Loop"))) 0 3041 3041
8979 1 croak("not a Hyperman::Loop");
8980 3041 return INT2PTR(hm_loop *, SvIV(SvRV(sv)));
8981 }
8982  
8983 /* The Perl wrapper for a loop. ONE blessed referent per loop, however many
8984 * times this is asked: Hyperman::Loop->new, psgix.loop, Hyperman->loop and
8985 * the ABI's sv_of_loop used to each mint their own, and the first wrapper
8986 * whose refcount hit zero freed a Perl-owned loop under every other one.
8987 * The loop keeps a WEAK copy, so holding the wrapper is what keeps the loop,
8988 * and the loop holding its own wrapper keeps nothing alive. */
8989 928 static SV *hm_loop_to_sv(pTHX_ hm_loop *loop) {
8990 SV *rv;
8991 928 ▶ if (loop->self_sv && SvROK(loop->self_sv)) return newSVsv(loop->self_sv);
Branch: 2 branches
BranchTrueFalseTotal
if (loop->self_sv && SvROK(loop->self_sv)) return newSVsv(loop->self_sv); 803 125 928
if (loop->self_sv && SvROK(loop->self_sv)) return newSVsv(loop->self_sv); 14 789 803
8992 914 rv = newRV_noinc(newSViv(PTR2IV(loop)));
8993 914 sv_bless(rv, gv_stashpv("Hyperman::Loop", GV_ADD));
8994 914 ▶ if (loop->self_sv) SvREFCNT_dec(loop->self_sv); /* a cleared weak ref */
Branch: 1 branch
BranchTrueFalseTotal
if (loop->self_sv) SvREFCNT_dec(loop->self_sv); /* a cleared weak ref */ 789 125 914
8995 914 loop->self_sv = newSVsv(rv);
8996 914 sv_rvweaken(loop->self_sv);
8997 914 return rv;
8998 }
8999  
9000 /* install_await: $Hyperman::Future::AWAIT pumps this loop */
9001 XS_INTERNAL(hm_xs_await_cb);
9002 1 XS_INTERNAL(hm_xs_await_cb) {
9003 1 dXSARGS;
9004 1 hm_clos *cl = hm_clos_of(aTHX_ cv);
9005 /*
9006 # uncoverable branch true class:ignore_covered_err
9007 # uncoverable condition left count:1 class:ignore_covered_err
9008 # uncoverable condition left count:2 class:ignore_covered_err */
9009 1 ▶ if (!cl || items < 1) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!cl || items < 1) XSRETURN_EMPTY; 1 0 1
if (!cl || items < 1) XSRETURN_EMPTY; 0 1 1
9010 1 hm_loop_run(aTHX_ hm_loop_from_sv(aTHX_ cl->a), ST(0));
9011 1 XSRETURN_EMPTY;
9012 }
9013  
9014 /* A run() option that is a count: a non-negative number, never a string
9015 * that is not one ("10M" read as 10) and never a negative ("-1" read as
9016 * UV_MAX, which for max_body was an unbounded request buffer). */
9017 40 static UV hm_opt_count(pTHX_ SV *val, const char *key) {
9018 /*
9019 # uncoverable condition left count:1 class:ignore_covered_err
9020 # uncoverable condition left count:2 class:ignore_covered_err
9021 # uncoverable condition right count:4 class:ignore_covered_err
9022 # uncoverable condition right count:5 class:ignore_covered_err */
9023 40 ▶ if (!SvOK(val) || SvROK(val)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(val) || SvROK(val) 40 0 40
if (!SvOK(val) || SvROK(val) 40 0 40
9024 40 ▶ || (!SvIOK(val) && !SvNOK(val) && !looks_like_number(val))
Branch: 3 branches
BranchTrueFalseTotal
|| (!SvIOK(val) && !SvNOK(val) && !looks_like_number(val)) 4 36 40
|| (!SvIOK(val) && !SvNOK(val) && !looks_like_number(val)) 4 0 4
|| (!SvIOK(val) && !SvNOK(val) && !looks_like_number(val)) 0 4 4
9025 36 ▶ || SvNV(val) < 0)
Branch: 1 branch
BranchTrueFalseTotal
|| SvNV(val) < 0) 4 32 36
9026 16 ▶ croak("Hyperman->run: %s must be a non-negative number, not '%s'",
Branch: 1 branch
BranchTrueFalseTotal
croak("Hyperman->run: %s must be a non-negative number, not '%s'", 8 0 8
9027 key, SvOK(val) ? SvPV_nolen(val) : "undef"); /* # uncoverable condition right count:1 class:ignore_covered_err */
9028 32 return SvUV(val);
9029 }
9030  
9031 #endif /* HM_CORE_H */
« include/hyperman/hm_future.h ↑ Summary include/hyperman/hm_compress.h »