feersum_core.c.inc

filter: Statementstmt 79.7% 905 / 1135 Branchbran 64.7% 497 / 768 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 73.6% 1402 / 1903 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« feersum_conn.xs ↑ Summary feersum_psgi.c.inc »
1  
2 INLINE_UNLESS_DEBUG
3 static SV*
4 472 fetch_av_normal (pTHX_ AV *av, I32 i)
5 {
6 472 SV **elt = av_fetch(av, i, 0);
7 472 ▶ if (elt == NULL) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (elt == NULL) return NULL; 0 472 472
8 472 SV *sv = *elt;
9 472 ▶ if (unlikely(SvMAGICAL(sv))) sv = sv_2mortal(newSVsv(sv));
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(SvMAGICAL(sv))) sv = sv_2mortal(newSVsv(sv)); 4 468 472
10 472 ▶ if (unlikely(!SvOK(sv))) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!SvOK(sv))) return NULL; 3 469 472
11 469 ▶ if (unlikely(SvROK(sv))) sv = SvRV(sv);
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(SvROK(sv))) sv = SvRV(sv); 6 463 469
12 469 return sv;
13 }
14  
15 INLINE_UNLESS_DEBUG
16 static struct iomatrix *
17 137415 next_iomatrix (struct feer_conn *c)
18 {
19 137415 bool add_iomatrix = 0;
20 struct iomatrix *m;
21  
22 137415 ▶ if (!c->wbuf_rinq) {
Branch: 1 branch
BranchTrueFalseTotal
if (!c->wbuf_rinq) { 47004 90411 137415
23 trace3("next_iomatrix(%d): head\n", c->fd);
24 47004 add_iomatrix = 1;
25 }
26 else {
27 90411 m = (struct iomatrix *)c->wbuf_rinq->prev->ref;
28 trace3("next_iomatrix(%d): tail, count=%d, offset=%d\n",
29 c->fd, m->count, m->offset);
30 90411 ▶ if (m->count >= FEERSUM_IOMATRIX_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if (m->count >= FEERSUM_IOMATRIX_SIZE) { 0 90411 90411
31 0 add_iomatrix = 1;
32 }
33 }
34  
35 137415 ▶ if (add_iomatrix) {
Branch: 1 branch
BranchTrueFalseTotal
if (add_iomatrix) { 47004 90411 137415
36 trace3("next_iomatrix(%d): alloc\n", c->fd);
37 47004 ▶ IOMATRIX_ALLOC(m);
Branch: 1 branch
BranchTrueFalseTotal
IOMATRIX_ALLOC(m); 46783 221 47004
38 47004 m->offset = m->count = 0;
39 47004 rinq_push(&c->wbuf_rinq, m);
40 }
41  
42 trace3("next_iomatrix(%d): end, count=%d, offset=%d\n",
43 c->fd, m->count, m->offset);
44 137415 return m;
45 }
46  
47 // PSGI strings are bytes: ship a UTF8-flagged value downgraded; wide chars
48 // (> 255) fail the downgrade and keep their UTF-8 bytes.
49 INLINE_UNLESS_DEBUG
50 static SV *
51 1 feer_bytes_mortal(const char *p, STRLEN len)
52 {
53 1 SV *sv = sv_2mortal(newSVpvn_utf8(p, len, 1));
54 1 (void)sv_utf8_downgrade(sv, TRUE);
55 1 return sv;
56 }
57  
58 INLINE_UNLESS_DEBUG
59 static STRLEN
60 46038 add_sv_to_wbuf(struct feer_conn *c, SV *sv)
61 {
62 46038 struct iomatrix *m = next_iomatrix(c);
63 46038 unsigned idx = m->count++;
64 STRLEN cur;
65 // newSVsv() below can croak on a magical SV, leaving the slot counted but unfilled
66 46038 m->iov[idx].iov_base = NULL;
67 46038 m->iov[idx].iov_len = 0;
68 46038 m->sv[idx] = NULL;
69 46038 ▶ if (unlikely(SvMAGICAL(sv) || SvUTF8(sv))) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(SvMAGICAL(sv) || SvUTF8(sv))) { 46037 1 46038
if (unlikely(SvMAGICAL(sv) || SvUTF8(sv))) { 3 46034 46037
70 4 sv = newSVsv(sv);
71 7 ▶ if (unlikely(SvUTF8(sv)))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(SvUTF8(sv))) 3 1 4
72 3 (void)sv_utf8_downgrade(sv, TRUE);
73 }
74 46034 ▶ else if (unlikely(SvPADTMP(sv))) {
Branch: 1 branch
BranchTrueFalseTotal
else if (unlikely(SvPADTMP(sv))) { 0 46034 46034
75 // a PADTMP's PV is reused, so it must be copied or stolen
76 #ifdef FEERSUM_STEAL
77 0 ▶ if (SvFLAGS(sv) == (SVs_PADTMP|SVf_POK|SVp_POK)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvFLAGS(sv) == (SVs_PADTMP|SVf_POK|SVp_POK)) { 0 0 0
78 trace3("STEALING\n");
79 0 SV *thief = newSV(0);
80 0 sv_upgrade(thief, SVt_PV);
81  
82 0 SvPV_set(thief, SvPVX(sv));
83 0 SvLEN_set(thief, SvLEN(sv));
84 0 SvCUR_set(thief, SvCUR(sv));
85  
86 0 ▶ (void)SvOK_off(sv);
Branch: 4 branches
BranchTrueFalseTotal
(void)SvOK_off(sv); 0 0 0
(void)SvOK_off(sv); 0 0 0
(void)SvOK_off(sv); 0 0 0
(void)SvOK_off(sv); 0 0 0
87 0 SvPV_set(sv, NULL);
88 0 SvLEN_set(sv, 0);
89 0 SvCUR_set(sv, 0);
90  
91 0 SvFLAGS(thief) |= SVf_READONLY|SVf_POK|SVp_POK;
92  
93 0 sv = thief;
94 }
95 else {
96 0 sv = newSVsv(sv);
97 }
98 #else
99 sv = newSVsv(sv);
100 #endif
101 }
102 else {
103 46034 sv = SvREFCNT_inc(sv);
104 }
105  
106 46038 m->iov[idx].iov_base = SvPV(sv, cur);
107 46038 m->iov[idx].iov_len = cur;
108 46038 m->sv[idx] = sv;
109  
110 46038 c->wbuf_len += cur;
111 46038 return cur;
112 }
113  
114 /* A raw streaming body stops at the app's Content-Length: a surplus would run
115 * into the next response on a reused connection. */
116 static void
117 531 add_raw_body_to_wbuf(struct feer_conn *c, SV *sv)
118 {
119 dTHX;
120 531 ▶ if (likely(!c->resp_cl_enforced)) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(!c->resp_cl_enforced)) { 65 466 531
121 65 add_sv_to_wbuf(c, sv);
122 531 return;
123 }
124 STRLEN len;
125 466 const char *p = SvPV(sv, len);
126 // count what reaches the wire, which add_sv_to_wbuf downgrades
127 466 ▶ if (unlikely(SvUTF8(sv))) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(SvUTF8(sv))) { 0 466 466
128 0 sv = feer_bytes_mortal(p, len);
129 0 p = SvPV(sv, len);
130 }
131 466 ▶ if (len <= c->resp_cl_owed) {
Branch: 1 branch
BranchTrueFalseTotal
if (len <= c->resp_cl_owed) { 466 0 466
132 466 c->resp_cl_owed -= len;
133 466 add_sv_to_wbuf(c, sv);
134 466 return;
135 }
136 trace("body over its Content-Length by %"Sz_uf", truncated fd=%d\n",
137 (Sz)(len - c->resp_cl_owed), c->fd);
138 0 ▶ if (c->resp_cl_owed) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->resp_cl_owed) { 0 0 0
139 0 SV *head = newSVpvn(p, c->resp_cl_owed);
140 0 add_sv_to_wbuf(c, head);
141 0 SvREFCNT_dec(head);
142 0 c->resp_cl_owed = 0;
143 }
144 }
145  
146 INLINE_UNLESS_DEBUG
147 static STRLEN
148 45112 add_const_to_wbuf(struct feer_conn *c, const char *str, size_t str_len)
149 {
150 45112 struct iomatrix *m = next_iomatrix(c);
151 45112 unsigned idx = m->count++;
152 45112 m->iov[idx].iov_base = (void*)str;
153 45112 m->iov[idx].iov_len = str_len;
154 45112 m->sv[idx] = NULL;
155 45112 c->wbuf_len += str_len;
156 45112 return str_len;
157 }
158  
159 INLINE_UNLESS_DEBUG
160 static void
161 45058 add_placeholder_to_wbuf(struct feer_conn *c, SV **sv, struct iovec **iov_ref)
162 {
163 45058 struct iomatrix *m = next_iomatrix(c);
164 45058 unsigned idx = m->count++;
165 // Counted now, filled later by update_wbuf_placeholder: a croak in between
166 // must leave an empty iovec, not stale memory.
167 45058 m->iov[idx].iov_base = NULL;
168 45058 m->iov[idx].iov_len = 0;
169 45058 m->sv[idx] = NULL;
170 45058 *sv = newSV(31);
171 45058 SvPOK_on(*sv);
172 45058 m->sv[idx] = *sv;
173 45058 *iov_ref = &m->iov[idx];
174 45058 }
175  
176 INLINE_UNLESS_DEBUG
177 static void
178 291 finish_wbuf(struct feer_conn *c)
179 {
180 // a raw body short of its Content-Length cannot end cleanly: close after it
181 291 ▶ if (unlikely(c->resp_cl_enforced && c->resp_cl_owed))
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(c->resp_cl_enforced && c->resp_cl_owed)) 94 197 291
if (unlikely(c->resp_cl_enforced && c->resp_cl_owed)) 5 89 94
182 5 c->is_keepalive = 0;
183 291 ▶ if (!c->use_chunked) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!c->use_chunked) return; 195 96 291
184 // HEAD: not even the terminating chunk, which a client that reads no body
185 // would parse as the start of the next response.
186 96 ▶ if (unlikely(c->no_resp_body)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->no_resp_body)) return; 4 92 96
187 92 add_const_to_wbuf(c, "0\r\n\r\n", 5);
188 }
189  
190 INLINE_UNLESS_DEBUG
191 static void
192 45058 update_wbuf_placeholder(struct feer_conn *c, SV *sv, struct iovec *iov)
193 {
194 STRLEN cur;
195 // iov_len cannot be passed as cur: incompatible pointer type on some systems
196 45058 iov->iov_base = SvPV(sv,cur);
197 45058 iov->iov_len = cur;
198 45058 c->wbuf_len += cur;
199 45058 }
200  
201 static void
202 45013 add_chunk_sv_to_wbuf(struct feer_conn *c, SV *sv)
203 {
204 STRLEN len;
205 (void)SvPV(sv, len);
206 45013 ▶ if (unlikely(len == 0)) return; /* an empty chunk would read as the terminator */
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(len == 0)) return; /* an empty chunk would read as the terminator */ 0 45013 45013
207  
208 SV *chunk;
209 struct iovec *chunk_iov;
210 45013 add_placeholder_to_wbuf(c, &chunk, &chunk_iov);
211 45013 STRLEN cur = add_sv_to_wbuf(c, sv);
212 45013 add_crlf_to_wbuf(c);
213 {
214 char tmp[2 * sizeof(Sz)];
215 45013 unsigned n = 0;
216 45013 Sz v = (Sz)cur;
217 45101 ▶ do { tmp[n++] = "0123456789abcdef"[v & 0xf]; v >>= 4; } while (v);
Branch: 1 branch
BranchTrueFalseTotal
do { tmp[n++] = "0123456789abcdef"[v & 0xf]; v >>= 4; } while (v); 88 45013 45101
218 45013 char *p = SvPVX(chunk);
219 90114 ▶ for (unsigned i = 0; i < n; i++) p[i] = tmp[n - 1 - i];
Branch: 1 branch
BranchTrueFalseTotal
for (unsigned i = 0; i < n; i++) p[i] = tmp[n - 1 - i]; 45101 45013 90114
220 45013 p[n] = '\r'; p[n + 1] = '\n'; p[n + 2] = '\0';
221 45013 SvCUR_set(chunk, n + 2);
222 }
223 45013 update_wbuf_placeholder(c, chunk, chunk_iov);
224 }
225  
226 static const char *
227 417 http_code_to_msg (int code) {
228 417 switch (code) {
229 0 case 100: return "Continue";
230 0 case 101: return "Switching Protocols";
231 0 case 102: return "Processing"; // RFC 2518
232 0 case 200: return "OK";
233 0 case 201: return "Created";
234 0 case 202: return "Accepted";
235 0 case 203: return "Non Authoritative Information";
236 0 case 204: return "No Content";
237 20 case 205: return "Reset Content";
238 0 case 206: return "Partial Content";
239 0 case 207: return "Multi-Status"; // RFC 4918 (WebDav)
240 0 case 300: return "Multiple Choices";
241 0 case 301: return "Moved Permanently";
242 0 case 302: return "Found";
243 0 case 303: return "See Other";
244 0 case 304: return "Not Modified";
245 0 case 305: return "Use Proxy";
246 0 case 307: return "Temporary Redirect";
247 174 case 400: return "Bad Request";
248 0 case 401: return "Unauthorized";
249 0 case 402: return "Payment Required";
250 0 case 403: return "Forbidden";
251 0 case 404: return "Not Found";
252 0 case 405: return "Method Not Allowed";
253 0 case 406: return "Not Acceptable";
254 0 case 407: return "Proxy Authentication Required";
255 15 case 408: return "Request Timeout";
256 0 case 409: return "Conflict";
257 0 case 410: return "Gone";
258 3 case 411: return "Length Required";
259 0 case 412: return "Precondition Failed";
260 18 case 413: return "Request Entity Too Large";
261 8 case 414: return "Request URI Too Long";
262 0 case 415: return "Unsupported Media Type";
263 0 case 416: return "Requested Range Not Satisfiable";
264 9 case 417: return "Expectation Failed";
265 0 case 418: return "I'm a teapot";
266 0 case 421: return "Misdirected Request"; // RFC 9110
267 0 case 422: return "Unprocessable Entity"; // RFC 4918
268 0 case 423: return "Locked"; // RFC 4918
269 0 case 424: return "Failed Dependency"; // RFC 4918
270 0 case 425: return "Too Early"; // RFC 8470
271 0 case 426: return "Upgrade Required"; // RFC 2817
272 0 case 429: return "Too Many Requests"; // RFC 6585
273 5 case 431: return "Request Header Fields Too Large"; // RFC 6585
274 0 case 449: return "Retry With"; // Microsoft
275 0 case 450: return "Blocked by Parental Controls"; // Microsoft
276 156 case 500: return "Internal Server Error";
277 9 case 501: return "Not Implemented";
278 0 case 502: return "Bad Gateway";
279 0 case 503: return "Service Unavailable";
280 0 case 504: return "Gateway Timeout";
281 0 case 505: return "HTTP Version Not Supported";
282 0 case 506: return "Variant Also Negotiates"; // RFC 2295
283 0 case 507: return "Insufficient Storage"; // RFC 4918
284 0 case 509: return "Bandwidth Limit Exceeded"; // Apache mod
285 0 case 510: return "Not Extended"; // RFC 2774
286 0 case 530: return "User access denied";
287 0 default: break;
288 }
289  
290 0 switch (code / 100) {
291 0 case 1: return "Informational";
292 0 case 2: return "Success";
293 0 case 3: return "Redirection";
294 0 case 4: return "Client Error";
295 0 default: return "Error";
296 }
297 }
298  
299 static int
300 1448 prep_socket(int fd, int is_tcp)
301 {
302 #ifdef HAS_ACCEPT4
303 1448 int flags = 1;
304 #else
305 int flags;
306  
307 flags = fcntl(fd, F_GETFL);
308 if (unlikely(flags < 0 || fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0))
309 return -1;
310 if (unlikely(fcntl(fd, F_SETFD, FD_CLOEXEC) < 0))
311 return -1;
312  
313 flags = 1;
314 #endif
315 1448 ▶ if (likely(is_tcp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(is_tcp)) { 1446 2 1448
316 1446 ▶ if (unlikely(setsockopt(fd, SOL_TCP, TCP_NODELAY, &flags, sizeof(int))))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(setsockopt(fd, SOL_TCP, TCP_NODELAY, &flags, sizeof(int)))) 0 1446 1446
317 0 return -1;
318 }
319  
320 1448 return 0;
321 }
322  
323 #if defined(TCP_CORK)
324 # define FEERSUM_TCP_CORK TCP_CORK
325 #elif defined(TCP_NOPUSH)
326 # define FEERSUM_TCP_CORK TCP_NOPUSH
327 #endif
328  
329 #ifdef FEERSUM_TCP_CORK
330 INLINE_UNLESS_DEBUG static void
331 20 set_cork(struct feer_conn *c, int cork)
332 {
333 20 ▶ if (likely(c->cached_is_tcp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(c->cached_is_tcp)) { 20 0 20
334 20 setsockopt(c->fd, SOL_TCP, FEERSUM_TCP_CORK, &cork, sizeof(cork));
335 }
336 20 }
337 #else
338 # define set_cork(c, cork) ((void)0)
339 #endif
340  
341 static void
342 160 invoke_shutdown_cb(pTHX_ struct feer_server *server)
343 {
344 160 dSP;
345 160 SV *cb = server->shutdown_cb_cv;
346 160 server->shutdown_cb_cv = NULL;
347  
348 /* Release the loop hold, as unlisten does, or ev_run never runs dry. */
349 160 ev_idle_stop(feersum_ev_loop, &server->ei);
350 160 ev_prepare_stop(feersum_ev_loop, &server->ep);
351 160 ev_check_stop(feersum_ev_loop, &server->ec);
352 #ifdef FEERSUM_HAS_TLS
353 160 ev_timer_stop(feersum_ev_loop, &server->tls_wbuf_trim_w);
354 #endif
355 160 ▶ if (server->watchers_initialized) {
Branch: 1 branch
BranchTrueFalseTotal
if (server->watchers_initialized) { 160 0 160
356 160 server->watchers_initialized = false;
357 160 ▶ if (--date_timer_refs <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--date_timer_refs <= 0) { 131 29 160
358 131 ev_timer_stop(feersum_ev_loop, &date_timer);
359 131 date_timer_refs = 0;
360 }
361 }
362  
363 160 ENTER;
364 160 SAVETMPS;
365 160 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 160 160
366 160 PUTBACK;
367 160 call_sv(cb, G_EVAL|G_VOID|G_DISCARD|G_NOARGS);
368 160 SPAGAIN;
369 trace3("called shutdown handler\n");
370 160 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 160 0 160
if (SvTRUE(ERRSV)) { 1 159 160
371 1 ▶ trouble("shutdown callback died: %-p\n", ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
trouble("shutdown callback died: %-p\n", ERRSV); 1 0 1
372 1 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 1 0 1
373 }
374 160 SvREFCNT_dec(cb);
375 160 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 1 159 160
376 160 LEAVE;
377 160 }
378  
379 /* Caller must have rejected the already-shutting-down case. */
380 static void
381 160 feer_begin_graceful_shutdown(pTHX_ struct feer_server *server, SV *cb)
382 {
383 160 SvREFCNT_dec(server->shutdown_cb_cv);
384 160 server->shutdown_cb_cv = newSVsv(cb);
385 trace("shutting down, active=%d\n", server->active_conns);
386  
387 160 server->shutting_down = 1;
388 320 ▶ for (int i = 0; i < server->n_listeners; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < server->n_listeners; i++) { 160 160 320
389 160 struct feer_listen *lsnr = &server->listeners[i];
390 160 ev_io_stop(feersum_ev_loop, &lsnr->accept_w);
391 /* A reuseport TCP listener's accept queue dies with its fd; UNIX
392 * listeners stay shared across workers, so theirs is left alone. */
393 160 ▶ if (unlikely(server->drain_accept_queue)
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(server->drain_accept_queue) 1 159 160
394 1 ▶ && lsnr->is_tcp && lsnr->fd >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
&& lsnr->is_tcp && lsnr->fd >= 0) { 1 0 1
&& lsnr->is_tcp && lsnr->fd >= 0) { 1 0 1
395 2 ▶ while (try_accept_one(feersum_ev_loop, lsnr, server))
Branch: 1 branch
BranchTrueFalseTotal
while (try_accept_one(feersum_ev_loop, lsnr, server)) 1 1 2
396 ;
397 }
398 160 ev_timer_stop(feersum_ev_loop, &lsnr->emfile_w);
399 160 ▶ if (lsnr->fd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (lsnr->fd >= 0) { 160 0 160
400 160 ▶ if (unlikely(close(lsnr->fd) < 0))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(close(lsnr->fd) < 0)) 0 160 160
401 0 trouble("close(listen fd) fd=%d: %s\n", lsnr->fd, strerror(errno));
402 160 ev_io_set(&lsnr->accept_w, -1, EV_READ);
403 160 lsnr->fd = -1;
404 }
405 #ifdef FEERSUM_HAS_TLS
406 160 feer_tls_cleanup_listener(lsnr);
407 #endif
408 160 lsnr->pause_flags = 0;
409 }
410  
411 160 ▶ while (feer_server_recycle_idle_conn(server))
Branch: 1 branch
BranchTrueFalseTotal
while (feer_server_recycle_idle_conn(server)) 0 160 160
412 ;
413  
414 160 ▶ if (server->active_conns <= 0 && server->shutdown_cb_cv) {
Branch: 2 branches
BranchTrueFalseTotal
if (server->active_conns <= 0 && server->shutdown_cb_cv) { 158 2 160
if (server->active_conns <= 0 && server->shutdown_cb_cv) { 158 0 158
415 trace("shutdown is immediate\n");
416 158 invoke_shutdown_cb(aTHX_ server);
417 }
418 160 }
419  
420 static void
421 2570 safe_close_conn(struct feer_conn *c, const char *where)
422 {
423 2570 ▶ if (unlikely(c->fd < 0))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->fd < 0)) 1122 1448 2570
424 1122 return;
425  
426 /* The app's IO handle owns this fd; a second close would hit a recycled
427 * number and kill an unrelated connection. */
428 1448 ▶ if (unlikely(c->fd_given_away)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->fd_given_away)) { 10 1438 1448
429 10 c->fd = -1;
430 10 return;
431 }
432  
433 #ifdef FEERSUM_HAS_H2
434 if (c->is_h2_stream) {
435 /* the parent owns the shared fd */
436 c->fd = -1;
437 return;
438 }
439 #endif
440  
441 1438 ▶ CLOSE_SENDFILE_FD(c);
Branch: 2 branches
BranchTrueFalseTotal
CLOSE_SENDFILE_FD(c); 0 1438 1438
CLOSE_SENDFILE_FD(c); 0 0 0
442  
443 #ifdef FEERSUM_HAS_TLS
444 1438 ▶ if (c->tls_tunnel)
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_tunnel) 9 1429 1438
445 9 feer_tls_tunnel_shutdown(c);
446  
447 // An alert encrypted now sequences after whatever is still queued in
448 // tls_wbuf, so flush first; if that fails send none.
449 1438 ▶ if (c->tls && c->tls_handshake_done && c->tls_wbuf.off > 0)
Branch: 3 branches
BranchTrueFalseTotal
if (c->tls && c->tls_handshake_done && c->tls_wbuf.off > 0) 170 1268 1438
if (c->tls && c->tls_handshake_done && c->tls_wbuf.off > 0) 153 17 170
if (c->tls && c->tls_handshake_done && c->tls_wbuf.off > 0) 1 152 153
450 1 (void)feer_tls_flush_wbuf(c);
451 1438 ▶ if (c->tls && c->tls_handshake_done && c->tls_wbuf.off == 0
Branch: 3 branches
BranchTrueFalseTotal
if (c->tls && c->tls_handshake_done && c->tls_wbuf.off == 0 170 1268 1438
if (c->tls && c->tls_handshake_done && c->tls_wbuf.off == 0 153 17 170
if (c->tls && c->tls_handshake_done && c->tls_wbuf.off == 0 152 1 153
452 152 ▶ && !c->tls_alert_sent) {
Branch: 1 branch
BranchTrueFalseTotal
&& !c->tls_alert_sent) { 152 0 152
453 ptls_buffer_t closebuf;
454 152 c->tls_alert_sent = 1;
455 152 ptls_buffer_init(&closebuf, "", 0);
456 152 ▶ if (unlikely(c->tls_fatal_alert))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->tls_fatal_alert)) 0 152 152
457 0 ptls_send_alert(c->tls, &closebuf, PTLS_ALERT_LEVEL_FATAL,
458 0 c->tls_fatal_alert);
459 else
460 152 ptls_send_alert(c->tls, &closebuf, PTLS_ALERT_LEVEL_WARNING,
461 PTLS_ALERT_CLOSE_NOTIFY);
462 152 ▶ if (closebuf.off > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (closebuf.off > 0) { 152 0 152
463 ssize_t wr PERL_UNUSED_DECL;
464 152 wr = write(c->fd, closebuf.base, closebuf.off);
465 }
466 152 ptls_buffer_dispose(&closebuf);
467 }
468 #endif
469  
470 // FIN before close: the peer sees a clean EOF, not RST
471 1438 shutdown(c->fd, SHUT_WR);
472  
473 /* close() on an unread client frame would RST the queued response away */
474 1438 ▶ if (unlikely(c->want_linger_close) && feer_arm_linger(c, where))
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(c->want_linger_close) && feer_arm_linger(c, where)) 0 1438 1438
if (unlikely(c->want_linger_close) && feer_arm_linger(c, where)) 0 0 0
475 0 return;
476  
477 1438 ▶ if (unlikely(close(c->fd) < 0))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(close(c->fd) < 0)) 0 1438 1438
478 0 trouble("close(%s) fd=%d: %s\n", where, c->fd, strerror(errno));
479  
480 1438 c->fd = -1;
481 }
482  
483 static struct feer_conn *
484 1448 new_feer_conn (EV_P_ int conn_fd, struct sockaddr *sa, socklen_t sa_len,
485 struct feer_server *srvr, struct feer_listen *lsnr)
486 {
487 1448 SV *self = newSV(0);
488 1448 ▶ SvUPGRADE(self, SVt_PVMG); // ensures sv_bless doesn't reallocate
Branch: 1 branch
BranchTrueFalseTotal
SvUPGRADE(self, SVt_PVMG); // ensures sv_bless doesn't reallocate 1448 0 1448
489 1448 ▶ SvGROW(self, sizeof(struct feer_conn));
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(self, sizeof(struct feer_conn)); 1448 0 1448
SvGROW(self, sizeof(struct feer_conn)); 1448 0 1448
490 1448 SvPOK_only(self);
491 1448 SvIOK_on(self);
492 1448 SvIV_set(self,conn_fd);
493  
494 1448 struct feer_conn *c = (struct feer_conn *)SvPVX(self);
495 1448 Zero(c, 1, struct feer_conn);
496  
497 1448 c->self = self;
498 1448 c->server = srvr;
499 1448 c->listener = lsnr;
500 1448 SvREFCNT_inc_void_NN(srvr->self); // prevent server GC while conn alive
501  
502 1448 c->cached_read_timeout = srvr->read_timeout;
503 1448 c->cached_write_timeout = srvr->write_timeout;
504 1448 c->cached_eof_park_timeout = srvr->eof_park_timeout;
505 1448 c->cached_max_conn_reqs = srvr->max_connection_reqs;
506 1448 c->cached_is_tcp = lsnr->is_tcp;
507 1448 c->cached_keepalive_default = srvr->is_keepalive;
508 1448 c->cached_use_reverse_proxy = srvr->use_reverse_proxy;
509 1448 c->cached_max_read_buf = srvr->max_read_buf;
510 1448 c->cached_max_body_len = srvr->max_body_len;
511 1448 c->cached_max_uri_len = srvr->max_uri_len;
512 1448 c->cached_wbuf_low_water = srvr->wbuf_low_water;
513 1448 c->fd = conn_fd;
514 1448 memcpy(&c->sa, sa, sa_len);
515 1448 ▶ c->receiving = srvr->use_proxy_protocol ? RECEIVE_PROXY_HEADER : RECEIVE_HEADERS;
Branch: 1 branch
BranchTrueFalseTotal
c->receiving = srvr->use_proxy_protocol ? RECEIVE_PROXY_HEADER : RECEIVE_HEADERS; 176 1272 1448
516 1448 c->sendfile_fd = -1;
517  
518 #ifdef FEERSUM_HAS_TLS
519 1448 ▶ if (lsnr->tls_ctx_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (lsnr->tls_ctx_ref) { 170 1278 1448
520 170 ev_io_init(&c->read_ev_io, try_tls_conn_read, conn_fd, EV_READ);
521 170 ev_io_init(&c->write_ev_io, try_tls_conn_write, conn_fd, EV_WRITE);
522 170 feer_tls_init_conn(c, lsnr->tls_ctx_ref);
523 } else
524 #endif
525 {
526 1278 ev_io_init(&c->read_ev_io, try_conn_read, conn_fd, EV_READ);
527 1278 ev_io_init(&c->write_ev_io, try_conn_write, conn_fd, EV_WRITE);
528 }
529 1448 ev_set_priority(&c->read_ev_io, srvr->read_priority);
530 1448 c->read_ev_io.data = (void *)c;
531  
532 1448 ev_set_priority(&c->write_ev_io, srvr->write_priority);
533 1448 c->write_ev_io.data = (void *)c;
534  
535 1448 ev_init(&c->read_ev_timer, conn_read_timeout);
536 1448 ev_set_priority(&c->read_ev_timer, srvr->read_priority);
537 1448 c->read_ev_timer.data = (void *)c;
538  
539 1448 ev_init(&c->header_ev_timer, conn_header_timeout);
540 1448 ev_set_priority(&c->header_ev_timer, srvr->read_priority);
541 1448 c->header_ev_timer.data = (void *)c;
542  
543 1448 ev_init(&c->write_ev_timer, conn_write_timeout);
544 1448 ev_set_priority(&c->write_ev_timer, srvr->write_priority);
545 1448 c->write_ev_timer.data = (void *)c;
546  
547 trace3("made conn fd=%d self=%p, c=%p, cur=%"Sz_uf", len=%"Sz_uf"\n",
548 c->fd, self, c, (Sz)SvCUR(self), (Sz)SvLEN(self));
549  
550 1448 SV *rv = newRV_inc(c->self);
551 1448 sv_bless(rv, feer_conn_stash); // so DESTROY can get called on read errors
552 1448 SvREFCNT_dec(rv);
553  
554 1448 SvREADONLY_on(self);
555 1448 srvr->active_conns++;
556 1448 return c;
557 }
558  
559 INLINE_UNLESS_DEBUG
560 static struct feer_conn *
561 3055 sv_2feer_conn (SV *rv)
562 {
563 3055 ▶ if (likely(SvROK(rv) && !SvGMAGICAL(rv))) {
Branch: 2 branches
BranchTrueFalseTotal
if (likely(SvROK(rv) && !SvGMAGICAL(rv))) { 3055 0 3055
if (likely(SvROK(rv) && !SvGMAGICAL(rv))) { 3055 0 3055
564 3055 SV *sv = SvRV(rv);
565 3055 ▶ if (likely(SvOBJECT(sv) && SvSTASH(sv) == feer_conn_stash))
Branch: 2 branches
BranchTrueFalseTotal
if (likely(SvOBJECT(sv) && SvSTASH(sv) == feer_conn_stash)) 3055 0 3055
if (likely(SvOBJECT(sv) && SvSTASH(sv) == feer_conn_stash)) 3055 0 3055
566 3055 return (struct feer_conn *)SvPVX(sv);
567 }
568 0 ▶ if (unlikely(!sv_isa(rv,"Feersum::Connection")))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!sv_isa(rv,"Feersum::Connection"))) 0 0 0
569 0 croak("object is not of type Feersum::Connection");
570 0 return (struct feer_conn *)SvPVX(SvRV(rv));
571 }
572  
573 INLINE_UNLESS_DEBUG
574 static SV*
575 993 feer_conn_2sv (struct feer_conn *c)
576 {
577 993 return newRV_inc(c->self);
578 }
579  
580 static feer_conn_handle *
581 46693 sv_2feer_conn_handle (SV *rv, bool can_croak)
582 {
583 trace3("sv 2 conn_handle\n");
584 46693 ▶ if (unlikely(!SvROK(rv))) croak("Expected a reference");
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!SvROK(rv))) croak("Expected a reference"); 0 46693 46693
585 // do not allow subclassing
586 46693 SV *sv = SvRV(rv);
587 46693 ▶ if (likely(
Branch: 4 branches
BranchTrueFalseTotal
if (likely( 46693 0 46693
if (likely( 359 46334 46693
if (likely( 359 0 359
if (likely( 46693 0 46693
588 sv_isobject(rv) &&
589 (SvSTASH(sv) == feer_conn_writer_stash ||
590 SvSTASH(sv) == feer_conn_reader_stash)
591 )) {
592 46693 ▶ UV uv = SvIOK(sv) ? SvUVX(sv) : SvUV(sv);
Branch: 1 branch
BranchTrueFalseTotal
UV uv = SvIOK(sv) ? SvUVX(sv) : SvUV(sv); 46693 0 46693
593 46693 ▶ if (uv == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (uv == 0) { 15 46678 46693
594 15 ▶ if (can_croak) croak("Operation not allowed: Handle is closed.");
Branch: 1 branch
BranchTrueFalseTotal
if (can_croak) croak("Operation not allowed: Handle is closed."); 15 0 15
595 0 return NULL;
596 }
597 46678 return INT2PTR(feer_conn_handle*,uv);
598 }
599  
600 0 ▶ if (can_croak)
Branch: 1 branch
BranchTrueFalseTotal
if (can_croak) 0 0 0
601 0 croak("Expected a Feersum::Connection::Writer or ::Reader object");
602 0 return NULL;
603 }
604  
605 /* DESTROY as free magic, without the method dispatch */
606 static int
607 47453 feer_handle_svt_free (pTHX_ SV *sv, MAGIC *mg)
608 {
609 47453 struct feer_conn *c = INT2PTR(struct feer_conn *, SvUVX(sv));
610 47453 ▶ if (!c) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!c) return 0; 46302 1151 47453
611 1151 SvUVX(sv) = 0;
612 /* closing can run Perl (an H2 sibling's poll_cb): own stack, as curse() */
613 1151 dSP;
614 1151 PUSHSTACKi(PERLSI_MAGIC);
615 1151 ▶ if (mg->mg_private)
Branch: 1 branch
BranchTrueFalseTotal
if (mg->mg_private) 64 1087 1151
616 64 feersum_close_handle(aTHX_ c, 1);
617 else
618 1087 SvREFCNT_dec(c->self);
619 1151 POPSTACK;
620 PERL_UNUSED_VAR(sp);
621 1151 return 0;
622 }
623  
624 static SV *
625 47453 new_feer_conn_handle (pTHX_ struct feer_conn *c, bool is_writer)
626 {
627 SV *sv, *obj;
628 47453 SvREFCNT_inc_void_NN(c->self);
629 47453 obj = newSVuv(PTR2UV(c));
630 47453 MAGIC *mg = sv_magicext(obj, NULL, PERL_MAGIC_ext, &feer_handle_vtbl, NULL, 0);
631 47453 mg->mg_private = is_writer;
632 47453 sv = newRV_noinc(obj);
633 47453 ▶ sv_bless(sv, is_writer ? feer_conn_writer_stash : feer_conn_reader_stash);
Branch: 1 branch
BranchTrueFalseTotal
sv_bless(sv, is_writer ? feer_conn_writer_stash : feer_conn_reader_stash); 46188 1265 47453
634 /* Pin the referent while the guarded callback runs (see pinned_writer);
635 * the !pinned_writer gate keeps a nested poll_cb's handle off the slot. */
636 47453 ▶ if (is_writer && unlikely(c->pin_writer) && !c->pinned_writer)
Branch: 3 branches
BranchTrueFalseTotal
if (is_writer && unlikely(c->pin_writer) && !c->pinned_writer) 46188 1265 47453
if (is_writer && unlikely(c->pin_writer) && !c->pinned_writer) 304 45884 46188
if (is_writer && unlikely(c->pin_writer) && !c->pinned_writer) 304 0 304
637 304 c->pinned_writer = SvREFCNT_inc_NN(SvRV(sv));
638 47453 return sv;
639 }
640  
641 static void
642 848 init_feer_server (struct feer_server *s)
643 {
644 848 Zero(s, 1, struct feer_server);
645 848 s->read_timeout = READ_TIMEOUT;
646 848 s->header_timeout = HEADER_TIMEOUT;
647 848 s->write_timeout = WRITE_TIMEOUT;
648 848 s->linger_timeout = LINGER_TIMEOUT;
649 848 s->eof_park_timeout = EOF_PARK_TIMEOUT;
650 848 s->max_accept_per_loop = DEFAULT_MAX_ACCEPT_PER_LOOP;
651 848 s->max_connections = 10000;
652 848 s->max_read_buf = MAX_READ_BUF;
653 848 s->max_body_len = MAX_BODY_LEN;
654 848 s->max_uri_len = MAX_URI_LEN;
655 848 s->psgix_io = true;
656 #ifdef FEERSUM_HAS_H2
657 s->max_h2_concurrent_streams = FEER_H2_MAX_CONCURRENT_STREAMS;
658 s->max_h2_conn_body = 0;
659 #endif
660 14416 ▶ for (int i = 0; i < FEER_MAX_LISTENERS; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < FEER_MAX_LISTENERS; i++) { 13568 848 14416
661 13568 s->listeners[i].fd = -1;
662 13568 s->listeners[i].server = s;
663 }
664 848 }
665  
666 static struct feer_server *
667 848 new_feer_server (pTHX)
668 {
669 848 SV *self = newSV(0);
670 848 ▶ SvUPGRADE(self, SVt_PVMG);
Branch: 1 branch
BranchTrueFalseTotal
SvUPGRADE(self, SVt_PVMG); 848 0 848
671 848 ▶ SvGROW(self, sizeof(struct feer_server));
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(self, sizeof(struct feer_server)); 848 0 848
SvGROW(self, sizeof(struct feer_server)); 848 0 848
672 848 SvPOK_only(self);
673 848 SvIOK_on(self);
674  
675 848 struct feer_server *s = (struct feer_server *)SvPVX(self);
676 848 init_feer_server(s);
677 848 s->self = self;
678  
679 848 SV *rv = newRV_inc(self);
680 848 sv_bless(rv, feer_stash);
681 848 SvREFCNT_dec(rv);
682  
683 848 SvREADONLY_on(self);
684 848 return s;
685 }
686  
687 INLINE_UNLESS_DEBUG
688 static struct feer_server *
689 31251 sv_2feer_server (SV *rv)
690 {
691 31251 ▶ if (sv_isa(rv, "Feersum")) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_isa(rv, "Feersum")) { 31218 33 31251
692 31218 return (struct feer_server *)SvPVX(SvRV(rv));
693 }
694 66 ▶ if (SvPOK(rv) && strEQ(SvPV_nolen(rv), "Feersum")) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvPOK(rv) && strEQ(SvPV_nolen(rv), "Feersum")) { 33 0 33
if (SvPOK(rv) && strEQ(SvPV_nolen(rv), "Feersum")) { 33 0 33
695 33 ▶ if (unlikely(!default_server))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!default_server)) 0 33 33
696 0 croak("Feersum: no server instance (call Feersum->new first)");
697 33 return default_server;
698 }
699 0 croak("object is not of type Feersum");
700 return NULL; // unreachable
701 }
702  
703 INLINE_UNLESS_DEBUG
704 static SV*
705 690 feer_server_2sv (struct feer_server *s)
706 {
707 690 return newRV_inc(s->self);
708 }
709  
710 INLINE_UNLESS_DEBUG static void
711 1743 start_read_watcher(struct feer_conn *c) {
712 ASSERT_EV_LOOP_INITIALIZED();
713 #ifdef FEERSUM_HAS_TLS
714 /* No input past close_notify, but linger still drains raw peer bytes */
715 1743 ▶ if (unlikely(c->tls_input_eof && !c->closing_linger))
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(c->tls_input_eof && !c->closing_linger)) 0 1743 1743
if (unlikely(c->tls_input_eof && !c->closing_linger)) 0 0 0
716 0 return;
717 #endif
718 1743 ▶ if (unlikely(ev_is_active(&c->read_ev_io)))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(ev_is_active(&c->read_ev_io))) 403 1340 1743
719 403 return;
720 trace("start read watcher %d\n",c->fd);
721 1340 ev_io_start(feersum_ev_loop, &c->read_ev_io);
722 1340 SvREFCNT_inc_void_NN(c->self);
723 }
724  
725 INLINE_UNLESS_DEBUG static void
726 4175 stop_read_watcher(struct feer_conn *c) {
727 4175 ▶ if (unlikely(!ev_is_active(&c->read_ev_io)))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!ev_is_active(&c->read_ev_io))) 3116 1059 4175
728 3116 return;
729 trace("stop read watcher %d\n",c->fd);
730 1059 ev_io_stop(feersum_ev_loop, &c->read_ev_io);
731 1059 SvREFCNT_dec(c->self);
732 }
733  
734 /* True only while the send queue shrinks: a peer that stopped reading holds
735 * it steady and stays reapable. */
736 INLINE_UNLESS_DEBUG static int
737 6 feer_conn_still_delivering(struct feer_conn *c) {
738 #ifdef FEERSUM_HAS_OUTQ
739 6 int outq = feer_sock_outq(c->fd);
740 6 ▶ if (outq > 0 && (c->prev_read_outq <= 0 || outq < c->prev_read_outq)) {
Branch: 3 branches
BranchTrueFalseTotal
if (outq > 0 && (c->prev_read_outq <= 0 || outq < c->prev_read_outq)) { 5 1 6
if (outq > 0 && (c->prev_read_outq <= 0 || outq < c->prev_read_outq)) { 3 2 5
if (outq > 0 && (c->prev_read_outq <= 0 || outq < c->prev_read_outq)) { 2 1 3
741 4 c->prev_read_outq = outq;
742 4 return 1;
743 }
744 2 c->prev_read_outq = 0;
745 #else
746 PERL_UNUSED_ARG(c);
747 #endif
748 2 return 0;
749 }
750  
751 INLINE_UNLESS_DEBUG static void
752 2336 restart_read_timer(struct feer_conn *c) {
753 /* While lingering this timer is the one-shot linger deadline, and
754 * ev_timer_again would stop it rather than restart it. */
755 2336 ▶ if (unlikely(c->fd < 0 || c->closing_linger))
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(c->fd < 0 || c->closing_linger)) 2336 0 2336
if (unlikely(c->fd < 0 || c->closing_linger)) 0 2336 2336
756 0 return;
757 2336 ▶ if (likely(!ev_is_active(&c->read_ev_timer))) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(!ev_is_active(&c->read_ev_timer))) { 1561 775 2336
758 trace("restart read timer %d\n",c->fd);
759 1561 c->read_ev_timer.repeat = c->cached_read_timeout;
760 1561 SvREFCNT_inc_void_NN(c->self);
761 }
762 2336 ev_timer_again(feersum_ev_loop, &c->read_ev_timer);
763 }
764  
765 INLINE_UNLESS_DEBUG static void
766 4177 stop_read_timer(struct feer_conn *c) {
767 4177 ▶ if (unlikely(!ev_is_active(&c->read_ev_timer)))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!ev_is_active(&c->read_ev_timer))) 1934 2243 4177
768 1934 return;
769 trace("stop read timer %d\n",c->fd);
770 2243 ev_timer_stop(feersum_ev_loop, &c->read_ev_timer);
771 2243 SvREFCNT_dec(c->self);
772 }
773  
774 /* Pure C, no on_eof: the peer_is_gone() predicate calls it */
775 INLINE_UNLESS_DEBUG static void
776 302 feer_latch_input_eof(struct feer_conn *c)
777 {
778 302 ▶ if (c->input_eof_seen) return;
Branch: 1 branch
BranchTrueFalseTotal
if (c->input_eof_seen) return; 260 42 302
779 #ifdef FEERSUM_HAS_TLS
780 /* Tunnels and takeovers own their input; the EOF park does not bound them */
781 42 ▶ if (c->tls_tunnel) return;
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_tunnel) return; 7 35 42
782 #endif
783 35 ▶ if (c->io_taken) return;
Branch: 1 branch
BranchTrueFalseTotal
if (c->io_taken) return; 0 35 35
784 35 c->input_eof_seen = 1;
785 35 c->eof_quiet_since = ev_now(feersum_ev_loop);
786 35 c->eof_writes_seen = c->poll_writes_seen;
787 }
788  
789 static void
790 591 feersum_maybe_fire_on_eof(struct feer_conn *c)
791 {
792 591 ▶ if (c->on_eof_cb && c->input_eof_seen && !c->on_eof_fired
Branch: 3 branches
BranchTrueFalseTotal
if (c->on_eof_cb && c->input_eof_seen && !c->on_eof_fired 575 16 591
if (c->on_eof_cb && c->input_eof_seen && !c->on_eof_fired 306 269 575
if (c->on_eof_cb && c->input_eof_seen && !c->on_eof_fired 35 271 306
793 35 ▶ && c->responding == RESPOND_STREAMING) {
Branch: 1 branch
BranchTrueFalseTotal
&& c->responding == RESPOND_STREAMING) { 35 0 35
794 35 c->on_eof_fired = 1;
795 35 call_on_eof_callback(c);
796 }
797 591 }
798  
799 static void
800 9 feersum_note_input_eof(struct feer_conn *c)
801 {
802 9 feer_latch_input_eof(c);
803 9 feersum_maybe_fire_on_eof(c);
804 9 }
805  
806 INLINE_UNLESS_DEBUG static bool
807 526 feer_eof_park_expired(struct feer_conn *c)
808 {
809 526 ▶ if (!c->input_eof_seen || c->cached_eof_park_timeout <= 0.0) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!c->input_eof_seen || c->cached_eof_park_timeout <= 0.0) return 0; 304 222 526
if (!c->input_eof_seen || c->cached_eof_park_timeout <= 0.0) return 0; 16 288 304
810 288 ▶ if (c->poll_writes_seen != c->eof_writes_seen) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->poll_writes_seen != c->eof_writes_seen) { 10 278 288
811 10 c->eof_writes_seen = c->poll_writes_seen;
812 10 c->eof_quiet_since = ev_now(feersum_ev_loop);
813 10 return 0;
814 }
815 278 return ev_now(feersum_ev_loop) - c->eof_quiet_since
816 278 >= c->cached_eof_park_timeout;
817 }
818  
819 /* A peek of 0 is a half-close, not a gone peer; only a hard error is */
820 INLINE_UNLESS_DEBUG static bool
821 526 peer_is_gone(struct feer_conn *c)
822 {
823 char probe;
824 ssize_t n;
825 526 ▶ if (c->fd < 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (c->fd < 0) return 1; 0 526 526
826 do {
827 526 n = recv(c->fd, &probe, 1, MSG_PEEK | MSG_DONTWAIT);
828 526 ▶ } while (n < 0 && errno == EINTR);
Branch: 2 branches
BranchTrueFalseTotal
} while (n < 0 && errno == EINTR); 233 293 526
} while (n < 0 && errno == EINTR); 0 233 233
829 526 ▶ if (n == 0) feer_latch_input_eof(c);
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) feer_latch_input_eof(c); 293 233 526
830 526 ▶ return n < 0 && errno != EAGAIN && errno != EWOULDBLOCK;
Branch: 3 branches
BranchTrueFalseTotal
return n < 0 && errno != EAGAIN && errno != EWOULDBLOCK; 233 293 526
return n < 0 && errno != EAGAIN && errno != EWOULDBLOCK; 0 233 233
return n < 0 && errno != EAGAIN && errno != EWOULDBLOCK; 0 0 0
831 }
832  
833 INLINE_UNLESS_DEBUG static bool
834 24 feer_conn_has_pending_input(struct feer_conn *c)
835 {
836 char probe;
837 ssize_t n;
838 24 ▶ if (c->rbuf && SvCUR(c->rbuf) > 0) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (c->rbuf && SvCUR(c->rbuf) > 0) return 1; 24 0 24
if (c->rbuf && SvCUR(c->rbuf) > 0) return 1; 0 24 24
839 #ifdef FEERSUM_HAS_TLS
840 24 ▶ if (c->tls_rbuf_len > 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_rbuf_len > 0) return 1; 0 24 24
841 #endif
842 24 ▶ if (c->fd < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (c->fd < 0) return 0; 0 24 24
843 do {
844 24 n = recv(c->fd, &probe, 1, MSG_PEEK | MSG_DONTWAIT);
845 24 ▶ } while (n < 0 && errno == EINTR);
Branch: 2 branches
BranchTrueFalseTotal
} while (n < 0 && errno == EINTR); 23 1 24
} while (n < 0 && errno == EINTR); 0 23 23
846 24 return n > 0;
847 }
848  
849 INLINE_UNLESS_DEBUG static void
850 92741 start_write_watcher(struct feer_conn *c) {
851 ASSERT_EV_LOOP_INITIALIZED();
852 92741 ▶ if (unlikely(ev_is_active(&c->write_ev_io)))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(ev_is_active(&c->write_ev_io))) 91485 1256 92741
853 91485 return;
854 trace("start write watcher %d\n",c->fd);
855 1256 ev_io_start(feersum_ev_loop, &c->write_ev_io);
856 1256 SvREFCNT_inc_void_NN(c->self);
857 }
858  
859 INLINE_UNLESS_DEBUG static void
860 4260 stop_write_watcher(struct feer_conn *c) {
861 4260 ▶ if (unlikely(!ev_is_active(&c->write_ev_io)))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!ev_is_active(&c->write_ev_io))) 3004 1256 4260
862 3004 return;
863 trace("stop write watcher %d\n",c->fd);
864 1256 ev_io_stop(feersum_ev_loop, &c->write_ev_io);
865 1256 SvREFCNT_dec(c->self);
866 }
867  
868 /* io()/psgix.io over TLS: the app owns the stream until it closes its end */
869 INLINE_UNLESS_DEBUG static bool
870 60 write_deadline_exempt(struct feer_conn *c) {
871 #ifdef FEERSUM_HAS_TLS
872 60 ▶ return c->tls_tunnel && !c->tls_tunnel_eof;
Branch: 2 branches
BranchTrueFalseTotal
return c->tls_tunnel && !c->tls_tunnel_eof; 0 60 60
return c->tls_tunnel && !c->tls_tunnel_eof; 0 0 0
873 #else
874 PERL_UNUSED_VAR(c);
875 return 0;
876 #endif
877 }
878  
879 INLINE_UNLESS_DEBUG static void
880 47285 restart_write_timer(struct feer_conn *c) {
881 /* Backoff resets on any progress, deadline or not; poll_retry_pending may
882 * only change where the timer is actually stopped or re-armed. */
883 47285 c->poll_retry_backoff = 0;
884 /* fresh bytes queued: the old outq sample means nothing now */
885 47285 c->prev_outq = 0;
886 47285 c->outq_stalls = 0;
887 47285 ▶ if (c->cached_write_timeout <= 0.0 || write_deadline_exempt(c)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (c->cached_write_timeout <= 0.0 || write_deadline_exempt(c)) return; 31 47254 47285
if (c->cached_write_timeout <= 0.0 || write_deadline_exempt(c)) return; 0 31 31
888 31 ▶ if (likely(!ev_is_active(&c->write_ev_timer))) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(!ev_is_active(&c->write_ev_timer))) { 1 30 31
889 trace("restart write timer %d\n",c->fd);
890 1 SvREFCNT_inc_void_NN(c->self);
891 }
892 /* a poll-retry repeat must not survive as the deadline interval */
893 31 c->poll_retry_pending = 0;
894 31 c->write_ev_timer.repeat = c->cached_write_timeout;
895 31 ev_timer_again(feersum_ev_loop, &c->write_ev_timer);
896 }
897  
898 /* Arm the write deadline without resetting a running one: "blocked on
899 * writability" recurs many times a second and must not push it back. */
900 INLINE_UNLESS_DEBUG static void
901 45665 ensure_write_timer(struct feer_conn *c) {
902 45665 ▶ if (c->cached_write_timeout <= 0.0 || write_deadline_exempt(c)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (c->cached_write_timeout <= 0.0 || write_deadline_exempt(c)) return; 29 45636 45665
if (c->cached_write_timeout <= 0.0 || write_deadline_exempt(c)) return; 0 29 29
903 /* armed as a poll retry: the deadline takes the timer back */
904 29 ▶ if (ev_is_active(&c->write_ev_timer) && likely(!c->poll_retry_pending))
Branch: 2 branches
BranchTrueFalseTotal
if (ev_is_active(&c->write_ev_timer) && likely(!c->poll_retry_pending)) 29 0 29
if (ev_is_active(&c->write_ev_timer) && likely(!c->poll_retry_pending)) 29 0 29
905 29 return;
906 trace("arm write timer %d\n",c->fd);
907 0 ▶ if (likely(!ev_is_active(&c->write_ev_timer)))
Branch: 1 branch
BranchTrueFalseTotal
if (likely(!ev_is_active(&c->write_ev_timer))) 0 0 0
908 0 SvREFCNT_inc_void_NN(c->self);
909 0 c->poll_retry_pending = 0;
910 0 c->poll_retry_backoff = 0;
911 0 c->write_ev_timer.repeat = c->cached_write_timeout;
912 0 ev_timer_again(feersum_ev_loop, &c->write_ev_timer);
913 }
914  
915 INLINE_UNLESS_DEBUG static void
916 3811 stop_write_timer(struct feer_conn *c) {
917 /* a pending poll retry dies with the timer */
918 3811 c->poll_retry_pending = 0;
919 3811 c->poll_retry_backoff = 0;
920 3811 ▶ if (unlikely(!ev_is_active(&c->write_ev_timer)))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!ev_is_active(&c->write_ev_timer))) 3803 8 3811
921 3803 return;
922 trace("stop write timer %d\n",c->fd);
923 8 ev_timer_stop(feersum_ev_loop, &c->write_ev_timer);
924 8 SvREFCNT_dec(c->self);
925 }
926  
927 /* A write poll_cb that wrote nothing: re-arming the level-triggered watcher
928 * would re-deliver it every iteration, so park and pace the retry instead. */
929 static void
930 485 feersum_poll_retry_park(struct feer_conn *c)
931 {
932 double iv;
933 485 stop_write_watcher(c);
934 485 iv = FEER_POLL_RETRY_MIN * (double)(1u << c->poll_retry_backoff);
935 485 ▶ if (c->input_eof_seen) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->input_eof_seen) { 272 213 485
936 272 ▶ if (iv > FEER_POLL_RETRY_EOF_MAX) iv = FEER_POLL_RETRY_EOF_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (iv > FEER_POLL_RETRY_EOF_MAX) iv = FEER_POLL_RETRY_EOF_MAX; 21 251 272
937 }
938 213 ▶ else if (iv > FEER_POLL_RETRY_MAX) iv = FEER_POLL_RETRY_MAX;
Branch: 1 branch
BranchTrueFalseTotal
else if (iv > FEER_POLL_RETRY_MAX) iv = FEER_POLL_RETRY_MAX; 134 79 213
939 485 ▶ if (c->poll_retry_backoff < FEER_POLL_RETRY_BACKOFF_CAP)
Branch: 1 branch
BranchTrueFalseTotal
if (c->poll_retry_backoff < FEER_POLL_RETRY_BACKOFF_CAP) 348 137 485
940 348 c->poll_retry_backoff++;
941 485 c->poll_retry_pending = 1;
942 485 ▶ if (likely(!ev_is_active(&c->write_ev_timer)))
Branch: 1 branch
BranchTrueFalseTotal
if (likely(!ev_is_active(&c->write_ev_timer))) 475 10 485
943 475 SvREFCNT_inc_void_NN(c->self);
944 485 c->write_ev_timer.repeat = iv;
945 485 ev_timer_again(feersum_ev_loop, &c->write_ev_timer);
946 trace("poll_cb declined, parked fd=%d retry in %.3fs\n", c->fd, iv);
947 485 }
948  
949 INLINE_UNLESS_DEBUG static void
950 5867 stop_header_timer(struct feer_conn *c) {
951 5867 ▶ if (unlikely(!ev_is_active(&c->header_ev_timer)))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!ev_is_active(&c->header_ev_timer))) 4217 1650 5867
952 4217 return;
953 trace("stop header timer %d\n", c->fd);
954 1650 ev_timer_stop(feersum_ev_loop, &c->header_ev_timer);
955 1650 SvREFCNT_dec(c->self);
956 }
957  
958 INLINE_UNLESS_DEBUG static void
959 217 restart_header_timer(struct feer_conn *c) {
960 217 double timeout = c->server->header_timeout;
961 217 ▶ if (timeout <= 0.0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (timeout <= 0.0) return; 0 217 217
962 217 stop_header_timer(c);
963 217 ev_timer_set(&c->header_ev_timer, timeout, 0.0);
964 217 ev_timer_start(feersum_ev_loop, &c->header_ev_timer);
965 217 SvREFCNT_inc_void_NN(c->self);
966 }
967  
968 INLINE_UNLESS_DEBUG static void
969 2144 stop_all_watchers(struct feer_conn *c) {
970 /* Before the watcher refs drop: a guard or io body can hold
971 * env -> psgi.input -> c, a cycle only this cleanup breaks. */
972 2144 feersum_queue_response_cleanup(c);
973 2144 stop_read_watcher(c);
974 2144 stop_read_timer(c);
975 2144 stop_header_timer(c);
976 2144 stop_write_watcher(c);
977 2144 stop_write_timer(c);
978 2144 }
979  
980 INLINE_UNLESS_DEBUG static void
981 1118 feer_shutdown_conn(struct feer_conn *c, const char *where) {
982 1118 stop_all_watchers(c);
983 1118 safe_close_conn(c, where);
984 1118 change_responding_state(c, RESPOND_SHUTDOWN);
985 1118 }
986  
987 static void
988 3759 feer_conn_set_busy(struct feer_conn *c)
989 {
990 3759 ▶ if (c->idle_rinq_node) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->idle_rinq_node) { 774 2985 3759
991 774 rinq_remove(&c->server->idle_keepalive_rinq, c->idle_rinq_node);
992 774 c->idle_rinq_node = NULL;
993 }
994 3759 }
995  
996 /* An idle keepalive conn is evictable, so it counts as free capacity */
997 static void
998 2467 feer_server_resume_capacity(struct feer_server *server)
999 {
1000 2467 ▶ if (server->shutting_down) return;
Branch: 1 branch
BranchTrueFalseTotal
if (server->shutting_down) return; 4 2463 2467
1001  
1002 2463 int socket_conns = server->active_conns;
1003 #ifdef FEERSUM_HAS_H2
1004 socket_conns -= server->active_h2_streams;
1005 #endif
1006 2463 ▶ if (server->max_connections > 0 && socket_conns >= server->max_connections
Branch: 2 branches
BranchTrueFalseTotal
if (server->max_connections > 0 && socket_conns >= server->max_connections 2367 96 2463
if (server->max_connections > 0 && socket_conns >= server->max_connections 31 2336 2367
1007 31 ▶ && !server->idle_keepalive_rinq)
Branch: 1 branch
BranchTrueFalseTotal
&& !server->idle_keepalive_rinq) 0 31 31
1008 0 return;
1009  
1010 int i;
1011 5357 ▶ for (i = 0; i < server->n_listeners; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < server->n_listeners; i++) { 2894 2463 5357
1012 2894 struct feer_listen *lsnr = &server->listeners[i];
1013 2894 ▶ if (lsnr->pause_flags & FEER_PAUSE_CAP) {
Branch: 1 branch
BranchTrueFalseTotal
if (lsnr->pause_flags & FEER_PAUSE_CAP) { 102 2792 2894
1014 102 lsnr->pause_flags &= ~FEER_PAUSE_CAP;
1015 102 ▶ if (!lsnr->pause_flags && lsnr->fd >= 0)
Branch: 2 branches
BranchTrueFalseTotal
if (!lsnr->pause_flags && lsnr->fd >= 0) 75 27 102
if (!lsnr->pause_flags && lsnr->fd >= 0) 75 0 75
1016 75 ev_io_start(feersum_ev_loop, &lsnr->accept_w);
1017 }
1018 }
1019 }
1020  
1021 static void
1022 1091 feer_conn_set_idle(struct feer_conn *c)
1023 {
1024 1091 ▶ if (c->idle_rinq_node) return;
Branch: 1 branch
BranchTrueFalseTotal
if (c->idle_rinq_node) return; 2 1089 1091
1025  
1026 1089 c->idle_rinq_node = rinq_push(&c->server->idle_keepalive_rinq, c);
1027 trace("conn fd=%d is now idle (added to MRU)\n", c->fd);
1028 #ifdef FEERSUM_HAS_TLS
1029 1089 feer_tls_note_idle(c);
1030 #endif
1031 1089 feer_server_resume_capacity(c->server);
1032 }
1033  
1034 static int
1035 286 feer_server_recycle_idle_conn(struct feer_server *srvr)
1036 {
1037 286 struct feer_conn *c = NULL;
1038 287 ▶ while (srvr->idle_keepalive_rinq) {
Branch: 1 branch
BranchTrueFalseTotal
while (srvr->idle_keepalive_rinq) { 25 262 287
1039 struct feer_conn *cand =
1040 25 (struct feer_conn *)rinq_shift(&srvr->idle_keepalive_rinq);
1041 25 ▶ if (unlikely(!cand)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!cand)) break; 0 25 25
1042 25 cand->idle_rinq_node = NULL;
1043 /* Closing with unread input would RST the arrived request away; its
1044 * read watcher serves it. Lingering conns only drain junk. */
1045 25 ▶ if (!cand->closing_linger && feer_conn_has_pending_input(cand)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!cand->closing_linger && feer_conn_has_pending_input(cand)) { 24 1 25
if (!cand->closing_linger && feer_conn_has_pending_input(cand)) { 1 23 24
1046 trace("idle conn fd=%d has pending input, not recycling it\n", cand->fd);
1047 1 continue;
1048 }
1049 24 c = cand;
1050 24 break;
1051 }
1052 286 ▶ if (!c) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!c) return 0; 262 24 286
1053  
1054 trace("recycling idle keepalive conn fd=%d to make room for new accept\n", c->fd);
1055  
1056 // Only watcher refs keep the conn alive: stop_all_watchers could DESTROY
1057 // it before safe_close_conn runs.
1058 24 SvREFCNT_inc_void_NN(c->self);
1059 #ifdef FEERSUM_HAS_H2
1060 /* GOAWAY before the FIN (RFC 9113 6.8) */
1061 if (c->h2_session && !c->h2_goaway_sent) {
1062 nghttp2_submit_goaway(c->h2_session, NGHTTP2_FLAG_NONE,
1063 nghttp2_session_get_last_proc_stream_id(c->h2_session),
1064 NGHTTP2_NO_ERROR, NULL, 0);
1065 c->h2_goaway_sent = 1;
1066 feer_h2_session_send(c);
1067 }
1068 #endif
1069 24 feer_shutdown_conn(c, "recycled for new connection");
1070 24 SvREFCNT_dec(c->self);
1071 24 return 1;
1072 }
1073  
1074  
1075 /* ------------------------- lingering close ------------------------- */
1076  
1077 /* An empty wbuf_rinq means the kernel took the bytes, not that the peer got
1078 * them, and a byte arriving after close() RSTs the queued response away.
1079 * So: shutdown(SHUT_WR), then drain until EOF, a byte cap or a deadline. */
1080  
1081 static void
1082 1051 linger_read_cb(EV_P_ ev_io *w, int revents)
1083 {
1084 1051 dCONN;
1085 char buf[8192];
1086 ssize_t n;
1087  
1088 1051 SvREFCNT_inc_void_NN(c->self);
1089  
1090 1051 ▶ if (unlikely(revents & EV_ERROR && !(revents & EV_READ))) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(revents & EV_ERROR && !(revents & EV_READ))) { 0 1051 1051
if (unlikely(revents & EV_ERROR && !(revents & EV_READ))) { 0 0 0
1091 trace("linger read EV error fd=%d revents=0x%08x\n", w->fd, revents);
1092 0 feer_shutdown_conn(c, "linger EV error");
1093 0 goto linger_out;
1094 }
1095  
1096 do {
1097 1051 n = read(w->fd, buf, sizeof(buf));
1098 1051 ▶ } while (n < 0 && errno == EINTR);
Branch: 2 branches
BranchTrueFalseTotal
} while (n < 0 && errno == EINTR); 10 1041 1051
} while (n < 0 && errno == EINTR); 0 10 10
1099  
1100 1051 ▶ if (likely(n > 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(n > 0)) { 357 694 1051
1101 714 c->linger_left = ((size_t)n < c->linger_left)
1102 357 ▶ ? c->linger_left - (size_t)n : 0;
Branch: 1 branch
BranchTrueFalseTotal
? c->linger_left - (size_t)n : 0; 357 0 357
1103 trace("linger drained fd=%d left=%"Sz_uf"\n", w->fd, (Sz)c->linger_left);
1104 357 ▶ if (likely(c->linger_left > 0))
Branch: 1 branch
BranchTrueFalseTotal
if (likely(c->linger_left > 0)) 357 0 357
1105 357 goto linger_out;
1106 0 feer_shutdown_conn(c, "linger byte cap");
1107 }
1108 694 ▶ else if (n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (n == 0) { 684 10 694
1109 684 feer_shutdown_conn(c, "linger peer EOF");
1110 }
1111 10 ▶ else if (errno != EAGAIN && errno != EWOULDBLOCK) {
Branch: 2 branches
BranchTrueFalseTotal
else if (errno != EAGAIN && errno != EWOULDBLOCK) { 0 10 10
else if (errno != EAGAIN && errno != EWOULDBLOCK) { 0 10 10
1112 10 feer_shutdown_conn(c, "linger read error");
1113 }
1114 0 linger_out:
1115 1051 SvREFCNT_dec(c->self);
1116 1051 }
1117  
1118 static void
1119 25 linger_timeout_cb(EV_P_ ev_timer *w, int revents)
1120 {
1121 25 dCONN;
1122 25 SvREFCNT_inc_void_NN(c->self);
1123  
1124 25 ▶ if (unlikely(revents & EV_ERROR))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(revents & EV_ERROR)) 0 25 25
1125 0 trouble("EV error on linger timer, fd=%d revents=0x%08x\n",
1126 c->fd, revents);
1127  
1128 trace("linger deadline fd=%d\n", c->fd);
1129 25 feer_shutdown_conn(c, "linger deadline");
1130 /* libev already stopped this one-shot timer, so stop_read_timer released
1131 * nothing: balance the ev_timer_start reference here */
1132 25 SvREFCNT_dec(c->self);
1133  
1134 25 SvREFCNT_dec(c->self); /* protection */
1135 25 }
1136  
1137 /* Plain HTTP/1.x only, work complete, server not draining: a linger holds an
1138 * fd and a max_connections slot, which shutdown and eviction must free. */
1139 static void
1140 1343 feer_linger_close(struct feer_conn *c, const char *where)
1141 {
1142 1343 ▶ if (likely(c->fd >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (likely(c->fd >= 0) 1343 0 1343
1143 1343 ▶ && c->server->linger_timeout > 0.0
Branch: 1 branch
BranchTrueFalseTotal
&& c->server->linger_timeout > 0.0 1068 275 1343
1144 1068 ▶ && !c->server->shutting_down
Branch: 1 branch
BranchTrueFalseTotal
&& !c->server->shutting_down 1064 4 1068
1145 1064 ▶ && !c->closing_linger
Branch: 1 branch
BranchTrueFalseTotal
&& !c->closing_linger 1064 0 1064
1146 1064 ▶ && !c->wbuf_rinq
Branch: 1 branch
BranchTrueFalseTotal
&& !c->wbuf_rinq 1056 8 1064
1147 1056 ▶ && (c->responding == RESPOND_SHUTDOWN
Branch: 1 branch
BranchTrueFalseTotal
&& (c->responding == RESPOND_SHUTDOWN 2 1054 1056
1148 2 ▶ || c->responding == RESPOND_NOT_STARTED)
Branch: 1 branch
BranchTrueFalseTotal
|| c->responding == RESPOND_NOT_STARTED) 2 0 2
1149 #ifdef __linux__
1150 1056 ▶ && c->sendfile_fd < 0
Branch: 1 branch
BranchTrueFalseTotal
&& c->sendfile_fd < 0 1056 0 1056
1151 #endif
1152 #ifdef FEERSUM_HAS_TLS
1153 1056 ▶ && !c->tls
Branch: 1 branch
BranchTrueFalseTotal
&& !c->tls 988 68 1056
1154 #endif
1155 #ifdef FEERSUM_HAS_H2
1156 && !c->h2_session && !c->is_h2_stream
1157 #endif
1158 ) {
1159 trace("linger begin fd=%d (%s)\n", c->fd, where);
1160 988 ▶ if (unlikely(shutdown(c->fd, SHUT_WR) < 0 && errno != ENOTCONN))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(shutdown(c->fd, SHUT_WR) < 0 && errno != ENOTCONN)) 5 983 988
1161 trace("linger shutdown fd=%d: %s\n", c->fd, strerror(errno));
1162 988 stop_all_watchers(c);
1163 988 change_responding_state(c, RESPOND_SHUTDOWN);
1164 988 change_receiving_state(c, RECEIVE_SHUTDOWN);
1165 988 c->closing_linger = 1;
1166 988 c->linger_left = LINGER_MAX_BYTES;
1167 988 ev_io_init(&c->read_ev_io, linger_read_cb, c->fd, EV_READ);
1168 988 ev_set_priority(&c->read_ev_io, c->server->read_priority);
1169 988 c->read_ev_io.data = (void *)c;
1170 988 start_read_watcher(c);
1171 988 ev_timer_init(&c->read_ev_timer, linger_timeout_cb,
1172 c->server->linger_timeout, 0.0);
1173 988 ev_set_priority(&c->read_ev_timer, c->server->read_priority);
1174 988 c->read_ev_timer.data = (void *)c;
1175 988 ev_timer_start(feersum_ev_loop, &c->read_ev_timer);
1176 988 SvREFCNT_inc_void_NN(c->self);
1177 /* Fire response_guard now, not after the drain. NULL before dec: its
1178 * DESTROY may register a replacement guard. */
1179 988 { SV *g = c->ext_guard; c->ext_guard = NULL; SvREFCNT_dec(g); }
1180 /* idle-listed so eviction and the shutdown sweep can reap it early */
1181 988 feer_conn_set_idle(c);
1182 988 return;
1183 }
1184 355 feer_shutdown_conn(c, where);
1185 }
1186  
1187 /* Shutdown-path linger for TLS/H2 (alert and FIN already queued), with its
1188 * own short cap. Returns 1 if the fd is now held. */
1189 static int
1190 0 feer_arm_linger(struct feer_conn *c, const char *where)
1191 {
1192 0 double cap = c->server->linger_timeout;
1193 0 ▶ if (c->fd < 0 || c->closing_linger)
Branch: 2 branches
BranchTrueFalseTotal
if (c->fd < 0 || c->closing_linger) 0 0 0
if (c->fd < 0 || c->closing_linger) 0 0 0
1194 0 return 0;
1195 0 ▶ if (cap <= 0.0 || cap > FEER_SHUTDOWN_LINGER_MAX)
Branch: 2 branches
BranchTrueFalseTotal
if (cap <= 0.0 || cap > FEER_SHUTDOWN_LINGER_MAX) 0 0 0
if (cap <= 0.0 || cap > FEER_SHUTDOWN_LINGER_MAX) 0 0 0
1196 0 cap = FEER_SHUTDOWN_LINGER_MAX;
1197  
1198 trace("shutdown linger begin fd=%d (%s)\n", c->fd, where);
1199 0 c->want_linger_close = 0; /* the drain's own close must not linger again */
1200 0 c->closing_linger = 1;
1201 0 c->linger_left = LINGER_MAX_BYTES;
1202 0 stop_all_watchers(c);
1203 0 change_receiving_state(c, RECEIVE_SHUTDOWN);
1204 0 ev_io_init(&c->read_ev_io, linger_read_cb, c->fd, EV_READ);
1205 0 ev_set_priority(&c->read_ev_io, c->server->read_priority);
1206 0 c->read_ev_io.data = (void *)c;
1207 0 start_read_watcher(c);
1208 0 ev_timer_init(&c->read_ev_timer, linger_timeout_cb, cap, 0.0);
1209 0 ev_set_priority(&c->read_ev_timer, c->server->read_priority);
1210 0 c->read_ev_timer.data = (void *)c;
1211 0 ev_timer_start(feersum_ev_loop, &c->read_ev_timer);
1212 0 SvREFCNT_inc_void_NN(c->self);
1213 0 return 1;
1214 }
1215  
1216 static void
1217 1182 process_request_ready_rinq (struct feer_server *server)
1218 {
1219 #ifdef FEERSUM_HAS_H2
1220 /* one H2 session_send per connection for the whole batch */
1221 server->h2_dispatch_depth++;
1222 #endif
1223 2568 ▶ while (server->request_ready_rinq) {
Branch: 1 branch
BranchTrueFalseTotal
while (server->request_ready_rinq) { 1386 1182 2568
1224 struct feer_conn *c =
1225 1386 (struct feer_conn *)rinq_shift(&server->request_ready_rinq);
1226 1386 ▶ if (unlikely(!c)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!c)) break; 0 1386 1386
1227  
1228 #ifdef FEERSUM_HAS_H2
1229 /* A reset or fatal conn error can strand a queued stream, and linger
1230 * may keep it allocated after its response path is gone. */
1231 if (unlikely(c->is_h2_stream)) {
1232 struct feer_h2_stream *stream = H2_STREAM_FROM_PC(c);
1233 struct feer_conn *parent = stream ? stream->parent : NULL;
1234 if (unlikely(!parent || parent->fd < 0 || parent->closing_linger
1235 || parent->h2_close_pending)) {
1236 SvREFCNT_dec(c->self);
1237 continue;
1238 }
1239 }
1240 #endif
1241  
1242 /* a response ended twice once left a queued conn with no request */
1243 1386 ▶ if (unlikely(!c->req)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!c->req)) { 0 1386 1386
1244 0 trouble("dispatch without a request fd=%d\n", c->fd);
1245 #ifdef FEERSUM_HAS_H2
1246 if (!c->is_h2_stream)
1247 #endif
1248 0 feer_shutdown_conn(c, "dispatch without a request");
1249 0 SvREFCNT_dec(c->self);
1250 0 continue;
1251 }
1252  
1253 #ifdef FEERSUM_HAS_H2
1254 /* only consecutive H2 streams share a flush: H1 work must not hold it */
1255 if (unlikely(server->h2_flush_rinq) && !c->is_h2_stream)
1256 h2_drain_flush_rinq(server);
1257 #endif
1258 1386 call_request_callback(c);
1259  
1260 1386 ▶ if (likely(c->wbuf_rinq)) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(c->wbuf_rinq)) { 1220 166 1386
1261 1220 conn_write_now(c);
1262 }
1263 #ifdef FEERSUM_HAS_H2
1264 else if (c->is_h2_stream) {
1265 struct feer_h2_stream *stream = H2_STREAM_FROM_PC(c);
1266 if (stream && stream->parent)
1267 h2_queue_flush(stream->parent);
1268 }
1269 #endif
1270 1386 SvREFCNT_dec(c->self); // for the rinq
1271 }
1272 #ifdef FEERSUM_HAS_H2
1273 server->h2_dispatch_depth--;
1274 h2_drain_flush_rinq(server);
1275 #endif
1276 1182 }
1277  
1278 static void
1279 439 prepare_cb (EV_P_ ev_prepare *w, int revents)
1280 {
1281 439 struct feer_server *srvr = (struct feer_server *)w->data;
1282  
1283 439 ▶ if (unlikely(revents & EV_ERROR)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(revents & EV_ERROR)) { 0 439 439
1284 0 trouble("EV error in prepare, revents=0x%08x\n", revents);
1285 0 ev_break(EV_A, EVBREAK_ALL);
1286 0 return;
1287 }
1288  
1289 439 ▶ if (!srvr->shutting_down) {
Branch: 1 branch
BranchTrueFalseTotal
if (!srvr->shutting_down) { 438 1 439
1290 964 ▶ for (int i = 0; i < srvr->n_listeners; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < srvr->n_listeners; i++) { 526 438 964
1291 526 struct feer_listen *lsnr = &srvr->listeners[i];
1292 526 ▶ if (!ev_is_active(&lsnr->accept_w) && !lsnr->pause_flags) {
Branch: 2 branches
BranchTrueFalseTotal
if (!ev_is_active(&lsnr->accept_w) && !lsnr->pause_flags) { 448 78 526
if (!ev_is_active(&lsnr->accept_w) && !lsnr->pause_flags) { 448 0 448
1293 448 ev_io_start(EV_A, &lsnr->accept_w);
1294 }
1295 }
1296 }
1297 439 ev_prepare_stop(EV_A, w);
1298 }
1299  
1300 static void
1301 59876 check_cb (EV_P_ ev_check *w, int revents)
1302 {
1303 59876 struct feer_server *server = (struct feer_server *)w->data;
1304  
1305 59876 ▶ if (unlikely(revents & EV_ERROR)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(revents & EV_ERROR)) { 0 59876 59876
1306 0 trouble("EV error in check, revents=0x%08x\n", revents);
1307 0 ev_break(EV_A, EVBREAK_ALL);
1308 0 return;
1309 }
1310  
1311 trace3("check! head=%p\n", server->request_ready_rinq);
1312 59876 ▶ if (server->request_ready_rinq)
Branch: 1 branch
BranchTrueFalseTotal
if (server->request_ready_rinq) 1181 58695 59876
1313 1181 process_request_ready_rinq(server);
1314  
1315 59876 ▶ if (server->write_ready_rinq)
Branch: 1 branch
BranchTrueFalseTotal
if (server->write_ready_rinq) 897 58979 59876
1316 897 feersum_drain_write_ready(server);
1317  
1318 #ifdef FEERSUM_HAS_H2
1319 /* queued by a nested event loop run inside a request handler */
1320 if (unlikely(server->h2_flush_rinq))
1321 h2_drain_flush_rinq(server);
1322 #endif
1323  
1324 59876 ▶ if (unlikely(server->response_cleanup_rinq)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(server->response_cleanup_rinq)) { 190 59686 59876
1325 dTHX;
1326 190 feersum_drain_response_cleanup(aTHX_ server);
1327 }
1328  
1329 /* Acted on at a loop boundary, not mid-dispatch: draining then would drop
1330 * responses of sibling requests (H2 streams) still queued. */
1331 59876 ▶ if (unlikely(server->retire_pending) && !server->shutting_down) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(server->retire_pending) && !server->shutting_down) { 0 59876 59876
if (unlikely(server->retire_pending) && !server->shutting_down) { 0 0 0
1332 dTHX;
1333 0 SV *cb = server->max_requests_cb_cv;
1334 0 SV *begin_cb = server->retire_begin_cb_cv;
1335 0 server->retire_pending = 0;
1336 0 server->max_requests_cb_cv = NULL;
1337 0 server->retire_begin_cb_cv = NULL;
1338 /* begin_cb first: an empty drain finishes inside the shutdown call */
1339 0 ▶ if (begin_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (begin_cb) { 0 0 0
1340 0 dSP;
1341 0 ENTER;
1342 0 SAVETMPS;
1343 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
1344 0 PUTBACK;
1345 0 call_sv(begin_cb, G_DISCARD|G_VOID|G_EVAL|G_NOARGS);
1346 0 SPAGAIN;
1347 0 ▶ if (unlikely(SvTRUE(ERRSV))) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(SvTRUE(ERRSV))) { 0 0 0
if (unlikely(SvTRUE(ERRSV))) { 0 0 0
1348 0 ▶ trouble("retirement callback died: %-p\n", ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
trouble("retirement callback died: %-p\n", ERRSV); 0 0 0
1349 0 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 0 0 0
1350 }
1351 0 PUTBACK;
1352 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
1353 0 LEAVE;
1354 0 SvREFCNT_dec(begin_cb);
1355 }
1356 0 ▶ if (cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (cb) { 0 0 0
1357 0 feer_begin_graceful_shutdown(aTHX_ server, cb);
1358 0 SvREFCNT_dec(cb);
1359 }
1360 }
1361 }
1362  
1363 static void
1364 1471 idle_cb (EV_P_ ev_idle *w, int revents)
1365 {
1366 1471 struct feer_server *server = (struct feer_server *)w->data;
1367  
1368 trace("idle_cb called, revents=0x%08x\n", revents);
1369 1471 ▶ if (unlikely(revents & EV_ERROR)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(revents & EV_ERROR)) { 0 1471 1471
1370 0 trouble("EV error in idle, revents=0x%08x\n", revents);
1371 0 ev_break(EV_A, EVBREAK_ALL);
1372 0 return;
1373 }
1374 trace3("idle! head=%p\n", server->request_ready_rinq);
1375 1471 ▶ if (server->request_ready_rinq)
Branch: 1 branch
BranchTrueFalseTotal
if (server->request_ready_rinq) 1 1470 1471
1376 1 process_request_ready_rinq(server);
1377 1471 ▶ if (server->write_ready_rinq)
Branch: 1 branch
BranchTrueFalseTotal
if (server->write_ready_rinq) 2 1469 1471
1378 2 feersum_drain_write_ready(server);
1379 #ifdef FEERSUM_HAS_H2
1380 if (server->h2_flush_rinq)
1381 h2_drain_flush_rinq(server);
1382 #endif
1383 1471 ▶ if (server->response_cleanup_rinq) {
Branch: 1 branch
BranchTrueFalseTotal
if (server->response_cleanup_rinq) { 0 1471 1471
1384 dTHX;
1385 0 feersum_drain_response_cleanup(aTHX_ server);
1386 }
1387 1471 ▶ if (!server->request_ready_rinq && !server->response_cleanup_rinq
Branch: 2 branches
BranchTrueFalseTotal
if (!server->request_ready_rinq && !server->response_cleanup_rinq 1471 0 1471
if (!server->request_ready_rinq && !server->response_cleanup_rinq 1471 0 1471
1388 1471 ▶ && !server->write_ready_rinq
Branch: 1 branch
BranchTrueFalseTotal
&& !server->write_ready_rinq 1471 0 1471
1389 #ifdef FEERSUM_HAS_H2
1390 && !server->h2_flush_rinq
1391 #endif
1392 )
1393 1471 ev_idle_stop(EV_A, w);
1394 }
1395  
1396 /* Leaving RECEIVE_WAIT re-arms the one-shot header deadline: without it the
1397 * 2nd and later keepalive requests would have none. */
1398 INLINE_UNLESS_DEBUG static void
1399 2083 begin_request_headers(struct feer_conn *c)
1400 {
1401 2083 ▶ if (c->receiving != RECEIVE_WAIT) return;
Branch: 1 branch
BranchTrueFalseTotal
if (c->receiving != RECEIVE_WAIT) return; 1871 212 2083
1402 212 change_receiving_state(c, RECEIVE_HEADERS);
1403 212 restart_header_timer(c);
1404 }
1405  
1406 /* Idempotent: the captured method/uri are released here. */
1407 static void
1408 2749 feer_emit_access_log(pTHX_ struct feer_conn *c)
1409 {
1410 2749 ▶ if (likely(!c->log_method)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (likely(!c->log_method)) return; 2745 4 2749
1411  
1412 4 SV *method = c->log_method, *uri = c->log_uri;
1413 4 c->log_method = NULL;
1414 4 c->log_uri = NULL;
1415  
1416 4 SV *cb = c->server->access_log_cb_cv;
1417 4 ▶ if (likely(cb != NULL)) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(cb != NULL)) { 4 0 4
1418 4 dSP;
1419 4 ENTER;
1420 4 SAVETMPS;
1421 4 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 4 4
1422 4 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 4 4
1423 4 PUSHs(sv_2mortal(method));
1424 4 ▶ PUSHs(sv_2mortal(uri ? uri : newSVpvs("")));
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(sv_2mortal(uri ? uri : newSVpvs(""))); 0 4 4
1425 4 PUSHs(sv_2mortal(newSVnv(ev_now(feersum_ev_loop) - c->req_start)));
1426 4 PUTBACK;
1427 4 call_sv(cb, G_DISCARD|G_VOID|G_EVAL);
1428 4 SPAGAIN;
1429 4 ▶ if (unlikely(SvTRUE(ERRSV))) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(SvTRUE(ERRSV))) { 4 0 4
if (unlikely(SvTRUE(ERRSV))) { 0 4 4
1430 0 ▶ trouble("access_log callback died: %-p\n", ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
trouble("access_log callback died: %-p\n", ERRSV); 0 0 0
1431 0 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 0 0 0
1432 }
1433 4 PUTBACK;
1434 4 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 4 0 4
1435 4 LEAVE;
1436 }
1437 else {
1438 0 SvREFCNT_dec(method);
1439 0 SvREFCNT_dec(uri);
1440 }
1441 }
1442  
1443 static void
1444 1600 handle_keepalive_or_close(struct feer_conn *c, conn_read_cb_t read_cb)
1445 {
1446 1600 stop_write_watcher(c);
1447 1600 stop_write_timer(c);
1448  
1449 /* fully flushed: what the access log means by "completed" */
1450 {
1451 dTHX;
1452 1600 feer_emit_access_log(aTHX_ c);
1453 }
1454  
1455 /* Strip body bytes the app never read (expected_cl is the decoded size for
1456 * chunked too), or they parse as the next request. */
1457 1600 ▶ if (c->is_keepalive && c->rbuf) {
Branch: 2 branches
BranchTrueFalseTotal
if (c->is_keepalive && c->rbuf) { 259 1341 1600
if (c->is_keepalive && c->rbuf) { 247 12 259
1458 247 ssize_t body_total = (c->expected_cl > 0) ? c->expected_cl : 0;
1459 247 ▶ if (body_total > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (body_total > 0) { 26 221 247
1460 26 ssize_t consumed = c->received_cl - (ssize_t)SvCUR(c->rbuf);
1461 26 ssize_t remaining = body_total - consumed;
1462 26 ▶ if (remaining > 0 && remaining <= (ssize_t)SvCUR(c->rbuf)) {
Branch: 2 branches
BranchTrueFalseTotal
if (remaining > 0 && remaining <= (ssize_t)SvCUR(c->rbuf)) { 6 20 26
if (remaining > 0 && remaining <= (ssize_t)SvCUR(c->rbuf)) { 6 0 6
1463 6 sv_chop(c->rbuf, SvPVX(c->rbuf) + remaining);
1464 trace("drained body fd=%d stripped=%"Ssz_df" pipeline=%"Sz_uf"\n",
1465 c->fd, (Ssz)remaining, (Sz)SvCUR(c->rbuf));
1466 }
1467 }
1468 }
1469  
1470 #ifdef FEERSUM_HAS_TLS
1471 /* TLS input ended: no request can follow the buffered ones */
1472 1600 ▶ if (c->tls_input_eof && (!c->rbuf || SvCUR(c->rbuf) == 0))
Branch: 3 branches
BranchTrueFalseTotal
if (c->tls_input_eof && (!c->rbuf || SvCUR(c->rbuf) == 0)) 0 1600 1600
if (c->tls_input_eof && (!c->rbuf || SvCUR(c->rbuf) == 0)) 0 0 0
if (c->tls_input_eof && (!c->rbuf || SvCUR(c->rbuf) == 0)) 0 0 0
1473 0 c->is_keepalive = 0;
1474 #endif
1475  
1476 /* not back to keepalive-idle during a shutdown: nothing sweeps it again */
1477 1703 ▶ if (c->is_keepalive && !c->server->shutting_down) {
Branch: 2 branches
BranchTrueFalseTotal
if (c->is_keepalive && !c->server->shutting_down) { 259 1341 1600
if (c->is_keepalive && !c->server->shutting_down) { 259 0 259
1478 /* leftover sendfile state would be written into the next response */
1479 259 ▶ CLOSE_SENDFILE_FD(c);
Branch: 2 branches
BranchTrueFalseTotal
CLOSE_SENDFILE_FD(c); 0 259 259
CLOSE_SENDFILE_FD(c); 0 0 0
1480 259 change_responding_state(c, RESPOND_NOT_STARTED);
1481 259 change_receiving_state(c, RECEIVE_WAIT);
1482 259 STRLEN pipelined = 0;
1483 259 ▶ if (c->rbuf) { pipelined = SvCUR(c->rbuf); }
Branch: 1 branch
BranchTrueFalseTotal
if (c->rbuf) { pipelined = SvCUR(c->rbuf); } 247 12 259
1484 259 ▶ if (likely(c->req)) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(c->req)) { 259 0 259
1485 350 ▶ if (likely(pipelined == 0) && c->req->buf && c->rbuf) {
Branch: 3 branches
BranchTrueFalseTotal
if (likely(pipelined == 0) && c->req->buf && c->rbuf) { 103 156 259
if (likely(pipelined == 0) && c->req->buf && c->rbuf) { 103 0 103
if (likely(pipelined == 0) && c->req->buf && c->rbuf) { 91 12 103
1486 91 SV *tmp = c->rbuf;
1487 91 c->rbuf = c->req->buf;
1488 91 c->req->buf = NULL;
1489 /* sv_chop may have left it OOK; SvPV_renew reallocs SvPVX directly */
1490 91 ▶ SvOOK_off(c->rbuf);
Branch: 4 branches
BranchTrueFalseTotal
SvOOK_off(c->rbuf); 0 91 91
SvOOK_off(c->rbuf); 0 0 0
SvOOK_off(c->rbuf); 0 0 0
SvOOK_off(c->rbuf); 3 88 91
1491 91 SvCUR_set(c->rbuf, 0);
1492 91 ▶ if (SvLEN(c->rbuf) > READ_BUFSZ * 4)
Branch: 1 branch
BranchTrueFalseTotal
if (SvLEN(c->rbuf) > READ_BUFSZ * 4) 0 91 91
1493 0 SvPV_renew(c->rbuf, READ_BUFSZ + 1);
1494 /* before the size test: a chopped body hides its length in OOK */
1495 91 ▶ SvOOK_off(tmp);
Branch: 4 branches
BranchTrueFalseTotal
SvOOK_off(tmp); 0 91 91
SvOOK_off(tmp); 0 0 0
SvOOK_off(tmp); 0 0 0
SvOOK_off(tmp); 2 89 91
1496 91 ▶ if (!c->rbuf_spare && SvREFCNT(tmp) == 1 && !SvMAGICAL(tmp)
Branch: 3 branches
BranchTrueFalseTotal
if (!c->rbuf_spare && SvREFCNT(tmp) == 1 && !SvMAGICAL(tmp) 91 0 91
if (!c->rbuf_spare && SvREFCNT(tmp) == 1 && !SvMAGICAL(tmp) 91 0 91
if (!c->rbuf_spare && SvREFCNT(tmp) == 1 && !SvMAGICAL(tmp) 91 0 91
1497 91 ▶ && SvLEN(tmp) <= READ_BUFSZ * 4) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvLEN(tmp) <= READ_BUFSZ * 4) { 91 0 91
1498 91 SvPOK_only(tmp);
1499 91 SvCUR_set(tmp, 0);
1500 91 c->rbuf_spare = tmp;
1501 } else
1502 0 SvREFCNT_dec(tmp);
1503 168 ▶ } else if (c->req->buf) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c->req->buf) { 168 0 168
1504 168 SvREFCNT_dec(c->req->buf);
1505 168 c->req->buf = NULL;
1506 }
1507 259 free_request(c);
1508 }
1509 // No is_http11 gate: a 1.0 keep-alive client pipelines too, and its
1510 // bytes would otherwise sit unparsed in rbuf.
1511 259 ▶ if (unlikely(pipelined > 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(pipelined > 0)) { 156 103 259
1512 156 c->pipelined = pipelined;
1513 156 ▶ if (c->pipeline_depth <= MAX_PIPELINE_DEPTH) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->pipeline_depth <= MAX_PIPELINE_DEPTH) { 156 0 156
1514 156 c->pipeline_depth++;
1515 156 read_cb(feersum_ev_loop, &c->read_ev_io, 0);
1516 156 c->pipeline_depth--;
1517 /* try_tls_conn_read never arms a watcher itself: a partial
1518 * pipelined request would otherwise never wake us */
1519 156 ▶ if (c->fd >= 0 && c->receiving != RECEIVE_SHUTDOWN)
Branch: 2 branches
BranchTrueFalseTotal
if (c->fd >= 0 && c->receiving != RECEIVE_SHUTDOWN) 156 0 156
if (c->fd >= 0 && c->receiving != RECEIVE_SHUTDOWN) 1 155 156
1520 1 start_read_watcher(c);
1521 156 return;
1522 }
1523 trace("pipeline depth limit reached on %d\n", c->fd);
1524 } else {
1525 103 c->pipelined = 0;
1526 }
1527 103 start_read_watcher(c);
1528 /* the drain sample belongs to the response just finished */
1529 103 c->prev_read_outq = 0;
1530 103 restart_read_timer(c);
1531 103 feer_conn_set_idle(c);
1532 } else {
1533 /* Not a bare safe_close_conn: the read watcher may still be armed (a
1534 * reader poll_cb starts it) and must be stopped before the fd goes,
1535 * or it keeps its ref on c->self and the conn is never freed. */
1536 1341 feer_linger_close(c, "close at write shutdown");
1537 }
1538 }
1539  
1540 static void
1541 47298 try_conn_write(EV_P_ struct ev_io *w, int revents)
1542 {
1543 47298 dCONN;
1544 unsigned i;
1545 struct iomatrix *m;
1546  
1547 47298 SvREFCNT_inc_void_NN(c->self);
1548  
1549 47298 ▶ if (unlikely(revents & EV_ERROR && !(revents & EV_WRITE))) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(revents & EV_ERROR && !(revents & EV_WRITE))) { 0 47298 47298
if (unlikely(revents & EV_ERROR && !(revents & EV_WRITE))) { 0 0 0
1550 trace("EV error on write, fd=%d revents=0x%08x\n", w->fd, revents);
1551 0 change_responding_state(c, RESPOND_SHUTDOWN);
1552 0 goto try_write_finished;
1553 }
1554  
1555 47298 ▶ if (unlikely(!c->wbuf_rinq)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!c->wbuf_rinq)) { 45795 1503 47298
1556 #ifdef __linux__
1557 // Before the RESPOND_SHUTDOWN early-out: closing the writer sets
1558 // RESPOND_SHUTDOWN while the sendfile is still in progress.
1559 45795 ▶ if (c->sendfile_fd >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (c->sendfile_fd >= 0) 2 45793 45795
1560 2 goto try_sendfile;
1561 #endif
1562  
1563 45793 ▶ if (unlikely(c->responding >= RESPOND_SHUTDOWN))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->responding >= RESPOND_SHUTDOWN)) 11 45782 45793
1564 11 goto try_write_finished;
1565  
1566 45782 ▶ if (!c->poll_write_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (!c->poll_write_cb) { 0 45782 45782
1567 0 ▶ if (c->responding == RESPOND_STREAMING)
Branch: 1 branch
BranchTrueFalseTotal
if (c->responding == RESPOND_STREAMING) 0 0 0
1568 0 goto try_write_paused;
1569  
1570 trace("tried to write with an empty buffer %d resp=%d\n",w->fd,c->responding);
1571 0 change_responding_state(c, RESPOND_SHUTDOWN);
1572 0 goto try_write_finished;
1573 }
1574  
1575 45782 unsigned int writes_before = c->poll_writes_seen;
1576 45782 ▶ if (c->poll_write_cb_is_io_handle)
Branch: 1 branch
BranchTrueFalseTotal
if (c->poll_write_cb_is_io_handle) 20 45762 45782
1577 20 pump_io_handle(c);
1578 else
1579 45762 call_poll_callback(c, 1);
1580  
1581 45782 ▶ if (unlikely(!c->wbuf_rinq)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!c->wbuf_rinq)) { 483 45299 45782
1582 /* nothing else notices a gone peer: the read watcher is stopped
1583 * and writev never runs on an empty buffer. on_eof fires before
1584 * either exit below. */
1585 483 bool gone = peer_is_gone(c);
1586 483 feersum_maybe_fire_on_eof(c);
1587 483 ▶ if (unlikely(gone)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(gone)) { 0 483 483
1588 trace("poll_cb produced nothing and the peer is gone %d\n", w->fd);
1589 0 c->is_keepalive = 0;
1590 0 change_responding_state(c, RESPOND_SHUTDOWN);
1591 0 goto try_write_finished;
1592 }
1593 483 ▶ if (unlikely(feer_eof_park_expired(c))) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(feer_eof_park_expired(c))) { 13 470 483
1594 trace("poll_cb produced nothing and the EOF park expired %d\n", w->fd);
1595 13 c->is_keepalive = 0;
1596 13 change_responding_state(c, RESPOND_SHUTDOWN);
1597 13 goto try_write_finished;
1598 }
1599 /* pure decline (no write call at all): park; an empty write()
1600 * counts as engagement and stays hot */
1601 470 ▶ if (c->responding == RESPOND_STREAMING && c->poll_write_cb
Branch: 2 branches
BranchTrueFalseTotal
if (c->responding == RESPOND_STREAMING && c->poll_write_cb 447 23 470
if (c->responding == RESPOND_STREAMING && c->poll_write_cb 447 0 447
1602 447 ▶ && c->poll_writes_seen == writes_before) {
Branch: 1 branch
BranchTrueFalseTotal
&& c->poll_writes_seen == writes_before) { 447 0 447
1603 447 feersum_poll_retry_park(c);
1604 447 goto try_write_cleanup;
1605 }
1606 23 goto try_write_again;
1607 }
1608 }
1609 1503 ▶ else if (c->cached_wbuf_low_water > 0
Branch: 1 branch
BranchTrueFalseTotal
else if (c->cached_wbuf_low_water > 0 1502 1 1503
1610 1 ▶ && c->wbuf_len <= c->cached_wbuf_low_water
Branch: 1 branch
BranchTrueFalseTotal
&& c->wbuf_len <= c->cached_wbuf_low_water 0 1 1
1611 1 ▶ && c->responding == RESPOND_STREAMING && c->poll_write_cb) {
Branch: 2 branches
BranchTrueFalseTotal
&& c->responding == RESPOND_STREAMING && c->poll_write_cb) { 0 1 1
&& c->responding == RESPOND_STREAMING && c->poll_write_cb) { 0 1 1
1612 1 ▶ if (c->poll_write_cb_is_io_handle)
Branch: 1 branch
BranchTrueFalseTotal
if (c->poll_write_cb_is_io_handle) 0 1 1
1613 0 pump_io_handle(c);
1614 else
1615 1 call_poll_callback(c, 1);
1616  
1617 // a nested event loop in the callback can re-enter and drain the ring
1618 1 ▶ if (unlikely(!c->wbuf_rinq)) goto try_write_again;
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!c->wbuf_rinq)) goto try_write_again; 1 0 1
1619 }
1620  
1621 1503 try_write_again_immediately:
1622 #if defined(__linux__) && defined(FEERSUM_TCP_CORK)
1623 46802 ▶ if (c->sendfile_fd >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (c->sendfile_fd >= 0) 4 46798 46802
1624 4 set_cork(c, 1);
1625 #endif
1626 46802 m = (struct iomatrix *)c->wbuf_rinq->ref;
1627  
1628 /* Skip zero-length iovecs: alone in a batch they make writev() return 0,
1629 * which the check below would take for a fatal error. */
1630 46802 ▶ while (m->offset < m->count && m->iov[m->offset].iov_len == 0) {
Branch: 2 branches
BranchTrueFalseTotal
while (m->offset < m->count && m->iov[m->offset].iov_len == 0) { 46802 0 46802
while (m->offset < m->count && m->iov[m->offset].iov_len == 0) { 0 46802 46802
1631 0 ▶ if (m->sv[m->offset]) {
Branch: 1 branch
BranchTrueFalseTotal
if (m->sv[m->offset]) { 0 0 0
1632 0 SvREFCNT_dec(m->sv[m->offset]);
1633 0 m->sv[m->offset] = NULL;
1634 }
1635 0 m->offset++;
1636 }
1637 46802 ▶ if (unlikely(m->offset >= m->count)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(m->offset >= m->count)) { 0 46802 46802
1638 0 rinq_shift(&c->wbuf_rinq);
1639 0 ▶ IOMATRIX_FREE(m);
Branch: 1 branch
BranchTrueFalseTotal
IOMATRIX_FREE(m); 0 0 0
1640 0 ▶ if (!c->wbuf_rinq) {
Branch: 1 branch
BranchTrueFalseTotal
if (!c->wbuf_rinq) { 0 0 0
1641 #ifdef __linux__
1642 0 ▶ if (c->sendfile_fd >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (c->sendfile_fd >= 0) 0 0 0
1643 0 goto try_sendfile;
1644 #endif
1645 0 goto try_write_finished;
1646 }
1647 0 goto try_write_again_immediately;
1648 }
1649 #if DEBUG >= 2
1650 warn("going to write to %d:\n",c->fd);
1651 for (i=0; i < m->count; i++) {
1652 fprintf(stderr,"%.*s",
1653 (int)m->iov[i].iov_len, (char*)m->iov[i].iov_base);
1654 }
1655 #endif
1656  
1657 trace("going to write %d off=%d count=%d\n", w->fd, m->offset, m->count);
1658 46802 errno = 0;
1659 46802 int iov_count = m->count - m->offset;
1660 #ifdef IOV_MAX
1661 46802 ▶ if (iov_count > IOV_MAX) iov_count = IOV_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (iov_count > IOV_MAX) iov_count = IOV_MAX; 0 46802 46802
1662 #endif
1663 ssize_t wrote;
1664 46802 ▶ if (iov_count == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (iov_count == 1) { 1670 45132 46802
1665 1670 wrote = write(w->fd, m->iov[m->offset].iov_base, m->iov[m->offset].iov_len);
1666 } else {
1667 45132 wrote = writev(w->fd, &m->iov[m->offset], iov_count);
1668 }
1669 trace("wrote %"Ssz_df" bytes to %d, errno=%d\n", (Ssz)wrote, w->fd, errno);
1670  
1671 46802 ▶ if (unlikely(wrote <= 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(wrote <= 0)) { 12 46790 46802
1672 12 ▶ if (likely(errno == EAGAIN || errno == EINTR))
Branch: 2 branches
BranchTrueFalseTotal
if (likely(errno == EAGAIN || errno == EINTR)) 12 0 12
if (likely(errno == EAGAIN || errno == EINTR)) 0 12 12
1673 0 goto try_write_again;
1674 12 ▶ if (errno == ECONNRESET || errno == EPIPE)
Branch: 2 branches
BranchTrueFalseTotal
if (errno == ECONNRESET || errno == EPIPE) 0 12 12
if (errno == ECONNRESET || errno == EPIPE) 0 0 0
1675 trace("try_conn_write fd=%d: %s\n", w->fd, strerror(errno));
1676 else
1677 0 trouble("try_conn_write fd=%d: %s\n", w->fd, strerror(errno));
1678 12 ▶ CLOSE_SENDFILE_FD(c);
Branch: 2 branches
BranchTrueFalseTotal
CLOSE_SENDFILE_FD(c); 0 12 12
CLOSE_SENDFILE_FD(c); 0 0 0
1679 12 set_cork(c, 0);
1680 12 stop_write_timer(c);
1681 12 c->is_keepalive = 0;
1682 12 change_responding_state(c, RESPOND_SHUTDOWN);
1683 12 goto try_write_finished;
1684 }
1685  
1686 46790 c->wbuf_len -= wrote;
1687 46790 restart_write_timer(c);
1688  
1689 46790 bool consume = 1;
1690 183970 ▶ for (i = m->offset; i < m->count && consume; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = m->offset; i < m->count && consume; i++) { 137180 46790 183970
for (i = m->offset; i < m->count && consume; i++) { 137180 0 137180
1691 137180 struct iovec *v = &m->iov[i];
1692 137180 ▶ if (unlikely(v->iov_len > wrote)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(v->iov_len > wrote)) { 1 137179 137180
1693 trace3("offset vector %d base=%p len=%"Sz_uf"\n",
1694 w->fd, v->iov_base, (Sz)v->iov_len);
1695 1 v->iov_base = (char*)v->iov_base + wrote;
1696 1 v->iov_len -= wrote;
1697 1 consume = 0;
1698 }
1699 else {
1700 trace3("consume vector %d base=%p len=%"Sz_uf" sv=%p\n",
1701 w->fd, v->iov_base, (Sz)v->iov_len, m->sv[i]);
1702 137179 wrote -= v->iov_len;
1703 137179 m->offset++;
1704 137179 ▶ if (m->sv[i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (m->sv[i]) { 92075 45104 137179
1705 92075 SvREFCNT_dec(m->sv[i]);
1706 92075 m->sv[i] = NULL;
1707 }
1708 }
1709 }
1710  
1711 46790 ▶ if (likely(m->offset >= m->count)) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(m->offset >= m->count)) { 46789 1 46790
1712 trace2("all done with iomatrix %d state=%d\n",w->fd,c->responding);
1713 46789 rinq_shift(&c->wbuf_rinq);
1714 46789 ▶ IOMATRIX_FREE(m);
Branch: 1 branch
BranchTrueFalseTotal
IOMATRIX_FREE(m); 46789 0 46789
1715 46789 ▶ if (!c->wbuf_rinq) {
Branch: 1 branch
BranchTrueFalseTotal
if (!c->wbuf_rinq) { 46789 0 46789
1716 #ifdef __linux__
1717 46789 ▶ if (c->sendfile_fd >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (c->sendfile_fd >= 0) 4 46785 46789
1718 4 goto try_sendfile;
1719 #endif
1720 46785 goto try_write_finished;
1721 }
1722 // low water: yield so poll_cb can refill
1723 0 ▶ if (c->cached_wbuf_low_water > 0
Branch: 1 branch
BranchTrueFalseTotal
if (c->cached_wbuf_low_water > 0 0 0 0
1724 0 ▶ && c->wbuf_len <= c->cached_wbuf_low_water
Branch: 1 branch
BranchTrueFalseTotal
&& c->wbuf_len <= c->cached_wbuf_low_water 0 0 0
1725 0 ▶ && c->responding == RESPOND_STREAMING && c->poll_write_cb) {
Branch: 2 branches
BranchTrueFalseTotal
&& c->responding == RESPOND_STREAMING && c->poll_write_cb) { 0 0 0
&& c->responding == RESPOND_STREAMING && c->poll_write_cb) { 0 0 0
1726 0 goto try_write_again;
1727 }
1728 trace2("write again immediately %d state=%d\n",w->fd,c->responding);
1729 0 goto try_write_again_immediately;
1730 }
1731 trace2("write fallthrough %d state=%d\n",w->fd,c->responding);
1732 1 goto try_write_again;
1733  
1734 #ifdef __linux__
1735 6 try_sendfile:
1736 {
1737 trace("sendfile %d: fd=%d off=%ld remain=%zu\n",
1738 w->fd, c->sendfile_fd, (long)c->sendfile_off, c->sendfile_remain);
1739 6 ssize_t sent = sendfile(w->fd, c->sendfile_fd,
1740 6 &c->sendfile_off, c->sendfile_remain);
1741 6 ▶ if (sent > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (sent > 0) { 6 0 6
1742 6 c->sendfile_remain -= sent;
1743 trace("sendfile sent %zd, remain=%zu\n", sent, c->sendfile_remain);
1744 6 ▶ if (c->sendfile_remain == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->sendfile_remain == 0) { 4 2 6
1745 4 ▶ CLOSE_SENDFILE_FD(c);
Branch: 2 branches
BranchTrueFalseTotal
CLOSE_SENDFILE_FD(c); 4 0 4
CLOSE_SENDFILE_FD(c); 0 4 4
1746 4 set_cork(c, 0);
1747 4 goto try_write_finished;
1748 }
1749 // without this write_timeout reaps a long sendfile mid-file
1750 2 restart_write_timer(c);
1751 2 goto try_write_again;
1752 }
1753 0 ▶ else if (sent == 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (sent == 0) { 0 0 0
1754 // file shrank mid-transfer: body is now shorter than the committed
1755 // Content-Length, so the connection cannot be reused
1756 0 ▶ CLOSE_SENDFILE_FD(c);
Branch: 2 branches
BranchTrueFalseTotal
CLOSE_SENDFILE_FD(c); 0 0 0
CLOSE_SENDFILE_FD(c); 0 0 0
1757 0 set_cork(c, 0);
1758 0 c->is_keepalive = 0;
1759 0 ▶ if (c->responding == RESPOND_STREAMING) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->responding == RESPOND_STREAMING) { 0 0 0
1760 0 change_responding_state(c, RESPOND_SHUTDOWN);
1761 }
1762 0 goto try_write_finished;
1763 }
1764 else {
1765 0 ▶ if (errno == EAGAIN || errno == EINTR) {
Branch: 2 branches
BranchTrueFalseTotal
if (errno == EAGAIN || errno == EINTR) { 0 0 0
if (errno == EAGAIN || errno == EINTR) { 0 0 0
1766 0 goto try_write_again;
1767 }
1768 0 trouble("sendfile fd=%d: %s\n", c->fd, strerror(errno));
1769 0 ▶ CLOSE_SENDFILE_FD(c);
Branch: 2 branches
BranchTrueFalseTotal
CLOSE_SENDFILE_FD(c); 0 0 0
CLOSE_SENDFILE_FD(c); 0 0 0
1770 0 set_cork(c, 0);
1771 0 c->is_keepalive = 0;
1772 0 change_responding_state(c, RESPOND_SHUTDOWN);
1773 0 goto try_write_finished;
1774 }
1775 }
1776 #endif
1777  
1778 45378 try_write_again:
1779 trace("write again %d state=%d\n",w->fd,c->responding);
1780 45378 start_write_watcher(c);
1781 /* also when blocked without a prior write: an app-driven response would
1782 * otherwise have no write_timeout coverage */
1783 45378 ensure_write_timer(c);
1784 45378 goto try_write_cleanup;
1785  
1786 46825 try_write_finished:
1787 46825 switch(c->responding) {
1788 0 case RESPOND_NOT_STARTED:
1789 // an interim 100 Continue tail just flushed; the request is still being
1790 // received, so stop writing and keep the read side going, do not reuse
1791 0 goto try_write_paused;
1792 0 case RESPOND_NORMAL:
1793 0 goto try_write_shutdown;
1794 45382 case RESPOND_STREAMING:
1795 45382 ▶ if (c->poll_write_cb) goto try_write_again;
Branch: 1 branch
BranchTrueFalseTotal
if (c->poll_write_cb) goto try_write_again; 45352 30 45382
1796 30 else goto try_write_paused;
1797 1443 case RESPOND_SHUTDOWN:
1798 1443 goto try_write_shutdown;
1799 0 default:
1800 0 goto try_write_cleanup;
1801 }
1802  
1803 30 try_write_paused:
1804 trace3("write PAUSED %d, refcnt=%d, state=%d\n", c->fd, SvREFCNT(c->self), c->responding);
1805 30 stop_write_watcher(c);
1806 30 stop_write_timer(c);
1807 30 goto try_write_cleanup;
1808  
1809 1443 try_write_shutdown:
1810 1443 handle_keepalive_or_close(c, try_conn_read);
1811  
1812 47298 try_write_cleanup:
1813 47298 SvREFCNT_dec(c->self);
1814 47298 return;
1815 }
1816  
1817 // "PROXY TCP4|TCP6|UNKNOWN src_addr dst_addr src_port dst_port\r\n"
1818 // Returns bytes consumed, -1 on error, -2 if more data is needed
1819 static int
1820 155 parse_proxy_v1(struct feer_conn *c)
1821 {
1822 155 char *buf = SvPVX(c->rbuf);
1823 155 STRLEN len = SvCUR(c->rbuf);
1824  
1825 155 ▶ if (len < 8) // "PROXY \r\n"
Branch: 1 branch
BranchTrueFalseTotal
if (len < 8) // "PROXY \r\n" 21 134 155
1826 21 return -2;
1827  
1828 134 ▶ if (memcmp(buf, PROXY_V1_PREFIX, PROXY_V1_PREFIX_LEN) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(buf, PROXY_V1_PREFIX, PROXY_V1_PREFIX_LEN) != 0) 0 134 134
1829 0 return -1;
1830  
1831 134 char *crlf = NULL;
1832 134 STRLEN search_len = len > PROXY_V1_MAX_LINE ? PROXY_V1_MAX_LINE : len;
1833 3085 ▶ for (STRLEN i = PROXY_V1_PREFIX_LEN; i < search_len - 1; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (STRLEN i = PROXY_V1_PREFIX_LEN; i < search_len - 1; i++) { 2982 103 3085
1834 2982 ▶ if (buf[i] == '\r' && buf[i+1] == '\n') {
Branch: 2 branches
BranchTrueFalseTotal
if (buf[i] == '\r' && buf[i+1] == '\n') { 30 2952 2982
if (buf[i] == '\r' && buf[i+1] == '\n') { 30 0 30
1835 30 crlf = buf + i;
1836 30 break;
1837 }
1838 // a bare LF is not a terminator; scanning past it would swallow the
1839 // following request line into the header
1840 2952 ▶ if (buf[i] == '\n')
Branch: 1 branch
BranchTrueFalseTotal
if (buf[i] == '\n') 1 2951 2952
1841 1 return -1;
1842 }
1843  
1844 133 ▶ if (!crlf) {
Branch: 1 branch
BranchTrueFalseTotal
if (!crlf) { 103 30 133
1845 103 ▶ if (len >= PROXY_V1_MAX_LINE)
Branch: 1 branch
BranchTrueFalseTotal
if (len >= PROXY_V1_MAX_LINE) 1 102 103
1846 1 return -1;
1847 102 return -2;
1848 }
1849  
1850 30 size_t header_len = (crlf - buf) + 2;
1851  
1852 30 char saved = *crlf;
1853 30 *crlf = '\0';
1854  
1855 30 char *p = buf + PROXY_V1_PREFIX_LEN;
1856  
1857 30 ▶ if (strncmp(p, "UNKNOWN", 7) == 0 && (p[7] == '\0' || p[7] == ' ')) {
Branch: 3 branches
BranchTrueFalseTotal
if (strncmp(p, "UNKNOWN", 7) == 0 && (p[7] == '\0' || p[7] == ' ')) { 1 29 30
if (strncmp(p, "UNKNOWN", 7) == 0 && (p[7] == '\0' || p[7] == ' ')) { 0 1 1
if (strncmp(p, "UNKNOWN", 7) == 0 && (p[7] == '\0' || p[7] == ' ')) { 0 0 0
1858 // UNKNOWN keeps the original address
1859 1 *crlf = saved;
1860 1 c->proxy_proto_version = 1;
1861 trace("PROXY v1 UNKNOWN, keeping original address\n");
1862 1 return (int)header_len;
1863 }
1864  
1865 29 int is_ipv6 = 0;
1866 29 ▶ if (strncmp(p, "TCP4 ", 5) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strncmp(p, "TCP4 ", 5) == 0) { 26 3 29
1867 26 p += 5;
1868 3 ▶ } else if (strncmp(p, "TCP6 ", 5) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strncmp(p, "TCP6 ", 5) == 0) { 2 1 3
1869 2 p += 5;
1870 2 is_ipv6 = 1;
1871 } else {
1872 1 *crlf = saved;
1873 1 return -1;
1874 }
1875  
1876 char src_addr[46], dst_addr[46]; // max IPv6 length
1877 long long src_port, dst_port;
1878  
1879 // %18lld bounds the conversion: an out-of-range numeric conversion is UB
1880 28 ▶ if (sscanf(p, "%45s %45s %18lld %18lld", src_addr, dst_addr, &src_port, &dst_port) != 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (sscanf(p, "%45s %45s %18lld %18lld", src_addr, dst_addr, &src_port, &dst_port) != 4) { 0 28 28
1881 0 *crlf = saved;
1882 0 return -1;
1883 }
1884  
1885 28 ▶ if (src_port < 0 || src_port > 65535 || dst_port < 0 || dst_port > 65535) {
Branch: 4 branches
BranchTrueFalseTotal
if (src_port < 0 || src_port > 65535 || dst_port < 0 || dst_port > 65535) { 28 0 28
if (src_port < 0 || src_port > 65535 || dst_port < 0 || dst_port > 65535) { 27 1 28
if (src_port < 0 || src_port > 65535 || dst_port < 0 || dst_port > 65535) { 27 0 27
if (src_port < 0 || src_port > 65535 || dst_port < 0 || dst_port > 65535) { 0 27 27
1886 1 *crlf = saved;
1887 1 return -1;
1888 }
1889  
1890 27 *crlf = saved;
1891  
1892 27 ▶ if (is_ipv6) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_ipv6) { 2 25 27
1893 2 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&c->sa;
1894 2 sa6->sin6_family = AF_INET6;
1895 2 ▶ if (inet_pton(AF_INET6, src_addr, &sa6->sin6_addr) != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (inet_pton(AF_INET6, src_addr, &sa6->sin6_addr) != 1) { 0 2 2
1896 0 return -1;
1897 }
1898 2 sa6->sin6_port = htons((uint16_t)src_port);
1899 } else {
1900 25 struct sockaddr_in *sa4 = (struct sockaddr_in *)&c->sa;
1901 25 sa4->sin_family = AF_INET;
1902 25 ▶ if (inet_pton(AF_INET, src_addr, &sa4->sin_addr) != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (inet_pton(AF_INET, src_addr, &sa4->sin_addr) != 1) { 1 24 25
1903 1 return -1;
1904 }
1905 24 sa4->sin_port = htons((uint16_t)src_port);
1906 }
1907  
1908 26 c->proxy_proto_version = 1;
1909 26 c->proxy_dst_port = (uint16_t)dst_port;
1910 trace("PROXY v1 %s src=%s:%lld dst_port=%lld\n", is_ipv6 ? "TCP6" : "TCP4", src_addr, src_port, dst_port);
1911 26 return (int)header_len;
1912 }
1913  
1914 // Returns bytes consumed, -1 on error, -2 if more data is needed
1915 static int
1916 126 parse_proxy_v2(struct feer_conn *c)
1917 {
1918 126 unsigned char *buf = (unsigned char *)SvPVX(c->rbuf);
1919 126 STRLEN len = SvCUR(c->rbuf);
1920  
1921 126 ▶ if (len < PROXY_V2_HDR_MIN)
Branch: 1 branch
BranchTrueFalseTotal
if (len < PROXY_V2_HDR_MIN) 3 123 126
1922 3 return -2;
1923  
1924 123 ▶ if (memcmp(buf, PROXY_V2_SIG, PROXY_V2_SIG_LEN) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(buf, PROXY_V2_SIG, PROXY_V2_SIG_LEN) != 0) 26 97 123
1925 26 return -1;
1926  
1927 97 unsigned char ver_cmd = buf[12];
1928 97 unsigned char version = ver_cmd & 0xF0;
1929 97 unsigned char command = ver_cmd & 0x0F;
1930  
1931 97 ▶ if (version != PROXY_V2_VERSION)
Branch: 1 branch
BranchTrueFalseTotal
if (version != PROXY_V2_VERSION) 32 65 97
1932 32 return -1;
1933  
1934 65 unsigned char fam_proto = buf[13];
1935 65 unsigned char family = fam_proto & 0xF0;
1936  
1937 65 uint16_t addr_len = (buf[14] << 8) | buf[15];
1938 65 ▶ if (unlikely(addr_len > 4096)) { /* spec allows 65535 */
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(addr_len > 4096)) { /* spec allows 65535 */ 1 64 65
1939 /* trace, not trouble: rejections here must not be a log-flood tap */
1940 trace("PROXY v2 addr_len too large: %u\n", addr_len);
1941 1 return -1;
1942 }
1943  
1944 64 size_t total_len = PROXY_V2_HDR_MIN + addr_len;
1945 64 ▶ if (len < total_len)
Branch: 1 branch
BranchTrueFalseTotal
if (len < total_len) 5 59 64
1946 5 return -2;
1947  
1948 59 ▶ if (command == PROXY_V2_CMD_LOCAL) {
Branch: 1 branch
BranchTrueFalseTotal
if (command == PROXY_V2_CMD_LOCAL) { 1 58 59
1949 // LOCAL keeps the original address
1950 1 c->proxy_proto_version = 2;
1951 trace("PROXY v2 LOCAL, keeping original address\n");
1952 1 return (int)total_len;
1953 }
1954  
1955 58 ▶ if (command != PROXY_V2_CMD_PROXY) {
Branch: 1 branch
BranchTrueFalseTotal
if (command != PROXY_V2_CMD_PROXY) { 0 58 58
1956 0 return -1;
1957 }
1958  
1959 58 unsigned char *addr_data = buf + PROXY_V2_HDR_MIN;
1960  
1961 58 ▶ if (family == PROXY_V2_FAM_INET) {
Branch: 1 branch
BranchTrueFalseTotal
if (family == PROXY_V2_FAM_INET) { 52 6 58
1962 // src_addr(4) dst_addr(4) src_port(2) dst_port(2)
1963 52 ▶ if (addr_len < PROXY_V2_ADDR_V4_LEN)
Branch: 1 branch
BranchTrueFalseTotal
if (addr_len < PROXY_V2_ADDR_V4_LEN) 0 52 52
1964 0 return -1;
1965  
1966 52 struct sockaddr_in *sa4 = (struct sockaddr_in *)&c->sa;
1967 52 sa4->sin_family = AF_INET;
1968 52 memcpy(&sa4->sin_addr, addr_data, 4);
1969 52 memcpy(&sa4->sin_port, addr_data + 8, 2); // already network order
1970  
1971 uint16_t dst_port_n;
1972 52 memcpy(&dst_port_n, addr_data + 10, 2);
1973 52 c->proxy_dst_port = ntohs(dst_port_n);
1974  
1975 trace("PROXY v2 TCP4 src=%d.%d.%d.%d:%d dst_port=%d\n",
1976 addr_data[0], addr_data[1], addr_data[2], addr_data[3],
1977 ntohs(sa4->sin_port), c->proxy_dst_port);
1978 6 ▶ } else if (family == PROXY_V2_FAM_INET6) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (family == PROXY_V2_FAM_INET6) { 1 5 6
1979 // src_addr(16) dst_addr(16) src_port(2) dst_port(2)
1980 1 ▶ if (addr_len < PROXY_V2_ADDR_V6_LEN)
Branch: 1 branch
BranchTrueFalseTotal
if (addr_len < PROXY_V2_ADDR_V6_LEN) 0 1 1
1981 0 return -1;
1982  
1983 1 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&c->sa;
1984 1 sa6->sin6_family = AF_INET6;
1985 1 memcpy(&sa6->sin6_addr, addr_data, 16);
1986 1 memcpy(&sa6->sin6_port, addr_data + 32, 2); // already network order
1987  
1988 uint16_t dst_port_n;
1989 1 memcpy(&dst_port_n, addr_data + 34, 2);
1990 1 c->proxy_dst_port = ntohs(dst_port_n);
1991  
1992 trace("PROXY v2 TCP6 port=%d dst_port=%d\n", ntohs(sa6->sin6_port), c->proxy_dst_port);
1993 5 ▶ } else if (family == PROXY_V2_FAM_UNSPEC) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (family == PROXY_V2_FAM_UNSPEC) { 1 4 5
1994 trace("PROXY v2 UNSPEC, keeping original address\n");
1995 1 ▶ } else if (family == PROXY_V2_FAM_UNIX) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (family == PROXY_V2_FAM_UNIX) { 1 0 1
1996 // UNIX paths are no use as REMOTE_ADDR: keep the original address
1997 1 ▶ if (addr_len < PROXY_V2_ADDR_UNIX_LEN)
Branch: 1 branch
BranchTrueFalseTotal
if (addr_len < PROXY_V2_ADDR_UNIX_LEN) 0 1 1
1998 0 return -1;
1999 trace("PROXY v2 UNIX, keeping original address\n");
2000 } else {
2001 0 return -1;
2002 }
2003  
2004 58 size_t addr_size = 0;
2005 58 ▶ if (family == PROXY_V2_FAM_INET) addr_size = PROXY_V2_ADDR_V4_LEN;
Branch: 1 branch
BranchTrueFalseTotal
if (family == PROXY_V2_FAM_INET) addr_size = PROXY_V2_ADDR_V4_LEN; 52 6 58
2006 6 ▶ else if (family == PROXY_V2_FAM_INET6) addr_size = PROXY_V2_ADDR_V6_LEN;
Branch: 1 branch
BranchTrueFalseTotal
else if (family == PROXY_V2_FAM_INET6) addr_size = PROXY_V2_ADDR_V6_LEN; 1 5 6
2007 5 ▶ else if (family == PROXY_V2_FAM_UNIX) addr_size = PROXY_V2_ADDR_UNIX_LEN;
Branch: 1 branch
BranchTrueFalseTotal
else if (family == PROXY_V2_FAM_UNIX) addr_size = PROXY_V2_ADDR_UNIX_LEN; 1 4 5
2008 /* UNSPEC has no address length: the whole field is opaque, TLVs included */
2009 4 ▶ else if (family == PROXY_V2_FAM_UNSPEC) addr_size = addr_len;
Branch: 1 branch
BranchTrueFalseTotal
else if (family == PROXY_V2_FAM_UNSPEC) addr_size = addr_len; 4 0 4
2010  
2011 58 ▶ if (addr_len > addr_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (addr_len > addr_size) { 30 28 58
2012 dTHX;
2013 30 unsigned char *tlv_start = addr_data + addr_size;
2014 30 size_t tlv_remaining = addr_len - addr_size;
2015  
2016 30 HV *tlv_hv = newHV();
2017 30 unsigned int tlv_count = 0;
2018  
2019 117 ▶ while (tlv_remaining >= 3) { // type(1) length(2)
Branch: 1 branch
BranchTrueFalseTotal
while (tlv_remaining >= 3) { // type(1) length(2) 103 14 117
2020 // bounds a payload packed with zero-length TLVs
2021 103 ▶ if (++tlv_count > 64) {
Branch: 1 branch
BranchTrueFalseTotal
if (++tlv_count > 64) { 1 102 103
2022 trace("PROXY v2 too many TLVs (>64), rejecting\n");
2023 1 SvREFCNT_dec((SV *)tlv_hv);
2024 1 return -1;
2025 }
2026 102 unsigned char tlv_type = tlv_start[0];
2027 102 uint16_t tlv_len = (tlv_start[1] << 8) | tlv_start[2];
2028  
2029 102 ▶ if (tlv_remaining < 3 + (size_t)tlv_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (tlv_remaining < 3 + (size_t)tlv_len) { 15 87 102
2030 trace("PROXY v2 malformed TLV: need %u bytes, have %zu\n",
2031 tlv_len, tlv_remaining - 3);
2032 15 SvREFCNT_dec((SV *)tlv_hv);
2033 15 return -1;
2034 }
2035  
2036 // pp2_tlv_ssl { uint8_t client; uint32_t verify; }
2037 87 ▶ if (tlv_type == PP2_TYPE_SSL && tlv_len >= 5
Branch: 2 branches
BranchTrueFalseTotal
if (tlv_type == PP2_TYPE_SSL && tlv_len >= 5 11 76 87
if (tlv_type == PP2_TYPE_SSL && tlv_len >= 5 11 0 11
2038 11 ▶ && (tlv_start[3] & PP2_CLIENT_SSL)) {
Branch: 1 branch
BranchTrueFalseTotal
&& (tlv_start[3] & PP2_CLIENT_SSL)) { 5 6 11
2039 5 c->proxy_ssl = 1;
2040 trace("PROXY v2 TLV PP2_TYPE_SSL, client bit set\n");
2041 }
2042  
2043 87 ▶ if (tlv_type != PP2_TYPE_NOOP) {
Branch: 1 branch
BranchTrueFalseTotal
if (tlv_type != PP2_TYPE_NOOP) { 87 0 87
2044 87 SV *val = newSVpvn((char *)(tlv_start + 3), tlv_len);
2045 char key[8];
2046 87 int key_len = snprintf(key, sizeof(key), "%u", tlv_type);
2047 87 hv_store(tlv_hv, key, key_len, val, 0);
2048 trace("PROXY v2 TLV type=%u len=%u\n", tlv_type, tlv_len);
2049 }
2050  
2051 87 tlv_start += 3 + (size_t)tlv_len;
2052 87 tlv_remaining -= 3 + (size_t)tlv_len;
2053 }
2054  
2055 /* a 1-2 byte tail is malformed: the spec pads with PP2_TYPE_NOOP */
2056 14 ▶ if (unlikely(tlv_remaining)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(tlv_remaining)) { 1 13 14
2057 trace("PROXY v2 trailing %zu bytes after TLVs, rejecting\n",
2058 tlv_remaining);
2059 1 SvREFCNT_dec((SV *)tlv_hv);
2060 1 return -1;
2061 }
2062  
2063 13 ▶ if (HvKEYS(tlv_hv) > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (HvKEYS(tlv_hv) > 0) { 0 13 13
if (HvKEYS(tlv_hv) > 0) { 13 0 13
2064 13 c->proxy_tlvs = newRV_noinc((SV *)tlv_hv);
2065 } else {
2066 0 SvREFCNT_dec((SV *)tlv_hv);
2067 }
2068 }
2069  
2070 41 c->proxy_proto_version = 2;
2071 41 return (int)total_len;
2072 }
2073  
2074 static int
2075 305 try_parse_proxy_header(struct feer_conn *c)
2076 {
2077 305 ▶ if (SvCUR(c->rbuf) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(c->rbuf) == 0) 0 305 305
2078 0 return -2;
2079  
2080 305 unsigned char first = ((unsigned char *)SvPVX(c->rbuf))[0];
2081  
2082 305 ▶ if (first == 'P') {
Branch: 1 branch
BranchTrueFalseTotal
if (first == 'P') { 155 150 305
2083 155 return parse_proxy_v1(c);
2084 150 ▶ } else if (first == 0x0D) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (first == 0x0D) { 126 24 150
2085 126 return parse_proxy_v2(c);
2086 } else {
2087 24 return -1;
2088 }
2089 }
« feersum_conn.xs ↑ Summary feersum_psgi.c.inc »