lighttpd1.4/src/connections.c

1515 lines
47 KiB
C
Raw Normal View History

#include "first.h"
#include "base.h"
#include "buffer.h"
#include "burl.h" /* HTTP_PARSEOPT_HEADER_STRICT */
2019-12-10 04:53:29 +00:00
#include "settings.h" /* BUFFER_MAX_REUSE_SIZE MAX_READ_LIMIT MAX_WRITE_LIMIT */
#include "log.h"
#include "connections.h"
#include "fdevent.h"
#include "http_header.h"
#include "request.h"
#include "response.h"
#include "network.h"
#include "http_chunk.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(con, n) ((con)->request.state = (n))
__attribute_cold__
static connection *connection_init(server *srv);
static int 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 int connection_del(server *srv, connection *con) {
if (-1 == con->ndx) return -1;
buffer_clear(con->uri.authority);
buffer_reset(con->uri.path);
buffer_reset(con->uri.query);
buffer_reset(con->request.target_orig);
connections * const conns = &srv->conns;
uint32_t i = 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
return 0;
}
__attribute_cold__
static void connection_plugin_ctx_check(server *srv, connection *con) {
/* 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 || NULL == con->request.plugin_ctx[pd->id]) continue;
log_error(con->conf.errh, __FILE__, __LINE__,
"missing cleanup in %s", p->name);
con->request.plugin_ctx[pd->id] = NULL;
}
}
static int connection_close(connection *con) {
if (con->fd < 0) con->fd = -con->fd;
plugins_call_handle_connection_close(con);
con->request_count = 0;
chunkqueue_reset(con->read_queue);
server * const srv = con->srv;
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(con->conf.errh, __FILE__, __LINE__,
"(warning) close: %d", con->fd);
if (con->conf.log_state_handling) {
log_error(con->conf.errh, __FILE__, __LINE__,
"connection closed for fd %d", con->fd);
}
con->fd = -1;
con->is_ssl_sock = 0;
/* plugins should have cleaned themselves up */
for (uint32_t i = 0; i < srv->plugins.used; ++i) {
if (NULL != con->request.plugin_ctx[i]) {
connection_plugin_ctx_check(srv, con);
break;
}
}
connection_del(srv, con);
connection_set_state(con, CON_STATE_CONNECT);
return 0;
}
static void connection_read_for_eos_plain(connection * const con) {
/* we have to do the linger_on_close stuff regardless
* of con->request.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;
connection_set_state(con, CON_STATE_CLOSE);
if (con->conf.log_state_handling) {
log_error(con->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(connection *con) {
/* log the request */
/* (even if error, connection dropped, still write to access log if http_status) */
if (con->http_status) {
plugins_call_handle_request_done(con);
}
if (con->request.state != CON_STATE_ERROR) ++con->srv->con_written;
if (con->request.reqbody_length != con->request.reqbody_queue->bytes_in
|| con->request.state == CON_STATE_ERROR) {
/* request body is present and has not been read completely */
con->request.keep_alive = 0;
}
if (con->request.keep_alive) {
connection_reset(con);
#if 0
con->request.start_ts = con->read_idle_ts = log_epoch_secs;
#endif
connection_set_state(con, CON_STATE_REQUEST_START);
} else {
connection_handle_shutdown(con);
}
}
__attribute_cold__
static void connection_handle_errdoc_init(connection *con) {
/* modules that produce headers required with error response should
* typically also produce an error document. Make an exception for
* mod_auth WWW-Authenticate response header. */
buffer *www_auth = NULL;
if (401 == con->http_status) {
const buffer *vb = http_header_response_get(con, HTTP_HEADER_OTHER, CONST_STR_LEN("WWW-Authenticate"));
if (NULL != vb) www_auth = buffer_init_buffer(vb);
}
buffer_reset(con->physical.path);
con->response.htags = 0;
array_reset_data_strings(&con->response.headers);
http_response_body_clear(con, 0);
if (NULL != www_auth) {
http_header_response_set(con, HTTP_HEADER_OTHER, CONST_STR_LEN("WWW-Authenticate"), CONST_BUF_LEN(www_auth));
buffer_free(www_auth);
}
}
__attribute_cold__
static void connection_handle_errdoc(connection *con) {
if (NULL == con->response.handler_module
? con->request.error_handler_saved_status >= 65535
: (!con->conf.error_intercept||con->request.error_handler_saved_status))
return;
connection_handle_errdoc_init(con);
con->response.resp_body_finished = 1;
/* try to send static errorfile */
if (!buffer_string_is_empty(con->conf.errorfile_prefix)) {
buffer_copy_buffer(con->physical.path, con->conf.errorfile_prefix);
buffer_append_int(con->physical.path, con->http_status);
buffer_append_string_len(con->physical.path, CONST_STR_LEN(".html"));
if (0 == http_chunk_append_file(con, con->physical.path)) {
2019-12-05 08:16:25 +00:00
stat_cache_entry *sce = stat_cache_get_entry(con->physical.path);
const buffer *content_type = (NULL != sce)
? stat_cache_content_type_get(con, sce)
: NULL;
if (content_type)
http_header_response_set(con, HTTP_HEADER_CONTENT_TYPE,
CONST_STR_LEN("Content-Type"),
2019-12-05 08:16:25 +00:00
CONST_BUF_LEN(content_type));
return;
}
}
/* build default error-page */
buffer_reset(con->physical.path);
buffer * const b = con->srv->tmp_buf;
buffer_copy_string_len(b, CONST_STR_LEN(
"<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n"
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
"<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n"
" <head>\n"
" <title>"));
http_status_append(b, con->http_status);
buffer_append_string_len(b, CONST_STR_LEN(
"</title>\n"
" </head>\n"
" <body>\n"
" <h1>"));
http_status_append(b, con->http_status);
buffer_append_string_len(b, CONST_STR_LEN(
"</h1>\n"
" </body>\n"
"</html>\n"));
(void)http_chunk_append_mem(con, CONST_BUF_LEN(b));
http_header_response_set(con, HTTP_HEADER_CONTENT_TYPE,
CONST_STR_LEN("Content-Type"),
CONST_STR_LEN("text/html"));
}
static int connection_handle_write_prepare(connection *con) {
if (NULL == con->response.handler_module) {
/* static files */
switch(con->request.http_method) {
case HTTP_METHOD_GET:
case HTTP_METHOD_POST:
case HTTP_METHOD_HEAD:
break;
case HTTP_METHOD_OPTIONS:
/*
* 400 is coming from the request-parser BEFORE uri.path is set
* 403 is from the response handler when noone else catched it
*
* */
if ((!con->http_status || con->http_status == 200) && !buffer_string_is_empty(con->uri.path) &&
con->uri.path->ptr[0] != '*') {
http_response_body_clear(con, 0);
http_header_response_append(con, HTTP_HEADER_OTHER, CONST_STR_LEN("Allow"), CONST_STR_LEN("OPTIONS, GET, HEAD, POST"));
con->http_status = 200;
con->response.resp_body_finished = 1;
}
break;
default:
if (0 == con->http_status) {
con->http_status = 501;
}
break;
}
}
if (con->http_status == 0) {
con->http_status = 403;
}
switch(con->http_status) {
case 204: /* class: header only */
case 205:
case 304:
/* disable chunked encoding again as we have no body */
http_response_body_clear(con, 1);
con->response.resp_body_finished = 1;
break;
default: /* class: header + body */
/* only custom body for 4xx and 5xx */
if (con->http_status >= 400 && con->http_status < 600)
connection_handle_errdoc(con);
break;
}
/* Allow filter plugins to change response headers before they are written. */
switch(plugins_call_handle_response_start(con)) {
case HANDLER_GO_ON:
case HANDLER_FINISHED:
break;
default:
log_error(con->conf.errh,__FILE__,__LINE__,"response_start plugin failed");
return -1;
}
if (con->response.resp_body_finished) {
/* we have all the content and chunked encoding is not used, set a content-length */
if (!(con->response.htags & (HTTP_HEADER_CONTENT_LENGTH|HTTP_HEADER_TRANSFER_ENCODING))) {
off_t qlen = chunkqueue_length(con->write_queue);
/**
* The Content-Length header only can be sent if we have content:
* - HEAD doesn't have a content-body (but have a content-length)
* - 1xx, 204 and 304 don't have a content-body (RFC 2616 Section 4.3)
*
* Otherwise generate a Content-Length header as chunked encoding is not
* available
*/
if ((con->http_status >= 100 && con->http_status < 200) ||
con->http_status == 204 ||
con->http_status == 304) {
/* no Content-Body, no Content-Length */
http_header_response_unset(con, HTTP_HEADER_CONTENT_LENGTH, CONST_STR_LEN("Content-Length"));
} else if (qlen > 0 || con->request.http_method != HTTP_METHOD_HEAD) {
/* qlen = 0 is important for Redirects (301, ...) as they MAY have
* a content. Browsers are waiting for a Content otherwise
*/
buffer * const tb = con->srv->tmp_buf;
buffer_copy_int(tb, qlen);
http_header_response_set(con, HTTP_HEADER_CONTENT_LENGTH,
CONST_STR_LEN("Content-Length"),
CONST_BUF_LEN(tb));
}
}
} else {
/**
* the file isn't finished yet, but we have all headers
*
* to get keep-alive we either need:
* - Content-Length: ... (HTTP/1.0 and HTTP/1.0) or
* - Transfer-Encoding: chunked (HTTP/1.1)
* - Upgrade: ... (lighttpd then acts as transparent proxy)
*/
if (!(con->response.htags & (HTTP_HEADER_CONTENT_LENGTH|HTTP_HEADER_TRANSFER_ENCODING|HTTP_HEADER_UPGRADE))) {
if (con->request.http_method == HTTP_METHOD_CONNECT
&& con->http_status == 200) {
/*(no transfer-encoding if successful CONNECT)*/
} else if (con->request.http_version == HTTP_VERSION_1_1) {
off_t qlen = chunkqueue_length(con->write_queue);
con->response.send_chunked = 1;
if (qlen) {
/* create initial Transfer-Encoding: chunked segment */
buffer * const b = chunkqueue_prepend_buffer_open(con->write_queue);
buffer_append_uint_hex(b, (uintmax_t)qlen);
buffer_append_string_len(b, CONST_STR_LEN("\r\n"));
chunkqueue_prepend_buffer_commit(con->write_queue);
chunkqueue_append_mem(con->write_queue, CONST_STR_LEN("\r\n"));
}
http_header_response_append(con, HTTP_HEADER_TRANSFER_ENCODING, CONST_STR_LEN("Transfer-Encoding"), CONST_STR_LEN("chunked"));
} else {
con->request.keep_alive = 0;
}
}
}
if (con->request.http_method == HTTP_METHOD_HEAD) {
/**
* a HEAD request has the same as a GET
* without the content
*/
http_response_body_clear(con, 1);
con->response.resp_body_finished = 1;
}
http_response_write_header(con);
return 0;
}
static void connection_handle_write(connection *con) {
switch(connection_write_chunkqueue(con, con->write_queue, MAX_WRITE_LIMIT)) {
case 0:
if (con->response.resp_body_finished) {
connection_set_state(con, CON_STATE_RESPONSE_END);
}
break;
case -1: /* error on our side */
log_error(con->conf.errh, __FILE__, __LINE__,
"connection closed: write failed on fd %d", con->fd);
connection_set_state(con, CON_STATE_ERROR);
break;
case -2: /* remote close */
connection_set_state(con, CON_STATE_ERROR);
break;
case 1:
con->is_writable = 0;
/* not finished yet -> WRITE */
break;
}
con->write_request_ts = log_epoch_secs;
}
static void connection_handle_write_state(connection *con) {
do {
/* only try to write if we have something in the queue */
if (!chunkqueue_is_empty(con->write_queue)) {
if (con->is_writable) {
connection_handle_write(con);
if (con->request.state != CON_STATE_WRITE) break;
}
} else if (con->response.resp_body_finished) {
connection_set_state(con, CON_STATE_RESPONSE_END);
break;
}
if (con->response.handler_module && !con->response.resp_body_finished) {
plugin * const p = con->response.handler_module;
int rc = p->handle_subrequest(con, p->data);
switch(rc) {
case HANDLER_WAIT_FOR_EVENT:
case HANDLER_FINISHED:
case HANDLER_GO_ON:
break;
case HANDLER_WAIT_FOR_FD:
connection_fdwaitqueue_append(con);
break;
case HANDLER_COMEBACK:
default:
log_error(con->conf.errh, __FILE__, __LINE__,
"unexpected subrequest handler ret-value: %d %d",
con->fd, rc);
/* fall through */
case HANDLER_ERROR:
connection_set_state(con, CON_STATE_ERROR);
break;
}
}
} while (con->request.state == CON_STATE_WRITE
&& (!chunkqueue_is_empty(con->write_queue)
? con->is_writable
: con->response.resp_body_finished));
}
__attribute_cold__
static connection *connection_init(server *srv) {
connection *con;
con = calloc(1, sizeof(*con));
force_assert(NULL != con);
con->fd = 0;
con->ndx = -1;
con->bytes_written = 0;
con->bytes_read = 0;
con->response.resp_header_len = 0;
con->request.loops_per_request = 0;
con->request.conf = &con->conf;
con->request.con = con;
#define CLEAN(x) \
con->x = buffer_init();
CLEAN(request.target);
CLEAN(request.target_orig);
CLEAN(request.pathinfo);
CLEAN(uri.scheme);
CLEAN(uri.authority);
CLEAN(uri.path);
CLEAN(uri.path_raw);
CLEAN(uri.query);
CLEAN(physical.doc_root);
CLEAN(physical.path);
CLEAN(physical.basedir);
CLEAN(physical.rel_path);
CLEAN(physical.etag);
CLEAN(dst_addr_buf);
CLEAN(request.server_name_buf);
#undef CLEAN
con->write_queue = chunkqueue_init();
con->read_queue = chunkqueue_init();
con->request.reqbody_queue = chunkqueue_init();
con->srv = srv;
con->plugin_slots = srv->plugin_slots;
con->config_data_base = srv->config_data_base;
/* init plugin specific connection structures */
con->request.plugin_ctx = calloc(1, (srv->plugins.used + 1) * sizeof(void *));
force_assert(NULL != con->request.plugin_ctx);
con->request.cond_cache = calloc(srv->config_context->used, sizeof(cond_cache_t));
force_assert(NULL != con->request.cond_cache);
#ifdef HAVE_PCRE_H
if (srv->config_context->used > 1) {/*save 128b per con if no conditions)*/
con->request.cond_match =
calloc(srv->config_context->used, sizeof(cond_match_t));
force_assert(NULL != con->request.cond_match);
}
#endif
config_reset_config(con);
return con;
}
void connections_free(server *srv) {
connections * const conns = &