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