]> git.sesse.net Git - vlc/blob - modules/access/http.c
Improvements to preferences
[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 "vlc_meta.h"
35 #include "network.h"
36
37 /*****************************************************************************
38  * Module descriptor
39  *****************************************************************************/
40 static int  Open ( vlc_object_t * );
41 static void Close( vlc_object_t * );
42
43 #define PROXY_TEXT N_("HTTP proxy")
44 #define PROXY_LONGTEXT N_( \
45     "You can specify an HTTP proxy to use. It must be of the form " \
46     "http://myproxy.mydomain:myport/. If none is specified, the HTTP_PROXY " \
47     "environment variable will be tried." )
48
49 #define CACHING_TEXT N_("Caching value in ms")
50 #define CACHING_LONGTEXT N_( \
51     "Allows you to modify the default caching value for http streams. This " \
52     "value should be set in millisecond units." )
53
54 #define USER_TEXT N_("HTTP user name")
55 #define USER_LONGTEXT N_("Allows you to modify the user name that will " \
56     "be used for the connection (Basic authentication only).")
57
58 #define PASS_TEXT N_("HTTP password")
59 #define PASS_LONGTEXT N_("Allows you to modify the password that will be " \
60     "used for the connection.")
61
62 #define AGENT_TEXT N_("HTTP user agent")
63 #define AGENT_LONGTEXT N_("Allows you to modify the user agent that will be " \
64     "used for the connection.")
65
66 #define RECONNECT_TEXT N_("Auto re-connect")
67 #define RECONNECT_LONGTEXT N_("Will automatically attempt a re-connection " \
68     "in case it was untimely closed.")
69
70 #define CONTINUOUS_TEXT N_("Continuous stream")
71 #define CONTINUOUS_LONGTEXT N_("Enable this option to read a file that is " \
72     "being constantly updated (for example, a JPG file on a server)")
73
74 vlc_module_begin();
75     set_description( _("HTTP input") );
76     set_capability( "access2", 0 );
77     set_name( _( "HTTP/HTTPS" ) );
78     set_category( CAT_INPUT );
79     set_subcategory( SUBCAT_INPUT_ACCESS );
80
81     add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
82                 VLC_FALSE );
83     add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
84                  CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
85     add_string( "http-user", NULL, NULL, USER_TEXT, USER_LONGTEXT, VLC_FALSE );
86     add_string( "http-pwd", NULL , NULL, PASS_TEXT, PASS_LONGTEXT, VLC_FALSE );
87     add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
88                 AGENT_LONGTEXT, VLC_FALSE );
89     add_bool( "http-reconnect", 0, NULL, RECONNECT_TEXT,
90               RECONNECT_LONGTEXT, VLC_TRUE );
91     add_bool( "http-continuous", 0, NULL, CONTINUOUS_TEXT,
92               CONTINUOUS_LONGTEXT, VLC_TRUE );
93
94     add_shortcut( "http" );
95     add_shortcut( "http4" );
96     add_shortcut( "http6" );
97     add_shortcut( "unsv" );
98     set_callbacks( Open, Close );
99 vlc_module_end();
100
101 /*****************************************************************************
102  * Local prototypes
103  *****************************************************************************/
104 struct access_sys_t
105 {
106     int fd;
107
108     /* From uri */
109     vlc_url_t url;
110     char    *psz_user;
111     char    *psz_passwd;
112     char    *psz_user_agent;
113
114     /* Proxy */
115     vlc_bool_t b_proxy;
116     vlc_url_t  proxy;
117
118     /* */
119     int        i_code;
120     char       *psz_protocol;
121     int        i_version;
122
123     char       *psz_mime;
124     char       *psz_pragma;
125     char       *psz_location;
126     vlc_bool_t b_mms;
127     vlc_bool_t b_icecast;
128
129     vlc_bool_t b_chunked;
130     int64_t    i_chunk;
131
132     int        i_icy_meta;
133     char       *psz_icy_name;
134     char       *psz_icy_genre;
135     char       *psz_icy_title;
136
137     int i_remaining;
138
139     vlc_bool_t b_seekable;
140     vlc_bool_t b_reconnect;
141     vlc_bool_t b_continuous;
142     vlc_bool_t b_pace_control;
143 };
144
145 /* */
146 static int Read( access_t *, uint8_t *, int );
147 static int Seek( access_t *, int64_t );
148 static int Control( access_t *, int, va_list );
149
150 /* */
151 static void ParseURL( access_sys_t *, char *psz_url );
152 static int  Connect( access_t *, int64_t );
153 static int Request( access_t *p_access, int64_t i_tell );
154
155 /*****************************************************************************
156  * Open:
157  *****************************************************************************/
158 static int Open( vlc_object_t *p_this )
159 {
160     access_t     *p_access = (access_t*)p_this;
161     access_sys_t *p_sys;
162     char         *psz;
163
164     /* First set ipv4/ipv6 */
165     var_Create( p_access, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
166     var_Create( p_access, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
167
168     if( *p_access->psz_access )
169     {
170         vlc_value_t val;
171         /* Find out which shortcut was used */
172         if( !strncmp( p_access->psz_access, "http4", 6 ) )
173         {
174             val.b_bool = VLC_TRUE;
175             var_Set( p_access, "ipv4", val );
176
177             val.b_bool = VLC_FALSE;
178             var_Set( p_access, "ipv6", val );
179         }
180         else if( !strncmp( p_access->psz_access, "http6", 6 ) )
181         {
182             val.b_bool = VLC_TRUE;
183             var_Set( p_access, "ipv6", val );
184
185             val.b_bool = VLC_FALSE;
186             var_Set( p_access, "ipv4", val );
187         }
188     }
189
190     /* Set up p_access */
191     p_access->pf_read = Read;
192     p_access->pf_block = NULL;
193     p_access->pf_control = Control;
194     p_access->pf_seek = Seek;
195     p_access->info.i_update = 0;
196     p_access->info.i_size = 0;
197     p_access->info.i_pos = 0;
198     p_access->info.b_eof = VLC_FALSE;
199     p_access->info.i_title = 0;
200     p_access->info.i_seekpoint = 0;
201     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
202     memset( p_sys, 0, sizeof( access_sys_t ) );
203     p_sys->fd = -1;
204     p_sys->b_proxy = VLC_FALSE;
205     p_sys->i_version = 1;
206     p_sys->b_seekable = VLC_TRUE;
207     p_sys->psz_mime = NULL;
208     p_sys->psz_pragma = NULL;
209     p_sys->b_mms = VLC_FALSE;
210     p_sys->b_icecast = VLC_FALSE;
211     p_sys->psz_location = NULL;
212     p_sys->psz_user_agent = NULL;
213     p_sys->b_pace_control = VLC_TRUE;
214     p_sys->i_icy_meta = 0;
215     p_sys->psz_icy_name = NULL;
216     p_sys->psz_icy_genre = NULL;
217     p_sys->psz_icy_title = NULL;
218     p_sys->i_remaining = 0;
219
220     /* Parse URI */
221     ParseURL( p_sys, p_access->psz_path );
222     if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
223     {
224         msg_Warn( p_access, "invalid host" );
225         goto error;
226     }
227     if( p_sys->url.i_port <= 0 )
228     {
229         p_sys->url.i_port = 80;
230     }
231     if( !p_sys->psz_user || *p_sys->psz_user == '\0' )
232     {
233         p_sys->psz_user = var_CreateGetString( p_access, "http-user" );
234         p_sys->psz_passwd = var_CreateGetString( p_access, "http-pwd" );
235     }
236
237     /* Do user agent */
238     p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
239
240     /* Check proxy */
241     psz = var_CreateGetString( p_access, "http-proxy" );
242     if( *psz )
243     {
244         p_sys->b_proxy = VLC_TRUE;
245         vlc_UrlParse( &p_sys->proxy, psz, 0 );
246     }
247     else
248     {
249         char *psz_proxy = getenv( "http_proxy" );
250         if( psz_proxy && *psz_proxy )
251         {
252             p_sys->b_proxy = VLC_TRUE;
253             vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
254         }
255         if( psz_proxy )
256             free( psz_proxy );
257     }
258     free( psz );
259
260     if( p_sys->b_proxy )
261     {
262         if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
263         {
264             msg_Warn( p_access, "invalid proxy host" );
265             goto error;
266         }
267         if( p_sys->proxy.i_port <= 0 )
268         {
269             p_sys->proxy.i_port = 80;
270         }
271     }
272
273     msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
274              p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
275     if( p_sys->b_proxy )
276     {
277         msg_Dbg( p_access, "      proxy %s:%d", p_sys->proxy.psz_host,
278                  p_sys->proxy.i_port );
279     }
280     if( p_sys->psz_user && *p_sys->psz_user )
281     {
282         msg_Dbg( p_access, "      user='%s', pwd='%s'",
283                  p_sys->psz_user, p_sys->psz_passwd );
284     }
285
286     p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
287     p_sys->b_continuous = var_CreateGetBool( p_access, "http-continuous" );
288
289     /* Connect */
290     if( Connect( p_access, 0 ) )
291     {
292         /* Retry with http 1.0 */
293         p_sys->i_version = 0;
294
295         if( p_access->b_die ||
296             Connect( p_access, 0 ) )
297         {
298             goto error;
299         }
300     }
301
302     if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
303           p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
304         p_sys->psz_location && *p_sys->psz_location )
305     {
306         playlist_t * p_playlist;
307
308         msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
309
310         p_playlist = vlc_object_find( p_access, VLC_OBJECT_PLAYLIST,
311                                       FIND_ANYWHERE );
312         if( !p_playlist )
313         {
314             msg_Err( p_access, "redirection failed: can't find playlist" );
315             goto error;
316         }
317         p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
318         playlist_Add( p_playlist, p_sys->psz_location, p_sys->psz_location,
319                       PLAYLIST_INSERT,
320                       p_playlist->i_index + 1 );
321         vlc_object_release( p_playlist );
322
323         p_access->info.i_size = 0;  /* Force to stop reading */
324     }
325
326     if( p_sys->b_mms )
327     {
328         msg_Dbg( p_access, "This is actually a live mms server, BAIL" );
329         goto error;
330     }
331
332     if( p_sys->b_icecast )
333     {
334         if( p_sys->psz_mime && !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
335             p_access->psz_demux = strdup( "mp3" );
336     }
337
338     if( !strcmp( p_sys->psz_protocol, "ICY" ) )
339     {
340         if( p_sys->psz_mime && !strcasecmp( p_sys->psz_mime, "video/nsv" ) )
341             p_access->psz_demux = strdup( "nsv" );
342         else if( p_sys->psz_mime &&
343                  ( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
344                    !strcasecmp( p_sys->psz_mime, "audio/aacp" ) ) )
345             p_access->psz_demux = strdup( "m4a" );
346         else
347             p_access->psz_demux = strdup( "mp3" );
348
349         msg_Info( p_access, "ICY server found, %s demuxer selected",
350                   p_access->psz_demux );
351
352 #if 0   /* Doesn't work really well because of the pre-buffering in shoutcast
353          * servers (the buffer content will be sent as fast as possible). */
354         p_sys->b_pace_control = VLC_FALSE;
355 #endif
356     }
357
358     if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
359
360     /* PTS delay */
361     var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
362
363     return VLC_SUCCESS;
364
365 error:
366     vlc_UrlClean( &p_sys->url );
367     vlc_UrlClean( &p_sys->proxy );
368     if( p_sys->psz_mime ) free( p_sys->psz_mime );
369     if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
370     if( p_sys->psz_location ) free( p_sys->psz_location );
371     if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
372     if( p_sys->psz_user ) free( p_sys->psz_user );
373     if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
374
375     if( p_sys->fd > 0 )
376     {
377         net_Close( p_sys->fd );
378     }
379     free( p_sys );
380     return VLC_EGENERIC;
381 }
382
383 /*****************************************************************************
384  * Close:
385  *****************************************************************************/
386 static void Close( vlc_object_t *p_this )
387 {
388     access_t     *p_access = (access_t*)p_this;
389     access_sys_t *p_sys = p_access->p_sys;
390
391     vlc_UrlClean( &p_sys->url );
392     vlc_UrlClean( &p_sys->proxy );
393
394     if( p_sys->psz_user ) free( p_sys->psz_user );
395     if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
396
397     if( p_sys->psz_mime ) free( p_sys->psz_mime );
398     if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
399     if( p_sys->psz_location ) free( p_sys->psz_location );
400
401     if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
402     if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
403     if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
404
405     if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
406
407     if( p_sys->fd > 0 )
408     {
409         net_Close( p_sys->fd );
410     }
411     free( p_sys );
412 }
413
414 /*****************************************************************************
415  * Read: Read up to i_len bytes from the http connection and place in
416  * p_buffer. Return the actual number of bytes read
417  *****************************************************************************/
418 static int ReadICYMeta( access_t *p_access );
419 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
420 {
421     access_sys_t *p_sys = p_access->p_sys;
422     int i_read;
423
424     if( p_sys->fd < 0 )
425     {
426         p_access->info.b_eof = VLC_TRUE;
427         return 0;
428     }
429
430     if( p_access->info.i_size > 0 &&
431         i_len + p_access->info.i_pos > p_access->info.i_size )
432     {
433         if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
434         {
435             p_access->info.b_eof = VLC_TRUE;
436             return 0;
437         }
438     }
439
440     if( p_sys->b_chunked )
441     {
442         if( p_sys->i_chunk < 0 )
443         {
444             p_access->info.b_eof = VLC_TRUE;
445             return 0;
446         }
447
448         if( p_sys->i_chunk <= 0 )
449         {
450             char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
451             /* read the chunk header */
452             if( psz == NULL )
453             {
454                 msg_Dbg( p_access, "failed reading chunk-header line" );
455                 return -1;
456             }
457             p_sys->i_chunk = strtoll( psz, NULL, 16 );
458             free( psz );
459
460             if( p_sys->i_chunk <= 0 )   /* eof */
461             {
462                 p_sys->i_chunk = -1;
463                 p_access->info.b_eof = VLC_TRUE;
464                 return 0;
465             }
466         }
467
468         if( i_len > p_sys->i_chunk )
469         {
470             i_len = p_sys->i_chunk;
471         }
472     }
473
474     if( p_sys->b_continuous && i_len > p_sys->i_remaining )
475     {
476         /* Only ask for the remaining length */
477         int i_new_len = p_sys->i_remaining;
478         if( i_new_len == 0 )
479         {
480             Request( p_access, 0 );
481             i_read = Read( p_access, p_buffer, i_len );
482             return i_read;
483         }
484         i_len = i_new_len;
485     }
486
487     if( p_sys->i_icy_meta > 0 && p_access->info.i_pos > 0 )
488     {
489         int64_t i_next = p_sys->i_icy_meta -
490                                     p_access->info.i_pos % p_sys->i_icy_meta;
491
492         if( i_next == p_sys->i_icy_meta )
493         {
494             if( ReadICYMeta( p_access ) )
495             {
496                 p_access->info.b_eof = VLC_TRUE;
497                 return -1;
498             }
499         }
500         if( i_len > i_next )
501             i_len = i_next;
502     }
503
504     i_read = net_Read( p_access, p_sys->fd, NULL, p_buffer, i_len, VLC_FALSE );
505
506     if( i_read > 0 )
507     {
508         p_access->info.i_pos += i_read;
509
510         if( p_sys->b_chunked )
511         {
512             p_sys->i_chunk -= i_read;
513             if( p_sys->i_chunk <= 0 )
514             {
515                 /* read the empty line */
516                 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
517                 if( psz ) free( psz );
518             }
519         }
520     }
521     else if( i_read == 0 )
522     {
523         if( p_sys->b_continuous )
524         {
525             Request( p_access, 0 );
526             p_sys->b_continuous = VLC_FALSE;
527             i_read = Read( p_access, p_buffer, i_len );
528             p_sys->b_continuous = VLC_TRUE;
529         }
530         if( p_sys->b_reconnect )
531         {
532             msg_Dbg( p_access, "got disconnected, trying to reconnect" );
533             net_Close( p_sys->fd ); p_sys->fd = -1;
534             if( Connect( p_access, p_access->info.i_pos ) )
535             {
536                 msg_Dbg( p_access, "reconnection failed" );
537             }
538             else
539             {
540                 p_sys->b_reconnect = VLC_FALSE;
541                 i_read = Read( p_access, p_buffer, i_len );
542                 p_sys->b_reconnect = VLC_TRUE;
543             }
544         }
545
546         if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
547     }
548
549     if( p_sys->b_continuous )
550     {
551         p_sys->i_remaining -= i_read;
552     }
553
554     return i_read;
555 }
556
557 static int ReadICYMeta( access_t *p_access )
558 {
559     access_sys_t *p_sys = p_access->p_sys;
560
561     uint8_t buffer[1];
562     char *psz_meta;
563     int i_read;
564     char *p;
565
566     /* Read meta data length */
567     i_read = net_Read( p_access, p_sys->fd, NULL, buffer, 1, VLC_TRUE );
568     if( i_read <= 0 )
569         return VLC_EGENERIC;
570
571
572     if( buffer[0] <= 0 )
573         return VLC_SUCCESS;
574
575     msg_Dbg( p_access, "ICY meta size=%d", buffer[0] * 16);
576
577     psz_meta = malloc( buffer[0] * 16 + 1 );
578     i_read = net_Read( p_access, p_sys->fd, NULL,
579                        psz_meta, buffer[0] * 16, VLC_TRUE );
580
581     if( i_read != buffer[0] * 16 )
582         return VLC_EGENERIC;
583
584     psz_meta[buffer[0]*16 + 1] = '\0'; /* Just in case */
585
586     msg_Warn( p_access, "icy-meta=%s", psz_meta );
587
588     /* Now parse the meta */
589     /* Look for StreamTitle= */
590     p = strcasestr( psz_meta, "StreamTitle=" );
591     if( p )
592     {
593         p += strlen( "StreamTitle=" );
594         if( *p == '\'' || *p == '"' )
595         {
596             char *psz = strchr( &p[1], p[0] );
597             if( !psz )
598                 psz = strchr( &p[1], ';' );
599
600             if( psz ) *psz = '\0';
601         }
602         else
603         {
604             char *psz = strchr( &p[1], ';' );
605             if( psz ) *psz = '\0';
606         }
607
608         if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
609
610         p_sys->psz_icy_title = strdup( &p[1] );
611
612         p_access->info.i_update |= INPUT_UPDATE_META;
613     }
614
615     free( psz_meta );
616
617     msg_Warn( p_access, "New Title=%s", p_sys->psz_icy_title );
618
619     return VLC_SUCCESS;
620 }
621
622 /*****************************************************************************
623  * Seek: close and re-open a connection at the right place
624  *****************************************************************************/
625 static int Seek( access_t *p_access, int64_t i_pos )
626 {
627     access_sys_t *p_sys = p_access->p_sys;
628
629     msg_Dbg( p_access, "trying to seek to "I64Fd, i_pos );
630
631     net_Close( p_sys->fd ); p_sys->fd = -1;
632
633     if( Connect( p_access, i_pos ) )
634     {
635         msg_Err( p_access, "seek failed" );
636         p_access->info.b_eof = VLC_TRUE;
637         return VLC_EGENERIC;
638     }
639     return VLC_SUCCESS;
640 }
641
642 /*****************************************************************************
643  * Control:
644  *****************************************************************************/
645 static int Control( access_t *p_access, int i_query, va_list args )
646 {
647     access_sys_t *p_sys = p_access->p_sys;
648     vlc_bool_t   *pb_bool;
649     int          *pi_int;
650     int64_t      *pi_64;
651     vlc_meta_t **pp_meta;
652
653     switch( i_query )
654     {
655         /* */
656         case ACCESS_CAN_SEEK:
657             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
658             *pb_bool = p_sys->b_seekable;
659             break;
660         case ACCESS_CAN_FASTSEEK:
661             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
662             *pb_bool = VLC_FALSE;
663             break;
664         case ACCESS_CAN_PAUSE:
665         case ACCESS_CAN_CONTROL_PACE:
666             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
667             *pb_bool = p_sys->b_pace_control;
668             break;
669
670         /* */
671         case ACCESS_GET_MTU:
672             pi_int = (int*)va_arg( args, int * );
673             *pi_int = 0;
674             break;
675
676         case ACCESS_GET_PTS_DELAY:
677             pi_64 = (int64_t*)va_arg( args, int64_t * );
678             *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
679             break;
680
681         /* */
682         case ACCESS_SET_PAUSE_STATE:
683             break;
684
685         case ACCESS_GET_META:
686             pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
687             *pp_meta = vlc_meta_New();
688             msg_Dbg( p_access, "GET META %s %s %s",
689                      p_sys->psz_icy_name, p_sys->psz_icy_genre, p_sys->psz_icy_title );
690             if( p_sys->psz_icy_name )
691                 vlc_meta_Add( *pp_meta, VLC_META_DESCRIPTION,
692                               p_sys->psz_icy_name );
693             if( p_sys->psz_icy_genre )
694                 vlc_meta_Add( *pp_meta, VLC_META_GENRE,
695                               p_sys->psz_icy_genre );
696             if( p_sys->psz_icy_title )
697                 vlc_meta_Add( *pp_meta, VLC_META_TITLE,
698                               p_sys->psz_icy_title );
699             break;
700
701         case ACCESS_GET_TITLE_INFO:
702         case ACCESS_SET_TITLE:
703         case ACCESS_SET_SEEKPOINT:
704         case ACCESS_SET_PRIVATE_ID_STATE:
705             return VLC_EGENERIC;
706
707         default:
708             msg_Warn( p_access, "unimplemented query in control" );
709             return VLC_EGENERIC;
710
711     }
712     return VLC_SUCCESS;
713 }
714
715 /*****************************************************************************
716  * ParseURL: extract user:password
717  *****************************************************************************/
718 static void ParseURL( access_sys_t *p_sys, char *psz_url )
719 {
720     char *psz_dup = strdup( psz_url );
721     char *p = psz_dup;
722     char *psz;
723
724     /* Syntax //[user:password]@<hostname>[:<port>][/<path>] */
725     while( *p == '/' )
726     {
727         p++;
728     }
729     psz = p;
730
731     /* Parse auth */
732     if( ( p = strchr( psz, '@' ) ) )
733     {
734         char *comma;
735
736         *p++ = '\0';
737         comma = strchr( psz, ':' );
738
739         /* Retreive user:password */
740         if( comma )
741         {
742             *comma++ = '\0';
743
744             p_sys->psz_user = strdup( psz );
745             p_sys->psz_passwd = strdup( comma );
746         }
747         else
748         {
749             p_sys->psz_user = strdup( psz );
750         }
751     }
752     else
753     {
754         p = psz;
755     }
756
757     /* Parse uri */
758     vlc_UrlParse( &p_sys->url, p, 0 );
759
760     free( psz_dup );
761 }
762
763 /*****************************************************************************
764  * Connect:
765  *****************************************************************************/
766 static int Connect( access_t *p_access, int64_t i_tell )
767 {
768     access_sys_t   *p_sys = p_access->p_sys;
769     vlc_url_t      srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
770     char           *psz;
771
772     /* Clean info */
773     if( p_sys->psz_location ) free( p_sys->psz_location );
774     if( p_sys->psz_mime ) free( p_sys->psz_mime );
775     if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
776
777     if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
778     if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
779     if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
780
781
782     p_sys->psz_location = NULL;
783     p_sys->psz_mime = NULL;
784     p_sys->psz_pragma = NULL;
785     p_sys->b_mms = VLC_FALSE;
786     p_sys->b_chunked = VLC_FALSE;
787     p_sys->i_chunk = 0;
788     p_sys->i_icy_meta = 0;
789     p_sys->psz_icy_name = NULL;
790     p_sys->psz_icy_genre = NULL;
791     p_sys->psz_icy_title = NULL;
792
793     p_access->info.i_size = 0;
794     p_access->info.i_pos  = i_tell;
795     p_access->info.b_eof  = VLC_FALSE;
796
797
798     /* Open connection */
799     p_sys->fd = net_OpenTCP( p_access, srv.psz_host, srv.i_port );
800     if( p_sys->fd < 0 )
801     {
802         msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
803         return VLC_EGENERIC;
804     }
805
806     return Request( p_access,i_tell );
807 }
808
809
810 static int Request( access_t *p_access, int64_t i_tell )
811 {
812     access_sys_t   *p_sys = p_access->p_sys;
813     char           *psz;
814     if( p_sys->b_proxy )
815     {
816         if( p_sys->url.psz_path )
817         {
818             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
819                         "GET http://%s:%d%s HTTP/1.%d\r\n",
820                         p_sys->url.psz_host, p_sys->url.i_port,
821                         p_sys->url.psz_path, p_sys->i_version );
822         }
823         else
824         {
825             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
826                         "GET http://%s:%d/ HTTP/1.%d\r\n",
827                         p_sys->url.psz_host, p_sys->url.i_port,
828                         p_sys->i_version );
829         }
830     }
831     else
832     {
833         char *psz_path = p_sys->url.psz_path;
834         if( !psz_path || !*psz_path )
835         {
836             psz_path = "/";
837         }
838         if( p_sys->url.i_port != 80)
839         {
840             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
841                         "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
842                         psz_path, p_sys->i_version, p_sys->url.psz_host,
843                         p_sys->url.i_port );
844         }
845         else
846         {
847             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
848                         "GET %s HTTP/1.%d\r\nHost: %s\r\n",
849                         psz_path, p_sys->i_version, p_sys->url.psz_host );
850         }
851     }
852     /* User Agent */
853     net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "User-Agent: %s\r\n",
854                 p_sys->psz_user_agent );
855     /* Offset */
856     if( p_sys->i_version == 1 )
857     {
858         net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
859                     "Range: bytes="I64Fd"-\r\n", i_tell );
860     }
861
862     /* Authentification */
863     if( p_sys->psz_user && *p_sys->psz_user )
864     {
865         char *buf;
866         char *b64;
867
868         asprintf( &buf, "%s:%s", p_sys->psz_user,
869                    p_sys->psz_passwd ? p_sys->psz_passwd : "" );
870
871         b64 = vlc_b64_encode( buf );
872
873         net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
874                     "Authorization: Basic %s\r\n", b64 );
875         free( b64 );
876     }
877
878     /* ICY meta data request */
879     net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "Icy-MetaData: 1\r\n" );
880
881
882     if( p_sys->b_continuous && p_sys->i_version == 1 )
883     {
884         net_Printf( VLC_OBJECT( p_access ), p_sys->fd, NULL,
885                     "Connection: keep-alive\r\n" );
886     }
887     else
888     {
889         net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
890                     "Connection: Close\r\n");
891         p_sys->b_continuous = VLC_FALSE;
892     }
893
894     if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "\r\n" ) < 0 )
895     {
896         msg_Err( p_access, "failed to send request" );
897         net_Close( p_sys->fd ); p_sys->fd = -1;
898         return VLC_EGENERIC;
899     }
900
901     /* Read Answer */
902     if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL ) ) == NULL )
903     {
904         msg_Err( p_access, "failed to read answer" );
905         goto error;
906     }
907     if( !strncmp( psz, "HTTP/1.", 7 ) )
908     {
909         p_sys->psz_protocol = "HTTP";
910         p_sys->i_code = atoi( &psz[9] );
911     }
912     else if( !strncmp( psz, "ICY", 3 ) )
913     {
914         p_sys->psz_protocol = "ICY";
915         p_sys->i_code = atoi( &psz[4] );
916         p_sys->b_reconnect = VLC_TRUE;
917     }
918     else
919     {
920         msg_Err( p_access, "invalid HTTP reply '%s'", psz );
921         free( psz );
922         goto error;
923     }
924     msg_Dbg( p_access, "protocol '%s' answer code %d",
925              p_sys->psz_protocol, p_sys->i_code );
926     if( !strcmp( p_sys->psz_protocol, "ICY" ) )
927     {
928         p_sys->b_seekable = VLC_FALSE;
929     }
930     if( p_sys->i_code != 206 )
931     {
932         p_sys->b_seekable = VLC_FALSE;
933     }
934     if( p_sys->i_code >= 400 )
935     {
936         msg_Err( p_access, "error: %s", psz );
937         free( psz );
938         goto error;
939     }
940     free( psz );
941
942     for( ;; )
943     {
944         char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
945         char *p;
946
947         if( psz == NULL )
948         {
949             msg_Err( p_access, "failed to read answer" );
950             goto error;
951         }
952
953         /* msg_Dbg( p_input, "Line=%s", psz ); */
954         if( *psz == '\0' )
955         {
956             free( psz );
957             break;
958         }
959
960
961         if( ( p = strchr( psz, ':' ) ) == NULL )
962         {
963             msg_Err( p_access, "malformed header line: %s", psz );
964             free( psz );
965             goto error;
966         }
967         *p++ = '\0';
968         while( *p == ' ' ) p++;
969
970         if( !strcasecmp( psz, "Content-Length" ) )
971         {
972             if( p_sys->b_continuous )
973             {
974                 p_access->info.i_size = -1;
975                 msg_Dbg( p_access, "this frame size="I64Fd, atoll(p ) );
976                 p_sys->i_remaining = atoll( p );
977             }
978             else
979             {
980                 p_access->info.i_size = i_tell + atoll( p );
981                 msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
982             }
983         }
984         else if( !strcasecmp( psz, "Location" ) )
985         {
986             if( p_sys->psz_location ) free( p_sys->psz_location );
987             p_sys->psz_location = strdup( p );
988         }
989         else if( !strcasecmp( psz, "Content-Type" ) )
990         {
991             if( p_sys->psz_mime ) free( p_sys->psz_mime );
992             p_sys->psz_mime = strdup( p );
993             msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
994         }
995         else if( !strcasecmp( psz, "Pragma" ) )
996         {
997             if( !strcasecmp( psz, "Pragma: features" ) )
998                 p_sys->b_mms = VLC_TRUE;
999             if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
1000             p_sys->psz_pragma = strdup( p );
1001             msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1002         }
1003         else if( !strcasecmp( psz, "Server" ) )
1004         {
1005             msg_Dbg( p_access, "Server: %s", p );
1006             if( !strncasecmp( p, "Icecast", 7 ) ||
1007                 !strncasecmp( p, "Nanocaster", 10 ) )
1008             {
1009                 /* Remember if this is Icecast
1010                  * we need to force mp3 in some cases without breaking
1011                  *  autodetection */
1012
1013                 /* Let live 65 streams (nanocaster) piggyback on the icecast
1014                  * routine. They look very similar */
1015
1016                 p_sys->b_reconnect = VLC_TRUE;
1017                 p_sys->b_pace_control = VLC_FALSE;
1018                 p_sys->b_icecast = VLC_TRUE;
1019             }
1020         }
1021         else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1022         {
1023             msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1024             if( !strncasecmp( p, "chunked", 7 ) )
1025             {
1026                 p_sys->b_chunked = VLC_TRUE;
1027             }
1028         }
1029         else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1030         {
1031             msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1032             p_sys->i_icy_meta = atoi( p );
1033             if( p_sys->i_icy_meta < 0 )
1034                 p_sys->i_icy_meta = 0;
1035
1036             msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1037         }
1038         else if( !strcasecmp( psz, "Icy-Name" ) )
1039         {
1040             if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
1041             p_sys->psz_icy_name = strdup( p );
1042             msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1043         }
1044         else if( !strcasecmp( psz, "Icy-Genre" ) )
1045         {
1046             if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
1047             p_sys->psz_icy_genre = strdup( p );
1048             msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1049         }
1050         else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1051         {
1052             msg_Dbg( p_access, "Icy-Notice: %s", p );
1053         }
1054         else if( !strncasecmp( psz, "icy-", 4 ) ||
1055                  !strncasecmp( psz, "ice-", 4 ) ||
1056                  !strncasecmp( psz, "x-audiocast", 11 ) )
1057         {
1058             msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1059         }
1060
1061         free( psz );
1062     }
1063     return VLC_SUCCESS;
1064
1065 error:
1066     net_Close( p_sys->fd ); p_sys->fd = -1;
1067     return VLC_EGENERIC;
1068 }
1069