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