Start implementing new http code

This commit is contained in:
twinaphex 2015-01-23 22:00:11 +01:00
parent 025791f95a
commit 5ff7bd53be
7 changed files with 76 additions and 67 deletions

View File

@ -42,49 +42,3 @@ http_retcode http_get_file(char *url, char **buf, int *len)
return http_get(urlfilename, buf, len, NULL);
}
/**
* http_download_file:
* @url : URL to file.
* @output_dir : Output directory for new file.
* @output_basename : Output basename for new file.
*
* Downloads a file at specified URL.
*
* Returns: bool (1) if successful, otherwise false (0).
**/
bool http_download_file(char *url, const char *output_dir,
const char *output_basename)
{
http_retcode status;
int len;
FILE *f;
char output_path[PATH_MAX_LENGTH];
char *buf;
status = http_get_file(url, &buf, &len);
if (status < 0)
{
RARCH_ERR("%i - Failure.\n", status);
return false;
}
fill_pathname_join(output_path, output_dir, output_basename,
sizeof(output_path));
f = fopen(output_path, "wb");
if (!f)
goto cleanup;
fwrite(buf, 1, len, f);
fclose(f);
cleanup:
if (buf)
free(buf);
return true;
}

View File

@ -39,19 +39,6 @@ extern "C" {
**/
http_retcode http_get_file(char *url, char **buf, int *len);
/**
* http_download_file:
* @url : URL to file.
* @output_dir : Output directory for new file.
* @output_basename : Output basename for new file.
*
* Downloads a file at specified URL.
*
* Returns: bool (1) if successful, otherwise false (0).
**/
bool http_download_file(char *url, const char *output_dir,
const char *output_basename);
#ifdef __cplusplus
}
#endif

View File

@ -30,6 +30,7 @@
#ifdef HAVE_NETPLAY
#include "../http_intf.h"
#include "../net_http.h"
#endif
#ifdef HAVE_LIBRETRODB
@ -810,6 +811,34 @@ static int action_ok_save_state(const char *path,
}
/* HACK - we have to find some way to pass state inbetween
* function pointer callback functions that don't necessarily
* call each other. */
static char core_manager_path[PATH_MAX_LENGTH];
static int cb_core_manager_download(void *data_, size_t len)
{
FILE *f;
char output_path[PATH_MAX_LENGTH];
char *data = (char*)data_;
if (!data)
return -1;
fill_pathname_join(output_path, g_settings.libretro_directory,
core_manager_path, sizeof(output_path));
f = fopen(output_path, "wb");
if (!f)
return -1;
fwrite(data, 1, len, f);
fclose(f);
return 0;
}
static int action_ok_core_manager_list(const char *path,
const char *label, unsigned type, size_t idx)
{
@ -818,8 +847,12 @@ static int action_ok_core_manager_list(const char *path,
fill_pathname_join(core_path, g_settings.network.buildbot_url,
path, sizeof(core_path));
if (!http_download_file(core_path, g_settings.libretro_directory, path))
return -1;
strlcpy(core_manager_path, path, sizeof(core_manager_path));
msg_queue_clear(g_extern.http_msg_queue);
msg_queue_push(g_extern.http_msg_queue, core_path, 0, 180);
net_http_set_pending_cb(cb_core_manager_download);
#endif
return 0;
}

View File

@ -183,6 +183,23 @@ static ssize_t net_http_recv(int fd, bool *error,
return -1;
}
static http_cb_t pending_cb;
void net_http_set_pending_cb(http_cb_t cb)
{
pending_cb = cb;
}
static http_cb_t net_http_get_pending_cb(void)
{
return pending_cb;
}
static void net_http_clear_pending_cb(void)
{
pending_cb = NULL;
}
http_t *net_http_new(const char * url)
{
bool error;
@ -238,6 +255,9 @@ http_t *net_http_new(const char * url)
state->len = 0;
state->buflen = 512;
state->data = (char*)malloc(state->buflen);
state->cb = net_http_get_pending_cb();
net_http_clear_pending_cb();
return state;

View File

@ -25,6 +25,8 @@
extern "C" {
#endif
typedef int (*http_cb_t )(void *data, size_t len);
typedef struct
{
int fd;
@ -41,10 +43,13 @@ typedef struct
size_t len;
size_t buflen;
char * data;
http_cb_t cb;
} http_t;
http_t *net_http_new(const char * url);
void net_http_set_pending_cb(http_cb_t cb);
/* You can use this to call net_http_update
* only when something will happen; select() it for reading. */
int net_http_fd(http_t *state);

View File

@ -5,7 +5,7 @@ int main(void)
{
char *w;
http_t *http1, *http2, *http3;
size_t q, pos = 0; size_t tot = 0;
size_t len, pos = 0; size_t tot = 0;
http1 = net_http_new("http://buildbot.libretro.com/nightly/win-x86/latest/mednafen_psx_libretro.dll.zip");
@ -15,7 +15,7 @@ int main(void)
http3 = net_http_new("http://www.wikipedia.org/");
while (!net_http_update(http3, NULL, NULL)) {}
w = (char*)net_http_data(http3, &q, false);
w = (char*)net_http_data(http3, &len, false);
printf("%.*s\n", (int)256, w);

View File

@ -888,6 +888,19 @@ static int rarch_main_iterate_http_transfer(void)
return 0;
}
static int rarch_main_iterate_http_parse(void)
{
size_t len;
char *w = (char*)net_http_data(g_extern.http_handle, &len, false);
if (w && g_extern.http_handle->cb)
g_extern.http_handle->cb(w, len);
net_http_delete(g_extern.http_handle);
return 0;
}
/**
* rarch_main_iterate_http_transfer:
*
@ -952,10 +965,7 @@ int rarch_main_iterate(void)
if (g_extern.http_handle)
{
if (!rarch_main_iterate_http_transfer())
{
/* TODO - we should have some function pointer
* we can call here. */
}
rarch_main_iterate_http_parse();
}
else
rarch_main_iterate_http_poll();