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 */
|
2019-12-10 04:53:29 +00:00
|
|
|
#include "settings.h" /* BUFFER_MAX_REUSE_SIZE MAX_READ_LIMIT MAX_WRITE_LIMIT */
|
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
|
|
|
|
|
|
|
#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 "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"
|
|
|
|
|
2019-06-21 10:51:23 +00:00
|
|
|
#define HTTP_LINGER_TIMEOUT 5
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
#define connection_set_state(r, n) ((r)->state = (n))
|
2019-06-21 11:02:45 +00:00
|
|
|
|
2019-02-04 04:27:57 +00:00
|
|
|
__attribute_cold__
|
|
|
|
static connection *connection_init(server *srv);
|
|
|
|
|
2020-07-25 01:02:08 +00:00
|
|
|
static void connection_reset(connection *con);
|
2019-02-04 04:27:57 +00:00
|
|
|
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
static connection *connections_get_new_connection(server *srv) {
|
2019-10-04 06:10:02 +00:00
|
|
|
connections * const conns = &srv->conns;
|
2005-02-20 14:27:00 +00:00
|
|
|
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);
|
2019-11-26 07:13:05 +00:00
|
|
|
connection_reset(conns->ptr[i]);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
conns->ptr[conns->used]->ndx = conns->used;
|
|
|
|
return conns->ptr[conns->used++];
|
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static void connection_del(server *srv, connection *con) {
|
2019-11-26 07:13:05 +00:00
|
|
|
connections * const conns = &srv->conns;
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (-1 == con->ndx) return;
|
|
|
|
uint32_t i = (uint32_t)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
|
|
|
|
2019-12-05 04:01:41 +00:00
|
|
|
if (i != --conns->used) {
|
2019-11-26 07:13:05 +00:00
|
|
|
connection * const temp = conns->ptr[i];
|
2019-12-05 04:01:41 +00:00
|
|
|
conns->ptr[i] = conns->ptr[conns->used];
|
|
|
|
conns->ptr[conns->used] = temp;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
conns->ptr[i]->ndx = i;
|
2019-12-05 04:01:41 +00:00
|
|
|
conns->ptr[conns->used]->ndx = -1;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2020-07-24 00:41:12 +00:00
|
|
|
#if 0 /* DEBUG_DEV */
|
2019-10-19 06:21:46 +00:00
|
|
|
__attribute_cold__
|
2020-01-13 02:51:12 +00:00
|
|
|
static void connection_plugin_ctx_check(server * const srv, request_st * const r) {
|
2019-10-19 06:21:46 +00:00
|
|
|
/* 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;
|
2020-07-24 00:41:12 +00:00
|
|
|
if (!pd) continue;
|
|
|
|
if (NULL == r->plugin_ctx[pd->id]
|
|
|
|
&& NULL == r->con->plugin_ctx[pd->id]) continue;
|
2020-01-13 02:51:12 +00:00
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
2019-11-25 06:54:08 +00:00
|
|
|
"missing cleanup in %s", p->name);
|
2020-01-13 02:51:12 +00:00
|
|
|
r->plugin_ctx[pd->id] = NULL;
|
2020-07-24 00:41:12 +00:00
|
|
|
r->con->plugin_ctx[pd->id] = NULL;
|
2019-10-19 06:21:46 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-24 00:41:12 +00:00
|
|
|
#endif
|
2019-10-19 06:21:46 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static void connection_close(connection *con) {
|
2017-05-06 05:20:17 +00:00
|
|
|
if (con->fd < 0) con->fd = -con->fd;
|
|
|
|
|
2019-11-26 07:13:05 +00:00
|
|
|
plugins_call_handle_connection_close(con);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
server * const srv = con->srv;
|
|
|
|
request_st * const r = &con->request;
|
|
|
|
|
2020-07-24 00:41:12 +00:00
|
|
|
#if 0 /* DEBUG_DEV */
|
2020-01-13 02:51:12 +00:00
|
|
|
/* plugins should have cleaned themselves up */
|
|
|
|
for (uint32_t i = 0; i < srv->plugins.used; ++i) {
|
2020-07-24 00:41:12 +00:00
|
|
|
if (NULL != r->plugin_ctx[i] || NULL != con->plugin_ctx[i]) {
|
2020-01-13 02:51:12 +00:00
|
|
|
connection_plugin_ctx_check(srv, r);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-07-24 00:41:12 +00:00
|
|
|
#endif
|
2020-01-13 02:51:12 +00:00
|
|
|
|
|
|
|
connection_set_state(r, CON_STATE_CONNECT);
|
|
|
|
buffer_clear(&r->uri.authority);
|
|
|
|
buffer_reset(&r->uri.path);
|
|
|
|
buffer_reset(&r->uri.query);
|
|
|
|
buffer_reset(&r->target_orig);
|
|
|
|
buffer_reset(&r->target); /*(see comments in connection_reset())*/
|
|
|
|
buffer_reset(&r->pathinfo); /*(see comments in connection_reset())*/
|
|
|
|
|
2019-02-05 02:50:53 +00:00
|
|
|
chunkqueue_reset(con->read_queue);
|
2020-01-13 02:51:12 +00:00
|
|
|
con->request_count = 0;
|
|
|
|
con->is_ssl_sock = 0;
|
2019-02-05 02:50:53 +00:00
|
|
|
|
2019-03-01 04:58:49 +00:00
|
|
|
fdevent_fdnode_event_del(srv->ev, con->fdn);
|
2005-02-20 14:27:00 +00:00
|
|
|
fdevent_unregister(srv->ev, con->fd);
|
2019-03-01 04:58:49 +00:00
|
|
|
con->fdn = NULL;
|
2005-02-20 14:27:00 +00:00
|
|
|
#ifdef __WIN32
|
2019-11-25 06:54:08 +00:00
|
|
|
if (0 == closesocket(con->fd))
|
2005-02-20 14:27:00 +00:00
|
|
|
#else
|
2019-11-25 06:54:08 +00:00
|
|
|
if (0 == close(con->fd))
|
2005-02-20 14:27:00 +00:00
|
|
|
#endif
|
2019-11-25 06:54:08 +00:00
|
|
|
--srv->cur_fds;
|
|
|
|
else
|
2020-01-13 02:51:12 +00:00
|
|
|
log_perror(r->conf.errh, __FILE__, __LINE__,
|
2019-11-25 06:54:08 +00:00
|
|
|
"(warning) close: %d", con->fd);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->conf.log_state_handling) {
|
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
2019-11-25 06:54:08 +00:00
|
|
|
"connection closed for fd %d", con->fd);
|
2016-12-13 19:05:47 +00:00
|
|
|
}
|
2013-12-24 04:28:44 +00:00
|
|
|
con->fd = -1;
|
2017-01-19 11:11:17 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
connection_del(srv, con);
|
|
|
|
}
|
|
|
|
|
2019-12-04 06:35:27 +00:00
|
|
|
static void connection_read_for_eos_plain(connection * const con) {
|
2016-07-26 19:55:45 +00:00
|
|
|
/* we have to do the linger_on_close stuff regardless
|
2020-01-13 02:51:12 +00:00
|
|
|
* of r->keep_alive; even non-keepalive sockets
|
2020-01-06 02:34:38 +00:00
|
|
|
* may still have unread data, and closing before reading
|
2016-07-26 19:55:45 +00:00
|
|
|
* 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)) */
|
2019-12-04 06:35:27 +00:00
|
|
|
con->close_timeout_ts = log_epoch_secs - (HTTP_LINGER_TIMEOUT+1);
|
2016-12-13 19:05:47 +00:00
|
|
|
}
|
|
|
|
|
2019-12-04 06:35:27 +00:00
|
|
|
static void connection_read_for_eos_ssl(connection * const con) {
|
2019-11-25 06:54:08 +00:00
|
|
|
if (con->network_read(con, con->read_queue, MAX_READ_LIMIT) < 0)
|
2019-12-04 06:35:27 +00:00
|
|
|
con->close_timeout_ts = log_epoch_secs - (HTTP_LINGER_TIMEOUT+1);
|
2019-02-18 18:16:49 +00:00
|
|
|
chunkqueue_reset(con->read_queue);
|
|
|
|
}
|
|
|
|
|
2019-12-04 06:35:27 +00:00
|
|
|
static void connection_read_for_eos(connection * const con) {
|
2019-02-18 18:16:49 +00:00
|
|
|
!con->is_ssl_sock
|
2019-12-04 06:35:27 +00:00
|
|
|
? connection_read_for_eos_plain(con)
|
|
|
|
: connection_read_for_eos_ssl(con);
|
2019-02-18 18:16:49 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 04:01:41 +00:00
|
|
|
static void connection_handle_close_state(connection *con) {
|
2019-12-04 06:35:27 +00:00
|
|
|
connection_read_for_eos(con);
|
2016-07-26 19:55:45 +00:00
|
|
|
|
2019-12-04 06:35:27 +00:00
|
|
|
if (log_epoch_secs - con->close_timeout_ts > HTTP_LINGER_TIMEOUT) {
|
2019-12-05 04:01:41 +00:00
|
|
|
connection_close(con);
|
2016-07-26 19:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-04 06:35:27 +00:00
|
|
|
static void connection_handle_shutdown(connection *con) {
|
2019-11-26 07:13:05 +00:00
|
|
|
plugins_call_handle_connection_shut_wr(con);
|
2016-07-26 19:55:45 +00:00
|
|
|
|
2019-11-26 07:13:05 +00:00
|
|
|
connection_reset(con);
|
2019-12-05 04:01:41 +00:00
|
|
|
++con->srv->con_closed;
|
2016-07-26 19:55:45 +00:00
|
|
|
|
|
|
|
/* close the connection */
|
2019-02-18 18:16:49 +00:00
|
|
|
if (con->fd >= 0
|
|
|
|
&& (con->is_ssl_sock || 0 == shutdown(con->fd, SHUT_WR))) {
|
2019-12-04 06:35:27 +00:00
|
|
|
con->close_timeout_ts = log_epoch_secs;
|
2016-07-26 19:55:45 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
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__,
|
2019-11-25 06:54:08 +00:00
|
|
|
"shutdown for fd %d", con->fd);
|
2016-07-26 19:55:45 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-12-05 04:01:41 +00:00
|
|
|
connection_close(con);
|
2016-07-26 19:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-04 06:10:02 +00:00
|
|
|
__attribute_cold__
|
2019-11-25 06:54:08 +00:00
|
|
|
static void connection_fdwaitqueue_append(connection *con) {
|
|
|
|
connection_list_append(&con->srv->fdwaitqueue, con);
|
2019-10-04 06:10:02 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static void connection_handle_response_end_state(request_st * const r, connection * const con) {
|
|
|
|
/* 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);
|
2016-07-26 19:55:45 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->state != CON_STATE_ERROR) ++con->srv->con_written;
|
2016-07-26 19:55:45 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->reqbody_length != r->reqbody_queue->bytes_in
|
|
|
|
|| r->state == CON_STATE_ERROR) {
|
2016-07-26 19:55:45 +00:00
|
|
|
/* request body is present and has not been read completely */
|
2020-01-13 02:51:12 +00:00
|
|
|
r->keep_alive = 0;
|
2016-07-26 19:55:45 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->keep_alive) {
|
2019-11-26 07:13:05 +00:00
|
|
|
connection_reset(con);
|
2016-07-26 19:55:45 +00:00
|
|
|
#if 0
|
2020-01-13 02:51:12 +00:00
|
|
|
r->start_ts = con->read_idle_ts = log_epoch_secs;
|
2016-07-26 19:55:45 +00:00
|
|
|
#endif
|
2020-01-13 02:51:12 +00:00
|
|
|
connection_set_state(r, CON_STATE_REQUEST_START);
|
2016-07-26 19:55:45 +00:00
|
|
|
} else {
|
2019-12-04 06:35:27 +00:00
|
|
|
connection_handle_shutdown(con);
|
2016-07-26 19:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 18:32:46 +00:00
|
|
|
__attribute_cold__
|
2020-01-13 02:51:12 +00:00
|
|
|
static void connection_handle_errdoc_init(request_st * const r) {
|
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;
|
2020-01-13 02:51:12 +00:00
|
|
|
if (401 == r->http_status) {
|
|
|
|
const buffer *vb = http_header_response_get(r, HTTP_HEADER_OTHER, CONST_STR_LEN("WWW-Authenticate"));
|
2018-09-09 05:50:33 +00:00
|
|
|
if (NULL != vb) www_auth = buffer_init_buffer(vb);
|
2016-05-11 01:38:09 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
buffer_reset(&r->physical.path);
|
|
|
|
r->resp_htags = 0;
|
|
|
|
array_reset_data_strings(&r->resp_headers);
|
|
|
|
http_response_body_clear(r, 0);
|
2016-05-11 01:38:09 +00:00
|
|
|
|
|
|
|
if (NULL != www_auth) {
|
2020-01-13 02:51:12 +00:00
|
|
|
http_header_response_set(r, 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
|
|
|
}
|
|
|
|
|
2019-11-23 18:32:46 +00:00
|
|
|
__attribute_cold__
|
2020-01-13 02:51:12 +00:00
|
|
|
static void connection_handle_errdoc(request_st * const r) {
|
|
|
|
if (NULL == r->handler_module
|
|
|
|
? r->error_handler_saved_status >= 65535
|
|
|
|
: (!r->conf.error_intercept||r->error_handler_saved_status))
|
2019-11-23 18:32:46 +00:00
|
|
|
return;
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
connection_handle_errdoc_init(r);
|
|
|
|
r->resp_body_finished = 1;
|
2019-11-23 18:32:46 +00:00
|
|
|
|
|
|
|
/* try to send static errorfile */
|
2020-01-13 02:51:12 +00:00
|
|
|
if (!buffer_string_is_empty(r->conf.errorfile_prefix)) {
|
|
|
|
buffer_copy_buffer(&r->physical.path, r->conf.errorfile_prefix);
|
|
|
|
buffer_append_int(&r->physical.path, r->http_status);
|
|
|
|
buffer_append_string_len(&r->physical.path, CONST_STR_LEN(".html"));
|
|
|
|
if (0 == http_chunk_append_file(r, &r->physical.path)) {
|
|
|
|
stat_cache_entry *sce = stat_cache_get_entry(&r->physical.path);
|
2019-12-05 08:16:25 +00:00
|
|
|
const buffer *content_type = (NULL != sce)
|
2020-01-13 02:51:12 +00:00
|
|
|
? stat_cache_content_type_get(sce, r)
|
2019-12-05 08:16:25 +00:00
|
|
|
: NULL;
|
|
|
|
if (content_type)
|
2020-01-13 02:51:12 +00:00
|
|
|
http_header_response_set(r, HTTP_HEADER_CONTENT_TYPE,
|
2019-11-23 18:32:46 +00:00
|
|
|
CONST_STR_LEN("Content-Type"),
|
2019-12-05 08:16:25 +00:00
|
|
|
CONST_BUF_LEN(content_type));
|
2019-11-23 18:32:46 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* build default error-page */
|
2020-01-13 02:51:12 +00:00
|
|
|
buffer_reset(&r->physical.path);
|
|
|
|
buffer * const b = r->tmp_buf;
|
2019-11-23 18:32:46 +00:00
|
|
|
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>"));
|
2020-01-13 02:51:12 +00:00
|
|
|
http_status_append(b, r->http_status);
|
2019-11-23 18:32:46 +00:00
|
|
|
buffer_append_string_len(b, CONST_STR_LEN(
|
|
|
|
"</title>\n"
|
|
|
|
" </head>\n"
|
|
|
|
" <body>\n"
|
|
|
|
" <h1>"));
|
2020-01-13 02:51:12 +00:00
|
|
|
http_status_append(b, r->http_status);
|
2019-11-23 18:32:46 +00:00
|
|
|
buffer_append_string_len(b, CONST_STR_LEN(
|
|
|
|
"</h1>\n"
|
|
|
|
" </body>\n"
|
|
|
|
"</html>\n"));
|
2020-01-13 02:51:12 +00:00
|
|
|
(void)http_chunk_append_mem(r, CONST_BUF_LEN(b));
|
2019-11-23 18:32:46 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
http_header_response_set(r, HTTP_HEADER_CONTENT_TYPE,
|
2019-11-23 18:32:46 +00:00
|
|
|
CONST_STR_LEN("Content-Type"),
|
|
|
|
CONST_STR_LEN("text/html"));
|
|
|
|
}
|
|
|
|
|
2020-07-28 11:32:29 +00:00
|
|
|
__attribute_cold__
|
|
|
|
static void
|
|
|
|
connection_merge_trailers (request_st * const r)
|
|
|
|
{
|
|
|
|
/* attempt to merge trailers into headers; header not yet sent by caller */
|
|
|
|
if (buffer_string_is_empty(&r->gw_dechunk->b)) return;
|
|
|
|
const int done = r->gw_dechunk->done;
|
|
|
|
if (!done) return; /* XXX: !done; could scan for '\n' and send only those */
|
|
|
|
|
|
|
|
/* do not include trailers if success status (when response was read from
|
|
|
|
* backend) subsequently changed to error status. http_chunk could add the
|
|
|
|
* trailers, but such actions are better on a different code layer than in
|
|
|
|
* http_chunk.c */
|
|
|
|
if (done < 400 && r->http_status >= 400) return;
|
|
|
|
|
|
|
|
/* XXX: trailers passed through; no sanity check currently done
|
|
|
|
* https://tools.ietf.org/html/rfc7230#section-4.1.2
|
|
|
|
*
|
|
|
|
* Not checking for disallowed fields
|
|
|
|
* Not handling (deprecated) line wrapping
|
|
|
|
* Not strictly checking fields
|
|
|
|
*/
|
|
|
|
const char *k = strchr(r->gw_dechunk->b.ptr, '\n'); /*(skip final chunk)*/
|
|
|
|
if (NULL == k) return; /*(should not happen)*/
|
|
|
|
++k;
|
|
|
|
for (const char *v, *e; (e = strchr(k, '\n')); k = e+1) {
|
|
|
|
v = memchr(k, ':', (size_t)(e - k));
|
|
|
|
if (NULL == v || v == k || *k == ' ' || *k == '\t') continue;
|
|
|
|
uint32_t klen = (uint32_t)(v - k);
|
|
|
|
do { ++v; } while (*v == ' ' || *v == '\t');
|
|
|
|
if (*v == '\r' || *v == '\n') continue;
|
|
|
|
enum http_header_e id = http_header_hkey_get(k, klen);
|
|
|
|
http_header_response_insert(r, id, k, klen, v, (size_t)(e - v));
|
|
|
|
}
|
|
|
|
http_header_response_unset(r, HTTP_HEADER_OTHER, CONST_STR_LEN("Trailer"));
|
|
|
|
buffer_clear(&r->gw_dechunk->b);
|
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static int connection_handle_write_prepare(request_st * const r) {
|
|
|
|
if (NULL == r->handler_module) {
|
2005-08-15 09:55:23 +00:00
|
|
|
/* static files */
|
2020-01-13 02:51:12 +00:00
|
|
|
switch(r->http_method) {
|
2005-08-15 09:55:23 +00:00
|
|
|
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
|
2020-02-22 22:24:12 +00:00
|
|
|
* 403 is from the response handler when no module handled request
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2006-01-03 13:59:46 +00:00
|
|
|
* */
|
2020-01-13 02:51:12 +00:00
|
|
|
if ((!r->http_status || r->http_status == 200)
|
|
|
|
&& !buffer_string_is_empty(&r->uri.path)
|
|
|
|
&& r->uri.path.ptr[0] != '*') {
|
|
|
|
http_response_body_clear(r, 0);
|
|
|
|
http_header_response_append(r, HTTP_HEADER_OTHER, CONST_STR_LEN("Allow"), CONST_STR_LEN("OPTIONS, GET, HEAD, POST"));
|
|
|
|
r->http_status = 200;
|
|
|
|
r->resp_body_finished = 1;
|
2005-08-19 08:37:52 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
2005-08-15 09:55:23 +00:00
|
|
|
default:
|
2020-01-13 02:51:12 +00:00
|
|
|
if (0 == r->http_status) {
|
|
|
|
r->http_status = 501;
|
2005-08-15 09:55:23 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->http_status == 0) {
|
|
|
|
r->http_status = 403;
|
2005-08-15 09:55:23 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
switch(r->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 */
|
2020-01-13 02:51:12 +00:00
|
|
|
http_response_body_clear(r, 1);
|
|
|
|
r->resp_body_finished = 1;
|
2008-04-23 19:10:42 +00:00
|
|
|
break;
|
|
|
|
default: /* class: header + body */
|
|
|
|
/* only custom body for 4xx and 5xx */
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->http_status >= 400 && r->http_status < 600)
|
|
|
|
connection_handle_errdoc(r);
|
2005-08-15 09:55:23 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-07-28 11:32:29 +00:00
|
|
|
if (r->gw_dechunk)
|
|
|
|
connection_merge_trailers(r);
|
|
|
|
|
2016-06-22 00:28:55 +00:00
|
|
|
/* Allow filter plugins to change response headers before they are written. */
|
2020-01-13 02:51:12 +00:00
|
|
|
switch(plugins_call_handle_response_start(r)) {
|
2016-06-22 00:28:55 +00:00
|
|
|
case HANDLER_GO_ON:
|
|
|
|
case HANDLER_FINISHED:
|
|
|
|
break;
|
|
|
|
default:
|
2020-01-13 02:51:12 +00:00
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
|
|
|
"response_start plugin failed");
|
2016-06-22 00:28:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->resp_body_finished) {
|
2006-10-04 13:26:23 +00:00
|
|
|
/* we have all the content and chunked encoding is not used, set a content-length */
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (!(r->resp_htags & (HTTP_HEADER_CONTENT_LENGTH|HTTP_HEADER_TRANSFER_ENCODING))) {
|
|
|
|
off_t qlen = chunkqueue_length(r->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
|
|
|
|
*/
|
2020-01-13 02:51:12 +00:00
|
|
|
if ((r->http_status >= 100 && r->http_status < 200) ||
|
|
|
|
r->http_status == 204 ||
|
|
|
|
r->http_status == 304) {
|
2007-08-18 10:40:20 +00:00
|
|
|
/* no Content-Body, no Content-Length */
|
2020-01-13 02:51:12 +00:00
|
|
|
http_header_response_unset(r, HTTP_HEADER_CONTENT_LENGTH, CONST_STR_LEN("Content-Length"));
|
2020-01-22 04:49:34 +00:00
|
|
|
} else if (qlen > 0) {
|
2020-01-13 02:51:12 +00:00
|
|
|
buffer * const tb = r->tmp_buf;
|
|
|
|
buffer_clear(tb);
|
|
|
|
buffer_append_int(tb, qlen);
|
|
|
|
http_header_response_set(r, HTTP_HEADER_CONTENT_LENGTH,
|
2019-11-25 06:54:08 +00:00
|
|
|
CONST_STR_LEN("Content-Length"),
|
|
|
|
CONST_BUF_LEN(tb));
|
2020-01-22 04:49:34 +00:00
|
|
|
} else if (r->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
|
|
|
|
*/
|
|
|
|
http_header_response_set(r, HTTP_HEADER_CONTENT_LENGTH,
|
|
|
|
CONST_STR_LEN("Content-Length"),
|
|
|
|
CONST_STR_LEN("0"));
|
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
|
|
|
*/
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (!(r->resp_htags & (HTTP_HEADER_CONTENT_LENGTH|HTTP_HEADER_TRANSFER_ENCODING|HTTP_HEADER_UPGRADE))) {
|
|
|
|
if (r->http_method == HTTP_METHOD_CONNECT
|
|
|
|
&& r->http_status == 200) {
|
2017-11-25 23:38:09 +00:00
|
|
|
/*(no transfer-encoding if successful CONNECT)*/
|
2020-01-13 02:51:12 +00:00
|
|
|
} else if (r->http_version == HTTP_VERSION_1_1) {
|
|
|
|
off_t qlen = chunkqueue_length(r->write_queue);
|
|
|
|
r->resp_send_chunked = 1;
|
2016-06-20 01:05:25 +00:00
|
|
|
if (qlen) {
|
|
|
|
/* create initial Transfer-Encoding: chunked segment */
|
2020-01-13 02:51:12 +00:00
|
|
|
buffer * const b = chunkqueue_prepend_buffer_open(r->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"));
|
2020-01-13 02:51:12 +00:00
|
|
|
chunkqueue_prepend_buffer_commit(r->write_queue);
|
|
|
|
chunkqueue_append_mem(r->write_queue, CONST_STR_LEN("\r\n"));
|
2016-06-20 01:05:25 +00:00
|
|
|
}
|
2020-01-13 02:51:12 +00:00
|
|
|
http_header_response_append(r, HTTP_HEADER_TRANSFER_ENCODING, CONST_STR_LEN("Transfer-Encoding"), CONST_STR_LEN("chunked"));
|
2016-06-20 01:05:25 +00:00
|
|
|
} else {
|
2020-01-13 02:51:12 +00:00
|
|
|
r->keep_alive = 0;
|
2016-06-20 01:05:25 +00:00
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->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
|
|
|
|
*/
|
2020-01-13 02:51:12 +00:00
|
|
|
http_response_body_clear(r, 1);
|
|
|
|
r->resp_body_finished = 1;
|
2005-08-15 09:55:23 +00:00
|
|
|
}
|
2005-08-19 08:37:52 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
http_response_write_header(r);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-11-26 07:13:05 +00:00
|
|
|
static void connection_handle_write(connection *con) {
|
2020-01-13 02:51:12 +00:00
|
|
|
int rc = connection_write_chunkqueue(con, con->write_queue, MAX_WRITE_LIMIT);
|
|
|
|
request_st * const r = &con->request;
|
|
|
|
switch (rc) {
|
2005-02-20 14:27:00 +00:00
|
|
|
case 0:
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->resp_body_finished) {
|
|
|
|
connection_set_state(r, CON_STATE_RESPONSE_END);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case -1: /* error on our side */
|
2020-01-13 02:51:12 +00:00
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
2019-11-25 06:54:08 +00:00
|
|
|
"connection closed: write failed on fd %d", con->fd);
|
2020-01-13 02:51:12 +00:00
|
|
|
connection_set_state(r, CON_STATE_ERROR);
|
2005-02-20 14:27:00 +00:00
|
|
|
break;
|
|
|
|
case -2: /* remote close */
|
2020-01-13 02:51:12 +00:00
|
|
|
connection_set_state(r, CON_STATE_ERROR);
|
2005-02-20 14:27:00 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static void connection_handle_write_state(request_st * const r, connection * const con) {
|
2019-02-05 02:50:53 +00:00
|
|
|
do {
|
|
|
|
/* only try to write if we have something in the queue */
|
|
|
|
if (!chunkqueue_is_empty(con->write_queue)) {
|
|
|
|
if (con->is_writable) {
|
2019-11-26 07:13:05 +00:00
|
|
|
connection_handle_write(con);
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->state != CON_STATE_WRITE) break;
|
2019-02-05 02:50:53 +00:00
|
|
|
}
|
2020-01-13 02:51:12 +00:00
|
|
|
} else if (r->resp_body_finished) {
|
|
|
|
connection_set_state(r, CON_STATE_RESPONSE_END);
|
2019-02-05 02:50:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->handler_module && !r->resp_body_finished) {
|
|
|
|
const plugin * const p = r->handler_module;
|
|
|
|
int rc = p->handle_subrequest(r, p->data);
|
2019-12-08 01:50:42 +00:00
|
|
|
switch(rc) {
|
2019-02-05 02:50:53 +00:00
|
|
|
case HANDLER_WAIT_FOR_EVENT:
|
|
|
|
case HANDLER_FINISHED:
|
|
|
|
case HANDLER_GO_ON:
|
|
|
|
break;
|
|
|
|
case HANDLER_WAIT_FOR_FD:
|
2019-11-25 06:54:08 +00:00
|
|
|
connection_fdwaitqueue_append(con);
|
2019-02-05 02:50:53 +00:00
|
|
|
break;
|
|
|
|
case HANDLER_COMEBACK:
|
|
|
|
default:
|
2020-01-13 02:51:12 +00:00
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
2019-11-25 06:54:08 +00:00
|
|
|
"unexpected subrequest handler ret-value: %d %d",
|
|