]> git.sesse.net Git - vlc/blob - modules/access/http.c
input options whitelisting, step 2 (refs #1371)
[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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <vlc/vlc.h>
30
31
32 #include <vlc_access.h>
33
34 #include <vlc_interface.h>
35 #include <vlc_meta.h>
36 #include <vlc_network.h>
37 #include <vlc_url.h>
38 #include <vlc_tls.h>
39 #include <vlc_strings.h>
40 #include <vlc_input.h>
41
42 /*****************************************************************************
43  * Module descriptor
44  *****************************************************************************/
45 static int  Open ( vlc_object_t * );
46 static void Close( vlc_object_t * );
47
48 #define PROXY_TEXT N_("HTTP proxy")
49 #define PROXY_LONGTEXT N_( \
50     "HTTP proxy to be used It must be of the form " \
51     "http://[user[:pass]@]myproxy.mydomain:myport/ ; " \
52     "if empty, the http_proxy environment variable will be tried." )
53
54 #define CACHING_TEXT N_("Caching value in ms")
55 #define CACHING_LONGTEXT N_( \
56     "Caching value for HTTP streams. This " \
57     "value should be set in milliseconds." )
58
59 #define AGENT_TEXT N_("HTTP user agent")
60 #define AGENT_LONGTEXT N_("User agent that will be " \
61     "used for the connection.")
62
63 #define RECONNECT_TEXT N_("Auto re-connect")
64 #define RECONNECT_LONGTEXT N_( \
65     "Automatically try to reconnect to the stream in case of a sudden " \
66     "disconnect." )
67
68 #define CONTINUOUS_TEXT N_("Continuous stream")
69 #define CONTINUOUS_LONGTEXT N_("Read a file that is " \
70     "being constantly updated (for example, a JPG file on a server). " \
71     "You should not globally enable this option as it will break all other " \
72     "types of HTTP streams." )
73
74 vlc_module_begin();
75     set_description( _("HTTP input") );
76     set_capability( "access2", 0 );
77     set_shortname( _( "HTTP(S)" ) );
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         change_safe();
84     add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
85                  CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
86         change_safe();
87     add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
88                 AGENT_LONGTEXT, VLC_TRUE );
89         change_safe();
90     add_bool( "http-reconnect", 0, NULL, RECONNECT_TEXT,
91               RECONNECT_LONGTEXT, VLC_TRUE );
92         change_safe();
93     add_bool( "http-continuous", 0, NULL, CONTINUOUS_TEXT,
94               CONTINUOUS_LONGTEXT, VLC_TRUE );
95         change_safe();
96     add_obsolete_string("http-user");
97     add_obsolete_string("http-pwd");
98     add_shortcut( "http" );
99     add_shortcut( "https" );
100     add_shortcut( "unsv" );
101     add_shortcut( "itpc" ); /* iTunes Podcast */
102     set_callbacks( Open, Close );
103 vlc_module_end();
104
105 /*****************************************************************************
106  * Local prototypes
107  *****************************************************************************/
108 struct access_sys_t
109 {
110     int fd;
111     tls_session_t *p_tls;
112     v_socket_t    *p_vs;
113
114     /* From uri */
115     vlc_url_t url;
116     char    *psz_user_agent;
117
118     /* Proxy */
119     vlc_bool_t b_proxy;
120     vlc_url_t  proxy;
121
122     /* */
123     int        i_code;
124     const char *psz_protocol;
125     int        i_version;
126
127     char       *psz_mime;
128     char       *psz_pragma;
129     char       *psz_location;
130     vlc_bool_t b_mms;
131     vlc_bool_t b_icecast;
132     vlc_bool_t b_ssl;
133
134     vlc_bool_t b_chunked;
135     int64_t    i_chunk;
136
137     int        i_icy_meta;
138     char       *psz_icy_name;
139     char       *psz_icy_genre;
140     char       *psz_icy_title;
141
142     int i_remaining;
143
144     vlc_bool_t b_seekable;
145     vlc_bool_t b_reconnect;
146     vlc_bool_t b_continuous;
147     vlc_bool_t b_pace_control;
148 };
149
150 /* */
151 static ssize_t Read( access_t *, uint8_t *, size_t );
152 static int Seek( access_t *, int64_t );
153 static int Control( access_t *, int, va_list );
154
155 /* */
156 static int Connect( access_t *, int64_t );
157 static int Request( access_t *p_access, int64_t i_tell );
158 static void Disconnect( access_t * );
159
160 /*****************************************************************************
161  * Open:
162  *****************************************************************************/
163 static int Open( vlc_object_t *p_this )
164 {
165     access_t     *p_access = (access_t*)p_this;
166     access_sys_t *p_sys;
167     char         *psz, *p;
168
169     /* Set up p_access */
170     STANDARD_READ_ACCESS_INIT;
171     p_sys->fd = -1;
172     p_sys->b_proxy = VLC_FALSE;
173     p_sys->i_version = 1;
174     p_sys->b_seekable = VLC_TRUE;
175     p_sys->psz_mime = NULL;
176     p_sys->psz_pragma = NULL;
177     p_sys->b_mms = VLC_FALSE;
178     p_sys->b_icecast = VLC_FALSE;
179     p_sys->psz_location = NULL;
180     p_sys->psz_user_agent = NULL;
181     p_sys->b_pace_control = VLC_TRUE;
182     p_sys->b_ssl = VLC_FALSE;
183     p_sys->p_tls = NULL;
184     p_sys->p_vs = NULL;
185     p_sys->i_icy_meta = 0;
186     p_sys->psz_icy_name = NULL;
187     p_sys->psz_icy_genre = NULL;
188     p_sys->psz_icy_title = NULL;
189     p_sys->i_remaining = 0;
190
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     /* Connect */
271     switch( Connect( p_access, 0 ) )
272     {
273         case -1:
274             goto error;
275
276         case -2:
277             /* Retry with http 1.0 */
278             msg_Dbg( p_access, "switching to HTTP version 1.0" );
279             p_sys->i_version = 0;
280             p_sys->b_seekable = VLC_FALSE;
281
282             if( p_access->b_die || Connect( p_access, 0 ) )
283                 goto error;
284
285 #ifdef DEBUG
286         case 0:
287             break;
288
289         default:
290             msg_Err( p_access, "You should not be here" );
291             abort();
292 #endif
293     }
294
295     if( p_sys->i_code == 401 )
296     {
297         char *psz_login = NULL; char *psz_password = NULL;
298         int i_ret;
299         msg_Dbg( p_access, "authentication failed" );
300         i_ret = intf_UserLoginPassword( p_access, _("HTTP authentication"),
301                         _("Please enter a valid login name and a password."),
302                                                 &psz_login, &psz_password );
303         if( i_ret == DIALOG_OK_YES )
304         {
305             msg_Dbg( p_access, "retrying with user=%s, pwd=%s",
306                         psz_login, psz_password );
307             if( psz_login ) p_sys->url.psz_username = strdup( psz_login );
308             if( psz_password ) p_sys->url.psz_password = strdup( psz_password );
309             if( psz_login ) free( psz_login );
310             if( psz_password ) free( psz_password );
311             goto connect;
312         }
313         else
314         {
315             if( psz_login ) free( psz_login );
316             if( psz_password ) free( psz_password );
317             goto error;
318         }
319     }
320
321     if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
322           p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
323         p_sys->psz_location && *p_sys->psz_location )
324     {
325         msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
326
327         /* Do not accept redirection outside of HTTP works */
328         if( strncmp( p_sys->psz_location, "http", 4 )
329          || ( ( p_sys->psz_location[4] != ':' ) /* HTTP */
330            && strncmp( p_sys->psz_location + 4, "s:", 2 ) /* HTTP/SSL */ ) )
331         {
332             msg_Err( p_access, "insecure redirection ignored" );
333             goto error;
334         }
335         if( p_sys->i_code == 301 )
336         {
337             /* Permanent redirection: Change the URI */
338             input_thread_t * p_input = vlc_object_find( p_access, VLC_OBJECT_INPUT, FIND_PARENT );
339             if( p_input )
340             {
341                 input_item_t * p_input_item = input_GetItem(p_input);
342                 input_item_SetURI( p_input_item, p_sys->psz_location );
343                 vlc_object_release( p_input );
344             }
345         }
346         free( p_access->psz_path );
347         p_access->psz_path = strdup( p_sys->psz_location );
348         /* Clean up current Open() run */
349         vlc_UrlClean( &p_sys->url );
350         vlc_UrlClean( &p_sys->proxy );
351         free( p_sys->psz_mime );
352         free( p_sys->psz_pragma );
353         free( p_sys->psz_location );
354         free( p_sys->psz_user_agent );
355
356         Disconnect( p_access );
357         free( p_sys );
358
359         /* Do new Open() run with new data */
360         return Open( p_this );
361     }
362
363     if( p_sys->b_mms )
364     {
365         msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
366         goto error;
367     }
368
369     if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
370     {
371         if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
372         {
373             if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
374                 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
375                 p_access->psz_demux = strdup( "nsv" );
376             else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
377                      !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
378                 p_access->psz_demux = strdup( "m4a" );
379             else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
380                 p_access->psz_demux = strdup( "mp3" );
381
382             msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
383                       p_access->psz_demux );
384
385 #if 0       /* Doesn't work really well because of the pre-buffering in
386              * shoutcast servers (the buffer content will be sent as fast as
387              * possible). */
388             p_sys->b_pace_control = VLC_FALSE;
389 #endif
390         }
391         else if( !p_sys->psz_mime )
392         {
393              /* Shoutcast */
394              p_access->psz_demux = strdup( "mp3" );
395         }
396         /* else probably Ogg Vorbis */
397     }
398     else if( !strcasecmp( p_access->psz_access, "unsv" ) &&
399              p_sys->psz_mime &&
400              !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
401     {
402         /* Grrrr! detect ultravox server and force NSV demuxer */
403         p_access->psz_demux = strdup( "nsv" );
404     }
405     else if( !strcmp( p_access->psz_access, "itpc" ) )
406     {
407         p_access->psz_demux = strdup( "podcast" );
408     }
409     else if( p_sys->psz_mime &&
410              !strncasecmp( p_sys->psz_mime, "application/xspf+xml", 20 ) &&
411              ( memchr( " ;\t", p_sys->psz_mime[20], 4 ) != NULL ) )
412         p_access->psz_demux = strdup( "xspf-open" );
413
414     if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
415
416     /* PTS delay */
417     var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
418
419     return VLC_SUCCESS;
420
421 error:
422     vlc_UrlClean( &p_sys->url );
423     vlc_UrlClean( &p_sys->proxy );
424     free( p_sys->psz_mime );
425     free( p_sys->psz_pragma );
426     free( p_sys->psz_location );
427     free( p_sys->psz_user_agent );
428
429     Disconnect( p_access );
430     free( p_sys );
431     return VLC_EGENERIC;
432 }
433
434 /*****************************************************************************
435  * Close:
436  *****************************************************************************/
437 static void Close( vlc_object_t *p_this )
438 {
439     access_t     *p_access = (access_t*)p_this;
440     access_sys_t *p_sys = p_access->p_sys;
441
442     vlc_UrlClean( &p_sys->url );
443     vlc_UrlClean( &p_sys->proxy );
444
445     free( p_sys->psz_mime );
446     free( p_sys->psz_pragma );
447     free( p_sys->psz_location );
448
449     free( p_sys->psz_icy_name );
450     free( p_sys->psz_icy_genre );
451     free( p_sys->psz_icy_title );
452
453     free( p_sys->psz_user_agent );
454
455     Disconnect( p_access );
456     free( p_sys );
457 }
458
459 /*****************************************************************************
460  * Read: Read up to i_len bytes from the http connection and place in
461  * p_buffer. Return the actual number of bytes read
462  *****************************************************************************/
463 static int ReadICYMeta( access_t *p_access );
464 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
465 {
466     access_sys_t *p_sys = p_access->p_sys;
467     int i_read;
468
469     if( p_sys->fd < 0 )
470     {
471         p_access->info.b_eof = VLC_TRUE;
472         return 0;
473     }
474
475     if( p_access->info.i_size > 0 &&
476         i_len + p_access->info.i_pos > p_access->info.i_size )
477     {
478         if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
479         {
480             p_access->info.b_eof = VLC_TRUE;
481             return 0;
482         }
483     }
484
485     if( p_sys->b_chunked )
486     {
487         if( p_sys->i_chunk < 0 )
488         {
489             p_access->info.b_eof = VLC_TRUE;
490             return 0;
491         }
492
493         if( p_sys->i_chunk <= 0 )
494         {
495             char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
496             /* read the chunk header */
497             if( psz == NULL )
498             {
499                 /* fatal error - end of file */
500                 msg_Dbg( p_access, "failed reading chunk-header line" );
501                 return 0;
502             }
503             p_sys->i_chunk = strtoll( psz, NULL, 16 );
504             free( psz );
505
506             if( p_sys->i_chunk <= 0 )   /* eof */
507             {
508                 p_sys->i_chunk = -1;
509                 p_access->info.b_eof = VLC_TRUE;
510                 return 0;
511             }
512         }
513
514         if( i_len > p_sys->i_chunk )
515         {
516             i_len = p_sys->i_chunk;
517         }
518     }
519
520     if( p_sys->b_continuous && i_len > p_sys->i_remaining )
521     {
522         /* Only ask for the remaining length */
523         int i_new_len = p_sys->i_remaining;
524         if( i_new_len == 0 )
525         {
526             Request( p_access, 0 );
527             i_read = Read( p_access, p_buffer, i_len );
528             return i_read;
529         }
530         i_len = i_new_len;
531     }
532
533     if( p_sys->i_icy_meta > 0 && p_access->info.i_pos > 0 )
534     {
535         int64_t i_next = p_sys->i_icy_meta -
536                                     p_access->info.i_pos % p_sys->i_icy_meta;
537
538         if( i_next == p_sys->i_icy_meta )
539         {
540             if( ReadICYMeta( p_access ) )
541             {
542                 p_access->info.b_eof = VLC_TRUE;
543                 return -1;
544             }
545         }
546         if( i_len > i_next )
547             i_len = i_next;
548     }
549
550     i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, VLC_FALSE );
551
552     if( i_read > 0 )
553     {
554         p_access->info.i_pos += i_read;
555
556         if( p_sys->b_chunked )
557         {
558             p_sys->i_chunk -= i_read;
559             if( p_sys->i_chunk <= 0 )
560             {
561                 /* read the empty line */
562                 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
563                 if( psz ) free( psz );
564             }
565         }
566     }
567     else if( i_read == 0 )
568     {
569         /*
570          * I very much doubt that this will work.
571          * If i_read == 0, the connection *IS* dead, so the only
572          * sensible thing to do is Disconnect() and then retry.
573          * Otherwise, I got recv() completely wrong. -- Courmisch
574          */
575         if( p_sys->b_continuous )
576         {
577             Request( p_access, 0 );
578             p_sys->b_continuous = VLC_FALSE;
579             i_read = Read( p_access, p_buffer, i_len );
580             p_sys->b_continuous = VLC_TRUE;
581         }
582         Disconnect( p_access );
583         if( p_sys->b_reconnect )
584         {
585             msg_Dbg( p_access, "got disconnected, trying to reconnect" );
586             if( Connect( p_access, p_access->info.i_pos ) )
587             {
588                 msg_Dbg( p_access, "reconnection failed" );
589             }
590             else
591             {
592                 p_sys->b_reconnect = VLC_FALSE;
593                 i_read = Read( p_access, p_buffer, i_len );
594                 p_sys->b_reconnect = VLC_TRUE;
595             }
596         }
597
598         if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
599     }
600
601     if( p_sys->b_continuous )
602     {
603         p_sys->i_remaining -= i_read;
604     }
605
606     return i_read;
607 }
608
609 static int ReadICYMeta( access_t *p_access )
610 {
611     access_sys_t *p_sys = p_access->p_sys;
612
613     uint8_t buffer;
614     char *p, *psz_meta;
615     int i_read;
616
617     /* Read meta data length */
618     i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
619                        VLC_TRUE );
620     if( i_read <= 0 )
621         return VLC_EGENERIC;
622     if( buffer == 0 )
623         return VLC_SUCCESS;
624
625     i_read = buffer << 4;
626     /* msg_Dbg( p_access, "ICY meta size=%u", i_read); */
627
628     psz_meta = malloc( i_read + 1 );
629     if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
630                   (uint8_t *)psz_meta, i_read, VLC_TRUE ) != i_read )
631         return VLC_EGENERIC;
632
633     psz_meta[i_read] = '\0'; /* Just in case */
634
635     /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
636
637     /* Now parse the meta */
638     /* Look for StreamTitle= */
639     p = strcasestr( (char *)psz_meta, "StreamTitle=" );
640     if( p )
641     {
642         p += strlen( "StreamTitle=" );
643         if( *p == '\'' || *p == '"' )
644         {
645             char closing[] = { p[0], ';', '\0' };
646             char *psz = strstr( &p[1], closing );
647             if( !psz )
648                 psz = strchr( &p[1], ';' );
649
650             if( psz ) *psz = '\0';
651         }
652         else
653         {
654             char *psz = strchr( &p[1], ';' );
655             if( psz ) *psz = '\0';
656         }
657
658         if( !p_sys->psz_icy_title ||
659             strcmp( p_sys->psz_icy_title, &p[1] ) )
660         {
661             if( p_sys->psz_icy_title )
662                 free( p_sys->psz_icy_title );
663             p_sys->psz_icy_title = strdup( &p[1] );
664             p_access->info.i_update |= INPUT_UPDATE_META;
665
666             msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
667         }
668     }
669     free( psz_meta );
670
671     return VLC_SUCCESS;
672 }
673
674 /*****************************************************************************
675  * Seek: close and re-open a connection at the right place
676  *****************************************************************************/
677 static int Seek( access_t *p_access, int64_t i_pos )
678 {
679     msg_Dbg( p_access, "trying to seek to "I64Fd, i_pos );
680
681     Disconnect( p_access );
682
683     if( Connect( p_access, i_pos ) )
684     {
685         msg_Err( p_access, "seek failed" );
686         p_access->info.b_eof = VLC_TRUE;
687         return VLC_EGENERIC;
688     }
689     return VLC_SUCCESS;
690 }
691
692 /*****************************************************************************
693  * Control:
694  *****************************************************************************/
695 static int Control( access_t *p_access, int i_query, va_list args )
696 {
697     access_sys_t *p_sys = p_access->p_sys;
698     vlc_bool_t   *pb_bool;
699     int          *pi_int;
700     int64_t      *pi_64;
701     vlc_meta_t   *p_meta;
702
703     switch( i_query )
704     {
705         /* */
706         case ACCESS_CAN_SEEK:
707             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
708             *pb_bool = p_sys->b_seekable;
709             break;
710         case ACCESS_CAN_FASTSEEK:
711             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
712             *pb_bool = VLC_FALSE;
713             break;
714         case ACCESS_CAN_PAUSE:
715         case ACCESS_CAN_CONTROL_PACE:
716             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
717
718 #if 0       /* Disable for now until we have a clock synchro algo
719              * which works with something else than MPEG over UDP */
720             *pb_bool = p_sys->b_pace_control;
721 #endif
722             *pb_bool = VLC_TRUE;
723             break;
724
725         /* */
726         case ACCESS_GET_MTU:
727             pi_int = (int*)va_arg( args, int * );
728             *pi_int = 0;
729             break;
730
731         case ACCESS_GET_PTS_DELAY:
732             pi_64 = (int64_t*)va_arg( args, int64_t * );
733             *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
734             break;
735
736         /* */
737         case ACCESS_SET_PAUSE_STATE:
738             break;
739
740         case ACCESS_GET_META:
741             p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
742
743             if( p_sys->psz_icy_name )
744                 vlc_meta_Set( p_meta, vlc_meta_Title, p_sys->psz_icy_name );
745             if( p_sys->psz_icy_genre )
746                 vlc_meta_Set( p_meta, vlc_meta_Genre, p_sys->psz_icy_genre );
747             if( p_sys->psz_icy_title )
748                 vlc_meta_Set( p_meta, vlc_meta_NowPlaying, p_sys->psz_icy_title );
749             break;
750
751         case ACCESS_GET_CONTENT_TYPE:
752             *va_arg( args, char ** ) =
753                 p_sys->psz_mime ? strdup( p_sys->psz_mime ) : NULL;
754             break;
755
756         case ACCESS_GET_TITLE_INFO:
757         case ACCESS_SET_TITLE:
758         case ACCESS_SET_SEEKPOINT:
759         case ACCESS_SET_PRIVATE_ID_STATE:
760             return VLC_EGENERIC;
761
762         default:
763             msg_Warn( p_access, "unimplemented query in control" );
764             return VLC_EGENERIC;
765
766     }
767     return VLC_SUCCESS;
768 }
769
770 /*****************************************************************************
771  * Connect:
772  *****************************************************************************/
773 static int Connect( access_t *p_access, int64_t i_tell )
774 {
775     access_sys_t   *p_sys = p_access->p_sys;
776     vlc_url_t      srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
777
778     /* Clean info */
779     free( p_sys->psz_location );
780     free( p_sys->psz_mime );
781     free( p_sys->psz_pragma );
782
783     free( p_sys->psz_icy_genre );
784     free( p_sys->psz_icy_name );
785     free( p_sys->psz_icy_title );
786
787
788     p_sys->psz_location = NULL;
789     p_sys->psz_mime = NULL;
790     p_sys->psz_pragma = NULL;
791     p_sys->b_mms = VLC_FALSE;
792     p_sys->b_chunked = VLC_FALSE;
793     p_sys->i_chunk = 0;
794     p_sys->i_icy_meta = 0;
795     p_sys->psz_icy_name = NULL;
796     p_sys->psz_icy_genre = NULL;
797     p_sys->psz_icy_title = NULL;
798
799     p_access->info.i_size = 0;
800     p_access->info.i_pos  = i_tell;
801     p_access->info.b_eof  = VLC_FALSE;
802
803
804     /* Open connection */
805     p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
806     if( p_sys->fd == -1 )
807     {
808         msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
809         return -1;
810     }
811
812     /* Initialize TLS/SSL session */
813     if( p_sys->b_ssl == VLC_TRUE )
814     {
815         /* CONNECT to establish TLS tunnel through HTTP proxy */
816         if( p_sys->b_proxy )
817         {
818             char *psz;
819             unsigned i_status = 0;
820
821             if( p_sys->i_version == 0 )
822             {
823                 /* CONNECT is not in HTTP/1.0 */
824                 Disconnect( p_access );
825                 return -1;
826             }
827
828             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
829                         "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
830                         p_sys->url.psz_host, p_sys->url.i_port,
831                         p_sys->i_version,
832                         p_sys->url.psz_host, p_sys->url.i_port);
833
834             psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
835             if( psz == NULL )
836             {
837                 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
838                 Disconnect( p_access );
839                 return -1;
840             }
841
842             sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
843             free( psz );
844
845             if( ( i_status / 100 ) != 2 )
846             {
847                 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
848                 Disconnect( p_access );
849                 return -1;
850             }
851
852             do
853             {
854                 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
855                 if( psz == NULL )
856                 {
857                     msg_Err( p_access, "HTTP proxy connection failed" );
858                     Disconnect( p_access );
859                     return -1;
860                 }
861
862                 if( *psz == '\0' )
863                     i_status = 0;
864
865                 free( psz );
866             }
867             while( i_status );
868         }
869
870         /* TLS/SSL handshake */
871         p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
872                                          srv.psz_host );
873         if( p_sys->p_tls == NULL )
874         {
875             msg_Err( p_access, "cannot establish HTTP/TLS session" );
876             Disconnect( p_access );
877             return -1;
878         }
879         p_sys->p_vs = &p_sys->p_tls->sock;
880     }
881
882     return Request( p_access, i_tell ) ? -2 : 0;
883 }
884
885
886 static int Request( access_t *p_access, int64_t i_tell )
887 {
888     access_sys_t   *p_sys = p_access->p_sys;
889     char           *psz ;
890     v_socket_t     *pvs = p_sys->p_vs;
891
892     if( p_sys->b_proxy )
893     {
894         if( p_sys->url.psz_path )
895         {
896             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
897                         "GET http://%s:%d%s HTTP/1.%d\r\n",
898                         p_sys->url.psz_host, p_sys->url.i_port,
899                         p_sys->url.psz_path, p_sys->i_version );
900         }
901         else
902         {
903             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
904                         "GET http://%s:%d/ HTTP/1.%d\r\n",
905                         p_sys->url.psz_host, p_sys->url.i_port,
906                         p_sys->i_version );
907         }
908     }
909     else
910     {
911         const char *psz_path = p_sys->url.psz_path;
912         if( !psz_path || !*psz_path )
913         {
914             psz_path = "/";
915         }
916         if( p_sys->url.i_port != 80)
917         {
918             net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
919                         "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
920                         psz_path, p_sys->i_version, p_sys->url.psz_host,
921                         p_sys->url.i_port );
922         }
923         else
924         {
925             net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
926                         "GET %s HTTP/1.%d\r\nHost: %s\r\n",
927                         psz_path, p_sys->i_version, p_sys->url.psz_host );
928         }
929     }
930     /* User Agent */
931     net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "User-Agent: %s\r\n",
932                 p_sys->psz_user_agent );
933     /* Offset */
934     if( p_sys->i_version == 1 )
935     {
936         net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
937                     "Range: bytes="I64Fd"-\r\n", i_tell );
938     }
939
940     /* Authentication */
941     if( p_sys->url.psz_username || p_sys->url.psz_password )
942     {
943         char buf[strlen( p_sys->url.psz_username ?: "" )
944                   + strlen( p_sys->url.psz_password ?: "" ) + 2];
945         char *b64;
946
947         snprintf( buf, sizeof( buf ), "%s:%s", p_sys->url.psz_username ?: "",
948                   p_sys->url.psz_password ?: "" );
949         b64 = vlc_b64_encode( buf );
950
951         if( b64 != NULL )
952         {
953              net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
954                          "Authorization: Basic %s\r\n", b64 );
955              free( b64 );
956         }
957     }
958
959     /* Proxy Authentication */
960     if( p_sys->proxy.psz_username || p_sys->proxy.psz_password )
961     {
962         char buf[strlen( p_sys->proxy.psz_username ?: "" )
963                   + strlen( p_sys->proxy.psz_password ?: "" )];
964         char *b64;
965
966         snprintf( buf, sizeof( buf ), "%s:%s", p_sys->proxy.psz_username ?: "",
967                   p_sys->proxy.psz_password ?: "" );
968         b64 = vlc_b64_encode( buf );
969
970         if( b64 != NULL)
971         {
972             net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
973                         "Proxy-Authorization: Basic %s\r\n", b64 );
974             free( b64 );
975         }
976     }
977
978     /* ICY meta data request */
979     net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
980
981
982     if( p_sys->b_continuous )
983     {
984         net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
985                     "Connection: Keep-Alive\r\n" );
986     }
987     else if( p_sys->i_version == 1 )
988     {
989         net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
990                     "Connection: Close\r\n");
991     }
992
993     if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "\r\n" ) < 0 )
994     {
995         msg_Err( p_access, "failed to send request" );
996         Disconnect( p_access );
997         return VLC_EGENERIC;
998     }
999
1000     /* Read Answer */
1001     if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs ) ) == NULL )
1002     {
1003         msg_Err( p_access, "failed to read answer" );
1004         goto error;
1005     }
1006     if( !strncmp( psz, "HTTP/1.", 7 ) )
1007     {
1008         p_sys->psz_protocol = "HTTP";
1009         p_sys->i_code = atoi( &psz[9] );
1010     }
1011     else if( !strncmp( psz, "ICY", 3 ) )
1012     {
1013         p_sys->psz_protocol = "ICY";
1014         p_sys->i_code = atoi( &psz[4] );
1015         p_sys->b_reconnect = VLC_TRUE;
1016     }
1017     else
1018     {
1019         msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1020         free( psz );
1021         goto error;
1022     }
1023     msg_Dbg( p_access, "protocol '%s' answer code %d",
1024              p_sys->psz_protocol, p_sys->i_code );
1025     if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1026     {
1027         p_sys->b_seekable = VLC_FALSE;
1028     }
1029     if( p_sys->i_code != 206 && p_sys->i_code != 401 )
1030     {
1031         p_sys->b_seekable = VLC_FALSE;
1032     }
1033     /* Authentication error - We'll have to display the dialog */
1034     if( p_sys->i_code == 401 )
1035     {
1036
1037     }
1038     /* Other fatal error */
1039     else if( p_sys->i_code >= 400 )
1040     {
1041         msg_Err( p_access, "error: %s", psz );
1042         free( psz );
1043         goto error;
1044     }
1045     free( psz );
1046
1047     for( ;; )
1048     {
1049         char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs );
1050         char *p;
1051
1052         if( psz == NULL )
1053         {
1054             msg_Err( p_access, "failed to read answer" );
1055             goto error;
1056         }
1057
1058         /* msg_Dbg( p_input, "Line=%s", psz ); */
1059         if( *psz == '\0' )
1060         {
1061             free( psz );
1062             break;
1063         }
1064
1065
1066         if( ( p = strchr( psz, ':' ) ) == NULL )
1067         {
1068             msg_Err( p_access, "malformed header line: %s", psz );
1069             free( psz );
1070             goto error;
1071         }
1072         *p++ = '\0';
1073         while( *p == ' ' ) p++;
1074
1075         if( !strcasecmp( psz, "Content-Length" ) )
1076         {
1077             if( p_sys->b_continuous )
1078             {
1079                 p_access->info.i_size = -1;
1080                 msg_Dbg( p_access, "this frame size=%lld", atoll(p ) );
1081                 p_sys->i_remaining = atoll( p );
1082             }
1083             else
1084             {
1085                 p_access->info.i_size = i_tell + atoll( p );
1086                 msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
1087             }
1088         }
1089         else if( !strcasecmp( psz, "Location" ) )
1090         {
1091             char * psz_new_loc;
1092
1093             /* This does not follow RFC 2068, but yet if the url is not absolute,
1094              * handle it as everyone does. */
1095             if( p[0] == '/' )
1096             {
1097                 const char *psz_http_ext = p_sys->b_ssl ? "s" : "" ;
1098
1099                 if( p_sys->url.i_port == ( p_sys->b_ssl ? 443 : 80 ) )
1100                 {
1101                     asprintf(&psz_new_loc, "http%s://%s%s", psz_http_ext,
1102                              p_sys->url.psz_host, p);
1103                 }
1104                 else
1105                 {
1106                     asprintf(&psz_new_loc, "http%s://%s:%d%s", psz_http_ext,
1107                              p_sys->url.psz_host, p_sys->url.i_port, p);
1108                 }
1109             }
1110             else
1111             {
1112                 psz_new_loc = strdup( p );
1113             }
1114
1115             if( p_sys->psz_location ) free( p_sys->psz_location );
1116             p_sys->psz_location = psz_new_loc;
1117         }
1118         else if( !strcasecmp( psz, "Content-Type" ) )
1119         {
1120             if( p_sys->psz_mime ) free( p_sys->psz_mime );
1121             p_sys->psz_mime = strdup( p );
1122             msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1123         }
1124         else if( !strcasecmp( psz, "Pragma" ) )
1125         {
1126             if( !strcasecmp( psz, "Pragma: features" ) )
1127                 p_sys->b_mms = VLC_TRUE;
1128             if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
1129             p_sys->psz_pragma = strdup( p );
1130             msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1131         }
1132         else if( !strcasecmp( psz, "Server" ) )
1133         {
1134             msg_Dbg( p_access, "Server: %s", p );
1135             if( !strncasecmp( p, "Icecast", 7 ) ||
1136                 !strncasecmp( p, "Nanocaster", 10 ) )
1137             {
1138                 /* Remember if this is Icecast
1139                  * we need to force demux in this case without breaking
1140                  *  autodetection */
1141
1142                 /* Let live 365 streams (nanocaster) piggyback on the icecast
1143                  * routine. They look very similar */
1144
1145                 p_sys->b_reconnect = VLC_TRUE;
1146                 p_sys->b_pace_control = VLC_FALSE;
1147                 p_sys->b_icecast = VLC_TRUE;
1148             }
1149         }
1150         else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1151         {
1152             msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1153             if( !strncasecmp( p, "chunked", 7 ) )
1154             {
1155                 p_sys->b_chunked = VLC_TRUE;
1156             }
1157         }
1158         else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1159         {
1160             msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1161             p_sys->i_icy_meta = atoi( p );
1162             if( p_sys->i_icy_meta < 0 )
1163                 p_sys->i_icy_meta = 0;
1164
1165             msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1166         }
1167         else if( !strcasecmp( psz, "Icy-Name" ) )
1168         {
1169             if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
1170             p_sys->psz_icy_name = strdup( p );
1171             msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1172
1173             p_sys->b_icecast = VLC_TRUE; /* be on the safeside. set it here as well. */
1174             p_sys->b_reconnect = VLC_TRUE;
1175             p_sys->b_pace_control = VLC_FALSE;
1176         }
1177         else if( !strcasecmp( psz, "Icy-Genre" ) )
1178         {
1179             if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
1180             p_sys->psz_icy_genre = strdup( p );
1181             msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1182         }
1183         else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1184         {
1185             msg_Dbg( p_access, "Icy-Notice: %s", p );
1186         }
1187         else if( !strncasecmp( psz, "icy-", 4 ) ||
1188                  !strncasecmp( psz, "ice-", 4 ) ||
1189                  !strncasecmp( psz, "x-audiocast", 11 ) )
1190         {
1191             msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1192         }
1193
1194         free( psz );
1195     }
1196     return VLC_SUCCESS;
1197
1198 error:
1199     Disconnect( p_access );
1200     return VLC_EGENERIC;
1201 }
1202
1203 /*****************************************************************************
1204  * Disconnect:
1205  *****************************************************************************/
1206 static void Disconnect( access_t *p_access )
1207 {
1208     access_sys_t *p_sys = p_access->p_sys;
1209
1210     if( p_sys->p_tls != NULL)
1211     {
1212         tls_ClientDelete( p_sys->p_tls );
1213         p_sys->p_tls = NULL;
1214         p_sys->p_vs = NULL;
1215     }
1216     if( p_sys->fd != -1)
1217     {
1218         net_Close(p_sys->fd);
1219         p_sys->fd = -1;
1220     }
1221
1222 }