1 /*****************************************************************************
2 * http.c: HTTP input module
3 *****************************************************************************
4 * Copyright (C) 2001-2008 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Christophe Massiot <massiot@via.ecp.fr>
9 * RĂ©mi Denis-Courmont <rem # videolan.org>
10 * Antoine Cellerier <dionoea at videolan dot org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
38 #include <vlc_access.h>
40 #include <vlc_dialog.h>
42 #include <vlc_network.h>
45 #include <vlc_strings.h>
46 #include <vlc_charset.h>
47 #include <vlc_input.h>
66 /*****************************************************************************
68 *****************************************************************************/
69 static int Open ( vlc_object_t * );
70 static void Close( vlc_object_t * );
72 #define PROXY_TEXT N_("HTTP proxy")
73 #define PROXY_LONGTEXT N_( \
74 "HTTP proxy to be used It must be of the form " \
75 "http://[user@]myproxy.mydomain:myport/ ; " \
76 "if empty, the http_proxy environment variable will be tried." )
78 #define PROXY_PASS_TEXT N_("HTTP proxy password")
79 #define PROXY_PASS_LONGTEXT N_( \
80 "If your HTTP proxy requires a password, set it here." )
82 #define RECONNECT_TEXT N_("Auto re-connect")
83 #define RECONNECT_LONGTEXT N_( \
84 "Automatically try to reconnect to the stream in case of a sudden " \
87 #define CONTINUOUS_TEXT N_("Continuous stream")
88 #define CONTINUOUS_LONGTEXT N_("Read a file that is " \
89 "being constantly updated (for example, a JPG file on a server). " \
90 "You should not globally enable this option as it will break all other " \
91 "types of HTTP streams." )
93 #define FORWARD_COOKIES_TEXT N_("Forward Cookies")
94 #define FORWARD_COOKIES_LONGTEXT N_("Forward Cookies across http redirections.")
96 #define REFERER_TEXT N_("HTTP referer value")
97 #define REFERER_LONGTEXT N_("Customize the HTTP referer, simulating a previous document")
99 #define UA_TEXT N_("User Agent")
100 #define UA_LONGTEXT N_("You can use a custom User agent or use a known one")
103 set_description( N_("HTTP input") )
104 set_capability( "access", 0 )
105 set_shortname( N_( "HTTP(S)" ) )
106 set_category( CAT_INPUT )
107 set_subcategory( SUBCAT_INPUT_ACCESS )
109 add_string( "http-proxy", NULL, PROXY_TEXT, PROXY_LONGTEXT,
111 add_password( "http-proxy-pwd", NULL,
112 PROXY_PASS_TEXT, PROXY_PASS_LONGTEXT, false )
113 add_string( "http-referrer", NULL, REFERER_TEXT, REFERER_LONGTEXT, false )
115 add_string( "http-user-agent", NULL, UA_TEXT, UA_LONGTEXT, false )
118 add_bool( "http-reconnect", false, RECONNECT_TEXT,
119 RECONNECT_LONGTEXT, true )
120 add_bool( "http-continuous", false, CONTINUOUS_TEXT,
121 CONTINUOUS_LONGTEXT, true )
123 add_bool( "http-forward-cookies", true, FORWARD_COOKIES_TEXT,
124 FORWARD_COOKIES_LONGTEXT, true )
125 /* 'itpc' = iTunes Podcast */
126 add_shortcut( "http", "https", "unsv", "itpc", "icyx" )
127 set_callbacks( Open, Close )
130 /*****************************************************************************
132 *****************************************************************************/
143 char *psz_user_agent;
150 http_auth_t proxy_auth;
151 char *psz_proxy_passbuf;
155 const char *psz_protocol;
177 uint64_t i_icy_offset;
182 uint64_t i_remaining;
191 vlc_array_t * cookies;
195 static int OpenWithCookies( vlc_object_t *p_this, const char *psz_access,
196 unsigned i_redirect, vlc_array_t *cookies );
199 static ssize_t Read( access_t *, uint8_t *, size_t );
200 static ssize_t ReadCompressed( access_t *, uint8_t *, size_t );
201 static int Seek( access_t *, uint64_t );
202 static int Control( access_t *, int, va_list );
205 static int Connect( access_t *, uint64_t );
206 static int Request( access_t *p_access, uint64_t i_tell );
207 static void Disconnect( access_t * );
209 /* Small Cookie utilities. Cookies support is partial. */
210 static char * cookie_get_content( const char * cookie );
211 static char * cookie_get_domain( const char * cookie );
212 static char * cookie_get_name( const char * cookie );
213 static void cookie_append( vlc_array_t * cookies, char * cookie );
216 static void AuthReply( access_t *p_acces, const char *psz_prefix,
217 vlc_url_t *p_url, http_auth_t *p_auth );
218 static int AuthCheckReply( access_t *p_access, const char *psz_header,
219 vlc_url_t *p_url, http_auth_t *p_auth );
221 /*****************************************************************************
223 *****************************************************************************/
224 static int Open( vlc_object_t *p_this )
226 access_t *p_access = (access_t*)p_this;
227 return OpenWithCookies( p_this, p_access->psz_access, 5, NULL );
231 * Open the given url using the given cookies
232 * @param p_this: the vlc object
233 * @psz_access: the acces to use (http, https, ...) (this value must be used
234 * instead of p_access->psz_access)
235 * @i_redirect: number of redirections remaining
236 * @cookies: the available cookies
237 * @return vlc error codes
239 static int OpenWithCookies( vlc_object_t *p_this, const char *psz_access,
240 unsigned i_redirect, vlc_array_t *cookies )
242 access_t *p_access = (access_t*)p_this;
246 /* Only forward an store cookies if the corresponding option is activated */
247 bool b_forward_cookies = var_InheritBool( p_access, "http-forward-cookies" );
248 vlc_array_t * saved_cookies = b_forward_cookies ? (cookies ? cookies : vlc_array_new()) : NULL;
250 /* Set up p_access */
251 STANDARD_READ_ACCESS_INIT;
253 p_access->pf_read = ReadCompressed;
256 p_sys->b_proxy = false;
257 p_sys->psz_proxy_passbuf = NULL;
258 p_sys->i_version = 1;
259 p_sys->b_seekable = true;
260 p_sys->psz_mime = NULL;
261 p_sys->psz_pragma = NULL;
262 p_sys->b_mms = false;
263 p_sys->b_icecast = false;
264 p_sys->psz_location = NULL;
265 p_sys->psz_user_agent = NULL;
266 p_sys->psz_referrer = NULL;
267 p_sys->b_pace_control = true;
268 p_sys->b_ssl = false;
270 p_sys->b_compressed = false;
271 /* 15 is the max windowBits, +32 to enable optional gzip decoding */
272 if( inflateInit2( &p_sys->inflate.stream, 32+15 ) != Z_OK )
273 msg_Warn( p_access, "Error during zlib initialisation: %s",
274 p_sys->inflate.stream.msg );
275 if( zlibCompileFlags() & (1<<17) )
276 msg_Warn( p_access, "Your zlib was compiled without gzip support." );
277 p_sys->inflate.p_buffer = NULL;
281 p_sys->i_icy_meta = 0;
282 p_sys->i_icy_offset = 0;
283 p_sys->psz_icy_name = NULL;
284 p_sys->psz_icy_genre = NULL;
285 p_sys->psz_icy_title = NULL;
286 p_sys->i_remaining = 0;
287 p_sys->b_persist = false;
288 p_sys->b_has_size = false;
289 p_access->info.i_size = 0;
290 p_access->info.i_pos = 0;
291 p_access->info.b_eof = false;
293 p_sys->cookies = saved_cookies;
295 http_auth_Init( &p_sys->auth );
296 http_auth_Init( &p_sys->proxy_auth );
298 /* Parse URI - remove spaces */
299 p = psz = strdup( p_access->psz_location );
300 while( (p = strchr( p, ' ' )) != NULL )
302 vlc_UrlParse( &p_sys->url, psz, 0 );
305 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
307 msg_Warn( p_access, "invalid host" );
310 if( !strncmp( psz_access, "https", 5 ) )
314 if( p_sys->url.i_port <= 0 )
315 p_sys->url.i_port = 443;
319 if( p_sys->url.i_port <= 0 )
320 p_sys->url.i_port = 80;
323 /* Determine the HTTP user agent */
324 /* See RFC2616 §2.2 token and comment definition, and §3.8 and
325 * §14.43 user-agent header */
326 p_sys->psz_user_agent = var_InheritString( p_access, "http-user-agent" );
327 if (p_sys->psz_user_agent)
329 unsigned comment_level = 0;
330 for( char *p = p_sys->psz_user_agent; *p; p++ )
333 if (comment_level == 0)
335 if( c < 32 || strchr( ")<>@,;:\\\"[]?={}", c ) )
336 *p = '_'; /* remove potentially harmful characters */
342 else if( c < 32 && strchr( "\t\r\n", c ) == NULL)
343 *p = '_'; /* remove potentially harmful characters */
347 if (comment_level == UINT_MAX)
352 /* truncate evil unclosed comments */
353 if (comment_level > 0)
355 char *p = strchr(p_sys->psz_user_agent, '(');
361 p_sys->psz_referrer = var_InheritString( p_access, "http-referrer" );
364 psz = var_InheritString( p_access, "http-proxy" );
367 p_sys->b_proxy = true;
368 vlc_UrlParse( &p_sys->proxy, psz, 0 );
374 pxProxyFactory *pf = px_proxy_factory_new();
379 i=asprintf(&buf, "%s://%s", psz_access, p_access->psz_location);
382 msg_Dbg(p_access, "asking libproxy about url '%s'", buf);
383 char **proxies = px_proxy_factory_get_proxies(pf, buf);
386 msg_Dbg(p_access, "libproxy suggest to use '%s'", proxies[0]);
387 if(strcmp(proxies[0],"direct://") != 0)
389 p_sys->b_proxy = true;
390 vlc_UrlParse( &p_sys->proxy, proxies[0], 0);
393 for(i=0;proxies[i];i++) free(proxies[i]);
397 px_proxy_factory_free(pf);
401 msg_Err(p_access, "Allocating memory for libproxy failed");
404 #elif defined( WIN32 )
407 /* Try to get the proxy server address from Windows internet settings using registry. */
410 if( RegOpenKeyEx( HKEY_CURRENT_USER, "Software\\Microsoft"
411 "\\Windows\\CurrentVersion\\Internet Settings",
412 0, KEY_READ, &h_key ) == ERROR_SUCCESS )
414 DWORD len = sizeof( DWORD );
417 /* Get the proxy enable value */
418 if( RegQueryValueEx( h_key, "ProxyEnable", NULL, NULL,
419 &proxyEnable, &len ) == ERROR_SUCCESS
422 /* Proxy is enabled */
423 /* Get the proxy URL :
424 Proxy server value in the registry can be something like "address:port"
425 or "ftp=address1:port1;http=address2:port2 ..." depending of the
427 unsigned char key[256];
430 if( RegQueryValueEx( h_key, "ProxyServer", NULL, NULL,
431 key, &len ) == ERROR_SUCCESS )
433 /* FIXME: This is lame. The string should be tokenized. */
435 char *psz_proxy = strstr( (char *)key, "http=" );
436 if( psz_proxy != NULL )
439 char *end = strchr( psz_proxy, ';' );
444 psz_proxy = (char *)key;
445 /* Set proxy enable for this connection. */
446 p_sys->b_proxy = true;
447 vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
451 msg_Dbg( p_access, "HTTP proxy disabled (MSIE)" );
457 psz = getenv( "http_proxy" );
460 p_sys->b_proxy = true;
461 vlc_UrlParse( &p_sys->proxy, psz, 0 );
466 if( psz ) /* No, this is NOT a use-after-free error */
468 psz = var_InheritString( p_access, "http-proxy-pwd" );
470 p_sys->proxy.psz_password = p_sys->psz_proxy_passbuf = psz;
475 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
477 msg_Warn( p_access, "invalid proxy host" );
480 if( p_sys->proxy.i_port <= 0 )
482 p_sys->proxy.i_port = 80;
486 msg_Dbg( p_access, "http: server='%s' port=%d file='%s'",
487 p_sys->url.psz_host, p_sys->url.i_port,
488 p_sys->url.psz_path != NULL ? p_sys->url.psz_path : "" );
491 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
492 p_sys->proxy.i_port );
494 if( p_sys->url.psz_username && *p_sys->url.psz_username )
496 msg_Dbg( p_access, " user='%s'", p_sys->url.psz_username );
499 p_sys->b_reconnect = var_InheritBool( p_access, "http-reconnect" );
500 p_sys->b_continuous = var_InheritBool( p_access, "http-continuous" );
504 switch( Connect( p_access, 0 ) )
510 /* Retry with http 1.0 */
511 msg_Dbg( p_access, "switching to HTTP version 1.0" );
512 p_sys->i_version = 0;
513 p_sys->b_seekable = false;
515 if( !vlc_object_alive (p_access) || Connect( p_access, 0 ) )
525 if( p_sys->i_code == 401 )
527 char *psz_login, *psz_password;
529 if( p_sys->url.psz_username && p_sys->url.psz_password &&
530 p_sys->auth.psz_nonce && p_sys->auth.i_nonce == 0 )
532 Disconnect( p_access );
535 msg_Dbg( p_access, "authentication failed for realm %s",
536 p_sys->auth.psz_realm );
537 dialog_Login( p_access, &psz_login, &psz_password,
538 _("HTTP authentication"),
539 _("Please enter a valid login name and a password for realm %s."),
540 p_sys->auth.psz_realm );
541 if( psz_login != NULL && psz_password != NULL )
543 msg_Dbg( p_access, "retrying with user=%s", psz_login );
544 p_sys->url.psz_username = psz_login;
545 p_sys->url.psz_password = psz_password;
546 Disconnect( p_access );
552 free( psz_password );
557 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
558 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
559 p_sys->psz_location && *p_sys->psz_location )
561 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
563 /* Check the number of redirection already done */
564 if( i_redirect == 0 )
566 msg_Err( p_access, "Too many redirection: break potential infinite"
572 /* Do not accept redirection outside of HTTP works */
573 const char *psz_protocol;
574 if( !strncmp( p_sys->psz_location, "http:", 5 ) )
575 psz_protocol = "http";
576 else if( !strncmp( p_sys->psz_location, "https:", 6 ) )
577 psz_protocol = "https";
580 msg_Err( p_access, "insecure redirection ignored" );
583 free( p_access->psz_location );
584 p_access->psz_location = strdup( p_sys->psz_location );
585 /* Clean up current Open() run */
586 vlc_UrlClean( &p_sys->url );
587 http_auth_Reset( &p_sys->auth );
588 vlc_UrlClean( &p_sys->proxy );
589 free( p_sys->psz_proxy_passbuf );
590 http_auth_Reset( &p_sys->proxy_auth );
591 free( p_sys->psz_mime );
592 free( p_sys->psz_pragma );
593 free( p_sys->psz_location );
594 free( p_sys->psz_user_agent );
595 free( p_sys->psz_referrer );
597 Disconnect( p_access );
598 cookies = p_sys->cookies;
600 inflateEnd( &p_sys->inflate.stream );
604 /* Do new Open() run with new data */
605 return OpenWithCookies( p_this, psz_protocol, i_redirect - 1,
611 msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
615 if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
617 if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
619 if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
620 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
622 free( p_access->psz_demux );
623 p_access->psz_demux = strdup( "nsv" );
625 else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
626 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
628 free( p_access->psz_demux );
629 p_access->psz_demux = strdup( "m4a" );
631 else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
633 free( p_access->psz_demux );
634 p_access->psz_demux = strdup( "mp3" );
637 msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
638 p_access->psz_demux );
640 #if 0 /* Doesn't work really well because of the pre-buffering in
641 * shoutcast servers (the buffer content will be sent as fast as
643 p_sys->b_pace_control = false;
646 else if( !p_sys->psz_mime )
648 free( p_access->psz_demux );
650 p_access->psz_demux = strdup( "mp3" );
652 /* else probably Ogg Vorbis */
654 else if( !strcasecmp( psz_access, "unsv" ) &&
656 !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
658 free( p_access->psz_demux );
659 /* Grrrr! detect ultravox server and force NSV demuxer */
660 p_access->psz_demux = strdup( "nsv" );
662 else if( !strcmp( psz_access, "itpc" ) )
664 free( p_access->psz_demux );
665 p_access->psz_demux = strdup( "podcast" );
667 else if( p_sys->psz_mime &&
668 !strncasecmp( p_sys->psz_mime, "application/xspf+xml", 20 ) &&
669 ( memchr( " ;\t", p_sys->psz_mime[20], 4 ) != NULL ) )
671 free( p_access->psz_demux );
672 p_access->psz_demux = strdup( "xspf-open" );
675 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
680 vlc_UrlClean( &p_sys->url );
681 vlc_UrlClean( &p_sys->proxy );
682 free( p_sys->psz_proxy_passbuf );
683 free( p_sys->psz_mime );
684 free( p_sys->psz_pragma );
685 free( p_sys->psz_location );
686 free( p_sys->psz_user_agent );
687 free( p_sys->psz_referrer );
689 Disconnect( p_access );
694 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
695 free(vlc_array_item_at_index( p_sys->cookies, i ));
696 vlc_array_destroy( p_sys->cookies );
700 inflateEnd( &p_sys->inflate.stream );
706 /*****************************************************************************
708 *****************************************************************************/
709 static void Close( vlc_object_t *p_this )
711 access_t *p_access = (access_t*)p_this;
712 access_sys_t *p_sys = p_access->p_sys;
714 vlc_UrlClean( &p_sys->url );
715 http_auth_Reset( &p_sys->auth );
716 vlc_UrlClean( &p_sys->proxy );
717 http_auth_Reset( &p_sys->proxy_auth );
719 free( p_sys->psz_mime );
720 free( p_sys->psz_pragma );
721 free( p_sys->psz_location );
723 free( p_sys->psz_icy_name );
724 free( p_sys->psz_icy_genre );
725 free( p_sys->psz_icy_title );
727 free( p_sys->psz_user_agent );
728 free( p_sys->psz_referrer );
730 Disconnect( p_access );
735 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
736 free(vlc_array_item_at_index( p_sys->cookies, i ));
737 vlc_array_destroy( p_sys->cookies );
741 inflateEnd( &p_sys->inflate.stream );
742 free( p_sys->inflate.p_buffer );
748 /*****************************************************************************
749 * Read: Read up to i_len bytes from the http connection and place in
750 * p_buffer. Return the actual number of bytes read
751 *****************************************************************************/
752 static int ReadICYMeta( access_t *p_access );
753 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
755 access_sys_t *p_sys = p_access->p_sys;
758 if( p_sys->fd == -1 )
761 if( p_sys->b_has_size )
763 /* Remaining bytes in the file */
764 uint64_t remainder = p_access->info.i_size - p_access->info.i_pos;
765 if( remainder < i_len )
768 /* Remaining bytes in the response */
769 if( p_sys->i_remaining < i_len )
770 i_len = p_sys->i_remaining;
773 if( p_sys->b_chunked )
775 if( p_sys->i_chunk < 0 )
778 if( p_sys->i_chunk <= 0 )
780 char *psz = net_Gets( p_access, p_sys->fd, p_sys->p_vs );
781 /* read the chunk header */
784 /* fatal error - end of file */
785 msg_Dbg( p_access, "failed reading chunk-header line" );
788 p_sys->i_chunk = strtoll( psz, NULL, 16 );
791 if( p_sys->i_chunk <= 0 ) /* eof */
798 if( i_len > p_sys->i_chunk )
799 i_len = p_sys->i_chunk;
805 if( p_sys->i_icy_meta > 0 && p_access->info.i_pos-p_sys->i_icy_offset > 0 )
807 int64_t i_next = p_sys->i_icy_meta -
808 (p_access->info.i_pos - p_sys->i_icy_offset ) % p_sys->i_icy_meta;
810 if( i_next == p_sys->i_icy_meta )
812 if( ReadICYMeta( p_access ) )
819 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, false );
823 if( p_sys->b_chunked )
825 p_sys->i_chunk -= i_read;
826 if( p_sys->i_chunk <= 0 )
828 /* read the empty line */
829 char *psz = net_Gets( p_access, p_sys->fd, p_sys->p_vs );
837 * I very much doubt that this will work.
838 * If i_read == 0, the connection *IS* dead, so the only
839 * sensible thing to do is Disconnect() and then retry.
840 * Otherwise, I got recv() completely wrong. -- Courmisch
842 if( p_sys->b_continuous )
844 Request( p_access, 0 );
845 p_sys->b_continuous = false;
846 i_read = Read( p_access, p_buffer, i_len );
847 p_sys->b_continuous = true;
849 Disconnect( p_access );
850 if( p_sys->b_reconnect && vlc_object_alive( p_access ) )
852 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
853 if( Connect( p_access, p_access->info.i_pos ) )
855 msg_Dbg( p_access, "reconnection failed" );
859 p_sys->b_reconnect = false;
860 i_read = Read( p_access, p_buffer, i_len );
861 p_sys->b_reconnect = true;
870 p_sys->b_error = true;
875 assert( i_read >= 0 );
876 p_access->info.i_pos += i_read;
877 if( p_sys->b_has_size )
879 assert( p_access->info.i_pos <= p_access->info.i_size );
880 assert( (unsigned)i_read <= p_sys->i_remaining );
881 p_sys->i_remaining -= i_read;
887 p_access->info.b_eof = true;
891 static int ReadICYMeta( access_t *p_access )
893 access_sys_t *p_sys = p_access->p_sys;
899 /* Read meta data length */
900 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
907 i_read = buffer << 4;
908 /* msg_Dbg( p_access, "ICY meta size=%u", i_read); */
910 psz_meta = malloc( i_read + 1 );
911 if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
912 (uint8_t *)psz_meta, i_read, true ) != i_read )
918 psz_meta[i_read] = '\0'; /* Just in case */
920 /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
922 /* Now parse the meta */
923 /* Look for StreamTitle= */
924 p = strcasestr( (char *)psz_meta, "StreamTitle=" );
927 p += strlen( "StreamTitle=" );
928 if( *p == '\'' || *p == '"' )
930 char closing[] = { p[0], ';', '\0' };
931 char *psz = strstr( &p[1], closing );
933 psz = strchr( &p[1], ';' );
935 if( psz ) *psz = '\0';
939 char *psz = strchr( &p[1], ';' );
940 if( psz ) *psz = '\0';
943 if( !p_sys->psz_icy_title ||
944 strcmp( p_sys->psz_icy_title, &p[1] ) )
946 free( p_sys->psz_icy_title );
947 char *psz_tmp = strdup( &p[1] );
948 p_sys->psz_icy_title = EnsureUTF8( psz_tmp );
949 if( !p_sys->psz_icy_title )
951 p_access->info.i_update |= INPUT_UPDATE_META;
953 msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
962 static ssize_t ReadCompressed( access_t *p_access, uint8_t *p_buffer,
965 access_sys_t *p_sys = p_access->p_sys;
967 if( p_sys->b_compressed )
971 if( !p_sys->inflate.p_buffer )
972 p_sys->inflate.p_buffer = malloc( 256 * 1024 );
974 if( p_sys->inflate.stream.avail_in == 0 )
976 ssize_t i_read = Read( p_access, p_sys->inflate.p_buffer, 256 * 1024 );
977 if( i_read <= 0 ) return i_read;
978 p_sys->inflate.stream.next_in = p_sys->inflate.p_buffer;
979 p_sys->inflate.stream.avail_in = i_read;
982 p_sys->inflate.stream.avail_out = i_len;
983 p_sys->inflate.stream.next_out = p_buffer;
985 i_ret = inflate( &p_sys->inflate.stream, Z_SYNC_FLUSH );
986 msg_Warn( p_access, "inflate return value: %d, %s", i_ret, p_sys->inflate.stream.msg );
988 return i_len - p_sys->inflate.stream.avail_out;
992 return Read( p_access, p_buffer, i_len );
997 /*****************************************************************************
998 * Seek: close and re-open a connection at the right place
999 *****************************************************************************/
1000 static int Seek( access_t *p_access, uint64_t i_pos )
1002 msg_Dbg( p_access, "trying to seek to %"PRId64, i_pos );
1004 Disconnect( p_access );
1006 if( p_access->info.i_size
1007 && i_pos >= p_access->info.i_size ) {
1008 msg_Err( p_access, "seek to far" );
1009 int retval = Seek( p_access, p_access->info.i_size - 1 );
1010 if( retval == VLC_SUCCESS ) {
1011 uint8_t p_buffer[2];
1012 Read( p_access, p_buffer, 1);
1013 p_access->info.b_eof = false;
1017 if( Connect( p_access, i_pos ) )
1019 msg_Err( p_access, "seek failed" );
1020 p_access->info.b_eof = true;
1021 return VLC_EGENERIC;
1026 /*****************************************************************************
1028 *****************************************************************************/
1029 static int Control( access_t *p_access, int i_query, va_list args )
1031 access_sys_t *p_sys = p_access->p_sys;
1039 case ACCESS_CAN_SEEK:
1040 pb_bool = (bool*)va_arg( args, bool* );
1041 *pb_bool = p_sys->b_seekable;
1043 case ACCESS_CAN_FASTSEEK:
1044 pb_bool = (bool*)va_arg( args, bool* );
1047 case ACCESS_CAN_PAUSE:
1048 case ACCESS_CAN_CONTROL_PACE:
1049 pb_bool = (bool*)va_arg( args, bool* );
1051 #if 0 /* Disable for now until we have a clock synchro algo
1052 * which works with something else than MPEG over UDP */
1053 *pb_bool = p_sys->b_pace_control;
1059 case ACCESS_GET_PTS_DELAY:
1060 pi_64 = (int64_t*)va_arg( args, int64_t * );
1061 *pi_64 = INT64_C(1000)
1062 * var_InheritInteger( p_access, "network-caching" );
1066 case ACCESS_SET_PAUSE_STATE:
1069 case ACCESS_GET_META:
1070 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
1072 if( p_sys->psz_icy_name )
1073 vlc_meta_Set( p_meta, vlc_meta_Title, p_sys->psz_icy_name );
1074 if( p_sys->psz_icy_genre )
1075 vlc_meta_Set( p_meta, vlc_meta_Genre, p_sys->psz_icy_genre );
1076 if( p_sys->psz_icy_title )
1077 vlc_meta_Set( p_meta, vlc_meta_NowPlaying, p_sys->psz_icy_title );
1080 case ACCESS_GET_CONTENT_TYPE:
1081 *va_arg( args, char ** ) =
1082 p_sys->psz_mime ? strdup( p_sys->psz_mime ) : NULL;
1085 case ACCESS_GET_TITLE_INFO:
1086 case ACCESS_SET_TITLE:
1087 case ACCESS_SET_SEEKPOINT:
1088 case ACCESS_SET_PRIVATE_ID_STATE:
1089 return VLC_EGENERIC;
1092 msg_Warn( p_access, "unimplemented query in control" );
1093 return VLC_EGENERIC;
1099 /*****************************************************************************
1101 *****************************************************************************/
1102 static int Connect( access_t *p_access, uint64_t i_tell )
1104 access_sys_t *p_sys = p_access->p_sys;
1105 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
1108 free( p_sys->psz_location );
1109 free( p_sys->psz_mime );
1110 free( p_sys->psz_pragma );
1112 free( p_sys->psz_icy_genre );
1113 free( p_sys->psz_icy_name );
1114 free( p_sys->psz_icy_title );
1117 p_sys->psz_location = NULL;
1118 p_sys->psz_mime = NULL;
1119 p_sys->psz_pragma = NULL;
1120 p_sys->b_mms = false;
1121 p_sys->b_chunked = false;
1123 p_sys->i_icy_meta = 0;
1124 p_sys->i_icy_offset = i_tell;
1125 p_sys->psz_icy_name = NULL;
1126 p_sys->psz_icy_genre = NULL;
1127 p_sys->psz_icy_title = NULL;
1128 p_sys->i_remaining = 0;
1129 p_sys->b_persist = false;
1130 p_sys->b_has_size = false;
1131 p_access->info.i_size = 0;
1132 p_access->info.i_pos = i_tell;
1133 p_access->info.b_eof = false;
1135 /* Open connection */
1136 assert( p_sys->fd == -1 ); /* No open sockets (leaking fds is BAD) */
1137 p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
1138 if( p_sys->fd == -1 )
1140 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
1143 setsockopt (p_sys->fd, SOL_SOCKET, SO_KEEPALIVE, &(int){ 1 }, sizeof (int));
1145 /* Initialize TLS/SSL session */
1148 /* CONNECT to establish TLS tunnel through HTTP proxy */
1149 if( p_sys->b_proxy )
1152 unsigned i_status = 0;
1154 if( p_sys->i_version == 0 )
1156 /* CONNECT is not in HTTP/1.0 */
1157 Disconnect( p_access );
1161 net_Printf( p_access, p_sys->fd, NULL,
1162 "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
1163 p_sys->url.psz_host, p_sys->url.i_port,
1165 p_sys->url.psz_host, p_sys->url.i_port);
1167 psz = net_Gets( p_access, p_sys->fd, NULL );
1170 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
1171 Disconnect( p_access );
1175 sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
1178 if( ( i_status / 100 ) != 2 )
1180 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
1181 Disconnect( p_access );
1187 psz = net_Gets( p_access, p_sys->fd, NULL );
1190 msg_Err( p_access, "HTTP proxy connection failed" );
1191 Disconnect( p_access );
1200 if( !vlc_object_alive (p_access) || p_sys->b_error )
1202 Disconnect( p_access );
1209 /* TLS/SSL handshake */
1210 p_sys->p_tls = vlc_tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
1211 p_sys->url.psz_host );
1212 if( p_sys->p_tls == NULL )
1214 msg_Err( p_access, "cannot establish HTTP/TLS session" );
1215 Disconnect( p_access );
1218 p_sys->p_vs = &p_sys->p_tls->sock;
1221 return Request( p_access, i_tell ) ? -2 : 0;
1225 static int Request( access_t *p_access, uint64_t i_tell )
1227 access_sys_t *p_sys = p_access->p_sys;
1229 v_socket_t *pvs = p_sys->p_vs;
1230 p_sys->b_persist = false;
1232 p_sys->i_remaining = 0;
1233 if( p_sys->b_proxy )
1235 if( p_sys->url.psz_path )
1237 net_Printf( p_access, p_sys->fd, NULL,
1238 "GET http://%s:%d%s HTTP/1.%d\r\n",
1239 p_sys->url.psz_host, p_sys->url.i_port,
1240 p_sys->url.psz_path, p_sys->i_version );
1244 net_Printf( p_access, p_sys->fd, NULL,
1245 "GET http://%s:%d/ HTTP/1.%d\r\n",
1246 p_sys->url.psz_host, p_sys->url.i_port,
1252 const char *psz_path = p_sys->url.psz_path;
1253 if( !psz_path || !*psz_path )
1257 if( p_sys->url.i_port != (pvs ? 443 : 80) )
1259 net_Printf( p_access, p_sys->fd, pvs,
1260 "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
1261 psz_path, p_sys->i_version, p_sys->url.psz_host,
1262 p_sys->url.i_port );
1266 net_Printf( p_access, p_sys->fd, pvs,
1267 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
1268 psz_path, p_sys->i_version, p_sys->url.psz_host );
1272 net_Printf( p_access, p_sys->fd, pvs,
1273 "User-Agent: %s\r\n",
1274 p_sys->psz_user_agent );
1276 if (p_sys->psz_referrer)
1278 net_Printf( p_access, p_sys->fd, pvs,
1280 p_sys->psz_referrer);
1283 if( p_sys->i_version == 1 && ! p_sys->b_continuous )
1285 p_sys->b_persist = true;
1286 net_Printf( p_access, p_sys->fd, pvs,
1287 "Range: bytes=%"PRIu64"-\r\n", i_tell );
1288 net_Printf( p_access, p_sys->fd, pvs, "Connection: close\r\n" );
1292 if( p_sys->cookies )
1295 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
1297 const char * cookie = vlc_array_item_at_index( p_sys->cookies, i );
1298 char * psz_cookie_content = cookie_get_content( cookie );
1299 char * psz_cookie_domain = cookie_get_domain( cookie );
1301 assert( psz_cookie_content );
1303 /* FIXME: This is clearly not conforming to the rfc */
1304 bool is_in_right_domain = (!psz_cookie_domain || strstr( p_sys->url.psz_host, psz_cookie_domain ));
1306 if( is_in_right_domain )
1308 msg_Dbg( p_access, "Sending Cookie %s", psz_cookie_content );
1309 if( net_Printf( p_access, p_sys->fd, pvs, "Cookie: %s\r\n", psz_cookie_content ) < 0 )
1310 msg_Err( p_access, "failed to send Cookie" );
1312 free( psz_cookie_content );
1313 free( psz_cookie_domain );
1317 /* Authentication */
1318 if( p_sys->url.psz_username || p_sys->url.psz_password )
1319 AuthReply( p_access, "", &p_sys->url, &p_sys->auth );
1321 /* Proxy Authentication */
1322 if( p_sys->proxy.psz_username || p_sys->proxy.psz_password )
1323 AuthReply( p_access, "Proxy-", &p_sys->proxy, &p_sys->proxy_auth );
1325 /* ICY meta data request */
1326 net_Printf( p_access, p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
1329 if( net_Printf( p_access, p_sys->fd, pvs, "\r\n" ) < 0 )
1331 msg_Err( p_access, "failed to send request" );
1332 Disconnect( p_access );
1333 return VLC_EGENERIC;
1337 if( ( psz = net_Gets( p_access, p_sys->fd, pvs ) ) == NULL )
1339 msg_Err( p_access, "failed to read answer" );
1342 if( !strncmp( psz, "HTTP/1.", 7 ) )
1344 p_sys->psz_protocol = "HTTP";
1345 p_sys->i_code = atoi( &psz[9] );
1347 else if( !strncmp( psz, "ICY", 3 ) )
1349 p_sys->psz_protocol = "ICY";
1350 p_sys->i_code = atoi( &psz[4] );
1351 p_sys->b_reconnect = true;
1355 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1359 msg_Dbg( p_access, "protocol '%s' answer code %d",
1360 p_sys->psz_protocol, p_sys->i_code );
1361 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1363 p_sys->b_seekable = false;
1365 if( p_sys->i_code != 206 && p_sys->i_code != 401 )
1367 p_sys->b_seekable = false;
1369 /* Authentication error - We'll have to display the dialog */
1370 if( p_sys->i_code == 401 )
1374 /* Other fatal error */
1375 else if( p_sys->i_code >= 400 )
1377 msg_Err( p_access, "error: %s", psz );
1385 char *psz = net_Gets( p_access, p_sys->fd, pvs );
1390 msg_Err( p_access, "failed to read answer" );
1394 if( !vlc_object_alive (p_access) || p_sys->b_error )
1400 /* msg_Dbg( p_input, "Line=%s", psz ); */
1407 if( ( p = strchr( psz, ':' ) ) == NULL )
1409 msg_Err( p_access, "malformed header line: %s", psz );
1414 while( *p == ' ' ) p++;
1416 if( !strcasecmp( psz, "Content-Length" ) )
1418 uint64_t i_size = i_tell + (p_sys->i_remaining = (uint64_t)atoll( p ));
1419 if(i_size > p_access->info.i_size) {
1420 p_sys->b_has_size = true;
1421 p_access->info.i_size = i_size;
1423 msg_Dbg( p_access, "this frame size=%"PRIu64, p_sys->i_remaining );
1425 else if( !strcasecmp( psz, "Content-Range" ) ) {
1426 uint64_t i_ntell = i_tell;
1427 uint64_t i_nend = (p_access->info.i_size > 0)?(p_access->info.i_size - 1):i_tell;
1428 uint64_t i_nsize = p_access->info.i_size;
1429 sscanf(p,"bytes %"SCNu64"-%"SCNu64"/%"SCNu64,&i_ntell,&i_nend,&i_nsize);
1430 if(i_nend > i_ntell ) {
1431 p_access->info.i_pos = i_ntell;
1432 p_sys->i_icy_offset = i_ntell;
1433 p_sys->i_remaining = i_nend+1-i_ntell;
1434 uint64_t i_size = (i_nsize > i_nend) ? i_nsize : (i_nend + 1);
1435 if(i_size > p_access->info.i_size) {
1436 p_sys->b_has_size = true;
1437 p_access->info.i_size = i_size;
1439 msg_Dbg( p_access, "stream size=%"PRIu64",pos=%"PRIu64",remaining=%"PRIu64,
1440 i_nsize, i_ntell, p_sys->i_remaining);
1443 else if( !strcasecmp( psz, "Connection" ) ) {
1444 msg_Dbg( p_access, "Connection: %s",p );
1446 sscanf(p, "close%n",&i);
1448 p_sys->b_persist = false;
1451 else if( !strcasecmp( psz, "Location" ) )
1455 /* This does not follow RFC 2068, but yet if the url is not absolute,
1456 * handle it as everyone does. */
1459 const char *psz_http_ext = p_sys->b_ssl ? "s" : "" ;
1461 if( p_sys->url.i_port == ( p_sys->b_ssl ? 443 : 80 ) )
1463 if( asprintf(&psz_new_loc, "http%s://%s%s", psz_http_ext,
1464 p_sys->url.psz_host, p) < 0 )
1469 if( asprintf(&psz_new_loc, "http%s://%s:%d%s", psz_http_ext,
1470 p_sys->url.psz_host, p_sys->url.i_port, p) < 0 )
1476 psz_new_loc = strdup( p );
1479 free( p_sys->psz_location );
1480 p_sys->psz_location = psz_new_loc;
1482 else if( !strcasecmp( psz, "Content-Type" ) )
1484 free( p_sys->psz_mime );
1485 p_sys->psz_mime = strdup( p );
1486 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1488 else if( !strcasecmp( psz, "Content-Encoding" ) )
1490 msg_Dbg( p_access, "Content-Encoding: %s", p );
1491 if( !strcasecmp( p, "identity" ) )
1494 else if( !strcasecmp( p, "gzip" ) || !strcasecmp( p, "deflate" ) )
1495 p_sys->b_compressed = true;
1498 msg_Warn( p_access, "Unknown content coding: %s", p );
1500 else if( !strcasecmp( psz, "Pragma" ) )
1502 if( !strcasecmp( psz, "Pragma: features" ) )
1503 p_sys->b_mms = true;
1504 free( p_sys->psz_pragma );
1505 p_sys->psz_pragma = strdup( p );
1506 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1508 else if( !strcasecmp( psz, "Server" ) )
1510 msg_Dbg( p_access, "Server: %s", p );
1511 if( !strncasecmp( p, "Icecast", 7 ) ||
1512 !strncasecmp( p, "Nanocaster", 10 ) )
1514 /* Remember if this is Icecast
1515 * we need to force demux in this case without breaking
1518 /* Let live 365 streams (nanocaster) piggyback on the icecast
1519 * routine. They look very similar */
1521 p_sys->b_reconnect = true;
1522 p_sys->b_pace_control = false;
1523 p_sys->b_icecast = true;
1526 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1528 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1529 if( !strncasecmp( p, "chunked", 7 ) )
1531 p_sys->b_chunked = true;
1534 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1536 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1537 p_sys->i_icy_meta = atoi( p );
1538 if( p_sys->i_icy_meta < 0 )
1539 p_sys->i_icy_meta = 0;
1540 if( p_sys->i_icy_meta > 0 )
1541 p_sys->b_icecast = true;
1543 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1545 else if( !strcasecmp( psz, "Icy-Name" ) )
1547 free( p_sys->psz_icy_name );
1548 char *psz_tmp = strdup( p );
1549 p_sys->psz_icy_name = EnsureUTF8( psz_tmp );
1550 if( !p_sys->psz_icy_name )
1552 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1554 p_sys->b_icecast = true; /* be on the safeside. set it here as well. */
1555 p_sys->b_reconnect = true;
1556 p_sys->b_pace_control = false;
1558 else if( !strcasecmp( psz, "Icy-Genre" ) )
1560 free( p_sys->psz_icy_genre );
1561 char *psz_tmp = strdup( p );
1562 p_sys->psz_icy_genre = EnsureUTF8( psz_tmp );
1563 if( !p_sys->psz_icy_genre )
1565 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1567 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1569 msg_Dbg( p_access, "Icy-Notice: %s", p );
1571 else if( !strncasecmp( psz, "icy-", 4 ) ||
1572 !strncasecmp( psz, "ice-", 4 ) ||
1573 !strncasecmp( psz, "x-audiocast", 11 ) )
1575 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1577 else if( !strcasecmp( psz, "Set-Cookie" ) )
1579 if( p_sys->cookies )
1581 msg_Dbg( p_access, "Accepting Cookie: %s", p );
1582 cookie_append( p_sys->cookies, strdup(p) );
1585 msg_Dbg( p_access, "We have a Cookie we won't remember: %s", p );
1587 else if( !strcasecmp( psz, "www-authenticate" ) )
1589 msg_Dbg( p_access, "Authentication header: %s", p );
1590 http_auth_ParseWwwAuthenticateHeader( VLC_OBJECT(p_access),
1593 else if( !strcasecmp( psz, "proxy-authenticate" ) )
1595 msg_Dbg( p_access, "Proxy authentication header: %s", p );
1596 http_auth_ParseWwwAuthenticateHeader( VLC_OBJECT(p_access),
1597 &p_sys->proxy_auth, p );
1599 else if( !strcasecmp( psz, "authentication-info" ) )
1601 msg_Dbg( p_access, "Authentication Info header: %s", p );
1602 if( AuthCheckReply( p_access, p, &p_sys->url, &p_sys->auth ) )
1605 else if( !strcasecmp( psz, "proxy-authentication-info" ) )
1607 msg_Dbg( p_access, "Proxy Authentication Info header: %s", p );
1608 if( AuthCheckReply( p_access, p, &p_sys->proxy, &p_sys->proxy_auth ) )
1611 else if( !strcasecmp( psz, "Accept-Ranges" ) )
1613 if( !strcasecmp( p, "bytes" ) )
1614 p_sys->b_seekable = true;
1619 /* We close the stream for zero length data, unless of course the
1620 * server has already promised to do this for us.
1622 if( p_sys->b_has_size && p_sys->i_remaining == 0 && p_sys->b_persist ) {
1623 Disconnect( p_access );
1628 Disconnect( p_access );
1629 return VLC_EGENERIC;
1632 /*****************************************************************************
1634 *****************************************************************************/
1635 static void Disconnect( access_t *p_access )
1637 access_sys_t *p_sys = p_access->p_sys;
1639 if( p_sys->p_tls != NULL)
1641 vlc_tls_ClientDelete( p_sys->p_tls );
1642 p_sys->p_tls = NULL;
1645 if( p_sys->fd != -1)
1647 net_Close(p_sys->fd);
1653 /*****************************************************************************
1654 * Cookies (FIXME: we may want to rewrite that using a nice structure to hold
1655 * them) (FIXME: only support the "domain=" param)
1656 *****************************************************************************/
1658 /* Get the NAME=VALUE part of the Cookie */
1659 static char * cookie_get_content( const char * cookie )
1661 char * ret = strdup( cookie );
1662 if( !ret ) return NULL;
1664 /* Look for a ';' */
1665 while( *str && *str != ';' ) str++;
1666 /* Replace it by a end-char */
1667 if( *str == ';' ) *str = 0;
1671 /* Get the domain where the cookie is stored */
1672 static char * cookie_get_domain( const char * cookie )
1674 const char * str = cookie;
1675 static const char domain[] = "domain=";
1678 /* Look for a ';' */
1681 if( !strncmp( str, domain, sizeof(domain) - 1 /* minus \0 */ ) )
1683 str += sizeof(domain) - 1 /* minus \0 */;
1684 char * ret = strdup( str );
1685 /* Now remove the next ';' if present */
1686 char * ret_iter = ret;
1687 while( *ret_iter && *ret_iter != ';' ) ret_iter++;
1688 if( *ret_iter == ';' )
1692 /* Go to next ';' field */
1693 while( *str && *str != ';' ) str++;
1694 if( *str == ';' ) str++;
1696 while( *str && *str == ' ' ) str++;
1701 /* Get NAME in the NAME=VALUE field */
1702 static char * cookie_get_name( const char * cookie )
1704 char * ret = cookie_get_content( cookie ); /* NAME=VALUE */
1705 if( !ret ) return NULL;
1707 while( *str && *str != '=' ) str++;
1712 /* Add a cookie in cookies, checking to see how it should be added */
1713 static void cookie_append( vlc_array_t * cookies, char * cookie )
1720 char * cookie_name = cookie_get_name( cookie );
1722 /* Don't send invalid cookies */
1726 char * cookie_domain = cookie_get_domain( cookie );
1727 for( i = 0; i < vlc_array_count( cookies ); i++ )
1729 char * current_cookie = vlc_array_item_at_index( cookies, i );
1730 char * current_cookie_name = cookie_get_name( current_cookie );
1731 char * current_cookie_domain = cookie_get_domain( current_cookie );
1733 assert( current_cookie_name );
1735 bool is_domain_matching = ( cookie_domain && current_cookie_domain &&
1736 !strcmp( cookie_domain, current_cookie_domain ) );
1738 if( is_domain_matching && !strcmp( cookie_name, current_cookie_name ) )
1740 /* Remove previous value for this cookie */
1741 free( current_cookie );
1742 vlc_array_remove( cookies, i );
1745 free( current_cookie_name );
1746 free( current_cookie_domain );
1749 free( current_cookie_name );
1750 free( current_cookie_domain );
1752 free( cookie_name );
1753 free( cookie_domain );
1754 vlc_array_append( cookies, cookie );
1758 /*****************************************************************************
1759 * HTTP authentication
1760 *****************************************************************************/
1762 static void AuthReply( access_t *p_access, const char *psz_prefix,
1763 vlc_url_t *p_url, http_auth_t *p_auth )
1765 access_sys_t *p_sys = p_access->p_sys;
1769 http_auth_FormatAuthorizationHeader( VLC_OBJECT(p_access), p_auth,
1770 "GET", p_url->psz_path,
1771 p_url->psz_username,
1772 p_url->psz_password );
1773 if ( psz_value == NULL )
1776 net_Printf( p_access, p_sys->fd, p_sys->p_vs,
1777 "%sAuthorization: %s\r\n", psz_prefix, psz_value );
1781 static int AuthCheckReply( access_t *p_access, const char *psz_header,
1782 vlc_url_t *p_url, http_auth_t *p_auth )
1785 http_auth_ParseAuthenticationInfoHeader( VLC_OBJECT(p_access), p_auth,
1788 p_url->psz_username,
1789 p_url->psz_password );