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 )
111 add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
112 AGENT_LONGTEXT, true )
113 add_bool( "http-reconnect", 0, NULL, RECONNECT_TEXT,
114 RECONNECT_LONGTEXT, true )
115 add_bool( "http-continuous", 0, NULL, CONTINUOUS_TEXT,
116 CONTINUOUS_LONGTEXT, true )
118 add_bool( "http-forward-cookies", true, NULL, FORWARD_COOKIES_TEXT,
119 FORWARD_COOKIES_LONGTEXT, true )
120 add_obsolete_string("http-user")
121 add_obsolete_string("http-pwd")
122 add_shortcut( "http" )
123 add_shortcut( "https" )
124 add_shortcut( "unsv" )
125 add_shortcut( "itpc" ) /* iTunes Podcast */
126 set_callbacks( Open, Close )
129 /*****************************************************************************
131 *****************************************************************************/
133 /* RFC 2617: Basic and Digest Access Authentication */
134 typedef struct http_auth_t
145 char *psz_HA1; /* stored H(A1) value if algorithm = "MD5-sess" */
151 tls_session_t *p_tls;
156 char *psz_user_agent;
162 http_auth_t proxy_auth;
163 char *psz_proxy_passbuf;
167 const char *psz_protocol;
189 int64_t i_icy_offset;
202 vlc_array_t * cookies;
206 static int OpenWithCookies( vlc_object_t *p_this, vlc_array_t *cookies );
209 static ssize_t Read( access_t *, uint8_t *, size_t );
210 static ssize_t ReadCompressed( access_t *, uint8_t *, size_t );
211 static int Seek( access_t *, int64_t );
212 static int Control( access_t *, int, va_list );
215 static int Connect( access_t *, int64_t );
216 static int Request( access_t *p_access, int64_t i_tell );
217 static void Disconnect( access_t * );
219 /* Small Cookie utilities. Cookies support is partial. */
220 static char * cookie_get_content( const char * cookie );
221 static char * cookie_get_domain( const char * cookie );
222 static char * cookie_get_name( const char * cookie );
223 static void cookie_append( vlc_array_t * cookies, char * cookie );
226 static void AuthParseHeader( access_t *p_access, const char *psz_header,
227 http_auth_t *p_auth );
228 static void AuthReply( access_t *p_acces, const char *psz_prefix,
229 vlc_url_t *p_url, http_auth_t *p_auth );
230 static int AuthCheckReply( access_t *p_access, const char *psz_header,
231 vlc_url_t *p_url, http_auth_t *p_auth );
232 static void AuthReset( http_auth_t *p_auth );
234 /*****************************************************************************
236 *****************************************************************************/
237 static int Open( vlc_object_t *p_this )
239 return OpenWithCookies( p_this, NULL );
242 static int OpenWithCookies( vlc_object_t *p_this, vlc_array_t *cookies )
244 access_t *p_access = (access_t*)p_this;
247 /* Only forward an store cookies if the corresponding option is activated */
248 bool b_forward_cookies = var_CreateGetBool( p_access, "http-forward-cookies" );
249 vlc_array_t * saved_cookies = b_forward_cookies ? (cookies ?: vlc_array_new()) : NULL;
251 /* Set up p_access */
252 STANDARD_READ_ACCESS_INIT;
254 p_access->pf_read = ReadCompressed;
257 p_sys->b_proxy = false;
258 p_sys->psz_proxy_passbuf = NULL;
259 p_sys->i_version = 1;
260 p_sys->b_seekable = true;
261 p_sys->psz_mime = NULL;
262 p_sys->psz_pragma = NULL;
263 p_sys->b_mms = false;
264 p_sys->b_icecast = false;
265 p_sys->psz_location = NULL;
266 p_sys->psz_user_agent = NULL;
267 p_sys->b_pace_control = true;
268 p_sys->b_ssl = false;
270 p_sys->b_compressed = false;
271 /* 15 is the max windowBits, +32 to enable optional gzip decoding */
272 if( inflateInit2( &p_sys->inflate.stream, 32+15 ) != Z_OK )
273 msg_Warn( p_access, "Error during zlib initialisation: %s",
274 p_sys->inflate.stream.msg );
275 if( zlibCompileFlags() & (1<<17) )
276 msg_Warn( p_access, "Your zlib was compiled without gzip support." );
277 p_sys->inflate.p_buffer = NULL;
281 p_sys->i_icy_meta = 0;
282 p_sys->i_icy_offset = 0;
283 p_sys->psz_icy_name = NULL;
284 p_sys->psz_icy_genre = NULL;
285 p_sys->psz_icy_title = NULL;
286 p_sys->i_remaining = 0;
287 p_sys->b_persist = false;
288 p_access->info.i_size = -1;
289 p_access->info.i_pos = 0;
290 p_access->info.b_eof = false;
292 p_sys->cookies = saved_cookies;
294 /* Parse URI - remove spaces */
295 p = psz = strdup( p_access->psz_path );
296 while( (p = strchr( p, ' ' )) != NULL )
298 vlc_UrlParse( &p_sys->url, psz, 0 );
301 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
303 msg_Warn( p_access, "invalid host" );
306 if( !strncmp( p_access->psz_access, "https", 5 ) )
310 if( p_sys->url.i_port <= 0 )
311 p_sys->url.i_port = 443;
315 if( p_sys->url.i_port <= 0 )
316 p_sys->url.i_port = 80;
320 p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
323 psz = var_CreateGetNonEmptyString( p_access, "http-proxy" );
326 p_sys->b_proxy = true;
327 vlc_UrlParse( &p_sys->proxy, psz, 0 );
333 pxProxyFactory *pf = px_proxy_factory_new();
338 i=asprintf(&buf, "%s://%s", p_access->psz_access, p_access->psz_path);
341 msg_Dbg(p_access, "asking libproxy about url '%s'", buf);
342 char **proxies = px_proxy_factory_get_proxies(pf, buf);
345 msg_Dbg(p_access, "libproxy suggest to use '%s'", proxies[0]);
346 if(strcmp(proxies[0],"direct://") != 0)
348 p_sys->b_proxy = true;
349 vlc_UrlParse( &p_sys->proxy, proxies[0], 0);
352 for(i=0;proxies[i];i++) free(proxies[i]);
356 px_proxy_factory_free(pf);
360 msg_Err(p_access, "Allocating memory for libproxy failed");
366 psz = getenv( "http_proxy" );
369 p_sys->b_proxy = true;
370 vlc_UrlParse( &p_sys->proxy, psz, 0 );
374 if( psz ) /* No, this is NOT a use-after-free error */
376 psz = var_CreateGetNonEmptyString( p_access, "http-proxy-pwd" );
378 p_sys->proxy.psz_password = p_sys->psz_proxy_passbuf = psz;
383 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
385 msg_Warn( p_access, "invalid proxy host" );
388 if( p_sys->proxy.i_port <= 0 )
390 p_sys->proxy.i_port = 80;
394 msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
395 p_sys->url.psz_host, p_sys->url.i_port, 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_CreateGetBool( p_access, "http-reconnect" );
407 p_sys->b_continuous = var_CreateGetBool( 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( !vlc_object_alive (p_access) || Connect( p_access, 0 ) )
430 msg_Err( p_access, "You should not be here" );
435 if( p_sys->i_code == 401 )
437 char *psz_login, *psz_password;
439 if( p_sys->url.psz_username && p_sys->url.psz_password &&
440 p_sys->auth.psz_nonce && p_sys->auth.i_nonce == 0 )
442 Disconnect( p_access );
445 msg_Dbg( p_access, "authentication failed for realm %s",
446 p_sys->auth.psz_realm );
447 dialog_Login( p_access, &psz_login, &psz_password,
448 _("HTTP authentication"),
449 _("Please enter a valid login name and a password for realm %s."),
450 p_sys->auth.psz_realm );
451 if( psz_login != NULL && psz_password != NULL )
453 msg_Dbg( p_access, "retrying with user=%s, pwd=%s",
454 psz_login, psz_password );
455 p_sys->url.psz_username = psz_login;
456 p_sys->url.psz_password = psz_password;
457 Disconnect( p_access );
463 free( psz_password );
468 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
469 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
470 p_sys->psz_location && *p_sys->psz_location )
472 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
474 /* Do not accept redirection outside of HTTP works */
475 if( strncmp( p_sys->psz_location, "http", 4 )
476 || ( ( p_sys->psz_location[4] != ':' ) /* HTTP */
477 && strncmp( p_sys->psz_location + 4, "s:", 2 ) /* HTTP/SSL */ ) )
479 msg_Err( p_access, "insecure redirection ignored" );
482 free( p_access->psz_path );
483 p_access->psz_path = strdup( p_sys->psz_location );
484 /* Clean up current Open() run */
485 vlc_UrlClean( &p_sys->url );
486 AuthReset( &p_sys->auth );
487 vlc_UrlClean( &p_sys->proxy );
488 free( p_sys->psz_proxy_passbuf );
489 AuthReset( &p_sys->proxy_auth );
490 free( p_sys->psz_mime );
491 free( p_sys->psz_pragma );
492 free( p_sys->psz_location );
493 free( p_sys->psz_user_agent );
495 Disconnect( p_access );
496 cookies = p_sys->cookies;
499 /* Do new Open() run with new data */
500 return OpenWithCookies( p_this, cookies );
505 msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
509 if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
511 if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
513 if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
514 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
516 free( p_access->psz_demux );
517 p_access->psz_demux = strdup( "nsv" );
519 else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
520 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
522 free( p_access->psz_demux );
523 p_access->psz_demux = strdup( "m4a" );
525 else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
527 free( p_access->psz_demux );
528 p_access->psz_demux = strdup( "mp3" );
531 msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
532 p_access->psz_demux );
534 #if 0 /* Doesn't work really well because of the pre-buffering in
535 * shoutcast servers (the buffer content will be sent as fast as
537 p_sys->b_pace_control = false;
540 else if( !p_sys->psz_mime )
542 free( p_access->psz_demux );
544 p_access->psz_demux = strdup( "mp3" );
546 /* else probably Ogg Vorbis */
548 else if( !strcasecmp( p_access->psz_access, "unsv" ) &&
550 !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
552 free( p_access->psz_demux );
553 /* Grrrr! detect ultravox server and force NSV demuxer */
554 p_access->psz_demux = strdup( "nsv" );
556 else if( !strcmp( p_access->psz_access, "itpc" ) )
558 free( p_access->psz_demux );
559 p_access->psz_demux = strdup( "podcast" );
561 else if( p_sys->psz_mime &&
562 !strncasecmp( p_sys->psz_mime, "application/xspf+xml", 20 ) &&
563 ( memchr( " ;\t", p_sys->psz_mime[20], 4 ) != NULL ) )
565 free( p_access->psz_demux );
566 p_access->psz_demux = strdup( "xspf-open" );
569 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
572 var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
577 vlc_UrlClean( &p_sys->url );
578 vlc_UrlClean( &p_sys->proxy );
579 free( p_sys->psz_proxy_passbuf );
580 free( p_sys->psz_mime );
581 free( p_sys->psz_pragma );
582 free( p_sys->psz_location );
583 free( p_sys->psz_user_agent );
585 Disconnect( p_access );
590 /*****************************************************************************
592 *****************************************************************************/
593 static void Close( vlc_object_t *p_this )
595 access_t *p_access = (access_t*)p_this;
596 access_sys_t *p_sys = p_access->p_sys;
598 vlc_UrlClean( &p_sys->url );
599 AuthReset( &p_sys->auth );
600 vlc_UrlClean( &p_sys->proxy );
601 AuthReset( &p_sys->proxy_auth );
603 free( p_sys->psz_mime );
604 free( p_sys->psz_pragma );
605 free( p_sys->psz_location );
607 free( p_sys->psz_icy_name );
608 free( p_sys->psz_icy_genre );
609 free( p_sys->psz_icy_title );
611 free( p_sys->psz_user_agent );
613 Disconnect( p_access );
618 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
619 free(vlc_array_item_at_index( p_sys->cookies, i ));
620 vlc_array_destroy( p_sys->cookies );
624 inflateEnd( &p_sys->inflate.stream );
625 free( p_sys->inflate.p_buffer );
631 /*****************************************************************************
632 * Read: Read up to i_len bytes from the http connection and place in
633 * p_buffer. Return the actual number of bytes read
634 *****************************************************************************/
635 static int ReadICYMeta( access_t *p_access );
636 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
638 access_sys_t *p_sys = p_access->p_sys;
641 if( p_sys->fd == -1 )
643 p_access->info.b_eof = true;
647 if( p_access->info.i_size >= 0 &&
648 i_len + p_access->info.i_pos > p_access->info.i_size )
650 if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
652 p_access->info.b_eof = true;
657 if( p_sys->b_chunked )
659 if( p_sys->i_chunk < 0 )
661 p_access->info.b_eof = true;
665 if( p_sys->i_chunk <= 0 )
667 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
668 /* read the chunk header */
671 /* fatal error - end of file */
672 msg_Dbg( p_access, "failed reading chunk-header line" );
675 p_sys->i_chunk = strtoll( psz, NULL, 16 );
678 if( p_sys->i_chunk <= 0 ) /* eof */
681 p_access->info.b_eof = true;
686 if( i_len > p_sys->i_chunk )
688 i_len = p_sys->i_chunk;
691 else if( p_access->info.i_size != -1 && (int64_t)i_len > p_sys->i_remaining) {
692 /* Only ask for the remaining length */
693 i_len = (size_t)p_sys->i_remaining;
695 p_access->info.b_eof = true;
701 if( p_sys->i_icy_meta > 0 && p_access->info.i_pos-p_sys->i_icy_offset > 0 )
703 int64_t i_next = p_sys->i_icy_meta -
704 (p_access->info.i_pos - p_sys->i_icy_offset ) % p_sys->i_icy_meta;
706 if( i_next == p_sys->i_icy_meta )
708 if( ReadICYMeta( p_access ) )
710 p_access->info.b_eof = true;
718 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, false );
722 p_access->info.i_pos += i_read;
724 if( p_sys->b_chunked )
726 p_sys->i_chunk -= i_read;
727 if( p_sys->i_chunk <= 0 )
729 /* read the empty line */
730 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
735 else if( i_read == 0 )
738 * I very much doubt that this will work.
739 * If i_read == 0, the connection *IS* dead, so the only
740 * sensible thing to do is Disconnect() and then retry.
741 * Otherwise, I got recv() completely wrong. -- Courmisch
743 if( p_sys->b_continuous )
745 Request( p_access, 0 );
746 p_sys->b_continuous = false;
747 i_read = Read( p_access, p_buffer, i_len );
748 p_sys->b_continuous = true;
750 Disconnect( p_access );
751 if( p_sys->b_reconnect )
753 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
754 if( Connect( p_access, p_access->info.i_pos ) )
756 msg_Dbg( p_access, "reconnection failed" );
760 p_sys->b_reconnect = false;
761 i_read = Read( p_access, p_buffer, i_len );
762 p_sys->b_reconnect = true;
766 if( i_read == 0 ) p_access->info.b_eof = true;
769 if( p_access->info.i_size != -1 )
771 p_sys->i_remaining -= i_read;
777 static int ReadICYMeta( access_t *p_access )
779 access_sys_t *p_sys = p_access->p_sys;
785 /* Read meta data length */
786 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
793 i_read = buffer << 4;
794 /* msg_Dbg( p_access, "ICY meta size=%u", i_read); */
796 psz_meta = malloc( i_read + 1 );
797 if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
798 (uint8_t *)psz_meta, i_read, true ) != i_read )
801 psz_meta[i_read] = '\0'; /* Just in case */
803 /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
805 /* Now parse the meta */
806 /* Look for StreamTitle= */
807 p = strcasestr( (char *)psz_meta, "StreamTitle=" );
810 p += strlen( "StreamTitle=" );
811 if( *p == '\'' || *p == '"' )
813 char closing[] = { p[0], ';', '\0' };
814 char *psz = strstr( &p[1], closing );
816 psz = strchr( &p[1], ';' );
818 if( psz ) *psz = '\0';
822 char *psz = strchr( &p[1], ';' );
823 if( psz ) *psz = '\0';
826 if( !p_sys->psz_icy_title ||
827 strcmp( p_sys->psz_icy_title, &p[1] ) )
829 free( p_sys->psz_icy_title );
830 p_sys->psz_icy_title = EnsureUTF8( strdup( &p[1] ));
831 p_access->info.i_update |= INPUT_UPDATE_META;
833 msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
842 static ssize_t ReadCompressed( access_t *p_access, uint8_t *p_buffer,
845 access_sys_t *p_sys = p_access->p_sys;
847 if( p_sys->b_compressed )
851 if( !p_sys->inflate.p_buffer )
852 p_sys->inflate.p_buffer = malloc( 256 * 1024 );
854 if( p_sys->inflate.stream.avail_in == 0 )
856 ssize_t i_read = Read( p_access, p_sys->inflate.p_buffer + p_sys->inflate.stream.avail_in, 256 * 1024 );
857 if( i_read <= 0 ) return i_read;
858 p_sys->inflate.stream.next_in = p_sys->inflate.p_buffer;
859 p_sys->inflate.stream.avail_in = i_read;
862 p_sys->inflate.stream.avail_out = i_len;
863 p_sys->inflate.stream.next_out = p_buffer;
865 i_ret = inflate( &p_sys->inflate.stream, Z_SYNC_FLUSH );
866 msg_Warn( p_access, "inflate return value: %d, %s", i_ret, p_sys->inflate.stream.msg );
868 return i_len - p_sys->inflate.stream.avail_out;
872 return Read( p_access, p_buffer, i_len );
877 /*****************************************************************************
878 * Seek: close and re-open a connection at the right place
879 *****************************************************************************/
880 static int Seek( access_t *p_access, int64_t i_pos )
882 msg_Dbg( p_access, "trying to seek to %"PRId64, i_pos );
884 Disconnect( p_access );
886 if( p_access->info.i_size
887 && (uint64_t)i_pos >= (uint64_t)p_access->info.i_size ) {
888 msg_Err( p_access, "seek to far" );
889 int retval = Seek( p_access, p_access->info.i_size - 1 );
890 if( retval == VLC_SUCCESS ) {
892 Read( p_access, p_buffer, 1);
893 p_access->info.b_eof = false;
897 if( Connect( p_access, i_pos ) )
899 msg_Err( p_access, "seek failed" );
900 p_access->info.b_eof = true;
906 /*****************************************************************************
908 *****************************************************************************/
909 static int Control( access_t *p_access, int i_query, va_list args )
911 access_sys_t *p_sys = p_access->p_sys;
919 case ACCESS_CAN_SEEK:
920 pb_bool = (bool*)va_arg( args, bool* );
921 *pb_bool = p_sys->b_seekable;
923 case ACCESS_CAN_FASTSEEK:
924 pb_bool = (bool*)va_arg( args, bool* );
927 case ACCESS_CAN_PAUSE:
928 case ACCESS_CAN_CONTROL_PACE:
929 pb_bool = (bool*)va_arg( args, bool* );
931 #if 0 /* Disable for now until we have a clock synchro algo
932 * which works with something else than MPEG over UDP */
933 *pb_bool = p_sys->b_pace_control;
939 case ACCESS_GET_PTS_DELAY:
940 pi_64 = (int64_t*)va_arg( args, int64_t * );
941 *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
945 case ACCESS_SET_PAUSE_STATE:
948 case ACCESS_GET_META:
949 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
951 if( p_sys->psz_icy_name )
952 vlc_meta_Set( p_meta, vlc_meta_Title, p_sys->psz_icy_name );
953 if( p_sys->psz_icy_genre )
954 vlc_meta_Set( p_meta, vlc_meta_Genre, p_sys->psz_icy_genre );
955 if( p_sys->psz_icy_title )
956 vlc_meta_Set( p_meta, vlc_meta_NowPlaying, p_sys->psz_icy_title );
959 case ACCESS_GET_CONTENT_TYPE:
960 *va_arg( args, char ** ) =
961 p_sys->psz_mime ? strdup( p_sys->psz_mime ) : NULL;
964 case ACCESS_GET_TITLE_INFO:
965 case ACCESS_SET_TITLE:
966 case ACCESS_SET_SEEKPOINT:
967 case ACCESS_SET_PRIVATE_ID_STATE:
971 msg_Warn( p_access, "unimplemented query in control" );
978 /*****************************************************************************
980 *****************************************************************************/
981 static int Connect( access_t *p_access, int64_t i_tell )
983 access_sys_t *p_sys = p_access->p_sys;
984 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
987 free( p_sys->psz_location );
988 free( p_sys->psz_mime );
989 free( p_sys->psz_pragma );
991 free( p_sys->psz_icy_genre );
992 free( p_sys->psz_icy_name );
993 free( p_sys->psz_icy_title );
996 p_sys->psz_location = NULL;
997 p_sys->psz_mime = NULL;
998 p_sys->psz_pragma = NULL;
999 p_sys->b_mms = false;
1000 p_sys->b_chunked = false;
1002 p_sys->i_icy_meta = 0;
1003 p_sys->i_icy_offset = i_tell;
1004 p_sys->psz_icy_name = NULL;
1005 p_sys->psz_icy_genre = NULL;
1006 p_sys->psz_icy_title = NULL;
1007 p_sys->i_remaining = 0;
1008 p_sys->b_persist = false;
1010 p_access->info.i_size = -1;
1011 p_access->info.i_pos = i_tell;
1012 p_access->info.b_eof = false;
1014 /* Open connection */
1015 assert( p_sys->fd == -1 ); /* No open sockets (leaking fds is BAD) */
1016 p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
1017 if( p_sys->fd == -1 )
1019 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
1022 setsockopt (p_sys->fd, SOL_SOCKET, SO_KEEPALIVE, &(int){ 1 }, sizeof (int));
1024 /* Initialize TLS/SSL session */
1025 if( p_sys->b_ssl == true )
1027 /* CONNECT to establish TLS tunnel through HTTP proxy */
1028 if( p_sys->b_proxy )
1031 unsigned i_status = 0;
1033 if( p_sys->i_version == 0 )
1035 /* CONNECT is not in HTTP/1.0 */
1036 Disconnect( p_access );
1040 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
1041 "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
1042 p_sys->url.psz_host, p_sys->url.i_port,
1044 p_sys->url.psz_host, p_sys->url.i_port);
1046 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
1049 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
1050 Disconnect( p_access );
1054 sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
1057 if( ( i_status / 100 ) != 2 )
1059 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
1060 Disconnect( p_access );
1066 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
1069 msg_Err( p_access, "HTTP proxy connection failed" );
1070 Disconnect( p_access );
1079 if( !vlc_object_alive (p_access) || p_access->b_error )
1081 Disconnect( p_access );
1088 /* TLS/SSL handshake */
1089 p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
1091 if( p_sys->p_tls == NULL )
1093 msg_Err( p_access, "cannot establish HTTP/TLS session" );
1094 Disconnect( p_access );
1097 p_sys->p_vs = &p_sys->p_tls->sock;
1100 return Request( p_access, i_tell ) ? -2 : 0;
1104 static int Request( access_t *p_access, int64_t i_tell )
1106 access_sys_t *p_sys = p_access->p_sys;
1108 v_socket_t *pvs = p_sys->p_vs;
1109 p_sys->b_persist = false;
1111 p_sys->i_remaining = 0;
1112 if( p_sys->b_proxy )
1114 if( p_sys->url.psz_path )
1116 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
1117 "GET http://%s:%d%s HTTP/1.%d\r\n",
1118 p_sys->url.psz_host, p_sys->url.i_port,
1119 p_sys->url.psz_path, p_sys->i_version );
1123 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
1124 "GET http://%s:%d/ HTTP/1.%d\r\n",
1125 p_sys->url.psz_host, p_sys->url.i_port,
1131 const char *psz_path = p_sys->url.psz_path;
1132 if( !psz_path || !*psz_path )
1136 if( p_sys->url.i_port != (pvs ? 443 : 80) )
1138 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1139 "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
1140 psz_path, p_sys->i_version, p_sys->url.psz_host,
1141 p_sys->url.i_port );
1145 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1146 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
1147 psz_path, p_sys->i_version, p_sys->url.psz_host );
1151 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "User-Agent: %s\r\n",
1152 p_sys->psz_user_agent );
1154 if( p_sys->i_version == 1 && ! p_sys->b_continuous )
1156 p_sys->b_persist = true;
1157 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1158 "Range: bytes=%"PRIu64"-\r\n", i_tell );
1162 if( p_sys->cookies )
1165 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
1167 const char * cookie = vlc_array_item_at_index( p_sys->cookies, i );
1168 char * psz_cookie_content = cookie_get_content( cookie );
1169 char * psz_cookie_domain = cookie_get_domain( cookie );
1171 assert( psz_cookie_content );
1173 /* FIXME: This is clearly not conforming to the rfc */
1174 bool is_in_right_domain = (!psz_cookie_domain || strstr( p_sys->url.psz_host, psz_cookie_domain ));
1176 if( is_in_right_domain )
1178 msg_Dbg( p_access, "Sending Cookie %s", psz_cookie_content );
1179 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Cookie: %s\r\n", psz_cookie_content ) < 0 )
1180 msg_Err( p_access, "failed to send Cookie" );
1182 free( psz_cookie_content );
1183 free( psz_cookie_domain );
1187 /* Authentication */
1188 if( p_sys->url.psz_username || p_sys->url.psz_password )
1189 AuthReply( p_access, "", &p_sys->url, &p_sys->auth );
1191 /* Proxy Authentication */
1192 if( p_sys->proxy.psz_username || p_sys->proxy.psz_password )
1193 AuthReply( p_access, "Proxy-", &p_sys->proxy, &p_sys->proxy_auth );
1195 /* ICY meta data request */
1196 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
1199 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "\r\n" ) < 0 )
1201 msg_Err( p_access, "failed to send request" );
1202 Disconnect( p_access );
1203 return VLC_EGENERIC;
1207 if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs ) ) == NULL )
1209 msg_Err( p_access, "failed to read answer" );
1212 if( !strncmp( psz, "HTTP/1.", 7 ) )
1214 p_sys->psz_protocol = "HTTP";
1215 p_sys->i_code = atoi( &psz[9] );
1217 else if( !strncmp( psz, "ICY", 3 ) )
1219 p_sys->psz_protocol = "ICY";
1220 p_sys->i_code = atoi( &psz[4] );
1221 p_sys->b_reconnect = true;
1225 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1229 msg_Dbg( p_access, "protocol '%s' answer code %d",
1230 p_sys->psz_protocol, p_sys->i_code );
1231 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1233 p_sys->b_seekable = false;
1235 if( p_sys->i_code != 206 && p_sys->i_code != 401 )
1237 p_sys->b_seekable = false;
1239 /* Authentication error - We'll have to display the dialog */
1240 if( p_sys->i_code == 401 )
1244 /* Other fatal error */
1245 else if( p_sys->i_code >= 400 )
1247 msg_Err( p_access, "error: %s", psz );
1255 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs );
1260 msg_Err( p_access, "failed to read answer" );
1264 if( !vlc_object_alive (p_access) || p_access->b_error )
1270 /* msg_Dbg( p_input, "Line=%s", psz ); */
1277 if( ( p = strchr( psz, ':' ) ) == NULL )
1279 msg_Err( p_access, "malformed header line: %s", psz );
1284 while( *p == ' ' ) p++;
1286 if( !strcasecmp( psz, "Content-Length" ) )
1288 int64_t i_size = i_tell + (p_sys->i_remaining = atoll( p ));
1289 if(i_size > p_access->info.i_size) {
1290 p_access->info.i_size = i_size;
1292 msg_Dbg( p_access, "this frame size=%"PRId64, p_sys->i_remaining );
1294 else if( !strcasecmp( psz, "Content-Range" ) ) {
1295 int64_t i_ntell = i_tell;
1296 int64_t i_nend = (p_access->info.i_size > 0)?(p_access->info.i_size - 1):i_tell;
1297 int64_t i_nsize = p_access->info.i_size;
1298 sscanf(p,"bytes %"PRId64"-%"PRId64"/%"PRId64,&i_ntell,&i_nend,&i_nsize);
1299 if(i_nend > i_ntell ) {
1300 p_access->info.i_pos = i_ntell;
1301 p_sys->i_remaining = i_nend+1-i_ntell;
1302 int64_t i_size = (i_nsize > i_nend) ? i_nsize : (i_nend + 1);
1303 if(i_size > p_access->info.i_size) {
1304 p_access->info.i_size = i_size;
1306 msg_Dbg( p_access, "stream size=%"PRId64",pos=%"PRId64",remaining=%"PRId64,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->b_ssl ? "s" : "" ;
1327 if( p_sys->url.i_port == ( p_sys->b_ssl ? 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" ) )
1359 p_sys->b_compressed = true;
1361 msg_Warn( p_access, "Compressed content not supported. Rebuild with zlib support." );
1364 else if( !strcasecmp( psz, "Pragma" ) )
1366 if( !strcasecmp( psz, "Pragma: features" ) )
1367 p_sys->b_mms = true;
1368 free( p_sys->psz_pragma );
1369 p_sys->psz_pragma = strdup( p );
1370 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1372 else if( !strcasecmp( psz, "Server" ) )
1374 msg_Dbg( p_access, "Server: %s", p );
1375 if( !strncasecmp( p, "Icecast", 7 ) ||
1376 !strncasecmp( p, "Nanocaster", 10 ) )
1378 /* Remember if this is Icecast
1379 * we need to force demux in this case without breaking
1382 /* Let live 365 streams (nanocaster) piggyback on the icecast
1383 * routine. They look very similar */
1385 p_sys->b_reconnect = true;
1386 p_sys->b_pace_control = false;
1387 p_sys->b_icecast = true;
1390 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1392 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1393 if( !strncasecmp( p, "chunked", 7 ) )
1395 p_sys->b_chunked = true;
1398 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1400 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1401 p_sys->i_icy_meta = atoi( p );
1402 if( p_sys->i_icy_meta < 0 )
1403 p_sys->i_icy_meta = 0;
1404 if( p_sys->i_icy_meta > 0 )
1405 p_sys->b_icecast = true;
1407 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1409 else if( !strcasecmp( psz, "Icy-Name" ) )
1411 free( p_sys->psz_icy_name );
1412 p_sys->psz_icy_name = EnsureUTF8( strdup( p ));
1413 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1415 p_sys->b_icecast = true; /* be on the safeside. set it here as well. */
1416 p_sys->b_reconnect = true;
1417 p_sys->b_pace_control = false;
1419 else if( !strcasecmp( psz, "Icy-Genre" ) )
1421 free( p_sys->psz_icy_genre );
1422 p_sys->psz_icy_genre = EnsureUTF8( strdup( p ));
1423 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1425 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1427 msg_Dbg( p_access, "Icy-Notice: %s", p );
1429 else if( !strncasecmp( psz, "icy-", 4 ) ||
1430 !strncasecmp( psz, "ice-", 4 ) ||
1431 !strncasecmp( psz, "x-audiocast", 11 ) )
1433 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1435 else if( !strcasecmp( psz, "Set-Cookie" ) )
1437 if( p_sys->cookies )
1439 msg_Dbg( p_access, "Accepting Cookie: %s", p );
1440 cookie_append( p_sys->cookies, strdup(p) );
1443 msg_Dbg( p_access, "We have a Cookie we won't remember: %s", p );
1445 else if( !strcasecmp( psz, "www-authenticate" ) )
1447 msg_Dbg( p_access, "Authentication header: %s", p );
1448 AuthParseHeader( p_access, p, &p_sys->auth );
1450 else if( !strcasecmp( psz, "proxy-authenticate" ) )
1452 msg_Dbg( p_access, "Proxy authentication header: %s", p );
1453 AuthParseHeader( p_access, p, &p_sys->proxy_auth );
1455 else if( !strcasecmp( psz, "authentication-info" ) )
1457 msg_Dbg( p_access, "Authentication Info header: %s", p );
1458 if( AuthCheckReply( p_access, p, &p_sys->url, &p_sys->auth ) )
1461 else if( !strcasecmp( psz, "proxy-authentication-info" ) )
1463 msg_Dbg( p_access, "Proxy Authentication Info header: %s", p );
1464 if( AuthCheckReply( p_access, p, &p_sys->proxy, &p_sys->proxy_auth ) )
1470 /* We close the stream for zero length data, unless of course the
1471 * server has already promised to do this for us.
1473 if( p_access->info.i_size != -1 && p_sys->i_remaining == 0 && p_sys->b_persist ) {
1474 Disconnect( p_access );
1479 Disconnect( p_access );
1480 return VLC_EGENERIC;
1483 /*****************************************************************************
1485 *****************************************************************************/
1486 static void Disconnect( access_t *p_access )
1488 access_sys_t *p_sys = p_access->p_sys;
1490 if( p_sys->p_tls != NULL)
1492 tls_ClientDelete( p_sys->p_tls );
1493 p_sys->p_tls = NULL;
1496 if( p_sys->fd != -1)
1498 net_Close(p_sys->fd);
1504 /*****************************************************************************
1505 * Cookies (FIXME: we may want to rewrite that using a nice structure to hold
1506 * them) (FIXME: only support the "domain=" param)
1507 *****************************************************************************/
1509 /* Get the NAME=VALUE part of the Cookie */
1510 static char * cookie_get_content( const char * cookie )
1512 char * ret = strdup( cookie );
1513 if( !ret ) return NULL;
1515 /* Look for a ';' */
1516 while( *str && *str != ';' ) str++;
1517 /* Replace it by a end-char */
1518 if( *str == ';' ) *str = 0;
1522 /* Get the domain where the cookie is stored */
1523 static char * cookie_get_domain( const char * cookie )
1525 const char * str = cookie;
1526 static const char domain[] = "domain=";
1529 /* Look for a ';' */
1532 if( !strncmp( str, domain, sizeof(domain) - 1 /* minus \0 */ ) )
1534 str += sizeof(domain) - 1 /* minus \0 */;
1535 char * ret = strdup( str );
1536 /* Now remove the next ';' if present */
1537 char * ret_iter = ret;
1538 while( *ret_iter && *ret_iter != ';' ) ret_iter++;
1539 if( *ret_iter == ';' )
1543 /* Go to next ';' field */
1544 while( *str && *str != ';' ) str++;
1545 if( *str == ';' ) str++;
1547 while( *str && *str == ' ' ) str++;
1552 /* Get NAME in the NAME=VALUE field */
1553 static char * cookie_get_name( const char * cookie )
1555 char * ret = cookie_get_content( cookie ); /* NAME=VALUE */
1556 if( !ret ) return NULL;
1558 while( *str && *str != '=' ) str++;
1563 /* Add a cookie in cookies, checking to see how it should be added */
1564 static void cookie_append( vlc_array_t * cookies, char * cookie )
1571 char * cookie_name = cookie_get_name( cookie );
1573 /* Don't send invalid cookies */
1577 char * cookie_domain = cookie_get_domain( cookie );
1578 for( i = 0; i < vlc_array_count( cookies ); i++ )
1580 char * current_cookie = vlc_array_item_at_index( cookies, i );
1581 char * current_cookie_name = cookie_get_name( current_cookie );
1582 char * current_cookie_domain = cookie_get_domain( current_cookie );
1584 assert( current_cookie_name );
1586 bool is_domain_matching = ( cookie_domain && current_cookie_domain &&
1587 !strcmp( cookie_domain, current_cookie_domain ) );
1589 if( is_domain_matching && !strcmp( cookie_name, current_cookie_name ) )
1591 /* Remove previous value for this cookie */
1592 free( current_cookie );
1593 vlc_array_remove( cookies, i );
1596 free( current_cookie_name );
1597 free( current_cookie_domain );
1600 free( current_cookie_name );
1601 free( current_cookie_domain );
1603 free( cookie_name );
1604 free( cookie_domain );
1605 vlc_array_append( cookies, cookie );
1608 /*****************************************************************************
1609 * "RFC 2617: Basic and Digest Access Authentication" header parsing
1610 *****************************************************************************/
1611 static char *AuthGetParam( const char *psz_header, const char *psz_param )
1613 char psz_what[strlen(psz_param)+3];
1614 sprintf( psz_what, "%s=\"", psz_param );
1615 psz_header = strstr( psz_header, psz_what );
1618 const char *psz_end;
1619 psz_header += strlen( psz_what );
1620 psz_end = strchr( psz_header, '"' );
1621 if( !psz_end ) /* Invalid since we should have a closing quote */
1622 return strdup( psz_header );
1623 return strndup( psz_header, psz_end - psz_header );
1631 static char *AuthGetParamNoQuotes( const char *psz_header, const char *psz_param )
1633 char psz_what[strlen(psz_param)+2];
1634 sprintf( psz_what, "%s=", psz_param );
1635 psz_header = strstr( psz_header, psz_what );
1638 const char *psz_end;
1639 psz_header += strlen( psz_what );
1640 psz_end = strchr( psz_header, ',' );
1641 /* XXX: Do we need to filter out trailing space between the value and
1642 * the comma/end of line? */
1643 if( !psz_end ) /* Can be valid if this is the last parameter */
1644 return strdup( psz_header );
1645 return strndup( psz_header, psz_end - psz_header );
1653 static void AuthParseHeader( access_t *p_access, const char *psz_header,
1654 http_auth_t *p_auth )
1656 /* FIXME: multiple auth methods can be listed (comma seperated) */
1658 /* 2 Basic Authentication Scheme */
1659 if( !strncasecmp( psz_header, "Basic ", strlen( "Basic " ) ) )
1661 msg_Dbg( p_access, "Using Basic Authentication" );
1662 psz_header += strlen( "Basic " );
1663 p_auth->psz_realm = AuthGetParam( psz_header, "realm" );
1664 if( !p_auth->psz_realm )
1665 msg_Warn( p_access, "Basic Authentication: "
1666 "Mandatory 'realm' parameter is missing" );
1668 /* 3 Digest Access Authentication Scheme */
1669 else if( !strncasecmp( psz_header, "Digest ", strlen( "Digest " ) ) )
1671 msg_Dbg( p_access, "Using Digest Access Authentication" );
1672 if( p_auth->psz_nonce ) return; /* FIXME */
1673 psz_header += strlen( "Digest " );
1674 p_auth->psz_realm = AuthGetParam( psz_header, "realm" );
1675 p_auth->psz_domain = AuthGetParam( psz_header, "domain" );
1676 p_auth->psz_nonce = AuthGetParam( psz_header, "nonce" );
1677 p_auth->psz_opaque = AuthGetParam( psz_header, "opaque" );
1678 p_auth->psz_stale = AuthGetParamNoQuotes( psz_header, "stale" );
1679 p_auth->psz_algorithm = AuthGetParamNoQuotes( psz_header, "algorithm" );
1680 p_auth->psz_qop = AuthGetParam( psz_header, "qop" );
1681 p_auth->i_nonce = 0;
1682 /* printf("realm: |%s|\ndomain: |%s|\nnonce: |%s|\nopaque: |%s|\n"
1683 "stale: |%s|\nalgorithm: |%s|\nqop: |%s|\n",
1684 p_auth->psz_realm,p_auth->psz_domain,p_auth->psz_nonce,
1685 p_auth->psz_opaque,p_auth->psz_stale,p_auth->psz_algorithm,
1686 p_auth->psz_qop); */
1687 if( !p_auth->psz_realm )
1688 msg_Warn( p_access, "Digest Access Authentication: "
1689 "Mandatory 'realm' parameter is missing" );
1690 if( !p_auth->psz_nonce )
1691 msg_Warn( p_access, "Digest Access Authentication: "
1692 "Mandatory 'nonce' parameter is missing" );
1693 if( p_auth->psz_qop ) /* FIXME: parse the qop list */
1695 char *psz_tmp = strchr( p_auth->psz_qop, ',' );
1696 if( psz_tmp ) *psz_tmp = '\0';
1701 const char *psz_end = strchr( psz_header, ' ' );
1703 msg_Warn( p_access, "Unknown authentication scheme: '%*s'",
1704 (int)(psz_end - psz_header), psz_header );
1706 msg_Warn( p_access, "Unknown authentication scheme: '%s'",
1711 static char *AuthDigest( access_t *p_access, vlc_url_t *p_url,
1712 http_auth_t *p_auth, const char *psz_method )
1715 const char *psz_username = p_url->psz_username ?: "";
1716 const char *psz_password = p_url->psz_password ?: "";
1718 char *psz_HA1 = NULL;
1719 char *psz_HA2 = NULL;
1720 char *psz_response = NULL;
1724 if( p_auth->psz_HA1 )
1726 psz_HA1 = strdup( p_auth->psz_HA1 );
1727 if( !psz_HA1 ) goto error;
1732 AddMD5( &md5, psz_username, strlen( psz_username ) );
1733 AddMD5( &md5, ":", 1 );
1734 AddMD5( &md5, p_auth->psz_realm, strlen( p_auth->psz_realm ) );
1735 AddMD5( &md5, ":", 1 );
1736 AddMD5( &md5, psz_password, strlen( psz_password ) );
1739 psz_HA1 = psz_md5_hash( &md5 );
1740 if( !psz_HA1 ) goto error;
1742 if( p_auth->psz_algorithm
1743 && !strcmp( p_auth->psz_algorithm, "MD5-sess" ) )
1746 AddMD5( &md5, psz_HA1, 32 );
1748 AddMD5( &md5, ":", 1 );
1749 AddMD5( &md5, p_auth->psz_nonce, strlen( p_auth->psz_nonce ) );
1750 AddMD5( &md5, ":", 1 );
1751 AddMD5( &md5, p_auth->psz_cnonce, strlen( p_auth->psz_cnonce ) );
1754 psz_HA1 = psz_md5_hash( &md5 );
1755 if( !psz_HA1 ) goto error;
1756 p_auth->psz_HA1 = strdup( psz_HA1 );
1757 if( !p_auth->psz_HA1 ) goto error;
1764 AddMD5( &md5, psz_method, strlen( psz_method ) );
1765 AddMD5( &md5, ":", 1 );
1766 if( p_url->psz_path )
1767 AddMD5( &md5, p_url->psz_path, strlen( p_url->psz_path ) );
1769 AddMD5( &md5, "/", 1 );
1770 if( p_auth->psz_qop && !strcmp( p_auth->psz_qop, "auth-int" ) )
1775 AddMD5( &ent, "", 0 ); /* XXX: entity-body. should be ok for GET */
1777 psz_ent = psz_md5_hash( &ent );
1778 if( !psz_ent ) goto error;
1779 AddMD5( &md5, ":", 1 );
1780 AddMD5( &md5, psz_ent, 32 );
1784 psz_HA2 = psz_md5_hash( &md5 );
1785 if( !psz_HA2 ) goto error;
1787 /* Request digest */
1789 AddMD5( &md5, psz_HA1, 32 );
1790 AddMD5( &md5, ":", 1 );
1791 AddMD5( &md5, p_auth->psz_nonce, strlen( p_auth->psz_nonce ) );
1792 AddMD5( &md5, ":", 1 );
1794 && ( !strcmp( p_auth->psz_qop, "auth" )
1795 || !strcmp( p_auth->psz_qop, "auth-int" ) ) )
1798 snprintf( psz_inonce, 9, "%08x", p_auth->i_nonce );
1799 AddMD5( &md5, psz_inonce, 8 );
1800 AddMD5( &md5, ":", 1 );
1801 AddMD5( &md5, p_auth->psz_cnonce, strlen( p_auth->psz_cnonce ) );
1802 AddMD5( &md5, ":", 1 );
1803 AddMD5( &md5, p_auth->psz_qop, strlen( p_auth->psz_qop ) );
1804 AddMD5( &md5, ":", 1 );
1806 AddMD5( &md5, psz_HA2, 32 );
1808 psz_response = psz_md5_hash( &md5 );
1813 return psz_response;
1817 static void AuthReply( access_t *p_access, const char *psz_prefix,
1818 vlc_url_t *p_url, http_auth_t *p_auth )
1820 access_sys_t *p_sys = p_access->p_sys;
1821 v_socket_t *pvs = p_sys->p_vs;
1823 const char *psz_username = p_url->psz_username ?: "";
1824 const char *psz_password = p_url->psz_password ?: "";
1826 if( p_auth->psz_nonce )
1828 /* Digest Access Authentication */
1831 if( p_auth->psz_algorithm
1832 && strcmp( p_auth->psz_algorithm, "MD5" )
1833 && strcmp( p_auth->psz_algorithm, "MD5-sess" ) )
1835 msg_Err( p_access, "Digest Access Authentication: "
1836 "Unknown algorithm '%s'", p_auth->psz_algorithm );
1840 if( p_auth->psz_qop || !p_auth->psz_cnonce )
1842 /* FIXME: needs to be really random to prevent man in the middle
1844 free( p_auth->psz_cnonce );
1845 p_auth->psz_cnonce = strdup( "Some random string FIXME" );
1849 psz_response = AuthDigest( p_access, p_url, p_auth, "GET" );
1850 if( !psz_response ) return;
1852 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1853 "%sAuthorization: Digest "
1854 /* Mandatory parameters */
1860 /* Optional parameters */
1861 "%s%s%s" /* algorithm */
1862 "%s%s%s" /* cnonce */
1863 "%s%s%s" /* opaque */
1864 "%s%s%s" /* message qop */
1865 "%s%08x%s" /* nonce count */
1867 /* Mandatory parameters */
1872 p_url->psz_path ?: "/",
1874 /* Optional parameters */
1875 p_auth->psz_algorithm ? "algorithm=\"" : "",
1876 p_auth->psz_algorithm ?: "",
1877 p_auth->psz_algorithm ? "\", " : "",
1878 p_auth->psz_cnonce ? "cnonce=\"" : "",
1879 p_auth->psz_cnonce ?: "",
1880 p_auth->psz_cnonce ? "\", " : "",
1881 p_auth->psz_opaque ? "opaque=\"" : "",
1882 p_auth->psz_opaque ?: "",
1883 p_auth->psz_opaque ? "\", " : "",
1884 p_auth->psz_qop ? "qop=\"" : "",
1885 p_auth->psz_qop ?: "",
1886 p_auth->psz_qop ? "\", " : "",
1887 p_auth->i_nonce ? "nc=\"" : "uglyhack=\"", /* Will be parsed as an unhandled extension */
1889 p_auth->i_nonce ? "\"" : "\""
1892 free( psz_response );
1896 /* Basic Access Authentication */
1897 char buf[strlen( psz_username ) + strlen( psz_password ) + 2];
1900 snprintf( buf, sizeof( buf ), "%s:%s", psz_username, psz_password );
1901 b64 = vlc_b64_encode( buf );
1905 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1906 "%sAuthorization: Basic %s\r\n", psz_prefix, b64 );
1912 static int AuthCheckReply( access_t *p_access, const char *psz_header,
1913 vlc_url_t *p_url, http_auth_t *p_auth )
1915 int i_ret = VLC_EGENERIC;
1916 char *psz_nextnonce = AuthGetParam( psz_header, "nextnonce" );
1917 char *psz_qop = AuthGetParamNoQuotes( psz_header, "qop" );
1918 char *psz_rspauth = AuthGetParam( psz_header, "rspauth" );
1919 char *psz_cnonce = AuthGetParam( psz_header, "cnonce" );
1920 char *psz_nc = AuthGetParamNoQuotes( psz_header, "nc" );
1926 if( strcmp( psz_cnonce, p_auth->psz_cnonce ) )
1928 msg_Err( p_access, "HTTP Digest Access Authentication: server replied with a different client nonce value." );
1935 i_nonce = strtol( psz_nc, NULL, 16 );
1936 if( i_nonce != p_auth->i_nonce )
1938 msg_Err( p_access, "HTTP Digest Access Authentication: server replied with a different nonce count value." );
1943 if( psz_qop && p_auth->psz_qop && strcmp( psz_qop, p_auth->psz_qop ) )
1944 msg_Warn( p_access, "HTTP Digest Access Authentication: server replied using a different 'quality of protection' option" );
1946 /* All the clear text values match, let's now check the response
1948 psz_digest = AuthDigest( p_access, p_url, p_auth, "" );
1949 if( strcmp( psz_digest, psz_rspauth ) )
1951 msg_Err( p_access, "HTTP Digest Access Authentication: server replied with an invalid response digest (expected value: %s).", psz_digest );
1960 free( p_auth->psz_nonce );
1961 p_auth->psz_nonce = psz_nextnonce;
1962 psz_nextnonce = NULL;
1965 i_ret = VLC_SUCCESS;
1967 free( psz_nextnonce );
1969 free( psz_rspauth );
1976 static void AuthReset( http_auth_t *p_auth )
1978 FREENULL( p_auth->psz_realm );
1979 FREENULL( p_auth->psz_domain );
1980 FREENULL( p_auth->psz_nonce );
1981 FREENULL( p_auth->psz_opaque );
1982 FREENULL( p_auth->psz_stale );
1983 FREENULL( p_auth->psz_algorithm );
1984 FREENULL( p_auth->psz_qop );
1985 p_auth->i_nonce = 0;
1986 FREENULL( p_auth->psz_cnonce );
1987 FREENULL( p_auth->psz_HA1 );