]> git.sesse.net Git - vlc/blob - modules/access/http.c
* modules/access/screen/win32.c: proper rgb mask for RV24.
[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         case ACCESS_SET_PRIVATE_ID_STATE:
523             return VLC_EGENERIC;
524
525         default:
526             msg_Warn( p_access, "unimplemented query in control" );
527             return VLC_EGENERIC;
528
529     }
530     return VLC_SUCCESS;
531 }
532
533 /*****************************************************************************
534  * ParseURL: extract user:password
535  *****************************************************************************/
536 static void ParseURL( access_sys_t *p_sys, char *psz_url )
537 {
538     char *psz_dup = strdup( psz_url );
539     char *p = psz_dup;
540     char *psz;
541
542     /* Syntax //[user:password]@<hostname>[:<port>][/<path>] */
543     while( *p == '/' )
544     {
545         p++;
546     }
547     psz = p;
548
549     /* Parse auth */
550     if( ( p = strchr( psz, '@' ) ) )
551     {
552         char *comma;
553
554         *p++ = '\0';
555         comma = strchr( psz, ':' );
556
557         /* Retreive user:password */
558         if( comma )
559         {
560             *comma++ = '\0';
561
562             p_sys->psz_user = strdup( psz );
563             p_sys->psz_passwd = strdup( comma );
564         }
565         else
566         {
567             p_sys->psz_user = strdup( psz );
568         }
569     }
570     else
571     {
572         p = psz;
573     }
574
575     /* Parse uri */
576     vlc_UrlParse( &p_sys->url, p, 0 );
577
578     free( psz_dup );
579 }
580
581 /*****************************************************************************
582  * Connect:
583  *****************************************************************************/
584 static int Connect( access_t *p_access, int64_t i_tell )
585 {
586     access_sys_t   *p_sys = p_access->p_sys;
587     vlc_url_t      srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
588     char           *psz;
589
590     /* Clean info */
591     if( p_sys->psz_location ) free( p_sys->psz_location );
592     if( p_sys->psz_mime ) free( p_sys->psz_mime );
593
594     p_sys->psz_location = NULL;
595     p_sys->psz_mime = NULL;
596     p_sys->b_chunked = VLC_FALSE;
597     p_sys->i_chunk = 0;
598
599     p_access->info.i_size = 0;
600     p_access->info.i_pos  = i_tell;
601     p_access->info.b_eof  = VLC_FALSE;
602
603
604     /* Open connection */
605     p_sys->fd = net_OpenTCP( p_access, srv.psz_host, srv.i_port );
606     if( p_sys->fd < 0 )
607     {
608         msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
609         return VLC_EGENERIC;
610     }
611
612     if( p_sys->b_proxy )
613     {
614         if( p_sys->url.psz_path )
615         {
616             net_Printf( VLC_OBJECT(p_access), p_sys->fd,
617                         "GET http://%s:%d/%s HTTP/1.%d\r\n",
618                         p_sys->url.psz_host, p_sys->url.i_port,
619                         p_sys->url.psz_path, p_sys->i_version );
620         }
621         else
622         {
623             net_Printf( VLC_OBJECT(p_access), p_sys->fd,
624                         "GET http://%s:%d/ HTTP/1.%d\r\n",
625                         p_sys->url.psz_host, p_sys->url.i_port,
626                         p_sys->i_version );
627
628         }
629     }
630     else
631     {
632         char *psz_path = p_sys->url.psz_path;
633         if( !psz_path || !*psz_path )
634         {
635             psz_path = "/";
636         }
637         if( p_sys->url.i_port != 80)
638         {
639             net_Printf( VLC_OBJECT(p_access), p_sys->fd,
640                         "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
641                         psz_path, p_sys->i_version, p_sys->url.psz_host,
642                         p_sys->url.i_port );
643         }
644         else
645         {        
646             net_Printf( VLC_OBJECT(p_access), p_sys->fd,
647                         "GET %s HTTP/1.%d\r\nHost: %s\r\n",
648                         psz_path, p_sys->i_version, p_sys->url.psz_host );
649         }
650     }
651     /* User Agent */
652     net_Printf( VLC_OBJECT(p_access), p_sys->fd, "User-Agent: %s\r\n",
653                 p_sys->psz_user_agent );
654     /* Offset */
655     if( p_sys->i_version == 1 )
656     {
657         net_Printf( VLC_OBJECT(p_access), p_sys->fd,
658                     "Range: bytes="I64Fd"-\r\n", i_tell );
659     }
660     /* Authentification */
661     if( p_sys->psz_user && *p_sys->psz_user )
662     {
663         char *buf;
664         char *b64;
665
666         asprintf( &buf, "%s:%s", p_sys->psz_user,
667                    p_sys->psz_passwd ? p_sys->psz_passwd : "" );
668
669         b64 = b64_encode( buf );
670
671         net_Printf( VLC_OBJECT(p_access), p_sys->fd,
672                     "Authorization: Basic %s\r\n", b64 );
673         free( b64 );
674     }
675     net_Printf( VLC_OBJECT(p_access), p_sys->fd, "Connection: Close\r\n" );
676
677     if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, "\r\n" ) < 0 )
678     {
679         msg_Err( p_access, "failed to send request" );
680         net_Close( p_sys->fd ); p_sys->fd = -1;
681         return VLC_EGENERIC;
682     }
683
684     /* Read Answer */
685     if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd ) ) == NULL )
686     {
687         msg_Err( p_access, "failed to read answer" );
688         goto error;
689     }
690     if( !strncmp( psz, "HTTP/1.", 7 ) )
691     {
692         p_sys->psz_protocol = "HTTP";
693         p_sys->i_code = atoi( &psz[9] );
694     }
695     else if( !strncmp( psz, "ICY", 3 ) )
696     {
697         p_sys->psz_protocol = "ICY";
698         p_sys->i_code = atoi( &psz[4] );
699         p_sys->b_reconnect = VLC_TRUE;
700     }
701     else
702     {
703         msg_Err( p_access, "invalid HTTP reply '%s'", psz );
704         free( psz );
705         goto error;
706     }
707     msg_Dbg( p_access, "protocol '%s' answer code %d",
708              p_sys->psz_protocol, p_sys->i_code );
709     if( !strcmp( p_sys->psz_protocol, "ICY" ) )
710     {
711         p_sys->b_seekable = VLC_FALSE;
712     }
713     if( p_sys->i_code != 206 )
714     {
715         p_sys->b_seekable = VLC_FALSE;
716     }
717     if( p_sys->i_code >= 400 )
718     {
719         msg_Err( p_access, "error: %s", psz );
720         free( psz );
721         goto error;
722     }
723     free( psz );
724
725     for( ;; )
726     {
727         char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd );
728         char *p;
729
730         if( psz == NULL )
731         {
732             msg_Err( p_access, "failed to read answer" );
733             goto error;
734         }
735
736         /* msg_Dbg( p_input, "Line=%s", psz ); */
737         if( *psz == '\0' )
738         {
739             free( psz );
740             break;
741         }
742
743
744         if( ( p = strchr( psz, ':' ) ) == NULL )
745         {
746             msg_Err( p_access, "malformed header line: %s", psz );
747             free( psz );
748             goto error;
749         }
750         *p++ = '\0';
751         while( *p == ' ' ) p++;
752
753         if( !strcasecmp( psz, "Content-Length" ) )
754         {
755             p_access->info.i_size = i_tell + atoll( p );
756             msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
757         }
758         else if( !strcasecmp( psz, "Location" ) )
759         {
760             if( p_sys->psz_location ) free( p_sys->psz_location );
761             p_sys->psz_location = strdup( p );
762         }
763         else if( !strcasecmp( psz, "Content-Type" ) )
764         {
765             if( p_sys->psz_mime ) free( p_sys->psz_mime );
766             p_sys->psz_mime = strdup( p );
767             msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
768         }
769         else if( !strcasecmp( psz, "Server" ) &&
770                  !strncasecmp( p, "Icecast", 7 ) )
771         {
772             p_sys->b_reconnect = VLC_TRUE;
773             msg_Dbg( p_access, "Server: %s", p );
774         }
775         else if( !strcasecmp( psz, "Transfer-Encoding" ) )
776         {
777             msg_Dbg( p_access, "Transfer-Encoding: %s", p );
778             if( !strncasecmp( p, "chunked", 7 ) )
779             {
780                 p_sys->b_chunked = VLC_TRUE;
781             }
782         }
783
784         free( psz );
785     }
786     return VLC_SUCCESS;
787
788 error:
789     net_Close( p_sys->fd ); p_sys->fd = -1;
790     return VLC_EGENERIC;
791 }
792
793 /*****************************************************************************
794  * b64_encode:
795  *****************************************************************************/
796 static char *b64_encode( unsigned char *src )
797 {
798     static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
799
800     char *dst = malloc( strlen( src ) * 4 / 3 + 12 );
801     char *ret = dst;
802     unsigned i_bits = 0;
803     unsigned i_shift = 0;
804
805     for( ;; )
806     {
807         if( *src )
808         {
809             i_bits = ( i_bits << 8 )|( *src++ );
810             i_shift += 8;
811         }
812         else if( i_shift > 0 )
813         {
814            i_bits <<= 6 - i_shift;
815            i_shift = 6;
816         }
817         else
818         {
819             *dst++ = '=';
820             break;
821         }
822
823         while( i_shift >= 6 )
824         {
825             i_shift -= 6;
826             *dst++ = b64[(i_bits >> i_shift)&0x3f];
827         }
828     }
829
830     *dst++ = '\0';
831
832     return ret;
833 }