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