]> git.sesse.net Git - vlc/blob - modules/access/http.c
9adde3d245d8405fc787b1a87ef2231aee3f2b45
[vlc] / modules / access / http.c
1 /*****************************************************************************
2  * http.c: HTTP input module
3  *****************************************************************************
4  * Copyright (C) 2001-2005 VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/input.h>
32
33 #include "vlc_playlist.h"
34 #include "vlc_meta.h"
35 #include "network.h"
36 #include "vlc_tls.h"
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41 static int  Open ( vlc_object_t * );
42 static void Close( vlc_object_t * );
43
44 #define PROXY_TEXT N_("HTTP proxy")
45 #define PROXY_LONGTEXT N_( \
46     "You can specify an HTTP proxy to use. It must be of the form " \
47     "http://myproxy.mydomain:myport/. If none is specified, the HTTP_PROXY " \
48     "environment variable will be tried." )
49
50 #define CACHING_TEXT N_("Caching value in ms")
51 #define CACHING_LONGTEXT N_( \
52     "Allows you to modify the default caching value for http streams. This " \
53     "value should be set in millisecond units." )
54
55 #define USER_TEXT N_("HTTP user name")
56 #define USER_LONGTEXT N_("Allows you to modify the user name that will " \
57     "be used for the connection (Basic authentication only).")
58
59 #define PASS_TEXT N_("HTTP password")
60 #define PASS_LONGTEXT N_("Allows you to modify the password that will be " \
61     "used for the connection.")
62
63 #define AGENT_TEXT N_("HTTP user agent")
64 #define AGENT_LONGTEXT N_("Allows you to modify the user agent that will be " \
65     "used for the connection.")
66
67 #define RECONNECT_TEXT N_("Auto re-connect")
68 #define RECONNECT_LONGTEXT N_("Will automatically attempt a re-connection " \
69     "in case it was untimely closed.")
70
71 #define CONTINUOUS_TEXT N_("Continuous stream")
72 #define CONTINUOUS_LONGTEXT N_("Enable this option to read a file that is " \
73     "being constantly updated (for example, a JPG file on a server)")
74
75 vlc_module_begin();
76     set_description( _("HTTP input") );
77     set_capability( "access2", 0 );
78     set_shortname( _( "HTTP/HTTPS" ) );
79     set_category( CAT_INPUT );
80     set_subcategory( SUBCAT_INPUT_ACCESS );
81
82     add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
83                 VLC_FALSE );
84     add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
85                  CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
86     add_string( "http-user", NULL, NULL, USER_TEXT, USER_LONGTEXT, VLC_FALSE );
87     add_string( "http-pwd", NULL , NULL, PASS_TEXT, PASS_LONGTEXT, VLC_FALSE );
88     add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
89                 AGENT_LONGTEXT, VLC_FALSE );
90     add_bool( "http-reconnect", 0, NULL, RECONNECT_TEXT,
91               RECONNECT_LONGTEXT, VLC_TRUE );
92     add_bool( "http-continuous", 0, NULL, CONTINUOUS_TEXT,
93               CONTINUOUS_LONGTEXT, VLC_TRUE );
94
95     add_shortcut( "http" );
96     add_shortcut( "http4" );
97     add_shortcut( "http6" );
98     add_shortcut( "https" );
99     add_shortcut( "unsv" );
100     set_callbacks( Open, Close );
101 vlc_module_end();
102
103 /*****************************************************************************
104  * Local prototypes
105  *****************************************************************************/
106 struct access_sys_t
107 {
108     int fd;
109     tls_session_t *p_tls;
110     v_socket_t    *p_vs;
111
112     /* From uri */
113     vlc_url_t url;
114     char    *psz_user;
115     char    *psz_passwd;
116     char    *psz_user_agent;
117
118     /* Proxy */
119     vlc_bool_t b_proxy;
120     vlc_url_t  proxy;
121
122     /* */
123     int        i_code;
124     char       *psz_protocol;
125     int        i_version;
126
127     char       *psz_mime;
128     char       *psz_pragma;
129     char       *psz_location;
130     vlc_bool_t b_mms;
131     vlc_bool_t b_icecast;
132     vlc_bool_t b_ssl;
133
134     vlc_bool_t b_chunked;
135     int64_t    i_chunk;
136
137     int        i_icy_meta;
138     char       *psz_icy_name;
139     char       *psz_icy_genre;
140     char       *psz_icy_title;
141
142     int i_remaining;
143
144     vlc_bool_t b_seekable;
145     vlc_bool_t b_reconnect;
146     vlc_bool_t b_continuous;
147     vlc_bool_t b_pace_control;
148 };
149
150 /* */
151 static int Read( access_t *, uint8_t *, int );
152 static int Seek( access_t *, int64_t );
153 static int Control( access_t *, int, va_list );
154
155 /* */
156 static void ParseURL( access_sys_t *, char *psz_url );
157 static int  Connect( access_t *, int64_t );
158 static int Request( access_t *p_access, int64_t i_tell );
159 static void Disconnect( access_t * );
160
161 /*****************************************************************************
162  * Open:
163  *****************************************************************************/
164 static int Open( vlc_object_t *p_this )
165 {
166     access_t     *p_access = (access_t*)p_this;
167     access_sys_t *p_sys;
168     char         *psz;
169
170     /* First set ipv4/ipv6 */
171     var_Create( p_access, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
172     var_Create( p_access, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
173
174     if( *p_access->psz_access )
175     {
176         vlc_value_t val;
177         /* Find out which shortcut was used */
178         if( !strncmp( p_access->psz_access, "http4", 6 ) )
179         {
180             val.b_bool = VLC_TRUE;
181             var_Set( p_access, "ipv4", val );
182
183             val.b_bool = VLC_FALSE;
184             var_Set( p_access, "ipv6", val );
185         }
186         else if( !strncmp( p_access->psz_access, "http6", 6 ) )
187         {
188             val.b_bool = VLC_TRUE;
189             var_Set( p_access, "ipv6", val );
190
191             val.b_bool = VLC_FALSE;
192             var_Set( p_access, "ipv4", val );
193         }
194     }
195
196     /* Set up p_access */
197     p_access->pf_read = Read;
198     p_access->pf_block = NULL;
199     p_access->pf_control = Control;
200     p_access->pf_seek = Seek;
201     p_access->info.i_update = 0;
202     p_access->info.i_size = 0;
203     p_access->info.i_pos = 0;
204     p_access->info.b_eof = VLC_FALSE;
205     p_access->info.i_title = 0;
206     p_access->info.i_seekpoint = 0;
207     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
208     memset( p_sys, 0, sizeof( access_sys_t ) );
209     p_sys->fd = -1;
210     p_sys->b_proxy = VLC_FALSE;
211     p_sys->i_version = 1;
212     p_sys->b_seekable = VLC_TRUE;
213     p_sys->psz_mime = NULL;
214     p_sys->psz_pragma = NULL;
215     p_sys->b_mms = VLC_FALSE;
216     p_sys->b_icecast = VLC_FALSE;
217     p_sys->psz_location = NULL;
218     p_sys->psz_user_agent = NULL;
219     p_sys->b_pace_control = VLC_TRUE;
220     p_sys->b_ssl = VLC_FALSE;
221     p_sys->p_tls = NULL;
222     p_sys->p_vs = NULL;
223     p_sys->i_icy_meta = 0;
224     p_sys->psz_icy_name = NULL;
225     p_sys->psz_icy_genre = NULL;
226     p_sys->psz_icy_title = NULL;
227     p_sys->i_remaining = 0;
228
229     /* Parse URI */
230     ParseURL( p_sys, p_access->psz_path );
231     if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
232     {
233         msg_Warn( p_access, "invalid host" );
234         goto error;
235     }
236     if( !strncmp( p_access->psz_access, "https", 5 ) )
237     {
238         /* SSL over HTTP */
239         p_sys->b_ssl = VLC_TRUE;
240         if( p_sys->url.i_port <= 0 )
241             p_sys->url.i_port = 443;
242     }
243     else
244     {
245         if( p_sys->url.i_port <= 0 )
246             p_sys->url.i_port = 80;
247     }
248     if( !p_sys->psz_user || *p_sys->psz_user == '\0' )
249     {
250         p_sys->psz_user = var_CreateGetString( p_access, "http-user" );
251         p_sys->psz_passwd = var_CreateGetString( p_access, "http-pwd" );
252     }
253
254     /* Do user agent */
255     p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
256
257     /* Check proxy */
258     psz = var_CreateGetString( p_access, "http-proxy" );
259     if( *psz )
260     {
261         p_sys->b_proxy = VLC_TRUE;
262         vlc_UrlParse( &p_sys->proxy, psz, 0 );
263     }
264     else
265     {
266         char *psz_proxy = getenv( "http_proxy" );
267         if( psz_proxy && *psz_proxy )
268         {
269             p_sys->b_proxy = VLC_TRUE;
270             vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
271         }
272         if( psz_proxy )
273             free( psz_proxy );
274     }
275     free( psz );
276
277     if( p_sys->b_proxy )
278     {
279         if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
280         {
281             msg_Warn( p_access, "invalid proxy host" );
282             goto error;
283         }
284         if( p_sys->proxy.i_port <= 0 )
285         {
286             p_sys->proxy.i_port = 80;
287         }
288     }
289
290     msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
291              p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
292     if( p_sys->b_proxy )
293     {
294         msg_Dbg( p_access, "      proxy %s:%d", p_sys->proxy.psz_host,
295                  p_sys->proxy.i_port );
296     }
297     if( p_sys->psz_user && *p_sys->psz_user )
298     {
299         msg_Dbg( p_access, "      user='%s', pwd='%s'",
300                  p_sys->psz_user, p_sys->psz_passwd );
301     }
302
303     p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
304     p_sys->b_continuous = var_CreateGetBool( p_access, "http-continuous" );
305
306     /* Connect */
307     if( Connect( p_access, 0 ) )
308     {
309         /* Retry with http 1.0 */
310         p_sys->i_version = 0;
311
312         if( p_access->b_die ||
313             Connect( p_access, 0 ) )
314         {
315             goto error;
316         }
317     }
318
319     if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
320           p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
321         p_sys->psz_location && *p_sys->psz_location )
322     {
323         playlist_t * p_playlist;
324         input_item_t *p_input_item;
325
326         msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
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         if( p_sys->psz_user ) free( p_sys->psz_user );
356         if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
357
358         Disconnect( p_access );
359         free( p_sys );
360
361         /* Do new Open() run with new data */
362         return Open( p_this );
363     }
364
365     if( p_sys->b_mms )
366     {
367         msg_Dbg( p_access, "This is actually a live mms server, BAIL" );
368         goto error;
369     }
370
371     if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
372     {
373         if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
374         { 
375             if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
376                    !strcasecmp( p_sys->psz_mime, "video/nsa") )
377                 p_access->psz_demux = strdup( "nsv" );
378             else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
379                      !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
380                 p_access->psz_demux = strdup( "m4a" );
381             else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
382                 p_access->psz_demux = strdup( "mp3" );
383
384             msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
385                       p_access->psz_demux );
386
387 #if 0       /* Doesn't work really well because of the pre-buffering in
388              * shoutcast servers (the buffer content will be sent as fast as
389              * possible). */
390             p_sys->b_pace_control = VLC_FALSE;
391 #endif
392         }
393         else if( !p_sys->psz_mime )
394         {
395              /* Shoutcast */
396              p_access->psz_demux = strdup( "mp3" );
397         }
398         /* else probably Ogg Vorbis */
399     }
400
401     if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
402
403     /* PTS delay */
404     var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
405
406     return VLC_SUCCESS;
407
408 error:
409     vlc_UrlClean( &p_sys->url );
410     vlc_UrlClean( &p_sys->proxy );
411     if( p_sys->psz_mime ) free( p_sys->psz_mime );
412     if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
413     if( p_sys->psz_location ) free( p_sys->psz_location );
414     if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
415     if( p_sys->psz_user ) free( p_sys->psz_user );
416     if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
417
418     Disconnect( p_access );
419     free( p_sys );
420     return VLC_EGENERIC;
421 }
422
423 /*****************************************************************************
424  * Close:
425  *****************************************************************************/
426 static void Close( vlc_object_t *p_this )
427 {
428     access_t     *p_access = (access_t*)p_this;
429     access_sys_t *p_sys = p_access->p_sys;
430
431     vlc_UrlClean( &p_sys->url );
432     vlc_UrlClean( &p_sys->proxy );
433
434     if( p_sys->psz_user ) free( p_sys->psz_user );
435     if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
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[1];
605     char *psz_meta;
606     int i_read;
607     char *p;
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
615
616     if( buffer[0] <= 0 )
617         return VLC_SUCCESS;
618
619     msg_Dbg( p_access, "ICY meta size=%d", buffer[0] * 16);
620
621     psz_meta = malloc( buffer[0] * 16 + 1 );
622     i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs,
623                        psz_meta, buffer[0] * 16, VLC_TRUE );
624
625     if( i_read != buffer[0] * 16 )
626         return VLC_EGENERIC;
627
628     psz_meta[buffer[0]*16 + 1] = '\0'; /* Just in case */
629
630     msg_Dbg( p_access, "icy-meta=%s", psz_meta );
631
632     /* Now parse the meta */
633     /* Look for StreamTitle= */
634     p = strcasestr( psz_meta, "StreamTitle=" );
635     if( p )
636     {
637         p += strlen( "StreamTitle=" );
638         if( *p == '\'' || *p == '"' )
639         {
640             char *psz = strchr( &p[1], p[0] );
641             if( !psz )
642                 psz = strchr( &p[1], ';' );
643
644             if( psz ) *psz = '\0';
645         }
646         else
647         {
648             char *psz = strchr( &p[1], ';' );
649             if( psz ) *psz = '\0';
650         }
651
652         if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
653
654         p_sys->psz_icy_title = strdup( &p[1] );
655
656         p_access->info.i_update |= INPUT_UPDATE_META;
657     }
658
659     free( psz_meta );
660
661     msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
662
663     return VLC_SUCCESS;
664 }
665
666 /*****************************************************************************
667  * Seek: close and re-open a connection at the right place
668  *****************************************************************************/
669 static int Seek( access_t *p_access, int64_t i_pos )
670 {
671     msg_Dbg( p_access, "trying to seek to "I64Fd, i_pos );
672
673     Disconnect( p_access );
674
675     if( Connect( p_access, i_pos ) )
676     {
677         msg_Err( p_access, "seek failed" );
678         p_access->info.b_eof = VLC_TRUE;
679         return VLC_EGENERIC;
680     }
681     return VLC_SUCCESS;
682 }
683
684 /*****************************************************************************
685  * Control:
686  *****************************************************************************/
687 static int Control( access_t *p_access, int i_query, va_list args )
688 {
689     access_sys_t *p_sys = p_access->p_sys;
690     vlc_bool_t   *pb_bool;
691     int          *pi_int;
692     int64_t      *pi_64;
693     vlc_meta_t **pp_meta;
694
695     switch( i_query )
696     {
697         /* */
698         case ACCESS_CAN_SEEK:
699             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
700             *pb_bool = p_sys->b_seekable;
701             break;
702         case ACCESS_CAN_FASTSEEK:
703             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
704             *pb_bool = VLC_FALSE;
705             break;
706         case ACCESS_CAN_PAUSE:
707         case ACCESS_CAN_CONTROL_PACE:
708             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
709
710 #if 0       /* Disable for now until we have a clock synchro algo
711              * which works with something else than MPEG over UDP */
712             *pb_bool = p_sys->b_pace_control;
713 #endif
714             *pb_bool = VLC_TRUE;
715             break;
716
717         /* */
718         case ACCESS_GET_MTU:
719             pi_int = (int*)va_arg( args, int * );
720             *pi_int = 0;
721             break;
722
723         case ACCESS_GET_PTS_DELAY:
724             pi_64 = (int64_t*)va_arg( args, int64_t * );
725             *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
726             break;
727
728         /* */
729         case ACCESS_SET_PAUSE_STATE:
730             break;
731
732         case ACCESS_GET_META:
733             pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
734             *pp_meta = vlc_meta_New();
735             msg_Dbg( p_access, "GET META %s %s %s",
736                      p_sys->psz_icy_name, p_sys->psz_icy_genre, p_sys->psz_icy_title );
737             if( p_sys->psz_icy_name )
738                 vlc_meta_Add( *pp_meta, VLC_META_DESCRIPTION,
739                               p_sys->psz_icy_name );
740             if( p_sys->psz_icy_genre )
741                 vlc_meta_Add( *pp_meta, VLC_META_GENRE,
742                               p_sys->psz_icy_genre );
743             if( p_sys->psz_icy_title )
744                 vlc_meta_Add( *pp_meta, VLC_META_TITLE,
745                               p_sys->psz_icy_title );
746             break;
747
748         case ACCESS_GET_TITLE_INFO:
749         case ACCESS_SET_TITLE:
750         case ACCESS_SET_SEEKPOINT:
751         case ACCESS_SET_PRIVATE_ID_STATE:
752             return VLC_EGENERIC;
753
754         default:
755             msg_Warn( p_access, "unimplemented query in control" );
756             return VLC_EGENERIC;
757
758     }
759     return VLC_SUCCESS;
760 }
761
762 /*****************************************************************************
763  * ParseURL: extract user:password
764  *****************************************************************************/
765 static void ParseURL( access_sys_t *p_sys, char *psz_url )
766 {
767     char *psz_dup = strdup( psz_url );
768     char *p = psz_dup;
769     char *psz;
770
771     /* Syntax //[user:password]@<hostname>[:<port>][/<path>] */
772     while( *p == '/' )
773     {
774         p++;
775     }
776     psz = p;
777
778     /* Parse auth */
779     if( ( p = strchr( psz, '@' ) ) )
780     {
781         char *comma;
782
783         *p++ = '\0';
784         comma = strchr( psz, ':' );
785
786         /* Retreive user:password */
787         if( comma )
788         {
789             *comma++ = '\0';
790
791             p_sys->psz_user = strdup( psz );
792             p_sys->psz_passwd = strdup( comma );
793         }
794         else
795         {
796             p_sys->psz_user = strdup( psz );
797         }
798     }
799     else
800     {
801         p = psz;
802     }
803
804     /* Parse uri */
805     vlc_UrlParse( &p_sys->url, p, 0 );
806
807     free( psz_dup );
808 }
809
810 /*****************************************************************************
811  * Connect:
812  *****************************************************************************/
813 static int Connect( access_t *p_access, int64_t i_tell )
814 {
815     access_sys_t   *p_sys = p_access->p_sys;
816     vlc_url_t      srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
817
818     /* Clean info */
819     if( p_sys->psz_location ) free( p_sys->psz_location );
820     if( p_sys->psz_mime ) free( p_sys->psz_mime );
821     if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
822
823     if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
824     if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
825     if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
826
827
828     p_sys->psz_location = NULL;
829     p_sys->psz_mime = NULL;
830     p_sys->psz_pragma = NULL;
831     p_sys->b_mms = VLC_FALSE;
832     p_sys->b_chunked = VLC_FALSE;
833     p_sys->i_chunk = 0;
834     p_sys->i_icy_meta = 0;
835     p_sys->psz_icy_name = NULL;
836     p_sys->psz_icy_genre = NULL;
837     p_sys->psz_icy_title = NULL;
838
839     p_access->info.i_size = 0;
840     p_access->info.i_pos  = i_tell;
841     p_access->info.b_eof  = VLC_FALSE;
842
843
844     /* Open connection */
845     p_sys->fd = net_OpenTCP( p_access, srv.psz_host, srv.i_port );
846     if( p_sys->fd < 0 )
847     {
848         msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
849         return VLC_EGENERIC;
850     }
851
852     /* Initialize TLS/SSL session */
853     /* FIXME: support proxy CONNECT for HTTP/SSL */
854     if( p_sys->b_ssl == VLC_TRUE )
855     {
856         if( p_sys->b_proxy )
857         {
858             msg_Err( p_access, "HTTP/SSL through HTTP proxy not supported yet" );
859             Disconnect( p_access );
860             return VLC_EGENERIC;
861         }
862
863         p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), NULL, p_sys->fd );
864         if( p_sys->p_tls == NULL )
865         {
866             msg_Err( p_access, "cannot establish HTTP/SSL session" );
867             Disconnect( p_access );
868             return VLC_EGENERIC;
869         }
870         p_sys->p_vs = &p_sys->p_tls->sock;
871     }
872
873     return Request( p_access,i_tell );
874 }
875
876
877 static int Request( access_t *p_access, int64_t i_tell )
878 {
879     access_sys_t   *p_sys = p_access->p_sys;
880     char           *psz ;
881     v_socket_t     *pvs = p_sys->p_vs;
882
883     if( p_sys->b_proxy )
884     {
885         if( p_sys->url.psz_path )
886         {
887             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
888                         "GET http://%s:%d%s HTTP/1.%d\r\n",
889                         p_sys->url.psz_host, p_sys->url.i_port,
890                         p_sys->url.psz_path, p_sys->i_version );
891         }
892         else
893         {
894             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
895                         "GET http://%s:%d/ HTTP/1.%d\r\n",
896                         p_sys->url.psz_host, p_sys->url.i_port,
897                         p_sys->i_version );
898         }
899     }
900     else
901     {
902         char *psz_path = p_sys->url.psz_path;
903         if( !psz_path || !*psz_path )
904         {
905             psz_path = "/";
906         }
907         if( p_sys->url.i_port != 80)
908         {
909             net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
910                         "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
911                         psz_path, p_sys->i_version, p_sys->url.psz_host,
912                         p_sys->url.i_port );
913         }
914         else
915         {
916             net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
917                         "GET %s HTTP/1.%d\r\nHost: %s\r\n",
918                         psz_path, p_sys->i_version, p_sys->url.psz_host );
919         }
920     }
921     /* User Agent */
922     net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "User-Agent: %s\r\n",
923                 p_sys->psz_user_agent );
924     /* Offset */
925     if( p_sys->i_version == 1 )
926     {
927         net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
928                     "Range: bytes="I64Fd"-\r\n", i_tell );
929     }
930
931     /* Authentification */
932     if( p_sys->psz_user && *p_sys->psz_user )
933     {
934         char *buf;
935         char *b64;
936
937         asprintf( &buf, "%s:%s", p_sys->psz_user,
938                    p_sys->psz_passwd ? p_sys->psz_passwd : "" );
939
940         b64 = vlc_b64_encode( buf );
941         free( buf );
942
943         net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
944                     "Authorization: Basic %s\r\n", b64 );
945         free( b64 );
946     }
947
948     /* ICY meta data request */
949     net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
950
951
952     if( p_sys->b_continuous && p_sys->i_version == 1 )
953     {
954         net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
955                     "Connection: keep-alive\r\n" );
956     }
957     else
958     {
959         net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
960                     "Connection: Close\r\n");
961         p_sys->b_continuous = VLC_FALSE;
962     }
963
964     if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "\r\n" ) < 0 )
965     {
966         msg_Err( p_access, "failed to send request" );
967         Disconnect( p_access );
968         return VLC_EGENERIC;
969     }
970
971     /* Read Answer */
972     if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs ) ) == NULL )
973     {
974         msg_Err( p_access, "failed to read answer" );
975         goto error;
976     }
977     if( !strncmp( psz, "HTTP/1.", 7 ) )
978     {
979         p_sys->psz_protocol = "HTTP";
980         p_sys->i_code = atoi( &psz[9] );
981     }
982     else if( !strncmp( psz, "ICY", 3 ) )
983     {
984         p_sys->psz_protocol = "ICY";
985         p_sys->i_code = atoi( &psz[4] );
986         p_sys->b_reconnect = VLC_TRUE;
987     }
988     else
989     {
990         msg_Err( p_access, "invalid HTTP reply '%s'", psz );
991         free( psz );
992         goto error;
993     }
994     msg_Dbg( p_access, "protocol '%s' answer code %d",
995              p_sys->psz_protocol, p_sys->i_code );
996     if( !strcmp( p_sys->psz_protocol, "ICY" ) )
997     {
998         p_sys->b_seekable = VLC_FALSE;
999     }
1000     if( p_sys->i_code != 206 )
1001     {
1002         p_sys->b_seekable = VLC_FALSE;
1003     }
1004     if( p_sys->i_code >= 400 )
1005     {
1006         msg_Err( p_access, "error: %s", psz );
1007         free( psz );
1008         goto error;
1009     }
1010     free( psz );
1011
1012     for( ;; )
1013     {
1014         char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs );
1015         char *p;
1016
1017         if( psz == NULL )
1018         {
1019             msg_Err( p_access, "failed to read answer" );
1020             goto error;
1021         }
1022
1023         /* msg_Dbg( p_input, "Line=%s", psz ); */
1024         if( *psz == '\0' )
1025         {
1026             free( psz );
1027             break;
1028         }
1029
1030
1031         if( ( p = strchr( psz, ':' ) ) == NULL )
1032         {
1033             msg_Err( p_access, "malformed header line: %s", psz );
1034             free( psz );
1035             goto error;
1036         }
1037         *p++ = '\0';
1038         while( *p == ' ' ) p++;
1039
1040         if( !strcasecmp( psz, "Content-Length" ) )
1041         {
1042             if( p_sys->b_continuous )
1043             {
1044                 p_access->info.i_size = -1;
1045                 msg_Dbg( p_access, "this frame size="I64Fd, atoll(p ) );
1046                 p_sys->i_remaining = atoll( p );
1047             }
1048             else
1049             {
1050                 p_access->info.i_size = i_tell + atoll( p );
1051                 msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
1052             }
1053         }
1054         else if( !strcasecmp( psz, "Location" ) )
1055         {
1056             if( p_sys->psz_location ) free( p_sys->psz_location );
1057             p_sys->psz_location = strdup( p );
1058         }
1059         else if( !strcasecmp( psz, "Content-Type" ) )
1060         {
1061             if( p_sys->psz_mime ) free( p_sys->psz_mime );
1062             p_sys->psz_mime = strdup( p );
1063             msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1064         }
1065         else if( !strcasecmp( psz, "Pragma" ) )
1066         {
1067             if( !strcasecmp( psz, "Pragma: features" ) )
1068                 p_sys->b_mms = VLC_TRUE;
1069             if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
1070             p_sys->psz_pragma = strdup( p );
1071             msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1072         }
1073         else if( !strcasecmp( psz, "Server" ) )
1074         {
1075             msg_Dbg( p_access, "Server: %s", p );
1076             if( !strncasecmp( p, "Icecast", 7 ) ||
1077                 !strncasecmp( p, "Nanocaster", 10 ) )
1078             {
1079                 /* Remember if this is Icecast
1080                  * we need to force demux in this case without breaking
1081                  *  autodetection */
1082
1083                 /* Let live 365 streams (nanocaster) piggyback on the icecast
1084                  * routine. They look very similar */
1085
1086                 p_sys->b_reconnect = VLC_TRUE;
1087                 p_sys->b_pace_control = VLC_FALSE;
1088                 p_sys->b_icecast = VLC_TRUE;
1089             }
1090         }
1091         else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1092         {
1093             msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1094             if( !strncasecmp( p, "chunked", 7 ) )
1095             {
1096                 p_sys->b_chunked = VLC_TRUE;
1097             }
1098         }
1099         else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1100         {
1101             msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1102             p_sys->i_icy_meta = atoi( p );
1103             if( p_sys->i_icy_meta < 0 )
1104                 p_sys->i_icy_meta = 0;
1105
1106             msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1107         }
1108         else if( !strcasecmp( psz, "Icy-Name" ) )
1109         {
1110             if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
1111             p_sys->psz_icy_name = strdup( p );
1112             msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1113
1114             p_sys->b_icecast = VLC_TRUE; /* be on the safeside. set it here as well. */
1115             p_sys->b_reconnect = VLC_TRUE;
1116             p_sys->b_pace_control = VLC_FALSE;
1117         }
1118         else if( !strcasecmp( psz, "Icy-Genre" ) )
1119         {
1120             if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
1121             p_sys->psz_icy_genre = strdup( p );
1122             msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1123         }
1124         else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1125         {
1126             msg_Dbg( p_access, "Icy-Notice: %s", p );
1127         }
1128         else if( !strncasecmp( psz, "icy-", 4 ) ||
1129                  !strncasecmp( psz, "ice-", 4 ) ||
1130                  !strncasecmp( psz, "x-audiocast", 11 ) )
1131         {
1132             msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1133         }
1134
1135         free( psz );
1136     }
1137     return VLC_SUCCESS;
1138
1139 error:
1140     Disconnect( p_access );
1141     return VLC_EGENERIC;
1142 }
1143
1144 /*****************************************************************************
1145  * Disconnect:
1146  *****************************************************************************/
1147 static void Disconnect( access_t *p_access )
1148 {
1149     access_sys_t *p_sys = p_access->p_sys;
1150
1151     if( p_sys->p_tls != NULL)
1152     {
1153         tls_ClientDelete( p_sys->p_tls );
1154         p_sys->p_tls = NULL;
1155         p_sys->p_vs = NULL;
1156     }
1157     if( p_sys->fd != -1)
1158     {
1159         net_Close(p_sys->fd);
1160         p_sys->fd = -1;
1161     }
1162     
1163 }