xs/hyperman.xs

filter: Statementstmt 99.0% 314 / 317 Branchbran 79.4% 296 / 374 (4 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 88.4% 610 / 691 (4 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/hyperman/hm_sa_bus.h ↑ Summary include/hyperman/hm_compat.h »
1 MODULE = Hyperman PACKAGE = Hyperman
2  
3 PROTOTYPES: DISABLE
4  
5 BOOT:
6 149 hm_fq = newAV();
7 149 hm_boot_argv_save(aTHX);
8 /* Shared::Arena's table, once. NULL when it is absent or too old, and
9 * every arena door then fails open. */
10 149 hm_sa_resolve(aTHX);
11  
12 # Run the server. Key/value options as documented in Hyperman.pm:
13 # app (required), host, port (scalar or arrayref), listen (arrayref of
14 # per-listener hashrefs), workers, idle_timeout, header_timeout, max_pipeline,
15 # reuseport, access_log, max_requests_per_worker, shutdown_grace, affinity,
16 # http2, tls_*, redirect_https.
17 void
18 run(class, ...)
19 SV *class
20 CODE:
21 {
22 hm_worker_cfg cfg;
23 hm_listener_spec dfl; /* top-level defaults, seeded into each spec */
24 161 SV *port_sv = NULL, *listen_sv = NULL;
25 int i;
26 PERL_UNUSED_VAR(class);
27  
28 161 ▶ if ((items - 1) % 2)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 1) % 2) 1 160 161
29 1 croak("Hyperman->run: odd number of options");
30  
31 160 memset(&cfg, 0, sizeof(cfg));
32 160 cfg.log_fd = -1;
33 160 cfg.err_fd = -1;
34 160 cfg.http3_max_conns = 1024; /* an explicit 0 lifts the cap */
35 /* -1 is "the caller said nothing", which is NOT the same as
36 * completion => 0. The memset above would otherwise read as an
37 * explicit request to turn completion-mode reads off on every
38 * server that never mentions them. */
39 160 cfg.completion = -1;
40 160 memset(&dfl, 0, sizeof(dfl));
41 160 dfl.host = "0.0.0.0";
42 160 dfl.port = 8080;
43  
44 813 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 685 128 813
45 685 const char *key = SvPV_nolen(ST(i));
46 685 SV *val = ST(i + 1);
47 685 ▶ if (strEQ(key, "app")) cfg.app = val;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(key, "app")) cfg.app = val; 159 526 685
48 616 ▶ # uncoverable branch true count:3 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch true count:3 class:ignore_covered_err 90 436 526
# uncoverable branch true count:3 class:ignore_covered_err 90 0 90
49 436 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 115 321 436
50 321 ▶ else if (strEQ(key, "host")) { if (SvOK(val)) dfl.host = SvPV_nolen(val); }
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(key, "host")) { if (SvOK(val)) dfl.host = SvPV_nolen(val); } 17 304 321
else if (strEQ(key, "host")) { if (SvOK(val)) dfl.host = SvPV_nolen(val); } 17 0 17
51 304 ▶ else if (strEQ(key, "port")) port_sv = val;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "port")) port_sv = val; 99 205 304
52 205 ▶ else if (strEQ(key, "listen")) listen_sv = SvOK(val) ? val : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "listen")) listen_sv = SvOK(val) ? val : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 204 205
53 204 ▶ else if (strEQ(key, "workers")) cfg.nworkers = (int)SvIV(val);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "workers")) cfg.nworkers = (int)SvIV(val); 4 200 204
54 200 ▶ else if (strEQ(key, "idle_timeout")) cfg.idle_t = SvNV(val);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "idle_timeout")) cfg.idle_t = SvNV(val); 3 197 200
55 197 ▶ else if (strEQ(key, "header_timeout")) cfg.header_t = SvNV(val);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "header_timeout")) cfg.header_t = SvNV(val); 1 196 197
56 196 ▶ else if (strEQ(key, "body_timeout")) cfg.body_t = SvNV(val);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "body_timeout")) cfg.body_t = SvNV(val); 3 193 196
57 193 ▶ else if (strEQ(key, "request_timeout")) cfg.request_t = SvNV(val);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "request_timeout")) cfg.request_t = SvNV(val); 2 191 193
58 191 ▶ else if (strEQ(key, "body_min_rate")) cfg.body_min_rate = hm_opt_count(aTHX_ val, key);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "body_min_rate")) cfg.body_min_rate = hm_opt_count(aTHX_ val, key); 6 185 191
59 else if (strEQ(key, "max_pipeline")) cfg.max_pipe = (int)SvIV(val);
60 else if (strEQ(key, "reuseport")) cfg.reuseport = SvTRUE(val) ? 1 : 0;
61 /* Completion-mode reads. On by default wherever the backend can
62 * do them - which is io_uring, and only when it was asked for by
63 185 ▶ * name - so this is here to turn them OFF. Inert everywhere else:
Branch: 1 branch
BranchTrueFalseTotal
* name - so this is here to turn them OFF. Inert everywhere else: 1 184 185
64 184 ▶ * a backend that cannot submit reads simply ignores it. */
Branch: 3 branches
BranchTrueFalseTotal
* a backend that cannot submit reads simply ignores it. */ 3 181 184
* a backend that cannot submit reads simply ignores it. */ 3 0 3
* a backend that cannot submit reads simply ignores it. */ 3 0 3
65 181 ▶ else if (strEQ(key, "completion")) cfg.completion = SvTRUE(val) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "completion")) cfg.completion = SvTRUE(val) ? 1 : 0; 1 180 181
66 180 ▶ # uncoverable branch true count:3 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true count:3 class:ignore_covered_err 1 179 180
67 # uncoverable condition right count:1 class:ignore_covered_err
68 # uncoverable condition right count:2 class:ignore_covered_err
69 else if (strEQ(key, "deny")) { if (SvOK(val) && SvROK(val)) cfg.deny = val; }
70 else if (strEQ(key, "deny_capacity")) cfg.deny_cap = (unsigned)SvUV(val);
71 179 ▶ else if (strEQ(key, "rate_capacity")) cfg.rate_cap = (unsigned)SvUV(val);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "rate_capacity")) cfg.rate_cap = (unsigned)SvUV(val); 1 178 179
72 178 ▶ /* The message bus. `bus_slots` is headroom before a slow worker
Branch: 1 branch
BranchTrueFalseTotal
/* The message bus. `bus_slots` is headroom before a slow worker 1 177 178
73 177 ▶ * is lapped; `bus_slot_size` is the largest message that will be
Branch: 1 branch
BranchTrueFalseTotal
* is lapped; `bus_slot_size` is the largest message that will be 1 176 177
74 * accepted. Two knobs because they answer two different
75 * complaints. */
76 else if (strEQ(key, "bus_slots")) cfg.bus_slots = (unsigned)SvUV(val);
77 176 ▶ else if (strEQ(key, "bus_slot_size")) cfg.bus_slot_size = (unsigned)SvUV(val);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "bus_slot_size")) cfg.bus_slot_size = (unsigned)SvUV(val); 6 170 176
78 12 ▶ else if (strEQ(key, "bus_groups")) cfg.bus_groups = (unsigned)SvUV(val);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "bus_groups")) cfg.bus_groups = (unsigned)SvUV(val); 6 0 6
79 /* The Shared::Arena. A name makes it attachable by another
80 170 ▶ * process and survives a restart; the size is computed from the
Branch: 1 branch
BranchTrueFalseTotal
* process and survives a restart; the size is computed from the 2 168 170
81 * tenants unless overridden. */
82 else if (strEQ(key, "arena_name")) {
83 # uncoverable branch false class:ignore_covered_err
84 168 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 2 166 168
85 166 ▶ if (SvOK(val)) cfg.arena_name = SvPV(val, cfg.arena_name_len);
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(val)) cfg.arena_name = SvPV(val, cfg.arena_name_len); 17 149 166
86 }
87 else if (strEQ(key, "arena_size")) cfg.arena_size = SvUV(val);
88 17 ▶ /* Count distinct peers at accept. Off by default: it is a hash
Branch: 1 branch
BranchTrueFalseTotal
/* Count distinct peers at accept. Off by default: it is a hash 1 16 17
89 1 * and a CAS-max on the accept path, about 20ns, and this server
90 16 ▶ * promises that path costs nothing it did not ask for. */
Branch: 2 branches
BranchTrueFalseTotal
* promises that path costs nothing it did not ask for. */ 4 12 16
* promises that path costs nothing it did not ask for. */ 1 3 4
91 1 else if (strEQ(key, "distinct_clients")) cfg.distinct_clients = SvTRUE(val) ? 1 : 0;
92 else if (strEQ(key, "access_log")) {
93 3 ▶ /* coderef -> per-request Perl callback (as before);
Branch: 1 branch
BranchTrueFalseTotal
/* coderef -> per-request Perl callback (as before); 3 0 3
94 18 ▶ * a filehandle or a path -> fast C-side Combined-log writer. */
Branch: 1 branch
BranchTrueFalseTotal
* a filehandle or a path -> fast C-side Combined-log writer. */ 3 12 15
95 15 ▶ if (!SvOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(val)) { 3 12 15
96 3 ▶ cfg.log_cb = NULL;
Branch: 4 branches
BranchTrueFalseTotal
cfg.log_cb = NULL; 3 0 3
cfg.log_cb = NULL; 0 3 3
cfg.log_cb = NULL; 0 0 0
cfg.log_cb = NULL; 3 0 3
97 3 ▶ } else if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 2 1 3
} else if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) { 2 0 2
98 3 ▶ cfg.log_cb = val;
Branch: 1 branch
BranchTrueFalseTotal
cfg.log_cb = val; 2 1 3
99 3 ▶ } else {
Branch: 1 branch
BranchTrueFalseTotal
} else { 1 2 3
100 1 SV *g = (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVGV)
101 2 ? SvRV(val) : val;
102 2 ▶ if (SvTYPE(g) == SVt_PVGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(g) == SVt_PVGV) { 1 1 2
103 1 IO *io = GvIO((GV *)g);
104 PerlIO *fp = io ? (IoOFP(io) ? IoOFP(io) : IoIFP(io)) : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */
105 int raw = fp ? PerlIO_fileno(fp) : -1;
106 1 if (raw < 0)
107 croak("Hyperman->run: access_log handle has no fileno");
108 cfg.log_fd = dup(raw); /* own it; freed with the loop */
109 12 # uncoverable branch true class:ignore_covered_err
110 # uncoverable condition left count:1 class:ignore_covered_err
111 if (cfg.log_fd < 0)
112 croak("Hyperman->run: dup access_log fd: %s", /* # uncoverable statement class:ignore_covered_err */
113 158 ▶ strerror(errno)); /* # uncoverable statement class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
strerror(errno)); /* # uncoverable statement class:ignore_covered_err */ 9 140 149
strerror(errno)); /* # uncoverable statement class:ignore_covered_err */ 9 0 9
114 142 ▶#ifndef _WIN32
Branch: 2 branches
BranchTrueFalseTotal
#ifndef _WIN32 2 138 140
#ifndef _WIN32 2 0 2
115 151 ▶ (void)fcntl(cfg.log_fd, F_SETFD, FD_CLOEXEC);
Branch: 2 branches
BranchTrueFalseTotal
(void)fcntl(cfg.log_fd, F_SETFD, FD_CLOEXEC); 13 125 138
(void)fcntl(cfg.log_fd, F_SETFD, FD_CLOEXEC); 13 0 13
116 125 ▶#endif
Branch: 1 branch
BranchTrueFalseTotal
#endif 8 117 125
117 117 ▶ } else {
Branch: 1 branch
BranchTrueFalseTotal
} else { 2 115 117
118 cfg.log_path = SvPV_nolen(val); /* opened in the parent */
119 }
120 2 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 2 0 2
121 2 ▶ }
Branch: 2 branches
BranchTrueFalseTotal
} 1 1 2
} 0 1 1
122 # uncoverable branch true count:3 class:ignore_covered_err
123 # uncoverable condition right count:2 class:ignore_covered_err
124 1 else if (strEQ(key, "user")) { if (SvOK(val)) cfg.drop_user = SvPV_nolen(val); }
125 1 # uncoverable branch true count:3 class:ignore_covered_err
126 1 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 1 0 1
# uncoverable condition right count:2 class:ignore_covered_err 1 0 1
127 1 else if (strEQ(key, "group")) { if (SvOK(val)) cfg.drop_group = SvPV_nolen(val); }
128 # uncoverable branch true count:3 class:ignore_covered_err
129 1 # uncoverable condition right count:2 class:ignore_covered_err
130 else if (strEQ(key, "pid_file")) { if (SvOK(val)) cfg.pid_path = SvPV_nolen(val); }
131 else if (strEQ(key, "listen_fds")) cfg.listen_fds = SvTRUE(val) ? 1 : 0;
132 115 ▶ else if (strEQ(key, "umask")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "umask")) { 9 106 115
133 18 ▶ /* a number as perl parsed it (0077 is already 63), or a string
Branch: 1 branch
BranchTrueFalseTotal
/* a number as perl parsed it (0077 is already 63), or a string 9 0 9
134 * of octal digits as a shell would write it ("077") */
135 106 ▶ # uncoverable branch false class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false class:ignore_covered_err 15 91 106
136 # uncoverable condition right count:1 class:ignore_covered_err
137 if (SvOK(val)) {
138 if (SvIOK(val) || SvNOK(val)) cfg.umask_val = (unsigned)SvUV(val); /* # uncoverable condition left count:2 class:ignore_covered_err */
139 15 ▶ else {
Branch: 1 branch
BranchTrueFalseTotal
else { 15 0 15
140 3 ▶ char *end;
Branch: 1 branch
BranchTrueFalseTotal
char *end; 3 0 3
141 18 ▶ const char *s = SvPV_nolen(val);
Branch: 1 branch
BranchTrueFalseTotal
const char *s = SvPV_nolen(val); 3 12 15
142 15 ▶ cfg.umask_val = (unsigned)strtoul(s, &end, 8);
Branch: 1 branch
BranchTrueFalseTotal
cfg.umask_val = (unsigned)strtoul(s, &end, 8); 3 12 15
143 3 ▶ # uncoverable branch false class:ignore_covered_err
Branch: 4 branches
BranchTrueFalseTotal
# uncoverable branch false class:ignore_covered_err 3 0 3
# uncoverable branch false class:ignore_covered_err 0 3 3
# uncoverable branch false class:ignore_covered_err 0 0 0
# uncoverable branch false class:ignore_covered_err 3 0 3
144 3 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 2 1 3
# uncoverable condition left count:1 class:ignore_covered_err 2 0 2
145 3 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 2 1 3
146 3 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 1 2 3
147 1 if (*s == '\0' || *end != '\0')
148 2 croak("Hyperman->run: umask '%s' is not an octal number", s);
149 2 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 1 1 2
150 1 cfg.umask_set = 1;
151 }
152 1 }
153 else if (strEQ(key, "access_log_format")) {
154 # uncoverable branch false class:ignore_covered_err
155 12 # uncoverable condition right count:1 class:ignore_covered_err
156 if (SvOK(val)) cfg.log_format = SvPV_nolen(val);
157 }
158 else if (strEQ(key, "error_log")) {
159 91 ▶ /* a filehandle or a path; the server's own messages go there
Branch: 1 branch
BranchTrueFalseTotal
/* a filehandle or a path; the server's own messages go there 1 90 91
160 1 * instead of stderr. psgi.errors is the application's and is
161 * not moved. */
162 # uncoverable branch false class:ignore_covered_err
163 # uncoverable condition right count:1 class:ignore_covered_err
164 90 ▶ if (SvOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(val)) { 5 85 90
165 85 ▶ SV *g = (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVGV)
Branch: 1 branch
BranchTrueFalseTotal
SV *g = (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVGV) 2 83 85
166 2 ? SvRV(val) : val;
167 if (SvTYPE(g) == SVt_PVGV) {
168 IO *io = GvIO((GV *)g);
169 PerlIO *fp = io ? (IoOFP(io) ? IoOFP(io) : IoIFP(io)) : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */
170 int raw = fp ? PerlIO_fileno(fp) : -1;
171 if (raw < 0)
172 croak("Hyperman->run: error_log handle has no fileno");
173 83 ▶ cfg.err_fd = dup(raw);
Branch: 1 branch
BranchTrueFalseTotal
cfg.err_fd = dup(raw); 32 51 83
174 32 # uncoverable branch true class:ignore_covered_err
175 28 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 16 12 28
176 16 if (cfg.err_fd < 0)
177 croak("Hyperman->run: dup error_log fd: %s", strerror(errno)); /* # uncoverable statement class:ignore_covered_err */
178 #ifndef _WIN32
179 12 (void)fcntl(cfg.err_fd, F_SETFD, FD_CLOEXEC);
180 #endif
181 51 ▶ } else {
Branch: 1 branch
BranchTrueFalseTotal
} else { 2 49 51
182 2 cfg.err_path = SvPV_nolen(val);
183 2 ▶ }
Branch: 2 branches
BranchTrueFalseTotal
} 2 0 2
} 1 1 2
184 1 }
185 1 }
186 else if (strEQ(key, "max_requests_per_worker"))
187 49 ▶ cfg.max_requests = hm_opt_count(aTHX_ val, key);
Branch: 1 branch
BranchTrueFalseTotal
cfg.max_requests = hm_opt_count(aTHX_ val, key); 5 44 49
188 44 ▶ /* Response compression. Off by default: a server that starts
Branch: 1 branch
BranchTrueFalseTotal
/* Response compression. Off by default: a server that starts 1 43 44
189 43 ▶ * compressing on upgrade is a surprise. Accepted and inert on a
Branch: 1 branch
BranchTrueFalseTotal
* compressing on upgrade is a surprise. Accepted and inert on a 1 42 43
190 42 ▶ * build without zlib, so a config is portable across them. */
Branch: 1 branch
BranchTrueFalseTotal
* build without zlib, so a config is portable across them. */ 2 40 42
191 40 ▶ else if (strEQ(key, "compress")) cfg.compress = SvTRUE(val) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "compress")) cfg.compress = SvTRUE(val) ? 1 : 0; 0 40 40
192 40 ▶ else if (strEQ(key, "compress_min_length"))
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "compress_min_length")) 2 38 40
193 51 ▶ cfg.compress_min = (size_t)hm_opt_count(aTHX_ val, key);
Branch: 2 branches
BranchTrueFalseTotal
cfg.compress_min = (size_t)hm_opt_count(aTHX_ val, key); 13 25 38
cfg.compress_min = (size_t)hm_opt_count(aTHX_ val, key); 13 0 13
194 37 ▶ /* The request ceiling: headers plus body, buffered per
Branch: 2 branches
BranchTrueFalseTotal
/* The request ceiling: headers plus body, buffered per 12 13 25
/* The request ceiling: headers plus body, buffered per 12 0 12
195 15 ▶ * connection before the app is called. This is the only thing
Branch: 2 branches
BranchTrueFalseTotal
* connection before the app is called. This is the only thing 2 11 13
* connection before the app is called. This is the only thing 2 0 2
196 11 ▶ * bounding a worker's memory against a large POST, so 0 is
Branch: 2 branches
BranchTrueFalseTotal
* bounding a worker's memory against a large POST, so 0 is 4 7 11
* bounding a worker's memory against a large POST, so 0 is 4 0 4
197 7 ▶ * REFUSED rather than read as "unlimited" - an unbounded read
Branch: 1 branch
BranchTrueFalseTotal
* REFUSED rather than read as "unlimited" - an unbounded read 1 6 7
198 6 ▶ * ceiling is a memory-exhaustion switch and must not be
Branch: 1 branch
BranchTrueFalseTotal
* ceiling is a memory-exhaustion switch and must not be 5 1 6
199 5 * reachable by a config typo that produces a falsy value. */
200 5 ▶ else if (strEQ(key, "max_body")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "max_body")) { 3 2 5
201 2 ▶ UV n = hm_opt_count(aTHX_ val, key); /* -1 was UV_MAX: unbounded */
Branch: 1 branch
BranchTrueFalseTotal
UV n = hm_opt_count(aTHX_ val, key); /* -1 was UV_MAX: unbounded */ 0 2 2
202 2 ▶ if (!n)
Branch: 1 branch
BranchTrueFalseTotal
if (!n) 1 1 2
203 1 croak("Hyperman->run: max_body must be a byte count - "
204 "0 would mean an unbounded request buffer, which "
205 1 "is a memory-exhaustion switch, not a setting");
206 cfg.max_read = (size_t)n;
207 128 ▶ }
Branch: 3 branches
BranchTrueFalseTotal
} 127 1 128
} 127 0 127
} 0 127 127
208 1 else if (strEQ(key, "compress_level")) {
209 127 ▶ IV lv = SvIV(val);
Branch: 1 branch
BranchTrueFalseTotal
IV lv = SvIV(val); 1 126 127
210 if (lv < 1 || lv > 9) /* # uncoverable condition left count:1 class:ignore_covered_err */
211 croak("Hyperman->run: compress_level must be 1..9");
212 cfg.compress_level = (int)lv;
213 }
214 else if (strEQ(key, "shutdown_grace")) cfg.grace = SvNV(val);
215 else if (strEQ(key, "affinity")) cfg.affinity = SvTRUE(val) ? 1 : 0;
216 else if (strEQ(key, "http2")) dfl.http2 = SvTRUE(val) ? 1 : 0;
217 else if (strEQ(key, "http3")) dfl.http3 = SvTRUE(val) ? 1 : 0;
218 17 ▶ # uncoverable branch true class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true class:ignore_covered_err 16 1 17
219 16 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 16 0 16
220 144 ▶ else if (strEQ(key, "proxy_protocol")) dfl.proxy_protocol = SvTRUE(val) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "proxy_protocol")) dfl.proxy_protocol = SvTRUE(val) ? 1 : 0; 17 110 127
221 104 ▶ else if (strEQ(key, "http3_max_conns")) cfg.http3_max_conns = hm_opt_count(aTHX_ val, key);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "http3_max_conns")) cfg.http3_max_conns = hm_opt_count(aTHX_ val, key); 2 102 104
222 2 ▶ else if (strEQ(key, "tls_cert")) dfl.tls_cert = SvOK(val) ? SvPV_nolen(val) : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "tls_cert")) dfl.tls_cert = SvOK(val) ? SvPV_nolen(val) : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 0 2
223 231 ▶ else if (strEQ(key, "tls_key")) dfl.tls_key = SvOK(val) ? SvPV_nolen(val) : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(key, "tls_key")) dfl.tls_key = SvOK(val) ? SvPV_nolen(val) : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 104 23 127
224 else if (strEQ(key, "tls_ca")) dfl.tls_ca = SvOK(val) ? SvPV_nolen(val) : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */
225 127 ▶ else if (strEQ(key, "tls_sni")) dfl.tls_sni = SvOK(val) ? val : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(key, "tls_sni")) dfl.tls_sni = SvOK(val) ? val : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 17 110 127
else if (strEQ(key, "tls_sni")) dfl.tls_sni = SvOK(val) ? val : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 16 17
226 1 else if (strEQ(key, "redirect_https")) dfl.redirect_https = (int)SvIV(val);
227 else if (strEQ(key, "tls_verify")) {
228 16 const char *m = SvPV_nolen(val);
229 254 ▶ if (strEQ(m, "require")) dfl.tls_verify = 2;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(m, "require")) dfl.tls_verify = 2; 16 110 126
230 112 ▶ # uncoverable branch true class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true class:ignore_covered_err 2 108 110
231 126 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 1 125 126
232 125 else if (strEQ(m, "optional")) dfl.tls_verify = 1;
233 else if (strEQ(m, "none")) dfl.tls_verify = 0;
234 247 ▶ else croak("Hyperman->run: tls_verify must be none/optional/require");
Branch: 1 branch
BranchTrueFalseTotal
else croak("Hyperman->run: tls_verify must be none/optional/require"); 133 114 247
235 133 }
236 133 ▶ else croak("Hyperman->run: unknown option '%s'", key);
Branch: 1 branch
BranchTrueFalseTotal
else croak("Hyperman->run: unknown option '%s'", key); 23 110 133
237 23 }
238 23 ▶ # uncoverable condition left count:2 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition left count:2 class:ignore_covered_err 23 0 23
# uncoverable condition left count:2 class:ignore_covered_err 22 1 23
# uncoverable condition left count:2 class:ignore_covered_err 0 22 22
239 1 # uncoverable condition left count:3 class:ignore_covered_err
240 22 if (!(cfg.app && SvROK(cfg.app) && SvTYPE(SvRV(cfg.app)) == SVt_PVCV))
241 110 ▶ croak("Hyperman->run: 'app' is required");
Branch: 1 branch
BranchTrueFalseTotal
croak("Hyperman->run: 'app' is required"); 2 108 110
242 2 if (dfl.redirect_https == 1) dfl.redirect_https = 443;
243 2 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
2 0 2
244 108 ▶ /* Build the listener specs. Three shapes, most specific wins:
Branch: 2 branches
BranchTrueFalseTotal
/* Build the listener specs. Three shapes, most specific wins: 102 6 108
/* Build the listener specs. Three shapes, most specific wins: 102 0 102
245 102 * listen => [ {..}, .. ] - full per-listener control (incl. TLS)
246 * port => [ N, M ] - several plain listeners sharing defaults
247 129 * port => N (or unset) - one listener from the top-level options */
248 {
249 hm_listener_spec *ls;
250 114 int n, j;
251 114 AV *lav = (listen_sv && SvROK(listen_sv)
252 114 && SvTYPE(SvRV(listen_sv)) == SVt_PVAV)
253 77 ? (AV *)SvRV(listen_sv) : NULL;
254 AV *pav = (port_sv && SvROK(port_sv)
255 && SvTYPE(SvRV(port_sv)) == SVt_PVAV)
256 ? (AV *)SvRV(port_sv) : NULL;
257  
258 if (listen_sv && !lav)
259 croak("Hyperman->run: 'listen' must be an arrayref of hashrefs");
260  
261 n = lav ? (int)(av_len(lav) + 1)
262 : pav ? (int)(av_len(pav) + 1)
263 : 1;
264 if (n < 1) croak("Hyperman->run: 'listen'/'port' is empty");
265 ls = (hm_listener_spec *)hm_xcalloc(n, sizeof(hm_listener_spec));
266  
267 for (j = 0; j < n; j++) {
268 ls[j] = dfl; /* seed with top-level defaults */
269 if (lav) {
270 SV **e = av_fetch(lav, j, 0);
271 # uncoverable condition left count:1 class:ignore_covered_err
272 # uncoverable condition left count:3 class:ignore_covered_err
273 if (!(e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV))
274 croak("Hyperman->run: listen[%d] must be a hashref", j);
275 hm_listener_from_hv(aTHX_ (HV *)SvRV(*e), &ls[j]);
276 } else if (pav) {
277 SV **e = av_fetch(pav, j, 0);
278 # uncoverable branch false class:ignore_covered_err
279 # uncoverable condition right count:1 class:ignore_covered_err
280 if (e) ls[j].port = (int)SvIV(*e);
281 } else if (port_sv && SvOK(port_sv)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
282 ls[j].port = (int)SvIV(port_sv);
283 }
284 hm_check_listener(aTHX_ &ls[j]);
285 }
286  
287 cfg.lspecs = ls;
288 cfg.nlspecs = n;
289 hm_run_server(aTHX_ &cfg); /* normally blocks until shutdown */
290 free(ls);
291 }
292 }
293  
294 # A Future resolved after $secs by the worker loop's timer watcher.
295 SV *
296 timer(class, secs)
297 SV *class
298 double secs
299 CODE:
300 {
301 6 hm_loop *loop = hm_need_loop();
302 6 SV *f = hmf_new(aTHX_ "Hyperman::Future");
303 PERL_UNUSED_VAR(class);
304 6 hm_add_timer_watch(aTHX_ loop, secs, f, HM_TW_FUTURE);
305 6 RETVAL = f;
306 }
307 OUTPUT:
308 RETVAL
309  
310 # A Future resolved when $fh becomes readable ('r') or writable ('w'),
311 # so an app can do non-blocking I/O integrated with the worker's loop.
312 SV *
313 io_ready(class, fh, ...)
314 SV *class
315 SV *fh
316 CODE:
317 {
318 3 hm_loop *loop = hm_need_loop();
319 3 ▶ const char *mode = (items > 2 && SvOK(ST(2))) ? SvPV_nolen(ST(2)) : "r"; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
const char *mode = (items > 2 && SvOK(ST(2))) ? SvPV_nolen(ST(2)) : "r"; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 1 2
const char *mode = (items > 2 && SvOK(ST(2))) ? SvPV_nolen(ST(2)) : "r"; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
320 2 SV *f = hmf_new(aTHX_ "Hyperman::Future");
321 PERL_UNUSED_VAR(class);
322 2 hm_add_io_watch(aTHX_ loop, hm_fd_of(aTHX_ fh), mode, f, 0);
323 1 RETVAL = f;
324 }
325 OUTPUT:
326 RETVAL
327  
328 # Per-worker stats: requests/accepts/bytes_out/connections/backend/pid.
329 # Hyperman->stats(pool => 1) is the POOL's: every worker's scoreboard row
330 # summed, from any process that has the arena - a worker, the supervisor, or
331 # one that attached by name. undef when there is no board.
332 SV *
333 stats(...)
334 CODE:
335 {
336 13 int pool = 0, i;
337 16 ▶ for (i = 1; i + 1 < items; i += 2)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) 3 13 16
338 6 ▶ # uncoverable branch false class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false class:ignore_covered_err 3 0 3
339 13 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 3 10 13
340 if (strEQ(SvPV_nolen(ST(i)), "pool")) pool = SvTRUE(ST(i + 1)) ? 1 : 0;
341 3 ▶ if (pool) {
Branch: 1 branch
BranchTrueFalseTotal
if (pool) { 1 2 3
342 hm_sa_pool_stats p;
343 2 if (!hm_sa_board_pool(&p)) RETVAL = &PL_sv_undef;
344 2 else {
345 2 HV *h = newHV();
346 2 hv_stores(h, "workers", newSVuv((UV)p.workers));
347 2 hv_stores(h, "alive", newSVuv((UV)p.alive));
348 2 hv_stores(h, "requests", newSVuv((UV)p.requests));
349 2 hv_stores(h, "accepts", newSVuv((UV)p.accepts));
350 2 hv_stores(h, "denied", newSVuv((UV)p.denied));
351 2 hv_stores(h, "connections", newSVuv((UV)p.conns));
352 2 hv_stores(h, "bytes_out", newSVuv((UV)p.bytes_out));
353 2 hv_stores(h, "datagrams", newSVuv((UV)p.datagrams));
354 hv_stores(h, "h3_requests", newSVuv((UV)p.h3));
355 RETVAL = newRV_noinc((SV *)h);
356 10 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 1 9 10
357 1 }
358 else if (!hm_cur_loop) {
359 9 RETVAL = &PL_sv_undef;
360 9 } else {
361 9 HV *h = newHV();
362 9 hv_stores(h, "requests", newSVuv(hm_cur_loop->requests));
363 9 hv_stores(h, "accepts", newSVuv(hm_cur_loop->accepts));
364 9 hv_stores(h, "denied", newSVuv(hm_cur_loop->denied));
365 9 hv_stores(h, "bytes_out", newSVuv(hm_cur_loop->bytes_out));
366 9 hv_stores(h, "datagrams", newSVuv(hm_cur_loop->datagrams));
367 9 hv_stores(h, "h3_conns", newSVuv(hm_cur_loop->h3_conns));
368 9 hv_stores(h, "h3_live", newSVuv(hm_cur_loop->h3_live));
369 9 hv_stores(h, "h3_requests", newSVuv(hm_cur_loop->h3_requests));
370 9 hv_stores(h, "datagrams_out", newSVuv(hm_cur_loop->datagrams_out));
371 9 hv_stores(h, "connections", newSViv(hm_cur_loop->nconns));
372 9 hv_stores(h, "backend", newSVpv(hm_cur_loop->be->name, 0));
373 hv_stores(h, "pid", newSViv((IV)hm_os_getpid()));
374 RETVAL = newRV_noinc((SV *)h);
375 }
376 }
377 OUTPUT:
378 RETVAL
379  
380 # True when HTTP/2 (h2c) support was built in (nghttp2 present).
381 int
382 has_http2(...)
383 CODE:
384 PERL_UNUSED_VAR(items);
385 11 RETVAL = hm_h2_available();
386 OUTPUT:
387 RETVAL
388  
389 # True when HTTP/3 (QUIC) support was built in - ngtcp2, ngtcp2_crypto_ossl,
390 # nghttp3 and an OpenSSL that can hand them its handshake, all four. There is
391 # no cleartext QUIC the way there is h2c, so this is false wherever has_tls
392 # is, and it is false on LibreSSL, which has no ngtcp2 crypto backend at all.
393 int
394 has_http3(...)
395 CODE:
396 PERL_UNUSED_VAR(items);
397 9 RETVAL = hm_h3_available();
398 OUTPUT:
399 RETVAL
400  
401 # The runtime QUIC stack banner ("ngtcp2 1.25.0, nghttp3 1.18.0"), or undef
402 # when HTTP/3 support was not built. Both libraries, because a QUIC problem
403 # is as often nghttp3's as ngtcp2's. The counterpart to tls_library.
404 SV *
405 quic_library(...)
406 PREINIT:
407 const char *s;
408 CODE:
409 PERL_UNUSED_VAR(items);
410 1 s = hm_quic_library();
411 1 ▶ RETVAL = s ? newSVpv(s, 0) : newSV(0); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = s ? newSVpv(s, 0) : newSV(0); /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 1 1
412 OUTPUT:
413 RETVAL
414  
415 # _accepts_gzip($header): the Accept-Encoding walk alone, for
416 # t/30-compress-accept.t. Author-facing, not documented.
417 int
418 _accepts_gzip(hdr)
419 SV *hdr
420 CODE:
421 {
422 30 STRLEN l = 0;
423 59 ▶ const char *h = SvOK(hdr) ? SvPV_const(hdr, l) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
const char *h = SvOK(hdr) ? SvPV_const(hdr, l) : NULL; 29 1 30
424 30 RETVAL = hz_accepts_gzip(h, (size_t)l);
425 }
426 OUTPUT:
427 RETVAL
428  
429 # _gzip($bytes): the compressor alone, for t/31-compress.t - returns the
430 # gzip member, or undef without zlib or when the result would not be
431 # smaller. Author-facing, not documented.
432 SV *
433 _gzip(bytes)
434 SV *bytes
435 CODE:
436 {
437 3 STRLEN l = 0;
438 5 ▶ const char *b = SvOK(bytes) ? SvPV_const(bytes, l) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
const char *b = SvOK(bytes) ? SvPV_const(bytes, l) : NULL; 2 1 3
439 3 char *out = NULL;
440 3 size_t ol = 0;
441 3 ▶ if (b && hz_gzip(b, (size_t)l, HZ_LEVEL, &out, &ol)) {
Branch: 2 branches
BranchTrueFalseTotal
if (b && hz_gzip(b, (size_t)l, HZ_LEVEL, &out, &ol)) { 2 1 3
if (b && hz_gzip(b, (size_t)l, HZ_LEVEL, &out, &ol)) { 1 1 2
442 1 RETVAL = newSVpvn(out, ol);
443 1 Safefree(out);
444 } else {
445 2 RETVAL = newSV(0);
446 }
447 }
448 OUTPUT:
449 RETVAL
450  
451 # True when response compression was built in (zlib present). Without it
452 # `compress => 1` is accepted and inert, so this is how a test skips
453 # honestly rather than guessing.
454 int
455 has_compression(...)
456 CODE:
457 PERL_UNUSED_VAR(items);
458 3 RETVAL = hz_available();
459 OUTPUT:
460 RETVAL
461  
462 # True when TLS/HTTPS support was built in (OpenSSL present).
463 int
464 has_tls(...)
465 CODE:
466 PERL_UNUSED_VAR(items);
467 17 RETVAL = hm_tls_available();
468 OUTPUT:
469 RETVAL
470  
471 # True when the kernel-TLS code was BUILT IN - an OpenSSL with enable-ktls.
472 # It says nothing about whether kTLS engages: that needs a kernel with the
473 # tls ULP and an offloadable cipher, is decided per connection at handshake,
474 # and falls back to ordinary OpenSSL writes whenever it does not happen.
475 int
476 has_ktls(...)
477 CODE:
478 PERL_UNUSED_VAR(items);
479 #ifdef HM_HAVE_KTLS
480 2 ▶ RETVAL = 1;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = 1; 0 2 2
481 #else
482 RETVAL = 0;
483 #endif
484 OUTPUT:
485 RETVAL
486  
487 # The runtime TLS library banner ("OpenSSL 3.0.13 30 Jan 2024",
488 # "LibreSSL 3.8.2"), or undef when TLS support was not built. The two
489 # stacks agree on the API but not on behaviour - LibreSSL has no TLS 1.3
490 # session resumption, for one - and this is how a caller tells them apart.
491 SV *
492 tls_library(...)
493 PREINIT:
494 const char *s;
495 CODE:
496 PERL_UNUSED_VAR(items);
497 1 s = hm_tls_library();
498 1 ▶ RETVAL = s ? newSVpv(s, 0) : newSV(0); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = s ? newSVpv(s, 0) : newSV(0); /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 0 1
499 OUTPUT:
500 RETVAL
501  
502 # Hyperman->tls_reload(\%sni) - replace this worker's TLS certificates
503 # without replacing the process.
504 #
505 # The context a listener serves is built once, in the parent, before the
506 # fork - so a certificate that arrives afterwards is not served until the
507 # process is restarted, and SIGHUP does not help because it re-forks from
508 # that same parent. This is the way to pick one up: called from inside a
509 # worker, it rebuilds that worker's contexts from the map given, on that
510 # worker's own loop, without unbinding anything.
511 #
512 # The map is the same shape `run` takes: { host => { cert => ..., key =>
513 # ... } }, paths not PEM bytes. Returns the number of listeners rebuilt -
514 # 0 outside a worker, 0 on a plain-only server, and 0 if the default
515 # certificate would not load, in which case what was being served still
516 # is.
517 int
518 tls_reload(class, sni = &PL_sv_undef)
519 SV *class
520 SV *sni
521 CODE:
522 PERL_UNUSED_VAR(class);
523 3 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 3 0 3
# uncoverable condition right count:1 class:ignore_covered_err 0 3 3
524 # uncoverable condition left count:2 class:ignore_covered_err
525 RETVAL = hm_cur_loop ? hm_tls_reload(aTHX_ hm_cur_loop, sni) : 0;
526 OUTPUT:
527 RETVAL
528  
529 # The current worker's loop, or undef outside a running loop.
530 SV *
531 loop(...)
532 CODE:
533 PERL_UNUSED_VAR(items);
534 8 ▶ if (hm_cur_loop) RETVAL = hm_loop_to_sv(aTHX_ hm_cur_loop);
Branch: 1 branch
BranchTrueFalseTotal
if (hm_cur_loop) RETVAL = hm_loop_to_sv(aTHX_ hm_cur_loop); 7 1 8
535 1 else RETVAL = &PL_sv_undef;
536 OUTPUT:
537 RETVAL
538  
539 # Hyperman::detach($env) - take this request's socket over for a protocol
540 # upgrade (WebSocket). Returns the client fd, now owned by the caller: the
541 # server has stopped watching it, forgotten the connection and will not
542 # close it. Watch it on psgix.loop, and close it when done.
543 IV
544 detach(env)
545 SV *env
546 CODE:
547 {
548 HV *ehv;
549 SV **e;
550 AV *cid;
551 SV **fsv, **isv;
552 int fd, rc;
553 UV id;
554 13 ▶ if (!hm_cur_loop)
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_cur_loop) 1 12 13
555 1 croak("Hyperman::detach: no running loop (call it from inside "
556 "a request)");
557 12 ▶ if (!SvROK(env) || SvTYPE(SvRV(env)) != SVt_PVHV) /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(env) || SvTYPE(SvRV(env)) != SVt_PVHV) /* # uncoverable condition left count:2 class:ignore_covered_err */ 11 1 12
if (!SvROK(env) || SvTYPE(SvRV(env)) != SVt_PVHV) /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 11 11
558 1 croak("Hyperman::detach: give it the PSGI env hashref");
559 11 ehv = (HV *)SvRV(env);
560 11 e = hv_fetchs(ehv, "psgix.hyperman.conn", 0);
561 11 ▶ # uncoverable condition left count:2 class:ignore_covered_err
Branch: 4 branches
BranchTrueFalseTotal
# uncoverable condition left count:2 class:ignore_covered_err 10 1 11
# uncoverable condition left count:2 class:ignore_covered_err 10 0 10
# uncoverable condition left count:2 class:ignore_covered_err 10 0 10
# uncoverable condition left count:2 class:ignore_covered_err 0 10 10
562 1 # uncoverable condition left count:3 class:ignore_covered_err
563 # uncoverable condition left count:4 class:ignore_covered_err
564 if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV))
565 10 croak("Hyperman::detach: no psgix.hyperman.conn in this env "
566 10 "(HTTP/2 and HTTP/3 streams share one connection, and "
567 10 "non-Hyperman servers have none, so none can detach)");
568 10 ▶ cid = (AV *)SvRV(*e);
Branch: 4 branches
BranchTrueFalseTotal
cid = (AV *)SvRV(*e); 9 1 10
cid = (AV *)SvRV(*e); 9 0 9
cid = (AV *)SvRV(*e); 9 0 9
cid = (AV *)SvRV(*e); 0 9 9
569 1 fsv = av_fetch(cid, 0, 0);
570 9 isv = av_fetch(cid, 1, 0);
571 9 # uncoverable condition left count:2 class:ignore_covered_err
572 9 # uncoverable condition left count:3 class:ignore_covered_err
573 9 # uncoverable condition left count:4 class:ignore_covered_err
574 4 if (!(fsv && *fsv && isv && *isv))
575 1 croak("Hyperman::detach: malformed psgix.hyperman.conn");
576 fd = (int)SvIV(*fsv);
577 0 id = SvUV(*isv);
578 rc = hm_detach(aTHX_ hm_cur_loop, fd, id);
579 1 # uncoverable branch true count:3
580 # uncoverable branch true count:7
581 1 # uncoverable branch true count:8
582 switch (rc) {
583 2 case 0: break;
584 0 case -1: croak("Hyperman::detach: the connection is gone "
585 "(the client hung up, or this env is stale)");
586 0 case -2: croak("Hyperman::detach: HTTP/2 connections cannot be " /* # uncoverable statement */
587 "detached (streams share one socket)");
588 4 ▶ case -3: croak("Hyperman::detach: TLS connections cannot be "
Branch: 1 branch
BranchTrueFalseTotal
case -3: croak("Hyperman::detach: TLS connections cannot be " 1 3 4
589 "detached (terminate TLS in front of Hyperman)");
590 case -4: croak("Hyperman::detach: a response is still draining "
591 "on this connection");
592 case -5: croak("Hyperman::detach: already detached");
593 case -6: croak("Hyperman::detach: not supported on this " /* # uncoverable statement */
594 "platform (it needs fork-style fd ownership)");
595 default: croak("Hyperman::detach: failed (%d)", rc); /* # uncoverable statement */
596 }
597 RETVAL = (IV)fd;
598 }
599 OUTPUT:
600 RETVAL
601  
602 # Hyperman::stream($env, $status, \@headers) - begin a streamed response body
603 # on this request and return a Hyperman::Writer for it.
604 #
605 # The Perl door onto ABI v6 (include/hyperman/hm_abi.h). The C door is the
606 # table's stream_open; both reach the one registry in hm_stream.h and have the
607 # same contract, so a pure-Perl application gets the transport neutrality an
608 # XS one gets. Unlike detach it works on HTTP/2 - it hands out a stream, not a
609 # socket - and it works on TLS, where there is no fd to hand over at all.
610 #
611 # The writer owns the handle: ->close ends the body, and letting the writer go
612 # out of scope closes it too.
613 SV *
614 stream(env, status = 200, headers = NULL)
615 SV *env
616 int status
617 SV *headers
618 CODE:
619 {
620 AV *w;
621 void *h;
622 int fd;
623 UV id;
624 10 int64_t sid = -1;
625 10 ▶ if (!hm_cur_loop)
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_cur_loop) 1 9 10
626 1 croak("Hyperman::stream: no running loop (call it from inside "
627 "a request)");
628 9 ▶ if (!SvROK(env) || SvTYPE(SvRV(env)) != SVt_PVHV) /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(env) || SvTYPE(SvRV(env)) != SVt_PVHV) /* # uncoverable condition left count:2 class:ignore_covered_err */ 8 1 9
if (!SvROK(env) || SvTYPE(SvRV(env)) != SVt_PVHV) /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 8 8
629 1 croak("Hyperman::stream: give it the PSGI env hashref");
630 8 ▶ if (!hm_stream_ticket(aTHX_ env, &fd, &id, &sid))
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_stream_ticket(aTHX_ env, &fd, &id, &sid)) 1 7 8
631 1 croak("Hyperman::stream: no usable psgix.hyperman.stream in this "
632 "env (not a Hyperman request, or the env is stale)");
633 7 ▶ # uncoverable branch true class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch true class:ignore_covered_err 7 0 7
# uncoverable branch true class:ignore_covered_err 0 7 7
634 7 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 7 0 7
# uncoverable condition left count:1 class:ignore_covered_err 6 1 7
635 6 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 0 6 6
636 1 # uncoverable condition left count:2 class:ignore_covered_err
637 6 if (headers && !SvOK(headers)) headers = NULL;
638 # uncoverable condition right count:1 class:ignore_covered_err
639 6 ▶ # uncoverable condition left count:3 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:3 class:ignore_covered_err 1 5 6
640 1 if (headers && !(SvROK(headers)
641 && SvTYPE(SvRV(headers)) == SVt_PVAV))
642 croak("Hyperman::stream: headers must be an array reference");
643 h = hm_stream_open(aTHX_ (void *)hm_cur_loop, fd, id, sid,
644 5 status, headers);
645 5 if (!h)
646 5 croak("Hyperman::stream: cannot stream on this request (the "
647 5 "client is gone, a response was already sent, or the "
648 5 "handler has not deferred - open a stream from a "
649 5 "psgi.streaming coderef or after parking on a Future)");
650 5 w = newAV();
651 av_extend(w, 2);
652 av_store(w, 0, newSViv(PTR2IV(h)));
653 av_store(w, 1, newSVuv(((hm_stream *)h)->serial));
654 av_store(w, 2, newSVpvs("stream"));
655 RETVAL = newRV_noinc((SV *)w);
656 sv_bless(RETVAL, gv_stashpv("Hyperman::Writer", GV_ADD));
657 }
658 OUTPUT:
659 RETVAL
660  
661 # ---- the fork-shared arena, from Perl ------------------------------------
662 #
663 # The denylist and the rate counters live in an anonymous shared mapping made
664 # before the workers fork, so every worker reads and writes the one copy.
665 # Until now they were reachable only through the C ABI, which meant an
666 # application had to be an XS module to ban an address or count a request -
667 # and the obvious Perl substitute, a hash in the worker, is wrong in the two
668 # ways that matter: a denylist becomes a different list per worker, and a
669 # limit of N becomes N per worker.
670 #
671 # All four fail OPEN when no arena is mapped (outside a running server, or on
672 # a platform without the atomics the arena needs): nothing is denied and
673 # nothing is limited. That is the same answer the accept path gives itself,
674 # and the safe one for a check that cannot run.
675  
676 # Hyperman->deny_add($ip, $ttl_seconds): block a peer at accept. A ttl of 0
677 # (the default) means until the server stops.
678 void
679 deny_add(class, ip, ttl = 0)
680 SV *class
681 SV *ip
682 IV ttl
683 CODE:
684 PERL_UNUSED_VAR(class);
685 58 ▶ # uncoverable branch false class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false class:ignore_covered_err 29 0 29
686 # uncoverable condition right count:1 class:ignore_covered_err
687 if (SvOK(ip)) hm_sa_deny_add(SvPV_nolen(ip), (long)ttl);
688  
689 # Hyperman->deny_remove($ip): unblock it again.
690 void
691 deny_remove(class, ip)
692 SV *class
693 SV *ip
694 CODE:
695 PERL_UNUSED_VAR(class);
696 6 ▶ # uncoverable branch false class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false class:ignore_covered_err 3 0 3
697 # uncoverable condition right count:1 class:ignore_covered_err
698 if (SvOK(ip)) hm_sa_deny_remove(SvPV_nolen(ip));
699  
700 # Hyperman->pool_stats: every worker's scoreboard row, as a list of hashrefs
701 # (pid, alive, updated, status, and the seven gauges). From any process that
702 # has the arena: a worker, the supervisor, or one that attached by name. The
703 # empty list when there is no board. `alive` is kill(pid, 0), which says 1
704 # for a process this user may not signal (EPERM) whether it lives or not.
705 void
706 pool_stats(class = &PL_sv_undef)
707 SV *class
708 PPCODE:
709 {
710 5 uint64_t i, n = hm_sa_board_slots();
711 PERL_UNUSED_VAR(class);
712 27 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 22 5 27
713 sa_sb_reading rd;
714 HV *h;
715 22 ▶ if (!hm_sa_board_read((uint32_t)i, &rd)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_sa_board_read((uint32_t)i, &rd)) continue; 15 7 22
716 7 h = newHV();
717 7 hv_stores(h, "pid", newSVuv((UV)rd.pid));
718 7 hv_stores(h, "alive", newSViv(rd.alive ? 1 : 0));
719 7 hv_stores(h, "updated", newSVuv((UV)rd.updated));
720 7 hv_stores(h, "status", newSVpvn(rd.status, rd.statuslen));
721 7 hv_stores(h, "requests", newSVuv((UV)rd.gauges[HM_SB_REQUESTS]));
722 7 hv_stores(h, "accepts", newSVuv((UV)rd.gauges[HM_SB_ACCEPTS]));
723 7 hv_stores(h, "denied", newSVuv((UV)rd.gauges[HM_SB_DENIED]));
724 7 hv_stores(h, "connections", newSVuv((UV)rd.gauges[HM_SB_CONNS]));
725 7 hv_stores(h, "bytes_out", newSVuv((UV)rd.gauges[HM_SB_BYTES_OUT]));
726 7 hv_stores(h, "datagrams", newSVuv((UV)rd.gauges[HM_SB_DATAGRAMS]));
727 7 hv_stores(h, "h3_requests", newSVuv((UV)rd.gauges[HM_SB_H3]));
728 7 ▶ XPUSHs(sv_2mortal(newRV_noinc((SV *)h)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_noinc((SV *)h))); 0 7 7
729 }
730 }
731  
732 # Hyperman->leader($name, ttl => $secs): the Shared::Arena::Lease of that
733 # name on the server's arena, so exactly one worker in the pool holds it at a
734 # time and a successor takes over when the holder dies or lapses. A
735 # convenience over Hyperman->arena->lease; undef with no arena. The fencing
736 # token is the part to read about in the POD before trusting it.
737 SV *
738 leader(class, name, ...)
739 SV *class
740 SV *name
741 CODE:
742 {
743 int i, count;
744 5 SV *ret = NULL;
745 PERL_UNUSED_VAR(class);
746 5 ▶ if (!hm_sa_arena_sv) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_sa_arena_sv) XSRETURN_UNDEF; 3 2 5
747 {
748 2 dSP;
749 2 ENTER; SAVETMPS;
750 2 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 2 2
751 2 ▶ EXTEND(SP, items + 1);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, items + 1); 2 0 2
EXTEND(SP, items + 1); 0 2 2
752 2 PUSHs(hm_sa_arena_sv);
753 2 PUSHs(name);
754 6 ▶ for (i = 2; i < items; i++) PUSHs(ST(i));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i < items; i++) PUSHs(ST(i)); 4 2 6
755 2 PUTBACK;
756 2 count = call_method("lease", G_SCALAR);
757 2 SPAGAIN;
758 2 ▶ # uncoverable branch false class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false class:ignore_covered_err 2 0 2
759 2 # uncoverable condition right count:1 class:ignore_covered_err
760 2 ▶ if (count == 1) ret = newSVsv(POPs);
Branch: 1 branch
BranchTrueFalseTotal
if (count == 1) ret = newSVsv(POPs); 2 0 2
761 PUTBACK;
762 2 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 2 2
763 2 }
764 # uncoverable branch true class:ignore_covered_err
765 # uncoverable condition left count:1 class:ignore_covered_err
766 if (!ret) XSRETURN_UNDEF;
767 RETVAL = ret;
768 }
769 OUTPUT:
770 RETVAL
771  
772 # Hyperman->distinct_clients -> ($estimate, $relative_error), or the empty
773 # list when the sketch is off (run(distinct_clients => 1) turns it on).
774 void
775 distinct_clients(class = &PL_sv_undef)
776 SV *class
777 PPCODE:
778 {
779 206 double c = hm_sa_hll_count();
780 PERL_UNUSED_VAR(class);
781 206 ▶ if (c < 0.0) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0.0) XSRETURN_EMPTY; 5 201 206
782 201 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 201 201
783 201 mPUSHn(c);
784 201 mPUSHn(1.04 / 128.0); /* 1.04 / sqrt(2^14) */
785 }
786  
787 # Hyperman->arena: the Shared::Arena the server created before the fork; it
788 # answers undef before run() and when there is no table. A fresh reference
789 # to the one object every call, so a caller's copy going out of scope frees
790 # nothing the workers share. (No comment line here may begin with a word
791 # the preprocessor knows: xsubpp passes these through as C.)
792 SV *
793 arena(class)
794 SV *class
795 CODE:
796 PERL_UNUSED_VAR(class);
797 20 ▶ RETVAL = hm_sa_arena_sv ? newSVsv(hm_sa_arena_sv) : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = hm_sa_arena_sv ? newSVsv(hm_sa_arena_sv) : newSV(0); 14 6 20
798 OUTPUT:
799 RETVAL
800  
801 # Hyperman->deny_check($ip): is it blocked right now?
802 IV
803 deny_check(class, ip)
804 SV *class
805 SV *ip
806 CODE:
807 PERL_UNUSED_VAR(class);
808 66 ▶ RETVAL = SvOK(ip) ? hm_sa_deny_check(SvPV_nolen(ip)) : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = SvOK(ip) ? hm_sa_deny_check(SvPV_nolen(ip)) : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 33 0 33
RETVAL = SvOK(ip) ? hm_sa_deny_check(SvPV_nolen(ip)) : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 23 10 33
809 OUTPUT:
810 RETVAL
811  
812 # Hyperman->ratelimit_hit($key, $limit, $window) -> ($allowed, $remaining,
813 # $reset). Counts one hit against $key in the current fixed window and says
814 # whether it is within $limit. $reset is the epoch second the window rolls,
815 # which is what an X-RateLimit-Reset header wants. A limit of 0 is unlimited
816 # and reports a remaining of -1.
817 void
818 ratelimit_hit(class, key, limit, window = 60)
819 SV *class
820 SV *key
821 IV limit
822 IV window
823 PPCODE:
824 {
825 STRLEN klen;
826 22 ▶ const char *kp = SvOK(key) ? SvPV_const(key, klen) : ""; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
const char *kp = SvOK(key) ? SvPV_const(key, klen) : ""; /* # uncoverable condition right count:1 class:ignore_covered_err */ 11 0 11
827 11 long rem = 0, reset = 0;
828 int ok;
829 PERL_UNUSED_VAR(class);
830 11 ▶ # uncoverable branch true class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true class:ignore_covered_err 0 11 11
831 11 # uncoverable condition left count:1 class:ignore_covered_err
832 if (!SvOK(key)) klen = 0;
833 11 ▶ ok = hm_sa_ratelimit_hit(kp, (size_t)klen, (long)limit, (long)window,
Branch: 1 branch
BranchTrueFalseTotal
ok = hm_sa_ratelimit_hit(kp, (size_t)klen, (long)limit, (long)window, 0 11 11
834 11 &rem, &reset);
835 11 EXTEND(SP, 3);
836 11 mPUSHi(ok);
837 mPUSHi(rem);
838 mPUSHi(reset);
839 }
840  
841 # ---- run code in every worker after the fork -----------------------------
842  
843 # Hyperman->on_worker_start(\&cb): register before run(); fires once in each
844 # worker, after the fork, before its loop starts turning. The Perl door onto
845 # the v4 registry the C ABI's on_worker_start writes to.
846 IV
847 on_worker_start(class, cb)
848 SV *class
849 SV *cb
850 CODE:
851 PERL_UNUSED_VAR(class);
852 6 ▶ RETVAL = hm_worker_hook_add_perl(aTHX_ cb);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = hm_worker_hook_add_perl(aTHX_ cb); 0 4 4
853 OUTPUT:
854 RETVAL
855  
856 # ---- compat shim selftest -----------------------------------------------
857  
858 # Hyperman->_proxy_selftest: drive hm_proxy.h's parser from C over hostile
859 # inputs (t/61). 0 = every step passed, else the number of the first failure.
860 IV
861 _proxy_selftest(class)
862 SV *class
863 CODE:
864 PERL_UNUSED_VAR(class);
865 2 ▶ RETVAL = hm_proxy_selftest();
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = hm_proxy_selftest(); 0 2 2
866 OUTPUT:
867 RETVAL
868  
869 # Hyperman->_croak_sv_selftest($sv): raise $sv through hm_compat.h's own
870 # croak_sv shim, whether or not this perl has a native croak_sv to shadow
871 # it. Private, and here for one reason: that shim stands in only on perls
872 # older than 5.14, so on any perl a developer can actually run, no other
873 # line of this distribution reaches it. t/38 drives it through the shapes
874 # it has to handle. It cannot prove the version-dependent behaviour - see
875 # the note there - only that the shim itself is sound.
876 #
877 # (Take care rewording this comment: a line beginning "# else" or "# if"
878 # is read by xsubpp as a preprocessor directive.)
879 void
880 _croak_sv_selftest(class, sv)
881 SV *class
882 SV *sv
883 CODE:
884 PERL_UNUSED_VAR(class);
885 4 hm_croak_sv(aTHX_ sv);
« include/hyperman/hm_sa_bus.h ↑ Summary include/hyperman/hm_compat.h »