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_access.h>
37 #include <vlc_interface.h>
38 #include <vlc_playlist.h>
40 #include <vlc_network.h>
43 #include <vlc_strings.h>
45 /*****************************************************************************
47 *****************************************************************************/
48 static int Open ( vlc_object_t * );
49 static void Close( vlc_object_t * );
51 #define PROXY_TEXT N_("HTTP proxy")
52 #define PROXY_LONGTEXT N_( \
53 "HTTP proxy to be usesd It must be of the form " \
54 "http://[user[:pass]@]myproxy.mydomain:myport/ ; " \
55 "if empty, the http_proxy environment variable will be tried." )
57 #define CACHING_TEXT N_("Caching value in ms")
58 #define CACHING_LONGTEXT N_( \
59 "Caching value for HTTP streams. This " \
60 "value should be set in milliseconds." )
62 #define AGENT_TEXT N_("HTTP user agent")
63 #define AGENT_LONGTEXT N_("User agent that will be " \
64 "used for the connection.")
66 #define RECONNECT_TEXT N_("Auto re-connect")
67 #define RECONNECT_LONGTEXT N_( \
68 "Automatically try to reconnect to the stream in case of a sudden " \
71 /// \bug missing space before you should
72 #define CONTINUOUS_TEXT N_("Continuous stream")
73 #define CONTINUOUS_LONGTEXT N_("Read a file that is " \
74 "being constantly updated (for example, a JPG file on a server)." \
75 "You should not globally enable this option as it will break all other " \
76 "types of HTTP streams." )
79 set_description( _("HTTP input") );
80 set_capability( "access2", 0 );
81 set_shortname( _( "HTTP(S)" ) );
82 set_category( CAT_INPUT );
83 set_subcategory( SUBCAT_INPUT_ACCESS );
85 add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
87 add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
88 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
89 add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
90 AGENT_LONGTEXT, VLC_TRUE );
91 add_bool( "http-reconnect", 0, NULL, RECONNECT_TEXT,
92 RECONNECT_LONGTEXT, VLC_TRUE );
93 add_bool( "http-continuous", 0, NULL, CONTINUOUS_TEXT,
94 CONTINUOUS_LONGTEXT, VLC_TRUE );
95 add_suppressed_string("http-user");
96 add_suppressed_string("http-pwd");
97 add_shortcut( "http" );
98 add_shortcut( "https" );
99 add_shortcut( "unsv" );
100 set_callbacks( Open, Close );
103 /*****************************************************************************
105 *****************************************************************************/
109 tls_session_t *p_tls;
114 char *psz_user_agent;
129 vlc_bool_t b_icecast;
132 vlc_bool_t b_chunked;
142 vlc_bool_t b_seekable;
143 vlc_bool_t b_reconnect;
144 vlc_bool_t b_continuous;
145 vlc_bool_t b_pace_control;
149 static int Read( access_t *, uint8_t *, int );
150 static int Seek( access_t *, int64_t );
151 static int Control( access_t *, int, va_list );
154 static int Connect( access_t *, int64_t );
155 static int Request( access_t *p_access, int64_t i_tell );
156 static void Disconnect( access_t * );
158 /*****************************************************************************
160 *****************************************************************************/
161 static int Open( vlc_object_t *p_this )
163 access_t *p_access = (access_t*)p_this;
167 /* Set up p_access */
168 STANDARD_READ_ACCESS_INIT;
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 /* Parse URI - remove spaces */
191 p = psz = strdup( p_access->psz_path );
192 while( (p = strchr( p, ' ' )) != NULL )
194 vlc_UrlParse( &p_sys->url, psz, 0 );
197 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
199 msg_Warn( p_access, "invalid host" );
202 if( !strncmp( p_access->psz_access, "https", 5 ) )
205 p_sys->b_ssl = VLC_TRUE;
206 if( p_sys->url.i_port <= 0 )
207 p_sys->url.i_port = 443;
211 if( p_sys->url.i_port <= 0 )
212 p_sys->url.i_port = 80;
216 p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
219 psz = var_CreateGetString( p_access, "http-proxy" );
222 p_sys->b_proxy = VLC_TRUE;
223 vlc_UrlParse( &p_sys->proxy, psz, 0 );
228 char *psz_proxy = getenv( "http_proxy" );
229 if( psz_proxy && *psz_proxy )
231 p_sys->b_proxy = VLC_TRUE;
232 vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
240 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
242 msg_Warn( p_access, "invalid proxy host" );
245 if( p_sys->proxy.i_port <= 0 )
247 p_sys->proxy.i_port = 80;
251 msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
252 p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
255 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
256 p_sys->proxy.i_port );
258 if( p_sys->url.psz_username && *p_sys->url.psz_username )
260 msg_Dbg( p_access, " user='%s', pwd='%s'",
261 p_sys->url.psz_username, p_sys->url.psz_password );
264 p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
265 p_sys->b_continuous = var_CreateGetBool( p_access, "http-continuous" );
269 switch( Connect( p_access, 0 ) )
275 /* Retry with http 1.0 */
276 msg_Dbg( p_access, "switching to HTTP version 1.0" );
277 p_sys->i_version = 0;
278 p_sys->b_seekable = VLC_FALSE;
280 if( p_access->b_die || Connect( p_access, 0 ) )
288 msg_Err( p_access, "You should not be here" );
293 if( p_sys->i_code == 401 )
295 char *psz_login = NULL; char *psz_password = NULL;
297 msg_Dbg( p_access, "authentication failed" );
298 i_ret = intf_UserLoginPassword( p_access, _("HTTP authentication"),
299 _("Please enter a valid login name and a password."),
300 &psz_login, &psz_password );
301 if( i_ret == DIALOG_OK_YES )
303 msg_Dbg( p_access, "retrying with user=%s, pwd=%s",
304 psz_login, psz_password );
305 if( psz_login ) p_sys->url.psz_username = strdup( psz_login );
306 if( psz_password ) p_sys->url.psz_password = strdup( psz_password );
307 if( psz_login ) free( psz_login );
308 if( psz_password ) free( psz_password );
313 if( psz_login ) free( psz_login );
314 if( psz_password ) free( psz_password );
319 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
320 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
321 p_sys->psz_location && *p_sys->psz_location )
323 playlist_t * p_playlist;
324 input_item_t *p_input_item;
326 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
328 /* Do not accept redirection outside of HTTP works */
329 if( strncmp( p_sys->psz_location, "http", 4 )
330 || ( ( p_sys->psz_location[4] != ':' ) /* HTTP */
331 && strncmp( p_sys->psz_location + 4, "s:", 2 ) /* HTTP/SSL */ ) )
333 msg_Err( p_access, "insecure redirection ignored" );
338 p_playlist = pl_Yield( p_access );
341 p_input_item = p_playlist->status.p_item->p_input;
342 vlc_mutex_lock( &p_input_item->lock );
343 free( p_input_item->psz_uri );
344 free( p_access->psz_path );
345 p_input_item->psz_uri = strdup( p_sys->psz_location );
346 p_access->psz_path = strdup( p_sys->psz_location );
347 vlc_mutex_unlock( &p_input_item->lock );
350 pl_Release( p_access );
352 /* Clean up current Open() run */
353 vlc_UrlClean( &p_sys->url );
354 vlc_UrlClean( &p_sys->proxy );
355 if( p_sys->psz_mime ) free( p_sys->psz_mime );
356 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
357 if( p_sys->psz_location ) free( p_sys->psz_location );
358 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
360 Disconnect( p_access );
363 /* Do new Open() run with new data */
364 return Open( p_this );
369 msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
373 if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
375 if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
377 if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
378 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
379 p_access->psz_demux = strdup( "nsv" );
380 else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
381 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
382 p_access->psz_demux = strdup( "m4a" );
383 else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
384 p_access->psz_demux = strdup( "mp3" );
386 msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
387 p_access->psz_demux );
389 #if 0 /* Doesn't work really well because of the pre-buffering in
390 * shoutcast servers (the buffer content will be sent as fast as
392 p_sys->b_pace_control = VLC_FALSE;
395 else if( !p_sys->psz_mime )
398 p_access->psz_demux = strdup( "mp3" );
400 /* else probably Ogg Vorbis */
402 else if( !strcasecmp( p_access->psz_access, "unsv" ) &&
404 !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
406 /* Grrrr! detect ultravox server and force NSV demuxer */
407 p_access->psz_demux = strdup( "nsv" );
409 else if( p_sys->psz_mime &&
410 !strncasecmp( p_sys->psz_mime, "application/xspf+xml", 20 ) &&
411 ( memchr( " ;\t", p_sys->psz_mime[20], 4 ) != NULL ) )
412 p_access->psz_demux = strdup( "xspf-open" );
414 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
417 var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
422 vlc_UrlClean( &p_sys->url );
423 vlc_UrlClean( &p_sys->proxy );
424 if( p_sys->psz_mime ) free( p_sys->psz_mime );
425 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
426 if( p_sys->psz_location ) free( p_sys->psz_location );
427 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
429 Disconnect( p_access );
434 /*****************************************************************************
436 *****************************************************************************/
437 static void Close( vlc_object_t *p_this )
439 access_t *p_access = (access_t*)p_this;
440 access_sys_t *p_sys = p_access->p_sys;
442 vlc_UrlClean( &p_sys->url );
443 vlc_UrlClean( &p_sys->proxy );
445 if( p_sys->psz_mime ) free( p_sys->psz_mime );
446 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
447 if( p_sys->psz_location ) free( p_sys->psz_location );
449 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
450 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
451 if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
453 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
455 Disconnect( p_access );
459 /*****************************************************************************
460 * Read: Read up to i_len bytes from the http connection and place in
461 * p_buffer. Return the actual number of bytes read
462 *****************************************************************************/
463 static int ReadICYMeta( access_t *p_access );
464 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
466 access_sys_t *p_sys = p_access->p_sys;
471 p_access->info.b_eof = VLC_TRUE;
475 if( p_access->info.i_size > 0 &&
476 i_len + p_access->info.i_pos > p_access->info.i_size )
478 if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
480 p_access->info.b_eof = VLC_TRUE;
485 if( p_sys->b_chunked )
487 if( p_sys->i_chunk < 0 )
489 p_access->info.b_eof = VLC_TRUE;
493 if( p_sys->i_chunk <= 0 )
495 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
496 /* read the chunk header */
499 /* fatal error - end of file */
500 msg_Dbg( p_access, "failed reading chunk-header line" );
503 p_sys->i_chunk = strtoll( psz, NULL, 16 );
506 if( p_sys->i_chunk <= 0 ) /* eof */
509 p_access->info.b_eof = VLC_TRUE;
514 if( i_len > p_sys->i_chunk )
516 i_len = p_sys->i_chunk;
520 if( p_sys->b_continuous && i_len > p_sys->i_remaining )
522 /* Only ask for the remaining length */
523 int i_new_len = p_sys->i_remaining;
526 Request( p_access, 0 );
527 i_read = Read( p_access, p_buffer, i_len );
533 if( p_sys->i_icy_meta > 0 && p_access->info.i_pos > 0 )
535 int64_t i_next = p_sys->i_icy_meta -
536 p_access->info.i_pos % p_sys->i_icy_meta;
538 if( i_next == p_sys->i_icy_meta )
540 if( ReadICYMeta( p_access ) )
542 p_access->info.b_eof = VLC_TRUE;
550 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, VLC_FALSE );
554 p_access->info.i_pos += i_read;
556 if( p_sys->b_chunked )
558 p_sys->i_chunk -= i_read;
559 if( p_sys->i_chunk <= 0 )
561 /* read the empty line */
562 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
563 if( psz ) free( psz );
567 else if( i_read == 0 )
570 * I very much doubt that this will work.
571 * If i_read == 0, the connection *IS* dead, so the only
572 * sensible thing to do is Disconnect() and then retry.
573 * Otherwise, I got recv() completely wrong. -- Courmisch
575 if( p_sys->b_continuous )
577 Request( p_access, 0 );
578 p_sys->b_continuous = VLC_FALSE;
579 i_read = Read( p_access, p_buffer, i_len );
580 p_sys->b_continuous = VLC_TRUE;
582 Disconnect( p_access );
583 if( p_sys->b_reconnect )
585 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
586 if( Connect( p_access, p_access->info.i_pos ) )
588 msg_Dbg( p_access, "reconnection failed" );
592 p_sys->b_reconnect = VLC_FALSE;
593 i_read = Read( p_access, p_buffer, i_len );
594 p_sys->b_reconnect = VLC_TRUE;
598 if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
601 if( p_sys->b_continuous )
603 p_sys->i_remaining -= i_read;
609 static int ReadICYMeta( access_t *p_access )
611 access_sys_t *p_sys = p_access->p_sys;
617 /* Read meta data length */
618 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
625 i_read = buffer << 4;
626 /* msg_Dbg( p_access, "ICY meta size=%u", i_read); */
628 psz_meta = malloc( i_read + 1 );
629 if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
630 (uint8_t *)psz_meta, i_read, VLC_TRUE ) != i_read )
633 psz_meta[i_read] = '\0'; /* Just in case */
635 /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
637 /* Now parse the meta */
638 /* Look for StreamTitle= */
639 p = strcasestr( (char *)psz_meta, "StreamTitle=" );
642 p += strlen( "StreamTitle=" );
643 if( *p == '\'' || *p == '"' )
645 char closing[] = { p[0], ';', '\0' };
646 char *psz = strstr( &p[1], closing );
648 psz = strchr( &p[1], ';' );
650 if( psz ) *psz = '\0';
654 char *psz = strchr( &p[1], ';' );
655 if( psz ) *psz = '\0';
658 if( !p_sys->psz_icy_title ||
659 strcmp( p_sys->psz_icy_title, &p[1] ) )
661 if( p_sys->psz_icy_title )
662 free( p_sys->psz_icy_title );
663 p_sys->psz_icy_title = strdup( &p[1] );
664 p_access->info.i_update |= INPUT_UPDATE_META;
666 msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
674 /*****************************************************************************
675 * Seek: close and re-open a connection at the right place
676 *****************************************************************************/
677 static int Seek( access_t *p_access, int64_t i_pos )
679 msg_Dbg( p_access, "trying to seek to "I64Fd, i_pos );
681 Disconnect( p_access );
683 if( Connect( p_access, i_pos ) )
685 msg_Err( p_access, "seek failed" );
686 p_access->info.b_eof = VLC_TRUE;
692 /*****************************************************************************
694 *****************************************************************************/
695 static int Control( access_t *p_access, int i_query, va_list args )
697 access_sys_t *p_sys = p_access->p_sys;
706 case ACCESS_CAN_SEEK:
707 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
708 *pb_bool = p_sys->b_seekable;
710 case ACCESS_CAN_FASTSEEK:
711 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
712 *pb_bool = VLC_FALSE;
714 case ACCESS_CAN_PAUSE:
715 case ACCESS_CAN_CONTROL_PACE:
716 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
718 #if 0 /* Disable for now until we have a clock synchro algo
719 * which works with something else than MPEG over UDP */
720 *pb_bool = p_sys->b_pace_control;
727 pi_int = (int*)va_arg( args, int * );
731 case ACCESS_GET_PTS_DELAY:
732 pi_64 = (int64_t*)va_arg( args, int64_t * );
733 *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
737 case ACCESS_SET_PAUSE_STATE:
740 case ACCESS_GET_META:
741 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
743 if( p_sys->psz_icy_name )
744 vlc_meta_SetTitle( p_meta, p_sys->psz_icy_name );
745 if( p_sys->psz_icy_genre )
746 vlc_meta_SetGenre( p_meta, p_sys->psz_icy_genre );
747 if( p_sys->psz_icy_title )
748 vlc_meta_SetNowPlaying( p_meta, p_sys->psz_icy_title );
751 case ACCESS_GET_TITLE_INFO:
752 case ACCESS_SET_TITLE:
753 case ACCESS_SET_SEEKPOINT:
754 case ACCESS_SET_PRIVATE_ID_STATE:
758 msg_Warn( p_access, "unimplemented query in control" );
765 /*****************************************************************************
767 *****************************************************************************/
768 static int Connect( access_t *p_access, int64_t i_tell )
770 access_sys_t *p_sys = p_access->p_sys;
771 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
774 if( p_sys->psz_location ) free( p_sys->psz_location );
775 if( p_sys->psz_mime ) free( p_sys->psz_mime );
776 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
778 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
779 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
780 if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
783 p_sys->psz_location = NULL;
784 p_sys->psz_mime = NULL;
785 p_sys->psz_pragma = NULL;
786 p_sys->b_mms = VLC_FALSE;
787 p_sys->b_chunked = VLC_FALSE;
789 p_sys->i_icy_meta = 0;
790 p_sys->psz_icy_name = NULL;
791 p_sys->psz_icy_genre = NULL;
792 p_sys->psz_icy_title = NULL;
794 p_access->info.i_size = 0;
795 p_access->info.i_pos = i_tell;
796 p_access->info.b_eof = VLC_FALSE;
799 /* Open connection */
800 p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
801 if( p_sys->fd == -1 )
803 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
807 /* Initialize TLS/SSL session */
808 if( p_sys->b_ssl == VLC_TRUE )
810 /* CONNECT to establish TLS tunnel through HTTP proxy */
814 unsigned i_status = 0;
816 if( p_sys->i_version == 0 )
818 /* CONNECT is not in HTTP/1.0 */
819 Disconnect( p_access );
823 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
824 "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
825 p_sys->url.psz_host, p_sys->url.i_port,
827 p_sys->url.psz_host, p_sys->url.i_port);
829 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
832 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
833 Disconnect( p_access );
837 sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
840 if( ( i_status / 100 ) != 2 )
842 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
843 Disconnect( p_access );
849 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
852 msg_Err( p_access, "HTTP proxy connection failed" );
853 Disconnect( p_access );
865 /* TLS/SSL handshake */
866 p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
868 if( p_sys->p_tls == NULL )
870 msg_Err( p_access, "cannot establish HTTP/TLS session" );
871 Disconnect( p_access );
874 p_sys->p_vs = &p_sys->p_tls->sock;
877 return Request( p_access, i_tell ) ? -2 : 0;
881 static int Request( access_t *p_access, int64_t i_tell )
883 access_sys_t *p_sys = p_access->p_sys;
885 v_socket_t *pvs = p_sys->p_vs;
889 if( p_sys->url.psz_path )
891 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
892 "GET http://%s:%d%s HTTP/1.%d\r\n",
893 p_sys->url.psz_host, p_sys->url.i_port,
894 p_sys->url.psz_path, p_sys->i_version );
898 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
899 "GET http://%s:%d/ HTTP/1.%d\r\n",
900 p_sys->url.psz_host, p_sys->url.i_port,
906 const char *psz_path = p_sys->url.psz_path;
907 if( !psz_path || !*psz_path )
911 if( p_sys->url.i_port != 80)
913 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
914 "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
915 psz_path, p_sys->i_version, p_sys->url.psz_host,
920 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
921 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
922 psz_path, p_sys->i_version, p_sys->url.psz_host );
926 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "User-Agent: %s\r\n",
927 p_sys->psz_user_agent );
929 if( p_sys->i_version == 1 )
931 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
932 "Range: bytes="I64Fd"-\r\n", i_tell );
936 if( p_sys->url.psz_username && *p_sys->url.psz_username )
941 asprintf( &buf, "%s:%s", p_sys->url.psz_username,
942 p_sys->url.psz_password ? p_sys->url.psz_password : "" );
944 b64 = vlc_b64_encode( buf );
947 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
948 "Authorization: Basic %s\r\n", b64 );
952 /* Proxy Authentication */
953 if( p_sys->proxy.psz_username && *p_sys->proxy.psz_username )
958 asprintf( &buf, "%s:%s", p_sys->proxy.psz_username,
959 p_sys->proxy.psz_password ? p_sys->proxy.psz_password : "" );
961 b64 = vlc_b64_encode( buf );
964 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
965 "Proxy-Authorization: Basic %s\r\n", b64 );
969 /* ICY meta data request */
970 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
973 if( p_sys->b_continuous )
975 net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
976 "Connection: Keep-Alive\r\n" );
978 else if( p_sys->i_version == 1 )
980 net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
981 "Connection: Close\r\n");
984 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "\r\n" ) < 0 )
986 msg_Err( p_access, "failed to send request" );
987 Disconnect( p_access );
992 if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs ) ) == NULL )
994 msg_Err( p_access, "failed to read answer" );
997 if( !strncmp( psz, "HTTP/1.", 7 ) )
999 p_sys->psz_protocol = "HTTP";
1000 p_sys->i_code = atoi( &psz[9] );
1002 else if( !strncmp( psz, "ICY", 3 ) )
1004 p_sys->psz_protocol = "ICY";
1005 p_sys->i_code = atoi( &psz[4] );
1006 p_sys->b_reconnect = VLC_TRUE;
1010 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1014 msg_Dbg( p_access, "protocol '%s' answer code %d",
1015 p_sys->psz_protocol, p_sys->i_code );
1016 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1018 p_sys->b_seekable = VLC_FALSE;
1020 if( p_sys->i_code != 206 )
1022 p_sys->b_seekable = VLC_FALSE;
1024 /* Authentication error - We'll have to display the dialog */
1025 if( p_sys->i_code == 401 )
1029 /* Other fatal error */
1030 else if( p_sys->i_code >= 400 )
1032 msg_Err( p_access, "error: %s", psz );
1040 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs );
1045 msg_Err( p_access, "failed to read answer" );
1049 /* msg_Dbg( p_input, "Line=%s", psz ); */
1057 if( ( p = strchr( psz, ':' ) ) == NULL )
1059 msg_Err( p_access, "malformed header line: %s", psz );
1064 while( *p == ' ' ) p++;
1066 if( !strcasecmp( psz, "Content-Length" ) )
1068 if( p_sys->b_continuous )
1070 p_access->info.i_size = -1;
1071 msg_Dbg( p_access, "this frame size="I64Fd, atoll(p ) );
1072 p_sys->i_remaining = atoll( p );
1076 p_access->info.i_size = i_tell + atoll( p );
1077 msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
1080 else if( !strcasecmp( psz, "Location" ) )
1082 if( p_sys->psz_location ) free( p_sys->psz_location );
1083 p_sys->psz_location = strdup( p );
1085 else if( !strcasecmp( psz, "Content-Type" ) )
1087 if( p_sys->psz_mime ) free( p_sys->psz_mime );
1088 p_sys->psz_mime = strdup( p );
1089 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1091 else if( !strcasecmp( psz, "Pragma" ) )
1093 if( !strcasecmp( psz, "Pragma: features" ) )
1094 p_sys->b_mms = VLC_TRUE;
1095 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
1096 p_sys->psz_pragma = strdup( p );
1097 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1099 else if( !strcasecmp( psz, "Server" ) )
1101 msg_Dbg( p_access, "Server: %s", p );
1102 if( !strncasecmp( p, "Icecast", 7 ) ||
1103 !strncasecmp( p, "Nanocaster", 10 ) )
1105 /* Remember if this is Icecast
1106 * we need to force demux in this case without breaking
1109 /* Let live 365 streams (nanocaster) piggyback on the icecast
1110 * routine. They look very similar */
1112 p_sys->b_reconnect = VLC_TRUE;
1113 p_sys->b_pace_control = VLC_FALSE;
1114 p_sys->b_icecast = VLC_TRUE;
1117 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1119 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1120 if( !strncasecmp( p, "chunked", 7 ) )
1122 p_sys->b_chunked = VLC_TRUE;
1125 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1127 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1128 p_sys->i_icy_meta = atoi( p );
1129 if( p_sys->i_icy_meta < 0 )
1130 p_sys->i_icy_meta = 0;
1132 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1134 else if( !strcasecmp( psz, "Icy-Name" ) )
1136 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
1137 p_sys->psz_icy_name = strdup( p );
1138 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1140 p_sys->b_icecast = VLC_TRUE; /* be on the safeside. set it here as well. */
1141 p_sys->b_reconnect = VLC_TRUE;
1142 p_sys->b_pace_control = VLC_FALSE;
1144 else if( !strcasecmp( psz, "Icy-Genre" ) )
1146 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
1147 p_sys->psz_icy_genre = strdup( p );
1148 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1150 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1152 msg_Dbg( p_access, "Icy-Notice: %s", p );
1154 else if( !strncasecmp( psz, "icy-", 4 ) ||
1155 !strncasecmp( psz, "ice-", 4 ) ||
1156 !strncasecmp( psz, "x-audiocast", 11 ) )
1158 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1166 Disconnect( p_access );
1167 return VLC_EGENERIC;
1170 /*****************************************************************************
1172 *****************************************************************************/
1173 static void Disconnect( access_t *p_access )
1175 access_sys_t *p_sys = p_access->p_sys;
1177 if( p_sys->p_tls != NULL)
1179 tls_ClientDelete( p_sys->p_tls );
1180 p_sys->p_tls = NULL;
1183 if( p_sys->fd != -1)
1185 net_Close(p_sys->fd);