]> git.sesse.net Git - vlc/blob - modules/access/http.c
access/http.c: More check on cookies we receive.
[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 OpenWithCookies( vlc_object_t *p_this, vlc_array_t *cookies );
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 OpenWithCookies( p_this, NULL );
177 }
178
179 static int OpenWithCookies( vlc_object_t *p_this, vlc_array_t *cookies )
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 ? (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         cookies = p_sys->cookies;
369         free( p_sys );
370
371         /* Do new Open() run with new data */
372         return OpenWithCookies( p_this, cookies );
373     }
374
375     if( p_sys->b_mms )
376     {
377         msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
378         goto error;
379     }
380
381     if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
382     {
383         if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
384         {
385             if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
386                 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
387                 p_access->psz_demux = strdup( "nsv" );
388             else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
389                      !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
390                 p_access->psz_demux = strdup( "m4a" );
391             else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
392                 p_access->psz_demux = strdup( "mp3" );
393
394             msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
395                       p_access->psz_demux );
396
397 #if 0       /* Doesn't work really well because of the pre-buffering in
398              * shoutcast servers (the buffer content will be sent as fast as
399              * possible). */
400             p_sys->b_pace_control = VLC_FALSE;
401 #endif
402         }
403         else if( !p_sys->psz_mime )
404         {
405              /* Shoutcast */
406              p_access->psz_demux = strdup( "mp3" );
407         }
408         /* else probably Ogg Vorbis */
409     }
410     else if( !strcasecmp( p_access->psz_access, "unsv" ) &&
411              p_sys->psz_mime &&
412              !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
413     {
414         /* Grrrr! detect ultravox server and force NSV demuxer */
415         p_access->psz_demux = strdup( "nsv" );
416     }
417     else if( !strcmp( p_access->psz_access, "itpc" ) )
418     {
419         p_access->psz_demux = strdup( "podcast" );
420     }
421     else if( p_sys->psz_mime &&
422              !strncasecmp( p_sys->psz_mime, "application/xspf+xml", 20 ) &&
423              ( memchr( " ;\t", p_sys->psz_mime[20], 4 ) != NULL ) )
424         p_access->psz_demux = strdup( "xspf-open" );
425
426     if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
427
428     /* PTS delay */
429     var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
430
431     return VLC_SUCCESS;
432
433 error:
434     vlc_UrlClean( &p_sys->url );
435     vlc_UrlClean( &p_sys->proxy );
436     free( p_sys->psz_mime );
437     free( p_sys->psz_pragma );
438     free( p_sys->psz_location );
439     free( p_sys->psz_user_agent );
440
441     Disconnect( p_access );
442     free( p_sys );
443     return VLC_EGENERIC;
444 }
445
446 /*****************************************************************************
447  * Close:
448  *****************************************************************************/
449 static void Close( vlc_object_t *p_this )
450 {
451     access_t     *p_access = (access_t*)p_this;
452     access_sys_t *p_sys = p_access->p_sys;
453
454     vlc_UrlClean( &p_sys->url );
455     vlc_UrlClean( &p_sys->proxy );
456
457     free( p_sys->psz_mime );
458     free( p_sys->psz_pragma );
459     free( p_sys->psz_location );
460
461     free( p_sys->psz_icy_name );
462     free( p_sys->psz_icy_genre );
463     free( p_sys->psz_icy_title );
464
465     free( p_sys->psz_user_agent );
466
467     Disconnect( p_access );
468
469     if( p_sys->cookies )
470     {
471         int i;
472         for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
473             free(vlc_array_item_at_index( p_sys->cookies, i ));
474         vlc_array_destroy( p_sys->cookies );
475     }
476
477     free( p_sys );
478 }
479
480 /*****************************************************************************
481  * Read: Read up to i_len bytes from the http connection and place in
482  * p_buffer. Return the actual number of bytes read
483  *****************************************************************************/
484 static int ReadICYMeta( access_t *p_access );
485 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
486 {
487     access_sys_t *p_sys = p_access->p_sys;
488     int i_read;
489
490     if( p_sys->fd < 0 )
491     {
492         p_access->info.b_eof = VLC_TRUE;
493         return 0;
494     }
495
496     if( p_access->info.i_size > 0 &&
497         i_len + p_access->info.i_pos > p_access->info.i_size )
498     {
499         if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
500         {
501             p_access->info.b_eof = VLC_TRUE;
502             return 0;
503         }
504     }
505
506     if( p_sys->b_chunked )
507     {
508         if( p_sys->i_chunk < 0 )
509         {
510             p_access->info.b_eof = VLC_TRUE;
511             return 0;
512         }
513
514         if( p_sys->i_chunk <= 0 )
515         {
516             char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
517             /* read the chunk header */
518             if( psz == NULL )
519             {
520                 /* fatal error - end of file */
521                 msg_Dbg( p_access, "failed reading chunk-header line" );
522                 return 0;
523             }
524             p_sys->i_chunk = strtoll( psz, NULL, 16 );
525             free( psz );
526
527             if( p_sys->i_chunk <= 0 )   /* eof */
528             {
529                 p_sys->i_chunk = -1;
530                 p_access->info.b_eof = VLC_TRUE;
531                 return 0;
532             }
533         }
534
535         if( i_len > p_sys->i_chunk )
536         {
537             i_len = p_sys->i_chunk;
538         }
539     }
540
541     if( p_sys->b_continuous && i_len > p_sys->i_remaining )
542     {
543         /* Only ask for the remaining length */
544         int i_new_len = p_sys->i_remaining;
545         if( i_new_len == 0 )
546         {
547             Request( p_access, 0 );
548             i_read = Read( p_access, p_buffer, i_len );
549             return i_read;
550         }
551         i_len = i_new_len;
552     }
553
554     if( p_sys->i_icy_meta > 0 && p_access->info.i_pos > 0 )
555     {
556         int64_t i_next = p_sys->i_icy_meta -
557                                     p_access->info.i_pos % p_sys->i_icy_meta;
558
559         if( i_next == p_sys->i_icy_meta )
560         {
561             if( ReadICYMeta( p_access ) )
562             {
563                 p_access->info.b_eof = VLC_TRUE;
564                 return -1;
565             }
566         }
567         if( i_len > i_next )
568             i_len = i_next;
569     }
570
571     i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, VLC_FALSE );
572
573     if( i_read > 0 )
574     {
575         p_access->info.i_pos += i_read;
576
577         if( p_sys->b_chunked )
578         {
579             p_sys->i_chunk -= i_read;
580             if( p_sys->i_chunk <= 0 )
581             {
582                 /* read the empty line */
583                 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
584                 if( psz ) free( psz );
585             }
586         }
587     }
588     else if( i_read == 0 )
589     {
590         /*
591          * I very much doubt that this will work.
592          * If i_read == 0, the connection *IS* dead, so the only
593          * sensible thing to do is Disconnect() and then retry.
594          * Otherwise, I got recv() completely wrong. -- Courmisch
595          */
596         if( p_sys->b_continuous )
597         {
598             Request( p_access, 0 );
599             p_sys->b_continuous = VLC_FALSE;
600             i_read = Read( p_access, p_buffer, i_len );
601             p_sys->b_continuous = VLC_TRUE;
602         }
603         Disconnect( p_access );
604         if( p_sys->b_reconnect )
605         {
606             msg_Dbg( p_access, "got disconnected, trying to reconnect" );
607             if( Connect( p_access, p_access->info.i_pos ) )
608             {
609                 msg_Dbg( p_access, "reconnection failed" );
610             }
611             else
612             {
613                 p_sys->b_reconnect = VLC_FALSE;
614                 i_read = Read( p_access, p_buffer, i_len );
615                 p_sys->b_reconnect = VLC_TRUE;
616             }
617         }
618
619         if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
620     }
621
622     if( p_sys->b_continuous )
623     {
624         p_sys->i_remaining -= i_read;
625     }
626
627     return i_read;
628 }
629
630 static int ReadICYMeta( access_t *p_access )
631 {
632     access_sys_t *p_sys = p_access->p_sys;
633
634     uint8_t buffer;
635     char *p, *psz_meta;
636     int i_read;
637
638     /* Read meta data length */
639     i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
640                        VLC_TRUE );
641     if( i_read <= 0 )
642         return VLC_EGENERIC;
643     if( buffer == 0 )
644         return VLC_SUCCESS;
645
646     i_read = buffer << 4;
647     /* msg_Dbg( p_access, "ICY meta size=%u", i_read); */
648
649     psz_meta = malloc( i_read + 1 );
650     if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
651                   (uint8_t *)psz_meta, i_read, VLC_TRUE ) != i_read )
652         return VLC_EGENERIC;
653
654     psz_meta[i_read] = '\0'; /* Just in case */
655
656     /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
657
658     /* Now parse the meta */
659     /* Look for StreamTitle= */
660     p = strcasestr( (char *)psz_meta, "StreamTitle=" );
661     if( p )
662     {
663         p += strlen( "StreamTitle=" );
664         if( *p == '\'' || *p == '"' )
665         {
666             char closing[] = { p[0], ';', '\0' };
667             char *psz = strstr( &p[1], closing );
668             if( !psz )
669                 psz = strchr( &p[1], ';' );
670
671             if( psz ) *psz = '\0';
672         }
673         else
674         {
675             char *psz = strchr( &p[1], ';' );
676             if( psz ) *psz = '\0';
677         }
678
679         if( !p_sys->psz_icy_title ||
680             strcmp( p_sys->psz_icy_title, &p[1] ) )
681         {
682             if( p_sys->psz_icy_title )
683                 free( p_sys->psz_icy_title );
684             p_sys->psz_icy_title = strdup( &p[1] );
685             p_access->info.i_update |= INPUT_UPDATE_META;
686
687             msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
688         }
689     }
690     free( psz_meta );
691
692     return VLC_SUCCESS;
693 }
694
695 /*****************************************************************************
696  * Seek: close and re-open a connection at the right place
697  *****************************************************************************/
698 static int Seek( access_t *p_access, int64_t i_pos )
699 {
700     msg_Dbg( p_access, "trying to seek to "I64Fd, i_pos );
701
702     Disconnect( p_access );
703
704     if( Connect( p_access, i_pos ) )
705     {
706         msg_Err( p_access, "seek failed" );
707         p_access->info.b_eof = VLC_TRUE;
708         return VLC_EGENERIC;
709     }
710     return VLC_SUCCESS;
711 }
712
713 /*****************************************************************************
714  * Control:
715  *****************************************************************************/
716 static int Control( access_t *p_access, int i_query, va_list args )
717 {
718     access_sys_t *p_sys = p_access->p_sys;
719     vlc_bool_t   *pb_bool;
720     int          *pi_int;
721     int64_t      *pi_64;
722     vlc_meta_t   *p_meta;
723
724     switch( i_query )
725     {
726         /* */
727         case ACCESS_CAN_SEEK:
728             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
729             *pb_bool = p_sys->b_seekable;
730             break;
731         case ACCESS_CAN_FASTSEEK:
732             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
733             *pb_bool = VLC_FALSE;
734             break;
735         case ACCESS_CAN_PAUSE:
736         case ACCESS_CAN_CONTROL_PACE:
737             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
738
739 #if 0       /* Disable for now until we have a clock synchro algo
740              * which works with something else than MPEG over UDP */
741             *pb_bool = p_sys->b_pace_control;
742 #endif
743             *pb_bool = VLC_TRUE;
744             break;
745
746         /* */
747         case ACCESS_GET_MTU:
748             pi_int = (int*)va_arg( args, int * );
749             *pi_int = 0;
750             break;
751
752         case ACCESS_GET_PTS_DELAY:
753             pi_64 = (int64_t*)va_arg( args, int64_t * );
754             *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
755             break;
756
757         /* */
758         case ACCESS_SET_PAUSE_STATE:
759             break;
760
761         case ACCESS_GET_META:
762             p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
763
764             if( p_sys->psz_icy_name )
765                 vlc_meta_Set( p_meta, vlc_meta_Title, p_sys->psz_icy_name );
766             if( p_sys->psz_icy_genre )
767                 vlc_meta_Set( p_meta, vlc_meta_Genre, p_sys->psz_icy_genre );
768             if( p_sys->psz_icy_title )
769                 vlc_meta_Set( p_meta, vlc_meta_NowPlaying, p_sys->psz_icy_title );
770             break;
771
772         case ACCESS_GET_CONTENT_TYPE:
773             *va_arg( args, char ** ) =
774                 p_sys->psz_mime ? strdup( p_sys->psz_mime ) : NULL;
775             break;
776
777         case ACCESS_GET_TITLE_INFO:
778         case ACCESS_SET_TITLE:
779         case ACCESS_SET_SEEKPOINT:
780         case ACCESS_SET_PRIVATE_ID_STATE:
781             return VLC_EGENERIC;
782
783         default:
784             msg_Warn( p_access, "unimplemented query in control" );
785             return VLC_EGENERIC;
786
787     }
788     return VLC_SUCCESS;
789 }
790
791 /*****************************************************************************
792  * Connect:
793  *****************************************************************************/
794 static int Connect( access_t *p_access, int64_t i_tell )
795 {
796     access_sys_t   *p_sys = p_access->p_sys;
797     vlc_url_t      srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
798
799     /* Clean info */
800     free( p_sys->psz_location );
801     free( p_sys->psz_mime );
802     free( p_sys->psz_pragma );
803
804     free( p_sys->psz_icy_genre );
805     free( p_sys->psz_icy_name );
806     free( p_sys->psz_icy_title );
807
808
809     p_sys->psz_location = NULL;
810     p_sys->psz_mime = NULL;
811     p_sys->psz_pragma = NULL;
812     p_sys->b_mms = VLC_FALSE;
813     p_sys->b_chunked = VLC_FALSE;
814     p_sys->i_chunk = 0;
815     p_sys->i_icy_meta = 0;
816     p_sys->psz_icy_name = NULL;
817     p_sys->psz_icy_genre = NULL;
818     p_sys->psz_icy_title = NULL;
819
820     p_access->info.i_size = 0;
821     p_access->info.i_pos  = i_tell;
822     p_access->info.b_eof  = VLC_FALSE;
823
824
825     /* Open connection */
826     p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
827     if( p_sys->fd == -1 )
828     {
829         msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
830         return -1;
831     }
832
833     /* Initialize TLS/SSL session */
834     if( p_sys->b_ssl == VLC_TRUE )
835     {
836         /* CONNECT to establish TLS tunnel through HTTP proxy */
837         if( p_sys->b_proxy )
838         {
839             char *psz;
840             unsigned i_status = 0;
841
842             if( p_sys->i_version == 0 )
843             {
844                 /* CONNECT is not in HTTP/1.0 */
845                 Disconnect( p_access );
846                 return -1;
847             }
848
849             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
850                         "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
851                         p_sys->url.psz_host, p_sys->url.i_port,
852                         p_sys->i_version,
853                         p_sys->url.psz_host, p_sys->url.i_port);
854
855             psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
856             if( psz == NULL )
857             {
858                 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
859                 Disconnect( p_access );
860                 return -1;
861             }
862
863             sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
864             free( psz );
865
866             if( ( i_status / 100 ) != 2 )
867             {
868                 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
869                 Disconnect( p_access );
870                 return -1;
871             }
872
873             do
874             {
875                 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
876                 if( psz == NULL )
877                 {
878                     msg_Err( p_access, "HTTP proxy connection failed" );
879                     Disconnect( p_access );
880                     return -1;
881                 }
882
883                 if( *psz == '\0' )
884                     i_status = 0;
885
886                 free( psz );
887             }
888             while( i_status );
889         }
890
891         /* TLS/SSL handshake */
892         p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
893                                          srv.psz_host );
894         if( p_sys->p_tls == NULL )
895         {
896             msg_Err( p_access, "cannot establish HTTP/TLS session" );
897             Disconnect( p_access );
898             return -1;
899         }
900         p_sys->p_vs = &p_sys->p_tls->sock;
901     }
902
903     return Request( p_access, i_tell ) ? -2 : 0;
904 }
905
906
907 static int Request( access_t *p_access, int64_t i_tell )
908 {
909     access_sys_t   *p_sys = p_access->p_sys;
910     char           *psz ;
911     v_socket_t     *pvs = p_sys->p_vs;
912
913     if( p_sys->b_proxy )
914     {
915         if( p_sys->url.psz_path )
916         {
917             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
918                         "GET http://%s:%d%s HTTP/1.%d\r\n",
919                         p_sys->url.psz_host, p_sys->url.i_port,
920                         p_sys->url.psz_path, p_sys->i_version );
921         }
922         else
923         {
924             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
925                         "GET http://%s:%d/ HTTP/1.%d\r\n",
926                         p_sys->url.psz_host, p_sys->url.i_port,
927                         p_sys->i_version );
928         }
929     }
930     else
931     {
932         const char *psz_path = p_sys->url.psz_path;
933         if( !psz_path || !*psz_path )
934         {
935             psz_path = "/";
936         }
937         if( p_sys->url.i_port != 80)
938         {
939             net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
940                         "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
941                         psz_path, p_sys->i_version, p_sys->url.psz_host,
942                         p_sys->url.i_port );
943         }
944         else
945         {
946             net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
947                         "GET %s HTTP/1.%d\r\nHost: %s\r\n",
948                         psz_path, p_sys->i_version, p_sys->url.psz_host );
949         }
950     }
951     /* User Agent */
952     net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "User-Agent: %s\r\n",
953                 p_sys->psz_user_agent );
954     /* Offset */
955     if( p_sys->i_version == 1 )
956     {
957         net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
958                     "Range: bytes="I64Fd"-\r\n", i_tell );
959     }
960
961     /* Cookies */
962     if( p_sys->cookies )
963     {
964         int i;
965         for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
966         {
967             const char * cookie = vlc_array_item_at_index( p_sys->cookies, i );
968             char * psz_cookie_content = cookie_get_content( cookie );
969             char * psz_cookie_domain = cookie_get_domain( cookie );
970             
971             assert( psz_cookie_content );
972
973             /* FIXME: This is clearly not conforming to the rfc */
974             vlc_bool_t is_in_right_domain = (!psz_cookie_domain || strstr( p_sys->url.psz_host, psz_cookie_domain ));
975
976             if( is_in_right_domain )
977             {
978                 msg_Dbg( p_access, "Sending Cookie %s", psz_cookie_content );
979                 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Cookie: %s\r\n", psz_cookie_content ) < 0 )
980                     msg_Err( p_access, "failed to send Cookie" );
981             }
982             free( psz_cookie_content );
983             free( psz_cookie_domain );
984         }
985     }
986
987     /* Authentication */
988     if( p_sys->url.psz_username || p_sys->url.psz_password )
989     {
990         char buf[strlen( p_sys->url.psz_username ?: "" )
991                   + strlen( p_sys->url.psz_password ?: "" ) + 2];
992         char *b64;
993
994         snprintf( buf, sizeof( buf ), "%s:%s", p_sys->url.psz_username ?: "",
995                   p_sys->url.psz_password ?: "" );
996         b64 = vlc_b64_encode( buf );
997
998         if( b64 != NULL )
999         {
1000              net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1001                          "Authorization: Basic %s\r\n", b64 );
1002              free( b64 );
1003         }
1004     }
1005
1006     /* Proxy Authentication */
1007     if( p_sys->proxy.psz_username || p_sys->proxy.psz_password )
1008     {
1009         char buf[strlen( p_sys->proxy.psz_username ?: "" )
1010                   + strlen( p_sys->proxy.psz_password ?: "" )];
1011         char *b64;
1012
1013         snprintf( buf, sizeof( buf ), "%s:%s", p_sys->proxy.psz_username ?: "",
1014                   p_sys->proxy.psz_password ?: "" );
1015         b64 = vlc_b64_encode( buf );
1016
1017         if( b64 != NULL)
1018         {
1019             net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1020                         "Proxy-Authorization: Basic %s\r\n", b64 );
1021             free( b64 );
1022         }
1023     }
1024
1025     /* ICY meta data request */
1026     net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
1027
1028
1029     if( p_sys->b_continuous )
1030     {
1031         net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
1032                     "Connection: Keep-Alive\r\n" );
1033     }
1034     else if( p_sys->i_version == 1 )
1035     {
1036         net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
1037                     "Connection: Close\r\n");
1038     }
1039
1040     if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "\r\n" ) < 0 )
1041     {
1042         msg_Err( p_access, "failed to send request" );
1043         Disconnect( p_access );
1044         return VLC_EGENERIC;
1045     }
1046
1047     /* Read Answer */
1048     if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs ) ) == NULL )
1049     {
1050         msg_Err( p_access, "failed to read answer" );
1051         goto error;
1052     }
1053     if( !strncmp( psz, "HTTP/1.", 7 ) )
1054     {
1055         p_sys->psz_protocol = "HTTP";
1056         p_sys->i_code = atoi( &psz[9] );
1057     }
1058     else if( !strncmp( psz, "ICY", 3 ) )
1059     {
1060         p_sys->psz_protocol = "ICY";
1061         p_sys->i_code = atoi( &psz[4] );
1062         p_sys->b_reconnect = VLC_TRUE;
1063     }
1064     else
1065     {
1066         msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1067         free( psz );
1068         goto error;
1069     }
1070     msg_Dbg( p_access, "protocol '%s' answer code %d",
1071              p_sys->psz_protocol, p_sys->i_code );
1072     if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1073     {
1074         p_sys->b_seekable = VLC_FALSE;
1075     }
1076     if( p_sys->i_code != 206 && p_sys->i_code != 401 )
1077     {
1078         p_sys->b_seekable = VLC_FALSE;
1079     }
1080     /* Authentication error - We'll have to display the dialog */
1081     if( p_sys->i_code == 401 )
1082     {
1083
1084     }
1085     /* Other fatal error */
1086     else if( p_sys->i_code >= 400 )
1087     {
1088         msg_Err( p_access, "error: %s", psz );
1089         free( psz );
1090         goto error;
1091     }
1092     free( psz );
1093
1094     for( ;; )
1095     {
1096         char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs );
1097         char *p;
1098
1099         if( psz == NULL )
1100         {
1101             msg_Err( p_access, "failed to read answer" );
1102             goto error;
1103         }
1104
1105         /* msg_Dbg( p_input, "Line=%s", psz ); */
1106         if( *psz == '\0' )
1107         {
1108             free( psz );
1109             break;
1110         }
1111
1112
1113         if( ( p = strchr( psz, ':' ) ) == NULL )
1114         {
1115             msg_Err( p_access, "malformed header line: %s", psz );
1116             free( psz );
1117             goto error;
1118         }
1119         *p++ = '\0';
1120         while( *p == ' ' ) p++;
1121
1122         if( !strcasecmp( psz, "Content-Length" ) )
1123         {
1124             if( p_sys->b_continuous )
1125             {
1126                 p_access->info.i_size = -1;
1127                 msg_Dbg( p_access, "this frame size=%lld", atoll(p ) );
1128                 p_sys->i_remaining = atoll( p );
1129             }
1130             else
1131             {
1132                 p_access->info.i_size = i_tell + atoll( p );
1133                 msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
1134             }
1135         }
1136         else if( !strcasecmp( psz, "Location" ) )
1137         {
1138             char * psz_new_loc;
1139
1140             /* This does not follow RFC 2068, but yet if the url is not absolute,
1141              * handle it as everyone does. */
1142             if( p[0] == '/' )
1143             {
1144                 const char *psz_http_ext = p_sys->b_ssl ? "s" : "" ;
1145
1146                 if( p_sys->url.i_port == ( p_sys->b_ssl ? 443 : 80 ) )
1147                 {
1148                     asprintf(&psz_new_loc, "http%s://%s%s", psz_http_ext,
1149                              p_sys->url.psz_host, p);
1150                 }
1151                 else
1152                 {
1153                     asprintf(&psz_new_loc, "http%s://%s:%d%s", psz_http_ext,
1154                              p_sys->url.psz_host, p_sys->url.i_port, p);
1155                 }
1156             }
1157             else
1158             {
1159                 psz_new_loc = strdup( p );
1160             }
1161
1162             if( p_sys->psz_location ) free( p_sys->psz_location );
1163             p_sys->psz_location = psz_new_loc;
1164         }
1165         else if( !strcasecmp( psz, "Content-Type" ) )
1166         {
1167             if( p_sys->psz_mime ) free( p_sys->psz_mime );
1168             p_sys->psz_mime = strdup( p );
1169             msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1170         }
1171         else if( !strcasecmp( psz, "Pragma" ) )
1172         {
1173             if( !strcasecmp( psz, "Pragma: features" ) )
1174                 p_sys->b_mms = VLC_TRUE;
1175             if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
1176             p_sys->psz_pragma = strdup( p );
1177             msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1178         }
1179         else if( !strcasecmp( psz, "Server" ) )
1180         {
1181             msg_Dbg( p_access, "Server: %s", p );
1182             if( !strncasecmp( p, "Icecast", 7 ) ||
1183                 !strncasecmp( p, "Nanocaster", 10 ) )
1184             {
1185                 /* Remember if this is Icecast
1186                  * we need to force demux in this case without breaking
1187                  *  autodetection */
1188
1189                 /* Let live 365 streams (nanocaster) piggyback on the icecast
1190                  * routine. They look very similar */
1191
1192                 p_sys->b_reconnect = VLC_TRUE;
1193                 p_sys->b_pace_control = VLC_FALSE;
1194                 p_sys->b_icecast = VLC_TRUE;
1195             }
1196         }
1197         else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1198         {
1199             msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1200             if( !strncasecmp( p, "chunked", 7 ) )
1201             {
1202                 p_sys->b_chunked = VLC_TRUE;
1203             }
1204         }
1205         else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1206         {
1207             msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1208             p_sys->i_icy_meta = atoi( p );
1209             if( p_sys->i_icy_meta < 0 )
1210                 p_sys->i_icy_meta = 0;
1211
1212             msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1213         }
1214         else if( !strcasecmp( psz, "Icy-Name" ) )
1215         {
1216             if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
1217             p_sys->psz_icy_name = strdup( p );
1218             msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1219
1220             p_sys->b_icecast = VLC_TRUE; /* be on the safeside. set it here as well. */
1221             p_sys->b_reconnect = VLC_TRUE;
1222             p_sys->b_pace_control = VLC_FALSE;
1223         }
1224         else if( !strcasecmp( psz, "Icy-Genre" ) )
1225         {
1226             if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
1227             p_sys->psz_icy_genre = strdup( p );
1228             msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1229         }
1230         else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1231         {
1232             msg_Dbg( p_access, "Icy-Notice: %s", p );
1233         }
1234         else if( !strncasecmp( psz, "icy-", 4 ) ||
1235                  !strncasecmp( psz, "ice-", 4 ) ||
1236                  !strncasecmp( psz, "x-audiocast", 11 ) )
1237         {
1238             msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1239         } else if( !strcasecmp( psz, "Set-Cookie" ) )
1240         {
1241             if( p_sys->cookies )
1242             {
1243                 msg_Dbg( p_access, "Accepting Cookie: %s", p );
1244                 cookie_append( p_sys->cookies, strdup(p) );
1245             }
1246             else
1247                 msg_Dbg( p_access, "We have a Cookie we won't remember: %s", p );
1248         }
1249
1250         free( psz );
1251     }
1252     return VLC_SUCCESS;
1253
1254 error:
1255     Disconnect( p_access );
1256     return VLC_EGENERIC;
1257 }
1258
1259 /*****************************************************************************
1260  * Disconnect:
1261  *****************************************************************************/
1262 static void Disconnect( access_t *p_access )
1263 {
1264     access_sys_t *p_sys = p_access->p_sys;
1265
1266     if( p_sys->p_tls != NULL)
1267     {
1268         tls_ClientDelete( p_sys->p_tls );
1269         p_sys->p_tls = NULL;
1270         p_sys->p_vs = NULL;
1271     }
1272     if( p_sys->fd != -1)
1273     {
1274         net_Close(p_sys->fd);
1275         p_sys->fd = -1;
1276     }
1277
1278 }
1279
1280 /*****************************************************************************
1281  * Cookies (FIXME: we may want to rewrite that using a nice structure to hold
1282  * them) (FIXME: only support the "domain=" param)
1283  *****************************************************************************/
1284
1285 /* Get the NAME=VALUE part of the Cookie */
1286 static char * cookie_get_content( const char * cookie )
1287 {
1288     char * ret = strdup( cookie );
1289     if( !ret ) return NULL;
1290     char * str = ret;
1291     /* Look for a ';' */
1292     while( *str && *str != ';' ) str++;
1293     /* Replace it by a end-char */
1294     if( *str == ';' ) *str = 0;
1295     return ret;
1296 }
1297
1298 /* Get the domain where the cookie is stored */
1299 static char * cookie_get_domain( const char * cookie )
1300 {
1301     const char * str = cookie;
1302     static const char domain[] = "domain=";
1303     if( !str )
1304         return NULL;
1305     /* Look for a ';' */
1306     while( *str )
1307     {
1308         if( !strncmp( str, domain, sizeof(domain) - 1 /* minus \0 */ ) )
1309         {
1310             str += sizeof(domain) - 1 /* minus \0 */;
1311             char * ret = strdup( str );
1312             /* Now remove the next ';' if present */
1313             char * ret_iter = ret;
1314             while( *ret_iter && *ret_iter != ';' ) ret_iter++;
1315             if( *ret_iter == ';' )
1316                 *ret_iter = 0;
1317             return ret;
1318         }
1319         /* Go to next ';' field */
1320         while( *str && *str != ';' ) str++;
1321         if( *str == ';' ) str++;
1322         /* skip blank */
1323         while( *str && *str == ' ' ) str++;
1324     }
1325     return NULL;
1326 }
1327
1328 /* Get NAME in the NAME=VALUE field */
1329 static char * cookie_get_name( const char * cookie )
1330 {
1331     char * ret = cookie_get_content( cookie ); /* NAME=VALUE */
1332     if( !ret ) return NULL;
1333     char * str = ret;
1334     while( *str && *str != '=' ) str++;
1335     *str = 0;
1336     return ret;
1337 }
1338
1339 /* Add a cookie in cookies, checking to see how it should be added */
1340 static void cookie_append( vlc_array_t * cookies, char * cookie )
1341 {
1342     int i;
1343
1344     if( !cookie )
1345         return;
1346
1347     char * cookie_name = cookie_get_name( cookie );
1348
1349     /* Don't send invalid cookies */
1350     if( !cookie_name )
1351         return;
1352
1353     char * cookie_domain = cookie_get_domain( cookie );
1354     for( i = 0; i < vlc_array_count( cookies ); i++ )
1355     {
1356         char * current_cookie = vlc_array_item_at_index( cookies, i );
1357         char * current_cookie_name = cookie_get_name( current_cookie );
1358         char * current_cookie_domain = cookie_get_domain( current_cookie );
1359
1360         assert( current_cookie_name );
1361
1362         vlc_bool_t is_domain_matching = ( cookie_domain && current_cookie_domain &&
1363                                          !strcmp( cookie_domain, current_cookie_domain ) );
1364
1365         if( is_domain_matching && !strcmp( cookie_name, current_cookie_name )  )
1366         {
1367             /* Remove previous value for this cookie */
1368             free( current_cookie );
1369             vlc_array_remove( cookies, i );
1370
1371             /* Clean */
1372             free( current_cookie_name );
1373             free( current_cookie_domain );
1374             break;
1375         }
1376         free( current_cookie_name );
1377         free( current_cookie_domain );
1378     }
1379     free( cookie_name );
1380     free( cookie_domain );
1381     vlc_array_append( cookies, cookie );
1382 }
1383