1 /*****************************************************************************
2 * http.c: HTTP input module
3 *****************************************************************************
4 * Copyright (C) 2001-2004 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"
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Open ( vlc_object_t * );
41 static void Close( vlc_object_t * );
43 #define PROXY_TEXT N_("HTTP proxy")
44 #define PROXY_LONGTEXT N_( \
45 "You can specify an HTTP proxy to use. It must be of the form " \
46 "http://myproxy.mydomain:myport/. If none is specified, the HTTP_PROXY " \
47 "environment variable will be tried." )
49 #define CACHING_TEXT N_("Caching value in ms")
50 #define CACHING_LONGTEXT N_( \
51 "Allows you to modify the default caching value for http streams. This " \
52 "value should be set in millisecond units." )
54 #define USER_TEXT N_("HTTP user name")
55 #define USER_LONGTEXT N_("Allows you to modify the user name that will " \
56 "be used for the connection (Basic authentication only).")
58 #define PASS_TEXT N_("HTTP password")
59 #define PASS_LONGTEXT N_("Allows you to modify the password that will be " \
60 "used for the connection.")
62 #define AGENT_TEXT N_("HTTP user agent")
63 #define AGENT_LONGTEXT N_("Allows you to modify the user agent that will be " \
64 "used for the connection.")
66 #define RECONNECT_TEXT N_("Auto re-connect")
67 #define RECONNECT_LONGTEXT N_("Will automatically attempt a re-connection " \
68 "in case it was untimely closed.")
70 #define CONTINUOUS_TEXT N_("Continuous stream")
71 #define CONTINUOUS_LONGTEXT N_("Enable this option to read a file that is " \
72 "being constantly updated (for example, a JPG file on a server)")
75 set_description( _("HTTP input") );
76 set_capability( "access2", 0 );
77 set_shortname( _( "HTTP/HTTPS" ) );
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", NULL, NULL, USER_TEXT, USER_LONGTEXT, VLC_FALSE );
86 add_string( "http-pwd", NULL , NULL, PASS_TEXT, PASS_LONGTEXT, VLC_FALSE );
87 add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
88 AGENT_LONGTEXT, VLC_FALSE );
89 add_bool( "http-reconnect", 0, NULL, RECONNECT_TEXT,
90 RECONNECT_LONGTEXT, VLC_TRUE );
91 add_bool( "http-continuous", 0, NULL, CONTINUOUS_TEXT,
92 CONTINUOUS_LONGTEXT, VLC_TRUE );
94 add_shortcut( "http" );
95 add_shortcut( "http4" );
96 add_shortcut( "http6" );
97 add_shortcut( "unsv" );
98 set_callbacks( Open, Close );
101 /*****************************************************************************
103 *****************************************************************************/
112 char *psz_user_agent;
127 vlc_bool_t b_icecast;
129 vlc_bool_t b_chunked;
139 vlc_bool_t b_seekable;
140 vlc_bool_t b_reconnect;
141 vlc_bool_t b_continuous;
142 vlc_bool_t b_pace_control;
146 static int Read( access_t *, uint8_t *, int );
147 static int Seek( access_t *, int64_t );
148 static int Control( access_t *, int, va_list );
151 static void ParseURL( access_sys_t *, char *psz_url );
152 static int Connect( access_t *, int64_t );
153 static int Request( access_t *p_access, int64_t i_tell );
155 /*****************************************************************************
157 *****************************************************************************/
158 static int Open( vlc_object_t *p_this )
160 access_t *p_access = (access_t*)p_this;
164 /* First set ipv4/ipv6 */
165 var_Create( p_access, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
166 var_Create( p_access, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
168 if( *p_access->psz_access )
171 /* Find out which shortcut was used */
172 if( !strncmp( p_access->psz_access, "http4", 6 ) )
174 val.b_bool = VLC_TRUE;
175 var_Set( p_access, "ipv4", val );
177 val.b_bool = VLC_FALSE;
178 var_Set( p_access, "ipv6", val );
180 else if( !strncmp( p_access->psz_access, "http6", 6 ) )
182 val.b_bool = VLC_TRUE;
183 var_Set( p_access, "ipv6", val );
185 val.b_bool = VLC_FALSE;
186 var_Set( p_access, "ipv4", val );
190 /* Set up p_access */
191 p_access->pf_read = Read;
192 p_access->pf_block = NULL;
193 p_access->pf_control = Control;
194 p_access->pf_seek = Seek;
195 p_access->info.i_update = 0;
196 p_access->info.i_size = 0;
197 p_access->info.i_pos = 0;
198 p_access->info.b_eof = VLC_FALSE;
199 p_access->info.i_title = 0;
200 p_access->info.i_seekpoint = 0;
201 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
202 memset( p_sys, 0, sizeof( access_sys_t ) );
204 p_sys->b_proxy = VLC_FALSE;
205 p_sys->i_version = 1;
206 p_sys->b_seekable = VLC_TRUE;
207 p_sys->psz_mime = NULL;
208 p_sys->psz_pragma = NULL;
209 p_sys->b_mms = VLC_FALSE;
210 p_sys->b_icecast = VLC_FALSE;
211 p_sys->psz_location = NULL;
212 p_sys->psz_user_agent = NULL;
213 p_sys->b_pace_control = VLC_TRUE;
214 p_sys->i_icy_meta = 0;
215 p_sys->psz_icy_name = NULL;
216 p_sys->psz_icy_genre = NULL;
217 p_sys->psz_icy_title = NULL;
218 p_sys->i_remaining = 0;
221 ParseURL( p_sys, p_access->psz_path );
222 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
224 msg_Warn( p_access, "invalid host" );
227 if( p_sys->url.i_port <= 0 )
229 p_sys->url.i_port = 80;
231 if( !p_sys->psz_user || *p_sys->psz_user == '\0' )
233 p_sys->psz_user = var_CreateGetString( p_access, "http-user" );
234 p_sys->psz_passwd = var_CreateGetString( p_access, "http-pwd" );
238 p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
241 psz = var_CreateGetString( p_access, "http-proxy" );
244 p_sys->b_proxy = VLC_TRUE;
245 vlc_UrlParse( &p_sys->proxy, psz, 0 );
249 char *psz_proxy = getenv( "http_proxy" );
250 if( psz_proxy && *psz_proxy )
252 p_sys->b_proxy = VLC_TRUE;
253 vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
262 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
264 msg_Warn( p_access, "invalid proxy host" );
267 if( p_sys->proxy.i_port <= 0 )
269 p_sys->proxy.i_port = 80;
273 msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
274 p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
277 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
278 p_sys->proxy.i_port );
280 if( p_sys->psz_user && *p_sys->psz_user )
282 msg_Dbg( p_access, " user='%s', pwd='%s'",
283 p_sys->psz_user, p_sys->psz_passwd );
286 p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
287 p_sys->b_continuous = var_CreateGetBool( p_access, "http-continuous" );
290 if( Connect( p_access, 0 ) )
292 /* Retry with http 1.0 */
293 p_sys->i_version = 0;
295 if( p_access->b_die ||
296 Connect( p_access, 0 ) )
302 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
303 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
304 p_sys->psz_location && *p_sys->psz_location )
306 playlist_t * p_playlist;
307 input_item_t *p_input_item;
309 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
311 p_playlist = vlc_object_find( p_access, VLC_OBJECT_PLAYLIST,
315 msg_Err( p_access, "redirection failed: can't find playlist" );
320 vlc_mutex_lock( &p_playlist->object_lock );
321 p_input_item = &p_playlist->status.p_item->input;
322 vlc_mutex_lock( &p_input_item->lock );
323 free( p_input_item->psz_uri );
324 free( p_access->psz_path );
325 p_input_item->psz_uri = strdup( p_sys->psz_location );
326 p_access->psz_path = strdup( p_sys->psz_location );
327 vlc_mutex_unlock( &p_input_item->lock );
328 vlc_mutex_unlock( &p_playlist->object_lock );
329 vlc_object_release( p_playlist );
331 /* Clean up current Open() run */
332 vlc_UrlClean( &p_sys->url );
333 vlc_UrlClean( &p_sys->proxy );
334 if( p_sys->psz_mime ) free( p_sys->psz_mime );
335 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
336 if( p_sys->psz_location ) free( p_sys->psz_location );
337 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
338 if( p_sys->psz_user ) free( p_sys->psz_user );
339 if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
343 net_Close( p_sys->fd );
347 /* Do new Open() run with new data */
348 return Open( p_this );
353 msg_Dbg( p_access, "This is actually a live mms server, BAIL" );
357 if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
359 if( p_sys->psz_mime && !strcasecmp( p_sys->psz_mime, "video/nsv" ) )
360 p_access->psz_demux = strdup( "nsv" );
361 else if( p_sys->psz_mime &&
362 ( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
363 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) ) )
364 p_access->psz_demux = strdup( "m4a" );
365 else if( p_sys->psz_mime && !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
366 p_access->psz_demux = strdup( "mp3" );
367 else /* assume mp3, though this is definetly not certain */
368 p_access->psz_demux = strdup( "mp3" );
370 msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
371 p_access->psz_demux );
373 #if 0 /* Doesn't work really well because of the pre-buffering in shoutcast
374 * servers (the buffer content will be sent as fast as possible). */
375 p_sys->b_pace_control = VLC_FALSE;
379 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
382 var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
387 vlc_UrlClean( &p_sys->url );
388 vlc_UrlClean( &p_sys->proxy );
389 if( p_sys->psz_mime ) free( p_sys->psz_mime );
390 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
391 if( p_sys->psz_location ) free( p_sys->psz_location );
392 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
393 if( p_sys->psz_user ) free( p_sys->psz_user );
394 if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
398 net_Close( p_sys->fd );
404 /*****************************************************************************
406 *****************************************************************************/
407 static void Close( vlc_object_t *p_this )
409 access_t *p_access = (access_t*)p_this;
410 access_sys_t *p_sys = p_access->p_sys;
412 vlc_UrlClean( &p_sys->url );
413 vlc_UrlClean( &p_sys->proxy );
415 if( p_sys->psz_user ) free( p_sys->psz_user );
416 if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
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 );
422 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
423 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
424 if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
426 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
430 net_Close( p_sys->fd );
435 /*****************************************************************************
436 * Read: Read up to i_len bytes from the http connection and place in
437 * p_buffer. Return the actual number of bytes read
438 *****************************************************************************/
439 static int ReadICYMeta( access_t *p_access );
440 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
442 access_sys_t *p_sys = p_access->p_sys;
447 p_access->info.b_eof = VLC_TRUE;
451 if( p_access->info.i_size > 0 &&
452 i_len + p_access->info.i_pos > p_access->info.i_size )
454 if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
456 p_access->info.b_eof = VLC_TRUE;
461 if( p_sys->b_chunked )
463 if( p_sys->i_chunk < 0 )
465 p_access->info.b_eof = VLC_TRUE;
469 if( p_sys->i_chunk <= 0 )
471 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
472 /* read the chunk header */
475 msg_Dbg( p_access, "failed reading chunk-header line" );
478 p_sys->i_chunk = strtoll( psz, NULL, 16 );
481 if( p_sys->i_chunk <= 0 ) /* eof */
484 p_access->info.b_eof = VLC_TRUE;
489 if( i_len > p_sys->i_chunk )
491 i_len = p_sys->i_chunk;
495 if( p_sys->b_continuous && i_len > p_sys->i_remaining )
497 /* Only ask for the remaining length */
498 int i_new_len = p_sys->i_remaining;
501 Request( p_access, 0 );
502 i_read = Read( p_access, p_buffer, i_len );
508 if( p_sys->i_icy_meta > 0 && p_access->info.i_pos > 0 )
510 int64_t i_next = p_sys->i_icy_meta -
511 p_access->info.i_pos % p_sys->i_icy_meta;
513 if( i_next == p_sys->i_icy_meta )
515 if( ReadICYMeta( p_access ) )
517 p_access->info.b_eof = VLC_TRUE;
525 i_read = net_Read( p_access, p_sys->fd, NULL, p_buffer, i_len, VLC_FALSE );
529 p_access->info.i_pos += i_read;
531 if( p_sys->b_chunked )
533 p_sys->i_chunk -= i_read;
534 if( p_sys->i_chunk <= 0 )
536 /* read the empty line */
537 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
538 if( psz ) free( psz );
542 else if( i_read == 0 )
544 if( p_sys->b_continuous )
546 Request( p_access, 0 );
547 p_sys->b_continuous = VLC_FALSE;
548 i_read = Read( p_access, p_buffer, i_len );
549 p_sys->b_continuous = VLC_TRUE;
551 if( p_sys->b_reconnect )
553 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
554 net_Close( p_sys->fd ); p_sys->fd = -1;
555 if( Connect( p_access, p_access->info.i_pos ) )
557 msg_Dbg( p_access, "reconnection failed" );
561 p_sys->b_reconnect = VLC_FALSE;
562 i_read = Read( p_access, p_buffer, i_len );
563 p_sys->b_reconnect = VLC_TRUE;
567 if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
570 if( p_sys->b_continuous )
572 p_sys->i_remaining -= i_read;
578 static int ReadICYMeta( access_t *p_access )
580 access_sys_t *p_sys = p_access->p_sys;
587 /* Read meta data length */
588 i_read = net_Read( p_access, p_sys->fd, NULL, buffer, 1, VLC_TRUE );
596 msg_Dbg( p_access, "ICY meta size=%d", buffer[0] * 16);
598 psz_meta = malloc( buffer[0] * 16 + 1 );
599 i_read = net_Read( p_access, p_sys->fd, NULL,
600 psz_meta, buffer[0] * 16, VLC_TRUE );
602 if( i_read != buffer[0] * 16 )
605 psz_meta[buffer[0]*16 + 1] = '\0'; /* Just in case */
607 msg_Dbg( p_access, "icy-meta=%s", psz_meta );
609 /* Now parse the meta */
610 /* Look for StreamTitle= */
611 p = strcasestr( psz_meta, "StreamTitle=" );
614 p += strlen( "StreamTitle=" );
615 if( *p == '\'' || *p == '"' )
617 char *psz = strchr( &p[1], p[0] );
619 psz = strchr( &p[1], ';' );
621 if( psz ) *psz = '\0';
625 char *psz = strchr( &p[1], ';' );
626 if( psz ) *psz = '\0';
629 if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
631 p_sys->psz_icy_title = strdup( &p[1] );
633 p_access->info.i_update |= INPUT_UPDATE_META;
638 msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
643 /*****************************************************************************
644 * Seek: close and re-open a connection at the right place
645 *****************************************************************************/
646 static int Seek( access_t *p_access, int64_t i_pos )
648 access_sys_t *p_sys = p_access->p_sys;
650 msg_Dbg( p_access, "trying to seek to "I64Fd, i_pos );
652 net_Close( p_sys->fd ); p_sys->fd = -1;
654 if( Connect( p_access, i_pos ) )
656 msg_Err( p_access, "seek failed" );
657 p_access->info.b_eof = VLC_TRUE;
663 /*****************************************************************************
665 *****************************************************************************/
666 static int Control( access_t *p_access, int i_query, va_list args )
668 access_sys_t *p_sys = p_access->p_sys;
672 vlc_meta_t **pp_meta;
677 case ACCESS_CAN_SEEK:
678 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
679 *pb_bool = p_sys->b_seekable;
681 case ACCESS_CAN_FASTSEEK:
682 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
683 *pb_bool = VLC_FALSE;
685 case ACCESS_CAN_PAUSE:
686 case ACCESS_CAN_CONTROL_PACE:
687 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
688 *pb_bool = p_sys->b_pace_control;
693 pi_int = (int*)va_arg( args, int * );
697 case ACCESS_GET_PTS_DELAY:
698 pi_64 = (int64_t*)va_arg( args, int64_t * );
699 *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
703 case ACCESS_SET_PAUSE_STATE:
706 case ACCESS_GET_META:
707 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
708 *pp_meta = vlc_meta_New();
709 msg_Dbg( p_access, "GET META %s %s %s",
710 p_sys->psz_icy_name, p_sys->psz_icy_genre, p_sys->psz_icy_title );
711 if( p_sys->psz_icy_name )
712 vlc_meta_Add( *pp_meta, VLC_META_DESCRIPTION,
713 p_sys->psz_icy_name );
714 if( p_sys->psz_icy_genre )
715 vlc_meta_Add( *pp_meta, VLC_META_GENRE,
716 p_sys->psz_icy_genre );
717 if( p_sys->psz_icy_title )
718 vlc_meta_Add( *pp_meta, VLC_META_TITLE,
719 p_sys->psz_icy_title );
722 case ACCESS_GET_TITLE_INFO:
723 case ACCESS_SET_TITLE:
724 case ACCESS_SET_SEEKPOINT:
725 case ACCESS_SET_PRIVATE_ID_STATE:
729 msg_Warn( p_access, "unimplemented query in control" );
736 /*****************************************************************************
737 * ParseURL: extract user:password
738 *****************************************************************************/
739 static void ParseURL( access_sys_t *p_sys, char *psz_url )
741 char *psz_dup = strdup( psz_url );
745 /* Syntax //[user:password]@<hostname>[:<port>][/<path>] */
753 if( ( p = strchr( psz, '@' ) ) )
758 comma = strchr( psz, ':' );
760 /* Retreive user:password */
765 p_sys->psz_user = strdup( psz );
766 p_sys->psz_passwd = strdup( comma );
770 p_sys->psz_user = strdup( psz );
779 vlc_UrlParse( &p_sys->url, p, 0 );
784 /*****************************************************************************
786 *****************************************************************************/
787 static int Connect( access_t *p_access, int64_t i_tell )
789 access_sys_t *p_sys = p_access->p_sys;
790 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
793 if( p_sys->psz_location ) free( p_sys->psz_location );
794 if( p_sys->psz_mime ) free( p_sys->psz_mime );
795 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
797 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
798 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
799 if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
802 p_sys->psz_location = NULL;
803 p_sys->psz_mime = NULL;
804 p_sys->psz_pragma = NULL;
805 p_sys->b_mms = VLC_FALSE;
806 p_sys->b_chunked = VLC_FALSE;
808 p_sys->i_icy_meta = 0;
809 p_sys->psz_icy_name = NULL;
810 p_sys->psz_icy_genre = NULL;
811 p_sys->psz_icy_title = NULL;
813 p_access->info.i_size = 0;
814 p_access->info.i_pos = i_tell;
815 p_access->info.b_eof = VLC_FALSE;
818 /* Open connection */
819 p_sys->fd = net_OpenTCP( p_access, srv.psz_host, srv.i_port );
822 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
826 return Request( p_access,i_tell );
830 static int Request( access_t *p_access, int64_t i_tell )
832 access_sys_t *p_sys = p_access->p_sys;
836 if( p_sys->url.psz_path )
838 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
839 "GET http://%s:%d%s HTTP/1.%d\r\n",
840 p_sys->url.psz_host, p_sys->url.i_port,
841 p_sys->url.psz_path, p_sys->i_version );
845 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
846 "GET http://%s:%d/ HTTP/1.%d\r\n",
847 p_sys->url.psz_host, p_sys->url.i_port,
853 char *psz_path = p_sys->url.psz_path;
854 if( !psz_path || !*psz_path )
858 if( p_sys->url.i_port != 80)
860 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
861 "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
862 psz_path, p_sys->i_version, p_sys->url.psz_host,
867 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
868 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
869 psz_path, p_sys->i_version, p_sys->url.psz_host );
873 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "User-Agent: %s\r\n",
874 p_sys->psz_user_agent );
876 if( p_sys->i_version == 1 )
878 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
879 "Range: bytes="I64Fd"-\r\n", i_tell );
882 /* Authentification */
883 if( p_sys->psz_user && *p_sys->psz_user )
888 asprintf( &buf, "%s:%s", p_sys->psz_user,
889 p_sys->psz_passwd ? p_sys->psz_passwd : "" );
891 b64 = vlc_b64_encode( buf );
893 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
894 "Authorization: Basic %s\r\n", b64 );
898 /* ICY meta data request */
899 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "Icy-MetaData: 1\r\n" );
902 if( p_sys->b_continuous && p_sys->i_version == 1 )
904 net_Printf( VLC_OBJECT( p_access ), p_sys->fd, NULL,
905 "Connection: keep-alive\r\n" );
909 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
910 "Connection: Close\r\n");
911 p_sys->b_continuous = VLC_FALSE;
914 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "\r\n" ) < 0 )
916 msg_Err( p_access, "failed to send request" );
917 net_Close( p_sys->fd ); p_sys->fd = -1;
922 if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL ) ) == NULL )
924 msg_Err( p_access, "failed to read answer" );
927 if( !strncmp( psz, "HTTP/1.", 7 ) )
929 p_sys->psz_protocol = "HTTP";
930 p_sys->i_code = atoi( &psz[9] );
932 else if( !strncmp( psz, "ICY", 3 ) )
934 p_sys->psz_protocol = "ICY";
935 p_sys->i_code = atoi( &psz[4] );
936 p_sys->b_reconnect = VLC_TRUE;
940 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
944 msg_Dbg( p_access, "protocol '%s' answer code %d",
945 p_sys->psz_protocol, p_sys->i_code );
946 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
948 p_sys->b_seekable = VLC_FALSE;
950 if( p_sys->i_code != 206 )
952 p_sys->b_seekable = VLC_FALSE;
954 if( p_sys->i_code >= 400 )
956 msg_Err( p_access, "error: %s", psz );
964 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
969 msg_Err( p_access, "failed to read answer" );
973 /* msg_Dbg( p_input, "Line=%s", psz ); */
981 if( ( p = strchr( psz, ':' ) ) == NULL )
983 msg_Err( p_access, "malformed header line: %s", psz );
988 while( *p == ' ' ) p++;
990 if( !strcasecmp( psz, "Content-Length" ) )
992 if( p_sys->b_continuous )
994 p_access->info.i_size = -1;
995 msg_Dbg( p_access, "this frame size="I64Fd, atoll(p ) );
996 p_sys->i_remaining = atoll( p );
1000 p_access->info.i_size = i_tell + atoll( p );
1001 msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
1004 else if( !strcasecmp( psz, "Location" ) )
1006 if( p_sys->psz_location ) free( p_sys->psz_location );
1007 p_sys->psz_location = strdup( p );
1009 else if( !strcasecmp( psz, "Content-Type" ) )
1011 if( p_sys->psz_mime ) free( p_sys->psz_mime );
1012 p_sys->psz_mime = strdup( p );
1013 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1015 else if( !strcasecmp( psz, "Pragma" ) )
1017 if( !strcasecmp( psz, "Pragma: features" ) )
1018 p_sys->b_mms = VLC_TRUE;
1019 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
1020 p_sys->psz_pragma = strdup( p );
1021 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1023 else if( !strcasecmp( psz, "Server" ) )
1025 msg_Dbg( p_access, "Server: %s", p );
1026 if( !strncasecmp( p, "Icecast", 7 ) ||
1027 !strncasecmp( p, "Nanocaster", 10 ) )
1029 /* Remember if this is Icecast
1030 * we need to force demux in this case without breaking
1033 /* Let live 365 streams (nanocaster) piggyback on the icecast
1034 * routine. They look very similar */
1036 p_sys->b_reconnect = VLC_TRUE;
1037 p_sys->b_pace_control = VLC_FALSE;
1038 p_sys->b_icecast = VLC_TRUE;
1041 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1043 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1044 if( !strncasecmp( p, "chunked", 7 ) )
1046 p_sys->b_chunked = VLC_TRUE;
1049 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1051 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1052 p_sys->i_icy_meta = atoi( p );
1053 if( p_sys->i_icy_meta < 0 )
1054 p_sys->i_icy_meta = 0;
1056 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1058 else if( !strcasecmp( psz, "Icy-Name" ) )
1060 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
1061 p_sys->psz_icy_name = strdup( p );
1062 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1064 p_sys->b_icecast = VLC_TRUE; /* be on the safeside. set it here as well. */
1065 p_sys->b_reconnect = VLC_TRUE;
1066 p_sys->b_pace_control = VLC_FALSE;
1068 else if( !strcasecmp( psz, "Icy-Genre" ) )
1070 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
1071 p_sys->psz_icy_genre = strdup( p );
1072 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1074 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1076 msg_Dbg( p_access, "Icy-Notice: %s", p );
1078 else if( !strncasecmp( psz, "icy-", 4 ) ||
1079 !strncasecmp( psz, "ice-", 4 ) ||
1080 !strncasecmp( psz, "x-audiocast", 11 ) )
1082 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1090 net_Close( p_sys->fd ); p_sys->fd = -1;
1091 return VLC_EGENERIC;