2
0
Fork 0
lighttpd2/src/main/virtualrequest.c

594 lines
16 KiB
C
Raw Normal View History

#include <lighttpd/base.h>
#include <lighttpd/plugin_core.h>
#include <lighttpd/filter_buffer_on_disk.h>
static void vrequest_job_cb(liJob *job) {
liVRequest *vr = LI_CONTAINER_OF(job, liVRequest, job);
li_vrequest_state_machine(vr);
}
2010-08-25 22:34:28 +00:00
liVRequest* li_vrequest_new(liWorker *wrk, liConInfo *coninfo) {
liServer *srv = wrk->srv;
liVRequest *vr = g_slice_new0(liVRequest);
vr->coninfo = coninfo;
2010-08-25 22:34:28 +00:00
vr->wrk = wrk;
vr->state = LI_VRS_CLEAN;
2013-05-22 15:26:33 +00:00
vr->backend = NULL;
vr->backend_drain = NULL;
vr->backend_source = NULL;
vr->direct_out = NULL;
vr->plugin_ctx = g_ptr_array_new();
g_ptr_array_set_size(vr->plugin_ctx, g_hash_table_size(srv->plugins));
vr->options = g_slice_copy(srv->option_def_values->len * sizeof(liOptionValue), srv->option_def_values->data);
vr->optionptrs = g_slice_copy(srv->optionptr_def_values->len * sizeof(liOptionPtrValue*), srv->optionptr_def_values->data);
{
guint i;
for (i = 0; i < srv->optionptr_def_values->len; i++) {
if (vr->optionptrs[i]) {
g_atomic_int_inc(&vr->optionptrs[i]->refcount);
}
}
}
2009-07-09 20:17:24 +00:00
li_request_init(&vr->request);
li_physical_init(&vr->physical);
li_response_init(&vr->response);
li_environment_init(&vr->env);
2013-05-22 15:26:33 +00:00
li_vrequest_filters_init(vr);
2013-05-22 15:26:33 +00:00
li_action_stack_init(&vr->action_stack);
li_job_init(&vr->job, vrequest_job_cb);
2009-03-26 22:05:17 +00:00
vr->stat_cache_entries = g_ptr_array_sized_new(2);
return vr;
}
2009-07-09 20:17:24 +00:00
void li_vrequest_free(liVRequest* vr) {
liServer *srv = vr->wrk->srv;
li_stream_safe_reset_and_release(&vr->backend_drain);
vr->direct_out = NULL;
li_stream_safe_reset_and_release(&vr->backend_source);
li_filter_buffer_on_disk_stop(vr->in_buffer_on_disk_stream);
li_stream_safe_reset_and_release(&vr->in_buffer_on_disk_stream);
li_stream_safe_reset_and_release(&vr->wait_for_request_body_stream);
2013-05-22 15:26:33 +00:00
2009-07-09 20:17:24 +00:00
li_action_stack_clear(vr, &vr->action_stack);
if (vr->state != LI_VRS_CLEAN) {
li_plugins_handle_vrclose(vr);
2013-05-22 15:26:33 +00:00
vr->state = LI_VRS_CLEAN;
vr->backend = NULL;
}
g_ptr_array_free(vr->plugin_ctx, TRUE);
2013-05-22 15:26:33 +00:00
vr->plugin_ctx = NULL;
2009-07-09 20:17:24 +00:00
li_request_clear(&vr->request);
li_physical_clear(&vr->physical);
li_response_clear(&vr->response);
li_environment_clear(&vr->env);
2013-05-22 15:26:33 +00:00
li_vrequest_filters_clear(vr);
li_job_clear(&vr->job);
g_slice_free1(srv->option_def_values->len * sizeof(liOptionValue), vr->options);
{
guint i;
for (i = 0; i < srv->optionptr_def_values->len; i++) {
li_release_optionptr(srv, vr->optionptrs[i]);
}
}
g_slice_free1(srv->optionptr_def_values->len * sizeof(liOptionPtrValue*), vr->optionptrs);
li_log_context_set(&vr->log_context, NULL);
2009-03-26 22:05:17 +00:00
2009-10-05 17:41:48 +00:00
while (vr->stat_cache_entries->len > 0 ) {
liStatCacheEntry *sce = g_ptr_array_index(vr->stat_cache_entries, 0);
2009-07-09 20:17:24 +00:00
li_stat_cache_entry_release(vr, sce);
2009-03-26 22:05:17 +00:00
}
g_ptr_array_free(vr->stat_cache_entries, TRUE);
g_slice_free(liVRequest, vr);
}
void li_vrequest_reset(liVRequest *vr, gboolean keepalive) {
liServer *srv = vr->wrk->srv;
2013-05-22 15:26:33 +00:00
if (NULL != vr->backend_drain) {
li_stream_disconnect(vr->backend_drain);
li_stream_release(vr->backend_drain);
vr->backend_drain = NULL;
}
if (NULL != vr->backend_source) {
if (NULL == vr->backend_source->dest) {
/* wasn't connected: disconnect source */
li_stream_disconnect(vr->backend_source);
}
li_stream_disconnect_dest(vr->backend_source);
li_stream_release(vr->backend_source);
vr->backend_source = NULL;
vr->direct_out = NULL;
}
li_filter_buffer_on_disk_stop(vr->in_buffer_on_disk_stream);
li_stream_safe_reset_and_release(&vr->in_buffer_on_disk_stream);
li_stream_safe_reset_and_release(&vr->wait_for_request_body_stream);
2009-07-09 20:17:24 +00:00
li_action_stack_reset(vr, &vr->action_stack);
if (vr->state != LI_VRS_CLEAN) {
li_plugins_handle_vrclose(vr);
2013-05-22 15:26:33 +00:00
vr->state = LI_VRS_CLEAN;
vr->backend = NULL;
}
{
gint len = vr->plugin_ctx->len;
g_ptr_array_set_size(vr->plugin_ctx, 0);
g_ptr_array_set_size(vr->plugin_ctx, len);
}
/* don't reset request for keep-alive tracking */
if (!keepalive) li_request_reset(&vr->request);
2009-07-09 20:17:24 +00:00
li_physical_reset(&vr->physical);
li_response_reset(&vr->response);
li_environment_reset(&vr->env);
2013-05-22 15:26:33 +00:00
li_vrequest_filters_reset(vr);
li_job_reset(&vr->job);
2009-10-05 17:41:48 +00:00
while (vr->stat_cache_entries->len > 0 ) {
liStatCacheEntry *sce = g_ptr_array_index(vr->stat_cache_entries, 0);
2009-07-09 20:17:24 +00:00
li_stat_cache_entry_release(vr, sce);
2009-03-01 20:16:58 +00:00
}
memcpy(vr->options, srv->option_def_values->data, srv->option_def_values->len * sizeof(liOptionValue));
{
guint i;
for (i = 0; i < srv->optionptr_def_values->len; i++) {
liOptionPtrValue *oval = g_array_index(srv->optionptr_def_values, liOptionPtrValue*, i);
if (vr->optionptrs[i] != oval) {
li_release_optionptr(srv, vr->optionptrs[i]);
if (oval)
g_atomic_int_inc(&oval->refcount);
vr->optionptrs[i] = oval;
}
}
}
li_log_context_set(&vr->log_context, NULL);
}
2009-07-09 20:17:24 +00:00
void li_vrequest_error(liVRequest *vr) {
vr->state = LI_VRS_ERROR;
2013-05-22 15:26:33 +00:00
li_stream_reset(vr->coninfo->req);
li_stream_reset(vr->coninfo->resp);
2009-07-09 20:17:24 +00:00
li_vrequest_joblist_append(vr);
}
2009-07-09 20:17:24 +00:00
void li_vrequest_backend_error(liVRequest *vr, liBackendError berror) {
2013-05-22 15:26:33 +00:00
if (vr->state < LI_VRS_READ_CONTENT) {
vr->action_stack.backend_failed = TRUE;
vr->action_stack.backend_error = berror;
li_vrequest_joblist_append(vr);
} else {
vr->response.http_status = 503;
li_vrequest_error(vr);
}
}
2009-07-09 20:17:24 +00:00
void li_vrequest_backend_overloaded(liVRequest *vr) {
li_vrequest_backend_error(vr, LI_BACKEND_OVERLOAD);
}
2009-07-09 20:17:24 +00:00
void li_vrequest_backend_dead(liVRequest *vr) {
li_vrequest_backend_error(vr, LI_BACKEND_DEAD);
}
/* resets fields which weren't reset in favor of keep-alive tracking */
void li_vrequest_start(liVRequest *vr) {
if (LI_VRS_CLEAN == vr->state) {
li_request_reset(&vr->request);
}
vr->ts_started = li_cur_ts(vr->wrk);
}
/* received all request headers */
2009-07-09 20:17:24 +00:00
void li_vrequest_handle_request_headers(liVRequest *vr) {
if (LI_VRS_CLEAN == vr->state) {
vr->state = LI_VRS_HANDLE_REQUEST_HEADERS;
}
2009-07-09 20:17:24 +00:00
li_vrequest_joblist_append(vr);
}
typedef struct {
liStream stream;
liVRequest *vr;
gboolean have_mem_chunk, ready;
} wait_for_request_body_stream;
static void wait_for_request_body_stream_cb(liStream *stream, liStreamEvent event) {
wait_for_request_body_stream *ws = LI_CONTAINER_OF(stream, wait_for_request_body_stream, stream);
switch (event) {
case LI_STREAM_NEW_DATA:
if (NULL == ws->stream.source) return;
if (ws->have_mem_chunk || ws->ready) {
li_chunkqueue_steal_all(ws->stream.out, ws->stream.source->out);
} else {
liChunkQueue *in = ws->stream.source->out, *out = ws->stream.out;
while (in->length > 0) {
liChunk *c = li_chunkqueue_first_chunk(in);
assert(NULL != c);
if (FILE_CHUNK != c->type) {
ws->have_mem_chunk = TRUE;
li_chunkqueue_steal_all(out, in);
break;
}
li_chunkqueue_steal_chunk(out, in);
}
}
if (ws->stream.source->out->is_closed) {
ws->stream.out->is_closed = TRUE;
}
if (!ws->ready && (ws->stream.out->is_closed ||
(ws->have_mem_chunk && li_chunkqueue_limit_available(ws->stream.out) < 1024))) {
ws->ready = TRUE;
if (NULL != ws->vr) li_vrequest_joblist_append(ws->vr);
ws->vr = NULL;
}
li_stream_notify(stream);
break;
case LI_STREAM_NEW_CQLIMIT:
break;
case LI_STREAM_CONNECTED_DEST:
ws->ready = TRUE;
ws->vr = NULL;
break;
case LI_STREAM_CONNECTED_SOURCE:
break;
case LI_STREAM_DISCONNECTED_DEST:
if (!ws->stream.out->is_closed || 0 != ws->stream.out->length) {
li_stream_disconnect(stream);
}
break;
case LI_STREAM_DISCONNECTED_SOURCE:
if (!ws->stream.out->is_closed) {
li_stream_disconnect_dest(stream);
if (!ws->ready) {
ws->ready = TRUE;
if (NULL != ws->vr) li_vrequest_joblist_append(ws->vr);
ws->vr = NULL;
}
}
break;
case LI_STREAM_DESTROY:
g_slice_free(wait_for_request_body_stream, ws);
break;
}
}
static liStream* wait_for_request_body_stream_new(liVRequest *vr) {
wait_for_request_body_stream *ws = g_slice_new0(wait_for_request_body_stream);
ws->vr = vr;
li_stream_init(&ws->stream, &vr->wrk->loop, wait_for_request_body_stream_cb);
return &ws->stream;
}
static gboolean wait_for_request_body_stream_ready(liStream *stream) {
wait_for_request_body_stream *ws;
if (NULL == stream) return FALSE;
assert(wait_for_request_body_stream_cb == stream->cb);
ws = LI_CONTAINER_OF(stream, wait_for_request_body_stream, stream);
return ws->ready;
}
gboolean li_vrequest_wait_for_request_body(liVRequest *vr) {
goffset lim_avail;
/* too late to wait? */
if (vr->state > LI_VRS_HANDLE_REQUEST_HEADERS) return TRUE;
if (0 == vr->request.content_length) return TRUE;
if (NULL != vr->wait_for_request_body_stream) {
if (wait_for_request_body_stream_ready(vr->wait_for_request_body_stream)) return TRUE;
return FALSE; /* still waiting */
}
lim_avail = li_chunkqueue_limit_available(vr->coninfo->req->out);
vr->wait_for_request_body_stream = wait_for_request_body_stream_new(vr);
if (vr->request.content_length < 0 || lim_avail < 0 || vr->request.content_length > lim_avail) {
vr->in_buffer_on_disk_stream = li_filter_buffer_on_disk(vr, -1, FALSE);
li_stream_connect(vr->coninfo->req, vr->in_buffer_on_disk_stream);
li_stream_connect(vr->in_buffer_on_disk_stream, vr->wait_for_request_body_stream);
} else {
li_stream_connect(vr->coninfo->req, vr->wait_for_request_body_stream);
}
return FALSE;
}
2013-05-22 15:26:33 +00:00
/* response completely ready */
gboolean li_vrequest_handle_direct(liVRequest *vr) {
if (li_vrequest_handle_indirect(vr, NULL)) {
li_vrequest_indirect_connect(vr, li_stream_null_new(&vr->wrk->loop), li_stream_plug_new(&vr->wrk->loop));
2013-05-22 15:26:33 +00:00
/* release reference from _new */
li_stream_release(vr->backend_drain);
li_stream_release(vr->backend_source);
2013-05-22 15:26:33 +00:00
vr->direct_out = vr->backend_source->out;
vr->direct_out->is_closed = TRUE;
li_vrequest_indirect_headers_ready(vr);
return TRUE;
} else {
return FALSE;
}
}
/* handle request over time */
2009-07-09 20:17:24 +00:00
gboolean li_vrequest_handle_indirect(liVRequest *vr, liPlugin *p) {
if (vr->state < LI_VRS_READ_CONTENT) {
vr->state = LI_VRS_READ_CONTENT;
vr->backend = p;
2013-05-22 15:26:33 +00:00
return TRUE;
} else {
return FALSE;
}
}
2013-05-22 15:26:33 +00:00
void li_vrequest_indirect_connect(liVRequest *vr, liStream *backend_drain, liStream* backend_source) {
liStream *req_in;
2013-05-22 15:26:33 +00:00
assert(LI_VRS_READ_CONTENT == vr->state);
assert(NULL != backend_drain);
assert(NULL != backend_source);
2013-05-22 15:26:33 +00:00
li_stream_acquire(backend_drain);
li_stream_acquire(backend_source);
2013-05-22 15:26:33 +00:00
vr->backend_drain = backend_drain;
if (NULL != vr->wait_for_request_body_stream) {
req_in = vr->wait_for_request_body_stream;
li_filter_buffer_on_disk_stop(vr->in_buffer_on_disk_stream);
} else {
req_in = vr->coninfo->req;
}
2013-05-22 15:26:33 +00:00
/* connect in-queue */
if (NULL != vr->filters_in_last) {
li_stream_connect(vr->filters_in_last, vr->backend_drain);
li_stream_connect(req_in, vr->filters_in_first);
2013-05-22 15:26:33 +00:00
} else {
/* no filters */
li_stream_connect(req_in, vr->backend_drain);
2013-05-22 15:26:33 +00:00
}
vr->backend_source = backend_source;
li_chunkqueue_set_limit(backend_source->out, vr->coninfo->resp->out->limit);
li_vrequest_joblist_append(vr);
}
/* received all response headers/status code - call once from your indirect handler */
void li_vrequest_indirect_headers_ready(liVRequest* vr) {
assert(LI_VRS_HANDLE_RESPONSE_HEADERS > vr->state);
vr->state = LI_VRS_HANDLE_RESPONSE_HEADERS;
li_vrequest_joblist_append(vr);
}
2009-07-09 20:17:24 +00:00
gboolean li_vrequest_is_handled(liVRequest *vr) {
return vr->state >= LI_VRS_READ_CONTENT;
2009-03-17 10:42:50 +00:00
}
static liHandlerResult vrequest_do_handle_actions(liVRequest *vr) {
2009-07-09 20:17:24 +00:00
liHandlerResult res = li_action_execute(vr);
switch (res) {
case LI_HANDLER_GO_ON:
if (vr->state == LI_VRS_HANDLE_REQUEST_HEADERS) {
/* request not handled */
2009-07-09 20:17:24 +00:00
li_vrequest_handle_direct(vr);
if (vr->request.http_method == LI_HTTP_METHOD_OPTIONS) {
vr->response.http_status = 200;
li_http_header_append(vr->response.headers, CONST_STR_LEN("Allow"), CONST_STR_LEN("OPTIONS, GET, HEAD, POST"));
} else {
vr->response.http_status = 404;
if (CORE_OPTION(LI_CORE_OPTION_DEBUG_REQUEST_HANDLING).boolean) {
VR_DEBUG(vr, "%s", "actions didn't handle request");
}
}
return LI_HANDLER_GO_ON;
}
/* otherwise state already changed */
break;
case LI_HANDLER_COMEBACK:
2009-07-09 20:17:24 +00:00
li_vrequest_joblist_append(vr); /* come back later */
return LI_HANDLER_COMEBACK;
case LI_HANDLER_WAIT_FOR_EVENT:
return LI_HANDLER_WAIT_FOR_EVENT;
case LI_HANDLER_ERROR:
return LI_HANDLER_ERROR;
}
return LI_HANDLER_GO_ON;
}
2009-07-09 20:17:24 +00:00
void li_vrequest_state_machine(liVRequest *vr) {
2013-05-22 15:26:33 +00:00
gboolean done;
do {
2013-05-22 15:26:33 +00:00
done = TRUE;
switch (vr->state) {
case LI_VRS_CLEAN:
break;
case LI_VRS_HANDLE_REQUEST_HEADERS:
if (CORE_OPTION(LI_CORE_OPTION_DEBUG_REQUEST_HANDLING).boolean) {
VR_DEBUG(vr, "%s", "handle request header");
}
switch (vrequest_do_handle_actions(vr)) {
case LI_HANDLER_GO_ON:
break;
case LI_HANDLER_COMEBACK:
li_vrequest_joblist_append(vr); /* come back later */
return;
case LI_HANDLER_WAIT_FOR_EVENT:
2013-05-22 15:26:33 +00:00
return;
case LI_HANDLER_ERROR:
li_vrequest_error(vr);
return;
}
2013-05-22 15:26:33 +00:00
done = FALSE;
break;
case LI_VRS_READ_CONTENT:
if (CORE_OPTION(LI_CORE_OPTION_DEBUG_REQUEST_HANDLING).boolean) {
VR_DEBUG(vr, "%s", "read content");
}
break;
case LI_VRS_HANDLE_RESPONSE_HEADERS:
if (CORE_OPTION(LI_CORE_OPTION_DEBUG_REQUEST_HANDLING).boolean) {
VR_DEBUG(vr, "%s", "handle response header");
}
switch (vrequest_do_handle_actions(vr)) {
case LI_HANDLER_GO_ON:
break;
case LI_HANDLER_COMEBACK:
2013-05-22 15:26:33 +00:00
li_vrequest_joblist_append(vr); /* come back later */
return;
case LI_HANDLER_WAIT_FOR_EVENT:
2013-05-22 15:26:33 +00:00
return;
case LI_HANDLER_ERROR:
2009-07-09 20:17:24 +00:00
li_vrequest_error(vr);
return;
}
2013-05-22 15:26:33 +00:00
if (LI_VRS_HANDLE_RESPONSE_HEADERS != vr->state) break;
vr->state = LI_VRS_WRITE_CONTENT;
2013-05-22 15:26:33 +00:00
/* connect out-queue to signal that the headers are ready */
if (NULL != vr->direct_out) vr->direct_out->is_closed = TRUE; /* make sure this is closed for direct responses */
if (NULL != vr->filters_out_last) {
li_stream_connect(vr->backend_source, vr->filters_out_first);
li_stream_connect(vr->filters_out_last, vr->coninfo->resp);
} else {
/* no filters */
li_stream_connect(vr->backend_source, vr->coninfo->resp);
}
done = FALSE;
break;
case LI_VRS_WRITE_CONTENT:
if (CORE_OPTION(LI_CORE_OPTION_DEBUG_REQUEST_HANDLING).boolean) {
VR_DEBUG(vr, "%s", "write content");
}
break;
case LI_VRS_ERROR:
if (CORE_OPTION(LI_CORE_OPTION_DEBUG_REQUEST_HANDLING).boolean) {
VR_DEBUG(vr, "%s", "error");
}
2013-05-22 15:26:33 +00:00
vr->coninfo->callbacks->handle_response_error(vr);
return;
}
} while (!done);
}
2009-07-09 20:17:24 +00:00
void li_vrequest_joblist_append(liVRequest *vr) {
li_job_later(&vr->wrk->loop.jobqueue, &vr->job);
}
liJobRef* li_vrequest_get_ref(liVRequest *vr) {
return li_job_ref(&vr->wrk->loop.jobqueue, &vr->job);
}
2009-07-09 20:17:24 +00:00
gboolean li_vrequest_redirect(liVRequest *vr, GString *uri) {
if (!li_vrequest_handle_direct(vr))
2009-03-01 20:16:58 +00:00
return FALSE;
vr->response.http_status = 301;
2009-07-09 20:17:24 +00:00
li_http_header_overwrite(vr->response.headers, CONST_STR_LEN("Location"), GSTR_LEN(uri));
2009-03-01 20:16:58 +00:00
return TRUE;
2009-03-11 15:57:07 +00:00
}
gboolean li_vrequest_redirect_directory(liVRequest *vr) {
GString *uri = vr->wrk->tmp_str;
/* redirect to scheme + host + path + / + querystring if directory without trailing slash */
/* TODO: local addr if HTTP 1.0 without host header, url encoding */
if (li_vrequest_is_handled(vr)) return FALSE;
g_string_truncate(uri, 0);
g_string_append_len(uri, GSTR_LEN(vr->request.uri.scheme));
g_string_append_len(uri, CONST_STR_LEN("://"));
if (vr->request.uri.authority->len > 0) {
g_string_append_len(uri, GSTR_LEN(vr->request.uri.authority));
} else {
g_string_append_len(uri, GSTR_LEN(vr->coninfo->local_addr_str));
}
g_string_append_len(uri, GSTR_LEN(vr->request.uri.raw_orig_path));
g_string_append_c(uri, '/');
if (vr->request.uri.query->len) {
g_string_append_c(uri, '?');
g_string_append_len(uri, GSTR_LEN(vr->request.uri.query));
}
return li_vrequest_redirect(vr, uri);
}
static void update_stats_avg(ev_tstamp now, liConInfo *coninfo) {
if ((now - coninfo->stats.last_avg) >= 5.0) {
coninfo->stats.bytes_out_5s_diff = coninfo->stats.bytes_out - coninfo->stats.bytes_out_5s;
coninfo->stats.bytes_out_5s = coninfo->stats.bytes_out;
coninfo->stats.bytes_in_5s_diff = coninfo->stats.bytes_in - coninfo->stats.bytes_in_5s;
coninfo->stats.bytes_in_5s = coninfo->stats.bytes_in;
coninfo->stats.last_avg = now;
}
}
void li_vrequest_update_stats_in(liVRequest *vr, goffset transferred) {
liConInfo *coninfo = vr->coninfo;
vr->wrk->stats.bytes_in += transferred;
coninfo->stats.bytes_in += transferred;
update_stats_avg(li_cur_ts(vr->wrk), coninfo);
}
void li_vrequest_update_stats_out(liVRequest *vr, goffset transferred) {
liConInfo *coninfo = vr->coninfo;
vr->wrk->stats.bytes_out += transferred;
coninfo->stats.bytes_out += transferred;
update_stats_avg(li_cur_ts(vr->wrk), coninfo);
}