]> git.sesse.net Git - vlc/blob - modules/access/http.c
1a6fd1f839a3a2a6f20f28e3b01769c22908072e
[vlc] / modules / access / http.c
1 /*****************************************************************************
2  * http.c: HTTP input module
3  *****************************************************************************
4  * Copyright (C) 2001-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/input.h>
32
33 #include "vlc_playlist.h"
34 #include "network.h"
35
36 /*****************************************************************************
37  * Module descriptor
38  *****************************************************************************/
39 static int  Open ( vlc_object_t * );
40 static void Close( vlc_object_t * );
41
42 #define PROXY_TEXT N_("HTTP proxy")
43 #define PROXY_LONGTEXT N_( \
44     "You can specify an HTTP proxy to use. It must be of the form " \
45     "http://myproxy.mydomain:myport/. If none is specified, the HTTP_PROXY " \
46     "environment variable will be tried." )
47
48 #define CACHING_TEXT N_("Caching value in ms")
49 #define CACHING_LONGTEXT N_( \
50     "Allows you to modify the default caching value for http streams. This " \
51     "value should be set in millisecond units." )
52
53 #define USER_TEXT N_("HTTP user name")
54 #define USER_LONGTEXT N_("Allows you to modify the user name that will " \
55     "be used for the connection (Basic authentication only).")
56
57 #define PASS_TEXT N_("HTTP password")
58 #define PASS_LONGTEXT N_("Allows you to modify the password that will be " \
59     "used for the connection.")
60
61 #define AGENT_TEXT N_("HTTP user agent")
62 #define AGENT_LONGTEXT N_("Allows you to modify the user agent that will be " \
63     "used for the connection.")
64
65 #define RECONNECT_TEXT N_("Auto re-connect")
66 #define RECONNECT_LONGTEXT N_("Will automatically attempt a re-connection " \
67     "in case it was untimely closed.")
68
69 vlc_module_begin();
70     set_description( _("HTTP input") );
71     set_capability( "access2", 0 );
72
73     add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
74                 VLC_FALSE );
75     add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
76                  CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
77     add_string( "http-user", NULL, NULL, USER_TEXT, USER_LONGTEXT, VLC_FALSE );
78     add_string( "http-pwd", NULL , NULL, PASS_TEXT, PASS_LONGTEXT, VLC_FALSE );
79     add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
80                 AGENT_LONGTEXT, VLC_FALSE );
81     add_bool( "http-reconnect", 0, NULL, RECONNECT_TEXT,
82               RECONNECT_LONGTEXT, VLC_TRUE );
83
84     add_shortcut( "http" );
85     add_shortcut( "http4" );
86     add_shortcut( "http6" );
87     set_callbacks( Open, Close );
88 vlc_module_end();
89
90 /*****************************************************************************
91  * Local prototypes
92  *****************************************************************************/
93 struct access_sys_t
94 {
95     int fd;
96
97     /* From uri */
98     vlc_url_t url;
99     char    *psz_user;
100     char    *psz_passwd;
101     char    *psz_user_agent;
102
103     /* Proxy */
104     vlc_bool_t b_proxy;
105     vlc_url_t  proxy;
106
107     /* */
108     int        i_code;
109     char      *psz_protocol;
110     int        i_version;
111
112     char       *psz_mime;
113     char       *psz_location;
114
115     vlc_bool_t b_chunked;
116     int64_t    i_chunk;
117
118     vlc_bool_t b_seekable;
119     vlc_bool_t b_reconnect;
120 };
121
122 /* */
123 static int Read( access_t *, uint8_t *, int );
124 static int Seek( access_t *, int64_t );
125 static int Control( access_t *, int, va_list );
126
127 /* */
128 static void ParseURL( access_sys_t *, char *psz_url );
129 static int  Connect( access_t *, int64_t );
130 static char *b64_encode( unsigned char *src );
131
132 /*****************************************************************************
133  * Open:
134  *****************************************************************************/
135 static int  Open ( vlc_object_t *p_this )
136 {
137     access_t     *p_access = (access_t*)p_this;
138     access_sys_t *p_sys;
139     char         *psz;
140
141     /* First set ipv4/ipv6 */
142     var_Create( p_access, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
143     var_Create( p_access, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
144
145     if( *p_access->psz_access )
146     {
147         vlc_value_t val;
148         /* Find out which shortcut was used */
149         if( !strncmp( p_access->psz_access, "http4", 6 ) )
150         {
151             val.b_bool = VLC_TRUE;
152             var_Set( p_access, "ipv4", val );
153
154             val.b_bool = VLC_FALSE;
155             var_Set( p_access, "ipv6", val );
156         }
157         else if( !strncmp( p_access->psz_access, "http6", 6 ) )
158         {
159             val.b_bool = VLC_TRUE;
160             var_Set( p_access, "ipv6", val );
161
162             val.b_bool = VLC_FALSE;
163             var_Set( p_access, "ipv4", val );
164         }
165     }
166
167     /* Set up p_access */
168     p_access->pf_read = Read;
169     p_access->pf_block = NULL;
170     p_access->pf_control = Control;
171     p_access->pf_seek = Seek;
172     p_access->info.i_update = 0;
173     p_access->info.i_size = 0;
174     p_access->info.i_pos = 0;
175     p_access->info.b_eof = VLC_FALSE;
176     p_access->info.i_title = 0;
177     p_access->info.i_seekpoint = 0;
178     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
179     memset( p_sys, 0, sizeof( access_sys_t ) );
180     p_sys->fd = -1;
181     p_sys->b_proxy = VLC_FALSE;
182     p_sys->i_version = 1;
183     p_sys->b_seekable = VLC_TRUE;
184     p_sys->psz_mime = NULL;
185     p_sys->psz_location = NULL;
186     p_sys->psz_user_agent = NULL;
187
188
189     /* Parse URI */
190     ParseURL( p_sys, p_access->psz_path );
191     if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
192     {
193         msg_Warn( p_access, "invalid host" );
194         goto error;
195     }
196     if( p_sys->url.i_port <= 0 )
197     {
198         p_sys->url.i_port = 80;
199     }
200     if( !p_sys->psz_user || *p_sys->psz_user == '\0' )
201     {
202         p_sys->psz_user = var_CreateGetString( p_access, "http-user" );
203         p_sys->psz_passwd = var_CreateGetString( p_access, "http-pwd" );
204     }
205
206     /* Do user agent */
207     p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
208
209     /* Check proxy */
210     psz = var_CreateGetString( p_access, "http-proxy" );
211     if( *psz )
212     {
213         p_sys->b_proxy = VLC_TRUE;
214         vlc_UrlParse( &p_sys->proxy, psz, 0 );
215     }
216     else
217     {
218         char *psz_proxy = getenv( "http_proxy" );
219         if( psz_proxy && *psz_proxy )
220         {
221             p_sys->b_proxy = VLC_TRUE;
222             vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
223         }
224         if( psz_proxy )
225             free( psz_proxy );
226     }
227     free( psz );
228
229     if( p_sys->b_proxy )
230     {
231         if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
232         {
233             msg_Warn( p_access, "invalid proxy host" );
234             goto error;
235         }
236         if( p_sys->proxy.i_port <= 0 )
237         {
238             p_sys->proxy.i_port = 80;
239         }
240     }
241
242     msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
243              p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
244     if( p_sys->b_proxy )
245     {
246         msg_Dbg( p_access, "      proxy %s:%d", p_sys->proxy.psz_host,
247                  p_sys->proxy.i_port );
248     }
249     if( p_sys->psz_user && *p_sys->psz_user )
250     {
251         msg_Dbg( p_access, "      user='%s', pwd='%s'",
252                  p_sys->psz_user, p_sys->psz_passwd );
253     }
254
255     p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
256
257     /* Connect */
258     if( Connect( p_access, 0 ) )
259     {
260         /* Retry with http 1.0 */
261         p_sys->i_version = 0;
262
263         if( p_access->b_die ||
264             Connect( p_access, 0 ) )
265         {
266             goto error;
267         }
268     }
269
270     if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
271           p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
272         p_sys->psz_location && *p_sys->psz_location )
273     {
274         playlist_t * p_playlist;
275
276         msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
277
278         p_playlist = vlc_object_find( p_access, VLC_OBJECT_PLAYLIST,
279                                       FIND_ANYWHERE );
280         if( !p_playlist )
281         {
282             msg_Err( p_access, "redirection failed: can't find playlist" );
283             goto error;
284         }
285         p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
286         playlist_Add( p_playlist, p_sys->psz_location, p_sys->psz_location,
287                       PLAYLIST_INSERT | PLAYLIST_GO,
288                       p_playlist->i_index + 1 );
289         vlc_object_release( p_playlist );
290
291         p_access->info.i_size = 0;  /* Force to stop reading */
292     }
293
294     if( !strcmp( p_sys->psz_protocol, "ICY" ) )
295     {
296         if( p_sys->psz_mime && !strcasecmp( p_sys->psz_mime, "video/nsv" ) )
297             p_access->psz_demux = strdup( "nsv" );
298         else
299             p_access->psz_demux = strdup( "mp3" );
300
301         msg_Info( p_access, "ICY server found, %s demuxer selected",
302                   p_access->psz_demux );
303     }
304
305     if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
306
307     /* PTS delay */
308     var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
309
310     return VLC_SUCCESS;
311
312 error:
313     vlc_UrlClean( &p_sys->url );
314     vlc_UrlClean( &p_sys->proxy );
315     if( p_sys->psz_mime ) free( p_sys->psz_mime );
316     if( p_sys->psz_location ) free( p_sys->psz_location );
317     if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
318     if( p_sys->psz_user ) free( p_sys->psz_user );
319     if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
320
321     if( p_sys->fd > 0 )
322     {
323         net_Close( p_sys->fd );
324     }
325     free( p_sys );
326     return VLC_EGENERIC;
327 }
328
329 /*****************************************************************************
330  * Close:
331  *****************************************************************************/
332 static void Close( vlc_object_t *p_this )
333 {
334     access_t     *p_access = (access_t*)p_this;
335     access_sys_t *p_sys = p_access->p_sys;
336
337     vlc_UrlClean( &p_sys->url );
338     vlc_UrlClean( &p_sys->proxy );
339
340     if( p_sys->psz_user ) free( p_sys->psz_user );
341     if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
342
343     if( p_sys->psz_mime ) free( p_sys->psz_mime );
344     if( p_sys->psz_location ) free( p_sys->psz_location );
345
346     if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
347
348     if( p_sys->fd > 0 )
349     {
350         net_Close( p_sys->fd );
351     }
352     free( p_sys );
353 }
354
355 /*****************************************************************************
356  * Read: Read up to i_len bytes from the http connection and place in
357  * p_buffer. Return the actual number of bytes read
358  *****************************************************************************/
359 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
360 {
361     access_sys_t *p_sys = p_access->p_sys;
362     int i_read;
363
364     if( p_sys->fd < 0 )
365     {
366         p_access->info.b_eof = VLC_TRUE;
367         return 0;
368     }
369
370     if( p_access->info.i_size > 0 &&
371         i_len + p_access->info.i_pos > p_access->info.i_size )
372     {
373         if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
374         {
375             p_access->info.b_eof = VLC_TRUE;
376             return 0;
377         }
378     }
379     if( p_sys->b_chunked )
380     {
381         if( p_sys->i_chunk < 0 )
382         {
383             p_access->info.b_eof = VLC_TRUE;
384             return 0;
385         }
386
387         if( p_sys->i_chunk <= 0 )
388         {
389             char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd );
390             /* read the chunk header */
391             if( psz == NULL )
392             {
393                 msg_Dbg( p_access, "failed reading chunk-header line" );
394                 return -1;
395             }
396             p_sys->i_chunk = strtoll( psz, NULL, 16 );
397             free( psz );
398
399             if( p_sys->i_chunk <= 0 )   /* eof */
400             {
401                 p_sys->i_chunk = -1;
402                 p_access->info.b_eof = VLC_TRUE;
403                 return 0;
404             }
405         }
406
407         if( i_len > p_sys->i_chunk )
408         {
409             i_len = p_sys->i_chunk;
410         }
411     }
412
413
414     i_read = net_Read( p_access, p_sys->fd, p_buffer, i_len, VLC_FALSE );
415     if( i_read > 0 )
416     {
417         p_access->info.i_pos += i_read;
418
419         if( p_sys->b_chunked )
420         {
421             p_sys->i_chunk -= i_read;
422             if( p_sys->i_chunk <= 0 )
423             {
424                 /* read the empty line */
425                 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd );
426                 if( psz ) free( psz );
427             }
428         }
429     }
430     else if( i_read == 0 )
431     {
432         if( p_sys->b_reconnect )
433         {
434             msg_Dbg( p_access, "got disconnected, trying to reconnect" );
435             net_Close( p_sys->fd ); p_sys->fd = -1;
436             if( Connect( p_access, p_access->info.i_pos ) )
437             {
438                 msg_Dbg( p_access, "reconnection failed" );
439             }
440             else
441             {
442                 p_sys->b_reconnect = VLC_FALSE;
443                 i_read = Read( p_access, p_buffer, i_len );
444                 p_sys->b_reconnect = VLC_TRUE;
445             }
446         }
447
448         if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
449     }
450
451     return i_read;
452 }
453
454 /*****************************************************************************
455  * Seek: close and re-open a connection at the right place
456  *****************************************************************************/
457 static int Seek( access_t *p_access, int64_t i_pos )
458 {
459     access_sys_t *p_sys = p_access->p_sys;
460
461     msg_Dbg( p_access, "trying to seek to "I64Fd, i_pos );
462
463     net_Close( p_sys->fd ); p_sys->fd = -1;
464
465     if( Connect( p_access, i_pos ) )
466     {
467         msg_Err( p_access, "seek failed" );
468         p_access->info.b_eof = VLC_TRUE;
469         return VLC_EGENERIC;
470     }
471     return VLC_SUCCESS;
472 }
473
474 /*****************************************************************************
475  * Control:
476  *****************************************************************************/
477 static int Control( access_t *p_access, int i_query, va_list args )
478 {
479     access_sys_t *p_sys = p_access->p_sys;
480     vlc_bool_t   *pb_bool;
481     int          *pi_int;
482     int64_t      *pi_64;
483
484     switch( i_query )
485     {
486         /* */
487         case ACCESS_CAN_SEEK:
488             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
489             *pb_bool = p_sys->b_seekable;
490             break;
491         case ACCESS_CAN_FASTSEEK:
492             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
493             *pb_bool = VLC_FALSE;
494             break;
495         case ACCESS_CAN_PAUSE:
496             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
497             *pb_bool = VLC_TRUE;    /* FIXME */
498             break;
499         case ACCESS_CAN_CONTROL_PACE:
500             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
501             *pb_bool = VLC_TRUE;    /* FIXME */
502             break;
503
504         /* */
505         case ACCESS_GET_MTU:
506             pi_int = (int*)va_arg( args, int * );
507             *pi_int = 0;
508             break;
509
510         case ACCESS_GET_PTS_DELAY:
511             pi_64 = (int64_t*)va_arg( args, int64_t * );
512             *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
513             break;
514
515         /* */
516         case ACCESS_SET_PAUSE_STATE:
517             break;
518
519         case ACCESS_GET_TITLE_INFO:
520         case ACCESS_SET_TITLE:
521         case ACCESS_SET_SEEKPOINT:
522             return VLC_EGENERIC;
523
524         default:
525             msg_Err( p_access, "unimplemented query in control" );
526             return VLC_EGENERIC;
527
528     }
529     return VLC_SUCCESS;
530 }
531
532 /*****************************************************************************
533  * ParseURL: extract user:password
534  *****************************************************************************/
535 static void ParseURL( access_sys_t *p_sys, char *psz_url )
536 {
537     char *psz_dup = strdup( psz_url );
538     char *p = psz_dup;
539     char *psz;
540
541     /* Syntax //[user:password]@<hostname>[:<port>][/<path>] */
542     while( *p == '/' )
543     {
544         p++;
545     }
546     psz = p;
547
548     /* Parse auth */
549     if( ( p = strchr( psz, '@' ) ) )
550     {
551         char *comma;
552
553         *p++ = '\0';
554         comma = strchr( psz, ':' );
555
556         /* Retreive user:password */
557         if( comma )
558         {
559             *comma++ = '\0';
560
561             p_sys->psz_user = strdup( psz );
562             p_sys->psz_passwd = strdup( comma );
563         }
564         else
565         {
566             p_sys->psz_user = strdup( psz );
567         }
568     }
569     else
570     {
571         p = psz;
572     }
573
574     /* Parse uri */
575     vlc_UrlParse( &p_sys->url, p, 0 );
576
577     free( psz_dup );
578 }
579
580 /*****************************************************************************
581  * Connect:
582  *****************************************************************************/
583 static int Connect( access_t *p_access, int64_t i_tell )
584 {
585     access_sys_t   *p_sys = p_access->p_sys;
586     vlc_url_t      srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
587     char           *psz;
588
589     /* Clean info */
590     if( p_sys->psz_location ) free( p_sys->psz_location );
591     if( p_sys->psz_mime ) free( p_sys->psz_mime );
592
593     p_sys->psz_location = NULL;
594     p_sys->psz_mime = NULL;
595     p_sys->b_chunked = VLC_FALSE;
596     p_sys->i_chunk = 0;
597
598     p_access->info.i_size = 0;
599     p_access->info.i_pos  = i_tell;
600     p_access->info.b_eof  = VLC_FALSE;
601
602
603     /* Open connection */
604     p_sys->fd = net_OpenTCP( p_access, srv.psz_host, srv.i_port );
605     if( p_sys->fd < 0 )
606     {
607         msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
608         return VLC_EGENERIC;
609     }
610
611     if( p_sys->b_proxy )
612     {
613         if( p_sys->url.psz_path )
614         {
615             net_Printf( VLC_OBJECT(p_access), p_sys->fd,
616                         "GET http://%s:%d/%s HTTP/1.%d\r\n",
617                         p_sys->url.psz_host, p_sys->url.i_port,
618                         p_sys->url.psz_path, p_sys->i_version );
619         }
620         else
621         {
622             net_Printf( VLC_OBJECT(p_access), p_sys->fd,
623                         "GET http://%s:%d/ HTTP/1.%d\r\n",
624                         p_sys->url.psz_host, p_sys->url.i_port,
625                         p_sys->i_version );
626
627         }
628     }
629     else
630     {
631         char *psz_path = p_sys->url.psz_path;
632         if( !psz_path || !*psz_path )
633         {
634             psz_path = "/";
635         }
636         if( p_sys->url.i_port != 80)
637         {
638             net_Printf( VLC_OBJECT(p_access), p_sys->fd,
639                         "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
640                         psz_path, p_sys->i_version, p_sys->url.psz_host,
641                         p_sys->url.i_port );
642         }
643         else
644         {        
645             net_Printf( VLC_OBJECT(p_access), p_sys->fd,
646                         "GET %s HTTP/1.%d\r\nHost: %s\r\n",
647                         psz_path, p_sys->i_version, p_sys->url.psz_host );
648         }
649     }
650     /* User Agent */
651     net_Printf( VLC_OBJECT(p_access), p_sys->fd, "User-Agent: %s\r\n",
652                 p_sys->psz_user_agent );
653     /* Offset */
654     if( p_sys->i_version == 1 )
655     {
656         net_Printf( VLC_OBJECT(p_access), p_sys->fd,
657                     "Range: bytes="I64Fd"-\r\n", i_tell );
658     }
659     /* Authentification */
660     if( p_sys->psz_user && *p_sys->psz_user )
661     {
662         char *buf;
663         char *b64;
664
665         asprintf( &buf, "%s:%s", p_sys->psz_user,
666                    p_sys->psz_passwd ? p_sys->psz_passwd : "" );
667
668         b64 = b64_encode( buf );
669
670         net_Printf( VLC_OBJECT(p_access), p_sys->fd,
671                     "Authorization: Basic %s\r\n", b64 );
672         free( b64 );
673     }
674     net_Printf( VLC_OBJECT(p_access), p_sys->fd, "Connection: Close\r\n" );
675
676     if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, "\r\n" ) < 0 )
677     {
678         msg_Err( p_access, "failed to send request" );
679         net_Close( p_sys->fd ); p_sys->fd = -1;
680         return VLC_EGENERIC;
681     }
682
683     /* Read Answer */
684     if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd ) ) == NULL )
685     {
686         msg_Err( p_access, "failed to read answer" );
687         goto error;
688     }
689     if( !strncmp( psz, "HTTP/1.", 7 ) )
690     {
691         p_sys->psz_protocol = "HTTP";
692         p_sys->i_code = atoi( &psz[9] );
693     }
694     else if( !strncmp( psz, "ICY", 3 ) )
695     {
696         p_sys->psz_protocol = "ICY";
697         p_sys->i_code = atoi( &psz[4] );
698         p_sys->b_reconnect = VLC_TRUE;
699     }
700     else
701     {
702         msg_Err( p_access, "invalid HTTP reply '%s'", psz );
703         free( psz );
704         goto error;
705     }
706     msg_Dbg( p_access, "protocol '%s' answer code %d",
707              p_sys->psz_protocol, p_sys->i_code );
708     if( !strcmp( p_sys->psz_protocol, "ICY" ) )
709     {
710         p_sys->b_seekable = VLC_FALSE;
711     }
712     if( p_sys->i_code != 206 )
713     {
714         p_sys->b_seekable = VLC_FALSE;
715     }
716     if( p_sys->i_code >= 400 )
717     {
718         msg_Err( p_access, "error: %s", psz );
719         free( psz );
720         goto error;
721     }
722     free( psz );
723
724     for( ;; )
725     {
726         char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd );
727         char *p;
728
729         if( psz == NULL )
730         {
731             msg_Err( p_access, "failed to read answer" );
732             goto error;
733         }
734
735         /* msg_Dbg( p_input, "Line=%s", psz ); */
736         if( *psz == '\0' )
737         {
738             free( psz );
739             break;
740         }
741
742
743         if( ( p = strchr( psz, ':' ) ) == NULL )
744         {
745             msg_Err( p_access, "malformed header line: %s", psz );
746             free( psz );
747             goto error;
748         }
749         *p++ = '\0';
750         while( *p == ' ' ) p++;
751
752         if( !strcasecmp( psz, "Content-Length" ) )
753         {
754             p_access->info.i_size = i_tell + atoll( p );
755             msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
756         }
757         else if( !strcasecmp( psz, "Location" ) )
758         {
759             if( p_sys->psz_location ) free( p_sys->psz_location );
760             p_sys->psz_location = strdup( p );
761         }
762         else if( !strcasecmp( psz, "Content-Type" ) )
763         {
764             if( p_sys->psz_mime ) free( p_sys->psz_mime );
765             p_sys->psz_mime = strdup( p );
766             msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
767         }
768         else if( !strcasecmp( psz, "Server" ) &&
769                  !strncasecmp( p, "Icecast", 7 ) )
770         {
771             p_sys->b_reconnect = VLC_TRUE;
772             msg_Dbg( p_access, "Server: %s", p );
773         }
774         else if( !strcasecmp( psz, "Transfer-Encoding" ) )
775         {
776             msg_Dbg( p_access, "Transfer-Encoding: %s", p );
777             if( !strncasecmp( p, "chunked", 7 ) )
778             {
779                 p_sys->b_chunked = VLC_TRUE;
780             }
781         }
782
783         free( psz );
784     }
785     return VLC_SUCCESS;
786
787 error:
788     net_Close( p_sys->fd ); p_sys->fd = -1;
789     return VLC_EGENERIC;
790 }
791
792 /*****************************************************************************
793  * b64_encode:
794  *****************************************************************************/
795 static char *b64_encode( unsigned char *src )
796 {
797     static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
798
799     char *dst = malloc( strlen( src ) * 4 / 3 + 12 );
800     char *ret = dst;
801     unsigned i_bits = 0;
802     unsigned i_shift = 0;
803
804     for( ;; )
805     {
806         if( *src )
807         {
808             i_bits = ( i_bits << 8 )|( *src++ );
809             i_shift += 8;
810         }
811         else if( i_shift > 0 )
812         {
813            i_bits <<= 6 - i_shift;
814            i_shift = 6;
815         }
816         else
817         {
818             *dst++ = '=';
819             break;
820         }
821
822         while( i_shift >= 6 )
823         {
824             i_shift -= 6;
825             *dst++ = b64[(i_bits >> i_shift)&0x3f];
826         }
827     }
828
829     *dst++ = '\0';
830
831     return ret;
832 }