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 "You can specify an HTTP proxy to use. 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 "Allows you to modify the default caching value for http streams. This " \
56 "value should be set in millisecond units." )
58 #define AGENT_TEXT N_("HTTP user agent")
59 #define AGENT_LONGTEXT N_("Allows you to modify the user agent that will be " \
60 "used for the connection.")
62 #define RECONNECT_TEXT N_("Auto re-connect")
63 #define RECONNECT_LONGTEXT N_("Will automatically attempt a re-connection " \
64 "in case it was untimely closed.")
66 #define CONTINUOUS_TEXT N_("Continuous stream")
67 #define CONTINUOUS_LONGTEXT N_("This allows you to read a file that is " \
68 "being constantly updated (for example, a JPG file on a server)." \
69 "You should not globally enable this option as it will break all other " \
70 "types of HTTP streams." )
73 set_description( _("HTTP input") );
74 set_capability( "access2", 0 );
75 set_shortname( _( "HTTP(S)" ) );
76 set_category( CAT_INPUT );
77 set_subcategory( SUBCAT_INPUT_ACCESS );
79 add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
81 add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
82 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
83 add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
84 AGENT_LONGTEXT, VLC_TRUE );
85 add_bool( "http-reconnect", 0, NULL, RECONNECT_TEXT,
86 RECONNECT_LONGTEXT, VLC_TRUE );
87 add_bool( "http-continuous", 0, NULL, CONTINUOUS_TEXT,
88 CONTINUOUS_LONGTEXT, VLC_TRUE );
89 add_suppressed_string("http-user");
90 add_suppressed_string("http-pwd");
91 add_shortcut( "http" );
92 add_shortcut( "https" );
93 add_shortcut( "unsv" );
94 set_callbacks( Open, Close );
97 /*****************************************************************************
99 *****************************************************************************/
103 tls_session_t *p_tls;
108 char *psz_user_agent;
123 vlc_bool_t b_icecast;
126 vlc_bool_t b_chunked;
136 vlc_bool_t b_seekable;
137 vlc_bool_t b_reconnect;
138 vlc_bool_t b_continuous;
139 vlc_bool_t b_pace_control;
143 static int Read( access_t *, uint8_t *, int );
144 static int Seek( access_t *, int64_t );
145 static int Control( access_t *, int, va_list );
148 static int Connect( access_t *, int64_t );
149 static int Request( access_t *p_access, int64_t i_tell );
150 static void Disconnect( access_t * );
152 /*****************************************************************************
154 *****************************************************************************/
155 static int Open( vlc_object_t *p_this )
157 access_t *p_access = (access_t*)p_this;
161 /* Set up p_access */
162 p_access->pf_read = Read;
163 p_access->pf_block = NULL;
164 p_access->pf_control = Control;
165 p_access->pf_seek = Seek;
166 p_access->info.i_update = 0;
167 p_access->info.i_size = 0;
168 p_access->info.i_pos = 0;
169 p_access->info.b_eof = VLC_FALSE;
170 p_access->info.i_title = 0;
171 p_access->info.i_seekpoint = 0;
172 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
173 memset( p_sys, 0, sizeof( access_sys_t ) );
175 p_sys->b_proxy = VLC_FALSE;
176 p_sys->i_version = 1;
177 p_sys->b_seekable = VLC_TRUE;
178 p_sys->psz_mime = NULL;
179 p_sys->psz_pragma = NULL;
180 p_sys->b_mms = VLC_FALSE;
181 p_sys->b_icecast = VLC_FALSE;
182 p_sys->psz_location = NULL;
183 p_sys->psz_user_agent = NULL;
184 p_sys->b_pace_control = VLC_TRUE;
185 p_sys->b_ssl = VLC_FALSE;
188 p_sys->i_icy_meta = 0;
189 p_sys->psz_icy_name = NULL;
190 p_sys->psz_icy_genre = NULL;
191 p_sys->psz_icy_title = NULL;
192 p_sys->i_remaining = 0;
195 /* Parse URI - remove spaces */
196 p = psz = strdup( p_access->psz_path );
197 while( (p = strchr( p, ' ' )) != NULL )
199 vlc_UrlParse( &p_sys->url, psz, 0 );
202 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
204 msg_Warn( p_access, "invalid host" );
207 if( !strncmp( p_access->psz_access, "https", 5 ) )
210 p_sys->b_ssl = VLC_TRUE;
211 if( p_sys->url.i_port <= 0 )
212 p_sys->url.i_port = 443;
216 if( p_sys->url.i_port <= 0 )
217 p_sys->url.i_port = 80;
221 p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
224 psz = var_CreateGetString( p_access, "http-proxy" );
227 p_sys->b_proxy = VLC_TRUE;
228 vlc_UrlParse( &p_sys->proxy, psz, 0 );
233 char *psz_proxy = getenv( "http_proxy" );
234 if( psz_proxy && *psz_proxy )
236 p_sys->b_proxy = VLC_TRUE;
237 vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
245 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
247 msg_Warn( p_access, "invalid proxy host" );
250 if( p_sys->proxy.i_port <= 0 )
252 p_sys->proxy.i_port = 80;
256 msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
257 p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
260 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
261 p_sys->proxy.i_port );
263 if( p_sys->url.psz_username && *p_sys->url.psz_username )
265 msg_Dbg( p_access, " user='%s', pwd='%s'",
266 p_sys->url.psz_username, p_sys->url.psz_password );
269 p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
270 p_sys->b_continuous = var_CreateGetBool( p_access, "http-continuous" );
274 if( Connect( p_access, 0 ) )
276 /* Retry with http 1.0 */
277 p_sys->i_version = 0;
279 if( p_access->b_die ||
280 Connect( p_access, 0 ) )
286 if( p_sys->i_code == 401 )
288 char *psz_login = NULL; char *psz_password = NULL;
290 msg_Dbg( p_access, "Authentication failed" );
291 i_ret = intf_UserLoginPassword( p_access, "HTTP authentication",
292 "Please enter a valid login and password.", &psz_login, &psz_password );
293 if( i_ret == DIALOG_OK_YES )
295 msg_Dbg( p_access, "retrying with user=%s, pwd=%s",
296 psz_login, psz_password );
297 if( psz_login ) p_sys->url.psz_username = strdup( psz_login );
298 if( psz_password ) p_sys->url.psz_password = strdup( psz_password );
299 if( psz_login ) free( psz_login );
300 if( psz_password ) free( psz_password );
305 if( psz_login ) free( psz_login );
306 if( psz_password ) free( psz_password );
311 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
312 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
313 p_sys->psz_location && *p_sys->psz_location )
315 playlist_t * p_playlist;
316 input_item_t *p_input_item;
318 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
320 /* Do not accept redirection outside of HTTP works */
321 if( strncmp( p_sys->psz_location, "http", 4 )
322 || ( ( p_sys->psz_location[4] != ':' ) /* HTTP */
323 && strncmp( p_sys->psz_location + 4, "s:", 2 ) /* HTTP/SSL */ ) )
325 msg_Err( p_access, "insecure redirection ignored" );
329 p_playlist = vlc_object_find( p_access, VLC_OBJECT_PLAYLIST,
333 msg_Err( p_access, "redirection failed: can't find playlist" );
338 vlc_mutex_lock( &p_playlist->object_lock );
339 p_input_item = &p_playlist->status.p_item->input;
340 vlc_mutex_lock( &p_input_item->lock );
341 free( p_input_item->psz_uri );
342 free( p_access->psz_path );
343 p_input_item->psz_uri = strdup( p_sys->psz_location );
344 p_access->psz_path = strdup( p_sys->psz_location );
345 vlc_mutex_unlock( &p_input_item->lock );
346 vlc_mutex_unlock( &p_playlist->object_lock );
347 vlc_object_release( p_playlist );
349 /* Clean up current Open() run */
350 vlc_UrlClean( &p_sys->url );
351 vlc_UrlClean( &p_sys->proxy );
352 if( p_sys->psz_mime ) free( p_sys->psz_mime );
353 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
354 if( p_sys->psz_location ) free( p_sys->psz_location );
355 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
357 Disconnect( p_access );
360 /* Do new Open() run with new data */
361 return Open( p_this );
366 msg_Dbg( p_access, "This is actually a live mms server, BAIL" );
370 if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
372 if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
374 if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
375 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
376 p_access->psz_demux = strdup( "nsv" );
377 else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
378 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
379 p_access->psz_demux = strdup( "m4a" );
380 else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
381 p_access->psz_demux = strdup( "mp3" );
383 msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
384 p_access->psz_demux );
386 #if 0 /* Doesn't work really well because of the pre-buffering in
387 * shoutcast servers (the buffer content will be sent as fast as
389 p_sys->b_pace_control = VLC_FALSE;
392 else if( !p_sys->psz_mime )
395 p_access->psz_demux = strdup( "mp3" );
397 /* else probably Ogg Vorbis */
399 else if( !strcasecmp( p_access->psz_access, "unsv" ) &&
401 !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
403 /* Grrrr! detect ultravox server and force NSV demuxer */
404 p_access->psz_demux = strdup( "nsv" );
406 else if( p_sys->psz_mime &&
407 !strcasecmp( p_sys->psz_mime, "application/xspf+xml" ) )
408 p_access->psz_demux = strdup( "xspf-open" );
410 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
413 var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
418 vlc_UrlClean( &p_sys->url );
419 vlc_UrlClean( &p_sys->proxy );
420 if( p_sys->psz_mime ) free( p_sys->psz_mime );
421 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
422 if( p_sys->psz_location ) free( p_sys->psz_location );
423 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
425 Disconnect( p_access );
430 /*****************************************************************************
432 *****************************************************************************/
433 static void Close( vlc_object_t *p_this )
435 access_t *p_access = (access_t*)p_this;
436 access_sys_t *p_sys = p_access->p_sys;
438 vlc_UrlClean( &p_sys->url );
439 vlc_UrlClean( &p_sys->proxy );
441 if( p_sys->psz_mime ) free( p_sys->psz_mime );
442 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
443 if( p_sys->psz_location ) free( p_sys->psz_location );
445 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
446 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
447 if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
449 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
451 Disconnect( p_access );
455 /*****************************************************************************
456 * Read: Read up to i_len bytes from the http connection and place in
457 * p_buffer. Return the actual number of bytes read
458 *****************************************************************************/
459 static int ReadICYMeta( access_t *p_access );
460 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
462 access_sys_t *p_sys = p_access->p_sys;
467 p_access->info.b_eof = VLC_TRUE;
471 if( p_access->info.i_size > 0 &&
472 i_len + p_access->info.i_pos > p_access->info.i_size )
474 if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
476 p_access->info.b_eof = VLC_TRUE;
481 if( p_sys->b_chunked )
483 if( p_sys->i_chunk < 0 )
485 p_access->info.b_eof = VLC_TRUE;
489 if( p_sys->i_chunk <= 0 )
491 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
492 /* read the chunk header */
495 msg_Dbg( p_access, "failed reading chunk-header line" );
498 p_sys->i_chunk = strtoll( psz, NULL, 16 );
501 if( p_sys->i_chunk <= 0 ) /* eof */
504 p_access->info.b_eof = VLC_TRUE;
509 if( i_len > p_sys->i_chunk )
511 i_len = p_sys->i_chunk;
515 if( p_sys->b_continuous && i_len > p_sys->i_remaining )
517 /* Only ask for the remaining length */
518 int i_new_len = p_sys->i_remaining;
521 Request( p_access, 0 );
522 i_read = Read( p_access, p_buffer, i_len );
528 if( p_sys->i_icy_meta > 0 && p_access->info.i_pos > 0 )
530 int64_t i_next = p_sys->i_icy_meta -
531 p_access->info.i_pos % p_sys->i_icy_meta;
533 if( i_next == p_sys->i_icy_meta )
535 if( ReadICYMeta( p_access ) )
537 p_access->info.b_eof = VLC_TRUE;
545 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, VLC_FALSE );
549 p_access->info.i_pos += i_read;
551 if( p_sys->b_chunked )
553 p_sys->i_chunk -= i_read;
554 if( p_sys->i_chunk <= 0 )
556 /* read the empty line */
557 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
558 if( psz ) free( psz );
562 else if( i_read == 0 )
565 * I very much doubt that this will work.
566 * If i_read == 0, the connection *IS* dead, so the only
567 * sensible thing to do is Disconnect() and then retry.
568 * Otherwise, I got recv() completely wrong. -- Courmisch
570 if( p_sys->b_continuous )
572 Request( p_access, 0 );
573 p_sys->b_continuous = VLC_FALSE;
574 i_read = Read( p_access, p_buffer, i_len );
575 p_sys->b_continuous = VLC_TRUE;
577 Disconnect( p_access );
578 if( p_sys->b_reconnect )
580 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
581 if( Connect( p_access, p_access->info.i_pos ) )
583 msg_Dbg( p_access, "reconnection failed" );
587 p_sys->b_reconnect = VLC_FALSE;
588 i_read = Read( p_access, p_buffer, i_len );
589 p_sys->b_reconnect = VLC_TRUE;
593 if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
596 if( p_sys->b_continuous )
598 p_sys->i_remaining -= i_read;
604 static int ReadICYMeta( access_t *p_access )
606 access_sys_t *p_sys = p_access->p_sys;
612 /* Read meta data length */
613 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
620 i_read = buffer << 4;
621 /* msg_Dbg( p_access, "ICY meta size=%u", i_read); */
623 psz_meta = malloc( i_read + 1 );
624 if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
625 (uint8_t *)psz_meta, i_read, VLC_TRUE ) != i_read )
628 psz_meta[i_read] = '\0'; /* Just in case */
630 /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
632 /* Now parse the meta */
633 /* Look for StreamTitle= */
634 p = strcasestr( (char *)psz_meta, "StreamTitle=" );
637 p += strlen( "StreamTitle=" );
638 if( *p == '\'' || *p == '"' )
640 char *psz = strchr( &p[1], p[0] );
642 psz = strchr( &p[1], ';' );
644 if( psz ) *psz = '\0';
648 char *psz = strchr( &p[1], ';' );
649 if( psz ) *psz = '\0';
652 if( !p_sys->psz_icy_title ||
653 strcmp( p_sys->psz_icy_title, &p[1] ) )
655 if( p_sys->psz_icy_title )
656 free( p_sys->psz_icy_title );
657 p_sys->psz_icy_title = strdup( &p[1] );
658 p_access->info.i_update |= INPUT_UPDATE_META;
660 msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
668 /*****************************************************************************
669 * Seek: close and re-open a connection at the right place
670 *****************************************************************************/
671 static int Seek( access_t *p_access, int64_t i_pos )
673 msg_Dbg( p_access, "trying to seek to "I64Fd, i_pos );
675 Disconnect( p_access );
677 if( Connect( p_access, i_pos ) )
679 msg_Err( p_access, "seek failed" );
680 p_access->info.b_eof = VLC_TRUE;
686 /*****************************************************************************
688 *****************************************************************************/
689 static int Control( access_t *p_access, int i_query, va_list args )
691 access_sys_t *p_sys = p_access->p_sys;
695 vlc_meta_t **pp_meta;
700 case ACCESS_CAN_SEEK:
701 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
702 *pb_bool = p_sys->b_seekable;
704 case ACCESS_CAN_FASTSEEK:
705 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
706 *pb_bool = VLC_FALSE;
708 case ACCESS_CAN_PAUSE:
709 case ACCESS_CAN_CONTROL_PACE:
710 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
712 #if 0 /* Disable for now until we have a clock synchro algo
713 * which works with something else than MPEG over UDP */
714 *pb_bool = p_sys->b_pace_control;
721 pi_int = (int*)va_arg( args, int * );
725 case ACCESS_GET_PTS_DELAY:
726 pi_64 = (int64_t*)va_arg( args, int64_t * );
727 *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
731 case ACCESS_SET_PAUSE_STATE:
734 case ACCESS_GET_META:
735 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
736 *pp_meta = vlc_meta_New();
738 if( p_sys->psz_icy_name )
739 vlc_meta_Add( *pp_meta, VLC_META_TITLE,
740 p_sys->psz_icy_name );
741 if( p_sys->psz_icy_genre )
742 vlc_meta_Add( *pp_meta, VLC_META_GENRE,
743 p_sys->psz_icy_genre );
744 if( p_sys->psz_icy_title )
745 vlc_meta_Add( *pp_meta, VLC_META_NOW_PLAYING,
746 p_sys->psz_icy_title );
749 case ACCESS_GET_TITLE_INFO:
750 case ACCESS_SET_TITLE:
751 case ACCESS_SET_SEEKPOINT:
752 case ACCESS_SET_PRIVATE_ID_STATE:
756 msg_Warn( p_access, "unimplemented query in control" );
763 /*****************************************************************************
765 *****************************************************************************/
766 static int Connect( access_t *p_access, int64_t i_tell )
768 access_sys_t *p_sys = p_access->p_sys;
769 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
772 if( p_sys->psz_location ) free( p_sys->psz_location );
773 if( p_sys->psz_mime ) free( p_sys->psz_mime );
774 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
776 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
777 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
778 if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
781 p_sys->psz_location = NULL;
782 p_sys->psz_mime = NULL;
783 p_sys->psz_pragma = NULL;
784 p_sys->b_mms = VLC_FALSE;
785 p_sys->b_chunked = VLC_FALSE;
787 p_sys->i_icy_meta = 0;
788 p_sys->psz_icy_name = NULL;
789 p_sys->psz_icy_genre = NULL;
790 p_sys->psz_icy_title = NULL;
792 p_access->info.i_size = 0;
793 p_access->info.i_pos = i_tell;
794 p_access->info.b_eof = VLC_FALSE;
797 /* Open connection */
798 p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
801 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
805 /* Initialize TLS/SSL session */
806 if( p_sys->b_ssl == VLC_TRUE )
808 /* CONNECT to establish TLS tunnel through HTTP proxy */
812 unsigned i_status = 0;
814 if( p_sys->i_version == 0 )
816 /* CONNECT is not in HTTP/1.0 */
817 Disconnect( p_access );
821 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
822 "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
823 p_sys->url.psz_host, p_sys->url.i_port,
825 p_sys->url.psz_host, p_sys->url.i_port);
827 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
830 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
831 Disconnect( p_access );
835 sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
838 if( ( i_status / 100 ) != 2 )
840 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
841 Disconnect( p_access );
847 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
850 msg_Err( p_access, "HTTP proxy connection failed" );
851 Disconnect( p_access );
863 /* TLS/SSL handshake */
864 p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
866 if( p_sys->p_tls == NULL )
868 msg_Err( p_access, "cannot establish HTTP/TLS session" );
869 Disconnect( p_access );
872 p_sys->p_vs = &p_sys->p_tls->sock;
875 return Request( p_access, i_tell );
879 static int Request( access_t *p_access, int64_t i_tell )
881 access_sys_t *p_sys = p_access->p_sys;
883 v_socket_t *pvs = p_sys->p_vs;
887 if( p_sys->url.psz_path )
889 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
890 "GET http://%s:%d%s HTTP/1.%d\r\n",
891 p_sys->url.psz_host, p_sys->url.i_port,
892 p_sys->url.psz_path, p_sys->i_version );
896 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
897 "GET http://%s:%d/ HTTP/1.%d\r\n",
898 p_sys->url.psz_host, p_sys->url.i_port,
904 char *psz_path = p_sys->url.psz_path;
905 if( !psz_path || !*psz_path )
909 if( p_sys->url.i_port != 80)
911 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
912 "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
913 psz_path, p_sys->i_version, p_sys->url.psz_host,
918 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
919 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
920 psz_path, p_sys->i_version, p_sys->url.psz_host );
924 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "User-Agent: %s\r\n",
925 p_sys->psz_user_agent );
927 if( p_sys->i_version == 1 )
929 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
930 "Range: bytes="I64Fd"-\r\n", i_tell );
934 if( p_sys->url.psz_username && *p_sys->url.psz_username )
939 asprintf( &buf, "%s:%s", p_sys->url.psz_username,
940 p_sys->url.psz_password ? p_sys->url.psz_password : "" );
942 b64 = vlc_b64_encode( buf );
945 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
946 "Authorization: Basic %s\r\n", b64 );
950 /* Proxy Authentication */
951 if( p_sys->proxy.psz_username && *p_sys->proxy.psz_username )
956 asprintf( &buf, "%s:%s", p_sys->proxy.psz_username,
957 p_sys->proxy.psz_password ? p_sys->proxy.psz_password : "" );
959 b64 = vlc_b64_encode( buf );
962 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
963 "Proxy-Authorization: Basic %s\r\n", b64 );
967 /* ICY meta data request */
968 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
971 if( p_sys->b_continuous && p_sys->i_version == 1 )
973 net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
974 "Connection: keep-alive\r\n" );
978 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
979 "Connection: Close\r\n");
980 p_sys->b_continuous = VLC_FALSE;
983 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "\r\n" ) < 0 )
985 msg_Err( p_access, "failed to send request" );
986 Disconnect( p_access );
991 if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs ) ) == NULL )
993 msg_Err( p_access, "failed to read answer" );
996 if( !strncmp( psz, "HTTP/1.", 7 ) )
998 p_sys->psz_protocol = "HTTP";
999 p_sys->i_code = atoi( &psz[9] );
1001 else if( !strncmp( psz, "ICY", 3 ) )
1003 p_sys->psz_protocol = "ICY";
1004 p_sys->i_code = atoi( &psz[4] );
1005 p_sys->b_reconnect = VLC_TRUE;
1009 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1013 msg_Dbg( p_access, "protocol '%s' answer code %d",
1014 p_sys->psz_protocol, p_sys->i_code );
1015 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1017 p_sys->b_seekable = VLC_FALSE;
1019 if( p_sys->i_code != 206 )
1021 p_sys->b_seekable = VLC_FALSE;
1023 /* Authentication error - We'll have to display the dialog */
1024 if( p_sys->i_code == 401 )
1028 /* Other fatal error */
1029 else if( p_sys->i_code >= 400 )
1031 msg_Err( p_access, "error: %s", psz );
1039 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs );
1044 msg_Err( p_access, "failed to read answer" );
1048 /* msg_Dbg( p_input, "Line=%s", psz ); */
1056 if( ( p = strchr( psz, ':' ) ) == NULL )
1058 msg_Err( p_access, "malformed header line: %s", psz );
1063 while( *p == ' ' ) p++;
1065 if( !strcasecmp( psz, "Content-Length" ) )
1067 if( p_sys->b_continuous )
1069 p_access->info.i_size = -1;
1070 msg_Dbg( p_access, "this frame size="I64Fd, atoll(p ) );
1071 p_sys->i_remaining = atoll( p );
1075 p_access->info.i_size = i_tell + atoll( p );
1076 msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
1079 else if( !strcasecmp( psz, "Location" ) )
1081 if( p_sys->psz_location ) free( p_sys->psz_location );
1082 p_sys->psz_location = strdup( p );
1084 else if( !strcasecmp( psz, "Content-Type" ) )
1086 if( p_sys->psz_mime ) free( p_sys->psz_mime );
1087 p_sys->psz_mime = strdup( p );
1088 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1090 else if( !strcasecmp( psz, "Pragma" ) )
1092 if( !strcasecmp( psz, "Pragma: features" ) )
1093 p_sys->b_mms = VLC_TRUE;
1094 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
1095 p_sys->psz_pragma = strdup( p );
1096 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1098 else if( !strcasecmp( psz, "Server" ) )
1100 msg_Dbg( p_access, "Server: %s", p );
1101 if( !strncasecmp( p, "Icecast", 7 ) ||
1102 !strncasecmp( p, "Nanocaster", 10 ) )
1104 /* Remember if this is Icecast
1105 * we need to force demux in this case without breaking
1108 /* Let live 365 streams (nanocaster) piggyback on the icecast
1109 * routine. They look very similar */
1111 p_sys->b_reconnect = VLC_TRUE;
1112 p_sys->b_pace_control = VLC_FALSE;
1113 p_sys->b_icecast = VLC_TRUE;
1116 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1118 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1119 if( !strncasecmp( p, "chunked", 7 ) )
1121 p_sys->b_chunked = VLC_TRUE;
1124 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1126 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1127 p_sys->i_icy_meta = atoi( p );
1128 if( p_sys->i_icy_meta < 0 )
1129 p_sys->i_icy_meta = 0;
1131 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1133 else if( !strcasecmp( psz, "Icy-Name" ) )
1135 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
1136 p_sys->psz_icy_name = strdup( p );
1137 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1139 p_sys->b_icecast = VLC_TRUE; /* be on the safeside. set it here as well. */
1140 p_sys->b_reconnect = VLC_TRUE;
1141 p_sys->b_pace_control = VLC_FALSE;
1143 else if( !strcasecmp( psz, "Icy-Genre" ) )
1145 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
1146 p_sys->psz_icy_genre = strdup( p );
1147 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1149 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1151 msg_Dbg( p_access, "Icy-Notice: %s", p );
1153 else if( !strncasecmp( psz, "icy-", 4 ) ||
1154 !strncasecmp( psz, "ice-", 4 ) ||
1155 !strncasecmp( psz, "x-audiocast", 11 ) )
1157 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1165 Disconnect( p_access );
1166 return VLC_EGENERIC;
1169 /*****************************************************************************
1171 *****************************************************************************/
1172 static void Disconnect( access_t *p_access )
1174 access_sys_t *p_sys = p_access->p_sys;
1176 if( p_sys->p_tls != NULL)
1178 tls_ClientDelete( p_sys->p_tls );
1179 p_sys->p_tls = NULL;
1182 if( p_sys->fd != -1)
1184 net_Close(p_sys->fd);