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