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 *****************************************************************************/
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
38 #include <vlc_access.h>
39 #include <vlc_dialog.h>
41 #include <vlc_network.h>
44 #include <vlc_strings.h>
45 #include <vlc_charset.h>
46 #include <vlc_input.h>
48 #include <vlc_interrupt.h>
57 /*****************************************************************************
59 *****************************************************************************/
60 static int Open ( vlc_object_t * );
61 static void Close( vlc_object_t * );
63 #define PROXY_TEXT N_("HTTP proxy")
64 #define PROXY_LONGTEXT N_( \
65 "HTTP proxy to be used It must be of the form " \
66 "http://[user@]myproxy.mydomain:myport/ ; " \
67 "if empty, the http_proxy environment variable will be tried." )
69 #define PROXY_PASS_TEXT N_("HTTP proxy password")
70 #define PROXY_PASS_LONGTEXT N_( \
71 "If your HTTP proxy requires a password, set it here." )
73 #define RECONNECT_TEXT N_("Auto re-connect")
74 #define RECONNECT_LONGTEXT N_( \
75 "Automatically try to reconnect to the stream in case of a sudden " \
78 #define CONTINUOUS_TEXT N_("Continuous stream")
79 #define CONTINUOUS_LONGTEXT N_("Read a file that is " \
80 "being constantly updated (for example, a JPG file on a server). " \
81 "You should not globally enable this option as it will break all other " \
82 "types of HTTP streams." )
84 #define FORWARD_COOKIES_TEXT N_("Forward Cookies")
85 #define FORWARD_COOKIES_LONGTEXT N_("Forward Cookies across http redirections.")
87 #define REFERER_TEXT N_("HTTP referer value")
88 #define REFERER_LONGTEXT N_("Customize the HTTP referer, simulating a previous document")
90 #define UA_TEXT N_("User Agent")
91 #define UA_LONGTEXT N_("The name and version of the program will be " \
92 "provided to the HTTP server. They must be separated by a forward " \
93 "slash, e.g. FooBar/1.2.3. This option can only be specified per input " \
94 "item, not globally.")
97 set_description( N_("HTTP input") )
98 set_capability( "access", 0 )
99 set_shortname( N_( "HTTP(S)" ) )
100 set_category( CAT_INPUT )
101 set_subcategory( SUBCAT_INPUT_ACCESS )
103 add_string( "http-proxy", NULL, PROXY_TEXT, PROXY_LONGTEXT,
105 add_password( "http-proxy-pwd", NULL,
106 PROXY_PASS_TEXT, PROXY_PASS_LONGTEXT, false )
107 add_obsolete_bool( "http-use-IE-proxy" )
108 add_string( "http-referrer", NULL, REFERER_TEXT, REFERER_LONGTEXT, false )
110 add_string( "http-user-agent", NULL, UA_TEXT, UA_LONGTEXT, false )
113 add_bool( "http-reconnect", false, RECONNECT_TEXT,
114 RECONNECT_LONGTEXT, true )
115 add_bool( "http-continuous", false, CONTINUOUS_TEXT,
116 CONTINUOUS_LONGTEXT, true )
118 add_bool( "http-forward-cookies", true, FORWARD_COOKIES_TEXT,
119 FORWARD_COOKIES_LONGTEXT, true )
120 /* 'itpc' = iTunes Podcast */
121 add_shortcut( "http", "https", "unsv", "itpc", "icyx" )
122 set_callbacks( Open, Close )
125 /*****************************************************************************
127 *****************************************************************************/
133 vlc_tls_creds_t *p_creds;
138 char *psz_user_agent;
145 http_auth_t proxy_auth;
146 char *psz_proxy_passbuf;
150 const char *psz_protocol;
171 uint64_t i_icy_offset;
176 uint64_t i_remaining;
179 /* cookie jar borrowed from playlist, do not free */
180 vlc_http_cookie_jar_t * cookies;
191 static int OpenRedirected( vlc_object_t *p_this, const char *psz_access,
192 unsigned i_redirect );
195 static ssize_t Read( access_t *, uint8_t *, size_t );
196 static ssize_t ReadCompressed( access_t *, uint8_t *, size_t );
197 static int Seek( access_t *, uint64_t );
198 static int Control( access_t *, int, va_list );
201 static int Connect( access_t *, uint64_t );
202 static int Request( access_t *p_access, uint64_t i_tell );
203 static void Disconnect( access_t * );
206 static void AuthReply( access_t *p_acces, const char *psz_prefix,
207 vlc_url_t *p_url, http_auth_t *p_auth );
208 static int AuthCheckReply( access_t *p_access, const char *psz_header,
209 vlc_url_t *p_url, http_auth_t *p_auth );
210 static vlc_http_cookie_jar_t *GetCookieJar( vlc_object_t *p_this );
212 /*****************************************************************************
214 *****************************************************************************/
215 static int Open( vlc_object_t *p_this )
217 access_t *p_access = (access_t*)p_this;
218 return OpenRedirected( p_this, p_access->psz_access, 5 );
222 * Open the given url with limited redirects
223 * @param p_this: the vlc object
224 * @psz_access: the acces to use (http, https, ...) (this value must be used
225 * instead of p_access->psz_access)
226 * @i_redirect: number of redirections remaining
227 * @return vlc error codes
229 static int OpenRedirected( vlc_object_t *p_this, const char *psz_access,
230 unsigned i_redirect )
232 access_t *p_access = (access_t*)p_this;
236 /* Set up p_access */
237 STANDARD_READ_ACCESS_INIT;
239 p_access->pf_read = ReadCompressed;
242 p_sys->b_proxy = false;
243 p_sys->psz_proxy_passbuf = NULL;
244 p_sys->i_version = 1;
245 p_sys->b_seekable = true;
246 p_sys->psz_mime = NULL;
247 p_sys->psz_pragma = NULL;
248 p_sys->b_mms = false;
249 p_sys->b_icecast = false;
250 p_sys->psz_location = NULL;
251 p_sys->psz_user_agent = NULL;
252 p_sys->psz_referrer = NULL;
253 p_sys->b_pace_control = true;
255 p_sys->b_compressed = false;
256 /* 15 is the max windowBits, +32 to enable optional gzip decoding */
257 if( inflateInit2( &p_sys->inflate.stream, 32+15 ) != Z_OK )
258 msg_Warn( p_access, "Error during zlib initialisation: %s",
259 p_sys->inflate.stream.msg );
260 if( zlibCompileFlags() & (1<<17) )
261 msg_Warn( p_access, "Your zlib was compiled without gzip support." );
262 p_sys->inflate.p_buffer = NULL;
265 p_sys->i_icy_meta = 0;
266 p_sys->i_icy_offset = 0;
267 p_sys->psz_icy_name = NULL;
268 p_sys->psz_icy_genre = NULL;
269 p_sys->psz_icy_title = NULL;
270 p_sys->i_remaining = 0;
271 p_sys->b_persist = false;
272 p_sys->b_has_size = false;
274 p_access->info.i_pos = 0;
275 p_access->info.b_eof = false;
277 /* Only forward an store cookies if the corresponding option is activated */
278 if( var_CreateGetBool( p_access, "http-forward-cookies" ) )
279 p_sys->cookies = GetCookieJar( p_this );
281 p_sys->cookies = NULL;
283 http_auth_Init( &p_sys->auth );
284 http_auth_Init( &p_sys->proxy_auth );
286 /* Parse URI - remove spaces */
287 p = psz = strdup( p_access->psz_location );
288 while( (p = strchr( p, ' ' )) != NULL )
290 vlc_UrlParse( &p_sys->url, psz );
293 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
295 msg_Warn( p_access, "invalid host" );
298 if( !strncmp( psz_access, "https", 5 ) )
301 p_sys->p_creds = vlc_tls_ClientCreate( p_this );
302 if( p_sys->p_creds == NULL )
304 if( p_sys->url.i_port <= 0 )
305 p_sys->url.i_port = 443;
309 if( p_sys->url.i_port <= 0 )
310 p_sys->url.i_port = 80;
313 /* Determine the HTTP user agent */
314 /* See RFC2616 §2.2 token and comment definition, and §3.8 and
315 * §14.43 user-agent header */
316 p_sys->psz_user_agent = var_InheritString( p_access, "http-user-agent" );
317 if (p_sys->psz_user_agent)
319 unsigned comment_level = 0;
320 for( char *p = p_sys->psz_user_agent; *p; p++ )
323 if (comment_level == 0)
325 if( c < 32 || strchr( ")<>@,;:\\\"[]?={}", c ) )
326 *p = '_'; /* remove potentially harmful characters */
332 else if( c < 32 && strchr( "\t\r\n", c ) == NULL)
333 *p = '_'; /* remove potentially harmful characters */
337 if (comment_level == UINT_MAX)
342 /* truncate evil unclosed comments */
343 if (comment_level > 0)
345 char *p = strchr(p_sys->psz_user_agent, '(');
351 p_sys->psz_referrer = var_InheritString( p_access, "http-referrer" );
354 psz = var_InheritString( p_access, "http-proxy" );
359 if (likely(asprintf(&url, "%s://%s", psz_access,
360 p_access->psz_location) != -1))
362 msg_Dbg(p_access, "querying proxy for %s", url);
363 psz = vlc_getProxyUrl(url);
368 msg_Dbg(p_access, "proxy: %s", psz);
370 msg_Dbg(p_access, "no proxy");
374 p_sys->b_proxy = true;
375 vlc_UrlParse( &p_sys->proxy, psz );
378 psz = var_InheritString( p_access, "http-proxy-pwd" );
380 p_sys->proxy.psz_password = p_sys->psz_proxy_passbuf = psz;
382 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
384 msg_Warn( p_access, "invalid proxy host" );
387 if( p_sys->proxy.i_port <= 0 )
389 p_sys->proxy.i_port = 80;
393 msg_Dbg( p_access, "http: server='%s' port=%d file='%s'",
394 p_sys->url.psz_host, p_sys->url.i_port,
395 p_sys->url.psz_path != NULL ? p_sys->url.psz_path : "" );
398 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
399 p_sys->proxy.i_port );
401 if( p_sys->url.psz_username && *p_sys->url.psz_username )
403 msg_Dbg( p_access, " user='%s'", p_sys->url.psz_username );
406 p_sys->b_reconnect = var_InheritBool( p_access, "http-reconnect" );
407 p_sys->b_continuous = var_InheritBool( p_access, "http-continuous" );
411 switch( Connect( p_access, 0 ) )
417 /* Retry with http 1.0 */
418 msg_Dbg( p_access, "switching to HTTP version 1.0" );
419 p_sys->i_version = 0;
420 p_sys->b_seekable = false;
422 if( Connect( p_access, 0 ) )
429 vlc_assert_unreachable();
432 if( p_sys->i_code == 401 )
434 if( p_sys->auth.psz_realm == NULL )
436 msg_Err( p_access, "authentication failed without realm" );
439 char *psz_login, *psz_password;
441 if( p_sys->url.psz_username && p_sys->url.psz_password &&
442 p_sys->auth.psz_nonce && p_sys->auth.i_nonce == 0 )
444 Disconnect( p_access );
447 msg_Dbg( p_access, "authentication failed for realm %s",
448 p_sys->auth.psz_realm );
449 dialog_Login( p_access, &psz_login, &psz_password,
450 _("HTTP authentication"),
451 _("Please enter a valid login name and a password for realm %s."),
452 p_sys->auth.psz_realm );
453 if( psz_login != NULL && psz_password != NULL )
455 msg_Dbg( p_access, "retrying with user=%s", psz_login );
456 p_sys->url.psz_username = psz_login;
457 p_sys->url.psz_password = psz_password;
458 Disconnect( p_access );
464 free( psz_password );
469 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
470 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
471 p_sys->psz_location && *p_sys->psz_location )
473 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
475 /* Check the number of redirection already done */
476 if( i_redirect == 0 )
478 msg_Err( p_access, "Too many redirection: break potential infinite"
483 const char *psz_protocol;
484 if( !strncmp( p_sys->psz_location, "http://", 7 ) )
485 psz_protocol = "http";
486 else if( !strncmp( p_sys->psz_location, "https://", 8 ) )
487 psz_protocol = "https";
489 { /* Do not accept redirection outside of HTTP */
490 msg_Err( p_access, "unsupported redirection ignored" );
493 free( p_access->psz_location );
494 p_access->psz_location = strdup( p_sys->psz_location
495 + strlen( psz_protocol ) + 3 );
496 /* Clean up current Open() run */
497 vlc_UrlClean( &p_sys->url );
498 http_auth_Reset( &p_sys->auth );
499 vlc_UrlClean( &p_sys->proxy );
500 free( p_sys->psz_proxy_passbuf );
501 http_auth_Reset( &p_sys->proxy_auth );
502 free( p_sys->psz_mime );
503 free( p_sys->psz_pragma );
504 free( p_sys->psz_location );
505 free( p_sys->psz_user_agent );
506 free( p_sys->psz_referrer );
508 Disconnect( p_access );
509 vlc_tls_Delete( p_sys->p_creds );
511 inflateEnd( &p_sys->inflate.stream );
515 /* Do new Open() run with new data */
516 return OpenRedirected( p_this, psz_protocol, i_redirect - 1 );
521 msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
525 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
530 vlc_UrlClean( &p_sys->url );
531 vlc_UrlClean( &p_sys->proxy );
532 free( p_sys->psz_proxy_passbuf );
533 free( p_sys->psz_mime );
534 free( p_sys->psz_pragma );
535 free( p_sys->psz_location );
536 free( p_sys->psz_user_agent );
537 free( p_sys->psz_referrer );
539 Disconnect( p_access );
540 vlc_tls_Delete( p_sys->p_creds );
543 inflateEnd( &p_sys->inflate.stream );
549 /*****************************************************************************
551 *****************************************************************************/
552 static void Close( vlc_object_t *p_this )
554 access_t *p_access = (access_t*)p_this;
555 access_sys_t *p_sys = p_access->p_sys;
557 vlc_UrlClean( &p_sys->url );
558 http_auth_Reset( &p_sys->auth );
559 vlc_UrlClean( &p_sys->proxy );
560 http_auth_Reset( &p_sys->proxy_auth );
562 free( p_sys->psz_mime );
563 free( p_sys->psz_pragma );
564 free( p_sys->psz_location );
566 free( p_sys->psz_icy_name );
567 free( p_sys->psz_icy_genre );
568 free( p_sys->psz_icy_title );
570 free( p_sys->psz_user_agent );
571 free( p_sys->psz_referrer );
573 Disconnect( p_access );
574 vlc_tls_Delete( p_sys->p_creds );
577 inflateEnd( &p_sys->inflate.stream );
578 free( p_sys->inflate.p_buffer );
584 /* Read data from the socket taking care of chunked transfer if needed */
585 static int ReadData( access_t *p_access, int *pi_read,
586 uint8_t *p_buffer, size_t i_len )
588 access_sys_t *p_sys = p_access->p_sys;
589 if( p_sys->b_chunked )
591 if( p_sys->i_chunk < 0 )
594 if( p_sys->i_chunk <= 0 )
597 if( p_sys->p_tls != NULL )
598 psz = vlc_tls_GetLine( p_sys->p_tls );
600 psz = net_Gets( p_access, p_sys->fd );
601 /* read the chunk header */
604 /* fatal error - end of file */
605 msg_Dbg( p_access, "failed reading chunk-header line" );
608 p_sys->i_chunk = strtoll( psz, NULL, 16 );
611 if( p_sys->i_chunk <= 0 ) /* eof */
618 if( i_len > p_sys->i_chunk )
619 i_len = p_sys->i_chunk;
622 if( p_sys->p_tls != NULL )
623 *pi_read = vlc_tls_Read( p_sys->p_tls, p_buffer, i_len, false );
625 *pi_read = vlc_recv_i11e( p_sys->fd, p_buffer, i_len, 0 );
626 if( *pi_read < 0 && errno != EINTR && errno != EAGAIN )
631 if( p_sys->b_chunked )
633 p_sys->i_chunk -= *pi_read;
634 if( p_sys->i_chunk <= 0 )
635 { /* read the empty line */
636 if( p_sys->p_tls != NULL )
637 free( vlc_tls_GetLine( p_sys->p_tls ) );
639 free( net_Gets( p_access, p_sys->fd ) );
645 /*****************************************************************************
646 * Read: Read up to i_len bytes from the http connection and place in
647 * p_buffer. Return the actual number of bytes read
648 *****************************************************************************/
649 static int ReadICYMeta( access_t *p_access );
650 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
652 access_sys_t *p_sys = p_access->p_sys;
655 if( p_sys->fd == -1 )
658 if( p_sys->b_has_size )
660 /* Remaining bytes in the file */
661 uint64_t remainder = p_sys->size - p_access->info.i_pos;
662 if( remainder < i_len )
665 /* Remaining bytes in the response */
666 if( p_sys->i_remaining < i_len )
667 i_len = p_sys->i_remaining;
672 if( p_sys->i_icy_meta > 0 && p_access->info.i_pos - p_sys->i_icy_offset > 0 )
674 int64_t i_next = p_sys->i_icy_meta -
675 (p_access->info.i_pos - p_sys->i_icy_offset ) % p_sys->i_icy_meta;
677 if( i_next == p_sys->i_icy_meta )
679 if( ReadICYMeta( p_access ) )
686 if( ReadData( p_access, &i_read, p_buffer, i_len ) )
692 * I very much doubt that this will work.
693 * If i_read == 0, the connection *IS* dead, so the only
694 * sensible thing to do is Disconnect() and then retry.
695 * Otherwise, I got recv() completely wrong. -- Courmisch
697 if( p_sys->b_continuous )
699 Request( p_access, 0 );
700 p_sys->b_continuous = false;
701 i_read = Read( p_access, p_buffer, i_len );
702 p_sys->b_continuous = true;
704 Disconnect( p_access );
705 if( p_sys->b_reconnect )
707 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
708 if( Connect( p_access, p_access->info.i_pos ) )
710 msg_Dbg( p_access, "reconnection failed" );
714 p_sys->b_reconnect = false;
715 i_read = Read( p_access, p_buffer, i_len );
716 p_sys->b_reconnect = true;
725 p_sys->b_error = true;
730 assert( i_read >= 0 );
731 p_access->info.i_pos += i_read;
732 if( p_sys->b_has_size )
734 assert( p_access->info.i_pos <= p_sys->size );
735 assert( (unsigned)i_read <= p_sys->i_remaining );
736 p_sys->i_remaining -= i_read;
742 p_access->info.b_eof = true;
746 static int ReadICYMeta( access_t *p_access )
748 access_sys_t *p_sys = p_access->p_sys;
754 /* Read meta data length */
755 if( ReadData( p_access, &i_read, &buffer, 1 ) )
759 const int i_size = buffer << 4;
760 /* msg_Dbg( p_access, "ICY meta size=%u", i_size); */
762 psz_meta = malloc( i_size + 1 );
763 for( i_read = 0; i_read < i_size; )
766 if( ReadData( p_access, &i_tmp, (uint8_t *)&psz_meta[i_read], i_size - i_read ) || i_tmp <= 0 )
773 psz_meta[i_read] = '\0'; /* Just in case */
775 /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
777 /* Now parse the meta */
778 /* Look for StreamTitle= */
779 p = strcasestr( (char *)psz_meta, "StreamTitle=" );
782 p += strlen( "StreamTitle=" );
783 if( *p == '\'' || *p == '"' )
785 char closing[] = { p[0], ';', '\0' };
786 char *psz = strstr( &p[1], closing );
788 psz = strchr( &p[1], ';' );
790 if( psz ) *psz = '\0';
794 char *psz = strchr( &p[1], ';' );
795 if( psz ) *psz = '\0';
798 if( !p_sys->psz_icy_title ||
799 strcmp( p_sys->psz_icy_title, &p[1] ) )
801 free( p_sys->psz_icy_title );
802 char *psz_tmp = strdup( &p[1] );
803 p_sys->psz_icy_title = EnsureUTF8( psz_tmp );
804 if( !p_sys->psz_icy_title )
807 msg_Dbg( p_access, "New Icy-Title=%s", p_sys->psz_icy_title );
808 input_thread_t *p_input = p_access->p_input;
811 input_item_t *p_input_item = input_GetItem( p_access->p_input );
813 input_item_SetMeta( p_input_item, vlc_meta_NowPlaying, p_sys->psz_icy_title );
823 static ssize_t ReadCompressed( access_t *p_access, uint8_t *p_buffer,
826 access_sys_t *p_sys = p_access->p_sys;
828 if( p_sys->b_compressed )
832 if( !p_sys->inflate.p_buffer )
833 p_sys->inflate.p_buffer = malloc( 256 * 1024 );
835 if( p_sys->inflate.stream.avail_in == 0 )
837 ssize_t i_read = Read( p_access, p_sys->inflate.p_buffer, 256 * 1024 );
838 if( i_read <= 0 ) return i_read;
839 p_sys->inflate.stream.next_in = p_sys->inflate.p_buffer;
840 p_sys->inflate.stream.avail_in = i_read;
843 p_sys->inflate.stream.avail_out = i_len;
844 p_sys->inflate.stream.next_out = p_buffer;
846 i_ret = inflate( &p_sys->inflate.stream, Z_SYNC_FLUSH );
847 if ( i_ret != Z_OK && i_ret != Z_STREAM_END )
848 msg_Warn( p_access, "inflate return value: %d, %s", i_ret, p_sys->inflate.stream.msg );
850 return i_len - p_sys->inflate.stream.avail_out;
854 return Read( p_access, p_buffer, i_len );
859 /*****************************************************************************
860 * Seek: close and re-open a connection at the right place
861 *****************************************************************************/
862 static int Seek( access_t *p_access, uint64_t i_pos )
864 access_sys_t *p_sys = p_access->p_sys;
866 msg_Dbg( p_access, "trying to seek to %"PRId64, i_pos );
867 Disconnect( p_access );
869 if( p_sys->size && i_pos >= p_sys->size )
871 msg_Err( p_access, "seek too far" );
872 int retval = Seek( p_access, p_sys->size - 1 );
873 if( retval == VLC_SUCCESS ) {
875 Read( p_access, p_buffer, 1);
876 p_access->info.b_eof = false;
880 if( Connect( p_access, i_pos ) )
882 msg_Err( p_access, "seek failed" );
883 p_access->info.b_eof = true;
889 /*****************************************************************************
891 *****************************************************************************/
892 static int Control( access_t *p_access, int i_query, va_list args )
894 access_sys_t *p_sys = p_access->p_sys;
901 case ACCESS_CAN_SEEK:
902 pb_bool = (bool*)va_arg( args, bool* );
903 *pb_bool = p_sys->b_seekable;
905 case ACCESS_CAN_FASTSEEK:
906 pb_bool = (bool*)va_arg( args, bool* );
909 case ACCESS_CAN_PAUSE:
910 case ACCESS_CAN_CONTROL_PACE:
911 pb_bool = (bool*)va_arg( args, bool* );
913 #if 0 /* Disable for now until we have a clock synchro algo
914 * which works with something else than MPEG over UDP */
915 *pb_bool = p_sys->b_pace_control;
921 case ACCESS_GET_PTS_DELAY:
922 pi_64 = (int64_t*)va_arg( args, int64_t * );
923 *pi_64 = INT64_C(1000)
924 * var_InheritInteger( p_access, "network-caching" );
927 case ACCESS_GET_SIZE:
928 pi_64 = (int64_t*)va_arg( args, int64_t * );
929 *pi_64 = p_sys->size;
933 case ACCESS_SET_PAUSE_STATE:
936 case ACCESS_GET_CONTENT_TYPE:
938 char **type = va_arg( args, char ** );
940 if( p_sys->b_icecast && p_sys->psz_mime == NULL )
941 *type = strdup( "audio/mpeg" );
942 else if( !strcasecmp( p_access->psz_access, "itpc" ) )
943 *type = strdup( "application/rss+xml" );
944 else if( !strcasecmp( p_access->psz_access, "unsv" ) &&
945 p_sys->psz_mime != NULL &&
946 !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
947 /* Grrrr! detect ultravox server and force NSV demuxer */
948 *type = strdup( "video/nsa" );
949 else if( p_sys->psz_mime )
950 *type = strdup( p_sys->psz_mime );
963 static int WriteHeaders( access_t *access, const char *fmt, ... )
965 access_sys_t *sys = access->p_sys;
970 va_start( args, fmt );
971 len = vasprintf( &str, fmt, args );
972 if( likely(len >= 0) )
974 if( ((sys->p_tls != NULL)
975 ? vlc_tls_Write( sys->p_tls, str, len )
976 : net_Write( access, sys->fd, str, len )) < len )
984 /*****************************************************************************
986 *****************************************************************************/
987 static int Connect( access_t *p_access, uint64_t i_tell )
989 access_sys_t *p_sys = p_access->p_sys;
990 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
993 free( p_sys->psz_location );
994 free( p_sys->psz_mime );
995 free( p_sys->psz_pragma );
997 free( p_sys->psz_icy_genre );
998 free( p_sys->psz_icy_name );
999 free( p_sys->psz_icy_title );
1002 p_sys->psz_location = NULL;
1003 p_sys->psz_mime = NULL;
1004 p_sys->psz_pragma = NULL;
1005 p_sys->b_mms = false;
1006 p_sys->b_chunked = false;
1008 p_sys->i_icy_meta = 0;
1009 p_sys->i_icy_offset = i_tell;
1010 p_sys->psz_icy_name = NULL;
1011 p_sys->psz_icy_genre = NULL;
1012 p_sys->psz_icy_title = NULL;
1013 p_sys->i_remaining = 0;
1014 p_sys->b_persist = false;
1015 p_sys->b_has_size = false;
1017 p_access->info.i_pos = i_tell;
1018 p_access->info.b_eof = false;
1020 /* Open connection */
1021 assert( p_sys->fd == -1 ); /* No open sockets (leaking fds is BAD) */
1022 p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
1023 if( p_sys->fd == -1 )
1025 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
1028 setsockopt (p_sys->fd, SOL_SOCKET, SO_KEEPALIVE, &(int){ 1 }, sizeof (int));
1030 /* Initialize TLS/SSL session */
1031 if( p_sys->p_creds != NULL )
1033 /* CONNECT to establish TLS tunnel through HTTP proxy */
1034 if( p_sys->b_proxy )
1037 unsigned i_status = 0;
1039 if( p_sys->i_version == 0 )
1041 /* CONNECT is not in HTTP/1.0 */
1042 Disconnect( p_access );
1046 WriteHeaders( p_access,
1047 "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
1048 p_sys->url.psz_host, p_sys->url.i_port,
1050 p_sys->url.psz_host, p_sys->url.i_port);
1052 psz = net_Gets( p_access, p_sys->fd );
1055 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
1056 Disconnect( p_access );
1060 sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
1063 if( ( i_status / 100 ) != 2 )
1065 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
1066 Disconnect( p_access );
1072 psz = net_Gets( p_access, p_sys->fd );
1075 msg_Err( p_access, "HTTP proxy connection failed" );
1076 Disconnect( p_access );
1085 if( p_sys->b_error )
1087 Disconnect( p_access );
1094 /* TLS/SSL handshake */
1095 const char *alpn[] = { "http/1.1", NULL };
1097 p_sys->p_tls = vlc_tls_ClientSessionCreate( p_sys->p_creds, p_sys->fd,
1098 p_sys->url.psz_host, "https",
1099 p_sys->i_version ? alpn : NULL, NULL );
1100 if( p_sys->p_tls == NULL )
1102 msg_Err( p_access, "cannot establish HTTP/TLS session" );
1103 Disconnect( p_access );
1108 return Request( p_access, i_tell ) ? -2 : 0;
1112 static int Request( access_t *p_access, uint64_t i_tell )
1114 access_sys_t *p_sys = p_access->p_sys;
1116 p_sys->b_persist = false;
1118 p_sys->i_remaining = 0;
1120 const char *psz_path = p_sys->url.psz_path;
1121 if( !psz_path || !*psz_path )
1123 if( p_sys->b_proxy && p_sys->p_tls == NULL )
1124 WriteHeaders( p_access, "GET http://%s:%d%s%s%s HTTP/1.%d\r\n",
1125 p_sys->url.psz_host, p_sys->url.i_port,
1126 psz_path, p_sys->url.psz_option ? "?" : "",
1127 p_sys->url.psz_option ? p_sys->url.psz_option : "",
1130 WriteHeaders( p_access, "GET %s%s%s HTTP/1.%d\r\n",
1131 psz_path, p_sys->url.psz_option ? "?" : "",
1132 p_sys->url.psz_option ? p_sys->url.psz_option : "",
1134 if( p_sys->url.i_port != (p_sys->p_tls ? 443 : 80) )
1135 WriteHeaders( p_access, "Host: %s:%d\r\n",
1136 p_sys->url.psz_host, p_sys->url.i_port );
1138 WriteHeaders( p_access, "Host: %s\r\n", p_sys->url.psz_host );
1140 WriteHeaders( p_access, "User-Agent: %s\r\n", p_sys->psz_user_agent );
1142 if (p_sys->psz_referrer)
1143 WriteHeaders( p_access, "Referer: %s\r\n", p_sys->psz_referrer );
1145 if( p_sys->i_version == 1 && ! p_sys->b_continuous )
1147 p_sys->b_persist = true;
1148 WriteHeaders( p_access, "Range: bytes=%"PRIu64"-\r\n", i_tell );
1149 WriteHeaders( p_access, "Connection: close\r\n" );
1153 if( p_sys->cookies )
1155 char * psz_cookiestring = vlc_http_cookies_for_url( p_sys->cookies, &p_sys->url );
1156 if ( psz_cookiestring )
1158 msg_Dbg( p_access, "Sending Cookie %s", psz_cookiestring );
1159 WriteHeaders( p_access, "Cookie: %s\r\n", psz_cookiestring );
1160 free( psz_cookiestring );
1164 /* Authentication */
1165 if( p_sys->url.psz_username && p_sys->url.psz_password )
1166 AuthReply( p_access, "", &p_sys->url, &p_sys->auth );
1168 /* Proxy Authentication */
1169 if( p_sys->proxy.psz_username && p_sys->proxy.psz_password )
1170 AuthReply( p_access, "Proxy-", &p_sys->proxy, &p_sys->proxy_auth );
1172 /* ICY meta data request */
1173 WriteHeaders( p_access, "Icy-MetaData: 1\r\n" );
1175 if( WriteHeaders( p_access, "\r\n" ) < 0 )
1177 msg_Err( p_access, "failed to send request" );
1178 Disconnect( p_access );
1179 return VLC_EGENERIC;
1183 if( p_sys->p_tls != NULL )
1184 psz = vlc_tls_GetLine( p_sys->p_tls );
1186 psz = net_Gets( p_access, p_sys->fd );
1189 msg_Err( p_access, "failed to read answer" );
1192 if( !strncmp( psz, "HTTP/1.", 7 ) )
1194 p_sys->psz_protocol = "HTTP";
1195 p_sys->i_code = atoi( &psz[9] );
1197 else if( !strncmp( psz, "ICY", 3 ) )
1199 p_sys->psz_protocol = "ICY";
1200 p_sys->i_code = atoi( &psz[4] );
1201 p_sys->b_icecast = true;
1202 p_sys->b_reconnect = true;
1206 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1210 msg_Dbg( p_access, "protocol '%s' answer code %d",
1211 p_sys->psz_protocol, p_sys->i_code );
1212 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1214 p_sys->b_seekable = false;
1216 if( p_sys->i_code != 206 && p_sys->i_code != 401 )
1218 p_sys->b_seekable = false;
1220 /* Authentication error - We'll have to display the dialog */
1221 if( p_sys->i_code == 401 )
1225 /* Other fatal error */
1226 else if( p_sys->i_code >= 400 )
1228 msg_Err( p_access, "error: %s", psz );
1236 char *psz, *p, *p_trailing;
1238 if( p_sys->p_tls != NULL )
1239 psz = vlc_tls_GetLine( p_sys->p_tls );
1241 psz = net_Gets( p_access, p_sys->fd );
1244 msg_Err( p_access, "failed to read answer" );
1248 if( p_sys->b_error )
1254 /* msg_Dbg( p_input, "Line=%s", psz ); */
1261 if( ( p = strchr( psz, ':' ) ) == NULL )
1263 msg_Err( p_access, "malformed header line: %s", psz );
1268 p += strspn( p, " \t" );
1270 /* trim trailing white space */
1271 p_trailing = p + strlen( p );
1272 if( p_trailing > p )
1275 while( ( *p_trailing == ' ' || *p_trailing == '\t' ) && p_trailing > p )
1282 if( !strcasecmp( psz, "Content-Length" ) )
1284 uint64_t i_size = i_tell + (p_sys->i_remaining = (uint64_t)atoll( p ));
1285 if(i_size > p_sys->size) {
1286 p_sys->b_has_size = true;
1287 p_sys->size = i_size;
1289 msg_Dbg( p_access, "this frame size=%"PRIu64, p_sys->i_remaining );
1291 else if( !strcasecmp( psz, "Content-Range" ) ) {
1292 uint64_t i_ntell = i_tell;
1293 uint64_t i_nend = (p_sys->size > 0) ? (p_sys->size - 1) : i_tell;
1294 uint64_t i_nsize = p_sys->size;
1295 sscanf(p,"bytes %"SCNu64"-%"SCNu64"/%"SCNu64,&i_ntell,&i_nend,&i_nsize);
1296 if(i_nend > i_ntell ) {
1297 p_access->info.i_pos = i_ntell;
1298 p_sys->i_icy_offset = i_ntell;
1299 p_sys->i_remaining = i_nend+1-i_ntell;
1300 uint64_t i_size = (i_nsize > i_nend) ? i_nsize : (i_nend + 1);
1301 if(i_size > p_sys->size) {
1302 p_sys->b_has_size = true;
1303 p_sys->size = i_size;
1305 msg_Dbg( p_access, "stream size=%"PRIu64",pos=%"PRIu64",remaining=%"PRIu64,
1306 i_nsize, i_ntell, p_sys->i_remaining);
1309 else if( !strcasecmp( psz, "Connection" ) ) {
1310 msg_Dbg( p_access, "Connection: %s",p );
1312 sscanf(p, "close%n",&i);
1314 p_sys->b_persist = false;
1317 else if( !strcasecmp( psz, "Location" ) )
1321 /* This does not follow RFC 2068, but yet if the url is not absolute,
1322 * handle it as everyone does. */
1325 const char *psz_http_ext = p_sys->p_tls ? "s" : "" ;
1327 if( p_sys->url.i_port == ( p_sys->p_tls ? 443 : 80 ) )
1329 if( asprintf(&psz_new_loc, "http%s://%s%s", psz_http_ext,
1330 p_sys->url.psz_host, p) < 0 )
1335 if( asprintf(&psz_new_loc, "http%s://%s:%d%s", psz_http_ext,
1336 p_sys->url.psz_host, p_sys->url.i_port, p) < 0 )
1342 psz_new_loc = strdup( p );
1345 free( p_sys->psz_location );
1346 p_sys->psz_location = psz_new_loc;
1348 else if( !strcasecmp( psz, "Content-Type" ) )
1350 free( p_sys->psz_mime );
1351 p_sys->psz_mime = strdup( p );
1352 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1354 else if( !strcasecmp( psz, "Content-Encoding" ) )
1356 msg_Dbg( p_access, "Content-Encoding: %s", p );
1357 if( !strcasecmp( p, "identity" ) )
1360 else if( !strcasecmp( p, "gzip" ) || !strcasecmp( p, "deflate" ) )
1361 p_sys->b_compressed = true;
1364 msg_Warn( p_access, "Unknown content coding: %s", p );
1366 else if( !strcasecmp( psz, "Pragma" ) )
1368 if( !strcasecmp( psz, "Pragma: features" ) )
1369 p_sys->b_mms = true;
1370 free( p_sys->psz_pragma );
1371 p_sys->psz_pragma = strdup( p );
1372 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1374 else if( !strcasecmp( psz, "Server" ) )
1376 msg_Dbg( p_access, "Server: %s", p );
1377 if( !strncasecmp( p, "Icecast", 7 ) ||
1378 !strncasecmp( p, "Nanocaster", 10 ) )
1380 /* Remember if this is Icecast
1381 * we need to force demux in this case without breaking
1384 /* Let live 365 streams (nanocaster) piggyback on the icecast
1385 * routine. They look very similar */
1387 p_sys->b_reconnect = true;
1388 p_sys->b_pace_control = false;
1389 p_sys->b_icecast = true;
1392 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1394 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1395 if( !strncasecmp( p, "chunked", 7 ) )
1397 p_sys->b_chunked = true;
1400 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1402 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1403 p_sys->i_icy_meta = atoi( p );
1404 if( p_sys->i_icy_meta < 0 )
1405 p_sys->i_icy_meta = 0;
1406 if( p_sys->i_icy_meta > 0 )
1407 p_sys->b_icecast = true;
1409 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1411 else if( !strcasecmp( psz, "Icy-Name" ) )
1413 free( p_sys->psz_icy_name );
1414 char *psz_tmp = strdup( p );
1415 p_sys->psz_icy_name = EnsureUTF8( psz_tmp );
1416 if( !p_sys->psz_icy_name )
1419 resolve_xml_special_chars( p_sys->psz_icy_name );
1420 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1421 input_thread_t *p_input = p_access->p_input;
1424 input_item_t *p_input_item = input_GetItem( p_access->p_input );
1426 input_item_SetMeta( p_input_item, vlc_meta_Title, p_sys->psz_icy_name );
1429 p_sys->b_icecast = true; /* be on the safeside. set it here as well. */
1430 p_sys->b_reconnect = true;
1431 p_sys->b_pace_control = false;
1433 else if( !strcasecmp( psz, "Icy-Genre" ) )
1435 free( p_sys->psz_icy_genre );
1436 char *psz_tmp = strdup( p );
1437 p_sys->psz_icy_genre = EnsureUTF8( psz_tmp );
1438 if( !p_sys->psz_icy_genre )
1441 resolve_xml_special_chars( p_sys->psz_icy_genre );
1442 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1443 input_thread_t *p_input = p_access->p_input;
1446 input_item_t *p_input_item = input_GetItem( p_access->p_input );
1448 input_item_SetMeta( p_input_item, vlc_meta_Genre, p_sys->psz_icy_genre );
1451 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1453 msg_Dbg( p_access, "Icy-Notice: %s", p );
1455 else if( !strncasecmp( psz, "icy-", 4 ) ||
1456 !strncasecmp( psz, "ice-", 4 ) ||
1457 !strncasecmp( psz, "x-audiocast", 11 ) )
1459 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1461 else if( !strcasecmp( psz, "Set-Cookie" ) )
1463 if( p_sys->cookies )
1465 if ( vlc_http_cookies_append( p_sys->cookies, p, &p_sys->url ) )
1466 msg_Dbg( p_access, "Accepting Cookie: %s", p );
1468 msg_Dbg( p_access, "Rejected Cookie: %s", p );
1471 msg_Dbg( p_access, "We have a Cookie we won't remember: %s", p );
1473 else if( !strcasecmp( psz, "www-authenticate" ) )
1475 msg_Dbg( p_access, "Authentication header: %s", p );
1476 http_auth_ParseWwwAuthenticateHeader( VLC_OBJECT(p_access),
1479 else if( !strcasecmp( psz, "proxy-authenticate" ) )
1481 msg_Dbg( p_access, "Proxy authentication header: %s", p );
1482 http_auth_ParseWwwAuthenticateHeader( VLC_OBJECT(p_access),
1483 &p_sys->proxy_auth, p );
1485 else if( !strcasecmp( psz, "authentication-info" ) )
1487 msg_Dbg( p_access, "Authentication Info header: %s", p );
1488 if( AuthCheckReply( p_access, p, &p_sys->url, &p_sys->auth ) )
1491 else if( !strcasecmp( psz, "proxy-authentication-info" ) )
1493 msg_Dbg( p_access, "Proxy Authentication Info header: %s", p );
1494 if( AuthCheckReply( p_access, p, &p_sys->proxy, &p_sys->proxy_auth ) )
1497 else if( !strcasecmp( psz, "Accept-Ranges" ) )
1499 if( !strcasecmp( p, "bytes" ) )
1500 p_sys->b_seekable = true;
1505 /* We close the stream for zero length data, unless of course the
1506 * server has already promised to do this for us.
1508 if( p_sys->b_has_size && p_sys->i_remaining == 0 && p_sys->b_persist ) {
1509 Disconnect( p_access );
1514 Disconnect( p_access );
1515 return VLC_EGENERIC;
1518 /*****************************************************************************
1520 *****************************************************************************/
1521 static void Disconnect( access_t *p_access )
1523 access_sys_t *p_sys = p_access->p_sys;
1525 if( p_sys->p_tls != NULL)
1527 vlc_tls_SessionDelete( p_sys->p_tls );
1528 p_sys->p_tls = NULL;
1530 if( p_sys->fd != -1)
1532 net_Close(p_sys->fd);
1538 /*****************************************************************************
1539 * HTTP authentication
1540 *****************************************************************************/
1542 static void AuthReply( access_t *p_access, const char *psz_prefix,
1543 vlc_url_t *p_url, http_auth_t *p_auth )
1548 http_auth_FormatAuthorizationHeader( VLC_OBJECT(p_access), p_auth,
1549 "GET", p_url->psz_path,
1550 p_url->psz_username,
1551 p_url->psz_password );
1552 if ( psz_value == NULL )
1555 WriteHeaders( p_access, "%sAuthorization: %s\r\n", psz_prefix, psz_value );
1559 static int AuthCheckReply( access_t *p_access, const char *psz_header,
1560 vlc_url_t *p_url, http_auth_t *p_auth )
1563 http_auth_ParseAuthenticationInfoHeader( VLC_OBJECT(p_access), p_auth,
1566 p_url->psz_username,
1567 p_url->psz_password );
1570 /*****************************************************************************
1572 *****************************************************************************/
1575 * Inherit the cookie jar from the playlist
1577 * @param p_this: http access object
1578 * @return A borrowed reference to a vlc_http_cookie_jar_t, do not free
1580 static vlc_http_cookie_jar_t *GetCookieJar( vlc_object_t *p_this )
1584 if ( var_Inherit( p_this, "http-cookies", VLC_VAR_ADDRESS, &val ) == VLC_SUCCESS )
1585 return val.p_address;