X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=libhb%2Fupdate.c;h=9f725237bcfb55527396b4a5eee6186a0eec7407;hb=033e32de9c380f54c7d1362a3979da205ebc3a29;hp=767807038bdfc0788cb0aa4b8dccc451cf771418;hpb=52d45181a7d1df6016d32c1842622660224538e0;p=handbrake-jp%2Fhandbrake-jp-git.git diff --git a/libhb/update.c b/libhb/update.c index 76780703..9f725237 100644 --- a/libhb/update.c +++ b/libhb/update.c @@ -1,13 +1,12 @@ /* $Id: update.c,v 1.7 2005/03/26 23:04:14 titer Exp $ This file is part of the HandBrake source code. - Homepage: . + Homepage: . It may be used under the terms of the GNU General Public License. */ #include "hb.h" -#define HB_URL "handbrake.m0k.org" -#define HB_QUERY "GET /LATEST HTTP/1.0\r\nHost: " HB_URL "\r\n\r\n" +static void UpdateFunc( void * ); typedef struct { @@ -16,8 +15,6 @@ typedef struct } hb_update_t; -static void UpdateFunc( void * ); - hb_thread_t * hb_update_init( int * build, char * version ) { hb_update_t * data = calloc( sizeof( hb_update_t ), 1 ); @@ -28,32 +25,68 @@ hb_thread_t * hb_update_init( int * build, char * version ) HB_NORMAL_PRIORITY ); } + static void UpdateFunc( void * _data ) { + hb_update_t * data = (hb_update_t *) _data; - hb_net_t * net; + char* const url = HB_PROJECT_URL_APPCAST; + char* const urlz = url + strlen( HB_PROJECT_URL_APPCAST ); /* marks null-term */ + char url_host[64]; + char url_path[128]; + char query[256]; + + hb_net_t * net; int ret; - char buf[1024]; - char * cur, * end, * p; + char buf[4096]; + char * cur, * end; int size; - int stable, unstable; - char stable_str[16], unstable_str[16]; + int i_vers; + char s_vers[32]; /* must be no larger than hb_handle_s.version */ int i; - if( !( net = hb_net_open( HB_URL, 80 ) ) ) + /* Setup hb_query and hb_query_two with the correct appcast file */ + hb_log( "Using %s", url ); + + /* extract host part */ + cur = strstr( HB_PROJECT_URL_APPCAST, "//" ); + if( !cur || cur+2 > urlz ) + goto error; + cur += 2; + + end = strstr( cur, "/" ); + if( !end || end > urlz ) + goto error; + + memset( url_host, 0, sizeof(url_host) ); + strncpy( url_host, cur, (end-cur) ); + + /* extract path part */ + memset( url_path, 0, sizeof(url_path) ); + strncpy( url_path, end, (urlz-end) ); + + if( !strlen( url_path )) + goto error; + + memset( query, 0, sizeof(query) ); + snprintf( query, sizeof(query), "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n", url_path, url_host ); + + /* Grab the data from the web server */ + if( !( net = hb_net_open( url_host, 80 ) ) ) { goto error; } - if( hb_net_send( net, HB_QUERY ) < 0 ) + if( hb_net_send( net, query ) < 0 ) { + hb_log("Error: Unable to connect to server"); hb_net_close( &net ); goto error; } size = 0; - memset( buf, 0, 1024 ); + memset( buf, 0, 4096 ); for( ;; ) { ret = hb_net_recv( net, &buf[size], sizeof( buf ) - size ); @@ -67,12 +100,12 @@ static void UpdateFunc( void * _data ) cur = buf; end = &buf[sizeof( buf )]; - + /* Make sure we got it */ cur += 9; if( size < 15 || strncmp( cur, "200 OK", 6 ) ) { - /* Something went wrong */ + hb_log("Error: We did not get a 200 OK from the server. \n"); goto error; } cur += 6; @@ -90,68 +123,96 @@ static void UpdateFunc( void * _data ) if( cur >= end ) { + hb_log("Error: Found the end of the buffer before the end of the HTTP header information! \n"); goto error; } - - stable = strtol( cur, &p, 10 ); - if( cur == p ) + + /* + * Find the tag + * Scan though each character of the buffer until we find that the first 4 characters of "cur" are "' ) + { + cur += 1; + break; + } + + /* If the CLI tag has not been found in the first 768 characters, or the end is reached, something bad happened.*/ + if (( i > 768) || ( cur >= end )) + { + hb_log("Error: Did not find the tag in the expected maximum amount of characters into the file. \n"); + goto error; + } } - cur = p + 1; - memset( stable_str, 0, sizeof( stable_str ) ); - for( i = 0; - i < sizeof( stable_str ) - 1 && cur < end && *cur != '\n'; - i++, cur++ ) + + if( cur >= end ) { - stable_str[i] = *cur; + goto error; } - - hb_log( "latest stable: %s, build %d", stable_str, stable ); - - cur++; + + /* + * Ok, The above code didn't position cur, it only found = end ) { + hb_log("Error: Unexpected end of buffer! Could not find the build information. \n"); goto error; } + + /* Stable HB_PROJECT_BUILD */ + i_vers = strtol( cur, &cur, 10 ); - unstable = strtol( cur, &p, 10 ); - if( cur == p ) + if( cur >= end ) { + hb_log("Error: Unexpected end of buffer! \n"); goto error; } - cur = p + 1; - memset( unstable_str, 0, sizeof( unstable_str ) ); - for( i = 0; - i < sizeof( unstable_str ) - 1 && cur < end && *cur != '\n'; - i++, cur++ ) + + /* + * The Version number is 2 places after the build, so shift cur, 2 places. + * Get all the characters in cur until the point where " is found. + */ + cur += 2; + + if( cur >= end ) { - unstable_str[i] = *cur; + hb_log("Error: Unexpected end of buffer! Could not get version number. \n"); + goto error; } - - hb_log( "latest unstable: %s, build %d", unstable_str, unstable ); - - if( HB_BUILD % 100 ) + memset( s_vers, 0, sizeof( s_vers ) ); + for( i = 0; i < sizeof( s_vers ) - 1 && cur < end && *cur != '"'; i++, cur++ ) { - /* We are runnning an unstable build */ - if( unstable > HB_BUILD ) + s_vers[i] = *cur; + + /* If the CLI tag has not been found in the first 768 characters, or the end is reached, something bad happened.*/ + if (( cur >= end )) { - memcpy( data->version, unstable_str, sizeof( unstable_str ) ); - *(data->build) = unstable; + hb_log("Error: Version number too long, or end of buffer reached. \n"); + goto error; } } - else + + if( cur >= end ) { - /* We are runnning an stable build */ - if( stable > HB_BUILD ) - { - memcpy( data->version, stable_str, sizeof( stable_str ) ); - *(data->build) = stable; - } + goto error; + } + + /* Print the version information */ + hb_log( "latest: %s, build %d", s_vers, i_vers ); + + /* Return the build information */ + if( i_vers > HB_PROJECT_BUILD ) + { + memcpy( data->version, s_vers, sizeof(s_vers) ); + *(data->build) = i_vers; } error: free( data ); return; } -