2015-07-13 16:26:30 +02:00
|
|
|
/* http -- http convenience functions
|
|
|
|
|
|
|
|
Copyright (C) 2008 bushing
|
2016-01-02 17:23:01 +01:00
|
|
|
2008-2014 Dimok, Cyan
|
2015-07-13 16:26:30 +02:00
|
|
|
|
|
|
|
This program 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 Foundation, version 2.
|
|
|
|
|
|
|
|
This program 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 this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2010-12-12 17:31:13 +01:00
|
|
|
#include "http.h"
|
|
|
|
#include "../svnrev.h"
|
2014-04-25 17:34:53 +02:00
|
|
|
#include "gecko.h"
|
2010-12-12 17:31:13 +01:00
|
|
|
|
|
|
|
extern char incommingIP[50];
|
2014-04-25 17:34:53 +02:00
|
|
|
static u8 retryloop = 0;
|
2010-12-12 17:31:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Emptyblock is a statically defined variable for functions to return if they are unable
|
|
|
|
* to complete a request
|
|
|
|
*/
|
|
|
|
const struct block emptyblock = { 0, NULL };
|
|
|
|
|
2016-01-02 17:23:01 +01:00
|
|
|
//The maximum amount of bytes to send per net_write() call
|
|
|
|
//#define NET_BUFFER_SIZE 1024
|
|
|
|
#define NET_BUFFER_SIZE 3600
|
|
|
|
|
2010-12-12 17:31:13 +01:00
|
|
|
// Write our message to the server
|
2016-01-02 17:23:01 +01:00
|
|
|
static s32 send_message(s32 server, char *msg)
|
2010-12-12 17:31:13 +01:00
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
s32 bytes_transferred = 0;
|
|
|
|
s32 remaining = strlen(msg);
|
|
|
|
while (remaining)
|
|
|
|
{
|
2016-01-02 17:23:01 +01:00
|
|
|
if ((bytes_transferred = net_write(server, msg, remaining > NET_BUFFER_SIZE ? NET_BUFFER_SIZE : remaining)) > 0)
|
2011-07-26 00:28:22 +02:00
|
|
|
{
|
|
|
|
remaining -= bytes_transferred;
|
2014-05-05 20:35:19 +02:00
|
|
|
msg += bytes_transferred;
|
2011-07-26 00:28:22 +02:00
|
|
|
usleep(20 * 1000);
|
|
|
|
}
|
|
|
|
else if (bytes_transferred < 0)
|
|
|
|
{
|
|
|
|
return bytes_transferred;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return -ENODATA;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2010-12-12 17:31:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect to a remote server via TCP on a specified port
|
|
|
|
*
|
|
|
|
* @param u32 ip address of the server to connect to
|
|
|
|
* @param u32 the port to connect to on the server
|
|
|
|
* @return s32 The connection to the server (negative number if connection could not be established)
|
|
|
|
*/
|
2016-01-02 17:23:01 +01:00
|
|
|
static s32 server_connect(u32 ipaddress, u16 socket_port)
|
2010-12-12 17:31:13 +01:00
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
//Initialize socket
|
|
|
|
s32 connection = net_socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
|
|
|
|
if (connection < 0) return connection;
|
|
|
|
|
|
|
|
struct sockaddr_in connect_addr;
|
|
|
|
memset(&connect_addr, 0, sizeof(connect_addr));
|
|
|
|
connect_addr.sin_family = AF_INET;
|
2014-05-05 20:35:19 +02:00
|
|
|
connect_addr.sin_port = htons(socket_port);
|
2011-07-26 00:28:22 +02:00
|
|
|
connect_addr.sin_addr.s_addr = ipaddress;
|
|
|
|
|
|
|
|
sprintf(incommingIP, "%s", inet_ntoa(connect_addr.sin_addr));
|
|
|
|
|
2016-01-02 17:23:01 +01:00
|
|
|
//Attemt to open the socket
|
2011-07-26 00:28:22 +02:00
|
|
|
if (net_connect(connection, (struct sockaddr*) &connect_addr, sizeof(connect_addr)) == -1)
|
|
|
|
{
|
|
|
|
net_close(connection);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return connection;
|
2010-12-12 17:31:13 +01:00
|
|
|
}
|
|
|
|
|
2016-01-02 17:23:01 +01:00
|
|
|
//The amount of memory in bytes reserved initially to store the HTTP response in
|
|
|
|
//Be careful in increasing this number, reading from a socket on the Wii
|
|
|
|
//will fail if you request more than 20k or so
|
|
|
|
#define HTTP_BUFFER_SIZE 1024 * 5
|
2015-07-13 16:26:30 +02:00
|
|
|
|
2016-01-02 17:23:01 +01:00
|
|
|
//The amount of memory the buffer should expanded with if the buffer is full
|
|
|
|
#define HTTP_BUFFER_GROWTH 1024 * 5
|
2010-12-12 17:31:13 +01:00
|
|
|
|
|
|
|
/**
|
2016-01-02 17:23:01 +01:00
|
|
|
* This function reads all the data from a connection into a buffer which it returns.
|
|
|
|
* It will return an empty buffer if something doesn't go as planned
|
2010-12-12 17:31:13 +01:00
|
|
|
*
|
|
|
|
* @param s32 connection The connection identifier to suck the response out of
|
2016-01-02 17:23:01 +01:00
|
|
|
* @return block A 'block' struct (see http.h) in which the buffer is located
|
2010-12-12 17:31:13 +01:00
|
|
|
*/
|
2016-01-02 17:23:01 +01:00
|
|
|
struct block read_message(s32 connection)
|
2010-12-12 17:31:13 +01:00
|
|
|
{
|
2016-01-02 17:23:01 +01:00
|
|
|
//Create a block of memory to put in the response
|
|
|
|
struct block buffer;
|
|
|
|
buffer.data = malloc(HTTP_BUFFER_SIZE);
|
|
|
|
buffer.size = HTTP_BUFFER_SIZE;
|
2011-07-26 00:28:22 +02:00
|
|
|
|
2016-01-02 17:23:01 +01:00
|
|
|
if (buffer.data == NULL)
|
2011-07-26 00:28:22 +02:00
|
|
|
{
|
2016-01-02 17:23:01 +01:00
|
|
|
return emptyblock;
|
2015-07-13 16:26:30 +02:00
|
|
|
}
|
|
|
|
|
2016-01-02 17:23:01 +01:00
|
|
|
//The offset variable always points to the first byte of memory that is free in the buffer
|
|
|
|
u32 offset = 0;
|
2015-07-13 16:26:30 +02:00
|
|
|
|
2016-01-02 17:23:01 +01:00
|
|
|
while (1)
|
2015-07-13 16:26:30 +02:00
|
|
|
{
|
2016-01-02 17:23:01 +01:00
|
|
|
//Fill the buffer with a new batch of bytes from the connection,
|
|
|
|
//starting from where we left of in the buffer till the end of the buffer
|
|
|
|
s32 bytes_read = net_read(connection, buffer.data + offset, buffer.size - offset);
|
2015-07-13 16:26:30 +02:00
|
|
|
|
2016-01-02 17:23:01 +01:00
|
|
|
//Anything below 0 is an error in the connection
|
|
|
|
if (bytes_read < 0)
|
2011-07-26 00:28:22 +02:00
|
|
|
{
|
2016-01-02 17:23:01 +01:00
|
|
|
//printf("Connection error from net_read() Errorcode: %i\n", bytes_read);
|
|
|
|
return emptyblock;
|
2015-07-13 16:26:30 +02:00
|
|
|
}
|
2011-07-26 00:28:22 +02:00
|
|
|
|
2016-01-02 17:23:01 +01:00
|
|
|
//No more bytes were read into the buffer,
|
|
|
|
//we assume this means the HTTP response is done
|
|
|
|
if (bytes_read == 0)
|
2015-07-13 16:26:30 +02:00
|
|
|
{
|
|
|
|
break;
|
2011-07-26 00:28:22 +02:00
|
|
|
}
|
|
|
|
|
2016-01-02 17:23:01 +01:00
|
|
|
offset += bytes_read;
|
2011-07-26 00:28:22 +02:00
|
|
|
|
2016-01-02 17:23:01 +01:00
|
|
|
//Check if we have enough buffer left over,
|
|
|
|
//if not expand it with an additional HTTP_BUFFER_GROWTH worth of bytes
|
|
|
|
if (offset >= buffer.size)
|
|
|
|
{
|
|
|
|
buffer.size += HTTP_BUFFER_GROWTH;
|
|
|
|
u8 * tmp = realloc(buffer.data, buffer.size);
|
|
|
|
|
|
|
|
if (tmp == NULL)
|
|
|
|
{
|
|
|
|
free(buffer.data);
|
|
|
|
return emptyblock;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
buffer.data = tmp;
|
|
|
|
}
|
2015-07-13 16:26:30 +02:00
|
|
|
}
|
2011-07-26 00:28:22 +02:00
|
|
|
|
2016-01-02 17:23:01 +01:00
|
|
|
//At the end of above loop offset should be precisely the amount of bytes that were read from the connection
|
|
|
|
buffer.size = offset;
|
|
|
|
|
|
|
|
//Shrink the size of the buffer so the data fits exactly in it
|
|
|
|
buffer.data = realloc(buffer.data, buffer.size);
|
|
|
|
|
|
|
|
return buffer;
|
2010-12-12 17:31:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Downloads the contents of a URL to memory
|
|
|
|
* This method is not threadsafe (because networking is not threadsafe on the Wii)
|
|
|
|
*/
|
|
|
|
struct block downloadfile(const char *url)
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
//Check if the url starts with "http://", if not it is not considered a valid url
|
2016-01-02 17:23:01 +01:00
|
|
|
if (strncmp(url, "http://", strlen("http://")) != 0)
|
2011-07-26 00:28:22 +02:00
|
|
|
{
|
2016-01-02 17:23:01 +01:00
|
|
|
//printf("URL '%s' doesn't start with 'http://'\n", url);
|
2015-07-13 16:26:30 +02:00
|
|
|
return emptyblock;
|
2016-01-02 17:23:01 +01:00
|
|
|
}
|
2011-07-26 00:28:22 +02:00
|
|
|
|
|
|
|
//Locate the path part of the url by searching for '/' past "http://"
|
2016-01-02 17:23:01 +01:00
|
|
|
char *path = strchr(url + strlen("http://"), '/');
|
2011-07-26 00:28:22 +02:00
|
|
|
|
|
|
|
//At the very least the url has to end with '/', ending with just a domain is invalid
|
|
|
|
if (path == NULL)
|
|
|
|
{
|
|
|
|
//printf("URL '%s' has no PATH part\n", url);
|
|
|
|
return emptyblock;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Extract the domain part out of the url
|
2016-01-02 17:23:01 +01:00
|
|
|
int domainlength = path - url - strlen("http://");
|
2011-07-26 00:28:22 +02:00
|
|
|
|
|
|
|
if (domainlength == 0)
|
|
|
|
{
|
|
|
|
//printf("No domain part in URL '%s'\n", url);
|
|
|
|
return emptyblock;
|
|
|
|
}
|
|
|
|
|
|
|
|
char domain[domainlength + 1];
|
2016-01-02 17:23:01 +01:00
|
|
|
strlcpy(domain, url + strlen("http://"), domainlength + 1);
|
2011-07-26 00:28:22 +02:00
|
|
|
|
|
|
|
//Parsing of the URL is done, start making an actual connection
|
|
|
|
u32 ipaddress = getipbynamecached(domain);
|
|
|
|
|
|
|
|
if (ipaddress == 0)
|
|
|
|
{
|
|
|
|
//printf("\ndomain %s could not be resolved", domain);
|
|
|
|
return emptyblock;
|
|
|
|
}
|
|
|
|
|
2016-01-02 17:23:01 +01:00
|
|
|
s32 connection = server_connect(ipaddress, 80);
|
2011-07-26 00:28:22 +02:00
|
|
|
|
|
|
|
if (connection < 0)
|
|
|
|
{
|
|
|
|
//printf("Error establishing connection");
|
|
|
|
return emptyblock;
|
|
|
|
}
|
|
|
|
|
2014-05-05 20:35:19 +02:00
|
|
|
// Remove Referer from the request header for incompatible websites (ex. Cloudflare proxy)
|
|
|
|
char referer[domainlength + 12];
|
|
|
|
snprintf(referer, sizeof(referer), "Referer: %s\r\n", domain);
|
|
|
|
if(strstr(url, "geckocodes"))
|
|
|
|
{
|
|
|
|
strcpy(referer, "");
|
|
|
|
}
|
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
//Form a nice request header to send to the webserver
|
2014-05-05 20:35:19 +02:00
|
|
|
char* headerformat = "GET %s HTTP/1.0\r\nHost: %s\r\n%sUser-Agent: USBLoaderGX r%s\r\n\r\n";
|
|
|
|
char header[strlen(headerformat) + strlen(path) + strlen(domain) + strlen(referer) + 100];
|
|
|
|
sprintf(header, headerformat, path, domain, referer, GetRev());
|
2014-04-25 17:34:53 +02:00
|
|
|
//gprintf("\nHTTP Request:\n");
|
|
|
|
//gprintf("%s\n",header);
|
2011-07-26 00:28:22 +02:00
|
|
|
|
|
|
|
//Do the request and get the response
|
2016-01-02 17:23:01 +01:00
|
|
|
send_message(connection, header);
|
|
|
|
struct block response = read_message(connection);
|
|
|
|
net_close(connection);
|
|
|
|
|
|
|
|
// dump response
|
|
|
|
// hexdump(response.data, response.size);
|
|
|
|
|
|
|
|
//Search for the 4-character sequence \r\n\r\n in the response which signals the start of the http payload (file)
|
|
|
|
unsigned char *filestart = NULL;
|
|
|
|
u32 filesize = 0;
|
|
|
|
u32 i;
|
|
|
|
char newURL[512];
|
|
|
|
bool redirect = false;
|
|
|
|
|
|
|
|
for (i = 3; i < response.size; i++)
|
2011-07-26 00:28:22 +02:00
|
|
|
{
|
2016-01-02 17:23:01 +01:00
|
|
|
if (response.data[i] == '\n' && response.data[i - 1] == '\r' && response.data[i - 2] == '\n' && response.data[i - 3] == '\r')
|
2015-07-13 16:26:30 +02:00
|
|
|
{
|
2016-01-02 17:23:01 +01:00
|
|
|
filestart = response.data + i + 1;
|
|
|
|
filesize = response.size - i - 1;
|
|
|
|
|
|
|
|
// Check the HTTP response code
|
|
|
|
if (response.size > 10 && strncmp((char*)response.data, "HTTP/", 5)==0)
|
|
|
|
{
|
|
|
|
char htstat[i];
|
|
|
|
strncpy(htstat, (char*)response.data, i);
|
|
|
|
htstat[i] = 0;
|
|
|
|
char *codep;
|
|
|
|
codep = strchr(htstat, ' ');
|
|
|
|
if (codep)
|
|
|
|
{
|
|
|
|
int code;
|
|
|
|
if (sscanf(codep+1, "%d", &code) == 1)
|
|
|
|
{
|
|
|
|
//gprintf("HTTP response code: %d\n", code);
|
|
|
|
if (code == 302) // 302 FOUND (redirected link)
|
|
|
|
{
|
|
|
|
char *ptr = strcasestr((char*)response.data, "Location: ");
|
|
|
|
if(ptr)
|
|
|
|
{
|
|
|
|
ptr += strlen("Location: ");
|
|
|
|
strncpy(newURL, ptr, sizeof(newURL));
|
|
|
|
*(strchr(newURL, '\r'))=0;
|
|
|
|
|
|
|
|
redirect = true;
|
|
|
|
//gprintf("New URL to download = %s \n", newURL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//gprintf("HTTP ERROR: %s\n", htstat);
|
|
|
|
free(response.data);
|
|
|
|
return emptyblock;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (code >=400) // Not found
|
|
|
|
{
|
|
|
|
//gprintf("HTTP ERROR: %s\n", htstat);
|
|
|
|
free(response.data);
|
|
|
|
return emptyblock;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2015-07-13 16:26:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-02 17:23:01 +01:00
|
|
|
if(redirect)
|
|
|
|
{
|
|
|
|
// Prevent endless loop
|
2014-04-25 17:34:53 +02:00
|
|
|
retryloop++;
|
|
|
|
if(retryloop > 3)
|
|
|
|
{
|
|
|
|
retryloop = 0;
|
2016-01-02 17:23:01 +01:00
|
|
|
free(response.data);
|
2014-04-25 17:34:53 +02:00
|
|
|
return emptyblock;
|
|
|
|
}
|
2016-01-02 17:23:01 +01:00
|
|
|
|
|
|
|
struct block redirected = downloadfile(newURL);
|
2014-04-25 17:34:53 +02:00
|
|
|
|
|
|
|
// copy the newURL data into the original data
|
|
|
|
u8 * tmp = realloc(response.data, redirected.size);
|
|
|
|
if (tmp == NULL)
|
|
|
|
{
|
2016-01-02 17:23:01 +01:00
|
|
|
//gprintf("Could not allocate enough memory for new URL. Download canceled.\n");
|
2014-04-25 17:34:53 +02:00
|
|
|
free(response.data);
|
|
|
|
free(redirected.data);
|
|
|
|
return emptyblock;
|
|
|
|
}
|
|
|
|
response.data = tmp;
|
|
|
|
memcpy(response.data, redirected.data, redirected.size);
|
|
|
|
|
2016-01-02 17:23:01 +01:00
|
|
|
// Set filestart's new size based on redirected file
|
|
|
|
filestart = response.data;
|
|
|
|
filesize = redirected.size;
|
|
|
|
free(redirected.data);
|
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
}
|
2015-07-13 16:26:30 +02:00
|
|
|
retryloop = 0;
|
2016-01-02 17:23:01 +01:00
|
|
|
|
|
|
|
if (filestart == NULL)
|
2011-07-26 00:28:22 +02:00
|
|
|
{
|
2016-01-02 17:23:01 +01:00
|
|
|
//printf("HTTP Response was without a file\n");
|
|
|
|
free(response.data);
|
|
|
|
return emptyblock;
|
2011-07-26 00:28:22 +02:00
|
|
|
}
|
|
|
|
|
2016-01-02 17:23:01 +01:00
|
|
|
//Copy the file part of the response into a new memoryblock to return
|
|
|
|
struct block file;
|
|
|
|
file.data = malloc(filesize);
|
|
|
|
file.size = filesize;
|
|
|
|
|
|
|
|
if (file.data == NULL)
|
|
|
|
{
|
|
|
|
//printf("No more memory to copy file from HTTP response\n");
|
|
|
|
free(response.data);
|
|
|
|
return emptyblock;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(file.data, filestart, filesize);
|
|
|
|
|
|
|
|
//Dispose of the original response
|
|
|
|
free(response.data);
|
|
|
|
|
|
|
|
return file;
|
2010-12-12 17:31:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
s32 GetConnection(char * domain)
|
|
|
|
{
|
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
u32 ipaddress = getipbynamecached(domain);
|
|
|
|
if (ipaddress == 0)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2016-01-02 17:23:01 +01:00
|
|
|
s32 connection = server_connect(ipaddress, 80);
|
2011-07-26 00:28:22 +02:00
|
|
|
return connection;
|
2010-12-12 17:31:13 +01:00
|
|
|
|
|
|
|
}
|