2008-12-24 08:58:23 +01:00
|
|
|
/****************************************************************************
|
2009-07-22 04:05:49 +02:00
|
|
|
* FCE Ultra
|
2008-12-24 08:58:23 +01:00
|
|
|
* Nintendo Wii/Gamecube Port
|
|
|
|
*
|
|
|
|
* Tantric December 2008
|
|
|
|
*
|
|
|
|
* http.cpp
|
|
|
|
*
|
|
|
|
* HTTP operations
|
2009-01-23 18:41:33 +01:00
|
|
|
* Written by dhewg/bushing, modified by Tantric
|
2008-12-24 08:58:23 +01:00
|
|
|
***************************************************************************/
|
|
|
|
|
2010-04-15 01:54:57 +02:00
|
|
|
#ifdef HW_RVL
|
|
|
|
|
2008-12-24 08:58:23 +01:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <ogcsys.h>
|
|
|
|
#include <network.h>
|
|
|
|
#include <ogc/lwp_watchdog.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2009-03-28 18:23:08 +01:00
|
|
|
#include "menu.h"
|
2008-12-24 08:58:23 +01:00
|
|
|
#include "http.h"
|
|
|
|
|
2010-06-23 19:22:46 +02:00
|
|
|
#define TCP_CONNECT_TIMEOUT 4000 // 4 secs to make a connection
|
|
|
|
#define TCP_SEND_SIZE (32 * 1024)
|
|
|
|
#define TCP_RECV_SIZE (32 * 1024)
|
|
|
|
#define TCP_BLOCK_RECV_TIMEOUT 4000 // 4 secs to receive
|
|
|
|
#define TCP_BLOCK_SEND_TIMEOUT 4000 // 4 secs to send
|
|
|
|
#define TCP_BLOCK_SIZE 1024
|
|
|
|
#define HTTP_TIMEOUT 10000 // 10 secs to get an http response
|
|
|
|
#define IOS_O_NONBLOCK 0x04
|
|
|
|
|
2008-12-24 08:58:23 +01:00
|
|
|
static s32 tcp_socket(void)
|
|
|
|
{
|
|
|
|
s32 s, res;
|
|
|
|
|
2010-06-27 02:42:46 +02:00
|
|
|
s = net_socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
|
2008-12-24 08:58:23 +01:00
|
|
|
if (s < 0)
|
|
|
|
return s;
|
|
|
|
|
2010-06-27 02:42:46 +02:00
|
|
|
// Switch off Nagle with TCP_NODELAY
|
|
|
|
u32 nodelay = 1;
|
|
|
|
net_setsockopt(s,IPPROTO_TCP,TCP_NODELAY,&nodelay,sizeof(nodelay));
|
|
|
|
|
2008-12-24 08:58:23 +01:00
|
|
|
res = net_fcntl(s, F_GETFL, 0);
|
|
|
|
if (res < 0)
|
|
|
|
{
|
|
|
|
net_close(s);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2010-06-23 19:22:46 +02:00
|
|
|
res = net_fcntl(s, F_SETFL, res | IOS_O_NONBLOCK);
|
2008-12-24 08:58:23 +01:00
|
|
|
if (res < 0)
|
|
|
|
{
|
|
|
|
net_close(s);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static s32 tcp_connect(char *host, const u16 port)
|
|
|
|
{
|
|
|
|
struct hostent *hp;
|
|
|
|
struct sockaddr_in sa;
|
2010-06-23 19:22:46 +02:00
|
|
|
struct in_addr val;
|
2008-12-24 08:58:23 +01:00
|
|
|
s32 s, res;
|
2011-06-05 07:24:49 +02:00
|
|
|
u64 t1;
|
2008-12-24 08:58:23 +01:00
|
|
|
|
|
|
|
s = tcp_socket();
|
|
|
|
if (s < 0)
|
|
|
|
return s;
|
|
|
|
|
|
|
|
memset(&sa, 0, sizeof(struct sockaddr_in));
|
2010-06-23 19:22:46 +02:00
|
|
|
sa.sin_family= PF_INET;
|
2008-12-24 08:58:23 +01:00
|
|
|
sa.sin_len = sizeof(struct sockaddr_in);
|
2010-06-23 19:22:46 +02:00
|
|
|
sa.sin_port= htons(port);
|
2008-12-24 08:58:23 +01:00
|
|
|
|
2010-06-23 19:22:46 +02:00
|
|
|
if(strlen(host) < 16 && inet_aton(host, &val))
|
2008-12-24 08:58:23 +01:00
|
|
|
{
|
2010-06-23 19:22:46 +02:00
|
|
|
sa.sin_addr.s_addr = val.s_addr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hp = net_gethostbyname (host);
|
|
|
|
if (!hp || !(hp->h_addrtype == PF_INET))
|
|
|
|
return errno;
|
2008-12-24 08:58:23 +01:00
|
|
|
|
2010-06-23 19:22:46 +02:00
|
|
|
memcpy((char *) &sa.sin_addr, hp->h_addr_list[0], hp->h_length);
|
|
|
|
}
|
2008-12-24 08:58:23 +01:00
|
|
|
|
2011-06-05 07:24:49 +02:00
|
|
|
t1=ticks_to_secs(gettime());
|
|
|
|
do
|
2010-06-23 19:22:46 +02:00
|
|
|
{
|
2011-06-05 07:24:49 +02:00
|
|
|
res = net_connect(s,(struct sockaddr*) &sa, sizeof (sa));
|
|
|
|
if(ticks_to_secs(gettime())-t1 > TCP_CONNECT_TIMEOUT*1000) break;
|
|
|
|
usleep(500);
|
|
|
|
}while(res != -EISCONN);
|
|
|
|
if(res != -EISCONN)
|
|
|
|
{
|
|
|
|
net_close(s);
|
|
|
|
return -1;
|
2008-12-24 08:58:23 +01:00
|
|
|
}
|
2011-06-05 07:24:49 +02:00
|
|
|
|
|
|
|
|
2008-12-24 08:58:23 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2010-07-31 04:22:44 +02:00
|
|
|
static int tcp_readln(const s32 s, char *buf, const u16 max_length)
|
2008-12-24 08:58:23 +01:00
|
|
|
{
|
2010-07-31 04:22:44 +02:00
|
|
|
s32 res = -1;
|
|
|
|
s32 ret;
|
|
|
|
u64 start_time = gettime();
|
|
|
|
u16 c = 0;
|
2008-12-24 08:58:23 +01:00
|
|
|
|
2010-07-31 04:22:44 +02:00
|
|
|
while (c < max_length)
|
2008-12-24 08:58:23 +01:00
|
|
|
{
|
2010-07-31 04:22:44 +02:00
|
|
|
if (ticks_to_millisecs(diff_ticks(start_time, gettime())) > HTTP_TIMEOUT)
|
2008-12-24 08:58:23 +01:00
|
|
|
break;
|
|
|
|
|
2010-07-31 04:22:44 +02:00
|
|
|
ret = net_read(s, &buf[c], 1);
|
2008-12-24 08:58:23 +01:00
|
|
|
|
2011-06-05 07:24:49 +02:00
|
|
|
if (ret == -EAGAIN)
|
2008-12-24 08:58:23 +01:00
|
|
|
{
|
|
|
|
usleep(20 * 1000);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-06-05 07:24:49 +02:00
|
|
|
if (ret <= 0)
|
2008-12-24 08:58:23 +01:00
|
|
|
break;
|
|
|
|
|
2010-07-31 04:22:44 +02:00
|
|
|
if (c > 0 && buf[c - 1] == '\r' && buf[c] == '\n')
|
2008-12-24 08:58:23 +01:00
|
|
|
{
|
2010-07-31 04:22:44 +02:00
|
|
|
res = 0;
|
|
|
|
buf[c-1] = 0;
|
2008-12-24 08:58:23 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
c++;
|
2011-06-05 07:24:49 +02:00
|
|
|
start_time = gettime();
|
2010-07-31 04:22:44 +02:00
|
|
|
usleep(100);
|
2008-12-24 08:58:23 +01:00
|
|
|
}
|
2010-07-31 04:22:44 +02:00
|
|
|
return res;
|
2008-12-24 08:58:23 +01:00
|
|
|
}
|
|
|
|
|
2011-06-05 07:24:49 +02:00
|
|
|
static u32 tcp_read(const s32 s, u8 *buffer, const u32 length)
|
2008-12-24 08:58:23 +01:00
|
|
|
{
|
2010-06-23 19:22:46 +02:00
|
|
|
char *p;
|
|
|
|
u32 left, block, received, step=0;
|
2011-06-05 07:24:49 +02:00
|
|
|
u64 t;
|
2008-12-24 08:58:23 +01:00
|
|
|
s32 res;
|
|
|
|
|
2010-06-23 19:22:46 +02:00
|
|
|
p = (char *)buffer;
|
2008-12-24 08:58:23 +01:00
|
|
|
left = length;
|
|
|
|
received = 0;
|
|
|
|
|
|
|
|
t = gettime();
|
|
|
|
while (left)
|
|
|
|
{
|
|
|
|
if (ticks_to_millisecs(diff_ticks(t, gettime()))
|
|
|
|
> TCP_BLOCK_RECV_TIMEOUT)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
block = left;
|
2010-06-23 19:22:46 +02:00
|
|
|
if (block > TCP_RECV_SIZE)
|
|
|
|
block = TCP_RECV_SIZE;
|
2008-12-24 08:58:23 +01:00
|
|
|
|
|
|
|
res = net_read(s, p, block);
|
|
|
|
|
2011-06-05 07:24:49 +02:00
|
|
|
if (res == -EAGAIN)
|
2008-12-24 08:58:23 +01:00
|
|
|
{
|
2011-06-05 07:24:49 +02:00
|
|
|
usleep(20 * 1000);
|
|
|
|
continue;
|
2008-12-24 08:58:23 +01:00
|
|
|
}
|
|
|
|
|
2011-06-05 07:24:49 +02:00
|
|
|
if(res<=0) break;
|
|
|
|
|
|
|
|
received += res;
|
|
|
|
left -= res;
|
|
|
|
p += res;
|
2010-06-23 19:22:46 +02:00
|
|
|
usleep(1000);
|
2008-12-24 08:58:23 +01:00
|
|
|
|
|
|
|
if ((received / TCP_BLOCK_SIZE) > step)
|
|
|
|
{
|
2010-06-23 19:22:46 +02:00
|
|
|
t = gettime ();
|
2008-12-24 08:58:23 +01:00
|
|
|
step++;
|
|
|
|
}
|
|
|
|
}
|
2010-06-23 19:22:46 +02:00
|
|
|
return received;
|
2008-12-24 08:58:23 +01:00
|
|
|
}
|
|
|
|
|
2011-06-05 07:24:49 +02:00
|
|
|
static u32 tcp_write(const s32 s, const u8 *buffer, const u32 length)
|
2008-12-24 08:58:23 +01:00
|
|
|
{
|
|
|
|
const u8 *p;
|
2010-06-23 19:22:46 +02:00
|
|
|
u32 left, block, sent, step=0;
|
2008-12-24 08:58:23 +01:00
|
|
|
s64 t;
|
|
|
|
s32 res;
|
|
|
|
|
|
|
|
p = buffer;
|
|
|
|
left = length;
|
|
|
|
sent = 0;
|
|
|
|
|
|
|
|
t = gettime();
|
|
|
|
while (left)
|
|
|
|
{
|
|
|
|
if (ticks_to_millisecs(diff_ticks(t, gettime()))
|
|
|
|
> TCP_BLOCK_SEND_TIMEOUT)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
block = left;
|
2010-06-23 19:22:46 +02:00
|
|
|
if (block > TCP_SEND_SIZE)
|
|
|
|
block = TCP_SEND_SIZE;
|
2008-12-24 08:58:23 +01:00
|
|
|
|
|
|
|
res = net_write(s, p, block);
|
|
|
|
|
|
|
|
if ((res == 0) || (res == -56))
|
|
|
|
{
|
|
|
|
usleep(20 * 1000);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
sent += res;
|
|
|
|
left -= res;
|
|
|
|
p += res;
|
2010-06-23 19:22:46 +02:00
|
|
|
usleep(100);
|
2008-12-24 08:58:23 +01:00
|
|
|
|
|
|
|
if ((sent / TCP_BLOCK_SIZE) > step)
|
|
|
|
{
|
2010-06-23 19:22:46 +02:00
|
|
|
t = gettime ();
|
2008-12-24 08:58:23 +01:00
|
|
|
step++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return left == 0;
|
|
|
|
}
|
2010-07-31 04:22:44 +02:00
|
|
|
|
|
|
|
static bool http_split_url(char *host, char *path, const char *url)
|
2008-12-24 08:58:23 +01:00
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
char *c;
|
|
|
|
|
|
|
|
if (strncasecmp(url, "http://", 7))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
p = url + 7;
|
|
|
|
c = strchr(p, '/');
|
|
|
|
|
2010-06-23 19:22:46 +02:00
|
|
|
if (c == NULL || c[0] == 0)
|
2008-12-24 08:58:23 +01:00
|
|
|
return false;
|
|
|
|
|
2010-10-07 21:55:00 +02:00
|
|
|
snprintf(host, c-p+1, "%s", p);
|
2010-07-31 04:22:44 +02:00
|
|
|
strcpy(path, c);
|
2008-12-24 08:58:23 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-06-23 19:22:46 +02:00
|
|
|
#define MAX_SIZE (1024*1024*15)
|
|
|
|
|
2008-12-24 08:58:23 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* http_request
|
|
|
|
* Retrieves the specified URL, and stores it in the specified file or buffer
|
|
|
|
***************************************************************************/
|
2010-07-31 04:22:44 +02:00
|
|
|
int http_request(const char *url, FILE *hfile, u8 *buffer, u32 maxsize, bool silent)
|
2008-12-24 08:58:23 +01:00
|
|
|
{
|
2009-07-01 19:35:39 +02:00
|
|
|
int res = 0;
|
2010-07-31 04:22:44 +02:00
|
|
|
char http_host[1024];
|
|
|
|
char http_path[1024];
|
2008-12-24 08:58:23 +01:00
|
|
|
u16 http_port;
|
|
|
|
|
|
|
|
http_res result;
|
|
|
|
u32 http_status;
|
2010-06-23 19:22:46 +02:00
|
|
|
u32 sizeread = 0, content_length = 0;
|
2008-12-24 08:58:23 +01:00
|
|
|
|
|
|
|
int linecount;
|
|
|
|
|
2010-06-23 19:22:46 +02:00
|
|
|
if(maxsize > MAX_SIZE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (url == NULL || (hfile == NULL && buffer == NULL))
|
|
|
|
return 0;
|
2010-07-31 04:22:44 +02:00
|
|
|
|
|
|
|
if (!http_split_url(http_host, http_path, url))
|
2010-06-23 19:22:46 +02:00
|
|
|
return 0;
|
2008-12-24 08:58:23 +01:00
|
|
|
|
|
|
|
http_port = 80;
|
|
|
|
http_status = 404;
|
|
|
|
|
|
|
|
int s = tcp_connect(http_host, http_port);
|
|
|
|
|
|
|
|
if (s < 0)
|
|
|
|
{
|
|
|
|
result = HTTPR_ERR_CONNECT;
|
2010-06-23 19:22:46 +02:00
|
|
|
return 0;
|
2008-12-24 08:58:23 +01:00
|
|
|
}
|
|
|
|
|
2010-07-31 04:22:44 +02:00
|
|
|
char request[1024];
|
2008-12-24 08:58:23 +01:00
|
|
|
char *r = request;
|
2010-06-23 19:22:46 +02:00
|
|
|
|
2008-12-24 08:58:23 +01:00
|
|
|
r += sprintf(r, "GET %s HTTP/1.1\r\n", http_path);
|
|
|
|
r += sprintf(r, "Host: %s\r\n", http_host);
|
|
|
|
r += sprintf(r, "Cache-Control: no-cache\r\n\r\n");
|
|
|
|
|
2009-07-01 19:35:39 +02:00
|
|
|
res = tcp_write(s, (u8 *) request, strlen(request));
|
2008-12-24 08:58:23 +01:00
|
|
|
|
2010-07-31 04:22:44 +02:00
|
|
|
char line[256];
|
2008-12-24 08:58:23 +01:00
|
|
|
|
|
|
|
for (linecount = 0; linecount < 32; linecount++)
|
|
|
|
{
|
2010-07-31 04:22:44 +02:00
|
|
|
if (tcp_readln(s, line, 255) != 0)
|
2008-12-24 08:58:23 +01:00
|
|
|
{
|
|
|
|
http_status = 404;
|
|
|
|
result = HTTPR_ERR_REQUEST;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen(line) < 1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
sscanf(line, "HTTP/1.%*u %u", &http_status);
|
|
|
|
sscanf(line, "Content-Length: %u", &content_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (http_status != 200)
|
|
|
|
{
|
|
|
|
result = HTTPR_ERR_STATUS;
|
|
|
|
net_close(s);
|
2010-06-23 19:22:46 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// length unknown - just read as much as we can
|
|
|
|
if(content_length == 0)
|
|
|
|
{
|
|
|
|
content_length = maxsize;
|
2008-12-24 08:58:23 +01:00
|
|
|
}
|
2010-06-23 19:22:46 +02:00
|
|
|
else if (content_length > maxsize)
|
2008-12-24 08:58:23 +01:00
|
|
|
{
|
|
|
|
result = HTTPR_ERR_TOOBIG;
|
|
|
|
net_close(s);
|
2010-06-23 19:22:46 +02:00
|
|
|
return 0;
|
2008-12-24 08:58:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer != NULL)
|
|
|
|
{
|
2010-06-23 19:22:46 +02:00
|
|
|
if(!silent)
|
|
|
|
ShowAction("Downloading...");
|
|
|
|
|
|
|
|
sizeread = tcp_read(s, buffer, content_length);
|
|
|
|
|
|
|
|
if(!silent)
|
|
|
|
CancelAction();
|
2008-12-24 08:58:23 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// read into file
|
2010-07-31 04:22:44 +02:00
|
|
|
u32 bufSize = (1024 * 32);
|
2008-12-24 08:58:23 +01:00
|
|
|
u32 bytesLeft = content_length;
|
|
|
|
u32 readSize;
|
|
|
|
|
2010-06-23 19:22:46 +02:00
|
|
|
if(!silent)
|
|
|
|
ShowProgress("Downloading...", 0, content_length);
|
2009-07-01 19:35:39 +02:00
|
|
|
u8 * fbuffer = (u8 *) malloc(bufSize);
|
|
|
|
if(fbuffer)
|
2008-12-24 08:58:23 +01:00
|
|
|
{
|
2009-07-01 19:35:39 +02:00
|
|
|
while (bytesLeft > 0)
|
|
|
|
{
|
|
|
|
if (bytesLeft < bufSize)
|
|
|
|
readSize = bytesLeft;
|
|
|
|
else
|
|
|
|
readSize = bufSize;
|
|
|
|
|
2010-06-23 19:22:46 +02:00
|
|
|
res = tcp_read(s, fbuffer, readSize);
|
2009-07-01 19:35:39 +02:00
|
|
|
if (!res)
|
|
|
|
break;
|
2010-06-23 19:22:46 +02:00
|
|
|
|
|
|
|
sizeread += res;
|
|
|
|
bytesLeft -= res;
|
|
|
|
|
|
|
|
res = fwrite(fbuffer, 1, res, hfile);
|
2009-07-01 19:35:39 +02:00
|
|
|
if (!res)
|
|
|
|
break;
|
|
|
|
|
2010-06-23 19:22:46 +02:00
|
|
|
if(!silent)
|
|
|
|
ShowProgress("Downloading...", (content_length - bytesLeft), content_length);
|
2009-07-01 19:35:39 +02:00
|
|
|
}
|
|
|
|
free(fbuffer);
|
2008-12-24 08:58:23 +01:00
|
|
|
}
|
2010-06-23 19:22:46 +02:00
|
|
|
if(!silent)
|
|
|
|
CancelAction();
|
2008-12-24 08:58:23 +01:00
|
|
|
}
|
|
|
|
|
2009-07-01 19:35:39 +02:00
|
|
|
net_close(s);
|
|
|
|
|
2010-06-23 19:22:46 +02:00
|
|
|
if (content_length < maxsize && sizeread != content_length)
|
2008-12-24 08:58:23 +01:00
|
|
|
{
|
|
|
|
result = HTTPR_ERR_RECEIVE;
|
2010-06-23 19:22:46 +02:00
|
|
|
return 0;
|
2008-12-24 08:58:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
result = HTTPR_OK;
|
2010-06-23 19:22:46 +02:00
|
|
|
return sizeread;
|
2008-12-24 08:58:23 +01:00
|
|
|
}
|
2010-04-15 01:54:57 +02:00
|
|
|
#endif
|