OSDN Git Service

update.c altered to use both appcast.xml and appcast_unstable.xml
[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 static int find_file( );
11
12 typedef struct
13 {
14     int  * build;
15     char * version;
16
17 } hb_update_t;
18
19 hb_thread_t * hb_update_init( int * build, char * version )
20 {
21     hb_update_t * data = calloc( sizeof( hb_update_t ), 1 );
22     data->build   = build;
23     data->version = version;
24
25     return hb_thread_init( "update", UpdateFunc, data,
26                            HB_NORMAL_PRIORITY );
27 }
28
29 /*
30  * Find out which appcast we want to use, and return appcast.xml or NULL
31  */
32 static int find_file ( void )
33 {
34         return ( strstr( APPCAST_URL, "appcast.xml" ) != NULL );
35 }
36
37 static void UpdateFunc( void * _data )
38 {
39
40     hb_update_t * data = (hb_update_t *) _data;
41         
42         /* New code to handle the hb_query stuff */
43         int file = find_file();
44         char* hb_query;
45         if (file != NULL)
46         {
47                 hb_query = "GET /appcast.xml HTTP/1.0\r\nHost: handbrake.fr\r\n\r\n";
48         }else {
49                 hb_query = "GET /appcast_unstable.xml HTTP/1.0\r\nHost: handbrake.fr\r\n\r\n";
50         }
51         
52         // ####################### Grab the data from the web server ##########################
53     hb_net_t * net;
54     int        ret;
55     char       buf[4096];
56     char     * cur, * end;
57     int        size;
58     int        stable;
59     char       stable_str[16];
60     int        i;
61
62     if( !( net = hb_net_open( "handbrake.fr", 80 ) ) )
63     {
64         goto error;
65     }
66
67     if( hb_net_send( net, hb_query ) < 0 )
68     {
69         hb_net_close( &net );
70         goto error;
71     }
72
73     size = 0;
74     memset( buf, 0, 4096 );
75     for( ;; )
76     {
77         ret = hb_net_recv( net, &buf[size], sizeof( buf ) - size );
78         if( ret < 1 )
79         {
80             hb_net_close( &net );
81             break;
82         }
83         size += ret;
84     }
85
86     cur = buf;
87     end = &buf[sizeof( buf )];
88         
89
90         
91     /* Make sure we got it */
92     cur += 9;
93     if( size < 15 || strncmp( cur, "200 OK", 6 ) )
94     {
95         /* Something went wrong */
96         goto error;
97     }
98     cur += 6;
99
100     /* Find the end of the headers and the beginning of the content */
101     for( ; &cur[3] < end; cur++ )
102     {
103         if( cur[0] == '\r' && cur[1] == '\n' &&
104             cur[2] == '\r' && cur[3] == '\n' )
105         {
106             cur += 4;
107             break;
108         }
109     }
110
111     if( cur >= end )
112     {
113         goto error;
114     }
115         
116         // ####################### Version Checking Here ##########################
117         
118         /*
119          * Find the <cli> tag
120          * Scan though each character of the buffer until we find that the first 4 characters of "cur" are "<cli"
121          */
122      for(i=0 ; &cur[3] < end; i++, cur++ )
123      {
124         
125         if( cur[0] == 'c' && cur[1] == 'l' && cur[2] == 'i' && cur[3] == '>' )
126          {
127             cur += 1;
128             break;
129                         
130          }
131                  
132                  // If the CLI tag has not been found in the first 768 characters, or the end is reached, something bad happened.
133                  if (( i > 768) || ( cur >= end ))
134                  {
135                         goto error;
136                  }
137      }
138          
139         if( cur >= end )
140     {
141         goto error;
142     }
143         
144         /*
145          * Ok, The above code didn't position cur, it only found <cli so we need to shift cur along 3 places.
146          * After which, the next 10 characters are the build number
147          */
148     cur += 3;
149         
150         if( cur >= end )
151     {
152         goto error;
153     }
154         
155         stable = strtol( cur, &cur, 10 );
156                 
157         if( cur >= end )
158     {
159         goto error;
160     }
161         
162         /*
163          * The Version number is 2 places after the build, so shift cur, 2 places.
164          * Get all the characters in cur until the point where " is found.
165          */
166         cur += 2;
167         
168         if( cur >= end )
169     {
170         goto error;
171     }
172         memset( stable_str, 0, sizeof( stable_str ) );
173         for( i = 0;   i < sizeof( stable_str ) - 1 && cur < end && *cur != '"'; i++, cur++ )
174         {
175                 stable_str[i] = *cur;
176                 
177                 // If the version number is longer than 7 characters, or the end is reached, something has gone wrong.
178                 if (( i > 7) || ( cur >= end ))
179                 {
180                         goto error;
181                 }
182         }
183         
184         if( cur >= end )
185     {
186         goto error;
187     }
188         
189     hb_log( "latest stable: %s, build %d", stable_str, stable );
190         
191         // END OF STABLE INFO ###################################################
192
193
194     if( stable > HB_BUILD )
195     {
196         memcpy( data->version, stable_str, sizeof( stable_str ) );
197         *(data->build) = stable;
198     }
199
200 error:
201     free( data );
202     return;
203 }