feersum_h1.c.inc

filter: Statementstmt 84.6% 597 / 705 Branchbran 72.3% 363 / 502 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 79.5% 960 / 1207 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« feersum_utils.c.inc ↑ Summary Feersum.xs »
1 static int
2 1537 try_parse_http(struct feer_conn *c, size_t last_read)
3 {
4 1537 struct feer_req *req = c->req;
5 1537 ▶ if (likely(!req)) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(!req)) { 1513 24 1537
6 1513 ▶ FEER_REQ_ALLOC(req);
Branch: 1 branch
BranchTrueFalseTotal
FEER_REQ_ALLOC(req); 1036 477 1513
7 1513 c->req = req;
8 }
9  
10 // phr_parse_request resets num_headers on each call; must restore to max
11 1537 req->num_headers = MAX_HEADERS;
12  
13 3074 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 1537 req->headers, &req->num_headers,
17 1537 (SvCUR(c->rbuf)-last_read));
18 }
19  
20 INLINE_UNLESS_DEBUG static void
21 1600 finish_receiving(struct feer_conn *c) {
22 /* error paths arm a lingering close first; it owns the read watcher/timer */
23 1600 ▶ if (unlikely(c->closing_linger))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->closing_linger)) 224 1376 1600
24 224 return;
25 1376 change_receiving_state(c, RECEIVE_SHUTDOWN);
26 1376 stop_read_watcher(c);
27 1376 stop_read_timer(c);
28 1376 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 16 feer_request_major_too_new (SV *rbuf)
34 {
35 dTHX;
36 STRLEN len;
37 16 const char *buf = SvPV(rbuf, len);
38 16 STRLEN scan = len > 16384 ? 16384 : len;
39 16 const char *lf = memchr(buf, '\n', scan);
40 const char *p;
41 16 ▶ if (!lf || lf - buf < 8)
Branch: 2 branches
BranchTrueFalseTotal
if (!lf || lf - buf < 8) 16 0 16
if (!lf || lf - buf < 8) 6 10 16
42 6 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 1884 try_conn_read(EV_P_ ev_io *w, int revents)
55 {
56 1884 dCONN;
57 1884 SvREFCNT_inc_void_NN(c->self);
58 1884 feer_conn_set_busy(c);
59 1884 ssize_t got_n = 0;
60  
61 1884 ▶ if (unlikely(c->pipelined)) goto pipelined;
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->pipelined)) goto pipelined; 151 1733 1884
62  
63 1733 ▶ if (unlikely(revents & EV_ERROR && !(revents & EV_READ))) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(revents & EV_ERROR && !(revents & EV_READ))) { 0 1733 1733
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 1733 ▶ if (unlikely(c->receiving == RECEIVE_SHUTDOWN))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->receiving == RECEIVE_SHUTDOWN)) 0 1733 1733
69 0 goto dont_read_again;
70  
71 trace("try read %d\n",w->fd);
72  
73 1733 ▶ if (unlikely(!c->rbuf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!c->rbuf)) { 1283 450 1733
74 trace("init rbuf for %d\n",w->fd);
75 1283 c->rbuf = newSV(READ_BUFSZ + 1);
76 1283 SvPOK_on(c->rbuf);
77 }
78  
79 1733 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 1743 ▶ 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 1647 1733
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 1723 ▶ else if (unlikely(space_free < READ_BUFSZ)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (unlikely(space_free < READ_BUFSZ)) { 76 1647 1723
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 1733 char *cur = SvPVX(c->rbuf) + SvCUR(c->rbuf);
112 1733 got_n = read(w->fd, cur, space_free);
113  
114 1733 ▶ if (unlikely(got_n <= 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(got_n <= 0)) { 35 1698 1733
115 35 ▶ if (unlikely(got_n == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(got_n == 0)) { 31 4 35
116 trace("EOF before complete request: fd=%d buf=%"Sz_uf"\n", w->fd, (Sz)SvCUR(c->rbuf));
117 31 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 1698 SvCUR(c->rbuf) += got_n;
130 1698 begin_request_headers(c);
131 1698 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 1849 try_parse:
141 1899 ▶ if (unlikely(c->receiving == RECEIVE_PROXY_HEADER)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->receiving == RECEIVE_PROXY_HEADER)) { 153 1746 1899
142 153 int ret = try_parse_proxy_header(c);
143 153 ▶ if (ret == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == -1) { 100 53 153
144 100 respond_with_server_error(c, "Invalid PROXY protocol header\n", 400);
145 100 goto dont_read_again;
146 }
147 53 ▶ if (ret == -2) goto try_read_again_reset_timer;
Branch: 1 branch
BranchTrueFalseTotal
if (ret == -2) goto try_read_again_reset_timer; 3 50 53
148  
149 50 STRLEN remaining = SvCUR(c->rbuf) - ret;
150 50 ▶ if (remaining > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (remaining > 0) 50 0 50
151 50 memmove(SvPVX(c->rbuf), SvPVX(c->rbuf) + ret, remaining);
152 50 SvCUR_set(c->rbuf, remaining);
153  
154 50 ▶ feer_clear_remote_cache(c);
Branch: 2 branches
BranchTrueFalseTotal
feer_clear_remote_cache(c); 0 50 50
feer_clear_remote_cache(c); 0 50 50
155  
156 50 change_receiving_state(c, RECEIVE_HEADERS);
157 50 ▶ if (remaining > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (remaining > 0) { 50 0 50
158 50 got_n = remaining;
159 50 goto try_parse;
160 }
161 0 goto try_read_again_reset_timer;
162 }
163  
164 1746 ▶ if (likely(c->receiving <= RECEIVE_HEADERS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(c->receiving <= RECEIVE_HEADERS)) { 1366 380 1746
165 1366 int ret = try_parse_http(c, (size_t)got_n);
166 1366 ▶ if (ret == -1) goto try_read_bad;
Branch: 1 branch
BranchTrueFalseTotal
if (ret == -1) goto try_read_bad; 18 1348 1366
167 1348 ▶ if (ret == -2) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == -2) { 30 1318 1348
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 1318 ▶ if (unlikely((STRLEN)ret > c->cached_max_read_buf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely((STRLEN)ret > c->cached_max_read_buf)) { 0 1318 1318
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 1318 ▶ if (process_request_headers(c, ret))
Branch: 1 branch
BranchTrueFalseTotal
if (process_request_headers(c, ret)) 48 1270 1318
192 48 goto try_read_again_reset_timer;
193 else
194 1270 goto dont_read_again;
195 }
196 380 ▶ else if (likely(c->receiving == RECEIVE_BODY)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (likely(c->receiving == RECEIVE_BODY)) { 25 355 380
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 355 ▶ else if (c->receiving == RECEIVE_CHUNKED) {
Branch: 1 branch
BranchTrueFalseTotal
else if (c->receiving == RECEIVE_CHUNKED) { 355 0 355
204 355 int ret = try_parse_chunked(c);
205 355 ▶ if (ret == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == 1) { 336 19 355
206 336 ▶ 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 335 336
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 335 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 33 try_read_error:
243 trace("READ ERROR %d, refcnt=%d\n", w->fd, SvREFCNT(c->self));
244 33 change_receiving_state(c, RECEIVE_SHUTDOWN);
245 33 change_responding_state(c, RESPOND_SHUTDOWN);
246 33 stop_all_watchers(c);
247 33 goto try_read_cleanup;
248  
249 18 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 18 ▶ if (unlikely(c->req && c->req->num_headers >= MAX_HEADERS))
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(c->req && c->req->num_headers >= MAX_HEADERS)) 18 0 18
if (unlikely(c->req && c->req->num_headers >= MAX_HEADERS)) 2 16 18
254 2 respond_with_server_error(c, "Too many header fields\n", 431);
255 16 ▶ 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))) { 16 0 16
else if (unlikely(c->rbuf && feer_request_major_too_new(c->rbuf))) { 0 16 16
256 0 c->is_http11 = 1;
257 0 respond_with_server_error(c, "HTTP Version Not Supported\n", 505);
258 }
259 else
260 16 respond_with_server_error(c, "Malformed request\n", 400);
261 1433 dont_read_again:
262 trace("done reading %d\n", w->fd);
263 1433 finish_receiving(c);
264 1433 goto try_read_cleanup;
265  
266 416 try_read_again_reset_timer:
267 trace("(reset read timer) %d\n", w->fd);
268 416 restart_read_timer(c);
269 418 try_read_again:
270 trace("read again %d\n", w->fd);
271 418 start_read_watcher(c);
272  
273 1884 try_read_cleanup:
274 1884 SvREFCNT_dec(c->self);
275 1884 }
276  
277 static void
278 19 conn_read_timeout (EV_P_ ev_timer *w, int revents)
279 {
280 19 dCONN;
281 19 SvREFCNT_inc_void_NN(c->self);
282  
283 19 ▶ if (unlikely(!(revents & EV_TIMER) || c->receiving == RECEIVE_SHUTDOWN)) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(!(revents & EV_TIMER) || c->receiving == RECEIVE_SHUTDOWN)) { 19 0 19
if (unlikely(!(revents & EV_TIMER) || c->receiving == RECEIVE_SHUTDOWN)) { 0 19 19
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 19 ▶ if (unlikely(c->io_taken))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->io_taken)) 0 19 19
352 0 goto read_timeout_cleanup;
353  
354 28 ▶ 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) { 19 0 19
if (likely(c->responding == RESPOND_NOT_STARTED) && c->receiving >= RECEIVE_HEADERS) { 13 6 19
355 #ifdef FEERSUM_HAS_TLS
356 13 ▶ if (c->tls && !c->tls_handshake_done) {
Branch: 2 branches
BranchTrueFalseTotal
if (c->tls && !c->tls_handshake_done) { 4 9 13
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 9 ▶ if (c->receiving == RECEIVE_PROXY_HEADER) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->receiving == RECEIVE_PROXY_HEADER) { 3 6 9
363 3 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 9 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 19 read_timeout_cleanup:
385 19 SvREFCNT_dec(c->self);
386 19 }
387  
388 // non-resetting header deadline (Slowloris)
389 static void
390 11 conn_header_timeout (EV_P_ ev_timer *w, int revents)
391 {
392 11 dCONN;
393 11 SvREFCNT_inc_void_NN(c->self);
394  
395 11 ▶ if (unlikely(!(revents & EV_TIMER) || c->receiving == RECEIVE_SHUTDOWN)) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(!(revents & EV_TIMER) || c->receiving == RECEIVE_SHUTDOWN)) { 11 0 11
if (unlikely(!(revents & EV_TIMER) || c->receiving == RECEIVE_SHUTDOWN)) { 0 11 11
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 11 ▶ if (unlikely(c->io_taken))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->io_taken)) 1 10 11
420 1 goto header_timeout_cleanup;
421  
422 10 ▶ if ((c->receiving == RECEIVE_HEADERS || c->receiving == RECEIVE_PROXY_HEADER)
Branch: 2 branches
BranchTrueFalseTotal
if ((c->receiving == RECEIVE_HEADERS || c->receiving == RECEIVE_PROXY_HEADER) 3 7 10
if ((c->receiving == RECEIVE_HEADERS || c->receiving == RECEIVE_PROXY_HEADER) 3 0 3
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 3 header_timeout_cleanup:
436 11 SvREFCNT_dec(c->self); // the timer's ref: one-shot, libev already stopped it
437 11 SvREFCNT_dec(c->self); // the callback guard
438 11 }
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)) { 467 0 467
458 467 c->poll_retry_pending = 0;
459 467 ▶ if (likely(ev_is_active(w))) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(ev_is_active(w))) { 467 0 467
460 467 ev_timer_stop(feersum_ev_loop, w);
461 467 SvREFCNT_dec(c->self);
462 }
463 trace("poll retry %d\n", c->fd);
464 467 conn_write_ready(c);
465 467 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 0 int outq = feer_sock_outq(c->fd);
474 0 ▶ if (outq > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (outq > 0) { 0 0 0
475 0 ▶ int stalls = (c->prev_outq > 0 && outq < c->prev_outq)
Branch: 1 branch
BranchTrueFalseTotal
int stalls = (c->prev_outq > 0 && outq < c->prev_outq) 0 0 0
476 0 ▶ ? 0 : c->outq_stalls + 1;
Branch: 1 branch
BranchTrueFalseTotal
? 0 : c->outq_stalls + 1; 0 0 0
477 0 ▶ if (stalls < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (stalls < 3) { 0 0 0
478 trace("write deadline deferred fd=%d outq=%d prev=%d stalls=%d\n",
479 c->fd, outq, c->prev_outq, stalls);
480 0 restart_write_timer(c); /* resets both trackers; re-store */
481 0 c->prev_outq = outq;
482 0 c->outq_stalls = stalls;
483 0 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 0 feer_shutdown_conn(c, "write timeout");
511 0 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 1443 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 1443 ▶ if (unlikely(prep_socket(fd, lsnr->is_tcp))) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(prep_socket(fd, lsnr->is_tcp))) { 0 1443 1443
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 1443 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 1443 restart_read_timer(c);
534  
535 1443 ▶ if (srvr->header_timeout > 0.0) {
Branch: 1 branch
BranchTrueFalseTotal
if (srvr->header_timeout > 0.0) { 1442 1 1443
536 1442 ev_timer_set(&c->header_ev_timer, srvr->header_timeout, 0.0);
537 1442 ev_timer_start(feersum_ev_loop, &c->header_ev_timer);
538 1442 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 1443 ▶ if (lsnr->tls_ctx_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (lsnr->tls_ctx_ref) { 170 1273 1443
546 // the TLS handshake needs event-driven I/O
547 170 start_read_watcher(c);
548 } else
549 #endif
550 {
551 1273 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 1443 SvREFCNT_dec(c->self);
570 1443 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 2910 try_accept_one(EV_P_ struct feer_listen *lsnr, struct feer_server *srvr)
588 {
589 struct sockaddr_storage sa_buf;
590 2910 socklen_t sa_len = sizeof(struct sockaddr_storage);
591 2910 errno = 0;
592 #ifdef HAS_ACCEPT4
593 2910 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 2910 ▶ if (fd == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd == -1) { 1365 1545 2910
599 1365 ▶ if (errno == EINTR) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) return 1; 0 1365 1365
600 /* the pending connection stays queued, so a level-triggered watcher
601 * would spin on the failing accept() */
602 1365 ▶ if (errno == EMFILE || errno == ENFILE
Branch: 2 branches
BranchTrueFalseTotal
if (errno == EMFILE || errno == ENFILE 1365 0 1365
if (errno == EMFILE || errno == ENFILE 1365 0 1365
603 1365 ▶ || errno == ENOBUFS || errno == ENOMEM) {
Branch: 2 branches
BranchTrueFalseTotal
|| errno == ENOBUFS || errno == ENOMEM) { 1365 0 1365
|| errno == ENOBUFS || errno == ENOMEM) { 0 1365 1365
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 1365 return 0;
612 }
613  
614 /* admit against sockets: active_conns also counts H2 pseudo-conns, which
615 * max_h2_concurrent_streams already bounds */
616 1545 int socket_conns = srvr->active_conns;
617 #ifdef FEERSUM_HAS_H2
618 socket_conns -= srvr->active_h2_streams;
619 #endif
620 1545 ▶ if (srvr->max_connections > 0 && socket_conns >= srvr->max_connections) {
Branch: 2 branches
BranchTrueFalseTotal
if (srvr->max_connections > 0 && socket_conns >= srvr->max_connections) { 1514 31 1545
if (srvr->max_connections > 0 && socket_conns >= srvr->max_connections) { 126 1388 1514
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 1443 setup_accepted_conn(EV_A, fd, (struct sockaddr *)&sa_buf, sa_len, srvr, lsnr);
633 1443 return 1;
634 }
635  
636 static void
637 1466 accept_cb (EV_P_ ev_io *w, int revents)
638 {
639 1466 struct feer_listen *lsnr = (struct feer_listen *)w->data;
640 1466 struct feer_server *srvr = lsnr->server;
641  
642 1466 ▶ if (unlikely(srvr->shutting_down)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(srvr->shutting_down)) { 0 1466 1466
643 0 ev_io_stop(EV_A, w);
644 0 return;
645 }
646  
647 1466 ▶ if (unlikely(revents & EV_ERROR)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(revents & EV_ERROR)) { 0 1466 1466
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 1466 int accept_count = 0;
656 2908 ▶ while (accept_count++ < srvr->max_accept_per_loop) {
Branch: 1 branch
BranchTrueFalseTotal
while (accept_count++ < srvr->max_accept_per_loop) { 2908 0 2908
657 2908 ▶ if (!try_accept_one(EV_A, lsnr, srvr)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!try_accept_one(EV_A, lsnr, srvr)) break; 1466 1442 2908
658 }
659 }
660  
661 static void
662 557 setup_accept_watcher(struct feer_listen *lsnr, int listen_fd)
663 {
664 557 struct feer_server *srvr = lsnr->server;
665 557 ev_io_init(&lsnr->accept_w, accept_cb, listen_fd, EV_READ);
666 557 lsnr->accept_w.data = (void *)lsnr;
667 557 ev_set_priority(&lsnr->accept_w, srvr->accept_priority);
668 557 ev_init(&lsnr->emfile_w, emfile_retry_cb);
669 557 lsnr->emfile_w.data = (void *)lsnr;
670 557 }
671  
672 static void
673 1375 sched_request_callback (struct feer_conn *c)
674 {
675 1375 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 1375 rinq_push(&server->request_ready_rinq, c);
678 1375 SvREFCNT_inc_void_NN(c->self); // for the rinq
679 1375 ▶ if (!ev_is_active(&server->ei)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ev_is_active(&server->ei)) { 940 435 1375
680 trace("starting idle watcher\n");
681 940 ev_idle_start(feersum_ev_loop, &server->ei);
682 }
683 1375 }
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 438 try_parse_chunked (struct feer_conn *c)
690 {
691 438 ▶ if (!c->rbuf) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!c->rbuf) return 1; 0 438 438
692  
693 438 char *buf = SvPVX(c->rbuf);
694 438 STRLEN buf_len = SvCUR(c->rbuf);
695 438 STRLEN read_pos = (c->received_cl >= 0) ? (STRLEN)c->received_cl : 0;
696 438 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 5997 ▶ while (read_pos < buf_len) {
Branch: 1 branch
BranchTrueFalseTotal
while (read_pos < buf_len) { 5995 2 5997
702 5995 ▶ if (c->chunk_remaining == CHUNK_STATE_NEED_CRLF) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->chunk_remaining == CHUNK_STATE_NEED_CRLF) { 2 5993 5995
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 5993 ▶ else if (c->chunk_remaining == CHUNK_STATE_PARSE_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
else if (c->chunk_remaining == CHUNK_STATE_PARSE_SIZE) { 3132 2861 5993
715 3132 char *line_start = buf + read_pos;
716 3132 char *line_end = NULL;
717 3132 STRLEN remaining = buf_len - read_pos;
718 STRLEN i;
719  
720 181399 ▶ for (i = 0; i + 1 < remaining; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < remaining; i++) { 181099 300 181399
721 181099 ▶ 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 178267 181099
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 3132 ▶ if (!line_end) {
Branch: 1 branch
BranchTrueFalseTotal
if (!line_end) { 300 2832 3132
728 // the scan restarts every read event: an unbounded line is quadratic
729 300 ▶ if (unlikely(remaining > MAX_CHUNK_SIZE_LINE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(remaining > MAX_CHUNK_SIZE_LINE)) { 2 298 300
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 298 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 358 ▶ if (read_pos > write_pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (read_pos > write_pos) { 57 301 358
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 358 c->received_cl = write_pos;
892 358 return 1;
893 }
894  
895 static bool
896 1485 process_request_headers (struct feer_conn *c, int body_offset)
897 {
898 int err_code;
899 const char *err;
900 1485 struct feer_req *req = c->req;
901  
902 1485 stop_header_timer(c);
903  
904 trace("processing headers %d minor_version=%d\n",c->fd,req->minor_version);
905 1485 bool body_is_required = 0;
906 1485 bool next_req_follows = 0;
907 1485 bool got_content_length = 0;
908 1485 bool connection_close = 0;
909 PERL_UNUSED_VAR(body_is_required); /* only read under DEBUG >= 2 */
910  
911 1485 ▶ if (unlikely(req->minor_version > 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(req->minor_version > 1)) { 0 1485 1485
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 1485 c->is_http11 = (req->minor_version == 1);
919 1485 ▶ c->is_keepalive = c->cached_keepalive_default && c->is_http11;
Branch: 2 branches
BranchTrueFalseTotal
c->is_keepalive = c->cached_keepalive_default && c->is_http11; 739 746 1485
c->is_keepalive = c->cached_keepalive_default && c->is_http11; 661 78 739
920 1485 c->expect_continue = 0;
921 1485 c->receive_chunked = 0;
922 1485 c->reqs++;
923  
924 1485 change_receiving_state(c, RECEIVE_BODY);
925 1485 c->expected_cl = 0;
926 1485 c->received_cl = 0;
927 1485 c->body_framed = 0;
928  
929 1485 switch (req->method_len) {
930 1191 case 3:
931 1191 ▶ if (likely(memcmp(req->method, "GET", 3) == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(memcmp(req->method, "GET", 3) == 0)) { 1190 1 1191
932 1190 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 1191 break;
939 285 case 4:
940 285 ▶ if (likely(memcmp(req->method, "POST", 4) == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(memcmp(req->method, "POST", 4) == 0)) { 243 42 285
941 243 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 285 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 1483 ▶ 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 1475 1483
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 1475 char *from = SvPV(c->rbuf,from_len);
993 1475 ▶ if (unlikely(body_offset < 0 || (STRLEN)body_offset > from_len)) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(body_offset < 0 || (STRLEN)body_offset > from_len)) { 1475 0 1475
if (unlikely(body_offset < 0 || (STRLEN)body_offset > from_len)) { 0 1475 1475
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 1475 from += body_offset;
1000 1475 STRLEN need = from_len - body_offset;
1001 trace("new rbuf for body %d need=%"Sz_uf"\n", c->fd, (Sz)need);
1002 1475 ▶ SV *new_rbuf = newSVpvn(need > 0 ? from : "", need);
Branch: 1 branch
BranchTrueFalseTotal
SV *new_rbuf = newSVpvn(need > 0 ? from : "", need); 366 1109 1475
1003  
1004 1475 req->buf = c->rbuf;
1005 1475 c->rbuf = new_rbuf;
1006 1475 SvCUR_set(req->buf, body_offset);
1007  
1008 1475 UV expected = 0;
1009 1475 bool got_host = 0;
1010 1475 bool got_transfer_encoding = 0;
1011 4271 ▶ for (size_t i = 0; i < req->num_headers; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < req->num_headers; i++) { 2857 1414 4271
1012 2857 struct phr_header *hdr = &req->headers[i];
1013 // picohttpparser reports obs-fold as a NULL name
1014 2857 ▶ if (unlikely(!hdr->name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!hdr->name)) { 1 2856 2857
1015 1 err_code = 400;
1016 1 err = "Obsolete header line folding not allowed\n";
1017 1 goto got_bad_request;
1018 }
1019 2856 ▶ if (unlikely(hdr->name_len > MAX_HEADER_NAME_LEN)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(hdr->name_len > MAX_HEADER_NAME_LEN)) { 2 2854 2856
1020 2 err_code = 431;
1021 2 err = "Header name too long\n";
1022 2 goto got_bad_request;
1023 }
1024 2854 ▶ if (unlikely(hdr->name_len == 14 &&
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(hdr->name_len == 14 && 148 2706 2854
if (unlikely(hdr->name_len == 14 && 148 0 148
1025 str_case_eq_fixed("content-length", hdr->name, 14)))
1026 130 {
1027 148 ▶ if (c->receive_chunked) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->receive_chunked) { 3 145 148
1028 3 err_code = 400;
1029 3 err = "Content-Length not allowed with Transfer-Encoding\n";
1030 18 goto got_bad_request;
1031 }
1032 145 UV new_expected = 0;
1033 145 ▶ 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, 136 9 145
1034 &new_expected))) {
1035 /* expected_cl is ssize_t: past SSIZE_MAX the cast goes negative */
1036 136 ▶ if (unlikely(new_expected > (UV)c->cached_max_body_len
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(new_expected > (UV)c->cached_max_body_len 132 4 136
if (unlikely(new_expected > (UV)c->cached_max_body_len 0 132 132
1037 || new_expected > (UV)SSIZE_MAX)) {
1038 4 err_code = 413;
1039 4 err = "Content length exceeds maximum\n";
1040 4 goto got_bad_request;
1041 }
1042 132 ▶ if (got_content_length && new_expected != expected) {
Branch: 2 branches
BranchTrueFalseTotal
if (got_content_length && new_expected != expected) { 4 128 132
if (got_content_length && new_expected != expected) { 2 2 4
1043 2 err_code = 400;
1044 2 err = "Multiple conflicting Content-Length headers\n";
1045 2 goto got_bad_request;
1046 }
1047 130 expected = new_expected;
1048 130 got_content_length = 1;
1049 }
1050 else {
1051 9 err_code = 400;
1052 9 err = "Invalid Content-Length\n";
1053 9 goto got_bad_request;
1054 }
1055 }
1056 2706 ▶ else if (unlikely(hdr->name_len == 10 &&
Branch: 2 branches
BranchTrueFalseTotal
else if (unlikely(hdr->name_len == 10 && 1090 1616 2706
else if (unlikely(hdr->name_len == 10 && 929 161 1090
1057 str_case_eq_fixed("connection", hdr->name, 10)))
1058 {
1059 929 ▶ 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)) 908 21 929
1060 {
1061 908 connection_close = 1;
1062 908 c->is_keepalive = 0;
1063 trace("setting conn %d to close after response\n", c->fd);
1064 }
1065 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
1066 1 feer_header_has_token(hdr->value, hdr->value_len, "keep-alive", 10))
1067 {
1068 1 c->is_keepalive = 1;
1069 trace("setting conn %d to keep after response\n", c->fd);
1070 }
1071 929 ▶ 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; 791 138 929
if (next_req_follows && c->receive_chunked && (!c->is_http11 || got_host)) break; 4 787 791
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
1072 }
1073 1777 ▶ else if (unlikely(c->is_http11 && hdr->name_len == 6 &&
Branch: 4 branches
BranchTrueFalseTotal
else if (unlikely(c->is_http11 && hdr->name_len == 6 && 1748 29 1777
else if (unlikely(c->is_http11 && hdr->name_len == 6 && 41 1707 1748
else if (unlikely(c->is_http11 && hdr->name_len == 6 && 41 1736 1777
else if (unlikely(c->is_http11 && hdr->name_len == 6 && 22 19 41
1074 str_case_eq_fixed("expect", hdr->name, 6)))
1075 {
1076 35 if (hdr->value_len == 12 &&
1077 13 str_case_eq_fixed("100-continue", hdr->value, 12))
1078 {
1079 13 c->expect_continue = 1;
1080 trace("got Expect: 100-continue on fd=%d\n", c->fd);
1081 }
1082 else {
1083 9 err_code = 417;
1084 9 err = "Expectation Failed\n";
1085 9 goto got_bad_request;
1086 }
1087 }
1088 1755 ▶ else if (unlikely(hdr->name_len == 17 &&
Branch: 2 branches
BranchTrueFalseTotal
else if (unlikely(hdr->name_len == 17 && 136 1619 1755
else if (unlikely(hdr->name_len == 17 && 124 12 136
1089 str_case_eq_fixed("transfer-encoding", hdr->name, 17)))
1090 99 {
1091 // TE is undefined for HTTP/1.0 (RFC 9112 6.1); rejecting keeps
1092 // receive_chunked unreachable there
1093 124 ▶ if (unlikely(!c->is_http11)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!c->is_http11)) { 5 119 124
1094 5 err_code = 400;
1095 5 err = "Transfer-Encoding not allowed in HTTP/1.0\n";
1096 5 goto got_bad_request;
1097 }
1098  
1099 119 ▶ if (got_transfer_encoding) {
Branch: 1 branch
BranchTrueFalseTotal
if (got_transfer_encoding) { 6 113 119
1100 6 err_code = 400;
1101 6 err = "Multiple Transfer-Encoding headers not allowed\n";
1102 6 goto got_bad_request;
1103 }
1104 113 got_transfer_encoding = 1;
1105  
1106 113 ▶ if (unlikely(hdr->value_len == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(hdr->value_len == 0)) { 0 113 113
1107 0 err_code = 400;
1108 0 err = "Empty Transfer-Encoding\n";
1109 0 goto got_bad_request;
1110 }
1111  
1112 /* transfer-coding = token *( OWS ";" OWS parameter ) (RFC 9112 6.1) */
1113 113 bool is_chunked = 0;
1114 224 if (hdr->value_len >= 7 &&
1115 111 str_case_eq_fixed("chunked", hdr->value, 7))
1116 {
1117 103 STRLEN j = 7;
1118 104 ▶ while (j < hdr->value_len &&
Branch: 1 branch
BranchTrueFalseTotal
while (j < hdr->value_len && 4 100 104
1119 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
1120 1 j++;
1121 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
1122 }
1123  
1124 118 bool is_identity = (hdr->value_len == 8 &&
1125 5 str_case_eq_fixed("identity", hdr->value, 8));
1126  
1127 113 ▶ if (is_chunked) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_chunked) { 103 10 113
1128 103 ▶ if (got_content_length) {
Branch: 1 branch
BranchTrueFalseTotal
if (got_content_length) { 8 95 103
1129 8 err_code = 400;
1130 8 err = "Content-Length not allowed with Transfer-Encoding\n";
1131 8 goto got_bad_request;
1132 }
1133 95 c->receive_chunked = 1;
1134 trace("got Transfer-Encoding: chunked on fd=%d\n", c->fd);
1135 }
1136 10 ▶ else if (is_identity) {
Branch: 1 branch
BranchTrueFalseTotal
else if (is_identity) { 6 4 10
1137 trace("got Transfer-Encoding: identity on fd=%d (ignored)\n", c->fd);
1138 }
1139 else {
1140 6 err_code = 501;
1141 6 err = "Unsupported Transfer-Encoding\n";
1142 6 goto got_bad_request;
1143 }
1144 }
1145 1631 ▶ else if (unlikely(hdr->name_len == 4 &&
Branch: 2 branches
BranchTrueFalseTotal
else if (unlikely(hdr->name_len == 4 && 1277 354 1631
else if (unlikely(hdr->name_len == 4 && 1277 0 1277
1146 str_case_eq_fixed("host", hdr->name, 4)))
1147 {
1148 1277 ▶ if (unlikely(got_host)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(got_host)) { 2 1275 1277
1149 2 err_code = 400;
1150 2 err = "Duplicate Host header\n";
1151 2 goto got_bad_request;
1152 }
1153 1275 ▶ if (unlikely(hdr->value_len == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(hdr->value_len == 0)) { 0 1275 1275
1154 0 err_code = 400;
1155 0 err = "Empty Host header\n";
1156 0 goto got_bad_request;
1157 }
1158 1275 got_host = 1;
1159 }
1160 }
1161  
1162 1418 ▶ if (unlikely(c->is_http11 && !got_host)) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(c->is_http11 && !got_host)) { 1209 209 1418
if (unlikely(c->is_http11 && !got_host)) { 1 1208 1209
1163 1 err_code = 400;
1164 1 err = "Host header required for HTTP/1.1\n";
1165 1 goto got_bad_request;
1166 }
1167  
1168 1417 ▶ 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 1392 1417
if (c->cached_max_conn_reqs > 0 && c->reqs >= c->cached_max_conn_reqs) { 1 24 25
1169 1 c->is_keepalive = 0;
1170 trace("reached max requests per connection (%d), will close after response\n", c->cached_max_conn_reqs);
1171 }
1172  
1173 // proxies disagree on TE for body-less methods: reject to keep framing unambiguous
1174 1417 ▶ if (unlikely(c->receive_chunked && next_req_follows)) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(c->receive_chunked && next_req_follows)) { 87 1330 1417
if (unlikely(c->receive_chunked && next_req_follows)) { 4 83 87
1175 4 err_code = 400;
1176 4 err = "Transfer-Encoding not allowed on this method\n";
1177 4 goto got_bad_request;
1178 }
1179  
1180 /* implicit empty body takes the CL path: rbuf may hold the next request */
1181 1413 ▶ 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; 1217 196 1413
if (likely(next_req_follows) && !got_content_length && !c->receive_chunked) goto got_cl; 1217 0 1217
if (likely(next_req_follows) && !got_content_length && !c->receive_chunked) goto got_cl; 1217 0 1217
1182 196 ▶ else if (likely(got_content_length)) goto got_cl;
Branch: 1 branch
BranchTrueFalseTotal
else if (likely(got_content_length)) goto got_cl; 110 86 196
1183 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
1184  
1185 3 err_code = 411;
1186 3 err = "Content-Length or Transfer-Encoding required\n";
1187  
1188 90 got_bad_request:
1189 90 respond_with_server_error(c, err, err_code);
1190 90 return 0;
1191  
1192 1327 got_cl:
1193 1327 c->expected_cl = (ssize_t)expected;
1194 1327 c->received_cl = SvCUR(c->rbuf);
1195 1327 c->body_framed = 1;
1196 trace("expecting body %d size=%"Ssz_df" have=%"Ssz_df"\n",
1197 c->fd, (Ssz)c->expected_cl, (Ssz)c->received_cl);
1198 /* never pre-grow to the peer's declared length (a Perl allocation failure
1199 * is a hard _exit); try_conn_read grows the rest as bytes arrive */
1200 {
1201 1327 STRLEN want = (STRLEN)c->expected_cl + 1;
1202 1327 ▶ if (want > BODY_PREGROW_MAX) want = BODY_PREGROW_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (want > BODY_PREGROW_MAX) want = BODY_PREGROW_MAX; 1 1326 1327
1203 1327 ▶ SvGROW(c->rbuf, want);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(c->rbuf, want); 1327 0 1327
SvGROW(c->rbuf, want); 18 1309 1327
1204 }
1205  
1206 1327 ▶ 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)) { 90 1237 1327
if (unlikely(c->expected_cl && c->received_cl < c->expected_cl)) { 28 62 90
1207 28 send_100_continue(c);
1208 28 return 1;
1209 }
1210 1299 goto got_it_all;
1211  
1212 83 got_chunked:
1213 83 c->chunk_remaining = CHUNK_STATE_PARSE_SIZE;
1214 83 c->chunk_count = 0;
1215 83 c->trailer_count = 0;
1216 83 c->expected_cl = 0;
1217 83 c->received_cl = 0;
1218 83 c->body_framed = 1;
1219 83 change_receiving_state(c, RECEIVE_CHUNKED);
1220 trace("starting chunked receive on fd=%d, have=%"Sz_uf" bytes\n",
1221 c->fd, (Sz)SvCUR(c->rbuf));
1222  
1223 83 send_100_continue(c);
1224  
1225 {
1226 83 int ret = try_parse_chunked(c);
1227 83 ▶ if (ret == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (ret == 1) 22 61 83
1228 22 return 1;
1229 61 ▶ if (ret == -2) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == -2) { 0 61 61
1230 // headers and an oversize chunk in one segment land here, not in
1231 // try_conn_read; falling through would dispatch a truncated body
1232 0 err_code = 413;
1233 0 err = "Request body too large\n";
1234 0 goto got_bad_request;
1235 }
1236 61 ▶ if (ret == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == -1) { 15 46 61
1237 15 err_code = 400;
1238 15 err = "Malformed chunked encoding\n";
1239 15 goto got_bad_request;
1240 }
1241 }
1242  
1243 46 got_it_all:
1244 1345 sched_request_callback(c);
1245 1345 return 0;
1246 }
1247  
1248 static void
1249 4210 conn_write_ready (struct feer_conn *c)
1250 {
1251 #ifdef FEERSUM_HAS_H2
1252 if (c->is_h2_stream) {
1253 h2_try_stream_write(aTHX_ c);
1254 return;
1255 }
1256 #endif
1257  
1258 4210 ▶ if (c->in_callback) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->in_callback) { 2081 2129 4210
1259 2081 start_write_watcher(c);
1260 2081 return;
1261 }
1262  
1263 #ifdef FEERSUM_HAS_TLS
1264 2129 ▶ if (c->tls) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls) { 199 1930 2129
1265 199 try_tls_conn_write(feersum_ev_loop, &c->write_ev_io, EV_WRITE);
1266 199 return;
1267 }
1268 #endif
1269  
1270 1930 try_conn_write(feersum_ev_loop, &c->write_ev_io, EV_WRITE);
1271 }
1272  
1273 /* H2 dispatch shims for XS CODE/PPCODE blocks (which cannot use #ifdef). */
1274 static int
1275 580 h2_try_write_chunk (pTHX_ struct feer_conn *c, SV *body)
1276 {
1277 #ifdef FEERSUM_HAS_H2
1278 if (unlikely(c->is_h2_stream)) {
1279 feersum_h2_write_chunk(aTHX_ c, body);
1280 return 1;
1281 }
1282 #endif
1283 PERL_UNUSED_VAR(c); PERL_UNUSED_VAR(body);
1284 580 return 0;
1285 }
1286  
1287 static int
1288 73 h2_is_stream (struct feer_conn *c)
1289 {
1290 #ifdef FEERSUM_HAS_H2
1291 if (unlikely(c->is_h2_stream))
1292 return 1;
1293 #endif
1294 PERL_UNUSED_VAR(c);
1295 73 return 0;
1296 }
1297  
1298 INLINE_UNLESS_DEBUG static void
1299 111 send_100_continue (struct feer_conn *c)
1300 {
1301 111 ▶ if (likely(!c->expect_continue))
Branch: 1 branch
BranchTrueFalseTotal
if (likely(!c->expect_continue)) 98 13 111
1302 98 return;
1303  
1304 static const char continue_response[] = "HTTP/1.1 100 Continue" CRLF CRLF;
1305 #ifdef FEERSUM_HAS_TLS
1306 13 ▶ if (c->tls) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls) { 2 11 13
1307 2 feer_tls_send(c, continue_response, sizeof(continue_response) - 1);
1308 2 feer_tls_flush_wbuf(c);
1309 2 ▶ if (c->tls_wbuf.off > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_wbuf.off > 0) 0 2 2
1310 0 start_write_watcher(c);
1311 2 c->expect_continue = 0;
1312 2 return;
1313 }
1314 #endif
1315 11 size_t cont_len = sizeof(continue_response) - 1;
1316 11 ssize_t wr = write(c->fd, continue_response, cont_len);
1317 // a wholly failed write is fine: the client sends the body anyway after its wait
1318 11 ▶ if (likely(wr == (ssize_t)cont_len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(wr == (ssize_t)cont_len)) { 0 11 11
1319 trace("sent 100 Continue to fd=%d\n", c->fd);
1320 }
1321 0 ▶ else if (wr > 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (wr > 0) { 0 0 0
1322 // queue the tail; a truncated status line reads as the start of the response
1323 0 add_const_to_wbuf(c, continue_response + wr, cont_len - (size_t)wr);
1324 0 start_write_watcher(c);
1325 }
1326 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
1327 trace("100 Continue write error fd=%d: %s\n", c->fd, strerror(errno));
1328 }
1329 11 c->expect_continue = 0;
1330 }
1331  
1332 INLINE_UNLESS_DEBUG static void
1333 1401 free_feer_req (struct feer_req *req)
1334 {
1335 1401 ▶ if (unlikely(!req))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!req)) 170 1231 1401
1336 170 return;
1337  
1338 1231 SvREFCNT_dec(req->buf);
1339 1231 SvREFCNT_dec(req->path);
1340 1231 SvREFCNT_dec(req->query);
1341 #ifdef FEERSUM_HAS_H2
1342 SvREFCNT_dec(req->h2_method_sv);
1343 SvREFCNT_dec(req->h2_uri_sv);
1344 #endif
1345 1231 ▶ FEER_REQ_FREE(req);
Branch: 1 branch
BranchTrueFalseTotal
FEER_REQ_FREE(req); 1231 0 1231
1346 }
1347  
1348 INLINE_UNLESS_DEBUG static void
1349 1401 free_request (struct feer_conn *c)
1350 {
1351 1401 free_feer_req(c->req);
1352 1401 c->req = NULL;
1353 1401 }
1354  
1355 static void
1356 397 respond_with_server_error (struct feer_conn *c, const char *msg, int err_code)
1357 {
1358 SV *tmp;
1359 STRLEN msg_len;
1360  
1361 #ifdef FEERSUM_HAS_H2
1362 if (unlikely(c->is_h2_stream)) {
1363 feersum_h2_respond_error(c, err_code, msg);
1364 return;
1365 }
1366 #endif
1367  
1368 397 ▶ if (unlikely(c->responding != RESPOND_NOT_STARTED)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->responding != RESPOND_NOT_STARTED)) { 0 397 397
1369 0 trouble("Tried to send server error but already responding!\n");
1370 0 return;
1371 }
1372  
1373 397 msg_len = strlen(msg);
1374 assert(msg_len < INT_MAX);
1375  
1376 // HEAD keeps the Content-Length but no content; c->req is absent before
1377 // the request line parses
1378 293 ▶ 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 202 293
1379 690 ▶ && memcmp(c->req->method, "HEAD", 4) == 0;
Branch: 2 branches
BranchTrueFalseTotal
&& memcmp(c->req->method, "HEAD", 4) == 0; 293 104 397
&& memcmp(c->req->method, "HEAD", 4) == 0; 6 85 91
1380  
1381 /* DATE_BUF already ends in CRLF */
1382 397 ▶ tmp = newSVpvf("HTTP/1.%d %d %s" CRLF
Branch: 1 branch
BranchTrueFalseTotal
tmp = newSVpvf("HTTP/1.%d %d %s" CRLF 6 391 397
1383 "Content-Type: text/plain" CRLF
1384 "Connection: close" CRLF
1385 "Cache-Control: no-cache, no-store" CRLF
1386 "%s"
1387 "Content-Length: %"Ssz_df"" CRLFx2
1388 "%.*s",
1389 c->is_http11 ? 1 : 0,
1390 err_code, http_code_to_msg(err_code),
1391 DATE_BUF,
1392 (Ssz)msg_len,
1393 no_body ? 0 : (int)msg_len, msg);
1394 /* not a mortal: ev callbacks have no FREETMPS scope, so it would leak */
1395 397 add_sv_to_wbuf(c, tmp);
1396 397 SvREFCNT_dec(tmp);
1397  
1398 397 stop_read_watcher(c);
1399 397 stop_read_timer(c);
1400 397 stop_header_timer(c);
1401 397 change_responding_state(c, RESPOND_SHUTDOWN);
1402 397 change_receiving_state(c, RECEIVE_SHUTDOWN);
1403 397 c->is_keepalive = 0;
1404 397 conn_write_ready(c);
1405 }
1406  
1407  
« feersum_utils.c.inc ↑ Summary Feersum.xs »