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