From afce03a20e7f84d85cbedaa8d0772030ed60c819 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Higor=20Eur=C3=ADpedes?= Date: Mon, 23 Nov 2015 22:30:57 -0300 Subject: [PATCH] (task_http) Abort task on error --- libretro-common/include/net/net_http.h | 2 ++ libretro-common/net/net_http.c | 10 +++++--- menu/cbs/menu_cbs_deferred_push.c | 12 ++++++++- tasks/task_http.c | 34 ++++++++++++++++++-------- 4 files changed, 44 insertions(+), 14 deletions(-) diff --git a/libretro-common/include/net/net_http.h b/libretro-common/include/net/net_http.h index b6b6b3a1e2..ee73406e6d 100644 --- a/libretro-common/include/net/net_http.h +++ b/libretro-common/include/net/net_http.h @@ -49,6 +49,8 @@ bool net_http_update(struct http_t *state, size_t* progress, size_t* total); /* 200, 404, or whatever. */ int net_http_status(struct http_t *state); +bool net_http_error(struct http_t *state); + /* Returns the downloaded data. The returned buffer is owned by the * HTTP handler; it's freed by net_http_delete. * diff --git a/libretro-common/net/net_http.c b/libretro-common/net/net_http.c index 24c69886b8..98953188af 100644 --- a/libretro-common/net/net_http.c +++ b/libretro-common/net/net_http.c @@ -523,7 +523,7 @@ fail: state->status = -1; } - return false; + return true; } int net_http_status(struct http_t *state) @@ -538,8 +538,7 @@ uint8_t* net_http_data(struct http_t *state, size_t* len, bool accept_error) if (!state) return NULL; - if (!accept_error && - (state->error || state->status<200 || state->status>299)) + if (!accept_error && net_http_error(state)) { if (len) *len=0; @@ -563,3 +562,8 @@ void net_http_delete(struct http_t *state) // free(state->data); free(state); } + +bool net_http_error(struct http_t *state) +{ + return (state->error || state->status<200 || state->status>299); +} diff --git a/menu/cbs/menu_cbs_deferred_push.c b/menu/cbs/menu_cbs_deferred_push.c index e5826e8cd3..4cc26afbd2 100644 --- a/menu/cbs/menu_cbs_deferred_push.c +++ b/menu/cbs/menu_cbs_deferred_push.c @@ -317,6 +317,9 @@ void cb_net_generic(void *task_data, void *user_data, const char *err) menu_entries_unset_refresh(true); finish: + if (err) + RARCH_ERR("Download failed: %s\n", err); + if (data) { if (data->data) @@ -456,6 +459,12 @@ void cb_generic_download(void *task_data, void *user_data, const char *err) event_command(EVENT_CMD_CORE_INFO_INIT); finish: + if (err) + { + RARCH_ERR("Download of '%s' failed: %s\n", + (transf ? transf->path: "unknown"), err); + } + if (data) { if (data->data) @@ -463,7 +472,8 @@ finish: free(data); } - free(transf); + if (transf) + free(transf); } static int deferred_push_core_updater_list(menu_displaylist_info_t *info) diff --git a/tasks/task_http.c b/tasks/task_http.c index 77e5ce0579..bc07608c7b 100644 --- a/tasks/task_http.c +++ b/tasks/task_http.c @@ -151,12 +151,11 @@ static void rarch_task_http_transfer_handler(rarch_task_t *task) if (!rarch_main_data_http_con_iterate_transfer(http)) http->status = HTTP_STATUS_CONNECTION_TRANSFER_PARSE; break; - case HTTP_STATUS_TRANSFER_PARSE: - goto task_finished; case HTTP_STATUS_TRANSFER: if (!rarch_main_data_http_iterate_transfer(http)) - http->status = HTTP_STATUS_TRANSFER_PARSE; + goto task_finished; break; + case HTTP_STATUS_TRANSFER_PARSE: case HTTP_STATUS_POLL: goto task_finished; default: @@ -167,17 +166,32 @@ static void rarch_task_http_transfer_handler(rarch_task_t *task) task_finished: task->finished = true; - data = (http_transfer_data_t*)calloc(1, sizeof(*data)); - task->task_data = data; - if (http->handle) { - data->data = (char*)net_http_data(http->handle, &data->len, false); + size_t len = 0; + char *tmp = (char*)net_http_data(http->handle, &len, false); - if (data->data && http->cb) - http->cb(data->data, data->len); + if (tmp && http->cb) + http->cb(tmp, len); + + if (net_http_error(http->handle)) + { + tmp = (char*)net_http_data(http->handle, &len, true); + + if (tmp) + free(tmp); + + task->error = strdup("Download failed."); + } + else + { + data = (http_transfer_data_t*)calloc(1, sizeof(*data)); + data->data = tmp; + data->len = len; + + task->task_data = data; + } - /* we can't let net_http_delete free our data */ net_http_delete(http->handle); }