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 */
|
2005-02-20 14:27:00 +00:00
|
|
|
#include "log.h"
|
|
|
|
#include "connections.h"
|
|
|
|
#include "fdevent.h"
|
2018-09-09 05:50:33 +00:00
|
|
|
#include "http_header.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2016-02-21 18:32:17 +00:00
|
|
|
#include "configfile.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
#include "request.h"
|
|
|
|
#include "response.h"
|
|
|
|
#include "network.h"
|
|
|
|
#include "http_chunk.h"
|
2005-08-08 08:22:06 +00:00
|
|
|
#include "stat_cache.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
#include "joblist.h"
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
2005-09-02 17:07:00 +00:00
|
|
|
typedef struct {
|
|
|
|
PLUGIN_DATA;
|
|
|
|
} plugin_data;
|
|
|
|
|
2019-02-04 04:27:57 +00:00
|
|
|
__attribute_cold__
|
|
|
|
static connection *connection_init(server *srv);
|
|
|
|
|
|
|
|
static int connection_reset(server *srv, connection *con);
|
|
|
|
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
static connection *connections_get_new_connection(server *srv) {
|
|
|
|
connections *conns = srv->conns;
|
|
|
|
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);
|
2018-10-21 15:14:08 +00:00
|
|
|
connection_reset(srv, conns->ptr[i]);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
conns->ptr[conns->used]->ndx = conns->used;
|
|
|
|
return conns->ptr[conns->used++];
|
|
|
|
}
|
|
|
|
|
|
|
|
static int connection_del(server *srv, connection *con) {
|
|
|
|
size_t i;
|
|
|
|
connections *conns = srv->conns;
|
|
|
|
connection *temp;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (con == NULL) return -1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (-1 == con->ndx) return -1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2018-11-24 21:43:26 +00:00
|
|
|
buffer_clear(con->uri.authority);
|
2010-04-28 19:08:11 +00:00
|
|
|
buffer_reset(con->uri.path);
|
|
|
|
buffer_reset(con->uri.query);
|
|
|
|
buffer_reset(con->request.orig_uri);
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
i = 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
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (i != conns->used - 1) {
|
|
|
|
temp = conns->ptr[i];
|
|
|
|
conns->ptr[i] = conns->ptr[conns->used - 1];
|
|
|
|
conns->ptr[conns->used - 1] = temp;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
conns->ptr[i]->ndx = i;
|
|
|
|
conns->ptr[conns->used - 1]->ndx = -1;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
conns->used--;
|
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
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-07 23:47:49 +00:00
|
|
|
static int connection_close(server *srv, connection *con) {
|
2017-05-06 05:20:17 +00:00
|
|
|
if (con->fd < 0) con->fd = -con->fd;
|
|
|
|
|
2016-12-21 05:07:12 +00:00
|
|
|
plugins_call_handle_connection_close(srv, con);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-02-05 02:50:53 +00:00
|
|
|
con->request_count = 0;
|
|
|
|
chunkqueue_reset(con->read_queue);
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
fdevent_event_del(srv->ev, &(con->fde_ndx), con->fd);
|
|
|
|
fdevent_unregister(srv->ev, con->fd);
|
|
|
|
#ifdef __WIN32
|
|
|
|
if (closesocket(con->fd)) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sds",
|
|
|
|
"(warning) close:", con->fd, strerror(errno));
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (close(con->fd)) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sds",
|
|
|
|
"(warning) close:", con->fd, strerror(errno));
|
|
|
|
}
|
|
|
|
#endif
|
2013-12-24 04:28:44 +00:00
|
|
|
else {
|
|
|
|
srv->cur_fds--;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2016-12-13 19:05:47 +00:00
|
|
|
if (srv->srvconf.log_state_handling) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sd",
|
|
|
|
"connection closed for fd", con->fd);
|
|
|
|
}
|
2013-12-24 04:28:44 +00:00
|
|
|
con->fd = -1;
|
2019-02-13 03:51:17 +00:00
|
|
|
con->is_ssl_sock = 0;
|
2017-01-19 11:11:17 +00:00
|
|
|
|
|
|
|
/* plugins should have cleaned themselves up */
|
|
|
|
for (size_t i = 0; i < srv->plugins.used; ++i) {
|
|
|
|
plugin *p = ((plugin **)(srv->plugins.ptr))[i];
|
|
|
|
plugin_data *pd = p->data;
|
|
|
|
if (!pd || NULL == con->plugin_ctx[pd->id]) continue;
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb",
|
|
|
|
"missing cleanup in", p->name);
|
|
|
|
con->plugin_ctx[pd->id] = NULL;
|
|
|
|
}
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
connection_del(srv, con);
|
|
|
|
connection_set_state(srv, con, CON_STATE_CONNECT);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-13 19:05:47 +00:00
|
|
|
static void connection_read_for_eos(server *srv, connection *con) {
|
2016-07-26 19:55:45 +00:00
|
|
|
/* we have to do the linger_on_close stuff regardless
|
|
|
|
* of con->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.
|
|
|
|
*/
|
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)) */
|
2016-07-26 19:55:45 +00:00
|
|
|
con->close_timeout_ts = srv->cur_ts - (HTTP_LINGER_TIMEOUT+1);
|
2016-12-13 19:05:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void connection_handle_close_state(server *srv, connection *con) {
|
|
|
|
connection_read_for_eos(srv, con);
|
2016-07-26 19:55:45 +00:00
|
|
|
|
|
|
|
if (srv->cur_ts - con->close_timeout_ts > HTTP_LINGER_TIMEOUT) {
|
|
|
|
connection_close(srv, con);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void connection_handle_shutdown(server *srv, connection *con) {
|
2016-12-21 05:07:12 +00:00
|
|
|
plugins_call_handle_connection_shut_wr(srv, con);
|
2016-07-26 19:55:45 +00:00
|
|
|
|
|
|
|
srv->con_closed++;
|
|
|
|
connection_reset(srv, con);
|
|
|
|
|
|
|
|
/* close the connection */
|
2017-05-06 05:20:17 +00:00
|
|
|
if (con->fd >= 0 && 0 == shutdown(con->fd, SHUT_WR)) {
|
2016-07-26 19:55:45 +00:00
|
|
|
con->close_timeout_ts = srv->cur_ts;
|
|
|
|
connection_set_state(srv, con, CON_STATE_CLOSE);
|
|
|
|
|
|
|
|
if (srv->srvconf.log_state_handling) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sd",
|
|
|
|
"shutdown for fd", con->fd);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
connection_close(srv, con);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void connection_handle_response_end_state(server *srv, 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(srv, con);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (con->state != CON_STATE_ERROR) srv->con_written++;
|
|
|
|
|
2016-12-16 16:06:29 +00:00
|
|
|
if (con->request.content_length != con->request_content_queue->bytes_in
|
2016-07-26 19:55:45 +00:00
|
|
|
|| con->state == CON_STATE_ERROR) {
|
|
|
|
/* request body is present and has not been read completely */
|
|
|
|
con->keep_alive = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (con->keep_alive) {
|
|
|
|
connection_reset(srv, con);
|
|
|
|
#if 0
|
|
|
|
con->request_start = srv->cur_ts;
|
|
|
|
con->read_idle_ts = srv->cur_ts;
|
|
|
|
#endif
|
|
|
|
connection_set_state(srv, con, CON_STATE_REQUEST_START);
|
|
|
|
} else {
|
|
|
|
connection_handle_shutdown(srv, con);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-09 05:50:33 +00:00
|
|
|
static void connection_handle_errdoc_init(connection *con) {
|
2016-05-11 01:38:09 +00:00
|
|
|
/* 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) {
|
2018-09-09 05:50:33 +00:00
|
|
|
buffer *vb = http_header_response_get(con, HTTP_HEADER_OTHER, CONST_STR_LEN("WWW-Authenticate"));
|
|
|
|
if (NULL != vb) www_auth = buffer_init_buffer(vb);
|
2016-05-11 01:38:09 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 20:23:51 +00:00
|
|
|
buffer_reset(con->physical.path);
|
2018-09-16 02:48:29 +00:00
|
|
|
con->response.htags = 0;
|
2018-10-23 00:28:53 +00:00
|
|
|
array_reset_data_strings(con->response.headers);
|
2018-09-16 02:48:29 +00:00
|
|
|
http_response_body_clear(con, 0);
|
2016-05-11 01:38:09 +00:00
|
|
|
|
|
|
|
if (NULL != www_auth) {
|
2018-09-09 05:50:33 +00:00
|
|
|
http_header_response_set(con, HTTP_HEADER_OTHER, CONST_STR_LEN("WWW-Authenticate"), CONST_BUF_LEN(www_auth));
|
2016-05-11 01:38:09 +00:00
|
|
|
buffer_free(www_auth);
|
|
|
|
}
|
2016-04-13 05:04:39 +00:00
|
|
|
}
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
static int connection_handle_write_prepare(server *srv, connection *con) {
|
2005-08-15 09:55:23 +00:00
|
|
|
if (con->mode == DIRECT) {
|
|
|
|
/* static files */
|
|
|
|
switch(con->request.http_method) {
|
|
|
|
case HTTP_METHOD_GET:
|
|
|
|
case HTTP_METHOD_POST:
|
|
|
|
case HTTP_METHOD_HEAD:
|
|
|
|
break;
|
2005-08-19 08:37:52 +00:00
|
|
|
case HTTP_METHOD_OPTIONS:
|
2006-01-03 13:59:46 +00:00
|
|
|
/*
|
|
|
|
* 400 is coming from the request-parser BEFORE uri.path is set
|
2006-10-04 13:26:23 +00:00
|
|
|
* 403 is from the response handler when noone else catched it
|
|
|
|
*
|
2006-01-03 13:59:46 +00:00
|
|
|
* */
|
2015-02-08 19:10:44 +00:00
|
|
|
if ((!con->http_status || con->http_status == 200) && !buffer_string_is_empty(con->uri.path) &&
|
2006-01-03 13:59:46 +00:00
|
|
|
con->uri.path->ptr[0] != '*') {
|
2018-09-16 02:48:29 +00:00
|
|
|
http_response_body_clear(con, 0);
|
2018-09-09 05:50:33 +00:00
|
|
|
http_header_response_append(con, HTTP_HEADER_OTHER, CONST_STR_LEN("Allow"), CONST_STR_LEN("OPTIONS, GET, HEAD, POST"));
|
2005-08-19 08:37:52 +00:00
|
|
|
con->http_status = 200;
|
|
|
|
con->file_finished = 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
2005-08-15 09:55:23 +00:00
|
|
|
default:
|
2013-06-29 10:53:22 +00:00
|
|
|
if (0 == con->http_status) {
|
2005-08-15 09:55:23 +00:00
|
|
|
con->http_status = 501;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
if (con->http_status == 0) {
|
|
|
|
con->http_status = 403;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
switch(con->http_status) {
|
2008-04-23 19:10:42 +00:00
|
|
|
case 204: /* class: header only */
|
|
|
|
case 205:
|
|
|
|
case 304:
|
|
|
|
/* disable chunked encoding again as we have no body */
|
2018-09-16 02:48:29 +00:00
|
|
|
http_response_body_clear(con, 1);
|
2008-04-23 19:10:42 +00:00
|
|
|
con->file_finished = 1;
|
|
|
|
break;
|
|
|
|
default: /* class: header + body */
|
|
|
|
/* only custom body for 4xx and 5xx */
|
|
|
|
if (con->http_status < 400 || con->http_status >= 600) break;
|
|
|
|
|
2017-02-11 19:35:28 +00:00
|
|
|
if (con->mode != DIRECT && (!con->conf.error_intercept || con->error_handler_saved_status)) break;
|
2018-10-14 21:29:10 +00:00
|
|
|
if (con->mode == DIRECT && con->error_handler_saved_status >= 65535) break;
|
2017-02-11 19:35:28 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
con->file_finished = 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2018-09-09 05:50:33 +00:00
|
|
|
connection_handle_errdoc_init(con);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
/* try to send static errorfile */
|
2015-02-08 12:37:10 +00:00
|
|
|
if (!buffer_string_is_empty(con->conf.errorfile_prefix)) {
|
2005-08-15 09:55:23 +00:00
|
|
|
stat_cache_entry *sce = NULL;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer_copy_buffer(con->physical.path, con->conf.errorfile_prefix);
|
|
|
|
buffer_append_int(con->physical.path, con->http_status);
|
2008-04-23 19:10:42 +00:00
|
|
|
buffer_append_string_len(con->physical.path, CONST_STR_LEN(".html"));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2016-03-30 10:39:33 +00:00
|
|
|
if (0 == http_chunk_append_file(srv, con, con->physical.path)) {
|
2005-08-15 09:55:23 +00:00
|
|
|
con->file_finished = 1;
|
2016-03-30 10:39:33 +00:00
|
|
|
if (HANDLER_ERROR != stat_cache_get_entry(srv, con, con->physical.path, &sce)) {
|
2018-02-03 04:24:22 +00:00
|
|
|
stat_cache_content_type_get(srv, con, con->physical.path, sce);
|
2018-09-09 05:50:33 +00:00
|
|
|
http_header_response_set(con, HTTP_HEADER_CONTENT_TYPE, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(sce->content_type));
|
2016-03-30 10:39:33 +00:00
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
if (!con->file_finished) {
|
2018-10-27 02:17:12 +00:00
|
|
|
buffer *b = srv->tmp_buf;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
buffer_reset(con->physical.path);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
con->file_finished = 1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
/* build default error-page */
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_copy_string_len(b, CONST_STR_LEN(
|
2005-08-15 09:55:23 +00:00
|
|
|
"<?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"
|
2008-07-30 19:38:32 +00:00
|
|
|
" <title>"));
|
2018-09-23 22:36:58 +00:00
|
|
|
http_status_append(b, con->http_status);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_append_string_len(b, CONST_STR_LEN(
|
2005-08-15 09:55:23 +00:00
|
|
|
"</title>\n"
|
|
|
|
" </head>\n"
|
|
|
|
" <body>\n"
|
2008-07-30 19:38:32 +00:00
|
|
|
" <h1>"));
|
2018-09-23 22:36:58 +00:00
|
|
|
http_status_append(b, con->http_status);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_append_string_len(b, CONST_STR_LEN("</h1>\n"
|
2005-08-15 09:55:23 +00:00
|
|
|
" </body>\n"
|
|
|
|
"</html>\n"
|
2008-07-30 19:38:32 +00:00
|
|
|
));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2018-10-27 02:17:12 +00:00
|
|
|
(void)http_chunk_append_mem(srv, con, CONST_BUF_LEN(b));
|
2015-02-08 19:10:36 +00:00
|
|
|
|
2018-09-09 05:50:33 +00:00
|
|
|
http_header_response_set(con, HTTP_HEADER_CONTENT_TYPE, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("text/html"));
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2005-08-15 09:55:23 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2016-06-22 00:28:55 +00:00
|
|
|
/* Allow filter plugins to change response headers before they are written. */
|
|
|
|
switch(plugins_call_handle_response_start(srv, con)) {
|
|
|
|
case HANDLER_GO_ON:
|
|
|
|
case HANDLER_FINISHED:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", "response_start plugin failed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
if (con->file_finished) {
|
2006-10-04 13:26:23 +00:00
|
|
|
/* we have all the content and chunked encoding is not used, set a content-length */
|
|
|
|
|
2018-09-09 05:50:33 +00:00
|
|
|
if (!(con->response.htags & (HTTP_HEADER_CONTENT_LENGTH|HTTP_HEADER_TRANSFER_ENCODING))) {
|
2006-10-07 17:47:49 +00:00
|
|
|
off_t qlen = chunkqueue_length(con->write_queue);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2007-08-18 10:40:20 +00:00
|
|
|
/**
|
|
|
|
* 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 */
|
2018-11-15 08:35:55 +00:00
|
|
|
http_header_response_unset(con, HTTP_HEADER_CONTENT_LENGTH, CONST_STR_LEN("Content-Length"));
|
2008-02-27 18:35:42 +00:00
|
|
|
} else if (qlen > 0 || con->request.http_method != HTTP_METHOD_HEAD) {
|
2007-09-06 11:24:20 +00:00
|
|
|
/* qlen = 0 is important for Redirects (301, ...) as they MAY have
|
|
|
|
* a content. Browsers are waiting for a Content otherwise
|
|
|
|
*/
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer_copy_int(srv->tmp_buf, qlen);
|
2018-09-09 05:50:33 +00:00
|
|
|
http_header_response_set(con, HTTP_HEADER_CONTENT_LENGTH, CONST_STR_LEN("Content-Length"), CONST_BUF_LEN(srv->tmp_buf));
|
2006-10-07 17:47:49 +00:00
|
|
|
}
|
2005-08-21 17:52:06 +00:00
|
|
|
}
|
2005-08-15 09:55:23 +00:00
|
|
|
} else {
|
2007-08-17 15:37:45 +00:00
|
|
|
/**
|
|
|
|
* 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)
|
2017-05-06 05:23:37 +00:00
|
|
|
* - Upgrade: ... (lighttpd then acts as transparent proxy)
|
2007-08-17 15:37:45 +00:00
|
|
|
*/
|
|
|
|
|
2018-09-09 05:50:33 +00:00
|
|
|
if (!(con->response.htags & (HTTP_HEADER_CONTENT_LENGTH|HTTP_HEADER_TRANSFER_ENCODING|HTTP_HEADER_UPGRADE))) {
|
2017-11-25 23:38:09 +00:00
|
|
|
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) {
|
2016-06-20 01:05:25 +00:00
|
|
|
off_t qlen = chunkqueue_length(con->write_queue);
|
2018-09-16 02:48:29 +00:00
|
|
|
con->response.send_chunked = 1;
|
2016-06-20 01:05:25 +00:00
|
|
|
if (qlen) {
|
|
|
|
/* create initial Transfer-Encoding: chunked segment */
|
2018-10-27 02:03:31 +00:00
|
|
|
buffer * const b = chunkqueue_prepend_buffer_open(con->write_queue);
|
2016-06-20 01:05:25 +00:00
|
|
|
buffer_append_uint_hex(b, (uintmax_t)qlen);
|
|
|
|
buffer_append_string_len(b, CONST_STR_LEN("\r\n"));
|
2018-10-27 02:03:31 +00:00
|
|
|
chunkqueue_prepend_buffer_commit(con->write_queue);
|
2016-06-20 01:05:25 +00:00
|
|
|
chunkqueue_append_mem(con->write_queue, CONST_STR_LEN("\r\n"));
|
|
|
|
}
|
2018-09-09 05:50:33 +00:00
|
|
|
http_header_response_append(con, HTTP_HEADER_TRANSFER_ENCODING, CONST_STR_LEN("Transfer-Encoding"), CONST_STR_LEN("chunked"));
|
2016-06-20 01:05:25 +00:00
|
|
|
} else {
|
|
|
|
con->keep_alive = 0;
|
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
if (con->request.http_method == HTTP_METHOD_HEAD) {
|
2006-10-05 10:23:27 +00:00
|
|
|
/**
|
|
|
|
* a HEAD request has the same as a GET
|
|
|
|
* without the content
|
|
|
|
*/
|
2018-09-16 02:48:29 +00:00
|
|
|
http_response_body_clear(con, 1);
|
2008-02-27 12:15:38 +00:00
|
|
|
con->file_finished = 1;
|
2005-08-15 09:55:23 +00:00
|
|
|
}
|
2005-08-19 08:37:52 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
http_response_write_header(srv, con);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-02-13 04:00:26 +00:00
|
|
|
static void connection_handle_write(server *srv, connection *con) {
|
2016-12-24 12:19:26 +00:00
|
|
|
switch(connection_write_chunkqueue(srv, con, con->write_queue, MAX_WRITE_LIMIT)) {
|
2005-02-20 14:27:00 +00:00
|
|
|
case 0:
|
2011-08-22 15:12:28 +00:00
|
|
|
con->write_request_ts = srv->cur_ts;
|
2005-02-20 14:27:00 +00:00
|
|
|
if (con->file_finished) {
|
|
|
|
connection_set_state(srv, con, CON_STATE_RESPONSE_END);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case -1: /* error on our side */
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sd",
|
|
|
|
"connection closed: write failed on fd", con->fd);
|
|
|
|
connection_set_state(srv, con, CON_STATE_ERROR);
|
|
|
|
break;
|
|
|
|
case -2: /* remote close */
|
|
|
|
connection_set_state(srv, con, CON_STATE_ERROR);
|
|
|
|
break;
|
|
|
|
case 1:
|
2011-08-22 15:12:28 +00:00
|
|
|
con->write_request_ts = srv->cur_ts;
|
2005-02-20 14:27:00 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-05 02:50:53 +00:00
|
|
|
static void connection_handle_write_state(server *srv, 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) {
|
2019-02-13 04:00:26 +00:00
|
|
|
connection_handle_write(srv, con);
|
2019-02-05 02:50:53 +00:00
|
|
|
if (con->state != CON_STATE_WRITE) break;
|
|
|
|
}
|
|
|
|
} else if (con->file_finished) {
|
|
|
|
connection_set_state(srv, con, CON_STATE_RESPONSE_END);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (con->mode != DIRECT && !con->file_finished) {
|
|
|
|
int r = plugins_call_handle_subrequest(srv, con);
|
|
|
|
switch(r) {
|
|
|
|
case HANDLER_WAIT_FOR_EVENT:
|
|
|
|
case HANDLER_FINISHED:
|
|
|
|
case HANDLER_GO_ON:
|
|
|
|
break;
|
|
|
|
case HANDLER_WAIT_FOR_FD:
|
|
|
|
srv->want_fds++;
|
|
|
|
fdwaitqueue_append(srv, con);
|
|
|
|
break;
|
|
|
|
case HANDLER_COMEBACK:
|
|
|
|
default:
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sdd",
|
|
|
|
"unexpected subrequest handler ret-value:",
|
|
|
|
con->fd, r);
|
|
|
|
/* fall through */
|
|
|
|
case HANDLER_ERROR:
|
|
|
|
connection_set_state(srv, con, CON_STATE_ERROR);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (con->state == CON_STATE_WRITE
|
|
|
|
&& (!chunkqueue_is_empty(con->write_queue)
|
|
|
|
? con->is_writable
|
|
|
|
: con->file_finished));
|
|
|
|
}
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
|
2019-02-04 04:27:57 +00:00
|
|
|
__attribute_cold__
|
|
|
|
static connection *connection_init(server *srv) {
|
2005-02-20 14:27:00 +00:00
|
|
|
connection *con;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
UNUSED(srv);
|
|
|
|
|
|
|
|
con = calloc(1, sizeof(*con));
|
2016-01-30 13:59:07 +00:00
|
|
|
force_assert(NULL != con);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
con->fd = 0;
|
|
|
|
con->ndx = -1;
|
|
|
|
con->fde_ndx = -1;
|
|
|
|
con->bytes_written = 0;
|
|
|
|
con->bytes_read = 0;
|
|
|
|
con->bytes_header = 0;
|
2005-07-23 20:42:34 +00:00
|
|
|
con->loops_per_request = 0;
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
#define CLEAN(x) \
|
|
|
|
con->x = buffer_init();
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
CLEAN(request.uri);
|
|
|
|
CLEAN(request.request);
|
|
|
|
CLEAN(request.pathinfo);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
CLEAN(request.orig_uri);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
CLEAN(uri.scheme);
|
|
|
|
CLEAN(uri.authority);
|
|
|
|
CLEAN(uri.path);
|
|
|
|
CLEAN(uri.path_raw);
|
|
|
|
CLEAN(uri.query);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
CLEAN(physical.doc_root);
|
|
|
|
CLEAN(physical.path);
|
2005-08-08 09:42:27 +00:00
|
|
|
CLEAN(physical.basedir);
|
2005-02-20 14:27:00 +00:00
|
|
|
CLEAN(physical.rel_path);
|
|
|
|
CLEAN(physical.etag);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
CLEAN(server_name);
|
2017-04-06 01:37:10 +00:00
|
|
|
CLEAN(proto);
|
2005-08-25 20:09:46 +00:00
|
|
|
CLEAN(dst_addr_buf);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#undef CLEAN
|
|
|
|
con->write_queue = chunkqueue_init();
|
|
|
|
con->read_queue = chunkqueue_init();
|
2005-09-26 08:56:39 +00:00
|
|
|
con->request_content_queue = chunkqueue_init();
|
2005-11-01 07:50:08 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
con->request.headers = array_init();
|
|
|
|
con->response.headers = array_init();
|
|
|
|
con->environment = array_init();
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* init plugin specific connection structures */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-11-17 14:08:58 +00:00
|
|
|
con->plugin_ctx = calloc(1, (srv->plugins.used + 1) * sizeof(void *));
|
2016-01-30 13:59:07 +00:00
|
|
|
force_assert(NULL != con->plugin_ctx);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-09 06:42:33 +00:00
|
|
|
con->cond_cache = calloc(srv->config_context->used, sizeof(cond_cache_t));
|
2016-01-30 13:59:07 +00:00
|
|
|
force_assert(NULL != con->cond_cache);
|
2005-02-20 14:27:00 +00:00
|
|
|
config_setup_connection(srv, con);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return con;
|
|
|
|
}
|
|
|
|
|
|
|
|
void connections_free(server *srv) {
|
|
|
|
connections *conns = srv->conns;
|
2006-10-04 13:26:23 +00:00
|
|
|
size_t i;
|
|
|
|
|
2017-01-24 16:29:10 +00:00
|
|
|
if (NULL == conns) return;
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
for (i = 0; i < conns->size; i++) {
|
|
|
|
connection *con = conns->ptr[i];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
connection_reset(srv, con);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
chunkqueue_free(con->write_queue);
|
|
|
|
chunkqueue_free(con->read_queue);
|
2005-09-26 08:56:39 +00:00
|
|
|
chunkqueue_free(con->request_content_queue);
|
2005-02-20 14:27:00 +00:00
|
|
|
array_free(con->request.headers);
|
|
|
|
array_free(con->response.headers);
|
|
|
|
array_free(con->environment);
|
|
|
|
|
|
|
|
#define CLEAN(x) \
|
|
|
|
buffer_free(con->x);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
CLEAN(request.uri);
|
|
|
|
CLEAN(request.request);
|
|
|
|
CLEAN(request.pathinfo);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
CLEAN(request.orig_uri);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
CLEAN(uri.scheme);
|
|
|
|
CLEAN(uri.authority);
|
|
|
|
CLEAN(uri.path);
|
|
|
|
CLEAN(uri.path_raw);
|
|
|
|
CLEAN(uri.query);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
CLEAN(physical.doc_root);
|
|
|
|
CLEAN(physical.path);
|
2005-08-08 09:42:27 +00:00
|
|
|
CLEAN(physical.basedir);
|
2005-02-20 14:27:00 +00:00
|
|
|
CLEAN(physical.etag);
|
|
|
|
CLEAN(physical.rel_path);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
CLEAN(server_name);
|
2017-04-06 01:37:10 +00:00
|
|
|
CLEAN(proto);
|
2005-08-25 20:09:46 +00:00
|
|
|
CLEAN(dst_addr_buf);
|
2005-02-20 14:27:00 +00:00
|
|
|
#undef CLEAN
|
|
|
|
free(con->plugin_ctx);
|
2005-08-09 06:42:33 +00:00
|
|
|
free(con->cond_cache);
|
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);
|
2017-01-24 16:29:10 +00:00
|
|
|
free(conns);
|
|
|
|
srv->conns = NULL;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-04 04:27:57 +00:00
|
|
|
static int connection_reset(server *srv, connection *con) {
|
2005-02-20 14:27:00 +00:00
|
|
|
plugins_call_connection_reset(srv, con);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2016-12-06 03:53:09 +00:00
|
|
|
connection_response_reset(srv, con);
|
2005-02-20 14:27:00 +00:00
|
|
|
con->is_readable = 1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
con->bytes_written = 0;
|
|
|
|
con->bytes_written_cur_second = 0;
|
|
|
|
con->bytes_read = 0;
|
|
|
|
con->bytes_header = 0;
|
2005-07-23 20:42:34 +00:00
|
|
|
con->loops_per_request = 0;
|
|