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",
456 "yeah right, like we're going to print a password."
461 p_sys->url.psz_username = psz_login;
462 p_sys->url.psz_password = psz_password;
463 Disconnect( p_access );
469 free( psz_password );
474 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
475 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
476 p_sys->psz_location && *p_sys->psz_location )
478 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
480 /* Do not accept redirection outside of HTTP works */
481 if( strncmp( p_sys->psz_location, "http", 4 )
482 || ( ( p_sys->psz_location[4] != ':' ) /* HTTP */
483 && strncmp( p_sys->psz_location + 4, "s:", 2 ) /* HTTP/SSL */ ) )
485 msg_Err( p_access, "insecure redirection ignored" );
488 free( p_access->psz_path );
489 p_access->psz_path = strdup( p_sys->psz_location );
490 /* Clean up current Open() run */
491 vlc_UrlClean( &p_sys->url );
492 AuthReset( &p_sys->auth );
493 vlc_UrlClean( &p_sys->proxy );
494 free( p_sys->psz_proxy_passbuf );
495 AuthReset( &p_sys->proxy_auth );
496 free( p_sys->psz_mime );
497 free( p_sys->psz_pragma );
498 free( p_sys->psz_location );
499 free( p_sys->psz_user_agent );
501 Disconnect( p_access );
502 cookies = p_sys->cookies;
505 /* Do new Open() run with new data */
506 return OpenWithCookies( p_this, cookies );
511 msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
515 if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
517 if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
519 if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
520 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
522 free( p_access->psz_demux );
523 p_access->psz_demux = strdup( "nsv" );
525 else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
526 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
528 free( p_access->psz_demux );
529 p_access->psz_demux = strdup( "m4a" );
531 else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
533 free( p_access->psz_demux );
534 p_access->psz_demux = strdup( "mp3" );
537 msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
538 p_access->psz_demux );
540 #if 0 /* Doesn't work really well because of the pre-buffering in
541 * shoutcast servers (the buffer content will be sent as fast as
543 p_sys->b_pace_control = false;
546 else if( !p_sys->psz_mime )
548 free( p_access->psz_demux );
550 p_access->psz_demux = strdup( "mp3" );
552 /* else probably Ogg Vorbis */
554 else if( !strcasecmp( p_access->psz_access, "unsv" ) &&
556 !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
558 free( p_access->psz_demux );
559 /* Grrrr! detect ultravox server and force NSV demuxer */
560 p_access->psz_demux = strdup( "nsv" );
562 else if( !strcmp( p_access->psz_access, "itpc" ) )
564 free( p_access->psz_demux );
565 p_access->psz_demux = strdup( "podcast" );
567 else if( p_sys->psz_mime &&
568 !strncasecmp( p_sys->psz_mime, "application/xspf+xml", 20 ) &&
569 ( memchr( " ;\t", p_sys->psz_mime[20], 4 ) != NULL ) )
571 free( p_access->psz_demux );
572 p_access->psz_demux = strdup( "xspf-open" );
575 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
578 var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
583 vlc_UrlClean( &p_sys->url );
584 vlc_UrlClean( &p_sys->proxy );
585 free( p_sys->psz_proxy_passbuf );
586 free( p_sys->psz_mime );
587 free( p_sys->psz_pragma );
588 free( p_sys->psz_location );
589 free( p_sys->psz_user_agent );
591 Disconnect( p_access );
596 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
597 free(vlc_array_item_at_index( p_sys->cookies, i ));
598 vlc_array_destroy( p_sys->cookies );
602 inflateEnd( &p_sys->inflate.stream );
608 /*****************************************************************************
610 *****************************************************************************/
611 static void Close( vlc_object_t *p_this )
613 access_t *p_access = (access_t*)p_this;
614 access_sys_t *p_sys = p_access->p_sys;
616 vlc_UrlClean( &p_sys->url );
617 AuthReset( &p_sys->auth );
618 vlc_UrlClean( &p_sys->proxy );
619 AuthReset( &p_sys->proxy_auth );
621 free( p_sys->psz_mime );
622 free( p_sys->psz_pragma );
623 free( p_sys->psz_location );
625 free( p_sys->psz_icy_name );
626 free( p_sys->psz_icy_genre );
627 free( p_sys->psz_icy_title );
629 free( p_sys->psz_user_agent );
631 Disconnect( p_access );
636 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
637 free(vlc_array_item_at_index( p_sys->cookies, i ));
638 vlc_array_destroy( p_sys->cookies );
642 inflateEnd( &p_sys->inflate.stream );
643 free( p_sys->inflate.p_buffer );
649 /*****************************************************************************
650 * Read: Read up to i_len bytes from the http connection and place in
651 * p_buffer. Return the actual number of bytes read
652 *****************************************************************************/
653 static int ReadICYMeta( access_t *p_access );
654 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
656 access_sys_t *p_sys = p_access->p_sys;
659 if( p_sys->fd == -1 )
661 p_access->info.b_eof = true;
665 if( p_access->info.i_size >= 0 &&
666 i_len + p_access->info.i_pos > p_access->info.i_size )
668 if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
670 p_access->info.b_eof = true;
675 if( p_sys->b_chunked )
677 if( p_sys->i_chunk < 0 )
679 p_access->info.b_eof = true;
683 if( p_sys->i_chunk <= 0 )
685 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
686 /* read the chunk header */
689 /* fatal error - end of file */
690 msg_Dbg( p_access, "failed reading chunk-header line" );
693 p_sys->i_chunk = strtoll( psz, NULL, 16 );
696 if( p_sys->i_chunk <= 0 ) /* eof */
699 p_access->info.b_eof = true;
704 if( i_len > p_sys->i_chunk )
706 i_len = p_sys->i_chunk;
709 else if( p_access->info.i_size != -1 && (int64_t)i_len > p_sys->i_remaining) {
710 /* Only ask for the remaining length */
711 i_len = (size_t)p_sys->i_remaining;
713 p_access->info.b_eof = true;
719 if( p_sys->i_icy_meta > 0 && p_access->info.i_pos-p_sys->i_icy_offset > 0 )
721 int64_t i_next = p_sys->i_icy_meta -
722 (p_access->info.i_pos - p_sys->i_icy_offset ) % p_sys->i_icy_meta;
724 if( i_next == p_sys->i_icy_meta )
726 if( ReadICYMeta( p_access ) )
728 p_access->info.b_eof = true;
736 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, false );
740 p_access->info.i_pos += i_read;
742 if( p_sys->b_chunked )
744 p_sys->i_chunk -= i_read;
745 if( p_sys->i_chunk <= 0 )
747 /* read the empty line */
748 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
753 else if( i_read == 0 )
756 * I very much doubt that this will work.
757 * If i_read == 0, the connection *IS* dead, so the only
758 * sensible thing to do is Disconnect() and then retry.
759 * Otherwise, I got recv() completely wrong. -- Courmisch
761 if( p_sys->b_continuous )
763 Request( p_access, 0 );
764 p_sys->b_continuous = false;
765 i_read = Read( p_access, p_buffer, i_len );
766 p_sys->b_continuous = true;
768 Disconnect( p_access );
769 if( p_sys->b_reconnect )
771 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
772 if( Connect( p_access, p_access->info.i_pos ) )
774 msg_Dbg( p_access, "reconnection failed" );
778 p_sys->b_reconnect = false;
779 i_read = Read( p_access, p_buffer, i_len );
780 p_sys->b_reconnect = true;
784 if( i_read == 0 ) p_access->info.b_eof = true;
787 if( p_access->info.i_size != -1 )
789 p_sys->i_remaining -= i_read;
795 static int ReadICYMeta( access_t *p_access )
797 access_sys_t *p_sys = p_access->p_sys;
803 /* Read meta data length */
804 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
811 i_read = buffer << 4;
812 /* msg_Dbg( p_access, "ICY meta size=%u", i_read); */
814 psz_meta = malloc( i_read + 1 );
815 if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
816 (uint8_t *)psz_meta, i_read, true ) != i_read )
819 psz_meta[i_read] = '\0'; /* Just in case */
821 /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
823 /* Now parse the meta */
824 /* Look for StreamTitle= */
825 p = strcasestr( (char *)psz_meta, "StreamTitle=" );
828 p += strlen( "StreamTitle=" );
829 if( *p == '\'' || *p == '"' )
831 char closing[] = { p[0], ';', '\0' };
832 char *psz = strstr( &p[1], closing );
834 psz = strchr( &p[1], ';' );
836 if( psz ) *psz = '\0';
840 char *psz = strchr( &p[1], ';' );
841 if( psz ) *psz = '\0';
844 if( !p_sys->psz_icy_title ||
845 strcmp( p_sys->psz_icy_title, &p[1] ) )
847 free( p_sys->psz_icy_title );
848 p_sys->psz_icy_title = EnsureUTF8( strdup( &p[1] ));
849 p_access->info.i_update |= INPUT_UPDATE_META;
851 msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
860 static ssize_t ReadCompressed( access_t *p_access, uint8_t *p_buffer,
863 access_sys_t *p_sys = p_access->p_sys;
865 if( p_sys->b_compressed )
869 if( !p_sys->inflate.p_buffer )
870 p_sys->inflate.p_buffer = malloc( 256 * 1024 );
872 if( p_sys->inflate.stream.avail_in == 0 )
874 ssize_t i_read = Read( p_access, p_sys->inflate.p_buffer + p_sys->inflate.stream.avail_in, 256 * 1024 );
875 if( i_read <= 0 ) return i_read;
876 p_sys->inflate.stream.next_in = p_sys->inflate.p_buffer;
877 p_sys->inflate.stream.avail_in = i_read;
880 p_sys->inflate.stream.avail_out = i_len;
881 p_sys->inflate.stream.next_out = p_buffer;
883 i_ret = inflate( &p_sys->inflate.stream, Z_SYNC_FLUSH );
884 msg_Warn( p_access, "inflate return value: %d, %s", i_ret, p_sys->inflate.stream.msg );
886 return i_len - p_sys->inflate.stream.avail_out;
890 return Read( p_access, p_buffer, i_len );
895 /*****************************************************************************
896 * Seek: close and re-open a connection at the right place
897 *****************************************************************************/
898 static int Seek( access_t *p_access, int64_t i_pos )
900 msg_Dbg( p_access, "trying to seek to %"PRId64, i_pos );
902 Disconnect( p_access );
904 if( p_access->info.i_size
905 && (uint64_t)i_pos >= (uint64_t)p_access->info.i_size ) {
906 msg_Err( p_access, "seek to far" );
907 int retval = Seek( p_access, p_access->info.i_size - 1 );
908 if( retval == VLC_SUCCESS ) {
910 Read( p_access, p_buffer, 1);
911 p_access->info.b_eof = false;
915 if( Connect( p_access, i_pos ) )
917 msg_Err( p_access, "seek failed" );
918 p_access->info.b_eof = true;
924 /*****************************************************************************
926 *****************************************************************************/
927 static int Control( access_t *p_access, int i_query, va_list args )
929 access_sys_t *p_sys = p_access->p_sys;
937 case ACCESS_CAN_SEEK:
938 pb_bool = (bool*)va_arg( args, bool* );
939 *pb_bool = p_sys->b_seekable;
941 case ACCESS_CAN_FASTSEEK:
942 pb_bool = (bool*)va_arg( args, bool* );
945 case ACCESS_CAN_PAUSE:
946 case ACCESS_CAN_CONTROL_PACE:
947 pb_bool = (bool*)va_arg( args, bool* );
949 #if 0 /* Disable for now until we have a clock synchro algo
950 * which works with something else than MPEG over UDP */
951 *pb_bool = p_sys->b_pace_control;
957 case ACCESS_GET_PTS_DELAY:
958 pi_64 = (int64_t*)va_arg( args, int64_t * );
959 *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
963 case ACCESS_SET_PAUSE_STATE:
966 case ACCESS_GET_META:
967 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
969 if( p_sys->psz_icy_name )
970 vlc_meta_Set( p_meta, vlc_meta_Title, p_sys->psz_icy_name );
971 if( p_sys->psz_icy_genre )
972 vlc_meta_Set( p_meta, vlc_meta_Genre, p_sys->psz_icy_genre );
973 if( p_sys->psz_icy_title )
974 vlc_meta_Set( p_meta, vlc_meta_NowPlaying, p_sys->psz_icy_title );
977 case ACCESS_GET_CONTENT_TYPE:
978 *va_arg( args, char ** ) =
979 p_sys->psz_mime ? strdup( p_sys->psz_mime ) : NULL;
982 case ACCESS_GET_TITLE_INFO:
983 case ACCESS_SET_TITLE:
984 case ACCESS_SET_SEEKPOINT:
985 case ACCESS_SET_PRIVATE_ID_STATE:
989 msg_Warn( p_access, "unimplemented query in control" );
996 /*****************************************************************************
998 *****************************************************************************/
999 static int Connect( access_t *p_access, int64_t i_tell )
1001 access_sys_t *p_sys = p_access->p_sys;
1002 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
1005 free( p_sys->psz_location );
1006 free( p_sys->psz_mime );
1007 free( p_sys->psz_pragma );
1009 free( p_sys->psz_icy_genre );
1010 free( p_sys->psz_icy_name );
1011 free( p_sys->psz_icy_title );
1014 p_sys->psz_location = NULL;
1015 p_sys->psz_mime = NULL;
1016 p_sys->psz_pragma = NULL;
1017 p_sys->b_mms = false;
1018 p_sys->b_chunked = false;
1020 p_sys->i_icy_meta = 0;
1021 p_sys->i_icy_offset = i_tell;
1022 p_sys->psz_icy_name = NULL;
1023 p_sys->psz_icy_genre = NULL;
1024 p_sys->psz_icy_title = NULL;
1025 p_sys->i_remaining = 0;
1026 p_sys->b_persist = false;
1028 p_access->info.i_size = -1;
1029 p_access->info.i_pos = i_tell;
1030 p_access->info.b_eof = false;
1032 /* Open connection */
1033 assert( p_sys->fd == -1 ); /* No open sockets (leaking fds is BAD) */
1034 p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
1035 if( p_sys->fd == -1 )
1037 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
1040 setsockopt (p_sys->fd, SOL_SOCKET, SO_KEEPALIVE, &(int){ 1 }, sizeof (int));
1042 /* Initialize TLS/SSL session */
1043 if( p_sys->b_ssl == true )
1045 /* CONNECT to establish TLS tunnel through HTTP proxy */
1046 if( p_sys->b_proxy )
1049 unsigned i_status = 0;
1051 if( p_sys->i_version == 0 )
1053 /* CONNECT is not in HTTP/1.0 */
1054 Disconnect( p_access );
1058 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
1059 "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
1060 p_sys->url.psz_host, p_sys->url.i_port,
1062 p_sys->url.psz_host, p_sys->url.i_port);
1064 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
1067 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
1068 Disconnect( p_access );
1072 sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
1075 if( ( i_status / 100 ) != 2 )
1077 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
1078 Disconnect( p_access );
1084 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
1087 msg_Err( p_access, "HTTP proxy connection failed" );
1088 Disconnect( p_access );
1097 if( !vlc_object_alive (p_access) || p_access->b_error )
1099 Disconnect( p_access );
1106 /* TLS/SSL handshake */
1107 p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
1109 if( p_sys->p_tls == NULL )
1111 msg_Err( p_access, "cannot establish HTTP/TLS session" );
1112 Disconnect( p_access );
1115 p_sys->p_vs = &p_sys->p_tls->sock;
1118 return Request( p_access, i_tell ) ? -2 : 0;
1122 static int Request( access_t *p_access, int64_t i_tell )
1124 access_sys_t *p_sys = p_access->p_sys;
1126 v_socket_t *pvs = p_sys->p_vs;
1127 p_sys->b_persist = false;
1129 p_sys->i_remaining = 0;
1130 if( p_sys->b_proxy )
1132 if( p_sys->url.psz_path )
1134 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
1135 "GET http://%s:%d%s HTTP/1.%d\r\n",
1136 p_sys->url.psz_host, p_sys->url.i_port,
1137 p_sys->url.psz_path, p_sys->i_version );
1141 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
1142 "GET http://%s:%d/ HTTP/1.%d\r\n",
1143 p_sys->url.psz_host, p_sys->url.i_port,
1149 const char *psz_path = p_sys->url.psz_path;
1150 if( !psz_path || !*psz_path )
1154 if( p_sys->url.i_port != (pvs ? 443 : 80) )
1156 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1157 "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
1158 psz_path, p_sys->i_version, p_sys->url.psz_host,
1159 p_sys->url.i_port );
1163 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1164 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
1165 psz_path, p_sys->i_version, p_sys->url.psz_host );
1169 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "User-Agent: %s\r\n",
1170 p_sys->psz_user_agent );
1172 if( p_sys->i_version == 1 && ! p_sys->b_continuous )
1174 p_sys->b_persist = true;
1175 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1176 "Range: bytes=%"PRIu64"-\r\n", i_tell );
1180 if( p_sys->cookies )
1183 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
1185 const char * cookie = vlc_array_item_at_index( p_sys->cookies, i );
1186 char * psz_cookie_content = cookie_get_content( cookie );
1187 char * psz_cookie_domain = cookie_get_domain( cookie );
1189 assert( psz_cookie_content );
1191 /* FIXME: This is clearly not conforming to the rfc */
1192 bool is_in_right_domain = (!psz_cookie_domain || strstr( p_sys->url.psz_host, psz_cookie_domain ));
1194 if( is_in_right_domain )
1196 msg_Dbg( p_access, "Sending Cookie %s", psz_cookie_content );
1197 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Cookie: %s\r\n", psz_cookie_content ) < 0 )
1198 msg_Err( p_access, "failed to send Cookie" );
1200 free( psz_cookie_content );
1201 free( psz_cookie_domain );
1205 /* Authentication */
1206 if( p_sys->url.psz_username || p_sys->url.psz_password )
1207 AuthReply( p_access, "", &p_sys->url, &p_sys->auth );
1209 /* Proxy Authentication */
1210 if( p_sys->proxy.psz_username || p_sys->proxy.psz_password )
1211 AuthReply( p_access, "Proxy-", &p_sys->proxy, &p_sys->proxy_auth );
1213 /* ICY meta data request */
1214 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
1217 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "\r\n" ) < 0 )
1219 msg_Err( p_access, "failed to send request" );
1220 Disconnect( p_access );
1221 return VLC_EGENERIC;
1225 if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs ) ) == NULL )
1227 msg_Err( p_access, "failed to read answer" );
1230 if( !strncmp( psz, "HTTP/1.", 7 ) )
1232 p_sys->psz_protocol = "HTTP";
1233 p_sys->i_code = atoi( &psz[9] );
1235 else if( !strncmp( psz, "ICY", 3 ) )
1237 p_sys->psz_protocol = "ICY";
1238 p_sys->i_code = atoi( &psz[4] );
1239 p_sys->b_reconnect = true;
1243 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1247 msg_Dbg( p_access, "protocol '%s' answer code %d",
1248 p_sys->psz_protocol, p_sys->i_code );
1249 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1251 p_sys->b_seekable = false;
1253 if( p_sys->i_code != 206 && p_sys->i_code != 401 )
1255 p_sys->b_seekable = false;
1257 /* Authentication error - We'll have to display the dialog */
1258 if( p_sys->i_code == 401 )
1262 /* Other fatal error */
1263 else if( p_sys->i_code >= 400 )
1265 msg_Err( p_access, "error: %s", psz );
1273 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs );
1278 msg_Err( p_access, "failed to read answer" );
1282 if( !vlc_object_alive (p_access) || p_access->b_error )
1288 /* msg_Dbg( p_input, "Line=%s", psz ); */
1295 if( ( p = strchr( psz, ':' ) ) == NULL )
1297 msg_Err( p_access, "malformed header line: %s", psz );
1302 while( *p == ' ' ) p++;
1304 if( !strcasecmp( psz, "Content-Length" ) )
1306 int64_t i_size = i_tell + (p_sys->i_remaining = atoll( p ));
1307 if(i_size > p_access->info.i_size) {
1308 p_access->info.i_size = i_size;
1310 msg_Dbg( p_access, "this frame size=%"PRId64, p_sys->i_remaining );
1312 else if( !strcasecmp( psz, "Content-Range" ) ) {
1313 int64_t i_ntell = i_tell;
1314 int64_t i_nend = (p_access->info.i_size > 0)?(p_access->info.i_size - 1):i_tell;
1315 int64_t i_nsize = p_access->info.i_size;
1316 sscanf(p,"bytes %"PRId64"-%"PRId64"/%"PRId64,&i_ntell,&i_nend,&i_nsize);
1317 if(i_nend > i_ntell ) {
1318 p_access->info.i_pos = i_ntell;
1319 p_sys->i_remaining = i_nend+1-i_ntell;
1320 int64_t i_size = (i_nsize > i_nend) ? i_nsize : (i_nend + 1);
1321 if(i_size > p_access->info.i_size) {
1322 p_access->info.i_size = i_size;
1324 msg_Dbg( p_access, "stream size=%"PRId64",pos=%"PRId64",remaining=%"PRId64,i_nsize,i_ntell,p_sys->i_remaining);
1327 else if( !strcasecmp( psz, "Connection" ) ) {
1328 msg_Dbg( p_access, "Connection: %s",p );
1330 sscanf(p, "close%n",&i);
1332 p_sys->b_persist = false;
1335 else if( !strcasecmp( psz, "Location" ) )
1339 /* This does not follow RFC 2068, but yet if the url is not absolute,
1340 * handle it as everyone does. */
1343 const char *psz_http_ext = p_sys->b_ssl ? "s" : "" ;
1345 if( p_sys->url.i_port == ( p_sys->b_ssl ? 443 : 80 ) )
1347 if( asprintf(&psz_new_loc, "http%s://%s%s", psz_http_ext,
1348 p_sys->url.psz_host, p) < 0 )
1353 if( asprintf(&psz_new_loc, "http%s://%s:%d%s", psz_http_ext,
1354 p_sys->url.psz_host, p_sys->url.i_port, p) < 0 )
1360 psz_new_loc = strdup( p );
1363 free( p_sys->psz_location );
1364 p_sys->psz_location = psz_new_loc;
1366 else if( !strcasecmp( psz, "Content-Type" ) )
1368 free( p_sys->psz_mime );
1369 p_sys->psz_mime = strdup( p );
1370 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1372 else if( !strcasecmp( psz, "Content-Encoding" ) )
1374 msg_Dbg( p_access, "Content-Encoding: %s", p );
1375 if( strcasecmp( p, "identity" ) )
1377 p_sys->b_compressed = true;
1379 msg_Warn( p_access, "Compressed content not supported. Rebuild with zlib support." );
1382 else if( !strcasecmp( psz, "Pragma" ) )
1384 if( !strcasecmp( psz, "Pragma: features" ) )
1385 p_sys->b_mms = true;
1386 free( p_sys->psz_pragma );
1387 p_sys->psz_pragma = strdup( p );
1388 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1390 else if( !strcasecmp( psz, "Server" ) )
1392 msg_Dbg( p_access, "Server: %s", p );
1393 if( !strncasecmp( p, "Icecast", 7 ) ||
1394 !strncasecmp( p, "Nanocaster", 10 ) )
1396 /* Remember if this is Icecast
1397 * we need to force demux in this case without breaking
1400 /* Let live 365 streams (nanocaster) piggyback on the icecast
1401 * routine. They look very similar */
1403 p_sys->b_reconnect = true;
1404 p_sys->b_pace_control = false;
1405 p_sys->b_icecast = true;
1408 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1410 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1411 if( !strncasecmp( p, "chunked", 7 ) )
1413 p_sys->b_chunked = true;
1416 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1418 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1419 p_sys->i_icy_meta = atoi( p );
1420 if( p_sys->i_icy_meta < 0 )
1421 p_sys->i_icy_meta = 0;
1422 if( p_sys->i_icy_meta > 0 )
1423 p_sys->b_icecast = true;
1425 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1427 else if( !strcasecmp( psz, "Icy-Name" ) )
1429 free( p_sys->psz_icy_name );
1430 p_sys->psz_icy_name = EnsureUTF8( strdup( p ));
1431 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1433 p_sys->b_icecast = true; /* be on the safeside. set it here as well. */
1434 p_sys->b_reconnect = true;
1435 p_sys->b_pace_control = false;
1437 else if( !strcasecmp( psz, "Icy-Genre" ) )
1439 free( p_sys->psz_icy_genre );
1440 p_sys->psz_icy_genre = EnsureUTF8( strdup( p ));
1441 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1443 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1445 msg_Dbg( p_access, "Icy-Notice: %s", p );
1447 else if( !strncasecmp( psz, "icy-", 4 ) ||
1448 !strncasecmp( psz, "ice-", 4 ) ||
1449 !strncasecmp( psz, "x-audiocast", 11 ) )
1451 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1453 else if( !strcasecmp( psz, "Set-Cookie" ) )
1455 if( p_sys->cookies )
1457 msg_Dbg( p_access, "Accepting Cookie: %s", p );
1458 cookie_append( p_sys->cookies, strdup(p) );
1461 msg_Dbg( p_access, "We have a Cookie we won't remember: %s", p );
1463 else if( !strcasecmp( psz, "www-authenticate" ) )
1465 msg_Dbg( p_access, "Authentication header: %s", p );
1466 AuthParseHeader( p_access, p, &p_sys->auth );
1468 else if( !strcasecmp( psz, "proxy-authenticate" ) )
1470 msg_Dbg( p_access, "Proxy authentication header: %s", p );
1471 AuthParseHeader( p_access, p, &p_sys->proxy_auth );
1473 else if( !strcasecmp( psz, "authentication-info" ) )
1475 msg_Dbg( p_access, "Authentication Info header: %s", p );
1476 if( AuthCheckReply( p_access, p, &p_sys->url, &p_sys->auth ) )
1479 else if( !strcasecmp( psz, "proxy-authentication-info" ) )
1481 msg_Dbg( p_access, "Proxy Authentication Info header: %s", p );
1482 if( AuthCheckReply( p_access, p, &p_sys->proxy, &p_sys->proxy_auth ) )
1488 /* We close the stream for zero length data, unless of course the
1489 * server has already promised to do this for us.
1491 if( p_access->info.i_size != -1 && p_sys->i_remaining == 0 && p_sys->b_persist ) {
1492 Disconnect( p_access );
1497 Disconnect( p_access );
1498 return VLC_EGENERIC;
1501 /*****************************************************************************
1503 *****************************************************************************/
1504 static void Disconnect( access_t *p_access )
1506 access_sys_t *p_sys = p_access->p_sys;
1508 if( p_sys->p_tls != NULL)
1510 tls_ClientDelete( p_sys->p_tls );
1511 p_sys->p_tls = NULL;
1514 if( p_sys->fd != -1)
1516 net_Close(p_sys->fd);
1522 /*****************************************************************************
1523 * Cookies (FIXME: we may want to rewrite that using a nice structure to hold
1524 * them) (FIXME: only support the "domain=" param)
1525 *****************************************************************************/
1527 /* Get the NAME=VALUE part of the Cookie */
1528 static char * cookie_get_content( const char * cookie )
1530 char * ret = strdup( cookie );
1531 if( !ret ) return NULL;
1533 /* Look for a ';' */
1534 while( *str && *str != ';' ) str++;
1535 /* Replace it by a end-char */
1536 if( *str == ';' ) *str = 0;
1540 /* Get the domain where the cookie is stored */
1541 static char * cookie_get_domain( const char * cookie )
1543 const char * str = cookie;
1544 static const char domain[] = "domain=";
1547 /* Look for a ';' */
1550 if( !strncmp( str, domain, sizeof(domain) - 1 /* minus \0 */ ) )
1552 str += sizeof(domain) - 1 /* minus \0 */;
1553 char * ret = strdup( str );
1554 /* Now remove the next ';' if present */
1555 char * ret_iter = ret;
1556 while( *ret_iter && *ret_iter != ';' ) ret_iter++;
1557 if( *ret_iter == ';' )
1561 /* Go to next ';' field */
1562 while( *str && *str != ';' ) str++;
1563 if( *str == ';' ) str++;
1565 while( *str && *str == ' ' ) str++;
1570 /* Get NAME in the NAME=VALUE field */
1571 static char * cookie_get_name( const char * cookie )
1573 char * ret = cookie_get_content( cookie ); /* NAME=VALUE */
1574 if( !ret ) return NULL;
1576 while( *str && *str != '=' ) str++;
1581 /* Add a cookie in cookies, checking to see how it should be added */
1582 static void cookie_append( vlc_array_t * cookies, char * cookie )
1589 char * cookie_name = cookie_get_name( cookie );
1591 /* Don't send invalid cookies */
1595 char * cookie_domain = cookie_get_domain( cookie );
1596 for( i = 0; i < vlc_array_count( cookies ); i++ )
1598 char * current_cookie = vlc_array_item_at_index( cookies, i );
1599 char * current_cookie_name = cookie_get_name( current_cookie );
1600 char * current_cookie_domain = cookie_get_domain( current_cookie );
1602 assert( current_cookie_name );
1604 bool is_domain_matching = ( cookie_domain && current_cookie_domain &&
1605 !strcmp( cookie_domain, current_cookie_domain ) );
1607 if( is_domain_matching && !strcmp( cookie_name, current_cookie_name ) )
1609 /* Remove previous value for this cookie */
1610 free( current_cookie );
1611 vlc_array_remove( cookies, i );
1614 free( current_cookie_name );
1615 free( current_cookie_domain );
1618 free( current_cookie_name );
1619 free( current_cookie_domain );
1621 free( cookie_name );
1622 free( cookie_domain );
1623 vlc_array_append( cookies, cookie );
1626 /*****************************************************************************
1627 * "RFC 2617: Basic and Digest Access Authentication" header parsing
1628 *****************************************************************************/
1629 static char *AuthGetParam( const char *psz_header, const char *psz_param )
1631 char psz_what[strlen(psz_param)+3];
1632 sprintf( psz_what, "%s=\"", psz_param );
1633 psz_header = strstr( psz_header, psz_what );
1636 const char *psz_end;
1637 psz_header += strlen( psz_what );
1638 psz_end = strchr( psz_header, '"' );
1639 if( !psz_end ) /* Invalid since we should have a closing quote */
1640 return strdup( psz_header );
1641 return strndup( psz_header, psz_end - psz_header );
1649 static char *AuthGetParamNoQuotes( const char *psz_header, const char *psz_param )
1651 char psz_what[strlen(psz_param)+2];
1652 sprintf( psz_what, "%s=", psz_param );
1653 psz_header = strstr( psz_header, psz_what );
1656 const char *psz_end;
1657 psz_header += strlen( psz_what );
1658 psz_end = strchr( psz_header, ',' );
1659 /* XXX: Do we need to filter out trailing space between the value and
1660 * the comma/end of line? */
1661 if( !psz_end ) /* Can be valid if this is the last parameter */
1662 return strdup( psz_header );
1663 return strndup( psz_header, psz_end - psz_header );
1671 static void AuthParseHeader( access_t *p_access, const char *psz_header,
1672 http_auth_t *p_auth )
1674 /* FIXME: multiple auth methods can be listed (comma seperated) */
1676 /* 2 Basic Authentication Scheme */
1677 if( !strncasecmp( psz_header, "Basic ", strlen( "Basic " ) ) )
1679 msg_Dbg( p_access, "Using Basic Authentication" );
1680 psz_header += strlen( "Basic " );
1681 p_auth->psz_realm = AuthGetParam( psz_header, "realm" );
1682 if( !p_auth->psz_realm )
1683 msg_Warn( p_access, "Basic Authentication: "
1684 "Mandatory 'realm' parameter is missing" );
1686 /* 3 Digest Access Authentication Scheme */
1687 else if( !strncasecmp( psz_header, "Digest ", strlen( "Digest " ) ) )
1689 msg_Dbg( p_access, "Using Digest Access Authentication" );
1690 if( p_auth->psz_nonce ) return; /* FIXME */
1691 psz_header += strlen( "Digest " );
1692 p_auth->psz_realm = AuthGetParam( psz_header, "realm" );
1693 p_auth->psz_domain = AuthGetParam( psz_header, "domain" );
1694 p_auth->psz_nonce = AuthGetParam( psz_header, "nonce" );
1695 p_auth->psz_opaque = AuthGetParam( psz_header, "opaque" );
1696 p_auth->psz_stale = AuthGetParamNoQuotes( psz_header, "stale" );
1697 p_auth->psz_algorithm = AuthGetParamNoQuotes( psz_header, "algorithm" );
1698 p_auth->psz_qop = AuthGetParam( psz_header, "qop" );
1699 p_auth->i_nonce = 0;
1700 /* printf("realm: |%s|\ndomain: |%s|\nnonce: |%s|\nopaque: |%s|\n"
1701 "stale: |%s|\nalgorithm: |%s|\nqop: |%s|\n",
1702 p_auth->psz_realm,p_auth->psz_domain,p_auth->psz_nonce,
1703 p_auth->psz_opaque,p_auth->psz_stale,p_auth->psz_algorithm,
1704 p_auth->psz_qop); */
1705 if( !p_auth->psz_realm )
1706 msg_Warn( p_access, "Digest Access Authentication: "
1707 "Mandatory 'realm' parameter is missing" );
1708 if( !p_auth->psz_nonce )
1709 msg_Warn( p_access, "Digest Access Authentication: "
1710 "Mandatory 'nonce' parameter is missing" );
1711 if( p_auth->psz_qop ) /* FIXME: parse the qop list */
1713 char *psz_tmp = strchr( p_auth->psz_qop, ',' );
1714 if( psz_tmp ) *psz_tmp = '\0';
1719 const char *psz_end = strchr( psz_header, ' ' );
1721 msg_Warn( p_access, "Unknown authentication scheme: '%*s'",
1722 (int)(psz_end - psz_header), psz_header );
1724 msg_Warn( p_access, "Unknown authentication scheme: '%s'",
1729 static char *AuthDigest( access_t *p_access, vlc_url_t *p_url,
1730 http_auth_t *p_auth, const char *psz_method )
1733 const char *psz_username = p_url->psz_username ?: "";
1734 const char *psz_password = p_url->psz_password ?: "";
1736 char *psz_HA1 = NULL;
1737 char *psz_HA2 = NULL;
1738 char *psz_response = NULL;
1742 if( p_auth->psz_HA1 )
1744 psz_HA1 = strdup( p_auth->psz_HA1 );
1745 if( !psz_HA1 ) goto error;
1750 AddMD5( &md5, psz_username, strlen( psz_username ) );
1751 AddMD5( &md5, ":", 1 );
1752 AddMD5( &md5, p_auth->psz_realm, strlen( p_auth->psz_realm ) );
1753 AddMD5( &md5, ":", 1 );
1754 AddMD5( &md5, psz_password, strlen( psz_password ) );
1757 psz_HA1 = psz_md5_hash( &md5 );
1758 if( !psz_HA1 ) goto error;
1760 if( p_auth->psz_algorithm
1761 && !strcmp( p_auth->psz_algorithm, "MD5-sess" ) )
1764 AddMD5( &md5, psz_HA1, 32 );
1766 AddMD5( &md5, ":", 1 );
1767 AddMD5( &md5, p_auth->psz_nonce, strlen( p_auth->psz_nonce ) );
1768 AddMD5( &md5, ":", 1 );
1769 AddMD5( &md5, p_auth->psz_cnonce, strlen( p_auth->psz_cnonce ) );
1772 psz_HA1 = psz_md5_hash( &md5 );
1773 if( !psz_HA1 ) goto error;
1774 p_auth->psz_HA1 = strdup( psz_HA1 );
1775 if( !p_auth->psz_HA1 ) goto error;
1782 AddMD5( &md5, psz_method, strlen( psz_method ) );
1783 AddMD5( &md5, ":", 1 );
1784 if( p_url->psz_path )
1785 AddMD5( &md5, p_url->psz_path, strlen( p_url->psz_path ) );
1787 AddMD5( &md5, "/", 1 );
1788 if( p_auth->psz_qop && !strcmp( p_auth->psz_qop, "auth-int" ) )
1793 AddMD5( &ent, "", 0 ); /* XXX: entity-body. should be ok for GET */
1795 psz_ent = psz_md5_hash( &ent );
1796 if( !psz_ent ) goto error;
1797 AddMD5( &md5, ":", 1 );
1798 AddMD5( &md5, psz_ent, 32 );
1802 psz_HA2 = psz_md5_hash( &md5 );
1803 if( !psz_HA2 ) goto error;
1805 /* Request digest */
1807 AddMD5( &md5, psz_HA1, 32 );
1808 AddMD5( &md5, ":", 1 );
1809 AddMD5( &md5, p_auth->psz_nonce, strlen( p_auth->psz_nonce ) );
1810 AddMD5( &md5, ":", 1 );
1812 && ( !strcmp( p_auth->psz_qop, "auth" )
1813 || !strcmp( p_auth->psz_qop, "auth-int" ) ) )
1816 snprintf( psz_inonce, 9, "%08x", p_auth->i_nonce );
1817 AddMD5( &md5, psz_inonce, 8 );
1818 AddMD5( &md5, ":", 1 );
1819 AddMD5( &md5, p_auth->psz_cnonce, strlen( p_auth->psz_cnonce ) );
1820 AddMD5( &md5, ":", 1 );
1821 AddMD5( &md5, p_auth->psz_qop, strlen( p_auth->psz_qop ) );
1822 AddMD5( &md5, ":", 1 );
1824 AddMD5( &md5, psz_HA2, 32 );
1826 psz_response = psz_md5_hash( &md5 );
1831 return psz_response;
1835 static void AuthReply( access_t *p_access, const char *psz_prefix,
1836 vlc_url_t *p_url, http_auth_t *p_auth )
1838 access_sys_t *p_sys = p_access->p_sys;
1839 v_socket_t *pvs = p_sys->p_vs;
1841 const char *psz_username = p_url->psz_username ?: "";
1842 const char *psz_password = p_url->psz_password ?: "";
1844 if( p_auth->psz_nonce )
1846 /* Digest Access Authentication */
1849 if( p_auth->psz_algorithm
1850 && strcmp( p_auth->psz_algorithm, "MD5" )
1851 && strcmp( p_auth->psz_algorithm, "MD5-sess" ) )
1853 msg_Err( p_access, "Digest Access Authentication: "
1854 "Unknown algorithm '%s'", p_auth->psz_algorithm );
1858 if( p_auth->psz_qop || !p_auth->psz_cnonce )
1860 /* FIXME: needs to be really random to prevent man in the middle
1862 free( p_auth->psz_cnonce );
1863 p_auth->psz_cnonce = strdup( "Some random string FIXME" );
1867 psz_response = AuthDigest( p_access, p_url, p_auth, "GET" );
1868 if( !psz_response ) return;
1870 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1871 "%sAuthorization: Digest "
1872 /* Mandatory parameters */
1878 /* Optional parameters */
1879 "%s%s%s" /* algorithm */
1880 "%s%s%s" /* cnonce */
1881 "%s%s%s" /* opaque */
1882 "%s%s%s" /* message qop */
1883 "%s%08x%s" /* nonce count */
1885 /* Mandatory parameters */
1890 p_url->psz_path ?: "/",
1892 /* Optional parameters */
1893 p_auth->psz_algorithm ? "algorithm=\"" : "",
1894 p_auth->psz_algorithm ?: "",
1895 p_auth->psz_algorithm ? "\", " : "",
1896 p_auth->psz_cnonce ? "cnonce=\"" : "",
1897 p_auth->psz_cnonce ?: "",
1898 p_auth->psz_cnonce ? "\", " : "",
1899 p_auth->psz_opaque ? "opaque=\"" : "",
1900 p_auth->psz_opaque ?: "",
1901 p_auth->psz_opaque ? "\", " : "",
1902 p_auth->psz_qop ? "qop=\"" : "",
1903 p_auth->psz_qop ?: "",
1904 p_auth->psz_qop ? "\", " : "",
1905 p_auth->i_nonce ? "nc=\"" : "uglyhack=\"", /* Will be parsed as an unhandled extension */
1907 p_auth->i_nonce ? "\"" : "\""
1910 free( psz_response );
1914 /* Basic Access Authentication */
1915 char buf[strlen( psz_username ) + strlen( psz_password ) + 2];
1918 snprintf( buf, sizeof( buf ), "%s:%s", psz_username, psz_password );
1919 b64 = vlc_b64_encode( buf );
1923 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1924 "%sAuthorization: Basic %s\r\n", psz_prefix, b64 );
1930 static int AuthCheckReply( access_t *p_access, const char *psz_header,
1931 vlc_url_t *p_url, http_auth_t *p_auth )
1933 int i_ret = VLC_EGENERIC;
1934 char *psz_nextnonce = AuthGetParam( psz_header, "nextnonce" );
1935 char *psz_qop = AuthGetParamNoQuotes( psz_header, "qop" );
1936 char *psz_rspauth = AuthGetParam( psz_header, "rspauth" );
1937 char *psz_cnonce = AuthGetParam( psz_header, "cnonce" );
1938 char *psz_nc = AuthGetParamNoQuotes( psz_header, "nc" );
1944 if( strcmp( psz_cnonce, p_auth->psz_cnonce ) )
1946 msg_Err( p_access, "HTTP Digest Access Authentication: server replied with a different client nonce value." );
1953 i_nonce = strtol( psz_nc, NULL, 16 );
1954 if( i_nonce != p_auth->i_nonce )
1956 msg_Err( p_access, "HTTP Digest Access Authentication: server replied with a different nonce count value." );
1961 if( psz_qop && p_auth->psz_qop && strcmp( psz_qop, p_auth->psz_qop ) )
1962 msg_Warn( p_access, "HTTP Digest Access Authentication: server replied using a different 'quality of protection' option" );
1964 /* All the clear text values match, let's now check the response
1966 psz_digest = AuthDigest( p_access, p_url, p_auth, "" );
1967 if( strcmp( psz_digest, psz_rspauth ) )
1969 msg_Err( p_access, "HTTP Digest Access Authentication: server replied with an invalid response digest (expected value: %s).", psz_digest );
1978 free( p_auth->psz_nonce );
1979 p_auth->psz_nonce = psz_nextnonce;
1980 psz_nextnonce = NULL;
1983 i_ret = VLC_SUCCESS;
1985 free( psz_nextnonce );
1987 free( psz_rspauth );
1994 static void AuthReset( http_auth_t *p_auth )
1996 FREENULL( p_auth->psz_realm );
1997 FREENULL( p_auth->psz_domain );
1998 FREENULL( p_auth->psz_nonce );
1999 FREENULL( p_auth->psz_opaque );
2000 FREENULL( p_auth->psz_stale );
2001 FREENULL( p_auth->psz_algorithm );
2002 FREENULL( p_auth->psz_qop );
2003 p_auth->i_nonce = 0;
2004 FREENULL( p_auth->psz_cnonce );
2005 FREENULL( p_auth->psz_HA1 );