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 *****************************************************************************/
32 #include <vlc_access.h>
34 #include <vlc_interface.h>
36 #include <vlc_network.h>
39 #include <vlc_strings.h>
40 #include <vlc_input.h>
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Open ( vlc_object_t * );
46 static void Close( vlc_object_t * );
48 #define PROXY_TEXT N_("HTTP proxy")
49 #define PROXY_LONGTEXT N_( \
50 "HTTP proxy to be used It must be of the form " \
51 "http://[user[:pass]@]myproxy.mydomain:myport/ ; " \
52 "if empty, the http_proxy environment variable will be tried." )
54 #define CACHING_TEXT N_("Caching value in ms")
55 #define CACHING_LONGTEXT N_( \
56 "Caching value for HTTP streams. This " \
57 "value should be set in milliseconds." )
59 #define AGENT_TEXT N_("HTTP user agent")
60 #define AGENT_LONGTEXT N_("User agent that will be " \
61 "used for the connection.")
63 #define RECONNECT_TEXT N_("Auto re-connect")
64 #define RECONNECT_LONGTEXT N_( \
65 "Automatically try to reconnect to the stream in case of a sudden " \
68 #define CONTINUOUS_TEXT N_("Continuous stream")
69 #define CONTINUOUS_LONGTEXT N_("Read a file that is " \
70 "being constantly updated (for example, a JPG file on a server). " \
71 "You should not globally enable this option as it will break all other " \
72 "types of HTTP streams." )
74 #define FORWARD_COOKIES_TEXT N_("Forward Cookies")
75 #define FORWARD_COOKIES_LONGTEXT N_("Forward Cookies Across http redirections ")
78 set_description( _("HTTP input") );
79 set_capability( "access2", 0 );
80 set_shortname( _( "HTTP(S)" ) );
81 set_category( CAT_INPUT );
82 set_subcategory( SUBCAT_INPUT_ACCESS );
84 add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
86 add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
87 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
88 add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
89 AGENT_LONGTEXT, VLC_TRUE );
90 add_bool( "http-reconnect", 0, NULL, RECONNECT_TEXT,
91 RECONNECT_LONGTEXT, VLC_TRUE );
92 add_bool( "http-continuous", 0, NULL, CONTINUOUS_TEXT,
93 CONTINUOUS_LONGTEXT, VLC_TRUE );
94 add_bool( "http-forward-cookies", 0, NULL, FORWARD_COOKIES_TEXT,
95 FORWARD_COOKIES_LONGTEXT, VLC_TRUE );
96 add_obsolete_string("http-user");
97 add_obsolete_string("http-pwd");
98 add_shortcut( "http" );
99 add_shortcut( "https" );
100 add_shortcut( "unsv" );
101 add_shortcut( "itpc" ); /* iTunes Podcast */
102 set_callbacks( Open, Close );
105 /*****************************************************************************
107 *****************************************************************************/
111 tls_session_t *p_tls;
116 char *psz_user_agent;
124 const char *psz_protocol;
131 vlc_bool_t b_icecast;
134 vlc_bool_t b_chunked;
144 vlc_bool_t b_seekable;
145 vlc_bool_t b_reconnect;
146 vlc_bool_t b_continuous;
147 vlc_bool_t b_pace_control;
149 vlc_array_t * cookies;
153 static int OpenWithRedirectionStatus( vlc_object_t *p_this, vlc_bool_t b_is_from_redirection );
156 static ssize_t Read( access_t *, uint8_t *, size_t );
157 static int Seek( access_t *, int64_t );
158 static int Control( access_t *, int, va_list );
161 static int Connect( access_t *, int64_t );
162 static int Request( access_t *p_access, int64_t i_tell );
163 static void Disconnect( access_t * );
165 /* Small Cookie utilities. Cookies support is partial. */
166 static char * cookie_get_content( const char * cookie );
167 static char * cookie_get_domain( const char * cookie );
168 static char * cookie_get_name( const char * cookie );
169 static void cookie_append( vlc_array_t * cookies, char * cookie );
171 /*****************************************************************************
173 *****************************************************************************/
174 static int Open( vlc_object_t *p_this )
176 return OpenWithRedirectionStatus( p_this, VLC_FALSE );
179 static int OpenWithRedirectionStatus( vlc_object_t *p_this, vlc_bool_t b_is_from_redirection )
181 access_t *p_access = (access_t*)p_this;
184 /* Only forward an store cookies if the corresponding option is activated */
185 vlc_bool_t b_forward_cookies = var_CreateGetBool( p_access, "http-forward-cookies" );
186 vlc_array_t * saved_cookies = b_forward_cookies ? (b_is_from_redirection ? p_access->p_sys->cookies : vlc_array_new()) : NULL;
188 /* Set up p_access */
189 STANDARD_READ_ACCESS_INIT;
191 p_sys->b_proxy = VLC_FALSE;
192 p_sys->i_version = 1;
193 p_sys->b_seekable = VLC_TRUE;
194 p_sys->psz_mime = NULL;
195 p_sys->psz_pragma = NULL;
196 p_sys->b_mms = VLC_FALSE;
197 p_sys->b_icecast = VLC_FALSE;
198 p_sys->psz_location = NULL;
199 p_sys->psz_user_agent = NULL;
200 p_sys->b_pace_control = VLC_TRUE;
201 p_sys->b_ssl = VLC_FALSE;
204 p_sys->i_icy_meta = 0;
205 p_sys->psz_icy_name = NULL;
206 p_sys->psz_icy_genre = NULL;
207 p_sys->psz_icy_title = NULL;
208 p_sys->i_remaining = 0;
210 p_sys->cookies = saved_cookies;
212 /* Parse URI - remove spaces */
213 p = psz = strdup( p_access->psz_path );
214 while( (p = strchr( p, ' ' )) != NULL )
216 vlc_UrlParse( &p_sys->url, psz, 0 );
219 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
221 msg_Warn( p_access, "invalid host" );
224 if( !strncmp( p_access->psz_access, "https", 5 ) )
227 p_sys->b_ssl = VLC_TRUE;
228 if( p_sys->url.i_port <= 0 )
229 p_sys->url.i_port = 443;
233 if( p_sys->url.i_port <= 0 )
234 p_sys->url.i_port = 80;
238 p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
241 psz = var_CreateGetString( p_access, "http-proxy" );
244 p_sys->b_proxy = VLC_TRUE;
245 vlc_UrlParse( &p_sys->proxy, psz, 0 );
250 char *psz_proxy = getenv( "http_proxy" );
251 if( psz_proxy && *psz_proxy )
253 p_sys->b_proxy = VLC_TRUE;
254 vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
262 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
264 msg_Warn( p_access, "invalid proxy host" );
267 if( p_sys->proxy.i_port <= 0 )
269 p_sys->proxy.i_port = 80;
273 msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
274 p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
277 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
278 p_sys->proxy.i_port );
280 if( p_sys->url.psz_username && *p_sys->url.psz_username )
282 msg_Dbg( p_access, " user='%s', pwd='%s'",
283 p_sys->url.psz_username, p_sys->url.psz_password );
286 p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
287 p_sys->b_continuous = var_CreateGetBool( p_access, "http-continuous" );
291 switch( Connect( p_access, 0 ) )
297 /* Retry with http 1.0 */
298 msg_Dbg( p_access, "switching to HTTP version 1.0" );
299 p_sys->i_version = 0;
300 p_sys->b_seekable = VLC_FALSE;
302 if( p_access->b_die || Connect( p_access, 0 ) )
310 msg_Err( p_access, "You should not be here" );
315 if( p_sys->i_code == 401 )
317 char *psz_login = NULL; char *psz_password = NULL;
319 msg_Dbg( p_access, "authentication failed" );
320 i_ret = intf_UserLoginPassword( p_access, _("HTTP authentication"),
321 _("Please enter a valid login name and a password."),
322 &psz_login, &psz_password );
323 if( i_ret == DIALOG_OK_YES )
325 msg_Dbg( p_access, "retrying with user=%s, pwd=%s",
326 psz_login, psz_password );
327 if( psz_login ) p_sys->url.psz_username = strdup( psz_login );
328 if( psz_password ) p_sys->url.psz_password = strdup( psz_password );
329 if( psz_login ) free( psz_login );
330 if( psz_password ) free( psz_password );
335 if( psz_login ) free( psz_login );
336 if( psz_password ) free( psz_password );
341 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
342 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
343 p_sys->psz_location && *p_sys->psz_location )
345 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
346 printf("redirection to %s", p_sys->psz_location );
349 /* Do not accept redirection outside of HTTP works */
350 if( strncmp( p_sys->psz_location, "http", 4 )
351 || ( ( p_sys->psz_location[4] != ':' ) /* HTTP */
352 && strncmp( p_sys->psz_location + 4, "s:", 2 ) /* HTTP/SSL */ ) )
354 msg_Err( p_access, "insecure redirection ignored" );
357 free( p_access->psz_path );
358 p_access->psz_path = strdup( p_sys->psz_location );
359 /* Clean up current Open() run */
360 vlc_UrlClean( &p_sys->url );
361 vlc_UrlClean( &p_sys->proxy );
362 free( p_sys->psz_mime );
363 free( p_sys->psz_pragma );
364 free( p_sys->psz_location );
365 free( p_sys->psz_user_agent );
367 Disconnect( p_access );
370 /* Do new Open() run with new data */
371 return OpenWithRedirectionStatus( p_this, VLC_TRUE );
376 msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
380 if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
382 if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
384 if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
385 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
386 p_access->psz_demux = strdup( "nsv" );
387 else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
388 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
389 p_access->psz_demux = strdup( "m4a" );
390 else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
391 p_access->psz_demux = strdup( "mp3" );
393 msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
394 p_access->psz_demux );
396 #if 0 /* Doesn't work really well because of the pre-buffering in
397 * shoutcast servers (the buffer content will be sent as fast as
399 p_sys->b_pace_control = VLC_FALSE;
402 else if( !p_sys->psz_mime )
405 p_access->psz_demux = strdup( "mp3" );
407 /* else probably Ogg Vorbis */
409 else if( !strcasecmp( p_access->psz_access, "unsv" ) &&
411 !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
413 /* Grrrr! detect ultravox server and force NSV demuxer */
414 p_access->psz_demux = strdup( "nsv" );
416 else if( !strcmp( p_access->psz_access, "itpc" ) )
418 p_access->psz_demux = strdup( "podcast" );
420 else if( p_sys->psz_mime &&
421 !strncasecmp( p_sys->psz_mime, "application/xspf+xml", 20 ) &&
422 ( memchr( " ;\t", p_sys->psz_mime[20], 4 ) != NULL ) )
423 p_access->psz_demux = strdup( "xspf-open" );
425 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
428 var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
433 vlc_UrlClean( &p_sys->url );
434 vlc_UrlClean( &p_sys->proxy );
435 free( p_sys->psz_mime );
436 free( p_sys->psz_pragma );
437 free( p_sys->psz_location );
438 free( p_sys->psz_user_agent );
440 Disconnect( p_access );
445 /*****************************************************************************
447 *****************************************************************************/
448 static void Close( vlc_object_t *p_this )
450 access_t *p_access = (access_t*)p_this;
451 access_sys_t *p_sys = p_access->p_sys;
453 vlc_UrlClean( &p_sys->url );
454 vlc_UrlClean( &p_sys->proxy );
456 free( p_sys->psz_mime );
457 free( p_sys->psz_pragma );
458 free( p_sys->psz_location );
460 free( p_sys->psz_icy_name );
461 free( p_sys->psz_icy_genre );
462 free( p_sys->psz_icy_title );
464 free( p_sys->psz_user_agent );
466 Disconnect( p_access );
471 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
472 free(vlc_array_item_at_index( p_sys->cookies, i ));
473 vlc_array_destroy( p_sys->cookies );
479 /*****************************************************************************
480 * Read: Read up to i_len bytes from the http connection and place in
481 * p_buffer. Return the actual number of bytes read
482 *****************************************************************************/
483 static int ReadICYMeta( access_t *p_access );
484 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
486 access_sys_t *p_sys = p_access->p_sys;
491 p_access->info.b_eof = VLC_TRUE;
495 if( p_access->info.i_size > 0 &&
496 i_len + p_access->info.i_pos > p_access->info.i_size )
498 if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
500 p_access->info.b_eof = VLC_TRUE;
505 if( p_sys->b_chunked )
507 if( p_sys->i_chunk < 0 )
509 p_access->info.b_eof = VLC_TRUE;
513 if( p_sys->i_chunk <= 0 )
515 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
516 /* read the chunk header */
519 /* fatal error - end of file */
520 msg_Dbg( p_access, "failed reading chunk-header line" );
523 p_sys->i_chunk = strtoll( psz, NULL, 16 );
526 if( p_sys->i_chunk <= 0 ) /* eof */
529 p_access->info.b_eof = VLC_TRUE;
534 if( i_len > p_sys->i_chunk )
536 i_len = p_sys->i_chunk;
540 if( p_sys->b_continuous && i_len > p_sys->i_remaining )
542 /* Only ask for the remaining length */
543 int i_new_len = p_sys->i_remaining;
546 Request( p_access, 0 );
547 i_read = Read( p_access, p_buffer, i_len );
553 if( p_sys->i_icy_meta > 0 && p_access->info.i_pos > 0 )
555 int64_t i_next = p_sys->i_icy_meta -
556 p_access->info.i_pos % p_sys->i_icy_meta;
558 if( i_next == p_sys->i_icy_meta )
560 if( ReadICYMeta( p_access ) )
562 p_access->info.b_eof = VLC_TRUE;
570 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, VLC_FALSE );
574 p_access->info.i_pos += i_read;
576 if( p_sys->b_chunked )
578 p_sys->i_chunk -= i_read;
579 if( p_sys->i_chunk <= 0 )
581 /* read the empty line */
582 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
583 if( psz ) free( psz );
587 else if( i_read == 0 )
590 * I very much doubt that this will work.
591 * If i_read == 0, the connection *IS* dead, so the only
592 * sensible thing to do is Disconnect() and then retry.
593 * Otherwise, I got recv() completely wrong. -- Courmisch
595 if( p_sys->b_continuous )
597 Request( p_access, 0 );
598 p_sys->b_continuous = VLC_FALSE;
599 i_read = Read( p_access, p_buffer, i_len );
600 p_sys->b_continuous = VLC_TRUE;
602 Disconnect( p_access );
603 if( p_sys->b_reconnect )
605 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
606 if( Connect( p_access, p_access->info.i_pos ) )
608 msg_Dbg( p_access, "reconnection failed" );
612 p_sys->b_reconnect = VLC_FALSE;
613 i_read = Read( p_access, p_buffer, i_len );
614 p_sys->b_reconnect = VLC_TRUE;
618 if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
621 if( p_sys->b_continuous )
623 p_sys->i_remaining -= i_read;
629 static int ReadICYMeta( access_t *p_access )
631 access_sys_t *p_sys = p_access->p_sys;
637 /* Read meta data length */
638 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
645 i_read = buffer << 4;
646 /* msg_Dbg( p_access, "ICY meta size=%u", i_read); */
648 psz_meta = malloc( i_read + 1 );
649 if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
650 (uint8_t *)psz_meta, i_read, VLC_TRUE ) != i_read )
653 psz_meta[i_read] = '\0'; /* Just in case */
655 /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
657 /* Now parse the meta */
658 /* Look for StreamTitle= */
659 p = strcasestr( (char *)psz_meta, "StreamTitle=" );
662 p += strlen( "StreamTitle=" );
663 if( *p == '\'' || *p == '"' )
665 char closing[] = { p[0], ';', '\0' };
666 char *psz = strstr( &p[1], closing );
668 psz = strchr( &p[1], ';' );
670 if( psz ) *psz = '\0';
674 char *psz = strchr( &p[1], ';' );
675 if( psz ) *psz = '\0';
678 if( !p_sys->psz_icy_title ||
679 strcmp( p_sys->psz_icy_title, &p[1] ) )
681 if( p_sys->psz_icy_title )
682 free( p_sys->psz_icy_title );
683 p_sys->psz_icy_title = strdup( &p[1] );
684 p_access->info.i_update |= INPUT_UPDATE_META;
686 msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
694 /*****************************************************************************
695 * Seek: close and re-open a connection at the right place
696 *****************************************************************************/
697 static int Seek( access_t *p_access, int64_t i_pos )
699 msg_Dbg( p_access, "trying to seek to "I64Fd, i_pos );
701 Disconnect( p_access );
703 if( Connect( p_access, i_pos ) )
705 msg_Err( p_access, "seek failed" );
706 p_access->info.b_eof = VLC_TRUE;
712 /*****************************************************************************
714 *****************************************************************************/
715 static int Control( access_t *p_access, int i_query, va_list args )
717 access_sys_t *p_sys = p_access->p_sys;
726 case ACCESS_CAN_SEEK:
727 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
728 *pb_bool = p_sys->b_seekable;
730 case ACCESS_CAN_FASTSEEK:
731 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
732 *pb_bool = VLC_FALSE;
734 case ACCESS_CAN_PAUSE:
735 case ACCESS_CAN_CONTROL_PACE:
736 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
738 #if 0 /* Disable for now until we have a clock synchro algo
739 * which works with something else than MPEG over UDP */
740 *pb_bool = p_sys->b_pace_control;
747 pi_int = (int*)va_arg( args, int * );
751 case ACCESS_GET_PTS_DELAY:
752 pi_64 = (int64_t*)va_arg( args, int64_t * );
753 *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
757 case ACCESS_SET_PAUSE_STATE:
760 case ACCESS_GET_META:
761 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
763 if( p_sys->psz_icy_name )
764 vlc_meta_Set( p_meta, vlc_meta_Title, p_sys->psz_icy_name );
765 if( p_sys->psz_icy_genre )
766 vlc_meta_Set( p_meta, vlc_meta_Genre, p_sys->psz_icy_genre );
767 if( p_sys->psz_icy_title )
768 vlc_meta_Set( p_meta, vlc_meta_NowPlaying, p_sys->psz_icy_title );
771 case ACCESS_GET_CONTENT_TYPE:
772 *va_arg( args, char ** ) =
773 p_sys->psz_mime ? strdup( p_sys->psz_mime ) : NULL;
776 case ACCESS_GET_TITLE_INFO:
777 case ACCESS_SET_TITLE:
778 case ACCESS_SET_SEEKPOINT:
779 case ACCESS_SET_PRIVATE_ID_STATE:
783 msg_Warn( p_access, "unimplemented query in control" );
790 /*****************************************************************************
792 *****************************************************************************/
793 static int Connect( access_t *p_access, int64_t i_tell )
795 access_sys_t *p_sys = p_access->p_sys;
796 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
799 free( p_sys->psz_location );
800 free( p_sys->psz_mime );
801 free( p_sys->psz_pragma );
803 free( p_sys->psz_icy_genre );
804 free( p_sys->psz_icy_name );
805 free( p_sys->psz_icy_title );
808 p_sys->psz_location = NULL;
809 p_sys->psz_mime = NULL;
810 p_sys->psz_pragma = NULL;
811 p_sys->b_mms = VLC_FALSE;
812 p_sys->b_chunked = VLC_FALSE;
814 p_sys->i_icy_meta = 0;
815 p_sys->psz_icy_name = NULL;
816 p_sys->psz_icy_genre = NULL;
817 p_sys->psz_icy_title = NULL;
819 p_access->info.i_size = 0;
820 p_access->info.i_pos = i_tell;
821 p_access->info.b_eof = VLC_FALSE;
824 /* Open connection */
825 p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
826 if( p_sys->fd == -1 )
828 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
832 /* Initialize TLS/SSL session */
833 if( p_sys->b_ssl == VLC_TRUE )
835 /* CONNECT to establish TLS tunnel through HTTP proxy */
839 unsigned i_status = 0;
841 if( p_sys->i_version == 0 )
843 /* CONNECT is not in HTTP/1.0 */
844 Disconnect( p_access );
848 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
849 "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
850 p_sys->url.psz_host, p_sys->url.i_port,
852 p_sys->url.psz_host, p_sys->url.i_port);
854 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
857 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
858 Disconnect( p_access );
862 sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
865 if( ( i_status / 100 ) != 2 )
867 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
868 Disconnect( p_access );
874 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
877 msg_Err( p_access, "HTTP proxy connection failed" );
878 Disconnect( p_access );
890 /* TLS/SSL handshake */
891 p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
893 if( p_sys->p_tls == NULL )
895 msg_Err( p_access, "cannot establish HTTP/TLS session" );
896 Disconnect( p_access );
899 p_sys->p_vs = &p_sys->p_tls->sock;
902 return Request( p_access, i_tell ) ? -2 : 0;
906 static int Request( access_t *p_access, int64_t i_tell )
908 access_sys_t *p_sys = p_access->p_sys;
910 v_socket_t *pvs = p_sys->p_vs;
914 if( p_sys->url.psz_path )
916 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
917 "GET http://%s:%d%s HTTP/1.%d\r\n",
918 p_sys->url.psz_host, p_sys->url.i_port,
919 p_sys->url.psz_path, p_sys->i_version );
923 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
924 "GET http://%s:%d/ HTTP/1.%d\r\n",
925 p_sys->url.psz_host, p_sys->url.i_port,
931 const char *psz_path = p_sys->url.psz_path;
932 if( !psz_path || !*psz_path )
936 if( p_sys->url.i_port != 80)
938 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
939 "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
940 psz_path, p_sys->i_version, p_sys->url.psz_host,
945 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
946 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
947 psz_path, p_sys->i_version, p_sys->url.psz_host );
951 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "User-Agent: %s\r\n",
952 p_sys->psz_user_agent );
954 if( p_sys->i_version == 1 )
956 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
957 "Range: bytes="I64Fd"-\r\n", i_tell );
964 for( i = 0; i < vlc_array_count( p_sys->cookies ); i++ )
966 const char * cookie = vlc_array_item_at_index( p_sys->cookies, i );
967 char * psz_cookie_content = cookie_get_content( cookie );
968 char * psz_cookie_domain = cookie_get_domain( cookie );
969 if( psz_cookie_content &&
970 /* Check to see if we are in the right domain */
971 ( !psz_cookie_domain || strstr( p_sys->url.psz_host, psz_cookie_domain ))
974 msg_Dbg( p_access, "Sending Cookie %s", psz_cookie_content );
975 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Cookie: %s\r\n", psz_cookie_content ) < 0 )
976 msg_Err( p_access, "failed to send Cookie" );
978 free( psz_cookie_content );
979 free( psz_cookie_domain );
984 if( p_sys->url.psz_username || p_sys->url.psz_password )
986 char buf[strlen( p_sys->url.psz_username ?: "" )
987 + strlen( p_sys->url.psz_password ?: "" ) + 2];
990 snprintf( buf, sizeof( buf ), "%s:%s", p_sys->url.psz_username ?: "",
991 p_sys->url.psz_password ?: "" );
992 b64 = vlc_b64_encode( buf );
996 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
997 "Authorization: Basic %s\r\n", b64 );
1002 /* Proxy Authentication */
1003 if( p_sys->proxy.psz_username || p_sys->proxy.psz_password )
1005 char buf[strlen( p_sys->proxy.psz_username ?: "" )
1006 + strlen( p_sys->proxy.psz_password ?: "" )];
1009 snprintf( buf, sizeof( buf ), "%s:%s", p_sys->proxy.psz_username ?: "",
1010 p_sys->proxy.psz_password ?: "" );
1011 b64 = vlc_b64_encode( buf );
1015 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
1016 "Proxy-Authorization: Basic %s\r\n", b64 );
1021 /* ICY meta data request */
1022 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
1025 if( p_sys->b_continuous )
1027 net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
1028 "Connection: Keep-Alive\r\n" );
1030 else if( p_sys->i_version == 1 )
1032 net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
1033 "Connection: Close\r\n");
1036 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "\r\n" ) < 0 )
1038 msg_Err( p_access, "failed to send request" );
1039 Disconnect( p_access );
1040 return VLC_EGENERIC;
1044 if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs ) ) == NULL )
1046 msg_Err( p_access, "failed to read answer" );
1049 if( !strncmp( psz, "HTTP/1.", 7 ) )
1051 p_sys->psz_protocol = "HTTP";
1052 p_sys->i_code = atoi( &psz[9] );
1054 else if( !strncmp( psz, "ICY", 3 ) )
1056 p_sys->psz_protocol = "ICY";
1057 p_sys->i_code = atoi( &psz[4] );
1058 p_sys->b_reconnect = VLC_TRUE;
1062 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1066 msg_Dbg( p_access, "protocol '%s' answer code %d",
1067 p_sys->psz_protocol, p_sys->i_code );
1068 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1070 p_sys->b_seekable = VLC_FALSE;
1072 if( p_sys->i_code != 206 && p_sys->i_code != 401 )
1074 p_sys->b_seekable = VLC_FALSE;
1076 /* Authentication error - We'll have to display the dialog */
1077 if( p_sys->i_code == 401 )
1081 /* Other fatal error */
1082 else if( p_sys->i_code >= 400 )
1084 msg_Err( p_access, "error: %s", psz );
1092 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs );
1097 msg_Err( p_access, "failed to read answer" );
1101 /* msg_Dbg( p_input, "Line=%s", psz ); */
1109 if( ( p = strchr( psz, ':' ) ) == NULL )
1111 msg_Err( p_access, "malformed header line: %s", psz );
1116 while( *p == ' ' ) p++;
1118 if( !strcasecmp( psz, "Content-Length" ) )
1120 if( p_sys->b_continuous )
1122 p_access->info.i_size = -1;
1123 msg_Dbg( p_access, "this frame size=%lld", atoll(p ) );
1124 p_sys->i_remaining = atoll( p );
1128 p_access->info.i_size = i_tell + atoll( p );
1129 msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
1132 else if( !strcasecmp( psz, "Location" ) )
1136 /* This does not follow RFC 2068, but yet if the url is not absolute,
1137 * handle it as everyone does. */
1140 const char *psz_http_ext = p_sys->b_ssl ? "s" : "" ;
1142 if( p_sys->url.i_port == ( p_sys->b_ssl ? 443 : 80 ) )
1144 asprintf(&psz_new_loc, "http%s://%s%s", psz_http_ext,
1145 p_sys->url.psz_host, p);
1149 asprintf(&psz_new_loc, "http%s://%s:%d%s", psz_http_ext,
1150 p_sys->url.psz_host, p_sys->url.i_port, p);
1155 psz_new_loc = strdup( p );
1158 if( p_sys->psz_location ) free( p_sys->psz_location );
1159 p_sys->psz_location = psz_new_loc;
1161 else if( !strcasecmp( psz, "Content-Type" ) )
1163 if( p_sys->psz_mime ) free( p_sys->psz_mime );
1164 p_sys->psz_mime = strdup( p );
1165 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1167 else if( !strcasecmp( psz, "Pragma" ) )
1169 if( !strcasecmp( psz, "Pragma: features" ) )
1170 p_sys->b_mms = VLC_TRUE;
1171 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
1172 p_sys->psz_pragma = strdup( p );
1173 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1175 else if( !strcasecmp( psz, "Server" ) )
1177 msg_Dbg( p_access, "Server: %s", p );
1178 if( !strncasecmp( p, "Icecast", 7 ) ||
1179 !strncasecmp( p, "Nanocaster", 10 ) )
1181 /* Remember if this is Icecast
1182 * we need to force demux in this case without breaking
1185 /* Let live 365 streams (nanocaster) piggyback on the icecast
1186 * routine. They look very similar */
1188 p_sys->b_reconnect = VLC_TRUE;
1189 p_sys->b_pace_control = VLC_FALSE;
1190 p_sys->b_icecast = VLC_TRUE;
1193 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1195 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1196 if( !strncasecmp( p, "chunked", 7 ) )
1198 p_sys->b_chunked = VLC_TRUE;
1201 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1203 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1204 p_sys->i_icy_meta = atoi( p );
1205 if( p_sys->i_icy_meta < 0 )
1206 p_sys->i_icy_meta = 0;
1208 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1210 else if( !strcasecmp( psz, "Icy-Name" ) )
1212 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
1213 p_sys->psz_icy_name = strdup( p );
1214 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1216 p_sys->b_icecast = VLC_TRUE; /* be on the safeside. set it here as well. */
1217 p_sys->b_reconnect = VLC_TRUE;
1218 p_sys->b_pace_control = VLC_FALSE;
1220 else if( !strcasecmp( psz, "Icy-Genre" ) )
1222 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
1223 p_sys->psz_icy_genre = strdup( p );
1224 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1226 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1228 msg_Dbg( p_access, "Icy-Notice: %s", p );
1230 else if( !strncasecmp( psz, "icy-", 4 ) ||
1231 !strncasecmp( psz, "ice-", 4 ) ||
1232 !strncasecmp( psz, "x-audiocast", 11 ) )
1234 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1235 } else if( !strcasecmp( psz, "Set-Cookie" ) )
1237 if( p_sys->cookies )
1239 msg_Dbg( p_access, "Accepting Cookie: %s", p );
1240 cookie_append( p_sys->cookies, strdup(p) );
1243 msg_Dbg( p_access, "We have a Cookie we won't remember: %s", p );
1251 Disconnect( p_access );
1252 return VLC_EGENERIC;
1255 /*****************************************************************************
1257 *****************************************************************************/
1258 static void Disconnect( access_t *p_access )
1260 access_sys_t *p_sys = p_access->p_sys;
1262 if( p_sys->p_tls != NULL)
1264 tls_ClientDelete( p_sys->p_tls );
1265 p_sys->p_tls = NULL;
1268 if( p_sys->fd != -1)
1270 net_Close(p_sys->fd);
1276 /*****************************************************************************
1277 * Cookies (FIXME: we may want to rewrite that using a nice structure to hold
1278 * them) (FIXME: only support the "domain=" param)
1279 *****************************************************************************/
1281 /* Get the NAME=VALUE part of the Cookie */
1282 static char * cookie_get_content( const char * cookie )
1284 char * ret = strdup( cookie );
1285 if( !ret ) return NULL;
1287 /* Look for a ';' */
1288 while( *str && *str != ';' ) str++;
1289 /* Replace it by a end-char */
1290 if( *str == ';' ) *str = 0;
1294 /* Get the domain where the cookie is stored */
1295 static char * cookie_get_domain( const char * cookie )
1297 const char * str = cookie;
1298 static const char domain[] = "domain=";
1301 /* Look for a ';' */
1304 if( !strncmp( str, domain, sizeof(domain) - 1 /* minus \0 */ ) )
1306 str += sizeof(domain) - 1 /* minus \0 */;
1307 char * ret = strdup( str );
1308 /* Now remove the next ';' if present */
1309 char * ret_iter = ret;
1310 while( *ret_iter && *ret_iter != ';' ) ret_iter++;
1311 if( *ret_iter == ';' )
1315 /* Go to next ';' field */
1316 while( *str && *str != ';' ) str++;
1317 if( *str == ';' ) str++;
1319 while( *str && *str == ' ' ) str++;
1324 /* Get NAME in the NAME=VALUE field */
1325 static char * cookie_get_name( const char * cookie )
1327 char * ret = cookie_get_content( cookie ); /* NAME=VALUE */
1328 if( !ret ) return NULL;
1330 while( *str && *str != '=' ) str++;
1335 /* Add a cookie in cookies, checking to see how it should be added */
1336 static void cookie_append( vlc_array_t * cookies, char * cookie )
1339 char * cookie_name = cookie_get_name( cookie );
1340 char * cookie_domain = cookie_get_domain( cookie );
1341 for( i = 0; i < vlc_array_count( cookies ); i++ )
1343 char * current_cookie = vlc_array_item_at_index( cookies, i );
1344 char * current_cookie_name = cookie_get_name( current_cookie );
1345 char * current_cookie_domain = cookie_get_domain( current_cookie );
1346 if(!strcmp( cookie_name, current_cookie_name ) &&
1347 !strcmp( cookie_domain, current_cookie_domain ))
1349 /* Remove previous value for this cookie */
1350 free( current_cookie );
1351 vlc_array_remove( cookies, i );
1354 free( current_cookie_name );
1355 free( current_cookie_domain );
1358 free( current_cookie_name );
1359 free( current_cookie_domain );
1361 free( cookie_name );
1362 free( cookie_domain );
1363 vlc_array_append( cookies, cookie );