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