From a91d2b2b8ce9ce4a1dd437c30a8c88b5609fa653 Mon Sep 17 00:00:00 2001 From: Niels Provos Date: Tue, 12 Dec 2006 03:51:30 +0000 Subject: [PATCH] do close-detection via a separate event svn:r301 --- http-internal.h | 1 + http.c | 23 +++++++++++------------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/http-internal.h b/http-internal.h index 66ba64fa..eab8e3b3 100644 --- a/http-internal.h +++ b/http-internal.h @@ -41,6 +41,7 @@ struct evhttp_connection { int fd; struct event ev; + struct event close_ev; struct evbuffer *input_buffer; struct evbuffer *output_buffer; diff --git a/http.c b/http.c index 2d0f8f56..ca0ac76c 100644 --- a/http.c +++ b/http.c @@ -658,6 +658,9 @@ evhttp_connection_free(struct evhttp_connection *evcon) TAILQ_REMOVE(&http->connections, evcon, next); } + if (event_initialized(&evcon->close_ev)) + event_del(&evcon->close_ev); + if (event_initialized(&evcon->ev)) event_del(&evcon->ev); @@ -729,18 +732,19 @@ static void evhttp_connection_start_detectclose(struct evhttp_connection *evcon) { evcon->flags |= EVHTTP_CON_CLOSEDETECT; - - event_del(&evcon->ev); - event_set(&evcon->ev, evcon->fd, EV_READ, + + if (event_initialized(&evcon->close_ev)) + event_del(&evcon->close_ev); + event_set(&evcon->close_ev, evcon->fd, EV_READ, evhttp_detect_close_cb, evcon); - event_add(&evcon->ev, NULL); + event_add(&evcon->close_ev, NULL); } static void evhttp_connection_stop_detectclose(struct evhttp_connection *evcon) { evcon->flags &= ~EVHTTP_CON_CLOSEDETECT; - event_del(&evcon->ev); + event_del(&evcon->close_ev); } static void @@ -1264,13 +1268,6 @@ evhttp_connection_set_closecb(struct evhttp_connection *evcon, { evcon->closecb = cb; evcon->closecb_arg = cbarg; - /* - * applications that stream to clients forever might want to - * detect when a browser or client has stopped receiving the - * stream. this would be detected on the next write in any case, - * however, we can release resources earlier using this. - */ - evhttp_connection_start_detectclose(evcon); } void @@ -1457,6 +1454,8 @@ void evhttp_send_reply_start(struct evhttp_request *req, int code, const char *reason) { + /* set up to watch for client close */ + evhttp_connection_start_detectclose(req->evcon); evhttp_response_code(req, code, reason); evhttp_make_header(req->evcon, req); evhttp_write_buffer(req->evcon, NULL, NULL);