2016-03-19 15:14:35 +00:00
|
|
|
#include "first.h"
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#include "buffer.h"
|
|
|
|
#include "server.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "connections.h"
|
|
|
|
#include "fdevent.h"
|
|
|
|
|
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;
|
|
|
|
|
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
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (conns->size == 0) {
|
2017-03-02 00:21:04 +00:00
|
|
|
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 = NULL;
|
|
|
|
conns->ptr = malloc(sizeof(*conns->ptr) * conns->size);
|
2016-01-30 13:59:07 +00:00
|
|
|
force_assert(NULL != conns->ptr);
|
2005-02-20 14:27:00 +00:00
|
|
|
for (i = 0; i < conns->size; i++) {
|
|
|
|
conns->ptr[i] = connection_init(srv);
|
|
|
|
}
|
|
|
|
} else if (conns->size == conns->used) {
|
2017-03-02 00:21:04 +00:00
|
|
|
conns->size += srv->max_conns >= 128 ? 128 : 16;
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
connection_reset(srv, conns->ptr[conns->used]);
|
2006-10-04 13:26:23 +00:00
|
|
|
#if 0
|
2005-02-20 14:27:00 +00:00
|
|
|
fprintf(stderr, "%s.%d: add: ", __FILE__, __LINE__);
|
|
|
|
for (i = 0; i < conns->used + 1; 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
|
|
|
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
|
|
|
|
2010-04-28 19:08:11 +00:00
|
|
|
buffer_reset(con->uri.authority);
|
|
|
|
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) {
|
2016-12-21 05:07:12 +00:00
|
|
|
plugins_call_handle_connection_close(srv, con);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
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;
|
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;
|
|
|
|
char buf[4096];
|
|
|
|
|
|
|
|
do {
|
|
|
|
len = read(con->fd, buf, sizeof(buf));
|
|
|
|
} 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 */
|
|
|
|
if ((0 == shutdown(con->fd, SHUT_WR))) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-11 01:38:09 +00:00
|
|
|
static void connection_handle_errdoc_init(server *srv, 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) {
|
|
|
|
data_string *ds = (data_string *)array_get_element(con->response.headers, "WWW-Authenticate");
|
|
|
|
if (NULL != ds) {
|
|
|
|
www_auth = buffer_init_buffer(ds->value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-19 00:39:00 +00:00
|
|
|
con->response.transfer_encoding = 0;
|
2016-05-02 20:23:51 +00:00
|
|
|
buffer_reset(con->physical.path);
|
|
|
|
array_reset(con->response.headers);
|
|
|
|
chunkqueue_reset(con->write_queue);
|
2016-05-11 01:38:09 +00:00
|
|
|
|
|
|
|
if (NULL != www_auth) {
|
|
|
|
response_header_insert(srv, con, CONST_STR_LEN("WWW-Authenticate"), CONST_BUF_LEN(www_auth));
|
|
|
|
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] != '*') {
|
2005-08-19 08:37:52 +00:00
|
|
|
response_header_insert(srv, con, CONST_STR_LEN("Allow"), CONST_STR_LEN("OPTIONS, GET, HEAD, POST"));
|
|
|
|
|
2008-02-27 12:15:38 +00:00
|
|
|
con->response.transfer_encoding &= ~HTTP_TRANSFER_ENCODING_CHUNKED;
|
|
|
|
con->parsed_response &= ~HTTP_CONTENT_LENGTH;
|
|
|
|
|
2005-08-19 08:37:52 +00:00
|
|
|
con->http_status = 200;
|
|
|
|
con->file_finished = 1;
|
|
|
|
|
|
|
|
chunkqueue_reset(con->write_queue);
|
|
|
|
}
|
|
|
|
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 */
|
|
|
|
con->response.transfer_encoding &= ~HTTP_TRANSFER_ENCODING_CHUNKED;
|
|
|
|
con->parsed_response &= ~HTTP_CONTENT_LENGTH;
|
|
|
|
chunkqueue_reset(con->write_queue);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
con->file_finished = 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2016-05-11 01:38:09 +00:00
|
|
|
connection_handle_errdoc_init(srv, 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)) {
|
|
|
|
response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(sce->content_type));
|
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
if (!con->file_finished) {
|
2005-08-15 09:55:23 +00:00
|
|
|
buffer *b;
|
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;
|
2015-02-08 19:10:36 +00:00
|
|
|
b = buffer_init();
|
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>"));
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer_append_int(b, con->http_status);
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_append_string_len(b, CONST_STR_LEN(" - "));
|
2005-08-15 09:55:23 +00:00
|
|
|
buffer_append_string(b, get_http_status_name(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>"));
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer_append_int(b, con->http_status);
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_append_string_len(b, CONST_STR_LEN(" - "));
|
2005-08-15 09:55:23 +00:00
|
|
|
buffer_append_string(b, get_http_status_name(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
|
|
|
|
2016-06-21 07:39:17 +00:00
|
|
|
(void)http_chunk_append_buffer(srv, con, b);
|
2015-02-08 19:10:36 +00:00
|
|
|
buffer_free(b);
|
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
response_header_overwrite(srv, con, 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 */
|
|
|
|
|
2017-02-05 01:33:45 +00:00
|
|
|
if (!(con->parsed_response & (HTTP_CONTENT_LENGTH|HTTP_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) {
|
2008-02-27 12:15:38 +00:00
|
|
|
data_string *ds;
|
2007-08-18 10:40:20 +00:00
|
|
|
/* no Content-Body, no Content-Length */
|
2008-02-27 12:15:38 +00:00
|
|
|
if (NULL != (ds = (data_string*) array_get_element(con->response.headers, "Content-Length"))) {
|
2008-04-23 13:10:41 +00:00
|
|
|
buffer_reset(ds->value); /* Headers with empty values are ignored for output */
|
2008-02-27 12:15:38 +00:00
|
|
|
}
|
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);
|
2006-10-07 17:47:49 +00:00
|
|
|
|
|
|
|
response_header_overwrite(srv, con, CONST_STR_LEN("Content-Length"), CONST_BUF_LEN(srv->tmp_buf));
|
|
|
|
}
|
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-02-05 01:33:45 +00:00
|
|
|
if (!(con->parsed_response & (HTTP_CONTENT_LENGTH|HTTP_TRANSFER_ENCODING))) {
|
2016-06-20 01:05:25 +00:00
|
|
|
if (con->request.http_version == HTTP_VERSION_1_1) {
|
|
|
|
off_t qlen = chunkqueue_length(con->write_queue);
|
|
|
|
con->response.transfer_encoding = HTTP_TRANSFER_ENCODING_CHUNKED;
|
|
|
|
if (qlen) {
|
|
|
|
/* create initial Transfer-Encoding: chunked segment */
|
|
|
|
buffer *b = srv->tmp_chunk_len;
|
|
|
|
buffer_string_set_length(b, 0);
|
|
|
|
buffer_append_uint_hex(b, (uintmax_t)qlen);
|
|
|
|
buffer_append_string_len(b, CONST_STR_LEN("\r\n"));
|
|
|
|
chunkqueue_prepend_buffer(con->write_queue, b);
|
|
|
|
chunkqueue_append_mem(con->write_queue, CONST_STR_LEN("\r\n"));
|
|
|
|
}
|
2017-02-05 01:33:45 +00:00
|
|
|
response_header_append(srv, con, 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
|
|
|
|
2007-08-17 15:37:45 +00:00
|
|
|
/**
|
|
|
|
* if the backend sent a Connection: close, follow the wish
|
|
|
|
*
|
|
|
|
* NOTE: if the backend sent Connection: Keep-Alive, but no Content-Length, we
|
|
|
|
* will close the connection. That's fine. We can always decide the close
|
|
|
|
* the connection
|
|
|
|
*
|
|
|
|
* FIXME: to be nice we should remove the Connection: ...
|
|
|
|
*/
|
|
|
|
if (con->parsed_response & HTTP_CONNECTION) {
|
2005-02-20 14:27:00 +00:00
|
|
|
/* a subrequest disable keep-alive although the client wanted it */
|
|
|
|
if (con->keep_alive && !con->response.keep_alive) {
|
|
|
|
con->keep_alive = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
*/
|
2008-02-27 12:15:38 +00:00
|
|
|
con->file_finished = 1;
|
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
chunkqueue_reset(con->write_queue);
|
2006-10-05 10:23:27 +00:00
|
|
|
con->response.transfer_encoding &= ~HTTP_TRANSFER_ENCODING_CHUNKED;
|
2017-02-05 01:33:45 +00:00
|
|
|
if (con->parsed_response & HTTP_TRANSFER_ENCODING) {
|
|
|
|
data_string *ds;
|
|
|
|
if (NULL != (ds = (data_string*) array_get_element(con->response.headers, "Transfer-Encoding"))) {
|
|
|
|
buffer_reset(ds->value); /* Headers with empty values are ignored for output */
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int 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;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
connection *connection_init(server *srv) {
|
|
|
|
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_line);
|
|
|
|
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);
|
|
|
|
CLEAN(parse_request);
|
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_line);
|
|
|
|
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);
|
|
|
|
CLEAN(parse_request);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int connection_reset(server *srv, connection *con) {
|
|
|
|
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;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
con->request.http_method = HTTP_METHOD_UNSET;
|
|
|
|
con->request.http_version = HTTP_VERSION_UNSET;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
con->request.http_if_modified_since = NULL;
|
|
|
|
con->request.http_if_none_match = NULL;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#define CLEAN(x) \
|
|
|
|
if (con->x) buffer_reset(con->x);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
CLEAN(request.uri);
|
|
|
|
CLEAN(request.request_line);
|
|
|
|
CLEAN(request.pathinfo);
|
|
|
|
CLEAN(request.request);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2009-06-21 17:25:34 +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);
|
2009-06-21 17:25:34 +00:00
|
|
|
|