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( "http4" );
88 add_shortcut( "http6" );
89 add_shortcut( "https" );
90 add_shortcut( "unsv" );
91 set_callbacks( Open, Close );
94 /*****************************************************************************
96 *****************************************************************************/
100 tls_session_t *p_tls;
105 char *psz_user_agent;
120 vlc_bool_t b_icecast;
123 vlc_bool_t b_chunked;
133 vlc_bool_t b_seekable;
134 vlc_bool_t b_reconnect;
135 vlc_bool_t b_continuous;
136 vlc_bool_t b_pace_control;
140 static int Read( access_t *, uint8_t *, int );
141 static int Seek( access_t *, int64_t );
142 static int Control( access_t *, int, va_list );
145 static int Connect( access_t *, int64_t );
146 static int Request( access_t *p_access, int64_t i_tell );
147 static void Disconnect( access_t * );
149 /*****************************************************************************
151 *****************************************************************************/
152 static int Open( vlc_object_t *p_this )
154 access_t *p_access = (access_t*)p_this;
158 /* First set ipv4/ipv6 */
159 var_Create( p_access, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
160 var_Create( p_access, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
162 if( *p_access->psz_access )
165 /* Find out which shortcut was used */
166 if( !strncmp( p_access->psz_access, "http4", 6 ) )
168 val.b_bool = VLC_TRUE;
169 var_Set( p_access, "ipv4", val );
171 val.b_bool = VLC_FALSE;
172 var_Set( p_access, "ipv6", val );
174 else if( !strncmp( p_access->psz_access, "http6", 6 ) )
176 val.b_bool = VLC_TRUE;
177 var_Set( p_access, "ipv6", val );
179 val.b_bool = VLC_FALSE;
180 var_Set( p_access, "ipv4", val );
184 /* Set up p_access */
185 p_access->pf_read = Read;
186 p_access->pf_block = NULL;
187 p_access->pf_control = Control;
188 p_access->pf_seek = Seek;
189 p_access->info.i_update = 0;
190 p_access->info.i_size = 0;
191 p_access->info.i_pos = 0;
192 p_access->info.b_eof = VLC_FALSE;
193 p_access->info.i_title = 0;
194 p_access->info.i_seekpoint = 0;
195 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
196 memset( p_sys, 0, sizeof( access_sys_t ) );
198 p_sys->b_proxy = VLC_FALSE;
199 p_sys->i_version = 1;
200 p_sys->b_seekable = VLC_TRUE;
201 p_sys->psz_mime = NULL;
202 p_sys->psz_pragma = NULL;
203 p_sys->b_mms = VLC_FALSE;
204 p_sys->b_icecast = VLC_FALSE;
205 p_sys->psz_location = NULL;
206 p_sys->psz_user_agent = NULL;
207 p_sys->b_pace_control = VLC_TRUE;
208 p_sys->b_ssl = VLC_FALSE;
211 p_sys->i_icy_meta = 0;
212 p_sys->psz_icy_name = NULL;
213 p_sys->psz_icy_genre = NULL;
214 p_sys->psz_icy_title = NULL;
215 p_sys->i_remaining = 0;
218 if( vlc_UrlIsNotEncoded( p_access->psz_path ) )
220 psz = vlc_UrlEncode( p_access->psz_path );
227 vlc_UrlParse( &p_sys->url, psz, 0 );
231 vlc_UrlParse( &p_sys->url, p_access->psz_path, 0 );
233 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
235 msg_Warn( p_access, "invalid host" );
238 if( !strncmp( p_access->psz_access, "https", 5 ) )
241 p_sys->b_ssl = VLC_TRUE;
242 if( p_sys->url.i_port <= 0 )
243 p_sys->url.i_port = 443;
247 if( p_sys->url.i_port <= 0 )
248 p_sys->url.i_port = 80;
252 p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
255 psz = var_CreateGetString( p_access, "http-proxy" );
258 p_sys->b_proxy = VLC_TRUE;
259 vlc_UrlParse( &p_sys->proxy, psz, 0 );
264 char *psz_proxy = getenv( "http_proxy" );
265 if( psz_proxy && *psz_proxy )
267 p_sys->b_proxy = VLC_TRUE;
268 vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
276 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
278 msg_Warn( p_access, "invalid proxy host" );
281 if( p_sys->proxy.i_port <= 0 )
283 p_sys->proxy.i_port = 80;
287 msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
288 p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
291 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
292 p_sys->proxy.i_port );
294 if( p_sys->url.psz_username && *p_sys->url.psz_username )
296 msg_Dbg( p_access, " user='%s', pwd='%s'",
297 p_sys->url.psz_username, p_sys->url.psz_password );
300 p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
301 p_sys->b_continuous = var_CreateGetBool( p_access, "http-continuous" );
304 if( Connect( p_access, 0 ) )
306 /* Retry with http 1.0 */
307 p_sys->i_version = 0;
309 if( p_access->b_die ||
310 Connect( p_access, 0 ) )
316 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
317 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
318 p_sys->psz_location && *p_sys->psz_location )
320 playlist_t * p_playlist;
321 input_item_t *p_input_item;
323 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
325 /* Do not accept redirection outside of HTTP works */
326 if( strncmp( p_sys->psz_location, "http", 4 )
327 || ( ( p_sys->psz_location[4] != ':' ) /* HTTP */
328 && strncmp( p_sys->psz_location + 4, "s:", 2 ) /* HTTP/SSL */ ) )
330 msg_Err( p_access, "insecure redirection ignored" );
334 p_playlist = vlc_object_find( p_access, VLC_OBJECT_PLAYLIST,
338 msg_Err( p_access, "redirection failed: can't find playlist" );
343 vlc_mutex_lock( &p_playlist->object_lock );
344 p_input_item = &p_playlist->status.p_item->input;
345 vlc_mutex_lock( &p_input_item->lock );
346 free( p_input_item->psz_uri );
347 free( p_access->psz_path );
348 p_input_item->psz_uri = strdup( p_sys->psz_location );
349 p_access->psz_path = strdup( p_sys->psz_location );
350 vlc_mutex_unlock( &p_input_item->lock );
351 vlc_mutex_unlock( &p_playlist->object_lock );
352 vlc_object_release( p_playlist );
354 /* Clean up current Open() run */
355 vlc_UrlClean( &p_sys->url );
356 vlc_UrlClean( &p_sys->proxy );
357 if( p_sys->psz_mime ) free( p_sys->psz_mime );
358 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
359 if( p_sys->psz_location ) free( p_sys->psz_location );
360 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
362 Disconnect( p_access );
365 /* Do new Open() run with new data */
366 return Open( p_this );
371 msg_Dbg( p_access, "This is actually a live mms server, BAIL" );
375 if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
377 if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
379 if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
380 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
381 p_access->psz_demux = strdup( "nsv" );
382 else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
383 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
384 p_access->psz_demux = strdup( "m4a" );
385 else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
386 p_access->psz_demux = strdup( "mp3" );
388 msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
389 p_access->psz_demux );
391 #if 0 /* Doesn't work really well because of the pre-buffering in
392 * shoutcast servers (the buffer content will be sent as fast as
394 p_sys->b_pace_control = VLC_FALSE;
397 else if( !p_sys->psz_mime )
400 p_access->psz_demux = strdup( "mp3" );
402 /* else probably Ogg Vorbis */
404 else if( !strcasecmp( p_access->psz_access, "unsv" ) &&
406 !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
408 /* Grrrr! detect ultravox server and force NSV demuxer */
409 p_access->psz_demux = strdup( "nsv" );
412 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
415 var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
420 vlc_UrlClean( &p_sys->url );
421 vlc_UrlClean( &p_sys->proxy );
422 if( p_sys->psz_mime ) free( p_sys->psz_mime );
423 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
424 if( p_sys->psz_location ) free( p_sys->psz_location );
425 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
427 Disconnect( p_access );
432 /*****************************************************************************
434 *****************************************************************************/
435 static void Close( vlc_object_t *p_this )
437 access_t *p_access = (access_t*)p_this;
438 access_sys_t *p_sys = p_access->p_sys;
440 vlc_UrlClean( &p_sys->url );
441 vlc_UrlClean( &p_sys->proxy );
443 if( p_sys->psz_mime ) free( p_sys->psz_mime );
444 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
445 if( p_sys->psz_location ) free( p_sys->psz_location );
447 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
448 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
449 if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
451 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
453 Disconnect( p_access );
457 /*****************************************************************************
458 * Read: Read up to i_len bytes from the http connection and place in
459 * p_buffer. Return the actual number of bytes read
460 *****************************************************************************/
461 static int ReadICYMeta( access_t *p_access );
462 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
464 access_sys_t *p_sys = p_access->p_sys;
469 p_access->info.b_eof = VLC_TRUE;
473 if( p_access->info.i_size > 0 &&
474 i_len + p_access->info.i_pos > p_access->info.i_size )
476 if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
478 p_access->info.b_eof = VLC_TRUE;
483 if( p_sys->b_chunked )
485 if( p_sys->i_chunk < 0 )
487 p_access->info.b_eof = VLC_TRUE;
491 if( p_sys->i_chunk <= 0 )
493 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
494 /* read the chunk header */
497 msg_Dbg( p_access, "failed reading chunk-header line" );
500 p_sys->i_chunk = strtoll( psz, NULL, 16 );
503 if( p_sys->i_chunk <= 0 ) /* eof */
506 p_access->info.b_eof = VLC_TRUE;
511 if( i_len > p_sys->i_chunk )
513 i_len = p_sys->i_chunk;
517 if( p_sys->b_continuous && i_len > p_sys->i_remaining )
519 /* Only ask for the remaining length */
520 int i_new_len = p_sys->i_remaining;
523 Request( p_access, 0 );
524 i_read = Read( p_access, p_buffer, i_len );
530 if( p_sys->i_icy_meta > 0 && p_access->info.i_pos > 0 )
532 int64_t i_next = p_sys->i_icy_meta -
533 p_access->info.i_pos % p_sys->i_icy_meta;
535 if( i_next == p_sys->i_icy_meta )
537 if( ReadICYMeta( p_access ) )
539 p_access->info.b_eof = VLC_TRUE;
547 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, VLC_FALSE );
551 p_access->info.i_pos += i_read;
553 if( p_sys->b_chunked )
555 p_sys->i_chunk -= i_read;
556 if( p_sys->i_chunk <= 0 )
558 /* read the empty line */
559 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
560 if( psz ) free( psz );
564 else if( i_read == 0 )
567 * I very much doubt that this will work.
568 * If i_read == 0, the connection *IS* dead, so the only
569 * sensible thing to do is Disconnect() and then retry.
570 * Otherwise, I got recv() completely wrong. -- Courmisch
572 if( p_sys->b_continuous )
574 Request( p_access, 0 );
575 p_sys->b_continuous = VLC_FALSE;
576 i_read = Read( p_access, p_buffer, i_len );
577 p_sys->b_continuous = VLC_TRUE;
579 Disconnect( p_access );
580 if( p_sys->b_reconnect )
582 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
583 if( Connect( p_access, p_access->info.i_pos ) )
585 msg_Dbg( p_access, "reconnection failed" );
589 p_sys->b_reconnect = VLC_FALSE;
590 i_read = Read( p_access, p_buffer, i_len );
591 p_sys->b_reconnect = VLC_TRUE;
595 if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
598 if( p_sys->b_continuous )
600 p_sys->i_remaining -= i_read;
606 static int ReadICYMeta( access_t *p_access )
608 access_sys_t *p_sys = p_access->p_sys;
614 /* Read meta data length */
615 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
617 if( ( i_read <= 0 ) || ( buffer == 0 ) )
620 i_read = buffer << 4;
621 msg_Dbg( p_access, "ICY meta size=%u", i_read);
623 psz_meta = malloc( i_read + 1 );
624 if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
625 (uint8_t *)psz_meta, i_read, VLC_TRUE ) != i_read )
628 psz_meta[i_read] = '\0'; /* Just in case */
630 msg_Dbg( p_access, "icy-meta=%s", psz_meta );
632 /* Now parse the meta */
633 /* Look for StreamTitle= */
634 p = strcasestr( (char *)psz_meta, "StreamTitle=" );
637 p += strlen( "StreamTitle=" );
638 if( *p == '\'' || *p == '"' )
640 char *psz = strchr( &p[1], p[0] );
642 psz = strchr( &p[1], ';' );
644 if( psz ) *psz = '\0';
648 char *psz = strchr( &p[1], ';' );
649 if( psz ) *psz = '\0';
652 if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
654 p_sys->psz_icy_title = strdup( &p[1] );
656 p_access->info.i_update |= INPUT_UPDATE_META;
661 msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
666 /*****************************************************************************
667 * Seek: close and re-open a connection at the right place
668 *****************************************************************************/
669 static int Seek( access_t *p_access, int64_t i_pos )
671 msg_Dbg( p_access, "trying to seek to "I64Fd, i_pos );
673 Disconnect( p_access );
675 if( Connect( p_access, i_pos ) )
677 msg_Err( p_access, "seek failed" );
678 p_access->info.b_eof = VLC_TRUE;
684 /*****************************************************************************
686 *****************************************************************************/
687 static int Control( access_t *p_access, int i_query, va_list args )
689 access_sys_t *p_sys = p_access->p_sys;
693 vlc_meta_t **pp_meta;
698 case ACCESS_CAN_SEEK:
699 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
700 *pb_bool = p_sys->b_seekable;
702 case ACCESS_CAN_FASTSEEK:
703 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
704 *pb_bool = VLC_FALSE;
706 case ACCESS_CAN_PAUSE:
707 case ACCESS_CAN_CONTROL_PACE:
708 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
710 #if 0 /* Disable for now until we have a clock synchro algo
711 * which works with something else than MPEG over UDP */
712 *pb_bool = p_sys->b_pace_control;
719 pi_int = (int*)va_arg( args, int * );
723 case ACCESS_GET_PTS_DELAY:
724 pi_64 = (int64_t*)va_arg( args, int64_t * );
725 *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
729 case ACCESS_SET_PAUSE_STATE:
732 case ACCESS_GET_META:
733 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
734 *pp_meta = vlc_meta_New();
735 msg_Dbg( p_access, "GET META %s %s %s",
736 p_sys->psz_icy_name, p_sys->psz_icy_genre, p_sys->psz_icy_title );
737 if( p_sys->psz_icy_name )
738 vlc_meta_Add( *pp_meta, VLC_META_TITLE,
739 p_sys->psz_icy_name );
740 if( p_sys->psz_icy_genre )
741 vlc_meta_Add( *pp_meta, VLC_META_GENRE,
742 p_sys->psz_icy_genre );
743 if( p_sys->psz_icy_title )
744 vlc_meta_Add( *pp_meta, VLC_META_NOW_PLAYING,
745 p_sys->psz_icy_title );
748 case ACCESS_GET_TITLE_INFO:
749 case ACCESS_SET_TITLE:
750 case ACCESS_SET_SEEKPOINT:
751 case ACCESS_SET_PRIVATE_ID_STATE:
755 msg_Warn( p_access, "unimplemented query in control" );
762 /*****************************************************************************
764 *****************************************************************************/
765 static int Connect( access_t *p_access, int64_t i_tell )
767 access_sys_t *p_sys = p_access->p_sys;
768 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
771 if( p_sys->psz_location ) free( p_sys->psz_location );
772 if( p_sys->psz_mime ) free( p_sys->psz_mime );
773 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
775 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
776 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
777 if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
780 p_sys->psz_location = NULL;
781 p_sys->psz_mime = NULL;
782 p_sys->psz_pragma = NULL;
783 p_sys->b_mms = VLC_FALSE;
784 p_sys->b_chunked = VLC_FALSE;
786 p_sys->i_icy_meta = 0;
787 p_sys->psz_icy_name = NULL;
788 p_sys->psz_icy_genre = NULL;
789 p_sys->psz_icy_title = NULL;
791 p_access->info.i_size = 0;
792 p_access->info.i_pos = i_tell;
793 p_access->info.b_eof = VLC_FALSE;
796 /* Open connection */
797 p_sys->fd = net_OpenTCP( p_access, srv.psz_host, srv.i_port );
800 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
804 /* Initialize TLS/SSL session */
805 if( p_sys->b_ssl == VLC_TRUE )
807 /* CONNECT to establish TLS tunnel through HTTP proxy */
811 unsigned i_status = 0;
813 if( p_sys->i_version == 0 )
815 /* CONNECT is not in HTTP/1.0 */
816 Disconnect( p_access );
820 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
821 "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
822 p_sys->url.psz_host, p_sys->url.i_port,
824 p_sys->url.psz_host, p_sys->url.i_port);
826 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
829 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
830 Disconnect( p_access );
834 sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
837 if( ( i_status / 100 ) != 2 )
839 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
840 Disconnect( p_access );
846 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
849 msg_Err( p_access, "HTTP proxy connection failed" );
850 Disconnect( p_access );
862 /* TLS/SSL handshake */
863 p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
865 if( p_sys->p_tls == NULL )
867 msg_Err( p_access, "cannot establish HTTP/TLS session" );
868 Disconnect( p_access );
871 p_sys->p_vs = &p_sys->p_tls->sock;
874 return Request( p_access, i_tell );
878 static int Request( access_t *p_access, int64_t i_tell )
880 access_sys_t *p_sys = p_access->p_sys;
882 v_socket_t *pvs = p_sys->p_vs;
886 if( p_sys->url.psz_path )
888 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
889 "GET http://%s:%d%s HTTP/1.%d\r\n",
890 p_sys->url.psz_host, p_sys->url.i_port,
891 p_sys->url.psz_path, p_sys->i_version );
895 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
896 "GET http://%s:%d/ HTTP/1.%d\r\n",
897 p_sys->url.psz_host, p_sys->url.i_port,
903 char *psz_path = p_sys->url.psz_path;
904 if( !psz_path || !*psz_path )
908 if( p_sys->url.i_port != 80)
910 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
911 "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
912 psz_path, p_sys->i_version, p_sys->url.psz_host,
917 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
918 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
919 psz_path, p_sys->i_version, p_sys->url.psz_host );
923 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "User-Agent: %s\r\n",
924 p_sys->psz_user_agent );
926 if( p_sys->i_version == 1 )
928 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
929 "Range: bytes="I64Fd"-\r\n", i_tell );
933 if( p_sys->url.psz_username && *p_sys->url.psz_username )
938 asprintf( &buf, "%s:%s", p_sys->url.psz_username,
939 p_sys->url.psz_password ? p_sys->url.psz_password : "" );
941 b64 = vlc_b64_encode( buf );
944 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
945 "Authorization: Basic %s\r\n", b64 );
949 /* Proxy Authentication */
950 if( p_sys->proxy.psz_username && *p_sys->proxy.psz_username )
955 asprintf( &buf, "%s:%s", p_sys->proxy.psz_username,
956 p_sys->proxy.psz_password ? p_sys->proxy.psz_password : "" );
958 b64 = vlc_b64_encode( buf );
961 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
962 "Proxy-Authorization: Basic %s\r\n", b64 );
966 /* ICY meta data request */
967 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
970 if( p_sys->b_continuous && p_sys->i_version == 1 )
972 net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
973 "Connection: keep-alive\r\n" );
977 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
978 "Connection: Close\r\n");
979 p_sys->b_continuous = VLC_FALSE;
982 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "\r\n" ) < 0 )
984 msg_Err( p_access, "failed to send request" );
985 Disconnect( p_access );
990 if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs ) ) == NULL )
992 msg_Err( p_access, "failed to read answer" );
995 if( !strncmp( psz, "HTTP/1.", 7 ) )
997 p_sys->psz_protocol = "HTTP";
998 p_sys->i_code = atoi( &psz[9] );
1000 else if( !strncmp( psz, "ICY", 3 ) )
1002 p_sys->psz_protocol = "ICY";
1003 p_sys->i_code = atoi( &psz[4] );
1004 p_sys->b_reconnect = VLC_TRUE;
1008 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1012 msg_Dbg( p_access, "protocol '%s' answer code %d",
1013 p_sys->psz_protocol, p_sys->i_code );
1014 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1016 p_sys->b_seekable = VLC_FALSE;
1018 if( p_sys->i_code != 206 )
1020 p_sys->b_seekable = VLC_FALSE;
1022 if( p_sys->i_code >= 400 )
1024 msg_Err( p_access, "error: %s", psz );
1032 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs );
1037 msg_Err( p_access, "failed to read answer" );
1041 /* msg_Dbg( p_input, "Line=%s", psz ); */
1049 if( ( p = strchr( psz, ':' ) ) == NULL )
1051 msg_Err( p_access, "malformed header line: %s", psz );
1056 while( *p == ' ' ) p++;
1058 if( !strcasecmp( psz, "Content-Length" ) )
1060 if( p_sys->b_continuous )
1062 p_access->info.i_size = -1;
1063 msg_Dbg( p_access, "this frame size="I64Fd, atoll(p ) );
1064 p_sys->i_remaining = atoll( p );
1068 p_access->info.i_size = i_tell + atoll( p );
1069 msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
1072 else if( !strcasecmp( psz, "Location" ) )
1074 if( p_sys->psz_location ) free( p_sys->psz_location );
1075 p_sys->psz_location = strdup( p );
1077 else if( !strcasecmp( psz, "Content-Type" ) )
1079 if( p_sys->psz_mime ) free( p_sys->psz_mime );
1080 p_sys->psz_mime = strdup( p );
1081 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1083 else if( !strcasecmp( psz, "Pragma" ) )
1085 if( !strcasecmp( psz, "Pragma: features" ) )
1086 p_sys->b_mms = VLC_TRUE;
1087 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
1088 p_sys->psz_pragma = strdup( p );
1089 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1091 else if( !strcasecmp( psz, "Server" ) )
1093 msg_Dbg( p_access, "Server: %s", p );
1094 if( !strncasecmp( p, "Icecast", 7 ) ||
1095 !strncasecmp( p, "Nanocaster", 10 ) )
1097 /* Remember if this is Icecast
1098 * we need to force demux in this case without breaking
1101 /* Let live 365 streams (nanocaster) piggyback on the icecast
1102 * routine. They look very similar */
1104 p_sys->b_reconnect = VLC_TRUE;
1105 p_sys->b_pace_control = VLC_FALSE;
1106 p_sys->b_icecast = VLC_TRUE;
1109 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1111 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1112 if( !strncasecmp( p, "chunked", 7 ) )
1114 p_sys->b_chunked = VLC_TRUE;
1117 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1119 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1120 p_sys->i_icy_meta = atoi( p );
1121 if( p_sys->i_icy_meta < 0 )
1122 p_sys->i_icy_meta = 0;
1124 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1126 else if( !strcasecmp( psz, "Icy-Name" ) )
1128 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
1129 p_sys->psz_icy_name = strdup( p );
1130 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1132 p_sys->b_icecast = VLC_TRUE; /* be on the safeside. set it here as well. */
1133 p_sys->b_reconnect = VLC_TRUE;
1134 p_sys->b_pace_control = VLC_FALSE;
1136 else if( !strcasecmp( psz, "Icy-Genre" ) )
1138 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
1139 p_sys->psz_icy_genre = strdup( p );
1140 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1142 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1144 msg_Dbg( p_access, "Icy-Notice: %s", p );
1146 else if( !strncasecmp( psz, "icy-", 4 ) ||
1147 !strncasecmp( psz, "ice-", 4 ) ||
1148 !strncasecmp( psz, "x-audiocast", 11 ) )
1150 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1158 Disconnect( p_access );
1159 return VLC_EGENERIC;
1162 /*****************************************************************************
1164 *****************************************************************************/
1165 static void Disconnect( access_t *p_access )
1167 access_sys_t *p_sys = p_access->p_sys;
1169 if( p_sys->p_tls != NULL)
1171 tls_ClientDelete( p_sys->p_tls );
1172 p_sys->p_tls = NULL;
1175 if( p_sys->fd != -1)
1177 net_Close(p_sys->fd);