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