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