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_name( _( "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;
308 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
310 p_playlist = vlc_object_find( p_access, VLC_OBJECT_PLAYLIST,
314 msg_Err( p_access, "redirection failed: can't find playlist" );
317 p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
318 playlist_Add( p_playlist, p_sys->psz_location, p_sys->psz_location,
320 p_playlist->i_index + 1 );
321 vlc_object_release( p_playlist );
323 p_access->info.i_size = 0; /* Force to stop reading */
328 msg_Dbg( p_access, "This is actually a live mms server, BAIL" );
332 if( p_sys->b_icecast )
334 if( p_sys->psz_mime && !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
335 p_access->psz_demux = strdup( "mp3" );
338 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
340 if( p_sys->psz_mime && !strcasecmp( p_sys->psz_mime, "video/nsv" ) )
341 p_access->psz_demux = strdup( "nsv" );
342 else if( p_sys->psz_mime &&
343 ( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
344 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) ) )
345 p_access->psz_demux = strdup( "m4a" );
347 p_access->psz_demux = strdup( "mp3" );
349 msg_Info( p_access, "ICY server found, %s demuxer selected",
350 p_access->psz_demux );
352 #if 0 /* Doesn't work really well because of the pre-buffering in shoutcast
353 * servers (the buffer content will be sent as fast as possible). */
354 p_sys->b_pace_control = VLC_FALSE;
358 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
361 var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
366 vlc_UrlClean( &p_sys->url );
367 vlc_UrlClean( &p_sys->proxy );
368 if( p_sys->psz_mime ) free( p_sys->psz_mime );
369 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
370 if( p_sys->psz_location ) free( p_sys->psz_location );
371 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
372 if( p_sys->psz_user ) free( p_sys->psz_user );
373 if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
377 net_Close( p_sys->fd );
383 /*****************************************************************************
385 *****************************************************************************/
386 static void Close( vlc_object_t *p_this )
388 access_t *p_access = (access_t*)p_this;
389 access_sys_t *p_sys = p_access->p_sys;
391 vlc_UrlClean( &p_sys->url );
392 vlc_UrlClean( &p_sys->proxy );
394 if( p_sys->psz_user ) free( p_sys->psz_user );
395 if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
397 if( p_sys->psz_mime ) free( p_sys->psz_mime );
398 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
399 if( p_sys->psz_location ) free( p_sys->psz_location );
401 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
402 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
403 if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
405 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
409 net_Close( p_sys->fd );
414 /*****************************************************************************
415 * Read: Read up to i_len bytes from the http connection and place in
416 * p_buffer. Return the actual number of bytes read
417 *****************************************************************************/
418 static int ReadICYMeta( access_t *p_access );
419 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
421 access_sys_t *p_sys = p_access->p_sys;
426 p_access->info.b_eof = VLC_TRUE;
430 if( p_access->info.i_size > 0 &&
431 i_len + p_access->info.i_pos > p_access->info.i_size )
433 if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
435 p_access->info.b_eof = VLC_TRUE;
440 if( p_sys->b_chunked )
442 if( p_sys->i_chunk < 0 )
444 p_access->info.b_eof = VLC_TRUE;
448 if( p_sys->i_chunk <= 0 )
450 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
451 /* read the chunk header */
454 msg_Dbg( p_access, "failed reading chunk-header line" );
457 p_sys->i_chunk = strtoll( psz, NULL, 16 );
460 if( p_sys->i_chunk <= 0 ) /* eof */
463 p_access->info.b_eof = VLC_TRUE;
468 if( i_len > p_sys->i_chunk )
470 i_len = p_sys->i_chunk;
474 if( p_sys->b_continuous && i_len > p_sys->i_remaining )
476 /* Only ask for the remaining length */
477 int i_new_len = p_sys->i_remaining;
480 Request( p_access, 0 );
481 i_read = Read( p_access, p_buffer, i_len );
487 if( p_sys->i_icy_meta > 0 && p_access->info.i_pos > 0 )
489 int64_t i_next = p_sys->i_icy_meta -
490 p_access->info.i_pos % p_sys->i_icy_meta;
492 if( i_next == p_sys->i_icy_meta )
494 if( ReadICYMeta( p_access ) )
496 p_access->info.b_eof = VLC_TRUE;
504 i_read = net_Read( p_access, p_sys->fd, NULL, p_buffer, i_len, VLC_FALSE );
508 p_access->info.i_pos += i_read;
510 if( p_sys->b_chunked )
512 p_sys->i_chunk -= i_read;
513 if( p_sys->i_chunk <= 0 )
515 /* read the empty line */
516 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
517 if( psz ) free( psz );
521 else if( i_read == 0 )
523 if( p_sys->b_continuous )
525 Request( p_access, 0 );
526 p_sys->b_continuous = VLC_FALSE;
527 i_read = Read( p_access, p_buffer, i_len );
528 p_sys->b_continuous = VLC_TRUE;
530 if( p_sys->b_reconnect )
532 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
533 net_Close( p_sys->fd ); p_sys->fd = -1;
534 if( Connect( p_access, p_access->info.i_pos ) )
536 msg_Dbg( p_access, "reconnection failed" );
540 p_sys->b_reconnect = VLC_FALSE;
541 i_read = Read( p_access, p_buffer, i_len );
542 p_sys->b_reconnect = VLC_TRUE;
546 if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
549 if( p_sys->b_continuous )
551 p_sys->i_remaining -= i_read;
557 static int ReadICYMeta( access_t *p_access )
559 access_sys_t *p_sys = p_access->p_sys;
566 /* Read meta data length */
567 i_read = net_Read( p_access, p_sys->fd, NULL, buffer, 1, VLC_TRUE );
575 msg_Dbg( p_access, "ICY meta size=%d", buffer[0] * 16);
577 psz_meta = malloc( buffer[0] * 16 + 1 );
578 i_read = net_Read( p_access, p_sys->fd, NULL,
579 psz_meta, buffer[0] * 16, VLC_TRUE );
581 if( i_read != buffer[0] * 16 )
584 psz_meta[buffer[0]*16 + 1] = '\0'; /* Just in case */
586 msg_Warn( p_access, "icy-meta=%s", psz_meta );
588 /* Now parse the meta */
589 /* Look for StreamTitle= */
590 p = strcasestr( psz_meta, "StreamTitle=" );
593 p += strlen( "StreamTitle=" );
594 if( *p == '\'' || *p == '"' )
596 char *psz = strchr( &p[1], p[0] );
598 psz = strchr( &p[1], ';' );
600 if( psz ) *psz = '\0';
604 char *psz = strchr( &p[1], ';' );
605 if( psz ) *psz = '\0';
608 if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
610 p_sys->psz_icy_title = strdup( &p[1] );
612 p_access->info.i_update |= INPUT_UPDATE_META;
617 msg_Warn( p_access, "New Title=%s", p_sys->psz_icy_title );
622 /*****************************************************************************
623 * Seek: close and re-open a connection at the right place
624 *****************************************************************************/
625 static int Seek( access_t *p_access, int64_t i_pos )
627 access_sys_t *p_sys = p_access->p_sys;
629 msg_Dbg( p_access, "trying to seek to "I64Fd, i_pos );
631 net_Close( p_sys->fd ); p_sys->fd = -1;
633 if( Connect( p_access, i_pos ) )
635 msg_Err( p_access, "seek failed" );
636 p_access->info.b_eof = VLC_TRUE;
642 /*****************************************************************************
644 *****************************************************************************/
645 static int Control( access_t *p_access, int i_query, va_list args )
647 access_sys_t *p_sys = p_access->p_sys;
651 vlc_meta_t **pp_meta;
656 case ACCESS_CAN_SEEK:
657 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
658 *pb_bool = p_sys->b_seekable;
660 case ACCESS_CAN_FASTSEEK:
661 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
662 *pb_bool = VLC_FALSE;
664 case ACCESS_CAN_PAUSE:
665 case ACCESS_CAN_CONTROL_PACE:
666 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
667 *pb_bool = p_sys->b_pace_control;
672 pi_int = (int*)va_arg( args, int * );
676 case ACCESS_GET_PTS_DELAY:
677 pi_64 = (int64_t*)va_arg( args, int64_t * );
678 *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
682 case ACCESS_SET_PAUSE_STATE:
685 case ACCESS_GET_META:
686 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
687 *pp_meta = vlc_meta_New();
688 msg_Dbg( p_access, "GET META %s %s %s",
689 p_sys->psz_icy_name, p_sys->psz_icy_genre, p_sys->psz_icy_title );
690 if( p_sys->psz_icy_name )
691 vlc_meta_Add( *pp_meta, VLC_META_DESCRIPTION,
692 p_sys->psz_icy_name );
693 if( p_sys->psz_icy_genre )
694 vlc_meta_Add( *pp_meta, VLC_META_GENRE,
695 p_sys->psz_icy_genre );
696 if( p_sys->psz_icy_title )
697 vlc_meta_Add( *pp_meta, VLC_META_TITLE,
698 p_sys->psz_icy_title );
701 case ACCESS_GET_TITLE_INFO:
702 case ACCESS_SET_TITLE:
703 case ACCESS_SET_SEEKPOINT:
704 case ACCESS_SET_PRIVATE_ID_STATE:
708 msg_Warn( p_access, "unimplemented query in control" );
715 /*****************************************************************************
716 * ParseURL: extract user:password
717 *****************************************************************************/
718 static void ParseURL( access_sys_t *p_sys, char *psz_url )
720 char *psz_dup = strdup( psz_url );
724 /* Syntax //[user:password]@<hostname>[:<port>][/<path>] */
732 if( ( p = strchr( psz, '@' ) ) )
737 comma = strchr( psz, ':' );
739 /* Retreive user:password */
744 p_sys->psz_user = strdup( psz );
745 p_sys->psz_passwd = strdup( comma );
749 p_sys->psz_user = strdup( psz );
758 vlc_UrlParse( &p_sys->url, p, 0 );
763 /*****************************************************************************
765 *****************************************************************************/
766 static int Connect( access_t *p_access, int64_t i_tell )
768 access_sys_t *p_sys = p_access->p_sys;
769 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
773 if( p_sys->psz_location ) free( p_sys->psz_location );
774 if( p_sys->psz_mime ) free( p_sys->psz_mime );
775 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
777 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
778 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
779 if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
782 p_sys->psz_location = NULL;
783 p_sys->psz_mime = NULL;
784 p_sys->psz_pragma = NULL;
785 p_sys->b_mms = VLC_FALSE;
786 p_sys->b_chunked = VLC_FALSE;
788 p_sys->i_icy_meta = 0;
789 p_sys->psz_icy_name = NULL;
790 p_sys->psz_icy_genre = NULL;
791 p_sys->psz_icy_title = NULL;
793 p_access->info.i_size = 0;
794 p_access->info.i_pos = i_tell;
795 p_access->info.b_eof = VLC_FALSE;
798 /* Open connection */
799 p_sys->fd = net_OpenTCP( p_access, srv.psz_host, srv.i_port );
802 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
806 return Request( p_access,i_tell );
810 static int Request( access_t *p_access, int64_t i_tell )
812 access_sys_t *p_sys = p_access->p_sys;
816 if( p_sys->url.psz_path )
818 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
819 "GET http://%s:%d%s HTTP/1.%d\r\n",
820 p_sys->url.psz_host, p_sys->url.i_port,
821 p_sys->url.psz_path, p_sys->i_version );
825 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
826 "GET http://%s:%d/ HTTP/1.%d\r\n",
827 p_sys->url.psz_host, p_sys->url.i_port,
833 char *psz_path = p_sys->url.psz_path;
834 if( !psz_path || !*psz_path )
838 if( p_sys->url.i_port != 80)
840 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
841 "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
842 psz_path, p_sys->i_version, p_sys->url.psz_host,
847 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
848 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
849 psz_path, p_sys->i_version, p_sys->url.psz_host );
853 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "User-Agent: %s\r\n",
854 p_sys->psz_user_agent );
856 if( p_sys->i_version == 1 )
858 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
859 "Range: bytes="I64Fd"-\r\n", i_tell );
862 /* Authentification */
863 if( p_sys->psz_user && *p_sys->psz_user )
868 asprintf( &buf, "%s:%s", p_sys->psz_user,
869 p_sys->psz_passwd ? p_sys->psz_passwd : "" );
871 b64 = vlc_b64_encode( buf );
873 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
874 "Authorization: Basic %s\r\n", b64 );
878 /* ICY meta data request */
879 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "Icy-MetaData: 1\r\n" );
882 if( p_sys->b_continuous && p_sys->i_version == 1 )
884 net_Printf( VLC_OBJECT( p_access ), p_sys->fd, NULL,
885 "Connection: keep-alive\r\n" );
889 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
890 "Connection: Close\r\n");
891 p_sys->b_continuous = VLC_FALSE;
894 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "\r\n" ) < 0 )
896 msg_Err( p_access, "failed to send request" );
897 net_Close( p_sys->fd ); p_sys->fd = -1;
902 if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL ) ) == NULL )
904 msg_Err( p_access, "failed to read answer" );
907 if( !strncmp( psz, "HTTP/1.", 7 ) )
909 p_sys->psz_protocol = "HTTP";
910 p_sys->i_code = atoi( &psz[9] );
912 else if( !strncmp( psz, "ICY", 3 ) )
914 p_sys->psz_protocol = "ICY";
915 p_sys->i_code = atoi( &psz[4] );
916 p_sys->b_reconnect = VLC_TRUE;
920 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
924 msg_Dbg( p_access, "protocol '%s' answer code %d",
925 p_sys->psz_protocol, p_sys->i_code );
926 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
928 p_sys->b_seekable = VLC_FALSE;
930 if( p_sys->i_code != 206 )
932 p_sys->b_seekable = VLC_FALSE;
934 if( p_sys->i_code >= 400 )
936 msg_Err( p_access, "error: %s", psz );
944 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
949 msg_Err( p_access, "failed to read answer" );
953 /* msg_Dbg( p_input, "Line=%s", psz ); */
961 if( ( p = strchr( psz, ':' ) ) == NULL )
963 msg_Err( p_access, "malformed header line: %s", psz );
968 while( *p == ' ' ) p++;
970 if( !strcasecmp( psz, "Content-Length" ) )
972 if( p_sys->b_continuous )
974 p_access->info.i_size = -1;
975 msg_Dbg( p_access, "this frame size="I64Fd, atoll(p ) );
976 p_sys->i_remaining = atoll( p );
980 p_access->info.i_size = i_tell + atoll( p );
981 msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
984 else if( !strcasecmp( psz, "Location" ) )
986 if( p_sys->psz_location ) free( p_sys->psz_location );
987 p_sys->psz_location = strdup( p );
989 else if( !strcasecmp( psz, "Content-Type" ) )
991 if( p_sys->psz_mime ) free( p_sys->psz_mime );
992 p_sys->psz_mime = strdup( p );
993 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
995 else if( !strcasecmp( psz, "Pragma" ) )
997 if( !strcasecmp( psz, "Pragma: features" ) )
998 p_sys->b_mms = VLC_TRUE;
999 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
1000 p_sys->psz_pragma = strdup( p );
1001 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1003 else if( !strcasecmp( psz, "Server" ) )
1005 msg_Dbg( p_access, "Server: %s", p );
1006 if( !strncasecmp( p, "Icecast", 7 ) ||
1007 !strncasecmp( p, "Nanocaster", 10 ) )
1009 /* Remember if this is Icecast
1010 * we need to force mp3 in some cases without breaking
1013 /* Let live 65 streams (nanocaster) piggyback on the icecast
1014 * routine. They look very similar */
1016 p_sys->b_reconnect = VLC_TRUE;
1017 p_sys->b_pace_control = VLC_FALSE;
1018 p_sys->b_icecast = VLC_TRUE;
1021 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1023 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1024 if( !strncasecmp( p, "chunked", 7 ) )
1026 p_sys->b_chunked = VLC_TRUE;
1029 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1031 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1032 p_sys->i_icy_meta = atoi( p );
1033 if( p_sys->i_icy_meta < 0 )
1034 p_sys->i_icy_meta = 0;
1036 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1038 else if( !strcasecmp( psz, "Icy-Name" ) )
1040 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
1041 p_sys->psz_icy_name = strdup( p );
1042 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1044 else if( !strcasecmp( psz, "Icy-Genre" ) )
1046 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
1047 p_sys->psz_icy_genre = strdup( p );
1048 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1050 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1052 msg_Dbg( p_access, "Icy-Notice: %s", p );
1054 else if( !strncasecmp( psz, "icy-", 4 ) ||
1055 !strncasecmp( psz, "ice-", 4 ) ||
1056 !strncasecmp( psz, "x-audiocast", 11 ) )
1058 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1066 net_Close( p_sys->fd ); p_sys->fd = -1;
1067 return VLC_EGENERIC;