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>
60 /*****************************************************************************
62 *****************************************************************************/
63 static int Open ( vlc_object_t * );
64 static void Close( vlc_object_t * );
66 #define PROXY_TEXT N_("HTTP proxy")
67 #define PROXY_LONGTEXT N_( \
68 "HTTP proxy to be used It must be of the form " \
69 "http://[user@]myproxy.mydomain:myport/ ; " \
70 "if empty, the http_proxy environment variable will be tried." )
72 #define PROXY_PASS_TEXT N_("HTTP proxy password")
73 #define PROXY_PASS_LONGTEXT N_( \
74 "If your HTTP proxy requires a password, set it here." )
76 #define CACHING_TEXT N_("Caching value in ms")
77 #define CACHING_LONGTEXT N_( \
78 "Caching value for HTTP streams. This " \
79 "value should be set in milliseconds." )
81 #define AGENT_TEXT N_("HTTP user agent")
82 #define AGENT_LONGTEXT N_("User agent that will be " \
83 "used for the connection.")
85 #define RECONNECT_TEXT N_("Auto re-connect")
86 #define RECONNECT_LONGTEXT N_( \
87 "Automatically try to reconnect to the stream in case of a sudden " \
90 #define CONTINUOUS_TEXT N_("Continuous stream")
91 #define CONTINUOUS_LONGTEXT N_("Read a file that is " \
92 "being constantly updated (for example, a JPG file on a server). " \
93 "You should not globally enable this option as it will break all other " \
94 "types of HTTP streams." )
96 #define FORWARD_COOKIES_TEXT N_("Forward Cookies")
97 #define FORWARD_COOKIES_LONGTEXT N_("Forward Cookies across http redirections.")
99 #define MAX_REDIRECT_TEXT N_("Max number of redirection")
100 #define MAX_REDIRECT_LONGTEXT N_("Limit the number of redirection to follow.")
103 set_description( N_("HTTP input") )
104 set_capability( "access", 0 )
105 set_shortname( N_( "HTTP(S)" ) )
106 set_category( CAT_INPUT )
107 set_subcategory( SUBCAT_INPUT_ACCESS )
109 add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
111 add_password( "http-proxy-pwd", NULL, NULL,
112 PROXY_PASS_TEXT, PROXY_PASS_LONGTEXT, false )
113 add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
114 CACHING_TEXT, CACHING_LONGTEXT, true )
116 add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
117 AGENT_LONGTEXT, true )
119 add_bool( "http-reconnect", 0, NULL, RECONNECT_TEXT,
120 RECONNECT_LONGTEXT, true )
121 add_bool( "http-continuous", 0, NULL, CONTINUOUS_TEXT,
122 CONTINUOUS_LONGTEXT, true )
124 add_bool( "http-forward-cookies", true, NULL, FORWARD_COOKIES_TEXT,
125 FORWARD_COOKIES_LONGTEXT, true )
126 add_integer( "http-max-redirect", 5, NULL, MAX_REDIRECT_TEXT,
127 MAX_REDIRECT_LONGTEXT, true )
128 add_obsolete_string("http-user")
129 add_obsolete_string("http-pwd")
130 add_shortcut( "http" )
131 add_shortcut( "https" )
132 add_shortcut( "unsv" )
133 add_shortcut( "itpc" ) /* iTunes Podcast */
134 add_shortcut( "icyx" )
135 set_callbacks( Open, Close )
138 /*****************************************************************************
140 *****************************************************************************/
145 tls_session_t *p_tls;
150 char *psz_user_agent;
156 http_auth_t proxy_auth;
157 char *psz_proxy_passbuf;
161 const char *psz_protocol;
183 int64_t i_icy_offset;
196 vlc_array_t * cookies;
200 static int OpenWithCookies( vlc_object_t *p_this, const char *psz_access,
201 int i_nb_redirect, int i_max_redirect,
202 vlc_array_t *cookies );
205 static ssize_t Read( access_t *, uint8_t *, size_t );
206 static ssize_t ReadCompressed( access_t *, uint8_t *, size_t );
207 static int Seek( access_t *, int64_t );
208 static int Control( access_t *, int, va_list );
211 static int Connect( access_t *, int64_t );
212 static int Request( access_t *p_access, int64_t i_tell );
213 static void Disconnect( access_t * );
215 /* Small Cookie utilities. Cookies support is partial. */
216 static char * cookie_get_content( const char * cookie );
217 static char * cookie_get_domain( const char * cookie );
218 static char * cookie_get_name( const char * cookie );
219 static void cookie_append( vlc_array_t * cookies, char * cookie );
222 static void AuthReply( access_t *p_acces, const char *psz_prefix,
223 vlc_url_t *p_url, http_auth_t *p_auth );
224 static int AuthCheckReply( access_t *p_access, const char *psz_header,
225 vlc_url_t *p_url, http_auth_t *p_auth );
227 /*****************************************************************************
229 *****************************************************************************/
230 static int Open( vlc_object_t *p_this )
232 access_t *p_access = (access_t*)p_this;
233 return OpenWithCookies( p_this, p_access->psz_access, 0,
234 var_CreateGetInteger( p_access, "http-max-redirect" ), NULL );
238 * Open the given url using the given cookies
239 * @param p_this: the vlc object
240 * @psz_access: the acces to use (http, https, ...) (this value must be used
241 * instead of p_access->psz_access)
242 * @i_nb_redirect: the number of redirection already done
243 * @i_max_redirect: limit to the number of redirection to follow
244 * @cookies: the available cookies
245 * @return vlc error codes
247 static int OpenWithCookies( vlc_object_t *p_this, const char *psz_access,
248 int i_nb_redirect, int i_max_redirect,
249 vlc_array_t *cookies )
251 access_t *p_access = (access_t*)p_this;
255 /* Only forward an store cookies if the corresponding option is activated */
256 bool b_forward_cookies = var_CreateGetBool( p_access, "http-forward-cookies" );
257 vlc_array_t * saved_cookies = b_forward_cookies ? (cookies ? cookies : vlc_array_new()) : NULL;
259 /* Set up p_access */
260 STANDARD_READ_ACCESS_INIT;
262 p_access->pf_read = ReadCompressed;
265 p_sys->b_proxy = false;
266 p_sys->psz_proxy_passbuf = NULL;
267 p_sys->i_version = 1;
268 p_sys->b_seekable = true;
269 p_sys->psz_mime = NULL;
270 p_sys->psz_pragma = NULL;
271 p_sys->b_mms = false;
272 p_sys->b_icecast = false;
273 p_sys->psz_location = NULL;
274 p_sys->psz_user_agent = NULL;
275 p_sys->b_pace_control = true;
276 p_sys->b_ssl = false;
278 p_sys->b_compressed = false;
279 /* 15 is the max windowBits, +32 to enable optional gzip decoding */
280 if( inflateInit2( &p_sys->inflate.stream, 32+15 ) != Z_OK )
281 msg_Warn( p_access, "Error during zlib initialisation: %s",
282 p_sys->inflate.stream.msg );
283 if( zlibCompileFlags() & (1<<17) )
284 msg_Warn( p_access, "Your zlib was compiled without gzip support." );
285 p_sys->inflate.p_buffer = NULL;
289 p_sys->i_icy_meta = 0;
290 p_sys->i_icy_offset = 0;
291 p_sys->psz_icy_name = NULL;
292 p_sys->psz_icy_genre = NULL;
293 p_sys->psz_icy_title = NULL;
294 p_sys->i_remaining = 0;
295 p_sys->b_persist = false;
296 p_access->info.i_size = -1;
297 p_access->info.i_pos = 0;
298 p_access->info.b_eof = false;
300 p_sys->cookies = saved_cookies;
302 http_auth_Init( &p_sys->auth );
303 http_auth_Init( &p_sys->proxy_auth );
305 /* Parse URI - remove spaces */
306 p = psz = strdup( p_access->psz_path );
307 while( (p = strchr( p, ' ' )) != NULL )
309 vlc_UrlParse( &p_sys->url, psz, 0 );
312 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
314 msg_Warn( p_access, "invalid host" );
317 if( !strncmp( psz_access, "https", 5 ) )
321 if( p_sys->url.i_port <= 0 )
322 p_sys->url.i_port = 443;
326 if( p_sys->url.i_port <= 0 )
327 p_sys->url.i_port = 80;
331 p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
334 psz = var_CreateGetNonEmptyString( p_access, "http-proxy" );
337 p_sys->b_proxy = true;
338 vlc_UrlParse( &p_sys->proxy, psz, 0 );
344 pxProxyFactory *pf = px_proxy_factory_new();
349 i=asprintf(&buf, "%s://%s", psz_access, p_access->psz_path);
352 msg_Dbg(p_access, "asking libproxy about url '%s'", buf);
353 char **proxies = px_proxy_factory_get_proxies(pf, buf);
356 msg_Dbg(p_access, "libproxy suggest to use '%s'", proxies[0]);
357 if(strcmp(proxies[0],"direct://") != 0)
359 p_sys->b_proxy = true;
360 vlc_UrlParse( &p_sys->proxy, proxies[0], 0);
363 for(i=0;proxies[i];i++) free(proxies[i]);
367 px_proxy_factory_free(pf);
371 msg_Err(p_access, "Allocating memory for libproxy failed");
377 psz = getenv( "http_proxy" );
380 p_sys->b_proxy = true;
381 vlc_UrlParse( &p_sys->proxy, psz, 0 );
385 if( psz ) /* No, this is NOT a use-after-free error */
387 psz = var_CreateGetNonEmptyString( p_access, "http-proxy-pwd" );
389 p_sys->proxy.psz_password = p_sys->psz_proxy_passbuf = psz;
394 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
396 msg_Warn( p_access, "invalid proxy host" );
399 if( p_sys->proxy.i_port <= 0 )
401 p_sys->proxy.i_port = 80;
405 msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
406 p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
409 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
410 p_sys->proxy.i_port );
412 if( p_sys->url.psz_username && *p_sys->url.psz_username )
414 msg_Dbg( p_access, " user='%s'", p_sys->url.psz_username );
417 p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
418 p_sys->b_continuous = var_CreateGetBool( p_access, "http-continuous" );
422 switch( Connect( p_access, 0 ) )
428 /* Retry with http 1.0 */
429 msg_Dbg( p_access, "switching to HTTP version 1.0" );
430 p_sys->i_version = 0;
431 p_sys->b_seekable = false;
433 if( !vlc_object_alive (p_access) || Connect( p_access, 0 ) )
441 msg_Err( p_access, "You should not be here" );
446 if( p_sys->i_code == 401 )
448 char *psz_login, *psz_password;
450 if( p_sys->url.psz_username && p_sys->url.psz_password &&
451 p_sys->auth.psz_nonce && p_sys->auth.i_nonce == 0 )
453 Disconnect( p_access );
456 msg_Dbg( p_access, "authentication failed for realm %s",
457 p_sys->auth.psz_realm );
458 dialog_Login( p_access, &psz_login, &psz_password,
459 _("HTTP authentication"),
460 _("Please enter a valid login name and a password for realm %s."),
461 p_sys->auth.psz_realm );
462 if( psz_login != NULL && psz_password != NULL )
464 msg_Dbg( p_access, "retrying with user=%s", psz_login );
465 p_sys->url.psz_username = psz_login;
466 p_sys->url.psz_password = psz_password;
467 Disconnect( p_access );
473 free( psz_password );
478 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
479 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
480 p_sys->psz_location && *p_sys->psz_location )
482 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
484 /* Check the number of redirection already done */
485 if( i_nb_redirect >= i_max_redirect )
487 msg_Err( p_access, "Too many redirection: break potential infinite"
493 /* Do not accept redirection outside of HTTP works */
494 const char *psz_protocol;
495 if( !strncmp( p_sys->psz_location, "http:", 5 ) )
496 psz_protocol = "http";
497 else if( !strncmp( p_sys->psz_location, "https:", 6 ) )
498 psz_protocol = "https";
501 msg_Err( p_access, "insecure redirection ignored" );
504 free( p_access->psz_path );
505 p_access->psz_path = strdup( p_sys->psz_location );
506 /* Clean up current Open() run */
507 vlc_UrlClean( &p_sys->url );
508 http_auth_Reset( &p_sys->auth );
509 vlc_UrlClean( &p_sys->proxy );
510 free( p_sys->psz_proxy_passbuf );
511 http_auth_Reset( &p_sys->proxy_auth );
512 free( p_sys->psz_mime );
513 free( p_sys->psz_pragma );
514 free( p_sys->psz_location );
515 free( p_sys->psz_user_agent );
517 Disconnect( p_access );
518 cookies = p_sys->cookies;
520 inflateEnd( &p_sys->inflate.stream );
524 /* Do new Open() run with new data */
525 return OpenWithCookies( p_this, psz_protocol, i_nb_redirect + 1,
526 i_max_redirect, cookies );
531 msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
535 if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
537 if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
539 if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
540 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
542 free( p_access->psz_demux );
543 p_access->psz_demux = strdup( "nsv" );
545 else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
546 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
548 free( p_access->psz_demux );
549 p_access->psz_demux = strdup( "m4a" );
551 else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
553 free( p_access->psz_demux );
554 p_access->psz_demux = strdup( "mp3" );
557 msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
558 p_access->psz_demux );
560 #if 0 /* Doesn't work really well because of the pre-buffering in
561 * shoutcast servers (the buffer content will be sent as fast as
563 p_sys->b_pace_control = false;
566 else if( !p_sys->psz_mime )
568 free( p_access->psz_demux );
570 p_access->psz_demux = strdup( "mp3" );
572 /* else probably Ogg Vorbis */
574 else if( !strcasecmp( psz_access, "unsv" ) &&
576 !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
578 free( p_access->psz_demux );
579 /* Grrrr! detect ultravox server and force NSV demuxer */
580 p_access->psz_demux = strdup( "nsv" );
582 else if( !strcmp( psz_access, "itpc" ) )
584 free( p_access->psz_demux );
585 p_access->psz_demux = strdup( "podcast" );
587 else if( p_sys->psz_mime &&
588 !strncasecmp( p_sys->psz_mime, "application/xspf+xml", 20 ) &&
589 ( memchr( " ;\t", p_sys->psz_mime[20], 4 ) != NULL ) )
591 free( p_access->psz_demux );
592 p_access->psz_demux = strdup( "xspf-open" );
595 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
598 var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
603 vlc_UrlClean( &p_sys->url );
604 vlc_UrlClean( &p_sys->proxy );
605 free( p_sys->psz_proxy_passbuf );
606 free( p_sys->psz_mime );
607 free( p_sys->psz_pragma );
608 free( p_sys->psz_location );
609 free( p_sys->psz_user_agent );
611 Disconnect( p_access );
616 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
617 free(vlc_array_item_at_index( p_sys->cookies, i ));
618 vlc_array_destroy( p_sys->cookies );
622 inflateEnd( &p_sys->inflate.stream );
628 /*****************************************************************************
630 *****************************************************************************/
631 static void Close( vlc_object_t *p_this )
633 access_t *p_access = (access_t*)p_this;
634 access_sys_t *p_sys = p_access->p_sys;
636 vlc_UrlClean( &p_sys->url );
637 http_auth_Reset( &p_sys->auth );
638 vlc_UrlClean( &p_sys->proxy );
639 http_auth_Reset( &p_sys->proxy_auth );
641 free( p_sys->psz_mime );
642 free( p_sys->psz_pragma );
643 free( p_sys->psz_location );
645 free( p_sys->psz_icy_name );
646 free( p_sys->psz_icy_genre );
647 free( p_sys->psz_icy_title );
649 free( p_sys->psz_user_agent );
651 Disconnect( p_access );
656 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
657 free(vlc_array_item_at_index( p_sys->cookies, i ));
658 vlc_array_destroy( p_sys->cookies );
662 inflateEnd( &p_sys->inflate.stream );
663 free( p_sys->inflate.p_buffer );
669 /*****************************************************************************
670 * Read: Read up to i_len bytes from the http connection and place in
671 * p_buffer. Return the actual number of bytes read
672 *****************************************************************************/
673 static int ReadICYMeta( access_t *p_access );
674 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
676 access_sys_t *p_sys = p_access->p_sys;
679 if( p_sys->fd == -1 )
681 p_access->info.b_eof = true;
685 if( p_access->info.i_size >= 0 &&
686 i_len + p_access->info.i_pos > p_access->info.i_size )
688 if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
690 p_access->info.b_eof = true;
695 if( p_sys->b_chunked )
697 if( p_sys->i_chunk < 0 )
699 p_access->info.b_eof = true;
703 if( p_sys->i_chunk <= 0 )
705 char *psz = net_Gets( p_access, p_sys->fd, p_sys->p_vs );
706 /* read the chunk header */
709 /* fatal error - end of file */
710 msg_Dbg( p_access, "failed reading chunk-header line" );
713 p_sys->i_chunk = strtoll( psz, NULL, 16 );
716 if( p_sys->i_chunk <= 0 ) /* eof */
719 p_access->info.b_eof = true;
724 if( i_len > p_sys->i_chunk )
726 i_len = p_sys->i_chunk;
729 else if( p_access->info.i_size != -1 && (int64_t)i_len > p_sys->i_remaining) {
730 /* Only ask for the remaining length */
731 i_len = (size_t)p_sys->i_remaining;
733 p_access->info.b_eof = true;
739 if( p_sys->i_icy_meta > 0 && p_access->info.i_pos-p_sys->i_icy_offset > 0 )
741 int64_t i_next = p_sys->i_icy_meta -
742 (p_access->info.i_pos - p_sys->i_icy_offset ) % p_sys->i_icy_meta;
744 if( i_next == p_sys->i_icy_meta )
746 if( ReadICYMeta( p_access ) )
748 p_access->info.b_eof = true;
756 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, false );
760 p_access->info.i_pos += i_read;
762 if( p_sys->b_chunked )
764 p_sys->i_chunk -= i_read;
765 if( p_sys->i_chunk <= 0 )
767 /* read the empty line */
768 char *psz = net_Gets( p_access, p_sys->fd, p_sys->p_vs );
773 else if( i_read <= 0 )
776 * I very much doubt that this will work.
777 * If i_read == 0, the connection *IS* dead, so the only
778 * sensible thing to do is Disconnect() and then retry.
779 * Otherwise, I got recv() completely wrong. -- Courmisch
781 if( p_sys->b_continuous )
783 Request( p_access, 0 );
784 p_sys->b_continuous = false;
785 i_read = Read( p_access, p_buffer, i_len );
786 p_sys->b_continuous = true;
788 Disconnect( p_access );
789 if( p_sys->b_reconnect && vlc_object_alive( p_access ) )
791 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
792 if( Connect( p_access, p_access->info.i_pos ) )
794 msg_Dbg( p_access, "reconnection failed" );
798 p_sys->b_reconnect = false;
799 i_read = Read( p_access, p_buffer, i_len );
800 p_sys->b_reconnect = true;
805 p_access->info.b_eof = true;
806 else if( i_read < 0 )
807 p_access->b_error = true;
810 if( p_access->info.i_size != -1 )
812 p_sys->i_remaining -= i_read;
818 static int ReadICYMeta( access_t *p_access )
820 access_sys_t *p_sys = p_access->p_sys;
826 /* Read meta data length */
827 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
834 i_read = buffer << 4;
835 /* msg_Dbg( p_access, "ICY meta size=%u", i_read); */
837 psz_meta = malloc( i_read + 1 );
838 if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
839 (uint8_t *)psz_meta, i_read, true ) != i_read )
845 psz_meta[i_read] = '\0'; /* Just in case */
847 /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
849 /* Now parse the meta */
850 /* Look for StreamTitle= */
851 p = strcasestr( (char *)psz_meta, "StreamTitle=" );
854 p += strlen( "StreamTitle=" );
855 if( *p == '\'' || *p == '"' )
857 char closing[] = { p[0], ';', '\0' };
858 char *psz = strstr( &p[1], closing );
860 psz = strchr( &p[1], ';' );
862 if( psz ) *psz = '\0';
866 char *psz = strchr( &p[1], ';' );
867 if( psz ) *psz = '\0';
870 if( !p_sys->psz_icy_title ||
871 strcmp( p_sys->psz_icy_title, &p[1] ) )
873 free( p_sys->psz_icy_title );
874 char *psz_tmp = strdup( &p[1] );
875 p_sys->psz_icy_title = EnsureUTF8( psz_tmp );
876 if( !p_sys->psz_icy_title )
878 p_access->info.i_update |= INPUT_UPDATE_META;
880 msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
889 static ssize_t ReadCompressed( access_t *p_access, uint8_t *p_buffer,
892 access_sys_t *p_sys = p_access->p_sys;
894 if( p_sys->b_compressed )
898 if( !p_sys->inflate.p_buffer )
899 p_sys->inflate.p_buffer = malloc( 256 * 1024 );
901 if( p_sys->inflate.stream.avail_in == 0 )
903 ssize_t i_read = Read( p_access, p_sys->inflate.p_buffer + p_sys->inflate.stream.avail_in, 256 * 1024 );
904 if( i_read <= 0 ) return i_read;
905 p_sys->inflate.stream.next_in = p_sys->inflate.p_buffer;
906 p_sys->inflate.stream.avail_in = i_read;
909 p_sys->inflate.stream.avail_out = i_len;
910 p_sys->inflate.stream.next_out = p_buffer;
912 i_ret = inflate( &p_sys->inflate.stream, Z_SYNC_FLUSH );
913 msg_Warn( p_access, "inflate return value: %d, %s", i_ret, p_sys->inflate.stream.msg );
915 return i_len - p_sys->inflate.stream.avail_out;
919 return Read( p_access, p_buffer, i_len );
924 /*****************************************************************************
925 * Seek: close and re-open a connection at the right place
926 *****************************************************************************/
927 static int Seek( access_t *p_access, int64_t i_pos )
929 msg_Dbg( p_access, "trying to seek to %"PRId64, i_pos );
931 Disconnect( p_access );
933 if( p_access->info.i_size
934 && (uint64_t)i_pos >= (uint64_t)p_access->info.i_size ) {
935 msg_Err( p_access, "seek to far" );
936 int retval = Seek( p_access, p_access->info.i_size - 1 );
937 if( retval == VLC_SUCCESS ) {
939 Read( p_access, p_buffer, 1);
940 p_access->info.b_eof = false;
944 if( Connect( p_access, i_pos ) )
946 msg_Err( p_access, "seek failed" );
947 p_access->info.b_eof = true;
953 /*****************************************************************************
955 *****************************************************************************/
956 static int Control( access_t *p_access, int i_query, va_list args )
958 access_sys_t *p_sys = p_access->p_sys;
966 case ACCESS_CAN_SEEK:
967 pb_bool = (bool*)va_arg( args, bool* );
968 *pb_bool = p_sys->b_seekable;
970 case ACCESS_CAN_FASTSEEK:
971 pb_bool = (bool*)va_arg( args, bool* );
974 case ACCESS_CAN_PAUSE:
975 case ACCESS_CAN_CONTROL_PACE:
976 pb_bool = (bool*)va_arg( args, bool* );
978 #if 0 /* Disable for now until we have a clock synchro algo
979 * which works with something else than MPEG over UDP */
980 *pb_bool = p_sys->b_pace_control;
986 case ACCESS_GET_PTS_DELAY:
987 pi_64 = (int64_t*)va_arg( args, int64_t * );
988 *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
992 case ACCESS_SET_PAUSE_STATE:
995 case ACCESS_GET_META:
996 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
998 if( p_sys->psz_icy_name )
999 vlc_meta_Set( p_meta, vlc_meta_Title, p_sys->psz_icy_name );
1000 if( p_sys->psz_icy_genre )
1001 vlc_meta_Set( p_meta, vlc_meta_Genre, p_sys->psz_icy_genre );
1002 if( p_sys->psz_icy_title )
1003 vlc_meta_Set( p_meta, vlc_meta_NowPlaying, p_sys->psz_icy_title );
1006 case ACCESS_GET_CONTENT_TYPE:
1007 *va_arg( args, char ** ) =
1008 p_sys->psz_mime ? strdup( p_sys->psz_mime ) : NULL;
1011 case ACCESS_GET_TITLE_INFO:
1012 case ACCESS_SET_TITLE:
1013 case ACCESS_SET_SEEKPOINT:
1014 case ACCESS_SET_PRIVATE_ID_STATE:
1015 return VLC_EGENERIC;
1018 msg_Warn( p_access, "unimplemented query in control" );
1019 return VLC_EGENERIC;
1025 /*****************************************************************************
1027 *****************************************************************************/
1028 static int Connect( access_t *p_access, int64_t i_tell )
1030 access_sys_t *p_sys = p_access->p_sys;
1031 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
1034 free( p_sys->psz_location );
1035 free( p_sys->psz_mime );
1036 free( p_sys->psz_pragma );
1038 free( p_sys->psz_icy_genre );
1039 free( p_sys->psz_icy_name );
1040 free( p_sys->psz_icy_title );
1043 p_sys->psz_location = NULL;
1044 p_sys->psz_mime = NULL;
1045 p_sys->psz_pragma = NULL;
1046 p_sys->b_mms = false;
1047 p_sys->b_chunked = false;
1049 p_sys->i_icy_meta = 0;
1050 p_sys->i_icy_offset = i_tell;
1051 p_sys->psz_icy_name = NULL;
1052 p_sys->psz_icy_genre = NULL;
1053 p_sys->psz_icy_title = NULL;
1054 p_sys->i_remaining = 0;
1055 p_sys->b_persist = false;
1057 p_access->info.i_size = -1;
1058 p_access->info.i_pos = i_tell;
1059 p_access->info.b_eof = false;
1061 /* Open connection */
1062 assert( p_sys->fd == -1 ); /* No open sockets (leaking fds is BAD) */
1063 p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
1064 if( p_sys->fd == -1 )
1066 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
1069 setsockopt (p_sys->fd, SOL_SOCKET, SO_KEEPALIVE, &(int){ 1 }, sizeof (int));
1071 /* Initialize TLS/SSL session */
1072 if( p_sys->b_ssl == true )
1074 /* CONNECT to establish TLS tunnel through HTTP proxy */
1075 if( p_sys->b_proxy )
1078 unsigned i_status = 0;
1080 if( p_sys->i_version == 0 )
1082 /* CONNECT is not in HTTP/1.0 */
1083 Disconnect( p_access );
1087 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
1088 "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
1089 p_sys->url.psz_host, p_sys->url.i_port,
1091 p_sys->url.psz_host, p_sys->url.i_port);
1093 psz = net_Gets( p_access, p_sys->fd, NULL );
1096 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
1097 Disconnect( p_access );
1101 sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
1104 if( ( i_status / 100 ) != 2 )
1106 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
1107 Disconnect( p_access );
1113 psz = net_Gets( p_access, p_sys->fd, NULL );
1116 msg_Err( p_access, "HTTP proxy connection failed" );
1117 Disconnect( p_access );
1126 if( !vlc_object_alive (p_access) || p_access->b_error )
1128 Disconnect( p_access );
1135 /* TLS/SSL handshake */
1136 p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
1137 p_sys->url.psz_host );
1138 if( p_sys->p_tls == NULL )
1140 msg_Err( p_access, "cannot establish HTTP/TLS session" );
1141 Disconnect( p_access );
1144 p_sys->p_vs = &p_sys->p_tls->sock;
1147 return Request( p_access, i_tell ) ? -2 : 0;
1151 static int Request( access_t *p_access, int64_t i_tell )
1153 access_sys_t *p_sys = p_access->p_sys;
1155 v_socket_t *pvs = p_sys->p_vs;
1156 p_sys->b_persist = false;
1158 p_sys->i_remaining = 0;
1159 if( p_sys->b_proxy )
1161 if( p_sys->url.psz_path )
1163 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
1164 "GET http://%s:%d%s HTTP/1.%d\r\n",
1165 p_sys->url.psz_host, p_sys->url.i_port,
1166 p_sys->url.psz_path, p_sys->i_version );
1170 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
1171 "GET http://%s:%d/ HTTP/1.%d\r\n",
1172 p_sys->url.psz_host, p_sys->url.i_port,
1178 const char *psz_path = p_sys->url.psz_path;
1179 if( !psz_path || !*psz_path )
1183 if( p_sys->url.i_port != (pvs ? 443 : 80) )
1185 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1186 "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
1187 psz_path, p_sys->i_version, p_sys->url.psz_host,
1188 p_sys->url.i_port );
1192 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1193 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
1194 psz_path, p_sys->i_version, p_sys->url.psz_host );
1198 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "User-Agent: %s\r\n",
1199 p_sys->psz_user_agent );
1201 if( p_sys->i_version == 1 && ! p_sys->b_continuous )
1203 p_sys->b_persist = true;
1204 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1205 "Range: bytes=%"PRIu64"-\r\n", i_tell );
1209 if( p_sys->cookies )
1212 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
1214 const char * cookie = vlc_array_item_at_index( p_sys->cookies, i );
1215 char * psz_cookie_content = cookie_get_content( cookie );
1216 char * psz_cookie_domain = cookie_get_domain( cookie );
1218 assert( psz_cookie_content );
1220 /* FIXME: This is clearly not conforming to the rfc */
1221 bool is_in_right_domain = (!psz_cookie_domain || strstr( p_sys->url.psz_host, psz_cookie_domain ));
1223 if( is_in_right_domain )
1225 msg_Dbg( p_access, "Sending Cookie %s", psz_cookie_content );
1226 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Cookie: %s\r\n", psz_cookie_content ) < 0 )
1227 msg_Err( p_access, "failed to send Cookie" );
1229 free( psz_cookie_content );
1230 free( psz_cookie_domain );
1234 /* Authentication */
1235 if( p_sys->url.psz_username || p_sys->url.psz_password )
1236 AuthReply( p_access, "", &p_sys->url, &p_sys->auth );
1238 /* Proxy Authentication */
1239 if( p_sys->proxy.psz_username || p_sys->proxy.psz_password )
1240 AuthReply( p_access, "Proxy-", &p_sys->proxy, &p_sys->proxy_auth );
1242 /* ICY meta data request */
1243 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
1246 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "\r\n" ) < 0 )
1248 msg_Err( p_access, "failed to send request" );
1249 Disconnect( p_access );
1250 return VLC_EGENERIC;
1254 if( ( psz = net_Gets( p_access, p_sys->fd, pvs ) ) == NULL )
1256 msg_Err( p_access, "failed to read answer" );
1259 if( !strncmp( psz, "HTTP/1.", 7 ) )
1261 p_sys->psz_protocol = "HTTP";
1262 p_sys->i_code = atoi( &psz[9] );
1264 else if( !strncmp( psz, "ICY", 3 ) )
1266 p_sys->psz_protocol = "ICY";
1267 p_sys->i_code = atoi( &psz[4] );
1268 p_sys->b_reconnect = true;
1272 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1276 msg_Dbg( p_access, "protocol '%s' answer code %d",
1277 p_sys->psz_protocol, p_sys->i_code );
1278 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1280 p_sys->b_seekable = false;
1282 if( p_sys->i_code != 206 && p_sys->i_code != 401 )
1284 p_sys->b_seekable = false;
1286 /* Authentication error - We'll have to display the dialog */
1287 if( p_sys->i_code == 401 )
1291 /* Other fatal error */
1292 else if( p_sys->i_code >= 400 )
1294 msg_Err( p_access, "error: %s", psz );
1302 char *psz = net_Gets( p_access, p_sys->fd, pvs );
1307 msg_Err( p_access, "failed to read answer" );
1311 if( !vlc_object_alive (p_access) || p_access->b_error )
1317 /* msg_Dbg( p_input, "Line=%s", psz ); */
1324 if( ( p = strchr( psz, ':' ) ) == NULL )
1326 msg_Err( p_access, "malformed header line: %s", psz );
1331 while( *p == ' ' ) p++;
1333 if( !strcasecmp( psz, "Content-Length" ) )
1335 int64_t i_size = i_tell + (p_sys->i_remaining = atoll( p ));
1336 if(i_size > p_access->info.i_size) {
1337 p_access->info.i_size = i_size;
1339 msg_Dbg( p_access, "this frame size=%"PRId64, p_sys->i_remaining );
1341 else if( !strcasecmp( psz, "Content-Range" ) ) {
1342 int64_t i_ntell = i_tell;
1343 int64_t i_nend = (p_access->info.i_size > 0)?(p_access->info.i_size - 1):i_tell;
1344 int64_t i_nsize = p_access->info.i_size;
1345 sscanf(p,"bytes %"PRId64"-%"PRId64"/%"PRId64,&i_ntell,&i_nend,&i_nsize);
1346 if(i_nend > i_ntell ) {
1347 p_access->info.i_pos = i_ntell;
1348 p_sys->i_remaining = i_nend+1-i_ntell;
1349 int64_t i_size = (i_nsize > i_nend) ? i_nsize : (i_nend + 1);
1350 if(i_size > p_access->info.i_size) {
1351 p_access->info.i_size = i_size;
1353 msg_Dbg( p_access, "stream size=%"PRId64",pos=%"PRId64",remaining=%"PRId64,i_nsize,i_ntell,p_sys->i_remaining);
1356 else if( !strcasecmp( psz, "Connection" ) ) {
1357 msg_Dbg( p_access, "Connection: %s",p );
1359 sscanf(p, "close%n",&i);
1361 p_sys->b_persist = false;
1364 else if( !strcasecmp( psz, "Location" ) )
1368 /* This does not follow RFC 2068, but yet if the url is not absolute,
1369 * handle it as everyone does. */
1372 const char *psz_http_ext = p_sys->b_ssl ? "s" : "" ;
1374 if( p_sys->url.i_port == ( p_sys->b_ssl ? 443 : 80 ) )
1376 if( asprintf(&psz_new_loc, "http%s://%s%s", psz_http_ext,
1377 p_sys->url.psz_host, p) < 0 )
1382 if( asprintf(&psz_new_loc, "http%s://%s:%d%s", psz_http_ext,
1383 p_sys->url.psz_host, p_sys->url.i_port, p) < 0 )
1389 psz_new_loc = strdup( p );
1392 free( p_sys->psz_location );
1393 p_sys->psz_location = psz_new_loc;
1395 else if( !strcasecmp( psz, "Content-Type" ) )
1397 free( p_sys->psz_mime );
1398 p_sys->psz_mime = strdup( p );
1399 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1401 else if( !strcasecmp( psz, "Content-Encoding" ) )
1403 msg_Dbg( p_access, "Content-Encoding: %s", p );
1404 if( strcasecmp( p, "identity" ) )
1406 p_sys->b_compressed = true;
1408 msg_Warn( p_access, "Compressed content not supported. Rebuild with zlib support." );
1411 else if( !strcasecmp( psz, "Pragma" ) )
1413 if( !strcasecmp( psz, "Pragma: features" ) )
1414 p_sys->b_mms = true;
1415 free( p_sys->psz_pragma );
1416 p_sys->psz_pragma = strdup( p );
1417 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1419 else if( !strcasecmp( psz, "Server" ) )
1421 msg_Dbg( p_access, "Server: %s", p );
1422 if( !strncasecmp( p, "Icecast", 7 ) ||
1423 !strncasecmp( p, "Nanocaster", 10 ) )
1425 /* Remember if this is Icecast
1426 * we need to force demux in this case without breaking
1429 /* Let live 365 streams (nanocaster) piggyback on the icecast
1430 * routine. They look very similar */
1432 p_sys->b_reconnect = true;
1433 p_sys->b_pace_control = false;
1434 p_sys->b_icecast = true;
1437 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1439 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1440 if( !strncasecmp( p, "chunked", 7 ) )
1442 p_sys->b_chunked = true;
1445 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1447 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1448 p_sys->i_icy_meta = atoi( p );
1449 if( p_sys->i_icy_meta < 0 )
1450 p_sys->i_icy_meta = 0;
1451 if( p_sys->i_icy_meta > 0 )
1452 p_sys->b_icecast = true;
1454 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1456 else if( !strcasecmp( psz, "Icy-Name" ) )
1458 free( p_sys->psz_icy_name );
1459 char *psz_tmp = strdup( p );
1460 p_sys->psz_icy_name = EnsureUTF8( psz_tmp );
1461 if( !p_sys->psz_icy_name )
1463 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1465 p_sys->b_icecast = true; /* be on the safeside. set it here as well. */
1466 p_sys->b_reconnect = true;
1467 p_sys->b_pace_control = false;
1469 else if( !strcasecmp( psz, "Icy-Genre" ) )
1471 free( p_sys->psz_icy_genre );
1472 char *psz_tmp = strdup( p );
1473 p_sys->psz_icy_genre = EnsureUTF8( psz_tmp );
1474 if( !p_sys->psz_icy_genre )
1476 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1478 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1480 msg_Dbg( p_access, "Icy-Notice: %s", p );
1482 else if( !strncasecmp( psz, "icy-", 4 ) ||
1483 !strncasecmp( psz, "ice-", 4 ) ||
1484 !strncasecmp( psz, "x-audiocast", 11 ) )
1486 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1488 else if( !strcasecmp( psz, "Set-Cookie" ) )
1490 if( p_sys->cookies )
1492 msg_Dbg( p_access, "Accepting Cookie: %s", p );
1493 cookie_append( p_sys->cookies, strdup(p) );
1496 msg_Dbg( p_access, "We have a Cookie we won't remember: %s", p );
1498 else if( !strcasecmp( psz, "www-authenticate" ) )
1500 msg_Dbg( p_access, "Authentication header: %s", p );
1501 http_auth_ParseWwwAuthenticateHeader( VLC_OBJECT(p_access),
1504 else if( !strcasecmp( psz, "proxy-authenticate" ) )
1506 msg_Dbg( p_access, "Proxy authentication header: %s", p );
1507 http_auth_ParseWwwAuthenticateHeader( VLC_OBJECT(p_access),
1508 &p_sys->proxy_auth, p );
1510 else if( !strcasecmp( psz, "authentication-info" ) )
1512 msg_Dbg( p_access, "Authentication Info header: %s", p );
1513 if( AuthCheckReply( p_access, p, &p_sys->url, &p_sys->auth ) )
1516 else if( !strcasecmp( psz, "proxy-authentication-info" ) )
1518 msg_Dbg( p_access, "Proxy Authentication Info header: %s", p );
1519 if( AuthCheckReply( p_access, p, &p_sys->proxy, &p_sys->proxy_auth ) )
1525 /* We close the stream for zero length data, unless of course the
1526 * server has already promised to do this for us.
1528 if( p_access->info.i_size != -1 && p_sys->i_remaining == 0 && p_sys->b_persist ) {
1529 Disconnect( p_access );
1534 Disconnect( p_access );
1535 return VLC_EGENERIC;
1538 /*****************************************************************************
1540 *****************************************************************************/
1541 static void Disconnect( access_t *p_access )
1543 access_sys_t *p_sys = p_access->p_sys;
1545 if( p_sys->p_tls != NULL)
1547 tls_ClientDelete( p_sys->p_tls );
1548 p_sys->p_tls = NULL;
1551 if( p_sys->fd != -1)
1553 net_Close(p_sys->fd);
1559 /*****************************************************************************
1560 * Cookies (FIXME: we may want to rewrite that using a nice structure to hold
1561 * them) (FIXME: only support the "domain=" param)
1562 *****************************************************************************/
1564 /* Get the NAME=VALUE part of the Cookie */
1565 static char * cookie_get_content( const char * cookie )
1567 char * ret = strdup( cookie );
1568 if( !ret ) return NULL;
1570 /* Look for a ';' */
1571 while( *str && *str != ';' ) str++;
1572 /* Replace it by a end-char */
1573 if( *str == ';' ) *str = 0;
1577 /* Get the domain where the cookie is stored */
1578 static char * cookie_get_domain( const char * cookie )
1580 const char * str = cookie;
1581 static const char domain[] = "domain=";
1584 /* Look for a ';' */
1587 if( !strncmp( str, domain, sizeof(domain) - 1 /* minus \0 */ ) )
1589 str += sizeof(domain) - 1 /* minus \0 */;
1590 char * ret = strdup( str );
1591 /* Now remove the next ';' if present */
1592 char * ret_iter = ret;
1593 while( *ret_iter && *ret_iter != ';' ) ret_iter++;
1594 if( *ret_iter == ';' )
1598 /* Go to next ';' field */
1599 while( *str && *str != ';' ) str++;
1600 if( *str == ';' ) str++;
1602 while( *str && *str == ' ' ) str++;
1607 /* Get NAME in the NAME=VALUE field */
1608 static char * cookie_get_name( const char * cookie )
1610 char * ret = cookie_get_content( cookie ); /* NAME=VALUE */
1611 if( !ret ) return NULL;
1613 while( *str && *str != '=' ) str++;
1618 /* Add a cookie in cookies, checking to see how it should be added */
1619 static void cookie_append( vlc_array_t * cookies, char * cookie )
1626 char * cookie_name = cookie_get_name( cookie );
1628 /* Don't send invalid cookies */
1632 char * cookie_domain = cookie_get_domain( cookie );
1633 for( i = 0; i < vlc_array_count( cookies ); i++ )
1635 char * current_cookie = vlc_array_item_at_index( cookies, i );
1636 char * current_cookie_name = cookie_get_name( current_cookie );
1637 char * current_cookie_domain = cookie_get_domain( current_cookie );
1639 assert( current_cookie_name );
1641 bool is_domain_matching = ( cookie_domain && current_cookie_domain &&
1642 !strcmp( cookie_domain, current_cookie_domain ) );
1644 if( is_domain_matching && !strcmp( cookie_name, current_cookie_name ) )
1646 /* Remove previous value for this cookie */
1647 free( current_cookie );
1648 vlc_array_remove( cookies, i );
1651 free( current_cookie_name );
1652 free( current_cookie_domain );
1655 free( current_cookie_name );
1656 free( current_cookie_domain );
1658 free( cookie_name );
1659 free( cookie_domain );
1660 vlc_array_append( cookies, cookie );
1664 /*****************************************************************************
1665 * HTTP authentication
1666 *****************************************************************************/
1667 static char *AuthGetParam( const char *psz_header, const char *psz_param )
1669 char psz_what[strlen(psz_param)+3];
1670 sprintf( psz_what, "%s=\"", psz_param );
1671 psz_header = strstr( psz_header, psz_what );
1674 const char *psz_end;
1675 psz_header += strlen( psz_what );
1676 psz_end = strchr( psz_header, '"' );
1677 if( !psz_end ) /* Invalid since we should have a closing quote */
1678 return strdup( psz_header );
1679 return strndup( psz_header, psz_end - psz_header );
1687 static char *AuthGetParamNoQuotes( const char *psz_header, const char *psz_param )
1689 char psz_what[strlen(psz_param)+2];
1690 sprintf( psz_what, "%s=", psz_param );
1691 psz_header = strstr( psz_header, psz_what );
1694 const char *psz_end;
1695 psz_header += strlen( psz_what );
1696 psz_end = strchr( psz_header, ',' );
1697 /* XXX: Do we need to filter out trailing space between the value and
1698 * the comma/end of line? */
1699 if( !psz_end ) /* Can be valid if this is the last parameter */
1700 return strdup( psz_header );
1701 return strndup( psz_header, psz_end - psz_header );
1709 static void AuthParseHeader( access_t *p_access, const char *psz_header,
1710 http_auth_t *p_auth )
1712 /* FIXME: multiple auth methods can be listed (comma seperated) */
1714 /* 2 Basic Authentication Scheme */
1715 if( !strncasecmp( psz_header, "Basic ", strlen( "Basic " ) ) )
1717 msg_Dbg( p_access, "Using Basic Authentication" );
1718 psz_header += strlen( "Basic " );
1719 p_auth->psz_realm = AuthGetParam( psz_header, "realm" );
1720 if( !p_auth->psz_realm )
1721 msg_Warn( p_access, "Basic Authentication: "
1722 "Mandatory 'realm' parameter is missing" );
1724 /* 3 Digest Access Authentication Scheme */
1725 else if( !strncasecmp( psz_header, "Digest ", strlen( "Digest " ) ) )
1727 msg_Dbg( p_access, "Using Digest Access Authentication" );
1728 if( p_auth->psz_nonce ) return; /* FIXME */
1729 psz_header += strlen( "Digest " );
1730 p_auth->psz_realm = AuthGetParam( psz_header, "realm" );
1731 p_auth->psz_domain = AuthGetParam( psz_header, "domain" );
1732 p_auth->psz_nonce = AuthGetParam( psz_header, "nonce" );
1733 p_auth->psz_opaque = AuthGetParam( psz_header, "opaque" );
1734 p_auth->psz_stale = AuthGetParamNoQuotes( psz_header, "stale" );
1735 p_auth->psz_algorithm = AuthGetParamNoQuotes( psz_header, "algorithm" );
1736 p_auth->psz_qop = AuthGetParam( psz_header, "qop" );
1737 p_auth->i_nonce = 0;
1738 /* printf("realm: |%s|\ndomain: |%s|\nnonce: |%s|\nopaque: |%s|\n"
1739 "stale: |%s|\nalgorithm: |%s|\nqop: |%s|\n",
1740 p_auth->psz_realm,p_auth->psz_domain,p_auth->psz_nonce,
1741 p_auth->psz_opaque,p_auth->psz_stale,p_auth->psz_algorithm,
1742 p_auth->psz_qop); */
1743 if( !p_auth->psz_realm )
1744 msg_Warn( p_access, "Digest Access Authentication: "
1745 "Mandatory 'realm' parameter is missing" );
1746 if( !p_auth->psz_nonce )
1747 msg_Warn( p_access, "Digest Access Authentication: "
1748 "Mandatory 'nonce' parameter is missing" );
1749 if( p_auth->psz_qop ) /* FIXME: parse the qop list */
1751 char *psz_tmp = strchr( p_auth->psz_qop, ',' );
1752 if( psz_tmp ) *psz_tmp = '\0';
1757 const char *psz_end = strchr( psz_header, ' ' );
1759 msg_Warn( p_access, "Unknown authentication scheme: '%*s'",
1760 (int)(psz_end - psz_header), psz_header );
1762 msg_Warn( p_access, "Unknown authentication scheme: '%s'",
1767 static char *AuthDigest( access_t *p_access, vlc_url_t *p_url,
1768 http_auth_t *p_auth, const char *psz_method )
1771 const char *psz_username = p_url->psz_username ? p_url->psz_username : "";
1772 const char *psz_password = p_url->psz_password ? p_url->psz_password : "";
1774 char *psz_HA1 = NULL;
1775 char *psz_HA2 = NULL;
1776 char *psz_response = NULL;
1780 if( p_auth->psz_HA1 )
1782 psz_HA1 = strdup( p_auth->psz_HA1 );
1783 if( !psz_HA1 ) goto error;
1788 AddMD5( &md5, psz_username, strlen( psz_username ) );
1789 AddMD5( &md5, ":", 1 );
1790 AddMD5( &md5, p_auth->psz_realm, strlen( p_auth->psz_realm ) );
1791 AddMD5( &md5, ":", 1 );
1792 AddMD5( &md5, psz_password, strlen( psz_password ) );
1795 psz_HA1 = psz_md5_hash( &md5 );
1796 if( !psz_HA1 ) goto error;
1798 if( p_auth->psz_algorithm
1799 && !strcmp( p_auth->psz_algorithm, "MD5-sess" ) )
1802 AddMD5( &md5, psz_HA1, 32 );
1804 AddMD5( &md5, ":", 1 );
1805 AddMD5( &md5, p_auth->psz_nonce, strlen( p_auth->psz_nonce ) );
1806 AddMD5( &md5, ":", 1 );
1807 AddMD5( &md5, p_auth->psz_cnonce, strlen( p_auth->psz_cnonce ) );
1810 psz_HA1 = psz_md5_hash( &md5 );
1811 if( !psz_HA1 ) goto error;
1812 p_auth->psz_HA1 = strdup( psz_HA1 );
1813 if( !p_auth->psz_HA1 ) goto error;
1820 AddMD5( &md5, psz_method, strlen( psz_method ) );
1821 AddMD5( &md5, ":", 1 );
1822 if( p_url->psz_path )
1823 AddMD5( &md5, p_url->psz_path, strlen( p_url->psz_path ) );
1825 AddMD5( &md5, "/", 1 );
1826 if( p_auth->psz_qop && !strcmp( p_auth->psz_qop, "auth-int" ) )
1831 AddMD5( &ent, "", 0 ); /* XXX: entity-body. should be ok for GET */
1833 psz_ent = psz_md5_hash( &ent );
1834 if( !psz_ent ) goto error;
1835 AddMD5( &md5, ":", 1 );
1836 AddMD5( &md5, psz_ent, 32 );
1840 psz_HA2 = psz_md5_hash( &md5 );
1841 if( !psz_HA2 ) goto error;
1843 /* Request digest */
1845 AddMD5( &md5, psz_HA1, 32 );
1846 AddMD5( &md5, ":", 1 );
1847 AddMD5( &md5, p_auth->psz_nonce, strlen( p_auth->psz_nonce ) );
1848 AddMD5( &md5, ":", 1 );
1850 && ( !strcmp( p_auth->psz_qop, "auth" )
1851 || !strcmp( p_auth->psz_qop, "auth-int" ) ) )
1854 snprintf( psz_inonce, 9, "%08x", p_auth->i_nonce );
1855 AddMD5( &md5, psz_inonce, 8 );
1856 AddMD5( &md5, ":", 1 );
1857 AddMD5( &md5, p_auth->psz_cnonce, strlen( p_auth->psz_cnonce ) );
1858 AddMD5( &md5, ":", 1 );
1859 AddMD5( &md5, p_auth->psz_qop, strlen( p_auth->psz_qop ) );
1860 AddMD5( &md5, ":", 1 );
1862 AddMD5( &md5, psz_HA2, 32 );
1864 psz_response = psz_md5_hash( &md5 );
1869 return psz_response;
1873 static void AuthReply( access_t *p_access, const char *psz_prefix,
1874 vlc_url_t *p_url, http_auth_t *p_auth )
1876 access_sys_t *p_sys = p_access->p_sys;
1880 http_auth_FormatAuthorizationHeader( VLC_OBJECT(p_access), p_auth,
1881 "GET", p_url->psz_path,
1882 p_url->psz_username,
1883 p_url->psz_password );
1884 if ( psz_value == NULL )
1887 net_Printf( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs,
1888 "%sAuthorization: %s\r\n", psz_prefix, psz_value );
1892 static int AuthCheckReply( access_t *p_access, const char *psz_header,
1893 vlc_url_t *p_url, http_auth_t *p_auth )
1896 http_auth_ParseAuthenticationInfoHeader( VLC_OBJECT(p_access), p_auth,
1899 p_url->psz_username,
1900 p_url->psz_password );