OSDN Git Service

fix: cannot preview with QT
[handbrake-jp/handbrake-jp.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     char* const url  = HB_PROJECT_URL_APPCAST;
35     char* const urlz = url + strlen( HB_PROJECT_URL_APPCAST ); /* marks null-term */
36     char        url_host[64];
37     char        url_path[128];
38     char        query[256];
39
40         hb_net_t * net;
41     int        ret;
42     char       buf[4096];
43     char     * cur, * end;
44     int        size;
45     int        i_vers;
46     char       s_vers[32]; /* must be no larger than hb_handle_s.version */
47     int        i;
48
49     /* Setup hb_query and hb_query_two with the correct appcast file */
50     hb_log( "Using %s", url );
51
52     /* extract host part */
53     cur = strstr( HB_PROJECT_URL_APPCAST, "//" );
54     if( !cur || cur+2 > urlz )
55         goto error;
56     cur += 2;
57
58     end = strstr( cur, "/" );
59     if( !end || end > urlz )
60         goto error;
61
62     memset( url_host, 0, sizeof(url_host) );
63     strncpy( url_host, cur, (end-cur) );
64
65     /* extract path part */
66     memset( url_path, 0, sizeof(url_path) );
67     strncpy( url_path, end, (urlz-end) );
68
69     if( !strlen( url_path ))
70         goto error;
71
72     memset( query, 0, sizeof(query) );
73     snprintf( query, sizeof(query), "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n", url_path, url_host );
74
75     /* Grab the data from the web server */
76     if( !( net = hb_net_open( url_host, 80 ) ) )
77     {
78         goto error;
79     }
80
81     if( hb_net_send( net, query ) < 0 )
82     {
83         hb_log("Error: Unable to connect to server");
84         hb_net_close( &net );
85         goto error;
86     }
87
88     size = 0;
89     memset( buf, 0, 4096 );
90     for( ;; )
91     {
92         ret = hb_net_recv( net, &buf[size], sizeof( buf ) - size );
93         if( ret < 1 )
94         {
95             hb_net_close( &net );
96             break;
97         }
98         size += ret;
99     }
100
101     cur = buf;
102     end = &buf[sizeof( buf )];
103         
104     /* Make sure we got it */
105     cur += 9;
106     if( size < 15 || strncmp( cur, "200 OK", 6 ) )
107     {
108         hb_log("Error: We did not get a 200 OK from the server. \n");
109         goto error;
110     }
111     cur += 6;
112
113     /* Find the end of the headers and the beginning of the content */
114     for( ; &cur[3] < end; cur++ )
115     {
116         if( cur[0] == '\r' && cur[1] == '\n' &&
117             cur[2] == '\r' && cur[3] == '\n' )
118         {
119             cur += 4;
120             break;
121         }
122     }
123
124     if( cur >= end )
125     {
126         hb_log("Error: Found the end of the buffer before the end of the HTTP header information! \n");
127         goto error;
128     }
129         
130     /*
131      * Find the <cli> tag
132      * Scan though each character of the buffer until we find that the first 4 characters of "cur" are "<cli"
133      */
134     for(i=0 ; &cur[3] < end; i++, cur++ )
135     {
136         if( cur[0] == 'c' && cur[1] == 'l' && cur[2] == 'i' && cur[3] == '>' )
137         {
138             cur += 1;
139             break;
140         }
141                  
142         /* If the CLI tag has not been found in the first 768 characters, or the end is reached, something bad happened.*/
143         if (( i > 768) || ( cur >= end ))
144                 {
145             hb_log("Error: Did not find the <cli> tag in the expected maximum amount of characters into the file. \n");
146             goto error;
147                 }
148     }
149          
150     if( cur >= end )
151     {
152         goto error;
153     }
154         
155     /*
156      * Ok, The above code didn't position cur, it only found <cli so we need to shift cur along 3 places.
157      * After which, the next 10 characters are the build number
158      */
159     cur += 3;
160         
161     if( cur >= end )
162     {
163         hb_log("Error: Unexpected end of buffer! Could not find the build information. \n");
164         goto error;
165     }
166         
167     /* Stable HB_PROJECT_BUILD */
168     i_vers = strtol( cur, &cur, 10 );
169
170     if( cur >= end )
171     {
172         hb_log("Error: Unexpected end of buffer! \n");
173         goto error;
174     }
175         
176     /*
177      * The Version number is 2 places after the build, so shift cur, 2 places.
178      * Get all the characters in cur until the point where " is found.
179      */
180     cur += 2;
181         
182     if( cur >= end )
183     {
184         hb_log("Error: Unexpected end of buffer! Could not get version number. \n");
185         goto error;
186     }
187     memset( s_vers, 0, sizeof( s_vers ) );
188     for( i = 0;   i < sizeof( s_vers ) - 1 && cur < end && *cur != '"'; i++, cur++ )
189     {
190         s_vers[i] = *cur;
191                 
192         /* If the CLI tag has not been found in the first 768 characters, or the end is reached, something bad happened.*/
193         if (( cur >= end ))
194         {
195             hb_log("Error: Version number too long, or end of buffer reached. \n");
196             goto error;
197         }
198     }
199
200     if( cur >= end )
201     {
202         goto error;
203     }
204
205     /* Print the version information */
206     hb_log( "latest: %s, build %d", s_vers, i_vers );
207
208     /* Return the build information */
209     if( i_vers > HB_PROJECT_BUILD )
210     {
211         memcpy( data->version, s_vers, sizeof(s_vers) );
212         *(data->build) = i_vers;
213     }
214
215 error:
216     free( data );
217     return;
218 }