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 *****************************************************************************/
32 #include <vlc_access.h>
34 #include <vlc_interface.h>
35 #include <vlc_playlist.h>
37 #include <vlc_network.h>
40 #include <vlc_strings.h>
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Open ( vlc_object_t * );
46 static void Close( vlc_object_t * );
48 #define PROXY_TEXT N_("HTTP proxy")
49 #define PROXY_LONGTEXT N_( \
50 "HTTP proxy to be used It must be of the form " \
51 "http://[user[:pass]@]myproxy.mydomain:myport/ ; " \
52 "if empty, the http_proxy environment variable will be tried." )
54 #define CACHING_TEXT N_("Caching value in ms")
55 #define CACHING_LONGTEXT N_( \
56 "Caching value for HTTP streams. This " \
57 "value should be set in milliseconds." )
59 #define AGENT_TEXT N_("HTTP user agent")
60 #define AGENT_LONGTEXT N_("User agent that will be " \
61 "used for the connection.")
63 #define RECONNECT_TEXT N_("Auto re-connect")
64 #define RECONNECT_LONGTEXT N_( \
65 "Automatically try to reconnect to the stream in case of a sudden " \
68 #define CONTINUOUS_TEXT N_("Continuous stream")
69 #define CONTINUOUS_LONGTEXT N_("Read a file that is " \
70 "being constantly updated (for example, a JPG file on a server). " \
71 "You should not globally enable this option as it will break all other " \
72 "types of HTTP streams." )
75 set_description( _("HTTP input") );
76 set_capability( "access2", 0 );
77 set_shortname( _( "HTTP(S)" ) );
78 set_category( CAT_INPUT );
79 set_subcategory( SUBCAT_INPUT_ACCESS );
81 add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
83 add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
84 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
85 add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
86 AGENT_LONGTEXT, VLC_TRUE );
87 add_bool( "http-reconnect", 0, NULL, RECONNECT_TEXT,
88 RECONNECT_LONGTEXT, VLC_TRUE );
89 add_bool( "http-continuous", 0, NULL, CONTINUOUS_TEXT,
90 CONTINUOUS_LONGTEXT, VLC_TRUE );
91 add_obsolete_string("http-user");
92 add_obsolete_string("http-pwd");
93 add_shortcut( "http" );
94 add_shortcut( "https" );
95 add_shortcut( "unsv" );
96 add_shortcut( "itpc" ); /* iTunes Podcast */
97 set_callbacks( Open, Close );
100 /*****************************************************************************
102 *****************************************************************************/
106 tls_session_t *p_tls;
111 char *psz_user_agent;
119 const char *psz_protocol;
126 vlc_bool_t b_icecast;
129 vlc_bool_t b_chunked;
139 vlc_bool_t b_seekable;
140 vlc_bool_t b_reconnect;
141 vlc_bool_t b_continuous;
142 vlc_bool_t b_pace_control;
146 static int Read( access_t *, uint8_t *, int );
147 static int Seek( access_t *, int64_t );
148 static int Control( access_t *, int, va_list );
151 static int Connect( access_t *, int64_t );
152 static int Request( access_t *p_access, int64_t i_tell );
153 static void Disconnect( access_t * );
155 /*****************************************************************************
157 *****************************************************************************/
158 static int Open( vlc_object_t *p_this )
160 access_t *p_access = (access_t*)p_this;
164 /* Set up p_access */
165 STANDARD_READ_ACCESS_INIT;
167 p_sys->b_proxy = VLC_FALSE;
168 p_sys->i_version = 1;
169 p_sys->b_seekable = VLC_TRUE;
170 p_sys->psz_mime = NULL;
171 p_sys->psz_pragma = NULL;
172 p_sys->b_mms = VLC_FALSE;
173 p_sys->b_icecast = VLC_FALSE;
174 p_sys->psz_location = NULL;
175 p_sys->psz_user_agent = NULL;
176 p_sys->b_pace_control = VLC_TRUE;
177 p_sys->b_ssl = VLC_FALSE;
180 p_sys->i_icy_meta = 0;
181 p_sys->psz_icy_name = NULL;
182 p_sys->psz_icy_genre = NULL;
183 p_sys->psz_icy_title = NULL;
184 p_sys->i_remaining = 0;
187 /* Parse URI - remove spaces */
188 p = psz = strdup( p_access->psz_path );
189 while( (p = strchr( p, ' ' )) != NULL )
191 vlc_UrlParse( &p_sys->url, psz, 0 );
194 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
196 msg_Warn( p_access, "invalid host" );
199 if( !strncmp( p_access->psz_access, "https", 5 ) )
202 p_sys->b_ssl = VLC_TRUE;
203 if( p_sys->url.i_port <= 0 )
204 p_sys->url.i_port = 443;
208 if( p_sys->url.i_port <= 0 )
209 p_sys->url.i_port = 80;
213 p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
216 psz = var_CreateGetString( p_access, "http-proxy" );
219 p_sys->b_proxy = VLC_TRUE;
220 vlc_UrlParse( &p_sys->proxy, psz, 0 );
225 char *psz_proxy = getenv( "http_proxy" );
226 if( psz_proxy && *psz_proxy )
228 p_sys->b_proxy = VLC_TRUE;
229 vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
237 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
239 msg_Warn( p_access, "invalid proxy host" );
242 if( p_sys->proxy.i_port <= 0 )
244 p_sys->proxy.i_port = 80;
248 msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
249 p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
252 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
253 p_sys->proxy.i_port );
255 if( p_sys->url.psz_username && *p_sys->url.psz_username )
257 msg_Dbg( p_access, " user='%s', pwd='%s'",
258 p_sys->url.psz_username, p_sys->url.psz_password );
261 p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
262 p_sys->b_continuous = var_CreateGetBool( p_access, "http-continuous" );
266 switch( Connect( p_access, 0 ) )
272 /* Retry with http 1.0 */
273 msg_Dbg( p_access, "switching to HTTP version 1.0" );
274 p_sys->i_version = 0;
275 p_sys->b_seekable = VLC_FALSE;
277 if( p_access->b_die || Connect( p_access, 0 ) )
285 msg_Err( p_access, "You should not be here" );
290 if( p_sys->i_code == 401 )
292 char *psz_login = NULL; char *psz_password = NULL;
294 msg_Dbg( p_access, "authentication failed" );
295 i_ret = intf_UserLoginPassword( p_access, _("HTTP authentication"),
296 _("Please enter a valid login name and a password."),
297 &psz_login, &psz_password );
298 if( i_ret == DIALOG_OK_YES )
300 msg_Dbg( p_access, "retrying with user=%s, pwd=%s",
301 psz_login, psz_password );
302 if( psz_login ) p_sys->url.psz_username = strdup( psz_login );
303 if( psz_password ) p_sys->url.psz_password = strdup( psz_password );
304 if( psz_login ) free( psz_login );
305 if( psz_password ) free( psz_password );
310 if( psz_login ) free( psz_login );
311 if( psz_password ) free( psz_password );
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" );
335 p_playlist = pl_Yield( p_access );
338 p_input_item = p_playlist->status.p_item->p_input;
339 input_item_SetURI( p_input_item, p_sys->psz_location );
340 free( p_access->psz_path );
341 p_access->psz_path = strdup( p_sys->psz_location );
344 pl_Release( p_access );
346 /* Clean up current Open() run */
347 vlc_UrlClean( &p_sys->url );
348 vlc_UrlClean( &p_sys->proxy );
349 free( p_sys->psz_mime );
350 free( p_sys->psz_pragma );
351 free( p_sys->psz_location );
352 free( p_sys->psz_user_agent );
354 Disconnect( p_access );
357 /* Do new Open() run with new data */
358 return Open( p_this );
363 msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
367 if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
369 if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
371 if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
372 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
373 p_access->psz_demux = strdup( "nsv" );
374 else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
375 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
376 p_access->psz_demux = strdup( "m4a" );
377 else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
378 p_access->psz_demux = strdup( "mp3" );
380 msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
381 p_access->psz_demux );
383 #if 0 /* Doesn't work really well because of the pre-buffering in
384 * shoutcast servers (the buffer content will be sent as fast as
386 p_sys->b_pace_control = VLC_FALSE;
389 else if( !p_sys->psz_mime )
392 p_access->psz_demux = strdup( "mp3" );
394 /* else probably Ogg Vorbis */
396 else if( !strcasecmp( p_access->psz_access, "unsv" ) &&
398 !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
400 /* Grrrr! detect ultravox server and force NSV demuxer */
401 p_access->psz_demux = strdup( "nsv" );
403 else if( !strcmp( p_access->psz_access, "itpc" ) )
405 p_access->psz_demux = strdup( "podcast" );
407 else if( p_sys->psz_mime &&
408 !strncasecmp( p_sys->psz_mime, "application/xspf+xml", 20 ) &&
409 ( memchr( " ;\t", p_sys->psz_mime[20], 4 ) != NULL ) )
410 p_access->psz_demux = strdup( "xspf-open" );
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 free( p_sys->psz_mime );
423 free( p_sys->psz_pragma );
424 free( p_sys->psz_location );
425 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 free( p_sys->psz_mime );
444 free( p_sys->psz_pragma );
445 free( p_sys->psz_location );
447 free( p_sys->psz_icy_name );
448 free( p_sys->psz_icy_genre );
449 free( p_sys->psz_icy_title );
451 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 /* fatal error - end of file */
498 msg_Dbg( p_access, "failed reading chunk-header line" );
501 p_sys->i_chunk = strtoll( psz, NULL, 16 );
504 if( p_sys->i_chunk <= 0 ) /* eof */
507 p_access->info.b_eof = VLC_TRUE;
512 if( i_len > p_sys->i_chunk )
514 i_len = p_sys->i_chunk;
518 if( p_sys->b_continuous && i_len > p_sys->i_remaining )
520 /* Only ask for the remaining length */
521 int i_new_len = p_sys->i_remaining;
524 Request( p_access, 0 );
525 i_read = Read( p_access, p_buffer, i_len );
531 if( p_sys->i_icy_meta > 0 && p_access->info.i_pos > 0 )
533 int64_t i_next = p_sys->i_icy_meta -
534 p_access->info.i_pos % p_sys->i_icy_meta;
536 if( i_next == p_sys->i_icy_meta )
538 if( ReadICYMeta( p_access ) )
540 p_access->info.b_eof = VLC_TRUE;
548 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, VLC_FALSE );
552 p_access->info.i_pos += i_read;
554 if( p_sys->b_chunked )
556 p_sys->i_chunk -= i_read;
557 if( p_sys->i_chunk <= 0 )
559 /* read the empty line */
560 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
561 if( psz ) free( psz );
565 else if( i_read == 0 )
568 * I very much doubt that this will work.
569 * If i_read == 0, the connection *IS* dead, so the only
570 * sensible thing to do is Disconnect() and then retry.
571 * Otherwise, I got recv() completely wrong. -- Courmisch
573 if( p_sys->b_continuous )
575 Request( p_access, 0 );
576 p_sys->b_continuous = VLC_FALSE;
577 i_read = Read( p_access, p_buffer, i_len );
578 p_sys->b_continuous = VLC_TRUE;
580 Disconnect( p_access );
581 if( p_sys->b_reconnect )
583 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
584 if( Connect( p_access, p_access->info.i_pos ) )
586 msg_Dbg( p_access, "reconnection failed" );
590 p_sys->b_reconnect = VLC_FALSE;
591 i_read = Read( p_access, p_buffer, i_len );
592 p_sys->b_reconnect = VLC_TRUE;
596 if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
599 if( p_sys->b_continuous )
601 p_sys->i_remaining -= i_read;
607 static int ReadICYMeta( access_t *p_access )
609 access_sys_t *p_sys = p_access->p_sys;
615 /* Read meta data length */
616 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
623 i_read = buffer << 4;
624 /* msg_Dbg( p_access, "ICY meta size=%u", i_read); */
626 psz_meta = malloc( i_read + 1 );
627 if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
628 (uint8_t *)psz_meta, i_read, VLC_TRUE ) != i_read )
631 psz_meta[i_read] = '\0'; /* Just in case */
633 /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
635 /* Now parse the meta */
636 /* Look for StreamTitle= */
637 p = strcasestr( (char *)psz_meta, "StreamTitle=" );
640 p += strlen( "StreamTitle=" );
641 if( *p == '\'' || *p == '"' )
643 char closing[] = { p[0], ';', '\0' };
644 char *psz = strstr( &p[1], closing );
646 psz = strchr( &p[1], ';' );
648 if( psz ) *psz = '\0';
652 char *psz = strchr( &p[1], ';' );
653 if( psz ) *psz = '\0';
656 if( !p_sys->psz_icy_title ||
657 strcmp( p_sys->psz_icy_title, &p[1] ) )
659 if( p_sys->psz_icy_title )
660 free( p_sys->psz_icy_title );
661 p_sys->psz_icy_title = strdup( &p[1] );
662 p_access->info.i_update |= INPUT_UPDATE_META;
664 msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
672 /*****************************************************************************
673 * Seek: close and re-open a connection at the right place
674 *****************************************************************************/
675 static int Seek( access_t *p_access, int64_t i_pos )
677 msg_Dbg( p_access, "trying to seek to "I64Fd, i_pos );
679 Disconnect( p_access );
681 if( Connect( p_access, i_pos ) )
683 msg_Err( p_access, "seek failed" );
684 p_access->info.b_eof = VLC_TRUE;
690 /*****************************************************************************
692 *****************************************************************************/
693 static int Control( access_t *p_access, int i_query, va_list args )
695 access_sys_t *p_sys = p_access->p_sys;
704 case ACCESS_CAN_SEEK:
705 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
706 *pb_bool = p_sys->b_seekable;
708 case ACCESS_CAN_FASTSEEK:
709 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
710 *pb_bool = VLC_FALSE;
712 case ACCESS_CAN_PAUSE:
713 case ACCESS_CAN_CONTROL_PACE:
714 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
716 #if 0 /* Disable for now until we have a clock synchro algo
717 * which works with something else than MPEG over UDP */
718 *pb_bool = p_sys->b_pace_control;
725 pi_int = (int*)va_arg( args, int * );
729 case ACCESS_GET_PTS_DELAY:
730 pi_64 = (int64_t*)va_arg( args, int64_t * );
731 *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
735 case ACCESS_SET_PAUSE_STATE:
738 case ACCESS_GET_META:
739 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
741 if( p_sys->psz_icy_name )
742 vlc_meta_Set( p_meta, vlc_meta_Title, p_sys->psz_icy_name );
743 if( p_sys->psz_icy_genre )
744 vlc_meta_Set( p_meta, vlc_meta_Genre, p_sys->psz_icy_genre );
745 if( p_sys->psz_icy_title )
746 vlc_meta_Set( p_meta, vlc_meta_NowPlaying, p_sys->psz_icy_title );
749 case ACCESS_GET_CONTENT_TYPE:
750 *va_arg( args, char ** ) =
751 p_sys->psz_mime ? strdup( p_sys->psz_mime ) : NULL;
754 case ACCESS_GET_TITLE_INFO:
755 case ACCESS_SET_TITLE:
756 case ACCESS_SET_SEEKPOINT:
757 case ACCESS_SET_PRIVATE_ID_STATE:
761 msg_Warn( p_access, "unimplemented query in control" );
768 /*****************************************************************************
770 *****************************************************************************/
771 static int Connect( access_t *p_access, int64_t i_tell )
773 access_sys_t *p_sys = p_access->p_sys;
774 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
777 free( p_sys->psz_location );
778 free( p_sys->psz_mime );
779 free( p_sys->psz_pragma );
781 free( p_sys->psz_icy_genre );
782 free( p_sys->psz_icy_name );
783 free( p_sys->psz_icy_title );
786 p_sys->psz_location = NULL;
787 p_sys->psz_mime = NULL;
788 p_sys->psz_pragma = NULL;
789 p_sys->b_mms = VLC_FALSE;
790 p_sys->b_chunked = VLC_FALSE;
792 p_sys->i_icy_meta = 0;
793 p_sys->psz_icy_name = NULL;
794 p_sys->psz_icy_genre = NULL;
795 p_sys->psz_icy_title = NULL;
797 p_access->info.i_size = 0;
798 p_access->info.i_pos = i_tell;
799 p_access->info.b_eof = VLC_FALSE;
802 /* Open connection */
803 p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
804 if( p_sys->fd == -1 )
806 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
810 /* Initialize TLS/SSL session */
811 if( p_sys->b_ssl == VLC_TRUE )
813 /* CONNECT to establish TLS tunnel through HTTP proxy */
817 unsigned i_status = 0;
819 if( p_sys->i_version == 0 )
821 /* CONNECT is not in HTTP/1.0 */
822 Disconnect( p_access );
826 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
827 "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
828 p_sys->url.psz_host, p_sys->url.i_port,
830 p_sys->url.psz_host, p_sys->url.i_port);
832 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
835 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
836 Disconnect( p_access );
840 sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
843 if( ( i_status / 100 ) != 2 )
845 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
846 Disconnect( p_access );
852 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
855 msg_Err( p_access, "HTTP proxy connection failed" );
856 Disconnect( p_access );
868 /* TLS/SSL handshake */
869 p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
871 if( p_sys->p_tls == NULL )
873 msg_Err( p_access, "cannot establish HTTP/TLS session" );
874 Disconnect( p_access );
877 p_sys->p_vs = &p_sys->p_tls->sock;
880 return Request( p_access, i_tell ) ? -2 : 0;
884 static int Request( access_t *p_access, int64_t i_tell )
886 access_sys_t *p_sys = p_access->p_sys;
888 v_socket_t *pvs = p_sys->p_vs;
892 if( p_sys->url.psz_path )
894 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
895 "GET http://%s:%d%s HTTP/1.%d\r\n",
896 p_sys->url.psz_host, p_sys->url.i_port,
897 p_sys->url.psz_path, p_sys->i_version );
901 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
902 "GET http://%s:%d/ HTTP/1.%d\r\n",
903 p_sys->url.psz_host, p_sys->url.i_port,
909 const char *psz_path = p_sys->url.psz_path;
910 if( !psz_path || !*psz_path )
914 if( p_sys->url.i_port != 80)
916 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
917 "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
918 psz_path, p_sys->i_version, p_sys->url.psz_host,
923 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
924 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
925 psz_path, p_sys->i_version, p_sys->url.psz_host );
929 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "User-Agent: %s\r\n",
930 p_sys->psz_user_agent );
932 if( p_sys->i_version == 1 )
934 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
935 "Range: bytes="I64Fd"-\r\n", i_tell );
939 if( p_sys->url.psz_username || p_sys->url.psz_password )
941 char buf[strlen( p_sys->url.psz_username ?: "" )
942 + strlen( p_sys->url.psz_password ?: "" ) + 2];
945 snprintf( buf, sizeof( buf ), "%s:%s", p_sys->url.psz_username ?: "",
946 p_sys->url.psz_password ?: "" );
947 b64 = vlc_b64_encode( buf );
951 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
952 "Authorization: Basic %s\r\n", b64 );
957 /* Proxy Authentication */
958 if( p_sys->proxy.psz_username || p_sys->proxy.psz_password )
960 char buf[strlen( p_sys->proxy.psz_username ?: "" )
961 + strlen( p_sys->proxy.psz_password ?: "" )];
964 snprintf( buf, sizeof( buf ), "%s:%s", p_sys->proxy.psz_username ?: "",
965 p_sys->proxy.psz_password ?: "" );
966 b64 = vlc_b64_encode( buf );
970 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
971 "Proxy-Authorization: Basic %s\r\n", b64 );
976 /* ICY meta data request */
977 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "Icy-MetaData: 1\r\n" );
980 if( p_sys->b_continuous )
982 net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
983 "Connection: Keep-Alive\r\n" );
985 else if( p_sys->i_version == 1 )
987 net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
988 "Connection: Close\r\n");
991 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "\r\n" ) < 0 )
993 msg_Err( p_access, "failed to send request" );
994 Disconnect( p_access );
999 if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs ) ) == NULL )
1001 msg_Err( p_access, "failed to read answer" );
1004 if( !strncmp( psz, "HTTP/1.", 7 ) )
1006 p_sys->psz_protocol = "HTTP";
1007 p_sys->i_code = atoi( &psz[9] );
1009 else if( !strncmp( psz, "ICY", 3 ) )
1011 p_sys->psz_protocol = "ICY";
1012 p_sys->i_code = atoi( &psz[4] );
1013 p_sys->b_reconnect = VLC_TRUE;
1017 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1021 msg_Dbg( p_access, "protocol '%s' answer code %d",
1022 p_sys->psz_protocol, p_sys->i_code );
1023 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1025 p_sys->b_seekable = VLC_FALSE;
1027 if( p_sys->i_code != 206 && p_sys->i_code != 401 )
1029 p_sys->b_seekable = VLC_FALSE;
1031 /* Authentication error - We'll have to display the dialog */
1032 if( p_sys->i_code == 401 )
1036 /* Other fatal error */
1037 else if( p_sys->i_code >= 400 )
1039 msg_Err( p_access, "error: %s", psz );
1047 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs );
1052 msg_Err( p_access, "failed to read answer" );
1056 /* msg_Dbg( p_input, "Line=%s", psz ); */
1064 if( ( p = strchr( psz, ':' ) ) == NULL )
1066 msg_Err( p_access, "malformed header line: %s", psz );
1071 while( *p == ' ' ) p++;
1073 if( !strcasecmp( psz, "Content-Length" ) )
1075 if( p_sys->b_continuous )
1077 p_access->info.i_size = -1;
1078 msg_Dbg( p_access, "this frame size=%lld", atoll(p ) );
1079 p_sys->i_remaining = atoll( p );
1083 p_access->info.i_size = i_tell + atoll( p );
1084 msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
1087 else if( !strcasecmp( psz, "Location" ) )
1091 /* This does not follow RFC 2068, but yet if the url is not absolute,
1092 * handle it as everyone does. */
1095 const char *psz_http_ext = p_sys->b_ssl ? "s" : "" ;
1097 if( p_sys->url.i_port == ( p_sys->b_ssl ? 443 : 80 ) )
1099 asprintf(&psz_new_loc, "http%s://%s%s", psz_http_ext,
1100 p_sys->url.psz_host, p);
1104 asprintf(&psz_new_loc, "http%s://%s:%d%s", psz_http_ext,
1105 p_sys->url.psz_host, p_sys->url.i_port, p);
1110 psz_new_loc = strdup( p );
1113 if( p_sys->psz_location ) free( p_sys->psz_location );
1114 p_sys->psz_location = psz_new_loc;
1116 else if( !strcasecmp( psz, "Content-Type" ) )
1118 if( p_sys->psz_mime ) free( p_sys->psz_mime );
1119 p_sys->psz_mime = strdup( p );
1120 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1122 else if( !strcasecmp( psz, "Pragma" ) )
1124 if( !strcasecmp( psz, "Pragma: features" ) )
1125 p_sys->b_mms = VLC_TRUE;
1126 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
1127 p_sys->psz_pragma = strdup( p );
1128 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1130 else if( !strcasecmp( psz, "Server" ) )
1132 msg_Dbg( p_access, "Server: %s", p );
1133 if( !strncasecmp( p, "Icecast", 7 ) ||
1134 !strncasecmp( p, "Nanocaster", 10 ) )
1136 /* Remember if this is Icecast
1137 * we need to force demux in this case without breaking
1140 /* Let live 365 streams (nanocaster) piggyback on the icecast
1141 * routine. They look very similar */
1143 p_sys->b_reconnect = VLC_TRUE;
1144 p_sys->b_pace_control = VLC_FALSE;
1145 p_sys->b_icecast = VLC_TRUE;
1148 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1150 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1151 if( !strncasecmp( p, "chunked", 7 ) )
1153 p_sys->b_chunked = VLC_TRUE;
1156 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1158 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1159 p_sys->i_icy_meta = atoi( p );
1160 if( p_sys->i_icy_meta < 0 )
1161 p_sys->i_icy_meta = 0;
1163 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1165 else if( !strcasecmp( psz, "Icy-Name" ) )
1167 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
1168 p_sys->psz_icy_name = strdup( p );
1169 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1171 p_sys->b_icecast = VLC_TRUE; /* be on the safeside. set it here as well. */
1172 p_sys->b_reconnect = VLC_TRUE;
1173 p_sys->b_pace_control = VLC_FALSE;
1175 else if( !strcasecmp( psz, "Icy-Genre" ) )
1177 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
1178 p_sys->psz_icy_genre = strdup( p );
1179 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1181 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1183 msg_Dbg( p_access, "Icy-Notice: %s", p );
1185 else if( !strncasecmp( psz, "icy-", 4 ) ||
1186 !strncasecmp( psz, "ice-", 4 ) ||
1187 !strncasecmp( psz, "x-audiocast", 11 ) )
1189 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1197 Disconnect( p_access );
1198 return VLC_EGENERIC;
1201 /*****************************************************************************
1203 *****************************************************************************/
1204 static void Disconnect( access_t *p_access )
1206 access_sys_t *p_sys = p_access->p_sys;
1208 if( p_sys->p_tls != NULL)
1210 tls_ClientDelete( p_sys->p_tls );
1211 p_sys->p_tls = NULL;
1214 if( p_sys->fd != -1)
1216 net_Close(p_sys->fd);