From ba22b4e753529ad33a204b3c215b354aeebbbd2a Mon Sep 17 00:00:00 2001 From: John Mark Bell Date: Sat, 10 Jul 2004 02:35:31 +0000 Subject: [project @ 2004-07-10 02:35:30 by jmb] Use libcurl's progress callback functionality to display fetch status. This will update the status line once a second, more frequently requires hacking libcurl. svn path=/import/netsurf/; revision=1066 --- content/fetch.c | 35 +++++++++++++++++++++++++++++++++++ content/fetch.h | 1 + content/fetchcache.c | 16 ++++++++++++++-- 3 files changed, 50 insertions(+), 2 deletions(-) (limited to 'content') diff --git a/content/fetch.c b/content/fetch.c index 184d78bcb..302dd6c66 100644 --- a/content/fetch.c +++ b/content/fetch.c @@ -79,11 +79,14 @@ static CURLM *curl_multi; /**< Global cURL multi handle. */ static CURL *fetch_blank_curl; static struct fetch *fetch_list = 0; /**< List of active fetches. */ static char fetch_error_buffer[CURL_ERROR_SIZE]; /**< Error buffer for cURL. */ +static char fetch_progress_buffer[256]; /**< Progress buffer for cURL */ static CURLcode fetch_set_options(struct fetch *f); static void fetch_free(struct fetch *f); static void fetch_stop(struct fetch *f); static void fetch_done(CURL *curl_handle, CURLcode result); +static int fetch_curl_progress(void *clientp, double dltotal, double dlnow, + double ultotal, double ulnow); static size_t fetch_curl_data(void *data, size_t size, size_t nmemb, struct fetch *f); static size_t fetch_curl_header(char *data, size_t size, size_t nmemb, @@ -142,6 +145,8 @@ void fetch_init(void) SETOPT(CURLOPT_ERRORBUFFER, fetch_error_buffer); SETOPT(CURLOPT_WRITEFUNCTION, fetch_curl_data); SETOPT(CURLOPT_HEADERFUNCTION, fetch_curl_header); + SETOPT(CURLOPT_PROGRESSFUNCTION, fetch_curl_progress); + SETOPT(CURLOPT_NOPROGRESS, 0); SETOPT(CURLOPT_USERAGENT, user_agent); SETOPT(CURLOPT_ENCODING, "gzip"); SETOPT(CURLOPT_LOW_SPEED_LIMIT, 1L); @@ -357,6 +362,7 @@ CURLcode fetch_set_options(struct fetch *f) SETOPT(CURLOPT_PRIVATE, f); SETOPT(CURLOPT_WRITEDATA, f); SETOPT(CURLOPT_WRITEHEADER, f); + SETOPT(CURLOPT_PROGRESSDATA, f); SETOPT(CURLOPT_REFERER, f->referer); SETOPT(CURLOPT_HTTPHEADER, f->headers); if (f->post_urlenc) { @@ -579,6 +585,35 @@ void fetch_done(CURL *curl_handle, CURLcode result) } +/** + * Callback function for fetch progress + */ +int fetch_curl_progress(void *clientp, double dltotal, double dlnow, + double ultotal, double ulnow) +{ + struct fetch *f = (struct fetch *)clientp; + double percent; + + if (dltotal > 0) { + percent = dlnow * 100.0f / dltotal; + snprintf(fetch_progress_buffer, 255, + messages_get("Progress"), + human_friendly_bytesize(dlnow), + human_friendly_bytesize(dltotal)); + f->callback(FETCH_PROGRESS, f->p, fetch_progress_buffer, + (unsigned long)percent); + } + else { + snprintf(fetch_progress_buffer, 255, + messages_get("ProgressU"), + human_friendly_bytesize(dlnow)); + f->callback(FETCH_PROGRESS, f->p, fetch_progress_buffer, 0); + } + + return 0; +} + + /** * Callback function for cURL. */ diff --git a/content/fetch.h b/content/fetch.h index 59767d255..eb2721b10 100644 --- a/content/fetch.h +++ b/content/fetch.h @@ -17,6 +17,7 @@ typedef enum { FETCH_TYPE, + FETCH_PROGRESS, FETCH_DATA, FETCH_FINISHED, FETCH_ERROR, diff --git a/content/fetchcache.c b/content/fetchcache.c index 8d3272c94..62da13106 100644 --- a/content/fetchcache.c +++ b/content/fetchcache.c @@ -225,9 +225,21 @@ void fetchcache_callback(fetch_msg msg, void *p, const char *data, } break; + case FETCH_PROGRESS: + if (size) + content_set_status(c, + messages_get("RecPercent"), + data, (unsigned int)size); + else + content_set_status(c, + messages_get("Received"), + data); + content_broadcast(c, CONTENT_MSG_STATUS, msg_data); + break; + case FETCH_DATA: LOG(("FETCH_DATA")); - if (c->total_size) +/* if (c->total_size) content_set_status(c, messages_get("RecPercent"), human_friendly_bytesize(c->source_size + size), @@ -238,7 +250,7 @@ void fetchcache_callback(fetch_msg msg, void *p, const char *data, messages_get("Received"), human_friendly_bytesize(c->source_size + size)); content_broadcast(c, CONTENT_MSG_STATUS, msg_data); - if (!content_process_data(c, data, size)) { +*/ if (!content_process_data(c, data, size)) { fetch_abort(c->fetch); c->fetch = 0; } -- cgit v1.2.3