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)" );
452 RegCloseKey( h_key );
458 psz = getenv( "http_proxy" );
461 p_sys->b_proxy = true;
462 vlc_UrlParse( &p_sys->proxy, psz, 0 );
467 if( psz ) /* No, this is NOT a use-after-free error */
469 psz = var_InheritString( p_access, "http-proxy-pwd" );
471 p_sys->proxy.psz_password = p_sys->psz_proxy_passbuf = psz;
476 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
478 msg_Warn( p_access, "invalid proxy host" );
481 if( p_sys->proxy.i_port <= 0 )
483 p_sys->proxy.i_port = 80;
487 msg_Dbg( p_access, "http: server='%s' port=%d file='%s'",
488 p_sys->url.psz_host, p_sys->url.i_port,
489 p_sys->url.psz_path != NULL ? p_sys->url.psz_path : "" );
492 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
493 p_sys->proxy.i_port );
495 if( p_sys->url.psz_username && *p_sys->url.psz_username )
497 msg_Dbg( p_access, " user='%s'", p_sys->url.psz_username );
500 p_sys->b_reconnect = var_InheritBool( p_access, "http-reconnect" );
501 p_sys->b_continuous = var_InheritBool( p_access, "http-continuous" );
505 switch( Connect( p_access, 0 ) )
511 /* Retry with http 1.0 */
512 msg_Dbg( p_access, "switching to HTTP version 1.0" );
513 p_sys->i_version = 0;
514 p_sys->b_seekable = false;
516 if( !vlc_object_alive (p_access) || Connect( p_access, 0 ) )
526 if( p_sys->i_code == 401 )
528 char *psz_login, *psz_password;
530 if( p_sys->url.psz_username && p_sys->url.psz_password &&
531 p_sys->auth.psz_nonce && p_sys->auth.i_nonce == 0 )
533 Disconnect( p_access );
536 msg_Dbg( p_access, "authentication failed for realm %s",
537 p_sys->auth.psz_realm );
538 dialog_Login( p_access, &psz_login, &psz_password,
539 _("HTTP authentication"),
540 _("Please enter a valid login name and a password for realm %s."),
541 p_sys->auth.psz_realm );
542 if( psz_login != NULL && psz_password != NULL )
544 msg_Dbg( p_access, "retrying with user=%s", psz_login );
545 p_sys->url.psz_username = psz_login;
546 p_sys->url.psz_password = psz_password;
547 Disconnect( p_access );
553 free( psz_password );
558 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
559 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
560 p_sys->psz_location && *p_sys->psz_location )
562 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
564 /* Check the number of redirection already done */
565 if( i_redirect == 0 )
567 msg_Err( p_access, "Too many redirection: break potential infinite"
573 /* Do not accept redirection outside of HTTP works */
574 const char *psz_protocol;
575 if( !strncmp( p_sys->psz_location, "http:", 5 ) )
576 psz_protocol = "http";
577 else if( !strncmp( p_sys->psz_location, "https:", 6 ) )
578 psz_protocol = "https";
581 msg_Err( p_access, "insecure redirection ignored" );
584 free( p_access->psz_location );
585 p_access->psz_location = strdup( p_sys->psz_location );
586 /* Clean up current Open() run */
587 vlc_UrlClean( &p_sys->url );
588 http_auth_Reset( &p_sys->auth );
589 vlc_UrlClean( &p_sys->proxy );
590 free( p_sys->psz_proxy_passbuf );
591 http_auth_Reset( &p_sys->proxy_auth );
592 free( p_sys->psz_mime );
593 free( p_sys->psz_pragma );
594 free( p_sys->psz_location );
595 free( p_sys->psz_user_agent );
596 free( p_sys->psz_referrer );
598 Disconnect( p_access );
599 cookies = p_sys->cookies;
601 inflateEnd( &p_sys->inflate.stream );
605 /* Do new Open() run with new data */
606 return OpenWithCookies( p_this, psz_protocol, i_redirect - 1,
612 msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
616 if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
618 if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
620 if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
621 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
623 free( p_access->psz_demux );
624 p_access->psz_demux = strdup( "nsv" );
626 else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
627 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
629 free( p_access->psz_demux );
630 p_access->psz_demux = strdup( "m4a" );
632 else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
634 free( p_access->psz_demux );
635 p_access->psz_demux = strdup( "mp3" );
638 msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
639 p_access->psz_demux );
641 #if 0 /* Doesn't work really well because of the pre-buffering in
642 * shoutcast servers (the buffer content will be sent as fast as
644 p_sys->b_pace_control = false;
647 else if( !p_sys->psz_mime )
649 free( p_access->psz_demux );
651 p_access->psz_demux = strdup( "mp3" );
653 /* else probably Ogg Vorbis */
655 else if( !strcasecmp( psz_access, "unsv" ) &&
657 !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
659 free( p_access->psz_demux );
660 /* Grrrr! detect ultravox server and force NSV demuxer */
661 p_access->psz_demux = strdup( "nsv" );
663 else if( !strcmp( psz_access, "itpc" ) )
665 free( p_access->psz_demux );
666 p_access->psz_demux = strdup( "podcast" );
668 else if( p_sys->psz_mime &&
669 !strncasecmp( p_sys->psz_mime, "application/xspf+xml", 20 ) &&
670 ( memchr( " ;\t", p_sys->psz_mime[20], 4 ) != NULL ) )
672 free( p_access->psz_demux );
673 p_access->psz_demux = strdup( "xspf-open" );
676 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
681 vlc_UrlClean( &p_sys->url );
682 vlc_UrlClean( &p_sys->proxy );
683 free( p_sys->psz_proxy_passbuf );
684 free( p_sys->psz_mime );
685 free( p_sys->psz_pragma );
686 free( p_sys->psz_location );
687 free( p_sys->psz_user_agent );
688 free( p_sys->psz_referrer );
690 Disconnect( p_access );
695 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
696 free(vlc_array_item_at_index( p_sys->cookies, i ));
697 vlc_array_destroy( p_sys->cookies );
701 inflateEnd( &p_sys->inflate.stream );
707 /*****************************************************************************
709 *****************************************************************************/
710 static void Close( vlc_object_t *p_this )
712 access_t *p_access = (access_t*)p_this;
713 access_sys_t *p_sys = p_access->p_sys;
715 vlc_UrlClean( &p_sys->url );
716 http_auth_Reset( &p_sys->auth );
717 vlc_UrlClean( &p_sys->proxy );
718 http_auth_Reset( &p_sys->proxy_auth );
720 free( p_sys->psz_mime );
721 free( p_sys->psz_pragma );
722 free( p_sys->psz_location );
724 free( p_sys->psz_icy_name );
725 free( p_sys->psz_icy_genre );
726 free( p_sys->psz_icy_title );
728 free( p_sys->psz_user_agent );
729 free( p_sys->psz_referrer );
731 Disconnect( p_access );
736 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
737 free(vlc_array_item_at_index( p_sys->cookies, i ));
738 vlc_array_destroy( p_sys->cookies );
742 inflateEnd( &p_sys->inflate.stream );
743 free( p_sys->inflate.p_buffer );
749 /* Read data from the socket taking care of chunked transfer if needed */
750 static int ReadData( access_t *p_access, int *pi_read,
751 uint8_t *p_buffer, size_t i_len )
753 access_sys_t *p_sys = p_access->p_sys;
754 if( p_sys->b_chunked )
756 if( p_sys->i_chunk < 0 )
759 if( p_sys->i_chunk <= 0 )
761 char *psz = net_Gets( p_access, p_sys->fd, p_sys->p_vs );
762 /* read the chunk header */
765 /* fatal error - end of file */
766 msg_Dbg( p_access, "failed reading chunk-header line" );
769 p_sys->i_chunk = strtoll( psz, NULL, 16 );
772 if( p_sys->i_chunk <= 0 ) /* eof */
779 if( i_len > p_sys->i_chunk )
780 i_len = p_sys->i_chunk;
782 *pi_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, false );
786 if( p_sys->b_chunked )
788 p_sys->i_chunk -= *pi_read;
789 if( p_sys->i_chunk <= 0 )
791 /* read the empty line */
792 char *psz = net_Gets( p_access, p_sys->fd, p_sys->p_vs );
799 /*****************************************************************************
800 * Read: Read up to i_len bytes from the http connection and place in
801 * p_buffer. Return the actual number of bytes read
802 *****************************************************************************/
803 static int ReadICYMeta( access_t *p_access );
804 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
806 access_sys_t *p_sys = p_access->p_sys;
809 if( p_sys->fd == -1 )
812 if( p_sys->b_has_size )
814 /* Remaining bytes in the file */
815 uint64_t remainder = p_access->info.i_size - p_access->info.i_pos;
816 if( remainder < i_len )
819 /* Remaining bytes in the response */
820 if( p_sys->i_remaining < i_len )
821 i_len = p_sys->i_remaining;
826 if( p_sys->i_icy_meta > 0 && p_access->info.i_pos-p_sys->i_icy_offset > 0 )
828 int64_t i_next = p_sys->i_icy_meta -
829 (p_access->info.i_pos - p_sys->i_icy_offset ) % p_sys->i_icy_meta;
831 if( i_next == p_sys->i_icy_meta )
833 if( ReadICYMeta( p_access ) )
840 if( ReadData( p_access, &i_read, p_buffer, i_len ) )
846 * I very much doubt that this will work.
847 * If i_read == 0, the connection *IS* dead, so the only
848 * sensible thing to do is Disconnect() and then retry.
849 * Otherwise, I got recv() completely wrong. -- Courmisch
851 if( p_sys->b_continuous )
853 Request( p_access, 0 );
854 p_sys->b_continuous = false;
855 i_read = Read( p_access, p_buffer, i_len );
856 p_sys->b_continuous = true;
858 Disconnect( p_access );
859 if( p_sys->b_reconnect && vlc_object_alive( p_access ) )
861 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
862 if( Connect( p_access, p_access->info.i_pos ) )
864 msg_Dbg( p_access, "reconnection failed" );
868 p_sys->b_reconnect = false;
869 i_read = Read( p_access, p_buffer, i_len );
870 p_sys->b_reconnect = true;
879 p_sys->b_error = true;
884 assert( i_read >= 0 );
885 p_access->info.i_pos += i_read;
886 if( p_sys->b_has_size )
888 assert( p_access->info.i_pos <= p_access->info.i_size );
889 assert( (unsigned)i_read <= p_sys->i_remaining );
890 p_sys->i_remaining -= i_read;
896 p_access->info.b_eof = true;
900 static int ReadICYMeta( access_t *p_access )
902 access_sys_t *p_sys = p_access->p_sys;
908 /* Read meta data length */
909 if( ReadData( p_access, &i_read, &buffer, 1 ) )
913 const int i_size = buffer << 4;
914 /* msg_Dbg( p_access, "ICY meta size=%u", i_size); */
916 psz_meta = malloc( i_size + 1 );
917 for( i_read = 0; i_read < i_size; )
920 if( ReadData( p_access, &i_tmp, (uint8_t *)&psz_meta[i_read], i_size - i_read ) )
929 psz_meta[i_read] = '\0'; /* Just in case */
931 /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
933 /* Now parse the meta */
934 /* Look for StreamTitle= */
935 p = strcasestr( (char *)psz_meta, "StreamTitle=" );
938 p += strlen( "StreamTitle=" );
939 if( *p == '\'' || *p == '"' )
941 char closing[] = { p[0], ';', '\0' };
942 char *psz = strstr( &p[1], closing );
944 psz = strchr( &p[1], ';' );
946 if( psz ) *psz = '\0';
950 char *psz = strchr( &p[1], ';' );
951 if( psz ) *psz = '\0';
954 if( !p_sys->psz_icy_title ||
955 strcmp( p_sys->psz_icy_title, &p[1] ) )
957 free( p_sys->psz_icy_title );
958 char *psz_tmp = strdup( &p[1] );
959 p_sys->psz_icy_title = EnsureUTF8( psz_tmp );
960 if( !p_sys->psz_icy_title )
962 p_access->info.i_update |= INPUT_UPDATE_META;
964 msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
973 static ssize_t ReadCompressed( access_t *p_access, uint8_t *p_buffer,
976 access_sys_t *p_sys = p_access->p_sys;
978 if( p_sys->b_compressed )
982 if( !p_sys->inflate.p_buffer )
983 p_sys->inflate.p_buffer = malloc( 256 * 1024 );
985 if( p_sys->inflate.stream.avail_in == 0 )
987 ssize_t i_read = Read( p_access, p_sys->inflate.p_buffer, 256 * 1024 );
988 if( i_read <= 0 ) return i_read;
989 p_sys->inflate.stream.next_in = p_sys->inflate.p_buffer;
990 p_sys->inflate.stream.avail_in = i_read;
993 p_sys->inflate.stream.avail_out = i_len;
994 p_sys->inflate.stream.next_out = p_buffer;
996 i_ret = inflate( &p_sys->inflate.stream, Z_SYNC_FLUSH );
997 msg_Warn( p_access, "inflate return value: %d, %s", i_ret, p_sys->inflate.stream.msg );
999 return i_len - p_sys->inflate.stream.avail_out;
1003 return Read( p_access, p_buffer, i_len );
1008 /*****************************************************************************
1009 * Seek: close and re-open a connection at the right place
1010 *****************************************************************************/
1011 static int Seek( access_t *p_access, uint64_t i_pos )
1013 msg_Dbg( p_access, "trying to seek to %"PRId64, i_pos );
1015 Disconnect( p_access );
1017 if( p_access->info.i_size
1018 && i_pos >= p_access->info.i_size ) {
1019 msg_Err( p_access, "seek to far" );
1020 int retval = Seek( p_access, p_access->info.i_size - 1 );
1021 if( retval == VLC_SUCCESS ) {
1022 uint8_t p_buffer[2];
1023 Read( p_access, p_buffer, 1);
1024 p_access->info.b_eof = false;
1028 if( Connect( p_access, i_pos ) )
1030 msg_Err( p_access, "seek failed" );
1031 p_access->info.b_eof = true;
1032 return VLC_EGENERIC;
1037 /*****************************************************************************
1039 *****************************************************************************/
1040 static int Control( access_t *p_access, int i_query, va_list args )
1042 access_sys_t *p_sys = p_access->p_sys;
1050 case ACCESS_CAN_SEEK:
1051 pb_bool = (bool*)va_arg( args, bool* );
1052 *pb_bool = p_sys->b_seekable;
1054 case ACCESS_CAN_FASTSEEK:
1055 pb_bool = (bool*)va_arg( args, bool* );
1058 case ACCESS_CAN_PAUSE:
1059 case ACCESS_CAN_CONTROL_PACE:
1060 pb_bool = (bool*)va_arg( args, bool* );
1062 #if 0 /* Disable for now until we have a clock synchro algo
1063 * which works with something else than MPEG over UDP */
1064 *pb_bool = p_sys->b_pace_control;
1070 case ACCESS_GET_PTS_DELAY:
1071 pi_64 = (int64_t*)va_arg( args, int64_t * );
1072 *pi_64 = INT64_C(1000)
1073 * var_InheritInteger( p_access, "network-caching" );
1077 case ACCESS_SET_PAUSE_STATE:
1080 case ACCESS_GET_META:
1081 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
1083 if( p_sys->psz_icy_name )
1084 vlc_meta_Set( p_meta, vlc_meta_Title, p_sys->psz_icy_name );
1085 if( p_sys->psz_icy_genre )
1086 vlc_meta_Set( p_meta, vlc_meta_Genre, p_sys->psz_icy_genre );
1087 if( p_sys->psz_icy_title )
1088 vlc_meta_Set( p_meta, vlc_meta_NowPlaying, p_sys->psz_icy_title );
1091 case ACCESS_GET_CONTENT_TYPE:
1092 *va_arg( args, char ** ) =
1093 p_sys->psz_mime ? strdup( p_sys->psz_mime ) : NULL;
1096 case ACCESS_GET_TITLE_INFO:
1097 case ACCESS_SET_TITLE:
1098 case ACCESS_SET_SEEKPOINT:
1099 case ACCESS_SET_PRIVATE_ID_STATE:
1100 return VLC_EGENERIC;
1103 msg_Warn( p_access, "unimplemented query in control" );
1104 return VLC_EGENERIC;
1110 /*****************************************************************************
1112 *****************************************************************************/
1113 static int Connect( access_t *p_access, uint64_t i_tell )
1115 access_sys_t *p_sys = p_access->p_sys;
1116 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
1119 free( p_sys->psz_location );
1120 free( p_sys->psz_mime );
1121 free( p_sys->psz_pragma );
1123 free( p_sys->psz_icy_genre );
1124 free( p_sys->psz_icy_name );
1125 free( p_sys->psz_icy_title );
1128 p_sys->psz_location = NULL;
1129 p_sys->psz_mime = NULL;
1130 p_sys->psz_pragma = NULL;
1131 p_sys->b_mms = false;
1132 p_sys->b_chunked = false;
1134 p_sys->i_icy_meta = 0;
1135 p_sys->i_icy_offset = i_tell;
1136 p_sys->psz_icy_name = NULL;
1137 p_sys->psz_icy_genre = NULL;
1138 p_sys->psz_icy_title = NULL;
1139 p_sys->i_remaining = 0;
1140 p_sys->b_persist = false;
1141 p_sys->b_has_size = false;
1142 p_access->info.i_size = 0;
1143 p_access->info.i_pos = i_tell;
1144 p_access->info.b_eof = false;
1146 /* Open connection */
1147 assert( p_sys->fd == -1 ); /* No open sockets (leaking fds is BAD) */
1148 p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
1149 if( p_sys->fd == -1 )
1151 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
1154 setsockopt (p_sys->fd, SOL_SOCKET, SO_KEEPALIVE, &(int){ 1 }, sizeof (int));
1156 /* Initialize TLS/SSL session */
1159 /* CONNECT to establish TLS tunnel through HTTP proxy */
1160 if( p_sys->b_proxy )
1163 unsigned i_status = 0;
1165 if( p_sys->i_version == 0 )
1167 /* CONNECT is not in HTTP/1.0 */
1168 Disconnect( p_access );
1172 net_Printf( p_access, p_sys->fd, NULL,
1173 "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
1174 p_sys->url.psz_host, p_sys->url.i_port,
1176 p_sys->url.psz_host, p_sys->url.i_port);
1178 psz = net_Gets( p_access, p_sys->fd, NULL );
1181 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
1182 Disconnect( p_access );
1186 sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
1189 if( ( i_status / 100 ) != 2 )
1191 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
1192 Disconnect( p_access );
1198 psz = net_Gets( p_access, p_sys->fd, NULL );
1201 msg_Err( p_access, "HTTP proxy connection failed" );
1202 Disconnect( p_access );
1211 if( !vlc_object_alive (p_access) || p_sys->b_error )
1213 Disconnect( p_access );
1220 /* TLS/SSL handshake */
1221 p_sys->p_tls = vlc_tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
1222 p_sys->url.psz_host );
1223 if( p_sys->p_tls == NULL )
1225 msg_Err( p_access, "cannot establish HTTP/TLS session" );
1226 Disconnect( p_access );
1229 p_sys->p_vs = &p_sys->p_tls->sock;
1232 return Request( p_access, i_tell ) ? -2 : 0;
1236 static int Request( access_t *p_access, uint64_t i_tell )
1238 access_sys_t *p_sys = p_access->p_sys;
1240 v_socket_t *pvs = p_sys->p_vs;
1241 p_sys->b_persist = false;
1243 p_sys->i_remaining = 0;
1244 if( p_sys->b_proxy )
1246 if( p_sys->url.psz_path )
1248 net_Printf( p_access, p_sys->fd, NULL,
1249 "GET http://%s:%d%s HTTP/1.%d\r\n",
1250 p_sys->url.psz_host, p_sys->url.i_port,
1251 p_sys->url.psz_path, p_sys->i_version );
1255 net_Printf( p_access, p_sys->fd, NULL,
1256 "GET http://%s:%d/ HTTP/1.%d\r\n",
1257 p_sys->url.psz_host, p_sys->url.i_port,
1263 const char *psz_path = p_sys->url.psz_path;
1264 if( !psz_path || !*psz_path )
1268 if( p_sys->url.i_port != (pvs ? 443 : 80) )
1270 net_Printf( p_access, p_sys->fd, pvs,
1271 "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
1272 psz_path, p_sys->i_version, p_sys->url.psz_host,
1273 p_sys->url.i_port );
1277 net_Printf( p_access, p_sys->fd, pvs,
1278 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
1279 psz_path, p_sys->i_version, p_sys->url.psz_host );
1283 net_Printf( p_access, p_sys->fd, pvs,
1284 "User-Agent: %s\r\n",
1285 p_sys->psz_user_agent );
1287 if (p_sys->psz_referrer)
1289 net_Printf( p_access, p_sys->fd, pvs,
1291 p_sys->psz_referrer);
1294 if( p_sys->i_version == 1 && ! p_sys->b_continuous )
1296 p_sys->b_persist = true;
1297 net_Printf( p_access, p_sys->fd, pvs,
1298 "Range: bytes=%"PRIu64"-\r\n", i_tell );
1299 net_Printf( p_access, p_sys->fd, pvs, "Connection: close\r\n" );
1303 if( p_sys->cookies )
1306 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
1308 const char * cookie = vlc_array_item_at_index( p_sys->cookies, i );
1309 char * psz_cookie_content = cookie_get_content( cookie );
1310 char * psz_cookie_domain = cookie_get_domain( cookie );
1312 assert( psz_cookie_content );
1314 /* FIXME: This is clearly not conforming to the rfc */
1315 bool is_in_right_domain = (!psz_cookie_domain || strstr( p_sys->url.psz_host, psz_cookie_domain ));
1317 if( is_in_right_domain )
1319 msg_Dbg( p_access, "Sending Cookie %s", psz_cookie_content );
1320 if( net_Printf( p_access, p_sys->fd, pvs, "Cookie: %s\r\n", psz_cookie_content ) < 0 )
1321 msg_Err( p_access, "failed to send Cookie" );
1323 free( psz_cookie_content );
1324 free( psz_cookie_domain );
1328 /* Authentication */
1329 if( p_sys->url.psz_username || p_sys->url.psz_password )
1330 AuthReply( p_access, "", &p_sys->url, &p_sys->auth );
1332 /* Proxy Authentication */
1333 if( p_sys->proxy.psz_username || p_sys->proxy.psz_password )
1334 AuthReply( p_access, "Proxy-", &p_sys->proxy, &p_sys->proxy_auth );
1336 /* ICY meta data request */
1337 net_Printf( p_access, p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
1340 if( net_Printf( p_access, p_sys->fd, pvs, "\r\n" ) < 0 )
1342 msg_Err( p_access, "failed to send request" );
1343 Disconnect( p_access );
1344 return VLC_EGENERIC;
1348 if( ( psz = net_Gets( p_access, p_sys->fd, pvs ) ) == NULL )
1350 msg_Err( p_access, "failed to read answer" );
1353 if( !strncmp( psz, "HTTP/1.", 7 ) )
1355 p_sys->psz_protocol = "HTTP";
1356 p_sys->i_code = atoi( &psz[9] );
1358 else if( !strncmp( psz, "ICY", 3 ) )
1360 p_sys->psz_protocol = "ICY";
1361 p_sys->i_code = atoi( &psz[4] );
1362 p_sys->b_reconnect = true;
1366 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1370 msg_Dbg( p_access, "protocol '%s' answer code %d",
1371 p_sys->psz_protocol, p_sys->i_code );
1372 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1374 p_sys->b_seekable = false;
1376 if( p_sys->i_code != 206 && p_sys->i_code != 401 )
1378 p_sys->b_seekable = false;
1380 /* Authentication error - We'll have to display the dialog */
1381 if( p_sys->i_code == 401 )
1385 /* Other fatal error */
1386 else if( p_sys->i_code >= 400 )
1388 msg_Err( p_access, "error: %s", psz );
1396 char *psz = net_Gets( p_access, p_sys->fd, pvs );
1401 msg_Err( p_access, "failed to read answer" );
1405 if( !vlc_object_alive (p_access) || p_sys->b_error )
1411 /* msg_Dbg( p_input, "Line=%s", psz ); */
1418 if( ( p = strchr( psz, ':' ) ) == NULL )
1420 msg_Err( p_access, "malformed header line: %s", psz );
1425 while( *p == ' ' ) p++;
1427 if( !strcasecmp( psz, "Content-Length" ) )
1429 uint64_t i_size = i_tell + (p_sys->i_remaining = (uint64_t)atoll( p ));
1430 if(i_size > p_access->info.i_size) {
1431 p_sys->b_has_size = true;
1432 p_access->info.i_size = i_size;
1434 msg_Dbg( p_access, "this frame size=%"PRIu64, p_sys->i_remaining );
1436 else if( !strcasecmp( psz, "Content-Range" ) ) {
1437 uint64_t i_ntell = i_tell;
1438 uint64_t i_nend = (p_access->info.i_size > 0)?(p_access->info.i_size - 1):i_tell;
1439 uint64_t i_nsize = p_access->info.i_size;
1440 sscanf(p,"bytes %"SCNu64"-%"SCNu64"/%"SCNu64,&i_ntell,&i_nend,&i_nsize);
1441 if(i_nend > i_ntell ) {
1442 p_access->info.i_pos = i_ntell;
1443 p_sys->i_icy_offset = i_ntell;
1444 p_sys->i_remaining = i_nend+1-i_ntell;
1445 uint64_t i_size = (i_nsize > i_nend) ? i_nsize : (i_nend + 1);
1446 if(i_size > p_access->info.i_size) {
1447 p_sys->b_has_size = true;
1448 p_access->info.i_size = i_size;
1450 msg_Dbg( p_access, "stream size=%"PRIu64",pos=%"PRIu64",remaining=%"PRIu64,
1451 i_nsize, i_ntell, p_sys->i_remaining);
1454 else if( !strcasecmp( psz, "Connection" ) ) {
1455 msg_Dbg( p_access, "Connection: %s",p );
1457 sscanf(p, "close%n",&i);
1459 p_sys->b_persist = false;
1462 else if( !strcasecmp( psz, "Location" ) )
1466 /* This does not follow RFC 2068, but yet if the url is not absolute,
1467 * handle it as everyone does. */
1470 const char *psz_http_ext = p_sys->b_ssl ? "s" : "" ;
1472 if( p_sys->url.i_port == ( p_sys->b_ssl ? 443 : 80 ) )
1474 if( asprintf(&psz_new_loc, "http%s://%s%s", psz_http_ext,
1475 p_sys->url.psz_host, p) < 0 )
1480 if( asprintf(&psz_new_loc, "http%s://%s:%d%s", psz_http_ext,
1481 p_sys->url.psz_host, p_sys->url.i_port, p) < 0 )
1487 psz_new_loc = strdup( p );
1490 free( p_sys->psz_location );
1491 p_sys->psz_location = psz_new_loc;
1493 else if( !strcasecmp( psz, "Content-Type" ) )
1495 free( p_sys->psz_mime );
1496 p_sys->psz_mime = strdup( p );
1497 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1499 else if( !strcasecmp( psz, "Content-Encoding" ) )
1501 msg_Dbg( p_access, "Content-Encoding: %s", p );
1502 if( !strcasecmp( p, "identity" ) )
1505 else if( !strcasecmp( p, "gzip" ) || !strcasecmp( p, "deflate" ) )
1506 p_sys->b_compressed = true;
1509 msg_Warn( p_access, "Unknown content coding: %s", p );
1511 else if( !strcasecmp( psz, "Pragma" ) )
1513 if( !strcasecmp( psz, "Pragma: features" ) )
1514 p_sys->b_mms = true;
1515 free( p_sys->psz_pragma );
1516 p_sys->psz_pragma = strdup( p );
1517 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1519 else if( !strcasecmp( psz, "Server" ) )
1521 msg_Dbg( p_access, "Server: %s", p );
1522 if( !strncasecmp( p, "Icecast", 7 ) ||
1523 !strncasecmp( p, "Nanocaster", 10 ) )
1525 /* Remember if this is Icecast
1526 * we need to force demux in this case without breaking
1529 /* Let live 365 streams (nanocaster) piggyback on the icecast
1530 * routine. They look very similar */
1532 p_sys->b_reconnect = true;
1533 p_sys->b_pace_control = false;
1534 p_sys->b_icecast = true;
1537 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1539 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1540 if( !strncasecmp( p, "chunked", 7 ) )
1542 p_sys->b_chunked = true;
1545 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1547 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1548 p_sys->i_icy_meta = atoi( p );
1549 if( p_sys->i_icy_meta < 0 )
1550 p_sys->i_icy_meta = 0;
1551 if( p_sys->i_icy_meta > 0 )
1552 p_sys->b_icecast = true;
1554 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1556 else if( !strcasecmp( psz, "Icy-Name" ) )
1558 free( p_sys->psz_icy_name );
1559 char *psz_tmp = strdup( p );
1560 p_sys->psz_icy_name = EnsureUTF8( psz_tmp );
1561 if( !p_sys->psz_icy_name )
1563 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1565 p_sys->b_icecast = true; /* be on the safeside. set it here as well. */
1566 p_sys->b_reconnect = true;
1567 p_sys->b_pace_control = false;
1569 else if( !strcasecmp( psz, "Icy-Genre" ) )
1571 free( p_sys->psz_icy_genre );
1572 char *psz_tmp = strdup( p );
1573 p_sys->psz_icy_genre = EnsureUTF8( psz_tmp );
1574 if( !p_sys->psz_icy_genre )
1576 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1578 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1580 msg_Dbg( p_access, "Icy-Notice: %s", p );
1582 else if( !strncasecmp( psz, "icy-", 4 ) ||
1583 !strncasecmp( psz, "ice-", 4 ) ||
1584 !strncasecmp( psz, "x-audiocast", 11 ) )
1586 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1588 else if( !strcasecmp( psz, "Set-Cookie" ) )
1590 if( p_sys->cookies )
1592 msg_Dbg( p_access, "Accepting Cookie: %s", p );
1593 cookie_append( p_sys->cookies, strdup(p) );
1596 msg_Dbg( p_access, "We have a Cookie we won't remember: %s", p );
1598 else if( !strcasecmp( psz, "www-authenticate" ) )
1600 msg_Dbg( p_access, "Authentication header: %s", p );
1601 http_auth_ParseWwwAuthenticateHeader( VLC_OBJECT(p_access),
1604 else if( !strcasecmp( psz, "proxy-authenticate" ) )
1606 msg_Dbg( p_access, "Proxy authentication header: %s", p );
1607 http_auth_ParseWwwAuthenticateHeader( VLC_OBJECT(p_access),
1608 &p_sys->proxy_auth, p );
1610 else if( !strcasecmp( psz, "authentication-info" ) )
1612 msg_Dbg( p_access, "Authentication Info header: %s", p );
1613 if( AuthCheckReply( p_access, p, &p_sys->url, &p_sys->auth ) )
1616 else if( !strcasecmp( psz, "proxy-authentication-info" ) )
1618 msg_Dbg( p_access, "Proxy Authentication Info header: %s", p );
1619 if( AuthCheckReply( p_access, p, &p_sys->proxy, &p_sys->proxy_auth ) )
1622 else if( !strcasecmp( psz, "Accept-Ranges" ) )
1624 if( !strcasecmp( p, "bytes" ) )
1625 p_sys->b_seekable = true;
1630 /* We close the stream for zero length data, unless of course the
1631 * server has already promised to do this for us.
1633 if( p_sys->b_has_size && p_sys->i_remaining == 0 && p_sys->b_persist ) {
1634 Disconnect( p_access );
1639 Disconnect( p_access );
1640 return VLC_EGENERIC;
1643 /*****************************************************************************
1645 *****************************************************************************/
1646 static void Disconnect( access_t *p_access )
1648 access_sys_t *p_sys = p_access->p_sys;
1650 if( p_sys->p_tls != NULL)
1652 vlc_tls_ClientDelete( p_sys->p_tls );
1653 p_sys->p_tls = NULL;
1656 if( p_sys->fd != -1)
1658 net_Close(p_sys->fd);
1664 /*****************************************************************************
1665 * Cookies (FIXME: we may want to rewrite that using a nice structure to hold
1666 * them) (FIXME: only support the "domain=" param)
1667 *****************************************************************************/
1669 /* Get the NAME=VALUE part of the Cookie */
1670 static char * cookie_get_content( const char * cookie )
1672 char * ret = strdup( cookie );
1673 if( !ret ) return NULL;
1675 /* Look for a ';' */
1676 while( *str && *str != ';' ) str++;
1677 /* Replace it by a end-char */
1678 if( *str == ';' ) *str = 0;
1682 /* Get the domain where the cookie is stored */
1683 static char * cookie_get_domain( const char * cookie )
1685 const char * str = cookie;
1686 static const char domain[] = "domain=";
1689 /* Look for a ';' */
1692 if( !strncmp( str, domain, sizeof(domain) - 1 /* minus \0 */ ) )
1694 str += sizeof(domain) - 1 /* minus \0 */;
1695 char * ret = strdup( str );
1696 /* Now remove the next ';' if present */
1697 char * ret_iter = ret;
1698 while( *ret_iter && *ret_iter != ';' ) ret_iter++;
1699 if( *ret_iter == ';' )
1703 /* Go to next ';' field */
1704 while( *str && *str != ';' ) str++;
1705 if( *str == ';' ) str++;
1707 while( *str && *str == ' ' ) str++;
1712 /* Get NAME in the NAME=VALUE field */
1713 static char * cookie_get_name( const char * cookie )
1715 char * ret = cookie_get_content( cookie ); /* NAME=VALUE */
1716 if( !ret ) return NULL;
1718 while( *str && *str != '=' ) str++;
1723 /* Add a cookie in cookies, checking to see how it should be added */
1724 static void cookie_append( vlc_array_t * cookies, char * cookie )
1731 char * cookie_name = cookie_get_name( cookie );
1733 /* Don't send invalid cookies */
1737 char * cookie_domain = cookie_get_domain( cookie );
1738 for( i = 0; i < vlc_array_count( cookies ); i++ )
1740 char * current_cookie = vlc_array_item_at_index( cookies, i );
1741 char * current_cookie_name = cookie_get_name( current_cookie );
1742 char * current_cookie_domain = cookie_get_domain( current_cookie );
1744 assert( current_cookie_name );
1746 bool is_domain_matching = ( cookie_domain && current_cookie_domain &&
1747 !strcmp( cookie_domain, current_cookie_domain ) );
1749 if( is_domain_matching && !strcmp( cookie_name, current_cookie_name ) )
1751 /* Remove previous value for this cookie */
1752 free( current_cookie );
1753 vlc_array_remove( cookies, i );
1756 free( current_cookie_name );
1757 free( current_cookie_domain );
1760 free( current_cookie_name );
1761 free( current_cookie_domain );
1763 free( cookie_name );
1764 free( cookie_domain );
1765 vlc_array_append( cookies, cookie );
1769 /*****************************************************************************
1770 * HTTP authentication
1771 *****************************************************************************/
1773 static void AuthReply( access_t *p_access, const char *psz_prefix,
1774 vlc_url_t *p_url, http_auth_t *p_auth )
1776 access_sys_t *p_sys = p_access->p_sys;
1780 http_auth_FormatAuthorizationHeader( VLC_OBJECT(p_access), p_auth,
1781 "GET", p_url->psz_path,
1782 p_url->psz_username,
1783 p_url->psz_password );
1784 if ( psz_value == NULL )
1787 net_Printf( p_access, p_sys->fd, p_sys->p_vs,
1788 "%sAuthorization: %s\r\n", psz_prefix, psz_value );
1792 static int AuthCheckReply( access_t *p_access, const char *psz_header,
1793 vlc_url_t *p_url, http_auth_t *p_auth )
1796 http_auth_ParseAuthenticationInfoHeader( VLC_OBJECT(p_access), p_auth,
1799 p_url->psz_username,
1800 p_url->psz_password );