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