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 set_callbacks( Open, Close );
99 /*****************************************************************************
101 *****************************************************************************/
105 tls_session_t *p_tls;
110 char *psz_user_agent;
118 const char *psz_protocol;
125 vlc_bool_t b_icecast;
128 vlc_bool_t b_chunked;
138 vlc_bool_t b_seekable;
139 vlc_bool_t b_reconnect;
140 vlc_bool_t b_continuous;
141 vlc_bool_t b_pace_control;
145 static int Read( access_t *, uint8_t *, int );
146 static int Seek( access_t *, int64_t );
147 static int Control( access_t *, int, va_list );
150 static int Connect( access_t *, int64_t );
151 static int Request( access_t *p_access, int64_t i_tell );
152 static void Disconnect( access_t * );
154 /*****************************************************************************
156 *****************************************************************************/
157 static int Open( vlc_object_t *p_this )
159 access_t *p_access = (access_t*)p_this;
163 /* Set up p_access */
164 STANDARD_READ_ACCESS_INIT;
166 p_sys->b_proxy = VLC_FALSE;
167 p_sys->i_version = 1;
168 p_sys->b_seekable = VLC_TRUE;
169 p_sys->psz_mime = NULL;
170 p_sys->psz_pragma = NULL;
171 p_sys->b_mms = VLC_FALSE;
172 p_sys->b_icecast = VLC_FALSE;
173 p_sys->psz_location = NULL;
174 p_sys->psz_user_agent = NULL;
175 p_sys->b_pace_control = VLC_TRUE;
176 p_sys->b_ssl = VLC_FALSE;
179 p_sys->i_icy_meta = 0;
180 p_sys->psz_icy_name = NULL;
181 p_sys->psz_icy_genre = NULL;
182 p_sys->psz_icy_title = NULL;
183 p_sys->i_remaining = 0;
186 /* Parse URI - remove spaces */
187 p = psz = strdup( p_access->psz_path );
188 while( (p = strchr( p, ' ' )) != NULL )
190 vlc_UrlParse( &p_sys->url, psz, 0 );
193 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
195 msg_Warn( p_access, "invalid host" );
198 if( !strncmp( p_access->psz_access, "https", 5 ) )
201 p_sys->b_ssl = VLC_TRUE;
202 if( p_sys->url.i_port <= 0 )
203 p_sys->url.i_port = 443;
207 if( p_sys->url.i_port <= 0 )
208 p_sys->url.i_port = 80;
212 p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
215 psz = var_CreateGetString( p_access, "http-proxy" );
218 p_sys->b_proxy = VLC_TRUE;
219 vlc_UrlParse( &p_sys->proxy, psz, 0 );
224 char *psz_proxy = getenv( "http_proxy" );
225 if( psz_proxy && *psz_proxy )
227 p_sys->b_proxy = VLC_TRUE;
228 vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
236 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
238 msg_Warn( p_access, "invalid proxy host" );
241 if( p_sys->proxy.i_port <= 0 )
243 p_sys->proxy.i_port = 80;
247 msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
248 p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
251 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
252 p_sys->proxy.i_port );
254 if( p_sys->url.psz_username && *p_sys->url.psz_username )
256 msg_Dbg( p_access, " user='%s', pwd='%s'",
257 p_sys->url.psz_username, p_sys->url.psz_password );
260 p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
261 p_sys->b_continuous = var_CreateGetBool( p_access, "http-continuous" );
265 switch( Connect( p_access, 0 ) )
271 /* Retry with http 1.0 */
272 msg_Dbg( p_access, "switching to HTTP version 1.0" );
273 p_sys->i_version = 0;
274 p_sys->b_seekable = VLC_FALSE;
276 if( p_access->b_die || Connect( p_access, 0 ) )
284 msg_Err( p_access, "You should not be here" );
289 if( p_sys->i_code == 401 )
291 char *psz_login = NULL; char *psz_password = NULL;
293 msg_Dbg( p_access, "authentication failed" );
294 i_ret = intf_UserLoginPassword( p_access, _("HTTP authentication"),
295 _("Please enter a valid login name and a password."),
296 &psz_login, &psz_password );
297 if( i_ret == DIALOG_OK_YES )
299 msg_Dbg( p_access, "retrying with user=%s, pwd=%s",
300 psz_login, psz_password );
301 if( psz_login ) p_sys->url.psz_username = strdup( psz_login );
302 if( psz_password ) p_sys->url.psz_password = strdup( psz_password );
303 if( psz_login ) free( psz_login );
304 if( psz_password ) free( psz_password );
309 if( psz_login ) free( psz_login );
310 if( psz_password ) free( psz_password );
315 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
316 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
317 p_sys->psz_location && *p_sys->psz_location )
319 playlist_t * p_playlist;
320 input_item_t *p_input_item;
322 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
324 /* Do not accept redirection outside of HTTP works */
325 if( strncmp( p_sys->psz_location, "http", 4 )
326 || ( ( p_sys->psz_location[4] != ':' ) /* HTTP */
327 && strncmp( p_sys->psz_location + 4, "s:", 2 ) /* HTTP/SSL */ ) )
329 msg_Err( p_access, "insecure redirection ignored" );
334 p_playlist = pl_Yield( p_access );
337 p_input_item = p_playlist->status.p_item->p_input;
338 input_item_SetURI( p_input_item, p_sys->psz_location );
339 free( p_access->psz_path );
340 p_access->psz_path = strdup( p_sys->psz_location );
343 pl_Release( p_access );
345 /* Clean up current Open() run */
346 vlc_UrlClean( &p_sys->url );
347 vlc_UrlClean( &p_sys->proxy );
348 if( p_sys->psz_mime ) free( p_sys->psz_mime );
349 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
350 if( p_sys->psz_location ) free( p_sys->psz_location );
351 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
353 Disconnect( p_access );
356 /* Do new Open() run with new data */
357 return Open( p_this );
362 msg_Dbg( p_access, "this is actually a live mms server, BAIL" );
366 if( !strcmp( p_sys->psz_protocol, "ICY" ) || p_sys->b_icecast )
368 if( p_sys->psz_mime && strcasecmp( p_sys->psz_mime, "application/ogg" ) )
370 if( !strcasecmp( p_sys->psz_mime, "video/nsv" ) ||
371 !strcasecmp( p_sys->psz_mime, "video/nsa" ) )
372 p_access->psz_demux = strdup( "nsv" );
373 else if( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
374 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) )
375 p_access->psz_demux = strdup( "m4a" );
376 else if( !strcasecmp( p_sys->psz_mime, "audio/mpeg" ) )
377 p_access->psz_demux = strdup( "mp3" );
379 msg_Info( p_access, "Raw-audio server found, %s demuxer selected",
380 p_access->psz_demux );
382 #if 0 /* Doesn't work really well because of the pre-buffering in
383 * shoutcast servers (the buffer content will be sent as fast as
385 p_sys->b_pace_control = VLC_FALSE;
388 else if( !p_sys->psz_mime )
391 p_access->psz_demux = strdup( "mp3" );
393 /* else probably Ogg Vorbis */
395 else if( !strcasecmp( p_access->psz_access, "unsv" ) &&
397 !strcasecmp( p_sys->psz_mime, "misc/ultravox" ) )
399 /* Grrrr! detect ultravox server and force NSV demuxer */
400 p_access->psz_demux = strdup( "nsv" );
402 else if( p_sys->psz_mime &&
403 !strncasecmp( p_sys->psz_mime, "application/xspf+xml", 20 ) &&
404 ( memchr( " ;\t", p_sys->psz_mime[20], 4 ) != NULL ) )
405 p_access->psz_demux = strdup( "xspf-open" );
407 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
410 var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
415 vlc_UrlClean( &p_sys->url );
416 vlc_UrlClean( &p_sys->proxy );
417 if( p_sys->psz_mime ) free( p_sys->psz_mime );
418 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
419 if( p_sys->psz_location ) free( p_sys->psz_location );
420 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
422 Disconnect( p_access );
427 /*****************************************************************************
429 *****************************************************************************/
430 static void Close( vlc_object_t *p_this )
432 access_t *p_access = (access_t*)p_this;
433 access_sys_t *p_sys = p_access->p_sys;
435 vlc_UrlClean( &p_sys->url );
436 vlc_UrlClean( &p_sys->proxy );
438 if( p_sys->psz_mime ) free( p_sys->psz_mime );
439 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
440 if( p_sys->psz_location ) free( p_sys->psz_location );
442 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
443 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
444 if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
446 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
448 Disconnect( p_access );
452 /*****************************************************************************
453 * Read: Read up to i_len bytes from the http connection and place in
454 * p_buffer. Return the actual number of bytes read
455 *****************************************************************************/
456 static int ReadICYMeta( access_t *p_access );
457 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
459 access_sys_t *p_sys = p_access->p_sys;
464 p_access->info.b_eof = VLC_TRUE;
468 if( p_access->info.i_size > 0 &&
469 i_len + p_access->info.i_pos > p_access->info.i_size )
471 if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
473 p_access->info.b_eof = VLC_TRUE;
478 if( p_sys->b_chunked )
480 if( p_sys->i_chunk < 0 )
482 p_access->info.b_eof = VLC_TRUE;
486 if( p_sys->i_chunk <= 0 )
488 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
489 /* read the chunk header */
492 /* fatal error - end of file */
493 msg_Dbg( p_access, "failed reading chunk-header line" );
496 p_sys->i_chunk = strtoll( psz, NULL, 16 );
499 if( p_sys->i_chunk <= 0 ) /* eof */
502 p_access->info.b_eof = VLC_TRUE;
507 if( i_len > p_sys->i_chunk )
509 i_len = p_sys->i_chunk;
513 if( p_sys->b_continuous && i_len > p_sys->i_remaining )
515 /* Only ask for the remaining length */
516 int i_new_len = p_sys->i_remaining;
519 Request( p_access, 0 );
520 i_read = Read( p_access, p_buffer, i_len );
526 if( p_sys->i_icy_meta > 0 && p_access->info.i_pos > 0 )
528 int64_t i_next = p_sys->i_icy_meta -
529 p_access->info.i_pos % p_sys->i_icy_meta;
531 if( i_next == p_sys->i_icy_meta )
533 if( ReadICYMeta( p_access ) )
535 p_access->info.b_eof = VLC_TRUE;
543 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, p_buffer, i_len, VLC_FALSE );
547 p_access->info.i_pos += i_read;
549 if( p_sys->b_chunked )
551 p_sys->i_chunk -= i_read;
552 if( p_sys->i_chunk <= 0 )
554 /* read the empty line */
555 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, p_sys->p_vs );
556 if( psz ) free( psz );
560 else if( i_read == 0 )
563 * I very much doubt that this will work.
564 * If i_read == 0, the connection *IS* dead, so the only
565 * sensible thing to do is Disconnect() and then retry.
566 * Otherwise, I got recv() completely wrong. -- Courmisch
568 if( p_sys->b_continuous )
570 Request( p_access, 0 );
571 p_sys->b_continuous = VLC_FALSE;
572 i_read = Read( p_access, p_buffer, i_len );
573 p_sys->b_continuous = VLC_TRUE;
575 Disconnect( p_access );
576 if( p_sys->b_reconnect )
578 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
579 if( Connect( p_access, p_access->info.i_pos ) )
581 msg_Dbg( p_access, "reconnection failed" );
585 p_sys->b_reconnect = VLC_FALSE;
586 i_read = Read( p_access, p_buffer, i_len );
587 p_sys->b_reconnect = VLC_TRUE;
591 if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
594 if( p_sys->b_continuous )
596 p_sys->i_remaining -= i_read;
602 static int ReadICYMeta( access_t *p_access )
604 access_sys_t *p_sys = p_access->p_sys;
610 /* Read meta data length */
611 i_read = net_Read( p_access, p_sys->fd, p_sys->p_vs, &buffer, 1,
618 i_read = buffer << 4;
619 /* msg_Dbg( p_access, "ICY meta size=%u", i_read); */
621 psz_meta = malloc( i_read + 1 );
622 if( net_Read( p_access, p_sys->fd, p_sys->p_vs,
623 (uint8_t *)psz_meta, i_read, VLC_TRUE ) != i_read )
626 psz_meta[i_read] = '\0'; /* Just in case */
628 /* msg_Dbg( p_access, "icy-meta=%s", psz_meta ); */
630 /* Now parse the meta */
631 /* Look for StreamTitle= */
632 p = strcasestr( (char *)psz_meta, "StreamTitle=" );
635 p += strlen( "StreamTitle=" );
636 if( *p == '\'' || *p == '"' )
638 char closing[] = { p[0], ';', '\0' };
639 char *psz = strstr( &p[1], closing );
641 psz = strchr( &p[1], ';' );
643 if( psz ) *psz = '\0';
647 char *psz = strchr( &p[1], ';' );
648 if( psz ) *psz = '\0';
651 if( !p_sys->psz_icy_title ||
652 strcmp( p_sys->psz_icy_title, &p[1] ) )
654 if( p_sys->psz_icy_title )
655 free( p_sys->psz_icy_title );
656 p_sys->psz_icy_title = strdup( &p[1] );
657 p_access->info.i_update |= INPUT_UPDATE_META;
659 msg_Dbg( p_access, "New Title=%s", p_sys->psz_icy_title );
667 /*****************************************************************************
668 * Seek: close and re-open a connection at the right place
669 *****************************************************************************/
670 static int Seek( access_t *p_access, int64_t i_pos )
672 msg_Dbg( p_access, "trying to seek to "I64Fd, i_pos );
674 Disconnect( p_access );
676 if( Connect( p_access, i_pos ) )
678 msg_Err( p_access, "seek failed" );
679 p_access->info.b_eof = VLC_TRUE;
685 /*****************************************************************************
687 *****************************************************************************/
688 static int Control( access_t *p_access, int i_query, va_list args )
690 access_sys_t *p_sys = p_access->p_sys;
699 case ACCESS_CAN_SEEK:
700 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
701 *pb_bool = p_sys->b_seekable;
703 case ACCESS_CAN_FASTSEEK:
704 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
705 *pb_bool = VLC_FALSE;
707 case ACCESS_CAN_PAUSE:
708 case ACCESS_CAN_CONTROL_PACE:
709 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
711 #if 0 /* Disable for now until we have a clock synchro algo
712 * which works with something else than MPEG over UDP */
713 *pb_bool = p_sys->b_pace_control;
720 pi_int = (int*)va_arg( args, int * );
724 case ACCESS_GET_PTS_DELAY:
725 pi_64 = (int64_t*)va_arg( args, int64_t * );
726 *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
730 case ACCESS_SET_PAUSE_STATE:
733 case ACCESS_GET_META:
734 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
736 if( p_sys->psz_icy_name )
737 vlc_meta_Set( p_meta, vlc_meta_Title, p_sys->psz_icy_name );
738 if( p_sys->psz_icy_genre )
739 vlc_meta_Set( p_meta, vlc_meta_Genre, p_sys->psz_icy_genre );
740 if( p_sys->psz_icy_title )
741 vlc_meta_Set( p_meta, vlc_meta_NowPlaying, p_sys->psz_icy_title );
744 case ACCESS_GET_TITLE_INFO:
745 case ACCESS_SET_TITLE:
746 case ACCESS_SET_SEEKPOINT:
747 case ACCESS_SET_PRIVATE_ID_STATE:
751 msg_Warn( p_access, "unimplemented query in control" );
758 /*****************************************************************************
760 *****************************************************************************/
761 static int Connect( access_t *p_access, int64_t i_tell )
763 access_sys_t *p_sys = p_access->p_sys;
764 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
767 if( p_sys->psz_location ) free( p_sys->psz_location );
768 if( p_sys->psz_mime ) free( p_sys->psz_mime );
769 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
771 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
772 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
773 if( p_sys->psz_icy_title ) free( p_sys->psz_icy_title );
776 p_sys->psz_location = NULL;
777 p_sys->psz_mime = NULL;
778 p_sys->psz_pragma = NULL;
779 p_sys->b_mms = VLC_FALSE;
780 p_sys->b_chunked = VLC_FALSE;
782 p_sys->i_icy_meta = 0;
783 p_sys->psz_icy_name = NULL;
784 p_sys->psz_icy_genre = NULL;
785 p_sys->psz_icy_title = NULL;
787 p_access->info.i_size = 0;
788 p_access->info.i_pos = i_tell;
789 p_access->info.b_eof = VLC_FALSE;
792 /* Open connection */
793 p_sys->fd = net_ConnectTCP( p_access, srv.psz_host, srv.i_port );
794 if( p_sys->fd == -1 )
796 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
800 /* Initialize TLS/SSL session */
801 if( p_sys->b_ssl == VLC_TRUE )
803 /* CONNECT to establish TLS tunnel through HTTP proxy */
807 unsigned i_status = 0;
809 if( p_sys->i_version == 0 )
811 /* CONNECT is not in HTTP/1.0 */
812 Disconnect( p_access );
816 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
817 "CONNECT %s:%d HTTP/1.%d\r\nHost: %s:%d\r\n\r\n",
818 p_sys->url.psz_host, p_sys->url.i_port,
820 p_sys->url.psz_host, p_sys->url.i_port);
822 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
825 msg_Err( p_access, "cannot establish HTTP/TLS tunnel" );
826 Disconnect( p_access );
830 sscanf( psz, "HTTP/%*u.%*u %3u", &i_status );
833 if( ( i_status / 100 ) != 2 )
835 msg_Err( p_access, "HTTP/TLS tunnel through proxy denied" );
836 Disconnect( p_access );
842 psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
845 msg_Err( p_access, "HTTP proxy connection failed" );
846 Disconnect( p_access );
858 /* TLS/SSL handshake */
859 p_sys->p_tls = tls_ClientCreate( VLC_OBJECT(p_access), p_sys->fd,
861 if( p_sys->p_tls == NULL )
863 msg_Err( p_access, "cannot establish HTTP/TLS session" );
864 Disconnect( p_access );
867 p_sys->p_vs = &p_sys->p_tls->sock;
870 return Request( p_access, i_tell ) ? -2 : 0;
874 static int Request( access_t *p_access, int64_t i_tell )
876 access_sys_t *p_sys = p_access->p_sys;
878 v_socket_t *pvs = p_sys->p_vs;
882 if( p_sys->url.psz_path )
884 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
885 "GET http://%s:%d%s HTTP/1.%d\r\n",
886 p_sys->url.psz_host, p_sys->url.i_port,
887 p_sys->url.psz_path, p_sys->i_version );
891 net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
892 "GET http://%s:%d/ HTTP/1.%d\r\n",
893 p_sys->url.psz_host, p_sys->url.i_port,
899 const char *psz_path = p_sys->url.psz_path;
900 if( !psz_path || !*psz_path )
904 if( p_sys->url.i_port != 80)
906 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
907 "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
908 psz_path, p_sys->i_version, p_sys->url.psz_host,
913 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
914 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
915 psz_path, p_sys->i_version, p_sys->url.psz_host );
919 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "User-Agent: %s\r\n",
920 p_sys->psz_user_agent );
922 if( p_sys->i_version == 1 )
924 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
925 "Range: bytes="I64Fd"-\r\n", i_tell );
929 if( p_sys->url.psz_username || p_sys->url.psz_password )
931 char buf[strlen( p_sys->url.psz_username ?: "" )
932 + strlen( p_sys->url.psz_password ?: "" ) + 2];
935 snprintf( buf, sizeof( buf ), "%s:%s", p_sys->url.psz_username ?: "",
936 p_sys->url.psz_password ?: "" );
937 b64 = vlc_b64_encode( buf );
941 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
942 "Authorization: Basic %s\r\n", b64 );
947 /* Proxy Authentication */
948 if( p_sys->proxy.psz_username || p_sys->proxy.psz_password )
950 char buf[strlen( p_sys->proxy.psz_username ?: "" )
951 + strlen( p_sys->proxy.psz_password ?: "" )];
954 snprintf( buf, sizeof( buf ), "%s:%s", p_sys->proxy.psz_username ?: "",
955 p_sys->proxy.psz_password ?: "" );
956 b64 = vlc_b64_encode( buf );
960 net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs,
961 "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 )
972 net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
973 "Connection: Keep-Alive\r\n" );
975 else if( p_sys->i_version == 1 )
977 net_Printf( VLC_OBJECT( p_access ), p_sys->fd, pvs,
978 "Connection: Close\r\n");
981 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, pvs, "\r\n" ) < 0 )
983 msg_Err( p_access, "failed to send request" );
984 Disconnect( p_access );
989 if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs ) ) == NULL )
991 msg_Err( p_access, "failed to read answer" );
994 if( !strncmp( psz, "HTTP/1.", 7 ) )
996 p_sys->psz_protocol = "HTTP";
997 p_sys->i_code = atoi( &psz[9] );
999 else if( !strncmp( psz, "ICY", 3 ) )
1001 p_sys->psz_protocol = "ICY";
1002 p_sys->i_code = atoi( &psz[4] );
1003 p_sys->b_reconnect = VLC_TRUE;
1007 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
1011 msg_Dbg( p_access, "protocol '%s' answer code %d",
1012 p_sys->psz_protocol, p_sys->i_code );
1013 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
1015 p_sys->b_seekable = VLC_FALSE;
1017 if( p_sys->i_code != 206 && p_sys->i_code != 401 )
1019 p_sys->b_seekable = VLC_FALSE;
1021 /* Authentication error - We'll have to display the dialog */
1022 if( p_sys->i_code == 401 )
1026 /* Other fatal error */
1027 else if( p_sys->i_code >= 400 )
1029 msg_Err( p_access, "error: %s", psz );
1037 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, pvs );
1042 msg_Err( p_access, "failed to read answer" );
1046 /* msg_Dbg( p_input, "Line=%s", psz ); */
1054 if( ( p = strchr( psz, ':' ) ) == NULL )
1056 msg_Err( p_access, "malformed header line: %s", psz );
1061 while( *p == ' ' ) p++;
1063 if( !strcasecmp( psz, "Content-Length" ) )
1065 if( p_sys->b_continuous )
1067 p_access->info.i_size = -1;
1068 msg_Dbg( p_access, "this frame size=%lld", atoll(p ) );
1069 p_sys->i_remaining = atoll( p );
1073 p_access->info.i_size = i_tell + atoll( p );
1074 msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
1077 else if( !strcasecmp( psz, "Location" ) )
1081 /* This does not follow RFC 2068, but yet if the url is not absolute,
1082 * handle it as everyone does. */
1085 const char *psz_http_ext = p_sys->b_ssl ? "s" : "" ;
1087 if( p_sys->url.i_port == ( p_sys->b_ssl ? 443 : 80 ) )
1089 asprintf(&psz_new_loc, "http%s://%s%s", psz_http_ext,
1090 p_sys->url.psz_host, p);
1094 asprintf(&psz_new_loc, "http%s://%s:%d%s", psz_http_ext,
1095 p_sys->url.psz_host, p_sys->url.i_port, p);
1100 psz_new_loc = strdup( p );
1103 if( p_sys->psz_location ) free( p_sys->psz_location );
1104 p_sys->psz_location = psz_new_loc;
1106 else if( !strcasecmp( psz, "Content-Type" ) )
1108 if( p_sys->psz_mime ) free( p_sys->psz_mime );
1109 p_sys->psz_mime = strdup( p );
1110 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
1112 else if( !strcasecmp( psz, "Pragma" ) )
1114 if( !strcasecmp( psz, "Pragma: features" ) )
1115 p_sys->b_mms = VLC_TRUE;
1116 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
1117 p_sys->psz_pragma = strdup( p );
1118 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
1120 else if( !strcasecmp( psz, "Server" ) )
1122 msg_Dbg( p_access, "Server: %s", p );
1123 if( !strncasecmp( p, "Icecast", 7 ) ||
1124 !strncasecmp( p, "Nanocaster", 10 ) )
1126 /* Remember if this is Icecast
1127 * we need to force demux in this case without breaking
1130 /* Let live 365 streams (nanocaster) piggyback on the icecast
1131 * routine. They look very similar */
1133 p_sys->b_reconnect = VLC_TRUE;
1134 p_sys->b_pace_control = VLC_FALSE;
1135 p_sys->b_icecast = VLC_TRUE;
1138 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
1140 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
1141 if( !strncasecmp( p, "chunked", 7 ) )
1143 p_sys->b_chunked = VLC_TRUE;
1146 else if( !strcasecmp( psz, "Icy-MetaInt" ) )
1148 msg_Dbg( p_access, "Icy-MetaInt: %s", p );
1149 p_sys->i_icy_meta = atoi( p );
1150 if( p_sys->i_icy_meta < 0 )
1151 p_sys->i_icy_meta = 0;
1153 msg_Warn( p_access, "ICY metaint=%d", p_sys->i_icy_meta );
1155 else if( !strcasecmp( psz, "Icy-Name" ) )
1157 if( p_sys->psz_icy_name ) free( p_sys->psz_icy_name );
1158 p_sys->psz_icy_name = strdup( p );
1159 msg_Dbg( p_access, "Icy-Name: %s", p_sys->psz_icy_name );
1161 p_sys->b_icecast = VLC_TRUE; /* be on the safeside. set it here as well. */
1162 p_sys->b_reconnect = VLC_TRUE;
1163 p_sys->b_pace_control = VLC_FALSE;
1165 else if( !strcasecmp( psz, "Icy-Genre" ) )
1167 if( p_sys->psz_icy_genre ) free( p_sys->psz_icy_genre );
1168 p_sys->psz_icy_genre = strdup( p );
1169 msg_Dbg( p_access, "Icy-Genre: %s", p_sys->psz_icy_genre );
1171 else if( !strncasecmp( psz, "Icy-Notice", 10 ) )
1173 msg_Dbg( p_access, "Icy-Notice: %s", p );
1175 else if( !strncasecmp( psz, "icy-", 4 ) ||
1176 !strncasecmp( psz, "ice-", 4 ) ||
1177 !strncasecmp( psz, "x-audiocast", 11 ) )
1179 msg_Dbg( p_access, "Meta-Info: %s: %s", psz, p );
1187 Disconnect( p_access );
1188 return VLC_EGENERIC;
1191 /*****************************************************************************
1193 *****************************************************************************/
1194 static void Disconnect( access_t *p_access )
1196 access_sys_t *p_sys = p_access->p_sys;
1198 if( p_sys->p_tls != NULL)
1200 tls_ClientDelete( p_sys->p_tls );
1201 p_sys->p_tls = NULL;
1204 if( p_sys->fd != -1)
1206 net_Close(p_sys->fd);