diff --git a/http_intf.c b/http_intf.c index a8b7e6eec1..84231439d4 100644 --- a/http_intf.c +++ b/http_intf.c @@ -18,8 +18,55 @@ #include #include "http_intf.h" #include "retroarch_logger.h" +#include #include "general.h" +#include + +/** + * 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 *urlfilename = NULL, *out; + + http_parse_url(url, &urlfilename); + + status = http_get(urlfilename, &out, &len, NULL); + + 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) + return false; + + fwrite(out, 1,len, f); + + fclose(f); + + return true; +} + int http_intf_command(unsigned mode, char *url) { int ret, lg, blocksize, r; diff --git a/http_intf.h b/http_intf.h index 91e517b3a2..cd35735b21 100644 --- a/http_intf.h +++ b/http_intf.h @@ -17,6 +17,7 @@ #ifndef _HTTP_INTF_H #define _HTTP_INTF_H +#include #include "netplay_compat.h" #include "http_lib.h" @@ -29,6 +30,19 @@ enum HTTP_INTF_HEAD }; +/** + * 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); + int http_intf_command(unsigned mode, char *url); #endif diff --git a/http_lib.c b/http_lib.c index a6673345a3..aa2816ff04 100644 --- a/http_lib.c +++ b/http_lib.c @@ -33,7 +33,7 @@ #define VERBOSE -/* http_lib - Http data exchanges mini library. +/* http_lib - HTTP data exchanges mini library. */ #include @@ -80,7 +80,7 @@ static int http_read_line (int fd, char *buffer, int max) /* not efficient on long lines (multiple unbuffered 1 char reads) */ int n = 0; - while (n end of header) */ + if (n > 0 && (*header) == '\0') + break; + + /* Try to parse some keywords : */ + /* convert to lower case 'till a : is found or end of string */ + for (pc=header; (*pc != ':' && *pc) ; pc++) + *pc = tolower(*pc); + sscanf(header, "content-length: %d", length); + + if (typebuf) + sscanf(header, "content-type: %s", typebuf); + } + return 0; +} /** * http_get: @@ -353,35 +386,18 @@ http_retcode http_get(const char *filename, if (ret == 200) { - while (1) + if ((ret = http_read_line_loop(fd, header, &length, typebuf)) != 0) { - n = http_read_line(fd,header,MAXBUF-1); -#ifdef VERBOSE - fputs(header,stderr); - putc('\n',stderr); -#endif - if (n <= 0) - { - close(fd); - return ERRRDHD; - } - /* empty line ? (=> end of header) */ - if ( n>0 && (*header) == '\0') - break; - /* try to parse some keywords : */ - /* convert to lower case 'till a : is found or end of string */ - for (pc=header; (*pc != ':' && *pc) ; pc++) - *pc=tolower(*pc); - sscanf(header,"content-length: %d", &length); - if (typebuf) - sscanf(header,"content-type: %s", typebuf); + close(fd); + return ret; } - if (length<=0) + if (length <= 0) { close(fd); return ERRNOLG; } + if (plength) *plength = length; @@ -428,8 +444,8 @@ http_retcode http_get(const char *filename, http_retcode http_head(const char *filename, int *plength, char *typebuf) { http_retcode ret; - char header[MAXBUF], *pc; - int fd, n, length = -1; + char header[MAXBUF]; + int fd, length = -1; if (plength) *plength=0; @@ -440,32 +456,12 @@ http_retcode http_head(const char *filename, int *plength, char *typebuf) if (ret == 200) { - while (1) + if ((ret = http_read_line_loop(fd, header, &length, typebuf)) != 0) { - n = http_read_line(fd, header, MAXBUF-1); -#ifdef VERBOSE - fputs(header, stderr); - putc('\n', stderr); -#endif - if (n <= 0) - { - close(fd); - return ERRRDHD; - } - - /* Empty line ? (=> end of header) */ - if (n > 0 && (*header) == '\0') - break; - - /* Try to parse some keywords : */ - /* convert to lower case 'till a : is found or end of string */ - for (pc=header; (*pc != ':' && *pc) ; pc++) - *pc = tolower(*pc); - sscanf(header, "content-length: %d", &length); - - if (typebuf) - sscanf(header, "content-type: %s", typebuf); + close(fd); + return ret; } + if (plength) *plength = length; close(fd); diff --git a/http_test.c b/http_test.c deleted file mode 100644 index 8159aeb088..0000000000 --- a/http_test.c +++ /dev/null @@ -1,34 +0,0 @@ -/* RetroArch - A frontend for libretro. - * Copyright (C) 2011-2015 - Daniel De Matteis - * Copyright (C) 2014-2015 - Alfred Agrell - * - * 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 . - */ - -#include -#include "http_lib.h" - -int main(void) -{ - char url[]="http://buildbot.libretro.com/nightly/android/latest/armeabi-v7a/2048_libretro.so.zip"; - char* urlfilename=NULL; - int len; - char * out; - FILE * f; - http_parse_url(url, &urlfilename); - http_retcode status=http_get(urlfilename, &out, &len, NULL); - if (status<0) printf("%i - failure...\n", status); - else printf("%i - success\n", status); - f=fopen("2048_libretro.so.zip", "wb"); - fwrite(out, 1,len, f); - fclose(f); -} diff --git a/menu/menu_entries_cbs.c b/menu/menu_entries_cbs.c index 4bdc0906af..5cfe738a58 100644 --- a/menu/menu_entries_cbs.c +++ b/menu/menu_entries_cbs.c @@ -28,6 +28,7 @@ #include "../retroarch.h" #include "../performance.h" +#include "../http_intf.h" #include "../input/input_remapping.h" #ifdef GEKKO @@ -798,9 +799,14 @@ static int generic_action_ok_command(unsigned cmd) return 0; } + static int action_ok_core_manager_list(const char *path, const char *label, unsigned type, size_t idx) { + char url[] = "http://buildbot.libretro.com/nightly/android/latest/armeabi-v7a/2048_libretro.so.zip"; + + if (!http_download_file(url, "/home/squarepusher", "2048_libretro.so.zip")) + return -1; return 0; }