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