Add function http_file_download

This commit is contained in:
twinaphex 2015-01-21 01:12:26 +01:00
parent 0e9e0aa48c
commit 181d0849c1
5 changed files with 115 additions and 86 deletions

View File

@ -18,8 +18,55 @@
#include <assert.h>
#include "http_intf.h"
#include "retroarch_logger.h"
#include <retro_miscellaneous.h>
#include "general.h"
#include <file/file_path.h>
/**
* 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;

View File

@ -17,6 +17,7 @@
#ifndef _HTTP_INTF_H
#define _HTTP_INTF_H
#include <boolean.h>
#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

View File

@ -33,7 +33,7 @@
#define VERBOSE
/* http_lib - Http data exchanges mini library.
/* http_lib - HTTP data exchanges mini library.
*/
#include <stdio.h>
@ -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<max)
while (n < max)
{
if (read(fd, buffer, 1) != 1)
{
@ -269,8 +269,9 @@ static http_retcode http_query(const char *command, const char *url,
return ret;
}
}
/* close socket */
close(s);
close(s); /* close socket */
return ret;
}
@ -305,9 +306,41 @@ http_retcode http_put(const char *filename, const char *data,
else
snprintf(header, sizeof(header), "Content-length: %d\015\012%s",length,
overwrite ? "Control: overwrite=1\015\012" : "");
return http_query("PUT", filename, header, CLOSE, data, length, NULL);
}
static http_retcode http_read_line_loop(int fd, char *header,
int *length, char *typebuf)
{
int n;
char *pc;
while (1)
{
n = http_read_line(fd, header, MAXBUF-1);
#ifdef VERBOSE
fputs(header, stderr);
putc('\n', stderr);
#endif
if (n <= 0)
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);
}
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);

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#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);
}

View File

@ -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;
}