lighttpd 1.4.x https://www.lighttpd.net/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1988 lines
68 KiB

#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 void connection_set_state_error(request_st * const r, const request_state_t state) {
connection_set_state(r, state);
}
__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
}
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;
request_reset_ex(r); /*(r->conf.* is still valid below)*/
connection_set_state(r, CON_STATE_CONNECT);
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 = sock_addr_get_family(&con->dst_addr);
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 may not have been read completely */
r->keep_alive = 0;
/* clean up failed partial write of 1xx intermediate responses*/
if (&r->write_queue != con->write_queue) { /*(for HTTP/1.1)*/
chunkqueue_free(con->write_queue);
con->write_queue = &r->write_queue;
}
}
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_hp.tv_sec = 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_1xx_info (request_st * const r, connection * const con)
{
/* (Note: prior 1xx intermediate responses may be present in cq) */
/* (Note: also choosing not to update con->write_request_ts
* which differs from connection_write_chunkqueue()) */
chunkqueue * const cq = con->write_queue;
off_t written = cq->bytes_out;
int rc = con->network_write(con, cq, MAX_WRITE_LIMIT);
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_error(r, CON_STATE_ERROR);
return 0; /* error */
}
if (!chunkqueue_is_empty(cq)) { /* partial write (unlikely) */
con->is_writable = 0;
if (cq == &r->write_queue) {
/* 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 */
con->write_queue = chunkqueue_init(NULL);
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)
*/
r->write_queue.bytes_in = r->write_queue.bytes_out = 0;
#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);
}
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 */
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);
}
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_error(r, CON_STATE_ERROR);
break;
case -2: /* remote close */
connection_set_state_error(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) {
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_error(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_data(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_data(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_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() */
}
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_error(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;
}
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);
}
/* detect if data is added to chunk */
c = cq->first;
return (c && (size_t)c->offset + olen < buffer_string_length(c->mem))
? c
: NULL;
}
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);