mirror of
https://github.com/dborth/fceugx.git
synced 2024-11-01 06:55:05 +01:00
tidy up http code
This commit is contained in:
parent
5aa1e79d75
commit
db6ab63ed1
@ -171,7 +171,7 @@ static char * tcp_readln(const s32 s, const u16 max_length, const u64 start_time
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool tcp_read(const s32 s, u8 **buffer, const u32 length)
|
static int tcp_read(const s32 s, u8 **buffer, const u32 length)
|
||||||
{
|
{
|
||||||
u8 *p;
|
u8 *p;
|
||||||
u32 step, left, block, received;
|
u32 step, left, block, received;
|
||||||
@ -224,7 +224,7 @@ static bool tcp_read(const s32 s, u8 **buffer, const u32 length)
|
|||||||
return left == 0;
|
return left == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool tcp_write(const s32 s, const u8 *buffer, const u32 length)
|
static int tcp_write(const s32 s, const u8 *buffer, const u32 length)
|
||||||
{
|
{
|
||||||
const u8 *p;
|
const u8 *p;
|
||||||
u32 step, left, block, sent;
|
u32 step, left, block, sent;
|
||||||
@ -304,6 +304,7 @@ static bool http_split_url(char **host, char **path, const char *url)
|
|||||||
bool http_request(const char *url, FILE * hfile, u8 * buffer,
|
bool http_request(const char *url, FILE * hfile, u8 * buffer,
|
||||||
const u32 max_size)
|
const u32 max_size)
|
||||||
{
|
{
|
||||||
|
int res = 0;
|
||||||
char *http_host;
|
char *http_host;
|
||||||
u16 http_port;
|
u16 http_port;
|
||||||
char *http_path;
|
char *http_path;
|
||||||
@ -336,10 +337,9 @@ bool http_request(const char *url, FILE * hfile, u8 * buffer,
|
|||||||
r += sprintf(r, "Host: %s\r\n", http_host);
|
r += sprintf(r, "Host: %s\r\n", http_host);
|
||||||
r += sprintf(r, "Cache-Control: no-cache\r\n\r\n");
|
r += sprintf(r, "Cache-Control: no-cache\r\n\r\n");
|
||||||
|
|
||||||
bool b = tcp_write(s, (u8 *) request, strlen(request));
|
res = tcp_write(s, (u8 *) request, strlen(request));
|
||||||
|
|
||||||
free(request);
|
free(request);
|
||||||
linecount = 0;
|
|
||||||
|
|
||||||
for (linecount = 0; linecount < 32; linecount++)
|
for (linecount = 0; linecount < 32; linecount++)
|
||||||
{
|
{
|
||||||
@ -382,11 +382,9 @@ bool http_request(const char *url, FILE * hfile, u8 * buffer,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int res = 1;
|
|
||||||
|
|
||||||
if (buffer != NULL)
|
if (buffer != NULL)
|
||||||
{
|
{
|
||||||
b = tcp_read(s, &buffer, content_length);
|
res = tcp_read(s, &buffer, content_length);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -395,8 +393,10 @@ bool http_request(const char *url, FILE * hfile, u8 * buffer,
|
|||||||
u32 bytesLeft = content_length;
|
u32 bytesLeft = content_length;
|
||||||
u32 readSize;
|
u32 readSize;
|
||||||
|
|
||||||
u8 * fbuffer = (u8 *) malloc(bufSize);
|
|
||||||
ShowProgress("Downloading...", 0, content_length);
|
ShowProgress("Downloading...", 0, content_length);
|
||||||
|
u8 * fbuffer = (u8 *) malloc(bufSize);
|
||||||
|
if(fbuffer)
|
||||||
|
{
|
||||||
while (bytesLeft > 0)
|
while (bytesLeft > 0)
|
||||||
{
|
{
|
||||||
if (bytesLeft < bufSize)
|
if (bytesLeft < bufSize)
|
||||||
@ -404,8 +404,8 @@ bool http_request(const char *url, FILE * hfile, u8 * buffer,
|
|||||||
else
|
else
|
||||||
readSize = bufSize;
|
readSize = bufSize;
|
||||||
|
|
||||||
b = tcp_read(s, &fbuffer, readSize);
|
res = tcp_read(s, &fbuffer, readSize);
|
||||||
if (!b)
|
if (!res)
|
||||||
break;
|
break;
|
||||||
res = fwrite(fbuffer, 1, readSize, hfile);
|
res = fwrite(fbuffer, 1, readSize, hfile);
|
||||||
if (!res)
|
if (!res)
|
||||||
@ -415,17 +415,20 @@ bool http_request(const char *url, FILE * hfile, u8 * buffer,
|
|||||||
ShowProgress("Downloading...", (content_length - bytesLeft),
|
ShowProgress("Downloading...", (content_length - bytesLeft),
|
||||||
content_length);
|
content_length);
|
||||||
}
|
}
|
||||||
|
free(fbuffer);
|
||||||
|
}
|
||||||
CancelAction();
|
CancelAction();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!b || !res)
|
net_close(s);
|
||||||
|
|
||||||
|
if (!res)
|
||||||
{
|
{
|
||||||
result = HTTPR_ERR_RECEIVE;
|
result = HTTPR_ERR_RECEIVE;
|
||||||
net_close(s);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
result = HTTPR_OK;
|
result = HTTPR_OK;
|
||||||
net_close(s);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user