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