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", PACKAGE_NAME" "PACKAGE_VERSION, NULL,
127 AGENT_TEXT, 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 *****************************************************************************/
160 tls_session_t *p_tls;
165 char *psz_user_agent;
171 http_auth_t proxy_auth;
172 char *psz_proxy_passbuf;
176 const char *psz_protocol;
198 uint64_t i_icy_offset;
203 uint64_t i_remaining;
212 vlc_array_t * cookies;
216 static int OpenWithCookies( vlc_object_t *p_this, const char *psz_access,
217 int i_nb_redirect, int i_max_redirect,
218 vlc_array_t *cookies );
221 static ssize_t Read( access_t *, uint8_t *, size_t );
222 static ssize_t ReadCompressed( access_t *, uint8_t *, size_t );
223 static int Seek( access_t *, uint64_t );
224 static int Control( access_t *, int, va_list );
227 static int Connect( access_t *, uint64_t );
228 static int Request( access_t *p_access, uint64_t i_tell );
229 static void Disconnect( access_t * );
231 /* Small Cookie utilities. Cookies support is partial. */
232 static char * cookie_get_content( const char * cookie );
233 static char * cookie_get_domain( const char * cookie );
234 static char * cookie_get_name( const char * cookie );
235 static void cookie_append( vlc_array_t * cookies, char * cookie );
238 static void AuthReply( access_t *p_acces, const char *psz_prefix,
239 vlc_url_t *p_url, http_auth_t *p_auth );
240 static int AuthCheckReply( access_t *p_access, const char *psz_header,
241 vlc_url_t *p_url, http_auth_t *p_auth );
243 /*****************************************************************************
245 *****************************************************************************/
246 static int Open( vlc_object_t *p_this )
248 access_t *p_access = (access_t*)p_this;
249 return OpenWithCookies( p_this, p_access->psz_access, 0,
250 var_CreateGetInteger( p_access, "http-max-redirect" ), NULL );
254 * Open the given url using the given cookies
255 * @param p_this: the vlc object
256 * @psz_access: the acces to use (http, https, ...) (this value must be used
257 * instead of p_access->psz_access)
258 * @i_nb_redirect: the number of redirection already done
259 * @i_max_redirect: limit to the number of redirection to follow
260 * @cookies: the available cookies
261 * @return vlc error codes
263 static int OpenWithCookies( vlc_object_t *p_this, const char *psz_access,
264 int i_nb_redirect, int i_max_redirect,
265 vlc_array_t *cookies )
267 access_t *p_access = (access_t*)p_this;
271 /* Only forward an store cookies if the corresponding option is activated */
272 bool b_forward_cookies = var_CreateGetBool( p_access, "http-forward-cookies" );
273 vlc_array_t * saved_cookies = b_forward_cookies ? (cookies ? cookies : vlc_array_new()) : NULL;
275 /* Set up p_access */
276 STANDARD_READ_ACCESS_INIT;
278 p_access->pf_read = ReadCompressed;
281 p_sys->b_proxy = false;
282 p_sys->psz_proxy_passbuf = NULL;
283 p_sys->i_version = 1;
284 p_sys->b_seekable = true;
285 p_sys->psz_mime = NULL;
286 p_sys->psz_pragma = NULL;
287 p_sys->b_mms = false;
288 p_sys->b_icecast = false;
289 p_sys->psz_location = NULL;
290 p_sys->psz_user_agent = NULL;
291 p_sys->b_pace_control = true;
292 p_sys->b_ssl = false;
294 p_sys->b_compressed = false;
295 /* 15 is the max windowBits, +32 to enable optional gzip decoding */
296 if( inflateInit2( &p_sys->inflate.stream, 32+15 ) != Z_OK )
297 msg_Warn( p_access, "Error during zlib initialisation: %s",
298 p_sys->inflate.stream.msg );
299 if( zlibCompileFlags() & (1<<17) )
300 msg_Warn( p_access, "Your zlib was compiled without gzip support." );
301 p_sys->inflate.p_buffer = NULL;
305 p_sys->i_icy_meta = 0;
306 p_sys->i_icy_offset = 0;
307 p_sys->psz_icy_name = NULL;
308 p_sys->psz_icy_genre = NULL;
309 p_sys->psz_icy_title = NULL;
310 p_sys->i_remaining = 0;
311 p_sys->b_persist = false;
312 p_sys->b_has_size = false;
313 p_access->info.i_size = 0;
314 p_access->info.i_pos = 0;
315 p_access->info.b_eof = false;
317 p_sys->cookies = saved_cookies;
319 http_auth_Init( &p_sys->auth );
320 http_auth_Init( &p_sys->proxy_auth );
322 /* Parse URI - remove spaces */
323 p = psz = strdup( p_access->psz_path );
324 while( (p = strchr( p, ' ' )) != NULL )
326 vlc_UrlParse( &p_sys->url, psz, 0 );
329 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
331 msg_Warn( p_access, "invalid host" );
334 if( !strncmp( psz_access, "https", 5 ) )
338 if( p_sys->url.i_port <= 0 )
339 p_sys->url.i_port = 443;
343 if( p_sys->url.i_port <= 0 )
344 p_sys->url.i_port = 80;
348 p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
351 psz = var_CreateGetNonEmptyString( p_access, "http-proxy" );
354 p_sys->b_proxy = true;
355 vlc_UrlParse( &p_sys->proxy, psz, 0 );
361 pxProxyFactory *pf = px_proxy_factory_new();
366 i=asprintf(&buf, "%s://%s", psz_access, p_access->psz_path);
369 msg_Dbg(p_access, "asking libproxy about url '%s'", buf);
370 char **proxies = px_proxy_factory_get_proxies(pf, buf);
373 msg_Dbg(p_access, "libproxy suggest to use '%s'", proxies[0]);
374 if(strcmp(proxies[0],"direct://") != 0)
376 p_sys->b_proxy = true;
377 vlc_UrlParse( &p_sys->proxy, proxies[0], 0);
380 for(i=0;proxies[i];i++) free(proxies[i]);
384 px_proxy_factory_free(pf);
388 msg_Err(p_access, "Allocating memory for libproxy failed");
391 #elif defined( WIN32 )
394 if( var_CreateGetBool( p_access, "http-use-IE-proxy" ) )
396 /* Try to get the proxy server address from Windows internet settings using registry. */
399 if( RegOpenKeyEx( HKEY_CURRENT_USER, "Software\\Microsoft" \
400 "\\Windows\\CurrentVersion\\Internet Settings",
401 0, KEY_READ, &h_key ) == ERROR_SUCCESS )
403 DWORD i_dataReadSize = 4; /* sizeof( DWORD ); */
404 DWORD proxyEnable = 0;
405 /* Get the proxy enable value */
406 if( RegQueryValueEx( h_key, "ProxyEnable", NULL, NULL,
407 (char *)&proxyEnable, &i_dataReadSize )
412 /* Proxy is enable */
414 i_dataReadSize = 256;
415 if( RegQueryValueEx( h_key, "ProxyServer",
420 /* Get the proxy URL :
421 Proxy server value in the registry can be something like "address:port"
422 or "ftp=adress1:port1;http=adress2:port2 ..." depending of the
425 psz_proxy = strstr( psz_key, "http=" );
426 if( psz_proxy != NULL )
428 psz_proxy += strlen( "http=" );
429 char *psz_endUrl = strchr( psz_proxy, ';' );
430 if( psz_endUrl != NULL )
435 /* Set proxy enable for this connection. */
436 p_sys->b_proxy = true;
437 vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
439 msg_Warn( p_access, "Couldn't read in registry " \
440 "the proxy server address." );
444 msg_Warn( p_access, "Couldn't read in registry if the " \
445 "proxy is enable or not." );
448 msg_Warn( p_access, "Couldn't open internet settings key " \
455 psz = getenv( "http_proxy" );
458 p_sys->b_proxy = true;
459 vlc_UrlParse( &p_sys->proxy, psz, 0 );
464 if( psz ) /* No, this is NOT a use-after-free error */
466 psz = var_CreateGetNonEmptyString( p_access, "http-proxy-pwd" );
468 p_sys->proxy.psz_password = p_sys->psz_proxy_passbuf = psz;
473 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
475 msg_Warn( p_access, "invalid proxy host" );
478 if( p_sys->proxy.i_port <= 0 )
480 p_sys->proxy.i_port = 80;
484 msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
485 p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
488 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
489 p_sys->proxy.i_port );
491 if( p_sys->url.psz_username && *p_sys->url.psz_username )
493 msg_Dbg( p_access, " user='%s'", p_sys->url.psz_username );
496 p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
497 p_sys->b_continuous = var_CreateGetBool( p_access, "http-continuous" );
501 switch( Connect( p_access, 0 ) )
507 /* Retry with http 1.0 */
508 msg_Dbg( p_access, "switching to HTTP version 1.0" );
509 p_sys->i_version = 0;
510 p_sys->b_seekable = false;
512 if( !vlc_object_alive (p_access) || Connect( p_access, 0 ) )
520 msg_Err( p_access, "You should not be here" );
525 if( p_sys->i_code == 401 )
527 char *psz_login, *psz_password;
529 if( p_sys->url.psz_username && p_sys->url.psz_password &&
530 p_sys->auth.psz_nonce && p_sys->auth.i_nonce == 0 )
532 Disconnect( p_access );
535 msg_Dbg( p_access, "authentication failed for realm %s",
536 p_sys->auth.psz_realm );
537 dialog_Login( p_access, &psz_login, &psz_password,
538 _("HTTP authentication"),
539 _("Please enter a valid login name and a password for realm %s."),
540 p_sys->auth.psz_realm );
541 if( psz_login != NULL && psz_password != NULL )
543 msg_Dbg( p_access, "retrying with user=%s", psz_login );
544 p_sys->url.psz_username = psz_login;
545 p_sys->url.psz_password = psz_password;
546 Disconnect( p_access );
552 free( psz_password );
557 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
558 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
559 p_sys->psz_location && *p_sys->psz_location )
561 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
563 /* Check the number of redirection already done */
564 if( i_nb_redirect >= i_max_redirect )
566 msg_Err( p_access, "Too many redirection: break potential infinite"
572 /* Do not accept redirection outside of HTTP works */
573 const char *psz_protocol;
574 if( !strncmp( p_sys->psz_location, "http:", 5 ) )
575 psz_protocol = "http";
576 else if( !strncmp( p_sys->psz_location, "https:", 6 ) )
577 psz_protocol = "https";
580 msg_Err( p_access, "insecure redirection ignored" );
583 free( p_access->psz_path );
584 p_access->psz_path = strdup( p_sys->psz_location );
585 /* Clean up current Open() run */
586 vlc_UrlClean( &p_sys->url );
587 http_auth_Reset( &p_sys->auth );
588 vlc_UrlClean( &p_sys->proxy );
589 free( p_sys->psz_proxy_passbuf );
590 http_auth_Reset( &p_sys->proxy_auth );
591 free( p_sys->psz_mime );
592 free( p_sys->psz_pragma );
593 free( p_sys->psz_location );
594 free( p_sys->psz_user_agent );
596 Disconnect( p_access );
597 cookies = p_sys->cookies;
599 inflateEnd( &p_sys->inflate.stream );
603 /* Do new Open() run with new data */
604 return OpenWithCookies( p_this, psz_protocol, i_nb_redirect + 1,
605 i_max_redirect, cookies );
610 msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
614 if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
616 if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
618 if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
619 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
621 free( p_access->psz_demux );
622 p_access->psz_demux = strdup( "nsv" );
624 else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
625 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
627 free( p_access->psz_demux );
628 p_access->psz_demux = strdup( "m4a" );
630 else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
632 free( p_access->psz_demux );
633 p_access->psz_demux = strdup( "mp3" );
636 msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
637 p_access->psz_demux );
639 #if 0 /* Doesn't work really well because of the pre-buffering in
640 * shoutcast servers (the buffer content will be sent as fast as
642 p_sys->b_pace_control = false;
645 else if( !p_sys->psz_mime )
647 free( p_access->psz_demux );
649 p_access->psz_demux = strdup( "mp3" );
651 /* else probably Ogg Vorbis */
653 else if( !strcasecmp( psz_access, "unsv" ) &&
655 !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
657 free( p_access->psz_demux );
658 /* Grrrr! detect ultravox server and force NSV demuxer */
659 p_access->psz_demux = strdup( "nsv" );
661 else if( !strcmp( psz_access, "itpc" ) )
663 free( p_access->psz_demux );
664 p_access->psz_demux = strdup( "podcast" );
666 else if( p_sys->psz_mime &&
667 !strncasecmp( p_sys->psz_mime, "application/xspf+xml", 20 ) &&
668 ( memchr( " ;\t", p_sys->psz_mime[20], 4 ) != NULL ) )
670 free( p_access->psz_demux );
671 p_access->psz_demux = strdup( "xspf-open" );
674 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
677 var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
682 vlc_UrlClean( &p_sys->url );
683 vlc_UrlClean( &p_sys->proxy );
684 free( p_sys->psz_proxy_passbuf );
685 free( p_sys->psz_mime );
686 free( p_sys->psz_pragma );
687 free( p_sys->psz_location );
688 free( p_sys->psz_user_agent );
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 );
730 Disconnect( p_access );
735 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
736 free(vlc_array_item_at_index( p_sys->cookies, i ));
737 vlc_array_destroy( p_sys->cookies );
741 inflateEnd( &p_sys->inflate.stream );
742 free( p_sys->inflate.p_buffer );
748 /*****************************************************************************
749 * Read: Read up to i_len bytes from the http connection and place in
750 * p_buffer. Return the actual number of bytes read
751 *****************************************************************************/
752 static int ReadICYMeta( access_t *p_access );
753 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
755 access_sys_t *p_sys = p_access->p_sys;
758 if( p_sys->fd == -1 )
760 p_access->info.b_eof = true;
764 if( p_sys->b_has_size )
766 /* Remaining bytes in the file */
767 uint64_t remainder = p_access->info.i_size - p_access->info.i_pos;
768 if( remainder < i_len )
771 /* Remaining bytes in the response */
772 if( p_sys->i_remaining < i_len )
773 i_len = p_sys->i_remaining;
776 if( p_sys->b_chunked )
778 if( p_sys->i_chunk < 0 )
780 p_access->info.b_eof = true;
784 if( p_sys->i_chunk <= 0 )
786 char *psz = net_Gets( p_access, p_sys->fd, p_sys->p_vs );
787 /* read the chunk header */
790 /* fatal error - end of file */
791 msg_Dbg( p_access, "failed reading chunk-header line" );
794 p_sys->i_chunk = strtoll( psz, NULL, 16 );
797 if( p_sys->i_chunk <= 0 ) /* eof */
800 p_access->info.b_eof = true;
805 if( i_len > p_sys->i_chunk )
806 i_len = p_sys->i_chunk;
811 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 if( p_sys->b_chunked )
838 p_sys->i_chunk -= i_read;
839 if( p_sys->i_chunk <= 0 )
841 /* read the empty line */
842 char *psz = net_Gets( p_access, p_sys->fd, p_sys->p_vs );
850 * I very much doubt that this will work.
851 * If i_read == 0, the connection *IS* dead, so the only
852 * sensible thing to do is Disconnect() and then retry.
853 * Otherwise, I got recv() completely wrong. -- Courmisch
855 if( p_sys->b_continuous )
857 Request( p_access, 0 );
858 p_sys->b_continuous = false;
859 i_read = Read( p_access, p_buffer, i_len );
860 p_sys->b_continuous = true;
862 Disconnect( p_access );
863 if( p_sys->b_reconnect && vlc_object_alive( p_access ) )
865 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
866 if( Connect( p_access, p_access->info.i_pos ) )
868 msg_Dbg( p_access, "reconnection failed" );
872 p_sys->b_reconnect = false;
873 i_read = Read( p_access, p_buffer, i_len );
874 p_sys->b_reconnect = true;
880 p_access->info.b_eof = true;
882 p_sys->b_error = true;
887 assert( i_read >= 0 );
888 p_access->info.i_pos += i_read;
889 if( p_sys->b_has_size )
891 assert( p_access->info.i_pos <= p_access->info.i_size );
892 assert( (unsigned)i_read <= p_sys->i_remaining );
893 p_sys->i_remaining -= i_read;
899 static int ReadICYMeta( access_t *p_access )
901 access_sys_t *p_sys = p_access->p_sys;
907 /* Read meta data length */
908 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
915 i_read = buffer << 4;
916 /* msg_Dbg( p_access, "ICY meta size=%u", i_read); */
918 psz_meta = malloc( i_read + 1 );
919 if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
920 (uint8_t *)psz_meta, i_read, true ) != i_read )
926 psz_meta[i_read] = '\0'; /* Just in case */
928 /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
930 /* Now parse the meta */
931 /* Look for StreamTitle= */
932 p = strcasestr( (char *)psz_meta, "StreamTitle=" );
935 p += strlen( "StreamTitle=" );
936 if( *p == '\'' || *p == '"' )
938 char closing[] = { p[0], ';', '\0' };
939 char *psz = strstr( &p[1], closing );
941 psz = strchr( &p[1], ';' );
943 if( psz ) *psz = '\0';
947 char *psz = strchr( &p[1], ';' );
948 if( psz ) *psz = '\0';
951 if( !p_sys->psz_icy_title ||
952 strcmp( p_sys->psz_icy_title, &p[1] ) )
954 free( p_sys->psz_icy_title );
955 char *psz_tmp = strdup( &p[1] );
956 p_sys->psz_icy_title = EnsureUTF8( psz_tmp );
957 if( !p_sys->psz_icy_title )
959 p_access->info.i_update |= INPUT_UPDATE_META;
961 msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
970 static ssize_t ReadCompressed( access_t *p_access, uint8_t *p_buffer,
973 access_sys_t *p_sys = p_access->p_sys;
975 if( p_sys->b_compressed )
979 if( !p_sys->inflate.p_buffer )
980 p_sys->inflate.p_buffer = malloc( 256 * 1024 );
982 if( p_sys->inflate.stream.avail_in == 0 )
984 ssize_t i_read = Read( p_access, p_sys->inflate.p_buffer + p_sys->inflate.stream.avail_in, 256 * 1024 );
985 if( i_read <= 0 ) return i_read;
986 p_sys->inflate.stream.next_in = p_sys->inflate.p_buffer;
987 p_sys->inflate.stream.avail_in = i_read;
990 p_sys->inflate.stream.avail_out = i_len;
991 p_sys->inflate.stream.next_out = p_buffer;
993 i_ret = inflate( &p_sys->inflate.stream, Z_SYNC_FLUSH );
994 msg_Warn( p_access, "inflate return value: %d, %s", i_ret, p_sys->inflate.stream.msg );
996 return i_len - p_sys->inflate.stream.avail_out;
1000 return Read( p_access, p_buffer, i_len );
1005 /*****************************************************************************
1006 * Seek: close and re-open a connection at the right place
1007 *****************************************************************************/
1008 static int Seek( access_t *p_access, uint64_t i_pos )
1010 msg_Dbg( p_access, "trying to seek to %"PRId64, i_pos );
1012 Disconnect( p_access );
1014 if( p_access->info.i_size
1015 && i_pos >= p_access->info.i_size ) {
1016 msg_Err( p_access, "seek to far" );
1017 int retval = Seek( p_access, p_access->info.i_size - 1 );
1018 if( retval == VLC_SUCCESS ) {
1019 uint8_t p_buffer[2];
1020 Read( p_access, p_buffer, 1);
1021 p_access->info.b_eof = false;
1025 if( Connect( p_access, i_pos ) )
1027 msg_Err( p_access, "seek failed" );
1028 p_access->info.b_eof = true;
1029 return VLC_EGENERIC;
1034 /*****************************************************************************
1036 *****************************************************************************/
1037 static int Control( access_t *p_access, int i_query, va_list args )
1039 access_sys_t *p_sys = p_access->p_sys;
1047 case ACCESS_CAN_SEEK:
1048 pb_bool = (bool*)va_arg( args, bool* );
1049 *pb_bool = p_sys->b_seekable;
1051 case ACCESS_CAN_FASTSEEK:
1052 pb_bool = (bool*)va_arg( args, bool* );
1055 case ACCESS_CAN_PAUSE:
1056 case ACCESS_CAN_CONTROL_PACE:
1057 pb_bool = (bool*)va_arg( args, bool* );
1059 #if 0 /* Disable for now until we have a clock synchro algo
1060 * which works with something else than MPEG over UDP */
1061 *pb_bool = p_sys->b_pace_control;
1067 case ACCESS_GET_PTS_DELAY:
1068 pi_64 = (int64_t*)va_arg( args, int64_t * );
1069 *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
1073 case ACCESS_SET_PAUSE_STATE:
1076 case ACCESS_GET_META:
1077 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
1079 if( p_sys->psz_icy_name )
1080 vlc_meta_Set( p_meta, vlc_meta_Title, p_sys->psz_icy_name );
1081 if( p_sys->psz_icy_genre )
1082 vlc_meta_Set( p_meta, vlc_meta_Genre, p_sys->psz_icy_genre );
1083 if( p_sys->psz_icy_title )
1084 vlc_meta_Set( p_meta, vlc_meta_NowPlaying, p_sys->psz_icy_title );
1087 case ACCESS_GET_CONTENT_TYPE:
1088 *va_arg( args, char ** ) =
1089 p_sys->psz_mime ? strdup( p_sys->psz_mime ) : NULL;
1092 case ACCESS_GET_TITLE_INFO:
1093 case ACCESS_SET_TITLE:
1094 case ACCESS_SET_SEEKPOINT:
1095 case ACCESS_SET_PRIVATE_ID_STATE:
1096 return VLC_EGENERIC;
1099 msg_Warn( p_access, "unimplemented query in control" );
1100 return VLC_EGENERIC;
1106 /*****************************************************************************
1108 *****************************************************************************/
1109 static int Connect( access_t *p_access, uint64_t i_tell )
1111 access_sys_t *p_sys = p_access->p_sys;
1112 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
1115 free( p_sys->psz_location );
1116 free( p_sys->psz_mime );
1117 free( p_sys->psz_pragma );
1119 free( p_sys->psz_icy_genre );
1120 free( p_sys->psz_icy_name );
1121 free( p_sys->psz_icy_title );
1124 p_sys->psz_location = NULL;
1125 p_sys->psz_mime = NULL;
1126 p_sys->psz_pragma = NULL;
1127 p_sys->b_mms = false;
1128 p_sys->b_chunked = false;
1130 p_sys->i_icy_meta = 0;
1131 p_sys->i_icy_offset = i_tell;
1132 p_sys->psz_icy_name = NULL;
1133 p_sys->psz_icy_genre = NULL;
1134 p_sys->psz_icy_title = NULL;
1135 p_sys->i_remaining = 0;
1136 p_sys->b_persist = false;
1137 p_sys->b_has_size = false;
1138 p_access->info.i_size = 0;
1139 p_access->info.i_pos = i_tell;
1140 p_access->info.b_eof = false;
1142 /* Open connection */
1143 assert( p_sys->fd == -1 ); /* No open sockets (leaking fds is BAD) */
1144 p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
1145 if( p_sys->fd == -1 )
1147 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
1150 setsockopt (p_sys->fd, SOL_SOCKET, SO_KEEPALIVE, &(int){ 1 }, sizeof (int));
1152 /* Initialize TLS/SSL session */
1153 if( p_sys->b_ssl == true )
1155 /* CONNECT to establish TLS tunnel through HTTP proxy */
1156 if( p_sys->b_proxy )
1159 unsigned i_status = 0;
1161 if( p_sys->i_version == 0 )
1163 /* CONNECT is not in HTTP/1.0 */
1164 Disconnect( p_access );
1168 net_Printf( p_access, p_sys->fd, NULL,
1169 "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
1170 p_sys->url.psz_host, p_sys->url.i_port,
1172 p_sys->url.psz_host, p_sys->url.i_port);
1174 psz = net_Gets( p_access, p_sys->fd, NULL );
1177 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
1178 Disconnect( p_access );
1182 sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
1185 if( ( i_status / 100 ) != 2 )
1187 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
1188 Disconnect( p_access );
1194 psz = net_Gets( p_access, p_sys->fd, NULL );
1197 msg_Err( p_access, "HTTP proxy connection failed" );
1198 Disconnect( p_access );
1207 if( !vlc_object_alive (p_access) || p_sys->b_error )
1209 Disconnect( p_access );
1216 /* TLS/SSL handshake */
1217 p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
1218 p_sys->url.psz_host );
1219 if( p_sys->p_tls == NULL )
1221 msg_Err( p_access, "cannot establish HTTP/TLS session" );
1222 Disconnect( p_access );
1225 p_sys->p_vs = &p_sys->p_tls->sock;
1228 return Request( p_access, i_tell ) ? -2 : 0;
1232 static int Request( access_t *p_access, uint64_t i_tell )
1234 access_sys_t *p_sys = p_access->p_sys;
1236 v_socket_t *pvs = p_sys->p_vs;
1237 p_sys->b_persist = false;
1239 p_sys->i_remaining = 0;
1240 if( p_sys->b_proxy )
1242 if( p_sys->url.psz_path )
1244 net_Printf( p_access, p_sys->fd, NULL,
1245 "GET http://%s:%d%s HTTP/1.%d\r\n",
1246 p_sys->url.psz_host, p_sys->url.i_port,
1247 p_sys->url.psz_path, p_sys->i_version );
1251 net_Printf( p_access, p_sys->fd, NULL,
1252 "GET http://%s:%d/ HTTP/1.%d\r\n",
1253 p_sys->url.psz_host, p_sys->url.i_port,
1259 const char *psz_path = p_sys->url.psz_path;
1260 if( !psz_path || !*psz_path )
1264 if( p_sys->url.i_port != (pvs ? 443 : 80) )
1266 net_Printf( p_access, p_sys->fd, pvs,
1267 "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
1268 psz_path, p_sys->i_version, p_sys->url.psz_host,
1269 p_sys->url.i_port );
1273 net_Printf( p_access, p_sys->fd, pvs,
1274 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
1275 psz_path, p_sys->i_version, p_sys->url.psz_host );
1279 net_Printf( p_access, p_sys->fd, pvs, "User-Agent: %s\r\n",
1280 p_sys->psz_user_agent );
1282 if( p_sys->i_version == 1 && ! p_sys->b_continuous )
1284 p_sys->b_persist = true;
1285 net_Printf( p_access, p_sys->fd, pvs,
1286 "Range: bytes=%"PRIu64"-\r\n", i_tell );
1290 if( p_sys->cookies )
1293 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
1295 const char * cookie = vlc_array_item_at_index( p_sys->cookies, i );
1296 char * psz_cookie_content = cookie_get_content( cookie );
1297 char * psz_cookie_domain = cookie_get_domain( cookie );
1299 assert( psz_cookie_content );
1301 /* FIXME: This is clearly not conforming to the rfc */
1302 bool is_in_right_domain = (!psz_cookie_domain || strstr( p_sys->url.psz_host, psz_cookie_domain ));
1304 if( is_in_right_domain )
1306 msg_Dbg( p_access, "Sending Cookie %s", psz_cookie_content );
1307 if( net_Printf( p_access, p_sys->fd, pvs, "Cookie: %s\r\n", psz_cookie_content ) < 0 )
1308 msg_Err( p_access, "failed to send Cookie" );
1310 free( psz_cookie_content );
1311 free( psz_cookie_domain );
1315 /* Authentication */
1316 if( p_sys->url.psz_username || p_sys->url.psz_password )
1317 AuthReply( p_access, "", &p_sys->url, &p_sys->auth );
1319 /* Proxy Authentication */
1320 if( p_sys->proxy.psz_username || p_sys->proxy.psz_password )
1321 AuthReply( p_access, "Proxy-", &p_sys->proxy, &p_sys->proxy_auth );
1323 /* ICY meta data request */
1324 net_Printf( p_access, p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
1327 if( net_Printf( p_access, p_sys->fd, pvs, "\r\n" ) < 0 )
1329 msg_Err( p_access, "failed to send request" );
1330 Disconnect( p_access );
1331 return VLC_EGENERIC;
1335 if( ( psz = net_Gets( p_access, p_sys->fd, pvs ) ) == NULL )
1337 msg_Err( p_access, "failed to read answer" );
1340 if( !strncmp( psz, "HTTP/1.", 7 ) )
1342 p_sys->psz_protocol = "HTTP";
1343 p_sys->i_code = atoi( &psz[9] );
1345 else if( !strncmp( psz, "ICY", 3 ) )
1347 p_sys->psz_protocol = "ICY";
1348 p_sys->i_code = atoi( &psz[4] );
1349 p_sys->b_reconnect = true;
1353 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1357 msg_Dbg( p_access, "protocol '%s' answer code %d",
1358 p_sys->psz_protocol, p_sys->i_code );
1359 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1361 p_sys->b_seekable = false;
1363 if( p_sys->i_code != 206 && p_sys->i_code != 401 )
1365 p_sys->b_seekable = false;
1367 /* Authentication error - We'll have to display the dialog */
1368 if( p_sys->i_code == 401 )
1372 /* Other fatal error */
1373 else if( p_sys->i_code >= 400 )
1375 msg_Err( p_access, "error: %s", psz );
1383 char *psz = net_Gets( p_access, p_sys->fd, pvs );
1388 msg_Err( p_access, "failed to read answer" );
1392 if( !vlc_object_alive (p_access) || p_sys->b_error )
1398 /* msg_Dbg( p_input, "Line=%s", psz ); */
1405 if( ( p = strchr( psz, ':' ) ) == NULL )
1407 msg_Err( p_access, "malformed header line: %s", psz );
1412 while( *p == ' ' ) p++;
1414 if( !strcasecmp( psz, "Content-Length" ) )
1416 uint64_t i_size = i_tell + (p_sys->i_remaining = (uint64_t)atoll( p ));
1417 if(i_size > p_access->info.i_size) {
1418 p_sys->b_has_size = true;
1419 p_access->info.i_size = i_size;
1421 msg_Dbg( p_access, "this frame size=%"PRIu64, p_sys->i_remaining );
1423 else if( !strcasecmp( psz, "Content-Range" ) ) {
1424 uint64_t i_ntell = i_tell;
1425 uint64_t i_nend = (p_access->info.i_size > 0)?(p_access->info.i_size - 1):i_tell;
1426 uint64_t i_nsize = p_access->info.i_size;
1427 sscanf(p,"bytes %"SCNu64"-%"SCNu64"/%"SCNu64,&i_ntell,&i_nend,&i_nsize);
1428 if(i_nend > i_ntell ) {
1429 p_access->info.i_pos = i_ntell;
1430 p_sys->i_icy_offset = i_ntell;
1431 p_sys->i_remaining = i_nend+1-i_ntell;
1432 int64_t i_size = (i_nsize > i_nend) ? i_nsize : (i_nend + 1);
1433 if(i_size > p_access->info.i_size) {
1434 p_sys->b_has_size = true;
1435 p_access->info.i_size = i_size;
1437 msg_Dbg( p_access, "stream size=%"PRIu64",pos=%"PRIu64",remaining=%"PRIu64,
1438 i_nsize, i_ntell, p_sys->i_remaining);
1441 else if( !strcasecmp( psz, "Connection" ) ) {
1442 msg_Dbg( p_access, "Connection: %s",p );
1444 sscanf(p, "close%n",&i);
1446 p_sys->b_persist = false;
1449 else if( !strcasecmp( psz, "Location" ) )
1453 /* This does not follow RFC 2068, but yet if the url is not absolute,
1454 * handle it as everyone does. */
1457 const char *psz_http_ext = p_sys->b_ssl ? "s" : "" ;
1459 if( p_sys->url.i_port == ( p_sys->b_ssl ? 443 : 80 ) )
1461 if( asprintf(&psz_new_loc, "http%s://%s%s", psz_http_ext,
1462 p_sys->url.psz_host, p) < 0 )
1467 if( asprintf(&psz_new_loc, "http%s://%s:%d%s", psz_http_ext,
1468 p_sys->url.psz_host, p_sys->url.i_port, p) < 0 )
1474 psz_new_loc = strdup( p );
1477 free( p_sys->psz_location );
1478 p_sys->psz_location = psz_new_loc;
1480 else if( !strcasecmp( psz, "Content-Type" ) )
1482 free( p_sys->psz_mime );
1483 p_sys->psz_mime = strdup( p );
1484 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1486 else if( !strcasecmp( psz, "Content-Encoding" ) )
1488 msg_Dbg( p_access, "Content-Encoding: %s", p );
1489 if( !strcasecmp( p, "identity" ) )
1492 else if( !strcasecmp( p, "gzip" ) || !strcasecmp( p, "deflate" ) )
1493 p_sys->b_compressed = true;
1496 msg_Warn( p_access, "Unknown content coding: %s", p );
1498 else if( !strcasecmp( psz, "Pragma" ) )
1500 if( !strcasecmp( psz, "Pragma: features" ) )
1501 p_sys->b_mms = true;
1502 free( p_sys->psz_pragma );
1503 p_sys->psz_pragma = strdup( p );
1504 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1506 else if( !strcasecmp( psz, "Server" ) )
1508 msg_Dbg( p_access, "Server: %s", p );
1509 if( !strncasecmp( p, "Icecast", 7 ) ||
1510 !strncasecmp( p, "Nanocaster", 10 ) )
1512 /* Remember if this is Icecast
1513 * we need to force demux in this case without breaking
1516 /* Let live 365 streams (nanocaster) piggyback on the icecast
1517 * routine. They look very similar */
1519 p_sys->b_reconnect = true;
1520 p_sys->b_pace_control = false;
1521 p_sys->b_icecast = true;
1524 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1526 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1527 if( !strncasecmp( p, "chunked", 7 ) )
1529 p_sys->b_chunked = true;
1532 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1534 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1535 p_sys->i_icy_meta = atoi( p );
1536 if( p_sys->i_icy_meta < 0 )
1537 p_sys->i_icy_meta = 0;
1538 if( p_sys->i_icy_meta > 0 )
1539 p_sys->b_icecast = true;
1541 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1543 else if( !strcasecmp( psz, "Icy-Name" ) )
1545 free( p_sys->psz_icy_name );
1546 char *psz_tmp = strdup( p );
1547 p_sys->psz_icy_name = EnsureUTF8( psz_tmp );
1548 if( !p_sys->psz_icy_name )
1550 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1552 p_sys->b_icecast = true; /* be on the safeside. set it here as well. */
1553 p_sys->b_reconnect = true;
1554 p_sys->b_pace_control = false;
1556 else if( !strcasecmp( psz, "Icy-Genre" ) )
1558 free( p_sys->psz_icy_genre );
1559 char *psz_tmp = strdup( p );
1560 p_sys->psz_icy_genre = EnsureUTF8( psz_tmp );
1561 if( !p_sys->psz_icy_genre )
1563 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1565 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1567 msg_Dbg( p_access, "Icy-Notice: %s", p );
1569 else if( !strncasecmp( psz, "icy-", 4 ) ||
1570 !strncasecmp( psz, "ice-", 4 ) ||
1571 !strncasecmp( psz, "x-audiocast", 11 ) )
1573 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1575 else if( !strcasecmp( psz, "Set-Cookie" ) )
1577 if( p_sys->cookies )
1579 msg_Dbg( p_access, "Accepting Cookie: %s", p );
1580 cookie_append( p_sys->cookies, strdup(p) );
1583 msg_Dbg( p_access, "We have a Cookie we won't remember: %s", p );
1585 else if( !strcasecmp( psz, "www-authenticate" ) )
1587 msg_Dbg( p_access, "Authentication header: %s", p );
1588 http_auth_ParseWwwAuthenticateHeader( VLC_OBJECT(p_access),
1591 else if( !strcasecmp( psz, "proxy-authenticate" ) )
1593 msg_Dbg( p_access, "Proxy authentication header: %s", p );
1594 http_auth_ParseWwwAuthenticateHeader( VLC_OBJECT(p_access),
1595 &p_sys->proxy_auth, p );
1597 else if( !strcasecmp( psz, "authentication-info" ) )
1599 msg_Dbg( p_access, "Authentication Info header: %s", p );
1600 if( AuthCheckReply( p_access, p, &p_sys->url, &p_sys->auth ) )
1603 else if( !strcasecmp( psz, "proxy-authentication-info" ) )
1605 msg_Dbg( p_access, "Proxy Authentication Info header: %s", p );
1606 if( AuthCheckReply( p_access, p, &p_sys->proxy, &p_sys->proxy_auth ) )
1612 /* We close the stream for zero length data, unless of course the
1613 * server has already promised to do this for us.
1615 if( p_sys->b_has_size && p_sys->i_remaining == 0 && p_sys->b_persist ) {
1616 Disconnect( p_access );
1621 Disconnect( p_access );
1622 return VLC_EGENERIC;
1625 /*****************************************************************************
1627 *****************************************************************************/
1628 static void Disconnect( access_t *p_access )
1630 access_sys_t *p_sys = p_access->p_sys;
1632 if( p_sys->p_tls != NULL)
1634 tls_ClientDelete( p_sys->p_tls );
1635 p_sys->p_tls = NULL;
1638 if( p_sys->fd != -1)
1640 net_Close(p_sys->fd);
1646 /*****************************************************************************
1647 * Cookies (FIXME: we may want to rewrite that using a nice structure to hold
1648 * them) (FIXME: only support the "domain=" param)
1649 *****************************************************************************/
1651 /* Get the NAME=VALUE part of the Cookie */
1652 static char * cookie_get_content( const char * cookie )
1654 char * ret = strdup( cookie );
1655 if( !ret ) return NULL;
1657 /* Look for a ';' */
1658 while( *str && *str != ';' ) str++;
1659 /* Replace it by a end-char */
1660 if( *str == ';' ) *str = 0;
1664 /* Get the domain where the cookie is stored */
1665 static char * cookie_get_domain( const char * cookie )
1667 const char * str = cookie;
1668 static const char domain[] = "domain=";
1671 /* Look for a ';' */
1674 if( !strncmp( str, domain, sizeof(domain) - 1 /* minus \0 */ ) )
1676 str += sizeof(domain) - 1 /* minus \0 */;
1677 char * ret = strdup( str );
1678 /* Now remove the next ';' if present */
1679 char * ret_iter = ret;
1680 while( *ret_iter && *ret_iter != ';' ) ret_iter++;
1681 if( *ret_iter == ';' )
1685 /* Go to next ';' field */
1686 while( *str && *str != ';' ) str++;
1687 if( *str == ';' ) str++;
1689 while( *str && *str == ' ' ) str++;
1694 /* Get NAME in the NAME=VALUE field */
1695 static char * cookie_get_name( const char * cookie )
1697 char * ret = cookie_get_content( cookie ); /* NAME=VALUE */
1698 if( !ret ) return NULL;
1700 while( *str && *str != '=' ) str++;
1705 /* Add a cookie in cookies, checking to see how it should be added */
1706 static void cookie_append( vlc_array_t * cookies, char * cookie )
1713 char * cookie_name = cookie_get_name( cookie );
1715 /* Don't send invalid cookies */
1719 char * cookie_domain = cookie_get_domain( cookie );
1720 for( i = 0; i < vlc_array_count( cookies ); i++ )
1722 char * current_cookie = vlc_array_item_at_index( cookies, i );
1723 char * current_cookie_name = cookie_get_name( current_cookie );
1724 char * current_cookie_domain = cookie_get_domain( current_cookie );
1726 assert( current_cookie_name );
1728 bool is_domain_matching = ( cookie_domain && current_cookie_domain &&
1729 !strcmp( cookie_domain, current_cookie_domain ) );
1731 if( is_domain_matching && !strcmp( cookie_name, current_cookie_name ) )
1733 /* Remove previous value for this cookie */
1734 free( current_cookie );
1735 vlc_array_remove( cookies, i );
1738 free( current_cookie_name );
1739 free( current_cookie_domain );
1742 free( current_cookie_name );
1743 free( current_cookie_domain );
1745 free( cookie_name );
1746 free( cookie_domain );
1747 vlc_array_append( cookies, cookie );
1751 /*****************************************************************************
1752 * HTTP authentication
1753 *****************************************************************************/
1755 static void AuthReply( access_t *p_access, const char *psz_prefix,
1756 vlc_url_t *p_url, http_auth_t *p_auth )
1758 access_sys_t *p_sys = p_access->p_sys;
1762 http_auth_FormatAuthorizationHeader( VLC_OBJECT(p_access), p_auth,
1763 "GET", p_url->psz_path,
1764 p_url->psz_username,
1765 p_url->psz_password );
1766 if ( psz_value == NULL )
1769 net_Printf( p_access, p_sys->fd, p_sys->p_vs,
1770 "%sAuthorization: %s\r\n", psz_prefix, psz_value );
1774 static int AuthCheckReply( access_t *p_access, const char *psz_header,
1775 vlc_url_t *p_url, http_auth_t *p_auth )
1778 http_auth_ParseAuthenticationInfoHeader( VLC_OBJECT(p_access), p_auth,
1781 p_url->psz_username,
1782 p_url->psz_password );