]> git.sesse.net Git - vlc/blob - modules/access/http.c
6e2a3a4afb9f96445ded726fff3208b58c4981c2
[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_shortname( _( "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         input_item_t *p_input_item;
308
309         msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
310
311         p_playlist = vlc_object_find( p_access, VLC_OBJECT_PLAYLIST,
312                                       FIND_ANYWHERE );
313         if( !p_playlist )
314         {
315             msg_Err( p_access, "redirection failed: can't find playlist" );
316             goto error;
317         }
318
319         /* Change the uri */
320         vlc_mutex_lock( &p_playlist->object_lock );
321         p_input_item = &p_playlist->status.p_item->input;
322         vlc_mutex_lock( &p_input_item->lock );
323         free( p_input_item->psz_uri );
324         free( p_access->psz_path );
325         p_input_item->psz_uri = strdup( p_sys->psz_location );
326         p_access->psz_path = strdup( p_sys->psz_location );
327         vlc_mutex_unlock( &p_input_item->lock );
328         vlc_mutex_unlock( &p_playlist->object_lock );
329         vlc_object_release( p_playlist );
330
331         /* Clean up current Open() run */
332         vlc_UrlClean( &p_sys->url );
333         vlc_UrlClean( &p_sys->proxy );
334         if( p_sys->psz_mime ) free( p_sys->psz_mime );
335         if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
336         if( p_sys->psz_location ) free( p_sys->psz_location );
337         if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
338         if( p_sys->psz_user ) free( p_sys->psz_user );
339         if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
340
341         if( p_sys->fd > 0 )
342         {
343             net_Close( p_sys->fd );
344         }
345         free( p_sys );
346
347         /* Do new Open() run with new data */
348         return Open( p_this );
349     }
350
351     if( p_sys->b_mms )
352     {
353         msg_Dbg( p_access, "This is actually a live mms server, BAIL" );
354         goto error;
355     }
356
357     if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
358     {
359         if( p_sys->psz_mime && !strcasecmp( p_sys->psz_mime, "video/nsv" ) )
360             p_access->psz_demux = strdup( "nsv" );
361         else if( p_sys->psz_mime &&
362                  ( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
363                    !strcasecmp( p_sys->psz_mime, "audio/aacp" ) ) )
364             p_access->psz_demux = strdup( "m4a" );
365         else if( p_sys->psz_mime && !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
366             p_access->psz_demux = strdup( "mp3" );
367         else /* assume mp3, though this is definetly not certain */
368             p_access->psz_demux = strdup( "mp3" );
369
370         msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
371                   p_access->psz_demux );
372
373 #if 0   /* Doesn't work really well because of the pre-buffering in shoutcast
374          * servers (the buffer content will be sent as fast as possible). */
375         p_sys->b_pace_control = VLC_FALSE;
376 #endif
377     }
378
379     if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
380
381     /* PTS delay */
382     var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
383
384     return VLC_SUCCESS;
385
386 error:
387     vlc_UrlClean( &p_sys->url );
388     vlc_UrlClean( &p_sys->proxy );
389     if( p_sys->psz_mime ) free( p_sys->psz_mime );
390     if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
391     if( p_sys->psz_location ) free( p_sys->psz_location );
392     if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
393     if( p_sys->psz_user ) free( p_sys->psz_user );
394     if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
395
396     if( p_sys->fd > 0 )
397     {
398         net_Close( p_sys->fd );
399     }
400     free( p_sys );
401     return VLC_EGENERIC;
402 }
403
404 /*****************************************************************************
405  * Close:
406  *****************************************************************************/
407 static void Close( vlc_object_t *p_this )
408 {
409     access_t     *p_access = (access_t*)p_this;
410     access_sys_t *p_sys = p_access->p_sys;
411
412     vlc_UrlClean( &p_sys->url );
413     vlc_UrlClean( &p_sys->proxy );
414
415     if( p_sys->psz_user ) free( p_sys->psz_user );
416     if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
417
418     if( p_sys->psz_mime ) free( p_sys->psz_mime );
419     if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
420     if( p_sys->psz_location ) free( p_sys->psz_location );
421
422     if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
423     if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
424     if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
425
426     if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
427
428     if( p_sys->fd > 0 )
429     {
430         net_Close( p_sys->fd );
431     }
432     free( p_sys );
433 }
434
435 /*****************************************************************************
436  * Read: Read up to i_len bytes from the http connection and place in
437  * p_buffer. Return the actual number of bytes read
438  *****************************************************************************/
439 static int ReadICYMeta( access_t *p_access );
440 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
441 {
442     access_sys_t *p_sys = p_access->p_sys;
443     int i_read;
444
445     if( p_sys->fd < 0 )
446     {
447         p_access->info.b_eof = VLC_TRUE;
448         return 0;
449     }
450
451     if( p_access->info.i_size > 0 &&
452         i_len + p_access->info.i_pos > p_access->info.i_size )
453     {
454         if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
455         {
456             p_access->info.b_eof = VLC_TRUE;
457             return 0;
458         }
459     }
460
461     if( p_sys->b_chunked )
462     {
463         if( p_sys->i_chunk < 0 )
464         {
465             p_access->info.b_eof = VLC_TRUE;
466             return 0;
467         }
468
469         if( p_sys->i_chunk <= 0 )
470         {
471             char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
472             /* read the chunk header */
473             if( psz == NULL )
474             {
475                 msg_Dbg( p_access, "failed reading chunk-header line" );
476                 return -1;
477             }
478             p_sys->i_chunk = strtoll( psz, NULL, 16 );
479             free( psz );
480
481             if( p_sys->i_chunk <= 0 )   /* eof */
482             {
483                 p_sys->i_chunk = -1;
484                 p_access->info.b_eof = VLC_TRUE;
485                 return 0;
486             }
487         }
488
489         if( i_len > p_sys->i_chunk )
490         {
491             i_len = p_sys->i_chunk;
492         }
493     }
494
495     if( p_sys->b_continuous && i_len > p_sys->i_remaining )
496     {
497         /* Only ask for the remaining length */
498         int i_new_len = p_sys->i_remaining;
499         if( i_new_len == 0 )
500         {
501             Request( p_access, 0 );
502             i_read = Read( p_access, p_buffer, i_len );
503             return i_read;
504         }
505         i_len = i_new_len;
506     }
507
508     if( p_sys->i_icy_meta > 0 && p_access->info.i_pos > 0 )
509     {
510         int64_t i_next = p_sys->i_icy_meta -
511                                     p_access->info.i_pos % p_sys->i_icy_meta;
512
513         if( i_next == p_sys->i_icy_meta )
514         {
515             if( ReadICYMeta( p_access ) )
516             {
517                 p_access->info.b_eof = VLC_TRUE;
518                 return -1;
519             }
520         }
521         if( i_len > i_next )
522             i_len = i_next;
523     }
524
525     i_read = net_Read( p_access, p_sys->fd, NULL, p_buffer, i_len, VLC_FALSE );
526
527     if( i_read > 0 )
528     {
529         p_access->info.i_pos += i_read;
530
531         if( p_sys->b_chunked )
532         {
533             p_sys->i_chunk -= i_read;
534             if( p_sys->i_chunk <= 0 )
535             {
536                 /* read the empty line */
537                 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
538                 if( psz ) free( psz );
539             }
540         }
541     }
542     else if( i_read == 0 )
543     {
544         if( p_sys->b_continuous )
545         {
546             Request( p_access, 0 );
547             p_sys->b_continuous = VLC_FALSE;
548             i_read = Read( p_access, p_buffer, i_len );
549             p_sys->b_continuous = VLC_TRUE;
550         }
551         if( p_sys->b_reconnect )
552         {
553             msg_Dbg( p_access, "got disconnected, trying to reconnect" );
554             net_Close( p_sys->fd ); p_sys->fd = -1;
555             if( Connect( p_access, p_access->info.i_pos ) )
556             {
557                 msg_Dbg( p_access, "reconnection failed" );
558             }
559             else
560             {
561                 p_sys->b_reconnect = VLC_FALSE;
562                 i_read = Read( p_access, p_buffer, i_len );
563                 p_sys->b_reconnect = VLC_TRUE;
564             }
565         }
566
567         if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
568     }
569
570     if( p_sys->b_continuous )
571     {
572         p_sys->i_remaining -= i_read;
573     }
574
575     return i_read;
576 }
577
578 static int ReadICYMeta( access_t *p_access )
579 {
580     access_sys_t *p_sys = p_access->p_sys;
581
582     uint8_t buffer[1];
583     char *psz_meta;
584     int i_read;
585     char *p;
586
587     /* Read meta data length */
588     i_read = net_Read( p_access, p_sys->fd, NULL, buffer, 1, VLC_TRUE );
589     if( i_read <= 0 )
590         return VLC_EGENERIC;
591
592
593     if( buffer[0] <= 0 )
594         return VLC_SUCCESS;
595
596     msg_Dbg( p_access, "ICY meta size=%d", buffer[0] * 16);
597
598     psz_meta = malloc( buffer[0] * 16 + 1 );
599     i_read = net_Read( p_access, p_sys->fd, NULL,
600                        psz_meta, buffer[0] * 16, VLC_TRUE );
601
602     if( i_read != buffer[0] * 16 )
603         return VLC_EGENERIC;
604
605     psz_meta[buffer[0]*16 + 1] = '\0'; /* Just in case */
606
607     msg_Dbg( p_access, "icy-meta=%s", psz_meta );
608
609     /* Now parse the meta */
610     /* Look for StreamTitle= */
611     p = strcasestr( psz_meta, "StreamTitle=" );
612     if( p )
613     {
614         p += strlen( "StreamTitle=" );
615         if( *p == '\'' || *p == '"' )
616         {
617             char *psz = strchr( &p[1], p[0] );
618             if( !psz )
619                 psz = strchr( &p[1], ';' );
620
621             if( psz ) *psz = '\0';
622         }
623         else
624         {
625             char *psz = strchr( &p[1], ';' );
626             if( psz ) *psz = '\0';
627         }
628
629         if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
630
631         p_sys->psz_icy_title = strdup( &p[1] );
632
633         p_access->info.i_update |= INPUT_UPDATE_META;
634     }
635
636     free( psz_meta );
637
638     msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
639
640     return VLC_SUCCESS;
641 }
642
643 /*****************************************************************************
644  * Seek: close and re-open a connection at the right place
645  *****************************************************************************/
646 static int Seek( access_t *p_access, int64_t i_pos )
647 {
648     access_sys_t *p_sys = p_access->p_sys;
649
650     msg_Dbg( p_access, "trying to seek to "I64Fd, i_pos );
651
652     net_Close( p_sys->fd ); p_sys->fd = -1;
653
654     if( Connect( p_access, i_pos ) )
655     {
656         msg_Err( p_access, "seek failed" );
657         p_access->info.b_eof = VLC_TRUE;
658         return VLC_EGENERIC;
659     }
660     return VLC_SUCCESS;
661 }
662
663 /*****************************************************************************
664  * Control:
665  *****************************************************************************/
666 static int Control( access_t *p_access, int i_query, va_list args )
667 {
668     access_sys_t *p_sys = p_access->p_sys;
669     vlc_bool_t   *pb_bool;
670     int          *pi_int;
671     int64_t      *pi_64;
672     vlc_meta_t **pp_meta;
673
674     switch( i_query )
675     {
676         /* */
677         case ACCESS_CAN_SEEK:
678             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
679             *pb_bool = p_sys->b_seekable;
680             break;
681         case ACCESS_CAN_FASTSEEK:
682             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
683             *pb_bool = VLC_FALSE;
684             break;
685         case ACCESS_CAN_PAUSE:
686         case ACCESS_CAN_CONTROL_PACE:
687             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
688             *pb_bool = p_sys->b_pace_control;
689             break;
690
691         /* */
692         case ACCESS_GET_MTU:
693             pi_int = (int*)va_arg( args, int * );
694             *pi_int = 0;
695             break;
696
697         case ACCESS_GET_PTS_DELAY:
698             pi_64 = (int64_t*)va_arg( args, int64_t * );
699             *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
700             break;
701
702         /* */
703         case ACCESS_SET_PAUSE_STATE:
704             break;
705
706         case ACCESS_GET_META:
707             pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
708             *pp_meta = vlc_meta_New();
709             msg_Dbg( p_access, "GET META %s %s %s",
710                      p_sys->psz_icy_name, p_sys->psz_icy_genre, p_sys->psz_icy_title );
711             if( p_sys->psz_icy_name )
712                 vlc_meta_Add( *pp_meta, VLC_META_DESCRIPTION,
713                               p_sys->psz_icy_name );
714             if( p_sys->psz_icy_genre )
715                 vlc_meta_Add( *pp_meta, VLC_META_GENRE,
716                               p_sys->psz_icy_genre );
717             if( p_sys->psz_icy_title )
718                 vlc_meta_Add( *pp_meta, VLC_META_TITLE,
719                               p_sys->psz_icy_title );
720             break;
721
722         case ACCESS_GET_TITLE_INFO:
723         case ACCESS_SET_TITLE:
724         case ACCESS_SET_SEEKPOINT:
725         case ACCESS_SET_PRIVATE_ID_STATE:
726             return VLC_EGENERIC;
727
728         default:
729             msg_Warn( p_access, "unimplemented query in control" );
730             return VLC_EGENERIC;
731
732     }
733     return VLC_SUCCESS;
734 }
735
736 /*****************************************************************************
737  * ParseURL: extract user:password
738  *****************************************************************************/
739 static void ParseURL( access_sys_t *p_sys, char *psz_url )
740 {
741     char *psz_dup = strdup( psz_url );
742     char *p = psz_dup;
743     char *psz;
744
745     /* Syntax //[user:password]@<hostname>[:<port>][/<path>] */
746     while( *p == '/' )
747     {
748         p++;
749     }
750     psz = p;
751
752     /* Parse auth */
753     if( ( p = strchr( psz, '@' ) ) )
754     {
755         char *comma;
756
757         *p++ = '\0';
758         comma = strchr( psz, ':' );
759
760         /* Retreive user:password */
761         if( comma )
762         {
763             *comma++ = '\0';
764
765             p_sys->psz_user = strdup( psz );
766             p_sys->psz_passwd = strdup( comma );
767         }
768         else
769         {
770             p_sys->psz_user = strdup( psz );
771         }
772     }
773     else
774     {
775         p = psz;
776     }
777
778     /* Parse uri */
779     vlc_UrlParse( &p_sys->url, p, 0 );
780
781     free( psz_dup );
782 }
783
784 /*****************************************************************************
785  * Connect:
786  *****************************************************************************/
787 static int Connect( access_t *p_access, int64_t i_tell )
788 {
789     access_sys_t   *p_sys = p_access->p_sys;
790     vlc_url_t      srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
791
792     /* Clean info */
793     if( p_sys->psz_location ) free( p_sys->psz_location );
794     if( p_sys->psz_mime ) free( p_sys->psz_mime );
795     if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
796
797     if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
798     if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
799     if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
800
801
802     p_sys->psz_location = NULL;
803     p_sys->psz_mime = NULL;
804     p_sys->psz_pragma = NULL;
805     p_sys->b_mms = VLC_FALSE;
806     p_sys->b_chunked = VLC_FALSE;
807     p_sys->i_chunk = 0;
808     p_sys->i_icy_meta = 0;
809     p_sys->psz_icy_name = NULL;
810     p_sys->psz_icy_genre = NULL;
811     p_sys->psz_icy_title = NULL;
812
813     p_access->info.i_size = 0;
814     p_access->info.i_pos  = i_tell;
815     p_access->info.b_eof  = VLC_FALSE;
816
817
818     /* Open connection */
819     p_sys->fd = net_OpenTCP( p_access, srv.psz_host, srv.i_port );
820     if( p_sys->fd < 0 )
821     {
822         msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
823         return VLC_EGENERIC;
824     }
825
826     return Request( p_access,i_tell );
827 }
828
829
830 static int Request( access_t *p_access, int64_t i_tell )
831 {
832     access_sys_t   *p_sys = p_access->p_sys;
833     char           *psz;
834     if( p_sys->b_proxy )
835     {
836         if( p_sys->url.psz_path )
837         {
838             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
839                         "GET http://%s:%d%s HTTP/1.%d\r\n",
840                         p_sys->url.psz_host, p_sys->url.i_port,
841                         p_sys->url.psz_path, p_sys->i_version );
842         }
843         else
844         {
845             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
846                         "GET http://%s:%d/ HTTP/1.%d\r\n",
847                         p_sys->url.psz_host, p_sys->url.i_port,
848                         p_sys->i_version );
849         }
850     }
851     else
852     {
853         char *psz_path = p_sys->url.psz_path;
854         if( !psz_path || !*psz_path )
855         {
856             psz_path = "/";
857         }
858         if( p_sys->url.i_port != 80)
859         {
860             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
861                         "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
862                         psz_path, p_sys->i_version, p_sys->url.psz_host,
863                         p_sys->url.i_port );
864         }
865         else
866         {
867             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
868                         "GET %s HTTP/1.%d\r\nHost: %s\r\n",
869                         psz_path, p_sys->i_version, p_sys->url.psz_host );
870         }
871     }
872     /* User Agent */
873     net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "User-Agent: %s\r\n",
874                 p_sys->psz_user_agent );
875     /* Offset */
876     if( p_sys->i_version == 1 )
877     {
878         net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
879                     "Range: bytes="I64Fd"-\r\n", i_tell );
880     }
881
882     /* Authentification */
883     if( p_sys->psz_user && *p_sys->psz_user )
884     {
885         char *buf;
886         char *b64;
887
888         asprintf( &buf, "%s:%s", p_sys->psz_user,
889                    p_sys->psz_passwd ? p_sys->psz_passwd : "" );
890
891         b64 = vlc_b64_encode( buf );
892
893         net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
894                     "Authorization: Basic %s\r\n", b64 );
895         free( b64 );
896     }
897
898     /* ICY meta data request */
899     net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "Icy-MetaData: 1\r\n" );
900
901
902     if( p_sys->b_continuous && p_sys->i_version == 1 )
903     {
904         net_Printf( VLC_OBJECT( p_access ), p_sys->fd, NULL,
905                     "Connection: keep-alive\r\n" );
906     }
907     else
908     {
909         net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
910                     "Connection: Close\r\n");
911         p_sys->b_continuous = VLC_FALSE;
912     }
913
914     if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "\r\n" ) < 0 )
915     {
916         msg_Err( p_access, "failed to send request" );
917         net_Close( p_sys->fd ); p_sys->fd = -1;
918         return VLC_EGENERIC;
919     }
920
921     /* Read Answer */
922     if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL ) ) == NULL )
923     {
924         msg_Err( p_access, "failed to read answer" );
925         goto error;
926     }
927     if( !strncmp( psz, "HTTP/1.", 7 ) )
928     {
929         p_sys->psz_protocol = "HTTP";
930         p_sys->i_code = atoi( &psz[9] );
931     }
932     else if( !strncmp( psz, "ICY", 3 ) )
933     {
934         p_sys->psz_protocol = "ICY";
935         p_sys->i_code = atoi( &psz[4] );
936         p_sys->b_reconnect = VLC_TRUE;
937     }
938     else
939     {
940         msg_Err( p_access, "invalid HTTP reply '%s'", psz );
941         free( psz );
942         goto error;
943     }
944     msg_Dbg( p_access, "protocol '%s' answer code %d",
945              p_sys->psz_protocol, p_sys->i_code );
946     if( !strcmp( p_sys->psz_protocol, "ICY" ) )
947     {
948         p_sys->b_seekable = VLC_FALSE;
949     }
950     if( p_sys->i_code != 206 )
951     {
952         p_sys->b_seekable = VLC_FALSE;
953     }
954     if( p_sys->i_code >= 400 )
955     {
956         msg_Err( p_access, "error: %s", psz );
957         free( psz );
958         goto error;
959     }
960     free( psz );
961
962     for( ;; )
963     {
964         char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
965         char *p;
966
967         if( psz == NULL )
968         {
969             msg_Err( p_access, "failed to read answer" );
970             goto error;
971         }
972
973         /* msg_Dbg( p_input, "Line=%s", psz ); */
974         if( *psz == '\0' )
975         {
976             free( psz );
977             break;
978         }
979
980
981         if( ( p = strchr( psz, ':' ) ) == NULL )
982         {
983             msg_Err( p_access, "malformed header line: %s", psz );
984             free( psz );
985             goto error;
986         }
987         *p++ = '\0';
988         while( *p == ' ' ) p++;
989
990         if( !strcasecmp( psz, "Content-Length" ) )
991         {
992             if( p_sys->b_continuous )
993             {
994                 p_access->info.i_size = -1;
995                 msg_Dbg( p_access, "this frame size="I64Fd, atoll(p ) );
996                 p_sys->i_remaining = atoll( p );
997             }
998             else
999             {
1000                 p_access->info.i_size = i_tell + atoll( p );
1001                 msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
1002             }
1003         }
1004         else if( !strcasecmp( psz, "Location" ) )
1005         {
1006             if( p_sys->psz_location ) free( p_sys->psz_location );
1007             p_sys->psz_location = strdup( p );
1008         }
1009         else if( !strcasecmp( psz, "Content-Type" ) )
1010         {
1011             if( p_sys->psz_mime ) free( p_sys->psz_mime );
1012             p_sys->psz_mime = strdup( p );
1013             msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1014         }
1015         else if( !strcasecmp( psz, "Pragma" ) )
1016         {
1017             if( !strcasecmp( psz, "Pragma: features" ) )
1018                 p_sys->b_mms = VLC_TRUE;
1019             if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
1020             p_sys->psz_pragma = strdup( p );
1021             msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1022         }
1023         else if( !strcasecmp( psz, "Server" ) )
1024         {
1025             msg_Dbg( p_access, "Server: %s", p );
1026             if( !strncasecmp( p, "Icecast", 7 ) ||
1027                 !strncasecmp( p, "Nanocaster", 10 ) )
1028             {
1029                 /* Remember if this is Icecast
1030                  * we need to force demux in this case without breaking
1031                  *  autodetection */
1032
1033                 /* Let live 365 streams (nanocaster) piggyback on the icecast
1034                  * routine. They look very similar */
1035
1036                 p_sys->b_reconnect = VLC_TRUE;
1037                 p_sys->b_pace_control = VLC_FALSE;
1038                 p_sys->b_icecast = VLC_TRUE;
1039             }
1040         }
1041         else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1042         {
1043             msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1044             if( !strncasecmp( p, "chunked", 7 ) )
1045             {
1046                 p_sys->b_chunked = VLC_TRUE;
1047             }
1048         }
1049         else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1050         {
1051             msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1052             p_sys->i_icy_meta = atoi( p );
1053             if( p_sys->i_icy_meta < 0 )
1054                 p_sys->i_icy_meta = 0;
1055
1056             msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1057         }
1058         else if( !strcasecmp( psz, "Icy-Name" ) )
1059         {
1060             if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
1061             p_sys->psz_icy_name = strdup( p );
1062             msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1063
1064             p_sys->b_icecast = VLC_TRUE; /* be on the safeside. set it here as well. */
1065             p_sys->b_reconnect = VLC_TRUE;
1066             p_sys->b_pace_control = VLC_FALSE;
1067         }
1068         else if( !strcasecmp( psz, "Icy-Genre" ) )
1069         {
1070             if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
1071             p_sys->psz_icy_genre = strdup( p );
1072             msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1073         }
1074         else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1075         {
1076             msg_Dbg( p_access, "Icy-Notice: %s", p );
1077         }
1078         else if( !strncasecmp( psz, "icy-", 4 ) ||
1079                  !strncasecmp( psz, "ice-", 4 ) ||
1080                  !strncasecmp( psz, "x-audiocast", 11 ) )
1081         {
1082             msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1083         }
1084
1085         free( psz );
1086     }
1087     return VLC_SUCCESS;
1088
1089 error:
1090     net_Close( p_sys->fd ); p_sys->fd = -1;
1091     return VLC_EGENERIC;
1092 }
1093