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