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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
35 #include <vlc/input.h>
37 #include "vlc_interaction.h"
38 #include "vlc_playlist.h"
44 /*****************************************************************************
46 *****************************************************************************/
47 static int Open ( vlc_object_t * );
48 static void Close( vlc_object_t * );
50 #define PROXY_TEXT N_("HTTP proxy")
51 #define PROXY_LONGTEXT N_( \
52 "HTTP proxy to be usesd It must be of the form " \
53 "http://[user[:pass]@]myproxy.mydomain:myport/ ; " \
54 "if empty, the http_proxy environment variable will be tried." )
56 #define CACHING_TEXT N_("Caching value in ms")
57 #define CACHING_LONGTEXT N_( \
58 "Caching value for HTTP streams. This " \
59 "value should be set in milliseconds." )
61 #define AGENT_TEXT N_("HTTP user agent")
62 #define AGENT_LONGTEXT N_("User agent that will be " \
63 "used for the connection.")
65 #define RECONNECT_TEXT N_("Auto re-connect")
66 #define RECONNECT_LONGTEXT N_( \
67 "Automatically try to reconnect to the stream in case of a sudden " \
70 /// \bug missing space before you should
71 #define CONTINUOUS_TEXT N_("Continuous stream")
72 #define CONTINUOUS_LONGTEXT N_("Read a file that is " \
73 "being constantly updated (for example, a JPG file on a server)." \
74 "You should not globally enable this option as it will break all other " \
75 "types of HTTP streams." )
78 set_description( _("HTTP input") );
79 set_capability( "access2", 0 );
80 set_shortname( _( "HTTP(S)" ) );
81 set_category( CAT_INPUT );
82 set_subcategory( SUBCAT_INPUT_ACCESS );
84 add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
86 add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
87 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
88 add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
89 AGENT_LONGTEXT, VLC_TRUE );
90 add_bool( "http-reconnect", 0, NULL, RECONNECT_TEXT,
91 RECONNECT_LONGTEXT, VLC_TRUE );
92 add_bool( "http-continuous", 0, NULL, CONTINUOUS_TEXT,
93 CONTINUOUS_LONGTEXT, VLC_TRUE );
94 add_suppressed_string("http-user");
95 add_suppressed_string("http-pwd");
96 add_shortcut( "http" );
97 add_shortcut( "https" );
98 add_shortcut( "unsv" );
99 set_callbacks( Open, Close );
102 /*****************************************************************************
104 *****************************************************************************/
108 tls_session_t *p_tls;
113 char *psz_user_agent;
128 vlc_bool_t b_icecast;
131 vlc_bool_t b_chunked;
141 vlc_bool_t b_seekable;
142 vlc_bool_t b_reconnect;
143 vlc_bool_t b_continuous;
144 vlc_bool_t b_pace_control;
148 static int Read( access_t *, uint8_t *, int );
149 static int Seek( access_t *, int64_t );
150 static int Control( access_t *, int, va_list );
153 static int Connect( access_t *, int64_t );
154 static int Request( access_t *p_access, int64_t i_tell );
155 static void Disconnect( access_t * );
157 /*****************************************************************************
159 *****************************************************************************/
160 static int Open( vlc_object_t *p_this )
162 access_t *p_access = (access_t*)p_this;
166 /* Set up p_access */
167 STANDARD_READ_ACCESS_INIT;
169 p_sys->b_proxy = VLC_FALSE;
170 p_sys->i_version = 1;
171 p_sys->b_seekable = VLC_TRUE;
172 p_sys->psz_mime = NULL;
173 p_sys->psz_pragma = NULL;
174 p_sys->b_mms = VLC_FALSE;
175 p_sys->b_icecast = VLC_FALSE;
176 p_sys->psz_location = NULL;
177 p_sys->psz_user_agent = NULL;
178 p_sys->b_pace_control = VLC_TRUE;
179 p_sys->b_ssl = VLC_FALSE;
182 p_sys->i_icy_meta = 0;
183 p_sys->psz_icy_name = NULL;
184 p_sys->psz_icy_genre = NULL;
185 p_sys->psz_icy_title = NULL;
186 p_sys->i_remaining = 0;
189 /* Parse URI - remove spaces */
190 p = psz = strdup( p_access->psz_path );
191 while( (p = strchr( p, ' ' )) != NULL )
193 vlc_UrlParse( &p_sys->url, psz, 0 );
196 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
198 msg_Warn( p_access, "invalid host" );
201 if( !strncmp( p_access->psz_access, "https", 5 ) )
204 p_sys->b_ssl = VLC_TRUE;
205 if( p_sys->url.i_port <= 0 )
206 p_sys->url.i_port = 443;
210 if( p_sys->url.i_port <= 0 )
211 p_sys->url.i_port = 80;
215 p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
218 psz = var_CreateGetString( p_access, "http-proxy" );
221 p_sys->b_proxy = VLC_TRUE;
222 vlc_UrlParse( &p_sys->proxy, psz, 0 );
227 char *psz_proxy = getenv( "http_proxy" );
228 if( psz_proxy && *psz_proxy )
230 p_sys->b_proxy = VLC_TRUE;
231 vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
239 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
241 msg_Warn( p_access, "invalid proxy host" );
244 if( p_sys->proxy.i_port <= 0 )
246 p_sys->proxy.i_port = 80;
250 msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
251 p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
254 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
255 p_sys->proxy.i_port );
257 if( p_sys->url.psz_username && *p_sys->url.psz_username )
259 msg_Dbg( p_access, " user='%s', pwd='%s'",
260 p_sys->url.psz_username, p_sys->url.psz_password );
263 p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
264 p_sys->b_continuous = var_CreateGetBool( p_access, "http-continuous" );
268 if( Connect( p_access, 0 ) )
270 /* Retry with http 1.0 */
271 msg_Dbg( p_access, "switching to HTTP version 1.0" );
272 p_sys->i_version = 0;
273 p_sys->b_seekable = VLC_FALSE;
275 if( p_access->b_die ||
276 Connect( p_access, 0 ) )
282 if( p_sys->i_code == 401 )
284 char *psz_login = NULL; char *psz_password = NULL;
286 msg_Dbg( p_access, "authentication failed" );
287 i_ret = intf_UserLoginPassword( p_access, _("HTTP authentication"),
288 _("Please enter a valid login name and a password."),
289 &psz_login, &psz_password );
290 if( i_ret == DIALOG_OK_YES )
292 msg_Dbg( p_access, "retrying with user=%s, pwd=%s",
293 psz_login, psz_password );
294 if( psz_login ) p_sys->url.psz_username = strdup( psz_login );
295 if( psz_password ) p_sys->url.psz_password = strdup( psz_password );
296 if( psz_login ) free( psz_login );
297 if( psz_password ) free( psz_password );
302 if( psz_login ) free( psz_login );
303 if( psz_password ) free( psz_password );
308 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
309 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
310 p_sys->psz_location && *p_sys->psz_location )
312 playlist_t * p_playlist;
313 input_item_t *p_input_item;
315 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
317 /* Do not accept redirection outside of HTTP works */
318 if( strncmp( p_sys->psz_location, "http", 4 )
319 || ( ( p_sys->psz_location[4] != ':' ) /* HTTP */
320 && strncmp( p_sys->psz_location + 4, "s:", 2 ) /* HTTP/SSL */ ) )
322 msg_Err( p_access, "insecure redirection ignored" );
327 p_playlist = pl_Yield( p_access );
330 p_input_item = p_playlist->status.p_item->p_input;
331 vlc_mutex_lock( &p_input_item->lock );
332 free( p_input_item->psz_uri );
333 free( p_access->psz_path );
334 p_input_item->psz_uri = strdup( p_sys->psz_location );
335 p_access->psz_path = strdup( p_sys->psz_location );
336 vlc_mutex_unlock( &p_input_item->lock );
339 pl_Release( p_access );
341 /* Clean up current Open() run */
342 vlc_UrlClean( &p_sys->url );
343 vlc_UrlClean( &p_sys->proxy );
344 if( p_sys->psz_mime ) free( p_sys->psz_mime );
345 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
346 if( p_sys->psz_location ) free( p_sys->psz_location );
347 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
349 Disconnect( p_access );
352 /* Do new Open() run with new data */
353 return Open( p_this );
358 msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
362 if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
364 if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
366 if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
367 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
368 p_access->psz_demux = strdup( "nsv" );
369 else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
370 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
371 p_access->psz_demux = strdup( "m4a" );
372 else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
373 p_access->psz_demux = strdup( "mp3" );
375 msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
376 p_access->psz_demux );
378 #if 0 /* Doesn't work really well because of the pre-buffering in
379 * shoutcast servers (the buffer content will be sent as fast as
381 p_sys->b_pace_control = VLC_FALSE;
384 else if( !p_sys->psz_mime )
387 p_access->psz_demux = strdup( "mp3" );
389 /* else probably Ogg Vorbis */
391 else if( !strcasecmp( p_access->psz_access, "unsv" ) &&
393 !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
395 /* Grrrr! detect ultravox server and force NSV demuxer */
396 p_access->psz_demux = strdup( "nsv" );
398 else if( p_sys->psz_mime &&
399 !strncasecmp( p_sys->psz_mime, "application/xspf+xml", 20 ) &&
400 ( memchr( " ;\t", p_sys->psz_mime[20], 4 ) != NULL ) )
401 p_access->psz_demux = strdup( "xspf-open" );
403 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
406 var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
411 vlc_UrlClean( &p_sys->url );
412 vlc_UrlClean( &p_sys->proxy );
413 if( p_sys->psz_mime ) free( p_sys->psz_mime );
414 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
415 if( p_sys->psz_location ) free( p_sys->psz_location );
416 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
418 Disconnect( p_access );
423 /*****************************************************************************
425 *****************************************************************************/
426 static void Close( vlc_object_t *p_this )
428 access_t *p_access = (access_t*)p_this;
429 access_sys_t *p_sys = p_access->p_sys;
431 vlc_UrlClean( &p_sys->url );
432 vlc_UrlClean( &p_sys->proxy );
434 if( p_sys->psz_mime ) free( p_sys->psz_mime );
435 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
436 if( p_sys->psz_location ) free( p_sys->psz_location );
438 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
439 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
440 if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
442 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
444 Disconnect( p_access );
448 /*****************************************************************************
449 * Read: Read up to i_len bytes from the http connection and place in
450 * p_buffer. Return the actual number of bytes read
451 *****************************************************************************/
452 static int ReadICYMeta( access_t *p_access );
453 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
455 access_sys_t *p_sys = p_access->p_sys;
460 p_access->info.b_eof = VLC_TRUE;
464 if( p_access->info.i_size > 0 &&
465 i_len + p_access->info.i_pos > p_access->info.i_size )
467 if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
469 p_access->info.b_eof = VLC_TRUE;
474 if( p_sys->b_chunked )
476 if( p_sys->i_chunk < 0 )
478 p_access->info.b_eof = VLC_TRUE;
482 if( p_sys->i_chunk <= 0 )
484 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
485 /* read the chunk header */
488 /* fatal error - end of file */
489 msg_Dbg( p_access, "failed reading chunk-header line" );
492 p_sys->i_chunk = strtoll( psz, NULL, 16 );
495 if( p_sys->i_chunk <= 0 ) /* eof */
498 p_access->info.b_eof = VLC_TRUE;
503 if( i_len > p_sys->i_chunk )
505 i_len = p_sys->i_chunk;
509 if( p_sys->b_continuous && i_len > p_sys->i_remaining )
511 /* Only ask for the remaining length */
512 int i_new_len = p_sys->i_remaining;
515 Request( p_access, 0 );
516 i_read = Read( p_access, p_buffer, i_len );
522 if( p_sys->i_icy_meta > 0 && p_access->info.i_pos > 0 )
524 int64_t i_next = p_sys->i_icy_meta -
525 p_access->info.i_pos % p_sys->i_icy_meta;
527 if( i_next == p_sys->i_icy_meta )
529 if( ReadICYMeta( p_access ) )
531 p_access->info.b_eof = VLC_TRUE;
539 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, VLC_FALSE );
543 p_access->info.i_pos += i_read;
545 if( p_sys->b_chunked )
547 p_sys->i_chunk -= i_read;
548 if( p_sys->i_chunk <= 0 )
550 /* read the empty line */
551 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
552 if( psz ) free( psz );
556 else if( i_read == 0 )
559 * I very much doubt that this will work.
560 * If i_read == 0, the connection *IS* dead, so the only
561 * sensible thing to do is Disconnect() and then retry.
562 * Otherwise, I got recv() completely wrong. -- Courmisch
564 if( p_sys->b_continuous )
566 Request( p_access, 0 );
567 p_sys->b_continuous = VLC_FALSE;
568 i_read = Read( p_access, p_buffer, i_len );
569 p_sys->b_continuous = VLC_TRUE;
571 Disconnect( p_access );
572 if( p_sys->b_reconnect )
574 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
575 if( Connect( p_access, p_access->info.i_pos ) )
577 msg_Dbg( p_access, "reconnection failed" );
581 p_sys->b_reconnect = VLC_FALSE;
582 i_read = Read( p_access, p_buffer, i_len );
583 p_sys->b_reconnect = VLC_TRUE;
587 if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
590 if( p_sys->b_continuous )
592 p_sys->i_remaining -= i_read;
598 static int ReadICYMeta( access_t *p_access )
600 access_sys_t *p_sys = p_access->p_sys;
606 /* Read meta data length */
607 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
614 i_read = buffer << 4;
615 /* msg_Dbg( p_access, "ICY meta size=%u", i_read); */
617 psz_meta = malloc( i_read + 1 );
618 if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
619 (uint8_t *)psz_meta, i_read, VLC_TRUE ) != i_read )
622 psz_meta[i_read] = '\0'; /* Just in case */
624 /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
626 /* Now parse the meta */
627 /* Look for StreamTitle= */
628 p = strcasestr( (char *)psz_meta, "StreamTitle=" );
631 p += strlen( "StreamTitle=" );
632 if( *p == '\'' || *p == '"' )
634 char closing[] = { p[0], ';', '\0' };
635 char *psz = strstr( &p[1], closing );
637 psz = strchr( &p[1], ';' );
639 if( psz ) *psz = '\0';
643 char *psz = strchr( &p[1], ';' );
644 if( psz ) *psz = '\0';
647 if( !p_sys->psz_icy_title ||
648 strcmp( p_sys->psz_icy_title, &p[1] ) )
650 if( p_sys->psz_icy_title )
651 free( p_sys->psz_icy_title );
652 p_sys->psz_icy_title = strdup( &p[1] );
653 p_access->info.i_update |= INPUT_UPDATE_META;
655 msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
663 /*****************************************************************************
664 * Seek: close and re-open a connection at the right place
665 *****************************************************************************/
666 static int Seek( access_t *p_access, int64_t i_pos )
668 msg_Dbg( p_access, "trying to seek to "I64Fd, i_pos );
670 Disconnect( p_access );
672 if( Connect( p_access, i_pos ) )
674 msg_Err( p_access, "seek failed" );
675 p_access->info.b_eof = VLC_TRUE;
681 /*****************************************************************************
683 *****************************************************************************/
684 static int Control( access_t *p_access, int i_query, va_list args )
686 access_sys_t *p_sys = p_access->p_sys;
695 case ACCESS_CAN_SEEK:
696 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
697 *pb_bool = p_sys->b_seekable;
699 case ACCESS_CAN_FASTSEEK:
700 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
701 *pb_bool = VLC_FALSE;
703 case ACCESS_CAN_PAUSE:
704 case ACCESS_CAN_CONTROL_PACE:
705 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
707 #if 0 /* Disable for now until we have a clock synchro algo
708 * which works with something else than MPEG over UDP */
709 *pb_bool = p_sys->b_pace_control;
716 pi_int = (int*)va_arg( args, int * );
720 case ACCESS_GET_PTS_DELAY:
721 pi_64 = (int64_t*)va_arg( args, int64_t * );
722 *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
726 case ACCESS_SET_PAUSE_STATE:
729 case ACCESS_GET_META:
730 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
732 if( p_sys->psz_icy_name )
733 vlc_meta_SetTitle( p_meta, p_sys->psz_icy_name );
734 if( p_sys->psz_icy_genre )
735 vlc_meta_SetGenre( p_meta, p_sys->psz_icy_genre );
736 if( p_sys->psz_icy_title )
737 vlc_meta_SetNowPlaying( p_meta, p_sys->psz_icy_title );
740 case ACCESS_GET_TITLE_INFO:
741 case ACCESS_SET_TITLE:
742 case ACCESS_SET_SEEKPOINT:
743 case ACCESS_SET_PRIVATE_ID_STATE:
747 msg_Warn( p_access, "unimplemented query in control" );
754 /*****************************************************************************
756 *****************************************************************************/
757 static int Connect( access_t *p_access, int64_t i_tell )
759 access_sys_t *p_sys = p_access->p_sys;
760 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
763 if( p_sys->psz_location ) free( p_sys->psz_location );
764 if( p_sys->psz_mime ) free( p_sys->psz_mime );
765 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
767 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
768 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
769 if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
772 p_sys->psz_location = NULL;
773 p_sys->psz_mime = NULL;
774 p_sys->psz_pragma = NULL;
775 p_sys->b_mms = VLC_FALSE;
776 p_sys->b_chunked = VLC_FALSE;
778 p_sys->i_icy_meta = 0;
779 p_sys->psz_icy_name = NULL;
780 p_sys->psz_icy_genre = NULL;
781 p_sys->psz_icy_title = NULL;
783 p_access->info.i_size = 0;
784 p_access->info.i_pos = i_tell;
785 p_access->info.b_eof = VLC_FALSE;
788 /* Open connection */
789 p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
792 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
796 /* Initialize TLS/SSL session */
797 if( p_sys->b_ssl == VLC_TRUE )
799 /* CONNECT to establish TLS tunnel through HTTP proxy */
803 unsigned i_status = 0;
805 if( p_sys->i_version == 0 )
807 /* CONNECT is not in HTTP/1.0 */
808 Disconnect( p_access );
812 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
813 "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
814 p_sys->url.psz_host, p_sys->url.i_port,
816 p_sys->url.psz_host, p_sys->url.i_port);
818 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
821 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
822 Disconnect( p_access );
826 sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
829 if( ( i_status / 100 ) != 2 )
831 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
832 Disconnect( p_access );
838 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
841 msg_Err( p_access, "HTTP proxy connection failed" );
842 Disconnect( p_access );
854 /* TLS/SSL handshake */
855 p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
857 if( p_sys->p_tls == NULL )
859 msg_Err( p_access, "cannot establish HTTP/TLS session" );
860 Disconnect( p_access );
863 p_sys->p_vs = &p_sys->p_tls->sock;
866 return Request( p_access, i_tell );
870 static int Request( access_t *p_access, int64_t i_tell )
872 access_sys_t *p_sys = p_access->p_sys;
874 v_socket_t *pvs = p_sys->p_vs;
878 if( p_sys->url.psz_path )
880 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
881 "GET http://%s:%d%s HTTP/1.%d\r\n",
882 p_sys->url.psz_host, p_sys->url.i_port,
883 p_sys->url.psz_path, p_sys->i_version );
887 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
888 "GET http://%s:%d/ HTTP/1.%d\r\n",
889 p_sys->url.psz_host, p_sys->url.i_port,
895 char *psz_path = p_sys->url.psz_path;
896 if( !psz_path || !*psz_path )
900 if( p_sys->url.i_port != 80)
902 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
903 "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
904 psz_path, p_sys->i_version, p_sys->url.psz_host,
909 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
910 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
911 psz_path, p_sys->i_version, p_sys->url.psz_host );
915 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "User-Agent: %s\r\n",
916 p_sys->psz_user_agent );
918 if( p_sys->i_version == 1 )
920 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
921 "Range: bytes="I64Fd"-\r\n", i_tell );
925 if( p_sys->url.psz_username && *p_sys->url.psz_username )
930 asprintf( &buf, "%s:%s", p_sys->url.psz_username,
931 p_sys->url.psz_password ? p_sys->url.psz_password : "" );
933 b64 = vlc_b64_encode( buf );
936 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
937 "Authorization: Basic %s\r\n", b64 );
941 /* Proxy Authentication */
942 if( p_sys->proxy.psz_username && *p_sys->proxy.psz_username )
947 asprintf( &buf, "%s:%s", p_sys->proxy.psz_username,
948 p_sys->proxy.psz_password ? p_sys->proxy.psz_password : "" );
950 b64 = vlc_b64_encode( buf );
953 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
954 "Proxy-Authorization: Basic %s\r\n", b64 );
958 /* ICY meta data request */
959 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
962 if( p_sys->b_continuous )
964 net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
965 "Connection: Keep-Alive\r\n" );
967 else if( p_sys->i_version == 1 )
969 net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
970 "Connection: Close\r\n");
973 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "\r\n" ) < 0 )
975 msg_Err( p_access, "failed to send request" );
976 Disconnect( p_access );
981 if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs ) ) == NULL )
983 msg_Err( p_access, "failed to read answer" );
986 if( !strncmp( psz, "HTTP/1.", 7 ) )
988 p_sys->psz_protocol = "HTTP";
989 p_sys->i_code = atoi( &psz[9] );
991 else if( !strncmp( psz, "ICY", 3 ) )
993 p_sys->psz_protocol = "ICY";
994 p_sys->i_code = atoi( &psz[4] );
995 p_sys->b_reconnect = VLC_TRUE;
999 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1003 msg_Dbg( p_access, "protocol '%s' answer code %d",
1004 p_sys->psz_protocol, p_sys->i_code );
1005 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1007 p_sys->b_seekable = VLC_FALSE;
1009 if( p_sys->i_code != 206 )
1011 p_sys->b_seekable = VLC_FALSE;
1013 /* Authentication error - We'll have to display the dialog */
1014 if( p_sys->i_code == 401 )
1018 /* Other fatal error */
1019 else if( p_sys->i_code >= 400 )
1021 msg_Err( p_access, "error: %s", psz );
1029 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs );
1034 msg_Err( p_access, "failed to read answer" );
1038 /* msg_Dbg( p_input, "Line=%s", psz ); */
1046 if( ( p = strchr( psz, ':' ) ) == NULL )
1048 msg_Err( p_access, "malformed header line: %s", psz );
1053 while( *p == ' ' ) p++;
1055 if( !strcasecmp( psz, "Content-Length" ) )
1057 if( p_sys->b_continuous )
1059 p_access->info.i_size = -1;
1060 msg_Dbg( p_access, "this frame size="I64Fd, atoll(p ) );
1061 p_sys->i_remaining = atoll( p );
1065 p_access->info.i_size = i_tell + atoll( p );
1066 msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
1069 else if( !strcasecmp( psz, "Location" ) )
1071 if( p_sys->psz_location ) free( p_sys->psz_location );
1072 p_sys->psz_location = strdup( p );
1074 else if( !strcasecmp( psz, "Content-Type" ) )
1076 if( p_sys->psz_mime ) free( p_sys->psz_mime );
1077 p_sys->psz_mime = strdup( p );
1078 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1080 else if( !strcasecmp( psz, "Pragma" ) )
1082 if( !strcasecmp( psz, "Pragma: features" ) )
1083 p_sys->b_mms = VLC_TRUE;
1084 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
1085 p_sys->psz_pragma = strdup( p );
1086 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1088 else if( !strcasecmp( psz, "Server" ) )
1090 msg_Dbg( p_access, "Server: %s", p );
1091 if( !strncasecmp( p, "Icecast", 7 ) ||
1092 !strncasecmp( p, "Nanocaster", 10 ) )
1094 /* Remember if this is Icecast
1095 * we need to force demux in this case without breaking
1098 /* Let live 365 streams (nanocaster) piggyback on the icecast
1099 * routine. They look very similar */
1101 p_sys->b_reconnect = VLC_TRUE;
1102 p_sys->b_pace_control = VLC_FALSE;
1103 p_sys->b_icecast = VLC_TRUE;
1106 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1108 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1109 if( !strncasecmp( p, "chunked", 7 ) )
1111 p_sys->b_chunked = VLC_TRUE;
1114 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1116 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1117 p_sys->i_icy_meta = atoi( p );
1118 if( p_sys->i_icy_meta < 0 )
1119 p_sys->i_icy_meta = 0;
1121 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1123 else if( !strcasecmp( psz, "Icy-Name" ) )
1125 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
1126 p_sys->psz_icy_name = strdup( p );
1127 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1129 p_sys->b_icecast = VLC_TRUE; /* be on the safeside. set it here as well. */
1130 p_sys->b_reconnect = VLC_TRUE;
1131 p_sys->b_pace_control = VLC_FALSE;
1133 else if( !strcasecmp( psz, "Icy-Genre" ) )
1135 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
1136 p_sys->psz_icy_genre = strdup( p );
1137 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1139 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1141 msg_Dbg( p_access, "Icy-Notice: %s", p );
1143 else if( !strncasecmp( psz, "icy-", 4 ) ||
1144 !strncasecmp( psz, "ice-", 4 ) ||
1145 !strncasecmp( psz, "x-audiocast", 11 ) )
1147 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1155 Disconnect( p_access );
1156 return VLC_EGENERIC;
1159 /*****************************************************************************
1161 *****************************************************************************/
1162 static void Disconnect( access_t *p_access )
1164 access_sys_t *p_sys = p_access->p_sys;
1166 if( p_sys->p_tls != NULL)
1168 tls_ClientDelete( p_sys->p_tls );
1169 p_sys->p_tls = NULL;
1172 if( p_sys->fd != -1)
1174 net_Close(p_sys->fd);