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_("The name and version of the program will be " \
101 "provided to the HTTP server. They must be separated by a forward " \
102 "slash, e.g. FooBar/1.2.3. This option can only be specified per input " \
103 "item, not globally.")
106 set_description( N_("HTTP input") )
107 set_capability( "access", 0 )
108 set_shortname( N_( "HTTP(S)" ) )
109 set_category( CAT_INPUT )
110 set_subcategory( SUBCAT_INPUT_ACCESS )
112 add_string( "http-proxy", NULL, PROXY_TEXT, PROXY_LONGTEXT,
114 add_password( "http-proxy-pwd", NULL,
115 PROXY_PASS_TEXT, PROXY_PASS_LONGTEXT, false )
116 add_obsolete_bool( "http-use-IE-proxy" )
117 add_string( "http-referrer", NULL, REFERER_TEXT, REFERER_LONGTEXT, false )
119 add_string( "http-user-agent", NULL, UA_TEXT, UA_LONGTEXT, false )
122 add_bool( "http-reconnect", false, RECONNECT_TEXT,
123 RECONNECT_LONGTEXT, true )
124 add_bool( "http-continuous", false, CONTINUOUS_TEXT,
125 CONTINUOUS_LONGTEXT, true )
127 add_bool( "http-forward-cookies", true, FORWARD_COOKIES_TEXT,
128 FORWARD_COOKIES_LONGTEXT, true )
129 /* 'itpc' = iTunes Podcast */
130 add_shortcut( "http", "https", "unsv", "itpc", "icyx" )
131 set_callbacks( Open, Close )
134 /*****************************************************************************
136 *****************************************************************************/
147 char *psz_user_agent;
154 http_auth_t proxy_auth;
155 char *psz_proxy_passbuf;
159 const char *psz_protocol;
181 uint64_t i_icy_offset;
186 uint64_t i_remaining;
195 vlc_array_t * cookies;
199 static int OpenWithCookies( vlc_object_t *p_this, const char *psz_access,
200 unsigned i_redirect, vlc_array_t *cookies );
203 static ssize_t Read( access_t *, uint8_t *, size_t );
204 static ssize_t ReadCompressed( access_t *, uint8_t *, size_t );
205 static int Seek( access_t *, uint64_t );
206 static int Control( access_t *, int, va_list );
209 static int Connect( access_t *, uint64_t );
210 static int Request( access_t *p_access, uint64_t i_tell );
211 static void Disconnect( access_t * );
213 /* Small Cookie utilities. Cookies support is partial. */
214 static char * cookie_get_content( const char * cookie );
215 static char * cookie_get_domain( const char * cookie );
216 static char * cookie_get_name( const char * cookie );
217 static void cookie_append( vlc_array_t * cookies, char * cookie );
220 static void AuthReply( access_t *p_acces, const char *psz_prefix,
221 vlc_url_t *p_url, http_auth_t *p_auth );
222 static int AuthCheckReply( access_t *p_access, const char *psz_header,
223 vlc_url_t *p_url, http_auth_t *p_auth );
225 /*****************************************************************************
227 *****************************************************************************/
228 static int Open( vlc_object_t *p_this )
230 access_t *p_access = (access_t*)p_this;
231 return OpenWithCookies( p_this, p_access->psz_access, 5, NULL );
235 * Open the given url using the given cookies
236 * @param p_this: the vlc object
237 * @psz_access: the acces to use (http, https, ...) (this value must be used
238 * instead of p_access->psz_access)
239 * @i_redirect: number of redirections remaining
240 * @cookies: the available cookies
241 * @return vlc error codes
243 static int OpenWithCookies( vlc_object_t *p_this, const char *psz_access,
244 unsigned i_redirect, vlc_array_t *cookies )
246 access_t *p_access = (access_t*)p_this;
250 /* Only forward an store cookies if the corresponding option is activated */
251 bool b_forward_cookies = var_InheritBool( p_access, "http-forward-cookies" );
252 vlc_array_t * saved_cookies = b_forward_cookies ? (cookies ? cookies : vlc_array_new()) : NULL;
254 /* Set up p_access */
255 STANDARD_READ_ACCESS_INIT;
257 p_access->pf_read = ReadCompressed;
260 p_sys->b_proxy = false;
261 p_sys->psz_proxy_passbuf = NULL;
262 p_sys->i_version = 1;
263 p_sys->b_seekable = true;
264 p_sys->psz_mime = NULL;
265 p_sys->psz_pragma = NULL;
266 p_sys->b_mms = false;
267 p_sys->b_icecast = false;
268 p_sys->psz_location = NULL;
269 p_sys->psz_user_agent = NULL;
270 p_sys->psz_referrer = NULL;
271 p_sys->b_pace_control = true;
272 p_sys->b_ssl = false;
274 p_sys->b_compressed = false;
275 /* 15 is the max windowBits, +32 to enable optional gzip decoding */
276 if( inflateInit2( &p_sys->inflate.stream, 32+15 ) != Z_OK )
277 msg_Warn( p_access, "Error during zlib initialisation: %s",
278 p_sys->inflate.stream.msg );
279 if( zlibCompileFlags() & (1<<17) )
280 msg_Warn( p_access, "Your zlib was compiled without gzip support." );
281 p_sys->inflate.p_buffer = NULL;
285 p_sys->i_icy_meta = 0;
286 p_sys->i_icy_offset = 0;
287 p_sys->psz_icy_name = NULL;
288 p_sys->psz_icy_genre = NULL;
289 p_sys->psz_icy_title = NULL;
290 p_sys->i_remaining = 0;
291 p_sys->b_persist = false;
292 p_sys->b_has_size = false;
293 p_access->info.i_size = 0;
294 p_access->info.i_pos = 0;
295 p_access->info.b_eof = false;
297 p_sys->cookies = saved_cookies;
299 http_auth_Init( &p_sys->auth );
300 http_auth_Init( &p_sys->proxy_auth );
302 /* Parse URI - remove spaces */
303 p = psz = strdup( p_access->psz_location );
304 while( (p = strchr( p, ' ' )) != NULL )
306 vlc_UrlParse( &p_sys->url, psz, 0 );
309 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
311 msg_Warn( p_access, "invalid host" );
314 if( !strncmp( psz_access, "https", 5 ) )
318 if( p_sys->url.i_port <= 0 )
319 p_sys->url.i_port = 443;
323 if( p_sys->url.i_port <= 0 )
324 p_sys->url.i_port = 80;
327 /* Determine the HTTP user agent */
328 /* See RFC2616 §2.2 token and comment definition, and §3.8 and
329 * §14.43 user-agent header */
330 p_sys->psz_user_agent = var_InheritString( p_access, "http-user-agent" );
331 if (p_sys->psz_user_agent)
333 unsigned comment_level = 0;
334 for( char *p = p_sys->psz_user_agent; *p; p++ )
337 if (comment_level == 0)
339 if( c < 32 || strchr( ")<>@,;:\\\"[]?={}", c ) )
340 *p = '_'; /* remove potentially harmful characters */
346 else if( c < 32 && strchr( "\t\r\n", c ) == NULL)
347 *p = '_'; /* remove potentially harmful characters */
351 if (comment_level == UINT_MAX)
356 /* truncate evil unclosed comments */
357 if (comment_level > 0)
359 char *p = strchr(p_sys->psz_user_agent, '(');
365 p_sys->psz_referrer = var_InheritString( p_access, "http-referrer" );
368 psz = var_InheritString( p_access, "http-proxy" );
371 p_sys->b_proxy = true;
372 vlc_UrlParse( &p_sys->proxy, psz, 0 );
378 pxProxyFactory *pf = px_proxy_factory_new();
383 i=asprintf(&buf, "%s://%s", psz_access, p_access->psz_location);
386 msg_Dbg(p_access, "asking libproxy about url '%s'", buf);
387 char **proxies = px_proxy_factory_get_proxies(pf, buf);
390 msg_Dbg(p_access, "libproxy suggest to use '%s'", proxies[0]);
391 if(strcmp(proxies[0],"direct://") != 0)
393 p_sys->b_proxy = true;
394 vlc_UrlParse( &p_sys->proxy, proxies[0], 0);
397 for(i=0;proxies[i];i++) free(proxies[i]);
401 px_proxy_factory_free(pf);
405 msg_Err(p_access, "Allocating memory for libproxy failed");
408 #elif (0) // defined( WIN32 ) The parsing is not complete enough
411 /* Try to get the proxy server address from Windows internet settings using registry. */
414 if( RegOpenKeyEx( HKEY_CURRENT_USER, "Software\\Microsoft"
415 "\\Windows\\CurrentVersion\\Internet Settings",
416 0, KEY_READ, &h_key ) == ERROR_SUCCESS )
418 DWORD len = sizeof( DWORD );
421 /* Get the proxy enable value */
422 if( RegQueryValueEx( h_key, "ProxyEnable", NULL, NULL,
423 &proxyEnable, &len ) == ERROR_SUCCESS
426 /* Proxy is enabled */
427 /* Get the proxy URL :
428 Proxy server value in the registry can be something like "address:port"
429 or "ftp=address1:port1;http=address2:port2 ..." depending of the
431 unsigned char key[256];
434 if( RegQueryValueEx( h_key, "ProxyServer", NULL, NULL,
435 key, &len ) == ERROR_SUCCESS )
437 /* FIXME: This is lame. The string should be tokenized. */
439 char *psz_proxy = strstr( (char *)key, "http=" );
440 if( psz_proxy != NULL )
443 char *end = strchr( psz_proxy, ';' );
448 psz_proxy = (char *)key;
449 /* Set proxy enable for this connection. */
450 p_sys->b_proxy = true;
451 vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
455 msg_Dbg( p_access, "HTTP proxy disabled (MSIE)" );
456 RegCloseKey( h_key );
462 psz = getenv( "http_proxy" );
465 p_sys->b_proxy = true;
466 vlc_UrlParse( &p_sys->proxy, psz, 0 );
471 if( psz ) /* No, this is NOT a use-after-free error */
473 psz = var_InheritString( p_access, "http-proxy-pwd" );
475 p_sys->proxy.psz_password = p_sys->psz_proxy_passbuf = psz;
480 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
482 msg_Warn( p_access, "invalid proxy host" );
485 if( p_sys->proxy.i_port <= 0 )
487 p_sys->proxy.i_port = 80;
491 msg_Dbg( p_access, "http: server='%s' port=%d file='%s'",
492 p_sys->url.psz_host, p_sys->url.i_port,
493 p_sys->url.psz_path != NULL ? p_sys->url.psz_path : "" );
496 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
497 p_sys->proxy.i_port );
499 if( p_sys->url.psz_username && *p_sys->url.psz_username )
501 msg_Dbg( p_access, " user='%s'", p_sys->url.psz_username );
504 p_sys->b_reconnect = var_InheritBool( p_access, "http-reconnect" );
505 p_sys->b_continuous = var_InheritBool( p_access, "http-continuous" );
509 switch( Connect( p_access, 0 ) )
515 /* Retry with http 1.0 */
516 msg_Dbg( p_access, "switching to HTTP version 1.0" );
517 p_sys->i_version = 0;
518 p_sys->b_seekable = false;
520 if( !vlc_object_alive (p_access) || Connect( p_access, 0 ) )
530 if( p_sys->i_code == 401 )
532 char *psz_login, *psz_password;
534 if( p_sys->url.psz_username && p_sys->url.psz_password &&
535 p_sys->auth.psz_nonce && p_sys->auth.i_nonce == 0 )
537 Disconnect( p_access );
540 msg_Dbg( p_access, "authentication failed for realm %s",
541 p_sys->auth.psz_realm );
542 dialog_Login( p_access, &psz_login, &psz_password,
543 _("HTTP authentication"),
544 _("Please enter a valid login name and a password for realm %s."),
545 p_sys->auth.psz_realm );
546 if( psz_login != NULL && psz_password != NULL )
548 msg_Dbg( p_access, "retrying with user=%s", psz_login );
549 p_sys->url.psz_username = psz_login;
550 p_sys->url.psz_password = psz_password;
551 Disconnect( p_access );
557 free( psz_password );
562 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
563 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
564 p_sys->psz_location && *p_sys->psz_location )
566 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
568 /* Check the number of redirection already done */
569 if( i_redirect == 0 )
571 msg_Err( p_access, "Too many redirection: break potential infinite"
577 /* Do not accept redirection outside of HTTP works */
578 const char *psz_protocol;
579 if( !strncmp( p_sys->psz_location, "http:", 5 ) )
580 psz_protocol = "http";
581 else if( !strncmp( p_sys->psz_location, "https:", 6 ) )
582 psz_protocol = "https";
585 msg_Err( p_access, "insecure redirection ignored" );
588 free( p_access->psz_location );
589 p_access->psz_location = strdup( p_sys->psz_location );
590 /* Clean up current Open() run */
591 vlc_UrlClean( &p_sys->url );
592 http_auth_Reset( &p_sys->auth );
593 vlc_UrlClean( &p_sys->proxy );
594 free( p_sys->psz_proxy_passbuf );
595 http_auth_Reset( &p_sys->proxy_auth );
596 free( p_sys->psz_mime );
597 free( p_sys->psz_pragma );
598 free( p_sys->psz_location );
599 free( p_sys->psz_user_agent );
600 free( p_sys->psz_referrer );
602 Disconnect( p_access );
603 cookies = p_sys->cookies;
605 inflateEnd( &p_sys->inflate.stream );
609 /* Do new Open() run with new data */
610 return OpenWithCookies( p_this, psz_protocol, i_redirect - 1,
616 msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
620 if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
622 if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
624 if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
625 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
627 free( p_access->psz_demux );
628 p_access->psz_demux = strdup( "nsv" );
630 else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
631 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
633 free( p_access->psz_demux );
634 p_access->psz_demux = strdup( "m4a" );
636 else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
638 free( p_access->psz_demux );
639 p_access->psz_demux = strdup( "mp3" );
642 msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
643 p_access->psz_demux );
645 #if 0 /* Doesn't work really well because of the pre-buffering in
646 * shoutcast servers (the buffer content will be sent as fast as
648 p_sys->b_pace_control = false;
651 else if( !p_sys->psz_mime )
653 free( p_access->psz_demux );
655 p_access->psz_demux = strdup( "mp3" );
657 /* else probably Ogg Vorbis */
659 else if( !strcasecmp( psz_access, "unsv" ) &&
661 !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
663 free( p_access->psz_demux );
664 /* Grrrr! detect ultravox server and force NSV demuxer */
665 p_access->psz_demux = strdup( "nsv" );
667 else if( !strcmp( psz_access, "itpc" ) )
669 free( p_access->psz_demux );
670 p_access->psz_demux = strdup( "podcast" );
672 else if( p_sys->psz_mime &&
673 !strncasecmp( p_sys->psz_mime, "application/xspf+xml", 20 ) &&
674 ( memchr( " ;\t", p_sys->psz_mime[20], 4 ) != NULL ) )
676 free( p_access->psz_demux );
677 p_access->psz_demux = strdup( "xspf-open" );
680 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
685 vlc_UrlClean( &p_sys->url );
686 vlc_UrlClean( &p_sys->proxy );
687 free( p_sys->psz_proxy_passbuf );
688 free( p_sys->psz_mime );
689 free( p_sys->psz_pragma );
690 free( p_sys->psz_location );
691 free( p_sys->psz_user_agent );
692 free( p_sys->psz_referrer );
694 Disconnect( p_access );
699 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
700 free(vlc_array_item_at_index( p_sys->cookies, i ));
701 vlc_array_destroy( p_sys->cookies );
705 inflateEnd( &p_sys->inflate.stream );
711 /*****************************************************************************
713 *****************************************************************************/
714 static void Close( vlc_object_t *p_this )
716 access_t *p_access = (access_t*)p_this;
717 access_sys_t *p_sys = p_access->p_sys;
719 vlc_UrlClean( &p_sys->url );
720 http_auth_Reset( &p_sys->auth );
721 vlc_UrlClean( &p_sys->proxy );
722 http_auth_Reset( &p_sys->proxy_auth );
724 free( p_sys->psz_mime );
725 free( p_sys->psz_pragma );
726 free( p_sys->psz_location );
728 free( p_sys->psz_icy_name );
729 free( p_sys->psz_icy_genre );
730 free( p_sys->psz_icy_title );
732 free( p_sys->psz_user_agent );
733 free( p_sys->psz_referrer );
735 Disconnect( p_access );
740 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
741 free(vlc_array_item_at_index( p_sys->cookies, i ));
742 vlc_array_destroy( p_sys->cookies );
746 inflateEnd( &p_sys->inflate.stream );
747 free( p_sys->inflate.p_buffer );
753 /* Read data from the socket taking care of chunked transfer if needed */
754 static int ReadData( access_t *p_access, int *pi_read,
755 uint8_t *p_buffer, size_t i_len )
757 access_sys_t *p_sys = p_access->p_sys;
758 if( p_sys->b_chunked )
760 if( p_sys->i_chunk < 0 )
763 if( p_sys->i_chunk <= 0 )
765 char *psz = net_Gets( p_access, p_sys->fd, p_sys->p_vs );
766 /* read the chunk header */
769 /* fatal error - end of file */
770 msg_Dbg( p_access, "failed reading chunk-header line" );
773 p_sys->i_chunk = strtoll( psz, NULL, 16 );
776 if( p_sys->i_chunk <= 0 ) /* eof */
783 if( i_len > p_sys->i_chunk )
784 i_len = p_sys->i_chunk;
786 *pi_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, false );
790 if( p_sys->b_chunked )
792 p_sys->i_chunk -= *pi_read;
793 if( p_sys->i_chunk <= 0 )
795 /* read the empty line */
796 char *psz = net_Gets( p_access, p_sys->fd, p_sys->p_vs );
803 /*****************************************************************************
804 * Read: Read up to i_len bytes from the http connection and place in
805 * p_buffer. Return the actual number of bytes read
806 *****************************************************************************/
807 static int ReadICYMeta( access_t *p_access );
808 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
810 access_sys_t *p_sys = p_access->p_sys;
813 if( p_sys->fd == -1 )
816 if( p_sys->b_has_size )
818 /* Remaining bytes in the file */
819 uint64_t remainder = p_access->info.i_size - p_access->info.i_pos;
820 if( remainder < i_len )
823 /* Remaining bytes in the response */
824 if( p_sys->i_remaining < i_len )
825 i_len = p_sys->i_remaining;
830 if( p_sys->i_icy_meta > 0 && p_access->info.i_pos - p_sys->i_icy_offset > 0 )
832 int64_t i_next = p_sys->i_icy_meta -
833 (p_access->info.i_pos - p_sys->i_icy_offset ) % p_sys->i_icy_meta;
835 if( i_next == p_sys->i_icy_meta )
837 if( ReadICYMeta( p_access ) )
844 if( ReadData( p_access, &i_read, p_buffer, i_len ) )
850 * I very much doubt that this will work.
851 * If i_read == 0, the connection *IS* dead, so the only
852 * sensible thing to do is Disconnect() and then retry.
853 * Otherwise, I got recv() completely wrong. -- Courmisch
855 if( p_sys->b_continuous )
857 Request( p_access, 0 );
858 p_sys->b_continuous = false;
859 i_read = Read( p_access, p_buffer, i_len );
860 p_sys->b_continuous = true;
862 Disconnect( p_access );
863 if( p_sys->b_reconnect && vlc_object_alive( p_access ) )
865 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
866 if( Connect( p_access, p_access->info.i_pos ) )
868 msg_Dbg( p_access, "reconnection failed" );
872 p_sys->b_reconnect = false;
873 i_read = Read( p_access, p_buffer, i_len );
874 p_sys->b_reconnect = true;
883 p_sys->b_error = true;
888 assert( i_read >= 0 );
889 p_access->info.i_pos += i_read;
890 if( p_sys->b_has_size )
892 assert( p_access->info.i_pos <= p_access->info.i_size );
893 assert( (unsigned)i_read <= p_sys->i_remaining );
894 p_sys->i_remaining -= i_read;
900 p_access->info.b_eof = true;
904 static int ReadICYMeta( access_t *p_access )
906 access_sys_t *p_sys = p_access->p_sys;
912 /* Read meta data length */
913 if( ReadData( p_access, &i_read, &buffer, 1 ) )
917 const int i_size = buffer << 4;
918 /* msg_Dbg( p_access, "ICY meta size=%u", i_size); */
920 psz_meta = malloc( i_size + 1 );
921 for( i_read = 0; i_read < i_size; )
924 if( ReadData( p_access, &i_tmp, (uint8_t *)&psz_meta[i_read], i_size - i_read ) || i_tmp <= 0 )
931 psz_meta[i_read] = '\0'; /* Just in case */
933 /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
935 /* Now parse the meta */
936 /* Look for StreamTitle= */
937 p = strcasestr( (char *)psz_meta, "StreamTitle=" );
940 p += strlen( "StreamTitle=" );
941 if( *p == '\'' || *p == '"' )
943 char closing[] = { p[0], ';', '\0' };
944 char *psz = strstr( &p[1], closing );
946 psz = strchr( &p[1], ';' );
948 if( psz ) *psz = '\0';
952 char *psz = strchr( &p[1], ';' );
953 if( psz ) *psz = '\0';
956 if( !p_sys->psz_icy_title ||
957 strcmp( p_sys->psz_icy_title, &p[1] ) )
959 free( p_sys->psz_icy_title );
960 char *psz_tmp = strdup( &p[1] );
961 p_sys->psz_icy_title = EnsureUTF8( psz_tmp );
962 if( !p_sys->psz_icy_title )
964 p_access->info.i_update |= INPUT_UPDATE_META;
966 msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
975 static ssize_t ReadCompressed( access_t *p_access, uint8_t *p_buffer,
978 access_sys_t *p_sys = p_access->p_sys;
980 if( p_sys->b_compressed )
984 if( !p_sys->inflate.p_buffer )
985 p_sys->inflate.p_buffer = malloc( 256 * 1024 );
987 if( p_sys->inflate.stream.avail_in == 0 )
989 ssize_t i_read = Read( p_access, p_sys->inflate.p_buffer, 256 * 1024 );
990 if( i_read <= 0 ) return i_read;
991 p_sys->inflate.stream.next_in = p_sys->inflate.p_buffer;
992 p_sys->inflate.stream.avail_in = i_read;
995 p_sys->inflate.stream.avail_out = i_len;
996 p_sys->inflate.stream.next_out = p_buffer;
998 i_ret = inflate( &p_sys->inflate.stream, Z_SYNC_FLUSH );
999 msg_Warn( p_access, "inflate return value: %d, %s", i_ret, p_sys->inflate.stream.msg );
1001 return i_len - p_sys->inflate.stream.avail_out;
1005 return Read( p_access, p_buffer, i_len );
1010 /*****************************************************************************
1011 * Seek: close and re-open a connection at the right place
1012 *****************************************************************************/
1013 static int Seek( access_t *p_access, uint64_t i_pos )
1015 msg_Dbg( p_access, "trying to seek to %"PRId64, i_pos );
1017 Disconnect( p_access );
1019 if( p_access->info.i_size
1020 && i_pos >= p_access->info.i_size ) {
1021 msg_Err( p_access, "seek to far" );
1022 int retval = Seek( p_access, p_access->info.i_size - 1 );
1023 if( retval == VLC_SUCCESS ) {
1024 uint8_t p_buffer[2];
1025 Read( p_access, p_buffer, 1);
1026 p_access->info.b_eof = false;
1030 if( Connect( p_access, i_pos ) )
1032 msg_Err( p_access, "seek failed" );
1033 p_access->info.b_eof = true;
1034 return VLC_EGENERIC;
1039 /*****************************************************************************
1041 *****************************************************************************/
1042 static int Control( access_t *p_access, int i_query, va_list args )
1044 access_sys_t *p_sys = p_access->p_sys;
1052 case ACCESS_CAN_SEEK:
1053 pb_bool = (bool*)va_arg( args, bool* );
1054 *pb_bool = p_sys->b_seekable;
1056 case ACCESS_CAN_FASTSEEK:
1057 pb_bool = (bool*)va_arg( args, bool* );
1060 case ACCESS_CAN_PAUSE:
1061 case ACCESS_CAN_CONTROL_PACE:
1062 pb_bool = (bool*)va_arg( args, bool* );
1064 #if 0 /* Disable for now until we have a clock synchro algo
1065 * which works with something else than MPEG over UDP */
1066 *pb_bool = p_sys->b_pace_control;
1072 case ACCESS_GET_PTS_DELAY:
1073 pi_64 = (int64_t*)va_arg( args, int64_t * );
1074 *pi_64 = INT64_C(1000)
1075 * var_InheritInteger( p_access, "network-caching" );
1079 case ACCESS_SET_PAUSE_STATE:
1082 case ACCESS_GET_META:
1083 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
1085 if( p_sys->psz_icy_name )
1086 vlc_meta_Set( p_meta, vlc_meta_Title, p_sys->psz_icy_name );
1087 if( p_sys->psz_icy_genre )
1088 vlc_meta_Set( p_meta, vlc_meta_Genre, p_sys->psz_icy_genre );
1089 if( p_sys->psz_icy_title )
1090 vlc_meta_Set( p_meta, vlc_meta_NowPlaying, p_sys->psz_icy_title );
1093 case ACCESS_GET_CONTENT_TYPE:
1094 *va_arg( args, char ** ) =
1095 p_sys->psz_mime ? strdup( p_sys->psz_mime ) : NULL;
1098 case ACCESS_GET_TITLE_INFO:
1099 case ACCESS_SET_TITLE:
1100 case ACCESS_SET_SEEKPOINT:
1101 case ACCESS_SET_PRIVATE_ID_STATE:
1102 return VLC_EGENERIC;
1105 msg_Warn( p_access, "unimplemented query in control" );
1106 return VLC_EGENERIC;
1112 /*****************************************************************************
1114 *****************************************************************************/
1115 static int Connect( access_t *p_access, uint64_t i_tell )
1117 access_sys_t *p_sys = p_access->p_sys;
1118 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
1121 free( p_sys->psz_location );
1122 free( p_sys->psz_mime );
1123 free( p_sys->psz_pragma );
1125 free( p_sys->psz_icy_genre );
1126 free( p_sys->psz_icy_name );
1127 free( p_sys->psz_icy_title );
1130 p_sys->psz_location = NULL;
1131 p_sys->psz_mime = NULL;
1132 p_sys->psz_pragma = NULL;
1133 p_sys->b_mms = false;
1134 p_sys->b_chunked = false;
1136 p_sys->i_icy_meta = 0;
1137 p_sys->i_icy_offset = i_tell;
1138 p_sys->psz_icy_name = NULL;
1139 p_sys->psz_icy_genre = NULL;
1140 p_sys->psz_icy_title = NULL;
1141 p_sys->i_remaining = 0;
1142 p_sys->b_persist = false;
1143 p_sys->b_has_size = false;
1144 p_access->info.i_size = 0;
1145 p_access->info.i_pos = i_tell;
1146 p_access->info.b_eof = false;
1148 /* Open connection */
1149 assert( p_sys->fd == -1 ); /* No open sockets (leaking fds is BAD) */
1150 p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
1151 if( p_sys->fd == -1 )
1153 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
1156 setsockopt (p_sys->fd, SOL_SOCKET, SO_KEEPALIVE, &(int){ 1 }, sizeof (int));
1158 /* Initialize TLS/SSL session */
1161 /* CONNECT to establish TLS tunnel through HTTP proxy */
1162 if( p_sys->b_proxy )
1165 unsigned i_status = 0;
1167 if( p_sys->i_version == 0 )
1169 /* CONNECT is not in HTTP/1.0 */
1170 Disconnect( p_access );
1174 net_Printf( p_access, p_sys->fd, NULL,
1175 "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
1176 p_sys->url.psz_host, p_sys->url.i_port,
1178 p_sys->url.psz_host, p_sys->url.i_port);
1180 psz = net_Gets( p_access, p_sys->fd, NULL );
1183 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
1184 Disconnect( p_access );
1188 sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
1191 if( ( i_status / 100 ) != 2 )
1193 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
1194 Disconnect( p_access );
1200 psz = net_Gets( p_access, p_sys->fd, NULL );
1203 msg_Err( p_access, "HTTP proxy connection failed" );
1204 Disconnect( p_access );
1213 if( !vlc_object_alive (p_access) || p_sys->b_error )
1215 Disconnect( p_access );
1222 /* TLS/SSL handshake */
1223 p_sys->p_tls = vlc_tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
1224 p_sys->url.psz_host );
1225 if( p_sys->p_tls == NULL )
1227 msg_Err( p_access, "cannot establish HTTP/TLS session" );
1228 Disconnect( p_access );
1231 p_sys->p_vs = &p_sys->p_tls->sock;
1234 return Request( p_access, i_tell ) ? -2 : 0;
1238 static int Request( access_t *p_access, uint64_t i_tell )
1240 access_sys_t *p_sys = p_access->p_sys;
1242 v_socket_t *pvs = p_sys->p_vs;
1243 p_sys->b_persist = false;
1245 p_sys->i_remaining = 0;
1247 const char *psz_path = p_sys->url.psz_path;
1248 if( !psz_path || !*psz_path )
1250 if( p_sys->b_proxy && pvs == NULL )
1251 net_Printf( p_access, p_sys->fd, NULL,
1252 "GET http://%s:%d HTTP/1.%d\r\n",
1253 p_sys->url.psz_host, p_sys->url.i_port, p_sys->i_version );
1255 net_Printf( p_access, p_sys->fd, pvs, "GET %s HTTP/1.%d\r\n",
1256 psz_path, p_sys->i_version );
1257 if( p_sys->url.i_port != (pvs ? 443 : 80) )
1258 net_Printf( p_access, p_sys->fd, pvs, "Host: %s:%d\r\n",
1259 p_sys->url.psz_host, p_sys->url.i_port );
1261 net_Printf( p_access, p_sys->fd, pvs, "Host: %s\r\n",
1262 p_sys->url.psz_host );
1264 net_Printf( p_access, p_sys->fd, pvs, "User-Agent: %s\r\n",
1265 p_sys->psz_user_agent );
1267 if (p_sys->psz_referrer)
1269 net_Printf( p_access, p_sys->fd, pvs, "Referer: %s\r\n",
1270 p_sys->psz_referrer);
1273 if( p_sys->i_version == 1 && ! p_sys->b_continuous )
1275 p_sys->b_persist = true;
1276 net_Printf( p_access, p_sys->fd, pvs,
1277 "Range: bytes=%"PRIu64"-\r\n", i_tell );
1278 net_Printf( p_access, p_sys->fd, pvs, "Connection: close\r\n" );
1282 if( p_sys->cookies )
1285 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
1287 const char * cookie = vlc_array_item_at_index( p_sys->cookies, i );
1288 char * psz_cookie_content = cookie_get_content( cookie );
1289 char * psz_cookie_domain = cookie_get_domain( cookie );
1291 assert( psz_cookie_content );
1293 /* FIXME: This is clearly not conforming to the rfc */
1294 bool is_in_right_domain = (!psz_cookie_domain || strstr( p_sys->url.psz_host, psz_cookie_domain ));
1296 if( is_in_right_domain )
1298 msg_Dbg( p_access, "Sending Cookie %s", psz_cookie_content );
1299 if( net_Printf( p_access, p_sys->fd, pvs, "Cookie: %s\r\n", psz_cookie_content ) < 0 )
1300 msg_Err( p_access, "failed to send Cookie" );
1302 free( psz_cookie_content );
1303 free( psz_cookie_domain );
1307 /* Authentication */
1308 if( p_sys->url.psz_username || p_sys->url.psz_password )
1309 AuthReply( p_access, "", &p_sys->url, &p_sys->auth );
1311 /* Proxy Authentication */
1312 if( p_sys->proxy.psz_username || p_sys->proxy.psz_password )
1313 AuthReply( p_access, "Proxy-", &p_sys->proxy, &p_sys->proxy_auth );
1315 /* ICY meta data request */
1316 net_Printf( p_access, p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
1319 if( net_Printf( p_access, p_sys->fd, pvs, "\r\n" ) < 0 )
1321 msg_Err( p_access, "failed to send request" );
1322 Disconnect( p_access );
1323 return VLC_EGENERIC;
1327 if( ( psz = net_Gets( p_access, p_sys->fd, pvs ) ) == NULL )
1329 msg_Err( p_access, "failed to read answer" );
1332 if( !strncmp( psz, "HTTP/1.", 7 ) )
1334 p_sys->psz_protocol = "HTTP";
1335 p_sys->i_code = atoi( &psz[9] );
1337 else if( !strncmp( psz, "ICY", 3 ) )
1339 p_sys->psz_protocol = "ICY";
1340 p_sys->i_code = atoi( &psz[4] );
1341 p_sys->b_reconnect = true;
1345 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1349 msg_Dbg( p_access, "protocol '%s' answer code %d",
1350 p_sys->psz_protocol, p_sys->i_code );
1351 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1353 p_sys->b_seekable = false;
1355 if( p_sys->i_code != 206 && p_sys->i_code != 401 )
1357 p_sys->b_seekable = false;
1359 /* Authentication error - We'll have to display the dialog */
1360 if( p_sys->i_code == 401 )
1364 /* Other fatal error */
1365 else if( p_sys->i_code >= 400 )
1367 msg_Err( p_access, "error: %s", psz );
1375 char *psz = net_Gets( p_access, p_sys->fd, pvs );
1380 msg_Err( p_access, "failed to read answer" );
1384 if( !vlc_object_alive (p_access) || p_sys->b_error )
1390 /* msg_Dbg( p_input, "Line=%s", psz ); */
1397 if( ( p = strchr( psz, ':' ) ) == NULL )
1399 msg_Err( p_access, "malformed header line: %s", psz );
1404 while( *p == ' ' ) p++;
1406 if( !strcasecmp( psz, "Content-Length" ) )
1408 uint64_t i_size = i_tell + (p_sys->i_remaining = (uint64_t)atoll( p ));
1409 if(i_size > p_access->info.i_size) {
1410 p_sys->b_has_size = true;
1411 p_access->info.i_size = i_size;
1413 msg_Dbg( p_access, "this frame size=%"PRIu64, p_sys->i_remaining );
1415 else if( !strcasecmp( psz, "Content-Range" ) ) {
1416 uint64_t i_ntell = i_tell;
1417 uint64_t i_nend = (p_access->info.i_size > 0)?(p_access->info.i_size - 1):i_tell;
1418 uint64_t i_nsize = p_access->info.i_size;
1419 sscanf(p,"bytes %"SCNu64"-%"SCNu64"/%"SCNu64,&i_ntell,&i_nend,&i_nsize);
1420 if(i_nend > i_ntell ) {
1421 p_access->info.i_pos = i_ntell;
1422 p_sys->i_icy_offset = i_ntell;
1423 p_sys->i_remaining = i_nend+1-i_ntell;
1424 uint64_t i_size = (i_nsize > i_nend) ? i_nsize : (i_nend + 1);
1425 if(i_size > p_access->info.i_size) {
1426 p_sys->b_has_size = true;
1427 p_access->info.i_size = i_size;
1429 msg_Dbg( p_access, "stream size=%"PRIu64",pos=%"PRIu64",remaining=%"PRIu64,
1430 i_nsize, i_ntell, p_sys->i_remaining);
1433 else if( !strcasecmp( psz, "Connection" ) ) {
1434 msg_Dbg( p_access, "Connection: %s",p );
1436 sscanf(p, "close%n",&i);
1438 p_sys->b_persist = false;
1441 else if( !strcasecmp( psz, "Location" ) )
1445 /* This does not follow RFC 2068, but yet if the url is not absolute,
1446 * handle it as everyone does. */
1449 const char *psz_http_ext = p_sys->b_ssl ? "s" : "" ;
1451 if( p_sys->url.i_port == ( p_sys->b_ssl ? 443 : 80 ) )
1453 if( asprintf(&psz_new_loc, "http%s://%s%s", psz_http_ext,
1454 p_sys->url.psz_host, p) < 0 )
1459 if( asprintf(&psz_new_loc, "http%s://%s:%d%s", psz_http_ext,
1460 p_sys->url.psz_host, p_sys->url.i_port, p) < 0 )
1466 psz_new_loc = strdup( p );
1469 free( p_sys->psz_location );
1470 p_sys->psz_location = psz_new_loc;
1472 else if( !strcasecmp( psz, "Content-Type" ) )
1474 free( p_sys->psz_mime );
1475 p_sys->psz_mime = strdup( p );
1476 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1478 else if( !strcasecmp( psz, "Content-Encoding" ) )
1480 msg_Dbg( p_access, "Content-Encoding: %s", p );
1481 if( !strcasecmp( p, "identity" ) )
1484 else if( !strcasecmp( p, "gzip" ) || !strcasecmp( p, "deflate" ) )
1485 p_sys->b_compressed = true;
1488 msg_Warn( p_access, "Unknown content coding: %s", p );
1490 else if( !strcasecmp( psz, "Pragma" ) )
1492 if( !strcasecmp( psz, "Pragma: features" ) )
1493 p_sys->b_mms = true;
1494 free( p_sys->psz_pragma );
1495 p_sys->psz_pragma = strdup( p );
1496 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1498 else if( !strcasecmp( psz, "Server" ) )
1500 msg_Dbg( p_access, "Server: %s", p );
1501 if( !strncasecmp( p, "Icecast", 7 ) ||
1502 !strncasecmp( p, "Nanocaster", 10 ) )
1504 /* Remember if this is Icecast
1505 * we need to force demux in this case without breaking
1508 /* Let live 365 streams (nanocaster) piggyback on the icecast
1509 * routine. They look very similar */
1511 p_sys->b_reconnect = true;
1512 p_sys->b_pace_control = false;
1513 p_sys->b_icecast = true;
1516 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1518 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1519 if( !strncasecmp( p, "chunked", 7 ) )
1521 p_sys->b_chunked = true;
1524 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1526 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1527 p_sys->i_icy_meta = atoi( p );
1528 if( p_sys->i_icy_meta < 0 )
1529 p_sys->i_icy_meta = 0;
1530 if( p_sys->i_icy_meta > 0 )
1531 p_sys->b_icecast = true;
1533 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1535 else if( !strcasecmp( psz, "Icy-Name" ) )
1537 free( p_sys->psz_icy_name );
1538 char *psz_tmp = strdup( p );
1539 p_sys->psz_icy_name = EnsureUTF8( psz_tmp );
1540 if( !p_sys->psz_icy_name )
1542 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1544 p_sys->b_icecast = true; /* be on the safeside. set it here as well. */
1545 p_sys->b_reconnect = true;
1546 p_sys->b_pace_control = false;
1548 else if( !strcasecmp( psz, "Icy-Genre" ) )
1550 free( p_sys->psz_icy_genre );
1551 char *psz_tmp = strdup( p );
1552 p_sys->psz_icy_genre = EnsureUTF8( psz_tmp );
1553 if( !p_sys->psz_icy_genre )
1555 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1557 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1559 msg_Dbg( p_access, "Icy-Notice: %s", p );
1561 else if( !strncasecmp( psz, "icy-", 4 ) ||
1562 !strncasecmp( psz, "ice-", 4 ) ||
1563 !strncasecmp( psz, "x-audiocast", 11 ) )
1565 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1567 else if( !strcasecmp( psz, "Set-Cookie" ) )
1569 if( p_sys->cookies )
1571 msg_Dbg( p_access, "Accepting Cookie: %s", p );
1572 cookie_append( p_sys->cookies, strdup(p) );
1575 msg_Dbg( p_access, "We have a Cookie we won't remember: %s", p );
1577 else if( !strcasecmp( psz, "www-authenticate" ) )
1579 msg_Dbg( p_access, "Authentication header: %s", p );
1580 http_auth_ParseWwwAuthenticateHeader( VLC_OBJECT(p_access),
1583 else if( !strcasecmp( psz, "proxy-authenticate" ) )
1585 msg_Dbg( p_access, "Proxy authentication header: %s", p );
1586 http_auth_ParseWwwAuthenticateHeader( VLC_OBJECT(p_access),
1587 &p_sys->proxy_auth, p );
1589 else if( !strcasecmp( psz, "authentication-info" ) )
1591 msg_Dbg( p_access, "Authentication Info header: %s", p );
1592 if( AuthCheckReply( p_access, p, &p_sys->url, &p_sys->auth ) )
1595 else if( !strcasecmp( psz, "proxy-authentication-info" ) )
1597 msg_Dbg( p_access, "Proxy Authentication Info header: %s", p );
1598 if( AuthCheckReply( p_access, p, &p_sys->proxy, &p_sys->proxy_auth ) )
1601 else if( !strcasecmp( psz, "Accept-Ranges" ) )
1603 if( !strcasecmp( p, "bytes" ) )
1604 p_sys->b_seekable = true;
1609 /* We close the stream for zero length data, unless of course the
1610 * server has already promised to do this for us.
1612 if( p_sys->b_has_size && p_sys->i_remaining == 0 && p_sys->b_persist ) {
1613 Disconnect( p_access );
1618 Disconnect( p_access );
1619 return VLC_EGENERIC;
1622 /*****************************************************************************
1624 *****************************************************************************/
1625 static void Disconnect( access_t *p_access )
1627 access_sys_t *p_sys = p_access->p_sys;
1629 if( p_sys->p_tls != NULL)
1631 vlc_tls_ClientDelete( p_sys->p_tls );
1632 p_sys->p_tls = NULL;
1635 if( p_sys->fd != -1)
1637 net_Close(p_sys->fd);
1643 /*****************************************************************************
1644 * Cookies (FIXME: we may want to rewrite that using a nice structure to hold
1645 * them) (FIXME: only support the "domain=" param)
1646 *****************************************************************************/
1648 /* Get the NAME=VALUE part of the Cookie */
1649 static char * cookie_get_content( const char * cookie )
1651 char * ret = strdup( cookie );
1652 if( !ret ) return NULL;
1654 /* Look for a ';' */
1655 while( *str && *str != ';' ) str++;
1656 /* Replace it by a end-char */
1657 if( *str == ';' ) *str = 0;
1661 /* Get the domain where the cookie is stored */
1662 static char * cookie_get_domain( const char * cookie )
1664 const char * str = cookie;
1665 static const char domain[] = "domain=";
1668 /* Look for a ';' */
1671 if( !strncmp( str, domain, sizeof(domain) - 1 /* minus \0 */ ) )
1673 str += sizeof(domain) - 1 /* minus \0 */;
1674 char * ret = strdup( str );
1675 /* Now remove the next ';' if present */
1676 char * ret_iter = ret;
1677 while( *ret_iter && *ret_iter != ';' ) ret_iter++;
1678 if( *ret_iter == ';' )
1682 /* Go to next ';' field */
1683 while( *str && *str != ';' ) str++;
1684 if( *str == ';' ) str++;
1686 while( *str && *str == ' ' ) str++;
1691 /* Get NAME in the NAME=VALUE field */
1692 static char * cookie_get_name( const char * cookie )
1694 char * ret = cookie_get_content( cookie ); /* NAME=VALUE */
1695 if( !ret ) return NULL;
1697 while( *str && *str != '=' ) str++;
1702 /* Add a cookie in cookies, checking to see how it should be added */
1703 static void cookie_append( vlc_array_t * cookies, char * cookie )
1710 char * cookie_name = cookie_get_name( cookie );
1712 /* Don't send invalid cookies */
1716 char * cookie_domain = cookie_get_domain( cookie );
1717 for( i = 0; i < vlc_array_count( cookies ); i++ )
1719 char * current_cookie = vlc_array_item_at_index( cookies, i );
1720 char * current_cookie_name = cookie_get_name( current_cookie );
1721 char * current_cookie_domain = cookie_get_domain( current_cookie );
1723 assert( current_cookie_name );
1725 bool is_domain_matching = (
1726 ( !cookie_domain && !current_cookie_domain ) ||
1727 ( cookie_domain && current_cookie_domain &&
1728 !strcmp( cookie_domain, current_cookie_domain ) ) );
1730 if( is_domain_matching && !strcmp( cookie_name, current_cookie_name ) )
1732 /* Remove previous value for this cookie */
1733 free( current_cookie );
1734 vlc_array_remove( cookies, i );
1737 free( current_cookie_name );
1738 free( current_cookie_domain );
1741 free( current_cookie_name );
1742 free( current_cookie_domain );
1744 free( cookie_name );
1745 free( cookie_domain );
1746 vlc_array_append( cookies, cookie );
1750 /*****************************************************************************
1751 * HTTP authentication
1752 *****************************************************************************/
1754 static void AuthReply( access_t *p_access, const char *psz_prefix,
1755 vlc_url_t *p_url, http_auth_t *p_auth )
1757 access_sys_t *p_sys = p_access->p_sys;
1761 http_auth_FormatAuthorizationHeader( VLC_OBJECT(p_access), p_auth,
1762 "GET", p_url->psz_path,
1763 p_url->psz_username,
1764 p_url->psz_password );
1765 if ( psz_value == NULL )
1768 net_Printf( p_access, p_sys->fd, p_sys->p_vs,
1769 "%sAuthorization: %s\r\n", psz_prefix, psz_value );
1773 static int AuthCheckReply( access_t *p_access, const char *psz_header,
1774 vlc_url_t *p_url, http_auth_t *p_auth )
1777 http_auth_ParseAuthenticationInfoHeader( VLC_OBJECT(p_access), p_auth,
1780 p_url->psz_username,
1781 p_url->psz_password );