WinGui: Fix another instance of the Caliburn vs Json.net sillyness where objects...
[HandBrake.git] / libhb / update.c
blob4b7322c059e4b07607b29b59f3e0321a3ae09e81
1 /* update.c
3 Copyright (c) 2003-2015 HandBrake Team
4 This file is part of the HandBrake source code
5 Homepage: <http://handbrake.fr/>.
6 It may be used under the terms of the GNU General Public License v2.
7 For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
8 */
10 #include "hb.h"
12 static void UpdateFunc( void * );
14 typedef struct
16 int * build;
17 char * version;
19 } hb_update_t;
21 hb_thread_t * hb_update_init( int * build, char * version )
23 hb_update_t * data = calloc( sizeof( hb_update_t ), 1 );
24 data->build = build;
25 data->version = version;
27 return hb_thread_init( "update", UpdateFunc, data,
28 HB_NORMAL_PRIORITY );
32 static void UpdateFunc( void * _data )
35 hb_update_t * data = (hb_update_t *) _data;
37 char* const url = HB_PROJECT_URL_APPCAST;
38 char* const urlz = url + strlen( HB_PROJECT_URL_APPCAST ); /* marks null-term */
39 char url_host[64];
40 char url_path[128];
41 char query[256];
43 hb_net_t * net;
44 int ret;
45 char buf[4096];
46 char * cur, * end;
47 int size;
48 int i_vers;
49 char s_vers[32]; /* must be no larger than hb_handle_s.version */
50 int i;
52 /* Setup hb_query and hb_query_two with the correct appcast file */
53 hb_log( "Using %s", url );
55 /* extract host part */
56 cur = strstr( HB_PROJECT_URL_APPCAST, "//" );
57 if( !cur || cur+2 > urlz )
58 goto error;
59 cur += 2;
61 end = strstr( cur, "/" );
62 if( !end || end > urlz )
63 goto error;
65 memset( url_host, 0, sizeof(url_host) );
66 strncpy( url_host, cur, (end-cur) );
68 /* extract path part */
69 memset( url_path, 0, sizeof(url_path) );
70 strncpy( url_path, end, (urlz-end) );
72 if( !strlen( url_path ))
73 goto error;
75 memset( query, 0, sizeof(query) );
76 snprintf( query, sizeof(query), "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n", url_path, url_host );
78 /* Grab the data from the web server */
79 if( !( net = hb_net_open( url_host, 80 ) ) )
81 goto error;
84 if( hb_net_send( net, query ) < 0 )
86 hb_log("Error: Unable to connect to server");
87 hb_net_close( &net );
88 goto error;
91 size = 0;
92 memset( buf, 0, 4096 );
93 for( ;; )
95 ret = hb_net_recv( net, &buf[size], sizeof( buf ) - size );
96 if( ret < 1 )
98 hb_net_close( &net );
99 break;
101 size += ret;
104 cur = buf;
105 end = &buf[sizeof( buf )];
107 /* Make sure we got it */
108 cur += 9;
109 if( size < 15 || strncmp( cur, "200 OK", 6 ) )
111 hb_log("Error: We did not get a 200 OK from the server. \n");
112 goto error;
114 cur += 6;
116 /* Find the end of the headers and the beginning of the content */
117 for( ; &cur[3] < end; cur++ )
119 if( cur[0] == '\r' && cur[1] == '\n' &&
120 cur[2] == '\r' && cur[3] == '\n' )
122 cur += 4;
123 break;
127 if( cur >= end )
129 hb_log("Error: Found the end of the buffer before the end of the HTTP header information! \n");
130 goto error;
134 * Find the <cli> tag
135 * Scan though each character of the buffer until we find that the first 4 characters of "cur" are "<cli"
137 for(i=0 ; &cur[3] < end; i++, cur++ )
139 if( cur[0] == 'c' && cur[1] == 'l' && cur[2] == 'i' && cur[3] == '>' )
141 cur += 1;
142 break;
145 /* If the CLI tag has not been found in the first 768 characters, or the end is reached, something bad happened.*/
146 if (( i > 768) || ( cur >= end ))
148 hb_log("Error: Did not find the <cli> tag in the expected maximum amount of characters into the file. \n");
149 goto error;
153 if( cur >= end )
155 goto error;
159 * Ok, The above code didn't position cur, it only found <cli so we need to shift cur along 3 places.
160 * After which, the next 10 characters are the build number
162 cur += 3;
164 if( cur >= end )
166 hb_log("Error: Unexpected end of buffer! Could not find the build information. \n");
167 goto error;
170 /* Stable HB_PROJECT_BUILD */
171 i_vers = strtol( cur, &cur, 10 );
173 if( cur >= end )
175 hb_log("Error: Unexpected end of buffer! \n");
176 goto error;
180 * The Version number is 2 places after the build, so shift cur, 2 places.
181 * Get all the characters in cur until the point where " is found.
183 cur += 2;
185 if( cur >= end )
187 hb_log("Error: Unexpected end of buffer! Could not get version number. \n");
188 goto error;
190 memset( s_vers, 0, sizeof( s_vers ) );
191 for( i = 0; i < sizeof( s_vers ) - 1 && cur < end && *cur != '"'; i++, cur++ )
193 s_vers[i] = *cur;
195 /* If the CLI tag has not been found in the first 768 characters, or the end is reached, something bad happened.*/
196 if (( cur >= end ))
198 hb_log("Error: Version number too long, or end of buffer reached. \n");
199 goto error;
203 if( cur >= end )
205 goto error;
208 /* Print the version information */
209 hb_log( "latest: %s, build %d", s_vers, i_vers );
211 /* Return the build information */
212 if( i_vers > HB_PROJECT_BUILD )
214 memcpy( data->version, s_vers, sizeof(s_vers) );
215 *(data->build) = i_vers;
218 error:
219 free( data );
220 return;