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