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>
65 /*****************************************************************************
67 *****************************************************************************/
68 static int Open ( vlc_object_t * );
69 static void Close( vlc_object_t * );
71 #define PROXY_TEXT N_("HTTP proxy")
72 #define PROXY_LONGTEXT N_( \
73 "HTTP proxy to be used It must be of the form " \
74 "http://[user@]myproxy.mydomain:myport/ ; " \
75 "if empty, the http_proxy environment variable will be tried." )
77 #define PROXY_PASS_TEXT N_("HTTP proxy password")
78 #define PROXY_PASS_LONGTEXT N_( \
79 "If your HTTP proxy requires a password, set it here." )
81 #define CACHING_TEXT N_("Caching value in ms")
82 #define CACHING_LONGTEXT N_( \
83 "Caching value for HTTP streams. This " \
84 "value should be set in milliseconds." )
86 #define AGENT_TEXT N_("HTTP user agent")
87 #define AGENT_LONGTEXT N_("User agent that will be " \
88 "used for the connection.")
90 #define RECONNECT_TEXT N_("Auto re-connect")
91 #define RECONNECT_LONGTEXT N_( \
92 "Automatically try to reconnect to the stream in case of a sudden " \
95 #define CONTINUOUS_TEXT N_("Continuous stream")
96 #define CONTINUOUS_LONGTEXT N_("Read a file that is " \
97 "being constantly updated (for example, a JPG file on a server). " \
98 "You should not globally enable this option as it will break all other " \
99 "types of HTTP streams." )
101 #define FORWARD_COOKIES_TEXT N_("Forward Cookies")
102 #define FORWARD_COOKIES_LONGTEXT N_("Forward Cookies across http redirections.")
104 #define MAX_REDIRECT_TEXT N_("Max number of redirection")
105 #define MAX_REDIRECT_LONGTEXT N_("Limit the number of redirection to follow.")
107 #define USE_IE_PROXY_TEXT N_("Use Internet Explorer entered HTTP proxy server")
108 #define USE_IE_PROXY_LONGTEXT N_("Use Internet Explorer entered HTTP proxy " \
109 "server for all URL. Don't take into account bypasses settings and auto " \
110 "configuration scripts.")
113 set_description( N_("HTTP input") )
114 set_capability( "access", 0 )
115 set_shortname( N_( "HTTP(S)" ) )
116 set_category( CAT_INPUT )
117 set_subcategory( SUBCAT_INPUT_ACCESS )
119 add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
121 add_password( "http-proxy-pwd", NULL, NULL,
122 PROXY_PASS_TEXT, PROXY_PASS_LONGTEXT, false )
123 add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
124 CACHING_TEXT, CACHING_LONGTEXT, true )
126 add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
127 AGENT_LONGTEXT, true )
129 add_bool( "http-reconnect", false, NULL, RECONNECT_TEXT,
130 RECONNECT_LONGTEXT, true )
131 add_bool( "http-continuous", false, NULL, CONTINUOUS_TEXT,
132 CONTINUOUS_LONGTEXT, true )
134 add_bool( "http-forward-cookies", true, NULL, FORWARD_COOKIES_TEXT,
135 FORWARD_COOKIES_LONGTEXT, true )
136 add_integer( "http-max-redirect", 5, NULL, MAX_REDIRECT_TEXT,
137 MAX_REDIRECT_LONGTEXT, true )
139 add_bool( "http-use-IE-proxy", false, NULL, USE_IE_PROXY_TEXT,
140 USE_IE_PROXY_LONGTEXT, true )
142 add_obsolete_string("http-user")
143 add_obsolete_string("http-pwd")
144 add_shortcut( "http" )
145 add_shortcut( "https" )
146 add_shortcut( "unsv" )
147 add_shortcut( "itpc" ) /* iTunes Podcast */
148 add_shortcut( "icyx" )
149 set_callbacks( Open, Close )
152 /*****************************************************************************
154 *****************************************************************************/
159 tls_session_t *p_tls;
164 char *psz_user_agent;
170 http_auth_t proxy_auth;
171 char *psz_proxy_passbuf;
175 const char *psz_protocol;
197 int64_t i_icy_offset;
210 vlc_array_t * cookies;
214 static int OpenWithCookies( vlc_object_t *p_this, const char *psz_access,
215 int i_nb_redirect, int i_max_redirect,
216 vlc_array_t *cookies );
219 static ssize_t Read( access_t *, uint8_t *, size_t );
220 static ssize_t ReadCompressed( access_t *, uint8_t *, size_t );
221 static int Seek( access_t *, int64_t );
222 static int Control( access_t *, int, va_list );
225 static int Connect( access_t *, int64_t );
226 static int Request( access_t *p_access, int64_t i_tell );
227 static void Disconnect( access_t * );
229 /* Small Cookie utilities. Cookies support is partial. */
230 static char * cookie_get_content( const char * cookie );
231 static char * cookie_get_domain( const char * cookie );
232 static char * cookie_get_name( const char * cookie );
233 static void cookie_append( vlc_array_t * cookies, char * cookie );
236 static void AuthReply( access_t *p_acces, const char *psz_prefix,
237 vlc_url_t *p_url, http_auth_t *p_auth );
238 static int AuthCheckReply( access_t *p_access, const char *psz_header,
239 vlc_url_t *p_url, http_auth_t *p_auth );
241 /*****************************************************************************
243 *****************************************************************************/
244 static int Open( vlc_object_t *p_this )
246 access_t *p_access = (access_t*)p_this;
247 return OpenWithCookies( p_this, p_access->psz_access, 0,
248 var_CreateGetInteger( p_access, "http-max-redirect" ), NULL );
252 * Open the given url using the given cookies
253 * @param p_this: the vlc object
254 * @psz_access: the acces to use (http, https, ...) (this value must be used
255 * instead of p_access->psz_access)
256 * @i_nb_redirect: the number of redirection already done
257 * @i_max_redirect: limit to the number of redirection to follow
258 * @cookies: the available cookies
259 * @return vlc error codes
261 static int OpenWithCookies( vlc_object_t *p_this, const char *psz_access,
262 int i_nb_redirect, int i_max_redirect,
263 vlc_array_t *cookies )
265 access_t *p_access = (access_t*)p_this;
269 /* Only forward an store cookies if the corresponding option is activated */
270 bool b_forward_cookies = var_CreateGetBool( p_access, "http-forward-cookies" );
271 vlc_array_t * saved_cookies = b_forward_cookies ? (cookies ? cookies : vlc_array_new()) : NULL;
273 /* Set up p_access */
274 STANDARD_READ_ACCESS_INIT;
276 p_access->pf_read = ReadCompressed;
279 p_sys->b_proxy = false;
280 p_sys->psz_proxy_passbuf = NULL;
281 p_sys->i_version = 1;
282 p_sys->b_seekable = true;
283 p_sys->psz_mime = NULL;
284 p_sys->psz_pragma = NULL;
285 p_sys->b_mms = false;
286 p_sys->b_icecast = false;
287 p_sys->psz_location = NULL;
288 p_sys->psz_user_agent = NULL;
289 p_sys->b_pace_control = true;
290 p_sys->b_ssl = false;
292 p_sys->b_compressed = false;
293 /* 15 is the max windowBits, +32 to enable optional gzip decoding */
294 if( inflateInit2( &p_sys->inflate.stream, 32+15 ) != Z_OK )
295 msg_Warn( p_access, "Error during zlib initialisation: %s",
296 p_sys->inflate.stream.msg );
297 if( zlibCompileFlags() & (1<<17) )
298 msg_Warn( p_access, "Your zlib was compiled without gzip support." );
299 p_sys->inflate.p_buffer = NULL;
303 p_sys->i_icy_meta = 0;
304 p_sys->i_icy_offset = 0;
305 p_sys->psz_icy_name = NULL;
306 p_sys->psz_icy_genre = NULL;
307 p_sys->psz_icy_title = NULL;
308 p_sys->i_remaining = 0;
309 p_sys->b_persist = false;
310 p_access->info.i_size = -1;
311 p_access->info.i_pos = 0;
312 p_access->info.b_eof = false;
314 p_sys->cookies = saved_cookies;
316 http_auth_Init( &p_sys->auth );
317 http_auth_Init( &p_sys->proxy_auth );
319 /* Parse URI - remove spaces */
320 p = psz = strdup( p_access->psz_path );
321 while( (p = strchr( p, ' ' )) != NULL )
323 vlc_UrlParse( &p_sys->url, psz, 0 );
326 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
328 msg_Warn( p_access, "invalid host" );
331 if( !strncmp( psz_access, "https", 5 ) )
335 if( p_sys->url.i_port <= 0 )
336 p_sys->url.i_port = 443;
340 if( p_sys->url.i_port <= 0 )
341 p_sys->url.i_port = 80;
345 p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
348 psz = var_CreateGetNonEmptyString( p_access, "http-proxy" );
351 p_sys->b_proxy = true;
352 vlc_UrlParse( &p_sys->proxy, psz, 0 );
358 pxProxyFactory *pf = px_proxy_factory_new();
363 i=asprintf(&buf, "%s://%s", psz_access, p_access->psz_path);
366 msg_Dbg(p_access, "asking libproxy about url '%s'", buf);
367 char **proxies = px_proxy_factory_get_proxies(pf, buf);
370 msg_Dbg(p_access, "libproxy suggest to use '%s'", proxies[0]);
371 if(strcmp(proxies[0],"direct://") != 0)
373 p_sys->b_proxy = true;
374 vlc_UrlParse( &p_sys->proxy, proxies[0], 0);
377 for(i=0;proxies[i];i++) free(proxies[i]);
381 px_proxy_factory_free(pf);
385 msg_Err(p_access, "Allocating memory for libproxy failed");
388 #elif defined( WIN32 )
391 if( var_CreateGetBool( p_access, "http-use-IE-proxy" ) )
393 /* Try to get the proxy server address from Windows internet settings using registry. */
396 if( RegOpenKeyEx( HKEY_CURRENT_USER, "Software\\Microsoft" \
397 "\\Windows\\CurrentVersion\\Internet Settings",
398 0, KEY_READ, &h_key ) == ERROR_SUCCESS )
400 DWORD i_dataReadSize = 4; /* sizeof( DWORD ); */
401 DWORD proxyEnable = 0;
402 /* Get the proxy enable value */
403 if( RegQueryValueEx( h_key, "ProxyEnable", NULL, NULL,
404 (char *)&proxyEnable, &i_dataReadSize )
409 /* Proxy is enable */
411 i_dataReadSize = 256;
412 if( RegQueryValueEx( h_key, "ProxyServer",
417 /* Get the proxy URL :
418 Proxy server value in the registry can be something like "address:port"
419 or "ftp=adress1:port1;http=adress2:port2 ..." depending of the
422 psz_proxy = strstr( psz_key, "http=" );
423 if( psz_proxy != NULL )
425 psz_proxy += strlen( "http=" );
426 char *psz_endUrl = strchr( psz_proxy, ';' );
427 if( psz_endUrl != NULL )
432 /* Set proxy enable for this connection. */
433 p_sys->b_proxy = true;
434 vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
436 msg_Warn( p_access, "Couldn't read in registry " \
437 "the proxy server address." );
441 msg_Warn( p_access, "Couldn't read in registry if the " \
442 "proxy is enable or not." );
445 msg_Warn( p_access, "Couldn't open internet settings key " \
452 psz = getenv( "http_proxy" );
455 p_sys->b_proxy = true;
456 vlc_UrlParse( &p_sys->proxy, psz, 0 );
461 if( psz ) /* No, this is NOT a use-after-free error */
463 psz = var_CreateGetNonEmptyString( p_access, "http-proxy-pwd" );
465 p_sys->proxy.psz_password = p_sys->psz_proxy_passbuf = psz;
470 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
472 msg_Warn( p_access, "invalid proxy host" );
475 if( p_sys->proxy.i_port <= 0 )
477 p_sys->proxy.i_port = 80;
481 msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
482 p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
485 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
486 p_sys->proxy.i_port );
488 if( p_sys->url.psz_username && *p_sys->url.psz_username )
490 msg_Dbg( p_access, " user='%s'", p_sys->url.psz_username );
493 p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
494 p_sys->b_continuous = var_CreateGetBool( p_access, "http-continuous" );
498 switch( Connect( p_access, 0 ) )
504 /* Retry with http 1.0 */
505 msg_Dbg( p_access, "switching to HTTP version 1.0" );
506 p_sys->i_version = 0;
507 p_sys->b_seekable = false;
509 if( !vlc_object_alive (p_access) || Connect( p_access, 0 ) )
517 msg_Err( p_access, "You should not be here" );
522 if( p_sys->i_code == 401 )
524 char *psz_login, *psz_password;
526 if( p_sys->url.psz_username && p_sys->url.psz_password &&
527 p_sys->auth.psz_nonce && p_sys->auth.i_nonce == 0 )
529 Disconnect( p_access );
532 msg_Dbg( p_access, "authentication failed for realm %s",
533 p_sys->auth.psz_realm );
534 dialog_Login( p_access, &psz_login, &psz_password,
535 _("HTTP authentication"),
536 _("Please enter a valid login name and a password for realm %s."),
537 p_sys->auth.psz_realm );
538 if( psz_login != NULL && psz_password != NULL )
540 msg_Dbg( p_access, "retrying with user=%s", psz_login );
541 p_sys->url.psz_username = psz_login;
542 p_sys->url.psz_password = psz_password;
543 Disconnect( p_access );
549 free( psz_password );
554 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
555 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
556 p_sys->psz_location && *p_sys->psz_location )
558 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
560 /* Check the number of redirection already done */
561 if( i_nb_redirect >= i_max_redirect )
563 msg_Err( p_access, "Too many redirection: break potential infinite"
569 /* Do not accept redirection outside of HTTP works */
570 const char *psz_protocol;
571 if( !strncmp( p_sys->psz_location, "http:", 5 ) )
572 psz_protocol = "http";
573 else if( !strncmp( p_sys->psz_location, "https:", 6 ) )
574 psz_protocol = "https";
577 msg_Err( p_access, "insecure redirection ignored" );
580 free( p_access->psz_path );
581 p_access->psz_path = strdup( p_sys->psz_location );
582 /* Clean up current Open() run */
583 vlc_UrlClean( &p_sys->url );
584 http_auth_Reset( &p_sys->auth );
585 vlc_UrlClean( &p_sys->proxy );
586 free( p_sys->psz_proxy_passbuf );
587 http_auth_Reset( &p_sys->proxy_auth );
588 free( p_sys->psz_mime );
589 free( p_sys->psz_pragma );
590 free( p_sys->psz_location );
591 free( p_sys->psz_user_agent );
593 Disconnect( p_access );
594 cookies = p_sys->cookies;
596 inflateEnd( &p_sys->inflate.stream );
600 /* Do new Open() run with new data */
601 return OpenWithCookies( p_this, psz_protocol, i_nb_redirect + 1,
602 i_max_redirect, cookies );
607 msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
611 if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
613 if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
615 if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
616 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
618 free( p_access->psz_demux );
619 p_access->psz_demux = strdup( "nsv" );
621 else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
622 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
624 free( p_access->psz_demux );
625 p_access->psz_demux = strdup( "m4a" );
627 else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
629 free( p_access->psz_demux );
630 p_access->psz_demux = strdup( "mp3" );
633 msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
634 p_access->psz_demux );
636 #if 0 /* Doesn't work really well because of the pre-buffering in
637 * shoutcast servers (the buffer content will be sent as fast as
639 p_sys->b_pace_control = false;
642 else if( !p_sys->psz_mime )
644 free( p_access->psz_demux );
646 p_access->psz_demux = strdup( "mp3" );
648 /* else probably Ogg Vorbis */
650 else if( !strcasecmp( psz_access, "unsv" ) &&
652 !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
654 free( p_access->psz_demux );
655 /* Grrrr! detect ultravox server and force NSV demuxer */
656 p_access->psz_demux = strdup( "nsv" );
658 else if( !strcmp( psz_access, "itpc" ) )
660 free( p_access->psz_demux );
661 p_access->psz_demux = strdup( "podcast" );
663 else if( p_sys->psz_mime &&
664 !strncasecmp( p_sys->psz_mime, "application/xspf+xml", 20 ) &&
665 ( memchr( " ;\t", p_sys->psz_mime[20], 4 ) != NULL ) )
667 free( p_access->psz_demux );
668 p_access->psz_demux = strdup( "xspf-open" );
671 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
674 var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
679 vlc_UrlClean( &p_sys->url );
680 vlc_UrlClean( &p_sys->proxy );
681 free( p_sys->psz_proxy_passbuf );
682 free( p_sys->psz_mime );
683 free( p_sys->psz_pragma );
684 free( p_sys->psz_location );
685 free( p_sys->psz_user_agent );
687 Disconnect( p_access );
692 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
693 free(vlc_array_item_at_index( p_sys->cookies, i ));
694 vlc_array_destroy( p_sys->cookies );
698 inflateEnd( &p_sys->inflate.stream );
704 /*****************************************************************************
706 *****************************************************************************/
707 static void Close( vlc_object_t *p_this )
709 access_t *p_access = (access_t*)p_this;
710 access_sys_t *p_sys = p_access->p_sys;
712 vlc_UrlClean( &p_sys->url );
713 http_auth_Reset( &p_sys->auth );
714 vlc_UrlClean( &p_sys->proxy );
715 http_auth_Reset( &p_sys->proxy_auth );
717 free( p_sys->psz_mime );
718 free( p_sys->psz_pragma );
719 free( p_sys->psz_location );
721 free( p_sys->psz_icy_name );
722 free( p_sys->psz_icy_genre );
723 free( p_sys->psz_icy_title );
725 free( p_sys->psz_user_agent );
727 Disconnect( p_access );
732 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
733 free(vlc_array_item_at_index( p_sys->cookies, i ));
734 vlc_array_destroy( p_sys->cookies );
738 inflateEnd( &p_sys->inflate.stream );
739 free( p_sys->inflate.p_buffer );
745 /*****************************************************************************
746 * Read: Read up to i_len bytes from the http connection and place in
747 * p_buffer. Return the actual number of bytes read
748 *****************************************************************************/
749 static int ReadICYMeta( access_t *p_access );
750 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
752 access_sys_t *p_sys = p_access->p_sys;
755 if( p_sys->fd == -1 )
757 p_access->info.b_eof = true;
761 if( p_access->info.i_size >= 0 &&
762 i_len + p_access->info.i_pos > p_access->info.i_size )
764 if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
766 p_access->info.b_eof = true;
771 if( p_sys->b_chunked )
773 if( p_sys->i_chunk < 0 )
775 p_access->info.b_eof = true;
779 if( p_sys->i_chunk <= 0 )
781 char *psz = net_Gets( p_access, p_sys->fd, p_sys->p_vs );
782 /* read the chunk header */
785 /* fatal error - end of file */
786 msg_Dbg( p_access, "failed reading chunk-header line" );
789 p_sys->i_chunk = strtoll( psz, NULL, 16 );
792 if( p_sys->i_chunk <= 0 ) /* eof */
795 p_access->info.b_eof = true;
800 if( i_len > p_sys->i_chunk )
802 i_len = p_sys->i_chunk;
805 else if( p_access->info.i_size != -1 && (int64_t)i_len > p_sys->i_remaining) {
806 /* Only ask for the remaining length */
807 i_len = (size_t)p_sys->i_remaining;
809 p_access->info.b_eof = true;
815 if( p_sys->i_icy_meta > 0 && p_access->info.i_pos-p_sys->i_icy_offset > 0 )
817 int64_t i_next = p_sys->i_icy_meta -
818 (p_access->info.i_pos - p_sys->i_icy_offset ) % p_sys->i_icy_meta;
820 if( i_next == p_sys->i_icy_meta )
822 if( ReadICYMeta( p_access ) )
824 p_access->info.b_eof = true;
832 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, false );
836 p_access->info.i_pos += i_read;
838 if( p_sys->b_chunked )
840 p_sys->i_chunk -= i_read;
841 if( p_sys->i_chunk <= 0 )
843 /* read the empty line */
844 char *psz = net_Gets( p_access, p_sys->fd, p_sys->p_vs );
849 else if( i_read <= 0 )
852 * I very much doubt that this will work.
853 * If i_read == 0, the connection *IS* dead, so the only
854 * sensible thing to do is Disconnect() and then retry.
855 * Otherwise, I got recv() completely wrong. -- Courmisch
857 if( p_sys->b_continuous )
859 Request( p_access, 0 );
860 p_sys->b_continuous = false;
861 i_read = Read( p_access, p_buffer, i_len );
862 p_sys->b_continuous = true;
864 Disconnect( p_access );
865 if( p_sys->b_reconnect && vlc_object_alive( p_access ) )
867 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
868 if( Connect( p_access, p_access->info.i_pos ) )
870 msg_Dbg( p_access, "reconnection failed" );
874 p_sys->b_reconnect = false;
875 i_read = Read( p_access, p_buffer, i_len );
876 p_sys->b_reconnect = true;
881 p_access->info.b_eof = true;
882 else if( i_read < 0 )
883 p_access->b_error = true;
886 if( p_access->info.i_size != -1 )
888 p_sys->i_remaining -= i_read;
894 static int ReadICYMeta( access_t *p_access )
896 access_sys_t *p_sys = p_access->p_sys;
902 /* Read meta data length */
903 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
910 i_read = buffer << 4;
911 /* msg_Dbg( p_access, "ICY meta size=%u", i_read); */
913 psz_meta = malloc( i_read + 1 );
914 if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
915 (uint8_t *)psz_meta, i_read, true ) != i_read )
921 psz_meta[i_read] = '\0'; /* Just in case */
923 /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
925 /* Now parse the meta */
926 /* Look for StreamTitle= */
927 p = strcasestr( (char *)psz_meta, "StreamTitle=" );
930 p += strlen( "StreamTitle=" );
931 if( *p == '\'' || *p == '"' )
933 char closing[] = { p[0], ';', '\0' };
934 char *psz = strstr( &p[1], closing );
936 psz = strchr( &p[1], ';' );
938 if( psz ) *psz = '\0';
942 char *psz = strchr( &p[1], ';' );
943 if( psz ) *psz = '\0';
946 if( !p_sys->psz_icy_title ||
947 strcmp( p_sys->psz_icy_title, &p[1] ) )
949 free( p_sys->psz_icy_title );
950 char *psz_tmp = strdup( &p[1] );
951 p_sys->psz_icy_title = EnsureUTF8( psz_tmp );
952 if( !p_sys->psz_icy_title )
954 p_access->info.i_update |= INPUT_UPDATE_META;
956 msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
965 static ssize_t ReadCompressed( access_t *p_access, uint8_t *p_buffer,
968 access_sys_t *p_sys = p_access->p_sys;
970 if( p_sys->b_compressed )
974 if( !p_sys->inflate.p_buffer )
975 p_sys->inflate.p_buffer = malloc( 256 * 1024 );
977 if( p_sys->inflate.stream.avail_in == 0 )
979 ssize_t i_read = Read( p_access, p_sys->inflate.p_buffer + p_sys->inflate.stream.avail_in, 256 * 1024 );
980 if( i_read <= 0 ) return i_read;
981 p_sys->inflate.stream.next_in = p_sys->inflate.p_buffer;
982 p_sys->inflate.stream.avail_in = i_read;
985 p_sys->inflate.stream.avail_out = i_len;
986 p_sys->inflate.stream.next_out = p_buffer;
988 i_ret = inflate( &p_sys->inflate.stream, Z_SYNC_FLUSH );
989 msg_Warn( p_access, "inflate return value: %d, %s", i_ret, p_sys->inflate.stream.msg );
991 return i_len - p_sys->inflate.stream.avail_out;
995 return Read( p_access, p_buffer, i_len );
1000 /*****************************************************************************
1001 * Seek: close and re-open a connection at the right place
1002 *****************************************************************************/
1003 static int Seek( access_t *p_access, int64_t i_pos )
1005 msg_Dbg( p_access, "trying to seek to %"PRId64, i_pos );
1007 Disconnect( p_access );
1009 if( p_access->info.i_size
1010 && (uint64_t)i_pos >= (uint64_t)p_access->info.i_size ) {
1011 msg_Err( p_access, "seek to far" );
1012 int retval = Seek( p_access, p_access->info.i_size - 1 );
1013 if( retval == VLC_SUCCESS ) {
1014 uint8_t p_buffer[2];
1015 Read( p_access, p_buffer, 1);
1016 p_access->info.b_eof = false;
1020 if( Connect( p_access, i_pos ) )
1022 msg_Err( p_access, "seek failed" );
1023 p_access->info.b_eof = true;
1024 return VLC_EGENERIC;
1029 /*****************************************************************************
1031 *****************************************************************************/
1032 static int Control( access_t *p_access, int i_query, va_list args )
1034 access_sys_t *p_sys = p_access->p_sys;
1042 case ACCESS_CAN_SEEK:
1043 pb_bool = (bool*)va_arg( args, bool* );
1044 *pb_bool = p_sys->b_seekable;
1046 case ACCESS_CAN_FASTSEEK:
1047 pb_bool = (bool*)va_arg( args, bool* );
1050 case ACCESS_CAN_PAUSE:
1051 case ACCESS_CAN_CONTROL_PACE:
1052 pb_bool = (bool*)va_arg( args, bool* );
1054 #if 0 /* Disable for now until we have a clock synchro algo
1055 * which works with something else than MPEG over UDP */
1056 *pb_bool = p_sys->b_pace_control;
1062 case ACCESS_GET_PTS_DELAY:
1063 pi_64 = (int64_t*)va_arg( args, int64_t * );
1064 *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
1068 case ACCESS_SET_PAUSE_STATE:
1071 case ACCESS_GET_META:
1072 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
1074 if( p_sys->psz_icy_name )
1075 vlc_meta_Set( p_meta, vlc_meta_Title, p_sys->psz_icy_name );
1076 if( p_sys->psz_icy_genre )
1077 vlc_meta_Set( p_meta, vlc_meta_Genre, p_sys->psz_icy_genre );
1078 if( p_sys->psz_icy_title )
1079 vlc_meta_Set( p_meta, vlc_meta_NowPlaying, p_sys->psz_icy_title );
1082 case ACCESS_GET_CONTENT_TYPE:
1083 *va_arg( args, char ** ) =
1084 p_sys->psz_mime ? strdup( p_sys->psz_mime ) : NULL;
1087 case ACCESS_GET_TITLE_INFO:
1088 case ACCESS_SET_TITLE:
1089 case ACCESS_SET_SEEKPOINT:
1090 case ACCESS_SET_PRIVATE_ID_STATE:
1091 return VLC_EGENERIC;
1094 msg_Warn( p_access, "unimplemented query in control" );
1095 return VLC_EGENERIC;
1101 /*****************************************************************************
1103 *****************************************************************************/
1104 static int Connect( access_t *p_access, int64_t i_tell )
1106 access_sys_t *p_sys = p_access->p_sys;
1107 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
1110 free( p_sys->psz_location );
1111 free( p_sys->psz_mime );
1112 free( p_sys->psz_pragma );
1114 free( p_sys->psz_icy_genre );
1115 free( p_sys->psz_icy_name );
1116 free( p_sys->psz_icy_title );
1119 p_sys->psz_location = NULL;
1120 p_sys->psz_mime = NULL;
1121 p_sys->psz_pragma = NULL;
1122 p_sys->b_mms = false;
1123 p_sys->b_chunked = false;
1125 p_sys->i_icy_meta = 0;
1126 p_sys->i_icy_offset = i_tell;
1127 p_sys->psz_icy_name = NULL;
1128 p_sys->psz_icy_genre = NULL;
1129 p_sys->psz_icy_title = NULL;
1130 p_sys->i_remaining = 0;
1131 p_sys->b_persist = false;
1133 p_access->info.i_size = -1;
1134 p_access->info.i_pos = i_tell;
1135 p_access->info.b_eof = false;
1137 /* Open connection */
1138 assert( p_sys->fd == -1 ); /* No open sockets (leaking fds is BAD) */
1139 p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
1140 if( p_sys->fd == -1 )
1142 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
1145 setsockopt (p_sys->fd, SOL_SOCKET, SO_KEEPALIVE, &(int){ 1 }, sizeof (int));
1147 /* Initialize TLS/SSL session */
1148 if( p_sys->b_ssl == true )
1150 /* CONNECT to establish TLS tunnel through HTTP proxy */
1151 if( p_sys->b_proxy )
1154 unsigned i_status = 0;
1156 if( p_sys->i_version == 0 )
1158 /* CONNECT is not in HTTP/1.0 */
1159 Disconnect( p_access );
1163 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
1164 "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
1165 p_sys->url.psz_host, p_sys->url.i_port,
1167 p_sys->url.psz_host, p_sys->url.i_port);
1169 psz = net_Gets( p_access, p_sys->fd, NULL );
1172 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
1173 Disconnect( p_access );
1177 sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
1180 if( ( i_status / 100 ) != 2 )
1182 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
1183 Disconnect( p_access );
1189 psz = net_Gets( p_access, p_sys->fd, NULL );
1192 msg_Err( p_access, "HTTP proxy connection failed" );
1193 Disconnect( p_access );
1202 if( !vlc_object_alive (p_access) || p_access->b_error )
1204 Disconnect( p_access );
1211 /* TLS/SSL handshake */
1212 p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
1213 p_sys->url.psz_host );
1214 if( p_sys->p_tls == NULL )
1216 msg_Err( p_access, "cannot establish HTTP/TLS session" );
1217 Disconnect( p_access );
1220 p_sys->p_vs = &p_sys->p_tls->sock;
1223 return Request( p_access, i_tell ) ? -2 : 0;
1227 static int Request( access_t *p_access, int64_t i_tell )
1229 access_sys_t *p_sys = p_access->p_sys;
1231 v_socket_t *pvs = p_sys->p_vs;
1232 p_sys->b_persist = false;
1234 p_sys->i_remaining = 0;
1235 if( p_sys->b_proxy )
1237 if( p_sys->url.psz_path )
1239 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
1240 "GET http://%s:%d%s HTTP/1.%d\r\n",
1241 p_sys->url.psz_host, p_sys->url.i_port,
1242 p_sys->url.psz_path, p_sys->i_version );
1246 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
1247 "GET http://%s:%d/ HTTP/1.%d\r\n",
1248 p_sys->url.psz_host, p_sys->url.i_port,
1254 const char *psz_path = p_sys->url.psz_path;
1255 if( !psz_path || !*psz_path )
1259 if( p_sys->url.i_port != (pvs ? 443 : 80) )
1261 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1262 "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
1263 psz_path, p_sys->i_version, p_sys->url.psz_host,
1264 p_sys->url.i_port );
1268 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1269 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
1270 psz_path, p_sys->i_version, p_sys->url.psz_host );
1274 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "User-Agent: %s\r\n",
1275 p_sys->psz_user_agent );
1277 if( p_sys->i_version == 1 && ! p_sys->b_continuous )
1279 p_sys->b_persist = true;
1280 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1281 "Range: bytes=%"PRIu64"-\r\n", i_tell );
1285 if( p_sys->cookies )
1288 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
1290 const char * cookie = vlc_array_item_at_index( p_sys->cookies, i );
1291 char * psz_cookie_content = cookie_get_content( cookie );
1292 char * psz_cookie_domain = cookie_get_domain( cookie );
1294 assert( psz_cookie_content );
1296 /* FIXME: This is clearly not conforming to the rfc */
1297 bool is_in_right_domain = (!psz_cookie_domain || strstr( p_sys->url.psz_host, psz_cookie_domain ));
1299 if( is_in_right_domain )
1301 msg_Dbg( p_access, "Sending Cookie %s", psz_cookie_content );
1302 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Cookie: %s\r\n", psz_cookie_content ) < 0 )
1303 msg_Err( p_access, "failed to send Cookie" );
1305 free( psz_cookie_content );
1306 free( psz_cookie_domain );
1310 /* Authentication */
1311 if( p_sys->url.psz_username || p_sys->url.psz_password )
1312 AuthReply( p_access, "", &p_sys->url, &p_sys->auth );
1314 /* Proxy Authentication */
1315 if( p_sys->proxy.psz_username || p_sys->proxy.psz_password )
1316 AuthReply( p_access, "Proxy-", &p_sys->proxy, &p_sys->proxy_auth );
1318 /* ICY meta data request */
1319 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
1322 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "\r\n" ) < 0 )
1324 msg_Err( p_access, "failed to send request" );
1325 Disconnect( p_access );
1326 return VLC_EGENERIC;
1330 if( ( psz = net_Gets( p_access, p_sys->fd, pvs ) ) == NULL )
1332 msg_Err( p_access, "failed to read answer" );
1335 if( !strncmp( psz, "HTTP/1.", 7 ) )
1337 p_sys->psz_protocol = "HTTP";
1338 p_sys->i_code = atoi( &psz[9] );
1340 else if( !strncmp( psz, "ICY", 3 ) )
1342 p_sys->psz_protocol = "ICY";
1343 p_sys->i_code = atoi( &psz[4] );
1344 p_sys->b_reconnect = true;
1348 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1352 msg_Dbg( p_access, "protocol '%s' answer code %d",
1353 p_sys->psz_protocol, p_sys->i_code );
1354 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1356 p_sys->b_seekable = false;
1358 if( p_sys->i_code != 206 && p_sys->i_code != 401 )
1360 p_sys->b_seekable = false;
1362 /* Authentication error - We'll have to display the dialog */
1363 if( p_sys->i_code == 401 )
1367 /* Other fatal error */
1368 else if( p_sys->i_code >= 400 )
1370 msg_Err( p_access, "error: %s", psz );
1378 char *psz = net_Gets( p_access, p_sys->fd, pvs );
1383 msg_Err( p_access, "failed to read answer" );
1387 if( !vlc_object_alive (p_access) || p_access->b_error )
1393 /* msg_Dbg( p_input, "Line=%s", psz ); */
1400 if( ( p = strchr( psz, ':' ) ) == NULL )
1402 msg_Err( p_access, "malformed header line: %s", psz );
1407 while( *p == ' ' ) p++;
1409 if( !strcasecmp( psz, "Content-Length" ) )
1411 int64_t i_size = i_tell + (p_sys->i_remaining = atoll( p ));
1412 if(i_size > p_access->info.i_size) {
1413 p_access->info.i_size = i_size;
1415 msg_Dbg( p_access, "this frame size=%"PRId64, p_sys->i_remaining );
1417 else if( !strcasecmp( psz, "Content-Range" ) ) {
1418 int64_t i_ntell = i_tell;
1419 int64_t i_nend = (p_access->info.i_size > 0)?(p_access->info.i_size - 1):i_tell;
1420 int64_t i_nsize = p_access->info.i_size;
1421 sscanf(p,"bytes %"SCNd64"-%"SCNd64"/%"SCNd64,&i_ntell,&i_nend,&i_nsize);
1422 if(i_nend > i_ntell ) {
1423 p_access->info.i_pos = i_ntell;
1424 p_sys->i_remaining = i_nend+1-i_ntell;
1425 int64_t i_size = (i_nsize > i_nend) ? i_nsize : (i_nend + 1);
1426 if(i_size > p_access->info.i_size) {
1427 p_access->info.i_size = i_size;
1429 msg_Dbg( p_access, "stream size=%"PRId64",pos=%"PRId64",remaining=%"PRId64,i_nsize,i_ntell,p_sys->i_remaining);
1432 else if( !strcasecmp( psz, "Connection" ) ) {
1433 msg_Dbg( p_access, "Connection: %s",p );
1435 sscanf(p, "close%n",&i);
1437 p_sys->b_persist = false;
1440 else if( !strcasecmp( psz, "Location" ) )
1444 /* This does not follow RFC 2068, but yet if the url is not absolute,
1445 * handle it as everyone does. */
1448 const char *psz_http_ext = p_sys->b_ssl ? "s" : "" ;
1450 if( p_sys->url.i_port == ( p_sys->b_ssl ? 443 : 80 ) )
1452 if( asprintf(&psz_new_loc, "http%s://%s%s", psz_http_ext,
1453 p_sys->url.psz_host, p) < 0 )
1458 if( asprintf(&psz_new_loc, "http%s://%s:%d%s", psz_http_ext,
1459 p_sys->url.psz_host, p_sys->url.i_port, p) < 0 )
1465 psz_new_loc = strdup( p );
1468 free( p_sys->psz_location );
1469 p_sys->psz_location = psz_new_loc;
1471 else if( !strcasecmp( psz, "Content-Type" ) )
1473 free( p_sys->psz_mime );
1474 p_sys->psz_mime = strdup( p );
1475 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1477 else if( !strcasecmp( psz, "Content-Encoding" ) )
1479 msg_Dbg( p_access, "Content-Encoding: %s", p );
1480 if( strcasecmp( p, "identity" ) )
1482 p_sys->b_compressed = true;
1484 msg_Warn( p_access, "Compressed content not supported. Rebuild with zlib support." );
1487 else if( !strcasecmp( psz, "Pragma" ) )
1489 if( !strcasecmp( psz, "Pragma: features" ) )
1490 p_sys->b_mms = true;
1491 free( p_sys->psz_pragma );
1492 p_sys->psz_pragma = strdup( p );
1493 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1495 else if( !strcasecmp( psz, "Server" ) )
1497 msg_Dbg( p_access, "Server: %s", p );
1498 if( !strncasecmp( p, "Icecast", 7 ) ||
1499 !strncasecmp( p, "Nanocaster", 10 ) )
1501 /* Remember if this is Icecast
1502 * we need to force demux in this case without breaking
1505 /* Let live 365 streams (nanocaster) piggyback on the icecast
1506 * routine. They look very similar */
1508 p_sys->b_reconnect = true;
1509 p_sys->b_pace_control = false;
1510 p_sys->b_icecast = true;
1513 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1515 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1516 if( !strncasecmp( p, "chunked", 7 ) )
1518 p_sys->b_chunked = true;
1521 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1523 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1524 p_sys->i_icy_meta = atoi( p );
1525 if( p_sys->i_icy_meta < 0 )
1526 p_sys->i_icy_meta = 0;
1527 if( p_sys->i_icy_meta > 0 )
1528 p_sys->b_icecast = true;
1530 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1532 else if( !strcasecmp( psz, "Icy-Name" ) )
1534 free( p_sys->psz_icy_name );
1535 char *psz_tmp = strdup( p );
1536 p_sys->psz_icy_name = EnsureUTF8( psz_tmp );
1537 if( !p_sys->psz_icy_name )
1539 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1541 p_sys->b_icecast = true; /* be on the safeside. set it here as well. */
1542 p_sys->b_reconnect = true;
1543 p_sys->b_pace_control = false;
1545 else if( !strcasecmp( psz, "Icy-Genre" ) )
1547 free( p_sys->psz_icy_genre );
1548 char *psz_tmp = strdup( p );
1549 p_sys->psz_icy_genre = EnsureUTF8( psz_tmp );
1550 if( !p_sys->psz_icy_genre )
1552 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1554 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1556 msg_Dbg( p_access, "Icy-Notice: %s", p );
1558 else if( !strncasecmp( psz, "icy-", 4 ) ||
1559 !strncasecmp( psz, "ice-", 4 ) ||
1560 !strncasecmp( psz, "x-audiocast", 11 ) )
1562 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1564 else if( !strcasecmp( psz, "Set-Cookie" ) )
1566 if( p_sys->cookies )
1568 msg_Dbg( p_access, "Accepting Cookie: %s", p );
1569 cookie_append( p_sys->cookies, strdup(p) );
1572 msg_Dbg( p_access, "We have a Cookie we won't remember: %s", p );
1574 else if( !strcasecmp( psz, "www-authenticate" ) )
1576 msg_Dbg( p_access, "Authentication header: %s", p );
1577 http_auth_ParseWwwAuthenticateHeader( VLC_OBJECT(p_access),
1580 else if( !strcasecmp( psz, "proxy-authenticate" ) )
1582 msg_Dbg( p_access, "Proxy authentication header: %s", p );
1583 http_auth_ParseWwwAuthenticateHeader( VLC_OBJECT(p_access),
1584 &p_sys->proxy_auth, p );
1586 else if( !strcasecmp( psz, "authentication-info" ) )
1588 msg_Dbg( p_access, "Authentication Info header: %s", p );
1589 if( AuthCheckReply( p_access, p, &p_sys->url, &p_sys->auth ) )
1592 else if( !strcasecmp( psz, "proxy-authentication-info" ) )
1594 msg_Dbg( p_access, "Proxy Authentication Info header: %s", p );
1595 if( AuthCheckReply( p_access, p, &p_sys->proxy, &p_sys->proxy_auth ) )
1601 /* We close the stream for zero length data, unless of course the
1602 * server has already promised to do this for us.
1604 if( p_access->info.i_size != -1 && p_sys->i_remaining == 0 && p_sys->b_persist ) {
1605 Disconnect( p_access );
1610 Disconnect( p_access );
1611 return VLC_EGENERIC;
1614 /*****************************************************************************
1616 *****************************************************************************/
1617 static void Disconnect( access_t *p_access )
1619 access_sys_t *p_sys = p_access->p_sys;
1621 if( p_sys->p_tls != NULL)
1623 tls_ClientDelete( p_sys->p_tls );
1624 p_sys->p_tls = NULL;
1627 if( p_sys->fd != -1)
1629 net_Close(p_sys->fd);
1635 /*****************************************************************************
1636 * Cookies (FIXME: we may want to rewrite that using a nice structure to hold
1637 * them) (FIXME: only support the "domain=" param)
1638 *****************************************************************************/
1640 /* Get the NAME=VALUE part of the Cookie */
1641 static char * cookie_get_content( const char * cookie )
1643 char * ret = strdup( cookie );
1644 if( !ret ) return NULL;
1646 /* Look for a ';' */
1647 while( *str && *str != ';' ) str++;
1648 /* Replace it by a end-char */
1649 if( *str == ';' ) *str = 0;
1653 /* Get the domain where the cookie is stored */
1654 static char * cookie_get_domain( const char * cookie )
1656 const char * str = cookie;
1657 static const char domain[] = "domain=";
1660 /* Look for a ';' */
1663 if( !strncmp( str, domain, sizeof(domain) - 1 /* minus \0 */ ) )
1665 str += sizeof(domain) - 1 /* minus \0 */;
1666 char * ret = strdup( str );
1667 /* Now remove the next ';' if present */
1668 char * ret_iter = ret;
1669 while( *ret_iter && *ret_iter != ';' ) ret_iter++;
1670 if( *ret_iter == ';' )
1674 /* Go to next ';' field */
1675 while( *str && *str != ';' ) str++;
1676 if( *str == ';' ) str++;
1678 while( *str && *str == ' ' ) str++;
1683 /* Get NAME in the NAME=VALUE field */
1684 static char * cookie_get_name( const char * cookie )
1686 char * ret = cookie_get_content( cookie ); /* NAME=VALUE */
1687 if( !ret ) return NULL;
1689 while( *str && *str != '=' ) str++;
1694 /* Add a cookie in cookies, checking to see how it should be added */
1695 static void cookie_append( vlc_array_t * cookies, char * cookie )
1702 char * cookie_name = cookie_get_name( cookie );
1704 /* Don't send invalid cookies */
1708 char * cookie_domain = cookie_get_domain( cookie );
1709 for( i = 0; i < vlc_array_count( cookies ); i++ )
1711 char * current_cookie = vlc_array_item_at_index( cookies, i );
1712 char * current_cookie_name = cookie_get_name( current_cookie );
1713 char * current_cookie_domain = cookie_get_domain( current_cookie );
1715 assert( current_cookie_name );
1717 bool is_domain_matching = ( cookie_domain && current_cookie_domain &&
1718 !strcmp( cookie_domain, current_cookie_domain ) );
1720 if( is_domain_matching && !strcmp( cookie_name, current_cookie_name ) )
1722 /* Remove previous value for this cookie */
1723 free( current_cookie );
1724 vlc_array_remove( cookies, i );
1727 free( current_cookie_name );
1728 free( current_cookie_domain );
1731 free( current_cookie_name );
1732 free( current_cookie_domain );
1734 free( cookie_name );
1735 free( cookie_domain );
1736 vlc_array_append( cookies, cookie );
1740 /*****************************************************************************
1741 * HTTP authentication
1742 *****************************************************************************/
1744 static void AuthReply( access_t *p_access, const char *psz_prefix,
1745 vlc_url_t *p_url, http_auth_t *p_auth )
1747 access_sys_t *p_sys = p_access->p_sys;
1751 http_auth_FormatAuthorizationHeader( VLC_OBJECT(p_access), p_auth,
1752 "GET", p_url->psz_path,
1753 p_url->psz_username,
1754 p_url->psz_password );
1755 if ( psz_value == NULL )
1758 net_Printf( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs,
1759 "%sAuthorization: %s\r\n", psz_prefix, psz_value );
1763 static int AuthCheckReply( access_t *p_access, const char *psz_header,
1764 vlc_url_t *p_url, http_auth_t *p_auth )
1767 http_auth_ParseAuthenticationInfoHeader( VLC_OBJECT(p_access), p_auth,
1770 p_url->psz_username,
1771 p_url->psz_password );