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