(libretro-common) net_http - cleanups

This commit is contained in:
twinaphex 2016-04-28 11:58:44 +02:00
parent ba415b6d3a
commit 5d449d5024

View File

@ -73,6 +73,7 @@ struct http_connection_t
static int net_http_new_socket(const char *domain, int port) static int net_http_new_socket(const char *domain, int port)
{ {
int fd; int fd;
int ret;
#ifndef _WIN32 #ifndef _WIN32
#ifndef VITA #ifndef VITA
struct timeval timeout; struct timeval timeout;
@ -106,22 +107,21 @@ static int net_http_new_socket(const char *domain, int port)
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout, sizeof timeout); setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout, sizeof timeout);
#endif #endif
#endif #endif
if (connect(fd, addr->ai_addr, addr->ai_addrlen) != 0) ret = connect(fd, addr->ai_addr, addr->ai_addrlen);
{
freeaddrinfo_retro(addr);
socket_close(fd);
return -1;
}
freeaddrinfo_retro(addr); freeaddrinfo_retro(addr);
if (ret != 0)
goto error;
if (!socket_nonblock(fd)) if (!socket_nonblock(fd))
{ goto error;
socket_close(fd);
return -1;
}
return fd; return fd;
error:
socket_close(fd);
return -1;
} }
static void net_http_send(int fd, bool * error, static void net_http_send(int fd, bool * error,
@ -177,28 +177,32 @@ static ssize_t net_http_recv(int fd, bool *error,
static char* urlencode(const char* url) static char* urlencode(const char* url)
{ {
unsigned i;
int outpos = 0;
int outlen = 0; int outlen = 0;
char *ret = NULL;
int i;
for (i=0;url[i]!='\0';i++) for (i = 0; url[i] != '\0'; i++)
{ {
outlen++; outlen++;
if (url[i]==' ') outlen+=2; if (url[i] == ' ')
outlen += 2;
} }
char* ret = (char*)malloc(outlen+1); ret = (char*)malloc(outlen + 1);
if (!ret) return NULL; if (!ret)
return NULL;
int outpos = 0; for (i = 0; url[i]; i++)
for (i=0;url[i];i++)
{ {
if (url[i]==' ') if (url[i] == ' ')
{ {
ret[outpos++] = '%'; ret[outpos++] = '%';
ret[outpos++] = '2'; ret[outpos++] = '2';
ret[outpos++] = '0'; ret[outpos++] = '0';
} }
else ret[outpos++] = url[i]; else
ret[outpos++] = url[i];
} }
ret[outpos] = '\0'; ret[outpos] = '\0';
@ -214,7 +218,7 @@ struct http_connection_t *net_http_connection_new(const char *url)
if (!conn) if (!conn)
return NULL; return NULL;
conn->urlcopy = urlencode(url); conn->urlcopy = urlencode(url);
if (!conn->urlcopy) if (!conn->urlcopy)
goto error; goto error;
@ -223,10 +227,8 @@ struct http_connection_t *net_http_connection_new(const char *url)
goto error; goto error;
conn->scan = conn->urlcopy + strlen("http://"); conn->scan = conn->urlcopy + strlen("http://");
domain = &conn->domain; domain = &conn->domain;
*domain = conn->scan;
*domain = conn->scan;
return conn; return conn;
@ -242,10 +244,10 @@ bool net_http_connection_iterate(struct http_connection_t *conn)
{ {
if (!conn) if (!conn)
return false; return false;
while (*conn->scan != '/' && *conn->scan != ':' && *conn->scan != '\0') while (*conn->scan != '/' && *conn->scan != ':' && *conn->scan != '\0')
{
conn->scan++; conn->scan++;
}
return true; return true;
} }
@ -256,17 +258,16 @@ bool net_http_connection_done(struct http_connection_t *conn)
if (!conn) if (!conn)
return false; return false;
location = &conn->location; location = &conn->location;
if (*conn->scan == '\0') if (*conn->scan == '\0')
return false; return false;
*conn->scan = '\0'; *conn->scan = '\0';
conn->port = 80; conn->port = 80;
if (*conn->scan == ':') if (*conn->scan == ':')
{ {
if (!isdigit((int)conn->scan[1])) if (!isdigit((int)conn->scan[1]))
return false; return false;
@ -299,9 +300,9 @@ const char *net_http_connection_url(struct http_connection_t *conn)
struct http_t *net_http_new(struct http_connection_t *conn) struct http_t *net_http_new(struct http_connection_t *conn)
{ {
bool error; bool error = false;
int fd = -1; int fd = -1;
struct http_t *state = NULL; struct http_t *state = NULL;
if (!conn) if (!conn)
goto error; goto error;