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