]> git.sesse.net Git - vlc/blob - modules/access/http.c
Fix erroneous use of freed data in the cookie forwarding code.
[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             if( psz_cookie_content &&
971                  /* Check to see if we are in the right domain */
972                 ( !psz_cookie_domain || strstr( p_sys->url.psz_host, psz_cookie_domain ))
973               )
974             {
975                 msg_Dbg( p_access, "Sending Cookie %s", psz_cookie_content );
976                 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Cookie: %s\r\n", psz_cookie_content ) < 0 )
977                     msg_Err( p_access, "failed to send Cookie" );
978             }
979             free( psz_cookie_content );
980             free( psz_cookie_domain );
981         }
982     }
983
984     /* Authentication */
985     if( p_sys->url.psz_username || p_sys->url.psz_password )
986     {
987         char buf[strlen( p_sys->url.psz_username ?: "" )
988                   + strlen( p_sys->url.psz_password ?: "" ) + 2];
989         char *b64;
990
991         snprintf( buf, sizeof( buf ), "%s:%s", p_sys->url.psz_username ?: "",
992                   p_sys->url.psz_password ?: "" );
993         b64 = vlc_b64_encode( buf );
994
995         if( b64 != NULL )
996         {
997              net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
998                          "Authorization: Basic %s\r\n", b64 );
999              free( b64 );
1000         }
1001     }
1002
1003     /* Proxy Authentication */
1004     if( p_sys->proxy.psz_username || p_sys->proxy.psz_password )
1005     {
1006         char buf[strlen( p_sys->proxy.psz_username ?: "" )
1007                   + strlen( p_sys->proxy.psz_password ?: "" )];
1008         char *b64;
1009
1010         snprintf( buf, sizeof( buf ), "%s:%s", p_sys->proxy.psz_username ?: "",
1011                   p_sys->proxy.psz_password ?: "" );
1012         b64 = vlc_b64_encode( buf );
1013
1014         if( b64 != NULL)
1015         {
1016             net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1017                         "Proxy-Authorization: Basic %s\r\n", b64 );
1018             free( b64 );
1019         }
1020     }
1021
1022     /* ICY meta data request */
1023     net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
1024
1025
1026     if( p_sys->b_continuous )
1027     {
1028         net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
1029                     "Connection: Keep-Alive\r\n" );
1030     }
1031     else if( p_sys->i_version == 1 )
1032     {
1033         net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
1034                     "Connection: Close\r\n");
1035     }
1036
1037     if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "\r\n" ) < 0 )
1038     {
1039         msg_Err( p_access, "failed to send request" );
1040         Disconnect( p_access );
1041         return VLC_EGENERIC;
1042     }
1043
1044     /* Read Answer */
1045     if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs ) ) == NULL )
1046     {
1047         msg_Err( p_access, "failed to read answer" );
1048         goto error;
1049     }
1050     if( !strncmp( psz, "HTTP/1.", 7 ) )
1051     {
1052         p_sys->psz_protocol = "HTTP";
1053         p_sys->i_code = atoi( &psz[9] );
1054     }
1055     else if( !strncmp( psz, "ICY", 3 ) )
1056     {
1057         p_sys->psz_protocol = "ICY";
1058         p_sys->i_code = atoi( &psz[4] );
1059         p_sys->b_reconnect = VLC_TRUE;
1060     }
1061     else
1062     {
1063         msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1064         free( psz );
1065         goto error;
1066     }
1067     msg_Dbg( p_access, "protocol '%s' answer code %d",
1068              p_sys->psz_protocol, p_sys->i_code );
1069     if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1070     {
1071         p_sys->b_seekable = VLC_FALSE;
1072     }
1073     if( p_sys->i_code != 206 && p_sys->i_code != 401 )
1074     {
1075         p_sys->b_seekable = VLC_FALSE;
1076     }
1077     /* Authentication error - We'll have to display the dialog */
1078     if( p_sys->i_code == 401 )
1079     {
1080
1081     }
1082     /* Other fatal error */
1083     else if( p_sys->i_code >= 400 )
1084     {
1085         msg_Err( p_access, "error: %s", psz );
1086         free( psz );
1087         goto error;
1088     }
1089     free( psz );
1090
1091     for( ;; )
1092     {
1093         char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs );
1094         char *p;
1095
1096         if( psz == NULL )
1097         {
1098             msg_Err( p_access, "failed to read answer" );
1099             goto error;
1100         }
1101
1102         /* msg_Dbg( p_input, "Line=%s", psz ); */
1103         if( *psz == '\0' )
1104         {
1105             free( psz );
1106             break;
1107         }
1108
1109
1110         if( ( p = strchr( psz, ':' ) ) == NULL )
1111         {
1112             msg_Err( p_access, "malformed header line: %s", psz );
1113             free( psz );
1114             goto error;
1115         }
1116         *p++ = '\0';
1117         while( *p == ' ' ) p++;
1118
1119         if( !strcasecmp( psz, "Content-Length" ) )
1120         {
1121             if( p_sys->b_continuous )
1122             {
1123                 p_access->info.i_size = -1;
1124                 msg_Dbg( p_access, "this frame size=%lld", atoll(p ) );
1125                 p_sys->i_remaining = atoll( p );
1126             }
1127             else
1128             {
1129                 p_access->info.i_size = i_tell + atoll( p );
1130                 msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
1131             }
1132         }
1133         else if( !strcasecmp( psz, "Location" ) )
1134         {
1135             char * psz_new_loc;
1136
1137             /* This does not follow RFC 2068, but yet if the url is not absolute,
1138              * handle it as everyone does. */
1139             if( p[0] == '/' )
1140             {
1141                 const char *psz_http_ext = p_sys->b_ssl ? "s" : "" ;
1142
1143                 if( p_sys->url.i_port == ( p_sys->b_ssl ? 443 : 80 ) )
1144                 {
1145                     asprintf(&psz_new_loc, "http%s://%s%s", psz_http_ext,
1146                              p_sys->url.psz_host, p);
1147                 }
1148                 else
1149                 {
1150                     asprintf(&psz_new_loc, "http%s://%s:%d%s", psz_http_ext,
1151                              p_sys->url.psz_host, p_sys->url.i_port, p);
1152                 }
1153             }
1154             else
1155             {
1156                 psz_new_loc = strdup( p );
1157             }
1158
1159             if( p_sys->psz_location ) free( p_sys->psz_location );
1160             p_sys->psz_location = psz_new_loc;
1161         }
1162         else if( !strcasecmp( psz, "Content-Type" ) )
1163         {
1164             if( p_sys->psz_mime ) free( p_sys->psz_mime );
1165             p_sys->psz_mime = strdup( p );
1166             msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1167         }
1168         else if( !strcasecmp( psz, "Pragma" ) )
1169         {
1170             if( !strcasecmp( psz, "Pragma: features" ) )
1171                 p_sys->b_mms = VLC_TRUE;
1172             if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
1173             p_sys->psz_pragma = strdup( p );
1174             msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1175         }
1176         else if( !strcasecmp( psz, "Server" ) )
1177         {
1178             msg_Dbg( p_access, "Server: %s", p );
1179             if( !strncasecmp( p, "Icecast", 7 ) ||
1180                 !strncasecmp( p, "Nanocaster", 10 ) )
1181             {
1182                 /* Remember if this is Icecast
1183                  * we need to force demux in this case without breaking
1184                  *  autodetection */
1185
1186                 /* Let live 365 streams (nanocaster) piggyback on the icecast
1187                  * routine. They look very similar */
1188
1189                 p_sys->b_reconnect = VLC_TRUE;
1190                 p_sys->b_pace_control = VLC_FALSE;
1191                 p_sys->b_icecast = VLC_TRUE;
1192             }
1193         }
1194         else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1195         {
1196             msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1197             if( !strncasecmp( p, "chunked", 7 ) )
1198             {
1199                 p_sys->b_chunked = VLC_TRUE;
1200             }
1201         }
1202         else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1203         {
1204             msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1205             p_sys->i_icy_meta = atoi( p );
1206             if( p_sys->i_icy_meta < 0 )
1207                 p_sys->i_icy_meta = 0;
1208
1209             msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1210         }
1211         else if( !strcasecmp( psz, "Icy-Name" ) )
1212         {
1213             if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
1214             p_sys->psz_icy_name = strdup( p );
1215             msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1216
1217             p_sys->b_icecast = VLC_TRUE; /* be on the safeside. set it here as well. */
1218             p_sys->b_reconnect = VLC_TRUE;
1219             p_sys->b_pace_control = VLC_FALSE;
1220         }
1221         else if( !strcasecmp( psz, "Icy-Genre" ) )
1222         {
1223             if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
1224             p_sys->psz_icy_genre = strdup( p );
1225             msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1226         }
1227         else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1228         {
1229             msg_Dbg( p_access, "Icy-Notice: %s", p );
1230         }
1231         else if( !strncasecmp( psz, "icy-", 4 ) ||
1232                  !strncasecmp( psz, "ice-", 4 ) ||
1233                  !strncasecmp( psz, "x-audiocast", 11 ) )
1234         {
1235             msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1236         } else if( !strcasecmp( psz, "Set-Cookie" ) )
1237         {
1238             if( p_sys->cookies )
1239             {
1240                 msg_Dbg( p_access, "Accepting Cookie: %s", p );
1241                 cookie_append( p_sys->cookies, strdup(p) );
1242             }
1243             else
1244                 msg_Dbg( p_access, "We have a Cookie we won't remember: %s", p );
1245         }
1246
1247         free( psz );
1248     }
1249     return VLC_SUCCESS;
1250
1251 error:
1252     Disconnect( p_access );
1253     return VLC_EGENERIC;
1254 }
1255
1256 /*****************************************************************************
1257  * Disconnect:
1258  *****************************************************************************/
1259 static void Disconnect( access_t *p_access )
1260 {
1261     access_sys_t *p_sys = p_access->p_sys;
1262
1263     if( p_sys->p_tls != NULL)
1264     {
1265         tls_ClientDelete( p_sys->p_tls );
1266         p_sys->p_tls = NULL;
1267         p_sys->p_vs = NULL;
1268     }
1269     if( p_sys->fd != -1)
1270     {
1271         net_Close(p_sys->fd);
1272         p_sys->fd = -1;
1273     }
1274
1275 }
1276
1277 /*****************************************************************************
1278  * Cookies (FIXME: we may want to rewrite that using a nice structure to hold
1279  * them) (FIXME: only support the "domain=" param)
1280  *****************************************************************************/
1281
1282 /* Get the NAME=VALUE part of the Cookie */
1283 static char * cookie_get_content( const char * cookie )
1284 {
1285     char * ret = strdup( cookie );
1286     if( !ret ) return NULL;
1287     char * str = ret;
1288     /* Look for a ';' */
1289     while( *str && *str != ';' ) str++;
1290     /* Replace it by a end-char */
1291     if( *str == ';' ) *str = 0;
1292     return ret;
1293 }
1294
1295 /* Get the domain where the cookie is stored */
1296 static char * cookie_get_domain( const char * cookie )
1297 {
1298     const char * str = cookie;
1299     static const char domain[] = "domain=";
1300     if( !str )
1301         return NULL;
1302     /* Look for a ';' */
1303     while( *str )
1304     {
1305         if( !strncmp( str, domain, sizeof(domain) - 1 /* minus \0 */ ) )
1306         {
1307             str += sizeof(domain) - 1 /* minus \0 */;
1308             char * ret = strdup( str );
1309             /* Now remove the next ';' if present */
1310             char * ret_iter = ret;
1311             while( *ret_iter && *ret_iter != ';' ) ret_iter++;
1312             if( *ret_iter == ';' )
1313                 *ret_iter = 0;
1314             return ret;
1315         }
1316         /* Go to next ';' field */
1317         while( *str && *str != ';' ) str++;
1318         if( *str == ';' ) str++;
1319         /* skip blank */
1320         while( *str && *str == ' ' ) str++;
1321     }
1322     return NULL;
1323 }
1324
1325 /* Get NAME in the NAME=VALUE field */
1326 static char * cookie_get_name( const char * cookie )
1327 {
1328     char * ret = cookie_get_content( cookie ); /* NAME=VALUE */
1329     if( !ret ) return NULL;
1330     char * str = ret;
1331     while( *str && *str != '=' ) str++;
1332     *str = 0;
1333     return ret;
1334 }
1335
1336 /* Add a cookie in cookies, checking to see how it should be added */
1337 static void cookie_append( vlc_array_t * cookies, char * cookie )
1338 {
1339     int i;
1340     char * cookie_name = cookie_get_name( cookie );
1341     char * cookie_domain = cookie_get_domain( cookie );
1342     for( i = 0; i < vlc_array_count( cookies ); i++ )
1343     {
1344         char * current_cookie = vlc_array_item_at_index( cookies, i );
1345         char * current_cookie_name = cookie_get_name( current_cookie );
1346         char * current_cookie_domain = cookie_get_domain( current_cookie );
1347         if(!strcmp( cookie_name, current_cookie_name ) &&
1348            !strcmp( cookie_domain, current_cookie_domain ))
1349         {
1350             /* Remove previous value for this cookie */
1351             free( current_cookie );
1352             vlc_array_remove( cookies, i );
1353
1354             /* Clean */
1355             free( current_cookie_name );
1356             free( current_cookie_domain );
1357             break;
1358         }
1359         free( current_cookie_name );
1360         free( current_cookie_domain );
1361     }
1362     free( cookie_name );
1363     free( cookie_domain );
1364     vlc_array_append( cookies, cookie );
1365 }
1366