]> git.sesse.net Git - vlc/blob - modules/access/http.c
Don't get stuck in Connect() when module is in Open() function, when it is being...
[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 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc/vlc.h>
34
35
36 #include <vlc_access.h>
37
38 #include <vlc_interface.h>
39 #include <vlc_meta.h>
40 #include <vlc_network.h>
41 #include <vlc_url.h>
42 #include <vlc_tls.h>
43 #include <vlc_strings.h>
44 #include <vlc_input.h>
45
46 #ifdef HAVE_ZLIB_H
47 #   include <zlib.h>
48 #endif
49
50 /*****************************************************************************
51  * Module descriptor
52  *****************************************************************************/
53 static int  Open ( vlc_object_t * );
54 static void Close( vlc_object_t * );
55
56 #define PROXY_TEXT N_("HTTP proxy")
57 #define PROXY_LONGTEXT N_( \
58     "HTTP proxy to be used It must be of the form " \
59     "http://[user[:pass]@]myproxy.mydomain:myport/ ; " \
60     "if empty, the http_proxy environment variable will be tried." )
61
62 #define CACHING_TEXT N_("Caching value in ms")
63 #define CACHING_LONGTEXT N_( \
64     "Caching value for HTTP streams. This " \
65     "value should be set in milliseconds." )
66
67 #define AGENT_TEXT N_("HTTP user agent")
68 #define AGENT_LONGTEXT N_("User agent that will be " \
69     "used for the connection.")
70
71 #define RECONNECT_TEXT N_("Auto re-connect")
72 #define RECONNECT_LONGTEXT N_( \
73     "Automatically try to reconnect to the stream in case of a sudden " \
74     "disconnect." )
75
76 #define CONTINUOUS_TEXT N_("Continuous stream")
77 #define CONTINUOUS_LONGTEXT N_("Read a file that is " \
78     "being constantly updated (for example, a JPG file on a server). " \
79     "You should not globally enable this option as it will break all other " \
80     "types of HTTP streams." )
81
82 #define FORWARD_COOKIES_TEXT N_("Forward Cookies")
83 #define FORWARD_COOKIES_LONGTEXT N_("Forward Cookies Across http redirections ")
84
85 vlc_module_begin();
86     set_description( _("HTTP input") );
87     set_capability( "access2", 0 );
88     set_shortname( _( "HTTP(S)" ) );
89     set_category( CAT_INPUT );
90     set_subcategory( SUBCAT_INPUT_ACCESS );
91
92     add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
93                 VLC_FALSE );
94     add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
95                  CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
96     add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
97                 AGENT_LONGTEXT, VLC_TRUE );
98     add_bool( "http-reconnect", 0, NULL, RECONNECT_TEXT,
99               RECONNECT_LONGTEXT, VLC_TRUE );
100     add_bool( "http-continuous", 0, NULL, CONTINUOUS_TEXT,
101               CONTINUOUS_LONGTEXT, VLC_TRUE );
102     add_bool( "http-forward-cookies", 0, NULL, FORWARD_COOKIES_TEXT,
103               FORWARD_COOKIES_LONGTEXT, VLC_TRUE );
104     add_obsolete_string("http-user");
105     add_obsolete_string("http-pwd");
106     add_shortcut( "http" );
107     add_shortcut( "https" );
108     add_shortcut( "unsv" );
109     add_shortcut( "itpc" ); /* iTunes Podcast */
110     set_callbacks( Open, Close );
111 vlc_module_end();
112
113 /*****************************************************************************
114  * Local prototypes
115  *****************************************************************************/
116 struct access_sys_t
117 {
118     int fd;
119     tls_session_t *p_tls;
120     v_socket_t    *p_vs;
121
122     /* From uri */
123     vlc_url_t url;
124     char    *psz_user_agent;
125
126     /* Proxy */
127     vlc_bool_t b_proxy;
128     vlc_url_t  proxy;
129
130     /* */
131     int        i_code;
132     const char *psz_protocol;
133     int        i_version;
134
135     char       *psz_mime;
136     char       *psz_pragma;
137     char       *psz_location;
138     vlc_bool_t b_mms;
139     vlc_bool_t b_icecast;
140     vlc_bool_t b_ssl;
141 #ifdef HAVE_ZLIB_H
142     vlc_bool_t b_compressed;
143     struct
144     {
145         z_stream   stream;
146         uint8_t   *p_buffer;
147     } inflate;
148 #endif
149
150     vlc_bool_t b_chunked;
151     int64_t    i_chunk;
152
153     int        i_icy_meta;
154     char       *psz_icy_name;
155     char       *psz_icy_genre;
156     char       *psz_icy_title;
157
158     int i_remaining;
159
160     vlc_bool_t b_seekable;
161     vlc_bool_t b_reconnect;
162     vlc_bool_t b_continuous;
163     vlc_bool_t b_pace_control;
164
165     vlc_array_t * cookies;
166 };
167
168 /* */
169 static int OpenWithCookies( vlc_object_t *p_this, vlc_array_t *cookies );
170
171 /* */
172 static ssize_t Read( access_t *, uint8_t *, size_t );
173 static ssize_t ReadCompressed( access_t *, uint8_t *, size_t );
174 static int Seek( access_t *, int64_t );
175 static int Control( access_t *, int, va_list );
176
177 /* */
178 static int Connect( access_t *, int64_t );
179 static int Request( access_t *p_access, int64_t i_tell );
180 static void Disconnect( access_t * );
181
182 /* Small Cookie utilities. Cookies support is partial. */
183 static char * cookie_get_content( const char * cookie );
184 static char * cookie_get_domain( const char * cookie );
185 static char * cookie_get_name( const char * cookie );
186 static void cookie_append( vlc_array_t * cookies, char * cookie );
187
188 /*****************************************************************************
189  * Open:
190  *****************************************************************************/
191 static int Open( vlc_object_t *p_this )
192 {
193     return OpenWithCookies( p_this, NULL );
194 }
195
196 static int OpenWithCookies( vlc_object_t *p_this, vlc_array_t *cookies )
197 {
198     access_t     *p_access = (access_t*)p_this;
199     access_sys_t *p_sys;
200     char         *psz, *p;
201     /* Only forward an store cookies if the corresponding option is activated */
202     vlc_bool_t   b_forward_cookies = var_CreateGetBool( p_access, "http-forward-cookies" );
203     vlc_array_t * saved_cookies = b_forward_cookies ? (cookies ?: vlc_array_new()) : NULL;
204
205     /* Set up p_access */
206     STANDARD_READ_ACCESS_INIT;
207 #ifdef HAVE_ZLIB_H
208     p_access->pf_read = ReadCompressed;
209 #endif
210     p_sys->fd = -1;
211     p_sys->b_proxy = VLC_FALSE;
212     p_sys->i_version = 1;
213     p_sys->b_seekable = VLC_TRUE;
214     p_sys->psz_mime = NULL;
215     p_sys->psz_pragma = NULL;
216     p_sys->b_mms = VLC_FALSE;
217     p_sys->b_icecast = VLC_FALSE;
218     p_sys->psz_location = NULL;
219     p_sys->psz_user_agent = NULL;
220     p_sys->b_pace_control = VLC_TRUE;
221     p_sys->b_ssl = VLC_FALSE;
222 #ifdef HAVE_ZLIB_H
223     p_sys->b_compressed = VLC_FALSE;
224     /* 15 is the max windowBits, +32 to enable optional gzip decoding */
225     if( inflateInit2( &p_sys->inflate.stream, 32+15 ) != Z_OK )
226         msg_Warn( p_access, "Error during zlib initialisation: %s",
227                   p_sys->inflate.stream.msg );
228     if( zlibCompileFlags() & (1<<17) )
229         msg_Warn( p_access, "Your zlib was compiled without gzip support." );
230     p_sys->inflate.p_buffer = NULL;
231 #endif
232     p_sys->p_tls = NULL;
233     p_sys->p_vs = NULL;
234     p_sys->i_icy_meta = 0;
235     p_sys->psz_icy_name = NULL;
236     p_sys->psz_icy_genre = NULL;
237     p_sys->psz_icy_title = NULL;
238     p_sys->i_remaining = 0;
239
240     p_sys->cookies = saved_cookies;
241
242     /* Parse URI - remove spaces */
243     p = psz = strdup( p_access->psz_path );
244     while( (p = strchr( p, ' ' )) != NULL )
245         *p = '+';
246     vlc_UrlParse( &p_sys->url, psz, 0 );
247     free( psz );
248
249     if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
250     {
251         msg_Warn( p_access, "invalid host" );
252         goto error;
253     }
254     if( !strncmp( p_access->psz_access, "https", 5 ) )
255     {
256         /* HTTP over SSL */
257         p_sys->b_ssl = VLC_TRUE;
258         if( p_sys->url.i_port <= 0 )
259             p_sys->url.i_port = 443;
260     }
261     else
262     {
263         if( p_sys->url.i_port <= 0 )
264             p_sys->url.i_port = 80;
265     }
266
267     /* Do user agent */
268     p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
269
270     /* Check proxy */
271     psz = var_CreateGetString( p_access, "http-proxy" );
272     if( *psz )
273     {
274         p_sys->b_proxy = VLC_TRUE;
275         vlc_UrlParse( &p_sys->proxy, psz, 0 );
276     }
277 #ifdef HAVE_GETENV
278     else
279     {
280         char *psz_proxy = getenv( "http_proxy" );
281         if( psz_proxy && *psz_proxy )
282         {
283             p_sys->b_proxy = VLC_TRUE;
284             vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
285         }
286     }
287 #endif
288     free( psz );
289
290     if( p_sys->b_proxy )
291     {
292         if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
293         {
294             msg_Warn( p_access, "invalid proxy host" );
295             goto error;
296         }
297         if( p_sys->proxy.i_port <= 0 )
298         {
299             p_sys->proxy.i_port = 80;
300         }
301     }
302
303     msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
304              p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
305     if( p_sys->b_proxy )
306     {
307         msg_Dbg( p_access, "      proxy %s:%d", p_sys->proxy.psz_host,
308                  p_sys->proxy.i_port );
309     }
310     if( p_sys->url.psz_username && *p_sys->url.psz_username )
311     {
312         msg_Dbg( p_access, "      user='%s', pwd='%s'",
313                  p_sys->url.psz_username, p_sys->url.psz_password );
314     }
315
316     p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
317     p_sys->b_continuous = var_CreateGetBool( p_access, "http-continuous" );
318
319 connect:
320     /* Connect */
321     switch( Connect( p_access, 0 ) )
322     {
323         case -1:
324             goto error;
325
326         case -2:
327             /* Retry with http 1.0 */
328             msg_Dbg( p_access, "switching to HTTP version 1.0" );
329             p_sys->i_version = 0;
330             p_sys->b_seekable = VLC_FALSE;
331
332             if( p_access->b_die || Connect( p_access, 0 ) )
333                 goto error;
334
335 #ifdef DEBUG
336         case 0:
337             break;
338
339         default:
340             msg_Err( p_access, "You should not be here" );
341             abort();
342 #endif
343     }
344
345     if( p_sys->i_code == 401 )
346     {
347         char *psz_login = NULL; char *psz_password = NULL;
348         int i_ret;
349         msg_Dbg( p_access, "authentication failed" );
350         i_ret = intf_UserLoginPassword( p_access, _("HTTP authentication"),
351                         _("Please enter a valid login name and a password."),
352                                                 &psz_login, &psz_password );
353         if( i_ret == DIALOG_OK_YES )
354         {
355             msg_Dbg( p_access, "retrying with user=%s, pwd=%s",
356                         psz_login, psz_password );
357             if( psz_login ) p_sys->url.psz_username = strdup( psz_login );
358             if( psz_password ) p_sys->url.psz_password = strdup( psz_password );
359             if( psz_login ) free( psz_login );
360             if( psz_password ) free( psz_password );
361             goto connect;
362         }
363         else
364         {
365             if( psz_login ) free( psz_login );
366             if( psz_password ) free( psz_password );
367             goto error;
368         }
369     }
370
371     if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
372           p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
373         p_sys->psz_location && *p_sys->psz_location )
374     {
375         msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
376
377         /* Do not accept redirection outside of HTTP works */
378         if( strncmp( p_sys->psz_location, "http", 4 )
379          || ( ( p_sys->psz_location[4] != ':' ) /* HTTP */
380            && strncmp( p_sys->psz_location + 4, "s:", 2 ) /* HTTP/SSL */ ) )
381         {
382             msg_Err( p_access, "insecure redirection ignored" );
383             goto error;
384         }
385         free( p_access->psz_path );
386         p_access->psz_path = strdup( p_sys->psz_location );
387         /* Clean up current Open() run */
388         vlc_UrlClean( &p_sys->url );
389         vlc_UrlClean( &p_sys->proxy );
390         free( p_sys->psz_mime );
391         free( p_sys->psz_pragma );
392         free( p_sys->psz_location );
393         free( p_sys->psz_user_agent );
394
395         Disconnect( p_access );
396         cookies = p_sys->cookies;
397         free( p_sys );
398
399         /* Do new Open() run with new data */
400         return OpenWithCookies( p_this, cookies );
401     }
402
403     if( p_sys->b_mms )
404     {
405         msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
406         goto error;
407     }
408
409     if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
410     {
411         if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
412         {
413             if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
414                 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
415                 p_access->psz_demux = strdup( "nsv" );
416             else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
417                      !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
418                 p_access->psz_demux = strdup( "m4a" );
419             else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
420                 p_access->psz_demux = strdup( "mp3" );
421
422             msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
423                       p_access->psz_demux );
424
425 #if 0       /* Doesn't work really well because of the pre-buffering in
426              * shoutcast servers (the buffer content will be sent as fast as
427              * possible). */
428             p_sys->b_pace_control = VLC_FALSE;
429 #endif
430         }
431         else if( !p_sys->psz_mime )
432         {
433              /* Shoutcast */
434              p_access->psz_demux = strdup( "mp3" );
435         }
436         /* else probably Ogg Vorbis */
437     }
438     else if( !strcasecmp( p_access->psz_access, "unsv" ) &&
439              p_sys->psz_mime &&
440              !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
441     {
442         /* Grrrr! detect ultravox server and force NSV demuxer */
443         p_access->psz_demux = strdup( "nsv" );
444     }
445     else if( !strcmp( p_access->psz_access, "itpc" ) )
446     {
447         p_access->psz_demux = strdup( "podcast" );
448     }
449     else if( p_sys->psz_mime &&
450              !strncasecmp( p_sys->psz_mime, "application/xspf+xml", 20 ) &&
451              ( memchr( " ;\t", p_sys->psz_mime[20], 4 ) != NULL ) )
452         p_access->psz_demux = strdup( "xspf-open" );
453
454     if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
455
456     /* PTS delay */
457     var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
458
459     return VLC_SUCCESS;
460
461 error:
462     vlc_UrlClean( &p_sys->url );
463     vlc_UrlClean( &p_sys->proxy );
464     free( p_sys->psz_mime );
465     free( p_sys->psz_pragma );
466     free( p_sys->psz_location );
467     free( p_sys->psz_user_agent );
468
469     Disconnect( p_access );
470     free( p_sys );
471     return VLC_EGENERIC;
472 }
473
474 /*****************************************************************************
475  * Close:
476  *****************************************************************************/
477 static void Close( vlc_object_t *p_this )
478 {
479     access_t     *p_access = (access_t*)p_this;
480     access_sys_t *p_sys = p_access->p_sys;
481
482     vlc_UrlClean( &p_sys->url );
483     vlc_UrlClean( &p_sys->proxy );
484
485     free( p_sys->psz_mime );
486     free( p_sys->psz_pragma );
487     free( p_sys->psz_location );
488
489     free( p_sys->psz_icy_name );
490     free( p_sys->psz_icy_genre );
491     free( p_sys->psz_icy_title );
492
493     free( p_sys->psz_user_agent );
494
495     Disconnect( p_access );
496
497     if( p_sys->cookies )
498     {
499         int i;
500         for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
501             free(vlc_array_item_at_index( p_sys->cookies, i ));
502         vlc_array_destroy( p_sys->cookies );
503     }
504
505 #ifdef HAVE_ZLIB_H
506     inflateEnd( &p_sys->inflate.stream );
507     free( p_sys->inflate.p_buffer );
508 #endif
509
510     free( p_sys );
511 }
512
513 /*****************************************************************************
514  * Read: Read up to i_len bytes from the http connection and place in
515  * p_buffer. Return the actual number of bytes read
516  *****************************************************************************/
517 static int ReadICYMeta( access_t *p_access );
518 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
519 {
520     access_sys_t *p_sys = p_access->p_sys;
521     int i_read;
522
523     if( p_sys->fd < 0 )
524     {
525         p_access->info.b_eof = VLC_TRUE;
526         return 0;
527     }
528
529     if( p_access->info.i_size > 0 &&
530         i_len + p_access->info.i_pos > p_access->info.i_size )
531     {
532         if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
533         {
534             p_access->info.b_eof = VLC_TRUE;
535             return 0;
536         }
537     }
538
539     if( p_sys->b_chunked )
540     {
541         if( p_sys->i_chunk < 0 )
542         {
543             p_access->info.b_eof = VLC_TRUE;
544             return 0;
545         }
546
547         if( p_sys->i_chunk <= 0 )
548         {
549             char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
550             /* read the chunk header */
551             if( psz == NULL )
552             {
553                 /* fatal error - end of file */
554                 msg_Dbg( p_access, "failed reading chunk-header line" );
555                 return 0;
556             }
557             p_sys->i_chunk = strtoll( psz, NULL, 16 );
558             free( psz );
559
560             if( p_sys->i_chunk <= 0 )   /* eof */
561             {
562                 p_sys->i_chunk = -1;
563                 p_access->info.b_eof = VLC_TRUE;
564                 return 0;
565             }
566         }
567
568         if( i_len > p_sys->i_chunk )
569         {
570             i_len = p_sys->i_chunk;
571         }
572     }
573
574     if( p_sys->b_continuous && (ssize_t)i_len > p_sys->i_remaining )
575     {
576         /* Only ask for the remaining length */
577         int i_new_len = p_sys->i_remaining;
578         if( i_new_len == 0 )
579         {
580             Request( p_access, 0 );
581             i_read = Read( p_access, p_buffer, i_len );
582             return i_read;
583         }
584         i_len = i_new_len;
585     }
586
587     if( p_sys->i_icy_meta > 0 && p_access->info.i_pos > 0 )
588     {
589         int64_t i_next = p_sys->i_icy_meta -
590                                     p_access->info.i_pos % p_sys->i_icy_meta;
591
592         if( i_next == p_sys->i_icy_meta )
593         {
594             if( ReadICYMeta( p_access ) )
595             {
596                 p_access->info.b_eof = VLC_TRUE;
597                 return -1;
598             }
599         }
600         if( i_len > i_next )
601             i_len = i_next;
602     }
603
604     i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, VLC_FALSE );
605
606     if( i_read > 0 )
607     {
608         p_access->info.i_pos += i_read;
609
610         if( p_sys->b_chunked )
611         {
612             p_sys->i_chunk -= i_read;
613             if( p_sys->i_chunk <= 0 )
614             {
615                 /* read the empty line */
616                 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
617                 if( psz ) free( psz );
618             }
619         }
620     }
621     else if( i_read == 0 )
622     {
623         /*
624          * I very much doubt that this will work.
625          * If i_read == 0, the connection *IS* dead, so the only
626          * sensible thing to do is Disconnect() and then retry.
627          * Otherwise, I got recv() completely wrong. -- Courmisch
628          */
629         if( p_sys->b_continuous )
630         {
631             Request( p_access, 0 );
632             p_sys->b_continuous = VLC_FALSE;
633             i_read = Read( p_access, p_buffer, i_len );
634             p_sys->b_continuous = VLC_TRUE;
635         }
636         Disconnect( p_access );
637         if( p_sys->b_reconnect )
638         {
639             msg_Dbg( p_access, "got disconnected, trying to reconnect" );
640             if( Connect( p_access, p_access->info.i_pos ) )
641             {
642                 msg_Dbg( p_access, "reconnection failed" );
643             }
644             else
645             {
646                 p_sys->b_reconnect = VLC_FALSE;
647                 i_read = Read( p_access, p_buffer, i_len );
648                 p_sys->b_reconnect = VLC_TRUE;
649             }
650         }
651
652         if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
653     }
654
655     if( p_sys->b_continuous )
656     {
657         p_sys->i_remaining -= i_read;
658     }
659
660     return i_read;
661 }
662
663 static int ReadICYMeta( access_t *p_access )
664 {
665     access_sys_t *p_sys = p_access->p_sys;
666
667     uint8_t buffer;
668     char *p, *psz_meta;
669     int i_read;
670
671     /* Read meta data length */
672     i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
673                        VLC_TRUE );
674     if( i_read <= 0 )
675         return VLC_EGENERIC;
676     if( buffer == 0 )
677         return VLC_SUCCESS;
678
679     i_read = buffer << 4;
680     /* msg_Dbg( p_access, "ICY meta size=%u", i_read); */
681
682     psz_meta = malloc( i_read + 1 );
683     if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
684                   (uint8_t *)psz_meta, i_read, VLC_TRUE ) != i_read )
685         return VLC_EGENERIC;
686
687     psz_meta[i_read] = '\0'; /* Just in case */
688
689     /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
690
691     /* Now parse the meta */
692     /* Look for StreamTitle= */
693     p = strcasestr( (char *)psz_meta, "StreamTitle=" );
694     if( p )
695     {
696         p += strlen( "StreamTitle=" );
697         if( *p == '\'' || *p == '"' )
698         {
699             char closing[] = { p[0], ';', '\0' };
700             char *psz = strstr( &p[1], closing );
701             if( !psz )
702                 psz = strchr( &p[1], ';' );
703
704             if( psz ) *psz = '\0';
705         }
706         else
707         {
708             char *psz = strchr( &p[1], ';' );
709             if( psz ) *psz = '\0';
710         }
711
712         if( !p_sys->psz_icy_title ||
713             strcmp( p_sys->psz_icy_title, &p[1] ) )
714         {
715             if( p_sys->psz_icy_title )
716                 free( p_sys->psz_icy_title );
717             p_sys->psz_icy_title = strdup( &p[1] );
718             p_access->info.i_update |= INPUT_UPDATE_META;
719
720             msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
721         }
722     }
723     free( psz_meta );
724
725     return VLC_SUCCESS;
726 }
727
728 #ifdef HAVE_ZLIB_H
729 static ssize_t ReadCompressed( access_t *p_access, uint8_t *p_buffer,
730                                size_t i_len )
731 {
732     access_sys_t *p_sys = p_access->p_sys;
733
734     if( p_sys->b_compressed )
735     {
736         int i_ret;
737
738         if( !p_sys->inflate.p_buffer )
739             p_sys->inflate.p_buffer = malloc( 256 * 1024 );
740
741         if( p_sys->inflate.stream.avail_in == 0 )
742         {
743             ssize_t i_read = Read( p_access, p_sys->inflate.p_buffer + p_sys->inflate.stream.avail_in, 256 * 1024 );
744             if( i_read <= 0 ) return i_read;
745             p_sys->inflate.stream.next_in = p_sys->inflate.p_buffer;
746             p_sys->inflate.stream.avail_in = i_read;
747         }
748
749         p_sys->inflate.stream.avail_out = i_len;
750         p_sys->inflate.stream.next_out = p_buffer;
751
752         i_ret = inflate( &p_sys->inflate.stream, Z_SYNC_FLUSH );
753         msg_Warn( p_access, "inflate return value: %d, %s", i_ret, p_sys->inflate.stream.msg );
754
755         return i_len - p_sys->inflate.stream.avail_out;
756     }
757     else
758     {
759         return Read( p_access, p_buffer, i_len );
760     }
761 }
762 #endif
763
764 /*****************************************************************************
765  * Seek: close and re-open a connection at the right place
766  *****************************************************************************/
767 static int Seek( access_t *p_access, int64_t i_pos )
768 {
769     msg_Dbg( p_access, "trying to seek to "I64Fd, i_pos );
770
771     Disconnect( p_access );
772
773     if( Connect( p_access, i_pos ) )
774     {
775         msg_Err( p_access, "seek failed" );
776         p_access->info.b_eof = VLC_TRUE;
777         return VLC_EGENERIC;
778     }
779     return VLC_SUCCESS;
780 }
781
782 /*****************************************************************************
783  * Control:
784  *****************************************************************************/
785 static int Control( access_t *p_access, int i_query, va_list args )
786 {
787     access_sys_t *p_sys = p_access->p_sys;
788     vlc_bool_t   *pb_bool;
789     int          *pi_int;
790     int64_t      *pi_64;
791     vlc_meta_t   *p_meta;
792
793     switch( i_query )
794     {
795         /* */
796         case ACCESS_CAN_SEEK:
797             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
798             *pb_bool = p_sys->b_seekable;
799             break;
800         case ACCESS_CAN_FASTSEEK:
801             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
802             *pb_bool = VLC_FALSE;
803             break;
804         case ACCESS_CAN_PAUSE:
805         case ACCESS_CAN_CONTROL_PACE:
806             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
807
808 #if 0       /* Disable for now until we have a clock synchro algo
809              * which works with something else than MPEG over UDP */
810             *pb_bool = p_sys->b_pace_control;
811 #endif
812             *pb_bool = VLC_TRUE;
813             break;
814
815         /* */
816         case ACCESS_GET_MTU:
817             pi_int = (int*)va_arg( args, int * );
818             *pi_int = 0;
819             break;
820
821         case ACCESS_GET_PTS_DELAY:
822             pi_64 = (int64_t*)va_arg( args, int64_t * );
823             *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
824             break;
825
826         /* */
827         case ACCESS_SET_PAUSE_STATE:
828             break;
829
830         case ACCESS_GET_META:
831             p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
832
833             if( p_sys->psz_icy_name )
834                 vlc_meta_Set( p_meta, vlc_meta_Title, p_sys->psz_icy_name );
835             if( p_sys->psz_icy_genre )
836                 vlc_meta_Set( p_meta, vlc_meta_Genre, p_sys->psz_icy_genre );
837             if( p_sys->psz_icy_title )
838                 vlc_meta_Set( p_meta, vlc_meta_NowPlaying, p_sys->psz_icy_title );
839             break;
840
841         case ACCESS_GET_CONTENT_TYPE:
842             *va_arg( args, char ** ) =
843                 p_sys->psz_mime ? strdup( p_sys->psz_mime ) : NULL;
844             break;
845
846         case ACCESS_GET_TITLE_INFO:
847         case ACCESS_SET_TITLE:
848         case ACCESS_SET_SEEKPOINT:
849         case ACCESS_SET_PRIVATE_ID_STATE:
850             return VLC_EGENERIC;
851
852         default:
853             msg_Warn( p_access, "unimplemented query in control" );
854             return VLC_EGENERIC;
855
856     }
857     return VLC_SUCCESS;
858 }
859
860 /*****************************************************************************
861  * Connect:
862  *****************************************************************************/
863 static int Connect( access_t *p_access, int64_t i_tell )
864 {
865     access_sys_t   *p_sys = p_access->p_sys;
866     vlc_url_t      srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
867
868     /* Clean info */
869     free( p_sys->psz_location );
870     free( p_sys->psz_mime );
871     free( p_sys->psz_pragma );
872
873     free( p_sys->psz_icy_genre );
874     free( p_sys->psz_icy_name );
875     free( p_sys->psz_icy_title );
876
877
878     p_sys->psz_location = NULL;
879     p_sys->psz_mime = NULL;
880     p_sys->psz_pragma = NULL;
881     p_sys->b_mms = VLC_FALSE;
882     p_sys->b_chunked = VLC_FALSE;
883     p_sys->i_chunk = 0;
884     p_sys->i_icy_meta = 0;
885     p_sys->psz_icy_name = NULL;
886     p_sys->psz_icy_genre = NULL;
887     p_sys->psz_icy_title = NULL;
888
889     p_access->info.i_size = 0;
890     p_access->info.i_pos  = i_tell;
891     p_access->info.b_eof  = VLC_FALSE;
892
893
894     /* Open connection */
895     p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
896     if( p_sys->fd == -1 )
897     {
898         msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
899         return -1;
900     }
901
902     /* Initialize TLS/SSL session */
903     if( p_sys->b_ssl == VLC_TRUE )
904     {
905         /* CONNECT to establish TLS tunnel through HTTP proxy */
906         if( p_sys->b_proxy )
907         {
908             char *psz;
909             unsigned i_status = 0;
910
911             if( p_sys->i_version == 0 )
912             {
913                 /* CONNECT is not in HTTP/1.0 */
914                 Disconnect( p_access );
915                 return -1;
916             }
917
918             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
919                         "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
920                         p_sys->url.psz_host, p_sys->url.i_port,
921                         p_sys->i_version,
922                         p_sys->url.psz_host, p_sys->url.i_port);
923
924             psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
925             if( psz == NULL )
926             {
927                 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
928                 Disconnect( p_access );
929                 return -1;
930             }
931
932             sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
933             free( psz );
934
935             if( ( i_status / 100 ) != 2 )
936             {
937                 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
938                 Disconnect( p_access );
939                 return -1;
940             }
941
942             do
943             {
944                 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
945                 if( psz == NULL )
946                 {
947                     msg_Err( p_access, "HTTP proxy connection failed" );
948                     Disconnect( p_access );
949                     return -1;
950                 }
951
952                 if( *psz == '\0' )
953                     i_status = 0;
954
955                 free( psz );
956
957                 if( p_access->b_die || p_access->b_error )
958                 {
959                     Disconnect( p_access );
960                     return -1;
961                 }
962             }
963             while( i_status );
964         }
965
966         /* TLS/SSL handshake */
967         p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
968                                          srv.psz_host );
969         if( p_sys->p_tls == NULL )
970         {
971             msg_Err( p_access, "cannot establish HTTP/TLS session" );
972             Disconnect( p_access );
973             return -1;
974         }
975         p_sys->p_vs = &p_sys->p_tls->sock;
976     }
977
978     return Request( p_access, i_tell ) ? -2 : 0;
979 }
980
981
982 static int Request( access_t *p_access, int64_t i_tell )
983 {
984     access_sys_t   *p_sys = p_access->p_sys;
985     char           *psz ;
986     v_socket_t     *pvs = p_sys->p_vs;
987
988     if( p_sys->b_proxy )
989     {
990         if( p_sys->url.psz_path )
991         {
992             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
993                         "GET http://%s:%d%s HTTP/1.%d\r\n",
994                         p_sys->url.psz_host, p_sys->url.i_port,
995                         p_sys->url.psz_path, p_sys->i_version );
996         }
997         else
998         {
999             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
1000                         "GET http://%s:%d/ HTTP/1.%d\r\n",
1001                         p_sys->url.psz_host, p_sys->url.i_port,
1002                         p_sys->i_version );
1003         }
1004     }
1005     else
1006     {
1007         const char *psz_path = p_sys->url.psz_path;
1008         if( !psz_path || !*psz_path )
1009         {
1010             psz_path = "/";
1011         }
1012         if( p_sys->url.i_port != 80)
1013         {
1014             net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1015                         "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
1016                         psz_path, p_sys->i_version, p_sys->url.psz_host,
1017                         p_sys->url.i_port );
1018         }
1019         else
1020         {
1021             net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1022                         "GET %s HTTP/1.%d\r\nHost: %s\r\n",
1023                         psz_path, p_sys->i_version, p_sys->url.psz_host );
1024         }
1025     }
1026     /* User Agent */
1027     net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "User-Agent: %s\r\n",
1028                 p_sys->psz_user_agent );
1029     /* Offset */
1030     if( p_sys->i_version == 1 )
1031     {
1032         net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1033                     "Range: bytes="I64Fd"-\r\n", i_tell );
1034     }
1035
1036     /* Cookies */
1037     if( p_sys->cookies )
1038     {
1039         int i;
1040         for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
1041         {
1042             const char * cookie = vlc_array_item_at_index( p_sys->cookies, i );
1043             char * psz_cookie_content = cookie_get_content( cookie );
1044             char * psz_cookie_domain = cookie_get_domain( cookie );
1045
1046             assert( psz_cookie_content );
1047
1048             /* FIXME: This is clearly not conforming to the rfc */
1049             vlc_bool_t is_in_right_domain = (!psz_cookie_domain || strstr( p_sys->url.psz_host, psz_cookie_domain ));
1050
1051             if( is_in_right_domain )
1052             {
1053                 msg_Dbg( p_access, "Sending Cookie %s", psz_cookie_content );
1054                 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Cookie: %s\r\n", psz_cookie_content ) < 0 )
1055                     msg_Err( p_access, "failed to send Cookie" );
1056             }
1057             free( psz_cookie_content );
1058             free( psz_cookie_domain );
1059         }
1060     }
1061
1062     /* Authentication */
1063     if( p_sys->url.psz_username || p_sys->url.psz_password )
1064     {
1065         char buf[strlen( p_sys->url.psz_username ?: "" )
1066                   + strlen( p_sys->url.psz_password ?: "" ) + 2];
1067         char *b64;
1068
1069         snprintf( buf, sizeof( buf ), "%s:%s", p_sys->url.psz_username ?: "",
1070                   p_sys->url.psz_password ?: "" );
1071         b64 = vlc_b64_encode( buf );
1072
1073         if( b64 != NULL )
1074         {
1075              net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1076                          "Authorization: Basic %s\r\n", b64 );
1077              free( b64 );
1078         }
1079     }
1080
1081     /* Proxy Authentication */
1082     if( p_sys->proxy.psz_username || p_sys->proxy.psz_password )
1083     {
1084         char buf[strlen( p_sys->proxy.psz_username ?: "" )
1085                   + strlen( p_sys->proxy.psz_password ?: "" )];
1086         char *b64;
1087
1088         snprintf( buf, sizeof( buf ), "%s:%s", p_sys->proxy.psz_username ?: "",
1089                   p_sys->proxy.psz_password ?: "" );
1090         b64 = vlc_b64_encode( buf );
1091
1092         if( b64 != NULL)
1093         {
1094             net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1095                         "Proxy-Authorization: Basic %s\r\n", b64 );
1096             free( b64 );
1097         }
1098     }
1099
1100     /* ICY meta data request */
1101     net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
1102
1103
1104     if( p_sys->b_continuous )
1105     {
1106         net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
1107                     "Connection: Keep-Alive\r\n" );
1108     }
1109     else if( p_sys->i_version == 1 )
1110     {
1111         net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
1112                     "Connection: Close\r\n");
1113     }
1114
1115     if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "\r\n" ) < 0 )
1116     {
1117         msg_Err( p_access, "failed to send request" );
1118         Disconnect( p_access );
1119         return VLC_EGENERIC;
1120     }
1121
1122     /* Read Answer */
1123     if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs ) ) == NULL )
1124     {
1125         msg_Err( p_access, "failed to read answer" );
1126         goto error;
1127     }
1128     if( !strncmp( psz, "HTTP/1.", 7 ) )
1129     {
1130         p_sys->psz_protocol = "HTTP";
1131         p_sys->i_code = atoi( &psz[9] );
1132     }
1133     else if( !strncmp( psz, "ICY", 3 ) )
1134     {
1135         p_sys->psz_protocol = "ICY";
1136         p_sys->i_code = atoi( &psz[4] );
1137         p_sys->b_reconnect = VLC_TRUE;
1138     }
1139     else
1140     {
1141         msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1142         free( psz );
1143         goto error;
1144     }
1145     msg_Dbg( p_access, "protocol '%s' answer code %d",
1146              p_sys->psz_protocol, p_sys->i_code );
1147     if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1148     {
1149         p_sys->b_seekable = VLC_FALSE;
1150     }
1151     if( p_sys->i_code != 206 && p_sys->i_code != 401 )
1152     {
1153         p_sys->b_seekable = VLC_FALSE;
1154     }
1155     /* Authentication error - We'll have to display the dialog */
1156     if( p_sys->i_code == 401 )
1157     {
1158
1159     }
1160     /* Other fatal error */
1161     else if( p_sys->i_code >= 400 )
1162     {
1163         msg_Err( p_access, "error: %s", psz );
1164         free( psz );
1165         goto error;
1166     }
1167     free( psz );
1168
1169     for( ;; )
1170     {
1171         char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs );
1172         char *p;
1173
1174         if( psz == NULL )
1175         {
1176             msg_Err( p_access, "failed to read answer" );
1177             goto error;
1178         }
1179
1180         if( p_access->b_die || p_access->b_error )
1181         {
1182             free( psz );
1183             goto error;
1184         }
1185
1186         /* msg_Dbg( p_input, "Line=%s", psz ); */
1187         if( *psz == '\0' )
1188         {
1189             free( psz );
1190             break;
1191         }
1192
1193         if( ( p = strchr( psz, ':' ) ) == NULL )
1194         {
1195             msg_Err( p_access, "malformed header line: %s", psz );
1196             free( psz );
1197             goto error;
1198         }
1199         *p++ = '\0';
1200         while( *p == ' ' ) p++;
1201
1202         if( !strcasecmp( psz, "Content-Length" ) )
1203         {
1204             if( p_sys->b_continuous )
1205             {
1206                 p_access->info.i_size = -1;
1207                 msg_Dbg( p_access, "this frame size=%lld", atoll(p ) );
1208                 p_sys->i_remaining = atoll( p );
1209             }
1210             else
1211             {
1212                 p_access->info.i_size = i_tell + atoll( p );
1213                 msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
1214             }
1215         }
1216         else if( !strcasecmp( psz, "Location" ) )
1217         {
1218             char * psz_new_loc;
1219
1220             /* This does not follow RFC 2068, but yet if the url is not absolute,
1221              * handle it as everyone does. */
1222             if( p[0] == '/' )
1223             {
1224                 const char *psz_http_ext = p_sys->b_ssl ? "s" : "" ;
1225
1226                 if( p_sys->url.i_port == ( p_sys->b_ssl ? 443 : 80 ) )
1227                 {
1228                     if( asprintf(&psz_new_loc, "http%s://%s%s", psz_http_ext,
1229                                  p_sys->url.psz_host, p) < 0 )
1230                         goto error;
1231                 }
1232                 else
1233                 {
1234                     if( asprintf(&psz_new_loc, "http%s://%s:%d%s", psz_http_ext,
1235                                  p_sys->url.psz_host, p_sys->url.i_port, p) < 0 )
1236                         goto error;
1237                 }
1238             }
1239             else
1240             {
1241                 psz_new_loc = strdup( p );
1242             }
1243
1244             if( p_sys->psz_location ) free( p_sys->psz_location );
1245             p_sys->psz_location = psz_new_loc;
1246         }
1247         else if( !strcasecmp( psz, "Content-Type" ) )
1248         {
1249             if( p_sys->psz_mime ) free( p_sys->psz_mime );
1250             p_sys->psz_mime = strdup( p );
1251             msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1252         }
1253         else if( !strcasecmp( psz, "Content-Encoding" ) )
1254         {
1255             msg_Dbg( p_access, "Content-Encoding: %s", p );
1256             if( strcasecmp( p, "identity" ) )
1257 #ifdef HAVE_ZLIB_H
1258                 p_sys->b_compressed = VLC_TRUE;
1259 #else
1260                 msg_Warn( p_access, "Compressed content not supported. Rebuild with zlib support." );
1261 #endif
1262         }
1263         else if( !strcasecmp( psz, "Pragma" ) )
1264         {
1265             if( !strcasecmp( psz, "Pragma: features" ) )
1266                 p_sys->b_mms = VLC_TRUE;
1267             if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
1268             p_sys->psz_pragma = strdup( p );
1269             msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1270         }
1271         else if( !strcasecmp( psz, "Server" ) )
1272         {
1273             msg_Dbg( p_access, "Server: %s", p );
1274             if( !strncasecmp( p, "Icecast", 7 ) ||
1275                 !strncasecmp( p, "Nanocaster", 10 ) )
1276             {
1277                 /* Remember if this is Icecast
1278                  * we need to force demux in this case without breaking
1279                  *  autodetection */
1280
1281                 /* Let live 365 streams (nanocaster) piggyback on the icecast
1282                  * routine. They look very similar */
1283
1284                 p_sys->b_reconnect = VLC_TRUE;
1285                 p_sys->b_pace_control = VLC_FALSE;
1286                 p_sys->b_icecast = VLC_TRUE;
1287             }
1288         }
1289         else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1290         {
1291             msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1292             if( !strncasecmp( p, "chunked", 7 ) )
1293             {
1294                 p_sys->b_chunked = VLC_TRUE;
1295             }
1296         }
1297         else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1298         {
1299             msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1300             p_sys->i_icy_meta = atoi( p );
1301             if( p_sys->i_icy_meta < 0 )
1302                 p_sys->i_icy_meta = 0;
1303
1304             msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1305         }
1306         else if( !strcasecmp( psz, "Icy-Name" ) )
1307         {
1308             if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
1309             p_sys->psz_icy_name = strdup( p );
1310             msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1311
1312             p_sys->b_icecast = VLC_TRUE; /* be on the safeside. set it here as well. */
1313             p_sys->b_reconnect = VLC_TRUE;
1314             p_sys->b_pace_control = VLC_FALSE;
1315         }
1316         else if( !strcasecmp( psz, "Icy-Genre" ) )
1317         {
1318             if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
1319             p_sys->psz_icy_genre = strdup( p );
1320             msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1321         }
1322         else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1323         {
1324             msg_Dbg( p_access, "Icy-Notice: %s", p );
1325         }
1326         else if( !strncasecmp( psz, "icy-", 4 ) ||
1327                  !strncasecmp( psz, "ice-", 4 ) ||
1328                  !strncasecmp( psz, "x-audiocast", 11 ) )
1329         {
1330             msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1331         } else if( !strcasecmp( psz, "Set-Cookie" ) )
1332         {
1333             if( p_sys->cookies )
1334             {
1335                 msg_Dbg( p_access, "Accepting Cookie: %s", p );
1336                 cookie_append( p_sys->cookies, strdup(p) );
1337             }
1338             else
1339                 msg_Dbg( p_access, "We have a Cookie we won't remember: %s", p );
1340         }
1341
1342         free( psz );
1343     }
1344     return VLC_SUCCESS;
1345
1346 error:
1347     Disconnect( p_access );
1348     return VLC_EGENERIC;
1349 }
1350
1351 /*****************************************************************************
1352  * Disconnect:
1353  *****************************************************************************/
1354 static void Disconnect( access_t *p_access )
1355 {
1356     access_sys_t *p_sys = p_access->p_sys;
1357
1358     if( p_sys->p_tls != NULL)
1359     {
1360         tls_ClientDelete( p_sys->p_tls );
1361         p_sys->p_tls = NULL;
1362         p_sys->p_vs = NULL;
1363     }
1364     if( p_sys->fd != -1)
1365     {
1366         net_Close(p_sys->fd);
1367         p_sys->fd = -1;
1368     }
1369
1370 }
1371
1372 /*****************************************************************************
1373  * Cookies (FIXME: we may want to rewrite that using a nice structure to hold
1374  * them) (FIXME: only support the "domain=" param)
1375  *****************************************************************************/
1376
1377 /* Get the NAME=VALUE part of the Cookie */
1378 static char * cookie_get_content( const char * cookie )
1379 {
1380     char * ret = strdup( cookie );
1381     if( !ret ) return NULL;
1382     char * str = ret;
1383     /* Look for a ';' */
1384     while( *str && *str != ';' ) str++;
1385     /* Replace it by a end-char */
1386     if( *str == ';' ) *str = 0;
1387     return ret;
1388 }
1389
1390 /* Get the domain where the cookie is stored */
1391 static char * cookie_get_domain( const char * cookie )
1392 {
1393     const char * str = cookie;
1394     static const char domain[] = "domain=";
1395     if( !str )
1396         return NULL;
1397     /* Look for a ';' */
1398     while( *str )
1399     {
1400         if( !strncmp( str, domain, sizeof(domain) - 1 /* minus \0 */ ) )
1401         {
1402             str += sizeof(domain) - 1 /* minus \0 */;
1403             char * ret = strdup( str );
1404             /* Now remove the next ';' if present */
1405             char * ret_iter = ret;
1406             while( *ret_iter && *ret_iter != ';' ) ret_iter++;
1407             if( *ret_iter == ';' )
1408                 *ret_iter = 0;
1409             return ret;
1410         }
1411         /* Go to next ';' field */
1412         while( *str && *str != ';' ) str++;
1413         if( *str == ';' ) str++;
1414         /* skip blank */
1415         while( *str && *str == ' ' ) str++;
1416     }
1417     return NULL;
1418 }
1419
1420 /* Get NAME in the NAME=VALUE field */
1421 static char * cookie_get_name( const char * cookie )
1422 {
1423     char * ret = cookie_get_content( cookie ); /* NAME=VALUE */
1424     if( !ret ) return NULL;
1425     char * str = ret;
1426     while( *str && *str != '=' ) str++;
1427     *str = 0;
1428     return ret;
1429 }
1430
1431 /* Add a cookie in cookies, checking to see how it should be added */
1432 static void cookie_append( vlc_array_t * cookies, char * cookie )
1433 {
1434     int i;
1435
1436     if( !cookie )
1437         return;
1438
1439     char * cookie_name = cookie_get_name( cookie );
1440
1441     /* Don't send invalid cookies */
1442     if( !cookie_name )
1443         return;
1444
1445     char * cookie_domain = cookie_get_domain( cookie );
1446     for( i = 0; i < vlc_array_count( cookies ); i++ )
1447     {
1448         char * current_cookie = vlc_array_item_at_index( cookies, i );
1449         char * current_cookie_name = cookie_get_name( current_cookie );
1450         char * current_cookie_domain = cookie_get_domain( current_cookie );
1451
1452         assert( current_cookie_name );
1453
1454         vlc_bool_t is_domain_matching = ( cookie_domain && current_cookie_domain &&
1455                                          !strcmp( cookie_domain, current_cookie_domain ) );
1456
1457         if( is_domain_matching && !strcmp( cookie_name, current_cookie_name )  )
1458         {
1459             /* Remove previous value for this cookie */
1460             free( current_cookie );
1461             vlc_array_remove( cookies, i );
1462
1463             /* Clean */
1464             free( current_cookie_name );
1465             free( current_cookie_domain );
1466             break;
1467         }
1468         free( current_cookie_name );
1469         free( current_cookie_domain );
1470     }
1471     free( cookie_name );
1472     free( cookie_domain );
1473     vlc_array_append( cookies, cookie );
1474 }
1475