From 5d449d502479886bb5afeefa71b07982a9848061 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Thu, 28 Apr 2016 11:58:44 +0200 Subject: [PATCH] (libretro-common) net_http - cleanups --- libretro-common/net/net_http.c | 65 +++++++++++++++++----------------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/libretro-common/net/net_http.c b/libretro-common/net/net_http.c index 2e86f7c29b..a4a1b78f73 100644 --- a/libretro-common/net/net_http.c +++ b/libretro-common/net/net_http.c @@ -73,6 +73,7 @@ struct http_connection_t static int net_http_new_socket(const char *domain, int port) { int fd; + int ret; #ifndef _WIN32 #ifndef VITA 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); #endif #endif - if (connect(fd, addr->ai_addr, addr->ai_addrlen) != 0) - { - freeaddrinfo_retro(addr); - socket_close(fd); - return -1; - } + ret = connect(fd, addr->ai_addr, addr->ai_addrlen); freeaddrinfo_retro(addr); + if (ret != 0) + goto error; + if (!socket_nonblock(fd)) - { - socket_close(fd); - return -1; - } + goto error; return fd; + +error: + socket_close(fd); + return -1; } 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) { + unsigned i; + int outpos = 0; int outlen = 0; - - int i; - for (i=0;url[i]!='\0';i++) + char *ret = NULL; + + for (i = 0; url[i] != '\0'; i++) { outlen++; - if (url[i]==' ') outlen+=2; + if (url[i] == ' ') + outlen += 2; } - char* ret = (char*)malloc(outlen+1); - if (!ret) return NULL; + ret = (char*)malloc(outlen + 1); + 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++] = '2'; ret[outpos++] = '0'; } - else ret[outpos++] = url[i]; + else + ret[outpos++] = url[i]; } ret[outpos] = '\0'; @@ -214,7 +218,7 @@ struct http_connection_t *net_http_connection_new(const char *url) if (!conn) return NULL; - conn->urlcopy = urlencode(url); + conn->urlcopy = urlencode(url); if (!conn->urlcopy) goto error; @@ -223,10 +227,8 @@ struct http_connection_t *net_http_connection_new(const char *url) goto error; conn->scan = conn->urlcopy + strlen("http://"); - domain = &conn->domain; - - *domain = conn->scan; + *domain = conn->scan; return conn; @@ -242,10 +244,10 @@ bool net_http_connection_iterate(struct http_connection_t *conn) { if (!conn) return false; + while (*conn->scan != '/' && *conn->scan != ':' && *conn->scan != '\0') - { conn->scan++; - } + return true; } @@ -256,17 +258,16 @@ bool net_http_connection_done(struct http_connection_t *conn) if (!conn) return false; - location = &conn->location; + location = &conn->location; if (*conn->scan == '\0') return false; - *conn->scan = '\0'; + *conn->scan = '\0'; conn->port = 80; if (*conn->scan == ':') { - if (!isdigit((int)conn->scan[1])) 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) { - bool error; - int fd = -1; - struct http_t *state = NULL; + bool error = false; + int fd = -1; + struct http_t *state = NULL; if (!conn) goto error;