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 "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 /// \bug missing space before you should
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." )
75 set_description( _("HTTP input") );
76 set_capability( "access2", 0 );
77 set_shortname( _( "HTTP(S)" ) );
78 set_category( CAT_INPUT );
79 set_subcategory( SUBCAT_INPUT_ACCESS );
81 add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
83 add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
84 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
85 add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
86 AGENT_LONGTEXT, VLC_TRUE );
87 add_bool( "http-reconnect", 0, NULL, RECONNECT_TEXT,
88 RECONNECT_LONGTEXT, VLC_TRUE );
89 add_bool( "http-continuous", 0, NULL, CONTINUOUS_TEXT,
90 CONTINUOUS_LONGTEXT, VLC_TRUE );
91 add_suppressed_string("http-user");
92 add_suppressed_string("http-pwd");
93 add_shortcut( "http" );
94 add_shortcut( "https" );
95 add_shortcut( "unsv" );
96 set_callbacks( Open, Close );
99 /*****************************************************************************
101 *****************************************************************************/
105 tls_session_t *p_tls;
110 char *psz_user_agent;
125 vlc_bool_t b_icecast;
128 vlc_bool_t b_chunked;
138 vlc_bool_t b_seekable;
139 vlc_bool_t b_reconnect;
140 vlc_bool_t b_continuous;
141 vlc_bool_t b_pace_control;
145 static int Read( access_t *, uint8_t *, int );
146 static int Seek( access_t *, int64_t );
147 static int Control( access_t *, int, va_list );
150 static int Connect( access_t *, int64_t );
151 static int Request( access_t *p_access, int64_t i_tell );
152 static void Disconnect( access_t * );
154 /*****************************************************************************
156 *****************************************************************************/
157 static int Open( vlc_object_t *p_this )
159 access_t *p_access = (access_t*)p_this;
163 /* Set up p_access */
164 p_access->pf_read = Read;
165 p_access->pf_block = NULL;
166 p_access->pf_control = Control;
167 p_access->pf_seek = Seek;
168 p_access->info.i_update = 0;
169 p_access->info.i_size = 0;
170 p_access->info.i_pos = 0;
171 p_access->info.b_eof = VLC_FALSE;
172 p_access->info.i_title = 0;
173 p_access->info.i_seekpoint = 0;
174 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
175 memset( p_sys, 0, sizeof( access_sys_t ) );
177 p_sys->b_proxy = VLC_FALSE;
178 p_sys->i_version = 1;
179 p_sys->b_seekable = VLC_TRUE;
180 p_sys->psz_mime = NULL;
181 p_sys->psz_pragma = NULL;
182 p_sys->b_mms = VLC_FALSE;
183 p_sys->b_icecast = VLC_FALSE;
184 p_sys->psz_location = NULL;
185 p_sys->psz_user_agent = NULL;
186 p_sys->b_pace_control = VLC_TRUE;
187 p_sys->b_ssl = VLC_FALSE;
190 p_sys->i_icy_meta = 0;
191 p_sys->psz_icy_name = NULL;
192 p_sys->psz_icy_genre = NULL;
193 p_sys->psz_icy_title = NULL;
194 p_sys->i_remaining = 0;
197 /* Parse URI - remove spaces */
198 p = psz = strdup( p_access->psz_path );
199 while( (p = strchr( p, ' ' )) != NULL )
201 vlc_UrlParse( &p_sys->url, psz, 0 );
204 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
206 msg_Warn( p_access, "invalid host" );
209 if( !strncmp( p_access->psz_access, "https", 5 ) )
212 p_sys->b_ssl = VLC_TRUE;
213 if( p_sys->url.i_port <= 0 )
214 p_sys->url.i_port = 443;
218 if( p_sys->url.i_port <= 0 )
219 p_sys->url.i_port = 80;
223 p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
226 psz = var_CreateGetString( p_access, "http-proxy" );
229 p_sys->b_proxy = VLC_TRUE;
230 vlc_UrlParse( &p_sys->proxy, psz, 0 );
235 char *psz_proxy = getenv( "http_proxy" );
236 if( psz_proxy && *psz_proxy )
238 p_sys->b_proxy = VLC_TRUE;
239 vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
247 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
249 msg_Warn( p_access, "invalid proxy host" );
252 if( p_sys->proxy.i_port <= 0 )
254 p_sys->proxy.i_port = 80;
258 msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
259 p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
262 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
263 p_sys->proxy.i_port );
265 if( p_sys->url.psz_username && *p_sys->url.psz_username )
267 msg_Dbg( p_access, " user='%s', pwd='%s'",
268 p_sys->url.psz_username, p_sys->url.psz_password );
271 p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
272 p_sys->b_continuous = var_CreateGetBool( p_access, "http-continuous" );
276 if( Connect( p_access, 0 ) )
278 /* Retry with http 1.0 */
279 p_sys->i_version = 0;
281 if( p_access->b_die ||
282 Connect( p_access, 0 ) )
288 if( p_sys->i_code == 401 )
290 char *psz_login = NULL; char *psz_password = NULL;
292 msg_Dbg( p_access, "authentication failed" );
293 i_ret = intf_UserLoginPassword( p_access, _("HTTP authentication"),
294 _("Please enter a valid login name and a password."),
295 &psz_login, &psz_password );
296 if( i_ret == DIALOG_OK_YES )
298 msg_Dbg( p_access, "retrying with user=%s, pwd=%s",
299 psz_login, psz_password );
300 if( psz_login ) p_sys->url.psz_username = strdup( psz_login );
301 if( psz_password ) p_sys->url.psz_password = strdup( psz_password );
302 if( psz_login ) free( psz_login );
303 if( psz_password ) free( psz_password );
308 if( psz_login ) free( psz_login );
309 if( psz_password ) free( psz_password );
314 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
315 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
316 p_sys->psz_location && *p_sys->psz_location )
318 playlist_t * p_playlist;
319 input_item_t *p_input_item;
321 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
323 /* Do not accept redirection outside of HTTP works */
324 if( strncmp( p_sys->psz_location, "http", 4 )
325 || ( ( p_sys->psz_location[4] != ':' ) /* HTTP */
326 && strncmp( p_sys->psz_location + 4, "s:", 2 ) /* HTTP/SSL */ ) )
328 msg_Err( p_access, "insecure redirection ignored" );
332 p_playlist = vlc_object_find( p_access, VLC_OBJECT_PLAYLIST,
336 msg_Err( p_access, "redirection failed: can't find playlist" );
341 vlc_mutex_lock( &p_playlist->object_lock );
342 p_input_item = &p_playlist->status.p_item->input;
343 vlc_mutex_lock( &p_input_item->lock );
344 free( p_input_item->psz_uri );
345 free( p_access->psz_path );
346 p_input_item->psz_uri = strdup( p_sys->psz_location );
347 p_access->psz_path = strdup( p_sys->psz_location );
348 vlc_mutex_unlock( &p_input_item->lock );
349 vlc_mutex_unlock( &p_playlist->object_lock );
350 vlc_object_release( p_playlist );
352 /* Clean up current Open() run */
353 vlc_UrlClean( &p_sys->url );
354 vlc_UrlClean( &p_sys->proxy );
355 if( p_sys->psz_mime ) free( p_sys->psz_mime );
356 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
357 if( p_sys->psz_location ) free( p_sys->psz_location );
358 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
360 Disconnect( p_access );
363 /* Do new Open() run with new data */
364 return Open( p_this );
369 msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
373 if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
375 if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
377 if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
378 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
379 p_access->psz_demux = strdup( "nsv" );
380 else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
381 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
382 p_access->psz_demux = strdup( "m4a" );
383 else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
384 p_access->psz_demux = strdup( "mp3" );
386 msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
387 p_access->psz_demux );
389 #if 0 /* Doesn't work really well because of the pre-buffering in
390 * shoutcast servers (the buffer content will be sent as fast as
392 p_sys->b_pace_control = VLC_FALSE;
395 else if( !p_sys->psz_mime )
398 p_access->psz_demux = strdup( "mp3" );
400 /* else probably Ogg Vorbis */
402 else if( !strcasecmp( p_access->psz_access, "unsv" ) &&
404 !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
406 /* Grrrr! detect ultravox server and force NSV demuxer */
407 p_access->psz_demux = strdup( "nsv" );
409 else if( p_sys->psz_mime &&
410 !strcasecmp( p_sys->psz_mime, "application/xspf+xml" ) )
411 p_access->psz_demux = strdup( "xspf-open" );
413 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
416 var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
421 vlc_UrlClean( &p_sys->url );
422 vlc_UrlClean( &p_sys->proxy );
423 if( p_sys->psz_mime ) free( p_sys->psz_mime );
424 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
425 if( p_sys->psz_location ) free( p_sys->psz_location );
426 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
428 Disconnect( p_access );
433 /*****************************************************************************
435 *****************************************************************************/
436 static void Close( vlc_object_t *p_this )
438 access_t *p_access = (access_t*)p_this;
439 access_sys_t *p_sys = p_access->p_sys;
441 vlc_UrlClean( &p_sys->url );
442 vlc_UrlClean( &p_sys->proxy );
444 if( p_sys->psz_mime ) free( p_sys->psz_mime );
445 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
446 if( p_sys->psz_location ) free( p_sys->psz_location );
448 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
449 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
450 if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
452 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
454 Disconnect( p_access );
458 /*****************************************************************************
459 * Read: Read up to i_len bytes from the http connection and place in
460 * p_buffer. Return the actual number of bytes read
461 *****************************************************************************/
462 static int ReadICYMeta( access_t *p_access );
463 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
465 access_sys_t *p_sys = p_access->p_sys;
470 p_access->info.b_eof = VLC_TRUE;
474 if( p_access->info.i_size > 0 &&
475 i_len + p_access->info.i_pos > p_access->info.i_size )
477 if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
479 p_access->info.b_eof = VLC_TRUE;
484 if( p_sys->b_chunked )
486 if( p_sys->i_chunk < 0 )
488 p_access->info.b_eof = VLC_TRUE;
492 if( p_sys->i_chunk <= 0 )
494 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
495 /* read the chunk header */
498 msg_Dbg( p_access, "failed reading chunk-header line" );
501 p_sys->i_chunk = strtoll( psz, NULL, 16 );
504 if( p_sys->i_chunk <= 0 ) /* eof */
507 p_access->info.b_eof = VLC_TRUE;
512 if( i_len > p_sys->i_chunk )
514 i_len = p_sys->i_chunk;
518 if( p_sys->b_continuous && i_len > p_sys->i_remaining )
520 /* Only ask for the remaining length */
521 int i_new_len = p_sys->i_remaining;
524 Request( p_access, 0 );
525 i_read = Read( p_access, p_buffer, i_len );
531 if( p_sys->i_icy_meta > 0 && p_access->info.i_pos > 0 )
533 int64_t i_next = p_sys->i_icy_meta -
534 p_access->info.i_pos % p_sys->i_icy_meta;
536 if( i_next == p_sys->i_icy_meta )
538 if( ReadICYMeta( p_access ) )
540 p_access->info.b_eof = VLC_TRUE;
548 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, VLC_FALSE );
552 p_access->info.i_pos += i_read;
554 if( p_sys->b_chunked )
556 p_sys->i_chunk -= i_read;
557 if( p_sys->i_chunk <= 0 )
559 /* read the empty line */
560 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
561 if( psz ) free( psz );
565 else if( i_read == 0 )
568 * I very much doubt that this will work.
569 * If i_read == 0, the connection *IS* dead, so the only
570 * sensible thing to do is Disconnect() and then retry.
571 * Otherwise, I got recv() completely wrong. -- Courmisch
573 if( p_sys->b_continuous )
575 Request( p_access, 0 );
576 p_sys->b_continuous = VLC_FALSE;
577 i_read = Read( p_access, p_buffer, i_len );
578 p_sys->b_continuous = VLC_TRUE;
580 Disconnect( p_access );
581 if( p_sys->b_reconnect )
583 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
584 if( Connect( p_access, p_access->info.i_pos ) )
586 msg_Dbg( p_access, "reconnection failed" );
590 p_sys->b_reconnect = VLC_FALSE;
591 i_read = Read( p_access, p_buffer, i_len );
592 p_sys->b_reconnect = VLC_TRUE;
596 if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
599 if( p_sys->b_continuous )
601 p_sys->i_remaining -= i_read;
607 static int ReadICYMeta( access_t *p_access )
609 access_sys_t *p_sys = p_access->p_sys;
615 /* Read meta data length */
616 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
623 i_read = buffer << 4;
624 /* msg_Dbg( p_access, "ICY meta size=%u", i_read); */
626 psz_meta = malloc( i_read + 1 );
627 if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
628 (uint8_t *)psz_meta, i_read, VLC_TRUE ) != i_read )
631 psz_meta[i_read] = '\0'; /* Just in case */
633 /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
635 /* Now parse the meta */
636 /* Look for StreamTitle= */
637 p = strcasestr( (char *)psz_meta, "StreamTitle=" );
640 p += strlen( "StreamTitle=" );
641 if( *p == '\'' || *p == '"' )
643 char *psz = strchr( &p[1], p[0] );
645 psz = strchr( &p[1], ';' );
647 if( psz ) *psz = '\0';
651 char *psz = strchr( &p[1], ';' );
652 if( psz ) *psz = '\0';
655 if( !p_sys->psz_icy_title ||
656 strcmp( p_sys->psz_icy_title, &p[1] ) )
658 if( p_sys->psz_icy_title )
659 free( p_sys->psz_icy_title );
660 p_sys->psz_icy_title = strdup( &p[1] );
661 p_access->info.i_update |= INPUT_UPDATE_META;
663 msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
671 /*****************************************************************************
672 * Seek: close and re-open a connection at the right place
673 *****************************************************************************/
674 static int Seek( access_t *p_access, int64_t i_pos )
676 msg_Dbg( p_access, "trying to seek to "I64Fd, i_pos );
678 Disconnect( p_access );
680 if( Connect( p_access, i_pos ) )
682 msg_Err( p_access, "seek failed" );
683 p_access->info.b_eof = VLC_TRUE;
689 /*****************************************************************************
691 *****************************************************************************/
692 static int Control( access_t *p_access, int i_query, va_list args )
694 access_sys_t *p_sys = p_access->p_sys;
698 vlc_meta_t **pp_meta;
703 case ACCESS_CAN_SEEK:
704 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
705 *pb_bool = p_sys->b_seekable;
707 case ACCESS_CAN_FASTSEEK:
708 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
709 *pb_bool = VLC_FALSE;
711 case ACCESS_CAN_PAUSE:
712 case ACCESS_CAN_CONTROL_PACE:
713 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
715 #if 0 /* Disable for now until we have a clock synchro algo
716 * which works with something else than MPEG over UDP */
717 *pb_bool = p_sys->b_pace_control;
724 pi_int = (int*)va_arg( args, int * );
728 case ACCESS_GET_PTS_DELAY:
729 pi_64 = (int64_t*)va_arg( args, int64_t * );
730 *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
734 case ACCESS_SET_PAUSE_STATE:
737 case ACCESS_GET_META:
738 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
739 *pp_meta = vlc_meta_New();
741 if( p_sys->psz_icy_name )
742 vlc_meta_Add( *pp_meta, VLC_META_TITLE,
743 p_sys->psz_icy_name );
744 if( p_sys->psz_icy_genre )
745 vlc_meta_Add( *pp_meta, VLC_META_GENRE,
746 p_sys->psz_icy_genre );
747 if( p_sys->psz_icy_title )
748 vlc_meta_Add( *pp_meta, VLC_META_NOW_PLAYING,
749 p_sys->psz_icy_title );
752 case ACCESS_GET_TITLE_INFO:
753 case ACCESS_SET_TITLE:
754 case ACCESS_SET_SEEKPOINT:
755 case ACCESS_SET_PRIVATE_ID_STATE:
759 msg_Warn( p_access, "unimplemented query in control" );
766 /*****************************************************************************
768 *****************************************************************************/
769 static int Connect( access_t *p_access, int64_t i_tell )
771 access_sys_t *p_sys = p_access->p_sys;
772 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
775 if( p_sys->psz_location ) free( p_sys->psz_location );
776 if( p_sys->psz_mime ) free( p_sys->psz_mime );
777 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
779 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
780 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
781 if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
784 p_sys->psz_location = NULL;
785 p_sys->psz_mime = NULL;
786 p_sys->psz_pragma = NULL;
787 p_sys->b_mms = VLC_FALSE;
788 p_sys->b_chunked = VLC_FALSE;
790 p_sys->i_icy_meta = 0;
791 p_sys->psz_icy_name = NULL;
792 p_sys->psz_icy_genre = NULL;
793 p_sys->psz_icy_title = NULL;
795 p_access->info.i_size = 0;
796 p_access->info.i_pos = i_tell;
797 p_access->info.b_eof = VLC_FALSE;
800 /* Open connection */
801 p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
804 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
808 /* Initialize TLS/SSL session */
809 if( p_sys->b_ssl == VLC_TRUE )
811 /* CONNECT to establish TLS tunnel through HTTP proxy */
815 unsigned i_status = 0;
817 if( p_sys->i_version == 0 )
819 /* CONNECT is not in HTTP/1.0 */
820 Disconnect( p_access );
824 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
825 "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
826 p_sys->url.psz_host, p_sys->url.i_port,
828 p_sys->url.psz_host, p_sys->url.i_port);
830 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
833 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
834 Disconnect( p_access );
838 sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
841 if( ( i_status / 100 ) != 2 )
843 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
844 Disconnect( p_access );
850 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
853 msg_Err( p_access, "HTTP proxy connection failed" );
854 Disconnect( p_access );
866 /* TLS/SSL handshake */
867 p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
869 if( p_sys->p_tls == NULL )
871 msg_Err( p_access, "cannot establish HTTP/TLS session" );
872 Disconnect( p_access );
875 p_sys->p_vs = &p_sys->p_tls->sock;
878 return Request( p_access, i_tell );
882 static int Request( access_t *p_access, int64_t i_tell )
884 access_sys_t *p_sys = p_access->p_sys;
886 v_socket_t *pvs = p_sys->p_vs;
890 if( p_sys->url.psz_path )
892 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
893 "GET http://%s:%d%s HTTP/1.%d\r\n",
894 p_sys->url.psz_host, p_sys->url.i_port,
895 p_sys->url.psz_path, p_sys->i_version );
899 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
900 "GET http://%s:%d/ HTTP/1.%d\r\n",
901 p_sys->url.psz_host, p_sys->url.i_port,
907 char *psz_path = p_sys->url.psz_path;
908 if( !psz_path || !*psz_path )
912 if( p_sys->url.i_port != 80)
914 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
915 "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
916 psz_path, p_sys->i_version, p_sys->url.psz_host,
921 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
922 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
923 psz_path, p_sys->i_version, p_sys->url.psz_host );
927 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "User-Agent: %s\r\n",
928 p_sys->psz_user_agent );
930 if( p_sys->i_version == 1 )
932 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
933 "Range: bytes="I64Fd"-\r\n", i_tell );
937 if( p_sys->url.psz_username && *p_sys->url.psz_username )
942 asprintf( &buf, "%s:%s", p_sys->url.psz_username,
943 p_sys->url.psz_password ? p_sys->url.psz_password : "" );
945 b64 = vlc_b64_encode( buf );
948 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
949 "Authorization: Basic %s\r\n", b64 );
953 /* Proxy Authentication */
954 if( p_sys->proxy.psz_username && *p_sys->proxy.psz_username )
959 asprintf( &buf, "%s:%s", p_sys->proxy.psz_username,
960 p_sys->proxy.psz_password ? p_sys->proxy.psz_password : "" );
962 b64 = vlc_b64_encode( buf );
965 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
966 "Proxy-Authorization: Basic %s\r\n", b64 );
970 /* ICY meta data request */
971 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
974 if( p_sys->b_continuous && p_sys->i_version == 1 )
976 net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
977 "Connection: keep-alive\r\n" );
981 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
982 "Connection: Close\r\n");
983 p_sys->b_continuous = VLC_FALSE;
986 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "\r\n" ) < 0 )
988 msg_Err( p_access, "failed to send request" );
989 Disconnect( p_access );
994 if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs ) ) == NULL )
996 msg_Err( p_access, "failed to read answer" );
999 if( !strncmp( psz, "HTTP/1.", 7 ) )
1001 p_sys->psz_protocol = "HTTP";
1002 p_sys->i_code = atoi( &psz[9] );
1004 else if( !strncmp( psz, "ICY", 3 ) )
1006 p_sys->psz_protocol = "ICY";
1007 p_sys->i_code = atoi( &psz[4] );
1008 p_sys->b_reconnect = VLC_TRUE;
1012 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1016 msg_Dbg( p_access, "protocol '%s' answer code %d",
1017 p_sys->psz_protocol, p_sys->i_code );
1018 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1020 p_sys->b_seekable = VLC_FALSE;
1022 if( p_sys->i_code != 206 )
1024 p_sys->b_seekable = VLC_FALSE;
1026 /* Authentication error - We'll have to display the dialog */
1027 if( p_sys->i_code == 401 )
1031 /* Other fatal error */
1032 else if( p_sys->i_code >= 400 )
1034 msg_Err( p_access, "error: %s", psz );
1042 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs );
1047 msg_Err( p_access, "failed to read answer" );
1051 /* msg_Dbg( p_input, "Line=%s", psz ); */
1059 if( ( p = strchr( psz, ':' ) ) == NULL )
1061 msg_Err( p_access, "malformed header line: %s", psz );
1066 while( *p == ' ' ) p++;
1068 if( !strcasecmp( psz, "Content-Length" ) )
1070 if( p_sys->b_continuous )
1072 p_access->info.i_size = -1;
1073 msg_Dbg( p_access, "this frame size="I64Fd, atoll(p ) );
1074 p_sys->i_remaining = atoll( p );
1078 p_access->info.i_size = i_tell + atoll( p );
1079 msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
1082 else if( !strcasecmp( psz, "Location" ) )
1084 if( p_sys->psz_location ) free( p_sys->psz_location );
1085 p_sys->psz_location = strdup( p );
1087 else if( !strcasecmp( psz, "Content-Type" ) )
1089 if( p_sys->psz_mime ) free( p_sys->psz_mime );
1090 p_sys->psz_mime = strdup( p );
1091 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1093 else if( !strcasecmp( psz, "Pragma" ) )
1095 if( !strcasecmp( psz, "Pragma: features" ) )
1096 p_sys->b_mms = VLC_TRUE;
1097 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
1098 p_sys->psz_pragma = strdup( p );
1099 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1101 else if( !strcasecmp( psz, "Server" ) )
1103 msg_Dbg( p_access, "Server: %s", p );
1104 if( !strncasecmp( p, "Icecast", 7 ) ||
1105 !strncasecmp( p, "Nanocaster", 10 ) )
1107 /* Remember if this is Icecast
1108 * we need to force demux in this case without breaking
1111 /* Let live 365 streams (nanocaster) piggyback on the icecast
1112 * routine. They look very similar */
1114 p_sys->b_reconnect = VLC_TRUE;
1115 p_sys->b_pace_control = VLC_FALSE;
1116 p_sys->b_icecast = VLC_TRUE;
1119 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1121 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1122 if( !strncasecmp( p, "chunked", 7 ) )
1124 p_sys->b_chunked = VLC_TRUE;
1127 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1129 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1130 p_sys->i_icy_meta = atoi( p );
1131 if( p_sys->i_icy_meta < 0 )
1132 p_sys->i_icy_meta = 0;
1134 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1136 else if( !strcasecmp( psz, "Icy-Name" ) )
1138 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
1139 p_sys->psz_icy_name = strdup( p );
1140 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1142 p_sys->b_icecast = VLC_TRUE; /* be on the safeside. set it here as well. */
1143 p_sys->b_reconnect = VLC_TRUE;
1144 p_sys->b_pace_control = VLC_FALSE;
1146 else if( !strcasecmp( psz, "Icy-Genre" ) )
1148 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
1149 p_sys->psz_icy_genre = strdup( p );
1150 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1152 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1154 msg_Dbg( p_access, "Icy-Notice: %s", p );
1156 else if( !strncasecmp( psz, "icy-", 4 ) ||
1157 !strncasecmp( psz, "ice-", 4 ) ||
1158 !strncasecmp( psz, "x-audiocast", 11 ) )
1160 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1168 Disconnect( p_access );
1169 return VLC_EGENERIC;
1172 /*****************************************************************************
1174 *****************************************************************************/
1175 static void Disconnect( access_t *p_access )
1177 access_sys_t *p_sys = p_access->p_sys;
1179 if( p_sys->p_tls != NULL)
1181 tls_ClientDelete( p_sys->p_tls );
1182 p_sys->p_tls = NULL;
1185 if( p_sys->fd != -1)
1187 net_Close(p_sys->fd);