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