1 /*****************************************************************************
2 * http.c: HTTP input module
3 *****************************************************************************
4 * Copyright (C) 2001-2005 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Christophe Massiot <massiot@via.ecp.fr>
9 * RĂ©mi Denis-Courmont <rem # videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
32 #include <vlc/input.h>
34 #include "vlc_playlist.h"
39 /*****************************************************************************
41 *****************************************************************************/
42 static int Open ( vlc_object_t * );
43 static void Close( vlc_object_t * );
45 #define PROXY_TEXT N_("HTTP proxy")
46 #define PROXY_LONGTEXT N_( \
47 "You can specify an HTTP proxy to use. It must be of the form " \
48 "http://[user[:pass]@]myproxy.mydomain:myport/ ; " \
49 "if empty, the http_proxy environment variable will be tried." )
51 #define CACHING_TEXT N_("Caching value in ms")
52 #define CACHING_LONGTEXT N_( \
53 "Allows you to modify the default caching value for http streams. This " \
54 "value should be set in millisecond units." )
56 #define AGENT_TEXT N_("HTTP user agent")
57 #define AGENT_LONGTEXT N_("Allows you to modify the user agent that will be " \
58 "used for the connection.")
60 #define RECONNECT_TEXT N_("Auto re-connect")
61 #define RECONNECT_LONGTEXT N_("Will automatically attempt a re-connection " \
62 "in case it was untimely closed.")
64 #define CONTINUOUS_TEXT N_("Continuous stream")
65 #define CONTINUOUS_LONGTEXT N_("Enable this option to read a file that is " \
66 "being constantly updated (for example, a JPG file on a server)")
69 set_description( _("HTTP input") );
70 set_capability( "access2", 0 );
71 set_shortname( _( "HTTP/HTTPS" ) );
72 set_category( CAT_INPUT );
73 set_subcategory( SUBCAT_INPUT_ACCESS );
75 add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
77 add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
78 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
79 add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
80 AGENT_LONGTEXT, VLC_FALSE );
81 add_bool( "http-reconnect", 0, NULL, RECONNECT_TEXT,
82 RECONNECT_LONGTEXT, VLC_TRUE );
83 add_bool( "http-continuous", 0, NULL, CONTINUOUS_TEXT,
84 CONTINUOUS_LONGTEXT, VLC_TRUE );
86 add_shortcut( "http" );
87 add_shortcut( "https" );
88 add_shortcut( "unsv" );
89 set_callbacks( Open, Close );
92 /*****************************************************************************
94 *****************************************************************************/
103 char *psz_user_agent;
118 vlc_bool_t b_icecast;
121 vlc_bool_t b_chunked;
131 vlc_bool_t b_seekable;
132 vlc_bool_t b_reconnect;
133 vlc_bool_t b_continuous;
134 vlc_bool_t b_pace_control;
138 static int Read( access_t *, uint8_t *, int );
139 static int Seek( access_t *, int64_t );
140 static int Control( access_t *, int, va_list );
143 static int Connect( access_t *, int64_t );
144 static int Request( access_t *p_access, int64_t i_tell );
145 static void Disconnect( access_t * );
147 /*****************************************************************************
149 *****************************************************************************/
150 static int Open( vlc_object_t *p_this )
152 access_t *p_access = (access_t*)p_this;
156 /* Set up p_access */
157 p_access->pf_read = Read;
158 p_access->pf_block = NULL;
159 p_access->pf_control = Control;
160 p_access->pf_seek = Seek;
161 p_access->info.i_update = 0;
162 p_access->info.i_size = 0;
163 p_access->info.i_pos = 0;
164 p_access->info.b_eof = VLC_FALSE;
165 p_access->info.i_title = 0;
166 p_access->info.i_seekpoint = 0;
167 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
168 memset( p_sys, 0, sizeof( access_sys_t ) );
170 p_sys->b_proxy = VLC_FALSE;
171 p_sys->i_version = 1;
172 p_sys->b_seekable = VLC_TRUE;
173 p_sys->psz_mime = NULL;
174 p_sys->psz_pragma = NULL;
175 p_sys->b_mms = VLC_FALSE;
176 p_sys->b_icecast = VLC_FALSE;
177 p_sys->psz_location = NULL;
178 p_sys->psz_user_agent = NULL;
179 p_sys->b_pace_control = VLC_TRUE;
180 p_sys->b_ssl = VLC_FALSE;
183 p_sys->i_icy_meta = 0;
184 p_sys->psz_icy_name = NULL;
185 p_sys->psz_icy_genre = NULL;
186 p_sys->psz_icy_title = NULL;
187 p_sys->i_remaining = 0;
190 if( vlc_UrlIsNotEncoded( p_access->psz_path ) )
192 psz = vlc_UrlEncode( p_access->psz_path );
199 vlc_UrlParse( &p_sys->url, psz, 0 );
203 vlc_UrlParse( &p_sys->url, p_access->psz_path, 0 );
205 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
207 msg_Warn( p_access, "invalid host" );
210 if( !strncmp( p_access->psz_access, "https", 5 ) )
213 p_sys->b_ssl = VLC_TRUE;
214 if( p_sys->url.i_port <= 0 )
215 p_sys->url.i_port = 443;
219 if( p_sys->url.i_port <= 0 )
220 p_sys->url.i_port = 80;
224 p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
227 psz = var_CreateGetString( p_access, "http-proxy" );
230 p_sys->b_proxy = VLC_TRUE;
231 vlc_UrlParse( &p_sys->proxy, psz, 0 );
236 char *psz_proxy = getenv( "http_proxy" );
237 if( psz_proxy && *psz_proxy )
239 p_sys->b_proxy = VLC_TRUE;
240 vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
248 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
250 msg_Warn( p_access, "invalid proxy host" );
253 if( p_sys->proxy.i_port <= 0 )
255 p_sys->proxy.i_port = 80;
259 msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
260 p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
263 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
264 p_sys->proxy.i_port );
266 if( p_sys->url.psz_username && *p_sys->url.psz_username )
268 msg_Dbg( p_access, " user='%s', pwd='%s'",
269 p_sys->url.psz_username, p_sys->url.psz_password );
272 p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
273 p_sys->b_continuous = var_CreateGetBool( p_access, "http-continuous" );
276 if( Connect( p_access, 0 ) )
278 /* Retry with http 1.0 */
279 p_sys->i_version = 0;
281 if( p_access->b_die ||
282 Connect( p_access, 0 ) )
288 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
289 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
290 p_sys->psz_location && *p_sys->psz_location )
292 playlist_t * p_playlist;
293 input_item_t *p_input_item;
295 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
297 /* Do not accept redirection outside of HTTP works */
298 if( strncmp( p_sys->psz_location, "http", 4 )
299 || ( ( p_sys->psz_location[4] != ':' ) /* HTTP */
300 && strncmp( p_sys->psz_location + 4, "s:", 2 ) /* HTTP/SSL */ ) )
302 msg_Err( p_access, "insecure redirection ignored" );
306 p_playlist = vlc_object_find( p_access, VLC_OBJECT_PLAYLIST,
310 msg_Err( p_access, "redirection failed: can't find playlist" );
315 vlc_mutex_lock( &p_playlist->object_lock );
316 p_input_item = &p_playlist->status.p_item->input;
317 vlc_mutex_lock( &p_input_item->lock );
318 free( p_input_item->psz_uri );
319 free( p_access->psz_path );
320 p_input_item->psz_uri = strdup( p_sys->psz_location );
321 p_access->psz_path = strdup( p_sys->psz_location );
322 vlc_mutex_unlock( &p_input_item->lock );
323 vlc_mutex_unlock( &p_playlist->object_lock );
324 vlc_object_release( p_playlist );
326 /* Clean up current Open() run */
327 vlc_UrlClean( &p_sys->url );
328 vlc_UrlClean( &p_sys->proxy );
329 if( p_sys->psz_mime ) free( p_sys->psz_mime );
330 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
331 if( p_sys->psz_location ) free( p_sys->psz_location );
332 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
334 Disconnect( p_access );
337 /* Do new Open() run with new data */
338 return Open( p_this );
343 msg_Dbg( p_access, "This is actually a live mms server, BAIL" );
347 if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
349 if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
351 if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
352 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
353 p_access->psz_demux = strdup( "nsv" );
354 else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
355 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
356 p_access->psz_demux = strdup( "m4a" );
357 else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
358 p_access->psz_demux = strdup( "mp3" );
360 msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
361 p_access->psz_demux );
363 #if 0 /* Doesn't work really well because of the pre-buffering in
364 * shoutcast servers (the buffer content will be sent as fast as
366 p_sys->b_pace_control = VLC_FALSE;
369 else if( !p_sys->psz_mime )
372 p_access->psz_demux = strdup( "mp3" );
374 /* else probably Ogg Vorbis */
376 else if( !strcasecmp( p_access->psz_access, "unsv" ) &&
378 !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
380 /* Grrrr! detect ultravox server and force NSV demuxer */
381 p_access->psz_demux = strdup( "nsv" );
384 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
387 var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
392 vlc_UrlClean( &p_sys->url );
393 vlc_UrlClean( &p_sys->proxy );
394 if( p_sys->psz_mime ) free( p_sys->psz_mime );
395 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
396 if( p_sys->psz_location ) free( p_sys->psz_location );
397 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
399 Disconnect( p_access );
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_mime ) free( p_sys->psz_mime );
416 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
417 if( p_sys->psz_location ) free( p_sys->psz_location );
419 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
420 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
421 if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
423 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
425 Disconnect( p_access );
429 /*****************************************************************************
430 * Read: Read up to i_len bytes from the http connection and place in
431 * p_buffer. Return the actual number of bytes read
432 *****************************************************************************/
433 static int ReadICYMeta( access_t *p_access );
434 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
436 access_sys_t *p_sys = p_access->p_sys;
441 p_access->info.b_eof = VLC_TRUE;
445 if( p_access->info.i_size > 0 &&
446 i_len + p_access->info.i_pos > p_access->info.i_size )
448 if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
450 p_access->info.b_eof = VLC_TRUE;
455 if( p_sys->b_chunked )
457 if( p_sys->i_chunk < 0 )
459 p_access->info.b_eof = VLC_TRUE;
463 if( p_sys->i_chunk <= 0 )
465 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
466 /* read the chunk header */
469 msg_Dbg( p_access, "failed reading chunk-header line" );
472 p_sys->i_chunk = strtoll( psz, NULL, 16 );
475 if( p_sys->i_chunk <= 0 ) /* eof */
478 p_access->info.b_eof = VLC_TRUE;
483 if( i_len > p_sys->i_chunk )
485 i_len = p_sys->i_chunk;
489 if( p_sys->b_continuous && i_len > p_sys->i_remaining )
491 /* Only ask for the remaining length */
492 int i_new_len = p_sys->i_remaining;
495 Request( p_access, 0 );
496 i_read = Read( p_access, p_buffer, i_len );
502 if( p_sys->i_icy_meta > 0 && p_access->info.i_pos > 0 )
504 int64_t i_next = p_sys->i_icy_meta -
505 p_access->info.i_pos % p_sys->i_icy_meta;
507 if( i_next == p_sys->i_icy_meta )
509 if( ReadICYMeta( p_access ) )
511 p_access->info.b_eof = VLC_TRUE;
519 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, VLC_FALSE );
523 p_access->info.i_pos += i_read;
525 if( p_sys->b_chunked )
527 p_sys->i_chunk -= i_read;
528 if( p_sys->i_chunk <= 0 )
530 /* read the empty line */
531 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
532 if( psz ) free( psz );
536 else if( i_read == 0 )
539 * I very much doubt that this will work.
540 * If i_read == 0, the connection *IS* dead, so the only
541 * sensible thing to do is Disconnect() and then retry.
542 * Otherwise, I got recv() completely wrong. -- Courmisch
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 Disconnect( p_access );
552 if( p_sys->b_reconnect )
554 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
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;
586 /* Read meta data length */
587 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
589 if( ( i_read <= 0 ) || ( buffer == 0 ) )
592 i_read = buffer << 4;
593 msg_Dbg( p_access, "ICY meta size=%u", i_read);
595 psz_meta = malloc( i_read + 1 );
596 if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
597 (uint8_t *)psz_meta, i_read, VLC_TRUE ) != i_read )
600 psz_meta[i_read] = '\0'; /* Just in case */
602 msg_Dbg( p_access, "icy-meta=%s", psz_meta );
604 /* Now parse the meta */
605 /* Look for StreamTitle= */
606 p = strcasestr( (char *)psz_meta, "StreamTitle=" );
609 p += strlen( "StreamTitle=" );
610 if( *p == '\'' || *p == '"' )
612 char *psz = strchr( &p[1], p[0] );
614 psz = strchr( &p[1], ';' );
616 if( psz ) *psz = '\0';
620 char *psz = strchr( &p[1], ';' );
621 if( psz ) *psz = '\0';
624 if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
626 p_sys->psz_icy_title = strdup( &p[1] );
628 p_access->info.i_update |= INPUT_UPDATE_META;
633 msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
638 /*****************************************************************************
639 * Seek: close and re-open a connection at the right place
640 *****************************************************************************/
641 static int Seek( access_t *p_access, int64_t i_pos )
643 msg_Dbg( p_access, "trying to seek to "I64Fd, i_pos );
645 Disconnect( p_access );
647 if( Connect( p_access, i_pos ) )
649 msg_Err( p_access, "seek failed" );
650 p_access->info.b_eof = VLC_TRUE;
656 /*****************************************************************************
658 *****************************************************************************/
659 static int Control( access_t *p_access, int i_query, va_list args )
661 access_sys_t *p_sys = p_access->p_sys;
665 vlc_meta_t **pp_meta;
670 case ACCESS_CAN_SEEK:
671 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
672 *pb_bool = p_sys->b_seekable;
674 case ACCESS_CAN_FASTSEEK:
675 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
676 *pb_bool = VLC_FALSE;
678 case ACCESS_CAN_PAUSE:
679 case ACCESS_CAN_CONTROL_PACE:
680 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
682 #if 0 /* Disable for now until we have a clock synchro algo
683 * which works with something else than MPEG over UDP */
684 *pb_bool = p_sys->b_pace_control;
691 pi_int = (int*)va_arg( args, int * );
695 case ACCESS_GET_PTS_DELAY:
696 pi_64 = (int64_t*)va_arg( args, int64_t * );
697 *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
701 case ACCESS_SET_PAUSE_STATE:
704 case ACCESS_GET_META:
705 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
706 *pp_meta = vlc_meta_New();
707 msg_Dbg( p_access, "GET META %s %s %s",
708 p_sys->psz_icy_name, p_sys->psz_icy_genre, p_sys->psz_icy_title );
709 if( p_sys->psz_icy_name )
710 vlc_meta_Add( *pp_meta, VLC_META_TITLE,
711 p_sys->psz_icy_name );
712 if( p_sys->psz_icy_genre )
713 vlc_meta_Add( *pp_meta, VLC_META_GENRE,
714 p_sys->psz_icy_genre );
715 if( p_sys->psz_icy_title )
716 vlc_meta_Add( *pp_meta, VLC_META_NOW_PLAYING,
717 p_sys->psz_icy_title );
720 case ACCESS_GET_TITLE_INFO:
721 case ACCESS_SET_TITLE:
722 case ACCESS_SET_SEEKPOINT:
723 case ACCESS_SET_PRIVATE_ID_STATE:
727 msg_Warn( p_access, "unimplemented query in control" );
734 /*****************************************************************************
736 *****************************************************************************/
737 static int Connect( access_t *p_access, int64_t i_tell )
739 access_sys_t *p_sys = p_access->p_sys;
740 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
743 if( p_sys->psz_location ) free( p_sys->psz_location );
744 if( p_sys->psz_mime ) free( p_sys->psz_mime );
745 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
747 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
748 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
749 if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
752 p_sys->psz_location = NULL;
753 p_sys->psz_mime = NULL;
754 p_sys->psz_pragma = NULL;
755 p_sys->b_mms = VLC_FALSE;
756 p_sys->b_chunked = VLC_FALSE;
758 p_sys->i_icy_meta = 0;
759 p_sys->psz_icy_name = NULL;
760 p_sys->psz_icy_genre = NULL;
761 p_sys->psz_icy_title = NULL;
763 p_access->info.i_size = 0;
764 p_access->info.i_pos = i_tell;
765 p_access->info.b_eof = VLC_FALSE;
768 /* Open connection */
769 p_sys->fd = net_OpenTCP( p_access, srv.psz_host, srv.i_port );
772 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
776 /* Initialize TLS/SSL session */
777 if( p_sys->b_ssl == VLC_TRUE )
779 /* CONNECT to establish TLS tunnel through HTTP proxy */
783 unsigned i_status = 0;
785 if( p_sys->i_version == 0 )
787 /* CONNECT is not in HTTP/1.0 */
788 Disconnect( p_access );
792 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
793 "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
794 p_sys->url.psz_host, p_sys->url.i_port,
796 p_sys->url.psz_host, p_sys->url.i_port);
798 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
801 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
802 Disconnect( p_access );
806 sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
809 if( ( i_status / 100 ) != 2 )
811 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
812 Disconnect( p_access );
818 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
821 msg_Err( p_access, "HTTP proxy connection failed" );
822 Disconnect( p_access );
834 /* TLS/SSL handshake */
835 p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
837 if( p_sys->p_tls == NULL )
839 msg_Err( p_access, "cannot establish HTTP/TLS session" );
840 Disconnect( p_access );
843 p_sys->p_vs = &p_sys->p_tls->sock;
846 return Request( p_access, i_tell );
850 static int Request( access_t *p_access, int64_t i_tell )
852 access_sys_t *p_sys = p_access->p_sys;
854 v_socket_t *pvs = p_sys->p_vs;
858 if( p_sys->url.psz_path )
860 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
861 "GET http://%s:%d%s HTTP/1.%d\r\n",
862 p_sys->url.psz_host, p_sys->url.i_port,
863 p_sys->url.psz_path, p_sys->i_version );
867 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
868 "GET http://%s:%d/ HTTP/1.%d\r\n",
869 p_sys->url.psz_host, p_sys->url.i_port,
875 char *psz_path = p_sys->url.psz_path;
876 if( !psz_path || !*psz_path )
880 if( p_sys->url.i_port != 80)
882 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
883 "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
884 psz_path, p_sys->i_version, p_sys->url.psz_host,
889 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
890 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
891 psz_path, p_sys->i_version, p_sys->url.psz_host );
895 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "User-Agent: %s\r\n",
896 p_sys->psz_user_agent );
898 if( p_sys->i_version == 1 )
900 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
901 "Range: bytes="I64Fd"-\r\n", i_tell );
905 if( p_sys->url.psz_username && *p_sys->url.psz_username )
910 asprintf( &buf, "%s:%s", p_sys->url.psz_username,
911 p_sys->url.psz_password ? p_sys->url.psz_password : "" );
913 b64 = vlc_b64_encode( buf );
916 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
917 "Authorization: Basic %s\r\n", b64 );
921 /* Proxy Authentication */
922 if( p_sys->proxy.psz_username && *p_sys->proxy.psz_username )
927 asprintf( &buf, "%s:%s", p_sys->proxy.psz_username,
928 p_sys->proxy.psz_password ? p_sys->proxy.psz_password : "" );
930 b64 = vlc_b64_encode( buf );
933 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
934 "Proxy-Authorization: Basic %s\r\n", b64 );
938 /* ICY meta data request */
939 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
942 if( p_sys->b_continuous && p_sys->i_version == 1 )
944 net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
945 "Connection: keep-alive\r\n" );
949 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
950 "Connection: Close\r\n");
951 p_sys->b_continuous = VLC_FALSE;
954 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "\r\n" ) < 0 )
956 msg_Err( p_access, "failed to send request" );
957 Disconnect( p_access );
962 if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs ) ) == NULL )
964 msg_Err( p_access, "failed to read answer" );
967 if( !strncmp( psz, "HTTP/1.", 7 ) )
969 p_sys->psz_protocol = "HTTP";
970 p_sys->i_code = atoi( &psz[9] );
972 else if( !strncmp( psz, "ICY", 3 ) )
974 p_sys->psz_protocol = "ICY";
975 p_sys->i_code = atoi( &psz[4] );
976 p_sys->b_reconnect = VLC_TRUE;
980 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
984 msg_Dbg( p_access, "protocol '%s' answer code %d",
985 p_sys->psz_protocol, p_sys->i_code );
986 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
988 p_sys->b_seekable = VLC_FALSE;
990 if( p_sys->i_code != 206 )
992 p_sys->b_seekable = VLC_FALSE;
994 if( p_sys->i_code >= 400 )
996 msg_Err( p_access, "error: %s", psz );
1004 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs );
1009 msg_Err( p_access, "failed to read answer" );
1013 /* msg_Dbg( p_input, "Line=%s", psz ); */
1021 if( ( p = strchr( psz, ':' ) ) == NULL )
1023 msg_Err( p_access, "malformed header line: %s", psz );
1028 while( *p == ' ' ) p++;
1030 if( !strcasecmp( psz, "Content-Length" ) )
1032 if( p_sys->b_continuous )
1034 p_access->info.i_size = -1;
1035 msg_Dbg( p_access, "this frame size="I64Fd, atoll(p ) );
1036 p_sys->i_remaining = atoll( p );
1040 p_access->info.i_size = i_tell + atoll( p );
1041 msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
1044 else if( !strcasecmp( psz, "Location" ) )
1046 if( p_sys->psz_location ) free( p_sys->psz_location );
1047 p_sys->psz_location = strdup( p );
1049 else if( !strcasecmp( psz, "Content-Type" ) )
1051 if( p_sys->psz_mime ) free( p_sys->psz_mime );
1052 p_sys->psz_mime = strdup( p );
1053 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1055 else if( !strcasecmp( psz, "Pragma" ) )
1057 if( !strcasecmp( psz, "Pragma: features" ) )
1058 p_sys->b_mms = VLC_TRUE;
1059 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
1060 p_sys->psz_pragma = strdup( p );
1061 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1063 else if( !strcasecmp( psz, "Server" ) )
1065 msg_Dbg( p_access, "Server: %s", p );
1066 if( !strncasecmp( p, "Icecast", 7 ) ||
1067 !strncasecmp( p, "Nanocaster", 10 ) )
1069 /* Remember if this is Icecast
1070 * we need to force demux in this case without breaking
1073 /* Let live 365 streams (nanocaster) piggyback on the icecast
1074 * routine. They look very similar */
1076 p_sys->b_reconnect = VLC_TRUE;
1077 p_sys->b_pace_control = VLC_FALSE;
1078 p_sys->b_icecast = VLC_TRUE;
1081 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1083 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1084 if( !strncasecmp( p, "chunked", 7 ) )
1086 p_sys->b_chunked = VLC_TRUE;
1089 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1091 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1092 p_sys->i_icy_meta = atoi( p );
1093 if( p_sys->i_icy_meta < 0 )
1094 p_sys->i_icy_meta = 0;
1096 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1098 else if( !strcasecmp( psz, "Icy-Name" ) )
1100 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
1101 p_sys->psz_icy_name = strdup( p );
1102 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1104 p_sys->b_icecast = VLC_TRUE; /* be on the safeside. set it here as well. */
1105 p_sys->b_reconnect = VLC_TRUE;
1106 p_sys->b_pace_control = VLC_FALSE;
1108 else if( !strcasecmp( psz, "Icy-Genre" ) )
1110 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
1111 p_sys->psz_icy_genre = strdup( p );
1112 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1114 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1116 msg_Dbg( p_access, "Icy-Notice: %s", p );
1118 else if( !strncasecmp( psz, "icy-", 4 ) ||
1119 !strncasecmp( psz, "ice-", 4 ) ||
1120 !strncasecmp( psz, "x-audiocast", 11 ) )
1122 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1130 Disconnect( p_access );
1131 return VLC_EGENERIC;
1134 /*****************************************************************************
1136 *****************************************************************************/
1137 static void Disconnect( access_t *p_access )
1139 access_sys_t *p_sys = p_access->p_sys;
1141 if( p_sys->p_tls != NULL)
1143 tls_ClientDelete( p_sys->p_tls );
1144 p_sys->p_tls = NULL;
1147 if( p_sys->fd != -1)
1149 net_Close(p_sys->fd);