2016-03-19 15:14:35 +00:00
|
|
|
#include "first.h"
|
|
|
|
|
2018-03-25 07:45:05 +00:00
|
|
|
#include "base.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
#include "buffer.h"
|
2019-02-08 06:00:48 +00:00
|
|
|
#include "burl.h" /* HTTP_PARSEOPT_HEADER_STRICT */
|
2020-08-10 23:38:40 +00:00
|
|
|
#include "chunk.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
#include "log.h"
|
|
|
|
#include "connections.h"
|
|
|
|
#include "fdevent.h"
|
2020-08-04 16:18:38 +00:00
|
|
|
#include "h2.h"
|
2018-09-09 05:50:33 +00:00
|
|
|
#include "http_header.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2020-08-10 04:13:00 +00:00
|
|
|
#include "reqpool.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
#include "request.h"
|
|
|
|
#include "response.h"
|
|
|
|
#include "network.h"
|
2005-08-08 08:22:06 +00:00
|
|
|
#include "stat_cache.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
#include "plugin.h"
|
|
|
|
|
2020-12-25 01:05:01 +00:00
|
|
|
#include "sock_addr_cache.h"
|
2005-08-09 06:42:33 +00:00
|
|
|
|
2009-10-11 14:31:42 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#ifdef HAVE_SYS_FILIO_H
|
|
|
|
# include <sys/filio.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "sys-socket.h"
|
|
|
|
|
2019-06-21 10:51:23 +00:00
|
|
|
#define HTTP_LINGER_TIMEOUT 5
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
#define connection_set_state(r, n) ((r)->state = (n))
|
2019-06-21 11:02:45 +00:00
|
|
|
|
2020-08-11 06:23:07 +00:00
|
|
|
__attribute_cold__
|
|
|
|
static void connection_set_state_error(request_st * const r, const request_state_t state) {
|
|
|
|
connection_set_state(r, state);
|
|
|
|
}
|
|
|
|
|
2019-02-04 04:27:57 +00:00
|
|
|
__attribute_cold__
|
|
|
|
static connection *connection_init(server *srv);
|
|
|
|
|
2020-07-25 01:02:08 +00:00
|
|
|
static void connection_reset(connection *con);
|
2019-02-04 04:27:57 +00:00
|
|
|
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
static connection *connections_get_new_connection(server *srv) {
|
2019-10-04 06:10:02 +00:00
|
|
|
connections * const conns = &srv->conns;
|
2005-02-20 14:27:00 +00:00
|
|
|
size_t i;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2018-10-21 15:14:08 +00:00
|
|
|
if (conns->size == conns->used) {
|
|
|
|
conns->size += srv->max_conns >= 128 ? 128 : srv->max_conns > 16 ? 16 : srv->max_conns;
|
2005-02-20 14:27:00 +00:00
|
|
|
conns->ptr = realloc(conns->ptr, sizeof(*conns->ptr) * conns->size);
|
2016-01-30 13:59:07 +00:00
|
|
|
force_assert(NULL != conns->ptr);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
for (i = conns->used; i < conns->size; i++) {
|
|
|
|
conns->ptr[i] = connection_init(srv);
|
2019-11-26 07:13:05 +00:00
|
|
|
connection_reset(conns->ptr[i]);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
conns->ptr[conns->used]->ndx = conns->used;
|
|
|
|
return conns->ptr[conns->used++];
|
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static void connection_del(server *srv, connection *con) {
|
2019-11-26 07:13:05 +00:00
|
|
|
connections * const conns = &srv->conns;
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (-1 == con->ndx) return;
|
|
|
|
uint32_t i = (uint32_t)con->ndx;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* not last element */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-12-05 04:01:41 +00:00
|
|
|
if (i != --conns->used) {
|
2019-11-26 07:13:05 +00:00
|
|
|
connection * const temp = conns->ptr[i];
|
2019-12-05 04:01:41 +00:00
|
|
|
conns->ptr[i] = conns->ptr[conns->used];
|
|
|
|
conns->ptr[conns->used] = temp;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
conns->ptr[i]->ndx = i;
|
2019-12-05 04:01:41 +00:00
|
|
|
conns->ptr[conns->used]->ndx = -1;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
con->ndx = -1;
|
|
|
|
#if 0
|
|
|
|
fprintf(stderr, "%s.%d: del: (%d)", __FILE__, __LINE__, conns->used);
|
|
|
|
for (i = 0; i < conns->used; i++) {
|
|
|
|
fprintf(stderr, "%d ", conns->ptr[i]->fd);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
2006-10-04 13:26:23 +00:00
|
|
|
#endif
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static void connection_close(connection *con) {
|
2017-05-06 05:20:17 +00:00
|
|
|
if (con->fd < 0) con->fd = -con->fd;
|
|
|
|
|
2019-11-26 07:13:05 +00:00
|
|
|
plugins_call_handle_connection_close(con);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
server * const srv = con->srv;
|
|
|
|
request_st * const r = &con->request;
|
2020-08-31 03:40:30 +00:00
|
|
|
request_reset_ex(r); /*(r->conf.* is still valid below)*/
|
2020-01-13 02:51:12 +00:00
|
|
|
connection_set_state(r, CON_STATE_CONNECT);
|
|
|
|
|
2019-02-05 02:50:53 +00:00
|
|
|
chunkqueue_reset(con->read_queue);
|
2020-01-13 02:51:12 +00:00
|
|
|
con->request_count = 0;
|
|
|
|
con->is_ssl_sock = 0;
|
2020-09-30 21:19:55 +00:00
|
|
|
con->revents_err = 0;
|
2019-02-05 02:50:53 +00:00
|
|
|
|
2019-03-01 04:58:49 +00:00
|
|
|
fdevent_fdnode_event_del(srv->ev, con->fdn);
|
2005-02-20 14:27:00 +00:00
|
|
|
fdevent_unregister(srv->ev, con->fd);
|
2019-03-01 04:58:49 +00:00
|
|
|
con->fdn = NULL;
|
2005-02-20 14:27:00 +00:00
|
|
|
#ifdef __WIN32
|
2019-11-25 06:54:08 +00:00
|
|
|
if (0 == closesocket(con->fd))
|
2005-02-20 14:27:00 +00:00
|
|
|
#else
|
2019-11-25 06:54:08 +00:00
|
|
|
if (0 == close(con->fd))
|
2005-02-20 14:27:00 +00:00
|
|
|
#endif
|
2019-11-25 06:54:08 +00:00
|
|
|
--srv->cur_fds;
|
|
|
|
else
|
2020-01-13 02:51:12 +00:00
|
|
|
log_perror(r->conf.errh, __FILE__, __LINE__,
|
2019-11-25 06:54:08 +00:00
|
|
|
"(warning) close: %d", con->fd);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->conf.log_state_handling) {
|
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
2019-11-25 06:54:08 +00:00
|
|
|
"connection closed for fd %d", con->fd);
|
2016-12-13 19:05:47 +00:00
|
|
|
}
|
2013-12-24 04:28:44 +00:00
|
|
|
con->fd = -1;
|
2017-01-19 11:11:17 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
connection_del(srv, con);
|
|
|
|
}
|
|
|
|
|
2019-12-04 06:35:27 +00:00
|
|
|
static void connection_read_for_eos_plain(connection * const con) {
|
2016-07-26 19:55:45 +00:00
|
|
|
/* we have to do the linger_on_close stuff regardless
|
2020-01-13 02:51:12 +00:00
|
|
|
* of r->keep_alive; even non-keepalive sockets
|
2020-01-06 02:34:38 +00:00
|
|
|
* may still have unread data, and closing before reading
|
2016-07-26 19:55:45 +00:00
|
|
|
* it will make the client not see all our output.
|
|
|
|
*/
|
2016-12-13 19:05:47 +00:00
|
|
|
ssize_t len;
|
2020-09-29 18:32:53 +00:00
|
|
|
const int type = sock_addr_get_family(&con->dst_addr);
|
2017-12-11 06:20:43 +00:00
|
|
|
char buf[16384];
|
2016-12-13 19:05:47 +00:00
|
|
|
do {
|
2017-12-11 06:20:43 +00:00
|
|
|
len = fdevent_socket_read_discard(con->fd, buf, sizeof(buf),
|
|
|
|
type, SOCK_STREAM);
|
2016-12-13 19:05:47 +00:00
|
|
|
} while (len > 0 || (len < 0 && errno == EINTR));
|
2016-07-26 19:55:45 +00:00
|
|
|
|
2016-12-13 19:05:47 +00:00
|
|
|
if (len < 0 && errno == EAGAIN) return;
|
|
|
|
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
|
|
|
|
if (len < 0 && errno == EWOULDBLOCK) return;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* 0 == len || (len < 0 && (errno is a non-recoverable error)) */
|
2019-12-04 06:35:27 +00:00
|
|
|
con->close_timeout_ts = log_epoch_secs - (HTTP_LINGER_TIMEOUT+1);
|
2016-12-13 19:05:47 +00:00
|
|
|
}
|
|
|
|
|
2019-12-04 06:35:27 +00:00
|
|
|
static void connection_read_for_eos_ssl(connection * const con) {
|
2019-11-25 06:54:08 +00:00
|
|
|
if (con->network_read(con, con->read_queue, MAX_READ_LIMIT) < 0)
|
2019-12-04 06:35:27 +00:00
|
|
|
con->close_timeout_ts = log_epoch_secs - (HTTP_LINGER_TIMEOUT+1);
|
2019-02-18 18:16:49 +00:00
|
|
|
chunkqueue_reset(con->read_queue);
|
|
|
|
}
|
|
|
|
|
2019-12-04 06:35:27 +00:00
|
|
|
static void connection_read_for_eos(connection * const con) {
|
2019-02-18 18:16:49 +00:00
|
|
|
!con->is_ssl_sock
|
2019-12-04 06:35:27 +00:00
|
|
|
? connection_read_for_eos_plain(con)
|
|
|
|
: connection_read_for_eos_ssl(con);
|
2019-02-18 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 04:01:41 +00:00
|
|
|
static void connection_handle_close_state(connection *con) {
|
2019-12-04 06:35:27 +00:00
|
|
|
connection_read_for_eos(con);
|
2016-07-26 19:55:45 +00:00
|
|
|
|
2019-12-04 06:35:27 +00:00
|
|
|
if (log_epoch_secs - con->close_timeout_ts > HTTP_LINGER_TIMEOUT) {
|
2019-12-05 04:01:41 +00:00
|
|
|
connection_close(con);
|
2016-07-26 19:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-04 06:35:27 +00:00
|
|
|
static void connection_handle_shutdown(connection *con) {
|
2019-11-26 07:13:05 +00:00
|
|
|
plugins_call_handle_connection_shut_wr(con);
|
2016-07-26 19:55:45 +00:00
|
|
|
|
2019-11-26 07:13:05 +00:00
|
|
|
connection_reset(con);
|
2019-12-05 04:01:41 +00:00
|
|
|
++con->srv->con_closed;
|
2016-07-26 19:55:45 +00:00
|
|
|
|
|
|
|
/* close the connection */
|
2019-02-18 18:16:49 +00:00
|
|
|
if (con->fd >= 0
|
|
|
|
&& (con->is_ssl_sock || 0 == shutdown(con->fd, SHUT_WR))) {
|
2019-12-04 06:35:27 +00:00
|
|
|
con->close_timeout_ts = log_epoch_secs;
|
2016-07-26 19:55:45 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
request_st * const r = &con->request;
|
|
|
|
connection_set_state(r, CON_STATE_CLOSE);
|
|
|
|
if (r->conf.log_state_handling) {
|
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
2019-11-25 06:54:08 +00:00
|
|
|
"shutdown for fd %d", con->fd);
|
2016-07-26 19:55:45 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-12-05 04:01:41 +00:00
|
|
|
connection_close(con);
|
2016-07-26 19:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-04 06:10:02 +00:00
|
|
|
__attribute_cold__
|
2019-11-25 06:54:08 +00:00
|
|
|
static void connection_fdwaitqueue_append(connection *con) {
|
|
|
|
connection_list_append(&con->srv->fdwaitqueue, con);
|
2019-10-04 06:10:02 +00:00
|
|
|
}
|
|
|
|
|
2020-08-04 16:18:38 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static void connection_handle_response_end_state(request_st * const r, connection * const con) {
|
2020-08-04 16:18:38 +00:00
|
|
|
if (r->http_version > HTTP_VERSION_1_1) {
|
|
|
|
h2_retire_con(r, con);
|
|
|
|
r->keep_alive = 0;
|
|
|
|
/* set a status so that mod_accesslog, mod_rrdtool hooks are called
|
|
|
|
* in plugins_call_handle_request_done() (XXX: or set to 0 to omit) */
|
|
|
|
r->http_status = 100; /* XXX: what if con->state == CON_STATE_ERROR? */
|
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
/* call request_done hook if http_status set (e.g. to log request) */
|
|
|
|
/* (even if error, connection dropped, as long as http_status is set) */
|
|
|
|
if (r->http_status) plugins_call_handle_request_done(r);
|
2016-07-26 19:55:45 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->state != CON_STATE_ERROR) ++con->srv->con_written;
|
2016-07-26 19:55:45 +00:00
|
|
|
|
2020-09-29 20:50:39 +00:00
|
|
|
if (r->reqbody_length != r->reqbody_queue.bytes_in
|
2020-01-13 02:51:12 +00:00
|
|
|
|| r->state == CON_STATE_ERROR) {
|
2020-09-18 17:24:39 +00:00
|
|
|
/* request body may not have been read completely */
|
2020-01-13 02:51:12 +00:00
|
|
|
r->keep_alive = 0;
|
2020-09-18 17:24:39 +00:00
|
|
|
/* clean up failed partial write of 1xx intermediate responses*/
|
2020-09-29 20:50:39 +00:00
|
|
|
if (&r->write_queue != con->write_queue) { /*(for HTTP/1.1)*/
|
2020-09-18 17:24:39 +00:00
|
|
|
chunkqueue_free(con->write_queue);
|
2020-09-29 20:50:39 +00:00
|
|
|
con->write_queue = &r->write_queue;
|
2020-09-18 17:24:39 +00:00
|
|
|
}
|
2016-07-26 19:55:45 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->keep_alive) {
|
2020-07-31 07:43:19 +00:00
|
|
|
request_reset(r);
|
2020-08-10 04:13:00 +00:00
|
|
|
config_reset_config(r);
|
2020-07-31 07:43:19 +00:00
|
|
|
con->is_readable = 1; /* potentially trigger optimistic read */
|
|
|
|
/*(accounting used by mod_accesslog for HTTP/1.0 and HTTP/1.1)*/
|
|
|
|
r->bytes_read_ckpt = con->bytes_read;
|
|
|
|
r->bytes_written_ckpt = con->bytes_written;
|
2016-07-26 19:55:45 +00:00
|
|
|
#if 0
|
2020-09-23 14:43:16 +00:00
|
|
|
r->start_hp.tv_sec = con->read_idle_ts = log_epoch_secs;
|
2016-07-26 19:55:45 +00:00
|
|
|
#endif
|
2020-01-13 02:51:12 +00:00
|
|
|
connection_set_state(r, CON_STATE_REQUEST_START);
|
2016-07-26 19:55:45 +00:00
|
|
|
} else {
|
2019-12-04 06:35:27 +00:00
|
|
|
connection_handle_shutdown(con);
|
2016-07-26 19:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-02 07:18:50 +00:00
|
|
|
|
|
|
|
__attribute_pure__
|
2020-08-11 05:10:57 +00:00
|
|
|
static off_t
|
2020-10-02 07:18:50 +00:00
|
|
|
connection_write_throttled (const connection * const con, off_t max_bytes)
|
2020-08-11 05:10:57 +00:00
|
|
|
{
|
2020-10-02 07:18:50 +00:00
|
|
|
const request_config * const restrict rconf = &con->request.conf;
|
|
|
|
if (0 == rconf->global_bytes_per_second && 0 == rconf->bytes_per_second)
|
|
|
|
return max_bytes;
|
|
|
|
|
|
|
|
if (rconf->global_bytes_per_second) {
|
|
|
|
off_t limit = (off_t)rconf->global_bytes_per_second
|
|
|
|
- *(rconf->global_bytes_per_second_cnt_ptr);
|
|
|
|
if (max_bytes > limit)
|
2020-08-11 05:10:57 +00:00
|
|
|
max_bytes = limit;
|
|
|
|
}
|
|
|
|
|
2020-10-02 07:18:50 +00:00
|
|
|
if (rconf->bytes_per_second) {
|
|
|
|
off_t limit = (off_t)rconf->bytes_per_second
|
2020-08-11 05:10:57 +00:00
|
|
|
- con->bytes_written_cur_second;
|
2020-10-02 07:18:50 +00:00
|
|
|
if (max_bytes > limit)
|
2020-08-11 05:10:57 +00:00
|
|
|
max_bytes = limit;
|
|
|
|
}
|
|
|
|
|
2020-10-02 07:18:50 +00:00
|
|
|
return max_bytes > 0 ? max_bytes : 0; /*(0 == reached traffic limit)*/
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static off_t
|
|
|
|
connection_write_throttle (connection * const con, off_t max_bytes)
|
|
|
|
{
|
|
|
|
/*assert(max_bytes > 0);*/
|
|
|
|
max_bytes = connection_write_throttled(con, max_bytes);
|
|
|
|
if (0 == max_bytes) con->traffic_limit_reached = 1;
|
2020-08-11 05:10:57 +00:00
|
|
|
return max_bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2020-10-02 07:19:37 +00:00
|
|
|
connection_write_chunkqueue (connection * const con, chunkqueue * const restrict cq, off_t max_bytes)
|
2020-08-11 05:10:57 +00:00
|
|
|
{
|
2020-10-02 07:19:37 +00:00
|
|
|
/*assert(!chunkqueue_is_empty(cq));*//* checked by callers */
|
|
|
|
|
2020-08-11 05:10:57 +00:00
|
|
|
con->write_request_ts = log_epoch_secs;
|
|
|
|
|
|
|
|
max_bytes = connection_write_throttle(con, max_bytes);
|
|
|
|
if (0 == max_bytes) return 1;
|
|
|
|
|
|
|
|
off_t written = cq->bytes_out;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
#ifdef TCP_CORK
|
|
|
|
int corked = 0;
|
2020-10-02 07:19:37 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* walk chunkqueue up to first FILE_CHUNK (if present)
|
|
|
|
* This may incur memory load misses for pointer chasing, but effectively
|
|
|
|
* preloads part of the chunkqueue, something which used to be a side effect
|
|
|
|
* of a previous (less efficient) version of chunkqueue_length() which
|
|
|
|
* walked the entire chunkqueue (on each and every call). The loads here
|
|
|
|
* make a measurable difference in performance in underlying call to
|
|
|
|
* con->network_write() */
|
|
|
|
if (cq->first->next) {
|
|
|
|
const chunk *c = cq->first;
|
|
|
|
while (c->type == MEM_CHUNK && NULL != (c = c->next)) ;
|
|
|
|
#ifdef TCP_CORK
|
|
|
|
/* Linux: put a cork into socket as we want to combine write() calls
|
|
|
|
* but only if we really have multiple chunks including non-MEM_CHUNK
|
|
|
|
* (or if multiple chunks and TLS), and only if TCP socket */
|
|
|
|
if (NULL != c || con->is_ssl_sock) {
|
|
|
|
const int sa_family = sock_addr_get_family(&con->srv_socket->addr);
|
|
|
|
if (sa_family == AF_INET || sa_family == AF_INET6) {
|
2020-08-11 05:10:57 +00:00
|
|
|
corked = 1;
|
|
|
|
(void)setsockopt(con->fd, IPPROTO_TCP, TCP_CORK,
|
|
|
|
&corked, sizeof(corked));
|
|
|
|
}
|
|
|
|
}
|
2020-10-02 07:19:37 +00:00
|
|
|
#endif
|
2020-08-11 05:10:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = con->network_write(con, cq, max_bytes);
|
|
|
|
if (ret >= 0) {
|
|
|
|
ret = chunkqueue_is_empty(cq) ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TCP_CORK
|
|
|
|
if (corked) {
|
|
|
|
corked = 0;
|
|
|
|
(void)setsockopt(con->fd, IPPROTO_TCP, TCP_CORK,
|
|
|
|
&corked, sizeof(corked));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
written = cq->bytes_out - written;
|
|
|
|
con->bytes_written += written;
|
|
|
|
con->bytes_written_cur_second += written;
|
|
|
|
request_st * const r = &con->request;
|
|
|
|
if (r->conf.global_bytes_per_second_cnt_ptr)
|
|
|
|
*(r->conf.global_bytes_per_second_cnt_ptr) += written;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2020-09-18 17:24:39 +00:00
|
|
|
connection_write_1xx_info (request_st * const r, connection * const con)
|
2020-08-11 05:10:57 +00:00
|
|
|
{
|
2020-09-18 17:24:39 +00:00
|
|
|
/* (Note: prior 1xx intermediate responses may be present in cq) */
|
2020-08-11 05:10:57 +00:00
|
|
|
/* (Note: also choosing not to update con->write_request_ts
|
|
|
|
* which differs from connection_write_chunkqueue()) */
|
2020-09-18 17:24:39 +00:00
|
|
|
chunkqueue * const cq = con->write_queue;
|
2020-08-11 05:10:57 +00:00
|
|
|
off_t written = cq->bytes_out;
|
|
|
|
|
2020-09-18 17:24:39 +00:00
|
|
|
int rc = con->network_write(con, cq, MAX_WRITE_LIMIT);
|
2020-08-11 05:10:57 +00:00
|
|
|
|
|
|
|
written = cq->bytes_out - written;
|
|
|
|
con->bytes_written += written;
|
|
|
|
con->bytes_written_cur_second += written;
|
|
|
|
if (r->conf.global_bytes_per_second_cnt_ptr)
|
|
|
|
*(r->conf.global_bytes_per_second_cnt_ptr) += written;
|
|
|
|
|
|
|
|
if (rc < 0) {
|
2020-08-11 06:23:07 +00:00
|
|
|
connection_set_state_error(r, CON_STATE_ERROR);
|
2020-08-11 05:10:57 +00:00
|
|
|
return 0; /* error */
|
|
|
|
}
|
|
|
|
|
2020-09-18 17:24:39 +00:00
|
|
|
if (!chunkqueue_is_empty(cq)) { /* partial write (unlikely) */
|
2020-08-11 05:10:57 +00:00
|
|
|
con->is_writable = 0;
|
2020-09-29 20:50:39 +00:00
|
|
|
if (cq == &r->write_queue) {
|
2020-09-18 17:24:39 +00:00
|
|
|
/* save partial write of 1xx in separate chunkqueue
|
|
|
|
* Note: sending of remainder of 1xx might be delayed
|
|
|
|
* until next set of response headers are sent */
|
2020-09-29 20:50:39 +00:00
|
|
|
con->write_queue = chunkqueue_init(NULL);
|
2020-09-18 17:24:39 +00:00
|
|
|
chunkqueue_append_chunkqueue(con->write_queue, cq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* XXX: accounting inconsistency
|
|
|
|
* 1xx is not currently included in r->resp_header_len,
|
|
|
|
* so mod_accesslog reporting of %b or %B (FORMAT_BYTES_OUT_NO_HEADER)
|
|
|
|
* reports all bytes out minus len of final response headers,
|
|
|
|
* but including 1xx intermediate responses. If 1xx intermediate
|
|
|
|
* responses were included in r->resp_header_len, then there are a
|
|
|
|
* few places in the code which must be adjusted to use r->resp_header_done
|
|
|
|
* instead of (0 == r->resp_header_len) as flag that final response was set
|
|
|
|
* (Doing the following would "discard" the 1xx len from bytes_out)
|
|
|
|
*/
|
2020-09-29 20:50:39 +00:00
|
|
|
r->write_queue.bytes_in = r->write_queue.bytes_out = 0;
|
2020-09-18 17:24:39 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return 1; /* success */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
connection_send_1xx (request_st * const r, connection * const con)
|
|
|
|
{
|
|
|
|
/* Make best effort to send HTTP/1.1 1xx intermediate */
|
|
|
|
/* (Note: if other modules set response headers *before* the
|
|
|
|
* handle_response_start hook, and the backends subsequently sends 1xx,
|
|
|
|
* then the response headers are sent here with 1xx and might be cleared
|
|
|
|
* by caller (http_response_parse_headers() and http_response_check_1xx()),
|
|
|
|
* instead of being sent with the final response.
|
|
|
|
* (e.g. mod_magnet setting response headers, then backend sending 103)) */
|
|
|
|
|
|
|
|
chunkqueue * const cq = con->write_queue; /*(bypass r->write_queue)*/
|
|
|
|
|
|
|
|
buffer * const b = chunkqueue_append_buffer_open(cq);
|
|
|
|
buffer_copy_string_len(b, CONST_STR_LEN("HTTP/1.1 "));
|
|
|
|
http_status_append(b, r->http_status);
|
|
|
|
for (uint32_t i = 0; i < r->resp_headers.used; ++i) {
|
|
|
|
const data_string * const ds = (data_string *)r->resp_headers.data[i];
|
|
|
|
|
|
|
|
if (buffer_string_is_empty(&ds->value)) continue;
|
|
|
|
if (buffer_string_is_empty(&ds->key)) continue;
|
|
|
|
|
|
|
|
buffer_append_string_len(b, CONST_STR_LEN("\r\n"));
|
|
|
|
buffer_append_string_buffer(b, &ds->key);
|
|
|
|
buffer_append_string_len(b, CONST_STR_LEN(": "));
|
|
|
|
buffer_append_string_buffer(b, &ds->value);
|
2020-08-11 05:10:57 +00:00
|
|
|
}
|
2020-09-18 17:24:39 +00:00
|
|
|
buffer_append_string_len(b, CONST_STR_LEN("\r\n\r\n"));
|
|
|
|
chunkqueue_append_buffer_commit(cq);
|
|
|
|
|
|
|
|
if (con->traffic_limit_reached)
|
|
|
|
return 1; /* success; send later if throttled */
|
|
|
|
|
|
|
|
return connection_write_1xx_info(r, con);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
connection_write_100_continue (request_st * const r, connection * const con)
|
|
|
|
{
|
|
|
|
/* Make best effort to send "HTTP/1.1 100 Continue" */
|
|
|
|
static const char http_100_continue[] = "HTTP/1.1 100 Continue\r\n\r\n";
|
|
|
|
|
|
|
|
if (con->traffic_limit_reached)
|
|
|
|
return 1; /* success; skip sending if throttled */
|
2020-08-11 05:10:57 +00:00
|
|
|
|
2020-09-18 17:24:39 +00:00
|
|
|
chunkqueue * const cq = con->write_queue; /*(bypass r->write_queue)*/
|
|
|
|
chunkqueue_append_mem(cq, http_100_continue, sizeof(http_100_continue)-1);
|
|
|
|
return connection_write_1xx_info(r, con);
|
2020-08-11 05:10:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-30 19:21:24 +00:00
|
|
|
static int connection_handle_write(request_st * const r, connection * const con) {
|
|
|
|
/*assert(!chunkqueue_is_empty(cq));*//* checked by callers */
|
|
|
|
|
|
|
|
if (!con->is_writable) return CON_STATE_WRITE;
|
2020-01-13 02:51:12 +00:00
|
|
|
int rc = connection_write_chunkqueue(con, con->write_queue, MAX_WRITE_LIMIT);
|
|
|
|
switch (rc) {
|
2005-02-20 14:27:00 +00:00
|
|
|
case 0:
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->resp_body_finished) {
|
|
|
|
connection_set_state(r, CON_STATE_RESPONSE_END);
|
2020-09-30 19:21:24 +00:00
|
|
|
return CON_STATE_RESPONSE_END;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case -1: /* error on our side */
|
2020-01-13 02:51:12 +00:00
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
2019-11-25 06:54:08 +00:00
|
|
|
"connection closed: write failed on fd %d", con->fd);
|
2020-08-11 06:23:07 +00:00
|
|
|
connection_set_state_error(r, CON_STATE_ERROR);
|
2020-09-30 19:21:24 +00:00
|
|
|
return CON_STATE_ERROR;
|
2005-02-20 14:27:00 +00:00
|
|
|
case -2: /* remote close */
|
2020-08-11 06:23:07 +00:00
|
|
|
connection_set_state_error(r, CON_STATE_ERROR);
|
2020-09-30 19:21:24 +00:00
|
|
|
return CON_STATE_ERROR;
|
2005-02-20 14:27:00 +00:00
|
|
|
case 1:
|
2020-08-04 16:18:38 +00:00
|
|
|
/* do not spin trying to send HTTP/2 server Connection Preface
|
|
|
|
* while waiting for TLS negotiation to complete */
|
|
|
|
if (con->write_queue->bytes_out)
|
|
|
|
con->is_writable = 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* not finished yet -> WRITE */
|
|
|
|
break;
|
|
|
|
}
|
2020-09-30 19:21:24 +00:00
|
|
|
|
|
|
|
return CON_STATE_WRITE; /*(state did not change)*/
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2020-09-30 19:21:24 +00:00
|
|
|
static int connection_handle_write_state(request_st * const r, connection * const con) {
|
2019-02-05 02:50:53 +00:00
|
|
|
do {
|
|
|
|
/* only try to write if we have something in the queue */
|
2020-09-29 20:50:39 +00:00
|
|
|
if (!chunkqueue_is_empty(&r->write_queue)) {
|
2020-09-30 19:21:24 +00:00
|
|
|
if (r->http_version <= HTTP_VERSION_1_1) {
|
|
|
|
int rc = connection_handle_write(r, con);
|
|
|
|
if (rc != CON_STATE_WRITE) return rc;
|
2019-02-05 02:50:53 +00:00
|
|
|
}
|
2020-01-13 02:51:12 +00:00
|
|
|
} else if (r->resp_body_finished) {
|
|
|
|
connection_set_state(r, CON_STATE_RESPONSE_END);
|
2020-09-30 19:21:24 +00:00
|
|
|
return CON_STATE_RESPONSE_END;
|
2019-02-05 02:50:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->handler_module && !r->resp_body_finished) {
|
|
|
|
const plugin * const p = r->handler_module;
|
|
|
|
int rc = p->handle_subrequest(r, p->data);
|
2019-12-08 01:50:42 +00:00
|
|
|
switch(rc) {
|
2019-02-05 02:50:53 +00:00
|
|
|
case HANDLER_WAIT_FOR_EVENT:
|
|
|
|
case HANDLER_FINISHED:
|
|
|
|
case HANDLER_GO_ON:
|
|
|
|
break;
|
|
|
|
case HANDLER_WAIT_FOR_FD:
|
2020-08-04 16:18:38 +00:00
|
|
|
/* (In addition to waiting for dispatch from fdwaitqueue,
|
|
|
|
* HTTP/2 connections may retry more frequently after any
|
|
|
|
* activity occurs on connection or on other streams) */
|
2019-11-25 06:54:08 +00:00
|
|
|
connection_fdwaitqueue_append(con);
|
2019-02-05 02:50:53 +00:00
|
|
|
break;
|
|
|
|
case HANDLER_COMEBACK:
|
|
|
|
default:
|
2020-01-13 02:51:12 +00:00
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
2019-11-25 06:54:08 +00:00
|
|
|
"unexpected subrequest handler ret-value: %d %d",
|
2019-12-08 01:50:42 +00:00
|
|
|
con->fd, rc);
|
2020-12-16 09:51:14 +00:00
|
|
|
__attribute_fallthrough__
|
2019-02-05 02:50:53 +00:00
|
|
|
case HANDLER_ERROR:
|
2020-08-11 06:23:07 +00:00
|
|
|
connection_set_state_error(r, CON_STATE_ERROR);
|
2020-09-30 19:21:24 +00:00
|
|
|
return CON_STATE_ERROR;
|
2019-02-05 02:50:53 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-30 19:21:24 +00:00
|
|
|
} while (r->http_version <= HTTP_VERSION_1_1
|
2020-09-29 20:50:39 +00:00
|
|
|
&& (!chunkqueue_is_empty(&r->write_queue)
|
2019-02-05 02:50:53 +00:00
|
|
|
? con->is_writable
|
2020-01-13 02:51:12 +00:00
|
|
|
: r->resp_body_finished));
|
2020-09-30 19:21:24 +00:00
|
|
|
|
|
|
|
return CON_STATE_WRITE;
|
2019-02-05 02:50:53 +00:00
|
|
|
}
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2020-07-25 01:02:08 +00:00
|
|
|
__attribute_cold__
|
|
|
|
static connection *connection_init(server *srv) {
|
|
|
|
connection * const con = calloc(1, sizeof(*con));
|
|
|
|
force_assert(NULL != con);
|
|
|
|
|
|
|
|
con->fd = 0;
|
|
|
|
con->ndx = -1;
|
|
|
|
con->bytes_written = 0;
|
|
|
|
con->bytes_read = 0;
|
|
|
|
|
|
|
|
con->dst_addr_buf = buffer_init();
|
|
|
|
con->srv = srv;
|
|
|
|
con->plugin_slots = srv->plugin_slots;
|
|
|
|
con->config_data_base = srv->config_data_base;
|
|
|
|
|
|
|
|
request_st * const r = &con->request;
|
2020-09-01 17:16:43 +00:00
|
|
|
request_init_data(r, con, srv);
|
2020-08-10 04:13:00 +00:00
|
|
|
config_reset_config(r);
|
2020-09-29 20:50:39 +00:00
|
|
|
con->write_queue = &r->write_queue;
|
|
|
|
con->read_queue = &r->read_queue;
|
2020-07-25 01:02:08 +00:00
|
|
|
|
|
|
|
/* init plugin-specific per-connection structures */
|
|
|
|
con->plugin_ctx = calloc(1, (srv->plugins.used + 1) * sizeof(void *));
|
|
|
|
force_assert(NULL != con->plugin_ctx);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return con;
|
|
|
|
}
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-07-25 01:02:08 +00:00
|
|
|
void connections_free(server *srv) {
|
|
|
|
connections * const conns = &srv->conns;
|
|
|
|
for (uint32_t i = 0; i < conns->size; ++i) {
|
|
|
|
connection *con = conns->ptr[i];
|
|
|
|
request_st * const r = &con->request;
|
|
|
|
|
|
|
|
connection_reset(con);
|
2020-09-29 20:50:39 +00:00
|
|
|
if (con->write_queue != &r->write_queue)
|
2020-07-25 01:02:08 +00:00
|
|
|
chunkqueue_free(con->write_queue);
|
2020-09-29 20:50:39 +00:00
|
|
|
if (con->read_queue != &r->read_queue)
|
2020-07-25 01:02:08 +00:00
|
|
|
chunkqueue_free(con->read_queue);
|
2020-09-01 17:16:43 +00:00
|
|
|
request_free_data(r);
|
2020-07-25 01:02:08 +00:00
|
|
|
|
2020-07-24 00:41:12 +00:00
|
|
|
free(con->plugin_ctx);
|
2020-01-13 02:51:12 +00:00
|
|
|
buffer_free(con->dst_addr_buf);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
free(con);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
free(conns->ptr);
|
2019-10-04 06:10:02 +00:00
|
|
|
conns->ptr = NULL;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-25 01:02:08 +00:00
|
|
|
static void connection_reset(connection *con) {
|
|
|
|
request_st * const r = &con->request;
|
|
|
|
request_reset(r);
|
2020-08-10 04:13:00 +00:00
|
|
|
config_reset_config(r);
|
2020-07-31 07:43:19 +00:00
|
|
|
r->bytes_read_ckpt = 0;
|
|
|
|
r->bytes_written_ckpt = 0;
|
2020-07-25 01:02:08 +00:00
|
|
|
con->is_readable = 1;
|
|
|
|
|
|
|
|
con->bytes_written = 0;
|
|
|
|
con->bytes_written_cur_second = 0;
|
|
|
|
con->bytes_read = 0;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-25 01:02:08 +00:00
|
|
|
|
2020-09-04 23:22:56 +00:00
|
|
|
__attribute_cold__
|
|
|
|
static chunk *
|
|
|
|
connection_discard_blank_line (chunkqueue * const cq, uint32_t header_len)
|
|
|
|
{
|
|
|
|
/*(separate func only to be able to mark with compiler hint as cold)*/
|
|
|
|
chunkqueue_mark_written(cq, header_len);
|
|
|
|
return cq->first; /* refresh c after chunkqueue_mark_written() */
|
2019-07-14 15:30:09 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 23:22:56 +00:00
|
|
|
|
2019-09-22 10:03:33 +00:00
|
|
|
static chunk * connection_read_header_more(connection *con, chunkqueue *cq, chunk *c, const size_t olen) {
|
2020-08-04 16:18:38 +00:00
|
|
|
/*(should not be reached by HTTP/2 streams)*/
|
|
|
|
/*if (r->http_version == HTTP_VERSION_2) return NULL;*/
|
|
|
|
/*(However, new connections over TLS may become HTTP/2 connections via ALPN
|
|
|
|
* and return from this routine with r->http_version == HTTP_VERSION_2) */
|
|
|
|
|
2019-09-22 10:03:33 +00:00
|
|
|
if ((NULL == c || NULL == c->next) && con->is_readable) {
|
|