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 )
118 add_bool( "http-reconnect", 0, NULL, RECONNECT_TEXT,
119 RECONNECT_LONGTEXT, true )
120 add_bool( "http-continuous", 0, NULL, CONTINUOUS_TEXT,
121 CONTINUOUS_LONGTEXT, true )
123 add_bool( "http-forward-cookies", true, NULL, FORWARD_COOKIES_TEXT,
124 FORWARD_COOKIES_LONGTEXT, true )
125 add_integer( "http-max-redirect", 5, NULL, MAX_REDIRECT_TEXT,
126 MAX_REDIRECT_LONGTEXT, true )
127 add_obsolete_string("http-user")
128 add_obsolete_string("http-pwd")
129 add_shortcut( "http" )
130 add_shortcut( "https" )
131 add_shortcut( "unsv" )
132 add_shortcut( "itpc" ) /* iTunes Podcast */
133 add_shortcut( "icyx" )
134 set_callbacks( Open, Close )
137 /*****************************************************************************
139 *****************************************************************************/
144 tls_session_t *p_tls;
149 char *psz_user_agent;
155 http_auth_t proxy_auth;
156 char *psz_proxy_passbuf;
160 const char *psz_protocol;
182 int64_t i_icy_offset;
195 vlc_array_t * cookies;
199 static int OpenWithCookies( vlc_object_t *p_this, const char *psz_access,
200 int i_nb_redirect, int i_max_redirect,
201 vlc_array_t *cookies );
204 static ssize_t Read( access_t *, uint8_t *, size_t );
205 static ssize_t ReadCompressed( access_t *, uint8_t *, size_t );
206 static int Seek( access_t *, int64_t );
207 static int Control( access_t *, int, va_list );
210 static int Connect( access_t *, int64_t );
211 static int Request( access_t *p_access, int64_t i_tell );
212 static void Disconnect( access_t * );
214 /* Small Cookie utilities. Cookies support is partial. */
215 static char * cookie_get_content( const char * cookie );
216 static char * cookie_get_domain( const char * cookie );
217 static char * cookie_get_name( const char * cookie );
218 static void cookie_append( vlc_array_t * cookies, char * cookie );
221 static void AuthReply( access_t *p_acces, const char *psz_prefix,
222 vlc_url_t *p_url, http_auth_t *p_auth );
223 static int AuthCheckReply( access_t *p_access, const char *psz_header,
224 vlc_url_t *p_url, http_auth_t *p_auth );
226 /*****************************************************************************
228 *****************************************************************************/
229 static int Open( vlc_object_t *p_this )
231 access_t *p_access = (access_t*)p_this;
232 return OpenWithCookies( p_this, p_access->psz_access, 0,
233 var_CreateGetInteger( p_access, "http-max-redirect" ), NULL );
237 * Open the given url using the given cookies
238 * @param p_this: the vlc object
239 * @psz_access: the acces to use (http, https, ...) (this value must be used
240 * instead of p_access->psz_access)
241 * @i_nb_redirect: the number of redirection already done
242 * @i_max_redirect: limit to the number of redirection to follow
243 * @cookies: the available cookies
244 * @return vlc error codes
246 static int OpenWithCookies( vlc_object_t *p_this, const char *psz_access,
247 int i_nb_redirect, int i_max_redirect,
248 vlc_array_t *cookies )
250 access_t *p_access = (access_t*)p_this;
254 /* Only forward an store cookies if the corresponding option is activated */
255 bool b_forward_cookies = var_CreateGetBool( p_access, "http-forward-cookies" );
256 vlc_array_t * saved_cookies = b_forward_cookies ? (cookies ? cookies : vlc_array_new()) : NULL;
258 /* Set up p_access */
259 STANDARD_READ_ACCESS_INIT;
261 p_access->pf_read = ReadCompressed;
264 p_sys->b_proxy = false;
265 p_sys->psz_proxy_passbuf = NULL;
266 p_sys->i_version = 1;
267 p_sys->b_seekable = true;
268 p_sys->psz_mime = NULL;
269 p_sys->psz_pragma = NULL;
270 p_sys->b_mms = false;
271 p_sys->b_icecast = false;
272 p_sys->psz_location = NULL;
273 p_sys->psz_user_agent = NULL;
274 p_sys->b_pace_control = true;
275 p_sys->b_ssl = false;
277 p_sys->b_compressed = false;
278 /* 15 is the max windowBits, +32 to enable optional gzip decoding */
279 if( inflateInit2( &p_sys->inflate.stream, 32+15 ) != Z_OK )
280 msg_Warn( p_access, "Error during zlib initialisation: %s",
281 p_sys->inflate.stream.msg );
282 if( zlibCompileFlags() & (1<<17) )
283 msg_Warn( p_access, "Your zlib was compiled without gzip support." );
284 p_sys->inflate.p_buffer = NULL;
288 p_sys->i_icy_meta = 0;
289 p_sys->i_icy_offset = 0;
290 p_sys->psz_icy_name = NULL;
291 p_sys->psz_icy_genre = NULL;
292 p_sys->psz_icy_title = NULL;
293 p_sys->i_remaining = 0;
294 p_sys->b_persist = false;
295 p_access->info.i_size = -1;
296 p_access->info.i_pos = 0;
297 p_access->info.b_eof = false;
299 p_sys->cookies = saved_cookies;
301 http_auth_Init( &p_sys->auth );
302 http_auth_Init( &p_sys->proxy_auth );
304 /* Parse URI - remove spaces */
305 p = psz = strdup( p_access->psz_path );
306 while( (p = strchr( p, ' ' )) != NULL )
308 vlc_UrlParse( &p_sys->url, psz, 0 );
311 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
313 msg_Warn( p_access, "invalid host" );
316 if( !strncmp( psz_access, "https", 5 ) )
320 if( p_sys->url.i_port <= 0 )
321 p_sys->url.i_port = 443;
325 if( p_sys->url.i_port <= 0 )
326 p_sys->url.i_port = 80;
330 p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
333 psz = var_CreateGetNonEmptyString( p_access, "http-proxy" );
336 p_sys->b_proxy = true;
337 vlc_UrlParse( &p_sys->proxy, psz, 0 );
343 pxProxyFactory *pf = px_proxy_factory_new();
348 i=asprintf(&buf, "%s://%s", psz_access, p_access->psz_path);
351 msg_Dbg(p_access, "asking libproxy about url '%s'", buf);
352 char **proxies = px_proxy_factory_get_proxies(pf, buf);
355 msg_Dbg(p_access, "libproxy suggest to use '%s'", proxies[0]);
356 if(strcmp(proxies[0],"direct://") != 0)
358 p_sys->b_proxy = true;
359 vlc_UrlParse( &p_sys->proxy, proxies[0], 0);
362 for(i=0;proxies[i];i++) free(proxies[i]);
366 px_proxy_factory_free(pf);
370 msg_Err(p_access, "Allocating memory for libproxy failed");
376 psz = getenv( "http_proxy" );
379 p_sys->b_proxy = true;
380 vlc_UrlParse( &p_sys->proxy, psz, 0 );
384 if( psz ) /* No, this is NOT a use-after-free error */
386 psz = var_CreateGetNonEmptyString( p_access, "http-proxy-pwd" );
388 p_sys->proxy.psz_password = p_sys->psz_proxy_passbuf = psz;
393 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
395 msg_Warn( p_access, "invalid proxy host" );
398 if( p_sys->proxy.i_port <= 0 )
400 p_sys->proxy.i_port = 80;
404 msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
405 p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
408 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
409 p_sys->proxy.i_port );
411 if( p_sys->url.psz_username && *p_sys->url.psz_username )
413 msg_Dbg( p_access, " user='%s'", p_sys->url.psz_username );
416 p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
417 p_sys->b_continuous = var_CreateGetBool( p_access, "http-continuous" );
421 switch( Connect( p_access, 0 ) )
427 /* Retry with http 1.0 */
428 msg_Dbg( p_access, "switching to HTTP version 1.0" );
429 p_sys->i_version = 0;
430 p_sys->b_seekable = false;
432 if( !vlc_object_alive (p_access) || Connect( p_access, 0 ) )
440 msg_Err( p_access, "You should not be here" );
445 if( p_sys->i_code == 401 )
447 char *psz_login, *psz_password;
449 if( p_sys->url.psz_username && p_sys->url.psz_password &&
450 p_sys->auth.psz_nonce && p_sys->auth.i_nonce == 0 )
452 Disconnect( p_access );
455 msg_Dbg( p_access, "authentication failed for realm %s",
456 p_sys->auth.psz_realm );
457 dialog_Login( p_access, &psz_login, &psz_password,
458 _("HTTP authentication"),
459 _("Please enter a valid login name and a password for realm %s."),
460 p_sys->auth.psz_realm );
461 if( psz_login != NULL && psz_password != NULL )
463 msg_Dbg( p_access, "retrying with user=%s", psz_login );
464 p_sys->url.psz_username = psz_login;
465 p_sys->url.psz_password = psz_password;
466 Disconnect( p_access );
472 free( psz_password );
477 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
478 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
479 p_sys->psz_location && *p_sys->psz_location )
481 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
483 /* Check the number of redirection already done */
484 if( i_nb_redirect >= i_max_redirect )
486 msg_Err( p_access, "Too many redirection: break potential infinite"
492 /* Do not accept redirection outside of HTTP works */
493 const char *psz_protocol;
494 if( !strncmp( p_sys->psz_location, "http:", 5 ) )
495 psz_protocol = "http";
496 else if( !strncmp( p_sys->psz_location, "https:", 6 ) )
497 psz_protocol = "https";
500 msg_Err( p_access, "insecure redirection ignored" );
503 free( p_access->psz_path );
504 p_access->psz_path = strdup( p_sys->psz_location );
505 /* Clean up current Open() run */
506 vlc_UrlClean( &p_sys->url );
507 http_auth_Reset( &p_sys->auth );
508 vlc_UrlClean( &p_sys->proxy );
509 free( p_sys->psz_proxy_passbuf );
510 http_auth_Reset( &p_sys->proxy_auth );
511 free( p_sys->psz_mime );
512 free( p_sys->psz_pragma );
513 free( p_sys->psz_location );
514 free( p_sys->psz_user_agent );
516 Disconnect( p_access );
517 cookies = p_sys->cookies;
519 inflateEnd( &p_sys->inflate.stream );
523 /* Do new Open() run with new data */
524 return OpenWithCookies( p_this, psz_protocol, i_nb_redirect + 1,
525 i_max_redirect, cookies );
530 msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
534 if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
536 if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
538 if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
539 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
541 free( p_access->psz_demux );
542 p_access->psz_demux = strdup( "nsv" );
544 else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
545 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
547 free( p_access->psz_demux );
548 p_access->psz_demux = strdup( "m4a" );
550 else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
552 free( p_access->psz_demux );
553 p_access->psz_demux = strdup( "mp3" );
556 msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
557 p_access->psz_demux );
559 #if 0 /* Doesn't work really well because of the pre-buffering in
560 * shoutcast servers (the buffer content will be sent as fast as
562 p_sys->b_pace_control = false;
565 else if( !p_sys->psz_mime )
567 free( p_access->psz_demux );
569 p_access->psz_demux = strdup( "mp3" );
571 /* else probably Ogg Vorbis */
573 else if( !strcasecmp( psz_access, "unsv" ) &&
575 !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
577 free( p_access->psz_demux );
578 /* Grrrr! detect ultravox server and force NSV demuxer */
579 p_access->psz_demux = strdup( "nsv" );
581 else if( !strcmp( psz_access, "itpc" ) )
583 free( p_access->psz_demux );
584 p_access->psz_demux = strdup( "podcast" );
586 else if( p_sys->psz_mime &&
587 !strncasecmp( p_sys->psz_mime, "application/xspf+xml", 20 ) &&
588 ( memchr( " ;\t", p_sys->psz_mime[20], 4 ) != NULL ) )
590 free( p_access->psz_demux );
591 p_access->psz_demux = strdup( "xspf-open" );
594 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
597 var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
602 vlc_UrlClean( &p_sys->url );
603 vlc_UrlClean( &p_sys->proxy );
604 free( p_sys->psz_proxy_passbuf );
605 free( p_sys->psz_mime );
606 free( p_sys->psz_pragma );
607 free( p_sys->psz_location );
608 free( p_sys->psz_user_agent );
610 Disconnect( p_access );
615 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
616 free(vlc_array_item_at_index( p_sys->cookies, i ));
617 vlc_array_destroy( p_sys->cookies );
621 inflateEnd( &p_sys->inflate.stream );
627 /*****************************************************************************
629 *****************************************************************************/
630 static void Close( vlc_object_t *p_this )
632 access_t *p_access = (access_t*)p_this;
633 access_sys_t *p_sys = p_access->p_sys;
635 vlc_UrlClean( &p_sys->url );
636 http_auth_Reset( &p_sys->auth );
637 vlc_UrlClean( &p_sys->proxy );
638 http_auth_Reset( &p_sys->proxy_auth );
640 free( p_sys->psz_mime );
641 free( p_sys->psz_pragma );
642 free( p_sys->psz_location );
644 free( p_sys->psz_icy_name );
645 free( p_sys->psz_icy_genre );
646 free( p_sys->psz_icy_title );
648 free( p_sys->psz_user_agent );
650 Disconnect( p_access );
655 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
656 free(vlc_array_item_at_index( p_sys->cookies, i ));
657 vlc_array_destroy( p_sys->cookies );
661 inflateEnd( &p_sys->inflate.stream );
662 free( p_sys->inflate.p_buffer );
668 /*****************************************************************************
669 * Read: Read up to i_len bytes from the http connection and place in
670 * p_buffer. Return the actual number of bytes read
671 *****************************************************************************/
672 static int ReadICYMeta( access_t *p_access );
673 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
675 access_sys_t *p_sys = p_access->p_sys;
678 if( p_sys->fd == -1 )
680 p_access->info.b_eof = true;
684 if( p_access->info.i_size >= 0 &&
685 i_len + p_access->info.i_pos > p_access->info.i_size )
687 if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
689 p_access->info.b_eof = true;
694 if( p_sys->b_chunked )
696 if( p_sys->i_chunk < 0 )
698 p_access->info.b_eof = true;
702 if( p_sys->i_chunk <= 0 )
704 char *psz = net_Gets( p_access, p_sys->fd, p_sys->p_vs );
705 /* read the chunk header */
708 /* fatal error - end of file */
709 msg_Dbg( p_access, "failed reading chunk-header line" );
712 p_sys->i_chunk = strtoll( psz, NULL, 16 );
715 if( p_sys->i_chunk <= 0 ) /* eof */
718 p_access->info.b_eof = true;
723 if( i_len > p_sys->i_chunk )
725 i_len = p_sys->i_chunk;
728 else if( p_access->info.i_size != -1 && (int64_t)i_len > p_sys->i_remaining) {
729 /* Only ask for the remaining length */
730 i_len = (size_t)p_sys->i_remaining;
732 p_access->info.b_eof = true;
738 if( p_sys->i_icy_meta > 0 && p_access->info.i_pos-p_sys->i_icy_offset > 0 )
740 int64_t i_next = p_sys->i_icy_meta -
741 (p_access->info.i_pos - p_sys->i_icy_offset ) % p_sys->i_icy_meta;
743 if( i_next == p_sys->i_icy_meta )
745 if( ReadICYMeta( p_access ) )
747 p_access->info.b_eof = true;
755 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, false );
759 p_access->info.i_pos += i_read;
761 if( p_sys->b_chunked )
763 p_sys->i_chunk -= i_read;
764 if( p_sys->i_chunk <= 0 )
766 /* read the empty line */
767 char *psz = net_Gets( p_access, p_sys->fd, p_sys->p_vs );
772 else if( i_read <= 0 )
775 * I very much doubt that this will work.
776 * If i_read == 0, the connection *IS* dead, so the only
777 * sensible thing to do is Disconnect() and then retry.
778 * Otherwise, I got recv() completely wrong. -- Courmisch
780 if( p_sys->b_continuous )
782 Request( p_access, 0 );
783 p_sys->b_continuous = false;
784 i_read = Read( p_access, p_buffer, i_len );
785 p_sys->b_continuous = true;
787 Disconnect( p_access );
788 if( p_sys->b_reconnect && vlc_object_alive( p_access ) )
790 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
791 if( Connect( p_access, p_access->info.i_pos ) )
793 msg_Dbg( p_access, "reconnection failed" );
797 p_sys->b_reconnect = false;
798 i_read = Read( p_access, p_buffer, i_len );
799 p_sys->b_reconnect = true;
804 p_access->info.b_eof = true;
805 else if( i_read < 0 )
806 p_access->b_error = true;
809 if( p_access->info.i_size != -1 )
811 p_sys->i_remaining -= i_read;
817 static int ReadICYMeta( access_t *p_access )
819 access_sys_t *p_sys = p_access->p_sys;
825 /* Read meta data length */
826 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
833 i_read = buffer << 4;
834 /* msg_Dbg( p_access, "ICY meta size=%u", i_read); */
836 psz_meta = malloc( i_read + 1 );
837 if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
838 (uint8_t *)psz_meta, i_read, true ) != i_read )
844 psz_meta[i_read] = '\0'; /* Just in case */
846 /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
848 /* Now parse the meta */
849 /* Look for StreamTitle= */
850 p = strcasestr( (char *)psz_meta, "StreamTitle=" );
853 p += strlen( "StreamTitle=" );
854 if( *p == '\'' || *p == '"' )
856 char closing[] = { p[0], ';', '\0' };
857 char *psz = strstr( &p[1], closing );
859 psz = strchr( &p[1], ';' );
861 if( psz ) *psz = '\0';
865 char *psz = strchr( &p[1], ';' );
866 if( psz ) *psz = '\0';
869 if( !p_sys->psz_icy_title ||
870 strcmp( p_sys->psz_icy_title, &p[1] ) )
872 free( p_sys->psz_icy_title );
873 char *psz_tmp = strdup( &p[1] );
874 p_sys->psz_icy_title = EnsureUTF8( psz_tmp );
875 if( !p_sys->psz_icy_title )
877 p_access->info.i_update |= INPUT_UPDATE_META;
879 msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
888 static ssize_t ReadCompressed( access_t *p_access, uint8_t *p_buffer,
891 access_sys_t *p_sys = p_access->p_sys;
893 if( p_sys->b_compressed )
897 if( !p_sys->inflate.p_buffer )
898 p_sys->inflate.p_buffer = malloc( 256 * 1024 );
900 if( p_sys->inflate.stream.avail_in == 0 )
902 ssize_t i_read = Read( p_access, p_sys->inflate.p_buffer + p_sys->inflate.stream.avail_in, 256 * 1024 );
903 if( i_read <= 0 ) return i_read;
904 p_sys->inflate.stream.next_in = p_sys->inflate.p_buffer;
905 p_sys->inflate.stream.avail_in = i_read;
908 p_sys->inflate.stream.avail_out = i_len;
909 p_sys->inflate.stream.next_out = p_buffer;
911 i_ret = inflate( &p_sys->inflate.stream, Z_SYNC_FLUSH );
912 msg_Warn( p_access, "inflate return value: %d, %s", i_ret, p_sys->inflate.stream.msg );
914 return i_len - p_sys->inflate.stream.avail_out;
918 return Read( p_access, p_buffer, i_len );
923 /*****************************************************************************
924 * Seek: close and re-open a connection at the right place
925 *****************************************************************************/
926 static int Seek( access_t *p_access, int64_t i_pos )
928 msg_Dbg( p_access, "trying to seek to %"PRId64, i_pos );
930 Disconnect( p_access );
932 if( p_access->info.i_size
933 && (uint64_t)i_pos >= (uint64_t)p_access->info.i_size ) {
934 msg_Err( p_access, "seek to far" );
935 int retval = Seek( p_access, p_access->info.i_size - 1 );
936 if( retval == VLC_SUCCESS ) {
938 Read( p_access, p_buffer, 1);
939 p_access->info.b_eof = false;
943 if( Connect( p_access, i_pos ) )
945 msg_Err( p_access, "seek failed" );
946 p_access->info.b_eof = true;
952 /*****************************************************************************
954 *****************************************************************************/
955 static int Control( access_t *p_access, int i_query, va_list args )
957 access_sys_t *p_sys = p_access->p_sys;
965 case ACCESS_CAN_SEEK:
966 pb_bool = (bool*)va_arg( args, bool* );
967 *pb_bool = p_sys->b_seekable;
969 case ACCESS_CAN_FASTSEEK:
970 pb_bool = (bool*)va_arg( args, bool* );
973 case ACCESS_CAN_PAUSE:
974 case ACCESS_CAN_CONTROL_PACE:
975 pb_bool = (bool*)va_arg( args, bool* );
977 #if 0 /* Disable for now until we have a clock synchro algo
978 * which works with something else than MPEG over UDP */
979 *pb_bool = p_sys->b_pace_control;
985 case ACCESS_GET_PTS_DELAY:
986 pi_64 = (int64_t*)va_arg( args, int64_t * );
987 *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
991 case ACCESS_SET_PAUSE_STATE:
994 case ACCESS_GET_META:
995 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
997 if( p_sys->psz_icy_name )
998 vlc_meta_Set( p_meta, vlc_meta_Title, p_sys->psz_icy_name );
999 if( p_sys->psz_icy_genre )
1000 vlc_meta_Set( p_meta, vlc_meta_Genre, p_sys->psz_icy_genre );
1001 if( p_sys->psz_icy_title )
1002 vlc_meta_Set( p_meta, vlc_meta_NowPlaying, p_sys->psz_icy_title );
1005 case ACCESS_GET_CONTENT_TYPE:
1006 *va_arg( args, char ** ) =
1007 p_sys->psz_mime ? strdup( p_sys->psz_mime ) : NULL;
1010 case ACCESS_GET_TITLE_INFO:
1011 case ACCESS_SET_TITLE:
1012 case ACCESS_SET_SEEKPOINT:
1013 case ACCESS_SET_PRIVATE_ID_STATE:
1014 return VLC_EGENERIC;
1017 msg_Warn( p_access, "unimplemented query in control" );
1018 return VLC_EGENERIC;
1024 /*****************************************************************************
1026 *****************************************************************************/
1027 static int Connect( access_t *p_access, int64_t i_tell )
1029 access_sys_t *p_sys = p_access->p_sys;
1030 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
1033 free( p_sys->psz_location );
1034 free( p_sys->psz_mime );
1035 free( p_sys->psz_pragma );
1037 free( p_sys->psz_icy_genre );
1038 free( p_sys->psz_icy_name );
1039 free( p_sys->psz_icy_title );
1042 p_sys->psz_location = NULL;
1043 p_sys->psz_mime = NULL;
1044 p_sys->psz_pragma = NULL;
1045 p_sys->b_mms = false;
1046 p_sys->b_chunked = false;
1048 p_sys->i_icy_meta = 0;
1049 p_sys->i_icy_offset = i_tell;
1050 p_sys->psz_icy_name = NULL;
1051 p_sys->psz_icy_genre = NULL;
1052 p_sys->psz_icy_title = NULL;
1053 p_sys->i_remaining = 0;
1054 p_sys->b_persist = false;
1056 p_access->info.i_size = -1;
1057 p_access->info.i_pos = i_tell;
1058 p_access->info.b_eof = false;
1060 /* Open connection */
1061 assert( p_sys->fd == -1 ); /* No open sockets (leaking fds is BAD) */
1062 p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
1063 if( p_sys->fd == -1 )
1065 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
1068 setsockopt (p_sys->fd, SOL_SOCKET, SO_KEEPALIVE, &(int){ 1 }, sizeof (int));
1070 /* Initialize TLS/SSL session */
1071 if( p_sys->b_ssl == true )
1073 /* CONNECT to establish TLS tunnel through HTTP proxy */
1074 if( p_sys->b_proxy )
1077 unsigned i_status = 0;
1079 if( p_sys->i_version == 0 )
1081 /* CONNECT is not in HTTP/1.0 */
1082 Disconnect( p_access );
1086 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
1087 "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
1088 p_sys->url.psz_host, p_sys->url.i_port,
1090 p_sys->url.psz_host, p_sys->url.i_port);
1092 psz = net_Gets( p_access, p_sys->fd, NULL );
1095 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
1096 Disconnect( p_access );
1100 sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
1103 if( ( i_status / 100 ) != 2 )
1105 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
1106 Disconnect( p_access );
1112 psz = net_Gets( p_access, p_sys->fd, NULL );
1115 msg_Err( p_access, "HTTP proxy connection failed" );
1116 Disconnect( p_access );
1125 if( !vlc_object_alive (p_access) || p_access->b_error )
1127 Disconnect( p_access );
1134 /* TLS/SSL handshake */
1135 p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
1136 p_sys->url.psz_host );
1137 if( p_sys->p_tls == NULL )
1139 msg_Err( p_access, "cannot establish HTTP/TLS session" );
1140 Disconnect( p_access );
1143 p_sys->p_vs = &p_sys->p_tls->sock;
1146 return Request( p_access, i_tell ) ? -2 : 0;
1150 static int Request( access_t *p_access, int64_t i_tell )
1152 access_sys_t *p_sys = p_access->p_sys;
1154 v_socket_t *pvs = p_sys->p_vs;
1155 p_sys->b_persist = false;
1157 p_sys->i_remaining = 0;
1158 if( p_sys->b_proxy )
1160 if( p_sys->url.psz_path )
1162 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
1163 "GET http://%s:%d%s HTTP/1.%d\r\n",
1164 p_sys->url.psz_host, p_sys->url.i_port,
1165 p_sys->url.psz_path, p_sys->i_version );
1169 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
1170 "GET http://%s:%d/ HTTP/1.%d\r\n",
1171 p_sys->url.psz_host, p_sys->url.i_port,
1177 const char *psz_path = p_sys->url.psz_path;
1178 if( !psz_path || !*psz_path )
1182 if( p_sys->url.i_port != (pvs ? 443 : 80) )
1184 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1185 "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
1186 psz_path, p_sys->i_version, p_sys->url.psz_host,
1187 p_sys->url.i_port );
1191 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1192 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
1193 psz_path, p_sys->i_version, p_sys->url.psz_host );
1197 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "User-Agent: %s\r\n",
1198 p_sys->psz_user_agent );
1200 if( p_sys->i_version == 1 && ! p_sys->b_continuous )
1202 p_sys->b_persist = true;
1203 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1204 "Range: bytes=%"PRIu64"-\r\n", i_tell );
1208 if( p_sys->cookies )
1211 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
1213 const char * cookie = vlc_array_item_at_index( p_sys->cookies, i );
1214 char * psz_cookie_content = cookie_get_content( cookie );
1215 char * psz_cookie_domain = cookie_get_domain( cookie );
1217 assert( psz_cookie_content );
1219 /* FIXME: This is clearly not conforming to the rfc */
1220 bool is_in_right_domain = (!psz_cookie_domain || strstr( p_sys->url.psz_host, psz_cookie_domain ));
1222 if( is_in_right_domain )
1224 msg_Dbg( p_access, "Sending Cookie %s", psz_cookie_content );
1225 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Cookie: %s\r\n", psz_cookie_content ) < 0 )
1226 msg_Err( p_access, "failed to send Cookie" );
1228 free( psz_cookie_content );
1229 free( psz_cookie_domain );
1233 /* Authentication */
1234 if( p_sys->url.psz_username || p_sys->url.psz_password )
1235 AuthReply( p_access, "", &p_sys->url, &p_sys->auth );
1237 /* Proxy Authentication */
1238 if( p_sys->proxy.psz_username || p_sys->proxy.psz_password )
1239 AuthReply( p_access, "Proxy-", &p_sys->proxy, &p_sys->proxy_auth );
1241 /* ICY meta data request */
1242 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
1245 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "\r\n" ) < 0 )
1247 msg_Err( p_access, "failed to send request" );
1248 Disconnect( p_access );
1249 return VLC_EGENERIC;
1253 if( ( psz = net_Gets( p_access, p_sys->fd, pvs ) ) == NULL )
1255 msg_Err( p_access, "failed to read answer" );
1258 if( !strncmp( psz, "HTTP/1.", 7 ) )
1260 p_sys->psz_protocol = "HTTP";
1261 p_sys->i_code = atoi( &psz[9] );
1263 else if( !strncmp( psz, "ICY", 3 ) )
1265 p_sys->psz_protocol = "ICY";
1266 p_sys->i_code = atoi( &psz[4] );
1267 p_sys->b_reconnect = true;
1271 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1275 msg_Dbg( p_access, "protocol '%s' answer code %d",
1276 p_sys->psz_protocol, p_sys->i_code );
1277 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1279 p_sys->b_seekable = false;
1281 if( p_sys->i_code != 206 && p_sys->i_code != 401 )
1283 p_sys->b_seekable = false;
1285 /* Authentication error - We'll have to display the dialog */
1286 if( p_sys->i_code == 401 )
1290 /* Other fatal error */
1291 else if( p_sys->i_code >= 400 )
1293 msg_Err( p_access, "error: %s", psz );
1301 char *psz = net_Gets( p_access, p_sys->fd, pvs );
1306 msg_Err( p_access, "failed to read answer" );
1310 if( !vlc_object_alive (p_access) || p_access->b_error )
1316 /* msg_Dbg( p_input, "Line=%s", psz ); */
1323 if( ( p = strchr( psz, ':' ) ) == NULL )
1325 msg_Err( p_access, "malformed header line: %s", psz );
1330 while( *p == ' ' ) p++;
1332 if( !strcasecmp( psz, "Content-Length" ) )
1334 int64_t i_size = i_tell + (p_sys->i_remaining = atoll( p ));
1335 if(i_size > p_access->info.i_size) {
1336 p_access->info.i_size = i_size;
1338 msg_Dbg( p_access, "this frame size=%"PRId64, p_sys->i_remaining );
1340 else if( !strcasecmp( psz, "Content-Range" ) ) {
1341 int64_t i_ntell = i_tell;
1342 int64_t i_nend = (p_access->info.i_size > 0)?(p_access->info.i_size - 1):i_tell;
1343 int64_t i_nsize = p_access->info.i_size;
1344 sscanf(p,"bytes %"PRId64"-%"PRId64"/%"PRId64,&i_ntell,&i_nend,&i_nsize);
1345 if(i_nend > i_ntell ) {
1346 p_access->info.i_pos = i_ntell;
1347 p_sys->i_remaining = i_nend+1-i_ntell;
1348 int64_t i_size = (i_nsize > i_nend) ? i_nsize : (i_nend + 1);
1349 if(i_size > p_access->info.i_size) {
1350 p_access->info.i_size = i_size;
1352 msg_Dbg( p_access, "stream size=%"PRId64",pos=%"PRId64",remaining=%"PRId64,i_nsize,i_ntell,p_sys->i_remaining);
1355 else if( !strcasecmp( psz, "Connection" ) ) {
1356 msg_Dbg( p_access, "Connection: %s",p );
1358 sscanf(p, "close%n",&i);
1360 p_sys->b_persist = false;
1363 else if( !strcasecmp( psz, "Location" ) )
1367 /* This does not follow RFC 2068, but yet if the url is not absolute,
1368 * handle it as everyone does. */
1371 const char *psz_http_ext = p_sys->b_ssl ? "s" : "" ;
1373 if( p_sys->url.i_port == ( p_sys->b_ssl ? 443 : 80 ) )
1375 if( asprintf(&psz_new_loc, "http%s://%s%s", psz_http_ext,
1376 p_sys->url.psz_host, p) < 0 )
1381 if( asprintf(&psz_new_loc, "http%s://%s:%d%s", psz_http_ext,
1382 p_sys->url.psz_host, p_sys->url.i_port, p) < 0 )
1388 psz_new_loc = strdup( p );
1391 free( p_sys->psz_location );
1392 p_sys->psz_location = psz_new_loc;
1394 else if( !strcasecmp( psz, "Content-Type" ) )
1396 free( p_sys->psz_mime );
1397 p_sys->psz_mime = strdup( p );
1398 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1400 else if( !strcasecmp( psz, "Content-Encoding" ) )
1402 msg_Dbg( p_access, "Content-Encoding: %s", p );
1403 if( strcasecmp( p, "identity" ) )
1405 p_sys->b_compressed = true;
1407 msg_Warn( p_access, "Compressed content not supported. Rebuild with zlib support." );
1410 else if( !strcasecmp( psz, "Pragma" ) )
1412 if( !strcasecmp( psz, "Pragma: features" ) )
1413 p_sys->b_mms = true;
1414 free( p_sys->psz_pragma );
1415 p_sys->psz_pragma = strdup( p );
1416 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1418 else if( !strcasecmp( psz, "Server" ) )
1420 msg_Dbg( p_access, "Server: %s", p );
1421 if( !strncasecmp( p, "Icecast", 7 ) ||
1422 !strncasecmp( p, "Nanocaster", 10 ) )
1424 /* Remember if this is Icecast
1425 * we need to force demux in this case without breaking
1428 /* Let live 365 streams (nanocaster) piggyback on the icecast
1429 * routine. They look very similar */
1431 p_sys->b_reconnect = true;
1432 p_sys->b_pace_control = false;
1433 p_sys->b_icecast = true;
1436 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1438 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1439 if( !strncasecmp( p, "chunked", 7 ) )
1441 p_sys->b_chunked = true;
1444 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1446 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1447 p_sys->i_icy_meta = atoi( p );
1448 if( p_sys->i_icy_meta < 0 )
1449 p_sys->i_icy_meta = 0;
1450 if( p_sys->i_icy_meta > 0 )
1451 p_sys->b_icecast = true;
1453 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1455 else if( !strcasecmp( psz, "Icy-Name" ) )
1457 free( p_sys->psz_icy_name );
1458 char *psz_tmp = strdup( p );
1459 p_sys->psz_icy_name = EnsureUTF8( psz_tmp );
1460 if( !p_sys->psz_icy_name )
1462 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1464 p_sys->b_icecast = true; /* be on the safeside. set it here as well. */
1465 p_sys->b_reconnect = true;
1466 p_sys->b_pace_control = false;
1468 else if( !strcasecmp( psz, "Icy-Genre" ) )
1470 free( p_sys->psz_icy_genre );
1471 char *psz_tmp = strdup( p );
1472 p_sys->psz_icy_genre = EnsureUTF8( psz_tmp );
1473 if( !p_sys->psz_icy_genre )
1475 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1477 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1479 msg_Dbg( p_access, "Icy-Notice: %s", p );
1481 else if( !strncasecmp( psz, "icy-", 4 ) ||
1482 !strncasecmp( psz, "ice-", 4 ) ||
1483 !strncasecmp( psz, "x-audiocast", 11 ) )
1485 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1487 else if( !strcasecmp( psz, "Set-Cookie" ) )
1489 if( p_sys->cookies )
1491 msg_Dbg( p_access, "Accepting Cookie: %s", p );
1492 cookie_append( p_sys->cookies, strdup(p) );
1495 msg_Dbg( p_access, "We have a Cookie we won't remember: %s", p );
1497 else if( !strcasecmp( psz, "www-authenticate" ) )
1499 msg_Dbg( p_access, "Authentication header: %s", p );
1500 http_auth_ParseWwwAuthenticateHeader( VLC_OBJECT(p_access),
1503 else if( !strcasecmp( psz, "proxy-authenticate" ) )
1505 msg_Dbg( p_access, "Proxy authentication header: %s", p );
1506 http_auth_ParseWwwAuthenticateHeader( VLC_OBJECT(p_access),
1507 &p_sys->proxy_auth, p );
1509 else if( !strcasecmp( psz, "authentication-info" ) )
1511 msg_Dbg( p_access, "Authentication Info header: %s", p );
1512 if( AuthCheckReply( p_access, p, &p_sys->url, &p_sys->auth ) )
1515 else if( !strcasecmp( psz, "proxy-authentication-info" ) )
1517 msg_Dbg( p_access, "Proxy Authentication Info header: %s", p );
1518 if( AuthCheckReply( p_access, p, &p_sys->proxy, &p_sys->proxy_auth ) )
1524 /* We close the stream for zero length data, unless of course the
1525 * server has already promised to do this for us.
1527 if( p_access->info.i_size != -1 && p_sys->i_remaining == 0 && p_sys->b_persist ) {
1528 Disconnect( p_access );
1533 Disconnect( p_access );
1534 return VLC_EGENERIC;
1537 /*****************************************************************************
1539 *****************************************************************************/
1540 static void Disconnect( access_t *p_access )
1542 access_sys_t *p_sys = p_access->p_sys;
1544 if( p_sys->p_tls != NULL)
1546 tls_ClientDelete( p_sys->p_tls );
1547 p_sys->p_tls = NULL;
1550 if( p_sys->fd != -1)
1552 net_Close(p_sys->fd);
1558 /*****************************************************************************
1559 * Cookies (FIXME: we may want to rewrite that using a nice structure to hold
1560 * them) (FIXME: only support the "domain=" param)
1561 *****************************************************************************/
1563 /* Get the NAME=VALUE part of the Cookie */
1564 static char * cookie_get_content( const char * cookie )
1566 char * ret = strdup( cookie );
1567 if( !ret ) return NULL;
1569 /* Look for a ';' */
1570 while( *str && *str != ';' ) str++;
1571 /* Replace it by a end-char */
1572 if( *str == ';' ) *str = 0;
1576 /* Get the domain where the cookie is stored */
1577 static char * cookie_get_domain( const char * cookie )
1579 const char * str = cookie;
1580 static const char domain[] = "domain=";
1583 /* Look for a ';' */
1586 if( !strncmp( str, domain, sizeof(domain) - 1 /* minus \0 */ ) )
1588 str += sizeof(domain) - 1 /* minus \0 */;
1589 char * ret = strdup( str );
1590 /* Now remove the next ';' if present */
1591 char * ret_iter = ret;
1592 while( *ret_iter && *ret_iter != ';' ) ret_iter++;
1593 if( *ret_iter == ';' )
1597 /* Go to next ';' field */
1598 while( *str && *str != ';' ) str++;
1599 if( *str == ';' ) str++;
1601 while( *str && *str == ' ' ) str++;
1606 /* Get NAME in the NAME=VALUE field */
1607 static char * cookie_get_name( const char * cookie )
1609 char * ret = cookie_get_content( cookie ); /* NAME=VALUE */
1610 if( !ret ) return NULL;
1612 while( *str && *str != '=' ) str++;
1617 /* Add a cookie in cookies, checking to see how it should be added */
1618 static void cookie_append( vlc_array_t * cookies, char * cookie )
1625 char * cookie_name = cookie_get_name( cookie );
1627 /* Don't send invalid cookies */
1631 char * cookie_domain = cookie_get_domain( cookie );
1632 for( i = 0; i < vlc_array_count( cookies ); i++ )
1634 char * current_cookie = vlc_array_item_at_index( cookies, i );
1635 char * current_cookie_name = cookie_get_name( current_cookie );
1636 char * current_cookie_domain = cookie_get_domain( current_cookie );
1638 assert( current_cookie_name );
1640 bool is_domain_matching = ( cookie_domain && current_cookie_domain &&
1641 !strcmp( cookie_domain, current_cookie_domain ) );
1643 if( is_domain_matching && !strcmp( cookie_name, current_cookie_name ) )
1645 /* Remove previous value for this cookie */
1646 free( current_cookie );
1647 vlc_array_remove( cookies, i );
1650 free( current_cookie_name );
1651 free( current_cookie_domain );
1654 free( current_cookie_name );
1655 free( current_cookie_domain );
1657 free( cookie_name );
1658 free( cookie_domain );
1659 vlc_array_append( cookies, cookie );
1663 /*****************************************************************************
1664 * HTTP authentication
1665 *****************************************************************************/
1666 static char *AuthGetParam( const char *psz_header, const char *psz_param )
1668 char psz_what[strlen(psz_param)+3];
1669 sprintf( psz_what, "%s=\"", psz_param );
1670 psz_header = strstr( psz_header, psz_what );
1673 const char *psz_end;
1674 psz_header += strlen( psz_what );
1675 psz_end = strchr( psz_header, '"' );
1676 if( !psz_end ) /* Invalid since we should have a closing quote */
1677 return strdup( psz_header );
1678 return strndup( psz_header, psz_end - psz_header );
1686 static char *AuthGetParamNoQuotes( const char *psz_header, const char *psz_param )
1688 char psz_what[strlen(psz_param)+2];
1689 sprintf( psz_what, "%s=", psz_param );
1690 psz_header = strstr( psz_header, psz_what );
1693 const char *psz_end;
1694 psz_header += strlen( psz_what );
1695 psz_end = strchr( psz_header, ',' );
1696 /* XXX: Do we need to filter out trailing space between the value and
1697 * the comma/end of line? */
1698 if( !psz_end ) /* Can be valid if this is the last parameter */
1699 return strdup( psz_header );
1700 return strndup( psz_header, psz_end - psz_header );
1708 static void AuthParseHeader( access_t *p_access, const char *psz_header,
1709 http_auth_t *p_auth )
1711 /* FIXME: multiple auth methods can be listed (comma seperated) */
1713 /* 2 Basic Authentication Scheme */
1714 if( !strncasecmp( psz_header, "Basic ", strlen( "Basic " ) ) )
1716 msg_Dbg( p_access, "Using Basic Authentication" );
1717 psz_header += strlen( "Basic " );
1718 p_auth->psz_realm = AuthGetParam( psz_header, "realm" );
1719 if( !p_auth->psz_realm )
1720 msg_Warn( p_access, "Basic Authentication: "
1721 "Mandatory 'realm' parameter is missing" );
1723 /* 3 Digest Access Authentication Scheme */
1724 else if( !strncasecmp( psz_header, "Digest ", strlen( "Digest " ) ) )
1726 msg_Dbg( p_access, "Using Digest Access Authentication" );
1727 if( p_auth->psz_nonce ) return; /* FIXME */
1728 psz_header += strlen( "Digest " );
1729 p_auth->psz_realm = AuthGetParam( psz_header, "realm" );
1730 p_auth->psz_domain = AuthGetParam( psz_header, "domain" );
1731 p_auth->psz_nonce = AuthGetParam( psz_header, "nonce" );
1732 p_auth->psz_opaque = AuthGetParam( psz_header, "opaque" );
1733 p_auth->psz_stale = AuthGetParamNoQuotes( psz_header, "stale" );
1734 p_auth->psz_algorithm = AuthGetParamNoQuotes( psz_header, "algorithm" );
1735 p_auth->psz_qop = AuthGetParam( psz_header, "qop" );
1736 p_auth->i_nonce = 0;
1737 /* printf("realm: |%s|\ndomain: |%s|\nnonce: |%s|\nopaque: |%s|\n"
1738 "stale: |%s|\nalgorithm: |%s|\nqop: |%s|\n",
1739 p_auth->psz_realm,p_auth->psz_domain,p_auth->psz_nonce,
1740 p_auth->psz_opaque,p_auth->psz_stale,p_auth->psz_algorithm,
1741 p_auth->psz_qop); */
1742 if( !p_auth->psz_realm )
1743 msg_Warn( p_access, "Digest Access Authentication: "
1744 "Mandatory 'realm' parameter is missing" );
1745 if( !p_auth->psz_nonce )
1746 msg_Warn( p_access, "Digest Access Authentication: "
1747 "Mandatory 'nonce' parameter is missing" );
1748 if( p_auth->psz_qop ) /* FIXME: parse the qop list */
1750 char *psz_tmp = strchr( p_auth->psz_qop, ',' );
1751 if( psz_tmp ) *psz_tmp = '\0';
1756 const char *psz_end = strchr( psz_header, ' ' );
1758 msg_Warn( p_access, "Unknown authentication scheme: '%*s'",
1759 (int)(psz_end - psz_header), psz_header );
1761 msg_Warn( p_access, "Unknown authentication scheme: '%s'",
1766 static char *AuthDigest( access_t *p_access, vlc_url_t *p_url,
1767 http_auth_t *p_auth, const char *psz_method )
1770 const char *psz_username = p_url->psz_username ? p_url->psz_username : "";
1771 const char *psz_password = p_url->psz_password ? p_url->psz_password : "";
1773 char *psz_HA1 = NULL;
1774 char *psz_HA2 = NULL;
1775 char *psz_response = NULL;
1779 if( p_auth->psz_HA1 )
1781 psz_HA1 = strdup( p_auth->psz_HA1 );
1782 if( !psz_HA1 ) goto error;
1787 AddMD5( &md5, psz_username, strlen( psz_username ) );
1788 AddMD5( &md5, ":", 1 );
1789 AddMD5( &md5, p_auth->psz_realm, strlen( p_auth->psz_realm ) );
1790 AddMD5( &md5, ":", 1 );
1791 AddMD5( &md5, psz_password, strlen( psz_password ) );
1794 psz_HA1 = psz_md5_hash( &md5 );
1795 if( !psz_HA1 ) goto error;
1797 if( p_auth->psz_algorithm
1798 && !strcmp( p_auth->psz_algorithm, "MD5-sess" ) )
1801 AddMD5( &md5, psz_HA1, 32 );
1803 AddMD5( &md5, ":", 1 );
1804 AddMD5( &md5, p_auth->psz_nonce, strlen( p_auth->psz_nonce ) );
1805 AddMD5( &md5, ":", 1 );
1806 AddMD5( &md5, p_auth->psz_cnonce, strlen( p_auth->psz_cnonce ) );
1809 psz_HA1 = psz_md5_hash( &md5 );
1810 if( !psz_HA1 ) goto error;
1811 p_auth->psz_HA1 = strdup( psz_HA1 );
1812 if( !p_auth->psz_HA1 ) goto error;
1819 AddMD5( &md5, psz_method, strlen( psz_method ) );
1820 AddMD5( &md5, ":", 1 );
1821 if( p_url->psz_path )
1822 AddMD5( &md5, p_url->psz_path, strlen( p_url->psz_path ) );
1824 AddMD5( &md5, "/", 1 );
1825 if( p_auth->psz_qop && !strcmp( p_auth->psz_qop, "auth-int" ) )
1830 AddMD5( &ent, "", 0 ); /* XXX: entity-body. should be ok for GET */
1832 psz_ent = psz_md5_hash( &ent );
1833 if( !psz_ent ) goto error;
1834 AddMD5( &md5, ":", 1 );
1835 AddMD5( &md5, psz_ent, 32 );
1839 psz_HA2 = psz_md5_hash( &md5 );
1840 if( !psz_HA2 ) goto error;
1842 /* Request digest */
1844 AddMD5( &md5, psz_HA1, 32 );
1845 AddMD5( &md5, ":", 1 );
1846 AddMD5( &md5, p_auth->psz_nonce, strlen( p_auth->psz_nonce ) );
1847 AddMD5( &md5, ":", 1 );
1849 && ( !strcmp( p_auth->psz_qop, "auth" )
1850 || !strcmp( p_auth->psz_qop, "auth-int" ) ) )
1853 snprintf( psz_inonce, 9, "%08x", p_auth->i_nonce );
1854 AddMD5( &md5, psz_inonce, 8 );
1855 AddMD5( &md5, ":", 1 );
1856 AddMD5( &md5, p_auth->psz_cnonce, strlen( p_auth->psz_cnonce ) );
1857 AddMD5( &md5, ":", 1 );
1858 AddMD5( &md5, p_auth->psz_qop, strlen( p_auth->psz_qop ) );
1859 AddMD5( &md5, ":", 1 );
1861 AddMD5( &md5, psz_HA2, 32 );
1863 psz_response = psz_md5_hash( &md5 );
1868 return psz_response;
1872 static void AuthReply( access_t *p_access, const char *psz_prefix,
1873 vlc_url_t *p_url, http_auth_t *p_auth )
1875 access_sys_t *p_sys = p_access->p_sys;
1879 http_auth_FormatAuthorizationHeader( VLC_OBJECT(p_access), p_auth,
1880 "GET", p_url->psz_path,
1881 p_url->psz_username,
1882 p_url->psz_password );
1883 if ( psz_value == NULL )
1886 net_Printf( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs,
1887 "%sAuthorization: %s\r\n", psz_prefix, psz_value );
1891 static int AuthCheckReply( access_t *p_access, const char *psz_header,
1892 vlc_url_t *p_url, http_auth_t *p_auth )
1895 http_auth_ParseAuthenticationInfoHeader( VLC_OBJECT(p_access), p_auth,
1898 p_url->psz_username,
1899 p_url->psz_password );