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/input.h>
34 #include "vlc_interaction.h"
35 #include "vlc_playlist.h"
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Open ( vlc_object_t * );
45 static void Close( vlc_object_t * );
47 #define PROXY_TEXT N_("HTTP proxy")
48 #define PROXY_LONGTEXT N_( \
49 "HTTP proxy to be usesd It must be of the form " \
50 "http://[user[:pass]@]myproxy.mydomain:myport/ ; " \
51 "if empty, the http_proxy environment variable will be tried." )
53 #define CACHING_TEXT N_("Caching value in ms")
54 #define CACHING_LONGTEXT N_( \
55 "Default caching value for HTTP streams. This " \
56 "value should be set in milliseconds." )
58 #define AGENT_TEXT N_("HTTP user agent")
59 #define AGENT_LONGTEXT N_("User agent that will be " \
60 "used for the connection.")
62 #define RECONNECT_TEXT N_("Auto re-connect")
63 #define RECONNECT_LONGTEXT N_( \
64 "Automatically try to reconnect to the stream in case of a sudden " \
67 #define CONTINUOUS_TEXT N_("Continuous stream")
68 #define CONTINUOUS_LONGTEXT N_("Read a file that is " \
69 "being constantly updated (for example, a JPG file on a server)." \
70 "You should not globally enable this option as it will break all other " \
71 "types of HTTP streams." )
74 set_description( _("HTTP input") );
75 set_capability( "access2", 0 );
76 set_shortname( _( "HTTP(S)" ) );
77 set_category( CAT_INPUT );
78 set_subcategory( SUBCAT_INPUT_ACCESS );
80 add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
82 add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
83 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
84 add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
85 AGENT_LONGTEXT, VLC_TRUE );
86 add_bool( "http-reconnect", 0, NULL, RECONNECT_TEXT,
87 RECONNECT_LONGTEXT, VLC_TRUE );
88 add_bool( "http-continuous", 0, NULL, CONTINUOUS_TEXT,
89 CONTINUOUS_LONGTEXT, VLC_TRUE );
90 add_suppressed_string("http-user");
91 add_suppressed_string("http-pwd");
92 add_shortcut( "http" );
93 add_shortcut( "https" );
94 add_shortcut( "unsv" );
95 set_callbacks( Open, Close );
98 /*****************************************************************************
100 *****************************************************************************/
104 tls_session_t *p_tls;
109 char *psz_user_agent;
124 vlc_bool_t b_icecast;
127 vlc_bool_t b_chunked;
137 vlc_bool_t b_seekable;
138 vlc_bool_t b_reconnect;
139 vlc_bool_t b_continuous;
140 vlc_bool_t b_pace_control;
144 static int Read( access_t *, uint8_t *, int );
145 static int Seek( access_t *, int64_t );
146 static int Control( access_t *, int, va_list );
149 static int Connect( access_t *, int64_t );
150 static int Request( access_t *p_access, int64_t i_tell );
151 static void Disconnect( access_t * );
153 /*****************************************************************************
155 *****************************************************************************/
156 static int Open( vlc_object_t *p_this )
158 access_t *p_access = (access_t*)p_this;
162 /* Set up p_access */
163 p_access->pf_read = Read;
164 p_access->pf_block = NULL;
165 p_access->pf_control = Control;
166 p_access->pf_seek = Seek;
167 p_access->info.i_update = 0;
168 p_access->info.i_size = 0;
169 p_access->info.i_pos = 0;
170 p_access->info.b_eof = VLC_FALSE;
171 p_access->info.i_title = 0;
172 p_access->info.i_seekpoint = 0;
173 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
174 memset( p_sys, 0, sizeof( access_sys_t ) );
176 p_sys->b_proxy = VLC_FALSE;
177 p_sys->i_version = 1;
178 p_sys->b_seekable = VLC_TRUE;
179 p_sys->psz_mime = NULL;
180 p_sys->psz_pragma = NULL;
181 p_sys->b_mms = VLC_FALSE;
182 p_sys->b_icecast = VLC_FALSE;
183 p_sys->psz_location = NULL;
184 p_sys->psz_user_agent = NULL;
185 p_sys->b_pace_control = VLC_TRUE;
186 p_sys->b_ssl = VLC_FALSE;
189 p_sys->i_icy_meta = 0;
190 p_sys->psz_icy_name = NULL;
191 p_sys->psz_icy_genre = NULL;
192 p_sys->psz_icy_title = NULL;
193 p_sys->i_remaining = 0;
196 /* Parse URI - remove spaces */
197 p = psz = strdup( p_access->psz_path );
198 while( (p = strchr( p, ' ' )) != NULL )
200 vlc_UrlParse( &p_sys->url, psz, 0 );
203 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
205 msg_Warn( p_access, "invalid host" );
208 if( !strncmp( p_access->psz_access, "https", 5 ) )
211 p_sys->b_ssl = VLC_TRUE;
212 if( p_sys->url.i_port <= 0 )
213 p_sys->url.i_port = 443;
217 if( p_sys->url.i_port <= 0 )
218 p_sys->url.i_port = 80;
222 p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
225 psz = var_CreateGetString( p_access, "http-proxy" );
228 p_sys->b_proxy = VLC_TRUE;
229 vlc_UrlParse( &p_sys->proxy, psz, 0 );
234 char *psz_proxy = getenv( "http_proxy" );
235 if( psz_proxy && *psz_proxy )
237 p_sys->b_proxy = VLC_TRUE;
238 vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
246 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
248 msg_Warn( p_access, "invalid proxy host" );
251 if( p_sys->proxy.i_port <= 0 )
253 p_sys->proxy.i_port = 80;
257 msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
258 p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
261 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
262 p_sys->proxy.i_port );
264 if( p_sys->url.psz_username && *p_sys->url.psz_username )
266 msg_Dbg( p_access, " user='%s', pwd='%s'",
267 p_sys->url.psz_username, p_sys->url.psz_password );
270 p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
271 p_sys->b_continuous = var_CreateGetBool( p_access, "http-continuous" );
275 if( Connect( p_access, 0 ) )
277 /* Retry with http 1.0 */
278 p_sys->i_version = 0;
280 if( p_access->b_die ||
281 Connect( p_access, 0 ) )
287 if( p_sys->i_code == 401 )
289 char *psz_login = NULL; char *psz_password = NULL;
291 msg_Dbg( p_access, "Authentication failed" );
292 i_ret = intf_UserLoginPassword( p_access, "HTTP authentication",
293 "Please enter a valid login and password.", &psz_login, &psz_password );
294 if( i_ret == DIALOG_OK_YES )
296 msg_Dbg( p_access, "retrying with user=%s, pwd=%s",
297 psz_login, psz_password );
298 if( psz_login ) p_sys->url.psz_username = strdup( psz_login );
299 if( psz_password ) p_sys->url.psz_password = strdup( psz_password );
300 if( psz_login ) free( psz_login );
301 if( psz_password ) free( psz_password );
306 if( psz_login ) free( psz_login );
307 if( psz_password ) free( psz_password );
312 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
313 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
314 p_sys->psz_location && *p_sys->psz_location )
316 playlist_t * p_playlist;
317 input_item_t *p_input_item;
319 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
321 /* Do not accept redirection outside of HTTP works */
322 if( strncmp( p_sys->psz_location, "http", 4 )
323 || ( ( p_sys->psz_location[4] != ':' ) /* HTTP */
324 && strncmp( p_sys->psz_location + 4, "s:", 2 ) /* HTTP/SSL */ ) )
326 msg_Err( p_access, "insecure redirection ignored" );
330 p_playlist = vlc_object_find( p_access, VLC_OBJECT_PLAYLIST,
334 msg_Err( p_access, "redirection failed: can't find playlist" );
339 vlc_mutex_lock( &p_playlist->object_lock );
340 p_input_item = &p_playlist->status.p_item->input;
341 vlc_mutex_lock( &p_input_item->lock );
342 free( p_input_item->psz_uri );
343 free( p_access->psz_path );
344 p_input_item->psz_uri = strdup( p_sys->psz_location );
345 p_access->psz_path = strdup( p_sys->psz_location );
346 vlc_mutex_unlock( &p_input_item->lock );
347 vlc_mutex_unlock( &p_playlist->object_lock );
348 vlc_object_release( p_playlist );
350 /* Clean up current Open() run */
351 vlc_UrlClean( &p_sys->url );
352 vlc_UrlClean( &p_sys->proxy );
353 if( p_sys->psz_mime ) free( p_sys->psz_mime );
354 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
355 if( p_sys->psz_location ) free( p_sys->psz_location );
356 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
358 Disconnect( p_access );
361 /* Do new Open() run with new data */
362 return Open( p_this );
367 msg_Dbg( p_access, "This is actually a live mms server, BAIL" );
371 if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
373 if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
375 if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
376 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
377 p_access->psz_demux = strdup( "nsv" );
378 else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
379 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
380 p_access->psz_demux = strdup( "m4a" );
381 else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
382 p_access->psz_demux = strdup( "mp3" );
384 msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
385 p_access->psz_demux );
387 #if 0 /* Doesn't work really well because of the pre-buffering in
388 * shoutcast servers (the buffer content will be sent as fast as
390 p_sys->b_pace_control = VLC_FALSE;
393 else if( !p_sys->psz_mime )
396 p_access->psz_demux = strdup( "mp3" );
398 /* else probably Ogg Vorbis */
400 else if( !strcasecmp( p_access->psz_access, "unsv" ) &&
402 !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
404 /* Grrrr! detect ultravox server and force NSV demuxer */
405 p_access->psz_demux = strdup( "nsv" );
407 else if( p_sys->psz_mime &&
408 !strcasecmp( p_sys->psz_mime, "application/xspf+xml" ) )
409 p_access->psz_demux = strdup( "xspf-open" );
411 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
414 var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
419 vlc_UrlClean( &p_sys->url );
420 vlc_UrlClean( &p_sys->proxy );
421 if( p_sys->psz_mime ) free( p_sys->psz_mime );
422 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
423 if( p_sys->psz_location ) free( p_sys->psz_location );
424 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
426 Disconnect( p_access );
431 /*****************************************************************************
433 *****************************************************************************/
434 static void Close( vlc_object_t *p_this )
436 access_t *p_access = (access_t*)p_this;
437 access_sys_t *p_sys = p_access->p_sys;
439 vlc_UrlClean( &p_sys->url );
440 vlc_UrlClean( &p_sys->proxy );
442 if( p_sys->psz_mime ) free( p_sys->psz_mime );
443 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
444 if( p_sys->psz_location ) free( p_sys->psz_location );
446 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
447 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
448 if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
450 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
452 Disconnect( p_access );
456 /*****************************************************************************
457 * Read: Read up to i_len bytes from the http connection and place in
458 * p_buffer. Return the actual number of bytes read
459 *****************************************************************************/
460 static int ReadICYMeta( access_t *p_access );
461 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
463 access_sys_t *p_sys = p_access->p_sys;
468 p_access->info.b_eof = VLC_TRUE;
472 if( p_access->info.i_size > 0 &&
473 i_len + p_access->info.i_pos > p_access->info.i_size )
475 if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
477 p_access->info.b_eof = VLC_TRUE;
482 if( p_sys->b_chunked )
484 if( p_sys->i_chunk < 0 )
486 p_access->info.b_eof = VLC_TRUE;
490 if( p_sys->i_chunk <= 0 )
492 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
493 /* read the chunk header */
496 msg_Dbg( p_access, "failed reading chunk-header line" );
499 p_sys->i_chunk = strtoll( psz, NULL, 16 );
502 if( p_sys->i_chunk <= 0 ) /* eof */
505 p_access->info.b_eof = VLC_TRUE;
510 if( i_len > p_sys->i_chunk )
512 i_len = p_sys->i_chunk;
516 if( p_sys->b_continuous && i_len > p_sys->i_remaining )
518 /* Only ask for the remaining length */
519 int i_new_len = p_sys->i_remaining;
522 Request( p_access, 0 );
523 i_read = Read( p_access, p_buffer, i_len );
529 if( p_sys->i_icy_meta > 0 && p_access->info.i_pos > 0 )
531 int64_t i_next = p_sys->i_icy_meta -
532 p_access->info.i_pos % p_sys->i_icy_meta;
534 if( i_next == p_sys->i_icy_meta )
536 if( ReadICYMeta( p_access ) )
538 p_access->info.b_eof = VLC_TRUE;
546 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, VLC_FALSE );
550 p_access->info.i_pos += i_read;
552 if( p_sys->b_chunked )
554 p_sys->i_chunk -= i_read;
555 if( p_sys->i_chunk <= 0 )
557 /* read the empty line */
558 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
559 if( psz ) free( psz );
563 else if( i_read == 0 )
566 * I very much doubt that this will work.
567 * If i_read == 0, the connection *IS* dead, so the only
568 * sensible thing to do is Disconnect() and then retry.
569 * Otherwise, I got recv() completely wrong. -- Courmisch
571 if( p_sys->b_continuous )
573 Request( p_access, 0 );
574 p_sys->b_continuous = VLC_FALSE;
575 i_read = Read( p_access, p_buffer, i_len );
576 p_sys->b_continuous = VLC_TRUE;
578 Disconnect( p_access );
579 if( p_sys->b_reconnect )
581 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
582 if( Connect( p_access, p_access->info.i_pos ) )
584 msg_Dbg( p_access, "reconnection failed" );
588 p_sys->b_reconnect = VLC_FALSE;
589 i_read = Read( p_access, p_buffer, i_len );
590 p_sys->b_reconnect = VLC_TRUE;
594 if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
597 if( p_sys->b_continuous )
599 p_sys->i_remaining -= i_read;
605 static int ReadICYMeta( access_t *p_access )
607 access_sys_t *p_sys = p_access->p_sys;
613 /* Read meta data length */
614 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
621 i_read = buffer << 4;
622 /* msg_Dbg( p_access, "ICY meta size=%u", i_read); */
624 psz_meta = malloc( i_read + 1 );
625 if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
626 (uint8_t *)psz_meta, i_read, VLC_TRUE ) != i_read )
629 psz_meta[i_read] = '\0'; /* Just in case */
631 /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
633 /* Now parse the meta */
634 /* Look for StreamTitle= */
635 p = strcasestr( (char *)psz_meta, "StreamTitle=" );
638 p += strlen( "StreamTitle=" );
639 if( *p == '\'' || *p == '"' )
641 char *psz = strchr( &p[1], p[0] );
643 psz = strchr( &p[1], ';' );
645 if( psz ) *psz = '\0';
649 char *psz = strchr( &p[1], ';' );
650 if( psz ) *psz = '\0';
653 if( !p_sys->psz_icy_title ||
654 strcmp( p_sys->psz_icy_title, &p[1] ) )
656 if( p_sys->psz_icy_title )
657 free( p_sys->psz_icy_title );
658 p_sys->psz_icy_title = strdup( &p[1] );
659 p_access->info.i_update |= INPUT_UPDATE_META;
661 msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
669 /*****************************************************************************
670 * Seek: close and re-open a connection at the right place
671 *****************************************************************************/
672 static int Seek( access_t *p_access, int64_t i_pos )
674 msg_Dbg( p_access, "trying to seek to "I64Fd, i_pos );
676 Disconnect( p_access );
678 if( Connect( p_access, i_pos ) )
680 msg_Err( p_access, "seek failed" );
681 p_access->info.b_eof = VLC_TRUE;
687 /*****************************************************************************
689 *****************************************************************************/
690 static int Control( access_t *p_access, int i_query, va_list args )
692 access_sys_t *p_sys = p_access->p_sys;
696 vlc_meta_t **pp_meta;
701 case ACCESS_CAN_SEEK:
702 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
703 *pb_bool = p_sys->b_seekable;
705 case ACCESS_CAN_FASTSEEK:
706 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
707 *pb_bool = VLC_FALSE;
709 case ACCESS_CAN_PAUSE:
710 case ACCESS_CAN_CONTROL_PACE:
711 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
713 #if 0 /* Disable for now until we have a clock synchro algo
714 * which works with something else than MPEG over UDP */
715 *pb_bool = p_sys->b_pace_control;
722 pi_int = (int*)va_arg( args, int * );
726 case ACCESS_GET_PTS_DELAY:
727 pi_64 = (int64_t*)va_arg( args, int64_t * );
728 *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
732 case ACCESS_SET_PAUSE_STATE:
735 case ACCESS_GET_META:
736 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
737 *pp_meta = vlc_meta_New();
739 if( p_sys->psz_icy_name )
740 vlc_meta_Add( *pp_meta, VLC_META_TITLE,
741 p_sys->psz_icy_name );
742 if( p_sys->psz_icy_genre )
743 vlc_meta_Add( *pp_meta, VLC_META_GENRE,
744 p_sys->psz_icy_genre );
745 if( p_sys->psz_icy_title )
746 vlc_meta_Add( *pp_meta, VLC_META_NOW_PLAYING,
747 p_sys->psz_icy_title );
750 case ACCESS_GET_TITLE_INFO:
751 case ACCESS_SET_TITLE:
752 case ACCESS_SET_SEEKPOINT:
753 case ACCESS_SET_PRIVATE_ID_STATE:
757 msg_Warn( p_access, "unimplemented query in control" );
764 /*****************************************************************************
766 *****************************************************************************/
767 static int Connect( access_t *p_access, int64_t i_tell )
769 access_sys_t *p_sys = p_access->p_sys;
770 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
773 if( p_sys->psz_location ) free( p_sys->psz_location );
774 if( p_sys->psz_mime ) free( p_sys->psz_mime );
775 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
777 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
778 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
779 if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
782 p_sys->psz_location = NULL;
783 p_sys->psz_mime = NULL;
784 p_sys->psz_pragma = NULL;
785 p_sys->b_mms = VLC_FALSE;
786 p_sys->b_chunked = VLC_FALSE;
788 p_sys->i_icy_meta = 0;
789 p_sys->psz_icy_name = NULL;
790 p_sys->psz_icy_genre = NULL;
791 p_sys->psz_icy_title = NULL;
793 p_access->info.i_size = 0;
794 p_access->info.i_pos = i_tell;
795 p_access->info.b_eof = VLC_FALSE;
798 /* Open connection */
799 p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
802 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
806 /* Initialize TLS/SSL session */
807 if( p_sys->b_ssl == VLC_TRUE )
809 /* CONNECT to establish TLS tunnel through HTTP proxy */
813 unsigned i_status = 0;
815 if( p_sys->i_version == 0 )
817 /* CONNECT is not in HTTP/1.0 */
818 Disconnect( p_access );
822 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
823 "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
824 p_sys->url.psz_host, p_sys->url.i_port,
826 p_sys->url.psz_host, p_sys->url.i_port);
828 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
831 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
832 Disconnect( p_access );
836 sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
839 if( ( i_status / 100 ) != 2 )
841 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
842 Disconnect( p_access );
848 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
851 msg_Err( p_access, "HTTP proxy connection failed" );
852 Disconnect( p_access );
864 /* TLS/SSL handshake */
865 p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
867 if( p_sys->p_tls == NULL )
869 msg_Err( p_access, "cannot establish HTTP/TLS session" );
870 Disconnect( p_access );
873 p_sys->p_vs = &p_sys->p_tls->sock;
876 return Request( p_access, i_tell );
880 static int Request( access_t *p_access, int64_t i_tell )
882 access_sys_t *p_sys = p_access->p_sys;
884 v_socket_t *pvs = p_sys->p_vs;
888 if( p_sys->url.psz_path )
890 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
891 "GET http://%s:%d%s HTTP/1.%d\r\n",
892 p_sys->url.psz_host, p_sys->url.i_port,
893 p_sys->url.psz_path, p_sys->i_version );
897 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
898 "GET http://%s:%d/ HTTP/1.%d\r\n",
899 p_sys->url.psz_host, p_sys->url.i_port,
905 char *psz_path = p_sys->url.psz_path;
906 if( !psz_path || !*psz_path )
910 if( p_sys->url.i_port != 80)
912 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
913 "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
914 psz_path, p_sys->i_version, p_sys->url.psz_host,
919 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
920 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
921 psz_path, p_sys->i_version, p_sys->url.psz_host );
925 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "User-Agent: %s\r\n",
926 p_sys->psz_user_agent );
928 if( p_sys->i_version == 1 )
930 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
931 "Range: bytes="I64Fd"-\r\n", i_tell );
935 if( p_sys->url.psz_username && *p_sys->url.psz_username )
940 asprintf( &buf, "%s:%s", p_sys->url.psz_username,
941 p_sys->url.psz_password ? p_sys->url.psz_password : "" );
943 b64 = vlc_b64_encode( buf );
946 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
947 "Authorization: Basic %s\r\n", b64 );
951 /* Proxy Authentication */
952 if( p_sys->proxy.psz_username && *p_sys->proxy.psz_username )
957 asprintf( &buf, "%s:%s", p_sys->proxy.psz_username,
958 p_sys->proxy.psz_password ? p_sys->proxy.psz_password : "" );
960 b64 = vlc_b64_encode( buf );
963 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
964 "Proxy-Authorization: Basic %s\r\n", b64 );
968 /* ICY meta data request */
969 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
972 if( p_sys->b_continuous && p_sys->i_version == 1 )
974 net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
975 "Connection: keep-alive\r\n" );
979 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
980 "Connection: Close\r\n");
981 p_sys->b_continuous = VLC_FALSE;
984 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "\r\n" ) < 0 )
986 msg_Err( p_access, "failed to send request" );
987 Disconnect( p_access );
992 if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs ) ) == NULL )
994 msg_Err( p_access, "failed to read answer" );
997 if( !strncmp( psz, "HTTP/1.", 7 ) )
999 p_sys->psz_protocol = "HTTP";
1000 p_sys->i_code = atoi( &psz[9] );
1002 else if( !strncmp( psz, "ICY", 3 ) )
1004 p_sys->psz_protocol = "ICY";
1005 p_sys->i_code = atoi( &psz[4] );
1006 p_sys->b_reconnect = VLC_TRUE;
1010 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1014 msg_Dbg( p_access, "protocol '%s' answer code %d",
1015 p_sys->psz_protocol, p_sys->i_code );
1016 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1018 p_sys->b_seekable = VLC_FALSE;
1020 if( p_sys->i_code != 206 )
1022 p_sys->b_seekable = VLC_FALSE;
1024 /* Authentication error - We'll have to display the dialog */
1025 if( p_sys->i_code == 401 )
1029 /* Other fatal error */
1030 else if( p_sys->i_code >= 400 )
1032 msg_Err( p_access, "error: %s", psz );
1040 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs );
1045 msg_Err( p_access, "failed to read answer" );
1049 /* msg_Dbg( p_input, "Line=%s", psz ); */
1057 if( ( p = strchr( psz, ':' ) ) == NULL )
1059 msg_Err( p_access, "malformed header line: %s", psz );
1064 while( *p == ' ' ) p++;
1066 if( !strcasecmp( psz, "Content-Length" ) )
1068 if( p_sys->b_continuous )
1070 p_access->info.i_size = -1;
1071 msg_Dbg( p_access, "this frame size="I64Fd, atoll(p ) );
1072 p_sys->i_remaining = atoll( p );
1076 p_access->info.i_size = i_tell + atoll( p );
1077 msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
1080 else if( !strcasecmp( psz, "Location" ) )
1082 if( p_sys->psz_location ) free( p_sys->psz_location );
1083 p_sys->psz_location = strdup( p );
1085 else if( !strcasecmp( psz, "Content-Type" ) )
1087 if( p_sys->psz_mime ) free( p_sys->psz_mime );
1088 p_sys->psz_mime = strdup( p );
1089 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1091 else if( !strcasecmp( psz, "Pragma" ) )
1093 if( !strcasecmp( psz, "Pragma: features" ) )
1094 p_sys->b_mms = VLC_TRUE;
1095 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
1096 p_sys->psz_pragma = strdup( p );
1097 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1099 else if( !strcasecmp( psz, "Server" ) )
1101 msg_Dbg( p_access, "Server: %s", p );
1102 if( !strncasecmp( p, "Icecast", 7 ) ||
1103 !strncasecmp( p, "Nanocaster", 10 ) )
1105 /* Remember if this is Icecast
1106 * we need to force demux in this case without breaking
1109 /* Let live 365 streams (nanocaster) piggyback on the icecast
1110 * routine. They look very similar */
1112 p_sys->b_reconnect = VLC_TRUE;
1113 p_sys->b_pace_control = VLC_FALSE;
1114 p_sys->b_icecast = VLC_TRUE;
1117 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1119 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1120 if( !strncasecmp( p, "chunked", 7 ) )
1122 p_sys->b_chunked = VLC_TRUE;
1125 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1127 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1128 p_sys->i_icy_meta = atoi( p );
1129 if( p_sys->i_icy_meta < 0 )
1130 p_sys->i_icy_meta = 0;
1132 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1134 else if( !strcasecmp( psz, "Icy-Name" ) )
1136 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
1137 p_sys->psz_icy_name = strdup( p );
1138 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1140 p_sys->b_icecast = VLC_TRUE; /* be on the safeside. set it here as well. */
1141 p_sys->b_reconnect = VLC_TRUE;
1142 p_sys->b_pace_control = VLC_FALSE;
1144 else if( !strcasecmp( psz, "Icy-Genre" ) )
1146 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
1147 p_sys->psz_icy_genre = strdup( p );
1148 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1150 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1152 msg_Dbg( p_access, "Icy-Notice: %s", p );
1154 else if( !strncasecmp( psz, "icy-", 4 ) ||
1155 !strncasecmp( psz, "ice-", 4 ) ||
1156 !strncasecmp( psz, "x-audiocast", 11 ) )
1158 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1166 Disconnect( p_access );
1167 return VLC_EGENERIC;
1170 /*****************************************************************************
1172 *****************************************************************************/
1173 static void Disconnect( access_t *p_access )
1175 access_sys_t *p_sys = p_access->p_sys;
1177 if( p_sys->p_tls != NULL)
1179 tls_ClientDelete( p_sys->p_tls );
1180 p_sys->p_tls = NULL;
1183 if( p_sys->fd != -1)
1185 net_Close(p_sys->fd);