From 8aed86f7d240ca43150f5dd5e0f7e12490072389 Mon Sep 17 00:00:00 2001 From: Richard Nyberg Date: Tue, 10 Jan 2006 15:05:44 +0000 Subject: [PATCH] Changed semantics of cancelled http requests. Before they were reported as cancelled, or possibly ok or failed if the request had come that far. Now cancelled requests are silently discarded and doesn't execute the callback. This makes the http api easier to use. Updated the tracker code for the new http semantics. --- btpd/http.c | 5 +++-- btpd/tracker_req.c | 56 ++++++++-------------------------------------- 2 files changed, 12 insertions(+), 49 deletions(-) diff --git a/btpd/http.c b/btpd/http.c index 468778d..cd42cfb 100644 --- a/btpd/http.c +++ b/btpd/http.c @@ -94,7 +94,7 @@ http_cancel(struct http *http) pthread_mutex_lock(&m_httpq_lock); if (http->state == HS_ADD) http->state = HS_NOADD; - else if (http->state == HS_ACTIVE) + else http->state = HS_CANCEL; pthread_mutex_unlock(&m_httpq_lock); } @@ -115,7 +115,8 @@ http_td_cb(void *arg) btpd_log(BTPD_L_ERROR, "Http error for url '%s' (%s).\n", h->url, curl_easy_strerror(h->res.code)); } - h->cb(h, &h->res, h->cb_arg); + if (h->state != HS_CANCEL) + h->cb(h, &h->res, h->cb_arg); curl_easy_cleanup(h->curlh); if (h->res.content != NULL) free(h->res.content); diff --git a/btpd/tracker_req.c b/btpd/tracker_req.c index 58246e3..c1a2aaa 100644 --- a/btpd/tracker_req.c +++ b/btpd/tracker_req.c @@ -25,16 +25,8 @@ enum timer_type { TIMER_RETRY }; -enum http_type { - HTTP_NONE, - HTTP_NORMAL, - HTTP_RETRY, - HTTP_NEW -}; - struct tracker { enum timer_type ttype; - enum http_type htype; enum tr_event event; int interval; unsigned nerrors; @@ -135,26 +127,14 @@ http_cb(struct http *req, struct http_res *res, void *arg) { struct torrent *tp = arg; struct tracker *tr = tp->tr; - switch (tr->htype) { - case HTTP_NORMAL: - if ((http_succeeded(res) && - parse_reply(tp, res->content, res->length) == 0)) { - tr->htype = HTTP_NONE; - tr->ttype = TIMER_INTERVAL; - event_add(&tr->timer, (& (struct timeval) { tr->interval, 0 })); - break; - } - case HTTP_RETRY: - tr->htype = HTTP_NONE; + assert(tr->ttype == TIMER_TIMEOUT); + if ((http_succeeded(res) && + parse_reply(tp, res->content, res->length) == 0)) { + tr->ttype = TIMER_INTERVAL; + event_add(&tr->timer, (& (struct timeval) { tr->interval, 0 })); + } else { tr->ttype = TIMER_RETRY; event_add(&tr->timer, RETRY_WAIT); - break; - case HTTP_NEW: - tr->htype = HTTP_NONE; - tr_send(tp, tr->event); - break; - default: - abort(); } } @@ -165,16 +145,12 @@ timer_cb(int fd, short type, void *arg) struct tracker *tr = tp->tr; switch (tr->ttype) { case TIMER_TIMEOUT: - http_cancel(tr->req); - tr->htype = HTTP_RETRY; - tr->ttype = TIMER_NONE; + case TIMER_RETRY: + tr_send(tp, tp->tr->event); break; case TIMER_INTERVAL: tr_send(tp, TR_EV_EMPTY); break; - case TIMER_RETRY: - tr_send(tp, tr->event); - break; default: abort(); } @@ -188,22 +164,8 @@ tr_send(struct torrent *tp, enum tr_event event) struct tracker *tr = tp->tr; tr->event = event; - switch (tr->htype) { - case HTTP_NORMAL: - tr->htype = HTTP_NEW; - tr->ttype = TIMER_NONE; - event_del(&tr->timer); + if (tr->ttype == TIMER_TIMEOUT) http_cancel(tr->req); - return; - case HTTP_RETRY: - tr->htype = HTTP_NEW; - return; - case HTTP_NEW: - return; - default: - tr->htype = HTTP_NORMAL; - } - tr->ttype = TIMER_TIMEOUT; event_add(&tr->timer, REQ_TIMEOUT);