lighttpd1.4/src/connections.c

1902 lines
65 KiB
C
Raw Normal View History

#include "first.h"
#include "base.h"
#include "buffer.h"
#include "burl.h" /* HTTP_PARSEOPT_HEADER_STRICT */
#include "chunk.h"
#include "log.h"
#include "connections.h"
#include "fdevent.h"
#include "h2.h"
#include "http_header.h"
#include "reqpool.h"
#include "request.h"
#include "response.h"
#include "network.h"
#include "stat_cache.h"
#include "plugin.h"
#include "inet_ntop_cache.h"
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#ifdef HAVE_SYS_FILIO_H
# include <sys/filio.h>
#endif
#include "sys-socket.h"
#define HTTP_LINGER_TIMEOUT 5
#define connection_set_state(r, n) ((r)->state = (n))
__attribute_cold__
static connection *connection_init(server *srv);
static void connection_reset(connection *con);
static connection *connections_get_new_connection(server *srv) {
connections * const conns = &srv->conns;
size_t i;
if (conns->size == conns->used) {
conns->size += srv->max_conns >= 128 ? 128 : srv->max_conns > 16 ? 16 : srv->max_conns;
conns->ptr = realloc(conns->ptr, sizeof(*conns->ptr) * conns->size);
force_assert(NULL != conns->ptr);
for (i = conns->used; i < conns->size; i++) {
conns->ptr[i] = connection_init(srv);
connection_reset(conns->ptr[i]);
}
}
conns->ptr[conns->used]->ndx = conns->used;
return conns->ptr[conns->used++];
}
static void connection_del(server *srv, connection *con) {
connections * const conns = &srv->conns;
if (-1 == con->ndx) return;
uint32_t i = (uint32_t)con->ndx;
/* not last element */
if (i != --conns->used) {
connection * const temp = conns->ptr[i];
conns->ptr[i] = conns->ptr[conns->used];
conns->ptr[conns->used] = temp;
conns->ptr[i]->ndx = i;
conns->ptr[conns->used]->ndx = -1;
}
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");
#endif
}
#if 0 /* DEBUG_DEV */
__attribute_cold__
static void connection_plugin_ctx_check(server * const srv, request_st * const r) {
/* 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;
if (!pd) continue;
if (NULL == r->plugin_ctx[pd->id]
&& NULL == r->con->plugin_ctx[pd->id]) continue;
log_error(r->conf.errh, __FILE__, __LINE__,
"missing cleanup in %s", p->name);
r->plugin_ctx[pd->id] = NULL;
r->con->plugin_ctx[pd->id] = NULL;
}
}
#endif
static void connection_close(connection *con) {
if (con->fd < 0) con->fd = -con->fd;
plugins_call_handle_connection_close(con);
server * const srv = con->srv;
request_st * const r = &con->request;
#if 0 /* DEBUG_DEV */
/* plugins should have cleaned themselves up */
for (uint32_t i = 0; i < srv->plugins.used; ++i) {
if (NULL != r->plugin_ctx[i] || NULL != con->plugin_ctx[i]) {
connection_plugin_ctx_check(srv, r);
break;
}
}
#endif
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);
buffer_reset(&r->target); /*(see comments in request_reset())*/
buffer_reset(&r->pathinfo); /*(see comments in request_reset())*/
chunkqueue_reset(con->read_queue);
con->request_count = 0;
con->is_ssl_sock = 0;
fdevent_fdnode_event_del(srv->ev, con->fdn);
fdevent_unregister(srv->ev, con->fd);
con->fdn = NULL;
#ifdef __WIN32
if (0 == closesocket(con->fd))
#else
if (0 == close(con->fd))
#endif
--srv->cur_fds;
else
log_perror(r->conf.errh, __FILE__, __LINE__,
"(warning) close: %d", con->fd);
if (r->conf.log_state_handling) {
log_error(r->conf.errh, __FILE__, __LINE__,
"connection closed for fd %d", con->fd);
}
con->fd = -1;
connection_del(srv, con);
}
static void connection_read_for_eos_plain(connection * const con) {
/* we have to do the linger_on_close stuff regardless
* of r->keep_alive; even non-keepalive sockets
* may still have unread data, and closing before reading
* it will make the client not see all our output.
*/
ssize_t len;
const int type = con->dst_addr.plain.sa_family;
char buf[16384];
do {
len = fdevent_socket_read_discard(con->fd, buf, sizeof(buf),
type, SOCK_STREAM);
} while (len > 0 || (len < 0 && errno == EINTR));
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)) */
con->close_timeout_ts = log_epoch_secs - (HTTP_LINGER_TIMEOUT+1);
}
static void connection_read_for_eos_ssl(connection * const con) {
if (con->network_read(con, con->read_queue, MAX_READ_LIMIT) < 0)
con->close_timeout_ts = log_epoch_secs - (HTTP_LINGER_TIMEOUT+1);
chunkqueue_reset(con->read_queue);
}
static void connection_read_for_eos(connection * const con) {
!con->is_ssl_sock
? connection_read_for_eos_plain(con)
: connection_read_for_eos_ssl(con);
}
static void connection_handle_close_state(connection *con) {
connection_read_for_eos(con);
if (log_epoch_secs - con->close_timeout_ts > HTTP_LINGER_TIMEOUT) {
connection_close(con);
}
}
static void connection_handle_shutdown(connection *con) {
plugins_call_handle_connection_shut_wr(con);
connection_reset(con);
++con->srv->con_closed;
/* close the connection */
if (con->fd >= 0
&& (con->is_ssl_sock || 0 == shutdown(con->fd, SHUT_WR))) {
con->close_timeout_ts = log_epoch_secs;
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__,
"shutdown for fd %d", con->fd);
}
} else {
connection_close(con);
}
}
__attribute_cold__
static void connection_fdwaitqueue_append(connection *con) {
connection_list_append(&con->srv->fdwaitqueue, con);
}
static void connection_handle_response_end_state(request_st * const r, connection * const con) {
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? */
}
/* 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);
if (r->state != CON_STATE_ERROR) ++con->srv->con_written;
if (r->reqbody_length != r->reqbody_queue->bytes_in
|| r->state == CON_STATE_ERROR) {
/* request body is present and has not been read completely */
r->keep_alive = 0;
}
if (r->keep_alive) {
request_reset(r);
config_reset_config(r);
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;
#if 0
r->start_ts = con->read_idle_ts = log_epoch_secs;
#endif
connection_set_state(r, CON_STATE_REQUEST_START);
} else {
connection_handle_shutdown(con);
}
}
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" */
}
static void connection_handle_write(request_st * const r, connection * const con) {
int rc = connection_write_chunkqueue(con, con->write_queue, MAX_WRITE_LIMIT);
switch (rc) {
case 0:
if (r->resp_body_finished) {
connection_set_state(r, CON_STATE_RESPONSE_END);
}
break;
case -1: /* error on our side */
log_error(r->conf.errh, __FILE__, __LINE__,
"connection closed: write failed on fd %d", con->fd);
connection_set_state(r, CON_STATE_ERROR);
break;
case -2: /* remote close */
connection_set_state(r, CON_STATE_ERROR);
break;
case 1:
/* 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;
/* not finished yet -> WRITE */
break;
}
}
static void connection_handle_write_state(request_st * const r, connection * const con) {
do {
/* only try to write if we have something in the queue */
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);
if (r->state != CON_STATE_WRITE) break;
}
} else if (r->resp_body_finished) {
connection_set_state(r, CON_STATE_RESPONSE_END);
break;
}
if (r->handler_module && !r->resp_body_finished) {
const plugin * const p = r->handler_module;
int rc = p->handle_subrequest(r, p->data);
switch(rc) {
case HANDLER_WAIT_FOR_EVENT:
case HANDLER_FINISHED:
case HANDLER_GO_ON:
break;
case HANDLER_WAIT_FOR_FD:
/* (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) */
connection_fdwaitqueue_append(con);
break;
case HANDLER_COMEBACK:
default:
log_error(r->conf.errh, __FILE__, __LINE__,
"unexpected subrequest handler ret-value: %d %d",
con->fd, rc);
/* fall through */
case HANDLER_ERROR:
connection_set_state(r, CON_STATE_ERROR);
break;
}
}
} while (r->state == CON_STATE_WRITE
&& r->http_version <= HTTP_VERSION_1_1
&& (!chunkqueue_is_empty(r->write_queue)
? con->is_writable
: r->resp_body_finished));
}
__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);
config_reset_config(r);
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);
return con;
}
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);
free(con->plugin_ctx);
buffer_free(con->dst_addr_buf);
free(con);
}
free(conns->ptr);
conns->ptr = NULL;
}
static void connection_reset(connection *con) {
request_st * const r = &con->request;
request_reset(r);
config_reset_config(r);
r->bytes_read_ckpt = 0;
r->bytes_written_ckpt = 0;
con->is_readable = 1;
con->bytes_written = 0;
con->bytes_written_cur_second = 0;
con->bytes_read = 0;
}
__attribute_noinline__
static void connection_discard_blank_line(request_st * const r, const char * const s, unsigned short * const hoff) {
if ((s[0] == '\r' && s[1] == '\n')
|| (s[0] == '\n'
&& !(r->conf.http_parseopts & HTTP_PARSEOPT_HEADER_STRICT))) {
hoff[2] += hoff[1];
memmove(hoff+1, hoff+2, (--hoff[0] - 1) * sizeof(unsigned short));
}
}
static chunk * connection_read_header_more(connection *con, chunkqueue *cq, chunk *c, const size_t olen) {
/*(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) */
if ((NULL == c || NULL == c->next) && con->is_readable) {
con->read_idle_ts = log_epoch_secs;
if (0 != con->network_read(con, cq, MAX_READ_LIMIT)) {
request_st * const r = &con->request;
connection_set_state(r, CON_STATE_ERROR);
}
/* 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
}
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
/* detect if data is added to chunk */
c = cq->first;
return (c && (size_t)c->offset + olen < buffer_string_length(c->mem))
? c
: NULL;
2019-07-14 12:04:15 +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);
}
/**
* handle request header read
*
* we get called by the state-engine and by the fdevent-handler
*/
__attribute_noinline__
static int connection_handle_read_state(connection * const con) {
/*(should not be reached by HTTP/2 streams)*/
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;
int keepalive_request_start = 0;
int pipelined_request_start = 0;
unsigned short hoff[8192]; /* max num header lines + 3; 16k on stack */
if (con->request_count > 1 && con->bytes_read == r->bytes_read_ckpt) {
keepalive_request_start = 1;
if (NULL != c) { /* !chunkqueue_is_empty(cq)) */
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())*/
}
}
do {
if (NULL == c) continue;
clen = buffer_string_length(c->mem) - c->offset;
if (0 == clen) continue;
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 */