mirror of
https://github.com/dborth/fceugx.git
synced 2025-01-10 07:39:39 +01:00
tidy up memory usage
This commit is contained in:
parent
5f58febc26
commit
884acb27af
@ -109,57 +109,39 @@ static s32 tcp_connect(char *host, const u16 port)
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char * tcp_readln(const s32 s, const u16 max_length, const u64 start_time,
|
static int tcp_readln(const s32 s, char *buf, const u16 max_length)
|
||||||
const u16 timeout)
|
|
||||||
{
|
{
|
||||||
char *buf;
|
s32 res = -1;
|
||||||
u16 c;
|
s32 ret;
|
||||||
s32 res;
|
u64 start_time = gettime();
|
||||||
char *ret;
|
u16 c = 0;
|
||||||
|
|
||||||
buf = (char *) malloc(max_length);
|
while (c < max_length)
|
||||||
|
|
||||||
c = 0;
|
|
||||||
ret = NULL;
|
|
||||||
while (true)
|
|
||||||
{
|
{
|
||||||
if (ticks_to_millisecs(diff_ticks(start_time, gettime())) > timeout)
|
if (ticks_to_millisecs(diff_ticks(start_time, gettime())) > HTTP_TIMEOUT)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
res = net_read(s, &buf[c], 1);
|
ret = net_read(s, &buf[c], 1);
|
||||||
|
|
||||||
if ((res == 0) || (res == -EAGAIN))
|
if (ret == 0 || ret == -EAGAIN)
|
||||||
{
|
{
|
||||||
usleep(20 * 1000);
|
usleep(20 * 1000);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (res < 0)
|
if (ret < 0)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if ((c > 0) && (buf[c - 1] == '\r') && (buf[c] == '\n'))
|
if (c > 0 && buf[c - 1] == '\r' && buf[c] == '\n')
|
||||||
{
|
{
|
||||||
if (c == 1)
|
res = 0;
|
||||||
{
|
buf[c-1] = 0;
|
||||||
ret = strdup("");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = strndup(buf, c - 1);
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
c++;
|
c++;
|
||||||
|
usleep(100);
|
||||||
if (c == max_length)
|
|
||||||
break;
|
|
||||||
|
|
||||||
usleep(300);
|
|
||||||
}
|
}
|
||||||
|
return res;
|
||||||
free(buf);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int tcp_read(const s32 s, u8 *buffer, const u32 length)
|
static int tcp_read(const s32 s, u8 *buffer, const u32 length)
|
||||||
@ -259,7 +241,8 @@ static int tcp_write(const s32 s, const u8 *buffer, const u32 length)
|
|||||||
|
|
||||||
return left == 0;
|
return left == 0;
|
||||||
}
|
}
|
||||||
static bool http_split_url(char **host, char **path, const char *url)
|
|
||||||
|
static bool http_split_url(char *host, char *path, const char *url)
|
||||||
{
|
{
|
||||||
const char *p;
|
const char *p;
|
||||||
char *c;
|
char *c;
|
||||||
@ -273,9 +256,8 @@ static bool http_split_url(char **host, char **path, const char *url)
|
|||||||
if (c == NULL || c[0] == 0)
|
if (c == NULL || c[0] == 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
*host = strndup(p, c - p);
|
snprintf(host, c-p, "%s", p);
|
||||||
*path = strdup(c);
|
strcpy(path, c);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -285,12 +267,12 @@ static bool http_split_url(char **host, char **path, const char *url)
|
|||||||
* http_request
|
* http_request
|
||||||
* Retrieves the specified URL, and stores it in the specified file or buffer
|
* Retrieves the specified URL, and stores it in the specified file or buffer
|
||||||
***************************************************************************/
|
***************************************************************************/
|
||||||
int http_request(const char *url, FILE * hfile, u8 * buffer, u32 maxsize, bool silent)
|
int http_request(const char *url, FILE *hfile, u8 *buffer, u32 maxsize, bool silent)
|
||||||
{
|
{
|
||||||
int res = 0;
|
int res = 0;
|
||||||
char *http_host;
|
char http_host[1024];
|
||||||
|
char http_path[1024];
|
||||||
u16 http_port;
|
u16 http_port;
|
||||||
char *http_path;
|
|
||||||
|
|
||||||
http_res result;
|
http_res result;
|
||||||
u32 http_status;
|
u32 http_status;
|
||||||
@ -304,7 +286,7 @@ int http_request(const char *url, FILE * hfile, u8 * buffer, u32 maxsize, bool s
|
|||||||
if (url == NULL || (hfile == NULL && buffer == NULL))
|
if (url == NULL || (hfile == NULL && buffer == NULL))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (!http_split_url(&http_host, &http_path, url))
|
if (!http_split_url(http_host, http_path, url))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
http_port = 80;
|
http_port = 80;
|
||||||
@ -318,7 +300,7 @@ int http_request(const char *url, FILE * hfile, u8 * buffer, u32 maxsize, bool s
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *request = (char *) malloc(1024);
|
char request[1024];
|
||||||
char *r = request;
|
char *r = request;
|
||||||
|
|
||||||
r += sprintf(r, "GET %s HTTP/1.1\r\n", http_path);
|
r += sprintf(r, "GET %s HTTP/1.1\r\n", http_path);
|
||||||
@ -327,13 +309,11 @@ int http_request(const char *url, FILE * hfile, u8 * buffer, u32 maxsize, bool s
|
|||||||
|
|
||||||
res = tcp_write(s, (u8 *) request, strlen(request));
|
res = tcp_write(s, (u8 *) request, strlen(request));
|
||||||
|
|
||||||
free(request);
|
char line[256];
|
||||||
|
|
||||||
for (linecount = 0; linecount < 32; linecount++)
|
for (linecount = 0; linecount < 32; linecount++)
|
||||||
{
|
{
|
||||||
char *line = tcp_readln(s, 0xff, gettime(), (const u16) HTTP_TIMEOUT);
|
if (tcp_readln(s, line, 255) != 0)
|
||||||
|
|
||||||
if (!line)
|
|
||||||
{
|
{
|
||||||
http_status = 404;
|
http_status = 404;
|
||||||
result = HTTPR_ERR_REQUEST;
|
result = HTTPR_ERR_REQUEST;
|
||||||
@ -341,18 +321,10 @@ int http_request(const char *url, FILE * hfile, u8 * buffer, u32 maxsize, bool s
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (strlen(line) < 1)
|
if (strlen(line) < 1)
|
||||||
{
|
|
||||||
free(line);
|
|
||||||
line = NULL;
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
sscanf(line, "HTTP/1.%*u %u", &http_status);
|
sscanf(line, "HTTP/1.%*u %u", &http_status);
|
||||||
sscanf(line, "Content-Length: %u", &content_length);
|
sscanf(line, "Content-Length: %u", &content_length);
|
||||||
|
|
||||||
free(line);
|
|
||||||
line = NULL;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (http_status != 200)
|
if (http_status != 200)
|
||||||
@ -387,7 +359,7 @@ int http_request(const char *url, FILE * hfile, u8 * buffer, u32 maxsize, bool s
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// read into file
|
// read into file
|
||||||
u32 bufSize = (1026 * 256);
|
u32 bufSize = (1024 * 32);
|
||||||
u32 bytesLeft = content_length;
|
u32 bytesLeft = content_length;
|
||||||
u32 readSize;
|
u32 readSize;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user