]> git.sesse.net Git - vlc/blob - modules/access/http.c
Only rewrite the URI if it's a permanent redirection. Thanks to Sebastian Wiedenroth...
[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 <vlc/vlc.h>
30
31
32 #include <vlc_access.h>
33
34 #include <vlc_interface.h>
35 #include <vlc_playlist.h>
36 #include <vlc_meta.h>
37 #include <vlc_network.h>
38 #include <vlc_url.h>
39 #include <vlc_tls.h>
40 #include <vlc_strings.h>
41
42 /*****************************************************************************
43  * Module descriptor
44  *****************************************************************************/
45 static int  Open ( vlc_object_t * );
46 static void Close( vlc_object_t * );
47
48 #define PROXY_TEXT N_("HTTP proxy")
49 #define PROXY_LONGTEXT N_( \
50     "HTTP proxy to be used It must be of the form " \
51     "http://[user[:pass]@]myproxy.mydomain:myport/ ; " \
52     "if empty, the http_proxy environment variable will be tried." )
53
54 #define CACHING_TEXT N_("Caching value in ms")
55 #define CACHING_LONGTEXT N_( \
56     "Caching value for HTTP streams. This " \
57     "value should be set in milliseconds." )
58
59 #define AGENT_TEXT N_("HTTP user agent")
60 #define AGENT_LONGTEXT N_("User agent that will be " \
61     "used for the connection.")
62
63 #define RECONNECT_TEXT N_("Auto re-connect")
64 #define RECONNECT_LONGTEXT N_( \
65     "Automatically try to reconnect to the stream in case of a sudden " \
66     "disconnect." )
67
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_obsolete_string("http-user");
92     add_obsolete_string("http-pwd");
93     add_shortcut( "http" );
94     add_shortcut( "https" );
95     add_shortcut( "unsv" );
96     add_shortcut( "itpc" ); /* iTunes Podcast */
97     set_callbacks( Open, Close );
98 vlc_module_end();
99
100 /*****************************************************************************
101  * Local prototypes
102  *****************************************************************************/
103 struct access_sys_t
104 {
105     int fd;
106     tls_session_t *p_tls;
107     v_socket_t    *p_vs;
108
109     /* From uri */
110     vlc_url_t url;
111     char    *psz_user_agent;
112
113     /* Proxy */
114     vlc_bool_t b_proxy;
115     vlc_url_t  proxy;
116
117     /* */
118     int        i_code;
119     const char *psz_protocol;
120     int        i_version;
121
122     char       *psz_mime;
123     char       *psz_pragma;
124     char       *psz_location;
125     vlc_bool_t b_mms;
126     vlc_bool_t b_icecast;
127     vlc_bool_t b_ssl;
128
129     vlc_bool_t b_chunked;
130     int64_t    i_chunk;
131
132     int        i_icy_meta;
133     char       *psz_icy_name;
134     char       *psz_icy_genre;
135     char       *psz_icy_title;
136
137     int i_remaining;
138
139     vlc_bool_t b_seekable;
140     vlc_bool_t b_reconnect;
141     vlc_bool_t b_continuous;
142     vlc_bool_t b_pace_control;
143 };
144
145 /* */
146 static ssize_t Read( access_t *, uint8_t *, size_t );
147 static int Seek( access_t *, int64_t );
148 static int Control( access_t *, int, va_list );
149
150 /* */
151 static int Connect( access_t *, int64_t );
152 static int Request( access_t *p_access, int64_t i_tell );
153 static void Disconnect( access_t * );
154
155 /*****************************************************************************
156  * Open:
157  *****************************************************************************/
158 static int Open( vlc_object_t *p_this )
159 {
160     access_t     *p_access = (access_t*)p_this;
161     access_sys_t *p_sys;
162     char         *psz, *p;
163
164     /* Set up p_access */
165     STANDARD_READ_ACCESS_INIT;
166     p_sys->fd = -1;
167     p_sys->b_proxy = VLC_FALSE;
168     p_sys->i_version = 1;
169     p_sys->b_seekable = VLC_TRUE;
170     p_sys->psz_mime = NULL;
171     p_sys->psz_pragma = NULL;
172     p_sys->b_mms = VLC_FALSE;
173     p_sys->b_icecast = VLC_FALSE;
174     p_sys->psz_location = NULL;
175     p_sys->psz_user_agent = NULL;
176     p_sys->b_pace_control = VLC_TRUE;
177     p_sys->b_ssl = VLC_FALSE;
178     p_sys->p_tls = NULL;
179     p_sys->p_vs = NULL;
180     p_sys->i_icy_meta = 0;
181     p_sys->psz_icy_name = NULL;
182     p_sys->psz_icy_genre = NULL;
183     p_sys->psz_icy_title = NULL;
184     p_sys->i_remaining = 0;
185
186
187     /* Parse URI - remove spaces */
188     p = psz = strdup( p_access->psz_path );
189     while( (p = strchr( p, ' ' )) != NULL )
190         *p = '+';
191     vlc_UrlParse( &p_sys->url, psz, 0 );
192     free( psz );
193
194     if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
195     {
196         msg_Warn( p_access, "invalid host" );
197         goto error;
198     }
199     if( !strncmp( p_access->psz_access, "https", 5 ) )
200     {
201         /* HTTP over SSL */
202         p_sys->b_ssl = VLC_TRUE;
203         if( p_sys->url.i_port <= 0 )
204             p_sys->url.i_port = 443;
205     }
206     else
207     {
208         if( p_sys->url.i_port <= 0 )
209             p_sys->url.i_port = 80;
210     }
211
212     /* Do user agent */
213     p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
214
215     /* Check proxy */
216     psz = var_CreateGetString( p_access, "http-proxy" );
217     if( *psz )
218     {
219         p_sys->b_proxy = VLC_TRUE;
220         vlc_UrlParse( &p_sys->proxy, psz, 0 );
221     }
222 #ifdef HAVE_GETENV
223     else
224     {
225         char *psz_proxy = getenv( "http_proxy" );
226         if( psz_proxy && *psz_proxy )
227         {
228             p_sys->b_proxy = VLC_TRUE;
229             vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
230         }
231     }
232 #endif
233     free( psz );
234
235     if( p_sys->b_proxy )
236     {
237         if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
238         {
239             msg_Warn( p_access, "invalid proxy host" );
240             goto error;
241         }
242         if( p_sys->proxy.i_port <= 0 )
243         {
244             p_sys->proxy.i_port = 80;
245         }
246     }
247
248     msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
249              p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
250     if( p_sys->b_proxy )
251     {
252         msg_Dbg( p_access, "      proxy %s:%d", p_sys->proxy.psz_host,
253                  p_sys->proxy.i_port );
254     }
255     if( p_sys->url.psz_username && *p_sys->url.psz_username )
256     {
257         msg_Dbg( p_access, "      user='%s', pwd='%s'",
258                  p_sys->url.psz_username, p_sys->url.psz_password );
259     }
260
261     p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
262     p_sys->b_continuous = var_CreateGetBool( p_access, "http-continuous" );
263
264 connect:
265     /* Connect */
266     switch( Connect( p_access, 0 ) )
267     {
268         case -1:
269             goto error;
270
271         case -2:
272             /* Retry with http 1.0 */
273             msg_Dbg( p_access, "switching to HTTP version 1.0" );
274             p_sys->i_version = 0;
275             p_sys->b_seekable = VLC_FALSE;
276
277             if( p_access->b_die || Connect( p_access, 0 ) )
278                 goto error;
279
280 #ifdef DEBUG
281         case 0:
282             break;
283
284         default:
285             msg_Err( p_access, "You should not be here" );
286             abort();
287 #endif
288     }
289
290     if( p_sys->i_code == 401 )
291     {
292         char *psz_login = NULL; char *psz_password = NULL;
293         int i_ret;
294         msg_Dbg( p_access, "authentication failed" );
295         i_ret = intf_UserLoginPassword( p_access, _("HTTP authentication"),
296                         _("Please enter a valid login name and a password."),
297                                                 &psz_login, &psz_password );
298         if( i_ret == DIALOG_OK_YES )
299         {
300             msg_Dbg( p_access, "retrying with user=%s, pwd=%s",
301                         psz_login, psz_password );
302             if( psz_login ) p_sys->url.psz_username = strdup( psz_login );
303             if( psz_password ) p_sys->url.psz_password = strdup( psz_password );
304             if( psz_login ) free( psz_login );
305             if( psz_password ) free( psz_password );
306             goto connect;
307         }
308         else
309         {
310             if( psz_login ) free( psz_login );
311             if( psz_password ) free( psz_password );
312             goto error;
313         }
314     }
315
316     if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
317           p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
318         p_sys->psz_location && *p_sys->psz_location )
319     {
320         playlist_t * p_playlist;
321         input_item_t *p_input_item;
322
323         msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
324
325         /* Do not accept redirection outside of HTTP works */
326         if( strncmp( p_sys->psz_location, "http", 4 )
327          || ( ( p_sys->psz_location[4] != ':' ) /* HTTP */
328            && strncmp( p_sys->psz_location + 4, "s:", 2 ) /* HTTP/SSL */ ) )
329         {
330             msg_Err( p_access, "insecure redirection ignored" );
331             goto error;
332         }
333         if( p_sys->i_code == 301 )
334         {
335             /* Permanent redirection: Change the URI */
336             p_playlist = pl_Yield( p_access );
337             PL_LOCK;
338
339             p_input_item = p_playlist->status.p_item->p_input;
340             input_item_SetURI( p_input_item, p_sys->psz_location );
341
342             PL_UNLOCK;
343             pl_Release( p_access );
344         }
345         free( p_access->psz_path );
346         p_access->psz_path = strdup( p_sys->psz_location );
347         /* Clean up current Open() run */
348         vlc_UrlClean( &p_sys->url );
349         vlc_UrlClean( &p_sys->proxy );
350         free( p_sys->psz_mime );
351         free( p_sys->psz_pragma );
352         free( p_sys->psz_location );
353         free( p_sys->psz_user_agent );
354
355         Disconnect( p_access );
356         free( p_sys );
357
358         /* Do new Open() run with new data */
359         return Open( p_this );
360     }
361
362     if( p_sys->b_mms )
363     {
364         msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
365         goto error;
366     }
367
368     if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
369     {
370         if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
371         {
372             if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
373                 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
374                 p_access->psz_demux = strdup( "nsv" );
375             else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
376                      !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
377                 p_access->psz_demux = strdup( "m4a" );
378             else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
379                 p_access->psz_demux = strdup( "mp3" );
380
381             msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
382                       p_access->psz_demux );
383
384 #if 0       /* Doesn't work really well because of the pre-buffering in
385              * shoutcast servers (the buffer content will be sent as fast as
386              * possible). */
387             p_sys->b_pace_control = VLC_FALSE;
388 #endif
389         }
390         else if( !p_sys->psz_mime )
391         {
392              /* Shoutcast */
393              p_access->psz_demux = strdup( "mp3" );
394         }
395         /* else probably Ogg Vorbis */
396     }
397     else if( !strcasecmp( p_access->psz_access, "unsv" ) &&
398              p_sys->psz_mime &&
399              !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
400     {
401         /* Grrrr! detect ultravox server and force NSV demuxer */
402         p_access->psz_demux = strdup( "nsv" );
403     }
404     else if( !strcmp( p_access->psz_access, "itpc" ) )
405     {
406         p_access->psz_demux = strdup( "podcast" );
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     free( p_sys->psz_mime );
424     free( p_sys->psz_pragma );
425     free( p_sys->psz_location );
426     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     free( p_sys->psz_mime );
445     free( p_sys->psz_pragma );
446     free( p_sys->psz_location );
447
448     free( p_sys->psz_icy_name );
449     free( p_sys->psz_icy_genre );
450     free( p_sys->psz_icy_title );
451
452     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 ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t 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_Set( p_meta, vlc_meta_Title, p_sys->psz_icy_name );
744             if( p_sys->psz_icy_genre )
745                 vlc_meta_Set( p_meta, vlc_meta_Genre, p_sys->psz_icy_genre );
746             if( p_sys->psz_icy_title )
747                 vlc_meta_Set( p_meta, vlc_meta_NowPlaying, p_sys->psz_icy_title );
748             break;
749
750         case ACCESS_GET_CONTENT_TYPE:
751             *va_arg( args, char ** ) =
752                 p_sys->psz_mime ? strdup( p_sys->psz_mime ) : NULL;
753             break;
754
755         case ACCESS_GET_TITLE_INFO:
756         case ACCESS_SET_TITLE:
757         case ACCESS_SET_SEEKPOINT:
758         case ACCESS_SET_PRIVATE_ID_STATE:
759             return VLC_EGENERIC;
760
761         default:
762             msg_Warn( p_access, "unimplemented query in control" );
763             return VLC_EGENERIC;
764
765     }
766     return VLC_SUCCESS;
767 }
768
769 /*****************************************************************************
770  * Connect:
771  *****************************************************************************/
772 static int Connect( access_t *p_access, int64_t i_tell )
773 {
774     access_sys_t   *p_sys = p_access->p_sys;
775     vlc_url_t      srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
776
777     /* Clean info */
778     free( p_sys->psz_location );
779     free( p_sys->psz_mime );
780     free( p_sys->psz_pragma );
781
782     free( p_sys->psz_icy_genre );
783     free( p_sys->psz_icy_name );
784     free( p_sys->psz_icy_title );
785
786
787     p_sys->psz_location = NULL;
788     p_sys->psz_mime = NULL;
789     p_sys->psz_pragma = NULL;
790     p_sys->b_mms = VLC_FALSE;
791     p_sys->b_chunked = VLC_FALSE;
792     p_sys->i_chunk = 0;
793     p_sys->i_icy_meta = 0;
794     p_sys->psz_icy_name = NULL;
795     p_sys->psz_icy_genre = NULL;
796     p_sys->psz_icy_title = NULL;
797
798     p_access->info.i_size = 0;
799     p_access->info.i_pos  = i_tell;
800     p_access->info.b_eof  = VLC_FALSE;
801
802
803     /* Open connection */
804     p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
805     if( p_sys->fd == -1 )
806     {
807         msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
808         return -1;
809     }
810
811     /* Initialize TLS/SSL session */
812     if( p_sys->b_ssl == VLC_TRUE )
813     {
814         /* CONNECT to establish TLS tunnel through HTTP proxy */
815         if( p_sys->b_proxy )
816         {
817             char *psz;
818             unsigned i_status = 0;
819
820             if( p_sys->i_version == 0 )
821             {
822                 /* CONNECT is not in HTTP/1.0 */
823                 Disconnect( p_access );
824                 return -1;
825             }
826
827             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
828                         "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
829                         p_sys->url.psz_host, p_sys->url.i_port,
830                         p_sys->i_version,
831                         p_sys->url.psz_host, p_sys->url.i_port);
832
833             psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
834             if( psz == NULL )
835             {
836                 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
837                 Disconnect( p_access );
838                 return -1;
839             }
840
841             sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
842             free( psz );
843
844             if( ( i_status / 100 ) != 2 )
845             {
846                 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
847                 Disconnect( p_access );
848                 return -1;
849             }
850
851             do
852             {
853                 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
854                 if( psz == NULL )
855                 {
856                     msg_Err( p_access, "HTTP proxy connection failed" );
857                     Disconnect( p_access );
858                     return -1;
859                 }
860
861                 if( *psz == '\0' )
862                     i_status = 0;
863
864                 free( psz );
865             }
866             while( i_status );
867         }
868
869         /* TLS/SSL handshake */
870         p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
871                                          srv.psz_host );
872         if( p_sys->p_tls == NULL )
873         {
874             msg_Err( p_access, "cannot establish HTTP/TLS session" );
875             Disconnect( p_access );
876             return -1;
877         }
878         p_sys->p_vs = &p_sys->p_tls->sock;
879     }
880
881     return Request( p_access, i_tell ) ? -2 : 0;
882 }
883
884
885 static int Request( access_t *p_access, int64_t i_tell )
886 {
887     access_sys_t   *p_sys = p_access->p_sys;
888     char           *psz ;
889     v_socket_t     *pvs = p_sys->p_vs;
890
891     if( p_sys->b_proxy )
892     {
893         if( p_sys->url.psz_path )
894         {
895             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
896                         "GET http://%s:%d%s HTTP/1.%d\r\n",
897                         p_sys->url.psz_host, p_sys->url.i_port,
898                         p_sys->url.psz_path, p_sys->i_version );
899         }
900         else
901         {
902             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
903                         "GET http://%s:%d/ HTTP/1.%d\r\n",
904                         p_sys->url.psz_host, p_sys->url.i_port,
905                         p_sys->i_version );
906         }
907     }
908     else
909     {
910         const char *psz_path = p_sys->url.psz_path;
911         if( !psz_path || !*psz_path )
912         {
913             psz_path = "/";
914         }
915         if( p_sys->url.i_port != 80)
916         {
917             net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
918                         "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
919                         psz_path, p_sys->i_version, p_sys->url.psz_host,
920                         p_sys->url.i_port );
921         }
922         else
923         {
924             net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
925                         "GET %s HTTP/1.%d\r\nHost: %s\r\n",
926                         psz_path, p_sys->i_version, p_sys->url.psz_host );
927         }
928     }
929     /* User Agent */
930     net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "User-Agent: %s\r\n",
931                 p_sys->psz_user_agent );
932     /* Offset */
933     if( p_sys->i_version == 1 )
934     {
935         net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
936                     "Range: bytes="I64Fd"-\r\n", i_tell );
937     }
938
939     /* Authentication */
940     if( p_sys->url.psz_username || p_sys->url.psz_password )
941     {
942         char buf[strlen( p_sys->url.psz_username ?: "" )
943                   + strlen( p_sys->url.psz_password ?: "" ) + 2];
944         char *b64;
945
946         snprintf( buf, sizeof( buf ), "%s:%s", p_sys->url.psz_username ?: "",
947                   p_sys->url.psz_password ?: "" );
948         b64 = vlc_b64_encode( buf );
949
950         if( b64 != NULL )
951         {
952              net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
953                          "Authorization: Basic %s\r\n", b64 );
954              free( b64 );
955         }
956     }
957
958     /* Proxy Authentication */
959     if( p_sys->proxy.psz_username || p_sys->proxy.psz_password )
960     {
961         char buf[strlen( p_sys->proxy.psz_username ?: "" )
962                   + strlen( p_sys->proxy.psz_password ?: "" )];
963         char *b64;
964
965         snprintf( buf, sizeof( buf ), "%s:%s", p_sys->proxy.psz_username ?: "",
966                   p_sys->proxy.psz_password ?: "" );
967         b64 = vlc_b64_encode( buf );
968
969         if( b64 != NULL)
970         {
971             net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
972                         "Proxy-Authorization: Basic %s\r\n", b64 );
973             free( b64 );
974         }
975     }
976
977     /* ICY meta data request */
978     net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
979
980
981     if( p_sys->b_continuous )
982     {
983         net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
984                     "Connection: Keep-Alive\r\n" );
985     }
986     else if( p_sys->i_version == 1 )
987     {
988         net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
989                     "Connection: Close\r\n");
990     }
991
992     if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "\r\n" ) < 0 )
993     {
994         msg_Err( p_access, "failed to send request" );
995         Disconnect( p_access );
996         return VLC_EGENERIC;
997     }
998
999     /* Read Answer */
1000     if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs ) ) == NULL )
1001     {
1002         msg_Err( p_access, "failed to read answer" );
1003         goto error;
1004     }
1005     if( !strncmp( psz, "HTTP/1.", 7 ) )
1006     {
1007         p_sys->psz_protocol = "HTTP";
1008         p_sys->i_code = atoi( &psz[9] );
1009     }
1010     else if( !strncmp( psz, "ICY", 3 ) )
1011     {
1012         p_sys->psz_protocol = "ICY";
1013         p_sys->i_code = atoi( &psz[4] );
1014         p_sys->b_reconnect = VLC_TRUE;
1015     }
1016     else
1017     {
1018         msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1019         free( psz );
1020         goto error;
1021     }
1022     msg_Dbg( p_access, "protocol '%s' answer code %d",
1023              p_sys->psz_protocol, p_sys->i_code );
1024     if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1025     {
1026         p_sys->b_seekable = VLC_FALSE;
1027     }
1028     if( p_sys->i_code != 206 && p_sys->i_code != 401 )
1029     {
1030         p_sys->b_seekable = VLC_FALSE;
1031     }
1032     /* Authentication error - We'll have to display the dialog */
1033     if( p_sys->i_code == 401 )
1034     {
1035
1036     }
1037     /* Other fatal error */
1038     else if( p_sys->i_code >= 400 )
1039     {
1040         msg_Err( p_access, "error: %s", psz );
1041         free( psz );
1042         goto error;
1043     }
1044     free( psz );
1045
1046     for( ;; )
1047     {
1048         char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs );
1049         char *p;
1050
1051         if( psz == NULL )
1052         {
1053             msg_Err( p_access, "failed to read answer" );
1054             goto error;
1055         }
1056
1057         /* msg_Dbg( p_input, "Line=%s", psz ); */
1058         if( *psz == '\0' )
1059         {
1060             free( psz );
1061             break;
1062         }
1063
1064
1065         if( ( p = strchr( psz, ':' ) ) == NULL )
1066         {
1067             msg_Err( p_access, "malformed header line: %s", psz );
1068             free( psz );
1069             goto error;
1070         }
1071         *p++ = '\0';
1072         while( *p == ' ' ) p++;
1073
1074         if( !strcasecmp( psz, "Content-Length" ) )
1075         {
1076             if( p_sys->b_continuous )
1077             {
1078                 p_access->info.i_size = -1;
1079                 msg_Dbg( p_access, "this frame size=%lld", atoll(p ) );
1080                 p_sys->i_remaining = atoll( p );
1081             }
1082             else
1083             {
1084                 p_access->info.i_size = i_tell + atoll( p );
1085                 msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
1086             }
1087         }
1088         else if( !strcasecmp( psz, "Location" ) )
1089         {
1090             char * psz_new_loc;
1091
1092             /* This does not follow RFC 2068, but yet if the url is not absolute,
1093              * handle it as everyone does. */
1094             if( p[0] == '/' )
1095             {
1096                 const char *psz_http_ext = p_sys->b_ssl ? "s" : "" ;
1097
1098                 if( p_sys->url.i_port == ( p_sys->b_ssl ? 443 : 80 ) )
1099                 {
1100                     asprintf(&psz_new_loc, "http%s://%s%s", psz_http_ext,
1101                              p_sys->url.psz_host, p);
1102                 }
1103                 else
1104                 {
1105                     asprintf(&psz_new_loc, "http%s://%s:%d%s", psz_http_ext,
1106                              p_sys->url.psz_host, p_sys->url.i_port, p);
1107                 }
1108             }
1109             else
1110             {
1111                 psz_new_loc = strdup( p );
1112             }
1113
1114             if( p_sys->psz_location ) free( p_sys->psz_location );
1115             p_sys->psz_location = psz_new_loc;
1116         }
1117         else if( !strcasecmp( psz, "Content-Type" ) )
1118         {
1119             if( p_sys->psz_mime ) free( p_sys->psz_mime );
1120             p_sys->psz_mime = strdup( p );
1121             msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1122         }
1123         else if( !strcasecmp( psz, "Pragma" ) )
1124         {
1125             if( !strcasecmp( psz, "Pragma: features" ) )
1126                 p_sys->b_mms = VLC_TRUE;
1127             if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
1128             p_sys->psz_pragma = strdup( p );
1129             msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1130         }
1131         else if( !strcasecmp( psz, "Server" ) )
1132         {
1133             msg_Dbg( p_access, "Server: %s", p );
1134             if( !strncasecmp( p, "Icecast", 7 ) ||
1135                 !strncasecmp( p, "Nanocaster", 10 ) )
1136             {
1137                 /* Remember if this is Icecast
1138                  * we need to force demux in this case without breaking
1139                  *  autodetection */
1140
1141                 /* Let live 365 streams (nanocaster) piggyback on the icecast
1142                  * routine. They look very similar */
1143
1144                 p_sys->b_reconnect = VLC_TRUE;
1145                 p_sys->b_pace_control = VLC_FALSE;
1146                 p_sys->b_icecast = VLC_TRUE;
1147             }
1148         }
1149         else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1150         {
1151             msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1152             if( !strncasecmp( p, "chunked", 7 ) )
1153             {
1154                 p_sys->b_chunked = VLC_TRUE;
1155             }
1156         }
1157         else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1158         {
1159             msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1160             p_sys->i_icy_meta = atoi( p );
1161             if( p_sys->i_icy_meta < 0 )
1162                 p_sys->i_icy_meta = 0;
1163
1164             msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1165         }
1166         else if( !strcasecmp( psz, "Icy-Name" ) )
1167         {
1168             if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
1169             p_sys->psz_icy_name = strdup( p );
1170             msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1171
1172             p_sys->b_icecast = VLC_TRUE; /* be on the safeside. set it here as well. */
1173             p_sys->b_reconnect = VLC_TRUE;
1174             p_sys->b_pace_control = VLC_FALSE;
1175         }
1176         else if( !strcasecmp( psz, "Icy-Genre" ) )
1177         {
1178             if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
1179             p_sys->psz_icy_genre = strdup( p );
1180             msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1181         }
1182         else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1183         {
1184             msg_Dbg( p_access, "Icy-Notice: %s", p );
1185         }
1186         else if( !strncasecmp( psz, "icy-", 4 ) ||
1187                  !strncasecmp( psz, "ice-", 4 ) ||
1188                  !strncasecmp( psz, "x-audiocast", 11 ) )
1189         {
1190             msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1191         }
1192
1193         free( psz );
1194     }
1195     return VLC_SUCCESS;
1196
1197 error:
1198     Disconnect( p_access );
1199     return VLC_EGENERIC;
1200 }
1201
1202 /*****************************************************************************
1203  * Disconnect:
1204  *****************************************************************************/
1205 static void Disconnect( access_t *p_access )
1206 {
1207     access_sys_t *p_sys = p_access->p_sys;
1208
1209     if( p_sys->p_tls != NULL)
1210     {
1211         tls_ClientDelete( p_sys->p_tls );
1212         p_sys->p_tls = NULL;
1213         p_sys->p_vs = NULL;
1214     }
1215     if( p_sys->fd != -1)
1216     {
1217         net_Close(p_sys->fd);
1218         p_sys->fd = -1;
1219     }
1220
1221 }