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"
|
|
|
|
|
2005-08-09 06:42:33 +00:00
|
|
|
#include "inet_ntop_cache.h"
|
|
|
|
|
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
|
|
|
|
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-07-24 00:41:12 +00:00
|
|
|
#if 0 /* DEBUG_DEV */
|
2019-10-19 06:21:46 +00:00
|
|
|
__attribute_cold__
|
2020-01-13 02:51:12 +00:00
|
|
|
static void connection_plugin_ctx_check(server * const srv, request_st * const r) {
|
2019-10-19 06:21:46 +00:00
|
|
|
/* plugins should have cleaned themselves up */
|
|
|
|
for (uint32_t i = 0; i < srv->plugins.used; ++i) {
|
|
|
|
plugin *p = ((plugin **)(srv->plugins.ptr))[i];
|
2019-10-22 02:49:59 +00:00
|
|
|
plugin_data_base *pd = p->data;
|
2020-07-24 00:41:12 +00:00
|
|
|
if (!pd) continue;
|
|
|
|
if (NULL == r->plugin_ctx[pd->id]
|
|
|
|
&& NULL == r->con->plugin_ctx[pd->id]) continue;
|
2020-01-13 02:51:12 +00:00
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
2019-11-25 06:54:08 +00:00
|
|
|
"missing cleanup in %s", p->name);
|
2020-01-13 02:51:12 +00:00
|
|
|
r->plugin_ctx[pd->id] = NULL;
|
2020-07-24 00:41:12 +00:00
|
|
|
r->con->plugin_ctx[pd->id] = NULL;
|
2019-10-19 06:21:46 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-24 00:41:12 +00:00
|
|
|
#endif
|
2019-10-19 06:21:46 +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-07-24 00:41:12 +00:00
|
|
|
#if 0 /* DEBUG_DEV */
|
2020-01-13 02:51:12 +00:00
|
|
|
/* plugins should have cleaned themselves up */
|
|
|
|
for (uint32_t i = 0; i < srv->plugins.used; ++i) {
|
2020-07-24 00:41:12 +00:00
|
|
|
if (NULL != r->plugin_ctx[i] || NULL != con->plugin_ctx[i]) {
|
2020-01-13 02:51:12 +00:00
|
|
|
connection_plugin_ctx_check(srv, r);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-07-24 00:41:12 +00:00
|
|
|
#endif
|
2020-01-13 02:51:12 +00:00
|
|
|
|
|
|
|
connection_set_state(r, CON_STATE_CONNECT);
|
|
|
|
buffer_clear(&r->uri.authority);
|
|
|
|
buffer_reset(&r->uri.path);
|
|
|
|
buffer_reset(&r->uri.query);
|
|
|
|
buffer_reset(&r->target_orig);
|
2020-08-04 16:18:38 +00:00
|
|
|
buffer_reset(&r->target); /*(see comments in request_reset())*/
|
|
|
|
buffer_reset(&r->pathinfo); /*(see comments in request_reset())*/
|
2020-01-13 02:51:12 +00:00
|
|
|
|
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;
|
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;
|
2017-12-11 06:20:43 +00:00
|
|
|
const int type = con->dst_addr.plain.sa_family;
|
|
|
|
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-01-13 02:51:12 +00:00
|
|
|
if (r->reqbody_length != r->reqbody_queue->bytes_in
|
|
|
|
|| r->state == CON_STATE_ERROR) {
|
2016-07-26 19:55:45 +00:00
|
|
|
/* request body is present and has not been read completely */
|
2020-01-13 02:51:12 +00:00
|
|
|
r->keep_alive = 0;
|
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-01-13 02:51:12 +00:00
|
|
|
r->start_ts = 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-08-11 05:10:57 +00:00
|
|
|
static off_t
|
|
|
|
connection_write_throttle (connection * const con, off_t max_bytes)
|
|
|
|
{
|
|
|
|
request_st * const r = &con->request;
|
|
|
|
if (r->conf.global_bytes_per_second) {
|
|
|
|
off_t limit = (off_t)r->conf.global_bytes_per_second
|
|
|
|
- *(r->conf.global_bytes_per_second_cnt_ptr);
|
|
|
|
if (limit <= 0) {
|
|
|
|
/* we reached the global traffic limit */
|
|
|
|
con->traffic_limit_reached = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (max_bytes > limit)
|
|
|
|
max_bytes = limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r->conf.bytes_per_second) {
|
|
|
|
off_t limit = (off_t)r->conf.bytes_per_second
|
|
|
|
- con->bytes_written_cur_second;
|
|
|
|
if (limit <= 0) {
|
|
|
|
/* we reached the traffic limit */
|
|
|
|
con->traffic_limit_reached = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (max_bytes > limit)
|
|
|
|
max_bytes = limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
return max_bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
connection_write_chunkqueue (connection * const con, chunkqueue * const cq, off_t max_bytes)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
/* 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,
|
|
|
|
* and only if TCP socket
|
|
|
|
*/
|
|
|
|
int corked = 0;
|
|
|
|
if (cq->first && cq->first->next) {
|
|
|
|
const int sa_family = sock_addr_get_family(&con->srv_socket->addr);
|
|
|
|
if (sa_family == AF_INET || sa_family == AF_INET6) {
|
|
|
|
chunk *c = cq->first;
|
|
|
|
while (c->type == MEM_CHUNK && NULL != (c = c->next)) ;
|
|
|
|
if (NULL != c) {
|
|
|
|
corked = 1;
|
|
|
|
(void)setsockopt(con->fd, IPPROTO_TCP, TCP_CORK,
|
|
|
|
&corked, sizeof(corked));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
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
|
|
|
|
connection_write_100_continue (request_st * const r, connection * const con)
|
|
|
|
{
|
|
|
|
/* Make best effort to send all or none of "HTTP/1.1 100 Continue" */
|
|
|
|
/* (Note: also choosing not to update con->write_request_ts
|
|
|
|
* which differs from connection_write_chunkqueue()) */
|
|
|
|
static const char http_100_continue[] = "HTTP/1.1 100 Continue\r\n\r\n";
|
|
|
|
|
|
|
|
off_t max_bytes =
|
|
|
|
connection_write_throttle(con, sizeof(http_100_continue)-1);
|
|
|
|
if (max_bytes < (off_t)sizeof(http_100_continue)-1) {
|
|
|
|
return 1; /* success; skip sending if throttled to partial */
|
|
|
|
}
|
|
|
|
|
|
|
|
chunkqueue * const cq = r->write_queue;
|
|
|
|
off_t written = cq->bytes_out;
|
|
|
|
|
|
|
|
chunkqueue_append_mem(cq,http_100_continue,sizeof(http_100_continue)-1);
|
|
|
|
int rc = con->network_write(con, cq, sizeof(http_100_continue)-1);
|
|
|
|
|
|
|
|
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) {
|
|
|
|
connection_set_state(r, CON_STATE_ERROR);
|
|
|
|
return 0; /* error */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 == written) {
|
|
|
|
/* skip sending 100 Continue if send would block */
|
|
|
|
chunkqueue_mark_written(cq, sizeof(http_100_continue)-1);
|
|
|
|
con->is_writable = 0;
|
|
|
|
}
|
|
|
|
/* else partial write (unlikely), which can cause corrupt
|
|
|
|
* response if response is later cleared, e.g. sending errdoc.
|
|
|
|
* However, situation of partial write can occur here only on
|
|
|
|
* keep-alive request where client has sent pipelined request,
|
|
|
|
* and more than 0 chars were written, but fewer than 25 chars */
|
|
|
|
|
|
|
|
return 1; /* success; sent all or none of "HTTP/1.1 100 Continue" */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-04 16:18:38 +00:00
|
|
|
static void connection_handle_write(request_st * const r, connection * const con) {
|
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);
|
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-01-13 02:51:12 +00:00
|
|
|
connection_set_state(r, CON_STATE_ERROR);
|
2005-02-20 14:27:00 +00:00
|
|
|
break;
|
|
|
|
case -2: /* remote close */
|
2020-01-13 02:51:12 +00:00
|
|
|
connection_set_state(r, CON_STATE_ERROR);
|
2005-02-20 14:27:00 +00:00
|
|
|
break;
|
|
|
|
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-01-13 02:51:12 +00:00
|
|
|
static void 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-08-04 16:18:38 +00:00
|
|
|
if (!chunkqueue_is_empty(r->write_queue)) {
|
|
|
|
if (r->http_version <= HTTP_VERSION_1_1 && con->is_writable) {
|
|
|
|
/*(r->write_queue == con->write_queue)*//*(not HTTP/2 stream)*/
|
|
|
|
connection_handle_write(r, con);
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->state != CON_STATE_WRITE) break;
|
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);
|
2019-02-05 02:50:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
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);
|
2019-02-05 02:50:53 +00:00
|
|
|
/* fall through */
|
|
|
|
case HANDLER_ERROR:
|
2020-01-13 02:51:12 +00:00
|
|
|
connection_set_state(r, CON_STATE_ERROR);
|
2019-02-05 02:50:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-01-13 02:51:12 +00:00
|
|
|
} while (r->state == CON_STATE_WRITE
|
2020-08-04 16:18:38 +00:00
|
|
|
&& r->http_version <= HTTP_VERSION_1_1
|
|
|
|
&& (!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));
|
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;
|
|
|
|
request_init(r, con, srv);
|
2020-08-10 04:13:00 +00:00
|
|
|
config_reset_config(r);
|
2020-07-25 01:02:08 +00:00
|
|
|
con->write_queue = r->write_queue;
|
|
|
|
con->read_queue = r->read_queue;
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
if (con->write_queue != r->write_queue)
|
|
|
|
chunkqueue_free(con->write_queue);
|
|
|
|
if (con->read_queue != r->read_queue)
|
|
|
|
chunkqueue_free(con->read_queue);
|
|
|
|
request_free(r);
|
|
|
|
|
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
|
|
|
|
2019-07-14 15:30:09 +00:00
|
|
|
__attribute_noinline__
|
2020-01-13 02:51:12 +00:00
|
|
|
static void connection_discard_blank_line(request_st * const r, const char * const s, unsigned short * const hoff) {
|
2019-07-14 15:30:09 +00:00
|
|
|
if ((s[0] == '\r' && s[1] == '\n')
|
|
|
|
|| (s[0] == '\n'
|
2020-01-13 02:51:12 +00:00
|
|
|
&& !(r->conf.http_parseopts & HTTP_PARSEOPT_HEADER_STRICT))) {
|
2019-07-14 15:30:09 +00:00
|
|
|
hoff[2] += hoff[1];
|
|
|
|
memmove(hoff+1, hoff+2, (--hoff[0] - 1) * sizeof(unsigned short));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2019-12-04 06:35:27 +00:00
|
|
|
con->read_idle_ts = log_epoch_secs;
|
2020-01-13 02:51:12 +00:00
|
|
|
if (0 != con->network_read(con, cq, MAX_READ_LIMIT)) {
|
|
|
|
request_st * const r = &con->request;
|
|
|
|
connection_set_state(r, CON_STATE_ERROR);
|
|
|
|
}
|
2020-08-04 16:18:38 +00:00
|
|
|
/* check if switched to HTTP/2 (ALPN "h2" during TLS negotiation) */
|
|
|
|
request_st * const r = &con->request;
|
|
|
|
if (r->http_version == HTTP_VERSION_2) return NULL;
|
2019-07-14 12:04:15 +00:00
|
|
|
}
|
|
|
|
|
2019-09-22 10:03:33 +00:00
|
|
|
if (cq->first != cq->last && 0 != olen) {
|
|
|
|
const size_t clen = chunkqueue_length(cq);
|
|
|
|
size_t block = (olen + (16384-1)) & (16384-1);
|
|
|
|
block += (block - olen > 1024 ? 0 : 16384);
|
|
|
|
chunkqueue_compact_mem(cq, block > clen ? clen : block);
|
|
|
|
}
|
2019-07-14 12:04:15 +00:00
|
|
|
|
2019-09-22 10:03:33 +00:00
|
|
|
/* detect if data is added to chunk */
|
|
|
|
c = cq->first;
|
2019-10-04 07:08:17 +00:00
|
|
|
return (c && (size_t)c->offset + olen < buffer_string_length(c->mem))
|
|
|
|
? c
|
|
|
|
: NULL;
|
2019-07-14 12:04:15 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 04:12:37 +00:00
|
|
|
|
2020-08-04 16:18:38 +00:00
|
|
|
static void
|
|
|
|
connection_transition_h2 (request_st * const h2r, connection * const con)
|
|
|
|
{
|
|
|
|
buffer_copy_string_len(&h2r->target, CONST_STR_LEN("*"));
|
|
|
|
buffer_copy_string_len(&h2r->target_orig, CONST_STR_LEN("*"));
|
|
|
|
buffer_copy_string_len(&h2r->uri.path, CONST_STR_LEN("*"));
|
|
|
|
h2r->http_method = HTTP_METHOD_PRI;
|
|
|
|
h2r->reqbody_length = -1; /*(unnecessary for h2r?)*/
|
|
|
|
h2r->conf.stream_request_body |= FDEVENT_STREAM_REQUEST_POLLIN;
|
|
|
|
|
|
|
|
/* (h2r->state == CON_STATE_READ) for transition by ALPN
|
|
|
|
* or starting cleartext HTTP/2 with Prior Knowledge
|
|
|
|
* (e.g. via HTTP Alternative Services)
|
|
|
|
* (h2r->state == CON_STATE_RESPONSE_END) for Upgrade: h2c */
|
|
|
|
|
|
|
|
if (h2r->state != CON_STATE_ERROR)
|
|
|
|
connection_set_state(h2r, CON_STATE_WRITE);
|
|
|
|
|
|
|
|
#if 0 /* ... if it turns out we need a separate fdevent handler for HTTP/2 */
|
|
|
|
con->fdn->handler = connection_handle_fdevent_h2;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (NULL == con->h2) /*(not yet transitioned to HTTP/2; not Upgrade: h2c)*/
|
|
|
|
h2_init_con(h2r, con, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-23 05:32:34 +00:00
|
|
|
/**
|
|
|
|
* handle request header read
|
|
|
|
*
|
|
|
|
* we get called by the state-engine and by the fdevent-handler
|
|
|
|
*/
|
2020-08-04 16:18:38 +00:00
|
|
|
__attribute_noinline__
|
2019-12-05 04:01:41 +00:00
|
|
|
static int connection_handle_read_state(connection * const con) {
|
2020-08-04 16:18:38 +00:00
|
|
|
/*(should not be reached by HTTP/2 streams)*/
|
2020-01-13 02:51:12 +00:00
|
|
|
chunkqueue * const cq = con->read_queue;
|
|
|
|
chunk *c = cq->first;
|
|
|
|
uint32_t clen = 0;
|
|
|
|
uint32_t header_len = 0;
|
|
|
|
request_st * const r = &con->request;
|
2019-09-23 05:32:34 +00:00
|
|
|
int keepalive_request_start = 0;
|
|
|
|
int pipelined_request_start = 0;
|
2020-01-13 02:51:12 +00:00
|
|
|
unsigned short hoff[8192]; /* max num header lines + 3; 16k on stack */
|
2019-09-23 05:32:34 +00:00
|
|
|
|
2020-07-31 07:43:19 +00:00
|
|
|
if (con->request_count > 1 && con->bytes_read == r->bytes_read_ckpt) {
|
2019-09-23 05:32:34 +00:00
|
|
|
keepalive_request_start = 1;
|
2020-01-13 02:51:12 +00:00
|
|
|
if (NULL != c) { /* !chunkqueue_is_empty(cq)) */
|
2019-09-23 05:32:34 +00:00
|
|
|
pipelined_request_start = 1;
|
|
|
|
/* partial header of next request has already been read,
|
|
|
|
* so optimistically check for more data received on
|
|
|
|
* socket while processing the previous request */
|
|
|
|
con->is_readable = 1;
|
|
|
|
/*(if partially read next request and unable to read() any bytes,
|
|
|
|
* then will unnecessarily scan again before subsequent read())*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-22 10:03:33 +00:00
|
|
|
do {
|
|
|
|
if (NULL == c) continue;
|
2019-07-14 15:30:09 +00:00
|
|
|
clen = buffer_string_length(c->mem) - c->offset;
|
2019-02-06 04:37:10 +00:00
|
|
|
if (0 == clen) continue;
|
2019-09-22 10:03:33 +00:00
|
|
|
if (c->offset > USHRT_MAX) /*(highly unlikely)*/
|
|
|
|
chunkqueue_compact_mem(cq, clen);
|
|
|
|
|
|
|
|
hoff[0] = 1; /* number of lines */
|
|
|
|
hoff[1] = (unsigned short)c->offset; /* base offset for all lines */
|
|
|
|
/*hoff[2] = ...;*/ /* offset from base for 2nd line */
|
|