xref: /lighttpd1.4/src/connections.c (revision 2f7839e8)
1 #include "first.h"
2 
3 #include "base.h"
4 #include "buffer.h"
5 #include "burl.h"       /* HTTP_PARSEOPT_HEADER_STRICT */
6 #include "chunk.h"
7 #include "log.h"
8 #include "connections.h"
9 #include "fdevent.h"
10 #include "h2.h"
11 #include "http_header.h"
12 
13 #include "reqpool.h"
14 #include "request.h"
15 #include "response.h"
16 #include "network.h"
17 #include "stat_cache.h"
18 
19 #include "plugin.h"
20 
21 #include "inet_ntop_cache.h"
22 
23 #include <sys/stat.h>
24 
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <string.h>
29 
30 #ifdef HAVE_SYS_FILIO_H
31 # include <sys/filio.h>
32 #endif
33 
34 #include "sys-socket.h"
35 
36 #define HTTP_LINGER_TIMEOUT 5
37 
38 #define connection_set_state(r, n) ((r)->state = (n))
39 
40 __attribute_cold__
41 static void connection_set_state_error(request_st * const r, const request_state_t state) {
42     connection_set_state(r, state);
43 }
44 
45 __attribute_cold__
46 static connection *connection_init(server *srv);
47 
48 static void connection_reset(connection *con);
49 
50 
51 static connection *connections_get_new_connection(server *srv) {
52 	connections * const conns = &srv->conns;
53 	size_t i;
54 
55 	if (conns->size == conns->used) {
56 		conns->size += srv->max_conns >= 128 ? 128 : srv->max_conns > 16 ? 16 : srv->max_conns;
57 		conns->ptr = realloc(conns->ptr, sizeof(*conns->ptr) * conns->size);
58 		force_assert(NULL != conns->ptr);
59 
60 		for (i = conns->used; i < conns->size; i++) {
61 			conns->ptr[i] = connection_init(srv);
62 			connection_reset(conns->ptr[i]);
63 		}
64 	}
65 
66 	conns->ptr[conns->used]->ndx = conns->used;
67 	return conns->ptr[conns->used++];
68 }
69 
70 static void connection_del(server *srv, connection *con) {
71 	connections * const conns = &srv->conns;
72 
73 	if (-1 == con->ndx) return;
74 	uint32_t i = (uint32_t)con->ndx;
75 
76 	/* not last element */
77 
78 	if (i != --conns->used) {
79 		connection * const temp = conns->ptr[i];
80 		conns->ptr[i] = conns->ptr[conns->used];
81 		conns->ptr[conns->used] = temp;
82 
83 		conns->ptr[i]->ndx = i;
84 		conns->ptr[conns->used]->ndx = -1;
85 	}
86 
87 	con->ndx = -1;
88 #if 0
89 	fprintf(stderr, "%s.%d: del: (%d)", __FILE__, __LINE__, conns->used);
90 	for (i = 0; i < conns->used; i++) {
91 		fprintf(stderr, "%d ", conns->ptr[i]->fd);
92 	}
93 	fprintf(stderr, "\n");
94 #endif
95 }
96 
97 static void connection_close(connection *con) {
98 	if (con->fd < 0) con->fd = -con->fd;
99 
100 	plugins_call_handle_connection_close(con);
101 
102 	server * const srv = con->srv;
103 	request_st * const r = &con->request;
104 	request_reset_ex(r); /*(r->conf.* is still valid below)*/
105 	connection_set_state(r, CON_STATE_CONNECT);
106 
107 	chunkqueue_reset(con->read_queue);
108 	con->request_count = 0;
109 	con->is_ssl_sock = 0;
110 	con->revents_err = 0;
111 
112 	fdevent_fdnode_event_del(srv->ev, con->fdn);
113 	fdevent_unregister(srv->ev, con->fd);
114 	con->fdn = NULL;
115 #ifdef __WIN32
116 	if (0 == closesocket(con->fd))
117 #else
118 	if (0 == close(con->fd))
119 #endif
120 		--srv->cur_fds;
121 	else
122 		log_perror(r->conf.errh, __FILE__, __LINE__,
123 		  "(warning) close: %d", con->fd);
124 
125 	if (r->conf.log_state_handling) {
126 		log_error(r->conf.errh, __FILE__, __LINE__,
127 		  "connection closed for fd %d", con->fd);
128 	}
129 	con->fd = -1;
130 
131 	connection_del(srv, con);
132 }
133 
134 static void connection_read_for_eos_plain(connection * const con) {
135 	/* we have to do the linger_on_close stuff regardless
136 	 * of r->keep_alive; even non-keepalive sockets
137 	 * may still have unread data, and closing before reading
138 	 * it will make the client not see all our output.
139 	 */
140 	ssize_t len;
141 	const int type = sock_addr_get_family(&con->dst_addr);
142 	char buf[16384];
143 	do {
144 		len = fdevent_socket_read_discard(con->fd, buf, sizeof(buf),
145 						  type, SOCK_STREAM);
146 	} while (len > 0 || (len < 0 && errno == EINTR));
147 
148 	if (len < 0 && errno == EAGAIN) return;
149       #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
150 	if (len < 0 && errno == EWOULDBLOCK) return;
151       #endif
152 
153 	/* 0 == len || (len < 0 && (errno is a non-recoverable error)) */
154 		con->close_timeout_ts = log_epoch_secs - (HTTP_LINGER_TIMEOUT+1);
155 }
156 
157 static void connection_read_for_eos_ssl(connection * const con) {
158 	if (con->network_read(con, con->read_queue, MAX_READ_LIMIT) < 0)
159 		con->close_timeout_ts = log_epoch_secs - (HTTP_LINGER_TIMEOUT+1);
160 	chunkqueue_reset(con->read_queue);
161 }
162 
163 static void connection_read_for_eos(connection * const con) {
164 	!con->is_ssl_sock
165 	  ? connection_read_for_eos_plain(con)
166 	  : connection_read_for_eos_ssl(con);
167 }
168 
169 static void connection_handle_close_state(connection *con) {
170 	connection_read_for_eos(con);
171 
172 	if (log_epoch_secs - con->close_timeout_ts > HTTP_LINGER_TIMEOUT) {
173 		connection_close(con);
174 	}
175 }
176 
177 static void connection_handle_shutdown(connection *con) {
178 	plugins_call_handle_connection_shut_wr(con);
179 
180 	connection_reset(con);
181 	++con->srv->con_closed;
182 
183 	/* close the connection */
184 	if (con->fd >= 0
185 	    && (con->is_ssl_sock || 0 == shutdown(con->fd, SHUT_WR))) {
186 		con->close_timeout_ts = log_epoch_secs;
187 
188 		request_st * const r = &con->request;
189 		connection_set_state(r, CON_STATE_CLOSE);
190 		if (r->conf.log_state_handling) {
191 			log_error(r->conf.errh, __FILE__, __LINE__,
192 			  "shutdown for fd %d", con->fd);
193 		}
194 	} else {
195 		connection_close(con);
196 	}
197 }
198 
199 __attribute_cold__
200 static void connection_fdwaitqueue_append(connection *con) {
201     connection_list_append(&con->srv->fdwaitqueue, con);
202 }
203 
204 
205 static void connection_handle_response_end_state(request_st * const r, connection * const con) {
206 	if (r->http_version > HTTP_VERSION_1_1) {
207 		h2_retire_con(r, con);
208 		r->keep_alive = 0;
209 		/* set a status so that mod_accesslog, mod_rrdtool hooks are called
210 		 * in plugins_call_handle_request_done() (XXX: or set to 0 to omit) */
211 		r->http_status = 100; /* XXX: what if con->state == CON_STATE_ERROR? */
212 	}
213 
214 	/* call request_done hook if http_status set (e.g. to log request) */
215 	/* (even if error, connection dropped, as long as http_status is set) */
216 	if (r->http_status) plugins_call_handle_request_done(r);
217 
218 	if (r->state != CON_STATE_ERROR) ++con->srv->con_written;
219 
220 	if (r->reqbody_length != r->reqbody_queue.bytes_in
221 	    || r->state == CON_STATE_ERROR) {
222 		/* request body may not have been read completely */
223 		r->keep_alive = 0;
224 		/* clean up failed partial write of 1xx intermediate responses*/
225 		if (&r->write_queue != con->write_queue) { /*(for HTTP/1.1)*/
226 			chunkqueue_free(con->write_queue);
227 			con->write_queue = &r->write_queue;
228 		}
229 	}
230 
231         if (r->keep_alive) {
232 		request_reset(r);
233 		config_reset_config(r);
234 		con->is_readable = 1; /* potentially trigger optimistic read */
235 		/*(accounting used by mod_accesslog for HTTP/1.0 and HTTP/1.1)*/
236 		r->bytes_read_ckpt = con->bytes_read;
237 		r->bytes_written_ckpt = con->bytes_written;
238 #if 0
239 		r->start_hp.tv_sec = con->read_idle_ts = log_epoch_secs;
240 #endif
241 		connection_set_state(r, CON_STATE_REQUEST_START);
242 	} else {
243 		connection_handle_shutdown(con);
244 	}
245 }
246 
247 
248 __attribute_pure__
249 static off_t
250 connection_write_throttled (const connection * const con, off_t max_bytes)
251 {
252     const request_config * const restrict rconf = &con->request.conf;
253     if (0 == rconf->global_bytes_per_second && 0 == rconf->bytes_per_second)
254         return max_bytes;
255 
256     if (rconf->global_bytes_per_second) {
257         off_t limit = (off_t)rconf->global_bytes_per_second
258                     - *(rconf->global_bytes_per_second_cnt_ptr);
259         if (max_bytes > limit)
260             max_bytes = limit;
261     }
262 
263     if (rconf->bytes_per_second) {
264         off_t limit = (off_t)rconf->bytes_per_second
265                     - con->bytes_written_cur_second;
266         if (max_bytes > limit)
267             max_bytes = limit;
268     }
269 
270     return max_bytes > 0 ? max_bytes : 0; /*(0 == reached traffic limit)*/
271 }
272 
273 
274 static off_t
275 connection_write_throttle (connection * const con, off_t max_bytes)
276 {
277     /*assert(max_bytes > 0);*/
278     max_bytes = connection_write_throttled(con, max_bytes);
279     if (0 == max_bytes) con->traffic_limit_reached = 1;
280     return max_bytes;
281 }
282 
283 
284 static int
285 connection_write_chunkqueue (connection * const con, chunkqueue * const restrict cq, off_t max_bytes)
286 {
287     /*assert(!chunkqueue_is_empty(cq));*//* checked by callers */
288 
289     con->write_request_ts = log_epoch_secs;
290 
291     max_bytes = connection_write_throttle(con, max_bytes);
292     if (0 == max_bytes) return 1;
293 
294     off_t written = cq->bytes_out;
295     int ret;
296 
297   #ifdef TCP_CORK
298     int corked = 0;
299   #endif
300 
301     /* walk chunkqueue up to first FILE_CHUNK (if present)
302      * This may incur memory load misses for pointer chasing, but effectively
303      * preloads part of the chunkqueue, something which used to be a side effect
304      * of a previous (less efficient) version of chunkqueue_length() which
305      * walked the entire chunkqueue (on each and every call).  The loads here
306      * make a measurable difference in performance in underlying call to
307      * con->network_write() */
308     if (cq->first->next) {
309         const chunk *c = cq->first;
310         while (c->type == MEM_CHUNK && NULL != (c = c->next)) ;
311       #ifdef TCP_CORK
312         /* Linux: put a cork into socket as we want to combine write() calls
313          * but only if we really have multiple chunks including non-MEM_CHUNK
314          * (or if multiple chunks and TLS), and only if TCP socket */
315         if (NULL != c || con->is_ssl_sock) {
316             const int sa_family = sock_addr_get_family(&con->srv_socket->addr);
317             if (sa_family == AF_INET || sa_family == AF_INET6) {
318                 corked = 1;
319                 (void)setsockopt(con->fd, IPPROTO_TCP, TCP_CORK,
320                                  &corked, sizeof(corked));
321             }
322         }
323       #endif
324     }
325 
326     ret = con->network_write(con, cq, max_bytes);
327     if (ret >= 0) {
328         ret = chunkqueue_is_empty(cq) ? 0 : 1;
329     }
330 
331   #ifdef TCP_CORK
332     if (corked) {
333         corked = 0;
334         (void)setsockopt(con->fd, IPPROTO_TCP, TCP_CORK,
335                          &corked, sizeof(corked));
336     }
337   #endif
338 
339     written = cq->bytes_out - written;
340     con->bytes_written += written;
341     con->bytes_written_cur_second += written;
342     request_st * const r = &con->request;
343     if (r->conf.global_bytes_per_second_cnt_ptr)
344         *(r->conf.global_bytes_per_second_cnt_ptr) += written;
345 
346     return ret;
347 }
348 
349 
350 static int
351 connection_write_1xx_info (request_st * const r, connection * const con)
352 {
353     /* (Note: prior 1xx intermediate responses may be present in cq) */
354     /* (Note: also choosing not to update con->write_request_ts
355      *  which differs from connection_write_chunkqueue()) */
356     chunkqueue * const cq = con->write_queue;
357     off_t written = cq->bytes_out;
358 
359     int rc = con->network_write(con, cq, MAX_WRITE_LIMIT);
360 
361     written = cq->bytes_out - written;
362     con->bytes_written += written;
363     con->bytes_written_cur_second += written;
364     if (r->conf.global_bytes_per_second_cnt_ptr)
365         *(r->conf.global_bytes_per_second_cnt_ptr) += written;
366 
367     if (rc < 0) {
368         connection_set_state_error(r, CON_STATE_ERROR);
369         return 0; /* error */
370     }
371 
372     if (!chunkqueue_is_empty(cq)) { /* partial write (unlikely) */
373         con->is_writable = 0;
374         if (cq == &r->write_queue) {
375             /* save partial write of 1xx in separate chunkqueue
376              * Note: sending of remainder of 1xx might be delayed
377              * until next set of response headers are sent */
378             con->write_queue = chunkqueue_init(NULL);
379             chunkqueue_append_chunkqueue(con->write_queue, cq);
380         }
381     }
382 
383   #if 0
384     /* XXX: accounting inconsistency
385      * 1xx is not currently included in r->resp_header_len,
386      * so mod_accesslog reporting of %b or %B (FORMAT_BYTES_OUT_NO_HEADER)
387      * reports all bytes out minus len of final response headers,
388      * but including 1xx intermediate responses.  If 1xx intermediate
389      * responses were included in r->resp_header_len, then there are a
390      * few places in the code which must be adjusted to use r->resp_header_done
391      * instead of (0 == r->resp_header_len) as flag that final response was set
392      * (Doing the following would "discard" the 1xx len from bytes_out)
393      */
394     r->write_queue.bytes_in = r->write_queue.bytes_out = 0;
395   #endif
396 
397     return 1; /* success */
398 }
399 
400 
401 int
402 connection_send_1xx (request_st * const r, connection * const con)
403 {
404     /* Make best effort to send HTTP/1.1 1xx intermediate */
405     /* (Note: if other modules set response headers *before* the
406      *  handle_response_start hook, and the backends subsequently sends 1xx,
407      *  then the response headers are sent here with 1xx and might be cleared
408      *  by caller (http_response_parse_headers() and http_response_check_1xx()),
409      *  instead of being sent with the final response.
410      *  (e.g. mod_magnet setting response headers, then backend sending 103)) */
411 
412     chunkqueue * const cq = con->write_queue; /*(bypass r->write_queue)*/
413 
414     buffer * const b = chunkqueue_append_buffer_open(cq);
415     buffer_copy_string_len(b, CONST_STR_LEN("HTTP/1.1 "));
416     http_status_append(b, r->http_status);
417     for (uint32_t i = 0; i < r->resp_headers.used; ++i) {
418         const data_string * const ds = (data_string *)r->resp_headers.data[i];
419 
420         if (buffer_string_is_empty(&ds->value)) continue;
421         if (buffer_string_is_empty(&ds->key)) continue;
422 
423         buffer_append_string_len(b, CONST_STR_LEN("\r\n"));
424         buffer_append_string_buffer(b, &ds->key);
425         buffer_append_string_len(b, CONST_STR_LEN(": "));
426         buffer_append_string_buffer(b, &ds->value);
427     }
428     buffer_append_string_len(b, CONST_STR_LEN("\r\n\r\n"));
429     chunkqueue_append_buffer_commit(cq);
430 
431     if (con->traffic_limit_reached)
432         return 1; /* success; send later if throttled */
433 
434     return connection_write_1xx_info(r, con);
435 }
436 
437 
438 static int
439 connection_write_100_continue (request_st * const r, connection * const con)
440 {
441     /* Make best effort to send "HTTP/1.1 100 Continue" */
442     static const char http_100_continue[] = "HTTP/1.1 100 Continue\r\n\r\n";
443 
444     if (con->traffic_limit_reached)
445         return 1; /* success; skip sending if throttled */
446 
447     chunkqueue * const cq = con->write_queue; /*(bypass r->write_queue)*/
448     chunkqueue_append_mem(cq, http_100_continue, sizeof(http_100_continue)-1);
449     return connection_write_1xx_info(r, con);
450 }
451 
452 
453 static int connection_handle_write(request_st * const r, connection * const con) {
454 	/*assert(!chunkqueue_is_empty(cq));*//* checked by callers */
455 
456 	if (!con->is_writable) return CON_STATE_WRITE;
457 	int rc = connection_write_chunkqueue(con, con->write_queue, MAX_WRITE_LIMIT);
458 	switch (rc) {
459 	case 0:
460 		if (r->resp_body_finished) {
461 			connection_set_state(r, CON_STATE_RESPONSE_END);
462 			return CON_STATE_RESPONSE_END;
463 		}
464 		break;
465 	case -1: /* error on our side */
466 		log_error(r->conf.errh, __FILE__, __LINE__,
467 		  "connection closed: write failed on fd %d", con->fd);
468 		connection_set_state_error(r, CON_STATE_ERROR);
469 		return CON_STATE_ERROR;
470 	case -2: /* remote close */
471 		connection_set_state_error(r, CON_STATE_ERROR);
472 		return CON_STATE_ERROR;
473 	case 1:
474 		/* do not spin trying to send HTTP/2 server Connection Preface
475 		 * while waiting for TLS negotiation to complete */
476 		if (con->write_queue->bytes_out)
477 			con->is_writable = 0;
478 
479 		/* not finished yet -> WRITE */
480 		break;
481 	}
482 
483 	return CON_STATE_WRITE; /*(state did not change)*/
484 }
485 
486 static int connection_handle_write_state(request_st * const r, connection * const con) {
487     do {
488         /* only try to write if we have something in the queue */
489         if (!chunkqueue_is_empty(&r->write_queue)) {
490             if (r->http_version <= HTTP_VERSION_1_1) {
491                 int rc = connection_handle_write(r, con);
492                 if (rc != CON_STATE_WRITE) return rc;
493             }
494         } else if (r->resp_body_finished) {
495             connection_set_state(r, CON_STATE_RESPONSE_END);
496             return CON_STATE_RESPONSE_END;
497         }
498 
499         if (r->handler_module && !r->resp_body_finished) {
500             const plugin * const p = r->handler_module;
501             int rc = p->handle_subrequest(r, p->data);
502             switch(rc) {
503             case HANDLER_WAIT_FOR_EVENT:
504             case HANDLER_FINISHED:
505             case HANDLER_GO_ON:
506                 break;
507             case HANDLER_WAIT_FOR_FD:
508                 /* (In addition to waiting for dispatch from fdwaitqueue,
509                  *  HTTP/2 connections may retry more frequently after any
510                  *  activity occurs on connection or on other streams) */
511                 connection_fdwaitqueue_append(con);
512                 break;
513             case HANDLER_COMEBACK:
514             default:
515                 log_error(r->conf.errh, __FILE__, __LINE__,
516                   "unexpected subrequest handler ret-value: %d %d",
517                   con->fd, rc);
518                 /* fall through */
519             case HANDLER_ERROR:
520                 connection_set_state_error(r, CON_STATE_ERROR);
521                 return CON_STATE_ERROR;
522             }
523         }
524     } while (r->http_version <= HTTP_VERSION_1_1
525              && (!chunkqueue_is_empty(&r->write_queue)
526                  ? con->is_writable
527                  : r->resp_body_finished));
528 
529     return CON_STATE_WRITE;
530 }
531 
532 
533 __attribute_cold__
534 static connection *connection_init(server *srv) {
535 	connection * const con = calloc(1, sizeof(*con));
536 	force_assert(NULL != con);
537 
538 	con->fd = 0;
539 	con->ndx = -1;
540 	con->bytes_written = 0;
541 	con->bytes_read = 0;
542 
543 	con->dst_addr_buf = buffer_init();
544 	con->srv  = srv;
545 	con->plugin_slots = srv->plugin_slots;
546 	con->config_data_base = srv->config_data_base;
547 
548 	request_st * const r = &con->request;
549 	request_init_data(r, con, srv);
550 	config_reset_config(r);
551 	con->write_queue = &r->write_queue;
552 	con->read_queue = &r->read_queue;
553 
554 	/* init plugin-specific per-connection structures */
555 	con->plugin_ctx = calloc(1, (srv->plugins.used + 1) * sizeof(void *));
556 	force_assert(NULL != con->plugin_ctx);
557 
558 	return con;
559 }
560 
561 
562 void connections_free(server *srv) {
563 	connections * const conns = &srv->conns;
564 	for (uint32_t i = 0; i < conns->size; ++i) {
565 		connection *con = conns->ptr[i];
566 		request_st * const r = &con->request;
567 
568 		connection_reset(con);
569 		if (con->write_queue != &r->write_queue)
570 			chunkqueue_free(con->write_queue);
571 		if (con->read_queue != &r->read_queue)
572 			chunkqueue_free(con->read_queue);
573 		request_free_data(r);
574 
575 		free(con->plugin_ctx);
576 		buffer_free(con->dst_addr_buf);
577 
578 		free(con);
579 	}
580 
581 	free(conns->ptr);
582 	conns->ptr = NULL;
583 }
584 
585 
586 static void connection_reset(connection *con) {
587 	request_st * const r = &con->request;
588 	request_reset(r);
589 	config_reset_config(r);
590 	r->bytes_read_ckpt = 0;
591 	r->bytes_written_ckpt = 0;
592 	con->is_readable = 1;
593 
594 	con->bytes_written = 0;
595 	con->bytes_written_cur_second = 0;
596 	con->bytes_read = 0;
597 }
598 
599 
600 __attribute_cold__
601 static chunk *
602 connection_discard_blank_line (chunkqueue * const cq, uint32_t header_len)
603 {
604     /*(separate func only to be able to mark with compiler hint as cold)*/
605     chunkqueue_mark_written(cq, header_len);
606     return cq->first; /* refresh c after chunkqueue_mark_written() */
607 }
608 
609 
610 static chunk * connection_read_header_more(connection *con, chunkqueue *cq, chunk *c, const size_t olen) {
611     /*(should not be reached by HTTP/2 streams)*/
612     /*if (r->http_version == HTTP_VERSION_2) return NULL;*/
613     /*(However, new connections over TLS may become HTTP/2 connections via ALPN
614      * and return from this routine with r->http_version == HTTP_VERSION_2) */
615 
616     if ((NULL == c || NULL == c->next) && con->is_readable) {
617         con->read_idle_ts = log_epoch_secs;
618         if (0 != con->network_read(con, cq, MAX_READ_LIMIT)) {
619             request_st * const r = &con->request;
620             connection_set_state_error(r, CON_STATE_ERROR);
621         }
622         /* check if switched to HTTP/2 (ALPN "h2" during TLS negotiation) */
623         request_st * const r = &con->request;
624         if (r->http_version == HTTP_VERSION_2) return NULL;
625     }
626 
627     if (cq->first != cq->last && 0 != olen) {
628         const size_t clen = chunkqueue_length(cq);
629         size_t block = (olen + (16384-1)) & (16384-1);
630         block += (block - olen > 1024 ? 0 : 16384);
631         chunkqueue_compact_mem(cq, block > clen ? clen : block);
632     }
633 
634     /* detect if data is added to chunk */
635     c = cq->first;
636     return (c && (size_t)c->offset + olen < buffer_string_length(c->mem))
637       ? c
638       : NULL;
639 }
640 
641 
642 static void
643 connection_transition_h2 (request_st * const h2r, connection * const con)
644 {
645     buffer_copy_string_len(&h2r->target,      CONST_STR_LEN("*"));
646     buffer_copy_string_len(&h2r->target_orig, CONST_STR_LEN("*"));
647     buffer_copy_string_len(&h2r->uri.path,    CONST_STR_LEN("*"));
648     h2r->http_method = HTTP_METHOD_PRI;
649     h2r->reqbody_length = -1; /*(unnecessary for h2r?)*/
650     h2r->conf.stream_request_body |= FDEVENT_STREAM_REQUEST_POLLIN;
651 
652     /* (h2r->state == CON_STATE_READ) for transition by ALPN
653      *   or starting cleartext HTTP/2 with Prior Knowledge
654      *   (e.g. via HTTP Alternative Services)
655      * (h2r->state == CON_STATE_RESPONSE_END) for Upgrade: h2c */
656 
657     if (h2r->state != CON_STATE_ERROR)
658         connection_set_state(h2r, CON_STATE_WRITE);
659 
660   #if 0 /* ... if it turns out we need a separate fdevent handler for HTTP/2 */
661     con->fdn->handler = connection_handle_fdevent_h2;
662   #endif
663 
664     if (NULL == con->h2) /*(not yet transitioned to HTTP/2; not Upgrade: h2c)*/
665         h2_init_con(h2r, con, NULL);
666 }
667 
668 
669 /**
670  * handle request header read
671  *
672  * we get called by the state-engine and by the fdevent-handler
673  */
674 __attribute_noinline__
675 static int connection_handle_read_state(connection * const con)  {
676     /*(should not be reached by HTTP/2 streams)*/
677     chunkqueue * const cq = con->read_queue;
678     chunk *c = cq->first;
679     uint32_t clen = 0;
680     uint32_t header_len = 0;
681     request_st * const r = &con->request;
682     uint8_t keepalive_request_start = 0;
683     uint8_t pipelined_request_start = 0;
684     uint8_t discard_blank = 0;
685     unsigned short hoff[8192]; /* max num header lines + 3; 16k on stack */
686 
687     if (con->request_count > 1) {
688         discard_blank = 1;
689         if (con->bytes_read == r->bytes_read_ckpt) {
690             keepalive_request_start = 1;
691             if (NULL != c) { /* !chunkqueue_is_empty(cq)) */
692                 pipelined_request_start = 1;
693                 /* partial header of next request has already been read,
694                  * so optimistically check for more data received on
695                  * socket while processing the previous request */
696                 con->is_readable = 1;
697                 /*(if partially read next request and unable to read any bytes,
698                  * then will unnecessarily scan again before subsequent read)*/
699             }
700         }
701     }
702 
703     do {
704         if (NULL == c) continue;
705         clen = buffer_string_length(c->mem) - c->offset;
706         if (0 == clen) continue;
707         if (c->offset > USHRT_MAX) { /*(highly unlikely)*/
708             chunkqueue_compact_mem(cq, clen);
709             c = cq->first; /*(reload c after chunkqueue_compact_mem())*/
710         }
711 
712         hoff[0] = 1;                         /* number of lines */
713         hoff[1] = (unsigned short)c->offset; /* base offset for all lines */
714         /*hoff[2] = ...;*/                   /* offset from base for 2nd line */
715 
716         header_len = http_header_parse_hoff(c->mem->ptr + c->offset,clen,hoff);
717 
718         /* casting to (unsigned short) might truncate, and the hoff[]
719          * addition might overflow, but max_request_field_size is USHRT_MAX,
720          * so failure will be detected below */
721         const uint32_t max_request_field_size=r->conf.max_request_field_size;
722         if ((header_len ? header_len : clen) > max_request_field_size
723             || hoff[0] >= sizeof(hoff)/sizeof(hoff[0])-1) {
724             log_error(r->conf.errh, __FILE__, __LINE__, "%s",
725                       "oversized request-header -> sending Status 431");
726             r->http_status = 431; /* Request Header Fields Too Large */
727             r->keep_alive = 0;
728             connection_set_state(r, CON_STATE_REQUEST_END);
729             return 1;
730         }
731 
732         if (0 != header_len) {
733             if (hoff[0] > 1) break; /* common case; request headers complete */
734 
735             if (discard_blank) { /* skip one blank line e.g. following POST */
736                 if (header_len == clen) continue;
737                 const int ch = c->mem->ptr[c->offset+header_len];
738                 if (ch != '\r' && ch != '\n') {
739                     /* discard prior blank line if next line is not blank */
740                     discard_blank = 0;
741                     clen = 0;/*(for connection_read_header_more() to return c)*/
742                     c = connection_discard_blank_line(cq, header_len);/*cold*/
743                     continue;
744                 } /*(else fall through to error out in next block)*/
745             }
746         }
747 
748         if (((unsigned char *)c->mem->ptr)[c->offset] < 32) {
749             /* expecting ASCII method beginning with alpha char
750              * or HTTP/2 pseudo-header beginning with ':' */
751             /*(TLS handshake begins with SYN 0x16 (decimal 22))*/
752             log_error(r->conf.errh, __FILE__, __LINE__, "%s",
753                       "invalid request-line -> sending Status 400");
754             r->http_status = 400; /* Bad Request */
755             r->keep_alive = 0;
756             connection_set_state(r, CON_STATE_REQUEST_END);
757             return 1;
758         }
759     } while ((c = connection_read_header_more(con, cq, c, clen)));
760 
761     if (keepalive_request_start) {
762         if (con->bytes_read > r->bytes_read_ckpt) {
763             /* update r->start_hp.tv_sec timestamp when first byte of
764              * next request is received on a keep-alive connection */
765             r->start_hp.tv_sec = log_epoch_secs;
766             if (r->conf.high_precision_timestamps)
767                 log_clock_gettime_realtime(&r->start_hp);
768         }
769         if (pipelined_request_start && c) con->read_idle_ts = log_epoch_secs;
770     }
771 
772     if (NULL == c) return 0; /* incomplete request headers */
773 
774   #ifdef __COVERITY__
775     if (buffer_string_length(c->mem) < hoff[1]) {
776         return 1;
777     }
778   #endif
779 
780     char * const hdrs = c->mem->ptr + hoff[1];
781 
782     if (con->request_count > 1) {
783         /* clear buffers which may have been kept for reporting on keep-alive,
784          * (e.g. mod_status) */
785         request_reset_ex(r);
786     }
787     /* RFC7540 3.5 HTTP/2 Connection Preface
788      * "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
789      * (Connection Preface MUST be exact match)
790      * If ALT-SVC used to advertise HTTP/2, then client might start
791      * http connection (not TLS) sending HTTP/2 connection preface.
792      * (note: intentionally checking only on initial request) */
793     else if (!con->is_ssl_sock && r->conf.h2proto
794              && hoff[0] == 2 && hoff[2] == 16
795              && hdrs[0]=='P' && hdrs[1]=='R' && hdrs[2]=='I' && hdrs[3]==' ') {
796         r->http_version = HTTP_VERSION_2;
797         return 0;
798     }
799 
800     r->rqst_header_len = header_len;
801     if (r->conf.log_request_header)
802         log_error(r->conf.errh, __FILE__, __LINE__,
803           "fd: %d request-len: %d\n%.*s", con->fd,
804           (int)header_len, (int)header_len, hdrs);
805     http_request_headers_process(r, hdrs, hoff, con->proto_default_port);
806     chunkqueue_mark_written(cq, r->rqst_header_len);
807     connection_set_state(r, CON_STATE_REQUEST_END);
808 
809     if (!con->is_ssl_sock && r->conf.h2proto && 0 == r->http_status
810         && h2_check_con_upgrade_h2c(r)) {
811         /*(Upgrade: h2c over cleartext does not have SNI; no COMP_HTTP_HOST)*/
812         r->conditional_is_valid = (1 << COMP_SERVER_SOCKET)
813                                 | (1 << COMP_HTTP_REMOTE_IP);
814         /*connection_handle_write(r, con);*//* defer write to network */
815         return 0;
816     }
817 
818     return 1;
819 }
820 
821 
822 static handler_t connection_handle_fdevent(void * const context, const int revents) {
823     connection * restrict con = context;
824     const int is_ssl_sock = con->is_ssl_sock;
825 
826     joblist_append(con);
827 
828     if (revents & ~(FDEVENT_IN | FDEVENT_OUT))
829         con->revents_err |= (revents & ~(FDEVENT_IN | FDEVENT_OUT));
830 
831     if (revents & (FDEVENT_IN | FDEVENT_OUT)) {
832         if (is_ssl_sock) /*(ssl may read and write for both reads and writes)*/
833             con->is_readable = con->is_writable = 1;
834         else {
835             if (revents & FDEVENT_IN)
836                 con->is_readable = 1;
837             if (revents & FDEVENT_OUT)
838                 con->is_writable = 1;
839         }
840     }
841 
842     return HANDLER_FINISHED;
843 }
844 
845 
846 connection *connection_accept(server *srv, server_socket *srv_socket) {
847 	int cnt;
848 	sock_addr cnt_addr;
849 	size_t cnt_len = sizeof(cnt_addr); /*(size_t intentional; not socklen_t)*/
850 
851 	/**
852 	 * check if we can still open a new connections
853 	 *
854 	 * see #1216
855 	 */
856 
857 	if (srv->conns.used >= srv->max_conns) {
858 		return NULL;
859 	}
860 
861 	cnt = fdevent_accept_listenfd(srv_socket->fd, (struct sockaddr *) &cnt_addr, &cnt_len);
862 	if (-1 == cnt) {
863 		switch (errno) {
864 		case EAGAIN:
865 #if EWOULDBLOCK != EAGAIN
866 		case EWOULDBLOCK:
867 #endif
868 		case EINTR:
869 			/* we were stopped _before_ we had a connection */
870 		case ECONNABORTED: /* this is a FreeBSD thingy */
871 			/* we were stopped _after_ we had a connection */
872 			break;
873 		case EMFILE:
874 			/* out of fds */
875 			break;
876 		default:
877 			log_perror(srv->errh, __FILE__, __LINE__, "accept failed");
878 		}
879 		return NULL;
880 	} else {
881 		if (sock_addr_get_family(&cnt_addr) != AF_UNIX) {
882 			network_accept_tcp_nagle_disable(cnt);
883 		}
884 		return connection_accepted(srv, srv_socket, &cnt_addr, cnt);
885 	}
886 }
887 
888 
889 __attribute_cold__
890 static int connection_read_cq_err(connection *con) {
891     request_st * const r = &con->request;
892   #if defined(__WIN32)
893     int lastError = WSAGetLastError();
894     switch (lastError) {
895     case EAGAIN:
896         return 0;
897     case EINTR:
898         /* we have been interrupted before we could read */
899         con->is_readable = 1;
900         return 0;
901     case ECONNRESET:
902         /* suppress logging for this error, expected for keep-alive */
903         break;
904     default:
905         log_error(r->conf.errh, __FILE__, __LINE__,
906           "connection closed - recv failed: %d", lastError);
907         break;
908     }
909   #else /* __WIN32 */
910     switch (errno) {
911     case EAGAIN:
912         return 0;
913     case EINTR:
914         /* we have been interrupted before we could read */
915         con->is_readable = 1;
916         return 0;
917     case ECONNRESET:
918         /* suppress logging for this error, expected for keep-alive */
919         break;
920     default:
921         log_perror(r->conf.errh, __FILE__, __LINE__,
922           "connection closed - read failed");
923         break;
924     }
925   #endif /* __WIN32 */
926 
927     connection_set_state_error(r, CON_STATE_ERROR);
928     return -1;
929 }
930 
931 
932 /* 0: everything ok, -1: error, -2: con closed */
933 static int connection_read_cq(connection *con, chunkqueue *cq, off_t max_bytes) {
934     ssize_t len;
935     size_t mem_len = 0;
936 
937     do {
938         /* obtain chunk memory into which to read
939          * fill previous chunk if it has a reasonable amount of space available
940          * (use mem_len=0 to obtain large buffer at least half of chunk_buf_sz)
941          */
942         chunk *ckpt = cq->last;
943         char * const mem = chunkqueue_get_memory(cq, &mem_len);
944         if (mem_len > (size_t)max_bytes) mem_len = (size_t)max_bytes;
945 
946       #if defined(__WIN32)
947         len = recv(con->fd, mem, mem_len, 0);
948       #else
949         len = read(con->fd, mem, mem_len);
950       #endif
951 
952         chunkqueue_use_memory(cq, ckpt, len > 0 ? len : 0);
953 
954         if (len != (ssize_t)mem_len) {
955             /* we got less then expected, wait for the next fd-event */
956             con->is_readable = 0;
957 
958             if (len > 0) {
959                 con->bytes_read += len;
960                 return 0;
961             }
962             else if (0 == len) /* other end close connection -> KEEP-ALIVE */
963                 return -2;     /* (pipelining) */
964             else
965                 return connection_read_cq_err(con);
966         }
967 
968         con->bytes_read += len;
969         max_bytes -= len;
970 
971         int frd;
972         mem_len = (0 == fdevent_ioctl_fionread(con->fd, S_IFSOCK, &frd))
973           ? (frd < max_bytes) ? (size_t)frd : (size_t)max_bytes
974           : 0;
975     } while (max_bytes);
976     return 0;
977 }
978 
979 
980 static int connection_write_cq(connection *con, chunkqueue *cq, off_t max_bytes) {
981     request_st * const r = &con->request;
982     return con->srv->network_backend_write(con->fd,cq,max_bytes,r->conf.errh);
983 }
984 
985 
986 static handler_t connection_handle_read_post_state(request_st * const r);
987 
988 connection *connection_accepted(server *srv, server_socket *srv_socket, sock_addr *cnt_addr, int cnt) {
989 		connection *con;
990 
991 		srv->cur_fds++;
992 
993 		/* ok, we have the connection, register it */
994 #if 0
995 		log_error(srv->errh, __FILE__, __LINE__, "accepted() %d", cnt);
996 #endif
997 		srv->con_opened++;
998 
999 		con = connections_get_new_connection(srv);
1000 
1001 		con->fd = cnt;
1002 		con->fdn = fdevent_register(srv->ev, con->fd, connection_handle_fdevent, con);
1003 		con->network_read = connection_read_cq;
1004 		con->network_write = connection_write_cq;
1005 		con->reqbody_read = connection_handle_read_post_state;
1006 
1007 		request_st * const r = &con->request;
1008 		connection_set_state(r, CON_STATE_REQUEST_START);
1009 
1010 		con->connection_start = log_epoch_secs;
1011 		con->dst_addr = *cnt_addr;
1012 		buffer_copy_string(con->dst_addr_buf, inet_ntop_cache_get_ip(srv, &(con->dst_addr)));
1013 		con->srv_socket = srv_socket;
1014 		con->is_ssl_sock = srv_socket->is_ssl;
1015 		con->proto_default_port = 80; /* "http" */
1016 
1017 		config_cond_cache_reset(r);
1018 		r->conditional_is_valid = (1 << COMP_SERVER_SOCKET)
1019 		                        | (1 << COMP_HTTP_REMOTE_IP);
1020 
1021 		if (HANDLER_GO_ON != plugins_call_handle_connection_accept(con)) {
1022 			connection_reset(con);
1023 			connection_close(con);
1024 			return NULL;
1025 		}
1026 		if (r->http_status < 0) connection_set_state(r, CON_STATE_WRITE);
1027 		return con;
1028 }
1029 
1030 
1031 __attribute_cold__
1032 __attribute_noinline__
1033 static const char *
1034 connection_get_state (request_state_t state)
1035 {
1036     switch (state) {
1037       case CON_STATE_CONNECT:        return "connect";
1038       case CON_STATE_READ:           return "read";
1039       case CON_STATE_READ_POST:      return "readpost";
1040       case CON_STATE_WRITE:          return "write";
1041       case CON_STATE_CLOSE:          return "close";
1042       case CON_STATE_ERROR:          return "error";
1043       case CON_STATE_HANDLE_REQUEST: return "handle-req";
1044       case CON_STATE_REQUEST_START:  return "req-start";
1045       case CON_STATE_REQUEST_END:    return "req-end";
1046       case CON_STATE_RESPONSE_START: return "resp-start";
1047       case CON_STATE_RESPONSE_END:   return "resp-end";
1048       default:                       return "(unknown)";
1049     }
1050 }
1051 
1052 
1053 static void connection_state_machine_h2 (request_st *h2r, connection *con);
1054 
1055 
1056 static void
1057 connection_state_machine_loop (request_st * const r, connection * const con)
1058 {
1059 	request_state_t ostate;
1060 	do {
1061 		if (r->conf.log_state_handling) {
1062 			log_error(r->conf.errh, __FILE__, __LINE__,
1063 			  "state for fd:%d id:%d %s", con->fd, r->h2id,
1064 			  connection_get_state(r->state));
1065 		}
1066 
1067 		switch ((ostate = r->state)) {
1068 		case CON_STATE_REQUEST_START: /* transient */
1069 			/*(should not be reached by HTTP/2 streams)*/
1070 			r->start_hp.tv_sec = con->read_idle_ts = log_epoch_secs;
1071 			if (r->conf.high_precision_timestamps)
1072 				log_clock_gettime_realtime(&r->start_hp);
1073 
1074 			con->request_count++;
1075 			r->loops_per_request = 0;
1076 
1077 			connection_set_state(r, CON_STATE_READ);
1078 			/* fall through */
1079 		case CON_STATE_READ:
1080 			/*(should not be reached by HTTP/2 streams)*/
1081 			if (!connection_handle_read_state(con)) {
1082 				if (r->http_version == HTTP_VERSION_2) {
1083 					connection_transition_h2(r, con);
1084 					connection_state_machine_h2(r, con);
1085 					return;
1086 				}
1087 				break;
1088 			}
1089 			/*if (r->state != CON_STATE_REQUEST_END) break;*/
1090 			/* fall through */
1091 		case CON_STATE_REQUEST_END: /* transient */
1092 			ostate = (0 == r->reqbody_length)
1093 			  ? CON_STATE_HANDLE_REQUEST
1094 			  : CON_STATE_READ_POST;
1095 			connection_set_state(r, ostate);
1096 			/* fall through */
1097 		case CON_STATE_READ_POST:
1098 		case CON_STATE_HANDLE_REQUEST:
1099 			switch (http_response_handler(r)) {
1100 			  case HANDLER_GO_ON:/*CON_STATE_RESPONSE_START occurred;transient*/
1101 			  case HANDLER_FINISHED:
1102 				break;
1103 			  case HANDLER_WAIT_FOR_EVENT:
1104 				return;
1105 			  case HANDLER_COMEBACK:
1106 				/* redo loop; will not match r->state */
1107 				ostate = CON_STATE_CONNECT;
1108 				continue;
1109 			  case HANDLER_WAIT_FOR_FD:
1110 		                connection_fdwaitqueue_append(con);
1111 				return;
1112 			  /*case HANDLER_ERROR:*/
1113 			  default:
1114 				connection_set_state_error(r, CON_STATE_ERROR);
1115 				continue;
1116 			}
1117 			/* fall through */
1118 		/*case CON_STATE_RESPONSE_START:*//*occurred;transient*/
1119 			if (r->http_version > HTTP_VERSION_1_1)
1120 				h2_send_headers(r, con);
1121 			else
1122 				http_response_write_header(r);
1123 			connection_set_state(r, CON_STATE_WRITE);
1124 			/* fall through */
1125 		case CON_STATE_WRITE:
1126 			if (connection_handle_write_state(r, con)
1127 			    != CON_STATE_RESPONSE_END)
1128 				break;
1129 			/* fall through */
1130 		case CON_STATE_RESPONSE_END: /* transient */
1131 		case CON_STATE_ERROR:        /* transient */
1132 			if (r->http_version > HTTP_VERSION_1_1 && r != &con->request)
1133 				return;
1134 			connection_handle_response_end_state(r, con);
1135 			break;
1136 		case CON_STATE_CLOSE:
1137 			/*(should not be reached by HTTP/2 streams)*/
1138 			connection_handle_close_state(con);
1139 			break;
1140 		case CON_STATE_CONNECT:
1141 			break;
1142 		default:
1143 			log_error(r->conf.errh, __FILE__, __LINE__,
1144 			  "unknown state: %d %d", con->fd, r->state);
1145 			break;
1146 		}
1147 	} while (ostate != (request_state_t)r->state);
1148 }
1149 
1150 
1151 __attribute_cold__
1152 static void
1153 connection_revents_err (request_st * const r, connection * const con)
1154 {
1155     /* defer handling FDEVENT_HUP and FDEVENT_ERR to here in order to
1156      * first attempt (in callers) to read data in kernel socket buffers */
1157     /*assert(con->revents_err & ~(FDEVENT_IN | FDEVENT_OUT));*/
1158     const int revents = (int)con->revents_err;
1159     con->revents_err = 0;
1160 
1161     if (r->state == CON_STATE_CLOSE)
1162         con->close_timeout_ts = log_epoch_secs - (HTTP_LINGER_TIMEOUT+1);
1163     else if (revents & FDEVENT_HUP)
1164         connection_set_state_error(r, CON_STATE_ERROR);
1165     else if (revents & FDEVENT_RDHUP) {
1166         int events = fdevent_fdnode_interest(con->fdn);
1167         events &= ~(FDEVENT_IN|FDEVENT_RDHUP);
1168         r->conf.stream_request_body &=
1169           ~(FDEVENT_STREAM_REQUEST_BUFMIN|FDEVENT_STREAM_REQUEST_POLLIN);
1170         r->conf.stream_request_body |= FDEVENT_STREAM_REQUEST_POLLRDHUP;
1171         con->is_readable = 1; /*(can read 0 for end-of-stream)*/
1172         if (chunkqueue_is_empty(con->read_queue)) r->keep_alive = 0;
1173         if (r->reqbody_length < -1)/*(transparent proxy mode; no more rd data)*/
1174             r->reqbody_length = r->reqbody_queue.bytes_in;
1175         if (sock_addr_get_family(&con->dst_addr) == AF_UNIX) {
1176             /* future: will getpeername() on AF_UNIX check if still connected?*/
1177             fdevent_fdnode_event_set(con->srv->ev, con->fdn, events);
1178         }
1179         else if (fdevent_is_tcp_half_closed(con->fd)) {
1180             /* Success of fdevent_is_tcp_half_closed() after FDEVENT_RDHUP
1181              * indicates TCP FIN received, but does not distinguish between
1182              * client shutdown(fd, SHUT_WR) and client close(fd).  Remove
1183              * FDEVENT_RDHUP so that we do not spin on ready event.  However,
1184              * a later TCP RST will not be detected until next write to socket.
1185              * future: might getpeername() to check for TCP RST on half-closed
1186              * sockets (without FDEVENT_RDHUP interest) when checking for write
1187              * timeouts once a second in server.c, though getpeername() on
1188              * Windows might not indicate this */
1189             r->conf.stream_request_body |= FDEVENT_STREAM_REQUEST_TCP_FIN;
1190             fdevent_fdnode_event_set(con->srv->ev, con->fdn, events);
1191         }
1192         else {
1193             /* Failure of fdevent_is_tcp_half_closed() indicates TCP RST
1194              * (or unable to tell (unsupported OS), though should not
1195              * be setting FDEVENT_RDHUP in that case) */
1196             connection_set_state_error(r, CON_STATE_ERROR);
1197         }
1198     }
1199     else if (revents & FDEVENT_ERR)  /* error, connection reset */
1200         connection_set_state_error(r, CON_STATE_ERROR);
1201     else
1202         log_error(r->conf.errh, __FILE__, __LINE__,
1203           "connection closed: poll() -> ??? %d", revents);
1204 }
1205 
1206 
1207 static void
1208 connection_set_fdevent_interest (request_st * const r, connection * const con)
1209 {
1210     if (con->fd < 0) return;
1211 
1212     if (con->revents_err && r->state != CON_STATE_ERROR) {
1213         connection_revents_err(r, con); /* resets con->revents_err = 0 */
1214         connection_state_machine(con);
1215         return;
1216         /* connection_state_machine() will end up calling back into
1217          * connection_set_fdevent_interest(), but with 0 == con->revents_err */
1218     }
1219 
1220     int n = 0;
1221     switch(r->state) {
1222       case CON_STATE_READ:
1223         n = FDEVENT_IN | FDEVENT_RDHUP;
1224         break;
1225       case CON_STATE_WRITE:
1226         if (!chunkqueue_is_empty(con->write_queue)
1227             && 0 == con->is_writable && 0 == con->traffic_limit_reached)
1228             n |= FDEVENT_OUT;
1229         __attribute_fallthrough__
1230       case CON_STATE_READ_POST:
1231         if (r->conf.stream_request_body & FDEVENT_STREAM_REQUEST_POLLIN)
1232             n |= FDEVENT_IN;
1233         if (!(r->conf.stream_request_body & FDEVENT_STREAM_REQUEST_POLLRDHUP))
1234             n |= FDEVENT_RDHUP;
1235         break;
1236       case CON_STATE_CLOSE:
1237         n = FDEVENT_IN;
1238         break;
1239       case CON_STATE_CONNECT:
1240         return;
1241       default:
1242         break;
1243     }
1244 
1245     const int events = fdevent_fdnode_interest(con->fdn);
1246     if (con->is_readable < 0) {
1247         con->is_readable = 0;
1248         n |= FDEVENT_IN;
1249     }
1250     if (con->is_writable < 0) {
1251         con->is_writable = 0;
1252         n |= FDEVENT_OUT;
1253     }
1254     if (events & FDEVENT_RDHUP)
1255         n |= FDEVENT_RDHUP;
1256 
1257     if (n == events) return;
1258 
1259     /* update timestamps when enabling interest in events */
1260     if ((n & FDEVENT_IN) && !(events & FDEVENT_IN))
1261         con->read_idle_ts = log_epoch_secs;
1262     if ((n & FDEVENT_OUT) && !(events & FDEVENT_OUT))
1263         con->write_request_ts = log_epoch_secs;
1264     fdevent_fdnode_event_set(con->srv->ev, con->fdn, n);
1265 }
1266 
1267 
1268 static void
1269 connection_state_machine_h2 (request_st * const h2r, connection * const con)
1270 {
1271     h2con * const h2c = con->h2;
1272 
1273     if (h2c->sent_goaway <= 0
1274         && (chunkqueue_is_empty(con->read_queue) || h2_parse_frames(con))
1275         && con->is_readable) {
1276         chunkqueue * const cq = con->read_queue;
1277         const off_t mark = cq->bytes_in;
1278         if (0 == con->network_read(con, cq, MAX_READ_LIMIT)) {
1279             if (mark < cq->bytes_in)
1280                 h2_parse_frames(con);
1281         }
1282         else {
1283             /* network error; do not send GOAWAY, but pretend that we did */
1284             h2c->sent_goaway = H2_E_CONNECT_ERROR; /*any error (not NO_ERROR)*/
1285             connection_set_state_error(h2r, CON_STATE_ERROR);
1286         }
1287     }
1288 
1289     /* process requests on HTTP/2 streams */
1290     int resched = 0;
1291     if (h2c->sent_goaway <= 0 && h2c->rused) {
1292         /* coarse check for write throttling
1293          * (connection.kbytes-per-second, server.kbytes-per-second)
1294          * obtain an approximate limit, not refreshed per request_st,
1295          * even though we are not calculating response HEADERS frames
1296          * or frame overhead here */
1297         off_t max_bytes = con->is_writable
1298           ? connection_write_throttle(con, MAX_WRITE_LIMIT)
1299           : 0;
1300         const off_t fsize = (off_t)h2c->s_max_frame_size;
1301 
1302         /* XXX: to avoid buffer bloat due to staging too much data in
1303          * con->write_queue, consider setting limit on how much is staged
1304          * for sending on con->write_queue: adjusting max_bytes down */
1305 
1306         /* XXX: TODO: process requests in stream priority order */
1307         for (uint32_t i = 0; i < h2c->rused; ++i) {
1308             request_st * const r = h2c->r[i];
1309             /* future: might track read/write interest per request
1310              * to avoid iterating through all active requests */
1311 
1312           #if 0
1313             const int log_state_handling = r->conf.log_state_handling;
1314             if (log_state_handling)
1315                 log_error(r->conf.errh, __FILE__, __LINE__,
1316                   "state at enter %d %d %s", con->fd, r->h2id,
1317                   connection_get_state(r->state));
1318           #endif
1319 
1320             connection_state_machine_loop(r, con);
1321 
1322             if (r->resp_header_len && !chunkqueue_is_empty(&r->write_queue)
1323                 && (r->resp_body_finished
1324                     || (r->conf.stream_response_body
1325                         & (FDEVENT_STREAM_RESPONSE
1326                           |FDEVENT_STREAM_RESPONSE_BUFMIN)))) {
1327 
1328                 chunkqueue * const cq = &r->write_queue;
1329                 off_t avail = chunkqueue_length(cq);
1330                 if (avail > max_bytes)    avail = max_bytes;
1331                 if (avail > fsize)        avail = fsize;
1332                 if (avail > r->h2_swin)   avail = r->h2_swin;
1333                 if (avail > h2r->h2_swin) avail = h2r->h2_swin;
1334 
1335                 if (avail > 0) {
1336                     max_bytes -= avail;
1337                     h2_send_cqdata(r, con, cq, (uint32_t)avail);
1338                 }
1339 
1340                 if (r->resp_body_finished && chunkqueue_is_empty(cq)) {
1341                     connection_set_state(r, CON_STATE_RESPONSE_END);
1342                     if (r->conf.log_state_handling)
1343                         connection_state_machine_loop(r, con);
1344                 }
1345                 else if (avail) /*(do not spin if swin empty window)*/
1346                     resched |= (!chunkqueue_is_empty(cq));
1347             }
1348 
1349           #if 0
1350             if (log_state_handling)
1351                 log_error(r->conf.errh, __FILE__, __LINE__,
1352                   "state at exit %d %d %s", con->fd, r->h2id,
1353                   connection_get_state(r->state));
1354           #endif
1355 
1356             if (r->state==CON_STATE_RESPONSE_END || r->state==CON_STATE_ERROR) {
1357                 /*(trigger reschedule of con if frames pending)*/
1358                 if (h2c->rused == sizeof(h2c->r)/sizeof(*h2c->r)
1359                     && !chunkqueue_is_empty(con->read_queue))
1360                     resched |= 1;
1361                 h2_send_end_stream(r, con);
1362                 h2_retire_stream(r, con);/*r invalidated;removed from h2c->r[]*/
1363                 --i;/* adjust loop i; h2c->rused was modified to retire r */
1364             }
1365         }
1366     }
1367 
1368     if (h2c->sent_goaway > 0 && h2c->rused) {
1369         /* retire streams if an error has occurred
1370          * note: this is not done to other streams in the loop above
1371          * (besides the current stream in the loop) due to the specific
1372          * implementation above, where doing so would mess up the iterator */
1373         for (uint32_t i = 0; i < h2c->rused; ++i) {
1374             request_st * const r = h2c->r[i];
1375             /*assert(r->h2state == H2_STATE_CLOSED);*/
1376             h2_retire_stream(r, con);/*r invalidated;removed from h2c->r[]*/
1377             --i;/* adjust loop i; h2c->rused was modified to retire r */
1378         }
1379         /* XXX: ? should we discard con->write_queue
1380          *        and change h2r->state to CON_STATE_RESPONSE_END ? */
1381     }
1382 
1383     if (h2r->state == CON_STATE_WRITE) {
1384         /* write HTTP/2 frames to socket */
1385         if (!chunkqueue_is_empty(con->write_queue))
1386             connection_handle_write(h2r, con);
1387 
1388         if (chunkqueue_is_empty(con->write_queue)
1389             && 0 == h2c->rused && h2c->sent_goaway)
1390             connection_set_state(h2r, CON_STATE_RESPONSE_END);
1391     }
1392 
1393     if (h2r->state == CON_STATE_WRITE) {
1394         if (resched && !con->traffic_limit_reached)
1395             joblist_append(con);
1396 
1397         if (h2_want_read(con))
1398             h2r->conf.stream_request_body |=  FDEVENT_STREAM_REQUEST_POLLIN;
1399         else
1400             h2r->conf.stream_request_body &= ~FDEVENT_STREAM_REQUEST_POLLIN;
1401     }
1402     else /* e.g. CON_STATE_RESPONSE_END or CON_STATE_ERROR */
1403         connection_state_machine_loop(h2r, con);
1404 
1405     connection_set_fdevent_interest(h2r, con);
1406 }
1407 
1408 
1409 static void
1410 connection_state_machine_h1 (request_st * const r, connection * const con)
1411 {
1412 	const int log_state_handling = r->conf.log_state_handling;
1413 	if (log_state_handling) {
1414 		log_error(r->conf.errh, __FILE__, __LINE__,
1415 		  "state at enter %d %s", con->fd, connection_get_state(r->state));
1416 	}
1417 
1418 	connection_state_machine_loop(r, con);
1419 
1420 	if (log_state_handling) {
1421 		log_error(r->conf.errh, __FILE__, __LINE__,
1422 		  "state at exit: %d %s", con->fd, connection_get_state(r->state));
1423 	}
1424 
1425 	connection_set_fdevent_interest(r, con);
1426 }
1427 
1428 
1429 void
1430 connection_state_machine (connection * const con)
1431 {
1432     request_st * const r = &con->request;
1433     if (r->http_version == HTTP_VERSION_2)
1434         connection_state_machine_h2(r, con);
1435     else /* if (r->http_version <= HTTP_VERSION_1_1) */
1436         connection_state_machine_h1(r, con);
1437 }
1438 
1439 
1440 static void connection_check_timeout (connection * const con, const time_t cur_ts) {
1441     const int waitevents = fdevent_fdnode_interest(con->fdn);
1442     int changed = 0;
1443     int t_diff;
1444 
1445     request_st * const r = &con->request;
1446     if (r->state == CON_STATE_CLOSE) {
1447         if (cur_ts - con->close_timeout_ts > HTTP_LINGER_TIMEOUT) {
1448             changed = 1;
1449         }
1450     }
1451     else if (con->h2 && r->state == CON_STATE_WRITE) {
1452         h2con * const h2c = con->h2;
1453         if (h2c->rused) {
1454             for (uint32_t i = 0; i < h2c->rused; ++i) {
1455                 request_st * const rr = h2c->r[i];
1456                 if (rr->state == CON_STATE_ERROR) { /*(should not happen)*/
1457                     changed = 1;
1458                     continue;
1459                 }
1460                 if (rr->reqbody_length != rr->reqbody_queue.bytes_in) {
1461                     /* XXX: should timeout apply if not trying to read on h2con?
1462                      * (still applying timeout to catch stuck connections) */
1463                     /* XXX: con->read_idle_ts is not per-request, so timeout
1464                      * will not occur if other read activity occurs on h2con
1465                      * (future: might keep separate timestamp per-request) */
1466                     if (cur_ts - con->read_idle_ts > rr->conf.max_read_idle) {
1467                         /* time - out */
1468                         if (rr->conf.log_request_handling) {
1469                             log_error(rr->conf.errh, __FILE__, __LINE__,
1470                               "request aborted - read timeout: %d", con->fd);
1471                         }
1472                         connection_set_state_error(r, CON_STATE_ERROR);
1473                         changed = 1;
1474                     }
1475                 }
1476 
1477                 if (rr->state != CON_STATE_READ_POST
1478                     && con->write_request_ts != 0) {
1479                     /* XXX: con->write_request_ts is not per-request, so timeout
1480                      * will not occur if other write activity occurs on h2con
1481                      * (future: might keep separate timestamp per-request) */
1482                     if (cur_ts - con->write_request_ts
1483                         > r->conf.max_write_idle) {
1484                         /*(see comment further down about max_write_idle)*/
1485                         /* time - out */
1486                         if (r->conf.log_timeouts) {
1487                             log_error(r->conf.errh, __FILE__, __LINE__,
1488                               "NOTE: a request from %.*s for %.*s timed out "
1489                               "after writing %lld bytes. We waited %d seconds. "
1490                               "If this is a problem, increase "
1491                               "server.max-write-idle",
1492                               BUFFER_INTLEN_PTR(con->dst_addr_buf),
1493                               BUFFER_INTLEN_PTR(&r->target),
1494                               (long long)r->write_queue.bytes_out,
1495                               (int)r->conf.max_write_idle);
1496                         }
1497                         connection_set_state_error(r, CON_STATE_ERROR);
1498                         changed = 1;
1499                     }
1500                 }
1501             }
1502         }
1503         else {
1504             if (cur_ts - con->read_idle_ts > con->keep_alive_idle) {
1505                 /* time - out */
1506                 if (r->conf.log_request_handling) {
1507                     log_error(r->conf.errh, __FILE__, __LINE__,
1508                               "connection closed - keep-alive timeout: %d",
1509                               con->fd);
1510                 }
1511                 connection_set_state(r, CON_STATE_RESPONSE_END);
1512                 changed = 1;
1513             }
1514         }
1515     }
1516     else if (waitevents & FDEVENT_IN) {
1517         if (con->request_count == 1 || r->state != CON_STATE_READ) {
1518             /* e.g. CON_STATE_READ_POST || CON_STATE_WRITE */
1519             if (cur_ts - con->read_idle_ts > r->conf.max_read_idle) {
1520                 /* time - out */
1521                 if (r->conf.log_request_handling) {
1522                     log_error(r->conf.errh, __FILE__, __LINE__,
1523                               "connection closed - read timeout: %d", con->fd);
1524                 }
1525 
1526                 connection_set_state_error(r, CON_STATE_ERROR);
1527                 changed = 1;
1528             }
1529         } else {
1530             if (cur_ts - con->read_idle_ts > con->keep_alive_idle) {
1531                 /* time - out */
1532                 if (r->conf.log_request_handling) {
1533                     log_error(r->conf.errh, __FILE__, __LINE__,
1534                               "connection closed - keep-alive timeout: %d",
1535                               con->fd);
1536                 }
1537 
1538                 connection_set_state_error(r, CON_STATE_ERROR);
1539                 changed = 1;
1540             }
1541         }
1542     }
1543 
1544     /* max_write_idle timeout currently functions as backend timeout,
1545      * too, after response has been started.
1546      * future: have separate backend timeout, and then change this
1547      * to check for write interest before checking for timeout */
1548     /*if (waitevents & FDEVENT_OUT)*/
1549     if (r->http_version <= HTTP_VERSION_1_1
1550         && r->state == CON_STATE_WRITE && con->write_request_ts != 0) {
1551       #if 0
1552         if (cur_ts - con->write_request_ts > 60) {
1553             log_error(r->conf.errh, __FILE__, __LINE__,
1554                       "connection closed - pre-write-request-timeout: %d %d",
1555                       con->fd, cur_ts - con->write_request_ts);
1556         }
1557       #endif
1558 
1559         if (cur_ts - con->write_request_ts > r->conf.max_write_idle) {
1560             /* time - out */
1561             if (r->conf.log_timeouts) {
1562                 log_error(r->conf.errh, __FILE__, __LINE__,
1563                   "NOTE: a request from %.*s for %.*s timed out after writing "
1564                   "%lld bytes. We waited %d seconds.  If this is a problem, "
1565                   "increase server.max-write-idle",
1566                   BUFFER_INTLEN_PTR(con->dst_addr_buf),
1567                   BUFFER_INTLEN_PTR(&r->target),
1568                   (long long)con->bytes_written, (int)r->conf.max_write_idle);
1569             }
1570             connection_set_state_error(r, CON_STATE_ERROR);
1571             changed = 1;
1572         }
1573     }
1574 
1575     /* lighttpd HTTP/2 limitation: rate limit config r->conf.bytes_per_second
1576      * (currently) taken only from top-level config (socket), with host if SNI
1577      * used, but not any other config conditions, e.g. not per-file-type */
1578 
1579     if (0 == (t_diff = cur_ts - con->connection_start)) t_diff = 1;
1580 
1581     if (con->traffic_limit_reached &&
1582         (r->conf.bytes_per_second == 0 ||
1583          con->bytes_written < (off_t)r->conf.bytes_per_second * t_diff)) {
1584         /* enable connection again */
1585         con->traffic_limit_reached = 0;
1586 
1587         changed = 1;
1588     }
1589 
1590     con->bytes_written_cur_second = 0;
1591 
1592     if (changed) {
1593         connection_state_machine(con);
1594     }
1595 }
1596 
1597 void connection_periodic_maint (server * const srv, const time_t cur_ts) {
1598     /* check all connections for timeouts */
1599     connections * const conns = &srv->conns;
1600     for (size_t ndx = 0; ndx < conns->used; ++ndx) {
1601         connection_check_timeout(conns->ptr[ndx], cur_ts);
1602     }
1603 }
1604 
1605 void connection_graceful_shutdown_maint (server *srv) {
1606     connections * const conns = &srv->conns;
1607     const int graceful_expire =
1608       (srv->graceful_expire_ts && srv->graceful_expire_ts < log_epoch_secs);
1609     for (uint32_t ndx = 0; ndx < conns->used; ++ndx) {
1610         connection * const con = conns->ptr[ndx];
1611         int changed = 0;
1612 
1613         request_st * const r = &con->request;
1614         if (r->state == CON_STATE_CLOSE) {
1615             /* reduce remaining linger timeout to be
1616              * (from zero) *up to* one more second, but no more */
1617             if (HTTP_LINGER_TIMEOUT > 1)
1618                 con->close_timeout_ts -= (HTTP_LINGER_TIMEOUT - 1);
1619             if (log_epoch_secs - con->close_timeout_ts > HTTP_LINGER_TIMEOUT)
1620                 changed = 1;
1621         }
1622         else if (con->h2 && r->state == CON_STATE_WRITE) {
1623             h2_send_goaway(con, H2_E_NO_ERROR);
1624             if (0 == con->h2->rused && chunkqueue_is_empty(con->write_queue)) {
1625                 connection_set_state(r, CON_STATE_RESPONSE_END);
1626                 changed = 1;
1627             }
1628         }
1629         else if (r->state == CON_STATE_READ && con->request_count > 1
1630                  && chunkqueue_is_empty(con->read_queue)) {
1631             /* close connections in keep-alive waiting for next request */
1632             connection_set_state_error(r, CON_STATE_ERROR);
1633             changed = 1;
1634         }
1635 
1636         if (graceful_expire) {
1637             connection_set_state_error(r, CON_STATE_ERROR);
1638             changed = 1;
1639         }
1640 
1641         r->keep_alive = 0;            /* disable keep-alive */
1642 
1643         r->conf.bytes_per_second = 0;         /* disable rate limit */
1644         r->conf.global_bytes_per_second = 0;  /* disable rate limit */
1645         if (con->traffic_limit_reached) {
1646             con->traffic_limit_reached = 0;
1647             changed = 1;
1648         }
1649 
1650         if (changed) {
1651             connection_state_machine(con);
1652         }
1653     }
1654 }
1655 
1656 
1657 static int
1658 connection_handle_read_post_cq_compact (chunkqueue * const cq)
1659 {
1660     /* combine first mem chunk with next non-empty mem chunk
1661      * (loop if next chunk is empty) */
1662     chunk *c;
1663     while (NULL != (c = cq->first) && NULL != c->next) {
1664         buffer *mem = c->next->mem;
1665         off_t offset = c->next->offset;
1666         size_t blen = buffer_string_length(mem) - (size_t)offset;
1667         force_assert(c->type == MEM_CHUNK);
1668         force_assert(c->next->type == MEM_CHUNK);
1669         buffer_append_string_len(c->mem, mem->ptr+offset, blen);
1670         c->next->offset = c->offset;
1671         c->next->mem = c->mem;
1672         c->mem = mem;
1673         c->offset = offset + (off_t)blen;
1674         chunkqueue_remove_finished_chunks(cq);
1675         if (0 != blen) return 1;
1676     }
1677     return 0;
1678 }
1679 
1680 
1681 static int
1682 connection_handle_read_post_chunked_crlf (chunkqueue * const cq)
1683 {
1684     /* caller might check chunkqueue_length(cq) >= 2 before calling here
1685      * to limit return value to either 1 for good or -1 for error */
1686     chunk *c;
1687     buffer *b;
1688     char *p;
1689     size_t len;
1690 
1691     /* caller must have called chunkqueue_remove_finished_chunks(cq), so if
1692      * chunkqueue is not empty, it contains chunk with at least one char */
1693     if (chunkqueue_is_empty(cq)) return 0;
1694 
1695     c = cq->first;
1696     b = c->mem;
1697     p = b->ptr+c->offset;
1698     if (p[0] != '\r') return -1; /* error */
1699     if (p[1] == '\n') return 1;
1700     len = buffer_string_length(b) - (size_t)c->offset;
1701     if (1 != len) return -1; /* error */
1702 
1703     while (NULL != (c = c->next)) {
1704         b = c->mem;
1705         len = buffer_string_length(b) - (size_t)c->offset;
1706         if (0 == len) continue;
1707         p = b->ptr+c->offset;
1708         return (p[0] == '\n') ? 1 : -1; /* error if not '\n' */
1709     }
1710     return 0;
1711 }
1712 
1713 
1714 static handler_t
1715 connection_handle_read_post_chunked (request_st * const r, chunkqueue * const cq, chunkqueue * const dst_cq)
1716 {
1717     /* r->conf.max_request_size is in kBytes */
1718     const off_t max_request_size = (off_t)r->conf.max_request_size << 10;
1719     off_t te_chunked = r->te_chunked;
1720     do {
1721         off_t len = chunkqueue_length(cq);
1722 
1723         while (0 == te_chunked) {
1724             char *p;
1725             chunk *c = cq->first;
1726             if (NULL == c) break;
1727             force_assert(c->type == MEM_CHUNK);
1728             p = strchr(c->mem->ptr+c->offset, '\n');
1729             if (NULL != p) { /* found HTTP chunked header line */
1730                 off_t hsz = p + 1 - (c->mem->ptr+c->offset);
1731                 unsigned char *s = (unsigned char *)c->mem->ptr+c->offset;
1732                 for (unsigned char u;(u=(unsigned char)hex2int(*s))!=0xFF;++s) {
1733                     if (te_chunked > (off_t)(1uLL<<(8*sizeof(off_t)-5))-1) {
1734                         log_error(r->conf.errh, __FILE__, __LINE__,
1735                           "chunked data size too large -> 400");
1736                         /* 400 Bad Request */
1737                         return http_response_reqbody_read_error(r, 400);
1738                     }
1739                     te_chunked <<= 4;
1740                     te_chunked |= u;
1741                 }
1742                 if (s == (unsigned char *)c->mem->ptr+c->offset) { /*(no hex)*/
1743                     log_error(r->conf.errh, __FILE__, __LINE__,
1744                       "chunked header invalid chars -> 400");
1745                     /* 400 Bad Request */
1746                     return http_response_reqbody_read_error(r, 400);
1747                 }
1748                 while (*s == ' ' || *s == '\t') ++s;
1749                 if (*s != '\r' && *s != ';') {
1750                     log_error(r->conf.errh, __FILE__, __LINE__,
1751                       "chunked header invalid chars -> 400");
1752                     /* 400 Bad Request */
1753                     return http_response_reqbody_read_error(r, 400);
1754                 }
1755 
1756                 if (hsz >= 1024) {
1757                     /* prevent theoretical integer overflow
1758                      * casting to (size_t) and adding 2 (for "\r\n") */
1759                     log_error(r->conf.errh, __FILE__, __LINE__,
1760                       "chunked header line too long -> 400");
1761                     /* 400 Bad Request */
1762                     return http_response_reqbody_read_error(r, 400);
1763                 }
1764 
1765                 if (0 == te_chunked) {
1766                     /* do not consume final chunked header until
1767                      * (optional) trailers received along with
1768                      * request-ending blank line "\r\n" */
1769                     if (p[0] == '\r' && p[1] == '\n') {
1770                         /*(common case with no trailers; final \r\n received)*/
1771                         hsz += 2;
1772                     }
1773                     else {
1774                         /* trailers or final CRLF crosses into next cq chunk */
1775                         hsz -= 2;
1776                         do {
1777                             c = cq->first;
1778                             p = strstr(c->mem->ptr+c->offset+hsz, "\r\n\r\n");
1779                         } while (NULL == p
1780                                  && connection_handle_read_post_cq_compact(cq));
1781                         if (NULL == p) {
1782                             /*(effectively doubles max request field size
1783                              * potentially received by backend, if in the future
1784                              * these trailers are added to request headers)*/
1785                             if ((off_t)buffer_string_length(c->mem) - c->offset
1786                                 < (off_t)r->conf.max_request_field_size) {
1787                                 break;
1788                             }
1789                             else {
1790                                 /* ignore excessively long trailers;
1791                                  * disable keep-alive on connection */
1792                                 r->keep_alive = 0;
1793                                 p = c->mem->ptr + buffer_string_length(c->mem)
1794                                   - 4;
1795                             }
1796                         }
1797                         hsz = p + 4 - (c->mem->ptr+c->offset);
1798                         /* trailers currently ignored, but could be processed
1799                          * here if 0 == (r->conf.stream_request_body &
1800                          *               & (FDEVENT_STREAM_REQUEST
1801                          *                 |FDEVENT_STREAM_REQUEST_BUFMIN))
1802                          * taking care to reject fields forbidden in trailers,
1803                          * making trailers available to CGI and other backends*/
1804                     }
1805                     chunkqueue_mark_written(cq, (size_t)hsz);
1806                     r->reqbody_length = dst_cq->bytes_in;
1807                     break; /* done reading HTTP chunked request body */
1808                 }
1809 
1810                 /* consume HTTP chunked header */
1811                 chunkqueue_mark_written(cq, (size_t)hsz);
1812                 len = chunkqueue_length(cq);
1813 
1814                 if (0 !=max_request_size
1815                     && (max_request_size < te_chunked
1816                      || max_request_size - te_chunked < dst_cq->bytes_in)) {
1817                     log_error(r->conf.errh, __FILE__, __LINE__,
1818                       "request-size too long: %lld -> 413",
1819                       (long long)(dst_cq->bytes_in + te_chunked));
1820                     /* 413 Payload Too Large */
1821                     return http_response_reqbody_read_error(r, 413);
1822                 }
1823 
1824                 te_chunked += 2; /*(for trailing "\r\n" after chunked data)*/
1825 
1826                 break; /* read HTTP chunked header */
1827             }
1828 
1829             /*(likely better ways to handle chunked header crossing chunkqueue
1830              * chunks, but this situation is not expected to occur frequently)*/
1831             if ((off_t)buffer_string_length(c->mem) - c->offset >= 1024) {
1832                 log_error(r->conf.errh, __FILE__, __LINE__,
1833                   "chunked header line too long -> 400");
1834                 /* 400 Bad Request */
1835                 return http_response_reqbody_read_error(r, 400);
1836             }
1837             else if (!connection_handle_read_post_cq_compact(cq)) {
1838                 break;
1839             }
1840         }
1841         if (0 == te_chunked) break;
1842 
1843         if (te_chunked > 2) {
1844             if (len > te_chunked-2) len = te_chunked-2;
1845             if (dst_cq->bytes_in + te_chunked <= 64*1024) {
1846                 /* avoid buffering request bodies <= 64k on disk */
1847                 chunkqueue_steal(dst_cq, cq, len);
1848             }
1849             else if (0 != chunkqueue_steal_with_tempfiles(dst_cq, cq, len,
1850                                                           r->conf.errh)) {
1851                 /* 500 Internal Server Error */
1852                 return http_response_reqbody_read_error(r, 500);
1853             }
1854             te_chunked -= len;
1855             len = chunkqueue_length(cq);
1856         }
1857 
1858         if (len < te_chunked) break;
1859 
1860         if (2 == te_chunked) {
1861             if (-1 == connection_handle_read_post_chunked_crlf(cq)) {
1862                 log_error(r->conf.errh, __FILE__, __LINE__,
1863                   "chunked data missing end CRLF -> 400");
1864                 /* 400 Bad Request */
1865                 return http_response_reqbody_read_error(r, 400);
1866             }
1867             chunkqueue_mark_written(cq, 2);/*consume \r\n at end of chunk data*/
1868             te_chunked -= 2;
1869         }
1870 
1871     } while (!chunkqueue_is_empty(cq));
1872 
1873     r->te_chunked = te_chunked;
1874     return HANDLER_GO_ON;
1875 }
1876 
1877 
1878 static handler_t
1879 connection_handle_read_body_unknown (request_st * const r, chunkqueue * const cq, chunkqueue * const dst_cq)
1880 {
1881     /* r->conf.max_request_size is in kBytes */
1882     const off_t max_request_size = (off_t)r->conf.max_request_size << 10;
1883     chunkqueue_append_chunkqueue(dst_cq, cq);
1884     if (0 != max_request_size && dst_cq->bytes_in > max_request_size) {
1885         log_error(r->conf.errh, __FILE__, __LINE__,
1886           "request-size too long: %lld -> 413", (long long)dst_cq->bytes_in);
1887         /* 413 Payload Too Large */
1888         return http_response_reqbody_read_error(r, 413);
1889     }
1890     return HANDLER_GO_ON;
1891 }
1892 
1893 
1894 __attribute_cold__
1895 static int
1896 connection_check_expect_100 (request_st * const r, connection * const con)
1897 {
1898     if (!con->is_writable)
1899         return 1;
1900 
1901     const buffer * const vb =
1902       http_header_request_get(r, HTTP_HEADER_EXPECT,
1903                               CONST_STR_LEN("Expect"));
1904     if (NULL == vb)
1905         return 1;
1906 
1907     /* (always unset Expect header so that check is not repeated for request */
1908     int rc = buffer_eq_icase_slen(vb, CONST_STR_LEN("100-continue"));
1909     http_header_request_unset(r, HTTP_HEADER_EXPECT,
1910                               CONST_STR_LEN("Expect"));
1911     if (!rc
1912         || 0 != r->reqbody_queue.bytes_in
1913         || !chunkqueue_is_empty(&r->read_queue)
1914         || !chunkqueue_is_empty(&r->write_queue))
1915         return 1;
1916 
1917     /* send 100 Continue only if no request body data received yet
1918      * and response has not yet started (checked above) */
1919     if (r->http_version > HTTP_VERSION_1_1)
1920         h2_send_100_continue(r, con);
1921     else if (r->http_version == HTTP_VERSION_1_1)
1922         return connection_write_100_continue(r, con);
1923 
1924     return 1;
1925 }
1926 
1927 
1928 static handler_t
1929 connection_handle_read_post_state (request_st * const r)
1930 {
1931     connection * const con = r->con;
1932     chunkqueue * const cq = &r->read_queue;
1933     chunkqueue * const dst_cq = &r->reqbody_queue;
1934 
1935     int is_closed = 0;
1936 
1937     if (r->http_version > HTTP_VERSION_1_1) {
1938         /*(H2_STATE_HALF_CLOSED_REMOTE or H2_STATE_CLOSED)*/
1939         if (r->h2state >= H2_STATE_HALF_CLOSED_REMOTE)
1940             is_closed = 1;
1941     }
1942     else if (con->is_readable) {
1943         con->read_idle_ts = log_epoch_secs;
1944 
1945         switch(con->network_read(con, cq, MAX_READ_LIMIT)) {
1946         case -1:
1947             connection_set_state_error(r, CON_STATE_ERROR);
1948             return HANDLER_ERROR;
1949         case -2:
1950             is_closed = 1;
1951             break;
1952         default:
1953             break;
1954         }
1955 
1956         chunkqueue_remove_finished_chunks(cq);
1957     }
1958 
1959     /* Check for Expect: 100-continue in request headers */
1960     if (light_btst(r->rqst_htags, HTTP_HEADER_EXPECT)
1961         && !connection_check_expect_100(r, con))
1962         return HANDLER_ERROR;
1963 
1964     if (r->http_version > HTTP_VERSION_1_1) {
1965         /* h2_recv_data() places frame payload directly into r->reqbody_queue */
1966     }
1967     else if (r->reqbody_length < 0) {
1968         /*(-1: Transfer-Encoding: chunked, -2: unspecified length)*/
1969         handler_t rc = (-1 == r->reqbody_length)
1970                      ? connection_handle_read_post_chunked(r, cq, dst_cq)
1971                      : connection_handle_read_body_unknown(r, cq, dst_cq);
1972         if (HANDLER_GO_ON != rc) return rc;
1973         chunkqueue_remove_finished_chunks(cq);
1974     }
1975     else {
1976         off_t len = (off_t)r->reqbody_length - dst_cq->bytes_in;
1977         if (r->reqbody_length <= 64*1024) {
1978             /* don't buffer request bodies <= 64k on disk */
1979             chunkqueue_steal(dst_cq, cq, len);
1980         }
1981         else if (0 !=
1982                  chunkqueue_steal_with_tempfiles(dst_cq,cq,len,r->conf.errh)) {
1983             /* writing to temp file failed */ /* Internal Server Error */
1984             return http_response_reqbody_read_error(r, 500);
1985         }
1986         chunkqueue_remove_finished_chunks(cq);
1987     }
1988 
1989     if (dst_cq->bytes_in == (off_t)r->reqbody_length) {
1990         /* Content is ready */
1991         r->conf.stream_request_body &= ~FDEVENT_STREAM_REQUEST_POLLIN;
1992         if (r->state == CON_STATE_READ_POST) {
1993             connection_set_state(r, CON_STATE_HANDLE_REQUEST);
1994         }
1995         return HANDLER_GO_ON;
1996     }
1997     else if (is_closed) {
1998       #if 0
1999         return http_response_reqbody_read_error(r, 400); /* Bad Request */
2000       #endif
2001         return HANDLER_ERROR;
2002     }
2003     else {
2004         r->conf.stream_request_body |= FDEVENT_STREAM_REQUEST_POLLIN;
2005         return (r->conf.stream_request_body & FDEVENT_STREAM_REQUEST)
2006           ? HANDLER_GO_ON
2007           : HANDLER_WAIT_FOR_EVENT;
2008     }
2009 }
2010