1 /*****************************************************************************
2 * http.c: HTTP input module
3 *****************************************************************************
4 * Copyright (C) 2001-2008 VLC authors and VideoLAN
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 it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * 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 *****************************************************************************/
142 vlc_tls_creds_t *p_creds;
148 char *psz_user_agent;
155 http_auth_t proxy_auth;
156 char *psz_proxy_passbuf;
160 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;
273 p_sys->b_compressed = false;
274 /* 15 is the max windowBits, +32 to enable optional gzip decoding */
275 if( inflateInit2( &p_sys->inflate.stream, 32+15 ) != Z_OK )
276 msg_Warn( p_access, "Error during zlib initialisation: %s",
277 p_sys->inflate.stream.msg );
278 if( zlibCompileFlags() & (1<<17) )
279 msg_Warn( p_access, "Your zlib was compiled without gzip support." );
280 p_sys->inflate.p_buffer = NULL;
284 p_sys->i_icy_meta = 0;
285 p_sys->i_icy_offset = 0;
286 p_sys->psz_icy_name = NULL;
287 p_sys->psz_icy_genre = NULL;
288 p_sys->psz_icy_title = NULL;
289 p_sys->i_remaining = 0;
290 p_sys->b_persist = false;
291 p_sys->b_has_size = false;
292 p_access->info.i_size = 0;
293 p_access->info.i_pos = 0;
294 p_access->info.b_eof = false;
296 p_sys->cookies = saved_cookies;
298 http_auth_Init( &p_sys->auth );
299 http_auth_Init( &p_sys->proxy_auth );
301 /* Parse URI - remove spaces */
302 p = psz = strdup( p_access->psz_location );
303 while( (p = strchr( p, ' ' )) != NULL )
305 vlc_UrlParse( &p_sys->url, psz, 0 );
308 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
310 msg_Warn( p_access, "invalid host" );
313 if( !strncmp( psz_access, "https", 5 ) )
316 p_sys->p_creds = vlc_tls_ClientCreate( p_this );
317 if( p_sys->p_creds == NULL )
319 if( p_sys->url.i_port <= 0 )
320 p_sys->url.i_port = 443;
324 if( p_sys->url.i_port <= 0 )
325 p_sys->url.i_port = 80;
328 /* Determine the HTTP user agent */
329 /* See RFC2616 §2.2 token and comment definition, and §3.8 and
330 * §14.43 user-agent header */
331 p_sys->psz_user_agent = var_InheritString( p_access, "http-user-agent" );
332 if (p_sys->psz_user_agent)
334 unsigned comment_level = 0;
335 for( char *p = p_sys->psz_user_agent; *p; p++ )
338 if (comment_level == 0)
340 if( c < 32 || strchr( ")<>@,;:\\\"[]?={}", c ) )
341 *p = '_'; /* remove potentially harmful characters */
347 else if( c < 32 && strchr( "\t\r\n", c ) == NULL)
348 *p = '_'; /* remove potentially harmful characters */
352 if (comment_level == UINT_MAX)
357 /* truncate evil unclosed comments */
358 if (comment_level > 0)
360 char *p = strchr(p_sys->psz_user_agent, '(');
366 p_sys->psz_referrer = var_InheritString( p_access, "http-referrer" );
369 psz = var_InheritString( p_access, "http-proxy" );
372 p_sys->b_proxy = true;
373 vlc_UrlParse( &p_sys->proxy, psz, 0 );
379 pxProxyFactory *pf = px_proxy_factory_new();
384 i=asprintf(&buf, "%s://%s", psz_access, p_access->psz_location);
387 msg_Dbg(p_access, "asking libproxy about url '%s'", buf);
388 char **proxies = px_proxy_factory_get_proxies(pf, buf);
391 msg_Dbg(p_access, "libproxy suggest to use '%s'", proxies[0]);
392 if(strcmp(proxies[0],"direct://") != 0)
394 p_sys->b_proxy = true;
395 vlc_UrlParse( &p_sys->proxy, proxies[0], 0);
398 for(i=0;proxies[i];i++) free(proxies[i]);
402 px_proxy_factory_free(pf);
406 msg_Err(p_access, "Allocating memory for libproxy failed");
409 #elif (0) // defined( WIN32 ) The parsing is not complete enough
412 /* Try to get the proxy server address from Windows internet settings using registry. */
415 if( RegOpenKeyEx( HKEY_CURRENT_USER, "Software\\Microsoft"
416 "\\Windows\\CurrentVersion\\Internet Settings",
417 0, KEY_READ, &h_key ) == ERROR_SUCCESS )
419 DWORD len = sizeof( DWORD );
422 /* Get the proxy enable value */
423 if( RegQueryValueEx( h_key, "ProxyEnable", NULL, NULL,
424 &proxyEnable, &len ) == ERROR_SUCCESS
427 /* Proxy is enabled */
428 /* Get the proxy URL :
429 Proxy server value in the registry can be something like "address:port"
430 or "ftp=address1:port1;http=address2:port2 ..." depending of the
432 unsigned char key[256];
435 if( RegQueryValueEx( h_key, "ProxyServer", NULL, NULL,
436 key, &len ) == ERROR_SUCCESS )
438 /* FIXME: This is lame. The string should be tokenized. */
440 char *psz_proxy = strstr( (char *)key, "http=" );
441 if( psz_proxy != NULL )
444 char *end = strchr( psz_proxy, ';' );
449 psz_proxy = (char *)key;
450 /* Set proxy enable for this connection. */
451 p_sys->b_proxy = true;
452 vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
456 msg_Dbg( p_access, "HTTP proxy disabled (MSIE)" );
457 RegCloseKey( h_key );
463 psz = getenv( "http_proxy" );
466 p_sys->b_proxy = true;
467 vlc_UrlParse( &p_sys->proxy, psz, 0 );
472 if( psz ) /* No, this is NOT a use-after-free error */
474 psz = var_InheritString( p_access, "http-proxy-pwd" );
476 p_sys->proxy.psz_password = p_sys->psz_proxy_passbuf = psz;
481 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
483 msg_Warn( p_access, "invalid proxy host" );
486 if( p_sys->proxy.i_port <= 0 )
488 p_sys->proxy.i_port = 80;
492 msg_Dbg( p_access, "http: server='%s' port=%d file='%s'",
493 p_sys->url.psz_host, p_sys->url.i_port,
494 p_sys->url.psz_path != NULL ? p_sys->url.psz_path : "" );
497 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
498 p_sys->proxy.i_port );
500 if( p_sys->url.psz_username && *p_sys->url.psz_username )
502 msg_Dbg( p_access, " user='%s'", p_sys->url.psz_username );
505 p_sys->b_reconnect = var_InheritBool( p_access, "http-reconnect" );
506 p_sys->b_continuous = var_InheritBool( p_access, "http-continuous" );
510 switch( Connect( p_access, 0 ) )
516 /* Retry with http 1.0 */
517 msg_Dbg( p_access, "switching to HTTP version 1.0" );
518 p_sys->i_version = 0;
519 p_sys->b_seekable = false;
521 if( !vlc_object_alive (p_access) || Connect( p_access, 0 ) )
531 if( p_sys->i_code == 401 )
533 char *psz_login, *psz_password;
535 if( p_sys->url.psz_username && p_sys->url.psz_password &&
536 p_sys->auth.psz_nonce && p_sys->auth.i_nonce == 0 )
538 Disconnect( p_access );
541 msg_Dbg( p_access, "authentication failed for realm %s",
542 p_sys->auth.psz_realm );
543 dialog_Login( p_access, &psz_login, &psz_password,
544 _("HTTP authentication"),
545 _("Please enter a valid login name and a password for realm %s."),
546 p_sys->auth.psz_realm );
547 if( psz_login != NULL && psz_password != NULL )
549 msg_Dbg( p_access, "retrying with user=%s", psz_login );
550 p_sys->url.psz_username = psz_login;
551 p_sys->url.psz_password = psz_password;
552 Disconnect( p_access );
558 free( psz_password );
563 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
564 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
565 p_sys->psz_location && *p_sys->psz_location )
567 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
569 /* Check the number of redirection already done */
570 if( i_redirect == 0 )
572 msg_Err( p_access, "Too many redirection: break potential infinite"
577 const char *psz_protocol;
578 if( !strncmp( p_sys->psz_location, "http://", 7 ) )
579 psz_protocol = "http";
580 else if( !strncmp( p_sys->psz_location, "https://", 8 ) )
581 psz_protocol = "https";
583 { /* Do not accept redirection outside of HTTP */
584 msg_Err( p_access, "unsupported redirection ignored" );
587 free( p_access->psz_location );
588 p_access->psz_location = strdup( p_sys->psz_location
589 + strlen( psz_protocol ) + 3 );
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 vlc_tls_Delete( p_sys->p_creds );
604 cookies = p_sys->cookies;
606 inflateEnd( &p_sys->inflate.stream );
610 /* Do new Open() run with new data */
611 return OpenWithCookies( p_this, psz_protocol, i_redirect - 1,
617 msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
621 if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
623 if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
625 if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
626 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
628 free( p_access->psz_demux );
629 p_access->psz_demux = strdup( "nsv" );
631 else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
632 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
634 free( p_access->psz_demux );
635 p_access->psz_demux = strdup( "m4a" );
637 else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
639 free( p_access->psz_demux );
640 p_access->psz_demux = strdup( "mp3" );
643 msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
644 p_access->psz_demux );
646 #if 0 /* Doesn't work really well because of the pre-buffering in
647 * shoutcast servers (the buffer content will be sent as fast as
649 p_sys->b_pace_control = false;
652 else if( !p_sys->psz_mime )
654 free( p_access->psz_demux );
656 p_access->psz_demux = strdup( "mp3" );
658 /* else probably Ogg Vorbis */
660 else if( !strcasecmp( psz_access, "unsv" ) &&
662 !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
664 free( p_access->psz_demux );
665 /* Grrrr! detect ultravox server and force NSV demuxer */
666 p_access->psz_demux = strdup( "nsv" );
668 else if( !strcmp( psz_access, "itpc" ) )
670 free( p_access->psz_demux );
671 p_access->psz_demux = strdup( "podcast" );
673 else if( p_sys->psz_mime &&
674 !strncasecmp( p_sys->psz_mime, "application/xspf+xml", 20 ) &&
675 ( memchr( " ;\t", p_sys->psz_mime[20], 4 ) != NULL ) )
677 free( p_access->psz_demux );
678 p_access->psz_demux = strdup( "xspf-open" );
681 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
686 vlc_UrlClean( &p_sys->url );
687 vlc_UrlClean( &p_sys->proxy );
688 free( p_sys->psz_proxy_passbuf );
689 free( p_sys->psz_mime );
690 free( p_sys->psz_pragma );
691 free( p_sys->psz_location );
692 free( p_sys->psz_user_agent );
693 free( p_sys->psz_referrer );
695 Disconnect( p_access );
696 vlc_tls_Delete( p_sys->p_creds );
701 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
702 free(vlc_array_item_at_index( p_sys->cookies, i ));
703 vlc_array_destroy( p_sys->cookies );
707 inflateEnd( &p_sys->inflate.stream );
713 /*****************************************************************************
715 *****************************************************************************/
716 static void Close( vlc_object_t *p_this )
718 access_t *p_access = (access_t*)p_this;
719 access_sys_t *p_sys = p_access->p_sys;
721 vlc_UrlClean( &p_sys->url );
722 http_auth_Reset( &p_sys->auth );
723 vlc_UrlClean( &p_sys->proxy );
724 http_auth_Reset( &p_sys->proxy_auth );
726 free( p_sys->psz_mime );
727 free( p_sys->psz_pragma );
728 free( p_sys->psz_location );
730 free( p_sys->psz_icy_name );
731 free( p_sys->psz_icy_genre );
732 free( p_sys->psz_icy_title );
734 free( p_sys->psz_user_agent );
735 free( p_sys->psz_referrer );
737 Disconnect( p_access );
738 vlc_tls_Delete( p_sys->p_creds );
743 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
744 free(vlc_array_item_at_index( p_sys->cookies, i ));
745 vlc_array_destroy( p_sys->cookies );
749 inflateEnd( &p_sys->inflate.stream );
750 free( p_sys->inflate.p_buffer );
756 /* Read data from the socket taking care of chunked transfer if needed */
757 static int ReadData( access_t *p_access, int *pi_read,
758 uint8_t *p_buffer, size_t i_len )
760 access_sys_t *p_sys = p_access->p_sys;
761 if( p_sys->b_chunked )
763 if( p_sys->i_chunk < 0 )
766 if( p_sys->i_chunk <= 0 )
768 char *psz = net_Gets( p_access, p_sys->fd, p_sys->p_vs );
769 /* read the chunk header */
772 /* fatal error - end of file */
773 msg_Dbg( p_access, "failed reading chunk-header line" );
776 p_sys->i_chunk = strtoll( psz, NULL, 16 );
779 if( p_sys->i_chunk <= 0 ) /* eof */
786 if( i_len > p_sys->i_chunk )
787 i_len = p_sys->i_chunk;
789 *pi_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, false );
793 if( p_sys->b_chunked )
795 p_sys->i_chunk -= *pi_read;
796 if( p_sys->i_chunk <= 0 )
798 /* read the empty line */
799 char *psz = net_Gets( p_access, p_sys->fd, p_sys->p_vs );
806 /*****************************************************************************
807 * Read: Read up to i_len bytes from the http connection and place in
808 * p_buffer. Return the actual number of bytes read
809 *****************************************************************************/
810 static int ReadICYMeta( access_t *p_access );
811 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
813 access_sys_t *p_sys = p_access->p_sys;
816 if( p_sys->fd == -1 )
819 if( p_sys->b_has_size )
821 /* Remaining bytes in the file */
822 uint64_t remainder = p_access->info.i_size - p_access->info.i_pos;
823 if( remainder < i_len )
826 /* Remaining bytes in the response */
827 if( p_sys->i_remaining < i_len )
828 i_len = p_sys->i_remaining;
833 if( p_sys->i_icy_meta > 0 && p_access->info.i_pos - p_sys->i_icy_offset > 0 )
835 int64_t i_next = p_sys->i_icy_meta -
836 (p_access->info.i_pos - p_sys->i_icy_offset ) % p_sys->i_icy_meta;
838 if( i_next == p_sys->i_icy_meta )
840 if( ReadICYMeta( p_access ) )
847 if( ReadData( p_access, &i_read, p_buffer, i_len ) )
853 * I very much doubt that this will work.
854 * If i_read == 0, the connection *IS* dead, so the only
855 * sensible thing to do is Disconnect() and then retry.
856 * Otherwise, I got recv() completely wrong. -- Courmisch
858 if( p_sys->b_continuous )
860 Request( p_access, 0 );
861 p_sys->b_continuous = false;
862 i_read = Read( p_access, p_buffer, i_len );
863 p_sys->b_continuous = true;
865 Disconnect( p_access );
866 if( p_sys->b_reconnect && vlc_object_alive( p_access ) )
868 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
869 if( Connect( p_access, p_access->info.i_pos ) )
871 msg_Dbg( p_access, "reconnection failed" );
875 p_sys->b_reconnect = false;
876 i_read = Read( p_access, p_buffer, i_len );
877 p_sys->b_reconnect = true;
886 p_sys->b_error = true;
891 assert( i_read >= 0 );
892 p_access->info.i_pos += i_read;
893 if( p_sys->b_has_size )
895 assert( p_access->info.i_pos <= p_access->info.i_size );
896 assert( (unsigned)i_read <= p_sys->i_remaining );
897 p_sys->i_remaining -= i_read;
903 p_access->info.b_eof = true;
907 static int ReadICYMeta( access_t *p_access )
909 access_sys_t *p_sys = p_access->p_sys;
915 /* Read meta data length */
916 if( ReadData( p_access, &i_read, &buffer, 1 ) )
920 const int i_size = buffer << 4;
921 /* msg_Dbg( p_access, "ICY meta size=%u", i_size); */
923 psz_meta = malloc( i_size + 1 );
924 for( i_read = 0; i_read < i_size; )
927 if( ReadData( p_access, &i_tmp, (uint8_t *)&psz_meta[i_read], i_size - i_read ) || i_tmp <= 0 )
934 psz_meta[i_read] = '\0'; /* Just in case */
936 /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
938 /* Now parse the meta */
939 /* Look for StreamTitle= */
940 p = strcasestr( (char *)psz_meta, "StreamTitle=" );
943 p += strlen( "StreamTitle=" );
944 if( *p == '\'' || *p == '"' )
946 char closing[] = { p[0], ';', '\0' };
947 char *psz = strstr( &p[1], closing );
949 psz = strchr( &p[1], ';' );
951 if( psz ) *psz = '\0';
955 char *psz = strchr( &p[1], ';' );
956 if( psz ) *psz = '\0';
959 if( !p_sys->psz_icy_title ||
960 strcmp( p_sys->psz_icy_title, &p[1] ) )
962 free( p_sys->psz_icy_title );
963 char *psz_tmp = strdup( &p[1] );
964 p_sys->psz_icy_title = EnsureUTF8( psz_tmp );
965 if( !p_sys->psz_icy_title )
967 p_access->info.i_update |= INPUT_UPDATE_META;
969 msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
978 static ssize_t ReadCompressed( access_t *p_access, uint8_t *p_buffer,
981 access_sys_t *p_sys = p_access->p_sys;
983 if( p_sys->b_compressed )
987 if( !p_sys->inflate.p_buffer )
988 p_sys->inflate.p_buffer = malloc( 256 * 1024 );
990 if( p_sys->inflate.stream.avail_in == 0 )
992 ssize_t i_read = Read( p_access, p_sys->inflate.p_buffer, 256 * 1024 );
993 if( i_read <= 0 ) return i_read;
994 p_sys->inflate.stream.next_in = p_sys->inflate.p_buffer;
995 p_sys->inflate.stream.avail_in = i_read;
998 p_sys->inflate.stream.avail_out = i_len;
999 p_sys->inflate.stream.next_out = p_buffer;
1001 i_ret = inflate( &p_sys->inflate.stream, Z_SYNC_FLUSH );
1002 msg_Warn( p_access, "inflate return value: %d, %s", i_ret, p_sys->inflate.stream.msg );
1004 return i_len - p_sys->inflate.stream.avail_out;
1008 return Read( p_access, p_buffer, i_len );
1013 /*****************************************************************************
1014 * Seek: close and re-open a connection at the right place
1015 *****************************************************************************/
1016 static int Seek( access_t *p_access, uint64_t i_pos )
1018 msg_Dbg( p_access, "trying to seek to %"PRId64, i_pos );
1020 Disconnect( p_access );
1022 if( p_access->info.i_size
1023 && i_pos >= p_access->info.i_size ) {
1024 msg_Err( p_access, "seek to far" );
1025 int retval = Seek( p_access, p_access->info.i_size - 1 );
1026 if( retval == VLC_SUCCESS ) {
1027 uint8_t p_buffer[2];
1028 Read( p_access, p_buffer, 1);
1029 p_access->info.b_eof = false;
1033 if( Connect( p_access, i_pos ) )
1035 msg_Err( p_access, "seek failed" );
1036 p_access->info.b_eof = true;
1037 return VLC_EGENERIC;
1042 /*****************************************************************************
1044 *****************************************************************************/
1045 static int Control( access_t *p_access, int i_query, va_list args )
1047 access_sys_t *p_sys = p_access->p_sys;
1055 case ACCESS_CAN_SEEK:
1056 pb_bool = (bool*)va_arg( args, bool* );
1057 *pb_bool = p_sys->b_seekable;
1059 case ACCESS_CAN_FASTSEEK:
1060 pb_bool = (bool*)va_arg( args, bool* );
1063 case ACCESS_CAN_PAUSE:
1064 case ACCESS_CAN_CONTROL_PACE:
1065 pb_bool = (bool*)va_arg( args, bool* );
1067 #if 0 /* Disable for now until we have a clock synchro algo
1068 * which works with something else than MPEG over UDP */
1069 *pb_bool = p_sys->b_pace_control;
1075 case ACCESS_GET_PTS_DELAY:
1076 pi_64 = (int64_t*)va_arg( args, int64_t * );
1077 *pi_64 = INT64_C(1000)
1078 * var_InheritInteger( p_access, "network-caching" );
1082 case ACCESS_SET_PAUSE_STATE:
1085 case ACCESS_GET_META:
1086 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
1088 if( p_sys->psz_icy_name )
1089 vlc_meta_Set( p_meta, vlc_meta_Title, p_sys->psz_icy_name );
1090 if( p_sys->psz_icy_genre )
1091 vlc_meta_Set( p_meta, vlc_meta_Genre, p_sys->psz_icy_genre );
1092 if( p_sys->psz_icy_title )
1093 vlc_meta_Set( p_meta, vlc_meta_NowPlaying, p_sys->psz_icy_title );
1096 case ACCESS_GET_CONTENT_TYPE:
1097 *va_arg( args, char ** ) =
1098 p_sys->psz_mime ? strdup( p_sys->psz_mime ) : NULL;
1101 case ACCESS_GET_TITLE_INFO:
1102 case ACCESS_SET_TITLE:
1103 case ACCESS_SET_SEEKPOINT:
1104 case ACCESS_SET_PRIVATE_ID_STATE:
1105 return VLC_EGENERIC;
1108 msg_Warn( p_access, "unimplemented query in control" );
1109 return VLC_EGENERIC;
1115 /*****************************************************************************
1117 *****************************************************************************/
1118 static int Connect( access_t *p_access, uint64_t i_tell )
1120 access_sys_t *p_sys = p_access->p_sys;
1121 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
1124 free( p_sys->psz_location );
1125 free( p_sys->psz_mime );
1126 free( p_sys->psz_pragma );
1128 free( p_sys->psz_icy_genre );
1129 free( p_sys->psz_icy_name );
1130 free( p_sys->psz_icy_title );
1133 p_sys->psz_location = NULL;
1134 p_sys->psz_mime = NULL;
1135 p_sys->psz_pragma = NULL;
1136 p_sys->b_mms = false;
1137 p_sys->b_chunked = false;
1139 p_sys->i_icy_meta = 0;
1140 p_sys->i_icy_offset = i_tell;
1141 p_sys->psz_icy_name = NULL;
1142 p_sys->psz_icy_genre = NULL;
1143 p_sys->psz_icy_title = NULL;
1144 p_sys->i_remaining = 0;
1145 p_sys->b_persist = false;
1146 p_sys->b_has_size = false;
1147 p_access->info.i_size = 0;
1148 p_access->info.i_pos = i_tell;
1149 p_access->info.b_eof = false;
1151 /* Open connection */
1152 assert( p_sys->fd == -1 ); /* No open sockets (leaking fds is BAD) */
1153 p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
1154 if( p_sys->fd == -1 )
1156 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
1159 setsockopt (p_sys->fd, SOL_SOCKET, SO_KEEPALIVE, &(int){ 1 }, sizeof (int));
1161 /* Initialize TLS/SSL session */
1162 if( p_sys->p_creds != NULL )
1164 /* CONNECT to establish TLS tunnel through HTTP proxy */
1165 if( p_sys->b_proxy )
1168 unsigned i_status = 0;
1170 if( p_sys->i_version == 0 )
1172 /* CONNECT is not in HTTP/1.0 */
1173 Disconnect( p_access );
1177 net_Printf( p_access, p_sys->fd, NULL,
1178 "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
1179 p_sys->url.psz_host, p_sys->url.i_port,
1181 p_sys->url.psz_host, p_sys->url.i_port);
1183 psz = net_Gets( p_access, p_sys->fd, NULL );
1186 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
1187 Disconnect( p_access );
1191 sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
1194 if( ( i_status / 100 ) != 2 )
1196 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
1197 Disconnect( p_access );
1203 psz = net_Gets( p_access, p_sys->fd, NULL );
1206 msg_Err( p_access, "HTTP proxy connection failed" );
1207 Disconnect( p_access );
1216 if( !vlc_object_alive (p_access) || p_sys->b_error )
1218 Disconnect( p_access );
1225 /* TLS/SSL handshake */
1226 p_sys->p_tls = vlc_tls_ClientSessionCreate( p_sys->p_creds, p_sys->fd,
1227 p_sys->url.psz_host, "https" );
1228 if( p_sys->p_tls == NULL )
1230 msg_Err( p_access, "cannot establish HTTP/TLS session" );
1231 Disconnect( p_access );
1234 p_sys->p_vs = &p_sys->p_tls->sock;
1237 return Request( p_access, i_tell ) ? -2 : 0;
1241 static int Request( access_t *p_access, uint64_t i_tell )
1243 access_sys_t *p_sys = p_access->p_sys;
1245 v_socket_t *pvs = p_sys->p_vs;
1246 p_sys->b_persist = false;
1248 p_sys->i_remaining = 0;
1250 const char *psz_path = p_sys->url.psz_path;
1251 if( !psz_path || !*psz_path )
1253 if( p_sys->b_proxy && pvs == NULL )
1254 net_Printf( p_access, p_sys->fd, NULL,
1255 "GET http://%s:%d%s HTTP/1.%d\r\n",
1256 p_sys->url.psz_host, p_sys->url.i_port,
1257 psz_path, p_sys->i_version );
1259 net_Printf( p_access, p_sys->fd, pvs, "GET %s HTTP/1.%d\r\n",
1260 psz_path, p_sys->i_version );
1261 if( p_sys->url.i_port != (pvs ? 443 : 80) )
1262 net_Printf( p_access, p_sys->fd, pvs, "Host: %s:%d\r\n",
1263 p_sys->url.psz_host, p_sys->url.i_port );
1265 net_Printf( p_access, p_sys->fd, pvs, "Host: %s\r\n",
1266 p_sys->url.psz_host );
1268 net_Printf( p_access, p_sys->fd, pvs, "User-Agent: %s\r\n",
1269 p_sys->psz_user_agent );
1271 if (p_sys->psz_referrer)
1273 net_Printf( p_access, p_sys->fd, pvs, "Referer: %s\r\n",
1274 p_sys->psz_referrer);
1277 if( p_sys->i_version == 1 && ! p_sys->b_continuous )
1279 p_sys->b_persist = true;
1280 net_Printf( p_access, p_sys->fd, pvs,
1281 "Range: bytes=%"PRIu64"-\r\n", i_tell );
1282 net_Printf( p_access, p_sys->fd, pvs, "Connection: close\r\n" );
1286 if( p_sys->cookies )
1289 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
1291 const char * cookie = vlc_array_item_at_index( p_sys->cookies, i );
1292 char * psz_cookie_content = cookie_get_content( cookie );
1293 char * psz_cookie_domain = cookie_get_domain( cookie );
1295 assert( psz_cookie_content );
1297 /* FIXME: This is clearly not conforming to the rfc */
1298 bool is_in_right_domain = (!psz_cookie_domain || strstr( p_sys->url.psz_host, psz_cookie_domain ));
1300 if( is_in_right_domain )
1302 msg_Dbg( p_access, "Sending Cookie %s", psz_cookie_content );
1303 if( net_Printf( p_access, p_sys->fd, pvs, "Cookie: %s\r\n", psz_cookie_content ) < 0 )
1304 msg_Err( p_access, "failed to send Cookie" );
1306 free( psz_cookie_content );
1307 free( psz_cookie_domain );
1311 /* Authentication */
1312 if( p_sys->url.psz_username || p_sys->url.psz_password )
1313 AuthReply( p_access, "", &p_sys->url, &p_sys->auth );
1315 /* Proxy Authentication */
1316 if( p_sys->proxy.psz_username || p_sys->proxy.psz_password )
1317 AuthReply( p_access, "Proxy-", &p_sys->proxy, &p_sys->proxy_auth );
1319 /* ICY meta data request */
1320 net_Printf( p_access, p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
1323 if( net_Printf( p_access, p_sys->fd, pvs, "\r\n" ) < 0 )
1325 msg_Err( p_access, "failed to send request" );
1326 Disconnect( p_access );
1327 return VLC_EGENERIC;
1331 if( ( psz = net_Gets( p_access, p_sys->fd, pvs ) ) == NULL )
1333 msg_Err( p_access, "failed to read answer" );
1336 if( !strncmp( psz, "HTTP/1.", 7 ) )
1338 p_sys->psz_protocol = "HTTP";
1339 p_sys->i_code = atoi( &psz[9] );
1341 else if( !strncmp( psz, "ICY", 3 ) )
1343 p_sys->psz_protocol = "ICY";
1344 p_sys->i_code = atoi( &psz[4] );
1345 p_sys->b_reconnect = true;
1349 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1353 msg_Dbg( p_access, "protocol '%s' answer code %d",
1354 p_sys->psz_protocol, p_sys->i_code );
1355 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1357 p_sys->b_seekable = false;
1359 if( p_sys->i_code != 206 && p_sys->i_code != 401 )
1361 p_sys->b_seekable = false;
1363 /* Authentication error - We'll have to display the dialog */
1364 if( p_sys->i_code == 401 )
1368 /* Other fatal error */
1369 else if( p_sys->i_code >= 400 )
1371 msg_Err( p_access, "error: %s", psz );
1379 char *psz = net_Gets( p_access, p_sys->fd, pvs );
1385 msg_Err( p_access, "failed to read answer" );
1389 if( !vlc_object_alive (p_access) || p_sys->b_error )
1395 /* msg_Dbg( p_input, "Line=%s", psz ); */
1402 if( ( p = strchr( psz, ':' ) ) == NULL )
1404 msg_Err( p_access, "malformed header line: %s", psz );
1409 p += strspn( p, " \t" );
1411 /* trim trailing white space */
1412 p_trailing = p + strlen( p );
1413 if( p_trailing > p )
1416 while( ( *p_trailing == ' ' || *p_trailing == '\t' ) && p_trailing > p )
1423 if( !strcasecmp( psz, "Content-Length" ) )
1425 uint64_t i_size = i_tell + (p_sys->i_remaining = (uint64_t)atoll( p ));
1426 if(i_size > p_access->info.i_size) {
1427 p_sys->b_has_size = true;
1428 p_access->info.i_size = i_size;
1430 msg_Dbg( p_access, "this frame size=%"PRIu64, p_sys->i_remaining );
1432 else if( !strcasecmp( psz, "Content-Range" ) ) {
1433 uint64_t i_ntell = i_tell;
1434 uint64_t i_nend = (p_access->info.i_size > 0)?(p_access->info.i_size - 1):i_tell;
1435 uint64_t i_nsize = p_access->info.i_size;
1436 sscanf(p,"bytes %"SCNu64"-%"SCNu64"/%"SCNu64,&i_ntell,&i_nend,&i_nsize);
1437 if(i_nend > i_ntell ) {
1438 p_access->info.i_pos = i_ntell;
1439 p_sys->i_icy_offset = i_ntell;
1440 p_sys->i_remaining = i_nend+1-i_ntell;
1441 uint64_t i_size = (i_nsize > i_nend) ? i_nsize : (i_nend + 1);
1442 if(i_size > p_access->info.i_size) {
1443 p_sys->b_has_size = true;
1444 p_access->info.i_size = i_size;
1446 msg_Dbg( p_access, "stream size=%"PRIu64",pos=%"PRIu64",remaining=%"PRIu64,
1447 i_nsize, i_ntell, p_sys->i_remaining);
1450 else if( !strcasecmp( psz, "Connection" ) ) {
1451 msg_Dbg( p_access, "Connection: %s",p );
1453 sscanf(p, "close%n",&i);
1455 p_sys->b_persist = false;
1458 else if( !strcasecmp( psz, "Location" ) )
1462 /* This does not follow RFC 2068, but yet if the url is not absolute,
1463 * handle it as everyone does. */
1466 const char *psz_http_ext = p_sys->p_tls ? "s" : "" ;
1468 if( p_sys->url.i_port == ( p_sys->p_tls ? 443 : 80 ) )
1470 if( asprintf(&psz_new_loc, "http%s://%s%s", psz_http_ext,
1471 p_sys->url.psz_host, p) < 0 )
1476 if( asprintf(&psz_new_loc, "http%s://%s:%d%s", psz_http_ext,
1477 p_sys->url.psz_host, p_sys->url.i_port, p) < 0 )
1483 psz_new_loc = strdup( p );
1486 free( p_sys->psz_location );
1487 p_sys->psz_location = psz_new_loc;
1489 else if( !strcasecmp( psz, "Content-Type" ) )
1491 free( p_sys->psz_mime );
1492 p_sys->psz_mime = strdup( p );
1493 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1495 else if( !strcasecmp( psz, "Content-Encoding" ) )
1497 msg_Dbg( p_access, "Content-Encoding: %s", p );
1498 if( !strcasecmp( p, "identity" ) )
1501 else if( !strcasecmp( p, "gzip" ) || !strcasecmp( p, "deflate" ) )
1502 p_sys->b_compressed = true;
1505 msg_Warn( p_access, "Unknown content coding: %s", p );
1507 else if( !strcasecmp( psz, "Pragma" ) )
1509 if( !strcasecmp( psz, "Pragma: features" ) )
1510 p_sys->b_mms = true;
1511 free( p_sys->psz_pragma );
1512 p_sys->psz_pragma = strdup( p );
1513 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1515 else if( !strcasecmp( psz, "Server" ) )
1517 msg_Dbg( p_access, "Server: %s", p );
1518 if( !strncasecmp( p, "Icecast", 7 ) ||
1519 !strncasecmp( p, "Nanocaster", 10 ) )
1521 /* Remember if this is Icecast
1522 * we need to force demux in this case without breaking
1525 /* Let live 365 streams (nanocaster) piggyback on the icecast
1526 * routine. They look very similar */
1528 p_sys->b_reconnect = true;
1529 p_sys->b_pace_control = false;
1530 p_sys->b_icecast = true;
1533 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1535 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1536 if( !strncasecmp( p, "chunked", 7 ) )
1538 p_sys->b_chunked = true;
1541 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1543 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1544 p_sys->i_icy_meta = atoi( p );
1545 if( p_sys->i_icy_meta < 0 )
1546 p_sys->i_icy_meta = 0;
1547 if( p_sys->i_icy_meta > 0 )
1548 p_sys->b_icecast = true;
1550 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1552 else if( !strcasecmp( psz, "Icy-Name" ) )
1554 free( p_sys->psz_icy_name );
1555 char *psz_tmp = strdup( p );
1556 p_sys->psz_icy_name = EnsureUTF8( psz_tmp );
1557 if( !p_sys->psz_icy_name )
1559 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1561 p_sys->b_icecast = true; /* be on the safeside. set it here as well. */
1562 p_sys->b_reconnect = true;
1563 p_sys->b_pace_control = false;
1565 else if( !strcasecmp( psz, "Icy-Genre" ) )
1567 free( p_sys->psz_icy_genre );
1568 char *psz_tmp = strdup( p );
1569 p_sys->psz_icy_genre = EnsureUTF8( psz_tmp );
1570 if( !p_sys->psz_icy_genre )
1572 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1574 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1576 msg_Dbg( p_access, "Icy-Notice: %s", p );
1578 else if( !strncasecmp( psz, "icy-", 4 ) ||
1579 !strncasecmp( psz, "ice-", 4 ) ||
1580 !strncasecmp( psz, "x-audiocast", 11 ) )
1582 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1584 else if( !strcasecmp( psz, "Set-Cookie" ) )
1586 if( p_sys->cookies )
1588 msg_Dbg( p_access, "Accepting Cookie: %s", p );
1589 cookie_append( p_sys->cookies, strdup(p) );
1592 msg_Dbg( p_access, "We have a Cookie we won't remember: %s", p );
1594 else if( !strcasecmp( psz, "www-authenticate" ) )
1596 msg_Dbg( p_access, "Authentication header: %s", p );
1597 http_auth_ParseWwwAuthenticateHeader( VLC_OBJECT(p_access),
1600 else if( !strcasecmp( psz, "proxy-authenticate" ) )
1602 msg_Dbg( p_access, "Proxy authentication header: %s", p );
1603 http_auth_ParseWwwAuthenticateHeader( VLC_OBJECT(p_access),
1604 &p_sys->proxy_auth, p );
1606 else if( !strcasecmp( psz, "authentication-info" ) )
1608 msg_Dbg( p_access, "Authentication Info header: %s", p );
1609 if( AuthCheckReply( p_access, p, &p_sys->url, &p_sys->auth ) )
1612 else if( !strcasecmp( psz, "proxy-authentication-info" ) )
1614 msg_Dbg( p_access, "Proxy Authentication Info header: %s", p );
1615 if( AuthCheckReply( p_access, p, &p_sys->proxy, &p_sys->proxy_auth ) )
1618 else if( !strcasecmp( psz, "Accept-Ranges" ) )
1620 if( !strcasecmp( p, "bytes" ) )
1621 p_sys->b_seekable = true;
1626 /* We close the stream for zero length data, unless of course the
1627 * server has already promised to do this for us.
1629 if( p_sys->b_has_size && p_sys->i_remaining == 0 && p_sys->b_persist ) {
1630 Disconnect( p_access );
1635 Disconnect( p_access );
1636 return VLC_EGENERIC;
1639 /*****************************************************************************
1641 *****************************************************************************/
1642 static void Disconnect( access_t *p_access )
1644 access_sys_t *p_sys = p_access->p_sys;
1646 if( p_sys->p_tls != NULL)
1648 vlc_tls_SessionDelete( p_sys->p_tls );
1649 p_sys->p_tls = NULL;
1652 if( p_sys->fd != -1)
1654 net_Close(p_sys->fd);
1660 /*****************************************************************************
1661 * Cookies (FIXME: we may want to rewrite that using a nice structure to hold
1662 * them) (FIXME: only support the "domain=" param)
1663 *****************************************************************************/
1665 /* Get the NAME=VALUE part of the Cookie */
1666 static char * cookie_get_content( const char * cookie )
1668 char * ret = strdup( cookie );
1669 if( !ret ) return NULL;
1671 /* Look for a ';' */
1672 while( *str && *str != ';' ) str++;
1673 /* Replace it by a end-char */
1674 if( *str == ';' ) *str = 0;
1678 /* Get the domain where the cookie is stored */
1679 static char * cookie_get_domain( const char * cookie )
1681 const char * str = cookie;
1682 static const char domain[] = "domain=";
1685 /* Look for a ';' */
1688 if( !strncmp( str, domain, sizeof(domain) - 1 /* minus \0 */ ) )
1690 str += sizeof(domain) - 1 /* minus \0 */;
1691 char * ret = strdup( str );
1692 /* Now remove the next ';' if present */
1693 char * ret_iter = ret;
1694 while( *ret_iter && *ret_iter != ';' ) ret_iter++;
1695 if( *ret_iter == ';' )
1699 /* Go to next ';' field */
1700 while( *str && *str != ';' ) str++;
1701 if( *str == ';' ) str++;
1703 while( *str && *str == ' ' ) str++;
1708 /* Get NAME in the NAME=VALUE field */
1709 static char * cookie_get_name( const char * cookie )
1711 char * ret = cookie_get_content( cookie ); /* NAME=VALUE */
1712 if( !ret ) return NULL;
1714 while( *str && *str != '=' ) str++;
1719 /* Add a cookie in cookies, checking to see how it should be added */
1720 static void cookie_append( vlc_array_t * cookies, char * cookie )
1727 char * cookie_name = cookie_get_name( cookie );
1729 /* Don't send invalid cookies */
1733 char * cookie_domain = cookie_get_domain( cookie );
1734 for( i = 0; i < vlc_array_count( cookies ); i++ )
1736 char * current_cookie = vlc_array_item_at_index( cookies, i );
1737 char * current_cookie_name = cookie_get_name( current_cookie );
1738 char * current_cookie_domain = cookie_get_domain( current_cookie );
1740 assert( current_cookie_name );
1742 bool is_domain_matching = (
1743 ( !cookie_domain && !current_cookie_domain ) ||
1744 ( cookie_domain && current_cookie_domain &&
1745 !strcmp( cookie_domain, current_cookie_domain ) ) );
1747 if( is_domain_matching && !strcmp( cookie_name, current_cookie_name ) )
1749 /* Remove previous value for this cookie */
1750 free( current_cookie );
1751 vlc_array_remove( cookies, i );
1754 free( current_cookie_name );
1755 free( current_cookie_domain );
1758 free( current_cookie_name );
1759 free( current_cookie_domain );
1761 free( cookie_name );
1762 free( cookie_domain );
1763 vlc_array_append( cookies, cookie );
1767 /*****************************************************************************
1768 * HTTP authentication
1769 *****************************************************************************/
1771 static void AuthReply( access_t *p_access, const char *psz_prefix,
1772 vlc_url_t *p_url, http_auth_t *p_auth )
1774 access_sys_t *p_sys = p_access->p_sys;
1778 http_auth_FormatAuthorizationHeader( VLC_OBJECT(p_access), p_auth,
1779 "GET", p_url->psz_path,
1780 p_url->psz_username,
1781 p_url->psz_password );
1782 if ( psz_value == NULL )
1785 net_Printf( p_access, p_sys->fd, p_sys->p_vs,
1786 "%sAuthorization: %s\r\n", psz_prefix, psz_value );
1790 static int AuthCheckReply( access_t *p_access, const char *psz_header,
1791 vlc_url_t *p_url, http_auth_t *p_auth )
1794 http_auth_ParseAuthenticationInfoHeader( VLC_OBJECT(p_access), p_auth,
1797 p_url->psz_username,
1798 p_url->psz_password );