1 /*****************************************************************************
2 * http.c: HTTP input module
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Christophe Massiot <massiot@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
31 #include <vlc/input.h>
33 #include "vlc_playlist.h"
36 /*****************************************************************************
38 *****************************************************************************/
39 static int Open ( vlc_object_t * );
40 static void Close( vlc_object_t * );
42 #define PROXY_TEXT N_("HTTP proxy")
43 #define PROXY_LONGTEXT N_( \
44 "You can specify an HTTP proxy to use. It must be of the form " \
45 "http://myproxy.mydomain:myport/. If none is specified, the HTTP_PROXY " \
46 "environment variable will be tried." )
48 #define CACHING_TEXT N_("Caching value in ms")
49 #define CACHING_LONGTEXT N_( \
50 "Allows you to modify the default caching value for http streams. This " \
51 "value should be set in millisecond units." )
53 #define USER_TEXT N_("HTTP user name")
54 #define USER_LONGTEXT N_("Allows you to modify the user name that will " \
55 "be used for the connection (Basic authentication only).")
57 #define PASS_TEXT N_("HTTP password")
58 #define PASS_LONGTEXT N_("Allows you to modify the password that will be " \
59 "used for the connection.")
61 #define AGENT_TEXT N_("HTTP user agent")
62 #define AGENT_LONGTEXT N_("Allows you to modify the user agent that will be " \
63 "used for the connection.")
65 #define RECONNECT_TEXT N_("Auto re-connect")
66 #define RECONNECT_LONGTEXT N_("Will automatically attempt a re-connection " \
67 "in case it was untimely closed.")
70 set_description( _("HTTP input") );
71 set_capability( "access2", 0 );
73 add_string( "http-proxy", NULL, NULL, PROXY_TEXT, PROXY_LONGTEXT,
75 add_integer( "http-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
76 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
77 add_string( "http-user", NULL, NULL, USER_TEXT, USER_LONGTEXT, VLC_FALSE );
78 add_string( "http-pwd", NULL , NULL, PASS_TEXT, PASS_LONGTEXT, VLC_FALSE );
79 add_string( "http-user-agent", COPYRIGHT_MESSAGE , NULL, AGENT_TEXT,
80 AGENT_LONGTEXT, VLC_FALSE );
81 add_bool( "http-reconnect", 0, NULL, RECONNECT_TEXT,
82 RECONNECT_LONGTEXT, VLC_TRUE );
84 add_shortcut( "http" );
85 add_shortcut( "http4" );
86 add_shortcut( "http6" );
87 set_callbacks( Open, Close );
90 /*****************************************************************************
92 *****************************************************************************/
101 char *psz_user_agent;
117 vlc_bool_t b_chunked;
120 vlc_bool_t b_seekable;
121 vlc_bool_t b_reconnect;
122 vlc_bool_t b_pace_control;
126 static int Read( access_t *, uint8_t *, int );
127 static int Seek( access_t *, int64_t );
128 static int Control( access_t *, int, va_list );
131 static void ParseURL( access_sys_t *, char *psz_url );
132 static int Connect( access_t *, int64_t );
134 /*****************************************************************************
136 *****************************************************************************/
137 static int Open( vlc_object_t *p_this )
139 access_t *p_access = (access_t*)p_this;
143 /* First set ipv4/ipv6 */
144 var_Create( p_access, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
145 var_Create( p_access, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
147 if( *p_access->psz_access )
150 /* Find out which shortcut was used */
151 if( !strncmp( p_access->psz_access, "http4", 6 ) )
153 val.b_bool = VLC_TRUE;
154 var_Set( p_access, "ipv4", val );
156 val.b_bool = VLC_FALSE;
157 var_Set( p_access, "ipv6", val );
159 else if( !strncmp( p_access->psz_access, "http6", 6 ) )
161 val.b_bool = VLC_TRUE;
162 var_Set( p_access, "ipv6", val );
164 val.b_bool = VLC_FALSE;
165 var_Set( p_access, "ipv4", val );
169 /* Set up p_access */
170 p_access->pf_read = Read;
171 p_access->pf_block = NULL;
172 p_access->pf_control = Control;
173 p_access->pf_seek = Seek;
174 p_access->info.i_update = 0;
175 p_access->info.i_size = 0;
176 p_access->info.i_pos = 0;
177 p_access->info.b_eof = VLC_FALSE;
178 p_access->info.i_title = 0;
179 p_access->info.i_seekpoint = 0;
180 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
181 memset( p_sys, 0, sizeof( access_sys_t ) );
183 p_sys->b_proxy = VLC_FALSE;
184 p_sys->i_version = 1;
185 p_sys->b_seekable = VLC_TRUE;
186 p_sys->psz_mime = NULL;
187 p_sys->psz_pragma = NULL;
188 p_sys->b_mms = VLC_FALSE;
189 p_sys->psz_location = NULL;
190 p_sys->psz_user_agent = NULL;
191 p_sys->b_pace_control = VLC_TRUE;
194 ParseURL( p_sys, p_access->psz_path );
195 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
197 msg_Warn( p_access, "invalid host" );
200 if( p_sys->url.i_port <= 0 )
202 p_sys->url.i_port = 80;
204 if( !p_sys->psz_user || *p_sys->psz_user == '\0' )
206 p_sys->psz_user = var_CreateGetString( p_access, "http-user" );
207 p_sys->psz_passwd = var_CreateGetString( p_access, "http-pwd" );
211 p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
214 psz = var_CreateGetString( p_access, "http-proxy" );
217 p_sys->b_proxy = VLC_TRUE;
218 vlc_UrlParse( &p_sys->proxy, psz, 0 );
222 char *psz_proxy = getenv( "http_proxy" );
223 if( psz_proxy && *psz_proxy )
225 p_sys->b_proxy = VLC_TRUE;
226 vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
235 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
237 msg_Warn( p_access, "invalid proxy host" );
240 if( p_sys->proxy.i_port <= 0 )
242 p_sys->proxy.i_port = 80;
246 msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
247 p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
250 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
251 p_sys->proxy.i_port );
253 if( p_sys->psz_user && *p_sys->psz_user )
255 msg_Dbg( p_access, " user='%s', pwd='%s'",
256 p_sys->psz_user, p_sys->psz_passwd );
259 p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
262 if( Connect( p_access, 0 ) )
264 /* Retry with http 1.0 */
265 p_sys->i_version = 0;
267 if( p_access->b_die ||
268 Connect( p_access, 0 ) )
274 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
275 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
276 p_sys->psz_location && *p_sys->psz_location )
278 playlist_t * p_playlist;
280 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
282 p_playlist = vlc_object_find( p_access, VLC_OBJECT_PLAYLIST,
286 msg_Err( p_access, "redirection failed: can't find playlist" );
289 p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
290 playlist_Add( p_playlist, p_sys->psz_location, p_sys->psz_location,
292 p_playlist->i_index + 1 );
293 vlc_object_release( p_playlist );
295 p_access->info.i_size = 0; /* Force to stop reading */
300 msg_Dbg( p_access, "This is actually a live mms server, BAIL" );
304 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
306 if( p_sys->psz_mime && !strcasecmp( p_sys->psz_mime, "video/nsv" ) )
307 p_access->psz_demux = strdup( "nsv" );
308 else if( p_sys->psz_mime &&
309 ( !strcasecmp( p_sys->psz_mime, "audio/aac" ) ||
310 !strcasecmp( p_sys->psz_mime, "audio/aacp" ) ) )
311 p_access->psz_demux = strdup( "m4a" );
313 p_access->psz_demux = strdup( "mp3" );
315 msg_Info( p_access, "ICY server found, %s demuxer selected",
316 p_access->psz_demux );
318 #if 0 /* Doesn't work really well because of the pre-buffering in shoutcast
319 * servers (the buffer content will be sent as fast as possible). */
320 p_sys->b_pace_control = VLC_FALSE;
324 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
327 var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
332 vlc_UrlClean( &p_sys->url );
333 vlc_UrlClean( &p_sys->proxy );
334 if( p_sys->psz_mime ) free( p_sys->psz_mime );
335 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
336 if( p_sys->psz_location ) free( p_sys->psz_location );
337 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
338 if( p_sys->psz_user ) free( p_sys->psz_user );
339 if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
343 net_Close( p_sys->fd );
349 /*****************************************************************************
351 *****************************************************************************/
352 static void Close( vlc_object_t *p_this )
354 access_t *p_access = (access_t*)p_this;
355 access_sys_t *p_sys = p_access->p_sys;
357 vlc_UrlClean( &p_sys->url );
358 vlc_UrlClean( &p_sys->proxy );
360 if( p_sys->psz_user ) free( p_sys->psz_user );
361 if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
363 if( p_sys->psz_mime ) free( p_sys->psz_mime );
364 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
365 if( p_sys->psz_location ) free( p_sys->psz_location );
367 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
371 net_Close( p_sys->fd );
376 /*****************************************************************************
377 * Read: Read up to i_len bytes from the http connection and place in
378 * p_buffer. Return the actual number of bytes read
379 *****************************************************************************/
380 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
382 access_sys_t *p_sys = p_access->p_sys;
387 p_access->info.b_eof = VLC_TRUE;
391 if( p_access->info.i_size > 0 &&
392 i_len + p_access->info.i_pos > p_access->info.i_size )
394 if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
396 p_access->info.b_eof = VLC_TRUE;
400 if( p_sys->b_chunked )
402 if( p_sys->i_chunk < 0 )
404 p_access->info.b_eof = VLC_TRUE;
408 if( p_sys->i_chunk <= 0 )
410 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd );
411 /* read the chunk header */
414 msg_Dbg( p_access, "failed reading chunk-header line" );
417 p_sys->i_chunk = strtoll( psz, NULL, 16 );
420 if( p_sys->i_chunk <= 0 ) /* eof */
423 p_access->info.b_eof = VLC_TRUE;
428 if( i_len > p_sys->i_chunk )
430 i_len = p_sys->i_chunk;
435 i_read = net_Read( p_access, p_sys->fd, p_buffer, i_len, VLC_FALSE );
438 p_access->info.i_pos += i_read;
440 if( p_sys->b_chunked )
442 p_sys->i_chunk -= i_read;
443 if( p_sys->i_chunk <= 0 )
445 /* read the empty line */
446 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd );
447 if( psz ) free( psz );
451 else if( i_read == 0 )
453 if( p_sys->b_reconnect )
455 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
456 net_Close( p_sys->fd ); p_sys->fd = -1;
457 if( Connect( p_access, p_access->info.i_pos ) )
459 msg_Dbg( p_access, "reconnection failed" );
463 p_sys->b_reconnect = VLC_FALSE;
464 i_read = Read( p_access, p_buffer, i_len );
465 p_sys->b_reconnect = VLC_TRUE;
469 if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
475 /*****************************************************************************
476 * Seek: close and re-open a connection at the right place
477 *****************************************************************************/
478 static int Seek( access_t *p_access, int64_t i_pos )
480 access_sys_t *p_sys = p_access->p_sys;
482 msg_Dbg( p_access, "trying to seek to "I64Fd, i_pos );
484 net_Close( p_sys->fd ); p_sys->fd = -1;
486 if( Connect( p_access, i_pos ) )
488 msg_Err( p_access, "seek failed" );
489 p_access->info.b_eof = VLC_TRUE;
495 /*****************************************************************************
497 *****************************************************************************/
498 static int Control( access_t *p_access, int i_query, va_list args )
500 access_sys_t *p_sys = p_access->p_sys;
508 case ACCESS_CAN_SEEK:
509 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
510 *pb_bool = p_sys->b_seekable;
512 case ACCESS_CAN_FASTSEEK:
513 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
514 *pb_bool = VLC_FALSE;
516 case ACCESS_CAN_PAUSE:
517 case ACCESS_CAN_CONTROL_PACE:
518 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
519 *pb_bool = p_sys->b_pace_control;
524 pi_int = (int*)va_arg( args, int * );
528 case ACCESS_GET_PTS_DELAY:
529 pi_64 = (int64_t*)va_arg( args, int64_t * );
530 *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
534 case ACCESS_SET_PAUSE_STATE:
537 case ACCESS_GET_TITLE_INFO:
538 case ACCESS_SET_TITLE:
539 case ACCESS_SET_SEEKPOINT:
540 case ACCESS_SET_PRIVATE_ID_STATE:
544 msg_Warn( p_access, "unimplemented query in control" );
551 /*****************************************************************************
552 * ParseURL: extract user:password
553 *****************************************************************************/
554 static void ParseURL( access_sys_t *p_sys, char *psz_url )
556 char *psz_dup = strdup( psz_url );
560 /* Syntax //[user:password]@<hostname>[:<port>][/<path>] */
568 if( ( p = strchr( psz, '@' ) ) )
573 comma = strchr( psz, ':' );
575 /* Retreive user:password */
580 p_sys->psz_user = strdup( psz );
581 p_sys->psz_passwd = strdup( comma );
585 p_sys->psz_user = strdup( psz );
594 vlc_UrlParse( &p_sys->url, p, 0 );
599 /*****************************************************************************
601 *****************************************************************************/
602 static int Connect( access_t *p_access, int64_t i_tell )
604 access_sys_t *p_sys = p_access->p_sys;
605 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
609 if( p_sys->psz_location ) free( p_sys->psz_location );
610 if( p_sys->psz_mime ) free( p_sys->psz_mime );
611 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
613 p_sys->psz_location = NULL;
614 p_sys->psz_mime = NULL;
615 p_sys->psz_pragma = NULL;
616 p_sys->b_mms = VLC_FALSE;
617 p_sys->b_chunked = VLC_FALSE;
620 p_access->info.i_size = 0;
621 p_access->info.i_pos = i_tell;
622 p_access->info.b_eof = VLC_FALSE;
625 /* Open connection */
626 p_sys->fd = net_OpenTCP( p_access, srv.psz_host, srv.i_port );
629 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
635 if( p_sys->url.psz_path )
637 net_Printf( VLC_OBJECT(p_access), p_sys->fd,
638 "GET http://%s:%d%s HTTP/1.%d\r\n",
639 p_sys->url.psz_host, p_sys->url.i_port,
640 p_sys->url.psz_path, p_sys->i_version );
644 net_Printf( VLC_OBJECT(p_access), p_sys->fd,
645 "GET http://%s:%d/ HTTP/1.%d\r\n",
646 p_sys->url.psz_host, p_sys->url.i_port,
652 char *psz_path = p_sys->url.psz_path;
653 if( !psz_path || !*psz_path )
657 if( p_sys->url.i_port != 80)
659 net_Printf( VLC_OBJECT(p_access), p_sys->fd,
660 "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
661 psz_path, p_sys->i_version, p_sys->url.psz_host,
666 net_Printf( VLC_OBJECT(p_access), p_sys->fd,
667 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
668 psz_path, p_sys->i_version, p_sys->url.psz_host );
672 net_Printf( VLC_OBJECT(p_access), p_sys->fd, "User-Agent: %s\r\n",
673 p_sys->psz_user_agent );
675 if( p_sys->i_version == 1 )
677 net_Printf( VLC_OBJECT(p_access), p_sys->fd,
678 "Range: bytes="I64Fd"-\r\n", i_tell );
680 /* Authentification */
681 if( p_sys->psz_user && *p_sys->psz_user )
686 asprintf( &buf, "%s:%s", p_sys->psz_user,
687 p_sys->psz_passwd ? p_sys->psz_passwd : "" );
689 b64 = vlc_b64_encode( buf );
691 net_Printf( VLC_OBJECT(p_access), p_sys->fd,
692 "Authorization: Basic %s\r\n", b64 );
695 net_Printf( VLC_OBJECT(p_access), p_sys->fd, "Connection: Close\r\n" );
697 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, "\r\n" ) < 0 )
699 msg_Err( p_access, "failed to send request" );
700 net_Close( p_sys->fd ); p_sys->fd = -1;
705 if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd ) ) == NULL )
707 msg_Err( p_access, "failed to read answer" );
710 if( !strncmp( psz, "HTTP/1.", 7 ) )
712 p_sys->psz_protocol = "HTTP";
713 p_sys->i_code = atoi( &psz[9] );
715 else if( !strncmp( psz, "ICY", 3 ) )
717 p_sys->psz_protocol = "ICY";
718 p_sys->i_code = atoi( &psz[4] );
719 p_sys->b_reconnect = VLC_TRUE;
723 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
727 msg_Dbg( p_access, "protocol '%s' answer code %d",
728 p_sys->psz_protocol, p_sys->i_code );
729 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
731 p_sys->b_seekable = VLC_FALSE;
733 if( p_sys->i_code != 206 )
735 p_sys->b_seekable = VLC_FALSE;
737 if( p_sys->i_code >= 400 )
739 msg_Err( p_access, "error: %s", psz );
747 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd );
752 msg_Err( p_access, "failed to read answer" );
756 /* msg_Dbg( p_input, "Line=%s", psz ); */
764 if( ( p = strchr( psz, ':' ) ) == NULL )
766 msg_Err( p_access, "malformed header line: %s", psz );
771 while( *p == ' ' ) p++;
773 if( !strcasecmp( psz, "Content-Length" ) )
775 p_access->info.i_size = i_tell + atoll( p );
776 msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
778 else if( !strcasecmp( psz, "Location" ) )
780 if( p_sys->psz_location ) free( p_sys->psz_location );
781 p_sys->psz_location = strdup( p );
783 else if( !strcasecmp( psz, "Content-Type" ) )
785 if( p_sys->psz_mime ) free( p_sys->psz_mime );
786 p_sys->psz_mime = strdup( p );
787 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
789 else if( !strcasecmp( psz, "Pragma" ) )
791 if( !strcasecmp( psz, "Pragma: features" ) )
792 p_sys->b_mms = VLC_TRUE;
793 if( p_sys->psz_pragma ) free( p_sys->psz_pragma );
794 p_sys->psz_pragma = strdup( p );
795 msg_Dbg( p_access, "Pragma: %s", p_sys->psz_pragma );
797 else if( !strcasecmp( psz, "Server" ) &&
798 !strncasecmp( p, "Icecast", 7 ) )
800 p_sys->b_reconnect = VLC_TRUE;
801 p_sys->b_pace_control = VLC_FALSE;
802 msg_Dbg( p_access, "Server: %s", p );
804 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
806 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
807 if( !strncasecmp( p, "chunked", 7 ) )
809 p_sys->b_chunked = VLC_TRUE;
818 net_Close( p_sys->fd ); p_sys->fd = -1;