From dcbafe6b87d33dd32042499db83cac1f0bac6f82 Mon Sep 17 00:00:00 2001 From: John Mark Bell Date: Tue, 8 Nov 2011 21:51:42 +0000 Subject: Clean up fetch callback API. Inject some long-needed type safety. svn path=/trunk/netsurf/; revision=13137 --- content/fetchers/about.c | 167 +++++++++++++++++++++++++++----------------- content/fetchers/curl.c | 83 +++++++++++----------- content/fetchers/data.c | 71 +++++++++++-------- content/fetchers/file.c | 100 ++++++++++++++------------ content/fetchers/resource.c | 33 +++++---- 5 files changed, 259 insertions(+), 195 deletions(-) (limited to 'content/fetchers') diff --git a/content/fetchers/about.c b/content/fetchers/about.c index 2ce4509ca..402bb40bf 100644 --- a/content/fetchers/about.c +++ b/content/fetchers/about.c @@ -77,12 +77,11 @@ struct fetch_about_context { static struct fetch_about_context *ring = NULL; /** issue fetch callbacks with locking */ -static inline bool fetch_about_send_callback(fetch_msg msg, - struct fetch_about_context *ctx, const void *data, - unsigned long size, fetch_error_code errorcode) +static inline bool fetch_about_send_callback(const fetch_msg *msg, + struct fetch_about_context *ctx) { ctx->locked = true; - fetch_send_callback(msg, ctx->fetchh, data, size, errorcode); + fetch_send_callback(msg, ctx->fetchh); ctx->locked = false; return ctx->aborted; @@ -92,6 +91,7 @@ static bool fetch_about_send_header(struct fetch_about_context *ctx, const char *fmt, ...) { char header[64]; + fetch_msg msg; va_list ap; va_start(ap, fmt); @@ -100,8 +100,11 @@ static bool fetch_about_send_header(struct fetch_about_context *ctx, va_end(ap); - fetch_about_send_callback(FETCH_HEADER, ctx, header, strlen(header), - FETCH_ERROR_NO_ERROR); + msg.type = FETCH_HEADER; + msg.data.header_or_data.buf = (const uint8_t *) header; + msg.data.header_or_data.len = strlen(header); + + fetch_about_send_callback(&msg, ctx); return ctx->aborted; } @@ -111,24 +114,26 @@ static bool fetch_about_send_header(struct fetch_about_context *ctx, static bool fetch_about_blank_handler(struct fetch_about_context *ctx) { - char buffer[2]; - int code = 200; + fetch_msg msg; + const char buffer[2] = { ' ', '\0' }; /* content is going to return ok */ - fetch_set_http_code(ctx->fetchh, code); + fetch_set_http_code(ctx->fetchh, 200); /* content type */ if (fetch_about_send_header(ctx, "Content-Type: text/html")) goto fetch_about_blank_handler_aborted; - buffer[0] = ' '; - buffer[1] = 0; - if (fetch_about_send_callback(FETCH_DATA, ctx, buffer, strlen(buffer), - FETCH_ERROR_NO_ERROR)) + msg.type = FETCH_DATA; + msg.data.header_or_data.buf = (const uint8_t *) buffer; + msg.data.header_or_data.len = strlen(buffer); + + if (fetch_about_send_callback(&msg, ctx)) goto fetch_about_blank_handler_aborted; - fetch_about_send_callback(FETCH_FINISHED, ctx, 0, 0, - FETCH_ERROR_NO_ERROR); + msg.type = FETCH_FINISHED; + + fetch_about_send_callback(&msg, ctx); return true; @@ -139,11 +144,15 @@ fetch_about_blank_handler_aborted: static bool fetch_about_credits_handler(struct fetch_about_context *ctx) { + fetch_msg msg; + /* content is going to return redirect */ fetch_set_http_code(ctx->fetchh, 302); - fetch_about_send_callback(FETCH_REDIRECT, ctx, "resource:credits.html", - 0, FETCH_ERROR_NO_ERROR); + msg.type = FETCH_REDIRECT; + msg.data.redirect = "resource:credits.html"; + + fetch_about_send_callback(&msg, ctx); return true; } @@ -151,11 +160,15 @@ static bool fetch_about_credits_handler(struct fetch_about_context *ctx) static bool fetch_about_licence_handler(struct fetch_about_context *ctx) { + fetch_msg msg; + /* content is going to return redirect */ fetch_set_http_code(ctx->fetchh, 302); - fetch_about_send_callback(FETCH_REDIRECT, ctx, "resource:licence.html", - 0, FETCH_ERROR_NO_ERROR); + msg.type = FETCH_REDIRECT; + msg.data.redirect = "resource:licence.html"; + + fetch_about_send_callback(&msg, ctx); return true; } @@ -167,6 +180,7 @@ static bool fetch_about_licence_handler(struct fetch_about_context *ctx) */ static bool fetch_about_imagecache_handler(struct fetch_about_context *ctx) { + fetch_msg msg; char buffer[1024]; /* output buffer */ int code = 200; int slen; @@ -180,6 +194,9 @@ static bool fetch_about_imagecache_handler(struct fetch_about_context *ctx) if (fetch_about_send_header(ctx, "Content-Type: text/html")) goto fetch_about_imagecache_handler_aborted; + msg.type = FETCH_DATA; + msg.data.header_or_data.buf = (const uint8_t *) buffer; + /* page head */ slen = snprintf(buffer, sizeof buffer, "\n\n" @@ -193,8 +210,8 @@ static bool fetch_about_imagecache_handler(struct fetch_about_context *ctx) "\"NetSurf\"" "

\n" "

NetSurf Browser Image Cache Status

\n" ); - if (fetch_about_send_callback(FETCH_DATA, ctx, buffer, - slen, FETCH_ERROR_NO_ERROR)) + msg.data.header_or_data.len = slen; + if (fetch_about_send_callback(&msg, ctx)) goto fetch_about_imagecache_handler_aborted; /* image cache summary */ @@ -216,8 +233,8 @@ static bool fetch_about_imagecache_handler(struct fetch_about_context *ctx) if (slen >= (int) (sizeof(buffer))) goto fetch_about_imagecache_handler_aborted; /* overflow */ - if (fetch_about_send_callback(FETCH_DATA, ctx, buffer, - slen, FETCH_ERROR_NO_ERROR)) + msg.data.header_or_data.len = slen; + if (fetch_about_send_callback(&msg, ctx)) goto fetch_about_imagecache_handler_aborted; @@ -240,8 +257,8 @@ static bool fetch_about_imagecache_handler(struct fetch_about_context *ctx) if (res >= (int) (sizeof buffer - slen)) { /* last entry would not fit in buffer, submit buffer */ - if (fetch_about_send_callback(FETCH_DATA, ctx, buffer, - slen, FETCH_ERROR_NO_ERROR)) + msg.data.header_or_data.len = slen; + if (fetch_about_send_callback(&msg, ctx)) goto fetch_about_imagecache_handler_aborted; slen = 0; } else { @@ -254,12 +271,12 @@ static bool fetch_about_imagecache_handler(struct fetch_about_context *ctx) slen += snprintf(buffer + slen, sizeof buffer - slen, "\n\n\n"); - if (fetch_about_send_callback(FETCH_DATA, ctx, buffer, slen, - FETCH_ERROR_NO_ERROR)) + msg.data.header_or_data.len = slen; + if (fetch_about_send_callback(&msg, ctx)) goto fetch_about_imagecache_handler_aborted; - fetch_about_send_callback(FETCH_FINISHED, ctx, 0, 0, - FETCH_ERROR_NO_ERROR); + msg.type = FETCH_FINISHED; + fetch_about_send_callback(&msg, ctx); return true; @@ -270,6 +287,7 @@ fetch_about_imagecache_handler_aborted: /** Handler to generate about:config page */ static bool fetch_about_config_handler(struct fetch_about_context *ctx) { + fetch_msg msg; char buffer[1024]; int code = 200; int slen; @@ -283,6 +301,9 @@ static bool fetch_about_config_handler(struct fetch_about_context *ctx) if (fetch_about_send_header(ctx, "Content-Type: text/html")) goto fetch_about_config_handler_aborted; + msg.type = FETCH_DATA; + msg.data.header_or_data.buf = (const uint8_t *) buffer; + slen = snprintf(buffer, sizeof buffer, "\n\n" "NetSurf Browser Config\n" @@ -307,8 +328,8 @@ static bool fetch_about_config_handler(struct fetch_about_context *ctx) if (res >= (int) (sizeof buffer - slen)) { /* last entry would not fit in buffer, submit buffer */ - if (fetch_about_send_callback(FETCH_DATA, ctx, buffer, - slen, FETCH_ERROR_NO_ERROR)) + msg.data.header_or_data.len = slen; + if (fetch_about_send_callback(&msg, ctx)) goto fetch_about_config_handler_aborted; slen = 0; } else { @@ -321,12 +342,12 @@ static bool fetch_about_config_handler(struct fetch_about_context *ctx) slen += snprintf(buffer + slen, sizeof buffer - slen, "\n\n\n"); - if (fetch_about_send_callback(FETCH_DATA, ctx, buffer, slen, - FETCH_ERROR_NO_ERROR)) + msg.data.header_or_data.len = slen; + if (fetch_about_send_callback(&msg, ctx)) goto fetch_about_config_handler_aborted; - fetch_about_send_callback(FETCH_FINISHED, ctx, 0, 0, - FETCH_ERROR_NO_ERROR); + msg.type = FETCH_FINISHED; + fetch_about_send_callback(&msg, ctx); return true; @@ -340,6 +361,7 @@ fetch_about_config_handler_aborted: */ static bool fetch_about_choices_handler(struct fetch_about_context *ctx) { + fetch_msg msg; char buffer[1024]; int code = 200; int slen; @@ -353,6 +375,9 @@ static bool fetch_about_choices_handler(struct fetch_about_context *ctx) if (fetch_about_send_header(ctx, "Content-Type: text/plain")) goto fetch_about_choices_handler_aborted; + msg.type = FETCH_DATA; + msg.data.header_or_data.buf = (const uint8_t *) buffer; + slen = snprintf(buffer, sizeof buffer, "# Automatically generated current NetSurf browser Choices\n"); @@ -366,8 +391,8 @@ static bool fetch_about_choices_handler(struct fetch_about_context *ctx) if (res >= (int) (sizeof buffer - slen)) { /* last entry would not fit in buffer, submit buffer */ - if (fetch_about_send_callback(FETCH_DATA, ctx, buffer, - slen, FETCH_ERROR_NO_ERROR)) + msg.data.header_or_data.len = slen; + if (fetch_about_send_callback(&msg, ctx)) goto fetch_about_choices_handler_aborted; slen = 0; } else { @@ -377,12 +402,12 @@ static bool fetch_about_choices_handler(struct fetch_about_context *ctx) } } while (res > 0); - if (fetch_about_send_callback(FETCH_DATA, ctx, buffer, slen, - FETCH_ERROR_NO_ERROR)) + msg.data.header_or_data.len = slen; + if (fetch_about_send_callback(&msg, ctx)) goto fetch_about_choices_handler_aborted; - fetch_about_send_callback(FETCH_FINISHED, ctx, 0, 0, - FETCH_ERROR_NO_ERROR); + msg.type = FETCH_FINISHED; + fetch_about_send_callback(&msg, ctx); return true; @@ -397,6 +422,7 @@ typedef struct { const char *leaf; const char modtype; } modification_t; static bool fetch_about_testament_handler(struct fetch_about_context *ctx) { static modification_t modifications[] = WT_MODIFICATIONS; + fetch_msg msg; char buffer[1024]; int code = 200; int slen; @@ -410,11 +436,14 @@ static bool fetch_about_testament_handler(struct fetch_about_context *ctx) if (fetch_about_send_header(ctx, "Content-Type: text/plain")) goto fetch_about_testament_handler_aborted; + msg.type = FETCH_DATA; + msg.data.header_or_data.buf = (const uint8_t *) buffer; + slen = snprintf(buffer, sizeof buffer, "# Automatically generated by NetSurf build system\n\n"); - if (fetch_about_send_callback(FETCH_DATA, ctx, buffer, slen, - FETCH_ERROR_NO_ERROR)) + msg.data.header_or_data.len = slen; + if (fetch_about_send_callback(&msg, ctx)) goto fetch_about_testament_handler_aborted; slen = snprintf(buffer, sizeof buffer, @@ -430,9 +459,9 @@ static bool fetch_about_testament_handler(struct fetch_about_context *ctx) "# This NetSurf was built from a branch.\n\n" #endif ); - - if (fetch_about_send_callback(FETCH_DATA, ctx, buffer, slen, - FETCH_ERROR_NO_ERROR)) + + msg.data.header_or_data.len = slen; + if (fetch_about_send_callback(&msg, ctx)) goto fetch_about_testament_handler_aborted; @@ -440,16 +469,16 @@ static bool fetch_about_testament_handler(struct fetch_about_context *ctx) "Built by %s (%s) from %s at revision %s\n\n", GECOS, USERNAME, WT_BRANCHPATH, WT_REVID); - if (fetch_about_send_callback(FETCH_DATA, ctx, buffer, slen, - FETCH_ERROR_NO_ERROR)) + msg.data.header_or_data.len = slen; + if (fetch_about_send_callback(&msg, ctx)) goto fetch_about_testament_handler_aborted; slen = snprintf(buffer, sizeof buffer, "Built on %s in %s\n\n", WT_HOSTNAME, WT_ROOT); - if (fetch_about_send_callback(FETCH_DATA, ctx, buffer, slen, - FETCH_ERROR_NO_ERROR)) + msg.data.header_or_data.len = slen; + if (fetch_about_send_callback(&msg, ctx)) goto fetch_about_testament_handler_aborted; if (WT_MODIFIED > 0) { @@ -461,8 +490,8 @@ static bool fetch_about_testament_handler(struct fetch_about_context *ctx) "Working tree is not modified.\n"); } - if (fetch_about_send_callback(FETCH_DATA, ctx, buffer, slen, - FETCH_ERROR_NO_ERROR)) + msg.data.header_or_data.len = slen; + if (fetch_about_send_callback(&msg, ctx)) goto fetch_about_testament_handler_aborted; for (i = 0; i < WT_MODIFIED; ++i) { @@ -470,14 +499,14 @@ static bool fetch_about_testament_handler(struct fetch_about_context *ctx) " %c %s\n", modifications[i].modtype, modifications[i].leaf); - if (fetch_about_send_callback(FETCH_DATA, ctx, buffer, slen, - FETCH_ERROR_NO_ERROR)) + msg.data.header_or_data.len = slen; + if (fetch_about_send_callback(&msg, ctx)) goto fetch_about_testament_handler_aborted; } - - fetch_about_send_callback(FETCH_FINISHED, ctx, 0, 0, - FETCH_ERROR_NO_ERROR); + + msg.type = FETCH_FINISHED; + fetch_about_send_callback(&msg, ctx); return true; @@ -487,11 +516,15 @@ fetch_about_testament_handler_aborted: static bool fetch_about_logo_handler(struct fetch_about_context *ctx) { + fetch_msg msg; + /* content is going to return redirect */ fetch_set_http_code(ctx->fetchh, 302); - fetch_about_send_callback(FETCH_REDIRECT, ctx, "resource:netsurf.png", - 0, FETCH_ERROR_NO_ERROR); + msg.type = FETCH_REDIRECT; + msg.data.redirect = "resource:netsurf.png"; + + fetch_about_send_callback(&msg, ctx); return true; } @@ -533,6 +566,7 @@ struct about_handlers about_handler_list[] = { */ static bool fetch_about_about_handler(struct fetch_about_context *ctx) { + fetch_msg msg; char buffer[1024]; int code = 200; int slen; @@ -546,6 +580,9 @@ static bool fetch_about_about_handler(struct fetch_about_context *ctx) if (fetch_about_send_header(ctx, "Content-Type: text/html")) goto fetch_about_config_handler_aborted; + msg.type = FETCH_DATA; + msg.data.header_or_data.buf = (const uint8_t *) buffer; + slen = snprintf(buffer, sizeof buffer, "\n\n" "NetSurf List of About pages\n" @@ -575,8 +612,8 @@ static bool fetch_about_about_handler(struct fetch_about_context *ctx) if (res >= (int)(sizeof buffer - slen)) { /* last entry would not fit in buffer, submit buffer */ - if (fetch_about_send_callback(FETCH_DATA, ctx, buffer, - slen, FETCH_ERROR_NO_ERROR)) + msg.data.header_or_data.len = slen; + if (fetch_about_send_callback(&msg, ctx)) goto fetch_about_config_handler_aborted; slen = 0; } else { @@ -588,12 +625,12 @@ static bool fetch_about_about_handler(struct fetch_about_context *ctx) slen += snprintf(buffer + slen, sizeof buffer - slen, "\n\n\n"); - if (fetch_about_send_callback(FETCH_DATA, ctx, buffer, slen, - FETCH_ERROR_NO_ERROR)) + msg.data.header_or_data.len = slen; + if (fetch_about_send_callback(&msg, ctx)) goto fetch_about_config_handler_aborted; - fetch_about_send_callback(FETCH_FINISHED, ctx, 0, 0, - FETCH_ERROR_NO_ERROR); + msg.type = FETCH_FINISHED; + fetch_about_send_callback(&msg, ctx); return true; diff --git a/content/fetchers/curl.c b/content/fetchers/curl.c index 87be66a09..09bfbdd00 100644 --- a/content/fetchers/curl.c +++ b/content/fetchers/curl.c @@ -787,9 +787,9 @@ void fetch_curl_poll(lwc_string *scheme_ignored) void fetch_curl_done(CURL *curl_handle, CURLcode result) { + fetch_msg msg; bool finished = false; bool error = false; - fetch_error_code errorcode = FETCH_ERROR_NO_ERROR; bool cert = false; bool abort_fetch; struct curl_fetch_info *f; @@ -830,7 +830,6 @@ void fetch_curl_done(CURL *curl_handle, CURLcode result) ; /* redirect with partial body, or similar */ else { error = true; - errorcode = FETCH_ERROR_PARTIAL_FILE; } } else if (result == CURLE_WRITE_ERROR && f->stopped) /* CURLE_WRITE_ERROR occurs when fetch_curl_data @@ -842,23 +841,19 @@ void fetch_curl_done(CURL *curl_handle, CURLcode result) memset(f->cert_data, 0, sizeof(f->cert_data)); cert = true; } - else if (result == CURLE_COULDNT_RESOLVE_HOST) { - error = true; - errorcode = FETCH_ERROR_COULDNT_RESOLVE_HOST; - } else { LOG(("Unknown cURL response code %d", result)); error = true; - errorcode = FETCH_ERROR_MISC; } fetch_curl_stop(f); if (abort_fetch) ; /* fetch was aborted: no callback */ - else if (finished) - fetch_send_callback(FETCH_FINISHED, f->fetch_handle, 0, 0, errorcode); - else if (cert) { + else if (finished) { + msg.type = FETCH_FINISHED; + fetch_send_callback(&msg, f->fetch_handle); + } else if (cert) { int i; BIO *mem; BUF_MEM *buf; @@ -934,14 +929,17 @@ void fetch_curl_done(CURL *curl_handle, CURLcode result) if (certs[i].cert->references == 0) X509_free(certs[i].cert); } - errorcode = FETCH_ERROR_CERT; - fetch_send_callback(FETCH_CERT_ERR, f->fetch_handle, - &ssl_certs, i, errorcode); + msg.type = FETCH_CERT_ERR; + msg.data.cert_err.certs = ssl_certs; + msg.data.cert_err.num_certs = i; + fetch_send_callback(&msg, f->fetch_handle); + } else if (error) { + msg.type = FETCH_ERROR; + msg.data.error = fetch_error_buffer; + + fetch_send_callback(&msg, f->fetch_handle); } - else if (error) - fetch_send_callback(FETCH_ERROR, f->fetch_handle, - fetch_error_buffer, 0, errorcode); fetch_free(f->fetch_handle); } @@ -957,11 +955,14 @@ int fetch_curl_progress(void *clientp, double dltotal, double dlnow, static char fetch_progress_buffer[256]; /**< Progress buffer for cURL */ struct curl_fetch_info *f = (struct curl_fetch_info *) clientp; unsigned int time_now_cs; - double percent; + fetch_msg msg; if (f->abort) return 0; + msg.type = FETCH_PROGRESS; + msg.data.progress = fetch_progress_buffer; + /* Rate limit each fetch's progress notifications to 2 a second */ #define UPDATES_PER_SECOND 2 #define UPDATE_DELAY_CS (100 / UPDATES_PER_SECOND) @@ -973,22 +974,16 @@ int fetch_curl_progress(void *clientp, double dltotal, double dlnow, #undef UPDATES_PERS_SECOND if (dltotal > 0) { - percent = dlnow * 100.0f / dltotal; snprintf(fetch_progress_buffer, 255, messages_get("Progress"), human_friendly_bytesize(dlnow), human_friendly_bytesize(dltotal)); - fetch_send_callback(FETCH_PROGRESS, f->fetch_handle, - fetch_progress_buffer, - (unsigned long) percent, - FETCH_ERROR_NO_ERROR); + fetch_send_callback(&msg, f->fetch_handle); } else { snprintf(fetch_progress_buffer, 255, messages_get("ProgressU"), human_friendly_bytesize(dlnow)); - fetch_send_callback(FETCH_PROGRESS, f->fetch_handle, - fetch_progress_buffer, 0, - FETCH_ERROR_NO_ERROR); + fetch_send_callback(&msg, f->fetch_handle); } return 0; @@ -1021,6 +1016,7 @@ size_t fetch_curl_data(char *data, size_t size, size_t nmemb, { struct curl_fetch_info *f = _f; CURLcode code; + fetch_msg msg; /* ensure we only have to get this information once */ if (!f->http_code) @@ -1039,17 +1035,16 @@ size_t fetch_curl_data(char *data, size_t size, size_t nmemb, return size * nmemb; } - /*LOG(("fetch %p, size %lu", f, size * nmemb));*/ - if (f->abort || (!f->had_headers && fetch_curl_process_headers(f))) { f->stopped = true; return 0; } /* send data to the caller */ - /*LOG(("FETCH_DATA"));*/ - fetch_send_callback(FETCH_DATA, f->fetch_handle, data, size * nmemb, - FETCH_ERROR_NO_ERROR); + msg.type = FETCH_DATA; + msg.data.header_or_data.buf = (const uint8_t *) data; + msg.data.header_or_data.len = size * nmemb; + fetch_send_callback(&msg, f->fetch_handle); if (f->abort) { f->stopped = true; @@ -1071,6 +1066,7 @@ size_t fetch_curl_header(char *data, size_t size, size_t nmemb, { struct curl_fetch_info *f = _f; int i; + fetch_msg msg; size *= nmemb; if (f->abort) { @@ -1078,8 +1074,10 @@ size_t fetch_curl_header(char *data, size_t size, size_t nmemb, return 0; } - fetch_send_callback(FETCH_HEADER, f->fetch_handle, data, size, - FETCH_ERROR_NO_ERROR); + msg.type = FETCH_HEADER; + msg.data.header_or_data.buf = (const uint8_t *) data; + msg.data.header_or_data.len = size; + fetch_send_callback(&msg, f->fetch_handle); #define SKIP_ST(o) for (i = (o); i < (int) size && (data[i] == ' ' || data[i] == '\t'); i++) @@ -1152,6 +1150,7 @@ bool fetch_curl_process_headers(struct curl_fetch_info *f) { long http_code; CURLcode code; + fetch_msg msg; f->had_headers = true; @@ -1167,32 +1166,34 @@ bool fetch_curl_process_headers(struct curl_fetch_info *f) if (http_code == 304 && !f->post_urlenc && !f->post_multipart) { /* Not Modified && GET request */ - fetch_send_callback(FETCH_NOTMODIFIED, f->fetch_handle, 0, 0, - FETCH_ERROR_NO_ERROR); + msg.type = FETCH_NOTMODIFIED; + fetch_send_callback(&msg, f->fetch_handle); return true; } /* handle HTTP redirects (3xx response codes) */ if (300 <= http_code && http_code < 400 && f->location != 0) { LOG(("FETCH_REDIRECT, '%s'", f->location)); - fetch_send_callback(FETCH_REDIRECT, f->fetch_handle, - f->location, 0, FETCH_ERROR_NO_ERROR); + msg.type = FETCH_REDIRECT; + msg.data.redirect = f->location; + fetch_send_callback(&msg, f->fetch_handle); return true; } /* handle HTTP 401 (Authentication errors) */ if (http_code == 401) { - fetch_send_callback(FETCH_AUTH, f->fetch_handle, f->realm, 0, - FETCH_ERROR_AUTHENTICATION); + msg.type = FETCH_AUTH; + msg.data.auth.realm = f->realm; + fetch_send_callback(&msg, f->fetch_handle); return true; } /* handle HTTP errors (non 2xx response codes) */ if (f->only_2xx && strncmp(nsurl_access(f->url), "http", 4) == 0 && (http_code < 200 || 299 < http_code)) { - fetch_send_callback(FETCH_ERROR, f->fetch_handle, - messages_get("Not2xx"), 0, - FETCH_ERROR_HTTP_NOT2); + msg.type = FETCH_ERROR; + msg.data.error = messages_get("Not2xx"); + fetch_send_callback(&msg, f->fetch_handle); return true; } diff --git a/content/fetchers/data.c b/content/fetchers/data.c index 68f59a792..bc2349733 100644 --- a/content/fetchers/data.c +++ b/content/fetchers/data.c @@ -127,17 +127,17 @@ static void fetch_data_abort(void *ctx) c->aborted = true; } -static void fetch_data_send_callback(fetch_msg msg, - struct fetch_data_context *c, const void *data, - unsigned long size, fetch_error_code errorcode) +static void fetch_data_send_callback(const fetch_msg *msg, + struct fetch_data_context *c) { c->locked = true; - fetch_send_callback(msg, c->parent_fetch, data, size, errorcode); + fetch_send_callback(msg, c->parent_fetch); c->locked = false; } static bool fetch_data_process(struct fetch_data_context *c) { + fetch_msg msg; char *params; char *comma; char *unescaped; @@ -153,8 +153,9 @@ static bool fetch_data_process(struct fetch_data_context *c) if (strlen(c->url) < 6) { /* 6 is the minimum possible length (data:,) */ - fetch_data_send_callback(FETCH_ERROR, c, - "Malformed data: URL", 0, FETCH_ERROR_URL); + msg.type = FETCH_ERROR; + msg.data.error = "Malformed data: URL"; + fetch_data_send_callback(&msg, c); return false; } @@ -163,8 +164,9 @@ static bool fetch_data_process(struct fetch_data_context *c) /* find the comma */ if ( (comma = strchr(params, ',')) == NULL) { - fetch_data_send_callback(FETCH_ERROR, c, - "Malformed data: URL", 0, FETCH_ERROR_URL); + msg.type = FETCH_ERROR; + msg.data.error = "Malformed data: URL"; + fetch_data_send_callback(&msg, c); return false; } @@ -177,9 +179,10 @@ static bool fetch_data_process(struct fetch_data_context *c) } if (c->mimetype == NULL) { - fetch_data_send_callback(FETCH_ERROR, c, - "Unable to allocate memory for mimetype in data: URL", - 0, FETCH_ERROR_MEMORY); + msg.type = FETCH_ERROR; + msg.data.error = + "Unable to allocate memory for mimetype in data: URL"; + fetch_data_send_callback(&msg, c); return false; } @@ -197,9 +200,9 @@ static bool fetch_data_process(struct fetch_data_context *c) unescaped = curl_easy_unescape(curl, comma + 1, 0, &templen); c->datalen = templen; if (unescaped == NULL) { - fetch_data_send_callback(FETCH_ERROR, c, - "Unable to URL decode data: URL", 0, - FETCH_ERROR_ENCODING); + msg.type = FETCH_ERROR; + msg.data.error = "Unable to URL decode data: URL"; + fetch_data_send_callback(&msg, c); return false; } @@ -207,18 +210,19 @@ static bool fetch_data_process(struct fetch_data_context *c) c->data = malloc(c->datalen); /* safe: always gets smaller */ if (base64_decode(unescaped, c->datalen, c->data, &(c->datalen)) == false) { - fetch_data_send_callback(FETCH_ERROR, c, - "Unable to Base64 decode data: URL", 0, - FETCH_ERROR_ENCODING); + msg.type = FETCH_ERROR; + msg.data.error = "Unable to Base64 decode data: URL"; + fetch_data_send_callback(&msg, c); curl_free(unescaped); return false; } } else { c->data = malloc(c->datalen); if (c->data == NULL) { - fetch_data_send_callback(FETCH_ERROR, c, - "Unable to allocate memory for data: URL", 0, - FETCH_ERROR_MEMORY); + msg.type = FETCH_ERROR; + msg.data.error = + "Unable to allocate memory for data: URL"; + fetch_data_send_callback(&msg, c); curl_free(unescaped); return false; } @@ -232,6 +236,7 @@ static bool fetch_data_process(struct fetch_data_context *c) static void fetch_data_poll(lwc_string *scheme) { + fetch_msg msg; struct fetch_data_context *c, *next; if (ring == NULL) return; @@ -263,27 +268,33 @@ static void fetch_data_poll(lwc_string *scheme) */ snprintf(header, sizeof header, "Content-Type: %s", c->mimetype); - fetch_data_send_callback(FETCH_HEADER, c, header, - strlen(header), FETCH_ERROR_NO_ERROR); + msg.type = FETCH_HEADER; + msg.data.header_or_data.buf = (const uint8_t *) header; + msg.data.header_or_data.len = strlen(header); + fetch_data_send_callback(&msg, c); if (c->aborted == false) { snprintf(header, sizeof header, "Content-Length: %zd", c->datalen); - fetch_data_send_callback(FETCH_HEADER, c, - header, strlen(header), - FETCH_ERROR_NO_ERROR); + msg.type = FETCH_HEADER; + msg.data.header_or_data.buf = + (const uint8_t *) header; + msg.data.header_or_data.len = strlen(header); + fetch_data_send_callback(&msg, c); } if (c->aborted == false) { - fetch_data_send_callback(FETCH_DATA, - c, c->data, c->datalen, - FETCH_ERROR_NO_ERROR); + msg.type = FETCH_DATA; + msg.data.header_or_data.buf = + (const uint8_t *) c->data; + msg.data.header_or_data.len = c->datalen; + fetch_data_send_callback(&msg, c); } if (c->aborted == false) { - fetch_data_send_callback(FETCH_FINISHED, - c, 0, 0, FETCH_ERROR_NO_ERROR); + msg.type = FETCH_FINISHED; + fetch_data_send_callback(&msg, c); } } else { LOG(("Processing of %s failed!", c->url)); diff --git a/content/fetchers/file.c b/content/fetchers/file.c index cd4ad5c34..f30637c87 100644 --- a/content/fetchers/file.c +++ b/content/fetchers/file.c @@ -70,12 +70,11 @@ struct fetch_file_context { static struct fetch_file_context *ring = NULL; /** issue fetch callbacks with locking */ -static inline bool fetch_file_send_callback(fetch_msg msg, - struct fetch_file_context *ctx, const void *data, - unsigned long size, fetch_error_code errorcode) +static inline bool fetch_file_send_callback(const fetch_msg *msg, + struct fetch_file_context *ctx) { ctx->locked = true; - fetch_send_callback(msg, ctx->fetchh, data, size, errorcode); + fetch_send_callback(msg, ctx->fetchh); ctx->locked = false; return ctx->aborted; @@ -84,6 +83,7 @@ static inline bool fetch_file_send_callback(fetch_msg msg, static bool fetch_file_send_header(struct fetch_file_context *ctx, const char *fmt, ...) { + fetch_msg msg; char header[64]; va_list ap; @@ -93,8 +93,10 @@ static bool fetch_file_send_header(struct fetch_file_context *ctx, va_end(ap); - fetch_file_send_callback(FETCH_HEADER, ctx, header, strlen(header), - FETCH_ERROR_NO_ERROR); + msg.type = FETCH_HEADER; + msg.data.header_or_data.buf = (const uint8_t *) header; + msg.data.header_or_data.len = strlen(header); + fetch_file_send_callback(&msg, ctx); return ctx->aborted; } @@ -204,6 +206,7 @@ static int fetch_file_errno_to_http_code(int error_no) static void fetch_file_process_error(struct fetch_file_context *ctx, int code) { + fetch_msg msg; char buffer[1024]; const char *title; char key[8]; @@ -223,12 +226,14 @@ static void fetch_file_process_error(struct fetch_file_context *ctx, int code) "

Error %d while fetching file %s

", title, title, code, nsurl_access(ctx->url)); - if (fetch_file_send_callback(FETCH_DATA, ctx, buffer, strlen(buffer), - FETCH_ERROR_NO_ERROR)) + msg.type = FETCH_DATA; + msg.data.header_or_data.buf = (const uint8_t *) buffer; + msg.data.header_or_data.len = strlen(buffer); + if (fetch_file_send_callback(&msg, ctx)) goto fetch_file_process_error_aborted; - fetch_file_send_callback(FETCH_FINISHED, ctx, 0, 0, - FETCH_ERROR_NO_ERROR); + msg.type = FETCH_FINISHED; + fetch_file_send_callback(&msg, ctx); fetch_file_process_error_aborted: return; @@ -239,6 +244,7 @@ fetch_file_process_error_aborted: static void fetch_file_process_plain(struct fetch_file_context *ctx, struct stat *fdstat) { + fetch_msg msg; char *buf; size_t buf_size; @@ -250,8 +256,8 @@ static void fetch_file_process_plain(struct fetch_file_context *ctx, /* Check if we can just return not modified */ if (ctx->file_etag != 0 && ctx->file_etag == fdstat->st_mtime) { fetch_set_http_code(ctx->fetchh, 304); - fetch_file_send_callback(FETCH_NOTMODIFIED, ctx, 0, 0, - FETCH_ERROR_NO_ERROR); + msg.type = FETCH_NOTMODIFIED; + fetch_file_send_callback(&msg, ctx); return; } @@ -271,9 +277,10 @@ static void fetch_file_process_plain(struct fetch_file_context *ctx, /* allocate the buffer storage */ buf = malloc(buf_size); if (buf == NULL) { - fetch_file_send_callback(FETCH_ERROR, ctx, - "Unable to allocate memory for file data buffer", - 0, FETCH_ERROR_MEMORY); + msg.type = FETCH_ERROR; + msg.data.error = + "Unable to allocate memory for file data buffer"; + fetch_file_send_callback(&msg, ctx); close(fd); return; } @@ -304,29 +311,32 @@ static void fetch_file_process_plain(struct fetch_file_context *ctx, while (tot_read < fdstat->st_size) { res = read(fd, buf, buf_size); if (res == -1) { - fetch_file_send_callback(FETCH_ERROR, ctx, - "Error reading file", 0, - FETCH_ERROR_PARTIAL_FILE); + msg.type = FETCH_ERROR; + msg.data.error = "Error reading file"; + fetch_file_send_callback(&msg, ctx); goto fetch_file_process_aborted; } if (res == 0) { - fetch_file_send_callback(FETCH_ERROR, ctx, - "Unexpected EOF reading file", 0, - FETCH_ERROR_PARTIAL_FILE); + msg.type = FETCH_ERROR; + msg.data.error = "Unexpected EOF reading file"; + fetch_file_send_callback(&msg, ctx); goto fetch_file_process_aborted; } tot_read += res; - if (fetch_file_send_callback(FETCH_DATA, ctx, buf, res, - FETCH_ERROR_NO_ERROR)) + msg.type = FETCH_DATA; + msg.data.header_or_data.buf = (const uint8_t *) buf; + msg.data.header_or_data.len = res; + if (fetch_file_send_callback(&msg, ctx)) break; } - if (ctx->aborted == false) - fetch_file_send_callback(FETCH_FINISHED, ctx, 0, 0, - FETCH_ERROR_NO_ERROR); + if (ctx->aborted == false) { + msg.type = FETCH_FINISHED; + fetch_file_send_callback(&msg, ctx); + } fetch_file_process_aborted: @@ -392,6 +402,7 @@ static char *gen_nice_title(char *path) static void fetch_file_process_dir(struct fetch_file_context *ctx, struct stat *fdstat) { + fetch_msg msg; char buffer[1024]; /* Output buffer */ bool even = false; /* formatting flag */ char *title; /* pretty printed title */ @@ -425,18 +436,21 @@ static void fetch_file_process_dir(struct fetch_file_context *ctx, if (fetch_file_send_header(ctx, "Content-Type: text/html")) goto fetch_file_process_dir_aborted; + msg.type = FETCH_DATA; + msg.data.header_or_data.buf = (const uint8_t *) buffer; + /* directory listing top */ dirlist_generate_top(buffer, sizeof buffer); - if (fetch_file_send_callback(FETCH_DATA, ctx, buffer, strlen(buffer), - FETCH_ERROR_NO_ERROR)) + msg.data.header_or_data.len = strlen(buffer); + if (fetch_file_send_callback(&msg, ctx)) goto fetch_file_process_dir_aborted; /* directory listing title */ title = gen_nice_title(ctx->path); dirlist_generate_title(title, buffer, sizeof buffer); free(title); - if (fetch_file_send_callback(FETCH_DATA, ctx, buffer, strlen(buffer), - FETCH_ERROR_NO_ERROR)) + msg.data.header_or_data.len = strlen(buffer); + if (fetch_file_send_callback(&msg, ctx)) goto fetch_file_process_dir_aborted; /* Print parent directory link */ @@ -446,11 +460,8 @@ static void fetch_file_process_dir(struct fetch_file_context *ctx, if ((res == URL_FUNC_OK) && compare == false) { dirlist_generate_parent_link(up, buffer, sizeof buffer); - fetch_file_send_callback(FETCH_DATA, ctx, - buffer, - strlen(buffer), - FETCH_ERROR_NO_ERROR); - + msg.data.header_or_data.len = strlen(buffer); + fetch_file_send_callback(&msg, ctx); } free(up); @@ -461,8 +472,8 @@ static void fetch_file_process_dir(struct fetch_file_context *ctx, /* directory list headings */ dirlist_generate_headings(buffer, sizeof buffer); - if (fetch_file_send_callback(FETCH_DATA, ctx, buffer, strlen(buffer), - FETCH_ERROR_NO_ERROR)) + msg.data.header_or_data.len = strlen(buffer); + if (fetch_file_send_callback(&msg, ctx)) goto fetch_file_process_dir_aborted; while ((ent = readdir(scandir)) != NULL) { @@ -532,10 +543,8 @@ static void fetch_file_process_dir(struct fetch_file_context *ctx, free(path); - if (fetch_file_send_callback(FETCH_DATA, ctx, - buffer, - strlen(buffer), - FETCH_ERROR_NO_ERROR)) + msg.data.header_or_data.len = strlen(buffer); + if (fetch_file_send_callback(&msg, ctx)) goto fetch_file_process_dir_aborted; even = !even; @@ -543,13 +552,12 @@ static void fetch_file_process_dir(struct fetch_file_context *ctx, /* directory listing bottom */ dirlist_generate_bottom(buffer, sizeof buffer); - if (fetch_file_send_callback(FETCH_DATA, ctx, buffer, strlen(buffer), - FETCH_ERROR_NO_ERROR)) + msg.data.header_or_data.len = strlen(buffer); + if (fetch_file_send_callback(&msg, ctx)) goto fetch_file_process_dir_aborted; - - fetch_file_send_callback(FETCH_FINISHED, ctx, 0, 0, - FETCH_ERROR_NO_ERROR); + msg.type = FETCH_FINISHED; + fetch_file_send_callback(&msg, ctx); fetch_file_process_dir_aborted: diff --git a/content/fetchers/resource.c b/content/fetchers/resource.c index b205a35c5..d46d377dc 100644 --- a/content/fetchers/resource.c +++ b/content/fetchers/resource.c @@ -89,12 +89,11 @@ static struct fetch_resource_map_entry { static uint32_t fetch_resource_path_count; /** issue fetch callbacks with locking */ -static inline bool fetch_resource_send_callback(fetch_msg msg, - struct fetch_resource_context *ctx, const void *data, - unsigned long size, fetch_error_code errorcode) +static inline bool fetch_resource_send_callback(const fetch_msg *msg, + struct fetch_resource_context *ctx) { ctx->locked = true; - fetch_send_callback(msg, ctx->fetchh, data, size, errorcode); + fetch_send_callback(msg, ctx->fetchh); ctx->locked = false; return ctx->aborted; @@ -103,6 +102,7 @@ static inline bool fetch_resource_send_callback(fetch_msg msg, static bool fetch_resource_send_header(struct fetch_resource_context *ctx, const char *fmt, ...) { + fetch_msg msg; char header[64]; va_list ap; @@ -112,8 +112,10 @@ static bool fetch_resource_send_header(struct fetch_resource_context *ctx, va_end(ap); - fetch_resource_send_callback(FETCH_HEADER, ctx, header, strlen(header), - FETCH_ERROR_NO_ERROR); + msg.type = FETCH_HEADER; + msg.data.header_or_data.buf = (const uint8_t *) header; + msg.data.header_or_data.len = strlen(header); + fetch_resource_send_callback(&msg, ctx); return ctx->aborted; } @@ -123,12 +125,14 @@ static bool fetch_resource_send_header(struct fetch_resource_context *ctx, static bool fetch_resource_redirect_handler(struct fetch_resource_context *ctx) { + fetch_msg msg; + /* content is going to return redirect */ fetch_set_http_code(ctx->fetchh, 302); - fetch_resource_send_callback(FETCH_REDIRECT, ctx, - nsurl_access(ctx->redirect_url), 0, - FETCH_ERROR_NO_ERROR); + msg.type = FETCH_REDIRECT; + msg.data.redirect = nsurl_access(ctx->redirect_url); + fetch_resource_send_callback(&msg, ctx); return true; } @@ -136,6 +140,7 @@ static bool fetch_resource_redirect_handler(struct fetch_resource_context *ctx) static bool fetch_resource_notfound_handler(struct fetch_resource_context *ctx) { + fetch_msg msg; int code = 404; char buffer[1024]; const char *title; @@ -156,12 +161,14 @@ static bool fetch_resource_notfound_handler(struct fetch_resource_context *ctx) "

Error %d while fetching file %s

", title, title, code, nsurl_access(ctx->url)); - if (fetch_resource_send_callback(FETCH_DATA, ctx, buffer, strlen(buffer), - FETCH_ERROR_NO_ERROR)) + msg.type = FETCH_DATA; + msg.data.header_or_data.buf = (const uint8_t *) buffer; + msg.data.header_or_data.len = strlen(buffer); + if (fetch_resource_send_callback(&msg, ctx)) goto fetch_resource_notfound_handler_aborted; - fetch_resource_send_callback(FETCH_FINISHED, ctx, 0, 0, - FETCH_ERROR_NO_ERROR); + msg.type = FETCH_FINISHED; + fetch_resource_send_callback(&msg, ctx); fetch_resource_notfound_handler_aborted: return false; -- cgit v1.2.3