]> git.sesse.net Git - vlc/blob - modules/access/http.c
309281b3e8233b5dfca72f655b4b6b9d3f5cbee6
[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     vlc_bool_t b_pace_control;
121 };
122
123 /* */
124 static int Read( access_t *, uint8_t *, int );
125 static int Seek( access_t *, int64_t );
126 static int Control( access_t *, int, va_list );
127
128 /* */
129 static void ParseURL( access_sys_t *, char *psz_url );
130 static int  Connect( access_t *, int64_t );
131 static char *b64_encode( unsigned char *src );
132
133 /*****************************************************************************
134  * Open:
135  *****************************************************************************/
136 static int Open( vlc_object_t *p_this )
137 {
138     access_t     *p_access = (access_t*)p_this;
139     access_sys_t *p_sys;
140     char         *psz;
141
142     /* First set ipv4/ipv6 */
143     var_Create( p_access, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
144     var_Create( p_access, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
145
146     if( *p_access->psz_access )
147     {
148         vlc_value_t val;
149         /* Find out which shortcut was used */
150         if( !strncmp( p_access->psz_access, "http4", 6 ) )
151         {
152             val.b_bool = VLC_TRUE;
153             var_Set( p_access, "ipv4", val );
154
155             val.b_bool = VLC_FALSE;
156             var_Set( p_access, "ipv6", val );
157         }
158         else if( !strncmp( p_access->psz_access, "http6", 6 ) )
159         {
160             val.b_bool = VLC_TRUE;
161             var_Set( p_access, "ipv6", val );
162
163             val.b_bool = VLC_FALSE;
164             var_Set( p_access, "ipv4", val );
165         }
166     }
167
168     /* Set up p_access */
169     p_access->pf_read = Read;
170     p_access->pf_block = NULL;
171     p_access->pf_control = Control;
172     p_access->pf_seek = Seek;
173     p_access->info.i_update = 0;
174     p_access->info.i_size = 0;
175     p_access->info.i_pos = 0;
176     p_access->info.b_eof = VLC_FALSE;
177     p_access->info.i_title = 0;
178     p_access->info.i_seekpoint = 0;
179     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
180     memset( p_sys, 0, sizeof( access_sys_t ) );
181     p_sys->fd = -1;
182     p_sys->b_proxy = VLC_FALSE;
183     p_sys->i_version = 1;
184     p_sys->b_seekable = VLC_TRUE;
185     p_sys->psz_mime = NULL;
186     p_sys->psz_location = NULL;
187     p_sys->psz_user_agent = NULL;
188     p_sys->b_pace_control = VLC_TRUE;
189
190     /* Parse URI */
191     ParseURL( p_sys, p_access->psz_path );
192     if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
193     {
194         msg_Warn( p_access, "invalid host" );
195         goto error;
196     }
197     if( p_sys->url.i_port <= 0 )
198     {
199         p_sys->url.i_port = 80;
200     }
201     if( !p_sys->psz_user || *p_sys->psz_user == '\0' )
202     {
203         p_sys->psz_user = var_CreateGetString( p_access, "http-user" );
204         p_sys->psz_passwd = var_CreateGetString( p_access, "http-pwd" );
205     }
206
207     /* Do user agent */
208     p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
209
210     /* Check proxy */
211     psz = var_CreateGetString( p_access, "http-proxy" );
212     if( *psz )
213     {
214         p_sys->b_proxy = VLC_TRUE;
215         vlc_UrlParse( &p_sys->proxy, psz, 0 );
216     }
217     else
218     {
219         char *psz_proxy = getenv( "http_proxy" );
220         if( psz_proxy && *psz_proxy )
221         {
222             p_sys->b_proxy = VLC_TRUE;
223             vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
224         }
225         if( psz_proxy )
226             free( psz_proxy );
227     }
228     free( psz );
229
230     if( p_sys->b_proxy )
231     {
232         if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
233         {
234             msg_Warn( p_access, "invalid proxy host" );
235             goto error;
236         }
237         if( p_sys->proxy.i_port <= 0 )
238         {
239             p_sys->proxy.i_port = 80;
240         }
241     }
242
243     msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
244              p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
245     if( p_sys->b_proxy )
246     {
247         msg_Dbg( p_access, "      proxy %s:%d", p_sys->proxy.psz_host,
248                  p_sys->proxy.i_port );
249     }
250     if( p_sys->psz_user && *p_sys->psz_user )
251     {
252         msg_Dbg( p_access, "      user='%s', pwd='%s'",
253                  p_sys->psz_user, p_sys->psz_passwd );
254     }
255
256     p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
257
258     /* Connect */
259     if( Connect( p_access, 0 ) )
260     {
261         /* Retry with http 1.0 */
262         p_sys->i_version = 0;
263
264         if( p_access->b_die ||
265             Connect( p_access, 0 ) )
266         {
267             goto error;
268         }
269     }
270
271     if( p_sys->psz_mime && !strcasecmp( p_sys->psz_mime, "video/x-ms-asf" ) )
272     {
273         if( !strcasestr( p_access->psz_path, "asx" ) )
274         {
275             msg_Dbg( p_access, "this isn't http, it's mms" );
276             goto error;
277         }
278     }
279
280     if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
281           p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
282         p_sys->psz_location && *p_sys->psz_location )
283     {
284         playlist_t * p_playlist;
285
286         msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
287
288         p_playlist = vlc_object_find( p_access, VLC_OBJECT_PLAYLIST,
289                                       FIND_ANYWHERE );
290         if( !p_playlist )
291         {
292             msg_Err( p_access, "redirection failed: can't find playlist" );
293             goto error;
294         }
295         p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
296         playlist_Add( p_playlist, p_sys->psz_location, p_sys->psz_location,
297                       PLAYLIST_INSERT,
298                       p_playlist->i_index + 1 );
299         vlc_object_release( p_playlist );
300
301         p_access->info.i_size = 0;  /* Force to stop reading */
302     }
303
304     if( !strcmp( p_sys->psz_protocol, "ICY" ) )
305     {
306         if( p_sys->psz_mime && !strcasecmp( p_sys->psz_mime, "video/nsv" ) )
307             p_access->psz_demux = strdup( "nsv" );
308         else if( p_sys->psz_mime && ( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
309                  !strcasecmp( p_sys->psz_mime, "audio/aacp" ) ) )
310             p_access->psz_demux = strdup( "m4a" );
311         else
312             p_access->psz_demux = strdup( "mp3" );
313
314         msg_Info( p_access, "ICY server found, %s demuxer selected",
315                   p_access->psz_demux );
316
317         p_sys->b_pace_control = VLC_FALSE;
318     }
319
320     if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
321
322     /* PTS delay */
323     var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
324
325     return VLC_SUCCESS;
326
327 error:
328     vlc_UrlClean( &p_sys->url );
329     vlc_UrlClean( &p_sys->proxy );
330     if( p_sys->psz_mime ) free( p_sys->psz_mime );
331     if( p_sys->psz_location ) free( p_sys->psz_location );
332     if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
333     if( p_sys->psz_user ) free( p_sys->psz_user );
334     if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
335
336     if( p_sys->fd > 0 )
337     {
338         net_Close( p_sys->fd );
339     }
340     free( p_sys );
341     return VLC_EGENERIC;
342 }
343
344 /*****************************************************************************
345  * Close:
346  *****************************************************************************/
347 static void Close( vlc_object_t *p_this )
348 {
349     access_t     *p_access = (access_t*)p_this;
350     access_sys_t *p_sys = p_access->p_sys;
351
352     vlc_UrlClean( &p_sys->url );
353     vlc_UrlClean( &p_sys->proxy );
354
355     if( p_sys->psz_user ) free( p_sys->psz_user );
356     if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
357
358     if( p_sys->psz_mime ) free( p_sys->psz_mime );
359     if( p_sys->psz_location ) free( p_sys->psz_location );
360
361     if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
362
363     if( p_sys->fd > 0 )
364     {
365         net_Close( p_sys->fd );
366     }
367     free( p_sys );
368 }
369
370 /*****************************************************************************
371  * Read: Read up to i_len bytes from the http connection and place in
372  * p_buffer. Return the actual number of bytes read
373  *****************************************************************************/
374 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
375 {
376     access_sys_t *p_sys = p_access->p_sys;
377     int i_read;
378
379     if( p_sys->fd < 0 )
380     {
381         p_access->info.b_eof = VLC_TRUE;
382         return 0;
383     }
384
385     if( p_access->info.i_size > 0 &&
386         i_len + p_access->info.i_pos > p_access->info.i_size )
387     {
388         if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
389         {
390             p_access->info.b_eof = VLC_TRUE;
391             return 0;
392         }
393     }
394     if( p_sys->b_chunked )
395     {
396         if( p_sys->i_chunk < 0 )
397         {
398             p_access->info.b_eof = VLC_TRUE;
399             return 0;
400         }
401
402         if( p_sys->i_chunk <= 0 )
403         {
404             char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd );
405             /* read the chunk header */
406             if( psz == NULL )
407             {
408                 msg_Dbg( p_access, "failed reading chunk-header line" );
409                 return -1;
410             }
411             p_sys->i_chunk = strtoll( psz, NULL, 16 );
412             free( psz );
413
414             if( p_sys->i_chunk <= 0 )   /* eof */
415             {
416                 p_sys->i_chunk = -1;
417                 p_access->info.b_eof = VLC_TRUE;
418                 return 0;
419             }
420         }
421
422         if( i_len > p_sys->i_chunk )
423         {
424             i_len = p_sys->i_chunk;
425         }
426     }
427
428
429     i_read = net_Read( p_access, p_sys->fd, p_buffer, i_len, VLC_FALSE );
430     if( i_read > 0 )
431     {
432         p_access->info.i_pos += i_read;
433
434         if( p_sys->b_chunked )
435         {
436             p_sys->i_chunk -= i_read;
437             if( p_sys->i_chunk <= 0 )
438             {
439                 /* read the empty line */
440                 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd );
441                 if( psz ) free( psz );
442             }
443         }
444     }
445     else if( i_read == 0 )
446     {
447         if( p_sys->b_reconnect )
448         {
449             msg_Dbg( p_access, "got disconnected, trying to reconnect" );
450             net_Close( p_sys->fd ); p_sys->fd = -1;
451             if( Connect( p_access, p_access->info.i_pos ) )
452             {
453                 msg_Dbg( p_access, "reconnection failed" );
454             }
455             else
456             {
457                 p_sys->b_reconnect = VLC_FALSE;
458                 i_read = Read( p_access, p_buffer, i_len );
459                 p_sys->b_reconnect = VLC_TRUE;
460             }
461         }
462
463         if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
464     }
465
466     return i_read;
467 }
468
469 /*****************************************************************************
470  * Seek: close and re-open a connection at the right place
471  *****************************************************************************/
472 static int Seek( access_t *p_access, int64_t i_pos )
473 {
474     access_sys_t *p_sys = p_access->p_sys;
475
476     msg_Dbg( p_access, "trying to seek to "I64Fd, i_pos );
477
478     net_Close( p_sys->fd ); p_sys->fd = -1;
479
480     if( Connect( p_access, i_pos ) )
481     {
482         msg_Err( p_access, "seek failed" );
483         p_access->info.b_eof = VLC_TRUE;
484         return VLC_EGENERIC;
485     }
486     return VLC_SUCCESS;
487 }
488
489 /*****************************************************************************
490  * Control:
491  *****************************************************************************/
492 static int Control( access_t *p_access, int i_query, va_list args )
493 {
494     access_sys_t *p_sys = p_access->p_sys;
495     vlc_bool_t   *pb_bool;
496     int          *pi_int;
497     int64_t      *pi_64;
498
499     switch( i_query )
500     {
501         /* */
502         case ACCESS_CAN_SEEK:
503             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
504             *pb_bool = p_sys->b_seekable;
505             break;
506         case ACCESS_CAN_FASTSEEK:
507             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
508             *pb_bool = VLC_FALSE;
509             break;
510         case ACCESS_CAN_PAUSE:
511         case ACCESS_CAN_CONTROL_PACE:
512             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
513             *pb_bool = p_sys->b_pace_control;
514             break;
515
516         /* */
517         case ACCESS_GET_MTU:
518             pi_int = (int*)va_arg( args, int * );
519             *pi_int = 0;
520             break;
521
522         case ACCESS_GET_PTS_DELAY:
523             pi_64 = (int64_t*)va_arg( args, int64_t * );
524             *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
525             break;
526
527         /* */
528         case ACCESS_SET_PAUSE_STATE:
529             break;
530
531         case ACCESS_GET_TITLE_INFO:
532         case ACCESS_SET_TITLE:
533         case ACCESS_SET_SEEKPOINT:
534         case ACCESS_SET_PRIVATE_ID_STATE:
535             return VLC_EGENERIC;
536
537         default:
538             msg_Warn( p_access, "unimplemented query in control" );
539             return VLC_EGENERIC;
540
541     }
542     return VLC_SUCCESS;
543 }
544
545 /*****************************************************************************
546  * ParseURL: extract user:password
547  *****************************************************************************/
548 static void ParseURL( access_sys_t *p_sys, char *psz_url )
549 {
550     char *psz_dup = strdup( psz_url );
551     char *p = psz_dup;
552     char *psz;
553
554     /* Syntax //[user:password]@<hostname>[:<port>][/<path>] */
555     while( *p == '/' )
556     {
557         p++;
558     }
559     psz = p;
560
561     /* Parse auth */
562     if( ( p = strchr( psz, '@' ) ) )
563     {
564         char *comma;
565
566         *p++ = '\0';
567         comma = strchr( psz, ':' );
568
569         /* Retreive user:password */
570         if( comma )
571         {
572             *comma++ = '\0';
573
574             p_sys->psz_user = strdup( psz );
575             p_sys->psz_passwd = strdup( comma );
576         }
577         else
578         {
579             p_sys->psz_user = strdup( psz );
580         }
581     }
582     else
583     {
584         p = psz;
585     }
586
587     /* Parse uri */
588     vlc_UrlParse( &p_sys->url, p, 0 );
589
590     free( psz_dup );
591 }
592
593 /*****************************************************************************
594  * Connect:
595  *****************************************************************************/
596 static int Connect( access_t *p_access, int64_t i_tell )
597 {
598     access_sys_t   *p_sys = p_access->p_sys;
599     vlc_url_t      srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
600     char           *psz;
601
602     /* Clean info */
603     if( p_sys->psz_location ) free( p_sys->psz_location );
604     if( p_sys->psz_mime ) free( p_sys->psz_mime );
605
606     p_sys->psz_location = NULL;
607     p_sys->psz_mime = NULL;
608     p_sys->b_chunked = VLC_FALSE;
609     p_sys->i_chunk = 0;
610
611     p_access->info.i_size = 0;
612     p_access->info.i_pos  = i_tell;
613     p_access->info.b_eof  = VLC_FALSE;
614
615
616     /* Open connection */
617     p_sys->fd = net_OpenTCP( p_access, srv.psz_host, srv.i_port );
618     if( p_sys->fd < 0 )
619     {
620         msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
621         return VLC_EGENERIC;
622     }
623
624     if( p_sys->b_proxy )
625     {
626         if( p_sys->url.psz_path )
627         {
628             net_Printf( VLC_OBJECT(p_access), p_sys->fd,
629                         "GET http://%s:%d/%s HTTP/1.%d\r\n",
630                         p_sys->url.psz_host, p_sys->url.i_port,
631                         p_sys->url.psz_path, p_sys->i_version );
632         }
633         else
634         {
635             net_Printf( VLC_OBJECT(p_access), p_sys->fd,
636                         "GET http://%s:%d/ HTTP/1.%d\r\n",
637                         p_sys->url.psz_host, p_sys->url.i_port,
638                         p_sys->i_version );
639
640         }
641     }
642     else
643     {
644         char *psz_path = p_sys->url.psz_path;
645         if( !psz_path || !*psz_path )
646         {
647             psz_path = "/";
648         }
649         if( p_sys->url.i_port != 80)
650         {
651             net_Printf( VLC_OBJECT(p_access), p_sys->fd,
652                         "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
653                         psz_path, p_sys->i_version, p_sys->url.psz_host,
654                         p_sys->url.i_port );
655         }
656         else
657         {        
658             net_Printf( VLC_OBJECT(p_access), p_sys->fd,
659                         "GET %s HTTP/1.%d\r\nHost: %s\r\n",
660                         psz_path, p_sys->i_version, p_sys->url.psz_host );
661         }
662     }
663     /* User Agent */
664     net_Printf( VLC_OBJECT(p_access), p_sys->fd, "User-Agent: %s\r\n",
665                 p_sys->psz_user_agent );
666     /* Offset */
667     if( p_sys->i_version == 1 )
668     {
669         net_Printf( VLC_OBJECT(p_access), p_sys->fd,
670                     "Range: bytes="I64Fd"-\r\n", i_tell );
671     }
672     /* Authentification */
673     if( p_sys->psz_user && *p_sys->psz_user )
674     {
675         char *buf;
676         char *b64;
677
678         asprintf( &buf, "%s:%s", p_sys->psz_user,
679                    p_sys->psz_passwd ? p_sys->psz_passwd : "" );
680
681         b64 = b64_encode( buf );
682
683         net_Printf( VLC_OBJECT(p_access), p_sys->fd,
684                     "Authorization: Basic %s\r\n", b64 );
685         free( b64 );
686     }
687     net_Printf( VLC_OBJECT(p_access), p_sys->fd, "Connection: Close\r\n" );
688
689     if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, "\r\n" ) < 0 )
690     {
691         msg_Err( p_access, "failed to send request" );
692         net_Close( p_sys->fd ); p_sys->fd = -1;
693         return VLC_EGENERIC;
694     }
695
696     /* Read Answer */
697     if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd ) ) == NULL )
698     {
699         msg_Err( p_access, "failed to read answer" );
700         goto error;
701     }
702     if( !strncmp( psz, "HTTP/1.", 7 ) )
703     {
704         p_sys->psz_protocol = "HTTP";
705         p_sys->i_code = atoi( &psz[9] );
706     }
707     else if( !strncmp( psz, "ICY", 3 ) )
708     {
709         p_sys->psz_protocol = "ICY";
710         p_sys->i_code = atoi( &psz[4] );
711         p_sys->b_reconnect = VLC_TRUE;
712     }
713     else
714     {
715         msg_Err( p_access, "invalid HTTP reply '%s'", psz );
716         free( psz );
717         goto error;
718     }
719     msg_Dbg( p_access, "protocol '%s' answer code %d",
720              p_sys->psz_protocol, p_sys->i_code );
721     if( !strcmp( p_sys->psz_protocol, "ICY" ) )
722     {
723         p_sys->b_seekable = VLC_FALSE;
724     }
725     if( p_sys->i_code != 206 )
726     {
727         p_sys->b_seekable = VLC_FALSE;
728     }
729     if( p_sys->i_code >= 400 )
730     {
731         msg_Err( p_access, "error: %s", psz );
732         free( psz );
733         goto error;
734     }
735     free( psz );
736
737     for( ;; )
738     {
739         char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd );
740         char *p;
741
742         if( psz == NULL )
743         {
744             msg_Err( p_access, "failed to read answer" );
745             goto error;
746         }
747
748         /* msg_Dbg( p_input, "Line=%s", psz ); */
749         if( *psz == '\0' )
750         {
751             free( psz );
752             break;
753         }
754
755
756         if( ( p = strchr( psz, ':' ) ) == NULL )
757         {
758             msg_Err( p_access, "malformed header line: %s", psz );
759             free( psz );
760             goto error;
761         }
762         *p++ = '\0';
763         while( *p == ' ' ) p++;
764
765         if( !strcasecmp( psz, "Content-Length" ) )
766         {
767             p_access->info.i_size = i_tell + atoll( p );
768             msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
769         }
770         else if( !strcasecmp( psz, "Location" ) )
771         {
772             if( p_sys->psz_location ) free( p_sys->psz_location );
773             p_sys->psz_location = strdup( p );
774         }
775         else if( !strcasecmp( psz, "Content-Type" ) )
776         {
777             if( p_sys->psz_mime ) free( p_sys->psz_mime );
778             p_sys->psz_mime = strdup( p );
779             msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
780         }
781         else if( !strcasecmp( psz, "Server" ) &&
782                  !strncasecmp( p, "Icecast", 7 ) )
783         {
784             p_sys->b_reconnect = VLC_TRUE;
785             msg_Dbg( p_access, "Server: %s", p );
786         }
787         else if( !strcasecmp( psz, "Transfer-Encoding" ) )
788         {
789             msg_Dbg( p_access, "Transfer-Encoding: %s", p );
790             if( !strncasecmp( p, "chunked", 7 ) )
791             {
792                 p_sys->b_chunked = VLC_TRUE;
793             }
794         }
795
796         free( psz );
797     }
798     return VLC_SUCCESS;
799
800 error:
801     net_Close( p_sys->fd ); p_sys->fd = -1;
802     return VLC_EGENERIC;
803 }
804
805 /*****************************************************************************
806  * b64_encode:
807  *****************************************************************************/
808 static char *b64_encode( unsigned char *src )
809 {
810     static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
811
812     char *dst = malloc( strlen( src ) * 4 / 3 + 12 );
813     char *ret = dst;
814     unsigned i_bits = 0;
815     unsigned i_shift = 0;
816
817     for( ;; )
818     {
819         if( *src )
820         {
821             i_bits = ( i_bits << 8 )|( *src++ );
822             i_shift += 8;
823         }
824         else if( i_shift > 0 )
825         {
826            i_bits <<= 6 - i_shift;
827            i_shift = 6;
828         }
829         else
830         {
831             *dst++ = '=';
832             break;
833         }
834
835         while( i_shift >= 6 )
836         {
837             i_shift -= 6;
838             *dst++ = b64[(i_bits >> i_shift)&0x3f];
839         }
840     }
841
842     *dst++ = '\0';
843
844     return ret;
845 }