1 /*****************************************************************************
2 * http.c: HTTP input module
3 *****************************************************************************
4 * Copyright (C) 2001-2005 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>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
36 #include <vlc_access.h>
38 #include <vlc_interface.h>
40 #include <vlc_network.h>
43 #include <vlc_strings.h>
44 #include <vlc_input.h>
50 /*****************************************************************************
52 *****************************************************************************/
53 static int Open ( vlc_object_t * );
54 static void Close( vlc_object_t * );
56 #define PROXY_TEXT N_("HTTP proxy")
57 #define PROXY_LONGTEXT N_( \
58 "HTTP proxy to be used It must be of the form " \
59 "http://[user[:pass]@]myproxy.mydomain:myport/ ; " \
60 "if empty, the http_proxy environment variable will be tried." )
62 #define CACHING_TEXT N_("Caching value in ms")
63 #define CACHING_LONGTEXT N_( \
64 "Caching value for HTTP streams. This " \
65 "value should be set in milliseconds." )
67 #define AGENT_TEXT N_("HTTP user agent")
68 #define AGENT_LONGTEXT N_("User agent that will be " \
69 "used for the connection.")
71 #define RECONNECT_TEXT N_("Auto re-connect")
72 #define RECONNECT_LONGTEXT N_( \
73 "Automatically try to reconnect to the stream in case of a sudden " \
76 #define CONTINUOUS_TEXT N_("Continuous stream")
77 #define CONTINUOUS_LONGTEXT N_("Read a file that is " \
78 "being constantly updated (for example, a JPG file on a server). " \
79 "You should not globally enable this option as it will break all other " \
80 "types of HTTP streams." )
82 #define FORWARD_COOKIES_TEXT N_("Forward Cookies")
83 #define FORWARD_COOKIES_LONGTEXT N_("Forward Cookies Across http redirections ")
86 set_description( _("HTTP input") );
87 set_capability( "access2", 0 );
88 set_shortname( _( "HTTP(S)" ) );
89 set_category( CAT_INPUT );
90 set_subcategory( SUBCAT_INPUT_ACCESS );
92 add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
94 add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
95 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
96 add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
97 AGENT_LONGTEXT, VLC_TRUE );
98 add_bool( "http-reconnect", 0, NULL, RECONNECT_TEXT,
99 RECONNECT_LONGTEXT, VLC_TRUE );
100 add_bool( "http-continuous", 0, NULL, CONTINUOUS_TEXT,
101 CONTINUOUS_LONGTEXT, VLC_TRUE );
102 add_bool( "http-forward-cookies", 0, NULL, FORWARD_COOKIES_TEXT,
103 FORWARD_COOKIES_LONGTEXT, VLC_TRUE );
104 add_obsolete_string("http-user");
105 add_obsolete_string("http-pwd");
106 add_shortcut( "http" );
107 add_shortcut( "https" );
108 add_shortcut( "unsv" );
109 add_shortcut( "itpc" ); /* iTunes Podcast */
110 set_callbacks( Open, Close );
113 /*****************************************************************************
115 *****************************************************************************/
119 tls_session_t *p_tls;
124 char *psz_user_agent;
132 const char *psz_protocol;
139 vlc_bool_t b_icecast;
142 vlc_bool_t b_compressed;
150 vlc_bool_t b_chunked;
160 vlc_bool_t b_seekable;
161 vlc_bool_t b_reconnect;
162 vlc_bool_t b_continuous;
163 vlc_bool_t b_pace_control;
165 vlc_array_t * cookies;
169 static int OpenWithCookies( vlc_object_t *p_this, vlc_array_t *cookies );
172 static ssize_t Read( access_t *, uint8_t *, size_t );
173 static ssize_t ReadCompressed( access_t *, uint8_t *, size_t );
174 static int Seek( access_t *, int64_t );
175 static int Control( access_t *, int, va_list );
178 static int Connect( access_t *, int64_t );
179 static int Request( access_t *p_access, int64_t i_tell );
180 static void Disconnect( access_t * );
182 /* Small Cookie utilities. Cookies support is partial. */
183 static char * cookie_get_content( const char * cookie );
184 static char * cookie_get_domain( const char * cookie );
185 static char * cookie_get_name( const char * cookie );
186 static void cookie_append( vlc_array_t * cookies, char * cookie );
188 /*****************************************************************************
190 *****************************************************************************/
191 static int Open( vlc_object_t *p_this )
193 return OpenWithCookies( p_this, NULL );
196 static int OpenWithCookies( vlc_object_t *p_this, vlc_array_t *cookies )
198 access_t *p_access = (access_t*)p_this;
201 /* Only forward an store cookies if the corresponding option is activated */
202 vlc_bool_t b_forward_cookies = var_CreateGetBool( p_access, "http-forward-cookies" );
203 vlc_array_t * saved_cookies = b_forward_cookies ? (cookies ?: vlc_array_new()) : NULL;
205 /* Set up p_access */
206 STANDARD_READ_ACCESS_INIT;
208 p_access->pf_read = ReadCompressed;
211 p_sys->b_proxy = VLC_FALSE;
212 p_sys->i_version = 1;
213 p_sys->b_seekable = VLC_TRUE;
214 p_sys->psz_mime = NULL;
215 p_sys->psz_pragma = NULL;
216 p_sys->b_mms = VLC_FALSE;
217 p_sys->b_icecast = VLC_FALSE;
218 p_sys->psz_location = NULL;
219 p_sys->psz_user_agent = NULL;
220 p_sys->b_pace_control = VLC_TRUE;
221 p_sys->b_ssl = VLC_FALSE;
223 p_sys->b_compressed = VLC_FALSE;
224 /* 15 is the max windowBits, +32 to enable optional gzip decoding */
225 if( inflateInit2( &p_sys->inflate.stream, 32+15 ) != Z_OK )
226 msg_Warn( p_access, "Error during zlib initialisation: %s",
227 p_sys->inflate.stream.msg );
228 if( zlibCompileFlags() & (1<<17) )
229 msg_Warn( p_access, "Your zlib was compiled without gzip support." );
230 p_sys->inflate.p_buffer = NULL;
234 p_sys->i_icy_meta = 0;
235 p_sys->psz_icy_name = NULL;
236 p_sys->psz_icy_genre = NULL;
237 p_sys->psz_icy_title = NULL;
238 p_sys->i_remaining = 0;
240 p_sys->cookies = saved_cookies;
242 /* Parse URI - remove spaces */
243 p = psz = strdup( p_access->psz_path );
244 while( (p = strchr( p, ' ' )) != NULL )
246 vlc_UrlParse( &p_sys->url, psz, 0 );
249 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
251 msg_Warn( p_access, "invalid host" );
254 if( !strncmp( p_access->psz_access, "https", 5 ) )
257 p_sys->b_ssl = VLC_TRUE;
258 if( p_sys->url.i_port <= 0 )
259 p_sys->url.i_port = 443;
263 if( p_sys->url.i_port <= 0 )
264 p_sys->url.i_port = 80;
268 p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
271 psz = var_CreateGetString( p_access, "http-proxy" );
274 p_sys->b_proxy = VLC_TRUE;
275 vlc_UrlParse( &p_sys->proxy, psz, 0 );
280 char *psz_proxy = getenv( "http_proxy" );
281 if( psz_proxy && *psz_proxy )
283 p_sys->b_proxy = VLC_TRUE;
284 vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
292 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
294 msg_Warn( p_access, "invalid proxy host" );
297 if( p_sys->proxy.i_port <= 0 )
299 p_sys->proxy.i_port = 80;
303 msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
304 p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
307 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
308 p_sys->proxy.i_port );
310 if( p_sys->url.psz_username && *p_sys->url.psz_username )
312 msg_Dbg( p_access, " user='%s', pwd='%s'",
313 p_sys->url.psz_username, p_sys->url.psz_password );
316 p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
317 p_sys->b_continuous = var_CreateGetBool( p_access, "http-continuous" );
321 switch( Connect( p_access, 0 ) )
327 /* Retry with http 1.0 */
328 msg_Dbg( p_access, "switching to HTTP version 1.0" );
329 p_sys->i_version = 0;
330 p_sys->b_seekable = VLC_FALSE;
332 if( p_access->b_die || Connect( p_access, 0 ) )
340 msg_Err( p_access, "You should not be here" );
345 if( p_sys->i_code == 401 )
347 char *psz_login = NULL; char *psz_password = NULL;
349 msg_Dbg( p_access, "authentication failed" );
350 i_ret = intf_UserLoginPassword( p_access, _("HTTP authentication"),
351 _("Please enter a valid login name and a password."),
352 &psz_login, &psz_password );
353 if( i_ret == DIALOG_OK_YES )
355 msg_Dbg( p_access, "retrying with user=%s, pwd=%s",
356 psz_login, psz_password );
357 if( psz_login ) p_sys->url.psz_username = strdup( psz_login );
358 if( psz_password ) p_sys->url.psz_password = strdup( psz_password );
359 if( psz_login ) free( psz_login );
360 if( psz_password ) free( psz_password );
365 if( psz_login ) free( psz_login );
366 if( psz_password ) free( psz_password );
371 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
372 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
373 p_sys->psz_location && *p_sys->psz_location )
375 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
377 /* Do not accept redirection outside of HTTP works */
378 if( strncmp( p_sys->psz_location, "http", 4 )
379 || ( ( p_sys->psz_location[4] != ':' ) /* HTTP */
380 && strncmp( p_sys->psz_location + 4, "s:", 2 ) /* HTTP/SSL */ ) )
382 msg_Err( p_access, "insecure redirection ignored" );
385 free( p_access->psz_path );
386 p_access->psz_path = strdup( p_sys->psz_location );
387 /* Clean up current Open() run */
388 vlc_UrlClean( &p_sys->url );
389 vlc_UrlClean( &p_sys->proxy );
390 free( p_sys->psz_mime );
391 free( p_sys->psz_pragma );
392 free( p_sys->psz_location );
393 free( p_sys->psz_user_agent );
395 Disconnect( p_access );
396 cookies = p_sys->cookies;
399 /* Do new Open() run with new data */
400 return OpenWithCookies( p_this, cookies );
405 msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
409 if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
411 if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
413 if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
414 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
415 p_access->psz_demux = strdup( "nsv" );
416 else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
417 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
418 p_access->psz_demux = strdup( "m4a" );
419 else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
420 p_access->psz_demux = strdup( "mp3" );
422 msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
423 p_access->psz_demux );
425 #if 0 /* Doesn't work really well because of the pre-buffering in
426 * shoutcast servers (the buffer content will be sent as fast as
428 p_sys->b_pace_control = VLC_FALSE;
431 else if( !p_sys->psz_mime )
434 p_access->psz_demux = strdup( "mp3" );
436 /* else probably Ogg Vorbis */
438 else if( !strcasecmp( p_access->psz_access, "unsv" ) &&
440 !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
442 /* Grrrr! detect ultravox server and force NSV demuxer */
443 p_access->psz_demux = strdup( "nsv" );
445 else if( !strcmp( p_access->psz_access, "itpc" ) )
447 p_access->psz_demux = strdup( "podcast" );
449 else if( p_sys->psz_mime &&
450 !strncasecmp( p_sys->psz_mime, "application/xspf+xml", 20 ) &&
451 ( memchr( " ;\t", p_sys->psz_mime[20], 4 ) != NULL ) )
452 p_access->psz_demux = strdup( "xspf-open" );
454 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
457 var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
462 vlc_UrlClean( &p_sys->url );
463 vlc_UrlClean( &p_sys->proxy );
464 free( p_sys->psz_mime );
465 free( p_sys->psz_pragma );
466 free( p_sys->psz_location );
467 free( p_sys->psz_user_agent );
469 Disconnect( p_access );
474 /*****************************************************************************
476 *****************************************************************************/
477 static void Close( vlc_object_t *p_this )
479 access_t *p_access = (access_t*)p_this;
480 access_sys_t *p_sys = p_access->p_sys;
482 vlc_UrlClean( &p_sys->url );
483 vlc_UrlClean( &p_sys->proxy );
485 free( p_sys->psz_mime );
486 free( p_sys->psz_pragma );
487 free( p_sys->psz_location );
489 free( p_sys->psz_icy_name );
490 free( p_sys->psz_icy_genre );
491 free( p_sys->psz_icy_title );
493 free( p_sys->psz_user_agent );
495 Disconnect( p_access );
500 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
501 free(vlc_array_item_at_index( p_sys->cookies, i ));
502 vlc_array_destroy( p_sys->cookies );
506 inflateEnd( &p_sys->inflate.stream );
507 free( p_sys->inflate.p_buffer );
513 /*****************************************************************************
514 * Read: Read up to i_len bytes from the http connection and place in
515 * p_buffer. Return the actual number of bytes read
516 *****************************************************************************/
517 static int ReadICYMeta( access_t *p_access );
518 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
520 access_sys_t *p_sys = p_access->p_sys;
525 p_access->info.b_eof = VLC_TRUE;
529 if( p_access->info.i_size > 0 &&
530 i_len + p_access->info.i_pos > p_access->info.i_size )
532 if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
534 p_access->info.b_eof = VLC_TRUE;
539 if( p_sys->b_chunked )
541 if( p_sys->i_chunk < 0 )
543 p_access->info.b_eof = VLC_TRUE;
547 if( p_sys->i_chunk <= 0 )
549 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
550 /* read the chunk header */
553 /* fatal error - end of file */
554 msg_Dbg( p_access, "failed reading chunk-header line" );
557 p_sys->i_chunk = strtoll( psz, NULL, 16 );
560 if( p_sys->i_chunk <= 0 ) /* eof */
563 p_access->info.b_eof = VLC_TRUE;
568 if( i_len > p_sys->i_chunk )
570 i_len = p_sys->i_chunk;
574 if( p_sys->b_continuous && (ssize_t)i_len > p_sys->i_remaining )
576 /* Only ask for the remaining length */
577 int i_new_len = p_sys->i_remaining;
580 Request( p_access, 0 );
581 i_read = Read( p_access, p_buffer, i_len );
587 if( p_sys->i_icy_meta > 0 && p_access->info.i_pos > 0 )
589 int64_t i_next = p_sys->i_icy_meta -
590 p_access->info.i_pos % p_sys->i_icy_meta;
592 if( i_next == p_sys->i_icy_meta )
594 if( ReadICYMeta( p_access ) )
596 p_access->info.b_eof = VLC_TRUE;
604 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, VLC_FALSE );
608 p_access->info.i_pos += i_read;
610 if( p_sys->b_chunked )
612 p_sys->i_chunk -= i_read;
613 if( p_sys->i_chunk <= 0 )
615 /* read the empty line */
616 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
617 if( psz ) free( psz );
621 else if( i_read == 0 )
624 * I very much doubt that this will work.
625 * If i_read == 0, the connection *IS* dead, so the only
626 * sensible thing to do is Disconnect() and then retry.
627 * Otherwise, I got recv() completely wrong. -- Courmisch
629 if( p_sys->b_continuous )
631 Request( p_access, 0 );
632 p_sys->b_continuous = VLC_FALSE;
633 i_read = Read( p_access, p_buffer, i_len );
634 p_sys->b_continuous = VLC_TRUE;
636 Disconnect( p_access );
637 if( p_sys->b_reconnect )
639 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
640 if( Connect( p_access, p_access->info.i_pos ) )
642 msg_Dbg( p_access, "reconnection failed" );
646 p_sys->b_reconnect = VLC_FALSE;
647 i_read = Read( p_access, p_buffer, i_len );
648 p_sys->b_reconnect = VLC_TRUE;
652 if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
655 if( p_sys->b_continuous )
657 p_sys->i_remaining -= i_read;
663 static int ReadICYMeta( access_t *p_access )
665 access_sys_t *p_sys = p_access->p_sys;
671 /* Read meta data length */
672 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
679 i_read = buffer << 4;
680 /* msg_Dbg( p_access, "ICY meta size=%u", i_read); */
682 psz_meta = malloc( i_read + 1 );
683 if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
684 (uint8_t *)psz_meta, i_read, VLC_TRUE ) != i_read )
687 psz_meta[i_read] = '\0'; /* Just in case */
689 /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
691 /* Now parse the meta */
692 /* Look for StreamTitle= */
693 p = strcasestr( (char *)psz_meta, "StreamTitle=" );
696 p += strlen( "StreamTitle=" );
697 if( *p == '\'' || *p == '"' )
699 char closing[] = { p[0], ';', '\0' };
700 char *psz = strstr( &p[1], closing );
702 psz = strchr( &p[1], ';' );
704 if( psz ) *psz = '\0';
708 char *psz = strchr( &p[1], ';' );
709 if( psz ) *psz = '\0';
712 if( !p_sys->psz_icy_title ||
713 strcmp( p_sys->psz_icy_title, &p[1] ) )
715 if( p_sys->psz_icy_title )
716 free( p_sys->psz_icy_title );
717 p_sys->psz_icy_title = strdup( &p[1] );
718 p_access->info.i_update |= INPUT_UPDATE_META;
720 msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
729 static ssize_t ReadCompressed( access_t *p_access, uint8_t *p_buffer,
732 access_sys_t *p_sys = p_access->p_sys;
734 if( p_sys->b_compressed )
738 if( !p_sys->inflate.p_buffer )
739 p_sys->inflate.p_buffer = malloc( 256 * 1024 );
741 if( p_sys->inflate.stream.avail_in == 0 )
743 ssize_t i_read = Read( p_access, p_sys->inflate.p_buffer + p_sys->inflate.stream.avail_in, 256 * 1024 );
744 if( i_read <= 0 ) return i_read;
745 p_sys->inflate.stream.next_in = p_sys->inflate.p_buffer;
746 p_sys->inflate.stream.avail_in = i_read;
749 p_sys->inflate.stream.avail_out = i_len;
750 p_sys->inflate.stream.next_out = p_buffer;
752 i_ret = inflate( &p_sys->inflate.stream, Z_SYNC_FLUSH );
753 msg_Warn( p_access, "inflate return value: %d, %s", i_ret, p_sys->inflate.stream.msg );
755 return i_len - p_sys->inflate.stream.avail_out;
759 return Read( p_access, p_buffer, i_len );
764 /*****************************************************************************
765 * Seek: close and re-open a connection at the right place
766 *****************************************************************************/
767 static int Seek( access_t *p_access, int64_t i_pos )
769 msg_Dbg( p_access, "trying to seek to "I64Fd, i_pos );
771 Disconnect( p_access );
773 if( Connect( p_access, i_pos ) )
775 msg_Err( p_access, "seek failed" );
776 p_access->info.b_eof = VLC_TRUE;
782 /*****************************************************************************
784 *****************************************************************************/
785 static int Control( access_t *p_access, int i_query, va_list args )
787 access_sys_t *p_sys = p_access->p_sys;
796 case ACCESS_CAN_SEEK:
797 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
798 *pb_bool = p_sys->b_seekable;
800 case ACCESS_CAN_FASTSEEK:
801 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
802 *pb_bool = VLC_FALSE;
804 case ACCESS_CAN_PAUSE:
805 case ACCESS_CAN_CONTROL_PACE:
806 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
808 #if 0 /* Disable for now until we have a clock synchro algo
809 * which works with something else than MPEG over UDP */
810 *pb_bool = p_sys->b_pace_control;
817 pi_int = (int*)va_arg( args, int * );
821 case ACCESS_GET_PTS_DELAY:
822 pi_64 = (int64_t*)va_arg( args, int64_t * );
823 *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
827 case ACCESS_SET_PAUSE_STATE:
830 case ACCESS_GET_META:
831 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
833 if( p_sys->psz_icy_name )
834 vlc_meta_Set( p_meta, vlc_meta_Title, p_sys->psz_icy_name );
835 if( p_sys->psz_icy_genre )
836 vlc_meta_Set( p_meta, vlc_meta_Genre, p_sys->psz_icy_genre );
837 if( p_sys->psz_icy_title )
838 vlc_meta_Set( p_meta, vlc_meta_NowPlaying, p_sys->psz_icy_title );
841 case ACCESS_GET_CONTENT_TYPE:
842 *va_arg( args, char ** ) =
843 p_sys->psz_mime ? strdup( p_sys->psz_mime ) : NULL;
846 case ACCESS_GET_TITLE_INFO:
847 case ACCESS_SET_TITLE:
848 case ACCESS_SET_SEEKPOINT:
849 case ACCESS_SET_PRIVATE_ID_STATE:
853 msg_Warn( p_access, "unimplemented query in control" );
860 /*****************************************************************************
862 *****************************************************************************/
863 static int Connect( access_t *p_access, int64_t i_tell )
865 access_sys_t *p_sys = p_access->p_sys;
866 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
869 free( p_sys->psz_location );
870 free( p_sys->psz_mime );
871 free( p_sys->psz_pragma );
873 free( p_sys->psz_icy_genre );
874 free( p_sys->psz_icy_name );
875 free( p_sys->psz_icy_title );
878 p_sys->psz_location = NULL;
879 p_sys->psz_mime = NULL;
880 p_sys->psz_pragma = NULL;
881 p_sys->b_mms = VLC_FALSE;
882 p_sys->b_chunked = VLC_FALSE;
884 p_sys->i_icy_meta = 0;
885 p_sys->psz_icy_name = NULL;
886 p_sys->psz_icy_genre = NULL;
887 p_sys->psz_icy_title = NULL;
889 p_access->info.i_size = 0;
890 p_access->info.i_pos = i_tell;
891 p_access->info.b_eof = VLC_FALSE;
894 /* Open connection */
895 p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
896 if( p_sys->fd == -1 )
898 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
902 /* Initialize TLS/SSL session */
903 if( p_sys->b_ssl == VLC_TRUE )
905 /* CONNECT to establish TLS tunnel through HTTP proxy */
909 unsigned i_status = 0;
911 if( p_sys->i_version == 0 )
913 /* CONNECT is not in HTTP/1.0 */
914 Disconnect( p_access );
918 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
919 "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
920 p_sys->url.psz_host, p_sys->url.i_port,
922 p_sys->url.psz_host, p_sys->url.i_port);
924 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
927 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
928 Disconnect( p_access );
932 sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
935 if( ( i_status / 100 ) != 2 )
937 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
938 Disconnect( p_access );
944 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
947 msg_Err( p_access, "HTTP proxy connection failed" );
948 Disconnect( p_access );
960 /* TLS/SSL handshake */
961 p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
963 if( p_sys->p_tls == NULL )
965 msg_Err( p_access, "cannot establish HTTP/TLS session" );
966 Disconnect( p_access );
969 p_sys->p_vs = &p_sys->p_tls->sock;
972 return Request( p_access, i_tell ) ? -2 : 0;
976 static int Request( access_t *p_access, int64_t i_tell )
978 access_sys_t *p_sys = p_access->p_sys;
980 v_socket_t *pvs = p_sys->p_vs;
984 if( p_sys->url.psz_path )
986 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
987 "GET http://%s:%d%s HTTP/1.%d\r\n",
988 p_sys->url.psz_host, p_sys->url.i_port,
989 p_sys->url.psz_path, p_sys->i_version );
993 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
994 "GET http://%s:%d/ HTTP/1.%d\r\n",
995 p_sys->url.psz_host, p_sys->url.i_port,
1001 const char *psz_path = p_sys->url.psz_path;
1002 if( !psz_path || !*psz_path )
1006 if( p_sys->url.i_port != 80)
1008 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1009 "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
1010 psz_path, p_sys->i_version, p_sys->url.psz_host,
1011 p_sys->url.i_port );
1015 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1016 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
1017 psz_path, p_sys->i_version, p_sys->url.psz_host );
1021 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "User-Agent: %s\r\n",
1022 p_sys->psz_user_agent );
1024 if( p_sys->i_version == 1 )
1026 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1027 "Range: bytes="I64Fd"-\r\n", i_tell );
1031 if( p_sys->cookies )
1034 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
1036 const char * cookie = vlc_array_item_at_index( p_sys->cookies, i );
1037 char * psz_cookie_content = cookie_get_content( cookie );
1038 char * psz_cookie_domain = cookie_get_domain( cookie );
1040 assert( psz_cookie_content );
1042 /* FIXME: This is clearly not conforming to the rfc */
1043 vlc_bool_t is_in_right_domain = (!psz_cookie_domain || strstr( p_sys->url.psz_host, psz_cookie_domain ));
1045 if( is_in_right_domain )
1047 msg_Dbg( p_access, "Sending Cookie %s", psz_cookie_content );
1048 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Cookie: %s\r\n", psz_cookie_content ) < 0 )
1049 msg_Err( p_access, "failed to send Cookie" );
1051 free( psz_cookie_content );
1052 free( psz_cookie_domain );
1056 /* Authentication */
1057 if( p_sys->url.psz_username || p_sys->url.psz_password )
1059 char buf[strlen( p_sys->url.psz_username ?: "" )
1060 + strlen( p_sys->url.psz_password ?: "" ) + 2];
1063 snprintf( buf, sizeof( buf ), "%s:%s", p_sys->url.psz_username ?: "",
1064 p_sys->url.psz_password ?: "" );
1065 b64 = vlc_b64_encode( buf );
1069 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1070 "Authorization: Basic %s\r\n", b64 );
1075 /* Proxy Authentication */
1076 if( p_sys->proxy.psz_username || p_sys->proxy.psz_password )
1078 char buf[strlen( p_sys->proxy.psz_username ?: "" )
1079 + strlen( p_sys->proxy.psz_password ?: "" )];
1082 snprintf( buf, sizeof( buf ), "%s:%s", p_sys->proxy.psz_username ?: "",
1083 p_sys->proxy.psz_password ?: "" );
1084 b64 = vlc_b64_encode( buf );
1088 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1089 "Proxy-Authorization: Basic %s\r\n", b64 );
1094 /* ICY meta data request */
1095 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
1098 if( p_sys->b_continuous )
1100 net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
1101 "Connection: Keep-Alive\r\n" );
1103 else if( p_sys->i_version == 1 )
1105 net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
1106 "Connection: Close\r\n");
1109 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "\r\n" ) < 0 )
1111 msg_Err( p_access, "failed to send request" );
1112 Disconnect( p_access );
1113 return VLC_EGENERIC;
1117 if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs ) ) == NULL )
1119 msg_Err( p_access, "failed to read answer" );
1122 if( !strncmp( psz, "HTTP/1.", 7 ) )
1124 p_sys->psz_protocol = "HTTP";
1125 p_sys->i_code = atoi( &psz[9] );
1127 else if( !strncmp( psz, "ICY", 3 ) )
1129 p_sys->psz_protocol = "ICY";
1130 p_sys->i_code = atoi( &psz[4] );
1131 p_sys->b_reconnect = VLC_TRUE;
1135 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1139 msg_Dbg( p_access, "protocol '%s' answer code %d",
1140 p_sys->psz_protocol, p_sys->i_code );
1141 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1143 p_sys->b_seekable = VLC_FALSE;
1145 if( p_sys->i_code != 206 && p_sys->i_code != 401 )
1147 p_sys->b_seekable = VLC_FALSE;
1149 /* Authentication error - We'll have to display the dialog */
1150 if( p_sys->i_code == 401 )
1154 /* Other fatal error */
1155 else if( p_sys->i_code >= 400 )
1157 msg_Err( p_access, "error: %s", psz );
1165 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs );
1170 msg_Err( p_access, "failed to read answer" );
1174 /* msg_Dbg( p_input, "Line=%s", psz ); */
1182 if( ( p = strchr( psz, ':' ) ) == NULL )
1184 msg_Err( p_access, "malformed header line: %s", psz );
1189 while( *p == ' ' ) p++;
1191 if( !strcasecmp( psz, "Content-Length" ) )
1193 if( p_sys->b_continuous )
1195 p_access->info.i_size = -1;
1196 msg_Dbg( p_access, "this frame size=%lld", atoll(p ) );
1197 p_sys->i_remaining = atoll( p );
1201 p_access->info.i_size = i_tell + atoll( p );
1202 msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
1205 else if( !strcasecmp( psz, "Location" ) )
1209 /* This does not follow RFC 2068, but yet if the url is not absolute,
1210 * handle it as everyone does. */
1213 const char *psz_http_ext = p_sys->b_ssl ? "s" : "" ;
1215 if( p_sys->url.i_port == ( p_sys->b_ssl ? 443 : 80 ) )
1217 if( asprintf(&psz_new_loc, "http%s://%s%s", psz_http_ext,
1218 p_sys->url.psz_host, p) < 0 )
1223 if( asprintf(&psz_new_loc, "http%s://%s:%d%s", psz_http_ext,
1224 p_sys->url.psz_host, p_sys->url.i_port, p) < 0 )
1230 psz_new_loc = strdup( p );
1233 if( p_sys->psz_location ) free( p_sys->psz_location );
1234 p_sys->psz_location = psz_new_loc;
1236 else if( !strcasecmp( psz, "Content-Type" ) )
1238 if( p_sys->psz_mime ) free( p_sys->psz_mime );
1239 p_sys->psz_mime = strdup( p );
1240 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1242 else if( !strcasecmp( psz, "Content-Encoding" ) )
1244 msg_Dbg( p_access, "Content-Encoding: %s", p );
1245 if( strcasecmp( p, "identity" ) )
1247 p_sys->b_compressed = VLC_TRUE;
1249 msg_Warn( p_access, "Compressed content not supported. Rebuild with zlib support." );
1252 else if( !strcasecmp( psz, "Pragma" ) )
1254 if( !strcasecmp( psz, "Pragma: features" ) )
1255 p_sys->b_mms = VLC_TRUE;
1256 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
1257 p_sys->psz_pragma = strdup( p );
1258 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1260 else if( !strcasecmp( psz, "Server" ) )
1262 msg_Dbg( p_access, "Server: %s", p );
1263 if( !strncasecmp( p, "Icecast", 7 ) ||
1264 !strncasecmp( p, "Nanocaster", 10 ) )
1266 /* Remember if this is Icecast
1267 * we need to force demux in this case without breaking
1270 /* Let live 365 streams (nanocaster) piggyback on the icecast
1271 * routine. They look very similar */
1273 p_sys->b_reconnect = VLC_TRUE;
1274 p_sys->b_pace_control = VLC_FALSE;
1275 p_sys->b_icecast = VLC_TRUE;
1278 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1280 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1281 if( !strncasecmp( p, "chunked", 7 ) )
1283 p_sys->b_chunked = VLC_TRUE;
1286 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1288 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1289 p_sys->i_icy_meta = atoi( p );
1290 if( p_sys->i_icy_meta < 0 )
1291 p_sys->i_icy_meta = 0;
1293 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1295 else if( !strcasecmp( psz, "Icy-Name" ) )
1297 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
1298 p_sys->psz_icy_name = strdup( p );
1299 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1301 p_sys->b_icecast = VLC_TRUE; /* be on the safeside. set it here as well. */
1302 p_sys->b_reconnect = VLC_TRUE;
1303 p_sys->b_pace_control = VLC_FALSE;
1305 else if( !strcasecmp( psz, "Icy-Genre" ) )
1307 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
1308 p_sys->psz_icy_genre = strdup( p );
1309 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1311 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1313 msg_Dbg( p_access, "Icy-Notice: %s", p );
1315 else if( !strncasecmp( psz, "icy-", 4 ) ||
1316 !strncasecmp( psz, "ice-", 4 ) ||
1317 !strncasecmp( psz, "x-audiocast", 11 ) )
1319 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1320 } else if( !strcasecmp( psz, "Set-Cookie" ) )
1322 if( p_sys->cookies )
1324 msg_Dbg( p_access, "Accepting Cookie: %s", p );
1325 cookie_append( p_sys->cookies, strdup(p) );
1328 msg_Dbg( p_access, "We have a Cookie we won't remember: %s", p );
1336 Disconnect( p_access );
1337 return VLC_EGENERIC;
1340 /*****************************************************************************
1342 *****************************************************************************/
1343 static void Disconnect( access_t *p_access )
1345 access_sys_t *p_sys = p_access->p_sys;
1347 if( p_sys->p_tls != NULL)
1349 tls_ClientDelete( p_sys->p_tls );
1350 p_sys->p_tls = NULL;
1353 if( p_sys->fd != -1)
1355 net_Close(p_sys->fd);
1361 /*****************************************************************************
1362 * Cookies (FIXME: we may want to rewrite that using a nice structure to hold
1363 * them) (FIXME: only support the "domain=" param)
1364 *****************************************************************************/
1366 /* Get the NAME=VALUE part of the Cookie */
1367 static char * cookie_get_content( const char * cookie )
1369 char * ret = strdup( cookie );
1370 if( !ret ) return NULL;
1372 /* Look for a ';' */
1373 while( *str && *str != ';' ) str++;
1374 /* Replace it by a end-char */
1375 if( *str == ';' ) *str = 0;
1379 /* Get the domain where the cookie is stored */
1380 static char * cookie_get_domain( const char * cookie )
1382 const char * str = cookie;
1383 static const char domain[] = "domain=";
1386 /* Look for a ';' */
1389 if( !strncmp( str, domain, sizeof(domain) - 1 /* minus \0 */ ) )
1391 str += sizeof(domain) - 1 /* minus \0 */;
1392 char * ret = strdup( str );
1393 /* Now remove the next ';' if present */
1394 char * ret_iter = ret;
1395 while( *ret_iter && *ret_iter != ';' ) ret_iter++;
1396 if( *ret_iter == ';' )
1400 /* Go to next ';' field */
1401 while( *str && *str != ';' ) str++;
1402 if( *str == ';' ) str++;
1404 while( *str && *str == ' ' ) str++;
1409 /* Get NAME in the NAME=VALUE field */
1410 static char * cookie_get_name( const char * cookie )
1412 char * ret = cookie_get_content( cookie ); /* NAME=VALUE */
1413 if( !ret ) return NULL;
1415 while( *str && *str != '=' ) str++;
1420 /* Add a cookie in cookies, checking to see how it should be added */
1421 static void cookie_append( vlc_array_t * cookies, char * cookie )
1428 char * cookie_name = cookie_get_name( cookie );
1430 /* Don't send invalid cookies */
1434 char * cookie_domain = cookie_get_domain( cookie );
1435 for( i = 0; i < vlc_array_count( cookies ); i++ )
1437 char * current_cookie = vlc_array_item_at_index( cookies, i );
1438 char * current_cookie_name = cookie_get_name( current_cookie );
1439 char * current_cookie_domain = cookie_get_domain( current_cookie );
1441 assert( current_cookie_name );
1443 vlc_bool_t is_domain_matching = ( cookie_domain && current_cookie_domain &&
1444 !strcmp( cookie_domain, current_cookie_domain ) );
1446 if( is_domain_matching && !strcmp( cookie_name, current_cookie_name ) )
1448 /* Remove previous value for this cookie */
1449 free( current_cookie );
1450 vlc_array_remove( cookies, i );
1453 free( current_cookie_name );
1454 free( current_cookie_domain );
1457 free( current_cookie_name );
1458 free( current_cookie_domain );
1460 free( cookie_name );
1461 free( cookie_domain );
1462 vlc_array_append( cookies, cookie );