2015-05-05 15:36:58 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
|
|
|
* Copyright (C) 2011-2015 - Daniel De Matteis
|
|
|
|
*
|
|
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <retro_miscellaneous.h>
|
|
|
|
#include <net/net_http.h>
|
|
|
|
#include <queues/message_queue.h>
|
|
|
|
#include <string/string_list.h>
|
2015-05-05 16:34:48 +00:00
|
|
|
#include <compat/strl.h>
|
2015-06-08 09:10:12 +00:00
|
|
|
#include <file/file_path.h>
|
2015-07-08 20:03:23 +00:00
|
|
|
#include <file/file_extract.h>
|
2015-06-28 17:10:36 +00:00
|
|
|
#include <net/net_compat.h>
|
2015-09-18 22:34:24 +00:00
|
|
|
#include <retro_file.h>
|
2015-09-21 23:45:16 +00:00
|
|
|
#include <retro_stat.h>
|
2015-05-05 15:36:58 +00:00
|
|
|
|
2015-06-08 09:10:12 +00:00
|
|
|
#include "../file_ops.h"
|
|
|
|
#include "../general.h"
|
2015-07-01 17:40:50 +00:00
|
|
|
#include "../msg_hash.h"
|
2015-11-23 11:03:38 +00:00
|
|
|
#include "../verbosity.h"
|
2015-05-05 15:36:58 +00:00
|
|
|
#include "tasks.h"
|
|
|
|
|
2015-06-08 09:10:12 +00:00
|
|
|
extern char core_updater_path[PATH_MAX_LENGTH];
|
|
|
|
|
2015-07-08 19:35:24 +00:00
|
|
|
enum http_status_enum
|
|
|
|
{
|
|
|
|
HTTP_STATUS_POLL = 0,
|
|
|
|
HTTP_STATUS_CONNECTION_TRANSFER,
|
|
|
|
HTTP_STATUS_CONNECTION_TRANSFER_PARSE,
|
|
|
|
HTTP_STATUS_TRANSFER,
|
|
|
|
HTTP_STATUS_TRANSFER_PARSE,
|
|
|
|
HTTP_STATUS_TRANSFER_PARSE_FREE
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct http_handle
|
|
|
|
{
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
struct http_connection_t *handle;
|
|
|
|
transfer_cb_t cb;
|
|
|
|
char elem1[PATH_MAX_LENGTH];
|
|
|
|
} connection;
|
|
|
|
struct http_t *handle;
|
|
|
|
transfer_cb_t cb;
|
|
|
|
unsigned status;
|
2015-11-25 02:12:31 +00:00
|
|
|
bool error;
|
2015-07-08 19:35:24 +00:00
|
|
|
} http_handle_t;
|
|
|
|
|
2015-05-05 15:36:58 +00:00
|
|
|
static int rarch_main_data_http_con_iterate_transfer(http_handle_t *http)
|
|
|
|
{
|
|
|
|
if (!net_http_connection_iterate(http->connection.handle))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rarch_main_data_http_conn_iterate_transfer_parse(http_handle_t *http)
|
|
|
|
{
|
|
|
|
if (net_http_connection_done(http->connection.handle))
|
|
|
|
{
|
|
|
|
if (http->connection.handle && http->connection.cb)
|
|
|
|
http->connection.cb(http, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
net_http_connection_free(http->connection.handle);
|
|
|
|
|
|
|
|
http->connection.handle = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cb_http_conn_default(void *data_, size_t len)
|
|
|
|
{
|
|
|
|
http_handle_t *http = (http_handle_t*)data_;
|
|
|
|
|
|
|
|
if (!http)
|
|
|
|
return -1;
|
|
|
|
|
2015-06-28 17:10:36 +00:00
|
|
|
if (!network_init())
|
|
|
|
return -1;
|
|
|
|
|
2015-05-05 15:36:58 +00:00
|
|
|
http->handle = net_http_new(http->connection.handle);
|
|
|
|
|
|
|
|
if (!http->handle)
|
|
|
|
{
|
2015-11-30 14:43:05 +00:00
|
|
|
RARCH_ERR("[http] Could not create new HTTP session handle.\n");
|
2015-11-25 02:12:31 +00:00
|
|
|
http->error = true;
|
2015-05-05 15:36:58 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
http->cb = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* rarch_main_data_http_iterate_transfer:
|
|
|
|
*
|
|
|
|
* Resumes HTTP transfer update.
|
|
|
|
*
|
|
|
|
* Returns: 0 when finished, -1 when we should continue
|
|
|
|
* with the transfer on the next frame.
|
|
|
|
**/
|
2015-11-30 12:17:46 +00:00
|
|
|
static int rarch_main_data_http_iterate_transfer(rarch_task_t *task)
|
2015-05-05 15:36:58 +00:00
|
|
|
{
|
2015-11-30 12:17:46 +00:00
|
|
|
http_handle_t *http = (http_handle_t*)task->state;
|
2015-07-01 17:40:50 +00:00
|
|
|
size_t pos = 0, tot = 0;
|
|
|
|
|
|
|
|
if (!net_http_update(http->handle, &pos, &tot))
|
|
|
|
{
|
2015-11-30 18:36:02 +00:00
|
|
|
task->progress = (tot == 0) ? -1 : (pos * 100 / tot);
|
|
|
|
/* FIXME/TODO - warning 68: integer conversion resulted in a change
|
|
|
|
* of sign ^*/
|
2015-07-01 17:40:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2015-05-05 15:36:58 +00:00
|
|
|
}
|
|
|
|
|
2015-11-23 14:13:26 +00:00
|
|
|
static void rarch_task_http_transfer_handler(rarch_task_t *task)
|
2015-05-05 15:36:58 +00:00
|
|
|
{
|
2015-11-23 14:13:26 +00:00
|
|
|
http_handle_t *http = (http_handle_t*)task->state;
|
|
|
|
http_transfer_data_t *data;
|
2015-05-05 15:36:58 +00:00
|
|
|
|
|
|
|
switch (http->status)
|
|
|
|
{
|
|
|
|
case HTTP_STATUS_CONNECTION_TRANSFER_PARSE:
|
|
|
|
rarch_main_data_http_conn_iterate_transfer_parse(http);
|
|
|
|
http->status = HTTP_STATUS_TRANSFER;
|
|
|
|
break;
|
|
|
|
case HTTP_STATUS_CONNECTION_TRANSFER:
|
|
|
|
if (!rarch_main_data_http_con_iterate_transfer(http))
|
|
|
|
http->status = HTTP_STATUS_CONNECTION_TRANSFER_PARSE;
|
|
|
|
break;
|
|
|
|
case HTTP_STATUS_TRANSFER:
|
2015-11-30 12:17:46 +00:00
|
|
|
if (!rarch_main_data_http_iterate_transfer(task))
|
2015-11-24 01:30:57 +00:00
|
|
|
goto task_finished;
|
2015-05-05 15:36:58 +00:00
|
|
|
break;
|
2015-11-24 01:30:57 +00:00
|
|
|
case HTTP_STATUS_TRANSFER_PARSE:
|
2015-05-05 15:36:58 +00:00
|
|
|
case HTTP_STATUS_POLL:
|
2015-11-23 14:13:26 +00:00
|
|
|
goto task_finished;
|
2015-05-05 15:36:58 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2015-07-08 19:18:34 +00:00
|
|
|
|
2015-11-25 02:12:31 +00:00
|
|
|
if (task->cancelled || http->error)
|
2015-11-25 01:17:38 +00:00
|
|
|
goto task_finished;
|
|
|
|
|
2015-11-23 14:13:26 +00:00
|
|
|
return;
|
|
|
|
task_finished:
|
|
|
|
task->finished = true;
|
2015-07-08 19:35:24 +00:00
|
|
|
|
2015-11-23 14:13:26 +00:00
|
|
|
if (http->handle)
|
|
|
|
{
|
2015-11-24 01:30:57 +00:00
|
|
|
size_t len = 0;
|
|
|
|
char *tmp = (char*)net_http_data(http->handle, &len, false);
|
|
|
|
|
|
|
|
if (tmp && http->cb)
|
|
|
|
http->cb(tmp, len);
|
|
|
|
|
2015-11-25 01:17:38 +00:00
|
|
|
if (net_http_error(http->handle) || task->cancelled)
|
2015-11-24 01:30:57 +00:00
|
|
|
{
|
|
|
|
tmp = (char*)net_http_data(http->handle, &len, true);
|
|
|
|
|
|
|
|
if (tmp)
|
|
|
|
free(tmp);
|
|
|
|
|
2015-11-25 01:17:38 +00:00
|
|
|
if (task->cancelled)
|
|
|
|
task->error = strdup("Task cancelled.");
|
|
|
|
else
|
|
|
|
task->error = strdup("Download failed.");
|
2015-11-24 01:30:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data = (http_transfer_data_t*)calloc(1, sizeof(*data));
|
|
|
|
data->data = tmp;
|
|
|
|
data->len = len;
|
2015-07-08 19:18:34 +00:00
|
|
|
|
2015-11-24 01:30:57 +00:00
|
|
|
task->task_data = data;
|
|
|
|
}
|
2015-07-08 19:35:24 +00:00
|
|
|
|
2015-11-23 14:13:26 +00:00
|
|
|
net_http_delete(http->handle);
|
2015-11-25 02:12:31 +00:00
|
|
|
} else if (http->error)
|
|
|
|
task->error = strdup("Internal error.");
|
2015-07-08 19:35:24 +00:00
|
|
|
|
2015-11-23 14:13:26 +00:00
|
|
|
free(http);
|
2015-07-08 19:35:24 +00:00
|
|
|
}
|
|
|
|
|
2015-11-30 14:36:53 +00:00
|
|
|
static bool rarch_task_http_finder(rarch_task_t *task, void *user_data)
|
|
|
|
{
|
|
|
|
http_handle_t *http = (http_handle_t*)task->state;
|
|
|
|
const char *handle_url;
|
|
|
|
if (task->handler != rarch_task_http_transfer_handler)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
handle_url = net_http_connection_url(http->connection.handle);
|
|
|
|
|
|
|
|
return strcmp(handle_url, (const char*)user_data) == 0;
|
|
|
|
}
|
|
|
|
|
2015-11-23 14:13:26 +00:00
|
|
|
bool rarch_task_push_http_transfer(const char *url, const char *type, rarch_task_callback_t cb, void *user_data)
|
2015-07-08 19:35:24 +00:00
|
|
|
{
|
2015-11-23 14:13:26 +00:00
|
|
|
rarch_task_t *t;
|
|
|
|
http_handle_t *http;
|
|
|
|
struct http_connection_t *conn;
|
2015-11-30 12:17:46 +00:00
|
|
|
char tmp[PATH_MAX_LENGTH];
|
2015-07-08 19:35:24 +00:00
|
|
|
|
2015-11-23 14:13:26 +00:00
|
|
|
if (!url || !*url)
|
|
|
|
return false;
|
2015-07-08 19:38:22 +00:00
|
|
|
|
2015-11-30 14:36:53 +00:00
|
|
|
/* Concurrent download of the same file is not allowed */
|
|
|
|
if (rarch_task_find(rarch_task_http_finder, (void*)url))
|
|
|
|
{
|
2015-11-30 14:43:05 +00:00
|
|
|
RARCH_LOG("[http] '%s'' is already being downloaded.\n", url);
|
2015-11-30 14:36:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-23 14:13:26 +00:00
|
|
|
conn = net_http_connection_new(url);
|
|
|
|
|
|
|
|
if (!conn)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
http = (http_handle_t*)calloc(1, sizeof(*http));
|
|
|
|
http->connection.handle = conn;
|
|
|
|
http->connection.cb = &cb_http_conn_default;
|
|
|
|
|
|
|
|
if (type)
|
|
|
|
strlcpy(http->connection.elem1, type, sizeof(http->connection.elem1));
|
|
|
|
|
|
|
|
http->status = HTTP_STATUS_CONNECTION_TRANSFER;
|
|
|
|
|
|
|
|
t = (rarch_task_t*)calloc(1, sizeof(*t));
|
|
|
|
t->handler = rarch_task_http_transfer_handler;
|
|
|
|
t->state = http;
|
|
|
|
t->callback = cb;
|
|
|
|
t->user_data = user_data;
|
2015-11-30 12:17:46 +00:00
|
|
|
t->progress = -1;
|
|
|
|
|
2015-11-30 13:02:36 +00:00
|
|
|
snprintf(tmp, sizeof(tmp), "%s '%s'", msg_hash_to_str(MSG_DOWNLOADING), path_basename(url));
|
2015-11-30 12:17:46 +00:00
|
|
|
t->title = strdup(tmp);
|
2015-11-23 14:13:26 +00:00
|
|
|
|
|
|
|
rarch_task_push(t);
|
|
|
|
|
|
|
|
return true;
|
2015-07-08 19:35:24 +00:00
|
|
|
}
|