void li_connection_reset(liConnection *con) { if (LI_CON_STATE_DEAD != con->state) { con->state = LI_CON_STATE_DEAD; con_iostream_close(con); li_stream_reset(&con->in); li_stream_reset(&con->out); li_vrequest_reset(con->mainvr, TRUE); li_stream_release(&con->in); li_stream_release(&con->out); con->info.keep_alive = TRUE; if (con->keep_alive_data.link) { g_queue_delete_link(&con->wrk->keep_alive_queue, con->keep_alive_data.link); con->keep_alive_data.link = NULL; } con->keep_alive_data.timeout = 0; con->keep_alive_data.max_idle = 0; li_event_stop(&con->keep_alive_data.watcher); con->keep_alive_requests = 0; } li_connection_update_io_wait(con); li_job_later(&con->wrk->loop.jobqueue, &con->job_reset); }
static void close_cb(liOpenSSLFilter *f, gpointer data) { openssl_connection_ctx *conctx = data; liConnection *con = conctx->con; assert(conctx->ssl_filter == f); conctx->ssl_filter = NULL; li_openssl_filter_free(f); if (NULL != conctx->con) { liStream *raw_out = con->con_sock.raw_out, *raw_in = con->con_sock.raw_in; assert(con->con_sock.data == conctx); conctx->con = NULL; con->con_sock.data = NULL; li_stream_acquire(raw_in); li_stream_reset(raw_out); li_stream_reset(raw_in); li_stream_release(raw_in); } if (NULL != conctx->sock_stream) { liIOStream *stream = conctx->sock_stream; conctx->sock_stream = NULL; li_iostream_release(stream); } }
static void close_cb(liGnuTLSFilter *f, gpointer data) { mod_connection_ctx *conctx = data; liConnection *con = conctx->con; LI_FORCE_ASSERT(conctx->tls_filter == f); conctx->tls_filter = NULL; li_gnutls_filter_free(f); gnutls_deinit(conctx->session); if (NULL != conctx->ctx) { mod_gnutls_context_release(conctx->ctx); conctx->ctx = NULL; } if (NULL != conctx->con) { liStream *raw_out = con->con_sock.raw_out, *raw_in = con->con_sock.raw_in; LI_FORCE_ASSERT(con->con_sock.data == conctx); conctx->con = NULL; con->con_sock.data = NULL; con->con_sock.callbacks = NULL; li_stream_acquire(raw_in); li_stream_reset(raw_out); li_stream_reset(raw_in); li_stream_release(raw_in); } #ifdef USE_SNI if (NULL != conctx->sni_db_wait) { li_fetch_cancel(&conctx->sni_db_wait); } if (NULL != conctx->sni_entry) { li_fetch_entry_release(conctx->sni_entry); conctx->sni_entry = NULL; } #endif if (NULL != conctx->client_hello_stream) { li_ssl_client_hello_stream_ready(conctx->client_hello_stream); li_stream_release(conctx->client_hello_stream); conctx->client_hello_stream = NULL; } #ifdef USE_SNI if (NULL != conctx->sni_jobref) { li_job_ref_release(conctx->sni_jobref); conctx->sni_jobref = NULL; } li_job_clear(&conctx->sni_job); if (NULL != conctx->sni_server_name) { g_string_free(conctx->sni_server_name, TRUE); conctx->sni_server_name = NULL; } #endif LI_FORCE_ASSERT(NULL != conctx->sock_stream); li_iostream_safe_release(&conctx->sock_stream); }
static void handshake_cb(liGnuTLSFilter *f, gpointer data, liStream *plain_source, liStream *plain_drain) { mod_connection_ctx *conctx = data; liConnection *con = conctx->con; UNUSED(f); if (NULL != con) { li_stream_connect(plain_source, con->con_sock.raw_in); li_stream_connect(con->con_sock.raw_out, plain_drain); } else { li_stream_reset(plain_source); li_stream_reset(plain_drain); } }
static void li_connection_reset2(liConnection *con) { con->response_headers_sent = FALSE; con->expect_100_cont = FALSE; con->out_has_all_data = FALSE; con_iostream_close(con); li_server_socket_release(con->srv_sock); con->srv_sock = NULL; con->info.is_ssl = FALSE; con->info.aborted = FALSE; con->info.out_queue_length = 0; li_stream_reset(&con->in); li_stream_reset(&con->out); li_vrequest_reset(con->mainvr, FALSE); li_http_request_parser_reset(&con->req_parser_ctx); g_string_truncate(con->info.remote_addr_str, 0); li_sockaddr_clear(&con->info.remote_addr); g_string_truncate(con->info.local_addr_str, 0); li_sockaddr_clear(&con->info.local_addr); con->info.keep_alive = TRUE; if (con->keep_alive_data.link) { g_queue_delete_link(&con->wrk->keep_alive_queue, con->keep_alive_data.link); con->keep_alive_data.link = NULL; } con->keep_alive_data.timeout = 0; con->keep_alive_data.max_idle = 0; li_event_stop(&con->keep_alive_data.watcher); con->keep_alive_requests = 0; /* reset stats */ con->info.stats.bytes_in = G_GUINT64_CONSTANT(0); con->info.stats.bytes_in_5s = G_GUINT64_CONSTANT(0); con->info.stats.bytes_in_5s_diff = G_GUINT64_CONSTANT(0); con->info.stats.bytes_out = G_GUINT64_CONSTANT(0); con->info.stats.bytes_out_5s = G_GUINT64_CONSTANT(0); con->info.stats.bytes_out_5s_diff = G_GUINT64_CONSTANT(0); con->info.stats.last_avg = 0; /* remove from timeout queue */ li_waitqueue_remove(&con->wrk->io_timeout_queue, &con->io_timeout_elem); li_job_reset(&con->job_reset); }
static void openssl_tcp_finished(liConnection *con, gboolean aborted) { openssl_connection_ctx *conctx = con->con_sock.data; UNUSED(aborted); con->info.is_ssl = FALSE; con->con_sock.callbacks = NULL; if (NULL != conctx) { assert(con == conctx->con); close_cb(conctx->ssl_filter, conctx); } { liStream *raw_out = con->con_sock.raw_out, *raw_in = con->con_sock.raw_in; con->con_sock.raw_out = con->con_sock.raw_in = NULL; if (NULL != raw_out) { li_stream_reset(raw_out); li_stream_release(raw_out); } if (NULL != raw_in) { li_stream_reset(raw_in); li_stream_release(raw_in); } } }
static void gnutls_tcp_finished(liConnection *con, gboolean aborted) { mod_connection_ctx *conctx = con->con_sock.data; UNUSED(aborted); con->info.is_ssl = FALSE; con->con_sock.callbacks = NULL; if (NULL != conctx) { LI_FORCE_ASSERT(con == conctx->con); close_cb(conctx->tls_filter, conctx); LI_FORCE_ASSERT(NULL == con->con_sock.data); } { liStream *raw_out = con->con_sock.raw_out, *raw_in = con->con_sock.raw_in; con->con_sock.raw_out = con->con_sock.raw_in = NULL; if (NULL != raw_out) { li_stream_reset(raw_out); li_stream_release(raw_out); } if (NULL != raw_in) { li_stream_reset(raw_in); li_stream_release(raw_in); } } }
static void simple_tcp_finished(liConnection *con, gboolean aborted) { simple_tcp_connection *data = con->con_sock.data; liIOStream *stream; if (NULL == data) return; data->con = NULL; con->con_sock.data = NULL; con->con_sock.callbacks = NULL; stream = data->sock_stream; data->sock_stream = NULL; li_stream_simple_socket_close(stream, aborted); li_iostream_release(stream); { liStream *raw_out = con->con_sock.raw_out, *raw_in = con->con_sock.raw_in; con->con_sock.raw_out = con->con_sock.raw_in = NULL; if (NULL != raw_out) { li_stream_reset(raw_out); li_stream_release(raw_out); } if (NULL != raw_in) { li_stream_reset(raw_in); li_stream_release(raw_in); } } }
static void stream_http_response_data(liStreamHttpResponse* shr) { if (NULL == shr->stream.source) return; if (!shr->response_headers_finished) { switch (li_http_response_parse(shr->vr, &shr->parse_response_ctx)) { case LI_HANDLER_GO_ON: check_response_header(shr); if (NULL == shr->stream.source) return; break; case LI_HANDLER_ERROR: VR_ERROR(shr->vr, "%s", "Parsing response header failed"); li_vrequest_error(shr->vr); return; case LI_HANDLER_WAIT_FOR_EVENT: if (shr->stream.source->out->is_closed) { VR_ERROR(shr->vr, "%s", "Parsing response header failed (eos)"); li_vrequest_error(shr->vr); } return; default: return; } } if (shr->transfer_encoding_chunked) { if (!li_filter_chunked_decode(shr->vr, shr->stream.out, shr->stream.source->out, &shr->chunked_decode_state)) { if (NULL != shr->vr) { VR_ERROR(shr->vr, "%s", "Decoding chunks failed"); li_vrequest_error(shr->vr); } else { li_stream_reset(&shr->stream); } } if (shr->stream.source->out->is_closed) { li_stream_disconnect(&shr->stream); } } else { li_chunkqueue_steal_all(shr->stream.out, shr->stream.source->out); if (shr->stream.source->out->is_closed) { shr->stream.out->is_closed = TRUE; li_stream_disconnect(&shr->stream); } } li_stream_notify(&shr->stream); }
static liHandlerResult memcache_store_filter(liVRequest *vr, liFilter *f) { memcache_filter *mf = (memcache_filter*) f->param; if (NULL == f->in) { memcache_store_filter_free(vr, f); /* didn't handle f->in->is_closed? abort forwarding */ if (!f->out->is_closed) li_stream_reset(&f->stream); return LI_HANDLER_GO_ON; } if (NULL == mf) goto forward; if (f->in->is_closed && 0 == f->in->length && f->out->is_closed) { /* nothing to do anymore */ return LI_HANDLER_GO_ON; } /* check if size still fits into buffer */ if ((gssize) (f->in->length + mf->buf->used) > (gssize) mf->ctx->maxsize) { /* response too big, switch to "forward" mode */ memcache_store_filter_free(vr, f); goto forward; } while (0 < f->in->length) { char *data; off_t len; liChunkIter ci; liHandlerResult res; GError *err = NULL; ci = li_chunkqueue_iter(f->in); if (LI_HANDLER_GO_ON != (res = li_chunkiter_read(ci, 0, 16*1024, &data, &len, &err))) { if (NULL != err) { VR_ERROR(vr, "Couldn't read data from chunkqueue: %s", err->message); g_error_free(err); } return res; } if ((gssize) (len + mf->buf->used) > (gssize) mf->ctx->maxsize) { /* response too big, switch to "forward" mode */ memcache_store_filter_free(vr, f); goto forward; } memcpy(mf->buf->addr + mf->buf->used, data, len); mf->buf->used += len; if (!f->out->is_closed) { li_chunkqueue_steal_len(f->out, f->in, len); } else { li_chunkqueue_skip(f->in, len); } } if (f->in->is_closed) { /* finally: store response in memcached */ liMemcachedCon *con; GError *err = NULL; liMemcachedRequest *req; memcached_ctx *ctx = mf->ctx; assert(0 == f->in->length); f->out->is_closed = TRUE; con = mc_ctx_prepare(ctx, vr->wrk); mc_ctx_build_key(vr->wrk->tmp_str, ctx, vr); if (NULL != vr && CORE_OPTION(LI_CORE_OPTION_DEBUG_REQUEST_HANDLING).boolean) { VR_DEBUG(vr, "memcached.store: storing response for key '%s'", vr->wrk->tmp_str->str); } req = li_memcached_set(con, vr->wrk->tmp_str, ctx->flags, ctx->ttl, mf->buf, NULL, NULL, &err); memcache_store_filter_free(vr, f); if (NULL == req) { if (NULL != err) { if (NULL != vr && LI_MEMCACHED_DISABLED != err->code) { VR_ERROR(vr, "memcached.store: set failed: %s", err->message); } g_clear_error(&err); } else if (NULL != vr) { VR_ERROR(vr, "memcached.store: set failed: %s", "Unkown error"); } } } return LI_HANDLER_GO_ON; forward: if (f->out->is_closed) { li_chunkqueue_skip_all(f->in); li_stream_disconnect(&f->stream); } else { li_chunkqueue_steal_all(f->out, f->in); if (f->in->is_closed) f->out->is_closed = f->in->is_closed; } return LI_HANDLER_GO_ON; }
/* abort buffering, disconnect streams */ static void bod_error(bod_state *state) { bod_close(state); li_stream_reset(&state->stream); state->vr = NULL; }
static liHandlerResult cache_etag_filter_miss(liVRequest *vr, liFilter *f) { cache_etag_file *cfile = (cache_etag_file*) f->param; ssize_t res; gchar *buf; off_t buflen; liChunkIter citer; GError *err = NULL; if (NULL == f->in) { cache_etag_filter_free(vr, f); /* didn't handle f->in->is_closed? abort forwarding */ if (!f->out->is_closed) li_stream_reset(&f->stream); return LI_HANDLER_GO_ON; } if (NULL == cfile) goto forward; if (f->in->length > 0) { citer = li_chunkqueue_iter(f->in); if (LI_HANDLER_GO_ON != li_chunkiter_read(citer, 0, 64*1024, &buf, &buflen, &err)) { if (NULL != err) { if (NULL != vr) VR_ERROR(vr, "Couldn't read data from chunkqueue: %s", err->message); g_error_free(err); } else { if (NULL != vr) VR_ERROR(vr, "%s", "Couldn't read data from chunkqueue"); } cache_etag_filter_free(vr, f); goto forward; } res = write(cfile->fd, buf, buflen); if (res < 0) { switch (errno) { case EINTR: case EAGAIN: return LI_HANDLER_COMEBACK; default: if (NULL != vr) VR_ERROR(vr, "Couldn't write to temporary cache file '%s': %s", cfile->tmpfilename->str, g_strerror(errno)); cache_etag_filter_free(vr, f); goto forward; } } else { if (!f->out->is_closed) { li_chunkqueue_steal_len(f->out, f->in, res); } else { li_chunkqueue_skip(f->in, res); } } } if (0 == f->in->length && f->in->is_closed) { f->out->is_closed = TRUE; f->param = NULL; cache_etag_file_finish(vr, cfile); return LI_HANDLER_GO_ON; } return LI_HANDLER_GO_ON; forward: if (f->out->is_closed) { li_chunkqueue_skip_all(f->in); li_stream_disconnect(&f->stream); } else { li_chunkqueue_steal_all(f->out, f->in); if (f->in->is_closed) f->out->is_closed = f->in->is_closed; } return LI_HANDLER_GO_ON; }