OSDN Git Service

Don't discard titles during scan just because of a read failure on one or more of...
[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.m0k.org/>.
5    It may be used under the terms of the GNU General Public License. */
6
7 #include "hb.h"
8
9 #define HB_URL   "handbrake.m0k.org"
10 #define HB_QUERY "GET /LATEST HTTP/1.0\r\nHost: " HB_URL "\r\n\r\n"
11
12 typedef struct
13 {
14     int  * build;
15     char * version;
16
17 } hb_update_t;
18
19 static void UpdateFunc( void * );
20
21 hb_thread_t * hb_update_init( int * build, char * version )
22 {
23     hb_update_t * data = calloc( sizeof( hb_update_t ), 1 );
24     data->build   = build;
25     data->version = version;
26
27     return hb_thread_init( "update", UpdateFunc, data,
28                            HB_NORMAL_PRIORITY );
29 }
30
31 static void UpdateFunc( void * _data )
32 {
33     hb_update_t * data = (hb_update_t *) _data;
34
35     hb_net_t * net;
36     int        ret;
37     char       buf[1024];
38     char     * cur, * end, * p;
39     int        size;
40     int        stable, unstable;
41     char       stable_str[16], unstable_str[16];
42     int        i;
43
44     if( !( net = hb_net_open( HB_URL, 80 ) ) )
45     {
46         goto error;
47     }
48
49     if( hb_net_send( net, HB_QUERY ) < 0 )
50     {
51         hb_net_close( &net );
52         goto error;
53     }
54
55     size = 0;
56     memset( buf, 0, 1024 );
57     for( ;; )
58     {
59         ret = hb_net_recv( net, &buf[size], sizeof( buf ) - size );
60         if( ret < 1 )
61         {
62             hb_net_close( &net );
63             break;
64         }
65         size += ret;
66     }
67
68     cur = buf;
69     end = &buf[sizeof( buf )];
70
71     /* Make sure we got it */
72     cur += 9;
73     if( size < 15 || strncmp( cur, "200 OK", 6 ) )
74     {
75         /* Something went wrong */
76         goto error;
77     }
78     cur += 6;
79
80     /* Find the end of the headers and the beginning of the content */
81     for( ; &cur[3] < end; cur++ )
82     {
83         if( cur[0] == '\r' && cur[1] == '\n' &&
84             cur[2] == '\r' && cur[3] == '\n' )
85         {
86             cur += 4;
87             break;
88         }
89     }
90
91     if( cur >= end )
92     {
93         goto error;
94     }
95
96     stable = strtol( cur, &p, 10 );
97     if( cur == p )
98     {
99         goto error;
100     }
101     cur = p + 1;
102     memset( stable_str, 0, sizeof( stable_str ) );
103     for( i = 0;
104          i < sizeof( stable_str ) - 1 && cur < end && *cur != '\n';
105          i++, cur++ )
106     {
107         stable_str[i] = *cur;
108     }
109
110     hb_log( "latest stable: %s, build %d", stable_str, stable );
111
112     cur++;
113     if( cur >= end )
114     {
115         goto error;
116     }
117
118     unstable = strtol( cur, &p, 10 );
119     if( cur == p )
120     {
121         goto error;
122     }
123     cur = p + 1;
124     memset( unstable_str, 0, sizeof( unstable_str ) );
125     for( i = 0;
126          i < sizeof( unstable_str ) - 1 && cur < end && *cur != '\n';
127          i++, cur++ )
128     {
129         unstable_str[i] = *cur;
130     }
131
132     hb_log( "latest unstable: %s, build %d", unstable_str, unstable );
133
134     if( HB_BUILD % 100 )
135     {
136         /* We are runnning an unstable build */
137         if( unstable > HB_BUILD )
138         {
139             memcpy( data->version, unstable_str, sizeof( unstable_str ) );
140             *(data->build) = unstable;
141         }
142     }
143     else
144     {
145         /* We are runnning an stable build */
146         if( stable > HB_BUILD )
147         {
148             memcpy( data->version, stable_str, sizeof( stable_str ) );
149             *(data->build) = stable;
150         }
151     }
152
153 error:
154     free( data );
155     return;
156 }
157