From 33fc0cc8036f20381541e4ee53d0520454959070 Mon Sep 17 00:00:00 2001 From: Andre Leiradella Date: Mon, 19 Oct 2015 23:20:10 -0200 Subject: [PATCH] moved net_http_get back to cheevos.c --- cheevos.c | 89 +++++++++++++++++++++++++- libretro-common/include/net/net_http.h | 3 - libretro-common/net/net_http.c | 83 ------------------------ 3 files changed, 86 insertions(+), 89 deletions(-) diff --git a/cheevos.c b/cheevos.c index 60433ecf52..2b2c611902 100644 --- a/cheevos.c +++ b/cheevos.c @@ -1130,6 +1130,89 @@ void cheevos_unload( void ) Load achievements from retroachievements.org. *****************************************************************************/ +static int cheevos_http_get(const char **result, size_t *size, const char *url, retro_time_t *timeout) +{ + struct http_connection_t* conn = NULL; + struct http_t* http = NULL; + int ret = -1; + retro_time_t t0; + uint8_t* data; + size_t length; + char* res; + + *result = NULL; + t0 = retro_get_time_usec(); + conn = net_http_connection_new(url); + + /* Error creating the connection descriptor. */ + if (!conn) + goto error; + + /* Don't bother with timeouts here, it's just a string scan. */ + while (!net_http_connection_iterate(conn)) {} + + /* Error finishing the connection descriptor. */ + if (!net_http_connection_done(conn)) + goto error; + + http = net_http_new(conn); + + /* Error connecting to the endpoint. */ + if (!http) + goto error; + + while (!net_http_update(http, NULL, NULL)) + { + /* Timeout error. */ + if (timeout && (retro_get_time_usec() - t0) > *timeout) + goto error; + } + + data = net_http_data(http, &length, false); + + if (data) + { + res = (char*)malloc(length + 1); + + /* Allocation error. */ + if ( !res ) + goto error; + + memcpy((void*)res, (void*)data, length); + res[length] = 0; + *result = res; + } + else + { + length = 0; + *result = NULL; + } + + if (size) + *size = length; + + ret = 0; + +error: + if ( http ) + net_http_delete( http ); + + if ( conn ) + net_http_connection_free( conn ); + + if (timeout) + { + t0 = retro_get_time_usec() - t0; + + if (t0 < *timeout) + *timeout -= t0; + else + *timeout = 0; + } + + return ret; +} + typedef struct { unsigned key_hash; @@ -1240,7 +1323,7 @@ static int cheevos_login( retro_time_t* timeout ) request[ sizeof( request ) - 1 ] = 0; - if ( !net_http_get( &json, NULL, request, timeout ) ) + if ( !cheevos_http_get( &json, NULL, request, timeout ) ) { res = cheevos_get_value( json, 0x0e2dbd26U /* Token */, token, sizeof( token ) ); free( (void*)json ); @@ -1278,7 +1361,7 @@ static int cheevos_get_by_game_id( const char** json, unsigned game_id, retro_ti request[ sizeof( request ) - 1 ] = 0; - if ( !net_http_get( json, NULL, request, timeout ) ) + if ( !cheevos_http_get( json, NULL, request, timeout ) ) { RARCH_LOG( "CHEEVOS got achievements for game id %u\n", game_id ); return 0; @@ -1309,7 +1392,7 @@ static unsigned cheevos_get_game_id( unsigned char* hash, retro_time_t* timeout request[ sizeof( request ) - 1 ] = 0; - if ( !net_http_get( &json, NULL, request, timeout ) ) + if ( !cheevos_http_get( &json, NULL, request, timeout ) ) { res = cheevos_get_value( json, 0xb4960eecU /* GameID */, game_id, sizeof( game_id ) ); free( (void*)json ); diff --git a/libretro-common/include/net/net_http.h b/libretro-common/include/net/net_http.h index 44f976f0bd..ca9adddd02 100644 --- a/libretro-common/include/net/net_http.h +++ b/libretro-common/include/net/net_http.h @@ -60,9 +60,6 @@ uint8_t* net_http_data(struct http_t *state, size_t* len, bool accept_error); /* Cleans up all memory. */ void net_http_delete(struct http_t *state); -/* Does a HTTP GET and returns whatever the server sends back. */ -int net_http_get(const char **result, size_t *size, const char *url, retro_time_t *timeout); - #ifdef __cplusplus } #endif diff --git a/libretro-common/net/net_http.c b/libretro-common/net/net_http.c index 05cf49d17c..9f94465525 100644 --- a/libretro-common/net/net_http.c +++ b/libretro-common/net/net_http.c @@ -559,86 +559,3 @@ void net_http_delete(struct http_t *state) free(state->data); free(state); } - -int net_http_get(const char **result, size_t *size, const char *url, retro_time_t *timeout) -{ - struct http_connection_t* conn = NULL; - struct http_t* http = NULL; - int ret = -1; - retro_time_t t0; - uint8_t* data; - size_t length; - char* res; - - *result = NULL; - t0 = retro_get_time_usec(); - conn = net_http_connection_new(url); - - /* Error creating the connection descriptor. */ - if (!conn) - goto error; - - /* Don't bother with timeouts here, it's just a string scan. */ - while (!net_http_connection_iterate(conn)) {} - - /* Error finishing the connection descriptor. */ - if (!net_http_connection_done(conn)) - goto error; - - http = net_http_new(conn); - - /* Error connecting to the endpoint. */ - if (!http) - goto error; - - while (!net_http_update(http, NULL, NULL)) - { - /* Timeout error. */ - if (timeout && (retro_get_time_usec() - t0) > *timeout) - goto error; - } - - data = net_http_data(http, &length, false); - - if (data) - { - res = (char*)malloc(length + 1); - - /* Allocation error. */ - if ( !res ) - goto error; - - memcpy((void*)res, (void*)data, length); - res[length] = 0; - *result = res; - } - else - { - length = 0; - *result = NULL; - } - - if (size) - *size = length; - - ret = 0; - -error: - if ( http ) - net_http_delete( http ); - - if ( conn ) - net_http_connection_free( conn ); - - if (timeout) - { - t0 = retro_get_time_usec() - t0; - - if (t0 < *timeout) - *timeout -= t0; - else - *timeout = 0; - } - - return ret; -}