1 /* $Id: update.c,v 1.7 2005/03/26 23:04:14 titer Exp $
3 This file is part of the HandBrake source code.
4 Homepage: <http://handbrake.fr/>.
5 It may be used under the terms of the GNU General Public License. */
9 static void UpdateFunc( void * );
18 hb_thread_t * hb_update_init( int * build, char * version )
20 hb_update_t * data = calloc( sizeof( hb_update_t ), 1 );
22 data->version = version;
24 return hb_thread_init( "update", UpdateFunc, data,
29 static void UpdateFunc( void * _data )
32 hb_update_t * data = (hb_update_t *) _data;
34 /* New code to handle the hb_query stuff */
38 hb_query = "GET /appcast_unstable.xml HTTP/1.0\r\nHost: handbrake.fr\r\n\r\n";
39 hb_log("Using http://handbrake.fr/appcast_unstable.xml");
43 hb_query = "GET /appcast.xml HTTP/1.0\r\nHost: handbrake.fr\r\n\r\n";
44 hb_log("Using http://handbrake.fr/appcast.xml");
47 // Grab the data from the web server
57 if( !( net = hb_net_open( "handbrake.fr", 80 ) ) )
62 if( hb_net_send( net, hb_query ) < 0 )
64 hb_log("Error: Unable to connect to server");
70 memset( buf, 0, 4096 );
73 ret = hb_net_recv( net, &buf[size], sizeof( buf ) - size );
83 end = &buf[sizeof( buf )];
85 /* Make sure we got it */
87 if( size < 15 || strncmp( cur, "200 OK", 6 ) )
89 /* Something went wrong */
90 hb_log("Error: We did not get a 200 OK from the server. \n");
95 /* Find the end of the headers and the beginning of the content */
96 for( ; &cur[3] < end; cur++ )
98 if( cur[0] == '\r' && cur[1] == '\n' &&
99 cur[2] == '\r' && cur[3] == '\n' )
108 hb_log("Error: Found the end of the buffer before the end of the HTTP header information! \n");
112 // Version Checking Here
116 * Scan though each character of the buffer until we find that the first 4 characters of "cur" are "<cli"
118 for(i=0 ; &cur[3] < end; i++, cur++ )
120 if( cur[0] == 'c' && cur[1] == 'l' && cur[2] == 'i' && cur[3] == '>' )
126 // If the CLI tag has not been found in the first 768 characters, or the end is reached, something bad happened.
127 if (( i > 768) || ( cur >= end ))
129 hb_log("Error: Did not find the <cli> tag in the expected maximum amount of characters into the file. \n");
140 * Ok, The above code didn't position cur, it only found <cli so we need to shift cur along 3 places.
141 * After which, the next 10 characters are the build number
147 hb_log("Error: Unexpected end of buffer! Could not find the build information. \n");
151 stable = strtol( cur, &cur, 10 );
155 hb_log("Error: Unexpected end of buffer! \n");
160 * The Version number is 2 places after the build, so shift cur, 2 places.
161 * Get all the characters in cur until the point where " is found.
167 hb_log("Error: Unexpected end of buffer! Could not get version number. \n");
170 memset( stable_str, 0, sizeof( stable_str ) );
171 for( i = 0; i < sizeof( stable_str ) - 1 && cur < end && *cur != '"'; i++, cur++ )
173 stable_str[i] = *cur;
175 // If the version number is longer than 7 characters, or the end is reached, something has gone wrong.
176 if (( i > 7) || ( cur >= end ))
178 hb_log("Error: Version number too long, or end of buffer reached. \n");
188 hb_log( "latest stable: %s, build %d", stable_str, stable );
190 // Return the build information
191 if( stable > HB_BUILD )
193 memcpy( data->version, stable_str, sizeof( stable_str ) );
194 *(data->build) = stable;