1 /*****************************************************************************
2 * http.c: HTTP input module
3 *****************************************************************************
4 * Copyright (C) 2001-2005 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Christophe Massiot <massiot@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
31 #include <vlc/input.h>
33 #include "vlc_playlist.h"
38 /*****************************************************************************
40 *****************************************************************************/
41 static int Open ( vlc_object_t * );
42 static void Close( vlc_object_t * );
44 #define PROXY_TEXT N_("HTTP proxy")
45 #define PROXY_LONGTEXT N_( \
46 "You can specify an HTTP proxy to use. It must be of the form " \
47 "http://myproxy.mydomain:myport/. If none is specified, the HTTP_PROXY " \
48 "environment variable will be tried." )
50 #define CACHING_TEXT N_("Caching value in ms")
51 #define CACHING_LONGTEXT N_( \
52 "Allows you to modify the default caching value for http streams. This " \
53 "value should be set in millisecond units." )
55 #define USER_TEXT N_("HTTP user name")
56 #define USER_LONGTEXT N_("Allows you to modify the user name that will " \
57 "be used for the connection (Basic authentication only).")
59 #define PASS_TEXT N_("HTTP password")
60 #define PASS_LONGTEXT N_("Allows you to modify the password that will be " \
61 "used for the connection.")
63 #define AGENT_TEXT N_("HTTP user agent")
64 #define AGENT_LONGTEXT N_("Allows you to modify the user agent that will be " \
65 "used for the connection.")
67 #define RECONNECT_TEXT N_("Auto re-connect")
68 #define RECONNECT_LONGTEXT N_("Will automatically attempt a re-connection " \
69 "in case it was untimely closed.")
71 #define CONTINUOUS_TEXT N_("Continuous stream")
72 #define CONTINUOUS_LONGTEXT N_("Enable this option to read a file that is " \
73 "being constantly updated (for example, a JPG file on a server)")
76 set_description( _("HTTP input") );
77 set_capability( "access2", 0 );
78 set_shortname( _( "HTTP/HTTPS" ) );
79 set_category( CAT_INPUT );
80 set_subcategory( SUBCAT_INPUT_ACCESS );
82 add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
84 add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
85 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
86 add_string( "http-user", NULL, NULL, USER_TEXT, USER_LONGTEXT, VLC_FALSE );
87 add_string( "http-pwd", NULL , NULL, PASS_TEXT, PASS_LONGTEXT, VLC_FALSE );
88 add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
89 AGENT_LONGTEXT, VLC_FALSE );
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 );
95 add_shortcut( "http" );
96 add_shortcut( "http4" );
97 add_shortcut( "http6" );
98 add_shortcut( "https" );
99 add_shortcut( "unsv" );
100 set_callbacks( Open, Close );
103 /*****************************************************************************
105 *****************************************************************************/
109 tls_session_t *p_tls;
116 char *psz_user_agent;
131 vlc_bool_t b_icecast;
134 vlc_bool_t b_chunked;
144 vlc_bool_t b_seekable;
145 vlc_bool_t b_reconnect;
146 vlc_bool_t b_continuous;
147 vlc_bool_t b_pace_control;
151 static int Read( access_t *, uint8_t *, int );
152 static int Seek( access_t *, int64_t );
153 static int Control( access_t *, int, va_list );
156 static void ParseURL( access_sys_t *, char *psz_url );
157 static int Connect( access_t *, int64_t );
158 static int Request( access_t *p_access, int64_t i_tell );
159 static void Disconnect( access_t * );
161 /*****************************************************************************
163 *****************************************************************************/
164 static int Open( vlc_object_t *p_this )
166 access_t *p_access = (access_t*)p_this;
170 /* First set ipv4/ipv6 */
171 var_Create( p_access, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
172 var_Create( p_access, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
174 if( *p_access->psz_access )
177 /* Find out which shortcut was used */
178 if( !strncmp( p_access->psz_access, "http4", 6 ) )
180 val.b_bool = VLC_TRUE;
181 var_Set( p_access, "ipv4", val );
183 val.b_bool = VLC_FALSE;
184 var_Set( p_access, "ipv6", val );
186 else if( !strncmp( p_access->psz_access, "http6", 6 ) )
188 val.b_bool = VLC_TRUE;
189 var_Set( p_access, "ipv6", val );
191 val.b_bool = VLC_FALSE;
192 var_Set( p_access, "ipv4", val );
196 /* Set up p_access */
197 p_access->pf_read = Read;
198 p_access->pf_block = NULL;
199 p_access->pf_control = Control;
200 p_access->pf_seek = Seek;
201 p_access->info.i_update = 0;
202 p_access->info.i_size = 0;
203 p_access->info.i_pos = 0;
204 p_access->info.b_eof = VLC_FALSE;
205 p_access->info.i_title = 0;
206 p_access->info.i_seekpoint = 0;
207 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
208 memset( p_sys, 0, sizeof( access_sys_t ) );
210 p_sys->b_proxy = VLC_FALSE;
211 p_sys->i_version = 1;
212 p_sys->b_seekable = VLC_TRUE;
213 p_sys->psz_mime = NULL;
214 p_sys->psz_pragma = NULL;
215 p_sys->b_mms = VLC_FALSE;
216 p_sys->b_icecast = VLC_FALSE;
217 p_sys->psz_location = NULL;
218 p_sys->psz_user_agent = NULL;
219 p_sys->b_pace_control = VLC_TRUE;
220 p_sys->b_ssl = VLC_FALSE;
223 p_sys->i_icy_meta = 0;
224 p_sys->psz_icy_name = NULL;
225 p_sys->psz_icy_genre = NULL;
226 p_sys->psz_icy_title = NULL;
227 p_sys->i_remaining = 0;
230 if( vlc_UrlIsNotEncoded( p_access->psz_path ) )
232 psz = vlc_UrlEncode( p_access->psz_path );
239 ParseURL( p_sys, psz );
243 ParseURL( p_sys, p_access->psz_path );
245 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
247 msg_Warn( p_access, "invalid host" );
250 if( !strncmp( p_access->psz_access, "https", 5 ) )
253 p_sys->b_ssl = VLC_TRUE;
254 if( p_sys->url.i_port <= 0 )
255 p_sys->url.i_port = 443;
259 if( p_sys->url.i_port <= 0 )
260 p_sys->url.i_port = 80;
262 if( !p_sys->psz_user || *p_sys->psz_user == '\0' )
264 p_sys->psz_user = var_CreateGetString( p_access, "http-user" );
265 p_sys->psz_passwd = var_CreateGetString( p_access, "http-pwd" );
269 p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
272 psz = var_CreateGetString( p_access, "http-proxy" );
275 p_sys->b_proxy = VLC_TRUE;
276 vlc_UrlParse( &p_sys->proxy, psz, 0 );
280 char *psz_proxy = getenv( "http_proxy" );
281 if( psz_proxy && *psz_proxy )
283 p_sys->b_proxy = VLC_TRUE;
284 vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
293 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
295 msg_Warn( p_access, "invalid proxy host" );
298 if( p_sys->proxy.i_port <= 0 )
300 p_sys->proxy.i_port = 80;
304 msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
305 p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
308 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
309 p_sys->proxy.i_port );
311 if( p_sys->psz_user && *p_sys->psz_user )
313 msg_Dbg( p_access, " user='%s', pwd='%s'",
314 p_sys->psz_user, p_sys->psz_passwd );
317 p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
318 p_sys->b_continuous = var_CreateGetBool( p_access, "http-continuous" );
321 if( Connect( p_access, 0 ) )
323 /* Retry with http 1.0 */
324 p_sys->i_version = 0;
326 if( p_access->b_die ||
327 Connect( p_access, 0 ) )
333 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
334 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
335 p_sys->psz_location && *p_sys->psz_location )
337 playlist_t * p_playlist;
338 input_item_t *p_input_item;
340 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
342 p_playlist = vlc_object_find( p_access, VLC_OBJECT_PLAYLIST,
346 msg_Err( p_access, "redirection failed: can't find playlist" );
351 vlc_mutex_lock( &p_playlist->object_lock );
352 p_input_item = &p_playlist->status.p_item->input;
353 vlc_mutex_lock( &p_input_item->lock );
354 free( p_input_item->psz_uri );
355 free( p_access->psz_path );
356 p_input_item->psz_uri = strdup( p_sys->psz_location );
357 p_access->psz_path = strdup( p_sys->psz_location );
358 vlc_mutex_unlock( &p_input_item->lock );
359 vlc_mutex_unlock( &p_playlist->object_lock );
360 vlc_object_release( p_playlist );
362 /* Clean up current Open() run */
363 vlc_UrlClean( &p_sys->url );
364 vlc_UrlClean( &p_sys->proxy );
365 if( p_sys->psz_mime ) free( p_sys->psz_mime );
366 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
367 if( p_sys->psz_location ) free( p_sys->psz_location );
368 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
369 if( p_sys->psz_user ) free( p_sys->psz_user );
370 if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
372 Disconnect( p_access );
375 /* Do new Open() run with new data */
376 return Open( p_this );
381 msg_Dbg( p_access, "This is actually a live mms server, BAIL" );
385 if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
387 if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
389 if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
390 !strcasecmp( p_sys->psz_mime, "video/nsa") )
391 p_access->psz_demux = strdup( "nsv" );
392 else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
393 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
394 p_access->psz_demux = strdup( "m4a" );
395 else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
396 p_access->psz_demux = strdup( "mp3" );
398 msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
399 p_access->psz_demux );
401 #if 0 /* Doesn't work really well because of the pre-buffering in
402 * shoutcast servers (the buffer content will be sent as fast as
404 p_sys->b_pace_control = VLC_FALSE;
407 else if( !p_sys->psz_mime )
410 p_access->psz_demux = strdup( "mp3" );
412 /* else probably Ogg Vorbis */
415 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
418 var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
423 vlc_UrlClean( &p_sys->url );
424 vlc_UrlClean( &p_sys->proxy );
425 if( p_sys->psz_mime ) free( p_sys->psz_mime );
426 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
427 if( p_sys->psz_location ) free( p_sys->psz_location );
428 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
429 if( p_sys->psz_user ) free( p_sys->psz_user );
430 if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
432 Disconnect( p_access );
437 /*****************************************************************************
439 *****************************************************************************/
440 static void Close( vlc_object_t *p_this )
442 access_t *p_access = (access_t*)p_this;
443 access_sys_t *p_sys = p_access->p_sys;
445 vlc_UrlClean( &p_sys->url );
446 vlc_UrlClean( &p_sys->proxy );
448 if( p_sys->psz_user ) free( p_sys->psz_user );
449 if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
451 if( p_sys->psz_mime ) free( p_sys->psz_mime );
452 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
453 if( p_sys->psz_location ) free( p_sys->psz_location );
455 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
456 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
457 if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
459 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
461 Disconnect( p_access );
465 /*****************************************************************************
466 * Read: Read up to i_len bytes from the http connection and place in
467 * p_buffer. Return the actual number of bytes read
468 *****************************************************************************/
469 static int ReadICYMeta( access_t *p_access );
470 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
472 access_sys_t *p_sys = p_access->p_sys;
477 p_access->info.b_eof = VLC_TRUE;
481 if( p_access->info.i_size > 0 &&
482 i_len + p_access->info.i_pos > p_access->info.i_size )
484 if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
486 p_access->info.b_eof = VLC_TRUE;
491 if( p_sys->b_chunked )
493 if( p_sys->i_chunk < 0 )
495 p_access->info.b_eof = VLC_TRUE;
499 if( p_sys->i_chunk <= 0 )
501 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
502 /* read the chunk header */
505 msg_Dbg( p_access, "failed reading chunk-header line" );
508 p_sys->i_chunk = strtoll( psz, NULL, 16 );
511 if( p_sys->i_chunk <= 0 ) /* eof */
514 p_access->info.b_eof = VLC_TRUE;
519 if( i_len > p_sys->i_chunk )
521 i_len = p_sys->i_chunk;
525 if( p_sys->b_continuous && i_len > p_sys->i_remaining )
527 /* Only ask for the remaining length */
528 int i_new_len = p_sys->i_remaining;
531 Request( p_access, 0 );
532 i_read = Read( p_access, p_buffer, i_len );
538 if( p_sys->i_icy_meta > 0 && p_access->info.i_pos > 0 )
540 int64_t i_next = p_sys->i_icy_meta -
541 p_access->info.i_pos % p_sys->i_icy_meta;
543 if( i_next == p_sys->i_icy_meta )
545 if( ReadICYMeta( p_access ) )
547 p_access->info.b_eof = VLC_TRUE;
555 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, VLC_FALSE );
559 p_access->info.i_pos += i_read;
561 if( p_sys->b_chunked )
563 p_sys->i_chunk -= i_read;
564 if( p_sys->i_chunk <= 0 )
566 /* read the empty line */
567 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
568 if( psz ) free( psz );
572 else if( i_read == 0 )
575 * I very much doubt that this will work.
576 * If i_read == 0, the connection *IS* dead, so the only
577 * sensible thing to do is Disconnect() and then retry.
578 * Otherwise, I got recv() completely wrong. -- Courmisch
580 if( p_sys->b_continuous )
582 Request( p_access, 0 );
583 p_sys->b_continuous = VLC_FALSE;
584 i_read = Read( p_access, p_buffer, i_len );
585 p_sys->b_continuous = VLC_TRUE;
587 Disconnect( p_access );
588 if( p_sys->b_reconnect )
590 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
591 if( Connect( p_access, p_access->info.i_pos ) )
593 msg_Dbg( p_access, "reconnection failed" );
597 p_sys->b_reconnect = VLC_FALSE;
598 i_read = Read( p_access, p_buffer, i_len );
599 p_sys->b_reconnect = VLC_TRUE;
603 if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
606 if( p_sys->b_continuous )
608 p_sys->i_remaining -= i_read;
614 static int ReadICYMeta( access_t *p_access )
616 access_sys_t *p_sys = p_access->p_sys;
623 /* Read meta data length */
624 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, buffer, 1,
633 msg_Dbg( p_access, "ICY meta size=%d", buffer[0] * 16);
635 psz_meta = malloc( buffer[0] * 16 + 1 );
636 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs,
637 psz_meta, buffer[0] * 16, VLC_TRUE );
639 if( i_read != buffer[0] * 16 )
642 psz_meta[buffer[0]*16 + 1] = '\0'; /* Just in case */
644 msg_Dbg( p_access, "icy-meta=%s", psz_meta );
646 /* Now parse the meta */
647 /* Look for StreamTitle= */
648 p = strcasestr( psz_meta, "StreamTitle=" );
651 p += strlen( "StreamTitle=" );
652 if( *p == '\'' || *p == '"' )
654 char *psz = strchr( &p[1], p[0] );
656 psz = strchr( &p[1], ';' );
658 if( psz ) *psz = '\0';
662 char *psz = strchr( &p[1], ';' );
663 if( psz ) *psz = '\0';
666 if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
668 p_sys->psz_icy_title = strdup( &p[1] );
670 p_access->info.i_update |= INPUT_UPDATE_META;
675 msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
680 /*****************************************************************************
681 * Seek: close and re-open a connection at the right place
682 *****************************************************************************/
683 static int Seek( access_t *p_access, int64_t i_pos )
685 msg_Dbg( p_access, "trying to seek to "I64Fd, i_pos );
687 Disconnect( p_access );
689 if( Connect( p_access, i_pos ) )
691 msg_Err( p_access, "seek failed" );
692 p_access->info.b_eof = VLC_TRUE;
698 /*****************************************************************************
700 *****************************************************************************/
701 static int Control( access_t *p_access, int i_query, va_list args )
703 access_sys_t *p_sys = p_access->p_sys;
707 vlc_meta_t **pp_meta;
712 case ACCESS_CAN_SEEK:
713 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
714 *pb_bool = p_sys->b_seekable;
716 case ACCESS_CAN_FASTSEEK:
717 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
718 *pb_bool = VLC_FALSE;
720 case ACCESS_CAN_PAUSE:
721 case ACCESS_CAN_CONTROL_PACE:
722 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
724 #if 0 /* Disable for now until we have a clock synchro algo
725 * which works with something else than MPEG over UDP */
726 *pb_bool = p_sys->b_pace_control;
733 pi_int = (int*)va_arg( args, int * );
737 case ACCESS_GET_PTS_DELAY:
738 pi_64 = (int64_t*)va_arg( args, int64_t * );
739 *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
743 case ACCESS_SET_PAUSE_STATE:
746 case ACCESS_GET_META:
747 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
748 *pp_meta = vlc_meta_New();
749 msg_Dbg( p_access, "GET META %s %s %s",
750 p_sys->psz_icy_name, p_sys->psz_icy_genre, p_sys->psz_icy_title );
751 if( p_sys->psz_icy_name )
752 vlc_meta_Add( *pp_meta, VLC_META_TITLE,
753 p_sys->psz_icy_name );
754 if( p_sys->psz_icy_genre )
755 vlc_meta_Add( *pp_meta, VLC_META_GENRE,
756 p_sys->psz_icy_genre );
757 if( p_sys->psz_icy_title )
758 vlc_meta_Add( *pp_meta, VLC_META_NOW_PLAYING,
759 p_sys->psz_icy_title );
762 case ACCESS_GET_TITLE_INFO:
763 case ACCESS_SET_TITLE:
764 case ACCESS_SET_SEEKPOINT:
765 case ACCESS_SET_PRIVATE_ID_STATE:
769 msg_Warn( p_access, "unimplemented query in control" );
776 /*****************************************************************************
777 * ParseURL: extract user:password
778 *****************************************************************************/
779 static void ParseURL( access_sys_t *p_sys, char *psz_url )
781 char *psz_dup = strdup( psz_url );
785 /* Syntax //[user:password]@<hostname>[:<port>][/<path>] */
793 if( ( p = strchr( psz, '@' ) ) )
798 comma = strchr( psz, ':' );
800 /* Retreive user:password */
805 p_sys->psz_user = strdup( psz );
806 p_sys->psz_passwd = strdup( comma );
810 p_sys->psz_user = strdup( psz );
819 vlc_UrlParse( &p_sys->url, p, 0 );
824 /*****************************************************************************
826 *****************************************************************************/
827 static int Connect( access_t *p_access, int64_t i_tell )
829 access_sys_t *p_sys = p_access->p_sys;
830 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
833 if( p_sys->psz_location ) free( p_sys->psz_location );
834 if( p_sys->psz_mime ) free( p_sys->psz_mime );
835 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
837 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
838 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
839 if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
842 p_sys->psz_location = NULL;
843 p_sys->psz_mime = NULL;
844 p_sys->psz_pragma = NULL;
845 p_sys->b_mms = VLC_FALSE;
846 p_sys->b_chunked = VLC_FALSE;
848 p_sys->i_icy_meta = 0;
849 p_sys->psz_icy_name = NULL;
850 p_sys->psz_icy_genre = NULL;
851 p_sys->psz_icy_title = NULL;
853 p_access->info.i_size = 0;
854 p_access->info.i_pos = i_tell;
855 p_access->info.b_eof = VLC_FALSE;
858 /* Open connection */
859 p_sys->fd = net_OpenTCP( p_access, srv.psz_host, srv.i_port );
862 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
866 /* Initialize TLS/SSL session */
867 /* FIXME: support proxy CONNECT for HTTP/SSL */
868 if( p_sys->b_ssl == VLC_TRUE )
872 msg_Err( p_access, "HTTP/SSL through HTTP proxy not supported yet" );
873 Disconnect( p_access );
877 p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd, NULL );
878 if( p_sys->p_tls == NULL )
880 msg_Err( p_access, "cannot establish HTTP/SSL session" );
881 Disconnect( p_access );
884 p_sys->p_vs = &p_sys->p_tls->sock;
887 return Request( p_access,i_tell );
891 static int Request( access_t *p_access, int64_t i_tell )
893 access_sys_t *p_sys = p_access->p_sys;
895 v_socket_t *pvs = p_sys->p_vs;
899 if( p_sys->url.psz_path )
901 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
902 "GET http://%s:%d%s HTTP/1.%d\r\n",
903 p_sys->url.psz_host, p_sys->url.i_port,
904 p_sys->url.psz_path, p_sys->i_version );
908 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
909 "GET http://%s:%d/ HTTP/1.%d\r\n",
910 p_sys->url.psz_host, p_sys->url.i_port,
916 char *psz_path = p_sys->url.psz_path;
917 if( !psz_path || !*psz_path )
921 if( p_sys->url.i_port != 80)
923 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
924 "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
925 psz_path, p_sys->i_version, p_sys->url.psz_host,
930 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
931 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
932 psz_path, p_sys->i_version, p_sys->url.psz_host );
936 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "User-Agent: %s\r\n",
937 p_sys->psz_user_agent );
939 if( p_sys->i_version == 1 )
941 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
942 "Range: bytes="I64Fd"-\r\n", i_tell );
945 /* Authentification */
946 if( p_sys->psz_user && *p_sys->psz_user )
951 asprintf( &buf, "%s:%s", p_sys->psz_user,
952 p_sys->psz_passwd ? p_sys->psz_passwd : "" );
954 b64 = vlc_b64_encode( buf );
957 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
958 "Authorization: Basic %s\r\n", b64 );
962 /* ICY meta data request */
963 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
966 if( p_sys->b_continuous && p_sys->i_version == 1 )
968 net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
969 "Connection: keep-alive\r\n" );
973 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
974 "Connection: Close\r\n");
975 p_sys->b_continuous = VLC_FALSE;
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 if( p_sys->i_code >= 400 )
1020 msg_Err( p_access, "error: %s", psz );
1028 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs );
1033 msg_Err( p_access, "failed to read answer" );
1037 /* msg_Dbg( p_input, "Line=%s", psz ); */
1045 if( ( p = strchr( psz, ':' ) ) == NULL )
1047 msg_Err( p_access, "malformed header line: %s", psz );
1052 while( *p == ' ' ) p++;
1054 if( !strcasecmp( psz, "Content-Length" ) )
1056 if( p_sys->b_continuous )
1058 p_access->info.i_size = -1;
1059 msg_Dbg( p_access, "this frame size="I64Fd, atoll(p ) );
1060 p_sys->i_remaining = atoll( p );
1064 p_access->info.i_size = i_tell + atoll( p );
1065 msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
1068 else if( !strcasecmp( psz, "Location" ) )
1070 if( p_sys->psz_location ) free( p_sys->psz_location );
1071 p_sys->psz_location = strdup( p );
1073 else if( !strcasecmp( psz, "Content-Type" ) )
1075 if( p_sys->psz_mime ) free( p_sys->psz_mime );
1076 p_sys->psz_mime = strdup( p );
1077 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1079 else if( !strcasecmp( psz, "Pragma" ) )
1081 if( !strcasecmp( psz, "Pragma: features" ) )
1082 p_sys->b_mms = VLC_TRUE;
1083 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
1084 p_sys->psz_pragma = strdup( p );
1085 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1087 else if( !strcasecmp( psz, "Server" ) )
1089 msg_Dbg( p_access, "Server: %s", p );
1090 if( !strncasecmp( p, "Icecast", 7 ) ||
1091 !strncasecmp( p, "Nanocaster", 10 ) )
1093 /* Remember if this is Icecast
1094 * we need to force demux in this case without breaking
1097 /* Let live 365 streams (nanocaster) piggyback on the icecast
1098 * routine. They look very similar */
1100 p_sys->b_reconnect = VLC_TRUE;
1101 p_sys->b_pace_control = VLC_FALSE;
1102 p_sys->b_icecast = VLC_TRUE;
1105 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1107 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1108 if( !strncasecmp( p, "chunked", 7 ) )
1110 p_sys->b_chunked = VLC_TRUE;
1113 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1115 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1116 p_sys->i_icy_meta = atoi( p );
1117 if( p_sys->i_icy_meta < 0 )
1118 p_sys->i_icy_meta = 0;
1120 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1122 else if( !strcasecmp( psz, "Icy-Name" ) )
1124 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
1125 p_sys->psz_icy_name = strdup( p );
1126 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1128 p_sys->b_icecast = VLC_TRUE; /* be on the safeside. set it here as well. */
1129 p_sys->b_reconnect = VLC_TRUE;
1130 p_sys->b_pace_control = VLC_FALSE;
1132 else if( !strcasecmp( psz, "Icy-Genre" ) )
1134 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
1135 p_sys->psz_icy_genre = strdup( p );
1136 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1138 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1140 msg_Dbg( p_access, "Icy-Notice: %s", p );
1142 else if( !strncasecmp( psz, "icy-", 4 ) ||
1143 !strncasecmp( psz, "ice-", 4 ) ||
1144 !strncasecmp( psz, "x-audiocast", 11 ) )
1146 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1154 Disconnect( p_access );
1155 return VLC_EGENERIC;
1158 /*****************************************************************************
1160 *****************************************************************************/
1161 static void Disconnect( access_t *p_access )
1163 access_sys_t *p_sys = p_access->p_sys;
1165 if( p_sys->p_tls != NULL)
1167 tls_ClientDelete( p_sys->p_tls );
1168 p_sys->p_tls = NULL;
1171 if( p_sys->fd != -1)
1173 net_Close(p_sys->fd);