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 char * hb_query, * hb_query_two;
41 char stable_str[16], unstable_str[16];
44 /* Setup hb_query and hb_query_two with the correct appcast file */
45 if( HB_PROJECT_BUILD % 100 )
47 hb_log("Using http://handbrake.fr/appcast_unstable.xml (primary)");
48 hb_log("Using http://handbrake.fr/appcast.xml (secondary)");
52 hb_log("Using http://handbrake.fr/appcast.xml (primary)");
53 hb_log("Using http://handbrake.fr/appcast_unstable.xml (secondary)");
56 hb_query = "GET /appcast.xml HTTP/1.0\r\nHost: handbrake.fr\r\n\r\n";
57 hb_query_two = "GET /appcast_unstable.xml HTTP/1.0\r\nHost: handbrake.fr\r\n\r\n";
59 /* Grab the data from the web server */
60 if( !( net = hb_net_open( "handbrake.fr", 80 ) ) )
65 if( hb_net_send( net, hb_query ) < 0 )
67 hb_log("Error: Unable to connect to server");
73 memset( buf, 0, 4096 );
76 ret = hb_net_recv( net, &buf[size], sizeof( buf ) - size );
86 end = &buf[sizeof( buf )];
88 /* Make sure we got it */
90 if( size < 15 || strncmp( cur, "200 OK", 6 ) )
92 hb_log("Error: We did not get a 200 OK from the server. \n");
97 /* Find the end of the headers and the beginning of the content */
98 for( ; &cur[3] < end; cur++ )
100 if( cur[0] == '\r' && cur[1] == '\n' &&
101 cur[2] == '\r' && cur[3] == '\n' )
110 hb_log("Error: Found the end of the buffer before the end of the HTTP header information! \n");
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 HB_PROJECT_BUILD */
152 stable = strtol( cur, &cur, 10 );
156 hb_log("Error: Unexpected end of buffer! \n");
161 * The Version number is 2 places after the build, so shift cur, 2 places.
162 * Get all the characters in cur until the point where " is found.
168 hb_log("Error: Unexpected end of buffer! Could not get version number. \n");
171 memset( stable_str, 0, sizeof( stable_str ) );
172 for( i = 0; i < sizeof( stable_str ) - 1 && cur < end && *cur != '"'; i++, cur++ )
174 stable_str[i] = *cur;
176 /* If the CLI tag has not been found in the first 768 characters, or the end is reached, something bad happened.*/
177 if (( i > 7) || ( cur >= end ))
179 hb_log("Error: Version number too long, or end of buffer reached. \n");
189 /* HANDLE THE SECOND APPCAST FILE NOW */
190 /* Grab the data from the web server */
191 if( !( net = hb_net_open( "handbrake.fr", 80 ) ) )
196 if( hb_net_send( net, hb_query_two ) < 0 )
198 hb_log("Error: Unable to connect to server");
199 hb_net_close( &net );
204 memset( buf, 0, 4096 );
207 ret = hb_net_recv( net, &buf[size], sizeof( buf ) - size );
210 hb_net_close( &net );
217 end = &buf[sizeof( buf )];
219 /* Make sure we got it */
221 if( size < 15 || strncmp( cur, "200 OK", 6 ) )
223 /* Something went wrong */
224 hb_log("Error: We did not get a 200 OK from the server. \n");
229 /* Find the end of the headers and the beginning of the content */
230 for( ; &cur[3] < end; cur++ )
232 if( cur[0] == '\r' && cur[1] == '\n' &&
233 cur[2] == '\r' && cur[3] == '\n' )
242 hb_log("Error: Found the end of the buffer before the end of the HTTP header information! \n");
248 * Scan though each character of the buffer until we find that the first 4 characters of "cur" are "<cli"
250 for(i=0 ; &cur[3] < end; i++, cur++ )
252 if( cur[0] == 'c' && cur[1] == 'l' && cur[2] == 'i' && cur[3] == '>' )
258 // If the CLI tag has not been found in the first 768 characters, or the end is reached, something bad happened.
259 if (( i > 768) || ( cur >= end ))
261 hb_log("Error: Did not find the <cli> tag in the expected maximum amount of characters into the file. \n");
272 * Ok, The above code didn't position cur, it only found <cli so we need to shift cur along 3 places.
273 * After which, the next 10 characters are the build number
279 hb_log("Error: Unexpected end of buffer! Could not find the build information. \n");
283 /* UnStable HB_PROJECT_BUILD */
284 unstable = strtol( cur, &cur, 10 );
288 hb_log("Error: Unexpected end of buffer! \n");
293 * The Version number is 2 places after the build, so shift cur, 2 places.
294 * Get all the characters in cur until the point where " is found.
300 hb_log("Error: Unexpected end of buffer! Could not get version number. \n");
303 memset( unstable_str, 0, sizeof( unstable_str ) );
304 for( i = 0; i < sizeof( unstable_str ) - 1 && cur < end && *cur != '"'; i++, cur++ )
306 unstable_str[i] = *cur;
308 // If the version number is longer than 7 characters, or the end is reached, something has gone wrong.
309 if (( i > 7) || ( cur >= end ))
311 hb_log("Error: Version number too long, or end of buffer reached. \n");
321 /* Print the version information */
322 hb_log( "latest stable: %s, build %d", stable_str, stable );
323 hb_log( "latest unstable: %s, build %d", unstable_str, unstable );
325 /* Return the build information */
326 if( HB_PROJECT_BUILD % 100 )
328 /* We are runnning an unstable build */
329 if( unstable > HB_PROJECT_BUILD )
331 memcpy( data->version, unstable_str, sizeof( unstable_str ) );
332 *(data->build) = unstable;
337 /* We are runnning an stable build */
338 if( stable > HB_PROJECT_BUILD )
340 memcpy( data->version, stable_str, sizeof( stable_str ) );
341 *(data->build) = stable;