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 *****************************************************************************/
35 #include <vlc/input.h>
37 #include "vlc_interaction.h"
38 #include "vlc_playlist.h"
44 /*****************************************************************************
46 *****************************************************************************/
47 static int Open ( vlc_object_t * );
48 static void Close( vlc_object_t * );
50 #define PROXY_TEXT N_("HTTP proxy")
51 #define PROXY_LONGTEXT N_( \
52 "HTTP proxy to be usesd It must be of the form " \
53 "http://[user[:pass]@]myproxy.mydomain:myport/ ; " \
54 "if empty, the http_proxy environment variable will be tried." )
56 #define CACHING_TEXT N_("Caching value in ms")
57 #define CACHING_LONGTEXT N_( \
58 "Caching value for HTTP streams. This " \
59 "value should be set in milliseconds." )
61 #define AGENT_TEXT N_("HTTP user agent")
62 #define AGENT_LONGTEXT N_("User agent that will be " \
63 "used for the connection.")
65 #define RECONNECT_TEXT N_("Auto re-connect")
66 #define RECONNECT_LONGTEXT N_( \
67 "Automatically try to reconnect to the stream in case of a sudden " \
70 /// \bug missing space before you should
71 #define CONTINUOUS_TEXT N_("Continuous stream")
72 #define CONTINUOUS_LONGTEXT N_("Read a file that is " \
73 "being constantly updated (for example, a JPG file on a server)." \
74 "You should not globally enable this option as it will break all other " \
75 "types of HTTP streams." )
78 set_description( _("HTTP input") );
79 set_capability( "access2", 0 );
80 set_shortname( _( "HTTP(S)" ) );
81 set_category( CAT_INPUT );
82 set_subcategory( SUBCAT_INPUT_ACCESS );
84 add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
86 add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
87 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
88 add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
89 AGENT_LONGTEXT, VLC_TRUE );
90 add_bool( "http-reconnect", 0, NULL, RECONNECT_TEXT,
91 RECONNECT_LONGTEXT, VLC_TRUE );
92 add_bool( "http-continuous", 0, NULL, CONTINUOUS_TEXT,
93 CONTINUOUS_LONGTEXT, VLC_TRUE );
94 add_suppressed_string("http-user");
95 add_suppressed_string("http-pwd");
96 add_shortcut( "http" );
97 add_shortcut( "https" );
98 add_shortcut( "unsv" );
99 set_callbacks( Open, Close );
102 /*****************************************************************************
104 *****************************************************************************/
108 tls_session_t *p_tls;
113 char *psz_user_agent;
128 vlc_bool_t b_icecast;
131 vlc_bool_t b_chunked;
141 vlc_bool_t b_seekable;
142 vlc_bool_t b_reconnect;
143 vlc_bool_t b_continuous;
144 vlc_bool_t b_pace_control;
148 static int Read( access_t *, uint8_t *, int );
149 static int Seek( access_t *, int64_t );
150 static int Control( access_t *, int, va_list );
153 static int Connect( access_t *, int64_t );
154 static int Request( access_t *p_access, int64_t i_tell );
155 static void Disconnect( access_t * );
157 /*****************************************************************************
159 *****************************************************************************/
160 static int Open( vlc_object_t *p_this )
162 access_t *p_access = (access_t*)p_this;
166 /* Set up p_access */
167 STANDARD_READ_ACCESS_INIT;
169 p_sys->b_proxy = VLC_FALSE;
170 p_sys->i_version = 1;
171 p_sys->b_seekable = VLC_TRUE;
172 p_sys->psz_mime = NULL;
173 p_sys->psz_pragma = NULL;
174 p_sys->b_mms = VLC_FALSE;
175 p_sys->b_icecast = VLC_FALSE;
176 p_sys->psz_location = NULL;
177 p_sys->psz_user_agent = NULL;
178 p_sys->b_pace_control = VLC_TRUE;
179 p_sys->b_ssl = VLC_FALSE;
182 p_sys->i_icy_meta = 0;
183 p_sys->psz_icy_name = NULL;
184 p_sys->psz_icy_genre = NULL;
185 p_sys->psz_icy_title = NULL;
186 p_sys->i_remaining = 0;
189 /* Parse URI - remove spaces */
190 p = psz = strdup( p_access->psz_path );
191 while( (p = strchr( p, ' ' )) != NULL )
193 vlc_UrlParse( &p_sys->url, psz, 0 );
196 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
198 msg_Warn( p_access, "invalid host" );
201 if( !strncmp( p_access->psz_access, "https", 5 ) )
204 p_sys->b_ssl = VLC_TRUE;
205 if( p_sys->url.i_port <= 0 )
206 p_sys->url.i_port = 443;
210 if( p_sys->url.i_port <= 0 )
211 p_sys->url.i_port = 80;
215 p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
218 psz = var_CreateGetString( p_access, "http-proxy" );
221 p_sys->b_proxy = VLC_TRUE;
222 vlc_UrlParse( &p_sys->proxy, psz, 0 );
227 char *psz_proxy = getenv( "http_proxy" );
228 if( psz_proxy && *psz_proxy )
230 p_sys->b_proxy = VLC_TRUE;
231 vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
239 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
241 msg_Warn( p_access, "invalid proxy host" );
244 if( p_sys->proxy.i_port <= 0 )
246 p_sys->proxy.i_port = 80;
250 msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
251 p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
254 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
255 p_sys->proxy.i_port );
257 if( p_sys->url.psz_username && *p_sys->url.psz_username )
259 msg_Dbg( p_access, " user='%s', pwd='%s'",
260 p_sys->url.psz_username, p_sys->url.psz_password );
263 p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
264 p_sys->b_continuous = var_CreateGetBool( p_access, "http-continuous" );
268 if( Connect( p_access, 0 ) )
270 /* Retry with http 1.0 */
271 msg_Dbg( p_access, "switching to HTTP version 1.0" );
272 p_sys->i_version = 0;
273 p_sys->b_seekable = VLC_FALSE;
275 if( p_access->b_die ||
276 Connect( p_access, 0 ) )
282 if( p_sys->i_code == 401 )
284 char *psz_login = NULL; char *psz_password = NULL;
286 msg_Dbg( p_access, "authentication failed" );
287 i_ret = intf_UserLoginPassword( p_access, _("HTTP authentication"),
288 _("Please enter a valid login name and a password."),
289 &psz_login, &psz_password );
290 if( i_ret == DIALOG_OK_YES )
292 msg_Dbg( p_access, "retrying with user=%s, pwd=%s",
293 psz_login, psz_password );
294 if( psz_login ) p_sys->url.psz_username = strdup( psz_login );
295 if( psz_password ) p_sys->url.psz_password = strdup( psz_password );
296 if( psz_login ) free( psz_login );
297 if( psz_password ) free( psz_password );
302 if( psz_login ) free( psz_login );
303 if( psz_password ) free( psz_password );
308 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
309 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
310 p_sys->psz_location && *p_sys->psz_location )
312 playlist_t * p_playlist;
313 input_item_t *p_input_item;
315 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
317 /* Do not accept redirection outside of HTTP works */
318 if( strncmp( p_sys->psz_location, "http", 4 )
319 || ( ( p_sys->psz_location[4] != ':' ) /* HTTP */
320 && strncmp( p_sys->psz_location + 4, "s:", 2 ) /* HTTP/SSL */ ) )
322 msg_Err( p_access, "insecure redirection ignored" );
326 p_playlist = vlc_object_find( p_access, VLC_OBJECT_PLAYLIST,
330 msg_Err( p_access, "redirection failed: can't find playlist" );
335 vlc_mutex_lock( &p_playlist->object_lock );
336 p_input_item = p_playlist->status.p_item->p_input;
337 vlc_mutex_lock( &p_input_item->lock );
338 free( p_input_item->psz_uri );
339 free( p_access->psz_path );
340 p_input_item->psz_uri = strdup( p_sys->psz_location );
341 p_access->psz_path = strdup( p_sys->psz_location );
342 vlc_mutex_unlock( &p_input_item->lock );
343 vlc_mutex_unlock( &p_playlist->object_lock );
344 vlc_object_release( p_playlist );
346 /* Clean up current Open() run */
347 vlc_UrlClean( &p_sys->url );
348 vlc_UrlClean( &p_sys->proxy );
349 if( p_sys->psz_mime ) free( p_sys->psz_mime );
350 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
351 if( p_sys->psz_location ) free( p_sys->psz_location );
352 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
354 Disconnect( p_access );
357 /* Do new Open() run with new data */
358 return Open( p_this );
363 msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
367 if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
369 if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
371 if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
372 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
373 p_access->psz_demux = strdup( "nsv" );
374 else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
375 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
376 p_access->psz_demux = strdup( "m4a" );
377 else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
378 p_access->psz_demux = strdup( "mp3" );
380 msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
381 p_access->psz_demux );
383 #if 0 /* Doesn't work really well because of the pre-buffering in
384 * shoutcast servers (the buffer content will be sent as fast as
386 p_sys->b_pace_control = VLC_FALSE;
389 else if( !p_sys->psz_mime )
392 p_access->psz_demux = strdup( "mp3" );
394 /* else probably Ogg Vorbis */
396 else if( !strcasecmp( p_access->psz_access, "unsv" ) &&
398 !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
400 /* Grrrr! detect ultravox server and force NSV demuxer */
401 p_access->psz_demux = strdup( "nsv" );
403 else if( p_sys->psz_mime &&
404 !strncasecmp( p_sys->psz_mime, "application/xspf+xml", 20 ) &&
405 ( memchr( " ;\t", p_sys->psz_mime[20], 4 ) != NULL ) )
406 p_access->psz_demux = strdup( "xspf-open" );
408 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
411 var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
416 vlc_UrlClean( &p_sys->url );
417 vlc_UrlClean( &p_sys->proxy );
418 if( p_sys->psz_mime ) free( p_sys->psz_mime );
419 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
420 if( p_sys->psz_location ) free( p_sys->psz_location );
421 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
423 Disconnect( p_access );
428 /*****************************************************************************
430 *****************************************************************************/
431 static void Close( vlc_object_t *p_this )
433 access_t *p_access = (access_t*)p_this;
434 access_sys_t *p_sys = p_access->p_sys;
436 vlc_UrlClean( &p_sys->url );
437 vlc_UrlClean( &p_sys->proxy );
439 if( p_sys->psz_mime ) free( p_sys->psz_mime );
440 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
441 if( p_sys->psz_location ) free( p_sys->psz_location );
443 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
444 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
445 if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
447 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
449 Disconnect( p_access );
453 /*****************************************************************************
454 * Read: Read up to i_len bytes from the http connection and place in
455 * p_buffer. Return the actual number of bytes read
456 *****************************************************************************/
457 static int ReadICYMeta( access_t *p_access );
458 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
460 access_sys_t *p_sys = p_access->p_sys;
465 p_access->info.b_eof = VLC_TRUE;
469 if( p_access->info.i_size > 0 &&
470 i_len + p_access->info.i_pos > p_access->info.i_size )
472 if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
474 p_access->info.b_eof = VLC_TRUE;
479 if( p_sys->b_chunked )
481 if( p_sys->i_chunk < 0 )
483 p_access->info.b_eof = VLC_TRUE;
487 if( p_sys->i_chunk <= 0 )
489 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
490 /* read the chunk header */
493 /* fatal error - end of file */
494 msg_Dbg( p_access, "failed reading chunk-header line" );
497 p_sys->i_chunk = strtoll( psz, NULL, 16 );
500 if( p_sys->i_chunk <= 0 ) /* eof */
503 p_access->info.b_eof = VLC_TRUE;
508 if( i_len > p_sys->i_chunk )
510 i_len = p_sys->i_chunk;
514 if( p_sys->b_continuous && i_len > p_sys->i_remaining )
516 /* Only ask for the remaining length */
517 int i_new_len = p_sys->i_remaining;
520 Request( p_access, 0 );
521 i_read = Read( p_access, p_buffer, i_len );
527 if( p_sys->i_icy_meta > 0 && p_access->info.i_pos > 0 )
529 int64_t i_next = p_sys->i_icy_meta -
530 p_access->info.i_pos % p_sys->i_icy_meta;
532 if( i_next == p_sys->i_icy_meta )
534 if( ReadICYMeta( p_access ) )
536 p_access->info.b_eof = VLC_TRUE;
544 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, VLC_FALSE );
548 p_access->info.i_pos += i_read;
550 if( p_sys->b_chunked )
552 p_sys->i_chunk -= i_read;
553 if( p_sys->i_chunk <= 0 )
555 /* read the empty line */
556 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
557 if( psz ) free( psz );
561 else if( i_read == 0 )
564 * I very much doubt that this will work.
565 * If i_read == 0, the connection *IS* dead, so the only
566 * sensible thing to do is Disconnect() and then retry.
567 * Otherwise, I got recv() completely wrong. -- Courmisch
569 if( p_sys->b_continuous )
571 Request( p_access, 0 );
572 p_sys->b_continuous = VLC_FALSE;
573 i_read = Read( p_access, p_buffer, i_len );
574 p_sys->b_continuous = VLC_TRUE;
576 Disconnect( p_access );
577 if( p_sys->b_reconnect )
579 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
580 if( Connect( p_access, p_access->info.i_pos ) )
582 msg_Dbg( p_access, "reconnection failed" );
586 p_sys->b_reconnect = VLC_FALSE;
587 i_read = Read( p_access, p_buffer, i_len );
588 p_sys->b_reconnect = VLC_TRUE;
592 if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
595 if( p_sys->b_continuous )
597 p_sys->i_remaining -= i_read;
603 static int ReadICYMeta( access_t *p_access )
605 access_sys_t *p_sys = p_access->p_sys;
611 /* Read meta data length */
612 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
619 i_read = buffer << 4;
620 /* msg_Dbg( p_access, "ICY meta size=%u", i_read); */
622 psz_meta = malloc( i_read + 1 );
623 if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
624 (uint8_t *)psz_meta, i_read, VLC_TRUE ) != i_read )
627 psz_meta[i_read] = '\0'; /* Just in case */
629 /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
631 /* Now parse the meta */
632 /* Look for StreamTitle= */
633 p = strcasestr( (char *)psz_meta, "StreamTitle=" );
636 p += strlen( "StreamTitle=" );
637 if( *p == '\'' || *p == '"' )
639 char closing[] = { p[0], ';', '\0' };
640 char *psz = strstr( &p[1], closing );
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;
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 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
737 if( p_sys->psz_icy_name )
738 vlc_meta_SetTitle( p_meta, p_sys->psz_icy_name );
739 if( p_sys->psz_icy_genre )
740 vlc_meta_SetGenre( p_meta, p_sys->psz_icy_genre );
741 if( p_sys->psz_icy_title )
742 vlc_meta_SetNowPlaying( p_meta, 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 )
969 net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
970 "Connection: Keep-Alive\r\n" );
972 else if( p_sys->i_version == 1 )
974 net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
975 "Connection: Close\r\n");
978 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "\r\n" ) < 0 )
980 msg_Err( p_access, "failed to send request" );
981 Disconnect( p_access );
986 if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs ) ) == NULL )
988 msg_Err( p_access, "failed to read answer" );
991 if( !strncmp( psz, "HTTP/1.", 7 ) )
993 p_sys->psz_protocol = "HTTP";
994 p_sys->i_code = atoi( &psz[9] );
996 else if( !strncmp( psz, "ICY", 3 ) )
998 p_sys->psz_protocol = "ICY";
999 p_sys->i_code = atoi( &psz[4] );
1000 p_sys->b_reconnect = VLC_TRUE;
1004 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1008 msg_Dbg( p_access, "protocol '%s' answer code %d",
1009 p_sys->psz_protocol, p_sys->i_code );
1010 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1012 p_sys->b_seekable = VLC_FALSE;
1014 if( p_sys->i_code != 206 )
1016 p_sys->b_seekable = VLC_FALSE;
1018 /* Authentication error - We'll have to display the dialog */
1019 if( p_sys->i_code == 401 )
1023 /* Other fatal error */
1024 else if( p_sys->i_code >= 400 )
1026 msg_Err( p_access, "error: %s", psz );
1034 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs );
1039 msg_Err( p_access, "failed to read answer" );
1043 /* msg_Dbg( p_input, "Line=%s", psz ); */
1051 if( ( p = strchr( psz, ':' ) ) == NULL )
1053 msg_Err( p_access, "malformed header line: %s", psz );
1058 while( *p == ' ' ) p++;
1060 if( !strcasecmp( psz, "Content-Length" ) )
1062 if( p_sys->b_continuous )
1064 p_access->info.i_size = -1;
1065 msg_Dbg( p_access, "this frame size="I64Fd, atoll(p ) );
1066 p_sys->i_remaining = atoll( p );
1070 p_access->info.i_size = i_tell + atoll( p );
1071 msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
1074 else if( !strcasecmp( psz, "Location" ) )
1076 if( p_sys->psz_location ) free( p_sys->psz_location );
1077 p_sys->psz_location = strdup( p );
1079 else if( !strcasecmp( psz, "Content-Type" ) )
1081 if( p_sys->psz_mime ) free( p_sys->psz_mime );
1082 p_sys->psz_mime = strdup( p );
1083 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1085 else if( !strcasecmp( psz, "Pragma" ) )
1087 if( !strcasecmp( psz, "Pragma: features" ) )
1088 p_sys->b_mms = VLC_TRUE;
1089 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
1090 p_sys->psz_pragma = strdup( p );
1091 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1093 else if( !strcasecmp( psz, "Server" ) )
1095 msg_Dbg( p_access, "Server: %s", p );
1096 if( !strncasecmp( p, "Icecast", 7 ) ||
1097 !strncasecmp( p, "Nanocaster", 10 ) )
1099 /* Remember if this is Icecast
1100 * we need to force demux in this case without breaking
1103 /* Let live 365 streams (nanocaster) piggyback on the icecast
1104 * routine. They look very similar */
1106 p_sys->b_reconnect = VLC_TRUE;
1107 p_sys->b_pace_control = VLC_FALSE;
1108 p_sys->b_icecast = VLC_TRUE;
1111 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1113 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1114 if( !strncasecmp( p, "chunked", 7 ) )
1116 p_sys->b_chunked = VLC_TRUE;
1119 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1121 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1122 p_sys->i_icy_meta = atoi( p );
1123 if( p_sys->i_icy_meta < 0 )
1124 p_sys->i_icy_meta = 0;
1126 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1128 else if( !strcasecmp( psz, "Icy-Name" ) )
1130 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
1131 p_sys->psz_icy_name = strdup( p );
1132 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1134 p_sys->b_icecast = VLC_TRUE; /* be on the safeside. set it here as well. */
1135 p_sys->b_reconnect = VLC_TRUE;
1136 p_sys->b_pace_control = VLC_FALSE;
1138 else if( !strcasecmp( psz, "Icy-Genre" ) )
1140 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
1141 p_sys->psz_icy_genre = strdup( p );
1142 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1144 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1146 msg_Dbg( p_access, "Icy-Notice: %s", p );
1148 else if( !strncasecmp( psz, "icy-", 4 ) ||
1149 !strncasecmp( psz, "ice-", 4 ) ||
1150 !strncasecmp( psz, "x-audiocast", 11 ) )
1152 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1160 Disconnect( p_access );
1161 return VLC_EGENERIC;
1164 /*****************************************************************************
1166 *****************************************************************************/
1167 static void Disconnect( access_t *p_access )
1169 access_sys_t *p_sys = p_access->p_sys;
1171 if( p_sys->p_tls != NULL)
1173 tls_ClientDelete( p_sys->p_tls );
1174 p_sys->p_tls = NULL;
1177 if( p_sys->fd != -1)
1179 net_Close(p_sys->fd);