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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
32 #include <vlc/input.h>
34 #include "vlc_interaction.h"
35 #include "vlc_playlist.h"
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Open ( vlc_object_t * );
44 static void Close( vlc_object_t * );
46 #define PROXY_TEXT N_("HTTP proxy")
47 #define PROXY_LONGTEXT N_( \
48 "You can specify an HTTP proxy to use. It must be of the form " \
49 "http://[user[:pass]@]myproxy.mydomain:myport/ ; " \
50 "if empty, the http_proxy environment variable will be tried." )
52 #define CACHING_TEXT N_("Caching value in ms")
53 #define CACHING_LONGTEXT N_( \
54 "Allows you to modify the default caching value for http streams. This " \
55 "value should be set in millisecond units." )
57 #define AGENT_TEXT N_("HTTP user agent")
58 #define AGENT_LONGTEXT N_("Allows you to modify the user agent that will be " \
59 "used for the connection.")
61 #define RECONNECT_TEXT N_("Auto re-connect")
62 #define RECONNECT_LONGTEXT N_("Will automatically attempt a re-connection " \
63 "in case it was untimely closed.")
65 #define CONTINUOUS_TEXT N_("Continuous stream")
66 #define CONTINUOUS_LONGTEXT N_("This allows you to read a file that is " \
67 "being constantly updated (for example, a JPG file on a server)." \
68 "You should not globally enable this option as it will break all other " \
69 "types of HTTP streams." )
72 set_description( _("HTTP input") );
73 set_capability( "access2", 0 );
74 set_shortname( _( "HTTP(S)" ) );
75 set_category( CAT_INPUT );
76 set_subcategory( SUBCAT_INPUT_ACCESS );
78 add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
80 add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
81 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
82 add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
83 AGENT_LONGTEXT, VLC_TRUE );
84 add_bool( "http-reconnect", 0, NULL, RECONNECT_TEXT,
85 RECONNECT_LONGTEXT, VLC_TRUE );
86 add_bool( "http-continuous", 0, NULL, CONTINUOUS_TEXT,
87 CONTINUOUS_LONGTEXT, VLC_TRUE );
88 add_suppressed_string("http-user");
89 add_suppressed_string("http-pwd");
90 add_shortcut( "http" );
91 add_shortcut( "https" );
92 add_shortcut( "unsv" );
93 set_callbacks( Open, Close );
96 /*****************************************************************************
98 *****************************************************************************/
102 tls_session_t *p_tls;
107 char *psz_user_agent;
122 vlc_bool_t b_icecast;
125 vlc_bool_t b_chunked;
135 vlc_bool_t b_seekable;
136 vlc_bool_t b_reconnect;
137 vlc_bool_t b_continuous;
138 vlc_bool_t b_pace_control;
142 static int Read( access_t *, uint8_t *, int );
143 static int Seek( access_t *, int64_t );
144 static int Control( access_t *, int, va_list );
147 static int Connect( access_t *, int64_t );
148 static int Request( access_t *p_access, int64_t i_tell );
149 static void Disconnect( access_t * );
151 /*****************************************************************************
153 *****************************************************************************/
154 static int Open( vlc_object_t *p_this )
156 access_t *p_access = (access_t*)p_this;
160 /* Set up p_access */
161 p_access->pf_read = Read;
162 p_access->pf_block = NULL;
163 p_access->pf_control = Control;
164 p_access->pf_seek = Seek;
165 p_access->info.i_update = 0;
166 p_access->info.i_size = 0;
167 p_access->info.i_pos = 0;
168 p_access->info.b_eof = VLC_FALSE;
169 p_access->info.i_title = 0;
170 p_access->info.i_seekpoint = 0;
171 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
172 memset( p_sys, 0, sizeof( access_sys_t ) );
174 p_sys->b_proxy = VLC_FALSE;
175 p_sys->i_version = 1;
176 p_sys->b_seekable = VLC_TRUE;
177 p_sys->psz_mime = NULL;
178 p_sys->psz_pragma = NULL;
179 p_sys->b_mms = VLC_FALSE;
180 p_sys->b_icecast = VLC_FALSE;
181 p_sys->psz_location = NULL;
182 p_sys->psz_user_agent = NULL;
183 p_sys->b_pace_control = VLC_TRUE;
184 p_sys->b_ssl = VLC_FALSE;
187 p_sys->i_icy_meta = 0;
188 p_sys->psz_icy_name = NULL;
189 p_sys->psz_icy_genre = NULL;
190 p_sys->psz_icy_title = NULL;
191 p_sys->i_remaining = 0;
194 /* Parse URI - remove spaces */
195 p = psz = strdup( p_access->psz_path );
196 while( (p = strchr( p, ' ' )) != NULL )
198 vlc_UrlParse( &p_sys->url, psz, 0 );
201 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
203 msg_Warn( p_access, "invalid host" );
206 if( !strncmp( p_access->psz_access, "https", 5 ) )
209 p_sys->b_ssl = VLC_TRUE;
210 if( p_sys->url.i_port <= 0 )
211 p_sys->url.i_port = 443;
215 if( p_sys->url.i_port <= 0 )
216 p_sys->url.i_port = 80;
220 p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
223 psz = var_CreateGetString( p_access, "http-proxy" );
226 p_sys->b_proxy = VLC_TRUE;
227 vlc_UrlParse( &p_sys->proxy, psz, 0 );
232 char *psz_proxy = getenv( "http_proxy" );
233 if( psz_proxy && *psz_proxy )
235 p_sys->b_proxy = VLC_TRUE;
236 vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
244 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
246 msg_Warn( p_access, "invalid proxy host" );
249 if( p_sys->proxy.i_port <= 0 )
251 p_sys->proxy.i_port = 80;
255 msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
256 p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
259 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
260 p_sys->proxy.i_port );
262 if( p_sys->url.psz_username && *p_sys->url.psz_username )
264 msg_Dbg( p_access, " user='%s', pwd='%s'",
265 p_sys->url.psz_username, p_sys->url.psz_password );
268 p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
269 p_sys->b_continuous = var_CreateGetBool( p_access, "http-continuous" );
273 if( Connect( p_access, 0 ) )
275 /* Retry with http 1.0 */
276 p_sys->i_version = 0;
278 if( p_access->b_die ||
279 Connect( p_access, 0 ) )
285 if( p_sys->i_code == 401 )
287 char *psz_login = NULL; char *psz_password = NULL;
289 msg_Dbg( p_access, "Authentication failed" );
290 i_ret = intf_UserLoginPassword( p_access, "HTTP authentication",
291 "Please enter a valid login and password.", &psz_login, &psz_password );
292 if( i_ret == DIALOG_OK_YES )
294 msg_Dbg( p_access, "retrying with user=%s, pwd=%s",
295 psz_login, psz_password );
296 if( psz_login ) p_sys->url.psz_username = strdup( psz_login );
297 if( psz_password ) p_sys->url.psz_password = strdup( psz_password );
298 if( psz_login ) free( psz_login );
299 if( psz_password ) free( psz_password );
304 if( psz_login ) free( psz_login );
305 if( psz_password ) free( psz_password );
310 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
311 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
312 p_sys->psz_location && *p_sys->psz_location )
314 playlist_t * p_playlist;
315 input_item_t *p_input_item;
317 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
319 /* Do not accept redirection outside of HTTP works */
320 if( strncmp( p_sys->psz_location, "http", 4 )
321 || ( ( p_sys->psz_location[4] != ':' ) /* HTTP */
322 && strncmp( p_sys->psz_location + 4, "s:", 2 ) /* HTTP/SSL */ ) )
324 msg_Err( p_access, "insecure redirection ignored" );
328 p_playlist = vlc_object_find( p_access, VLC_OBJECT_PLAYLIST,
332 msg_Err( p_access, "redirection failed: can't find playlist" );
337 vlc_mutex_lock( &p_playlist->object_lock );
338 p_input_item = &p_playlist->status.p_item->input;
339 vlc_mutex_lock( &p_input_item->lock );
340 free( p_input_item->psz_uri );
341 free( p_access->psz_path );
342 p_input_item->psz_uri = strdup( p_sys->psz_location );
343 p_access->psz_path = strdup( p_sys->psz_location );
344 vlc_mutex_unlock( &p_input_item->lock );
345 vlc_mutex_unlock( &p_playlist->object_lock );
346 vlc_object_release( p_playlist );
348 /* Clean up current Open() run */
349 vlc_UrlClean( &p_sys->url );
350 vlc_UrlClean( &p_sys->proxy );
351 if( p_sys->psz_mime ) free( p_sys->psz_mime );
352 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
353 if( p_sys->psz_location ) free( p_sys->psz_location );
354 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
356 Disconnect( p_access );
359 /* Do new Open() run with new data */
360 return Open( p_this );
365 msg_Dbg( p_access, "This is actually a live mms server, BAIL" );
369 if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
371 if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
373 if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
374 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
375 p_access->psz_demux = strdup( "nsv" );
376 else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
377 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
378 p_access->psz_demux = strdup( "m4a" );
379 else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
380 p_access->psz_demux = strdup( "mp3" );
382 msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
383 p_access->psz_demux );
385 #if 0 /* Doesn't work really well because of the pre-buffering in
386 * shoutcast servers (the buffer content will be sent as fast as
388 p_sys->b_pace_control = VLC_FALSE;
391 else if( !p_sys->psz_mime )
394 p_access->psz_demux = strdup( "mp3" );
396 /* else probably Ogg Vorbis */
398 else if( !strcasecmp( p_access->psz_access, "unsv" ) &&
400 !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
402 /* Grrrr! detect ultravox server and force NSV demuxer */
403 p_access->psz_demux = strdup( "nsv" );
406 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
409 var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
414 vlc_UrlClean( &p_sys->url );
415 vlc_UrlClean( &p_sys->proxy );
416 if( p_sys->psz_mime ) free( p_sys->psz_mime );
417 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
418 if( p_sys->psz_location ) free( p_sys->psz_location );
419 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
421 Disconnect( p_access );
426 /*****************************************************************************
428 *****************************************************************************/
429 static void Close( vlc_object_t *p_this )
431 access_t *p_access = (access_t*)p_this;
432 access_sys_t *p_sys = p_access->p_sys;
434 vlc_UrlClean( &p_sys->url );
435 vlc_UrlClean( &p_sys->proxy );
437 if( p_sys->psz_mime ) free( p_sys->psz_mime );
438 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
439 if( p_sys->psz_location ) free( p_sys->psz_location );
441 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
442 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
443 if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
445 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
447 Disconnect( p_access );
451 /*****************************************************************************
452 * Read: Read up to i_len bytes from the http connection and place in
453 * p_buffer. Return the actual number of bytes read
454 *****************************************************************************/
455 static int ReadICYMeta( access_t *p_access );
456 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
458 access_sys_t *p_sys = p_access->p_sys;
463 p_access->info.b_eof = VLC_TRUE;
467 if( p_access->info.i_size > 0 &&
468 i_len + p_access->info.i_pos > p_access->info.i_size )
470 if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
472 p_access->info.b_eof = VLC_TRUE;
477 if( p_sys->b_chunked )
479 if( p_sys->i_chunk < 0 )
481 p_access->info.b_eof = VLC_TRUE;
485 if( p_sys->i_chunk <= 0 )
487 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
488 /* read the chunk header */
491 msg_Dbg( p_access, "failed reading chunk-header line" );
494 p_sys->i_chunk = strtoll( psz, NULL, 16 );
497 if( p_sys->i_chunk <= 0 ) /* eof */
500 p_access->info.b_eof = VLC_TRUE;
505 if( i_len > p_sys->i_chunk )
507 i_len = p_sys->i_chunk;
511 if( p_sys->b_continuous && i_len > p_sys->i_remaining )
513 /* Only ask for the remaining length */
514 int i_new_len = p_sys->i_remaining;
517 Request( p_access, 0 );
518 i_read = Read( p_access, p_buffer, i_len );
524 if( p_sys->i_icy_meta > 0 && p_access->info.i_pos > 0 )
526 int64_t i_next = p_sys->i_icy_meta -
527 p_access->info.i_pos % p_sys->i_icy_meta;
529 if( i_next == p_sys->i_icy_meta )
531 if( ReadICYMeta( p_access ) )
533 p_access->info.b_eof = VLC_TRUE;
541 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, VLC_FALSE );
545 p_access->info.i_pos += i_read;
547 if( p_sys->b_chunked )
549 p_sys->i_chunk -= i_read;
550 if( p_sys->i_chunk <= 0 )
552 /* read the empty line */
553 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
554 if( psz ) free( psz );
558 else if( i_read == 0 )
561 * I very much doubt that this will work.
562 * If i_read == 0, the connection *IS* dead, so the only
563 * sensible thing to do is Disconnect() and then retry.
564 * Otherwise, I got recv() completely wrong. -- Courmisch
566 if( p_sys->b_continuous )
568 Request( p_access, 0 );
569 p_sys->b_continuous = VLC_FALSE;
570 i_read = Read( p_access, p_buffer, i_len );
571 p_sys->b_continuous = VLC_TRUE;
573 Disconnect( p_access );
574 if( p_sys->b_reconnect )
576 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
577 if( Connect( p_access, p_access->info.i_pos ) )
579 msg_Dbg( p_access, "reconnection failed" );
583 p_sys->b_reconnect = VLC_FALSE;
584 i_read = Read( p_access, p_buffer, i_len );
585 p_sys->b_reconnect = VLC_TRUE;
589 if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
592 if( p_sys->b_continuous )
594 p_sys->i_remaining -= i_read;
600 static int ReadICYMeta( access_t *p_access )
602 access_sys_t *p_sys = p_access->p_sys;
608 /* Read meta data length */
609 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
616 i_read = buffer << 4;
617 /* msg_Dbg( p_access, "ICY meta size=%u", i_read); */
619 psz_meta = malloc( i_read + 1 );
620 if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
621 (uint8_t *)psz_meta, i_read, VLC_TRUE ) != i_read )
624 psz_meta[i_read] = '\0'; /* Just in case */
626 /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
628 /* Now parse the meta */
629 /* Look for StreamTitle= */
630 p = strcasestr( (char *)psz_meta, "StreamTitle=" );
633 p += strlen( "StreamTitle=" );
634 if( *p == '\'' || *p == '"' )
636 char *psz = strchr( &p[1], p[0] );
638 psz = strchr( &p[1], ';' );
640 if( psz ) *psz = '\0';
644 char *psz = strchr( &p[1], ';' );
645 if( psz ) *psz = '\0';
648 if( !p_sys->psz_icy_title ||
649 strcmp( p_sys->psz_icy_title, &p[1] ) )
651 if( p_sys->psz_icy_title )
652 free( p_sys->psz_icy_title );
653 p_sys->psz_icy_title = strdup( &p[1] );
654 p_access->info.i_update |= INPUT_UPDATE_META;
656 msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
664 /*****************************************************************************
665 * Seek: close and re-open a connection at the right place
666 *****************************************************************************/
667 static int Seek( access_t *p_access, int64_t i_pos )
669 msg_Dbg( p_access, "trying to seek to "I64Fd, i_pos );
671 Disconnect( p_access );
673 if( Connect( p_access, i_pos ) )
675 msg_Err( p_access, "seek failed" );
676 p_access->info.b_eof = VLC_TRUE;
682 /*****************************************************************************
684 *****************************************************************************/
685 static int Control( access_t *p_access, int i_query, va_list args )
687 access_sys_t *p_sys = p_access->p_sys;
691 vlc_meta_t **pp_meta;
696 case ACCESS_CAN_SEEK:
697 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
698 *pb_bool = p_sys->b_seekable;
700 case ACCESS_CAN_FASTSEEK:
701 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
702 *pb_bool = VLC_FALSE;
704 case ACCESS_CAN_PAUSE:
705 case ACCESS_CAN_CONTROL_PACE:
706 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
708 #if 0 /* Disable for now until we have a clock synchro algo
709 * which works with something else than MPEG over UDP */
710 *pb_bool = p_sys->b_pace_control;
717 pi_int = (int*)va_arg( args, int * );
721 case ACCESS_GET_PTS_DELAY:
722 pi_64 = (int64_t*)va_arg( args, int64_t * );
723 *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
727 case ACCESS_SET_PAUSE_STATE:
730 case ACCESS_GET_META:
731 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
732 *pp_meta = vlc_meta_New();
734 if( p_sys->psz_icy_name )
735 vlc_meta_Add( *pp_meta, VLC_META_TITLE,
736 p_sys->psz_icy_name );
737 if( p_sys->psz_icy_genre )
738 vlc_meta_Add( *pp_meta, VLC_META_GENRE,
739 p_sys->psz_icy_genre );
740 if( p_sys->psz_icy_title )
741 vlc_meta_Add( *pp_meta, VLC_META_NOW_PLAYING,
742 p_sys->psz_icy_title );
745 case ACCESS_GET_TITLE_INFO:
746 case ACCESS_SET_TITLE:
747 case ACCESS_SET_SEEKPOINT:
748 case ACCESS_SET_PRIVATE_ID_STATE:
752 msg_Warn( p_access, "unimplemented query in control" );
759 /*****************************************************************************
761 *****************************************************************************/
762 static int Connect( access_t *p_access, int64_t i_tell )
764 access_sys_t *p_sys = p_access->p_sys;
765 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
768 if( p_sys->psz_location ) free( p_sys->psz_location );
769 if( p_sys->psz_mime ) free( p_sys->psz_mime );
770 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
772 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
773 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
774 if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
777 p_sys->psz_location = NULL;
778 p_sys->psz_mime = NULL;
779 p_sys->psz_pragma = NULL;
780 p_sys->b_mms = VLC_FALSE;
781 p_sys->b_chunked = VLC_FALSE;
783 p_sys->i_icy_meta = 0;
784 p_sys->psz_icy_name = NULL;
785 p_sys->psz_icy_genre = NULL;
786 p_sys->psz_icy_title = NULL;
788 p_access->info.i_size = 0;
789 p_access->info.i_pos = i_tell;
790 p_access->info.b_eof = VLC_FALSE;
793 /* Open connection */
794 p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
797 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
801 /* Initialize TLS/SSL session */
802 if( p_sys->b_ssl == VLC_TRUE )
804 /* CONNECT to establish TLS tunnel through HTTP proxy */
808 unsigned i_status = 0;
810 if( p_sys->i_version == 0 )
812 /* CONNECT is not in HTTP/1.0 */
813 Disconnect( p_access );
817 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
818 "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
819 p_sys->url.psz_host, p_sys->url.i_port,
821 p_sys->url.psz_host, p_sys->url.i_port);
823 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
826 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
827 Disconnect( p_access );
831 sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
834 if( ( i_status / 100 ) != 2 )
836 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
837 Disconnect( p_access );
843 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
846 msg_Err( p_access, "HTTP proxy connection failed" );
847 Disconnect( p_access );
859 /* TLS/SSL handshake */
860 p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
862 if( p_sys->p_tls == NULL )
864 msg_Err( p_access, "cannot establish HTTP/TLS session" );
865 Disconnect( p_access );
868 p_sys->p_vs = &p_sys->p_tls->sock;
871 return Request( p_access, i_tell );
875 static int Request( access_t *p_access, int64_t i_tell )
877 access_sys_t *p_sys = p_access->p_sys;
879 v_socket_t *pvs = p_sys->p_vs;
883 if( p_sys->url.psz_path )
885 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
886 "GET http://%s:%d%s HTTP/1.%d\r\n",
887 p_sys->url.psz_host, p_sys->url.i_port,
888 p_sys->url.psz_path, p_sys->i_version );
892 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
893 "GET http://%s:%d/ HTTP/1.%d\r\n",
894 p_sys->url.psz_host, p_sys->url.i_port,
900 char *psz_path = p_sys->url.psz_path;
901 if( !psz_path || !*psz_path )
905 if( p_sys->url.i_port != 80)
907 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
908 "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
909 psz_path, p_sys->i_version, p_sys->url.psz_host,
914 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
915 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
916 psz_path, p_sys->i_version, p_sys->url.psz_host );
920 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "User-Agent: %s\r\n",
921 p_sys->psz_user_agent );
923 if( p_sys->i_version == 1 )
925 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
926 "Range: bytes="I64Fd"-\r\n", i_tell );
930 if( p_sys->url.psz_username && *p_sys->url.psz_username )
935 asprintf( &buf, "%s:%s", p_sys->url.psz_username,
936 p_sys->url.psz_password ? p_sys->url.psz_password : "" );
938 b64 = vlc_b64_encode( buf );
941 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
942 "Authorization: Basic %s\r\n", b64 );
946 /* Proxy Authentication */
947 if( p_sys->proxy.psz_username && *p_sys->proxy.psz_username )
952 asprintf( &buf, "%s:%s", p_sys->proxy.psz_username,
953 p_sys->proxy.psz_password ? p_sys->proxy.psz_password : "" );
955 b64 = vlc_b64_encode( buf );
958 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
959 "Proxy-Authorization: Basic %s\r\n", b64 );
963 /* ICY meta data request */
964 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
967 if( p_sys->b_continuous && p_sys->i_version == 1 )
969 net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
970 "Connection: keep-alive\r\n" );
974 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
975 "Connection: Close\r\n");
976 p_sys->b_continuous = VLC_FALSE;
979 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "\r\n" ) < 0 )
981 msg_Err( p_access, "failed to send request" );
982 Disconnect( p_access );
987 if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs ) ) == NULL )
989 msg_Err( p_access, "failed to read answer" );
992 if( !strncmp( psz, "HTTP/1.", 7 ) )
994 p_sys->psz_protocol = "HTTP";
995 p_sys->i_code = atoi( &psz[9] );
997 else if( !strncmp( psz, "ICY", 3 ) )
999 p_sys->psz_protocol = "ICY";
1000 p_sys->i_code = atoi( &psz[4] );
1001 p_sys->b_reconnect = VLC_TRUE;
1005 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1009 msg_Dbg( p_access, "protocol '%s' answer code %d",
1010 p_sys->psz_protocol, p_sys->i_code );
1011 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1013 p_sys->b_seekable = VLC_FALSE;
1015 if( p_sys->i_code != 206 )
1017 p_sys->b_seekable = VLC_FALSE;
1019 /* Authentication error - We'll have to display the dialog */
1020 if( p_sys->i_code == 401 )
1024 /* Other fatal error */
1025 else if( p_sys->i_code >= 400 )
1027 msg_Err( p_access, "error: %s", psz );
1035 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs );
1040 msg_Err( p_access, "failed to read answer" );
1044 /* msg_Dbg( p_input, "Line=%s", psz ); */
1052 if( ( p = strchr( psz, ':' ) ) == NULL )
1054 msg_Err( p_access, "malformed header line: %s", psz );
1059 while( *p == ' ' ) p++;
1061 if( !strcasecmp( psz, "Content-Length" ) )
1063 if( p_sys->b_continuous )
1065 p_access->info.i_size = -1;
1066 msg_Dbg( p_access, "this frame size="I64Fd, atoll(p ) );
1067 p_sys->i_remaining = atoll( p );
1071 p_access->info.i_size = i_tell + atoll( p );
1072 msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
1075 else if( !strcasecmp( psz, "Location" ) )
1077 if( p_sys->psz_location ) free( p_sys->psz_location );
1078 p_sys->psz_location = strdup( p );
1080 else if( !strcasecmp( psz, "Content-Type" ) )
1082 if( p_sys->psz_mime ) free( p_sys->psz_mime );
1083 p_sys->psz_mime = strdup( p );
1084 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1086 else if( !strcasecmp( psz, "Pragma" ) )
1088 if( !strcasecmp( psz, "Pragma: features" ) )
1089 p_sys->b_mms = VLC_TRUE;
1090 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
1091 p_sys->psz_pragma = strdup( p );
1092 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1094 else if( !strcasecmp( psz, "Server" ) )
1096 msg_Dbg( p_access, "Server: %s", p );
1097 if( !strncasecmp( p, "Icecast", 7 ) ||
1098 !strncasecmp( p, "Nanocaster", 10 ) )
1100 /* Remember if this is Icecast
1101 * we need to force demux in this case without breaking
1104 /* Let live 365 streams (nanocaster) piggyback on the icecast
1105 * routine. They look very similar */
1107 p_sys->b_reconnect = VLC_TRUE;
1108 p_sys->b_pace_control = VLC_FALSE;
1109 p_sys->b_icecast = VLC_TRUE;
1112 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1114 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1115 if( !strncasecmp( p, "chunked", 7 ) )
1117 p_sys->b_chunked = VLC_TRUE;
1120 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1122 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1123 p_sys->i_icy_meta = atoi( p );
1124 if( p_sys->i_icy_meta < 0 )
1125 p_sys->i_icy_meta = 0;
1127 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1129 else if( !strcasecmp( psz, "Icy-Name" ) )
1131 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
1132 p_sys->psz_icy_name = strdup( p );
1133 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1135 p_sys->b_icecast = VLC_TRUE; /* be on the safeside. set it here as well. */
1136 p_sys->b_reconnect = VLC_TRUE;
1137 p_sys->b_pace_control = VLC_FALSE;
1139 else if( !strcasecmp( psz, "Icy-Genre" ) )
1141 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
1142 p_sys->psz_icy_genre = strdup( p );
1143 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1145 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1147 msg_Dbg( p_access, "Icy-Notice: %s", p );
1149 else if( !strncasecmp( psz, "icy-", 4 ) ||
1150 !strncasecmp( psz, "ice-", 4 ) ||
1151 !strncasecmp( psz, "x-audiocast", 11 ) )
1153 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1161 Disconnect( p_access );
1162 return VLC_EGENERIC;
1165 /*****************************************************************************
1167 *****************************************************************************/
1168 static void Disconnect( access_t *p_access )
1170 access_sys_t *p_sys = p_access->p_sys;
1172 if( p_sys->p_tls != NULL)
1174 tls_ClientDelete( p_sys->p_tls );
1175 p_sys->p_tls = NULL;
1178 if( p_sys->fd != -1)
1180 net_Close(p_sys->fd);