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;
115 vlc_bool_t b_chunked;
118 vlc_bool_t b_seekable;
119 vlc_bool_t b_reconnect;
123 static int Read( access_t *, uint8_t *, int );
124 static int Seek( access_t *, int64_t );
125 static int Control( access_t *, int, va_list );
128 static void ParseURL( access_sys_t *, char *psz_url );
129 static int Connect( access_t *, int64_t );
130 static char *b64_encode( unsigned char *src );
132 /*****************************************************************************
134 *****************************************************************************/
135 static int Open ( vlc_object_t *p_this )
137 access_t *p_access = (access_t*)p_this;
141 /* First set ipv4/ipv6 */
142 var_Create( p_access, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
143 var_Create( p_access, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
145 if( *p_access->psz_access )
148 /* Find out which shortcut was used */
149 if( !strncmp( p_access->psz_access, "http4", 6 ) )
151 val.b_bool = VLC_TRUE;
152 var_Set( p_access, "ipv4", val );
154 val.b_bool = VLC_FALSE;
155 var_Set( p_access, "ipv6", val );
157 else if( !strncmp( p_access->psz_access, "http6", 6 ) )
159 val.b_bool = VLC_TRUE;
160 var_Set( p_access, "ipv6", val );
162 val.b_bool = VLC_FALSE;
163 var_Set( p_access, "ipv4", val );
167 /* Set up p_access */
168 p_access->pf_read = Read;
169 p_access->pf_block = NULL;
170 p_access->pf_control = Control;
171 p_access->pf_seek = Seek;
172 p_access->info.i_update = 0;
173 p_access->info.i_size = 0;
174 p_access->info.i_pos = 0;
175 p_access->info.b_eof = VLC_FALSE;
176 p_access->info.i_title = 0;
177 p_access->info.i_seekpoint = 0;
178 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
179 memset( p_sys, 0, sizeof( access_sys_t ) );
181 p_sys->b_proxy = VLC_FALSE;
182 p_sys->i_version = 1;
183 p_sys->b_seekable = VLC_TRUE;
184 p_sys->psz_mime = NULL;
185 p_sys->psz_location = NULL;
186 p_sys->psz_user_agent = NULL;
190 ParseURL( p_sys, p_access->psz_path );
191 if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
193 msg_Warn( p_access, "invalid host" );
196 if( p_sys->url.i_port <= 0 )
198 p_sys->url.i_port = 80;
200 if( !p_sys->psz_user || *p_sys->psz_user == '\0' )
202 p_sys->psz_user = var_CreateGetString( p_access, "http-user" );
203 p_sys->psz_passwd = var_CreateGetString( p_access, "http-pwd" );
207 p_sys->psz_user_agent = var_CreateGetString( p_access, "http-user-agent" );
210 psz = var_CreateGetString( p_access, "http-proxy" );
213 p_sys->b_proxy = VLC_TRUE;
214 vlc_UrlParse( &p_sys->proxy, psz, 0 );
218 char *psz_proxy = getenv( "http_proxy" );
219 if( psz_proxy && *psz_proxy )
221 p_sys->b_proxy = VLC_TRUE;
222 vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
231 if( p_sys->proxy.psz_host == NULL || *p_sys->proxy.psz_host == '\0' )
233 msg_Warn( p_access, "invalid proxy host" );
236 if( p_sys->proxy.i_port <= 0 )
238 p_sys->proxy.i_port = 80;
242 msg_Dbg( p_access, "http: server='%s' port=%d file='%s",
243 p_sys->url.psz_host, p_sys->url.i_port, p_sys->url.psz_path );
246 msg_Dbg( p_access, " proxy %s:%d", p_sys->proxy.psz_host,
247 p_sys->proxy.i_port );
249 if( p_sys->psz_user && *p_sys->psz_user )
251 msg_Dbg( p_access, " user='%s', pwd='%s'",
252 p_sys->psz_user, p_sys->psz_passwd );
255 p_sys->b_reconnect = var_CreateGetBool( p_access, "http-reconnect" );
258 if( Connect( p_access, 0 ) )
260 /* Retry with http 1.0 */
261 p_sys->i_version = 0;
263 if( p_access->b_die ||
264 Connect( p_access, 0 ) )
270 if( ( p_sys->i_code == 301 || p_sys->i_code == 302 ||
271 p_sys->i_code == 303 || p_sys->i_code == 307 ) &&
272 p_sys->psz_location && *p_sys->psz_location )
274 playlist_t * p_playlist;
276 msg_Dbg( p_access, "redirection to %s", p_sys->psz_location );
278 p_playlist = vlc_object_find( p_access, VLC_OBJECT_PLAYLIST,
282 msg_Err( p_access, "redirection failed: can't find playlist" );
285 p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
286 playlist_Add( p_playlist, p_sys->psz_location, p_sys->psz_location,
287 PLAYLIST_INSERT | PLAYLIST_GO,
288 p_playlist->i_index + 1 );
289 vlc_object_release( p_playlist );
291 p_access->info.i_size = 0; /* Force to stop reading */
294 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
296 if( p_sys->psz_mime && !strcasecmp( p_sys->psz_mime, "video/nsv" ) )
297 p_access->psz_demux = strdup( "nsv" );
299 p_access->psz_demux = strdup( "mp3" );
301 msg_Info( p_access, "ICY server found, %s demuxer selected",
302 p_access->psz_demux );
305 if( p_sys->b_reconnect ) msg_Dbg( p_access, "auto re-connect enabled" );
308 var_Create( p_access, "http-caching", VLC_VAR_INTEGER |VLC_VAR_DOINHERIT );
313 vlc_UrlClean( &p_sys->url );
314 vlc_UrlClean( &p_sys->proxy );
315 if( p_sys->psz_mime ) free( p_sys->psz_mime );
316 if( p_sys->psz_location ) free( p_sys->psz_location );
317 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
318 if( p_sys->psz_user ) free( p_sys->psz_user );
319 if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
323 net_Close( p_sys->fd );
329 /*****************************************************************************
331 *****************************************************************************/
332 static void Close( vlc_object_t *p_this )
334 access_t *p_access = (access_t*)p_this;
335 access_sys_t *p_sys = p_access->p_sys;
337 vlc_UrlClean( &p_sys->url );
338 vlc_UrlClean( &p_sys->proxy );
340 if( p_sys->psz_user ) free( p_sys->psz_user );
341 if( p_sys->psz_passwd ) free( p_sys->psz_passwd );
343 if( p_sys->psz_mime ) free( p_sys->psz_mime );
344 if( p_sys->psz_location ) free( p_sys->psz_location );
346 if( p_sys->psz_user_agent ) free( p_sys->psz_user_agent );
350 net_Close( p_sys->fd );
355 /*****************************************************************************
356 * Read: Read up to i_len bytes from the http connection and place in
357 * p_buffer. Return the actual number of bytes read
358 *****************************************************************************/
359 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
361 access_sys_t *p_sys = p_access->p_sys;
366 p_access->info.b_eof = VLC_TRUE;
370 if( p_access->info.i_size > 0 &&
371 i_len + p_access->info.i_pos > p_access->info.i_size )
373 if( ( i_len = p_access->info.i_size - p_access->info.i_pos ) == 0 )
375 p_access->info.b_eof = VLC_TRUE;
379 if( p_sys->b_chunked )
381 if( p_sys->i_chunk < 0 )
383 p_access->info.b_eof = VLC_TRUE;
387 if( p_sys->i_chunk <= 0 )
389 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd );
390 /* read the chunk header */
393 msg_Dbg( p_access, "failed reading chunk-header line" );
396 p_sys->i_chunk = strtoll( psz, NULL, 16 );
399 if( p_sys->i_chunk <= 0 ) /* eof */
402 p_access->info.b_eof = VLC_TRUE;
407 if( i_len > p_sys->i_chunk )
409 i_len = p_sys->i_chunk;
414 i_read = net_Read( p_access, p_sys->fd, p_buffer, i_len, VLC_FALSE );
417 p_access->info.i_pos += i_read;
419 if( p_sys->b_chunked )
421 p_sys->i_chunk -= i_read;
422 if( p_sys->i_chunk <= 0 )
424 /* read the empty line */
425 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd );
426 if( psz ) free( psz );
430 else if( i_read == 0 )
432 if( p_sys->b_reconnect )
434 msg_Dbg( p_access, "got disconnected, trying to reconnect" );
435 net_Close( p_sys->fd ); p_sys->fd = -1;
436 if( Connect( p_access, p_access->info.i_pos ) )
438 msg_Dbg( p_access, "reconnection failed" );
442 p_sys->b_reconnect = VLC_FALSE;
443 i_read = Read( p_access, p_buffer, i_len );
444 p_sys->b_reconnect = VLC_TRUE;
448 if( i_read == 0 ) p_access->info.b_eof = VLC_TRUE;
454 /*****************************************************************************
455 * Seek: close and re-open a connection at the right place
456 *****************************************************************************/
457 static int Seek( access_t *p_access, int64_t i_pos )
459 access_sys_t *p_sys = p_access->p_sys;
461 msg_Dbg( p_access, "trying to seek to "I64Fd, i_pos );
463 net_Close( p_sys->fd ); p_sys->fd = -1;
465 if( Connect( p_access, i_pos ) )
467 msg_Err( p_access, "seek failed" );
468 p_access->info.b_eof = VLC_TRUE;
474 /*****************************************************************************
476 *****************************************************************************/
477 static int Control( access_t *p_access, int i_query, va_list args )
479 access_sys_t *p_sys = p_access->p_sys;
487 case ACCESS_CAN_SEEK:
488 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
489 *pb_bool = p_sys->b_seekable;
491 case ACCESS_CAN_FASTSEEK:
492 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
493 *pb_bool = VLC_FALSE;
495 case ACCESS_CAN_PAUSE:
496 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
497 *pb_bool = VLC_TRUE; /* FIXME */
499 case ACCESS_CAN_CONTROL_PACE:
500 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
501 *pb_bool = VLC_TRUE; /* FIXME */
506 pi_int = (int*)va_arg( args, int * );
510 case ACCESS_GET_PTS_DELAY:
511 pi_64 = (int64_t*)va_arg( args, int64_t * );
512 *pi_64 = (int64_t)var_GetInteger( p_access, "http-caching" ) * 1000;
516 case ACCESS_SET_PAUSE_STATE:
519 case ACCESS_GET_TITLE_INFO:
520 case ACCESS_SET_TITLE:
521 case ACCESS_SET_SEEKPOINT:
522 case ACCESS_SET_PRIVATE_ID_STATE:
526 msg_Warn( p_access, "unimplemented query in control" );
533 /*****************************************************************************
534 * ParseURL: extract user:password
535 *****************************************************************************/
536 static void ParseURL( access_sys_t *p_sys, char *psz_url )
538 char *psz_dup = strdup( psz_url );
542 /* Syntax //[user:password]@<hostname>[:<port>][/<path>] */
550 if( ( p = strchr( psz, '@' ) ) )
555 comma = strchr( psz, ':' );
557 /* Retreive user:password */
562 p_sys->psz_user = strdup( psz );
563 p_sys->psz_passwd = strdup( comma );
567 p_sys->psz_user = strdup( psz );
576 vlc_UrlParse( &p_sys->url, p, 0 );
581 /*****************************************************************************
583 *****************************************************************************/
584 static int Connect( access_t *p_access, int64_t i_tell )
586 access_sys_t *p_sys = p_access->p_sys;
587 vlc_url_t srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;
591 if( p_sys->psz_location ) free( p_sys->psz_location );
592 if( p_sys->psz_mime ) free( p_sys->psz_mime );
594 p_sys->psz_location = NULL;
595 p_sys->psz_mime = NULL;
596 p_sys->b_chunked = VLC_FALSE;
599 p_access->info.i_size = 0;
600 p_access->info.i_pos = i_tell;
601 p_access->info.b_eof = VLC_FALSE;
604 /* Open connection */
605 p_sys->fd = net_OpenTCP( p_access, srv.psz_host, srv.i_port );
608 msg_Err( p_access, "cannot connect to %s:%d", srv.psz_host, srv.i_port );
614 if( p_sys->url.psz_path )
616 net_Printf( VLC_OBJECT(p_access), p_sys->fd,
617 "GET http://%s:%d/%s HTTP/1.%d\r\n",
618 p_sys->url.psz_host, p_sys->url.i_port,
619 p_sys->url.psz_path, p_sys->i_version );
623 net_Printf( VLC_OBJECT(p_access), p_sys->fd,
624 "GET http://%s:%d/ HTTP/1.%d\r\n",
625 p_sys->url.psz_host, p_sys->url.i_port,
632 char *psz_path = p_sys->url.psz_path;
633 if( !psz_path || !*psz_path )
637 if( p_sys->url.i_port != 80)
639 net_Printf( VLC_OBJECT(p_access), p_sys->fd,
640 "GET %s HTTP/1.%d\r\nHost: %s:%d\r\n",
641 psz_path, p_sys->i_version, p_sys->url.psz_host,
646 net_Printf( VLC_OBJECT(p_access), p_sys->fd,
647 "GET %s HTTP/1.%d\r\nHost: %s\r\n",
648 psz_path, p_sys->i_version, p_sys->url.psz_host );
652 net_Printf( VLC_OBJECT(p_access), p_sys->fd, "User-Agent: %s\r\n",
653 p_sys->psz_user_agent );
655 if( p_sys->i_version == 1 )
657 net_Printf( VLC_OBJECT(p_access), p_sys->fd,
658 "Range: bytes="I64Fd"-\r\n", i_tell );
660 /* Authentification */
661 if( p_sys->psz_user && *p_sys->psz_user )
666 asprintf( &buf, "%s:%s", p_sys->psz_user,
667 p_sys->psz_passwd ? p_sys->psz_passwd : "" );
669 b64 = b64_encode( buf );
671 net_Printf( VLC_OBJECT(p_access), p_sys->fd,
672 "Authorization: Basic %s\r\n", b64 );
675 net_Printf( VLC_OBJECT(p_access), p_sys->fd, "Connection: Close\r\n" );
677 if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, "\r\n" ) < 0 )
679 msg_Err( p_access, "failed to send request" );
680 net_Close( p_sys->fd ); p_sys->fd = -1;
685 if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd ) ) == NULL )
687 msg_Err( p_access, "failed to read answer" );
690 if( !strncmp( psz, "HTTP/1.", 7 ) )
692 p_sys->psz_protocol = "HTTP";
693 p_sys->i_code = atoi( &psz[9] );
695 else if( !strncmp( psz, "ICY", 3 ) )
697 p_sys->psz_protocol = "ICY";
698 p_sys->i_code = atoi( &psz[4] );
699 p_sys->b_reconnect = VLC_TRUE;
703 msg_Err( p_access, "invalid HTTP reply '%s'", psz );
707 msg_Dbg( p_access, "protocol '%s' answer code %d",
708 p_sys->psz_protocol, p_sys->i_code );
709 if( !strcmp( p_sys->psz_protocol, "ICY" ) )
711 p_sys->b_seekable = VLC_FALSE;
713 if( p_sys->i_code != 206 )
715 p_sys->b_seekable = VLC_FALSE;
717 if( p_sys->i_code >= 400 )
719 msg_Err( p_access, "error: %s", psz );
727 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd );
732 msg_Err( p_access, "failed to read answer" );
736 /* msg_Dbg( p_input, "Line=%s", psz ); */
744 if( ( p = strchr( psz, ':' ) ) == NULL )
746 msg_Err( p_access, "malformed header line: %s", psz );
751 while( *p == ' ' ) p++;
753 if( !strcasecmp( psz, "Content-Length" ) )
755 p_access->info.i_size = i_tell + atoll( p );
756 msg_Dbg( p_access, "stream size="I64Fd, p_access->info.i_size );
758 else if( !strcasecmp( psz, "Location" ) )
760 if( p_sys->psz_location ) free( p_sys->psz_location );
761 p_sys->psz_location = strdup( p );
763 else if( !strcasecmp( psz, "Content-Type" ) )
765 if( p_sys->psz_mime ) free( p_sys->psz_mime );
766 p_sys->psz_mime = strdup( p );
767 msg_Dbg( p_access, "Content-Type: %s", p_sys->psz_mime );
769 else if( !strcasecmp( psz, "Server" ) &&
770 !strncasecmp( p, "Icecast", 7 ) )
772 p_sys->b_reconnect = VLC_TRUE;
773 msg_Dbg( p_access, "Server: %s", p );
775 else if( !strcasecmp( psz, "Transfer-Encoding" ) )
777 msg_Dbg( p_access, "Transfer-Encoding: %s", p );
778 if( !strncasecmp( p, "chunked", 7 ) )
780 p_sys->b_chunked = VLC_TRUE;
789 net_Close( p_sys->fd ); p_sys->fd = -1;
793 /*****************************************************************************
795 *****************************************************************************/
796 static char *b64_encode( unsigned char *src )
798 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
800 char *dst = malloc( strlen( src ) * 4 / 3 + 12 );
803 unsigned i_shift = 0;
809 i_bits = ( i_bits << 8 )|( *src++ );
812 else if( i_shift > 0 )
814 i_bits <<= 6 - i_shift;
823 while( i_shift >= 6 )
826 *dst++ = b64[(i_bits >> i_shift)&0x3f];