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