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>
59 /*****************************************************************************
61 *****************************************************************************/
62 static int Open ( vlc_object_t * );
63 static void Close( vlc_object_t * );
65 #define PROXY_TEXT N_("HTTP proxy")
66 #define PROXY_LONGTEXT N_( \
67 "HTTP proxy to be used It must be of the form " \
68 "http://[user@]myproxy.mydomain:myport/ ; " \
69 "if empty, the http_proxy environment variable will be tried." )
71 #define PROXY_PASS_TEXT N_("HTTP proxy password")
72 #define PROXY_PASS_LONGTEXT N_( \
73 "If your HTTP proxy requires a password, set it here." )
75 #define CACHING_TEXT N_("Caching value in ms")
76 #define CACHING_LONGTEXT N_( \
77 "Caching value for HTTP streams. This " \
78 "value should be set in milliseconds." )
80 #define AGENT_TEXT N_("HTTP user agent")
81 #define AGENT_LONGTEXT N_("User agent that will be " \
82 "used for the connection.")
84 #define RECONNECT_TEXT N_("Auto re-connect")
85 #define RECONNECT_LONGTEXT N_( \
86 "Automatically try to reconnect to the stream in case of a sudden " \
89 #define CONTINUOUS_TEXT N_("Continuous stream")
90 #define CONTINUOUS_LONGTEXT N_("Read a file that is " \
91 "being constantly updated (for example, a JPG file on a server). " \
92 "You should not globally enable this option as it will break all other " \
93 "types of HTTP streams." )
95 #define FORWARD_COOKIES_TEXT N_("Forward Cookies")
96 #define FORWARD_COOKIES_LONGTEXT N_("Forward Cookies across http redirections ")
99 set_description( N_("HTTP input") )
100 set_capability( "access", 0 )
101 set_shortname( N_( "HTTP(S)" ) )
102 set_category( CAT_INPUT )
103 set_subcategory( SUBCAT_INPUT_ACCESS )
105 add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
107 add_password( "http-proxy-pwd", NULL, NULL,
108 PROXY_PASS_TEXT, PROXY_PASS_LONGTEXT, false )
109 add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
110 CACHING_TEXT, CACHING_LONGTEXT, true )
112 add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
113 AGENT_LONGTEXT, true )
114 add_bool( "http-reconnect", 0, NULL, RECONNECT_TEXT,
115 RECONNECT_LONGTEXT, true )
116 add_bool( "http-continuous", 0, NULL, CONTINUOUS_TEXT,
117 CONTINUOUS_LONGTEXT, true )
119 add_bool( "http-forward-cookies", true, NULL, FORWARD_COOKIES_TEXT,
120 FORWARD_COOKIES_LONGTEXT, true )
121 add_obsolete_string("http-user")
122 add_obsolete_string("http-pwd")
123 add_shortcut( "http" )
124 add_shortcut( "https" )
125 add_shortcut( "unsv" )
126 add_shortcut( "itpc" ) /* iTunes Podcast */
127 add_shortcut( "icyx" )
128 set_callbacks( Open, Close )
131 /*****************************************************************************
133 *****************************************************************************/
135 /* RFC 2617: Basic and Digest Access Authentication */
136 typedef struct http_auth_t
147 char *psz_HA1; /* stored H(A1) value if algorithm = "MD5-sess" */
153 tls_session_t *p_tls;
158 char *psz_user_agent;
164 http_auth_t proxy_auth;
165 char *psz_proxy_passbuf;
169 const char *psz_protocol;
191 int64_t i_icy_offset;
204 vlc_array_t * cookies;
208 static int OpenWithCookies( vlc_object_t *p_this, vlc_array_t *cookies );
211 static ssize_t Read( access_t *, uint8_t *, size_t );
212 static ssize_t ReadCompressed( access_t *, uint8_t *, size_t );
213 static int Seek( access_t *, int64_t );
214 static int Control( access_t *, int, va_list );
217 static int Connect( access_t *, int64_t );
218 static int Request( access_t *p_access, int64_t i_tell );
219 static void Disconnect( access_t * );
221 /* Small Cookie utilities. Cookies support is partial. */
222 static char * cookie_get_content( const char * cookie );
223 static char * cookie_get_domain( const char * cookie );
224 static char * cookie_get_name( const char * cookie );
225 static void cookie_append( vlc_array_t * cookies, char * cookie );
228 static void AuthParseHeader( access_t *p_access, const char *psz_header,
229 http_auth_t *p_auth );
230 static void AuthReply( access_t *p_acces, const char *psz_prefix,
231 vlc_url_t *p_url, http_auth_t *p_auth );
232 static int AuthCheckReply( access_t *p_access, const char *psz_header,
233 vlc_url_t *p_url, http_auth_t *p_auth );
234 static void AuthReset( http_auth_t *p_auth );
236 /*****************************************************************************
238 *****************************************************************************/
239 static int Open( vlc_object_t *p_this )
241 return OpenWithCookies( p_this, NULL );
244 static int OpenWithCookies( vlc_object_t *p_this, vlc_array_t *cookies )
246 access_t *p_access = (access_t*)p_this;
249 /* Only forward an store cookies if the corresponding option is activated */
250 bool b_forward_cookies = var_CreateGetBool( p_access, "http-forward-cookies" );
251 vlc_array_t * saved_cookies = b_forward_cookies ? (cookies ? cookies : vlc_array_new()) : NULL;
253 /* Set up p_access */
254 STANDARD_READ_ACCESS_INIT;
256 p_access->pf_read = ReadCompressed;
259 p_sys->b_proxy = false;
260 p_sys->psz_proxy_passbuf = NULL;
261 p_sys->i_version = 1;
262 p_sys->b_seekable = true;
263 p_sys->psz_mime = NULL;
264 p_sys->psz_pragma = NULL;
265 p_sys->b_mms = false;
266 p_sys->b_icecast = false;
267 p_sys->psz_location = NULL;
268 p_sys->psz_user_agent = NULL;
269 p_sys->b_pace_control = true;
270 p_sys->b_ssl = false;
272 p_sys->b_compressed = false;
273 /* 15 is the max windowBits, +32 to enable optional gzip decoding */
274 if( inflateInit2( &p_sys->inflate.stream, 32+15 ) != Z_OK )
275 msg_Warn( p_access, "Error during zlib initialisation: %s",
276 p_sys->inflate.stream.msg );
277 if( zlibCompileFlags() & (1<<17) )
278 msg_Warn( p_access, "Your zlib was compiled without gzip support." );
279 p_sys->inflate.p_buffer = NULL;
283 p_sys->i_icy_meta = 0;
284 p_sys->i_icy_offset = 0;
285 p_sys->psz_icy_name = NULL;
286 p_sys->psz_icy_genre = NULL;
287 p_sys->psz_icy_title = NULL;
288 p_sys->i_remaining = 0;
289 p_sys->b_persist = false;
290 p_access->info.i_size = -1;
291 p_access->info.i_pos = 0;
292 p_access->info.b_eof = false;
294 p_sys->cookies = saved_cookies;
296 /* Parse URI - remove spaces */
297 p = psz = strdup( p_access->psz_path );
298 while( (p = strchr( p, ' ' )) != NULL )
300 vlc_UrlParse( &p_sys->url, psz, 0 );
303 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
305 msg_Warn( p_access, "invalid host" );
308 if( !strncmp( p_access->psz_access, "https", 5 ) )
312 if( p_sys->url.i_port <= 0 )
313 p_sys->url.i_port = 443;
317 if( p_sys->url.i_port <= 0 )
318 p_sys->url.i_port = 80;
322 p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
325 psz = var_CreateGetNonEmptyString( p_access, "http-proxy" );
328 p_sys->b_proxy = true;
329 vlc_UrlParse( &p_sys->proxy, psz, 0 );
335 pxProxyFactory *pf = px_proxy_factory_new();
340 i=asprintf(&buf, "%s://%s", p_access->psz_access, p_access->psz_path);
343 msg_Dbg(p_access, "asking libproxy about url '%s'", buf);
344 char **proxies = px_proxy_factory_get_proxies(pf, buf);
347 msg_Dbg(p_access, "libproxy suggest to use '%s'", proxies[0]);
348 if(strcmp(proxies[0],"direct://") != 0)
350 p_sys->b_proxy = true;
351 vlc_UrlParse( &p_sys->proxy, proxies[0], 0);
354 for(i=0;proxies[i];i++) free(proxies[i]);
358 px_proxy_factory_free(pf);
362 msg_Err(p_access, "Allocating memory for libproxy failed");
368 psz = getenv( "http_proxy" );
371 p_sys->b_proxy = true;
372 vlc_UrlParse( &p_sys->proxy, psz, 0 );
376 if( psz ) /* No, this is NOT a use-after-free error */
378 psz = var_CreateGetNonEmptyString( p_access, "http-proxy-pwd" );
380 p_sys->proxy.psz_password = p_sys->psz_proxy_passbuf = psz;
385 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
387 msg_Warn( p_access, "invalid proxy host" );
390 if( p_sys->proxy.i_port <= 0 )
392 p_sys->proxy.i_port = 80;
396 msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
397 p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
400 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
401 p_sys->proxy.i_port );
403 if( p_sys->url.psz_username && *p_sys->url.psz_username )
405 msg_Dbg( p_access, " user='%s'", p_sys->url.psz_username );
408 p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
409 p_sys->b_continuous = var_CreateGetBool( p_access, "http-continuous" );
413 switch( Connect( p_access, 0 ) )
419 /* Retry with http 1.0 */
420 msg_Dbg( p_access, "switching to HTTP version 1.0" );
421 p_sys->i_version = 0;
422 p_sys->b_seekable = false;
424 if( !vlc_object_alive (p_access) || Connect( p_access, 0 ) )
432 msg_Err( p_access, "You should not be here" );
437 if( p_sys->i_code == 401 )
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 /* Do not accept redirection outside of HTTP works */
476 if( strncmp( p_sys->psz_location, "http", 4 )
477 || ( ( p_sys->psz_location[4] != ':' ) /* HTTP */
478 && strncmp( p_sys->psz_location + 4, "s:", 2 ) /* HTTP/SSL */ ) )
480 msg_Err( p_access, "insecure redirection ignored" );
483 free( p_access->psz_path );
484 p_access->psz_path = strdup( p_sys->psz_location );
485 /* Clean up current Open() run */
486 vlc_UrlClean( &p_sys->url );
487 AuthReset( &p_sys->auth );
488 vlc_UrlClean( &p_sys->proxy );
489 free( p_sys->psz_proxy_passbuf );
490 AuthReset( &p_sys->proxy_auth );
491 free( p_sys->psz_mime );
492 free( p_sys->psz_pragma );
493 free( p_sys->psz_location );
494 free( p_sys->psz_user_agent );
496 Disconnect( p_access );
497 cookies = p_sys->cookies;
500 /* Do new Open() run with new data */
501 return OpenWithCookies( p_this, cookies );
506 msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
510 if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
512 if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
514 if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
515 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
517 free( p_access->psz_demux );
518 p_access->psz_demux = strdup( "nsv" );
520 else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
521 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
523 free( p_access->psz_demux );
524 p_access->psz_demux = strdup( "m4a" );
526 else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
528 free( p_access->psz_demux );
529 p_access->psz_demux = strdup( "mp3" );
532 msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
533 p_access->psz_demux );
535 #if 0 /* Doesn't work really well because of the pre-buffering in
536 * shoutcast servers (the buffer content will be sent as fast as
538 p_sys->b_pace_control = false;
541 else if( !p_sys->psz_mime )
543 free( p_access->psz_demux );
545 p_access->psz_demux = strdup( "mp3" );
547 /* else probably Ogg Vorbis */
549 else if( !strcasecmp( p_access->psz_access, "unsv" ) &&
551 !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
553 free( p_access->psz_demux );
554 /* Grrrr! detect ultravox server and force NSV demuxer */
555 p_access->psz_demux = strdup( "nsv" );
557 else if( !strcmp( p_access->psz_access, "itpc" ) )
559 free( p_access->psz_demux );
560 p_access->psz_demux = strdup( "podcast" );
562 else if( p_sys->psz_mime &&
563 !strncasecmp( p_sys->psz_mime, "application/xspf+xml", 20 ) &&
564 ( memchr( " ;\t", p_sys->psz_mime[20], 4 ) != NULL ) )
566 free( p_access->psz_demux );
567 p_access->psz_demux = strdup( "xspf-open" );
570 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
573 var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
578 vlc_UrlClean( &p_sys->url );
579 vlc_UrlClean( &p_sys->proxy );
580 free( p_sys->psz_proxy_passbuf );
581 free( p_sys->psz_mime );
582 free( p_sys->psz_pragma );
583 free( p_sys->psz_location );
584 free( p_sys->psz_user_agent );
586 Disconnect( p_access );
591 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
592 free(vlc_array_item_at_index( p_sys->cookies, i ));
593 vlc_array_destroy( p_sys->cookies );
597 inflateEnd( &p_sys->inflate.stream );
603 /*****************************************************************************
605 *****************************************************************************/
606 static void Close( vlc_object_t *p_this )
608 access_t *p_access = (access_t*)p_this;
609 access_sys_t *p_sys = p_access->p_sys;
611 vlc_UrlClean( &p_sys->url );
612 AuthReset( &p_sys->auth );
613 vlc_UrlClean( &p_sys->proxy );
614 AuthReset( &p_sys->proxy_auth );
616 free( p_sys->psz_mime );
617 free( p_sys->psz_pragma );
618 free( p_sys->psz_location );
620 free( p_sys->psz_icy_name );
621 free( p_sys->psz_icy_genre );
622 free( p_sys->psz_icy_title );
624 free( p_sys->psz_user_agent );
626 Disconnect( p_access );
631 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
632 free(vlc_array_item_at_index( p_sys->cookies, i ));
633 vlc_array_destroy( p_sys->cookies );
637 inflateEnd( &p_sys->inflate.stream );
638 free( p_sys->inflate.p_buffer );
644 /*****************************************************************************
645 * Read: Read up to i_len bytes from the http connection and place in
646 * p_buffer. Return the actual number of bytes read
647 *****************************************************************************/
648 static int ReadICYMeta( access_t *p_access );
649 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
651 access_sys_t *p_sys = p_access->p_sys;
654 if( p_sys->fd == -1 )
656 p_access->info.b_eof = true;
660 if( p_access->info.i_size >= 0 &&
661 i_len + p_access->info.i_pos > p_access->info.i_size )
663 if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
665 p_access->info.b_eof = true;
670 if( p_sys->b_chunked )
672 if( p_sys->i_chunk < 0 )
674 p_access->info.b_eof = true;
678 if( p_sys->i_chunk <= 0 )
680 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
681 /* read the chunk header */
684 /* fatal error - end of file */
685 msg_Dbg( p_access, "failed reading chunk-header line" );
688 p_sys->i_chunk = strtoll( psz, NULL, 16 );
691 if( p_sys->i_chunk <= 0 ) /* eof */
694 p_access->info.b_eof = true;
699 if( i_len > p_sys->i_chunk )
701 i_len = p_sys->i_chunk;
704 else if( p_access->info.i_size != -1 && (int64_t)i_len > p_sys->i_remaining) {
705 /* Only ask for the remaining length */
706 i_len = (size_t)p_sys->i_remaining;
708 p_access->info.b_eof = true;
714 if( p_sys->i_icy_meta > 0 && p_access->info.i_pos-p_sys->i_icy_offset > 0 )
716 int64_t i_next = p_sys->i_icy_meta -
717 (p_access->info.i_pos - p_sys->i_icy_offset ) % p_sys->i_icy_meta;
719 if( i_next == p_sys->i_icy_meta )
721 if( ReadICYMeta( p_access ) )
723 p_access->info.b_eof = true;
731 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, false );
735 p_access->info.i_pos += i_read;
737 if( p_sys->b_chunked )
739 p_sys->i_chunk -= i_read;
740 if( p_sys->i_chunk <= 0 )
742 /* read the empty line */
743 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
748 else if( i_read <= 0 )
751 * I very much doubt that this will work.
752 * If i_read == 0, the connection *IS* dead, so the only
753 * sensible thing to do is Disconnect() and then retry.
754 * Otherwise, I got recv() completely wrong. -- Courmisch
756 if( p_sys->b_continuous )
758 Request( p_access, 0 );
759 p_sys->b_continuous = false;
760 i_read = Read( p_access, p_buffer, i_len );
761 p_sys->b_continuous = true;
763 Disconnect( p_access );
764 if( p_sys->b_reconnect )
766 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
767 if( Connect( p_access, p_access->info.i_pos ) )
769 msg_Dbg( p_access, "reconnection failed" );
773 p_sys->b_reconnect = false;
774 i_read = Read( p_access, p_buffer, i_len );
775 p_sys->b_reconnect = true;
780 p_access->info.b_eof = true;
781 else if( i_read < 0 )
782 p_access->b_error = true;
785 if( p_access->info.i_size != -1 )
787 p_sys->i_remaining -= i_read;
793 static int ReadICYMeta( access_t *p_access )
795 access_sys_t *p_sys = p_access->p_sys;
801 /* Read meta data length */
802 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
809 i_read = buffer << 4;
810 /* msg_Dbg( p_access, "ICY meta size=%u", i_read); */
812 psz_meta = malloc( i_read + 1 );
813 if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
814 (uint8_t *)psz_meta, i_read, true ) != i_read )
820 psz_meta[i_read] = '\0'; /* Just in case */
822 /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
824 /* Now parse the meta */
825 /* Look for StreamTitle= */
826 p = strcasestr( (char *)psz_meta, "StreamTitle=" );
829 p += strlen( "StreamTitle=" );
830 if( *p == '\'' || *p == '"' )
832 char closing[] = { p[0], ';', '\0' };
833 char *psz = strstr( &p[1], closing );
835 psz = strchr( &p[1], ';' );
837 if( psz ) *psz = '\0';
841 char *psz = strchr( &p[1], ';' );
842 if( psz ) *psz = '\0';
845 if( !p_sys->psz_icy_title ||
846 strcmp( p_sys->psz_icy_title, &p[1] ) )
848 free( p_sys->psz_icy_title );
849 char *psz_tmp = strdup( &p[1] );
850 p_sys->psz_icy_title = EnsureUTF8( psz_tmp );
851 if( !p_sys->psz_icy_title )
853 p_access->info.i_update |= INPUT_UPDATE_META;
855 msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
864 static ssize_t ReadCompressed( access_t *p_access, uint8_t *p_buffer,
867 access_sys_t *p_sys = p_access->p_sys;
869 if( p_sys->b_compressed )
873 if( !p_sys->inflate.p_buffer )
874 p_sys->inflate.p_buffer = malloc( 256 * 1024 );
876 if( p_sys->inflate.stream.avail_in == 0 )
878 ssize_t i_read = Read( p_access, p_sys->inflate.p_buffer + p_sys->inflate.stream.avail_in, 256 * 1024 );
879 if( i_read <= 0 ) return i_read;
880 p_sys->inflate.stream.next_in = p_sys->inflate.p_buffer;
881 p_sys->inflate.stream.avail_in = i_read;
884 p_sys->inflate.stream.avail_out = i_len;
885 p_sys->inflate.stream.next_out = p_buffer;
887 i_ret = inflate( &p_sys->inflate.stream, Z_SYNC_FLUSH );
888 msg_Warn( p_access, "inflate return value: %d, %s", i_ret, p_sys->inflate.stream.msg );
890 return i_len - p_sys->inflate.stream.avail_out;
894 return Read( p_access, p_buffer, i_len );
899 /*****************************************************************************
900 * Seek: close and re-open a connection at the right place
901 *****************************************************************************/
902 static int Seek( access_t *p_access, int64_t i_pos )
904 msg_Dbg( p_access, "trying to seek to %"PRId64, i_pos );
906 Disconnect( p_access );
908 if( p_access->info.i_size
909 && (uint64_t)i_pos >= (uint64_t)p_access->info.i_size ) {
910 msg_Err( p_access, "seek to far" );
911 int retval = Seek( p_access, p_access->info.i_size - 1 );
912 if( retval == VLC_SUCCESS ) {
914 Read( p_access, p_buffer, 1);
915 p_access->info.b_eof = false;
919 if( Connect( p_access, i_pos ) )
921 msg_Err( p_access, "seek failed" );
922 p_access->info.b_eof = true;
928 /*****************************************************************************
930 *****************************************************************************/
931 static int Control( access_t *p_access, int i_query, va_list args )
933 access_sys_t *p_sys = p_access->p_sys;
941 case ACCESS_CAN_SEEK:
942 pb_bool = (bool*)va_arg( args, bool* );
943 *pb_bool = p_sys->b_seekable;
945 case ACCESS_CAN_FASTSEEK:
946 pb_bool = (bool*)va_arg( args, bool* );
949 case ACCESS_CAN_PAUSE:
950 case ACCESS_CAN_CONTROL_PACE:
951 pb_bool = (bool*)va_arg( args, bool* );
953 #if 0 /* Disable for now until we have a clock synchro algo
954 * which works with something else than MPEG over UDP */
955 *pb_bool = p_sys->b_pace_control;
961 case ACCESS_GET_PTS_DELAY:
962 pi_64 = (int64_t*)va_arg( args, int64_t * );
963 *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
967 case ACCESS_SET_PAUSE_STATE:
970 case ACCESS_GET_META:
971 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
973 if( p_sys->psz_icy_name )
974 vlc_meta_Set( p_meta, vlc_meta_Title, p_sys->psz_icy_name );
975 if( p_sys->psz_icy_genre )
976 vlc_meta_Set( p_meta, vlc_meta_Genre, p_sys->psz_icy_genre );
977 if( p_sys->psz_icy_title )
978 vlc_meta_Set( p_meta, vlc_meta_NowPlaying, p_sys->psz_icy_title );
981 case ACCESS_GET_CONTENT_TYPE:
982 *va_arg( args, char ** ) =
983 p_sys->psz_mime ? strdup( p_sys->psz_mime ) : NULL;
986 case ACCESS_GET_TITLE_INFO:
987 case ACCESS_SET_TITLE:
988 case ACCESS_SET_SEEKPOINT:
989 case ACCESS_SET_PRIVATE_ID_STATE:
993 msg_Warn( p_access, "unimplemented query in control" );
1000 /*****************************************************************************
1002 *****************************************************************************/
1003 static int Connect( access_t *p_access, int64_t i_tell )
1005 access_sys_t *p_sys = p_access->p_sys;
1006 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
1009 free( p_sys->psz_location );
1010 free( p_sys->psz_mime );
1011 free( p_sys->psz_pragma );
1013 free( p_sys->psz_icy_genre );
1014 free( p_sys->psz_icy_name );
1015 free( p_sys->psz_icy_title );
1018 p_sys->psz_location = NULL;
1019 p_sys->psz_mime = NULL;
1020 p_sys->psz_pragma = NULL;
1021 p_sys->b_mms = false;
1022 p_sys->b_chunked = false;
1024 p_sys->i_icy_meta = 0;
1025 p_sys->i_icy_offset = i_tell;
1026 p_sys->psz_icy_name = NULL;
1027 p_sys->psz_icy_genre = NULL;
1028 p_sys->psz_icy_title = NULL;
1029 p_sys->i_remaining = 0;
1030 p_sys->b_persist = false;
1032 p_access->info.i_size = -1;
1033 p_access->info.i_pos = i_tell;
1034 p_access->info.b_eof = false;
1036 /* Open connection */
1037 assert( p_sys->fd == -1 ); /* No open sockets (leaking fds is BAD) */
1038 p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
1039 if( p_sys->fd == -1 )
1041 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
1044 setsockopt (p_sys->fd, SOL_SOCKET, SO_KEEPALIVE, &(int){ 1 }, sizeof (int));
1046 /* Initialize TLS/SSL session */
1047 if( p_sys->b_ssl == true )
1049 /* CONNECT to establish TLS tunnel through HTTP proxy */
1050 if( p_sys->b_proxy )
1053 unsigned i_status = 0;
1055 if( p_sys->i_version == 0 )
1057 /* CONNECT is not in HTTP/1.0 */
1058 Disconnect( p_access );
1062 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
1063 "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
1064 p_sys->url.psz_host, p_sys->url.i_port,
1066 p_sys->url.psz_host, p_sys->url.i_port);
1068 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
1071 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
1072 Disconnect( p_access );
1076 sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
1079 if( ( i_status / 100 ) != 2 )
1081 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
1082 Disconnect( p_access );
1088 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
1091 msg_Err( p_access, "HTTP proxy connection failed" );
1092 Disconnect( p_access );
1101 if( !vlc_object_alive (p_access) || p_access->b_error )
1103 Disconnect( p_access );
1110 /* TLS/SSL handshake */
1111 p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
1112 p_sys->url.psz_host );
1113 if( p_sys->p_tls == NULL )
1115 msg_Err( p_access, "cannot establish HTTP/TLS session" );
1116 Disconnect( p_access );
1119 p_sys->p_vs = &p_sys->p_tls->sock;
1122 return Request( p_access, i_tell ) ? -2 : 0;
1126 static int Request( access_t *p_access, int64_t i_tell )
1128 access_sys_t *p_sys = p_access->p_sys;
1130 v_socket_t *pvs = p_sys->p_vs;
1131 p_sys->b_persist = false;
1133 p_sys->i_remaining = 0;
1134 if( p_sys->b_proxy )
1136 if( p_sys->url.psz_path )
1138 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
1139 "GET http://%s:%d%s HTTP/1.%d\r\n",
1140 p_sys->url.psz_host, p_sys->url.i_port,
1141 p_sys->url.psz_path, p_sys->i_version );
1145 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
1146 "GET http://%s:%d/ HTTP/1.%d\r\n",
1147 p_sys->url.psz_host, p_sys->url.i_port,
1153 const char *psz_path = p_sys->url.psz_path;
1154 if( !psz_path || !*psz_path )
1158 if( p_sys->url.i_port != (pvs ? 443 : 80) )
1160 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1161 "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
1162 psz_path, p_sys->i_version, p_sys->url.psz_host,
1163 p_sys->url.i_port );
1167 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1168 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
1169 psz_path, p_sys->i_version, p_sys->url.psz_host );
1173 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "User-Agent: %s\r\n",
1174 p_sys->psz_user_agent );
1176 if( p_sys->i_version == 1 && ! p_sys->b_continuous )
1178 p_sys->b_persist = true;
1179 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1180 "Range: bytes=%"PRIu64"-\r\n", i_tell );
1184 if( p_sys->cookies )
1187 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
1189 const char * cookie = vlc_array_item_at_index( p_sys->cookies, i );
1190 char * psz_cookie_content = cookie_get_content( cookie );
1191 char * psz_cookie_domain = cookie_get_domain( cookie );
1193 assert( psz_cookie_content );
1195 /* FIXME: This is clearly not conforming to the rfc */
1196 bool is_in_right_domain = (!psz_cookie_domain || strstr( p_sys->url.psz_host, psz_cookie_domain ));
1198 if( is_in_right_domain )
1200 msg_Dbg( p_access, "Sending Cookie %s", psz_cookie_content );
1201 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Cookie: %s\r\n", psz_cookie_content ) < 0 )
1202 msg_Err( p_access, "failed to send Cookie" );
1204 free( psz_cookie_content );
1205 free( psz_cookie_domain );
1209 /* Authentication */
1210 if( p_sys->url.psz_username || p_sys->url.psz_password )
1211 AuthReply( p_access, "", &p_sys->url, &p_sys->auth );
1213 /* Proxy Authentication */
1214 if( p_sys->proxy.psz_username || p_sys->proxy.psz_password )
1215 AuthReply( p_access, "Proxy-", &p_sys->proxy, &p_sys->proxy_auth );
1217 /* ICY meta data request */
1218 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
1221 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "\r\n" ) < 0 )
1223 msg_Err( p_access, "failed to send request" );
1224 Disconnect( p_access );
1225 return VLC_EGENERIC;
1229 if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs ) ) == NULL )
1231 msg_Err( p_access, "failed to read answer" );
1234 if( !strncmp( psz, "HTTP/1.", 7 ) )
1236 p_sys->psz_protocol = "HTTP";
1237 p_sys->i_code = atoi( &psz[9] );
1239 else if( !strncmp( psz, "ICY", 3 ) )
1241 p_sys->psz_protocol = "ICY";
1242 p_sys->i_code = atoi( &psz[4] );
1243 p_sys->b_reconnect = true;
1247 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1251 msg_Dbg( p_access, "protocol '%s' answer code %d",
1252 p_sys->psz_protocol, p_sys->i_code );
1253 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1255 p_sys->b_seekable = false;
1257 if( p_sys->i_code != 206 && p_sys->i_code != 401 )
1259 p_sys->b_seekable = false;
1261 /* Authentication error - We'll have to display the dialog */
1262 if( p_sys->i_code == 401 )
1266 /* Other fatal error */
1267 else if( p_sys->i_code >= 400 )
1269 msg_Err( p_access, "error: %s", psz );
1277 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs );
1282 msg_Err( p_access, "failed to read answer" );
1286 if( !vlc_object_alive (p_access) || p_access->b_error )
1292 /* msg_Dbg( p_input, "Line=%s", psz ); */
1299 if( ( p = strchr( psz, ':' ) ) == NULL )
1301 msg_Err( p_access, "malformed header line: %s", psz );
1306 while( *p == ' ' ) p++;
1308 if( !strcasecmp( psz, "Content-Length" ) )
1310 int64_t i_size = i_tell + (p_sys->i_remaining = atoll( p ));
1311 if(i_size > p_access->info.i_size) {
1312 p_access->info.i_size = i_size;
1314 msg_Dbg( p_access, "this frame size=%"PRId64, p_sys->i_remaining );
1316 else if( !strcasecmp( psz, "Content-Range" ) ) {
1317 int64_t i_ntell = i_tell;
1318 int64_t i_nend = (p_access->info.i_size > 0)?(p_access->info.i_size - 1):i_tell;
1319 int64_t i_nsize = p_access->info.i_size;
1320 sscanf(p,"bytes %"PRId64"-%"PRId64"/%"PRId64,&i_ntell,&i_nend,&i_nsize);
1321 if(i_nend > i_ntell ) {
1322 p_access->info.i_pos = i_ntell;
1323 p_sys->i_remaining = i_nend+1-i_ntell;
1324 int64_t i_size = (i_nsize > i_nend) ? i_nsize : (i_nend + 1);
1325 if(i_size > p_access->info.i_size) {
1326 p_access->info.i_size = i_size;
1328 msg_Dbg( p_access, "stream size=%"PRId64",pos=%"PRId64",remaining=%"PRId64,i_nsize,i_ntell,p_sys->i_remaining);
1331 else if( !strcasecmp( psz, "Connection" ) ) {
1332 msg_Dbg( p_access, "Connection: %s",p );
1334 sscanf(p, "close%n",&i);
1336 p_sys->b_persist = false;
1339 else if( !strcasecmp( psz, "Location" ) )
1343 /* This does not follow RFC 2068, but yet if the url is not absolute,
1344 * handle it as everyone does. */
1347 const char *psz_http_ext = p_sys->b_ssl ? "s" : "" ;
1349 if( p_sys->url.i_port == ( p_sys->b_ssl ? 443 : 80 ) )
1351 if( asprintf(&psz_new_loc, "http%s://%s%s", psz_http_ext,
1352 p_sys->url.psz_host, p) < 0 )
1357 if( asprintf(&psz_new_loc, "http%s://%s:%d%s", psz_http_ext,
1358 p_sys->url.psz_host, p_sys->url.i_port, p) < 0 )
1364 psz_new_loc = strdup( p );
1367 free( p_sys->psz_location );
1368 p_sys->psz_location = psz_new_loc;
1370 else if( !strcasecmp( psz, "Content-Type" ) )
1372 free( p_sys->psz_mime );
1373 p_sys->psz_mime = strdup( p );
1374 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1376 else if( !strcasecmp( psz, "Content-Encoding" ) )
1378 msg_Dbg( p_access, "Content-Encoding: %s", p );
1379 if( strcasecmp( p, "identity" ) )
1381 p_sys->b_compressed = true;
1383 msg_Warn( p_access, "Compressed content not supported. Rebuild with zlib support." );
1386 else if( !strcasecmp( psz, "Pragma" ) )
1388 if( !strcasecmp( psz, "Pragma: features" ) )
1389 p_sys->b_mms = true;
1390 free( p_sys->psz_pragma );
1391 p_sys->psz_pragma = strdup( p );
1392 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1394 else if( !strcasecmp( psz, "Server" ) )
1396 msg_Dbg( p_access, "Server: %s", p );
1397 if( !strncasecmp( p, "Icecast", 7 ) ||
1398 !strncasecmp( p, "Nanocaster", 10 ) )
1400 /* Remember if this is Icecast
1401 * we need to force demux in this case without breaking
1404 /* Let live 365 streams (nanocaster) piggyback on the icecast
1405 * routine. They look very similar */
1407 p_sys->b_reconnect = true;
1408 p_sys->b_pace_control = false;
1409 p_sys->b_icecast = true;
1412 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1414 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1415 if( !strncasecmp( p, "chunked", 7 ) )
1417 p_sys->b_chunked = true;
1420 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1422 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1423 p_sys->i_icy_meta = atoi( p );
1424 if( p_sys->i_icy_meta < 0 )
1425 p_sys->i_icy_meta = 0;
1426 if( p_sys->i_icy_meta > 0 )
1427 p_sys->b_icecast = true;
1429 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1431 else if( !strcasecmp( psz, "Icy-Name" ) )
1433 free( p_sys->psz_icy_name );
1434 char *psz_tmp = strdup( p );
1435 p_sys->psz_icy_name = EnsureUTF8( psz_tmp );
1436 if( !p_sys->psz_icy_name )
1438 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1440 p_sys->b_icecast = true; /* be on the safeside. set it here as well. */
1441 p_sys->b_reconnect = true;
1442 p_sys->b_pace_control = false;
1444 else if( !strcasecmp( psz, "Icy-Genre" ) )
1446 free( p_sys->psz_icy_genre );
1447 char *psz_tmp = strdup( p );
1448 p_sys->psz_icy_genre = EnsureUTF8( psz_tmp );
1449 if( !p_sys->psz_icy_genre )
1451 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1453 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1455 msg_Dbg( p_access, "Icy-Notice: %s", p );
1457 else if( !strncasecmp( psz, "icy-", 4 ) ||
1458 !strncasecmp( psz, "ice-", 4 ) ||
1459 !strncasecmp( psz, "x-audiocast", 11 ) )
1461 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1463 else if( !strcasecmp( psz, "Set-Cookie" ) )
1465 if( p_sys->cookies )
1467 msg_Dbg( p_access, "Accepting Cookie: %s", p );
1468 cookie_append( p_sys->cookies, strdup(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 AuthParseHeader( p_access, p, &p_sys->auth );
1478 else if( !strcasecmp( psz, "proxy-authenticate" ) )
1480 msg_Dbg( p_access, "Proxy authentication header: %s", p );
1481 AuthParseHeader( p_access, p, &p_sys->proxy_auth );
1483 else if( !strcasecmp( psz, "authentication-info" ) )
1485 msg_Dbg( p_access, "Authentication Info header: %s", p );
1486 if( AuthCheckReply( p_access, p, &p_sys->url, &p_sys->auth ) )
1489 else if( !strcasecmp( psz, "proxy-authentication-info" ) )
1491 msg_Dbg( p_access, "Proxy Authentication Info header: %s", p );
1492 if( AuthCheckReply( p_access, p, &p_sys->proxy, &p_sys->proxy_auth ) )
1498 /* We close the stream for zero length data, unless of course the
1499 * server has already promised to do this for us.
1501 if( p_access->info.i_size != -1 && p_sys->i_remaining == 0 && p_sys->b_persist ) {
1502 Disconnect( p_access );
1507 Disconnect( p_access );
1508 return VLC_EGENERIC;
1511 /*****************************************************************************
1513 *****************************************************************************/
1514 static void Disconnect( access_t *p_access )
1516 access_sys_t *p_sys = p_access->p_sys;
1518 if( p_sys->p_tls != NULL)
1520 tls_ClientDelete( p_sys->p_tls );
1521 p_sys->p_tls = NULL;
1524 if( p_sys->fd != -1)
1526 net_Close(p_sys->fd);
1532 /*****************************************************************************
1533 * Cookies (FIXME: we may want to rewrite that using a nice structure to hold
1534 * them) (FIXME: only support the "domain=" param)
1535 *****************************************************************************/
1537 /* Get the NAME=VALUE part of the Cookie */
1538 static char * cookie_get_content( const char * cookie )
1540 char * ret = strdup( cookie );
1541 if( !ret ) return NULL;
1543 /* Look for a ';' */
1544 while( *str && *str != ';' ) str++;
1545 /* Replace it by a end-char */
1546 if( *str == ';' ) *str = 0;
1550 /* Get the domain where the cookie is stored */
1551 static char * cookie_get_domain( const char * cookie )
1553 const char * str = cookie;
1554 static const char domain[] = "domain=";
1557 /* Look for a ';' */
1560 if( !strncmp( str, domain, sizeof(domain) - 1 /* minus \0 */ ) )
1562 str += sizeof(domain) - 1 /* minus \0 */;
1563 char * ret = strdup( str );
1564 /* Now remove the next ';' if present */
1565 char * ret_iter = ret;
1566 while( *ret_iter && *ret_iter != ';' ) ret_iter++;
1567 if( *ret_iter == ';' )
1571 /* Go to next ';' field */
1572 while( *str && *str != ';' ) str++;
1573 if( *str == ';' ) str++;
1575 while( *str && *str == ' ' ) str++;
1580 /* Get NAME in the NAME=VALUE field */
1581 static char * cookie_get_name( const char * cookie )
1583 char * ret = cookie_get_content( cookie ); /* NAME=VALUE */
1584 if( !ret ) return NULL;
1586 while( *str && *str != '=' ) str++;
1591 /* Add a cookie in cookies, checking to see how it should be added */
1592 static void cookie_append( vlc_array_t * cookies, char * cookie )
1599 char * cookie_name = cookie_get_name( cookie );
1601 /* Don't send invalid cookies */
1605 char * cookie_domain = cookie_get_domain( cookie );
1606 for( i = 0; i < vlc_array_count( cookies ); i++ )
1608 char * current_cookie = vlc_array_item_at_index( cookies, i );
1609 char * current_cookie_name = cookie_get_name( current_cookie );
1610 char * current_cookie_domain = cookie_get_domain( current_cookie );
1612 assert( current_cookie_name );
1614 bool is_domain_matching = ( cookie_domain && current_cookie_domain &&
1615 !strcmp( cookie_domain, current_cookie_domain ) );
1617 if( is_domain_matching && !strcmp( cookie_name, current_cookie_name ) )
1619 /* Remove previous value for this cookie */
1620 free( current_cookie );
1621 vlc_array_remove( cookies, i );
1624 free( current_cookie_name );
1625 free( current_cookie_domain );
1628 free( current_cookie_name );
1629 free( current_cookie_domain );
1631 free( cookie_name );
1632 free( cookie_domain );
1633 vlc_array_append( cookies, cookie );
1636 /*****************************************************************************
1637 * "RFC 2617: Basic and Digest Access Authentication" header parsing
1638 *****************************************************************************/
1639 static char *AuthGetParam( const char *psz_header, const char *psz_param )
1641 char psz_what[strlen(psz_param)+3];
1642 sprintf( psz_what, "%s=\"", psz_param );
1643 psz_header = strstr( psz_header, psz_what );
1646 const char *psz_end;
1647 psz_header += strlen( psz_what );
1648 psz_end = strchr( psz_header, '"' );
1649 if( !psz_end ) /* Invalid since we should have a closing quote */
1650 return strdup( psz_header );
1651 return strndup( psz_header, psz_end - psz_header );
1659 static char *AuthGetParamNoQuotes( const char *psz_header, const char *psz_param )
1661 char psz_what[strlen(psz_param)+2];
1662 sprintf( psz_what, "%s=", psz_param );
1663 psz_header = strstr( psz_header, psz_what );
1666 const char *psz_end;
1667 psz_header += strlen( psz_what );
1668 psz_end = strchr( psz_header, ',' );
1669 /* XXX: Do we need to filter out trailing space between the value and
1670 * the comma/end of line? */
1671 if( !psz_end ) /* Can be valid if this is the last parameter */
1672 return strdup( psz_header );
1673 return strndup( psz_header, psz_end - psz_header );
1681 static void AuthParseHeader( access_t *p_access, const char *psz_header,
1682 http_auth_t *p_auth )
1684 /* FIXME: multiple auth methods can be listed (comma seperated) */
1686 /* 2 Basic Authentication Scheme */
1687 if( !strncasecmp( psz_header, "Basic ", strlen( "Basic " ) ) )
1689 msg_Dbg( p_access, "Using Basic Authentication" );
1690 psz_header += strlen( "Basic " );
1691 p_auth->psz_realm = AuthGetParam( psz_header, "realm" );
1692 if( !p_auth->psz_realm )
1693 msg_Warn( p_access, "Basic Authentication: "
1694 "Mandatory 'realm' parameter is missing" );
1696 /* 3 Digest Access Authentication Scheme */
1697 else if( !strncasecmp( psz_header, "Digest ", strlen( "Digest " ) ) )
1699 msg_Dbg( p_access, "Using Digest Access Authentication" );
1700 if( p_auth->psz_nonce ) return; /* FIXME */
1701 psz_header += strlen( "Digest " );
1702 p_auth->psz_realm = AuthGetParam( psz_header, "realm" );
1703 p_auth->psz_domain = AuthGetParam( psz_header, "domain" );
1704 p_auth->psz_nonce = AuthGetParam( psz_header, "nonce" );
1705 p_auth->psz_opaque = AuthGetParam( psz_header, "opaque" );
1706 p_auth->psz_stale = AuthGetParamNoQuotes( psz_header, "stale" );
1707 p_auth->psz_algorithm = AuthGetParamNoQuotes( psz_header, "algorithm" );
1708 p_auth->psz_qop = AuthGetParam( psz_header, "qop" );
1709 p_auth->i_nonce = 0;
1710 /* printf("realm: |%s|\ndomain: |%s|\nnonce: |%s|\nopaque: |%s|\n"
1711 "stale: |%s|\nalgorithm: |%s|\nqop: |%s|\n",
1712 p_auth->psz_realm,p_auth->psz_domain,p_auth->psz_nonce,
1713 p_auth->psz_opaque,p_auth->psz_stale,p_auth->psz_algorithm,
1714 p_auth->psz_qop); */
1715 if( !p_auth->psz_realm )
1716 msg_Warn( p_access, "Digest Access Authentication: "
1717 "Mandatory 'realm' parameter is missing" );
1718 if( !p_auth->psz_nonce )
1719 msg_Warn( p_access, "Digest Access Authentication: "
1720 "Mandatory 'nonce' parameter is missing" );
1721 if( p_auth->psz_qop ) /* FIXME: parse the qop list */
1723 char *psz_tmp = strchr( p_auth->psz_qop, ',' );
1724 if( psz_tmp ) *psz_tmp = '\0';
1729 const char *psz_end = strchr( psz_header, ' ' );
1731 msg_Warn( p_access, "Unknown authentication scheme: '%*s'",
1732 (int)(psz_end - psz_header), psz_header );
1734 msg_Warn( p_access, "Unknown authentication scheme: '%s'",
1739 static char *AuthDigest( access_t *p_access, vlc_url_t *p_url,
1740 http_auth_t *p_auth, const char *psz_method )
1743 const char *psz_username = p_url->psz_username ? p_url->psz_username : "";
1744 const char *psz_password = p_url->psz_password ? p_url->psz_password : "";
1746 char *psz_HA1 = NULL;
1747 char *psz_HA2 = NULL;
1748 char *psz_response = NULL;
1752 if( p_auth->psz_HA1 )
1754 psz_HA1 = strdup( p_auth->psz_HA1 );
1755 if( !psz_HA1 ) goto error;
1760 AddMD5( &md5, psz_username, strlen( psz_username ) );
1761 AddMD5( &md5, ":", 1 );
1762 AddMD5( &md5, p_auth->psz_realm, strlen( p_auth->psz_realm ) );
1763 AddMD5( &md5, ":", 1 );
1764 AddMD5( &md5, psz_password, strlen( psz_password ) );
1767 psz_HA1 = psz_md5_hash( &md5 );
1768 if( !psz_HA1 ) goto error;
1770 if( p_auth->psz_algorithm
1771 && !strcmp( p_auth->psz_algorithm, "MD5-sess" ) )
1774 AddMD5( &md5, psz_HA1, 32 );
1776 AddMD5( &md5, ":", 1 );
1777 AddMD5( &md5, p_auth->psz_nonce, strlen( p_auth->psz_nonce ) );
1778 AddMD5( &md5, ":", 1 );
1779 AddMD5( &md5, p_auth->psz_cnonce, strlen( p_auth->psz_cnonce ) );
1782 psz_HA1 = psz_md5_hash( &md5 );
1783 if( !psz_HA1 ) goto error;
1784 p_auth->psz_HA1 = strdup( psz_HA1 );
1785 if( !p_auth->psz_HA1 ) goto error;
1792 AddMD5( &md5, psz_method, strlen( psz_method ) );
1793 AddMD5( &md5, ":", 1 );
1794 if( p_url->psz_path )
1795 AddMD5( &md5, p_url->psz_path, strlen( p_url->psz_path ) );
1797 AddMD5( &md5, "/", 1 );
1798 if( p_auth->psz_qop && !strcmp( p_auth->psz_qop, "auth-int" ) )
1803 AddMD5( &ent, "", 0 ); /* XXX: entity-body. should be ok for GET */
1805 psz_ent = psz_md5_hash( &ent );
1806 if( !psz_ent ) goto error;
1807 AddMD5( &md5, ":", 1 );
1808 AddMD5( &md5, psz_ent, 32 );
1812 psz_HA2 = psz_md5_hash( &md5 );
1813 if( !psz_HA2 ) goto error;
1815 /* Request digest */
1817 AddMD5( &md5, psz_HA1, 32 );
1818 AddMD5( &md5, ":", 1 );
1819 AddMD5( &md5, p_auth->psz_nonce, strlen( p_auth->psz_nonce ) );
1820 AddMD5( &md5, ":", 1 );
1822 && ( !strcmp( p_auth->psz_qop, "auth" )
1823 || !strcmp( p_auth->psz_qop, "auth-int" ) ) )
1826 snprintf( psz_inonce, 9, "%08x", p_auth->i_nonce );
1827 AddMD5( &md5, psz_inonce, 8 );
1828 AddMD5( &md5, ":", 1 );
1829 AddMD5( &md5, p_auth->psz_cnonce, strlen( p_auth->psz_cnonce ) );
1830 AddMD5( &md5, ":", 1 );
1831 AddMD5( &md5, p_auth->psz_qop, strlen( p_auth->psz_qop ) );
1832 AddMD5( &md5, ":", 1 );
1834 AddMD5( &md5, psz_HA2, 32 );
1836 psz_response = psz_md5_hash( &md5 );
1841 return psz_response;
1845 static void AuthReply( access_t *p_access, const char *psz_prefix,
1846 vlc_url_t *p_url, http_auth_t *p_auth )
1848 access_sys_t *p_sys = p_access->p_sys;
1849 v_socket_t *pvs = p_sys->p_vs;
1851 const char *psz_username = p_url->psz_username ? p_url->psz_username : "";
1852 const char *psz_password = p_url->psz_password ? p_url->psz_password : "";
1854 if( p_auth->psz_nonce )
1856 /* Digest Access Authentication */
1859 if( p_auth->psz_algorithm
1860 && strcmp( p_auth->psz_algorithm, "MD5" )
1861 && strcmp( p_auth->psz_algorithm, "MD5-sess" ) )
1863 msg_Err( p_access, "Digest Access Authentication: "
1864 "Unknown algorithm '%s'", p_auth->psz_algorithm );
1868 if( p_auth->psz_qop || !p_auth->psz_cnonce )
1870 /* FIXME: needs to be really random to prevent man in the middle
1872 free( p_auth->psz_cnonce );
1873 p_auth->psz_cnonce = strdup( "Some random string FIXME" );
1877 psz_response = AuthDigest( p_access, p_url, p_auth, "GET" );
1878 if( !psz_response ) return;
1880 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1881 "%sAuthorization: Digest "
1882 /* Mandatory parameters */
1888 /* Optional parameters */
1889 "%s%s%s" /* algorithm */
1890 "%s%s%s" /* cnonce */
1891 "%s%s%s" /* opaque */
1892 "%s%s%s" /* message qop */
1893 "%s%08x%s" /* nonce count */
1895 /* Mandatory parameters */
1900 p_url->psz_path ? p_url->psz_path : "/",
1902 /* Optional parameters */
1903 p_auth->psz_algorithm ? "algorithm=\"" : "",
1904 p_auth->psz_algorithm ? p_auth->psz_algorithm : "",
1905 p_auth->psz_algorithm ? "\", " : "",
1906 p_auth->psz_cnonce ? "cnonce=\"" : "",
1907 p_auth->psz_cnonce ? p_auth->psz_cnonce : "",
1908 p_auth->psz_cnonce ? "\", " : "",
1909 p_auth->psz_opaque ? "opaque=\"" : "",
1910 p_auth->psz_opaque ? p_auth->psz_opaque : "",
1911 p_auth->psz_opaque ? "\", " : "",
1912 p_auth->psz_qop ? "qop=\"" : "",
1913 p_auth->psz_qop ? p_auth->psz_qop : "",
1914 p_auth->psz_qop ? "\", " : "",
1915 p_auth->i_nonce ? "nc=\"" : "uglyhack=\"", /* Will be parsed as an unhandled extension */
1917 p_auth->i_nonce ? "\"" : "\""
1920 free( psz_response );
1924 /* Basic Access Authentication */
1925 char buf[strlen( psz_username ) + strlen( psz_password ) + 2];
1928 snprintf( buf, sizeof( buf ), "%s:%s", psz_username, psz_password );
1929 b64 = vlc_b64_encode( buf );
1933 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1934 "%sAuthorization: Basic %s\r\n", psz_prefix, b64 );
1940 static int AuthCheckReply( access_t *p_access, const char *psz_header,
1941 vlc_url_t *p_url, http_auth_t *p_auth )
1943 int i_ret = VLC_EGENERIC;
1944 char *psz_nextnonce = AuthGetParam( psz_header, "nextnonce" );
1945 char *psz_qop = AuthGetParamNoQuotes( psz_header, "qop" );
1946 char *psz_rspauth = AuthGetParam( psz_header, "rspauth" );
1947 char *psz_cnonce = AuthGetParam( psz_header, "cnonce" );
1948 char *psz_nc = AuthGetParamNoQuotes( psz_header, "nc" );
1954 if( strcmp( psz_cnonce, p_auth->psz_cnonce ) )
1956 msg_Err( p_access, "HTTP Digest Access Authentication: server replied with a different client nonce value." );
1963 i_nonce = strtol( psz_nc, NULL, 16 );
1964 if( i_nonce != p_auth->i_nonce )
1966 msg_Err( p_access, "HTTP Digest Access Authentication: server replied with a different nonce count value." );
1971 if( psz_qop && p_auth->psz_qop && strcmp( psz_qop, p_auth->psz_qop ) )
1972 msg_Warn( p_access, "HTTP Digest Access Authentication: server replied using a different 'quality of protection' option" );
1974 /* All the clear text values match, let's now check the response
1976 psz_digest = AuthDigest( p_access, p_url, p_auth, "" );
1977 if( strcmp( psz_digest, psz_rspauth ) )
1979 msg_Err( p_access, "HTTP Digest Access Authentication: server replied with an invalid response digest (expected value: %s).", psz_digest );
1988 free( p_auth->psz_nonce );
1989 p_auth->psz_nonce = psz_nextnonce;
1990 psz_nextnonce = NULL;
1993 i_ret = VLC_SUCCESS;
1995 free( psz_nextnonce );
1997 free( psz_rspauth );
2004 static void AuthReset( http_auth_t *p_auth )
2006 FREENULL( p_auth->psz_realm );
2007 FREENULL( p_auth->psz_domain );
2008 FREENULL( p_auth->psz_nonce );
2009 FREENULL( p_auth->psz_opaque );
2010 FREENULL( p_auth->psz_stale );
2011 FREENULL( p_auth->psz_algorithm );
2012 FREENULL( p_auth->psz_qop );
2013 p_auth->i_nonce = 0;
2014 FREENULL( p_auth->psz_cnonce );
2015 FREENULL( p_auth->psz_HA1 );