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