feersum_h1.c.inc

filter: Statementstmt 86.4% 638 / 738 Branchbran 72.8% 389 / 534 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.7% 1027 / 1272 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« feersum_utils.c.inc ↑ Summary Feersum.xs »
1 static int
2 1545 try_parse_http(struct feer_conn *c, size_t last_read)
3 {
4 1545 struct feer_req *req = c->req;
5 1545 ▶ if (likely(!req)) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(!req)) { 1521 24 1545
6 1521 ▶ FEER_REQ_ALLOC(req);
Branch: 1 branch
BranchTrueFalseTotal
FEER_REQ_ALLOC(req); 1039 482 1521
7 1521 c->req = req;
8 }
9  
10 // phr_parse_request resets num_headers on each call; must restore to max
11 1545 req->num_headers = MAX_HEADERS;
12  
13 3090 return phr_parse_request(SvPVX(c->rbuf), SvCUR(c->rbuf),
14 &req->method, &req->method_len,
15 &req->uri, &req->uri_len, &req->minor_version,
16 1545 req->headers, &req->num_headers,
17 1545 (SvCUR(c->rbuf)-last_read));
18 }
19  
20 INLINE_UNLESS_DEBUG static void
21 1613 finish_receiving(struct feer_conn *c) {
22 /* error paths arm a lingering close first; it owns the read watcher/timer */
23 1613 ▶ if (unlikely(c->closing_linger))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->closing_linger)) 0 1613 1613
24 0 return;
25 1613 change_receiving_state(c, RECEIVE_SHUTDOWN);
26 1613 stop_read_watcher(c);
27 1613 stop_read_timer(c);
28 1613 stop_header_timer(c);
29 }
30  
31 /* after a failed parse: an unsupported major version is a 505 (RFC 9112 2.6) */
32 static int
33 13 feer_request_major_too_new (SV *rbuf)
34 {
35 dTHX;
36 STRLEN len;
37 13 const char *buf = SvPV(rbuf, len);
38 13 STRLEN scan = len > 16384 ? 16384 : len;
39 13 const char *lf = memchr(buf, '\n', scan);
40 const char *p;
41 13 ▶ if (!lf || lf - buf < 8)
Branch: 2 branches
BranchTrueFalseTotal
if (!lf || lf - buf < 8) 13 0 13
if (!lf || lf - buf < 8) 3 10 13
42 3 return 0;
43 10 p = lf;
44 10 ▶ if (p[-1] == '\r')
Branch: 1 branch
BranchTrueFalseTotal
if (p[-1] == '\r') 5 5 10
45 5 p--;
46 10 ▶ if (p - buf >= 2 && p[-2] == '.' && p[-1] >= '0' && p[-1] <= '9')
Branch: 4 branches
BranchTrueFalseTotal
if (p - buf >= 2 && p[-2] == '.' && p[-1] >= '0' && p[-1] <= '9') 10 0 10
if (p - buf >= 2 && p[-2] == '.' && p[-1] >= '0' && p[-1] <= '9') 8 2 10
if (p - buf >= 2 && p[-2] == '.' && p[-1] >= '0' && p[-1] <= '9') 8 0 8
if (p - buf >= 2 && p[-2] == '.' && p[-1] >= '0' && p[-1] <= '9') 8 0 8
47 8 p -= 2;
48 10 ▶ if (p - buf < 6 || p[-1] < '2' || p[-1] > '9')
Branch: 3 branches
BranchTrueFalseTotal
if (p - buf < 6 || p[-1] < '2' || p[-1] > '9') 10 0 10
if (p - buf < 6 || p[-1] < '2' || p[-1] > '9') 1 9 10
if (p - buf < 6 || p[-1] < '2' || p[-1] > '9') 1 0 1
49 10 return 0;
50 0 return memcmp(p - 6, "HTTP/", 5) == 0;
51 }
52  
53 static void
54 2061 try_conn_read(EV_P_ ev_io *w, int revents)
55 {
56 2061 dCONN;
57 2061 SvREFCNT_inc_void_NN(c->self);
58 2061 feer_conn_set_busy(c);
59 2061 ssize_t got_n = 0;
60  
61 2061 ▶ if (unlikely(c->pipelined)) goto pipelined;
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->pipelined)) goto pipelined; 151 1910 2061
62  
63 1910 ▶ if (unlikely(revents & EV_ERROR && !(revents & EV_READ))) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(revents & EV_ERROR && !(revents & EV_READ))) { 0 1910 1910
if (unlikely(revents & EV_ERROR && !(revents & EV_READ))) { 0 0 0
64 trace("EV error on read, fd=%d revents=0x%08x\n", w->fd, revents);
65 0 goto try_read_error;
66 }
67  
68 1910 ▶ if (unlikely(c->receiving == RECEIVE_SHUTDOWN))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->receiving == RECEIVE_SHUTDOWN)) 0 1910 1910
69 0 goto dont_read_again;
70  
71 trace("try read %d\n",w->fd);
72  
73 1910 ▶ if (unlikely(!c->rbuf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!c->rbuf)) { 1290 620 1910
74 trace("init rbuf for %d\n",w->fd);
75 1290 c->rbuf = newSV(READ_BUFSZ + 1);
76 1290 SvPOK_on(c->rbuf);
77 }
78  
79 1910 ssize_t space_free = SvLEN(c->rbuf) - SvCUR(c->rbuf);
80 // a Content-Length body is bounded by expected_cl, not max_read_buf
81 1920 ▶ if (unlikely(space_free < READ_BUFSZ) && c->receiving == RECEIVE_BODY) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(space_free < READ_BUFSZ) && c->receiving == RECEIVE_BODY) { 86 1824 1910
if (unlikely(space_free < READ_BUFSZ) && c->receiving == RECEIVE_BODY) { 10 76 86
82 10 size_t cur_len = SvLEN(c->rbuf);
83 10 size_t need = (size_t)c->expected_cl + 1;
84 10 ▶ if (cur_len < need) {
Branch: 1 branch
BranchTrueFalseTotal
if (cur_len < need) { 0 10 10
85 0 size_t new_len = cur_len + READ_GROW_FACTOR*READ_BUFSZ;
86 0 ▶ if (new_len > need) new_len = need;
Branch: 1 branch
BranchTrueFalseTotal
if (new_len > need) new_len = need; 0 0 0
87 0 ▶ SvGROW(c->rbuf, new_len);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(c->rbuf, new_len); 0 0 0
SvGROW(c->rbuf, new_len); 0 0 0
88 0 space_free = (ssize_t)(SvLEN(c->rbuf) - SvCUR(c->rbuf));
89 }
90 }
91 1900 ▶ else if (unlikely(space_free < READ_BUFSZ)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (unlikely(space_free < READ_BUFSZ)) { 76 1824 1900
92 76 size_t cur_len = SvLEN(c->rbuf);
93 // bound what is buffered, not where the next grow step would land
94 76 ▶ if (unlikely(SvCUR(c->rbuf) > c->cached_max_read_buf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(SvCUR(c->rbuf) > c->cached_max_read_buf)) { 0 76 76
95 trace("buffer too large %d: %"Sz_uf" > %"Sz_uf"\n",
96 w->fd, (Sz)SvCUR(c->rbuf), (Sz)c->cached_max_read_buf);
97 // read side only: stop_all_watchers would drop the queued response
98 0 ▶ if (c->receiving == RECEIVE_HEADERS)
Branch: 1 branch
BranchTrueFalseTotal
if (c->receiving == RECEIVE_HEADERS) 0 0 0
99 0 respond_with_server_error(c, "Request header fields too large\n", 431);
100 else
101 0 respond_with_server_error(c, "Request too large\n", 413);
102 0 goto dont_read_again;
103 }
104 76 size_t new_len = cur_len + READ_GROW_FACTOR*READ_BUFSZ;
105 trace("moar memory %d: %"Sz_uf" to %"Sz_uf"\n",
106 w->fd, (Sz)SvLEN(c->rbuf), (Sz)new_len);
107 76 ▶ SvGROW(c->rbuf, new_len);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(c->rbuf, new_len); 76 0 76
SvGROW(c->rbuf, new_len); 76 0 76
108 76 space_free = (ssize_t)(SvLEN(c->rbuf) - SvCUR(c->rbuf));
109 }
110  
111 1910 char *cur = SvPVX(c->rbuf) + SvCUR(c->rbuf);
112 1910 got_n = read(w->fd, cur, space_free);
113  
114 1910 ▶ if (unlikely(got_n <= 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(got_n <= 0)) { 37 1873 1910
115 37 ▶ if (unlikely(got_n == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(got_n == 0)) { 33 4 37
116 trace("EOF before complete request: fd=%d buf=%"Sz_uf"\n", w->fd, (Sz)SvCUR(c->rbuf));
117 33 goto try_read_error;
118 }
119 4 ▶ if (likely(errno == EAGAIN || errno == EINTR))
Branch: 2 branches
BranchTrueFalseTotal
if (likely(errno == EAGAIN || errno == EINTR)) 2 2 4
if (likely(errno == EAGAIN || errno == EINTR)) 0 2 2
120 2 goto try_read_again;
121 2 ▶ if (errno == ECONNRESET || errno == EPIPE)
Branch: 2 branches
BranchTrueFalseTotal
if (errno == ECONNRESET || errno == EPIPE) 0 2 2
if (errno == ECONNRESET || errno == EPIPE) 0 0 0
122 trace("try_conn_read fd=%d: %s\n", w->fd, strerror(errno));
123 else
124 0 trouble("try_conn_read fd=%d: %s\n", w->fd, strerror(errno));
125 2 goto try_read_error;
126 }
127  
128 trace("read %d %"Ssz_df"\n", w->fd, (Ssz)got_n);
129 1873 SvCUR(c->rbuf) += got_n;
130 1873 begin_request_headers(c);
131 1873 goto try_parse;
132  
133 151 pipelined:
134 151 got_n = c->pipelined;
135 151 c->pipelined = 0;
136 /* no socket event for these bytes: without the WAIT->HEADERS transition
137 * a half-written pipelined request gets no header deadline */
138 151 begin_request_headers(c);
139  
140 2024 try_parse:
141 2071 ▶ if (unlikely(c->receiving == RECEIVE_PROXY_HEADER)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->receiving == RECEIVE_PROXY_HEADER)) { 153 1918 2071
142 153 int ret = try_parse_proxy_header(c);
143 153 ▶ if (ret == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == -1) { 105 48 153
144 105 respond_with_server_error(c, "Invalid PROXY protocol header\n", 400);
145 105 goto dont_read_again;
146 }
147 48 ▶ if (ret == -2) goto try_read_again_reset_timer;
Branch: 1 branch
BranchTrueFalseTotal
if (ret == -2) goto try_read_again_reset_timer; 1 47 48
148  
149 47 STRLEN remaining = SvCUR(c->rbuf) - ret;
150 47 ▶ if (remaining > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (remaining > 0) 47 0 47
151 47 memmove(SvPVX(c->rbuf), SvPVX(c->rbuf) + ret, remaining);
152 47 SvCUR_set(c->rbuf, remaining);
153  
154 47 ▶ feer_clear_remote_cache(c);
Branch: 2 branches
BranchTrueFalseTotal
feer_clear_remote_cache(c); 0 47 47
feer_clear_remote_cache(c); 0 47 47
155  
156 47 change_receiving_state(c, RECEIVE_HEADERS);
157 47 ▶ if (remaining > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (remaining > 0) { 47 0 47
158 47 got_n = remaining;
159 47 goto try_parse;
160 }
161 0 goto try_read_again_reset_timer;
162 }
163  
164 1918 ▶ if (likely(c->receiving <= RECEIVE_HEADERS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(c->receiving <= RECEIVE_HEADERS)) { 1374 544 1918
165 1374 int ret = try_parse_http(c, (size_t)got_n);
166 1374 ▶ if (ret == -1) goto try_read_bad;
Branch: 1 branch
BranchTrueFalseTotal
if (ret == -1) goto try_read_bad; 15 1359 1374
167 1359 ▶ if (ret == -2) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == -2) { 30 1329 1359
168 /* the growth check only fires when space runs out; cap here too */
169 30 ▶ if (unlikely(SvCUR(c->rbuf) > c->cached_max_read_buf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(SvCUR(c->rbuf) > c->cached_max_read_buf)) { 1 29 30
170 trace("header buffer too large %d: %"Sz_uf" > %"Sz_uf"\n",
171 w->fd, (Sz)SvCUR(c->rbuf), (Sz)c->cached_max_read_buf);
172 1 respond_with_server_error(c,
173 "Request header fields too large\n", 431);
174 1 goto dont_read_again;
175 }
176 #ifdef TCP_DEFER_ACCEPT
177 29 goto try_read_again_reset_timer;
178 #else
179 if (c->cached_is_tcp) goto try_read_again;
180 else goto try_read_again_reset_timer;
181 #endif
182 }
183  
184 /* ret is the body offset, i.e. the header block size */
185 1329 ▶ if (unlikely((STRLEN)ret > c->cached_max_read_buf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely((STRLEN)ret > c->cached_max_read_buf)) { 0 1329 1329
186 trace("header block too large %d: %"Sz_uf" > %"Sz_uf"\n",
187 w->fd, (Sz)ret, (Sz)c->cached_max_read_buf);
188 0 respond_with_server_error(c, "Request header fields too large\n", 431);
189 0 goto dont_read_again;
190 }
191 1329 ▶ if (process_request_headers(c, ret))
Branch: 1 branch
BranchTrueFalseTotal
if (process_request_headers(c, ret)) 48 1281 1329
192 48 goto try_read_again_reset_timer;
193 else
194 1281 goto dont_read_again;
195 }
196 544 ▶ else if (likely(c->receiving == RECEIVE_BODY)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (likely(c->receiving == RECEIVE_BODY)) { 25 519 544
197 25 c->received_cl += got_n;
198 25 ▶ if (c->received_cl < c->expected_cl)
Branch: 1 branch
BranchTrueFalseTotal
if (c->received_cl < c->expected_cl) 1 24 25
199 1 goto try_read_again_reset_timer;
200 24 sched_request_callback(c);
201 24 goto dont_read_again;
202 }
203 519 ▶ else if (c->receiving == RECEIVE_CHUNKED) {
Branch: 1 branch
BranchTrueFalseTotal
else if (c->receiving == RECEIVE_CHUNKED) { 519 0 519
204 519 int ret = try_parse_chunked(c);
205 519 ▶ if (ret == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == 1) { 500 19 519
206 500 ▶ if (unlikely(SvCUR(c->rbuf) > c->cached_max_read_buf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(SvCUR(c->rbuf) > c->cached_max_read_buf)) { 1 499 500
207 trace("chunked buffer too large %d: %"Sz_uf" > %"Sz_uf"\n",
208 w->fd, (Sz)SvCUR(c->rbuf), (Sz)c->cached_max_read_buf);
209 1 respond_with_server_error(c, "Request too large\n", 413);
210 1 goto dont_read_again;
211 }
212 499 goto try_read_again_reset_timer;
213 }
214 19 ▶ if (ret == -2) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == -2) { 13 6 19
215 13 respond_with_server_error(c, "Request body too large\n", 413);
216 13 goto dont_read_again;
217 }
218 6 ▶ if (ret == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == -1) { 2 4 6
219 2 respond_with_server_error(c, "Malformed chunked encoding\n", 400);
220 2 goto dont_read_again;
221 }
222 4 sched_request_callback(c);
223 4 goto dont_read_again;
224 }
225 0 ▶ else if (c->receiving == RECEIVE_STREAMING) {
Branch: 1 branch
BranchTrueFalseTotal
else if (c->receiving == RECEIVE_STREAMING) { 0 0 0
226 0 c->received_cl += got_n;
227 0 ▶ if (c->poll_read_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->poll_read_cb) { 0 0 0
228 0 call_poll_callback(c, 0); // 0 = read callback
229 }
230 // the callback may have closed the connection
231 0 ▶ if (c->receiving >= RECEIVE_SHUTDOWN)
Branch: 1 branch
BranchTrueFalseTotal
if (c->receiving >= RECEIVE_SHUTDOWN) 0 0 0
232 0 goto dont_read_again;
233 0 ▶ if (c->expected_cl > 0 && c->received_cl >= c->expected_cl) {
Branch: 2 branches
BranchTrueFalseTotal
if (c->expected_cl > 0 && c->received_cl >= c->expected_cl) { 0 0 0
if (c->expected_cl > 0 && c->received_cl >= c->expected_cl) { 0 0 0
234 0 goto dont_read_again;
235 }
236 0 goto try_read_again_reset_timer;
237 }
238 else {
239 0 trouble("unknown read state fd=%d state=%d\n", w->fd, c->receiving);
240 }
241  
242 35 try_read_error:
243 trace("READ ERROR %d, refcnt=%d\n", w->fd, SvREFCNT(c->self));
244 35 change_receiving_state(c, RECEIVE_SHUTDOWN);
245 35 change_responding_state(c, RESPOND_SHUTDOWN);
246 35 stop_all_watchers(c);
247 35 goto try_read_cleanup;
248  
249 15 try_read_bad:
250 trace("bad request %d\n", w->fd);
251 /* picohttpparser returns -1 for malformed and for too many headers alike;
252 * only the cap leaves num_headers at MAX_HEADERS */
253 15 ▶ if (unlikely(c->req && c->req->num_headers >= MAX_HEADERS))
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(c->req && c->req->num_headers >= MAX_HEADERS)) 15 0 15
if (unlikely(c->req && c->req->num_headers >= MAX_HEADERS)) 2 13 15
254 2 respond_with_server_error(c, "Too many header fields\n", 431);
255 13 ▶ else if (unlikely(c->rbuf && feer_request_major_too_new(c->rbuf))) {
Branch: 2 branches
BranchTrueFalseTotal
else if (unlikely(c->rbuf && feer_request_major_too_new(c->rbuf))) { 13 0 13
else if (unlikely(c->rbuf && feer_request_major_too_new(c->rbuf))) { 0 13 13
256 0 c->is_http11 = 1;
257 0 respond_with_server_error(c, "HTTP Version Not Supported\n", 505);
258 }
259 else
260 13 respond_with_server_error(c, "Malformed request\n", 400);
261 1446 dont_read_again:
262 trace("done reading %d\n", w->fd);
263 1446 finish_receiving(c);
264 1446 goto try_read_cleanup;
265  
266 578 try_read_again_reset_timer:
267 trace("(reset read timer) %d\n", w->fd);
268 578 restart_read_timer(c);
269 580 try_read_again:
270 trace("read again %d\n", w->fd);
271 580 start_read_watcher(c);
272  
273 2061 try_read_cleanup:
274 2061 SvREFCNT_dec(c->self);
275 2061 }
276  
277 static void
278 17 conn_read_timeout (EV_P_ ev_timer *w, int revents)
279 {
280 17 dCONN;
281 17 SvREFCNT_inc_void_NN(c->self);
282  
283 17 ▶ if (unlikely(!(revents & EV_TIMER) || c->receiving == RECEIVE_SHUTDOWN)) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(!(revents & EV_TIMER) || c->receiving == RECEIVE_SHUTDOWN)) { 17 0 17
if (unlikely(!(revents & EV_TIMER) || c->receiving == RECEIVE_SHUTDOWN)) { 0 17 17
284 0 ▶ if (revents & EV_ERROR)
Branch: 1 branch
BranchTrueFalseTotal
if (revents & EV_ERROR) 0 0 0
285 0 trouble("EV error on read timer, fd=%d revents=0x%08x\n",
286 c->fd,revents);
287 0 goto read_timeout_cleanup;
288 }
289  
290 trace("read timeout %d\n", c->fd);
291  
292 #ifdef FEERSUM_HAS_H2
293 if (c->h2_session) {
294 /* H2 cannot stop these timers at tunnel takeover (sibling streams still
295 * need the idle bound), so re-arm. A dispatched stream is not idle, but
296 * one owing bytes the peer will not take is stalled: strike it. */
297 /* once nghttp2 has terminated, app handles cannot excuse owed output */
298 int stalled = c->h2_close_pending || c->tls_wbuf.off > 0;
299 int app_waiting = 0;
300 int tunnel_live = 0;
301 int wire_progress = 0;
302 #ifdef FEERSUM_HAS_OUTQ
303 /* a TCP send backlog is owed output; only a shrinking one is progress */
304 int outq = feer_sock_outq(c->fd);
305 if (outq > 0) {
306 stalled = 1;
307 wire_progress = c->prev_read_outq > 0 && outq < c->prev_read_outq;
308 c->prev_read_outq = outq;
309 } else
310 c->prev_read_outq = 0;
311 #endif
312 for (struct feer_h2_stream *s = c->h2_close_pending ? NULL : c->h2_streams;
313 s; s = s->next) {
314 if (!s->tunnel_established && !s->pseudo_conn)
315 continue;
316 /* past transport EOF no WINDOW_UPDATE can unblock a tunnel */
317 if (s->tunnel_established && !c->tls_input_eof)
318 tunnel_live = 1;
319 else if (h2_stream_send_pending(s))
320 stalled = 1;
321 else
322 app_waiting = 1;
323 }
324 /* decide after scanning all siblings: a waiting app must not mask a stall */
325 if (tunnel_live || wire_progress || (app_waiting && !stalled)) {
326 trace("H2 idle timeout deferred, stream live fd=%d\n", c->fd);
327 c->h2_stall_strikes = 0;
328 restart_read_timer(c);
329 goto read_timeout_cleanup;
330 }
331 if (stalled && ++c->h2_stall_strikes < FEER_H2_STALL_STRIKES) {
332 trace("H2 stall strike %d fd=%d\n", c->h2_stall_strikes, c->fd);
333 restart_read_timer(c);
334 goto read_timeout_cleanup;
335 }
336 /* respond_with_server_error writes HTTP/1.1; H2 gets a GOAWAY */
337 trace("H2 idle timeout fd=%d\n", c->fd);
338 if (!c->h2_goaway_sent) {
339 nghttp2_submit_goaway(c->h2_session, NGHTTP2_FLAG_NONE,
340 nghttp2_session_get_last_proc_stream_id(c->h2_session),
341 NGHTTP2_NO_ERROR, NULL, 0);
342 c->h2_goaway_sent = 1;
343 feer_h2_session_send(c);
344 }
345 feer_shutdown_conn(c, "H2 idle timeout");
346 goto read_timeout_cleanup;
347 }
348 #endif
349  
350 /* a taken socket's deadline belongs to the app */
351 17 ▶ if (unlikely(c->io_taken))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->io_taken)) 0 17 17
352 0 goto read_timeout_cleanup;
353  
354 24 ▶ if (likely(c->responding == RESPOND_NOT_STARTED) && c->receiving >= RECEIVE_HEADERS) {
Branch: 2 branches
BranchTrueFalseTotal
if (likely(c->responding == RESPOND_NOT_STARTED) && c->receiving >= RECEIVE_HEADERS) { 17 0 17
if (likely(c->responding == RESPOND_NOT_STARTED) && c->receiving >= RECEIVE_HEADERS) { 11 6 17
355 #ifdef FEERSUM_HAS_TLS
356 11 ▶ if (c->tls && !c->tls_handshake_done) {
Branch: 2 branches
BranchTrueFalseTotal
if (c->tls && !c->tls_handshake_done) { 4 7 11
if (c->tls && !c->tls_handshake_done) { 4 0 4
357 4 feer_shutdown_conn(c, "TLS handshake timeout (read)");
358 4 goto read_timeout_cleanup;
359 }
360 #endif
361 const char *msg;
362 7 ▶ if (c->receiving == RECEIVE_PROXY_HEADER) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->receiving == RECEIVE_PROXY_HEADER) { 1 6 7
363 1 msg = "PROXY protocol header timeout.";
364 }
365 6 ▶ else if (c->receiving == RECEIVE_HEADERS) {
Branch: 1 branch
BranchTrueFalseTotal
else if (c->receiving == RECEIVE_HEADERS) { 3 3 6
366 3 msg = "Headers took too long.";
367 }
368 else {
369 3 msg = "Timeout reading body.";
370 }
371 7 respond_with_server_error(c, msg, 408);
372 6 ▶ } else if (feer_conn_still_delivering(c)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (feer_conn_still_delivering(c)) { 4 2 6
373 /* this clock doubles as the keepalive idle clock; delivering is not idle */
374 trace("read deadline deferred, still delivering fd=%d\n", c->fd);
375 4 restart_read_timer(c);
376 } else {
377 trace("read timeout in keepalive conn: %d\n", c->fd);
378 /* linger so a late pipelined request is absorbed instead of RST-ing
379 * away the tail of the previous response */
380 2 feer_linger_close(c, "close at read timeout");
381 2 change_receiving_state(c, RECEIVE_SHUTDOWN);
382 }
383  
384 17 read_timeout_cleanup:
385 17 SvREFCNT_dec(c->self);
386 17 }
387  
388 // non-resetting header deadline (Slowloris)
389 static void
390 8 conn_header_timeout (EV_P_ ev_timer *w, int revents)
391 {
392 8 dCONN;
393 8 SvREFCNT_inc_void_NN(c->self);
394  
395 8 ▶ if (unlikely(!(revents & EV_TIMER) || c->receiving == RECEIVE_SHUTDOWN)) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(!(revents & EV_TIMER) || c->receiving == RECEIVE_SHUTDOWN)) { 8 0 8
if (unlikely(!(revents & EV_TIMER) || c->receiving == RECEIVE_SHUTDOWN)) { 0 8 8
396 0 ▶ if (revents & EV_ERROR)
Branch: 1 branch
BranchTrueFalseTotal
if (revents & EV_ERROR) 0 0 0
397 0 trouble("EV error on header timer, fd=%d revents=0x%08x\n",
398 c->fd, revents);
399 0 goto header_timeout_cleanup;
400 }
401  
402 #ifdef FEERSUM_HAS_H2
403 if (c->h2_session) {
404 /* an unfinished header block blocks every stream: a reset cannot recover */
405 trace("H2 header deadline fd=%d\n", c->fd);
406 if (!c->h2_goaway_sent) {
407 nghttp2_submit_goaway(c->h2_session, NGHTTP2_FLAG_NONE,
408 nghttp2_session_get_last_proc_stream_id(c->h2_session),
409 NGHTTP2_ENHANCE_YOUR_CALM, NULL, 0);
410 c->h2_goaway_sent = 1;
411 feer_h2_session_send(c);
412 }
413 feer_shutdown_conn(c, "H2 header timeout");
414 change_receiving_state(c, RECEIVE_SHUTDOWN);
415 goto header_timeout_cleanup;
416 }
417 #endif
418  
419 8 ▶ if (unlikely(c->io_taken))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->io_taken)) 1 7 8
420 1 goto header_timeout_cleanup;
421  
422 7 ▶ if ((c->receiving == RECEIVE_HEADERS || c->receiving == RECEIVE_PROXY_HEADER)
Branch: 2 branches
BranchTrueFalseTotal
if ((c->receiving == RECEIVE_HEADERS || c->receiving == RECEIVE_PROXY_HEADER) 0 7 7
if ((c->receiving == RECEIVE_HEADERS || c->receiving == RECEIVE_PROXY_HEADER) 0 0 0
423 7 ▶ && c->responding == RESPOND_NOT_STARTED) {
Branch: 1 branch
BranchTrueFalseTotal
&& c->responding == RESPOND_NOT_STARTED) { 0 7 7
424 trace("header deadline timeout %d (Slowloris protection)\n", c->fd);
425 #ifdef FEERSUM_HAS_TLS
426 7 ▶ if (c->tls && !c->tls_handshake_done) {
Branch: 2 branches
BranchTrueFalseTotal
if (c->tls && !c->tls_handshake_done) { 1 6 7
if (c->tls && !c->tls_handshake_done) { 0 1 1
427 0 feer_shutdown_conn(c, "TLS handshake timeout");
428 } else
429 #endif
430 {
431 7 respond_with_server_error(c, "Header timeout (possible Slowloris attack)\n", 408);
432 }
433 }
434  
435 0 header_timeout_cleanup:
436 8 SvREFCNT_dec(c->self); // the timer's ref: one-shot, libev already stopped it
437 8 SvREFCNT_dec(c->self); // the callback guard
438 8 }
439  
440 static void
441 467 conn_write_timeout (EV_P_ ev_timer *w, int revents)
442 {
443 467 dCONN;
444 467 SvREFCNT_inc_void_NN(c->self);
445  
446 // no RESPOND_SHUTDOWN guard: a complete response enters it when queued,
447 // before the drain
448 467 ▶ if (unlikely(!(revents & EV_TIMER))) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!(revents & EV_TIMER))) { 0 467 467
449 0 ▶ if (revents & EV_ERROR)
Branch: 1 branch
BranchTrueFalseTotal
if (revents & EV_ERROR) 0 0 0
450 0 trouble("EV error on write timer, fd=%d revents=0x%08x\n",
451 c->fd, revents);
452 0 goto write_timeout_cleanup;
453 }
454  
455 /* poll_cb re-invitation, not a deadline; stop the timer first since
456 * ev_timer_again made it periodic */
457 467 ▶ if (unlikely(c->poll_retry_pending)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->poll_retry_pending)) { 464 3 467
458 464 c->poll_retry_pending = 0;
459 464 ▶ if (likely(ev_is_active(w))) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(ev_is_active(w))) { 464 0 464
460 464 ev_timer_stop(feersum_ev_loop, w);
461 464 SvREFCNT_dec(c->self);
462 }
463 trace("poll retry %d\n", c->fd);
464 464 conn_write_ready(c);
465 464 goto write_timeout_cleanup;
466 }
467  
468 /* no writev progress is not a stalled peer: EPOLLOUT needs half the sndbuf
469 * free. A shrinking kernel queue means the peer is draining; ACKs arrive
470 * in clumps, so reap after three drain-free intervals, not one. */
471 #ifdef FEERSUM_HAS_OUTQ
472 {
473 3 int outq = feer_sock_outq(c->fd);
474 3 ▶ if (outq > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (outq > 0) { 3 0 3
475 2 ▶ int stalls = (c->prev_outq > 0 && outq < c->prev_outq)
Branch: 1 branch
BranchTrueFalseTotal
int stalls = (c->prev_outq > 0 && outq < c->prev_outq) 2 0 2
476 5 ▶ ? 0 : c->outq_stalls + 1;
Branch: 1 branch
BranchTrueFalseTotal
? 0 : c->outq_stalls + 1; 2 1 3
477 3 ▶ if (stalls < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (stalls < 3) { 2 1 3
478 trace("write deadline deferred fd=%d outq=%d prev=%d stalls=%d\n",
479 c->fd, outq, c->prev_outq, stalls);
480 2 restart_write_timer(c); /* resets both trackers; re-store */
481 2 c->prev_outq = outq;
482 2 c->outq_stalls = stalls;
483 2 goto write_timeout_cleanup;
484 }
485 }
486 }
487 #endif
488  
489 trace("write timeout %d\n", c->fd);
490 #ifdef FEERSUM_HAS_H2
491 /* a live tunnel is exempt; the deadline exists for H2 only to bound
492 * tls_wbuf, so bound that by size instead */
493 if (c->h2_session && c->tls && c->tls_wbuf.off < FEER_TUNNEL_MAX_WBUF) {
494 for (struct feer_h2_stream *s = c->h2_streams; s; s = s->next) {
495 if (!s->tunnel_established) continue;
496 trace("H2 write deadline deferred, tunnel live fd=%d\n", c->fd);
497 restart_write_timer(c);
498 goto write_timeout_cleanup;
499 }
500 }
501 /* GOAWAY(last_stream_id) tells the client which in-flight requests it may retry */
502 if (c->h2_session && !c->h2_goaway_sent) {
503 nghttp2_submit_goaway(c->h2_session, NGHTTP2_FLAG_NONE,
504 nghttp2_session_get_last_proc_stream_id(c->h2_session),
505 NGHTTP2_NO_ERROR, NULL, 0);
506 c->h2_goaway_sent = 1;
507 feer_h2_session_send(c);
508 }
509 #endif
510 1 feer_shutdown_conn(c, "write timeout");
511 1 change_receiving_state(c, RECEIVE_SHUTDOWN);
512  
513 467 write_timeout_cleanup:
514 467 SvREFCNT_dec(c->self);
515 467 }
516  
517 // returns -1 with fd already closed
518 static int
519 1448 setup_accepted_conn(EV_P_ int fd, struct sockaddr *sa, socklen_t sa_len,
520 struct feer_server *srvr, struct feer_listen *lsnr)
521 {
522 1448 ▶ if (unlikely(prep_socket(fd, lsnr->is_tcp))) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(prep_socket(fd, lsnr->is_tcp))) { 0 1448 1448
523 0 trouble("prep_socket failed for fd=%d: %s\n", fd, strerror(errno));
524 0 ▶ if (unlikely(close(fd) < 0))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(close(fd) < 0)) 0 0 0
525 0 trouble("close(prep_socket error) fd=%d: %s\n", fd, strerror(errno));
526 0 return -1;
527 }
528  
529 1448 struct feer_conn *c = new_feer_conn(EV_A, fd, sa, sa_len, srvr, lsnr);
530  
531 // arm at accept: the lazy arming needs parsed bytes first and
532 // header_timeout may be 0
533 1448 restart_read_timer(c);
534  
535 1448 ▶ if (srvr->header_timeout > 0.0) {
Branch: 1 branch
BranchTrueFalseTotal
if (srvr->header_timeout > 0.0) { 1447 1 1448
536 1447 ev_timer_set(&c->header_ev_timer, srvr->header_timeout, 0.0);
537 1447 ev_timer_start(feersum_ev_loop, &c->header_ev_timer);
538 1447 SvREFCNT_inc_void_NN(c->self);
539 trace("started header deadline timer %d (%.1fs)\n", c->fd, srvr->header_timeout);
540 }
541  
542 #ifdef TCP_DEFER_ACCEPT
543 // drain_accept_queue turns TCP_DEFER_ACCEPT off, so data may not be there
544 #ifdef FEERSUM_HAS_TLS
545 1448 ▶ if (lsnr->tls_ctx_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (lsnr->tls_ctx_ref) { 170 1278 1448
546 // the TLS handshake needs event-driven I/O
547 170 start_read_watcher(c);
548 } else
549 #endif
550 {
551 1278 try_conn_read(EV_A, &c->read_ev_io, EV_READ);
552 }
553 assert(SvREFCNT(c->self) <= (srvr->header_timeout > 0.0 ? 5 : 4));
554 #else
555 if (lsnr->is_tcp) {
556 start_read_watcher(c);
557 assert(SvREFCNT(c->self) == (srvr->header_timeout > 0.0 ? 4 : 3));
558 } else {
559 #ifdef FEERSUM_HAS_TLS
560 // try_tls_conn_read never arms a watcher itself
561 if (lsnr->tls_ctx_ref)
562 start_read_watcher(c);
563 else
564 #endif
565 try_conn_read(EV_A, &c->read_ev_io, EV_READ);
566 assert(SvREFCNT(c->self) <= (srvr->header_timeout > 0.0 ? 5 : 4));
567 }
568 #endif
569 1448 SvREFCNT_dec(c->self);
570 1448 return 0;
571 }
572  
573 static void
574 0 emfile_retry_cb (EV_P_ ev_timer *w, int revents)
575 {
576 PERL_UNUSED_VAR(revents);
577 0 struct feer_listen *lsnr = (struct feer_listen *)w->data;
578 0 ▶ if (lsnr->server->shutting_down) return;
Branch: 1 branch
BranchTrueFalseTotal
if (lsnr->server->shutting_down) return; 0 0 0
579 trace("EMFILE retry: clearing emfile pause on fd=%d\n", lsnr->fd);
580 0 lsnr->pause_flags &= ~FEER_PAUSE_EMFILE;
581 0 ▶ if (!lsnr->pause_flags && lsnr->fd >= 0)
Branch: 2 branches
BranchTrueFalseTotal
if (!lsnr->pause_flags && lsnr->fd >= 0) 0 0 0
if (!lsnr->pause_flags && lsnr->fd >= 0) 0 0 0
582 0 ev_io_start(EV_A, &lsnr->accept_w);
583 }
584  
585 /* returns 1 to keep accepting, 0 to stop */
586 static int
587 2921 try_accept_one(EV_P_ struct feer_listen *lsnr, struct feer_server *srvr)
588 {
589 struct sockaddr_storage sa_buf;
590 2921 socklen_t sa_len = sizeof(struct sockaddr_storage);
591 2921 errno = 0;
592 #ifdef HAS_ACCEPT4
593 2921 int fd = accept4(lsnr->fd, (struct sockaddr *)&sa_buf, &sa_len, SOCK_CLOEXEC|SOCK_NONBLOCK);
594 #else
595 int fd = accept(lsnr->fd, (struct sockaddr *)&sa_buf, &sa_len);
596 #endif
597 trace("accepted fd=%d, errno=%d\n", fd, errno);
598 2921 ▶ if (fd == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd == -1) { 1371 1550 2921
599 1371 ▶ if (errno == EINTR) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) return 1; 0 1371 1371
600 /* the pending connection stays queued, so a level-triggered watcher
601 * would spin on the failing accept() */
602 1371 ▶ if (errno == EMFILE || errno == ENFILE
Branch: 2 branches
BranchTrueFalseTotal
if (errno == EMFILE || errno == ENFILE 1371 0 1371
if (errno == EMFILE || errno == ENFILE 1371 0 1371
603 1371 ▶ || errno == ENOBUFS || errno == ENOMEM) {
Branch: 2 branches
BranchTrueFalseTotal
|| errno == ENOBUFS || errno == ENOMEM) { 1371 0 1371
|| errno == ENOBUFS || errno == ENOMEM) { 0 1371 1371
604 0 trouble("accept: %s - pausing accept for 1s on fd=%d\n",
605 strerror(errno), lsnr->fd);
606 0 ev_io_stop(EV_A, &lsnr->accept_w);
607 0 lsnr->pause_flags |= FEER_PAUSE_EMFILE;
608 0 ev_timer_set(&lsnr->emfile_w, 1.0, 0.0);
609 0 ev_timer_start(EV_A, &lsnr->emfile_w);
610 }
611 1371 return 0;
612 }
613  
614 /* admit against sockets: active_conns also counts H2 pseudo-conns, which
615 * max_h2_concurrent_streams already bounds */
616 1550 int socket_conns = srvr->active_conns;
617 #ifdef FEERSUM_HAS_H2
618 socket_conns -= srvr->active_h2_streams;
619 #endif
620 1550 ▶ if (srvr->max_connections > 0 && socket_conns >= srvr->max_connections) {
Branch: 2 branches
BranchTrueFalseTotal
if (srvr->max_connections > 0 && socket_conns >= srvr->max_connections) { 1519 31 1550
if (srvr->max_connections > 0 && socket_conns >= srvr->max_connections) { 126 1393 1519
621 126 ▶ if (!feer_server_recycle_idle_conn(srvr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!feer_server_recycle_idle_conn(srvr)) { 102 24 126
622 /* resumed when a socket closes or turns idle/evictable */
623 trace("max_connections limit reached (%d), pausing accept on fd=%d\n",
624 srvr->max_connections, lsnr->fd);
625 102 close(fd);
626 102 ev_io_stop(EV_A, &lsnr->accept_w);
627 102 lsnr->pause_flags |= FEER_PAUSE_CAP;
628 102 return 0;
629 }
630 }
631  
632 1448 setup_accepted_conn(EV_A, fd, (struct sockaddr *)&sa_buf, sa_len, srvr, lsnr);
633 1448 return 1;
634 }
635  
636 static void
637 1472 accept_cb (EV_P_ ev_io *w, int revents)
638 {
639 1472 struct feer_listen *lsnr = (struct feer_listen *)w->data;
640 1472 struct feer_server *srvr = lsnr->server;
641  
642 1472 ▶ if (unlikely(srvr->shutting_down)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(srvr->shutting_down)) { 0 1472 1472
643 0 ev_io_stop(EV_A, w);
644 0 return;
645 }
646  
647 1472 ▶ if (unlikely(revents & EV_ERROR)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(revents & EV_ERROR)) { 0 1472 1472
648 0 trouble("EV error in accept_cb, fd=%d, revents=0x%08x\n",w->fd,revents);
649 0 ev_break(EV_A, EVBREAK_ALL);
650 0 return;
651 }
652  
653 trace2("accept! revents=0x%08x\n", revents);
654  
655 1472 int accept_count = 0;
656 2919 ▶ while (accept_count++ < srvr->max_accept_per_loop) {
Branch: 1 branch
BranchTrueFalseTotal
while (accept_count++ < srvr->max_accept_per_loop) { 2919 0 2919
657 2919 ▶ if (!try_accept_one(EV_A, lsnr, srvr)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!try_accept_one(EV_A, lsnr, srvr)) break; 1472 1447 2919
658 }
659 }
660  
661 static void
662 565 setup_accept_watcher(struct feer_listen *lsnr, int listen_fd)
663 {
664 565 struct feer_server *srvr = lsnr->server;
665 565 ev_io_init(&lsnr->accept_w, accept_cb, listen_fd, EV_READ);
666 565 lsnr->accept_w.data = (void *)lsnr;
667 565 ev_set_priority(&lsnr->accept_w, srvr->accept_priority);
668 565 ev_init(&lsnr->emfile_w, emfile_retry_cb);
669 565 lsnr->emfile_w.data = (void *)lsnr;
670 565 }
671  
672 static void
673 1386 sched_request_callback (struct feer_conn *c)
674 {
675 1386 struct feer_server *server = c->server;
676 trace("sched req callback: %d c=%p, head=%p\n", c->fd, c, server->request_ready_rinq);
677 1386 rinq_push(&server->request_ready_rinq, c);
678 1386 SvREFCNT_inc_void_NN(c->self); // for the rinq
679 1386 ▶ if (!ev_is_active(&server->ei)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ev_is_active(&server->ei)) { 937 449 1386
680 trace("starting idle watcher\n");
681 937 ev_idle_start(feersum_ev_loop, &server->ei);
682 }
683 1386 }
684  
685 // Decodes in place, rbuf = [decoded body][unparsed]: received_cl is the decoded
686 // length, chunk_remaining the state (CHUNK_STATE_*). Returns 1 need more,
687 // 0 complete, -1 malformed, -2 over the size limit
688 static int
689 602 try_parse_chunked (struct feer_conn *c)
690 {
691 602 ▶ if (!c->rbuf) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!c->rbuf) return 1; 0 602 602
692  
693 602 char *buf = SvPVX(c->rbuf);
694 602 STRLEN buf_len = SvCUR(c->rbuf);
695 602 STRLEN read_pos = (c->received_cl >= 0) ? (STRLEN)c->received_cl : 0;
696 602 STRLEN write_pos = read_pos;
697  
698 trace("try_parse_chunked fd=%d buf_len=%"Sz_uf" read_pos=%"Sz_uf" chunk_remaining=%"Ssz_df"\n",
699 c->fd, (Sz)buf_len, (Sz)read_pos, (Ssz)c->chunk_remaining);
700  
701 6161 ▶ while (read_pos < buf_len) {
Branch: 1 branch
BranchTrueFalseTotal
while (read_pos < buf_len) { 6159 2 6161
702 6159 ▶ if (c->chunk_remaining == CHUNK_STATE_NEED_CRLF) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->chunk_remaining == CHUNK_STATE_NEED_CRLF) { 2 6157 6159
703 2 STRLEN remaining = buf_len - read_pos;
704 2 ▶ if (remaining < 2)
Branch: 1 branch
BranchTrueFalseTotal
if (remaining < 2) 0 2 2
705 0 goto need_more;
706 2 ▶ if (buf[read_pos] != '\r' || buf[read_pos+1] != '\n') {
Branch: 2 branches
BranchTrueFalseTotal
if (buf[read_pos] != '\r' || buf[read_pos+1] != '\n') { 2 0 2
if (buf[read_pos] != '\r' || buf[read_pos+1] != '\n') { 0 2 2
707 trace("chunked: missing CRLF after chunk data\n");
708 0 return -1;
709 }
710 2 read_pos += 2;
711 2 c->chunk_remaining = CHUNK_STATE_PARSE_SIZE;
712 2 continue;
713 }
714 6157 ▶ else if (c->chunk_remaining == CHUNK_STATE_PARSE_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
else if (c->chunk_remaining == CHUNK_STATE_PARSE_SIZE) { 3296 2861 6157
715 3296 char *line_start = buf + read_pos;
716 3296 char *line_end = NULL;
717 3296 STRLEN remaining = buf_len - read_pos;
718 STRLEN i;
719  
720 309927 ▶ for (i = 0; i + 1 < remaining; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < remaining; i++) { 309463 464 309927
721 309463 ▶ if (line_start[i] == '\r' && line_start[i+1] == '\n') {
Branch: 2 branches
BranchTrueFalseTotal
if (line_start[i] == '\r' && line_start[i+1] == '\n') { 2832 306631 309463
if (line_start[i] == '\r' && line_start[i+1] == '\n') { 2832 0 2832
722 2832 line_end = line_start + i;
723 2832 break;
724 }
725 }
726  
727 3296 ▶ if (!line_end) {
Branch: 1 branch
BranchTrueFalseTotal
if (!line_end) { 464 2832 3296
728 // the scan restarts every read event: an unbounded line is quadratic
729 464 ▶ if (unlikely(remaining > MAX_CHUNK_SIZE_LINE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(remaining > MAX_CHUNK_SIZE_LINE)) { 2 462 464
730 trace("chunked: chunk size line too long (%"Sz_uf")\n",
731 (Sz)remaining);
732 2 return -1;
733 }
734 trace("chunked: need more data for chunk size line\n");
735 462 goto need_more;
736 }
737  
738 2832 UV chunk_size = 0;
739 2832 int hex_digits = 0;
740 2832 char *p = line_start;
741 5879 ▶ while (p < line_end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < line_end) { 3063 2816 5879
742 3063 unsigned char ch = (unsigned char)*p;
743 3063 unsigned char val = hex_decode_table[ch];
744 3063 ▶ if (val != 0xFF) {
Branch: 1 branch
BranchTrueFalseTotal
if (val != 0xFF) { 3048 15 3063
745 3048 hex_digits++;
746 3048 ▶ if (chunk_size > (UV_MAX >> 4)) {
Branch: 1 branch
BranchTrueFalseTotal
if (chunk_size > (UV_MAX >> 4)) { 1 3047 3048
747 trace("chunked: chunk size overflow\n");
748 1 return -1;
749 }
750 3047 chunk_size = (chunk_size << 4) | val;
751 }
752 15 ▶ else if (ch == ';') {
Branch: 1 branch
BranchTrueFalseTotal
else if (ch == ';') { 7 8 15
753 7 break;
754 }
755 8 ▶ else if (ch == ' ' || ch == '\t') {
Branch: 2 branches
BranchTrueFalseTotal
else if (ch == ' ' || ch == '\t') { 6 2 8
else if (ch == ' ' || ch == '\t') { 0 6 6
756 2 char *q = p + 1;
757 4 ▶ while (q < line_end && (*q == ' ' || *q == '\t'))
Branch: 3 branches
BranchTrueFalseTotal
while (q < line_end && (*q == ' ' || *q == '\t')) 3 1 4
while (q < line_end && (*q == ' ' || *q == '\t')) 2 1 3
while (q < line_end && (*q == ' ' || *q == '\t')) 0 1 1
758 2 q++;
759 2 ▶ if (q >= line_end || *q == ';')
Branch: 2 branches
BranchTrueFalseTotal
if (q >= line_end || *q == ';') 1 1 2
if (q >= line_end || *q == ';') 0 1 1
760 break;
761 trace("chunked: junk after chunk size\n");
762 0 return -1;
763 }
764 else {
765 trace("chunked: invalid hex char '%c'\n", ch);
766 6 return -1;
767 }
768 3047 p++;
769 }
770  
771 2825 ▶ if (hex_digits == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (hex_digits == 0) { 4 2821 2825
772 trace("chunked: no hex digits in chunk size\n");
773 4 return -1;
774 }
775  
776 trace("chunked: parsed size=%"UVuf" at pos=%"Sz_uf"\n",
777 chunk_size, (Sz)(line_start - buf));
778  
779 2821 read_pos = (line_end - buf) + 2;
780  
781 2821 ▶ if (chunk_size == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (chunk_size == 0) { 52 2769 2821
782 // last chunk: the chunk_remaining == 0 branch scans the trailers
783 52 c->trailer_count = 0;
784 52 c->chunk_remaining = 0;
785 52 continue;
786 }
787  
788 // two checks: the subtraction wraps when chunk_size is the larger
789 2769 ▶ if (unlikely(chunk_size > (UV)c->cached_max_body_len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(chunk_size > (UV)c->cached_max_body_len)) { 0 2769 2769
790 trace("chunked: chunk too large %"UVuf"\n", chunk_size);
791 0 return -2;
792 }
793 2769 ▶ if (unlikely(write_pos > (STRLEN)(c->cached_max_body_len - chunk_size))) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(write_pos > (STRLEN)(c->cached_max_body_len - chunk_size))) { 13 2756 2769
794 trace("chunked: body too large %"UVuf" + %"Sz_uf"\n",
795 chunk_size, (Sz)write_pos);
796 13 return -2;
797 }
798  
799 2756 c->chunk_count++;
800 2756 ▶ if (unlikely(c->chunk_count > MAX_CHUNK_COUNT)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->chunk_count > MAX_CHUNK_COUNT)) { 0 2756 2756
801 trace("chunked: too many chunks (%u)\n", c->chunk_count);
802 0 return -1;
803 }
804  
805 2756 ▶ if (chunk_size > (UV)SSIZE_MAX) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (chunk_size > (UV)SSIZE_MAX) return -1; 0 2756 2756
806 2756 c->chunk_remaining = (ssize_t)chunk_size;
807 }
808 2861 ▶ else if (c->chunk_remaining == 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (c->chunk_remaining == 0) { 52 2809 2861
809 // after the last chunk: trailers
810 52 STRLEN remaining = buf_len - read_pos;
811 52 char *trailer_start = buf + read_pos;
812 52 STRLEN i = 0;
813 5794 ▶ while (i + 1 < remaining) {
Branch: 1 branch
BranchTrueFalseTotal
while (i + 1 < remaining) { 5794 0 5794
814 5794 ▶ if (trailer_start[i] == '\r' && trailer_start[i+1] == '\n') {
Branch: 2 branches
BranchTrueFalseTotal
if (trailer_start[i] == '\r' && trailer_start[i+1] == '\n') { 317 5477 5794
if (trailer_start[i] == '\r' && trailer_start[i+1] == '\n') { 317 0 317
815 317 ▶ if (i == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (i == 0) { 50 267 317
816 50 read_pos += 2;
817 50 ▶ STRLEN pipelined = (read_pos < buf_len) ? buf_len - read_pos : 0;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN pipelined = (read_pos < buf_len) ? buf_len - read_pos : 0; 2 48 50
818 50 c->expected_cl = write_pos;
819 50 c->received_cl = write_pos + pipelined;
820 50 ▶ if (pipelined > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (pipelined > 0) 2 48 50
821 2 memmove(buf + write_pos, buf + read_pos, pipelined);
822 50 SvCUR_set(c->rbuf, write_pos + pipelined);
823 50 return 0;
824 }
825 267 ▶ if (!memchr(trailer_start, ':', i)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!memchr(trailer_start, ':', i)) { 0 267 267
826 trace("chunked: trailer without a colon\n");
827 0 return -1;
828 }
829 267 ▶ if (unlikely(++c->trailer_count > MAX_TRAILER_HEADERS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(++c->trailer_count > MAX_TRAILER_HEADERS)) { 2 265 267
830 trace("chunked: too many trailer headers\n");
831 2 return -1;
832 }
833 265 read_pos += i + 2;
834 265 remaining = buf_len - read_pos;
835 265 trailer_start = buf + read_pos;
836 265 i = 0;
837 265 continue;
838 }
839 5477 i++;
840 }
841 // same rescan shape as the chunk-size line
842 0 ▶ if (unlikely(remaining > MAX_TRAILER_LINE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(remaining > MAX_TRAILER_LINE)) { 0 0 0
843 trace("chunked: trailer line too long (%"Sz_uf")\n", (Sz)remaining);
844 0 return -1;
845 }
846 0 goto need_more;
847 }
848 else {
849 // chunk_remaining > 0: chunk data
850 2809 STRLEN remaining = buf_len - read_pos;
851 2809 STRLEN to_copy = (STRLEN)c->chunk_remaining;
852 2809 ▶ if (to_copy > remaining)
Branch: 1 branch
BranchTrueFalseTotal
if (to_copy > remaining) 56 2753 2809
853 56 to_copy = remaining;
854  
855 2809 ▶ if (write_pos != read_pos && to_copy > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (write_pos != read_pos && to_copy > 0) { 2756 53 2809
if (write_pos != read_pos && to_copy > 0) { 2756 0 2756
856 2756 memmove(buf + write_pos, buf + read_pos, to_copy);
857 }
858 2809 write_pos += to_copy;
859 2809 read_pos += to_copy;
860 2809 c->chunk_remaining -= to_copy;
861 2809 c->received_cl = write_pos;
862  
863 2809 ▶ if (c->chunk_remaining > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->chunk_remaining > 0) { 56 2753 2809
864 56 goto need_more;
865 }
866  
867 2753 remaining = buf_len - read_pos;
868 2753 ▶ if (remaining < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (remaining < 2) { 2 2751 2753
869 2 c->chunk_remaining = CHUNK_STATE_NEED_CRLF;
870 2 goto need_more;
871 }
872 2751 ▶ if (buf[read_pos] != '\r' || buf[read_pos+1] != '\n') {
Branch: 2 branches
BranchTrueFalseTotal
if (buf[read_pos] != '\r' || buf[read_pos+1] != '\n') { 2749 2 2751
if (buf[read_pos] != '\r' || buf[read_pos+1] != '\n') { 0 2749 2749
873 trace("chunked: missing CRLF after chunk data\n");
874 2 return -1;
875 }
876 2749 read_pos += 2;
877 2749 c->chunk_remaining = CHUNK_STATE_PARSE_SIZE;
878 }
879 }
880  
881 2 need_more:
882 522 ▶ if (read_pos > write_pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (read_pos > write_pos) { 57 465 522
883 57 ▶ if (read_pos < buf_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (read_pos < buf_len) { 0 57 57
884 0 STRLEN unparsed = buf_len - read_pos;
885 0 memmove(buf + write_pos, buf + read_pos, unparsed);
886 0 SvCUR_set(c->rbuf, write_pos + unparsed);
887 } else {
888 57 SvCUR_set(c->rbuf, write_pos);
889 }
890 }
891 522 c->received_cl = write_pos;
892 522 return 1;
893 }
894  
895 static bool
896 1496 process_request_headers (struct feer_conn *c, int body_offset)
897 {
898 int err_code;
899 const char *err;
900 1496 struct feer_req *req = c->req;
901  
902 1496 stop_header_timer(c);
903  
904 trace("processing headers %d minor_version=%d\n",c->fd,req->minor_version);
905 1496 bool body_is_required = 0;
906 1496 bool next_req_follows = 0;
907 1496 bool got_content_length = 0;
908 1496 bool connection_close = 0;
909 PERL_UNUSED_VAR(body_is_required); /* only read under DEBUG >= 2 */
910  
911 1496 ▶ if (unlikely(req->minor_version > 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(req->minor_version > 1)) { 0 1496 1496
912 0 c->is_http11 = 1;
913 0 err_code = 505;
914 0 err = "HTTP Version Not Supported\n";
915 0 goto got_bad_request;
916 }
917  
918 1496 c->is_http11 = (req->minor_version == 1);
919 1496 ▶ c->is_keepalive = c->cached_keepalive_default && c->is_http11;
Branch: 2 branches
BranchTrueFalseTotal
c->is_keepalive = c->cached_keepalive_default && c->is_http11; 747 749 1496
c->is_keepalive = c->cached_keepalive_default && c->is_http11; 669 78 747
920 1496 c->expect_continue = 0;
921 1496 c->receive_chunked = 0;
922 1496 c->reqs++;
923  
924 1496 change_receiving_state(c, RECEIVE_BODY);
925 1496 c->expected_cl = 0;
926 1496 c->received_cl = 0;
927 1496 c->body_framed = 0;
928  
929 1496 switch (req->method_len) {
930 1200 case 3:
931 1200 ▶ if (likely(memcmp(req->method, "GET", 3) == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(memcmp(req->method, "GET", 3) == 0)) { 1199 1 1200
932 1199 next_req_follows = 1;
933 1 ▶ } else if (memcmp(req->method, "PUT", 3) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (memcmp(req->method, "PUT", 3) == 0) { 1 0 1
934 1 body_is_required = 1;
935 } else {
936 0 goto unsupported_method;
937 }
938 1200 break;
939 287 case 4:
940 287 ▶ if (likely(memcmp(req->method, "POST", 4) == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(memcmp(req->method, "POST", 4) == 0)) { 245 42 287
941 245 body_is_required = 1;
942 42 ▶ } else if (memcmp(req->method, "HEAD", 4) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (memcmp(req->method, "HEAD", 4) == 0) { 42 0 42
943 42 next_req_follows = 1;
944 } else {
945 0 goto unsupported_method;
946 }
947 287 break;
948 2 case 5:
949 2 ▶ if (memcmp(req->method, "PATCH", 5) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(req->method, "PATCH", 5) == 0) { 2 0 2
950 2 body_is_required = 1;
951 } else {
952 0 goto unsupported_method;
953 }
954 2 break;
955 3 case 6:
956 3 ▶ if (memcmp(req->method, "DELETE", 6) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(req->method, "DELETE", 6) == 0) { 2 1 3
957 2 next_req_follows = 1;
958 } else {
959 1 goto unsupported_method;
960 }
961 2 break;
962 3 case 7:
963 3 ▶ if (memcmp(req->method, "OPTIONS", 7) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(req->method, "OPTIONS", 7) == 0) { 3 0 3
964 3 next_req_follows = 1;
965 } else {
966 0 goto unsupported_method;
967 }
968 3 break;
969 default:
970 2 unsupported_method:
971 2 err = "Feersum doesn't support that method yet\n";
972 2 err_code = 501;
973 2 goto got_bad_request;
974 }
975  
976 1494 ▶ if (unlikely(req->uri_len > c->cached_max_uri_len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(req->uri_len > c->cached_max_uri_len)) { 8 1486 1494
977 8 err_code = 414;
978 8 err = "URI Too Long\n";
979 8 goto got_bad_request;
980 }
981  
982 #if DEBUG >= 2
983 if (next_req_follows)
984 trace2("next req follows fd=%d, boff=%d\n",c->fd,body_offset);
985 if (body_is_required)
986 trace2("body is required fd=%d, boff=%d\n",c->fd,body_offset);
987 #endif
988  
989 // req keeps the old rbuf (its header pointers point into it); the body
990 // moves to a fresh one
991 STRLEN from_len;
992 1486 char *from = SvPV(c->rbuf,from_len);
993 1486 ▶ if (unlikely(body_offset < 0 || (STRLEN)body_offset > from_len)) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(body_offset < 0 || (STRLEN)body_offset > from_len)) { 1486 0 1486
if (unlikely(body_offset < 0 || (STRLEN)body_offset > from_len)) { 0 1486 1486
994 0 trouble("invalid body_offset %d > from_len %"Sz_uf" fd=%d\n",
995 body_offset, (Sz)from_len, c->fd);
996 0 respond_with_server_error(c, "Internal parser error\n", 500);
997 0 return 0;
998 }
999 1486 from += body_offset;
1000 1486 STRLEN need = from_len - body_offset;
1001 trace("new rbuf for body %d need=%"Sz_uf"\n", c->fd, (Sz)need);
1002 1486 SV *new_rbuf = c->rbuf_spare;
1003 1486 ▶ if (new_rbuf) {
Branch: 1 branch
BranchTrueFalseTotal
if (new_rbuf) { 42 1444 1486
1004 42 c->rbuf_spare = NULL;
1005 42 ▶ sv_setpvn(new_rbuf, need > 0 ? from : "", need);
Branch: 1 branch
BranchTrueFalseTotal
sv_setpvn(new_rbuf, need > 0 ? from : "", need); 3 39 42
1006 } else
1007 1444 ▶ new_rbuf = newSVpvn(need > 0 ? from : "", need);
Branch: 1 branch
BranchTrueFalseTotal
new_rbuf = newSVpvn(need > 0 ? from : "", need); 365 1079 1444
1008  
1009 1486 req->buf = c->rbuf;
1010 1486 c->rbuf = new_rbuf;
1011 1486 SvCUR_set(req->buf, body_offset);
1012  
1013 1486 UV expected = 0;
1014 1486 bool got_host = 0;
1015 1486 bool got_transfer_encoding = 0;
1016 4298 ▶ for (size_t i = 0; i < req->num_headers; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < req->num_headers; i++) { 2873 1425 4298
1017 2873 struct phr_header *hdr = &req->headers[i];
1018 // picohttpparser reports obs-fold as a NULL name
1019 2873 ▶ if (unlikely(!hdr->name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!hdr->name)) { 1 2872 2873
1020 1 err_code = 400;
1021 1 err = "Obsolete header line folding not allowed\n";
1022 1 goto got_bad_request;
1023 }
1024 2872 ▶ if (unlikely(hdr->name_len > MAX_HEADER_NAME_LEN)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(hdr->name_len > MAX_HEADER_NAME_LEN)) { 2 2870 2872
1025 2 err_code = 431;
1026 2 err = "Header name too long\n";
1027 2 goto got_bad_request;
1028 }
1029 2870 ▶ if (unlikely(hdr->name_len == 14 &&
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(hdr->name_len == 14 && 150 2720 2870
if (unlikely(hdr->name_len == 14 && 150 0 150
1030 str_case_eq_fixed("content-length", hdr->name, 14)))
1031 132 {
1032 150 ▶ if (c->receive_chunked) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->receive_chunked) { 3 147 150
1033 3 err_code = 400;
1034 3 err = "Content-Length not allowed with Transfer-Encoding\n";
1035 18 goto got_bad_request;
1036 }
1037 147 UV new_expected = 0;
1038 147 ▶ if (likely(feer_parse_content_length(hdr->value, hdr->value_len,
Branch: 1 branch
BranchTrueFalseTotal
if (likely(feer_parse_content_length(hdr->value, hdr->value_len, 138 9 147
1039 &new_expected))) {
1040 /* expected_cl is ssize_t: past SSIZE_MAX the cast goes negative */
1041 138 ▶ if (unlikely(new_expected > (UV)c->cached_max_body_len
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(new_expected > (UV)c->cached_max_body_len 134 4 138
if (unlikely(new_expected > (UV)c->cached_max_body_len 0 134 134
1042 || new_expected > (UV)SSIZE_MAX)) {
1043 4 err_code = 413;
1044 4 err = "Content length exceeds maximum\n";
1045 4 goto got_bad_request;
1046 }
1047 134 ▶ if (got_content_length && new_expected != expected) {
Branch: 2 branches
BranchTrueFalseTotal
if (got_content_length && new_expected != expected) { 4 130 134
if (got_content_length && new_expected != expected) { 2 2 4
1048 2 err_code = 400;
1049 2 err = "Multiple conflicting Content-Length headers\n";
1050 2 goto got_bad_request;
1051 }
1052 132 expected = new_expected;
1053 132 got_content_length = 1;
1054 }
1055 else {
1056 9 err_code = 400;
1057 9 err = "Invalid Content-Length\n";
1058 9 goto got_bad_request;
1059 }
1060 }
1061 2720 ▶ else if (unlikely(hdr->name_len == 10 &&
Branch: 2 branches
BranchTrueFalseTotal
else if (unlikely(hdr->name_len == 10 && 1093 1627 2720
else if (unlikely(hdr->name_len == 10 && 932 161 1093
1062 str_case_eq_fixed("connection", hdr->name, 10)))
1063 {
1064 932 ▶ if (feer_header_has_token(hdr->value, hdr->value_len, "close", 5))
Branch: 1 branch
BranchTrueFalseTotal
if (feer_header_has_token(hdr->value, hdr->value_len, "close", 5)) 911 21 932
1065 {
1066 911 connection_close = 1;
1067 911 c->is_keepalive = 0;
1068 trace("setting conn %d to close after response\n", c->fd);
1069 }
1070 22 ▶ else if (!c->is_http11 && c->cached_keepalive_default && !connection_close &&
Branch: 2 branches
BranchTrueFalseTotal
else if (!c->is_http11 && c->cached_keepalive_default && !connection_close && 1 20 21
else if (!c->is_http11 && c->cached_keepalive_default && !connection_close && 1 0 1
1071 1 feer_header_has_token(hdr->value, hdr->value_len, "keep-alive", 10))
1072 {
1073 1 c->is_keepalive = 1;
1074 trace("setting conn %d to keep after response\n", c->fd);
1075 }
1076 932 ▶ if (next_req_follows && c->receive_chunked && (!c->is_http11 || got_host)) break;
Branch: 4 branches
BranchTrueFalseTotal
if (next_req_follows && c->receive_chunked && (!c->is_http11 || got_host)) break; 794 138 932
if (next_req_follows && c->receive_chunked && (!c->is_http11 || got_host)) break; 4 790 794
if (next_req_follows && c->receive_chunked && (!c->is_http11 || got_host)) break; 4 0 4
if (next_req_follows && c->receive_chunked && (!c->is_http11 || got_host)) break; 0 4 4
1077 }
1078 1788 ▶ else if (unlikely(c->is_http11 && hdr->name_len == 6 &&
Branch: 4 branches
BranchTrueFalseTotal
else if (unlikely(c->is_http11 && hdr->name_len == 6 && 1759 29 1788
else if (unlikely(c->is_http11 && hdr->name_len == 6 && 41 1718 1759
else if (unlikely(c->is_http11 && hdr->name_len == 6 && 41 1747 1788
else if (unlikely(c->is_http11 && hdr->name_len == 6 && 22 19 41
1079 str_case_eq_fixed("expect", hdr->name, 6)))
1080 {
1081 35 if (hdr->value_len == 12 &&
1082 13 str_case_eq_fixed("100-continue", hdr->value, 12))
1083 {
1084 13 c->expect_continue = 1;
1085 trace("got Expect: 100-continue on fd=%d\n", c->fd);
1086 }
1087 else {
1088 9 err_code = 417;
1089 9 err = "Expectation Failed\n";
1090 9 goto got_bad_request;
1091 }
1092 }
1093 1766 ▶ else if (unlikely(hdr->name_len == 17 &&
Branch: 2 branches
BranchTrueFalseTotal
else if (unlikely(hdr->name_len == 17 && 136 1630 1766
else if (unlikely(hdr->name_len == 17 && 124 12 136
1094 str_case_eq_fixed("transfer-encoding", hdr->name, 17)))
1095 99 {
1096 // TE is undefined for HTTP/1.0 (RFC 9112 6.1); rejecting keeps
1097 // receive_chunked unreachable there
1098 124 ▶ if (unlikely(!c->is_http11)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!c->is_http11)) { 5 119 124
1099 5 err_code = 400;
1100 5 err = "Transfer-Encoding not allowed in HTTP/1.0\n";
1101 5 goto got_bad_request;
1102 }
1103  
1104 119 ▶ if (got_transfer_encoding) {
Branch: 1 branch
BranchTrueFalseTotal
if (got_transfer_encoding) { 6 113 119
1105 6 err_code = 400;
1106 6 err = "Multiple Transfer-Encoding headers not allowed\n";
1107 6 goto got_bad_request;
1108 }
1109 113 got_transfer_encoding = 1;
1110  
1111 113 ▶ if (unlikely(hdr->value_len == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(hdr->value_len == 0)) { 0 113 113
1112 0 err_code = 400;
1113 0 err = "Empty Transfer-Encoding\n";
1114 0 goto got_bad_request;
1115 }
1116  
1117 /* transfer-coding = token *( OWS ";" OWS parameter ) (RFC 9112 6.1) */
1118 113 bool is_chunked = 0;
1119 224 if (hdr->value_len >= 7 &&
1120 111 str_case_eq_fixed("chunked", hdr->value, 7))
1121 {
1122 103 STRLEN j = 7;
1123 104 ▶ while (j < hdr->value_len &&
Branch: 1 branch
BranchTrueFalseTotal
while (j < hdr->value_len && 4 100 104
1124 4 ▶ (hdr->value[j] == ' ' || hdr->value[j] == '\t'))
Branch: 2 branches
BranchTrueFalseTotal
(hdr->value[j] == ' ' || hdr->value[j] == '\t')) 1 3 4
(hdr->value[j] == ' ' || hdr->value[j] == '\t')) 0 3 3
1125 1 j++;
1126 103 ▶ is_chunked = (j == hdr->value_len || hdr->value[j] == ';');
Branch: 2 branches
BranchTrueFalseTotal
is_chunked = (j == hdr->value_len || hdr->value[j] == ';'); 3 100 103
is_chunked = (j == hdr->value_len || hdr->value[j] == ';'); 3 0 3
1127 }
1128  
1129 118 bool is_identity = (hdr->value_len == 8 &&
1130 5 str_case_eq_fixed("identity", hdr->value, 8));
1131  
1132 113 ▶ if (is_chunked) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_chunked) { 103 10 113
1133 103 ▶ if (got_content_length) {
Branch: 1 branch
BranchTrueFalseTotal
if (got_content_length) { 8 95 103
1134 8 err_code = 400;
1135 8 err = "Content-Length not allowed with Transfer-Encoding\n";
1136 8 goto got_bad_request;
1137 }
1138 95 c->receive_chunked = 1;
1139 trace("got Transfer-Encoding: chunked on fd=%d\n", c->fd);
1140 }
1141 10 ▶ else if (is_identity) {
Branch: 1 branch
BranchTrueFalseTotal
else if (is_identity) { 6 4 10
1142 trace("got Transfer-Encoding: identity on fd=%d (ignored)\n", c->fd);
1143 }
1144 else {
1145 6 err_code = 501;
1146 6 err = "Unsupported Transfer-Encoding\n";
1147 6 goto got_bad_request;
1148 }
1149 }
1150 1642 ▶ else if (unlikely(hdr->name_len == 4 &&
Branch: 2 branches
BranchTrueFalseTotal
else if (unlikely(hdr->name_len == 4 && 1288 354 1642
else if (unlikely(hdr->name_len == 4 && 1288 0 1288
1151 str_case_eq_fixed("host", hdr->name, 4)))
1152 {
1153 1288 ▶ if (unlikely(got_host)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(got_host)) { 2 1286 1288
1154 2 err_code = 400;
1155 2 err = "Duplicate Host header\n";
1156 2 goto got_bad_request;
1157 }
1158 1286 ▶ if (unlikely(hdr->value_len == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(hdr->value_len == 0)) { 0 1286 1286
1159 0 err_code = 400;
1160 0 err = "Empty Host header\n";
1161 0 goto got_bad_request;
1162 }
1163 1286 got_host = 1;
1164 }
1165 }
1166  
1167 1429 ▶ if (unlikely(c->is_http11 && !got_host)) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(c->is_http11 && !got_host)) { 1220 209 1429
if (unlikely(c->is_http11 && !got_host)) { 1 1219 1220
1168 1 err_code = 400;
1169 1 err = "Host header required for HTTP/1.1\n";
1170 1 goto got_bad_request;
1171 }
1172  
1173 1428 ▶ if (c->cached_max_conn_reqs > 0 && c->reqs >= c->cached_max_conn_reqs) {
Branch: 2 branches
BranchTrueFalseTotal
if (c->cached_max_conn_reqs > 0 && c->reqs >= c->cached_max_conn_reqs) { 25 1403 1428
if (c->cached_max_conn_reqs > 0 && c->reqs >= c->cached_max_conn_reqs) { 1 24 25
1174 1 c->is_keepalive = 0;
1175 trace("reached max requests per connection (%d), will close after response\n", c->cached_max_conn_reqs);
1176 }
1177  
1178 // proxies disagree on TE for body-less methods: reject to keep framing unambiguous
1179 1428 ▶ if (unlikely(c->receive_chunked && next_req_follows)) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(c->receive_chunked && next_req_follows)) { 87 1341 1428
if (unlikely(c->receive_chunked && next_req_follows)) { 4 83 87
1180 4 err_code = 400;
1181 4 err = "Transfer-Encoding not allowed on this method\n";
1182 4 goto got_bad_request;
1183 }
1184  
1185 /* implicit empty body takes the CL path: rbuf may hold the next request */
1186 1424 ▶ if (likely(next_req_follows) && !got_content_length && !c->receive_chunked) goto got_cl;
Branch: 3 branches
BranchTrueFalseTotal
if (likely(next_req_follows) && !got_content_length && !c->receive_chunked) goto got_cl; 1226 198 1424
if (likely(next_req_follows) && !got_content_length && !c->receive_chunked) goto got_cl; 1226 0 1226
if (likely(next_req_follows) && !got_content_length && !c->receive_chunked) goto got_cl; 1226 0 1226
1187 198 ▶ else if (likely(got_content_length)) goto got_cl;
Branch: 1 branch
BranchTrueFalseTotal
else if (likely(got_content_length)) goto got_cl; 112 86 198
1188 86 ▶ else if (unlikely(c->receive_chunked)) goto got_chunked;
Branch: 1 branch
BranchTrueFalseTotal
else if (unlikely(c->receive_chunked)) goto got_chunked; 83 3 86
1189  
1190 3 err_code = 411;
1191 3 err = "Content-Length or Transfer-Encoding required\n";
1192  
1193 90 got_bad_request:
1194 90 respond_with_server_error(c, err, err_code);
1195 90 return 0;
1196  
1197 1338 got_cl:
1198 1338 c->expected_cl = (ssize_t)expected;
1199 1338 c->received_cl = SvCUR(c->rbuf);
1200 1338 c->body_framed = 1;
1201 trace("expecting body %d size=%"Ssz_df" have=%"Ssz_df"\n",
1202 c->fd, (Ssz)c->expected_cl, (Ssz)c->received_cl);
1203 /* never pre-grow to the peer's declared length (a Perl allocation failure
1204 * is a hard _exit); try_conn_read grows the rest as bytes arrive */
1205 {
1206 1338 STRLEN want = (STRLEN)c->expected_cl + 1;
1207 1338 ▶ if (want > BODY_PREGROW_MAX) want = BODY_PREGROW_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (want > BODY_PREGROW_MAX) want = BODY_PREGROW_MAX; 1 1337 1338
1208 1338 ▶ SvGROW(c->rbuf, want);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(c->rbuf, want); 1338 0 1338
SvGROW(c->rbuf, want); 18 1320 1338
1209 }
1210  
1211 1338 ▶ if (unlikely(c->expected_cl && c->received_cl < c->expected_cl)) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(c->expected_cl && c->received_cl < c->expected_cl)) { 92 1246 1338
if (unlikely(c->expected_cl && c->received_cl < c->expected_cl)) { 28 64 92
1212 28 send_100_continue(c);
1213 28 return 1;
1214 }
1215 1310 goto got_it_all;
1216  
1217 83 got_chunked:
1218 83 c->chunk_remaining = CHUNK_STATE_PARSE_SIZE;
1219 83 c->chunk_count = 0;
1220 83 c->trailer_count = 0;
1221 83 c->expected_cl = 0;
1222 83 c->received_cl = 0;
1223 83 c->body_framed = 1;
1224 83 change_receiving_state(c, RECEIVE_CHUNKED);
1225 trace("starting chunked receive on fd=%d, have=%"Sz_uf" bytes\n",
1226 c->fd, (Sz)SvCUR(c->rbuf));
1227  
1228 83 send_100_continue(c);
1229  
1230 {
1231 83 int ret = try_parse_chunked(c);
1232 83 ▶ if (ret == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (ret == 1) 22 61 83
1233 22 return 1;
1234 61 ▶ if (ret == -2) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == -2) { 0 61 61
1235 // headers and an oversize chunk in one segment land here, not in
1236 // try_conn_read; falling through would dispatch a truncated body
1237 0 err_code = 413;
1238 0 err = "Request body too large\n";
1239 0 goto got_bad_request;
1240 }
1241 61 ▶ if (ret == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == -1) { 15 46 61
1242 15 err_code = 400;
1243 15 err = "Malformed chunked encoding\n";
1244 15 goto got_bad_request;
1245 }
1246 }
1247  
1248 46 got_it_all:
1249 1356 sched_request_callback(c);
1250 1356 return 0;
1251 }
1252  
1253 static void
1254 2118 conn_write_now (struct feer_conn *c)
1255 {
1256 #ifdef FEERSUM_HAS_H2
1257 if (c->is_h2_stream) {
1258 h2_try_stream_write(aTHX_ c);
1259 return;
1260 }
1261 #endif
1262  
1263 #ifdef FEERSUM_HAS_TLS
1264 2118 ▶ if (c->tls) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls) { 195 1923 2118
1265 195 try_tls_conn_write(feersum_ev_loop, &c->write_ev_io, EV_WRITE);
1266 195 return;
1267 }
1268 #endif
1269  
1270 1923 try_conn_write(feersum_ev_loop, &c->write_ev_io, EV_WRITE);
1271 }
1272  
1273 /* outside a Feersum callback, a loop turn's writes share one check_cb flush */
1274 static void
1275 45247 conn_write_ready (struct feer_conn *c)
1276 {
1277 #ifdef FEERSUM_HAS_H2
1278 if (c->is_h2_stream) {
1279 h2_try_stream_write(aTHX_ c);
1280 return;
1281 }
1282 #endif
1283  
1284 45247 ▶ if (c->in_callback || unlikely(!ev_is_active(&c->server->ec))) {
Branch: 2 branches
BranchTrueFalseTotal
if (c->in_callback || unlikely(!ev_is_active(&c->server->ec))) { 922 44325 45247
if (c->in_callback || unlikely(!ev_is_active(&c->server->ec))) { 0 922 922
1285 44325 start_write_watcher(c);
1286 44325 return;
1287 }
1288  
1289 922 ▶ if (c->write_flush_queued) return;
Branch: 1 branch
BranchTrueFalseTotal
if (c->write_flush_queued) return; 21 901 922
1290 901 c->write_flush_queued = 1;
1291 901 rinq_push(&c->server->write_ready_rinq, c);
1292 901 SvREFCNT_inc_void_NN(c->self);
1293 901 ▶ if (!ev_is_active(&c->server->ei))
Branch: 1 branch
BranchTrueFalseTotal
if (!ev_is_active(&c->server->ei)) 616 285 901
1294 616 ev_idle_start(feersum_ev_loop, &c->server->ei);
1295 }
1296  
1297 /* idle keepalive and lingering conns are done: a stale flush must not redo */
1298 static int
1299 900 conn_has_write_work (struct feer_conn *c)
1300 {
1301 900 ▶ return c->fd >= 0 && !c->closing_linger
Branch: 1 branch
BranchTrueFalseTotal
return c->fd >= 0 && !c->closing_linger 899 1 900
1302 1800 ▶ && !(c->responding == RESPOND_NOT_STARTED && !c->wbuf_rinq
Branch: 3 branches
BranchTrueFalseTotal
&& !(c->responding == RESPOND_NOT_STARTED && !c->wbuf_rinq 900 0 900
&& !(c->responding == RESPOND_NOT_STARTED && !c->wbuf_rinq 0 899 899
&& !(c->responding == RESPOND_NOT_STARTED && !c->wbuf_rinq 0 0 0
1303 0 ▶ && c->sendfile_fd < 0);
Branch: 1 branch
BranchTrueFalseTotal
&& c->sendfile_fd < 0); 0 0 0
1304 }
1305  
1306 static void
1307 896 feersum_drain_write_ready (struct feer_server *server)
1308 {
1309 1795 ▶ while (server->write_ready_rinq) {
Branch: 1 branch
BranchTrueFalseTotal
while (server->write_ready_rinq) { 899 896 1795
1310 struct feer_conn *c =
1311 899 (struct feer_conn *)rinq_shift(&server->write_ready_rinq);
1312 899 ▶ if (unlikely(!c)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!c)) break; 0 899 899
1313 899 c->write_flush_queued = 0;
1314 899 ▶ if (likely(conn_has_write_work(c)))
Branch: 1 branch
BranchTrueFalseTotal
if (likely(conn_has_write_work(c))) 898 1 899
1315 898 conn_write_now(c);
1316 899 SvREFCNT_dec(c->self);
1317 }
1318 896 }
1319  
1320 static void
1321 26 feersum_write_ready_to_watchers (struct feer_server *server)
1322 {
1323 27 ▶ while (server->write_ready_rinq) {
Branch: 1 branch
BranchTrueFalseTotal
while (server->write_ready_rinq) { 1 26 27
1324 struct feer_conn *c =
1325 1 (struct feer_conn *)rinq_shift(&server->write_ready_rinq);
1326 1 ▶ if (unlikely(!c)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!c)) break; 0 1 1
1327 1 c->write_flush_queued = 0;
1328 1 ▶ if (conn_has_write_work(c))
Branch: 1 branch
BranchTrueFalseTotal
if (conn_has_write_work(c)) 1 0 1
1329 1 start_write_watcher(c);
1330 1 SvREFCNT_dec(c->self);
1331 }
1332 26 }
1333  
1334 /* H2 dispatch shims for XS CODE/PPCODE blocks (which cannot use #ifdef). */
1335 static int
1336 42814 h2_try_write_chunk (pTHX_ struct feer_conn *c, SV *body)
1337 {
1338 #ifdef FEERSUM_HAS_H2
1339 if (unlikely(c->is_h2_stream)) {
1340 feersum_h2_write_chunk(aTHX_ c, body);
1341 return 1;
1342 }
1343 #endif
1344 PERL_UNUSED_VAR(c); PERL_UNUSED_VAR(body);
1345 42814 return 0;
1346 }
1347  
1348 static int
1349 73 h2_is_stream (struct feer_conn *c)
1350 {
1351 #ifdef FEERSUM_HAS_H2
1352 if (unlikely(c->is_h2_stream))
1353 return 1;
1354 #endif
1355 PERL_UNUSED_VAR(c);
1356 73 return 0;
1357 }
1358  
1359 INLINE_UNLESS_DEBUG static void
1360 111 send_100_continue (struct feer_conn *c)
1361 {
1362 111 ▶ if (likely(!c->expect_continue))
Branch: 1 branch
BranchTrueFalseTotal
if (likely(!c->expect_continue)) 98 13 111
1363 98 return;
1364  
1365 static const char continue_response[] = "HTTP/1.1 100 Continue" CRLF CRLF;
1366 #ifdef FEERSUM_HAS_TLS
1367 13 ▶ if (c->tls) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls) { 2 11 13
1368 2 feer_tls_send(c, continue_response, sizeof(continue_response) - 1);
1369 2 feer_tls_flush_wbuf(c);
1370 2 ▶ if (c->tls_wbuf.off > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_wbuf.off > 0) 0 2 2
1371 0 start_write_watcher(c);
1372 2 c->expect_continue = 0;
1373 2 return;
1374 }
1375 #endif
1376 11 size_t cont_len = sizeof(continue_response) - 1;
1377 11 ssize_t wr = write(c->fd, continue_response, cont_len);
1378 // a wholly failed write is fine: the client sends the body anyway after its wait
1379 11 ▶ if (likely(wr == (ssize_t)cont_len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(wr == (ssize_t)cont_len)) { 0 11 11
1380 trace("sent 100 Continue to fd=%d\n", c->fd);
1381 }
1382 0 ▶ else if (wr > 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (wr > 0) { 0 0 0
1383 // queue the tail; a truncated status line reads as the start of the response
1384 0 add_const_to_wbuf(c, continue_response + wr, cont_len - (size_t)wr);
1385 0 start_write_watcher(c);
1386 }
1387 0 ▶ else if (wr < 0 && errno != EAGAIN && errno != EINTR) {
Branch: 2 branches
BranchTrueFalseTotal
else if (wr < 0 && errno != EAGAIN && errno != EINTR) { 0 0 0
else if (wr < 0 && errno != EAGAIN && errno != EINTR) { 0 0 0
1388 trace("100 Continue write error fd=%d: %s\n", c->fd, strerror(errno));
1389 }
1390 11 c->expect_continue = 0;
1391 }
1392  
1393 INLINE_UNLESS_DEBUG static void
1394 1413 free_feer_req (struct feer_req *req)
1395 {
1396 1413 ▶ if (unlikely(!req))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!req)) 175 1238 1413
1397 175 return;
1398  
1399 1238 SvREFCNT_dec(req->buf);
1400 1238 SvREFCNT_dec(req->path);
1401 1238 SvREFCNT_dec(req->query);
1402 #ifdef FEERSUM_HAS_H2
1403 SvREFCNT_dec(req->h2_method_sv);
1404 SvREFCNT_dec(req->h2_uri_sv);
1405 #endif
1406 1238 ▶ FEER_REQ_FREE(req);
Branch: 1 branch
BranchTrueFalseTotal
FEER_REQ_FREE(req); 1238 0 1238
1407 }
1408  
1409 INLINE_UNLESS_DEBUG static void
1410 1413 free_request (struct feer_conn *c)
1411 {
1412 1413 free_feer_req(c->req);
1413 1413 c->req = NULL;
1414 1413 }
1415  
1416 static void
1417 397 respond_with_server_error (struct feer_conn *c, const char *msg, int err_code)
1418 {
1419 SV *tmp;
1420 STRLEN msg_len;
1421  
1422 #ifdef FEERSUM_HAS_H2
1423 if (unlikely(c->is_h2_stream)) {
1424 feersum_h2_respond_error(c, err_code, msg);
1425 return;
1426 }
1427 #endif
1428  
1429 397 ▶ if (unlikely(c->responding != RESPOND_NOT_STARTED)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->responding != RESPOND_NOT_STARTED)) { 0 397 397
1430 0 trouble("Tried to send server error but already responding!\n");
1431 0 return;
1432 }
1433  
1434 397 msg_len = strlen(msg);
1435 assert(msg_len < INT_MAX);
1436  
1437 // HEAD keeps the Content-Length but no content; c->req is absent before
1438 // the request line parses
1439 290 ▶ const bool no_body = c->req && c->req->method_len == 4
Branch: 1 branch
BranchTrueFalseTotal
const bool no_body = c->req && c->req->method_len == 4 91 199 290
1440 687 ▶ && memcmp(c->req->method, "HEAD", 4) == 0;
Branch: 2 branches
BranchTrueFalseTotal
&& memcmp(c->req->method, "HEAD", 4) == 0; 290 107 397
&& memcmp(c->req->method, "HEAD", 4) == 0; 6 85 91
1441  
1442 /* DATE_BUF already ends in CRLF */
1443 397 ▶ tmp = newSVpvf("HTTP/1.%d %d %s" CRLF
Branch: 1 branch
BranchTrueFalseTotal
tmp = newSVpvf("HTTP/1.%d %d %s" CRLF 6 391 397
1444 "Content-Type: text/plain" CRLF
1445 "Connection: close" CRLF
1446 "Cache-Control: no-cache, no-store" CRLF
1447 "%s"
1448 "Content-Length: %"Ssz_df"" CRLFx2
1449 "%.*s",
1450 c->is_http11 ? 1 : 0,
1451 err_code, http_code_to_msg(err_code),
1452 DATE_BUF,
1453 (Ssz)msg_len,
1454 no_body ? 0 : (int)msg_len, msg);
1455 /* not a mortal: ev callbacks have no FREETMPS scope, so it would leak */
1456 397 add_sv_to_wbuf(c, tmp);
1457 397 SvREFCNT_dec(tmp);
1458  
1459 397 stop_read_watcher(c);
1460 397 stop_read_timer(c);
1461 397 stop_header_timer(c);
1462 397 change_responding_state(c, RESPOND_SHUTDOWN);
1463 397 change_receiving_state(c, RECEIVE_SHUTDOWN);
1464 397 c->is_keepalive = 0;
1465 397 conn_write_ready(c);
1466 }
1467  
1468  
« feersum_utils.c.inc ↑ Summary Feersum.xs »