OSDN Git Service

libhb:
[handbrake-jp/handbrake-jp-git.git] / libhb / update.c
1 /* $Id: update.c,v 1.7 2005/03/26 23:04:14 titer Exp $
2
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. */
6
7 #include "hb.h"
8
9 static void UpdateFunc( void * );
10
11 typedef struct
12 {
13     int  * build;
14     char * version;
15
16 } hb_update_t;
17
18 hb_thread_t * hb_update_init( int * build, char * version )
19 {
20     hb_update_t * data = calloc( sizeof( hb_update_t ), 1 );
21     data->build   = build;
22     data->version = version;
23
24     return hb_thread_init( "update", UpdateFunc, data,
25                            HB_NORMAL_PRIORITY );
26 }
27
28
29 static void UpdateFunc( void * _data )
30 {
31
32     hb_update_t * data = (hb_update_t *) _data;
33         
34     /* New code to handle the hb_query stuff */
35     char* hb_query;
36     if( HB_BUILD % 100 )
37     {   
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");
40     }
41         else 
42         {
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");
45     }
46
47         // Grab the data from the web server
48     hb_net_t * net;
49     int        ret;
50     char       buf[4096];
51     char     * cur, * end;
52     int        size;
53     int        stable;
54     char       stable_str[16];
55     int        i;
56
57     if( !( net = hb_net_open( "handbrake.fr", 80 ) ) )
58     {
59         goto error;
60     }
61
62     if( hb_net_send( net, hb_query ) < 0 )
63     {
64         hb_log("Error: Unable to connect to server");
65         hb_net_close( &net );
66         goto error;
67     }
68
69     size = 0;
70     memset( buf, 0, 4096 );
71     for( ;; )
72     {
73         ret = hb_net_recv( net, &buf[size], sizeof( buf ) - size );
74         if( ret < 1 )
75         {
76             hb_net_close( &net );
77             break;
78         }
79         size += ret;
80     }
81
82     cur = buf;
83     end = &buf[sizeof( buf )];
84         
85     /* Make sure we got it */
86     cur += 9;
87     if( size < 15 || strncmp( cur, "200 OK", 6 ) )
88     {
89         /* Something went wrong */
90         hb_log("Error: We did not get a 200 OK from the server. \n");
91         goto error;
92     }
93     cur += 6;
94
95     /* Find the end of the headers and the beginning of the content */
96     for( ; &cur[3] < end; cur++ )
97     {
98         if( cur[0] == '\r' && cur[1] == '\n' &&
99             cur[2] == '\r' && cur[3] == '\n' )
100         {
101             cur += 4;
102             break;
103         }
104     }
105
106     if( cur >= end )
107     {
108         hb_log("Error: Found the end of the buffer before the end of the HTTP header information! \n");
109         goto error;
110     }
111         
112         // Version Checking Here
113         
114     /*
115      * Find the <cli> tag
116      * Scan though each character of the buffer until we find that the first 4 characters of "cur" are "<cli"
117      */
118     for(i=0 ; &cur[3] < end; i++, cur++ )
119     {
120         if( cur[0] == 'c' && cur[1] == 'l' && cur[2] == 'i' && cur[3] == '>' )
121         {
122             cur += 1;
123             break;
124         }
125                  
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 ))
128                 {
129             hb_log("Error: Did not find the <cli> tag in the expected maximum amount of characters into the file. \n");
130             goto error;
131                 }
132     }
133          
134     if( cur >= end )
135     {
136         goto error;
137     }
138         
139     /*
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
142      */
143     cur += 3;
144         
145     if( cur >= end )
146     {
147         hb_log("Error: Unexpected end of buffer! Could not find the build information. \n");
148         goto error;
149     }
150         
151     stable = strtol( cur, &cur, 10 );
152                 
153     if( cur >= end )
154     {
155      hb_log("Error: Unexpected end of buffer! \n");
156         goto error;
157     }
158         
159     /*
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.
162      */
163     cur += 2;
164         
165     if( cur >= end )
166     {
167         hb_log("Error: Unexpected end of buffer! Could not get version number. \n");
168         goto error;
169     }
170     memset( stable_str, 0, sizeof( stable_str ) );
171     for( i = 0;   i < sizeof( stable_str ) - 1 && cur < end && *cur != '"'; i++, cur++ )
172     {
173         stable_str[i] = *cur;
174                 
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 ))
177         {
178             hb_log("Error: Version number too long, or end of buffer reached. \n");
179             goto error;
180         }
181     }
182         
183     if( cur >= end )
184     {
185         goto error;
186     }
187         
188     hb_log( "latest stable: %s, build %d", stable_str, stable );
189         
190         // Return the build information
191     if( stable > HB_BUILD )
192     {
193         memcpy( data->version, stable_str, sizeof( stable_str ) );
194         *(data->build) = stable;
195     }
196
197 error:
198     free( data );
199     return;
200 }