Пример #1
0
static void handle_udp_recv_slave(void *arg, struct udp_pcb *pcb, struct pbuf *p,  ip_addr_t *addr, u16_t port) {
	os_printf("UDP Receive: %s \r\n", p->payload);
	while(lock) { os_delay_us(100); }
	network_request(p->payload, addr->addr);
	os_printf("all callbacks done\r\n");
	pbuf_free(p);
	os_printf("freeing pbuf \r\n");
}
Пример #2
0
/****************************************************************************
 * Download request
 ***************************************************************************/
s32 download_request(const char * url, char * filename)
{
	//Check if the url starts with "http://", if not it is not considered a valid url
	if (strncmp(url, "http://", strlen("http://")) != 0)
	{
		return -1;
	}

	//Locate the path part of the url by searching for '/' past "http://"
	char *path = strchr(url + strlen("http://"), '/');

	//At the very least the url has to end with '/', ending with just a domain is invalid
	if (path == NULL)
	{
		return -1;
	}

	//Extract the domain part out of the url
	int domainlength = path - url - strlen("http://");

	if (domainlength == 0)
	{
		return -1;
	}

	char domain[domainlength + 1];
	strlcpy(domain, url + strlen("http://"), domainlength + 1);

	connection = GetConnection(domain);
	if (connection < 0)
	{
		return -1;
	}

	//Form a nice request header to send to the webserver
	char header[strlen(path) + strlen(domain) + strlen(url) + 100];
	sprintf(header, "GET %s HTTP/1.1\r\nHost: %s\r\nReferer: %s\r\nUser-Agent: USBLoaderGX\r\nConnection: close\r\n\r\n", path, domain, url);

	s32 filesize = network_request(connection, header, filename);

	return filesize;
}
/****************************************************************************
 * Download a file from a given url with a Progressbar
 ****************************************************************************/
int DownloadFileToMem(const char *url, u8 **inbuffer, u32 *size)
{
	if(strncasecmp(url, "http://", strlen("http://")) != 0)
	{
		ShowError(tr("Not a valid URL"));
		return -1;
	}
	char *path = strchr(url + strlen("http://"), '/');

	if(!path)
	{
		ShowError(tr("Not a valid URL path"));
		return -2;
	}

	int domainlength = path - url - strlen("http://");

	if(domainlength == 0)
	{
		ShowError(tr("Not a valid domain"));
		return -3;
	}

	char domain[domainlength + 1];
	strncpy(domain, url + strlen("http://"), domainlength);
	domain[domainlength] = '\0';

	int connection = GetConnection(domain);

	if(connection < 0)
	{
		ShowError(tr("Could not connect to the server."));
		return -4;
	}

	char header[1024];
	char * ptr = header;
	ptr += sprintf(ptr, "GET %s HTTP/1.1\r\n", path);
	ptr += sprintf(ptr, "Host: %s\r\n", domain);
	ptr += sprintf(ptr, "Referer: %s\r\n", domain);
	ptr += sprintf(ptr, "User-Agent: USB Loader GX\r\n");
	ptr += sprintf(ptr, "Pragma: no-cache\r\n");
	ptr += sprintf(ptr, "Cache-Control: no-cache\r\n");
	ptr += sprintf(ptr, "Connection: close\r\n\r\n");

	char filename[255];
	memset(filename, 0, sizeof(filename));

	int filesize = network_request(connection, header, filename);

	if(filesize <= 0)
	{
		net_close(connection);
		ShowError(tr("Filesize is 0 Byte."));
		return -5;
	}

	int blocksize = 4*1024;

	u8 * buffer = (u8 *) malloc(filesize);
	if(!buffer)
	{
		net_close(connection);
		ShowError(tr("Not enough memory."));
		return -6;
	}

	ProgressCancelEnable(true);
	StartProgress(tr("Downloading file..."), 0, filename, true, true);

	int done = 0;

	while(done < filesize)
	{
		if(ProgressCanceled())
		{
			done = PROGRESS_CANCELED;
			break;
		}

		ShowProgress(done, filesize);

		if(blocksize > filesize - done)
			blocksize = filesize - done;


		s32 read = network_read(connection, buffer+done, blocksize);

		if(read < 0)
		{
			done = -8;
			ShowError(tr("Transfer failed"));
			break;
		}
		else if(!read)
			break;

		done += read;
	}

	ProgressStop();
	ProgressCancelEnable(false);
	net_close(connection);

	if(done < 0)
	{
		free(buffer);
		return done;
	}

	*inbuffer = buffer;
	*size = filesize;

	return done;
}
/****************************************************************************
 * Download a file from a given url to a given path with a Progressbar
 ****************************************************************************/
int DownloadFileToPath(const char *orig_url, const char *dest, bool UseFilename)
{
	if(!orig_url || !dest)
	{
		ShowError(tr("No URL or Path specified."));
		return -2;
	}

	bool addhttp = false;

	if(strncasecmp(orig_url, "http://", strlen("http://")) != 0)
	{
		addhttp = true;
	}

	char url[strlen(orig_url) + (addhttp ? strlen("http://") : 0) + 1];

	if(addhttp)
		snprintf(url, sizeof(url), "http://%s", orig_url);
	else
		strcpy(url, orig_url);

	char *path = strchr(url + strlen("http://"), '/');

	if(!path)
	{
		ShowError(tr("Not a valid URL path"));
		return -2;
	}

	int domainlength = path - url - strlen("http://");

	if(domainlength == 0)
	{
		ShowError(tr("Not a valid domain"));
		return -3;
	}

	char domain[domainlength + 1];
	strncpy(domain, url + strlen("http://"), domainlength);
	domain[domainlength] = '\0';

	int connection = GetConnection(domain);

	if(connection < 0)
	{
		ShowError(tr("Could not connect to the server."));
		return -4;
	}

	char header[1024];
	char * ptr = header;
	ptr += sprintf(ptr, "GET %s HTTP/1.1\r\n", path);
	ptr += sprintf(ptr, "Host: %s\r\n", domain);
	ptr += sprintf(ptr, "Referer: %s\r\n", domain);
	ptr += sprintf(ptr, "User-Agent: WiiXplorer\r\n");
	ptr += sprintf(ptr, "Pragma: no-cache\r\n");
	ptr += sprintf(ptr, "Cache-Control: no-cache\r\n");
	ptr += sprintf(ptr, "Connection: close\r\n\r\n");

	char filename[255];
	memset(filename, 0, sizeof(filename));

	int filesize = network_request(connection, header, filename);

	if(filesize <= 0)
	{
		net_close(connection);
		ShowError(tr("Filesize is %i Byte."), filesize);
		return -5;
	}

	int blocksize = 4*1024;

	u8 *buffer = (u8 *) malloc(blocksize);
	if(!buffer)
	{
		net_close(connection);
		ShowError(tr("Not enough memory."));
		return -6;
	}

	if(UseFilename)
	{
		if(dest[strlen(dest)-1] != '/')
			strcat((char *) dest, "/");

		CreateSubfolder(dest);

		strcat((char *) dest, filename);
	}

	if(!UseFilename && strcmp(filename, "") == 0)
	{
		const char * ptr = strrchr(dest, '/');
		if(ptr) ptr++;
		else ptr = dest;

		snprintf(filename, sizeof(filename), "%s", ptr);
	}

	FILE *file = fopen(dest, "wb");
	if(!file)
	{
		net_close(connection);
		free(buffer);
		ShowError(tr("Cannot write to destination."));
		return -7;
	}

	ProgressCancelEnable(true);
	StartProgress(tr("Downloading file..."), 0, filename, true, true);

	int done = 0;

	while(done < filesize)
	{
		if(ProgressCanceled())
		{
			done = PROGRESS_CANCELED;
			break;
		}

		ShowProgress(done, filesize);

		if(blocksize > filesize - done)
			blocksize = filesize - done;

		s32 read = network_read(connection, buffer, blocksize);

		if(read < 0)
		{
			done = -8;
			ShowError(tr("Transfer failed"));
			break;
		}
		else if(!read)
			break;

		fwrite(buffer, 1, read, file);

		done += read;
	}

	free(buffer);
	ProgressStop();
	net_close(connection);
	fclose(file);
	ProgressStop();
	ProgressCancelEnable(false);

	return done;
}