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 *****************************************************************************/
35 #include <vlc_plugin.h>
38 #include <vlc_access.h>
40 #include <vlc_interface.h>
42 #include <vlc_network.h>
45 #include <vlc_strings.h>
46 #include <vlc_input.h>
53 /*****************************************************************************
55 *****************************************************************************/
56 static int Open ( vlc_object_t * );
57 static void Close( vlc_object_t * );
59 #define PROXY_TEXT N_("HTTP proxy")
60 #define PROXY_LONGTEXT N_( \
61 "HTTP proxy to be used It must be of the form " \
62 "http://[user[:pass]@]myproxy.mydomain:myport/ ; " \
63 "if empty, the http_proxy environment variable will be tried." )
65 #define CACHING_TEXT N_("Caching value in ms")
66 #define CACHING_LONGTEXT N_( \
67 "Caching value for HTTP streams. This " \
68 "value should be set in milliseconds." )
70 #define AGENT_TEXT N_("HTTP user agent")
71 #define AGENT_LONGTEXT N_("User agent that will be " \
72 "used for the connection.")
74 #define RECONNECT_TEXT N_("Auto re-connect")
75 #define RECONNECT_LONGTEXT N_( \
76 "Automatically try to reconnect to the stream in case of a sudden " \
79 #define CONTINUOUS_TEXT N_("Continuous stream")
80 #define CONTINUOUS_LONGTEXT N_("Read a file that is " \
81 "being constantly updated (for example, a JPG file on a server). " \
82 "You should not globally enable this option as it will break all other " \
83 "types of HTTP streams." )
85 #define FORWARD_COOKIES_TEXT N_("Forward Cookies")
86 #define FORWARD_COOKIES_LONGTEXT N_("Forward Cookies Across http redirections ")
89 set_description( _("HTTP input") );
90 set_capability( "access", 0 );
91 set_shortname( _( "HTTP(S)" ) );
92 set_category( CAT_INPUT );
93 set_subcategory( SUBCAT_INPUT_ACCESS );
95 add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
97 add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
98 CACHING_TEXT, CACHING_LONGTEXT, true );
99 add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
100 AGENT_LONGTEXT, true );
101 add_bool( "http-reconnect", 0, NULL, RECONNECT_TEXT,
102 RECONNECT_LONGTEXT, true );
103 add_bool( "http-continuous", 0, NULL, CONTINUOUS_TEXT,
104 CONTINUOUS_LONGTEXT, true );
105 add_bool( "http-forward-cookies", 0, NULL, FORWARD_COOKIES_TEXT,
106 FORWARD_COOKIES_LONGTEXT, true );
107 add_obsolete_string("http-user");
108 add_obsolete_string("http-pwd");
109 add_shortcut( "http" );
110 add_shortcut( "https" );
111 add_shortcut( "unsv" );
112 add_shortcut( "itpc" ); /* iTunes Podcast */
113 set_callbacks( Open, Close );
116 /*****************************************************************************
118 *****************************************************************************/
120 /* RFC 2617: Basic and Digest Access Authentication */
121 typedef struct http_auth_t
132 char *psz_HA1; /* stored H(A1) value if algorithm = "MD5-sess" */
138 tls_session_t *p_tls;
143 char *psz_user_agent;
149 http_auth_t proxy_auth;
153 const char *psz_protocol;
186 vlc_array_t * cookies;
190 static int OpenWithCookies( vlc_object_t *p_this, vlc_array_t *cookies );
193 static ssize_t Read( access_t *, uint8_t *, size_t );
194 static ssize_t ReadCompressed( access_t *, uint8_t *, size_t );
195 static int Seek( access_t *, int64_t );
196 static int Control( access_t *, int, va_list );
199 static int Connect( access_t *, int64_t );
200 static int Request( access_t *p_access, int64_t i_tell );
201 static void Disconnect( access_t * );
203 /* Small Cookie utilities. Cookies support is partial. */
204 static char * cookie_get_content( const char * cookie );
205 static char * cookie_get_domain( const char * cookie );
206 static char * cookie_get_name( const char * cookie );
207 static void cookie_append( vlc_array_t * cookies, char * cookie );
210 static void AuthParseHeader( access_t *p_access, const char *psz_header,
211 http_auth_t *p_auth );
212 static void AuthReply( access_t *p_acces, const char *psz_prefix,
213 vlc_url_t *p_url, http_auth_t *p_auth );
214 static int AuthCheckReply( access_t *p_access, const char *psz_header,
215 vlc_url_t *p_url, http_auth_t *p_auth );
216 static void AuthReset( http_auth_t *p_auth );
218 /*****************************************************************************
220 *****************************************************************************/
221 static int Open( vlc_object_t *p_this )
223 return OpenWithCookies( p_this, NULL );
226 static int OpenWithCookies( vlc_object_t *p_this, vlc_array_t *cookies )
228 access_t *p_access = (access_t*)p_this;
231 /* Only forward an store cookies if the corresponding option is activated */
232 bool b_forward_cookies = var_CreateGetBool( p_access, "http-forward-cookies" );
233 vlc_array_t * saved_cookies = b_forward_cookies ? (cookies ?: vlc_array_new()) : NULL;
235 /* Set up p_access */
236 STANDARD_READ_ACCESS_INIT;
238 p_access->pf_read = ReadCompressed;
241 p_sys->b_proxy = false;
242 p_sys->i_version = 1;
243 p_sys->b_seekable = true;
244 p_sys->psz_mime = NULL;
245 p_sys->psz_pragma = NULL;
246 p_sys->b_mms = false;
247 p_sys->b_icecast = false;
248 p_sys->psz_location = NULL;
249 p_sys->psz_user_agent = NULL;
250 p_sys->b_pace_control = true;
251 p_sys->b_ssl = false;
253 p_sys->b_compressed = false;
254 /* 15 is the max windowBits, +32 to enable optional gzip decoding */
255 if( inflateInit2( &p_sys->inflate.stream, 32+15 ) != Z_OK )
256 msg_Warn( p_access, "Error during zlib initialisation: %s",
257 p_sys->inflate.stream.msg );
258 if( zlibCompileFlags() & (1<<17) )
259 msg_Warn( p_access, "Your zlib was compiled without gzip support." );
260 p_sys->inflate.p_buffer = NULL;
264 p_sys->i_icy_meta = 0;
265 p_sys->psz_icy_name = NULL;
266 p_sys->psz_icy_genre = NULL;
267 p_sys->psz_icy_title = NULL;
268 p_sys->i_remaining = 0;
270 p_sys->cookies = saved_cookies;
272 /* Parse URI - remove spaces */
273 p = psz = strdup( p_access->psz_path );
274 while( (p = strchr( p, ' ' )) != NULL )
276 vlc_UrlParse( &p_sys->url, psz, 0 );
279 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
281 msg_Warn( p_access, "invalid host" );
284 if( !strncmp( p_access->psz_access, "https", 5 ) )
288 if( p_sys->url.i_port <= 0 )
289 p_sys->url.i_port = 443;
293 if( p_sys->url.i_port <= 0 )
294 p_sys->url.i_port = 80;
298 p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
301 psz = var_CreateGetString( p_access, "http-proxy" );
304 p_sys->b_proxy = true;
305 vlc_UrlParse( &p_sys->proxy, psz, 0 );
310 char *psz_proxy = getenv( "http_proxy" );
311 if( psz_proxy && *psz_proxy )
313 p_sys->b_proxy = true;
314 vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
322 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
324 msg_Warn( p_access, "invalid proxy host" );
327 if( p_sys->proxy.i_port <= 0 )
329 p_sys->proxy.i_port = 80;
333 msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
334 p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
337 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
338 p_sys->proxy.i_port );
340 if( p_sys->url.psz_username && *p_sys->url.psz_username )
342 msg_Dbg( p_access, " user='%s', pwd='%s'",
343 p_sys->url.psz_username, p_sys->url.psz_password );
346 p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
347 p_sys->b_continuous = var_CreateGetBool( p_access, "http-continuous" );
351 switch( Connect( p_access, 0 ) )
357 /* Retry with http 1.0 */
358 msg_Dbg( p_access, "switching to HTTP version 1.0" );
359 p_sys->i_version = 0;
360 p_sys->b_seekable = false;
362 if( p_access->b_die || Connect( p_access, 0 ) )
370 msg_Err( p_access, "You should not be here" );
375 if( p_sys->i_code == 401 )
377 char *psz_login = NULL; char *psz_password = NULL;
381 if( p_sys->url.psz_username && p_sys->url.psz_password &&
382 p_sys->auth.psz_nonce && p_sys->auth.i_nonce == 0 )
386 snprintf( psz_msg, 250,
387 _("Please enter a valid login name and a password for realm %s."),
388 p_sys->auth.psz_realm );
389 msg_Dbg( p_access, "authentication failed for realm %s",
390 p_sys->auth.psz_realm );
391 i_ret = intf_UserLoginPassword( p_access, _("HTTP authentication"),
392 psz_msg, &psz_login, &psz_password );
393 if( i_ret == DIALOG_OK_YES )
395 msg_Dbg( p_access, "retrying with user=%s, pwd=%s",
396 psz_login, psz_password );
397 if( psz_login ) p_sys->url.psz_username = strdup( psz_login );
398 if( psz_password ) p_sys->url.psz_password = strdup( psz_password );
400 free( psz_password );
406 free( psz_password );
411 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
412 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
413 p_sys->psz_location && *p_sys->psz_location )
415 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
417 /* Do not accept redirection outside of HTTP works */
418 if( strncmp( p_sys->psz_location, "http", 4 )
419 || ( ( p_sys->psz_location[4] != ':' ) /* HTTP */
420 && strncmp( p_sys->psz_location + 4, "s:", 2 ) /* HTTP/SSL */ ) )
422 msg_Err( p_access, "insecure redirection ignored" );
425 free( p_access->psz_path );
426 p_access->psz_path = strdup( p_sys->psz_location );
427 /* Clean up current Open() run */
428 vlc_UrlClean( &p_sys->url );
429 AuthReset( &p_sys->auth );
430 vlc_UrlClean( &p_sys->proxy );
431 AuthReset( &p_sys->proxy_auth );
432 free( p_sys->psz_mime );
433 free( p_sys->psz_pragma );
434 free( p_sys->psz_location );
435 free( p_sys->psz_user_agent );
437 Disconnect( p_access );
438 cookies = p_sys->cookies;
441 /* Do new Open() run with new data */
442 return OpenWithCookies( p_this, cookies );
447 msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
451 if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
453 if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
455 if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
456 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
458 free( p_access->psz_demux );
459 p_access->psz_demux = strdup( "nsv" );
461 else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
462 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
464 free( p_access->psz_demux );
465 p_access->psz_demux = strdup( "m4a" );
467 else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
469 free( p_access->psz_demux );
470 p_access->psz_demux = strdup( "mp3" );
473 msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
474 p_access->psz_demux );
476 #if 0 /* Doesn't work really well because of the pre-buffering in
477 * shoutcast servers (the buffer content will be sent as fast as
479 p_sys->b_pace_control = false;
482 else if( !p_sys->psz_mime )
484 free( p_access->psz_demux );
486 p_access->psz_demux = strdup( "mp3" );
488 /* else probably Ogg Vorbis */
490 else if( !strcasecmp( p_access->psz_access, "unsv" ) &&
492 !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
494 free( p_access->psz_demux );
495 /* Grrrr! detect ultravox server and force NSV demuxer */
496 p_access->psz_demux = strdup( "nsv" );
498 else if( !strcmp( p_access->psz_access, "itpc" ) )
500 free( p_access->psz_demux );
501 p_access->psz_demux = strdup( "podcast" );
503 else if( p_sys->psz_mime &&
504 !strncasecmp( p_sys->psz_mime, "application/xspf+xml", 20 ) &&
505 ( memchr( " ;\t", p_sys->psz_mime[20], 4 ) != NULL ) )
507 free( p_access->psz_demux );
508 p_access->psz_demux = strdup( "xspf-open" );
511 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
514 var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
519 vlc_UrlClean( &p_sys->url );
520 vlc_UrlClean( &p_sys->proxy );
521 free( p_sys->psz_mime );
522 free( p_sys->psz_pragma );
523 free( p_sys->psz_location );
524 free( p_sys->psz_user_agent );
526 Disconnect( p_access );
531 /*****************************************************************************
533 *****************************************************************************/
534 static void Close( vlc_object_t *p_this )
536 access_t *p_access = (access_t*)p_this;
537 access_sys_t *p_sys = p_access->p_sys;
539 vlc_UrlClean( &p_sys->url );
540 AuthReset( &p_sys->auth );
541 vlc_UrlClean( &p_sys->proxy );
542 AuthReset( &p_sys->proxy_auth );
544 free( p_sys->psz_mime );
545 free( p_sys->psz_pragma );
546 free( p_sys->psz_location );
548 free( p_sys->psz_icy_name );
549 free( p_sys->psz_icy_genre );
550 free( p_sys->psz_icy_title );
552 free( p_sys->psz_user_agent );
554 Disconnect( p_access );
559 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
560 free(vlc_array_item_at_index( p_sys->cookies, i ));
561 vlc_array_destroy( p_sys->cookies );
565 inflateEnd( &p_sys->inflate.stream );
566 free( p_sys->inflate.p_buffer );
572 /*****************************************************************************
573 * Read: Read up to i_len bytes from the http connection and place in
574 * p_buffer. Return the actual number of bytes read
575 *****************************************************************************/
576 static int ReadICYMeta( access_t *p_access );
577 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
579 access_sys_t *p_sys = p_access->p_sys;
584 p_access->info.b_eof = true;
588 if( p_access->info.i_size > 0 &&
589 i_len + p_access->info.i_pos > p_access->info.i_size )
591 if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
593 p_access->info.b_eof = true;
598 if( p_sys->b_chunked )
600 if( p_sys->i_chunk < 0 )
602 p_access->info.b_eof = true;
606 if( p_sys->i_chunk <= 0 )
608 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
609 /* read the chunk header */
612 /* fatal error - end of file */
613 msg_Dbg( p_access, "failed reading chunk-header line" );
616 p_sys->i_chunk = strtoll( psz, NULL, 16 );
619 if( p_sys->i_chunk <= 0 ) /* eof */
622 p_access->info.b_eof = true;
627 if( i_len > p_sys->i_chunk )
629 i_len = p_sys->i_chunk;
633 if( p_sys->b_continuous && (ssize_t)i_len > p_sys->i_remaining )
635 /* Only ask for the remaining length */
636 int i_new_len = p_sys->i_remaining;
639 Request( p_access, 0 );
640 i_read = Read( p_access, p_buffer, i_len );
646 if( p_sys->i_icy_meta > 0 && p_access->info.i_pos > 0 )
648 int64_t i_next = p_sys->i_icy_meta -
649 p_access->info.i_pos % p_sys->i_icy_meta;
651 if( i_next == p_sys->i_icy_meta )
653 if( ReadICYMeta( p_access ) )
655 p_access->info.b_eof = true;
663 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, false );
667 p_access->info.i_pos += i_read;
669 if( p_sys->b_chunked )
671 p_sys->i_chunk -= i_read;
672 if( p_sys->i_chunk <= 0 )
674 /* read the empty line */
675 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
680 else if( i_read == 0 )
683 * I very much doubt that this will work.
684 * If i_read == 0, the connection *IS* dead, so the only
685 * sensible thing to do is Disconnect() and then retry.
686 * Otherwise, I got recv() completely wrong. -- Courmisch
688 if( p_sys->b_continuous )
690 Request( p_access, 0 );
691 p_sys->b_continuous = false;
692 i_read = Read( p_access, p_buffer, i_len );
693 p_sys->b_continuous = true;
695 Disconnect( p_access );
696 if( p_sys->b_reconnect )
698 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
699 if( Connect( p_access, p_access->info.i_pos ) )
701 msg_Dbg( p_access, "reconnection failed" );
705 p_sys->b_reconnect = false;
706 i_read = Read( p_access, p_buffer, i_len );
707 p_sys->b_reconnect = true;
711 if( i_read == 0 ) p_access->info.b_eof = true;
714 if( p_sys->b_continuous )
716 p_sys->i_remaining -= i_read;
722 static int ReadICYMeta( access_t *p_access )
724 access_sys_t *p_sys = p_access->p_sys;
730 /* Read meta data length */
731 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
738 i_read = buffer << 4;
739 /* msg_Dbg( p_access, "ICY meta size=%u", i_read); */
741 psz_meta = malloc( i_read + 1 );
742 if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
743 (uint8_t *)psz_meta, i_read, true ) != i_read )
746 psz_meta[i_read] = '\0'; /* Just in case */
748 /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
750 /* Now parse the meta */
751 /* Look for StreamTitle= */
752 p = strcasestr( (char *)psz_meta, "StreamTitle=" );
755 p += strlen( "StreamTitle=" );
756 if( *p == '\'' || *p == '"' )
758 char closing[] = { p[0], ';', '\0' };
759 char *psz = strstr( &p[1], closing );
761 psz = strchr( &p[1], ';' );
763 if( psz ) *psz = '\0';
767 char *psz = strchr( &p[1], ';' );
768 if( psz ) *psz = '\0';
771 if( !p_sys->psz_icy_title ||
772 strcmp( p_sys->psz_icy_title, &p[1] ) )
774 free( p_sys->psz_icy_title );
775 p_sys->psz_icy_title = strdup( &p[1] );
776 p_access->info.i_update |= INPUT_UPDATE_META;
778 msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
787 static ssize_t ReadCompressed( access_t *p_access, uint8_t *p_buffer,
790 access_sys_t *p_sys = p_access->p_sys;
792 if( p_sys->b_compressed )
796 if( !p_sys->inflate.p_buffer )
797 p_sys->inflate.p_buffer = malloc( 256 * 1024 );
799 if( p_sys->inflate.stream.avail_in == 0 )
801 ssize_t i_read = Read( p_access, p_sys->inflate.p_buffer + p_sys->inflate.stream.avail_in, 256 * 1024 );
802 if( i_read <= 0 ) return i_read;
803 p_sys->inflate.stream.next_in = p_sys->inflate.p_buffer;
804 p_sys->inflate.stream.avail_in = i_read;
807 p_sys->inflate.stream.avail_out = i_len;
808 p_sys->inflate.stream.next_out = p_buffer;
810 i_ret = inflate( &p_sys->inflate.stream, Z_SYNC_FLUSH );
811 msg_Warn( p_access, "inflate return value: %d, %s", i_ret, p_sys->inflate.stream.msg );
813 return i_len - p_sys->inflate.stream.avail_out;
817 return Read( p_access, p_buffer, i_len );
822 /*****************************************************************************
823 * Seek: close and re-open a connection at the right place
824 *****************************************************************************/
825 static int Seek( access_t *p_access, int64_t i_pos )
827 msg_Dbg( p_access, "trying to seek to %"PRId64, i_pos );
829 Disconnect( p_access );
831 if( Connect( p_access, i_pos ) )
833 msg_Err( p_access, "seek failed" );
834 p_access->info.b_eof = true;
840 /*****************************************************************************
842 *****************************************************************************/
843 static int Control( access_t *p_access, int i_query, va_list args )
845 access_sys_t *p_sys = p_access->p_sys;
854 case ACCESS_CAN_SEEK:
855 pb_bool = (bool*)va_arg( args, bool* );
856 *pb_bool = p_sys->b_seekable;
858 case ACCESS_CAN_FASTSEEK:
859 pb_bool = (bool*)va_arg( args, bool* );
862 case ACCESS_CAN_PAUSE:
863 case ACCESS_CAN_CONTROL_PACE:
864 pb_bool = (bool*)va_arg( args, bool* );
866 #if 0 /* Disable for now until we have a clock synchro algo
867 * which works with something else than MPEG over UDP */
868 *pb_bool = p_sys->b_pace_control;
875 pi_int = (int*)va_arg( args, int * );
879 case ACCESS_GET_PTS_DELAY:
880 pi_64 = (int64_t*)va_arg( args, int64_t * );
881 *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
885 case ACCESS_SET_PAUSE_STATE:
888 case ACCESS_GET_META:
889 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
891 if( p_sys->psz_icy_name )
892 vlc_meta_Set( p_meta, vlc_meta_Title, p_sys->psz_icy_name );
893 if( p_sys->psz_icy_genre )
894 vlc_meta_Set( p_meta, vlc_meta_Genre, p_sys->psz_icy_genre );
895 if( p_sys->psz_icy_title )
896 vlc_meta_Set( p_meta, vlc_meta_NowPlaying, p_sys->psz_icy_title );
899 case ACCESS_GET_CONTENT_TYPE:
900 *va_arg( args, char ** ) =
901 p_sys->psz_mime ? strdup( p_sys->psz_mime ) : NULL;
904 case ACCESS_GET_TITLE_INFO:
905 case ACCESS_SET_TITLE:
906 case ACCESS_SET_SEEKPOINT:
907 case ACCESS_SET_PRIVATE_ID_STATE:
911 msg_Warn( p_access, "unimplemented query in control" );
918 /*****************************************************************************
920 *****************************************************************************/
921 static int Connect( access_t *p_access, int64_t i_tell )
923 access_sys_t *p_sys = p_access->p_sys;
924 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
927 free( p_sys->psz_location );
928 free( p_sys->psz_mime );
929 free( p_sys->psz_pragma );
931 free( p_sys->psz_icy_genre );
932 free( p_sys->psz_icy_name );
933 free( p_sys->psz_icy_title );
936 p_sys->psz_location = NULL;
937 p_sys->psz_mime = NULL;
938 p_sys->psz_pragma = NULL;
939 p_sys->b_mms = false;
940 p_sys->b_chunked = false;
942 p_sys->i_icy_meta = 0;
943 p_sys->psz_icy_name = NULL;
944 p_sys->psz_icy_genre = NULL;
945 p_sys->psz_icy_title = NULL;
947 p_access->info.i_size = 0;
948 p_access->info.i_pos = i_tell;
949 p_access->info.b_eof = false;
952 /* Open connection */
953 p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
954 if( p_sys->fd == -1 )
956 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
960 /* Initialize TLS/SSL session */
961 if( p_sys->b_ssl == true )
963 /* CONNECT to establish TLS tunnel through HTTP proxy */
967 unsigned i_status = 0;
969 if( p_sys->i_version == 0 )
971 /* CONNECT is not in HTTP/1.0 */
972 Disconnect( p_access );
976 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
977 "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
978 p_sys->url.psz_host, p_sys->url.i_port,
980 p_sys->url.psz_host, p_sys->url.i_port);
982 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
985 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
986 Disconnect( p_access );
990 sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
993 if( ( i_status / 100 ) != 2 )
995 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
996 Disconnect( p_access );
1002 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
1005 msg_Err( p_access, "HTTP proxy connection failed" );
1006 Disconnect( p_access );
1015 if( p_access->b_die || p_access->b_error )
1017 Disconnect( p_access );
1024 /* TLS/SSL handshake */
1025 p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
1027 if( p_sys->p_tls == NULL )
1029 msg_Err( p_access, "cannot establish HTTP/TLS session" );
1030 Disconnect( p_access );
1033 p_sys->p_vs = &p_sys->p_tls->sock;
1036 return Request( p_access, i_tell ) ? -2 : 0;
1040 static int Request( access_t *p_access, int64_t i_tell )
1042 access_sys_t *p_sys = p_access->p_sys;
1044 v_socket_t *pvs = p_sys->p_vs;
1046 if( p_sys->b_proxy )
1048 if( p_sys->url.psz_path )
1050 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
1051 "GET http://%s:%d%s HTTP/1.%d\r\n",
1052 p_sys->url.psz_host, p_sys->url.i_port,
1053 p_sys->url.psz_path, p_sys->i_version );
1057 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
1058 "GET http://%s:%d/ HTTP/1.%d\r\n",
1059 p_sys->url.psz_host, p_sys->url.i_port,
1065 const char *psz_path = p_sys->url.psz_path;
1066 if( !psz_path || !*psz_path )
1070 if( p_sys->url.i_port != (pvs ? 443 : 80) )
1072 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1073 "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
1074 psz_path, p_sys->i_version, p_sys->url.psz_host,
1075 p_sys->url.i_port );
1079 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1080 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
1081 psz_path, p_sys->i_version, p_sys->url.psz_host );
1085 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "User-Agent: %s\r\n",
1086 p_sys->psz_user_agent );
1088 if( p_sys->i_version == 1 )
1090 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1091 "Range: bytes=%"PRId64"-\r\n", i_tell );
1095 if( p_sys->cookies )
1098 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
1100 const char * cookie = vlc_array_item_at_index( p_sys->cookies, i );
1101 char * psz_cookie_content = cookie_get_content( cookie );
1102 char * psz_cookie_domain = cookie_get_domain( cookie );
1104 assert( psz_cookie_content );
1106 /* FIXME: This is clearly not conforming to the rfc */
1107 bool is_in_right_domain = (!psz_cookie_domain || strstr( p_sys->url.psz_host, psz_cookie_domain ));
1109 if( is_in_right_domain )
1111 msg_Dbg( p_access, "Sending Cookie %s", psz_cookie_content );
1112 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Cookie: %s\r\n", psz_cookie_content ) < 0 )
1113 msg_Err( p_access, "failed to send Cookie" );
1115 free( psz_cookie_content );
1116 free( psz_cookie_domain );
1120 /* Authentication */
1121 if( p_sys->url.psz_username || p_sys->url.psz_password )
1122 AuthReply( p_access, "", &p_sys->url, &p_sys->auth );
1124 /* Proxy Authentication */
1125 if( p_sys->proxy.psz_username || p_sys->proxy.psz_password )
1126 AuthReply( p_access, "Proxy-", &p_sys->proxy, &p_sys->proxy_auth );
1128 /* ICY meta data request */
1129 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
1132 if( p_sys->b_continuous )
1134 net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
1135 "Connection: Keep-Alive\r\n" );
1137 else if( p_sys->i_version == 1 )
1139 net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
1140 "Connection: Close\r\n");
1143 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "\r\n" ) < 0 )
1145 msg_Err( p_access, "failed to send request" );
1146 Disconnect( p_access );
1147 return VLC_EGENERIC;
1151 if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs ) ) == NULL )
1153 msg_Err( p_access, "failed to read answer" );
1156 if( !strncmp( psz, "HTTP/1.", 7 ) )
1158 p_sys->psz_protocol = "HTTP";
1159 p_sys->i_code = atoi( &psz[9] );
1161 else if( !strncmp( psz, "ICY", 3 ) )
1163 p_sys->psz_protocol = "ICY";
1164 p_sys->i_code = atoi( &psz[4] );
1165 p_sys->b_reconnect = true;
1169 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1173 msg_Dbg( p_access, "protocol '%s' answer code %d",
1174 p_sys->psz_protocol, p_sys->i_code );
1175 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1177 p_sys->b_seekable = false;
1179 if( p_sys->i_code != 206 && p_sys->i_code != 401 )
1181 p_sys->b_seekable = false;
1183 /* Authentication error - We'll have to display the dialog */
1184 if( p_sys->i_code == 401 )
1188 /* Other fatal error */
1189 else if( p_sys->i_code >= 400 )
1191 msg_Err( p_access, "error: %s", psz );
1199 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs );
1204 msg_Err( p_access, "failed to read answer" );
1208 if( p_access->b_die || p_access->b_error )
1214 /* msg_Dbg( p_input, "Line=%s", psz ); */
1221 if( ( p = strchr( psz, ':' ) ) == NULL )
1223 msg_Err( p_access, "malformed header line: %s", psz );
1228 while( *p == ' ' ) p++;
1230 if( !strcasecmp( psz, "Content-Length" ) )
1232 if( p_sys->b_continuous )
1234 p_access->info.i_size = -1;
1235 msg_Dbg( p_access, "this frame size=%lld", atoll(p ) );
1236 p_sys->i_remaining = atoll( p );
1240 p_access->info.i_size = i_tell + atoll( p );
1241 msg_Dbg( p_access, "stream size=%"PRId64, p_access->info.i_size );
1244 else if( !strcasecmp( psz, "Location" ) )
1248 /* This does not follow RFC 2068, but yet if the url is not absolute,
1249 * handle it as everyone does. */
1252 const char *psz_http_ext = p_sys->b_ssl ? "s" : "" ;
1254 if( p_sys->url.i_port == ( p_sys->b_ssl ? 443 : 80 ) )
1256 if( asprintf(&psz_new_loc, "http%s://%s%s", psz_http_ext,
1257 p_sys->url.psz_host, p) < 0 )
1262 if( asprintf(&psz_new_loc, "http%s://%s:%d%s", psz_http_ext,
1263 p_sys->url.psz_host, p_sys->url.i_port, p) < 0 )
1269 psz_new_loc = strdup( p );
1272 free( p_sys->psz_location );
1273 p_sys->psz_location = psz_new_loc;
1275 else if( !strcasecmp( psz, "Content-Type" ) )
1277 free( p_sys->psz_mime );
1278 p_sys->psz_mime = strdup( p );
1279 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1281 else if( !strcasecmp( psz, "Content-Encoding" ) )
1283 msg_Dbg( p_access, "Content-Encoding: %s", p );
1284 if( strcasecmp( p, "identity" ) )
1286 p_sys->b_compressed = true;
1288 msg_Warn( p_access, "Compressed content not supported. Rebuild with zlib support." );
1291 else if( !strcasecmp( psz, "Pragma" ) )
1293 if( !strcasecmp( psz, "Pragma: features" ) )
1294 p_sys->b_mms = true;
1295 free( p_sys->psz_pragma );
1296 p_sys->psz_pragma = strdup( p );
1297 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1299 else if( !strcasecmp( psz, "Server" ) )
1301 msg_Dbg( p_access, "Server: %s", p );
1302 if( !strncasecmp( p, "Icecast", 7 ) ||
1303 !strncasecmp( p, "Nanocaster", 10 ) )
1305 /* Remember if this is Icecast
1306 * we need to force demux in this case without breaking
1309 /* Let live 365 streams (nanocaster) piggyback on the icecast
1310 * routine. They look very similar */
1312 p_sys->b_reconnect = true;
1313 p_sys->b_pace_control = false;
1314 p_sys->b_icecast = true;
1317 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1319 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1320 if( !strncasecmp( p, "chunked", 7 ) )
1322 p_sys->b_chunked = true;
1325 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1327 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1328 p_sys->i_icy_meta = atoi( p );
1329 if( p_sys->i_icy_meta < 0 )
1330 p_sys->i_icy_meta = 0;
1332 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1334 else if( !strcasecmp( psz, "Icy-Name" ) )
1336 free( p_sys->psz_icy_name );
1337 p_sys->psz_icy_name = strdup( p );
1338 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1340 p_sys->b_icecast = true; /* be on the safeside. set it here as well. */
1341 p_sys->b_reconnect = true;
1342 p_sys->b_pace_control = false;
1344 else if( !strcasecmp( psz, "Icy-Genre" ) )
1346 free( p_sys->psz_icy_genre );
1347 p_sys->psz_icy_genre = strdup( p );
1348 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1350 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1352 msg_Dbg( p_access, "Icy-Notice: %s", p );
1354 else if( !strncasecmp( psz, "icy-", 4 ) ||
1355 !strncasecmp( psz, "ice-", 4 ) ||
1356 !strncasecmp( psz, "x-audiocast", 11 ) )
1358 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1360 else if( !strcasecmp( psz, "Set-Cookie" ) )
1362 if( p_sys->cookies )
1364 msg_Dbg( p_access, "Accepting Cookie: %s", p );
1365 cookie_append( p_sys->cookies, strdup(p) );
1368 msg_Dbg( p_access, "We have a Cookie we won't remember: %s", p );
1370 else if( !strcasecmp( psz, "www-authenticate" ) )
1372 msg_Dbg( p_access, "Authentication header: %s", p );
1373 AuthParseHeader( p_access, p, &p_sys->auth );
1375 else if( !strcasecmp( psz, "proxy-authenticate" ) )
1377 msg_Dbg( p_access, "Proxy authentication header: %s", p );
1378 AuthParseHeader( p_access, p, &p_sys->proxy_auth );
1380 else if( !strcasecmp( psz, "authentication-info" ) )
1382 msg_Dbg( p_access, "Authentication Info header: %s", p );
1383 if( AuthCheckReply( p_access, p, &p_sys->url, &p_sys->auth ) )
1386 else if( !strcasecmp( psz, "proxy-authentication-info" ) )
1388 msg_Dbg( p_access, "Proxy Authentication Info header: %s", p );
1389 if( AuthCheckReply( p_access, p, &p_sys->proxy, &p_sys->proxy_auth ) )
1398 Disconnect( p_access );
1399 return VLC_EGENERIC;
1402 /*****************************************************************************
1404 *****************************************************************************/
1405 static void Disconnect( access_t *p_access )
1407 access_sys_t *p_sys = p_access->p_sys;
1409 if( p_sys->p_tls != NULL)
1411 tls_ClientDelete( p_sys->p_tls );
1412 p_sys->p_tls = NULL;
1415 if( p_sys->fd != -1)
1417 net_Close(p_sys->fd);
1423 /*****************************************************************************
1424 * Cookies (FIXME: we may want to rewrite that using a nice structure to hold
1425 * them) (FIXME: only support the "domain=" param)
1426 *****************************************************************************/
1428 /* Get the NAME=VALUE part of the Cookie */
1429 static char * cookie_get_content( const char * cookie )
1431 char * ret = strdup( cookie );
1432 if( !ret ) return NULL;
1434 /* Look for a ';' */
1435 while( *str && *str != ';' ) str++;
1436 /* Replace it by a end-char */
1437 if( *str == ';' ) *str = 0;
1441 /* Get the domain where the cookie is stored */
1442 static char * cookie_get_domain( const char * cookie )
1444 const char * str = cookie;
1445 static const char domain[] = "domain=";
1448 /* Look for a ';' */
1451 if( !strncmp( str, domain, sizeof(domain) - 1 /* minus \0 */ ) )
1453 str += sizeof(domain) - 1 /* minus \0 */;
1454 char * ret = strdup( str );
1455 /* Now remove the next ';' if present */
1456 char * ret_iter = ret;
1457 while( *ret_iter && *ret_iter != ';' ) ret_iter++;
1458 if( *ret_iter == ';' )
1462 /* Go to next ';' field */
1463 while( *str && *str != ';' ) str++;
1464 if( *str == ';' ) str++;
1466 while( *str && *str == ' ' ) str++;
1471 /* Get NAME in the NAME=VALUE field */
1472 static char * cookie_get_name( const char * cookie )
1474 char * ret = cookie_get_content( cookie ); /* NAME=VALUE */
1475 if( !ret ) return NULL;
1477 while( *str && *str != '=' ) str++;
1482 /* Add a cookie in cookies, checking to see how it should be added */
1483 static void cookie_append( vlc_array_t * cookies, char * cookie )
1490 char * cookie_name = cookie_get_name( cookie );
1492 /* Don't send invalid cookies */
1496 char * cookie_domain = cookie_get_domain( cookie );
1497 for( i = 0; i < vlc_array_count( cookies ); i++ )
1499 char * current_cookie = vlc_array_item_at_index( cookies, i );
1500 char * current_cookie_name = cookie_get_name( current_cookie );
1501 char * current_cookie_domain = cookie_get_domain( current_cookie );
1503 assert( current_cookie_name );
1505 bool is_domain_matching = ( cookie_domain && current_cookie_domain &&
1506 !strcmp( cookie_domain, current_cookie_domain ) );
1508 if( is_domain_matching && !strcmp( cookie_name, current_cookie_name ) )
1510 /* Remove previous value for this cookie */
1511 free( current_cookie );
1512 vlc_array_remove( cookies, i );
1515 free( current_cookie_name );
1516 free( current_cookie_domain );
1519 free( current_cookie_name );
1520 free( current_cookie_domain );
1522 free( cookie_name );
1523 free( cookie_domain );
1524 vlc_array_append( cookies, cookie );
1527 /*****************************************************************************
1528 * "RFC 2617: Basic and Digest Access Authentication" header parsing
1529 *****************************************************************************/
1530 static char *AuthGetParam( const char *psz_header, const char *psz_param )
1532 char psz_what[strlen(psz_param)+3];
1533 sprintf( psz_what, "%s=\"", psz_param );
1534 psz_header = strstr( psz_header, psz_what );
1537 const char *psz_end;
1538 psz_header += strlen( psz_what );
1539 psz_end = strchr( psz_header, '"' );
1540 if( !psz_end ) /* Invalid since we should have a closing quote */
1541 return strdup( psz_header );
1542 return strndup( psz_header, psz_end - psz_header );
1550 static char *AuthGetParamNoQuotes( const char *psz_header, const char *psz_param )
1552 char psz_what[strlen(psz_param)+2];
1553 sprintf( psz_what, "%s=", psz_param );
1554 psz_header = strstr( psz_header, psz_what );
1557 const char *psz_end;
1558 psz_header += strlen( psz_what );
1559 psz_end = strchr( psz_header, ',' );
1560 /* XXX: Do we need to filter out trailing space between the value and
1561 * the comma/end of line? */
1562 if( !psz_end ) /* Can be valid if this is the last parameter */
1563 return strdup( psz_header );
1564 return strndup( psz_header, psz_end - psz_header );
1572 static void AuthParseHeader( access_t *p_access, const char *psz_header,
1573 http_auth_t *p_auth )
1575 /* FIXME: multiple auth methods can be listed (comma seperated) */
1577 /* 2 Basic Authentication Scheme */
1578 if( !strncasecmp( psz_header, "Basic ", strlen( "Basic " ) ) )
1580 msg_Dbg( p_access, "Using Basic Authentication" );
1581 psz_header += strlen( "Basic " );
1582 p_auth->psz_realm = AuthGetParam( psz_header, "realm" );
1583 if( !p_auth->psz_realm )
1584 msg_Warn( p_access, "Basic Authentication: "
1585 "Mandatory 'realm' parameter is missing" );
1587 /* 3 Digest Access Authentication Scheme */
1588 else if( !strncasecmp( psz_header, "Digest ", strlen( "Digest " ) ) )
1590 msg_Dbg( p_access, "Using Digest Access Authentication" );
1591 if( p_auth->psz_nonce ) return; /* FIXME */
1592 psz_header += strlen( "Digest " );
1593 p_auth->psz_realm = AuthGetParam( psz_header, "realm" );
1594 p_auth->psz_domain = AuthGetParam( psz_header, "domain" );
1595 p_auth->psz_nonce = AuthGetParam( psz_header, "nonce" );
1596 p_auth->psz_opaque = AuthGetParam( psz_header, "opaque" );
1597 p_auth->psz_stale = AuthGetParamNoQuotes( psz_header, "stale" );
1598 p_auth->psz_algorithm = AuthGetParamNoQuotes( psz_header, "algorithm" );
1599 p_auth->psz_qop = AuthGetParam( psz_header, "qop" );
1600 p_auth->i_nonce = 0;
1601 /* printf("realm: |%s|\ndomain: |%s|\nnonce: |%s|\nopaque: |%s|\n"
1602 "stale: |%s|\nalgorithm: |%s|\nqop: |%s|\n",
1603 p_auth->psz_realm,p_auth->psz_domain,p_auth->psz_nonce,
1604 p_auth->psz_opaque,p_auth->psz_stale,p_auth->psz_algorithm,
1605 p_auth->psz_qop); */
1606 if( !p_auth->psz_realm )
1607 msg_Warn( p_access, "Digest Access Authentication: "
1608 "Mandatory 'realm' parameter is missing" );
1609 if( !p_auth->psz_nonce )
1610 msg_Warn( p_access, "Digest Access Authentication: "
1611 "Mandatory 'nonce' parameter is missing" );
1612 if( p_auth->psz_qop ) /* FIXME: parse the qop list */
1614 char *psz_tmp = strchr( p_auth->psz_qop, ',' );
1615 if( psz_tmp ) *psz_tmp = '\0';
1620 const char *psz_end = strchr( psz_header, ' ' );
1622 msg_Warn( p_access, "Unknown authentication scheme: '%*s'",
1623 psz_end - psz_header, psz_header );
1625 msg_Warn( p_access, "Unknown authentication scheme: '%s'",
1630 static char *AuthDigest( access_t *p_access, vlc_url_t *p_url,
1631 http_auth_t *p_auth, const char *psz_method )
1634 const char *psz_username = p_url->psz_username ?: "";
1635 const char *psz_password = p_url->psz_password ?: "";
1637 char *psz_HA1 = NULL;
1638 char *psz_HA2 = NULL;
1639 char *psz_response = NULL;
1643 if( p_auth->psz_HA1 )
1645 psz_HA1 = strdup( p_auth->psz_HA1 );
1646 if( !psz_HA1 ) goto error;
1651 AddMD5( &md5, psz_username, strlen( psz_username ) );
1652 AddMD5( &md5, ":", 1 );
1653 AddMD5( &md5, p_auth->psz_realm, strlen( p_auth->psz_realm ) );
1654 AddMD5( &md5, ":", 1 );
1655 AddMD5( &md5, psz_password, strlen( psz_password ) );
1658 psz_HA1 = psz_md5_hash( &md5 );
1659 if( !psz_HA1 ) goto error;
1661 if( p_auth->psz_algorithm
1662 && !strcmp( p_auth->psz_algorithm, "MD5-sess" ) )
1665 AddMD5( &md5, psz_HA1, 32 );
1667 AddMD5( &md5, ":", 1 );
1668 AddMD5( &md5, p_auth->psz_nonce, strlen( p_auth->psz_nonce ) );
1669 AddMD5( &md5, ":", 1 );
1670 AddMD5( &md5, p_auth->psz_cnonce, strlen( p_auth->psz_cnonce ) );
1673 psz_HA1 = psz_md5_hash( &md5 );
1674 if( !psz_HA1 ) goto error;
1675 p_auth->psz_HA1 = strdup( psz_HA1 );
1676 if( !p_auth->psz_HA1 ) goto error;
1683 AddMD5( &md5, psz_method, strlen( psz_method ) );
1684 AddMD5( &md5, ":", 1 );
1685 if( p_url->psz_path )
1686 AddMD5( &md5, p_url->psz_path, strlen( p_url->psz_path ) );
1688 AddMD5( &md5, "/", 1 );
1689 if( p_auth->psz_qop && !strcmp( p_auth->psz_qop, "auth-int" ) )
1694 AddMD5( &ent, "", 0 ); /* XXX: entity-body. should be ok for GET */
1696 psz_ent = psz_md5_hash( &ent );
1697 if( !psz_ent ) goto error;
1698 AddMD5( &md5, ":", 1 );
1699 AddMD5( &md5, psz_ent, 32 );
1703 psz_HA2 = psz_md5_hash( &md5 );
1704 if( !psz_HA2 ) goto error;
1706 /* Request digest */
1708 AddMD5( &md5, psz_HA1, 32 );
1709 AddMD5( &md5, ":", 1 );
1710 AddMD5( &md5, p_auth->psz_nonce, strlen( p_auth->psz_nonce ) );
1711 AddMD5( &md5, ":", 1 );
1713 && ( !strcmp( p_auth->psz_qop, "auth" )
1714 || !strcmp( p_auth->psz_qop, "auth-int" ) ) )
1717 snprintf( psz_inonce, 9, "%08x", p_auth->i_nonce );
1718 AddMD5( &md5, psz_inonce, 8 );
1719 AddMD5( &md5, ":", 1 );
1720 AddMD5( &md5, p_auth->psz_cnonce, strlen( p_auth->psz_cnonce ) );
1721 AddMD5( &md5, ":", 1 );
1722 AddMD5( &md5, p_auth->psz_qop, strlen( p_auth->psz_qop ) );
1723 AddMD5( &md5, ":", 1 );
1725 AddMD5( &md5, psz_HA2, 32 );
1727 psz_response = psz_md5_hash( &md5 );
1732 return psz_response;
1736 static void AuthReply( access_t *p_access, const char *psz_prefix,
1737 vlc_url_t *p_url, http_auth_t *p_auth )
1739 access_sys_t *p_sys = p_access->p_sys;
1740 v_socket_t *pvs = p_sys->p_vs;
1742 const char *psz_username = p_url->psz_username ?: "";
1743 const char *psz_password = p_url->psz_password ?: "";
1745 if( p_auth->psz_nonce )
1747 /* Digest Access Authentication */
1750 if( p_auth->psz_algorithm
1751 && strcmp( p_auth->psz_algorithm, "MD5" )
1752 && strcmp( p_auth->psz_algorithm, "MD5-sess" ) )
1754 msg_Err( p_access, "Digest Access Authentication: "
1755 "Unknown algorithm '%s'", p_auth->psz_algorithm );
1759 if( p_auth->psz_qop || !p_auth->psz_cnonce )
1761 /* FIXME: needs to be really random to prevent man in the middle
1763 free( p_auth->psz_cnonce );
1764 p_auth->psz_cnonce = strdup( "Some random string FIXME" );
1768 psz_response = AuthDigest( p_access, p_url, p_auth, "GET" );
1769 if( !psz_response ) return;
1771 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1772 "%sAuthorization: Digest "
1773 /* Mandatory parameters */
1779 /* Optional parameters */
1780 "%s%s%s" /* algorithm */
1781 "%s%s%s" /* cnonce */
1782 "%s%s%s" /* opaque */
1783 "%s%s%s" /* message qop */
1784 "%s%08x%s" /* nonce count */
1786 /* Mandatory parameters */
1791 p_url->psz_path ?: "/",
1793 /* Optional parameters */
1794 p_auth->psz_algorithm ? "algorithm=\"" : "",
1795 p_auth->psz_algorithm ?: "",
1796 p_auth->psz_algorithm ? "\", " : "",
1797 p_auth->psz_cnonce ? "cnonce=\"" : "",
1798 p_auth->psz_cnonce ?: "",
1799 p_auth->psz_cnonce ? "\", " : "",
1800 p_auth->psz_opaque ? "opaque=\"" : "",
1801 p_auth->psz_opaque ?: "",
1802 p_auth->psz_opaque ? "\", " : "",
1803 p_auth->psz_qop ? "qop=\"" : "",
1804 p_auth->psz_qop ?: "",
1805 p_auth->psz_qop ? "\", " : "",
1806 p_auth->i_nonce ? "nc=\"" : "uglyhack=\"", /* Will be parsed as an unhandled extension */
1808 p_auth->i_nonce ? "\"" : "\""
1811 free( psz_response );
1815 /* Basic Access Authentication */
1816 char buf[strlen( psz_username ) + strlen( psz_password ) + 2];
1819 snprintf( buf, sizeof( buf ), "%s:%s", psz_username, psz_password );
1820 b64 = vlc_b64_encode( buf );
1824 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1825 "%sAuthorization: Basic %s\r\n", psz_prefix, b64 );
1831 static int AuthCheckReply( access_t *p_access, const char *psz_header,
1832 vlc_url_t *p_url, http_auth_t *p_auth )
1834 int i_ret = VLC_EGENERIC;
1835 char *psz_nextnonce = AuthGetParam( psz_header, "nextnonce" );
1836 char *psz_qop = AuthGetParamNoQuotes( psz_header, "qop" );
1837 char *psz_rspauth = AuthGetParam( psz_header, "rspauth" );
1838 char *psz_cnonce = AuthGetParam( psz_header, "cnonce" );
1839 char *psz_nc = AuthGetParamNoQuotes( psz_header, "nc" );
1845 if( strcmp( psz_cnonce, p_auth->psz_cnonce ) )
1847 msg_Err( p_access, "HTTP Digest Access Authentication: server replied with a different client nonce value." );
1854 i_nonce = strtol( psz_nc, NULL, 16 );
1855 if( i_nonce != p_auth->i_nonce )
1857 msg_Err( p_access, "HTTP Digest Access Authentication: server replied with a different nonce count value." );
1862 if( psz_qop && p_auth->psz_qop && strcmp( psz_qop, p_auth->psz_qop ) )
1863 msg_Warn( p_access, "HTTP Digest Access Authentication: server replied using a different 'quality of protection' option" );
1865 /* All the clear text values match, let's now check the response
1867 psz_digest = AuthDigest( p_access, p_url, p_auth, "" );
1868 if( strcmp( psz_digest, psz_rspauth ) )
1870 msg_Err( p_access, "HTTP Digest Access Authentication: server replied with an invalid response digest (expected value: %s).", psz_digest );
1879 free( p_auth->psz_nonce );
1880 p_auth->psz_nonce = psz_nextnonce;
1881 psz_nextnonce = NULL;
1884 i_ret = VLC_SUCCESS;
1886 free( psz_nextnonce );
1888 free( psz_rspauth );
1895 static void AuthReset( http_auth_t *p_auth )
1897 FREENULL( p_auth->psz_realm );
1898 FREENULL( p_auth->psz_domain );
1899 FREENULL( p_auth->psz_nonce );
1900 FREENULL( p_auth->psz_opaque );
1901 FREENULL( p_auth->psz_stale );
1902 FREENULL( p_auth->psz_algorithm );
1903 FREENULL( p_auth->psz_qop );
1904 p_auth->i_nonce = 0;
1905 FREENULL( p_auth->psz_cnonce );
1906 FREENULL( p_auth->psz_HA1 );