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